Merge remote-tracking branch 'lsk/v3.10/topic/coresight' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.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  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 /*
26 Driver: dt9812
27 Description: Data Translation DT9812 USB module
28 Author: anders.blomdell@control.lth.se (Anders Blomdell)
29 Status: in development
30 Devices: [Data Translation] DT9812 (dt9812)
31 Updated: Sun Nov 20 20:18:34 EST 2005
32
33 This driver works, but bulk transfers not implemented. Might be a starting point
34 for someone else. I found out too late that USB has too high latencies (>1 ms)
35 for my needs.
36 */
37
38 /*
39  * Nota Bene:
40  *   1. All writes to command pipe has to be 32 bytes (ISP1181B SHRTP=0 ?)
41  *   2. The DDK source (as of sep 2005) is in error regarding the
42  *      input MUX bits (example code says P4, but firmware schematics
43  *      says P1).
44  */
45
46 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
47
48 #include <linux/kernel.h>
49 #include <linux/errno.h>
50 #include <linux/init.h>
51 #include <linux/slab.h>
52 #include <linux/module.h>
53 #include <linux/kref.h>
54 #include <linux/uaccess.h>
55 #include <linux/usb.h>
56
57 #include "../comedidev.h"
58
59 #define DT9812_DIAGS_BOARD_INFO_ADDR    0xFBFF
60 #define DT9812_MAX_WRITE_CMD_PIPE_SIZE  32
61 #define DT9812_MAX_READ_CMD_PIPE_SIZE   32
62
63 /*
64  * See Silican Laboratories C8051F020/1/2/3 manual
65  */
66 #define F020_SFR_P4                     0x84
67 #define F020_SFR_P1                     0x90
68 #define F020_SFR_P2                     0xa0
69 #define F020_SFR_P3                     0xb0
70 #define F020_SFR_AMX0CF                 0xba
71 #define F020_SFR_AMX0SL                 0xbb
72 #define F020_SFR_ADC0CF                 0xbc
73 #define F020_SFR_ADC0L                  0xbe
74 #define F020_SFR_ADC0H                  0xbf
75 #define F020_SFR_DAC0L                  0xd2
76 #define F020_SFR_DAC0H                  0xd3
77 #define F020_SFR_DAC0CN                 0xd4
78 #define F020_SFR_DAC1L                  0xd5
79 #define F020_SFR_DAC1H                  0xd6
80 #define F020_SFR_DAC1CN                 0xd7
81 #define F020_SFR_ADC0CN                 0xe8
82
83 #define F020_MASK_ADC0CF_AMP0GN0        0x01
84 #define F020_MASK_ADC0CF_AMP0GN1        0x02
85 #define F020_MASK_ADC0CF_AMP0GN2        0x04
86
87 #define F020_MASK_ADC0CN_AD0EN          0x80
88 #define F020_MASK_ADC0CN_AD0INT         0x20
89 #define F020_MASK_ADC0CN_AD0BUSY        0x10
90
91 #define F020_MASK_DACxCN_DACxEN         0x80
92
93 enum {
94         /* A/D  D/A  DI  DO  CT */
95         DT9812_DEVID_DT9812_10, /*  8    2   8   8   1  +/- 10V */
96         DT9812_DEVID_DT9812_2PT5,       /* 8    2   8   8   1  0-2.44V */
97 #if 0
98         DT9812_DEVID_DT9813,    /*  16   2   4   4   1  +/- 10V */
99         DT9812_DEVID_DT9814     /*  24   2   0   0   1  +/- 10V */
100 #endif
101 };
102
103 enum dt9812_gain {
104         DT9812_GAIN_0PT25 = 1,
105         DT9812_GAIN_0PT5 = 2,
106         DT9812_GAIN_1 = 4,
107         DT9812_GAIN_2 = 8,
108         DT9812_GAIN_4 = 16,
109         DT9812_GAIN_8 = 32,
110         DT9812_GAIN_16 = 64,
111 };
112
113 enum {
114         DT9812_LEAST_USB_FIRMWARE_CMD_CODE = 0,
115         /* Write Flash memory */
116         DT9812_W_FLASH_DATA = 0,
117         /* Read Flash memory misc config info */
118         DT9812_R_FLASH_DATA = 1,
119
120         /*
121          * Register read/write commands for processor
122          */
123
124         /* Read a single byte of USB memory */
125         DT9812_R_SINGLE_BYTE_REG = 2,
126         /* Write a single byte of USB memory */
127         DT9812_W_SINGLE_BYTE_REG = 3,
128         /* Multiple Reads of USB memory */
129         DT9812_R_MULTI_BYTE_REG = 4,
130         /* Multiple Writes of USB memory */
131         DT9812_W_MULTI_BYTE_REG = 5,
132         /* Read, (AND) with mask, OR value, then write (single) */
133         DT9812_RMW_SINGLE_BYTE_REG = 6,
134         /* Read, (AND) with mask, OR value, then write (multiple) */
135         DT9812_RMW_MULTI_BYTE_REG = 7,
136
137         /*
138          * Register read/write commands for SMBus
139          */
140
141         /* Read a single byte of SMBus */
142         DT9812_R_SINGLE_BYTE_SMBUS = 8,
143         /* Write a single byte of SMBus */
144         DT9812_W_SINGLE_BYTE_SMBUS = 9,
145         /* Multiple Reads of SMBus */
146         DT9812_R_MULTI_BYTE_SMBUS = 10,
147         /* Multiple Writes of SMBus */
148         DT9812_W_MULTI_BYTE_SMBUS = 11,
149
150         /*
151          * Register read/write commands for a device
152          */
153
154         /* Read a single byte of a device */
155         DT9812_R_SINGLE_BYTE_DEV = 12,
156         /* Write a single byte of a device */
157         DT9812_W_SINGLE_BYTE_DEV = 13,
158         /* Multiple Reads of a device */
159         DT9812_R_MULTI_BYTE_DEV = 14,
160         /* Multiple Writes of a device */
161         DT9812_W_MULTI_BYTE_DEV = 15,
162
163         /* Not sure if we'll need this */
164         DT9812_W_DAC_THRESHOLD = 16,
165
166         /* Set interrupt on change mask */
167         DT9812_W_INT_ON_CHANGE_MASK = 17,
168
169         /* Write (or Clear) the CGL for the ADC */
170         DT9812_W_CGL = 18,
171         /* Multiple Reads of USB memory */
172         DT9812_R_MULTI_BYTE_USBMEM = 19,
173         /* Multiple Writes to USB memory */
174         DT9812_W_MULTI_BYTE_USBMEM = 20,
175
176         /* Issue a start command to a given subsystem */
177         DT9812_START_SUBSYSTEM = 21,
178         /* Issue a stop command to a given subsystem */
179         DT9812_STOP_SUBSYSTEM = 22,
180
181         /* calibrate the board using CAL_POT_CMD */
182         DT9812_CALIBRATE_POT = 23,
183         /* set the DAC FIFO size */
184         DT9812_W_DAC_FIFO_SIZE = 24,
185         /* Write or Clear the CGL for the DAC */
186         DT9812_W_CGL_DAC = 25,
187         /* Read a single value from a subsystem */
188         DT9812_R_SINGLE_VALUE_CMD = 26,
189         /* Write a single value to a subsystem */
190         DT9812_W_SINGLE_VALUE_CMD = 27,
191         /* Valid DT9812_USB_FIRMWARE_CMD_CODE's will be less than this number */
192         DT9812_MAX_USB_FIRMWARE_CMD_CODE,
193 };
194
195 struct dt9812_flash_data {
196         u16 numbytes;
197         u16 address;
198 };
199
200 #define DT9812_MAX_NUM_MULTI_BYTE_RDS  \
201         ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
202
203 struct dt9812_read_multi {
204         u8 count;
205         u8 address[DT9812_MAX_NUM_MULTI_BYTE_RDS];
206 };
207
208 struct dt9812_write_byte {
209         u8 address;
210         u8 value;
211 };
212
213 #define DT9812_MAX_NUM_MULTI_BYTE_WRTS  \
214         ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
215          sizeof(struct dt9812_write_byte))
216
217 struct dt9812_write_multi {
218         u8 count;
219         struct dt9812_write_byte write[DT9812_MAX_NUM_MULTI_BYTE_WRTS];
220 };
221
222 struct dt9812_rmw_byte {
223         u8 address;
224         u8 and_mask;
225         u8 or_value;
226 };
227
228 #define DT9812_MAX_NUM_MULTI_BYTE_RMWS  \
229         ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
230          sizeof(struct dt9812_rmw_byte))
231
232 struct dt9812_rmw_multi {
233         u8 count;
234         struct dt9812_rmw_byte rmw[DT9812_MAX_NUM_MULTI_BYTE_RMWS];
235 };
236
237 struct dt9812_usb_cmd {
238         u32 cmd;
239         union {
240                 struct dt9812_flash_data flash_data_info;
241                 struct dt9812_read_multi read_multi_info;
242                 struct dt9812_write_multi write_multi_info;
243                 struct dt9812_rmw_multi rmw_multi_info;
244         } u;
245 #if 0
246         WRITE_BYTE_INFO WriteByteInfo;
247         READ_BYTE_INFO ReadByteInfo;
248         WRITE_MULTI_INFO WriteMultiInfo;
249         READ_MULTI_INFO ReadMultiInfo;
250         RMW_BYTE_INFO RMWByteInfo;
251         RMW_MULTI_INFO RMWMultiInfo;
252         DAC_THRESHOLD_INFO DacThresholdInfo;
253         INT_ON_CHANGE_MASK_INFO IntOnChangeMaskInfo;
254         CGL_INFO CglInfo;
255         SUBSYSTEM_INFO SubsystemInfo;
256         CAL_POT_CMD CalPotCmd;
257         WRITE_DEV_BYTE_INFO WriteDevByteInfo;
258         READ_DEV_BYTE_INFO ReadDevByteInfo;
259         WRITE_DEV_MULTI_INFO WriteDevMultiInfo;
260         READ_DEV_MULTI_INFO ReadDevMultiInfo;
261         READ_SINGLE_VALUE_INFO ReadSingleValueInfo;
262         WRITE_SINGLE_VALUE_INFO WriteSingleValueInfo;
263 #endif
264 };
265
266 #define DT9812_NUM_SLOTS        16
267
268 static DEFINE_SEMAPHORE(dt9812_mutex);
269
270 static const struct usb_device_id dt9812_table[] = {
271         {USB_DEVICE(0x0867, 0x9812)},
272         {}                      /* Terminating entry */
273 };
274
275 MODULE_DEVICE_TABLE(usb, dt9812_table);
276
277 struct usb_dt9812 {
278         struct slot_dt9812 *slot;
279         struct usb_device *udev;
280         struct usb_interface *interface;
281         u16 vendor;
282         u16 product;
283         u16 device;
284         u32 serial;
285         struct {
286                 __u8 addr;
287                 size_t size;
288         } message_pipe, command_write, command_read, write_stream, read_stream;
289         struct kref kref;
290         u16 analog_out_shadow[2];
291         u8 digital_out_shadow;
292 };
293
294 struct comedi_dt9812 {
295         struct slot_dt9812 *slot;
296         u32 serial;
297 };
298
299 struct slot_dt9812 {
300         struct semaphore mutex;
301         u32 serial;
302         struct usb_dt9812 *usb;
303         struct comedi_dt9812 *comedi;
304 };
305
306 static struct slot_dt9812 dt9812[DT9812_NUM_SLOTS];
307
308 static inline struct usb_dt9812 *to_dt9812_dev(struct kref *d)
309 {
310         return container_of(d, struct usb_dt9812, kref);
311 }
312
313 static void dt9812_delete(struct kref *kref)
314 {
315         struct usb_dt9812 *dev = to_dt9812_dev(kref);
316
317         usb_put_dev(dev->udev);
318         kfree(dev);
319 }
320
321 static int dt9812_read_info(struct usb_dt9812 *dev, int offset, void *buf,
322                             size_t buf_size)
323 {
324         struct dt9812_usb_cmd cmd;
325         int count, retval;
326
327         cmd.cmd = cpu_to_le32(DT9812_R_FLASH_DATA);
328         cmd.u.flash_data_info.address =
329             cpu_to_le16(DT9812_DIAGS_BOARD_INFO_ADDR + offset);
330         cmd.u.flash_data_info.numbytes = cpu_to_le16(buf_size);
331
332         /* DT9812 only responds to 32 byte writes!! */
333         count = 32;
334         retval = usb_bulk_msg(dev->udev,
335                               usb_sndbulkpipe(dev->udev,
336                                               dev->command_write.addr),
337                               &cmd, 32, &count, HZ * 1);
338         if (retval)
339                 return retval;
340         retval = usb_bulk_msg(dev->udev,
341                               usb_rcvbulkpipe(dev->udev,
342                                               dev->command_read.addr),
343                               buf, buf_size, &count, HZ * 1);
344         return retval;
345 }
346
347 static int dt9812_read_multiple_registers(struct usb_dt9812 *dev, int reg_count,
348                                           u8 *address, u8 *value)
349 {
350         struct dt9812_usb_cmd cmd;
351         int i, count, retval;
352
353         cmd.cmd = cpu_to_le32(DT9812_R_MULTI_BYTE_REG);
354         cmd.u.read_multi_info.count = reg_count;
355         for (i = 0; i < reg_count; i++)
356                 cmd.u.read_multi_info.address[i] = address[i];
357
358         /* DT9812 only responds to 32 byte writes!! */
359         count = 32;
360         retval = usb_bulk_msg(dev->udev,
361                               usb_sndbulkpipe(dev->udev,
362                                               dev->command_write.addr),
363                               &cmd, 32, &count, HZ * 1);
364         if (retval)
365                 return retval;
366         retval = usb_bulk_msg(dev->udev,
367                               usb_rcvbulkpipe(dev->udev,
368                                               dev->command_read.addr),
369                               value, reg_count, &count, HZ * 1);
370         return retval;
371 }
372
373 static int dt9812_write_multiple_registers(struct usb_dt9812 *dev,
374                                            int reg_count, u8 *address,
375                                            u8 *value)
376 {
377         struct dt9812_usb_cmd cmd;
378         int i, count, retval;
379
380         cmd.cmd = cpu_to_le32(DT9812_W_MULTI_BYTE_REG);
381         cmd.u.read_multi_info.count = reg_count;
382         for (i = 0; i < reg_count; i++) {
383                 cmd.u.write_multi_info.write[i].address = address[i];
384                 cmd.u.write_multi_info.write[i].value = value[i];
385         }
386         /* DT9812 only responds to 32 byte writes!! */
387         retval = usb_bulk_msg(dev->udev,
388                               usb_sndbulkpipe(dev->udev,
389                                               dev->command_write.addr),
390                               &cmd, 32, &count, HZ * 1);
391         return retval;
392 }
393
394 static int dt9812_rmw_multiple_registers(struct usb_dt9812 *dev, int reg_count,
395                                          struct dt9812_rmw_byte *rmw)
396 {
397         struct dt9812_usb_cmd cmd;
398         int i, count, retval;
399
400         cmd.cmd = cpu_to_le32(DT9812_RMW_MULTI_BYTE_REG);
401         cmd.u.rmw_multi_info.count = reg_count;
402         for (i = 0; i < reg_count; i++)
403                 cmd.u.rmw_multi_info.rmw[i] = rmw[i];
404
405         /* DT9812 only responds to 32 byte writes!! */
406         retval = usb_bulk_msg(dev->udev,
407                               usb_sndbulkpipe(dev->udev,
408                                               dev->command_write.addr),
409                               &cmd, 32, &count, HZ * 1);
410         return retval;
411 }
412
413 static int dt9812_digital_in(struct slot_dt9812 *slot, u8 *bits)
414 {
415         int result = -ENODEV;
416
417         down(&slot->mutex);
418         if (slot->usb) {
419                 u8 reg[2] = { F020_SFR_P3, F020_SFR_P1 };
420                 u8 value[2];
421
422                 result = dt9812_read_multiple_registers(slot->usb, 2, reg,
423                                                         value);
424                 if (result == 0) {
425                         /*
426                          * bits 0-6 in F020_SFR_P3 are bits 0-6 in the digital
427                          * input port bit 3 in F020_SFR_P1 is bit 7 in the
428                          * digital input port
429                          */
430                         *bits = (value[0] & 0x7f) | ((value[1] & 0x08) << 4);
431                         /* printk("%2.2x, %2.2x -> %2.2x\n",
432                            value[0], value[1], *bits); */
433                 }
434         }
435         up(&slot->mutex);
436
437         return result;
438 }
439
440 static int dt9812_digital_out(struct slot_dt9812 *slot, u8 bits)
441 {
442         int result = -ENODEV;
443
444         down(&slot->mutex);
445         if (slot->usb) {
446                 u8 reg[1];
447                 u8 value[1];
448
449                 reg[0] = F020_SFR_P2;
450                 value[0] = bits;
451                 result = dt9812_write_multiple_registers(slot->usb, 1, reg,
452                                                          value);
453                 slot->usb->digital_out_shadow = bits;
454         }
455         up(&slot->mutex);
456         return result;
457 }
458
459 static int dt9812_digital_out_shadow(struct slot_dt9812 *slot, u8 *bits)
460 {
461         int result = -ENODEV;
462
463         down(&slot->mutex);
464         if (slot->usb) {
465                 *bits = slot->usb->digital_out_shadow;
466                 result = 0;
467         }
468         up(&slot->mutex);
469         return result;
470 }
471
472 static void dt9812_configure_mux(struct usb_dt9812 *dev,
473                                  struct dt9812_rmw_byte *rmw, int channel)
474 {
475         if (dev->device == DT9812_DEVID_DT9812_10) {
476                 /* In the DT9812/10V MUX is selected by P1.5-7 */
477                 rmw->address = F020_SFR_P1;
478                 rmw->and_mask = 0xe0;
479                 rmw->or_value = channel << 5;
480         } else {
481                 /* In the DT9812/2.5V, internal mux is selected by bits 0:2 */
482                 rmw->address = F020_SFR_AMX0SL;
483                 rmw->and_mask = 0xff;
484                 rmw->or_value = channel & 0x07;
485         }
486 }
487
488 static void dt9812_configure_gain(struct usb_dt9812 *dev,
489                                   struct dt9812_rmw_byte *rmw,
490                                   enum dt9812_gain gain)
491 {
492         if (dev->device == DT9812_DEVID_DT9812_10) {
493                 /* In the DT9812/10V, there is an external gain of 0.5 */
494                 gain <<= 1;
495         }
496
497         rmw->address = F020_SFR_ADC0CF;
498         rmw->and_mask = F020_MASK_ADC0CF_AMP0GN2 |
499             F020_MASK_ADC0CF_AMP0GN1 | F020_MASK_ADC0CF_AMP0GN0;
500         switch (gain) {
501                 /*
502                  * 000 -> Gain =  1
503                  * 001 -> Gain =  2
504                  * 010 -> Gain =  4
505                  * 011 -> Gain =  8
506                  * 10x -> Gain = 16
507                  * 11x -> Gain =  0.5
508                  */
509         case DT9812_GAIN_0PT5:
510                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2 |
511                     F020_MASK_ADC0CF_AMP0GN1;
512                 break;
513         case DT9812_GAIN_1:
514                 rmw->or_value = 0x00;
515                 break;
516         case DT9812_GAIN_2:
517                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN0;
518                 break;
519         case DT9812_GAIN_4:
520                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1;
521                 break;
522         case DT9812_GAIN_8:
523                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1 |
524                     F020_MASK_ADC0CF_AMP0GN0;
525                 break;
526         case DT9812_GAIN_16:
527                 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2;
528                 break;
529         default:
530                 dev_err(&dev->interface->dev, "Illegal gain %d\n", gain);
531
532         }
533 }
534
535 static int dt9812_analog_in(struct slot_dt9812 *slot, int channel, u16 *value,
536                             enum dt9812_gain gain)
537 {
538         struct dt9812_rmw_byte rmw[3];
539         u8 reg[3] = {
540                 F020_SFR_ADC0CN,
541                 F020_SFR_ADC0H,
542                 F020_SFR_ADC0L
543         };
544         u8 val[3];
545         int result = -ENODEV;
546
547         down(&slot->mutex);
548         if (!slot->usb)
549                 goto exit;
550
551         /* 1 select the gain */
552         dt9812_configure_gain(slot->usb, &rmw[0], gain);
553
554         /* 2 set the MUX to select the channel */
555         dt9812_configure_mux(slot->usb, &rmw[1], channel);
556
557         /* 3 start conversion */
558         rmw[2].address = F020_SFR_ADC0CN;
559         rmw[2].and_mask = 0xff;
560         rmw[2].or_value = F020_MASK_ADC0CN_AD0EN | F020_MASK_ADC0CN_AD0BUSY;
561
562         result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
563         if (result)
564                 goto exit;
565
566         /* read the status and ADC */
567         result = dt9812_read_multiple_registers(slot->usb, 3, reg, val);
568         if (result)
569                 goto exit;
570         /*
571          * An ADC conversion takes 16 SAR clocks cycles, i.e. about 9us.
572          * Therefore, between the instant that AD0BUSY was set via
573          * dt9812_rmw_multiple_registers and the read of AD0BUSY via
574          * dt9812_read_multiple_registers, the conversion should be complete
575          * since these two operations require two USB transactions each taking
576          * at least a millisecond to complete.  However, lets make sure that
577          * conversion is finished.
578          */
579         if ((val[0] & (F020_MASK_ADC0CN_AD0INT | F020_MASK_ADC0CN_AD0BUSY)) ==
580             F020_MASK_ADC0CN_AD0INT) {
581                 switch (slot->usb->device) {
582                 case DT9812_DEVID_DT9812_10:
583                         /*
584                          * For DT9812-10V the personality module set the
585                          * encoding to 2's complement. Hence, convert it before
586                          * returning it
587                          */
588                         *value = ((val[1] << 8) | val[2]) + 0x800;
589                         break;
590                 case DT9812_DEVID_DT9812_2PT5:
591                         *value = (val[1] << 8) | val[2];
592                         break;
593                 }
594         }
595
596 exit:
597         up(&slot->mutex);
598         return result;
599 }
600
601 static int dt9812_analog_out_shadow(struct slot_dt9812 *slot, int channel,
602                                     u16 *value)
603 {
604         int result = -ENODEV;
605
606         down(&slot->mutex);
607         if (slot->usb) {
608                 *value = slot->usb->analog_out_shadow[channel];
609                 result = 0;
610         }
611         up(&slot->mutex);
612
613         return result;
614 }
615
616 static int dt9812_analog_out(struct slot_dt9812 *slot, int channel, u16 value)
617 {
618         int result = -ENODEV;
619
620         down(&slot->mutex);
621         if (slot->usb) {
622                 struct dt9812_rmw_byte rmw[3];
623
624                 switch (channel) {
625                 case 0:
626                         /* 1. Set DAC mode */
627                         rmw[0].address = F020_SFR_DAC0CN;
628                         rmw[0].and_mask = 0xff;
629                         rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
630
631                         /* 2 load low byte of DAC value first */
632                         rmw[1].address = F020_SFR_DAC0L;
633                         rmw[1].and_mask = 0xff;
634                         rmw[1].or_value = value & 0xff;
635
636                         /* 3 load high byte of DAC value next to latch the
637                            12-bit value */
638                         rmw[2].address = F020_SFR_DAC0H;
639                         rmw[2].and_mask = 0xff;
640                         rmw[2].or_value = (value >> 8) & 0xf;
641                         break;
642
643                 case 1:
644                         /* 1. Set DAC mode */
645                         rmw[0].address = F020_SFR_DAC1CN;
646                         rmw[0].and_mask = 0xff;
647                         rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
648
649                         /* 2 load low byte of DAC value first */
650                         rmw[1].address = F020_SFR_DAC1L;
651                         rmw[1].and_mask = 0xff;
652                         rmw[1].or_value = value & 0xff;
653
654                         /* 3 load high byte of DAC value next to latch the
655                            12-bit value */
656                         rmw[2].address = F020_SFR_DAC1H;
657                         rmw[2].and_mask = 0xff;
658                         rmw[2].or_value = (value >> 8) & 0xf;
659                         break;
660                 }
661                 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
662                 slot->usb->analog_out_shadow[channel] = value;
663         }
664         up(&slot->mutex);
665
666         return result;
667 }
668
669 /*
670  * USB framework functions
671  */
672
673 static int dt9812_probe(struct usb_interface *interface,
674                         const struct usb_device_id *id)
675 {
676         int retval = -ENOMEM;
677         struct usb_dt9812 *dev = NULL;
678         struct usb_host_interface *iface_desc;
679         struct usb_endpoint_descriptor *endpoint;
680         int i;
681         u8 fw;
682
683         /* allocate memory for our device state and initialize it */
684         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
685         if (dev == NULL)
686                 goto error;
687
688         kref_init(&dev->kref);
689
690         dev->udev = usb_get_dev(interface_to_usbdev(interface));
691         dev->interface = interface;
692
693         /* Check endpoints */
694         iface_desc = interface->cur_altsetting;
695
696         if (iface_desc->desc.bNumEndpoints != 5) {
697                 dev_err(&interface->dev, "Wrong number of endpoints.\n");
698                 retval = -ENODEV;
699                 goto error;
700         }
701
702         for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
703                 int direction = -1;
704                 endpoint = &iface_desc->endpoint[i].desc;
705                 switch (i) {
706                 case 0:
707                         direction = USB_DIR_IN;
708                         dev->message_pipe.addr = endpoint->bEndpointAddress;
709                         dev->message_pipe.size =
710                             le16_to_cpu(endpoint->wMaxPacketSize);
711
712                         break;
713                 case 1:
714                         direction = USB_DIR_OUT;
715                         dev->command_write.addr = endpoint->bEndpointAddress;
716                         dev->command_write.size =
717                             le16_to_cpu(endpoint->wMaxPacketSize);
718                         break;
719                 case 2:
720                         direction = USB_DIR_IN;
721                         dev->command_read.addr = endpoint->bEndpointAddress;
722                         dev->command_read.size =
723                             le16_to_cpu(endpoint->wMaxPacketSize);
724                         break;
725                 case 3:
726                         direction = USB_DIR_OUT;
727                         dev->write_stream.addr = endpoint->bEndpointAddress;
728                         dev->write_stream.size =
729                             le16_to_cpu(endpoint->wMaxPacketSize);
730                         break;
731                 case 4:
732                         direction = USB_DIR_IN;
733                         dev->read_stream.addr = endpoint->bEndpointAddress;
734                         dev->read_stream.size =
735                             le16_to_cpu(endpoint->wMaxPacketSize);
736                         break;
737                 }
738                 if ((endpoint->bEndpointAddress & USB_DIR_IN) != direction) {
739                         dev_err(&interface->dev,
740                                 "Endpoint has wrong direction.\n");
741                         retval = -ENODEV;
742                         goto error;
743                 }
744         }
745         if (dt9812_read_info(dev, 0, &fw, sizeof(fw)) != 0) {
746                 /*
747                  * Seems like a configuration reset is necessary if driver is
748                  * reloaded while device is attached
749                  */
750                 usb_reset_configuration(dev->udev);
751                 for (i = 0; i < 10; i++) {
752                         retval = dt9812_read_info(dev, 1, &fw, sizeof(fw));
753                         if (retval == 0) {
754                                 dev_info(&interface->dev,
755                                          "usb_reset_configuration succeeded "
756                                          "after %d iterations\n", i);
757                                 break;
758                         }
759                 }
760         }
761
762         if (dt9812_read_info(dev, 1, &dev->vendor, sizeof(dev->vendor)) != 0) {
763                 dev_err(&interface->dev, "Failed to read vendor.\n");
764                 retval = -ENODEV;
765                 goto error;
766         }
767         if (dt9812_read_info(dev, 3, &dev->product, sizeof(dev->product)) != 0) {
768                 dev_err(&interface->dev, "Failed to read product.\n");
769                 retval = -ENODEV;
770                 goto error;
771         }
772         if (dt9812_read_info(dev, 5, &dev->device, sizeof(dev->device)) != 0) {
773                 dev_err(&interface->dev, "Failed to read device.\n");
774                 retval = -ENODEV;
775                 goto error;
776         }
777         if (dt9812_read_info(dev, 7, &dev->serial, sizeof(dev->serial)) != 0) {
778                 dev_err(&interface->dev, "Failed to read serial.\n");
779                 retval = -ENODEV;
780                 goto error;
781         }
782
783         dev->vendor = le16_to_cpu(dev->vendor);
784         dev->product = le16_to_cpu(dev->product);
785         dev->device = le16_to_cpu(dev->device);
786         dev->serial = le32_to_cpu(dev->serial);
787         switch (dev->device) {
788         case DT9812_DEVID_DT9812_10:
789                 dev->analog_out_shadow[0] = 0x0800;
790                 dev->analog_out_shadow[1] = 0x800;
791                 break;
792         case DT9812_DEVID_DT9812_2PT5:
793                 dev->analog_out_shadow[0] = 0x0000;
794                 dev->analog_out_shadow[1] = 0x0000;
795                 break;
796         }
797         dev->digital_out_shadow = 0;
798
799         /* save our data pointer in this interface device */
800         usb_set_intfdata(interface, dev);
801
802         /* let the user know what node this device is now attached to */
803         dev_info(&interface->dev, "USB DT9812 (%4.4x.%4.4x.%4.4x) #0x%8.8x\n",
804                  dev->vendor, dev->product, dev->device, dev->serial);
805
806         down(&dt9812_mutex);
807         {
808                 /* Find a slot for the USB device */
809                 struct slot_dt9812 *first = NULL;
810                 struct slot_dt9812 *best = NULL;
811
812                 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
813                         if (!first && !dt9812[i].usb && dt9812[i].serial == 0)
814                                 first = &dt9812[i];
815                         if (!best && dt9812[i].serial == dev->serial)
816                                 best = &dt9812[i];
817                 }
818
819                 if (!best)
820                         best = first;
821
822                 if (best) {
823                         down(&best->mutex);
824                         best->usb = dev;
825                         dev->slot = best;
826                         up(&best->mutex);
827                 }
828         }
829         up(&dt9812_mutex);
830
831         return 0;
832
833 error:
834         if (dev)
835                 kref_put(&dev->kref, dt9812_delete);
836         return retval;
837 }
838
839 static void dt9812_disconnect(struct usb_interface *interface)
840 {
841         struct usb_dt9812 *dev;
842         int minor = interface->minor;
843
844         down(&dt9812_mutex);
845         dev = usb_get_intfdata(interface);
846         if (dev->slot) {
847                 down(&dev->slot->mutex);
848                 dev->slot->usb = NULL;
849                 up(&dev->slot->mutex);
850                 dev->slot = NULL;
851         }
852         usb_set_intfdata(interface, NULL);
853         up(&dt9812_mutex);
854
855         /* queue final destruction */
856         kref_put(&dev->kref, dt9812_delete);
857
858         dev_info(&interface->dev, "USB Dt9812 #%d now disconnected\n", minor);
859 }
860
861 static struct usb_driver dt9812_usb_driver = {
862         .name = "dt9812",
863         .probe = dt9812_probe,
864         .disconnect = dt9812_disconnect,
865         .id_table = dt9812_table,
866 };
867
868 /*
869  * Comedi functions
870  */
871
872 static int dt9812_comedi_open(struct comedi_device *dev)
873 {
874         struct comedi_dt9812 *devpriv = dev->private;
875         int result = -ENODEV;
876
877         down(&devpriv->slot->mutex);
878         if (devpriv->slot->usb) {
879                 /* We have an attached device, fill in current range info */
880                 struct comedi_subdevice *s;
881
882                 s = &dev->subdevices[0];
883                 s->n_chan = 8;
884                 s->maxdata = 1;
885
886                 s = &dev->subdevices[1];
887                 s->n_chan = 8;
888                 s->maxdata = 1;
889
890                 s = &dev->subdevices[2];
891                 s->n_chan = 8;
892                 switch (devpriv->slot->usb->device) {
893                 case 0:{
894                                 s->maxdata = 4095;
895                                 s->range_table = &range_bipolar10;
896                         }
897                         break;
898                 case 1:{
899                                 s->maxdata = 4095;
900                                 s->range_table = &range_unipolar2_5;
901                         }
902                         break;
903                 }
904
905                 s = &dev->subdevices[3];
906                 s->n_chan = 2;
907                 switch (devpriv->slot->usb->device) {
908                 case 0:{
909                                 s->maxdata = 4095;
910                                 s->range_table = &range_bipolar10;
911                         }
912                         break;
913                 case 1:{
914                                 s->maxdata = 4095;
915                                 s->range_table = &range_unipolar2_5;
916                         }
917                         break;
918                 }
919                 result = 0;
920         }
921         up(&devpriv->slot->mutex);
922         return result;
923 }
924
925 static int dt9812_di_rinsn(struct comedi_device *dev,
926                            struct comedi_subdevice *s, struct comedi_insn *insn,
927                            unsigned int *data)
928 {
929         struct comedi_dt9812 *devpriv = dev->private;
930         unsigned int channel = CR_CHAN(insn->chanspec);
931         int n;
932         u8 bits = 0;
933
934         dt9812_digital_in(devpriv->slot, &bits);
935         for (n = 0; n < insn->n; n++)
936                 data[n] = ((1 << channel) & bits) != 0;
937         return n;
938 }
939
940 static int dt9812_do_winsn(struct comedi_device *dev,
941                            struct comedi_subdevice *s, struct comedi_insn *insn,
942                            unsigned int *data)
943 {
944         struct comedi_dt9812 *devpriv = dev->private;
945         unsigned int channel = CR_CHAN(insn->chanspec);
946         int n;
947         u8 bits = 0;
948
949         dt9812_digital_out_shadow(devpriv->slot, &bits);
950         for (n = 0; n < insn->n; n++) {
951                 u8 mask = 1 << channel;
952
953                 bits &= ~mask;
954                 if (data[n])
955                         bits |= mask;
956         }
957         dt9812_digital_out(devpriv->slot, bits);
958         return n;
959 }
960
961 static int dt9812_ai_rinsn(struct comedi_device *dev,
962                            struct comedi_subdevice *s, struct comedi_insn *insn,
963                            unsigned int *data)
964 {
965         struct comedi_dt9812 *devpriv = dev->private;
966         unsigned int channel = CR_CHAN(insn->chanspec);
967         int n;
968
969         for (n = 0; n < insn->n; n++) {
970                 u16 value = 0;
971
972                 dt9812_analog_in(devpriv->slot, channel, &value, DT9812_GAIN_1);
973                 data[n] = value;
974         }
975         return n;
976 }
977
978 static int dt9812_ao_rinsn(struct comedi_device *dev,
979                            struct comedi_subdevice *s, struct comedi_insn *insn,
980                            unsigned int *data)
981 {
982         struct comedi_dt9812 *devpriv = dev->private;
983         unsigned int channel = CR_CHAN(insn->chanspec);
984         int n;
985         u16 value;
986
987         for (n = 0; n < insn->n; n++) {
988                 value = 0;
989                 dt9812_analog_out_shadow(devpriv->slot, channel, &value);
990                 data[n] = value;
991         }
992         return n;
993 }
994
995 static int dt9812_ao_winsn(struct comedi_device *dev,
996                            struct comedi_subdevice *s, struct comedi_insn *insn,
997                            unsigned int *data)
998 {
999         struct comedi_dt9812 *devpriv = dev->private;
1000         unsigned int channel = CR_CHAN(insn->chanspec);
1001         int n;
1002
1003         for (n = 0; n < insn->n; n++)
1004                 dt9812_analog_out(devpriv->slot, channel, data[n]);
1005         return n;
1006 }
1007
1008 static int dt9812_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1009 {
1010         struct comedi_dt9812 *devpriv;
1011         int i;
1012         struct comedi_subdevice *s;
1013         int ret;
1014
1015         devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1016         if (!devpriv)
1017                 return -ENOMEM;
1018         dev->private = devpriv;
1019
1020         /*
1021          * Special open routine, since USB unit may be unattached at
1022          * comedi_config time, hence range can not be determined
1023          */
1024         dev->open = dt9812_comedi_open;
1025
1026         devpriv->serial = it->options[0];
1027
1028         ret = comedi_alloc_subdevices(dev, 4);
1029         if (ret)
1030                 return ret;
1031
1032         /* digital input subdevice */
1033         s = &dev->subdevices[0];
1034         s->type = COMEDI_SUBD_DI;
1035         s->subdev_flags = SDF_READABLE;
1036         s->n_chan = 0;
1037         s->maxdata = 1;
1038         s->range_table = &range_digital;
1039         s->insn_read = &dt9812_di_rinsn;
1040
1041         /* digital output subdevice */
1042         s = &dev->subdevices[1];
1043         s->type = COMEDI_SUBD_DO;
1044         s->subdev_flags = SDF_WRITEABLE;
1045         s->n_chan = 0;
1046         s->maxdata = 1;
1047         s->range_table = &range_digital;
1048         s->insn_write = &dt9812_do_winsn;
1049
1050         /* analog input subdevice */
1051         s = &dev->subdevices[2];
1052         s->type = COMEDI_SUBD_AI;
1053         s->subdev_flags = SDF_READABLE | SDF_GROUND;
1054         s->n_chan = 0;
1055         s->maxdata = 1;
1056         s->range_table = NULL;
1057         s->insn_read = &dt9812_ai_rinsn;
1058
1059         /* analog output subdevice */
1060         s = &dev->subdevices[3];
1061         s->type = COMEDI_SUBD_AO;
1062         s->subdev_flags = SDF_WRITEABLE;
1063         s->n_chan = 0;
1064         s->maxdata = 1;
1065         s->range_table = NULL;
1066         s->insn_write = &dt9812_ao_winsn;
1067         s->insn_read = &dt9812_ao_rinsn;
1068
1069         dev_info(dev->class_dev, "successfully attached to dt9812.\n");
1070
1071         down(&dt9812_mutex);
1072         /* Find a slot for the comedi device */
1073         {
1074                 struct slot_dt9812 *first = NULL;
1075                 struct slot_dt9812 *best = NULL;
1076                 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1077                         if (!first && !dt9812[i].comedi) {
1078                                 /* First free slot from comedi side */
1079                                 first = &dt9812[i];
1080                         }
1081                         if (!best &&
1082                             dt9812[i].usb &&
1083                             dt9812[i].usb->serial == devpriv->serial) {
1084                                 /* We have an attaced device with matching ID */
1085                                 best = &dt9812[i];
1086                         }
1087                 }
1088                 if (!best)
1089                         best = first;
1090                 if (best) {
1091                         down(&best->mutex);
1092                         best->comedi = devpriv;
1093                         best->serial = devpriv->serial;
1094                         devpriv->slot = best;
1095                         up(&best->mutex);
1096                 }
1097         }
1098         up(&dt9812_mutex);
1099
1100         return 0;
1101 }
1102
1103 static void dt9812_detach(struct comedi_device *dev)
1104 {
1105         /* Nothing to cleanup */
1106 }
1107
1108 static struct comedi_driver dt9812_comedi_driver = {
1109         .module = THIS_MODULE,
1110         .driver_name = "dt9812",
1111         .attach = dt9812_attach,
1112         .detach = dt9812_detach,
1113 };
1114
1115 static int __init usb_dt9812_init(void)
1116 {
1117         int i;
1118
1119         /* Initialize all driver slots */
1120         for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1121                 sema_init(&dt9812[i].mutex, 1);
1122                 dt9812[i].serial = 0;
1123                 dt9812[i].usb = NULL;
1124                 dt9812[i].comedi = NULL;
1125         }
1126         dt9812[12].serial = 0x0;
1127
1128         return comedi_usb_driver_register(&dt9812_comedi_driver,
1129                                                 &dt9812_usb_driver);
1130 }
1131
1132 static void __exit usb_dt9812_exit(void)
1133 {
1134         comedi_usb_driver_unregister(&dt9812_comedi_driver, &dt9812_usb_driver);
1135 }
1136
1137 module_init(usb_dt9812_init);
1138 module_exit(usb_dt9812_exit);
1139
1140 MODULE_AUTHOR("Anders Blomdell <anders.blomdell@control.lth.se>");
1141 MODULE_DESCRIPTION("Comedi DT9812 driver");
1142 MODULE_LICENSE("GPL");