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