]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/dt9812.c
Linux 3.12-rc6
[karo-tx-linux.git] / drivers / staging / comedi / drivers / dt9812.c
1 /*
2  * comedi/drivers/dt9812.c
3  *   COMEDI driver for DataTranslation DT9812 USB module
4  *
5  * Copyright (C) 2005 Anders Blomdell <anders.blomdell@control.lth.se>
6  *
7  * COMEDI - Linux Control and Measurement Device Interface
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13
14  *  This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  */
19
20 /*
21 Driver: dt9812
22 Description: Data Translation DT9812 USB module
23 Author: anders.blomdell@control.lth.se (Anders Blomdell)
24 Status: in development
25 Devices: [Data Translation] DT9812 (dt9812)
26 Updated: Sun Nov 20 20:18:34 EST 2005
27
28 This driver works, but bulk transfers not implemented. Might be a starting point
29 for someone else. I found out too late that USB has too high latencies (>1 ms)
30 for my needs.
31 */
32
33 /*
34  * Nota Bene:
35  *   1. All writes to command pipe has to be 32 bytes (ISP1181B SHRTP=0 ?)
36  *   2. The DDK source (as of sep 2005) is in error regarding the
37  *      input MUX bits (example code says P4, but firmware schematics
38  *      says P1).
39  */
40
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/errno.h>
44 #include <linux/init.h>
45 #include <linux/uaccess.h>
46 #include <linux/usb.h>
47
48 #include "../comedidev.h"
49
50 #define DT9812_DIAGS_BOARD_INFO_ADDR    0xFBFF
51 #define DT9812_MAX_WRITE_CMD_PIPE_SIZE  32
52 #define DT9812_MAX_READ_CMD_PIPE_SIZE   32
53
54 /* usb_bulk_msg() timout in milliseconds */
55 #define DT9812_USB_TIMEOUT              1000
56
57 /*
58  * See Silican Laboratories C8051F020/1/2/3 manual
59  */
60 #define F020_SFR_P4                     0x84
61 #define F020_SFR_P1                     0x90
62 #define F020_SFR_P2                     0xa0
63 #define F020_SFR_P3                     0xb0
64 #define F020_SFR_AMX0CF                 0xba
65 #define F020_SFR_AMX0SL                 0xbb
66 #define F020_SFR_ADC0CF                 0xbc
67 #define F020_SFR_ADC0L                  0xbe
68 #define F020_SFR_ADC0H                  0xbf
69 #define F020_SFR_DAC0L                  0xd2
70 #define F020_SFR_DAC0H                  0xd3
71 #define F020_SFR_DAC0CN                 0xd4
72 #define F020_SFR_DAC1L                  0xd5
73 #define F020_SFR_DAC1H                  0xd6
74 #define F020_SFR_DAC1CN                 0xd7
75 #define F020_SFR_ADC0CN                 0xe8
76
77 #define F020_MASK_ADC0CF_AMP0GN0        0x01
78 #define F020_MASK_ADC0CF_AMP0GN1        0x02
79 #define F020_MASK_ADC0CF_AMP0GN2        0x04
80
81 #define F020_MASK_ADC0CN_AD0EN          0x80
82 #define F020_MASK_ADC0CN_AD0INT         0x20
83 #define F020_MASK_ADC0CN_AD0BUSY        0x10
84
85 #define F020_MASK_DACxCN_DACxEN         0x80
86
87 enum {
88         /* A/D  D/A  DI  DO  CT */
89         DT9812_DEVID_DT9812_10, /*  8    2   8   8   1  +/- 10V */
90         DT9812_DEVID_DT9812_2PT5,       /* 8    2   8   8   1  0-2.44V */
91 #if 0
92         DT9812_DEVID_DT9813,    /*  16   2   4   4   1  +/- 10V */
93         DT9812_DEVID_DT9814     /*  24   2   0   0   1  +/- 10V */
94 #endif
95 };
96
97 enum dt9812_gain {
98         DT9812_GAIN_0PT25 = 1,
99         DT9812_GAIN_0PT5 = 2,
100         DT9812_GAIN_1 = 4,
101         DT9812_GAIN_2 = 8,
102         DT9812_GAIN_4 = 16,
103         DT9812_GAIN_8 = 32,
104         DT9812_GAIN_16 = 64,
105 };
106
107 enum {
108         DT9812_LEAST_USB_FIRMWARE_CMD_CODE = 0,
109         /* Write Flash memory */
110         DT9812_W_FLASH_DATA = 0,
111         /* Read Flash memory misc config info */
112         DT9812_R_FLASH_DATA = 1,
113
114         /*
115          * Register read/write commands for processor
116          */
117
118         /* Read a single byte of USB memory */
119         DT9812_R_SINGLE_BYTE_REG = 2,
120         /* Write a single byte of USB memory */
121         DT9812_W_SINGLE_BYTE_REG = 3,
122         /* Multiple Reads of USB memory */
123         DT9812_R_MULTI_BYTE_REG = 4,
124         /* Multiple Writes of USB memory */
125         DT9812_W_MULTI_BYTE_REG = 5,
126         /* Read, (AND) with mask, OR value, then write (single) */
127         DT9812_RMW_SINGLE_BYTE_REG = 6,
128         /* Read, (AND) with mask, OR value, then write (multiple) */
129         DT9812_RMW_MULTI_BYTE_REG = 7,
130
131         /*
132          * Register read/write commands for SMBus
133          */
134
135         /* Read a single byte of SMBus */
136         DT9812_R_SINGLE_BYTE_SMBUS = 8,
137         /* Write a single byte of SMBus */
138         DT9812_W_SINGLE_BYTE_SMBUS = 9,
139         /* Multiple Reads of SMBus */
140         DT9812_R_MULTI_BYTE_SMBUS = 10,
141         /* Multiple Writes of SMBus */
142         DT9812_W_MULTI_BYTE_SMBUS = 11,
143
144         /*
145          * Register read/write commands for a device
146          */
147
148         /* Read a single byte of a device */
149         DT9812_R_SINGLE_BYTE_DEV = 12,
150         /* Write a single byte of a device */
151         DT9812_W_SINGLE_BYTE_DEV = 13,
152         /* Multiple Reads of a device */
153         DT9812_R_MULTI_BYTE_DEV = 14,
154         /* Multiple Writes of a device */
155         DT9812_W_MULTI_BYTE_DEV = 15,
156
157         /* Not sure if we'll need this */
158         DT9812_W_DAC_THRESHOLD = 16,
159
160         /* Set interrupt on change mask */
161         DT9812_W_INT_ON_CHANGE_MASK = 17,
162
163         /* Write (or Clear) the CGL for the ADC */
164         DT9812_W_CGL = 18,
165         /* Multiple Reads of USB memory */
166         DT9812_R_MULTI_BYTE_USBMEM = 19,
167         /* Multiple Writes to USB memory */
168         DT9812_W_MULTI_BYTE_USBMEM = 20,
169
170         /* Issue a start command to a given subsystem */
171         DT9812_START_SUBSYSTEM = 21,
172         /* Issue a stop command to a given subsystem */
173         DT9812_STOP_SUBSYSTEM = 22,
174
175         /* calibrate the board using CAL_POT_CMD */
176         DT9812_CALIBRATE_POT = 23,
177         /* set the DAC FIFO size */
178         DT9812_W_DAC_FIFO_SIZE = 24,
179         /* Write or Clear the CGL for the DAC */
180         DT9812_W_CGL_DAC = 25,
181         /* Read a single value from a subsystem */
182         DT9812_R_SINGLE_VALUE_CMD = 26,
183         /* Write a single value to a subsystem */
184         DT9812_W_SINGLE_VALUE_CMD = 27,
185         /* Valid DT9812_USB_FIRMWARE_CMD_CODE's will be less than this number */
186         DT9812_MAX_USB_FIRMWARE_CMD_CODE,
187 };
188
189 struct dt9812_flash_data {
190         __le16 numbytes;
191         __le16 address;
192 };
193
194 #define DT9812_MAX_NUM_MULTI_BYTE_RDS  \
195         ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
196
197 struct dt9812_read_multi {
198         u8 count;
199         u8 address[DT9812_MAX_NUM_MULTI_BYTE_RDS];
200 };
201
202 struct dt9812_write_byte {
203         u8 address;
204         u8 value;
205 };
206
207 #define DT9812_MAX_NUM_MULTI_BYTE_WRTS  \
208         ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
209          sizeof(struct dt9812_write_byte))
210
211 struct dt9812_write_multi {
212         u8 count;
213         struct dt9812_write_byte write[DT9812_MAX_NUM_MULTI_BYTE_WRTS];
214 };
215
216 struct dt9812_rmw_byte {
217         u8 address;
218         u8 and_mask;
219         u8 or_value;
220 };
221
222 #define DT9812_MAX_NUM_MULTI_BYTE_RMWS  \
223         ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
224          sizeof(struct dt9812_rmw_byte))
225
226 struct dt9812_rmw_multi {
227         u8 count;
228         struct dt9812_rmw_byte rmw[DT9812_MAX_NUM_MULTI_BYTE_RMWS];
229 };
230
231 struct dt9812_usb_cmd {
232         __le32 cmd;
233         union {
234                 struct dt9812_flash_data flash_data_info;
235                 struct dt9812_read_multi read_multi_info;
236                 struct dt9812_write_multi write_multi_info;
237                 struct dt9812_rmw_multi rmw_multi_info;
238         } u;
239 };
240
241 struct dt9812_private {
242         struct semaphore sem;
243         struct {
244                 __u8 addr;
245                 size_t size;
246         } cmd_wr, cmd_rd;
247         u16 device;
248         u16 ao_shadow[2];
249 };
250
251 static int dt9812_read_info(struct comedi_device *dev,
252                             int offset, void *buf, size_t buf_size)
253 {
254         struct usb_device *usb = comedi_to_usb_dev(dev);
255         struct dt9812_private *devpriv = dev->private;
256         struct dt9812_usb_cmd cmd;
257         int count, ret;
258
259         cmd.cmd = cpu_to_le32(DT9812_R_FLASH_DATA);
260         cmd.u.flash_data_info.address =
261             cpu_to_le16(DT9812_DIAGS_BOARD_INFO_ADDR + offset);
262         cmd.u.flash_data_info.numbytes = cpu_to_le16(buf_size);
263
264         /* DT9812 only responds to 32 byte writes!! */
265         ret = usb_bulk_msg(usb, usb_sndbulkpipe(usb, devpriv->cmd_wr.addr),
266                            &cmd, 32, &count, DT9812_USB_TIMEOUT);
267         if (ret)
268                 return ret;
269
270         return usb_bulk_msg(usb, usb_rcvbulkpipe(usb, devpriv->cmd_rd.addr),
271                             buf, buf_size, &count, DT9812_USB_TIMEOUT);
272 }
273
274 static int dt9812_read_multiple_registers(struct comedi_device *dev,
275                                           int reg_count, u8 *address,
276                                           u8 *value)
277 {
278         struct usb_device *usb = comedi_to_usb_dev(dev);
279         struct dt9812_private *devpriv = dev->private;
280         struct dt9812_usb_cmd cmd;
281         int i, count, ret;
282
283         cmd.cmd = cpu_to_le32(DT9812_R_MULTI_BYTE_REG);
284         cmd.u.read_multi_info.count = reg_count;
285         for (i = 0; i < reg_count; i++)
286                 cmd.u.read_multi_info.address[i] = address[i];
287
288         /* DT9812 only responds to 32 byte writes!! */
289         ret = usb_bulk_msg(usb, usb_sndbulkpipe(usb, devpriv->cmd_wr.addr),
290                            &cmd, 32, &count, DT9812_USB_TIMEOUT);
291         if (ret)
292                 return ret;
293
294         return usb_bulk_msg(usb, usb_rcvbulkpipe(usb, devpriv->cmd_rd.addr),
295                             value, reg_count, &count, DT9812_USB_TIMEOUT);
296 }
297
298 static int dt9812_write_multiple_registers(struct comedi_device *dev,
299                                            int reg_count, u8 *address,
300                                            u8 *value)
301 {
302         struct usb_device *usb = comedi_to_usb_dev(dev);
303         struct dt9812_private *devpriv = dev->private;
304         struct dt9812_usb_cmd cmd;
305         int i, count;
306
307         cmd.cmd = cpu_to_le32(DT9812_W_MULTI_BYTE_REG);
308         cmd.u.read_multi_info.count = reg_count;
309         for (i = 0; i < reg_count; i++) {
310                 cmd.u.write_multi_info.write[i].address = address[i];
311                 cmd.u.write_multi_info.write[i].value = value[i];
312         }
313
314         /* DT9812 only responds to 32 byte writes!! */
315         return usb_bulk_msg(usb, usb_sndbulkpipe(usb, devpriv->cmd_wr.addr),
316                             &cmd, 32, &count, DT9812_USB_TIMEOUT);
317 }
318
319 static int dt9812_rmw_multiple_registers(struct comedi_device *dev,
320                                          int reg_count,
321                                          struct dt9812_rmw_byte *rmw)
322 {
323         struct usb_device *usb = comedi_to_usb_dev(dev);
324         struct dt9812_private *devpriv = dev->private;
325         struct dt9812_usb_cmd cmd;
326         int i, count;
327
328         cmd.cmd = cpu_to_le32(DT9812_RMW_MULTI_BYTE_REG);
329         cmd.u.rmw_multi_info.count = reg_count;
330         for (i = 0; i < reg_count; i++)
331                 cmd.u.rmw_multi_info.rmw[i] = rmw[i];
332
333         /* DT9812 only responds to 32 byte writes!! */
334         return usb_bulk_msg(usb, usb_sndbulkpipe(usb, devpriv->cmd_wr.addr),
335                             &cmd, 32, &count, DT9812_USB_TIMEOUT);
336 }
337
338 static int dt9812_digital_in(struct comedi_device *dev, u8 *bits)
339 {
340         struct dt9812_private *devpriv = dev->private;
341         u8 reg[2] = { F020_SFR_P3, F020_SFR_P1 };
342         u8 value[2];
343         int ret;
344
345         down(&devpriv->sem);
346         ret = dt9812_read_multiple_registers(dev, 2, reg, value);
347         if (ret == 0) {
348                 /*
349                  * bits 0-6 in F020_SFR_P3 are bits 0-6 in the digital
350                  * input port bit 3 in F020_SFR_P1 is bit 7 in the
351                  * digital input port
352                  */
353                 *bits = (value[0] & 0x7f) | ((value[1] & 0x08) << 4);
354         }
355         up(&devpriv->sem);
356
357         return ret;
358 }
359
360 static int dt9812_digital_out(struct comedi_device *dev, u8 bits)
361 {
362         struct dt9812_private *devpriv = dev->private;
363         u8 reg[1] = { F020_SFR_P2 };
364         u8 value[1] = { bits };
365         int ret;
366
367         down(&devpriv->sem);
368         ret = dt9812_write_multiple_registers(dev, 1, reg, value);
369         up(&devpriv->sem);
370
371         return ret;
372 }
373
374 static void dt9812_configure_mux(struct comedi_device *dev,
375                                  struct dt9812_rmw_byte *rmw, int channel)
376 {
377         struct dt9812_private *devpriv = dev->private;
378
379         if (devpriv->device == DT9812_DEVID_DT9812_10) {
380                 /* In the DT9812/10V MUX is selected by P1.5-7 */
381                 rmw->address = F020_SFR_P1;
382                 rmw->and_mask = 0xe0;
383                 rmw->or_value = channel << 5;
384         } else {
385                 /* In the DT9812/2.5V, internal mux is selected by bits 0:2 */
386                 rmw->address = F020_SFR_AMX0SL;
387                 rmw->and_mask = 0xff;
388                 rmw->or_value = channel & 0x07;
389         }
390 }
391
392 static void dt9812_configure_gain(struct comedi_device *dev,
393                                   struct dt9812_rmw_byte *rmw,
394                                   enum dt9812_gain gain)
395 {
396         struct dt9812_private *devpriv = dev->private;
397
398         /* In the DT9812/10V, there is an external gain of 0.5 */
399         if (devpriv->device == DT9812_DEVID_DT9812_10)
400                 gain <<= 1;
401
402         rmw->address = F020_SFR_ADC0CF;
403         rmw->and_mask = F020_MASK_ADC0CF_AMP0GN2 |
404                         F020_MASK_ADC0CF_AMP0GN1 |
405                         F020_MASK_ADC0CF_AMP0GN0;
406
407         switch (gain) {
408                 /*
409                  * 000 -> Gain =  1
410                  * 001 -> Gain =  2
411                  * 010 -> Gain =  4
412                  * 011 -> Gain =  8
413                  * 10x -> Gain = 16
414                  * 11x -> Gain =  0.5
415                  */
416         case DT9812_GAIN_0PT5:
417                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2 |
418                                 F020_MASK_ADC0CF_AMP0GN1;
419                 break;
420         default:
421                 /* this should never happen, just use a gain of 1 */
422         case DT9812_GAIN_1:
423                 rmw->or_value = 0x00;
424                 break;
425         case DT9812_GAIN_2:
426                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN0;
427                 break;
428         case DT9812_GAIN_4:
429                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1;
430                 break;
431         case DT9812_GAIN_8:
432                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1 |
433                                 F020_MASK_ADC0CF_AMP0GN0;
434                 break;
435         case DT9812_GAIN_16:
436                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2;
437                 break;
438         }
439 }
440
441 static int dt9812_analog_in(struct comedi_device *dev,
442                             int channel, u16 *value, enum dt9812_gain gain)
443 {
444         struct dt9812_private *devpriv = dev->private;
445         struct dt9812_rmw_byte rmw[3];
446         u8 reg[3] = {
447                 F020_SFR_ADC0CN,
448                 F020_SFR_ADC0H,
449                 F020_SFR_ADC0L
450         };
451         u8 val[3];
452         int ret;
453
454         down(&devpriv->sem);
455
456         /* 1 select the gain */
457         dt9812_configure_gain(dev, &rmw[0], gain);
458
459         /* 2 set the MUX to select the channel */
460         dt9812_configure_mux(dev, &rmw[1], channel);
461
462         /* 3 start conversion */
463         rmw[2].address = F020_SFR_ADC0CN;
464         rmw[2].and_mask = 0xff;
465         rmw[2].or_value = F020_MASK_ADC0CN_AD0EN | F020_MASK_ADC0CN_AD0BUSY;
466
467         ret = dt9812_rmw_multiple_registers(dev, 3, rmw);
468         if (ret)
469                 goto exit;
470
471         /* read the status and ADC */
472         ret = dt9812_read_multiple_registers(dev, 3, reg, val);
473         if (ret)
474                 goto exit;
475
476         /*
477          * An ADC conversion takes 16 SAR clocks cycles, i.e. about 9us.
478          * Therefore, between the instant that AD0BUSY was set via
479          * dt9812_rmw_multiple_registers and the read of AD0BUSY via
480          * dt9812_read_multiple_registers, the conversion should be complete
481          * since these two operations require two USB transactions each taking
482          * at least a millisecond to complete.  However, lets make sure that
483          * conversion is finished.
484          */
485         if ((val[0] & (F020_MASK_ADC0CN_AD0INT | F020_MASK_ADC0CN_AD0BUSY)) ==
486             F020_MASK_ADC0CN_AD0INT) {
487                 switch (devpriv->device) {
488                 case DT9812_DEVID_DT9812_10:
489                         /*
490                          * For DT9812-10V the personality module set the
491                          * encoding to 2's complement. Hence, convert it before
492                          * returning it
493                          */
494                         *value = ((val[1] << 8) | val[2]) + 0x800;
495                         break;
496                 case DT9812_DEVID_DT9812_2PT5:
497                         *value = (val[1] << 8) | val[2];
498                         break;
499                 }
500         }
501
502 exit:
503         up(&devpriv->sem);
504
505         return ret;
506 }
507
508 static int dt9812_analog_out(struct comedi_device *dev, int channel, u16 value)
509 {
510         struct dt9812_private *devpriv = dev->private;
511         struct dt9812_rmw_byte rmw[3];
512         int ret;
513
514         down(&devpriv->sem);
515
516         switch (channel) {
517         case 0:
518                 /* 1. Set DAC mode */
519                 rmw[0].address = F020_SFR_DAC0CN;
520                 rmw[0].and_mask = 0xff;
521                 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
522
523                 /* 2 load low byte of DAC value first */
524                 rmw[1].address = F020_SFR_DAC0L;
525                 rmw[1].and_mask = 0xff;
526                 rmw[1].or_value = value & 0xff;
527
528                 /* 3 load high byte of DAC value next to latch the
529                         12-bit value */
530                 rmw[2].address = F020_SFR_DAC0H;
531                 rmw[2].and_mask = 0xff;
532                 rmw[2].or_value = (value >> 8) & 0xf;
533                 break;
534
535         case 1:
536                 /* 1. Set DAC mode */
537                 rmw[0].address = F020_SFR_DAC1CN;
538                 rmw[0].and_mask = 0xff;
539                 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
540
541                 /* 2 load low byte of DAC value first */
542                 rmw[1].address = F020_SFR_DAC1L;
543                 rmw[1].and_mask = 0xff;
544                 rmw[1].or_value = value & 0xff;
545
546                 /* 3 load high byte of DAC value next to latch the
547                         12-bit value */
548                 rmw[2].address = F020_SFR_DAC1H;
549                 rmw[2].and_mask = 0xff;
550                 rmw[2].or_value = (value >> 8) & 0xf;
551                 break;
552         }
553         ret = dt9812_rmw_multiple_registers(dev, 3, rmw);
554         devpriv->ao_shadow[channel] = value;
555
556         up(&devpriv->sem);
557
558         return ret;
559 }
560
561 static int dt9812_di_insn_bits(struct comedi_device *dev,
562                                struct comedi_subdevice *s,
563                                struct comedi_insn *insn,
564                                unsigned int *data)
565 {
566         u8 bits = 0;
567         int ret;
568
569         ret = dt9812_digital_in(dev, &bits);
570         if (ret)
571                 return ret;
572
573         data[1] = bits;
574
575         return insn->n;
576 }
577
578 static int dt9812_do_insn_bits(struct comedi_device *dev,
579                                struct comedi_subdevice *s,
580                                struct comedi_insn *insn,
581                                unsigned int *data)
582 {
583         unsigned int mask = data[0];
584         unsigned int bits = data[1];
585
586         if (mask) {
587                 s->state &= ~mask;
588                 s->state |= (bits & mask);
589
590                 dt9812_digital_out(dev, s->state);
591         }
592
593         data[1] = s->state;
594
595         return insn->n;
596 }
597
598 static int dt9812_ai_insn_read(struct comedi_device *dev,
599                                struct comedi_subdevice *s,
600                                struct comedi_insn *insn,
601                                unsigned int *data)
602 {
603         unsigned int chan = CR_CHAN(insn->chanspec);
604         u16 val = 0;
605         int ret;
606         int i;
607
608         for (i = 0; i < insn->n; i++) {
609                 ret = dt9812_analog_in(dev, chan, &val, DT9812_GAIN_1);
610                 if (ret)
611                         return ret;
612                 data[i] = val;
613         }
614
615         return insn->n;
616 }
617
618 static int dt9812_ao_insn_read(struct comedi_device *dev,
619                                struct comedi_subdevice *s,
620                                struct comedi_insn *insn,
621                                unsigned int *data)
622 {
623         struct dt9812_private *devpriv = dev->private;
624         unsigned int chan = CR_CHAN(insn->chanspec);
625         int i;
626
627         down(&devpriv->sem);
628         for (i = 0; i < insn->n; i++)
629                 data[i] = devpriv->ao_shadow[chan];
630         up(&devpriv->sem);
631
632         return insn->n;
633 }
634
635 static int dt9812_ao_insn_write(struct comedi_device *dev,
636                                 struct comedi_subdevice *s,
637                                 struct comedi_insn *insn,
638                                 unsigned int *data)
639 {
640         unsigned int chan = CR_CHAN(insn->chanspec);
641         int ret;
642         int i;
643
644         for (i = 0; i < insn->n; i++) {
645                 ret = dt9812_analog_out(dev, chan, data[i]);
646                 if (ret)
647                         return ret;
648         }
649
650         return insn->n;
651 }
652
653 static int dt9812_find_endpoints(struct comedi_device *dev)
654 {
655         struct usb_interface *intf = comedi_to_usb_interface(dev);
656         struct usb_host_interface *host = intf->cur_altsetting;
657         struct dt9812_private *devpriv = dev->private;
658         struct usb_endpoint_descriptor *ep;
659         int i;
660
661         if (host->desc.bNumEndpoints != 5) {
662                 dev_err(dev->class_dev, "Wrong number of endpoints\n");
663                 return -ENODEV;
664         }
665
666         for (i = 0; i < host->desc.bNumEndpoints; ++i) {
667                 int dir = -1;
668                 ep = &host->endpoint[i].desc;
669                 switch (i) {
670                 case 0:
671                         /* unused message pipe */
672                         dir = USB_DIR_IN;
673                         break;
674                 case 1:
675                         dir = USB_DIR_OUT;
676                         devpriv->cmd_wr.addr = ep->bEndpointAddress;
677                         devpriv->cmd_wr.size = le16_to_cpu(ep->wMaxPacketSize);
678                         break;
679                 case 2:
680                         dir = USB_DIR_IN;
681                         devpriv->cmd_rd.addr = ep->bEndpointAddress;
682                         devpriv->cmd_rd.size = le16_to_cpu(ep->wMaxPacketSize);
683                         break;
684                 case 3:
685                         /* unused write stream */
686                         dir = USB_DIR_OUT;
687                         break;
688                 case 4:
689                         /* unused read stream */
690                         dir = USB_DIR_IN;
691                         break;
692                 }
693                 if ((ep->bEndpointAddress & USB_DIR_IN) != dir) {
694                         dev_err(dev->class_dev,
695                                 "Endpoint has wrong direction\n");
696                         return -ENODEV;
697                 }
698         }
699         return 0;
700 }
701
702 static int dt9812_reset_device(struct comedi_device *dev)
703 {
704         struct usb_device *usb = comedi_to_usb_dev(dev);
705         struct dt9812_private *devpriv = dev->private;
706         u32 serial;
707         u16 vendor;
708         u16 product;
709         u8 tmp8;
710         __le16 tmp16;
711         __le32 tmp32;
712         int ret;
713         int i;
714
715         ret = dt9812_read_info(dev, 0, &tmp8, sizeof(tmp8));
716         if (ret) {
717                 /*
718                  * Seems like a configuration reset is necessary if driver is
719                  * reloaded while device is attached
720                  */
721                 usb_reset_configuration(usb);
722                 for (i = 0; i < 10; i++) {
723                         ret = dt9812_read_info(dev, 1, &tmp8, sizeof(tmp8));
724                         if (ret == 0)
725                                 break;
726                 }
727                 if (ret) {
728                         dev_err(dev->class_dev,
729                                 "unable to reset configuration\n");
730                         return ret;
731                 }
732         }
733
734         ret = dt9812_read_info(dev, 1, &tmp16, sizeof(tmp16));
735         if (ret) {
736                 dev_err(dev->class_dev, "failed to read vendor id\n");
737                 return ret;
738         }
739         vendor = le16_to_cpu(tmp16);
740
741         ret = dt9812_read_info(dev, 3, &tmp16, sizeof(tmp16));
742         if (ret) {
743                 dev_err(dev->class_dev, "failed to read product id\n");
744                 return ret;
745         }
746         product = le16_to_cpu(tmp16);
747
748         ret = dt9812_read_info(dev, 5, &tmp16, sizeof(tmp16));
749         if (ret) {
750                 dev_err(dev->class_dev, "failed to read device id\n");
751                 return ret;
752         }
753         devpriv->device = le16_to_cpu(tmp16);
754
755         ret = dt9812_read_info(dev, 7, &tmp32, sizeof(tmp32));
756         if (ret) {
757                 dev_err(dev->class_dev, "failed to read serial number\n");
758                 return ret;
759         }
760         serial = le32_to_cpu(tmp32);
761
762         /* let the user know what node this device is now attached to */
763         dev_info(dev->class_dev, "USB DT9812 (%4.4x.%4.4x.%4.4x) #0x%8.8x\n",
764                  vendor, product, devpriv->device, serial);
765
766         if (devpriv->device != DT9812_DEVID_DT9812_10 &&
767             devpriv->device != DT9812_DEVID_DT9812_2PT5) {
768                 dev_err(dev->class_dev, "Unsupported device!\n");
769                 return -EINVAL;
770         }
771
772         return 0;
773 }
774
775 static int dt9812_auto_attach(struct comedi_device *dev,
776                               unsigned long context)
777 {
778         struct usb_interface *intf = comedi_to_usb_interface(dev);
779         struct dt9812_private *devpriv;
780         struct comedi_subdevice *s;
781         bool is_unipolar;
782         int ret;
783
784         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
785         if (!devpriv)
786                 return -ENOMEM;
787
788         sema_init(&devpriv->sem, 1);
789         usb_set_intfdata(intf, devpriv);
790
791         ret = dt9812_find_endpoints(dev);
792         if (ret)
793                 return ret;
794
795         ret = dt9812_reset_device(dev);
796         if (ret)
797                 return ret;
798
799         is_unipolar = (devpriv->device == DT9812_DEVID_DT9812_2PT5);
800
801         ret = comedi_alloc_subdevices(dev, 4);
802         if (ret)
803                 return ret;
804
805         /* Digital Input subdevice */
806         s = &dev->subdevices[0];
807         s->type         = COMEDI_SUBD_DI;
808         s->subdev_flags = SDF_READABLE;
809         s->n_chan       = 8;
810         s->maxdata      = 1;
811         s->range_table  = &range_digital;
812         s->insn_bits    = dt9812_di_insn_bits;
813
814         /* Digital Output subdevice */
815         s = &dev->subdevices[1];
816         s->type         = COMEDI_SUBD_DO;
817         s->subdev_flags = SDF_WRITEABLE;
818         s->n_chan       = 8;
819         s->maxdata      = 1;
820         s->range_table  = &range_digital;
821         s->insn_bits    = dt9812_do_insn_bits;
822
823         /* Analog Input subdevice */
824         s = &dev->subdevices[2];
825         s->type         = COMEDI_SUBD_AI;
826         s->subdev_flags = SDF_READABLE | SDF_GROUND;
827         s->n_chan       = 8;
828         s->maxdata      = 0x0fff;
829         s->range_table  = is_unipolar ? &range_unipolar2_5 : &range_bipolar10;
830         s->insn_read    = dt9812_ai_insn_read;
831
832         /* Analog Output subdevice */
833         s = &dev->subdevices[3];
834         s->type         = COMEDI_SUBD_AO;
835         s->subdev_flags = SDF_WRITEABLE;
836         s->n_chan       = 2;
837         s->maxdata      = 0x0fff;
838         s->range_table  = is_unipolar ? &range_unipolar2_5 : &range_bipolar10;
839         s->insn_write   = dt9812_ao_insn_write;
840         s->insn_read    = dt9812_ao_insn_read;
841
842         devpriv->ao_shadow[0] = is_unipolar ? 0x0000 : 0x0800;
843         devpriv->ao_shadow[1] = is_unipolar ? 0x0000 : 0x0800;
844
845         return 0;
846 }
847
848 static void dt9812_detach(struct comedi_device *dev)
849 {
850         struct usb_interface *intf = comedi_to_usb_interface(dev);
851         struct dt9812_private *devpriv = dev->private;
852
853         if (!devpriv)
854                 return;
855
856         down(&devpriv->sem);
857
858         usb_set_intfdata(intf, NULL);
859
860         up(&devpriv->sem);
861 }
862
863 static struct comedi_driver dt9812_driver = {
864         .driver_name    = "dt9812",
865         .module         = THIS_MODULE,
866         .auto_attach    = dt9812_auto_attach,
867         .detach         = dt9812_detach,
868 };
869
870 static int dt9812_usb_probe(struct usb_interface *intf,
871                             const struct usb_device_id *id)
872 {
873         return comedi_usb_auto_config(intf, &dt9812_driver, id->driver_info);
874 }
875
876 static const struct usb_device_id dt9812_usb_table[] = {
877         { USB_DEVICE(0x0867, 0x9812) },
878         { }
879 };
880 MODULE_DEVICE_TABLE(usb, dt9812_usb_table);
881
882 static struct usb_driver dt9812_usb_driver = {
883         .name           = "dt9812",
884         .id_table       = dt9812_usb_table,
885         .probe          = dt9812_usb_probe,
886         .disconnect     = comedi_usb_auto_unconfig,
887 };
888 module_comedi_usb_driver(dt9812_driver, dt9812_usb_driver);
889
890 MODULE_AUTHOR("Anders Blomdell <anders.blomdell@control.lth.se>");
891 MODULE_DESCRIPTION("Comedi DT9812 driver");
892 MODULE_LICENSE("GPL");