GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / comedi / drivers / dt9812.c
blobda37adeac9b46fd01ba4d84154131b466d02da52
1 /*
2 * comedi/drivers/dt9812.c
3 * COMEDI driver for DataTranslation DT9812 USB module
5 * Copyright (C) 2005 Anders Blomdell <anders.blomdell@control.lth.se>
7 * COMEDI - Linux Control and Measurement Device Interface
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.
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.
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.
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
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.
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).
46 #include <linux/kernel.h>
47 #include <linux/errno.h>
48 #include <linux/init.h>
49 #include <linux/slab.h>
50 #include <linux/module.h>
51 #include <linux/kref.h>
52 #include <linux/uaccess.h>
53 #include <linux/usb.h>
55 #include "../comedidev.h"
57 #define DT9812_DIAGS_BOARD_INFO_ADDR 0xFBFF
58 #define DT9812_MAX_WRITE_CMD_PIPE_SIZE 32
59 #define DT9812_MAX_READ_CMD_PIPE_SIZE 32
62 * See Silican Laboratories C8051F020/1/2/3 manual
64 #define F020_SFR_P4 0x84
65 #define F020_SFR_P1 0x90
66 #define F020_SFR_P2 0xa0
67 #define F020_SFR_P3 0xb0
68 #define F020_SFR_AMX0CF 0xba
69 #define F020_SFR_AMX0SL 0xbb
70 #define F020_SFR_ADC0CF 0xbc
71 #define F020_SFR_ADC0L 0xbe
72 #define F020_SFR_ADC0H 0xbf
73 #define F020_SFR_DAC0L 0xd2
74 #define F020_SFR_DAC0H 0xd3
75 #define F020_SFR_DAC0CN 0xd4
76 #define F020_SFR_DAC1L 0xd5
77 #define F020_SFR_DAC1H 0xd6
78 #define F020_SFR_DAC1CN 0xd7
79 #define F020_SFR_ADC0CN 0xe8
81 #define F020_MASK_ADC0CF_AMP0GN0 0x01
82 #define F020_MASK_ADC0CF_AMP0GN1 0x02
83 #define F020_MASK_ADC0CF_AMP0GN2 0x04
85 #define F020_MASK_ADC0CN_AD0EN 0x80
86 #define F020_MASK_ADC0CN_AD0INT 0x20
87 #define F020_MASK_ADC0CN_AD0BUSY 0x10
89 #define F020_MASK_DACxCN_DACxEN 0x80
91 enum {
92 /* A/D D/A DI DO CT */
93 DT9812_DEVID_DT9812_10, /* 8 2 8 8 1 +/- 10V */
94 DT9812_DEVID_DT9812_2PT5, /* 8 2 8 8 1 0-2.44V */
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,
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,
115 * Register read/write commands for processor
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,
132 * Register read/write commands for SMBus
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,
145 * Register read/write commands for a device
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,
157 /* Not sure if we'll need this */
158 DT9812_W_DAC_THRESHOLD = 16,
160 /* Set interrupt on change mask */
161 DT9812_W_INT_ON_CHANGE_MASK = 17,
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,
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,
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,
189 struct dt9812_flash_data {
190 u16 numbytes;
191 u16 address;
194 #define DT9812_MAX_NUM_MULTI_BYTE_RDS \
195 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
197 struct dt9812_read_multi {
198 u8 count;
199 u8 address[DT9812_MAX_NUM_MULTI_BYTE_RDS];
202 struct dt9812_write_byte {
203 u8 address;
204 u8 value;
207 #define DT9812_MAX_NUM_MULTI_BYTE_WRTS \
208 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
209 sizeof(struct dt9812_write_byte))
211 struct dt9812_write_multi {
212 u8 count;
213 struct dt9812_write_byte write[DT9812_MAX_NUM_MULTI_BYTE_WRTS];
216 struct dt9812_rmw_byte {
217 u8 address;
218 u8 and_mask;
219 u8 or_value;
222 #define DT9812_MAX_NUM_MULTI_BYTE_RMWS \
223 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(struct dt9812_rmw_byte))
225 struct dt9812_rmw_multi {
226 u8 count;
227 struct dt9812_rmw_byte rmw[DT9812_MAX_NUM_MULTI_BYTE_RMWS];
230 struct dt9812_usb_cmd {
231 u32 cmd;
232 union {
233 struct dt9812_flash_data flash_data_info;
234 struct dt9812_read_multi read_multi_info;
235 struct dt9812_write_multi write_multi_info;
236 struct dt9812_rmw_multi rmw_multi_info;
237 } u;
240 #define DT9812_NUM_SLOTS 16
242 static DECLARE_MUTEX(dt9812_mutex);
244 static const struct usb_device_id dt9812_table[] = {
245 {USB_DEVICE(0x0867, 0x9812)},
246 {} /* Terminating entry */
249 MODULE_DEVICE_TABLE(usb, dt9812_table);
251 struct usb_dt9812 {
252 struct slot_dt9812 *slot;
253 struct usb_device *udev;
254 struct usb_interface *interface;
255 u16 vendor;
256 u16 product;
257 u16 device;
258 u32 serial;
259 struct {
260 __u8 addr;
261 size_t size;
262 } message_pipe, command_write, command_read, write_stream, read_stream;
263 struct kref kref;
264 u16 analog_out_shadow[2];
265 u8 digital_out_shadow;
268 struct comedi_dt9812 {
269 struct slot_dt9812 *slot;
270 u32 serial;
273 struct slot_dt9812 {
274 struct semaphore mutex;
275 u32 serial;
276 struct usb_dt9812 *usb;
277 struct comedi_dt9812 *comedi;
280 static const struct comedi_lrange dt9812_10_ain_range = { 1, {
281 BIP_RANGE(10),
285 static const struct comedi_lrange dt9812_2pt5_ain_range = { 1, {
286 UNI_RANGE(2.5),
290 static const struct comedi_lrange dt9812_10_aout_range = { 1, {
291 BIP_RANGE(10),
295 static const struct comedi_lrange dt9812_2pt5_aout_range = { 1, {
296 UNI_RANGE(2.5),
300 static struct slot_dt9812 dt9812[DT9812_NUM_SLOTS];
302 /* Useful shorthand access to private data */
303 #define devpriv ((struct comedi_dt9812 *)dev->private)
305 static inline struct usb_dt9812 *to_dt9812_dev(struct kref *d)
307 return container_of(d, struct usb_dt9812, kref);
310 static void dt9812_delete(struct kref *kref)
312 struct usb_dt9812 *dev = to_dt9812_dev(kref);
314 usb_put_dev(dev->udev);
315 kfree(dev);
318 static int dt9812_read_info(struct usb_dt9812 *dev, int offset, void *buf,
319 size_t buf_size)
321 struct dt9812_usb_cmd cmd;
322 int count, retval;
324 cmd.cmd = cpu_to_le32(DT9812_R_FLASH_DATA);
325 cmd.u.flash_data_info.address =
326 cpu_to_le16(DT9812_DIAGS_BOARD_INFO_ADDR + offset);
327 cmd.u.flash_data_info.numbytes = cpu_to_le16(buf_size);
329 /* DT9812 only responds to 32 byte writes!! */
330 count = 32;
331 retval = usb_bulk_msg(dev->udev,
332 usb_sndbulkpipe(dev->udev,
333 dev->command_write.addr),
334 &cmd, 32, &count, HZ * 1);
335 if (retval)
336 return retval;
337 retval = usb_bulk_msg(dev->udev,
338 usb_rcvbulkpipe(dev->udev,
339 dev->command_read.addr),
340 buf, buf_size, &count, HZ * 1);
341 return retval;
344 static int dt9812_read_multiple_registers(struct usb_dt9812 *dev, int reg_count,
345 u8 * address, u8 * value)
347 struct dt9812_usb_cmd cmd;
348 int i, count, retval;
350 cmd.cmd = cpu_to_le32(DT9812_R_MULTI_BYTE_REG);
351 cmd.u.read_multi_info.count = reg_count;
352 for (i = 0; i < reg_count; i++)
353 cmd.u.read_multi_info.address[i] = address[i];
355 /* DT9812 only responds to 32 byte writes!! */
356 count = 32;
357 retval = usb_bulk_msg(dev->udev,
358 usb_sndbulkpipe(dev->udev,
359 dev->command_write.addr),
360 &cmd, 32, &count, HZ * 1);
361 if (retval)
362 return retval;
363 retval = usb_bulk_msg(dev->udev,
364 usb_rcvbulkpipe(dev->udev,
365 dev->command_read.addr),
366 value, reg_count, &count, HZ * 1);
367 return retval;
370 static int dt9812_write_multiple_registers(struct usb_dt9812 *dev,
371 int reg_count, u8 * address,
372 u8 * value)
374 struct dt9812_usb_cmd cmd;
375 int i, count, retval;
377 cmd.cmd = cpu_to_le32(DT9812_W_MULTI_BYTE_REG);
378 cmd.u.read_multi_info.count = reg_count;
379 for (i = 0; i < reg_count; i++) {
380 cmd.u.write_multi_info.write[i].address = address[i];
381 cmd.u.write_multi_info.write[i].value = value[i];
383 /* DT9812 only responds to 32 byte writes!! */
384 retval = usb_bulk_msg(dev->udev,
385 usb_sndbulkpipe(dev->udev,
386 dev->command_write.addr),
387 &cmd, 32, &count, HZ * 1);
388 return retval;
391 static int dt9812_rmw_multiple_registers(struct usb_dt9812 *dev, int reg_count,
392 struct dt9812_rmw_byte *rmw)
394 struct dt9812_usb_cmd cmd;
395 int i, count, retval;
397 cmd.cmd = cpu_to_le32(DT9812_RMW_MULTI_BYTE_REG);
398 cmd.u.rmw_multi_info.count = reg_count;
399 for (i = 0; i < reg_count; i++)
400 cmd.u.rmw_multi_info.rmw[i] = rmw[i];
402 /* DT9812 only responds to 32 byte writes!! */
403 retval = usb_bulk_msg(dev->udev,
404 usb_sndbulkpipe(dev->udev,
405 dev->command_write.addr),
406 &cmd, 32, &count, HZ * 1);
407 return retval;
410 static int dt9812_digital_in(struct slot_dt9812 *slot, u8 * bits)
412 int result = -ENODEV;
414 down(&slot->mutex);
415 if (slot->usb) {
416 u8 reg[2] = { F020_SFR_P3, F020_SFR_P1 };
417 u8 value[2];
419 result = dt9812_read_multiple_registers(slot->usb, 2, reg,
420 value);
421 if (result == 0) {
423 * bits 0-6 in F020_SFR_P3 are bits 0-6 in the digital
424 * input port bit 3 in F020_SFR_P1 is bit 7 in the
425 * digital input port
427 *bits = (value[0] & 0x7f) | ((value[1] & 0x08) << 4);
428 /* printk("%2.2x, %2.2x -> %2.2x\n",
429 value[0], value[1], *bits); */
432 up(&slot->mutex);
434 return result;
437 static int dt9812_digital_out(struct slot_dt9812 *slot, u8 bits)
439 int result = -ENODEV;
441 down(&slot->mutex);
442 if (slot->usb) {
443 u8 reg[1];
444 u8 value[1];
446 reg[0] = F020_SFR_P2;
447 value[0] = bits;
448 result = dt9812_write_multiple_registers(slot->usb, 1, reg,
449 value);
450 slot->usb->digital_out_shadow = bits;
452 up(&slot->mutex);
453 return result;
456 static int dt9812_digital_out_shadow(struct slot_dt9812 *slot, u8 * bits)
458 int result = -ENODEV;
460 down(&slot->mutex);
461 if (slot->usb) {
462 *bits = slot->usb->digital_out_shadow;
463 result = 0;
465 up(&slot->mutex);
466 return result;
469 static void dt9812_configure_mux(struct usb_dt9812 *dev,
470 struct dt9812_rmw_byte *rmw, int channel)
472 if (dev->device == DT9812_DEVID_DT9812_10) {
473 /* In the DT9812/10V MUX is selected by P1.5-7 */
474 rmw->address = F020_SFR_P1;
475 rmw->and_mask = 0xe0;
476 rmw->or_value = channel << 5;
477 } else {
478 /* In the DT9812/2.5V, internal mux is selected by bits 0:2 */
479 rmw->address = F020_SFR_AMX0SL;
480 rmw->and_mask = 0xff;
481 rmw->or_value = channel & 0x07;
485 static void dt9812_configure_gain(struct usb_dt9812 *dev,
486 struct dt9812_rmw_byte *rmw,
487 enum dt9812_gain gain)
489 if (dev->device == DT9812_DEVID_DT9812_10) {
490 /* In the DT9812/10V, there is an external gain of 0.5 */
491 gain <<= 1;
494 rmw->address = F020_SFR_ADC0CF;
495 rmw->and_mask = F020_MASK_ADC0CF_AMP0GN2 |
496 F020_MASK_ADC0CF_AMP0GN1 | F020_MASK_ADC0CF_AMP0GN0;
497 switch (gain) {
499 * 000 -> Gain = 1
500 * 001 -> Gain = 2
501 * 010 -> Gain = 4
502 * 011 -> Gain = 8
503 * 10x -> Gain = 16
504 * 11x -> Gain = 0.5
506 case DT9812_GAIN_0PT5:
507 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2 ||
508 F020_MASK_ADC0CF_AMP0GN1;
509 break;
510 case DT9812_GAIN_1:
511 rmw->or_value = 0x00;
512 break;
513 case DT9812_GAIN_2:
514 rmw->or_value = F020_MASK_ADC0CF_AMP0GN0;
515 break;
516 case DT9812_GAIN_4:
517 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1;
518 break;
519 case DT9812_GAIN_8:
520 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1 ||
521 F020_MASK_ADC0CF_AMP0GN0;
522 break;
523 case DT9812_GAIN_16:
524 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2;
525 break;
526 default:
527 err("Illegal gain %d\n", gain);
532 static int dt9812_analog_in(struct slot_dt9812 *slot, int channel, u16 * value,
533 enum dt9812_gain gain)
535 struct dt9812_rmw_byte rmw[3];
536 u8 reg[3] = {
537 F020_SFR_ADC0CN,
538 F020_SFR_ADC0H,
539 F020_SFR_ADC0L
541 u8 val[3];
542 int result = -ENODEV;
544 down(&slot->mutex);
545 if (!slot->usb)
546 goto exit;
548 /* 1 select the gain */
549 dt9812_configure_gain(slot->usb, &rmw[0], gain);
551 /* 2 set the MUX to select the channel */
552 dt9812_configure_mux(slot->usb, &rmw[1], channel);
554 /* 3 start conversion */
555 rmw[2].address = F020_SFR_ADC0CN;
556 rmw[2].and_mask = 0xff;
557 rmw[2].or_value = F020_MASK_ADC0CN_AD0EN | F020_MASK_ADC0CN_AD0BUSY;
559 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
560 if (result)
561 goto exit;
563 /* read the status and ADC */
564 result = dt9812_read_multiple_registers(slot->usb, 3, reg, val);
565 if (result)
566 goto exit;
568 * An ADC conversion takes 16 SAR clocks cycles, i.e. about 9us.
569 * Therefore, between the instant that AD0BUSY was set via
570 * dt9812_rmw_multiple_registers and the read of AD0BUSY via
571 * dt9812_read_multiple_registers, the conversion should be complete
572 * since these two operations require two USB transactions each taking
573 * at least a millisecond to complete. However, lets make sure that
574 * conversion is finished.
576 if ((val[0] & (F020_MASK_ADC0CN_AD0INT | F020_MASK_ADC0CN_AD0BUSY)) ==
577 F020_MASK_ADC0CN_AD0INT) {
578 switch (slot->usb->device) {
579 case DT9812_DEVID_DT9812_10:
581 * For DT9812-10V the personality module set the
582 * encoding to 2's complement. Hence, convert it before
583 * returning it
585 *value = ((val[1] << 8) | val[2]) + 0x800;
586 break;
587 case DT9812_DEVID_DT9812_2PT5:
588 *value = (val[1] << 8) | val[2];
589 break;
593 exit:
594 up(&slot->mutex);
595 return result;
598 static int dt9812_analog_out_shadow(struct slot_dt9812 *slot, int channel,
599 u16 * value)
601 int result = -ENODEV;
603 down(&slot->mutex);
604 if (slot->usb) {
605 *value = slot->usb->analog_out_shadow[channel];
606 result = 0;
608 up(&slot->mutex);
610 return result;
613 static int dt9812_analog_out(struct slot_dt9812 *slot, int channel, u16 value)
615 int result = -ENODEV;
617 down(&slot->mutex);
618 if (slot->usb) {
619 struct dt9812_rmw_byte rmw[3];
621 switch (channel) {
622 case 0:
623 /* 1. Set DAC mode */
624 rmw[0].address = F020_SFR_DAC0CN;
625 rmw[0].and_mask = 0xff;
626 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
628 /* 2 load low byte of DAC value first */
629 rmw[1].address = F020_SFR_DAC0L;
630 rmw[1].and_mask = 0xff;
631 rmw[1].or_value = value & 0xff;
633 /* 3 load high byte of DAC value next to latch the
634 12-bit value */
635 rmw[2].address = F020_SFR_DAC0H;
636 rmw[2].and_mask = 0xff;
637 rmw[2].or_value = (value >> 8) & 0xf;
638 break;
640 case 1:
641 /* 1. Set DAC mode */
642 rmw[0].address = F020_SFR_DAC1CN;
643 rmw[0].and_mask = 0xff;
644 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
646 /* 2 load low byte of DAC value first */
647 rmw[1].address = F020_SFR_DAC1L;
648 rmw[1].and_mask = 0xff;
649 rmw[1].or_value = value & 0xff;
651 /* 3 load high byte of DAC value next to latch the
652 12-bit value */
653 rmw[2].address = F020_SFR_DAC1H;
654 rmw[2].and_mask = 0xff;
655 rmw[2].or_value = (value >> 8) & 0xf;
656 break;
658 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
659 slot->usb->analog_out_shadow[channel] = value;
661 up(&slot->mutex);
663 return result;
667 * USB framework functions
670 static int dt9812_probe(struct usb_interface *interface,
671 const struct usb_device_id *id)
673 int retval = -ENOMEM;
674 struct usb_dt9812 *dev = NULL;
675 struct usb_host_interface *iface_desc;
676 struct usb_endpoint_descriptor *endpoint;
677 int i;
678 u8 fw;
680 /* allocate memory for our device state and initialize it */
681 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
682 if (dev == NULL) {
683 dev_err(&interface->dev, "Out of memory\n");
684 goto error;
686 kref_init(&dev->kref);
688 dev->udev = usb_get_dev(interface_to_usbdev(interface));
689 dev->interface = interface;
691 /* Check endpoints */
692 iface_desc = interface->cur_altsetting;
694 if (iface_desc->desc.bNumEndpoints != 5) {
695 err("Wrong number of endpints.");
696 retval = -ENODEV;
697 goto error;
700 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
701 int direction = -1;
702 endpoint = &iface_desc->endpoint[i].desc;
703 switch (i) {
704 case 0:
705 direction = USB_DIR_IN;
706 dev->message_pipe.addr = endpoint->bEndpointAddress;
707 dev->message_pipe.size =
708 le16_to_cpu(endpoint->wMaxPacketSize);
710 break;
711 case 1:
712 direction = USB_DIR_OUT;
713 dev->command_write.addr = endpoint->bEndpointAddress;
714 dev->command_write.size =
715 le16_to_cpu(endpoint->wMaxPacketSize);
716 break;
717 case 2:
718 direction = USB_DIR_IN;
719 dev->command_read.addr = endpoint->bEndpointAddress;
720 dev->command_read.size =
721 le16_to_cpu(endpoint->wMaxPacketSize);
722 break;
723 case 3:
724 direction = USB_DIR_OUT;
725 dev->write_stream.addr = endpoint->bEndpointAddress;
726 dev->write_stream.size =
727 le16_to_cpu(endpoint->wMaxPacketSize);
728 break;
729 case 4:
730 direction = USB_DIR_IN;
731 dev->read_stream.addr = endpoint->bEndpointAddress;
732 dev->read_stream.size =
733 le16_to_cpu(endpoint->wMaxPacketSize);
734 break;
736 if ((endpoint->bEndpointAddress & USB_DIR_IN) != direction) {
737 dev_err(&interface->dev,
738 "Endpoint has wrong direction.\n");
739 retval = -ENODEV;
740 goto error;
743 if (dt9812_read_info(dev, 0, &fw, sizeof(fw)) != 0) {
745 * Seems like a configuration reset is necessary if driver is
746 * reloaded while device is attached
748 usb_reset_configuration(dev->udev);
749 for (i = 0; i < 10; i++) {
750 retval = dt9812_read_info(dev, 1, &fw, sizeof(fw));
751 if (retval == 0) {
752 dev_info(&interface->dev,
753 "usb_reset_configuration succeded "
754 "after %d iterations\n", i);
755 break;
760 if (dt9812_read_info(dev, 1, &dev->vendor, sizeof(dev->vendor)) != 0) {
761 err("Failed to read vendor.");
762 retval = -ENODEV;
763 goto error;
765 if (dt9812_read_info(dev, 3, &dev->product, sizeof(dev->product)) != 0) {
766 err("Failed to read product.");
767 retval = -ENODEV;
768 goto error;
770 if (dt9812_read_info(dev, 5, &dev->device, sizeof(dev->device)) != 0) {
771 err("Failed to read device.");
772 retval = -ENODEV;
773 goto error;
775 if (dt9812_read_info(dev, 7, &dev->serial, sizeof(dev->serial)) != 0) {
776 err("Failed to read serial.");
777 retval = -ENODEV;
778 goto error;
781 dev->vendor = le16_to_cpu(dev->vendor);
782 dev->product = le16_to_cpu(dev->product);
783 dev->device = le16_to_cpu(dev->device);
784 dev->serial = le32_to_cpu(dev->serial);
785 switch (dev->device) {
786 case DT9812_DEVID_DT9812_10:
787 dev->analog_out_shadow[0] = 0x0800;
788 dev->analog_out_shadow[1] = 0x800;
789 break;
790 case DT9812_DEVID_DT9812_2PT5:
791 dev->analog_out_shadow[0] = 0x0000;
792 dev->analog_out_shadow[1] = 0x0000;
793 break;
795 dev->digital_out_shadow = 0;
797 /* save our data pointer in this interface device */
798 usb_set_intfdata(interface, dev);
800 /* let the user know what node this device is now attached to */
801 dev_info(&interface->dev, "USB DT9812 (%4.4x.%4.4x.%4.4x) #0x%8.8x\n",
802 dev->vendor, dev->product, dev->device, dev->serial);
804 down(&dt9812_mutex);
806 /* Find a slot for the USB device */
807 struct slot_dt9812 *first = NULL;
808 struct slot_dt9812 *best = NULL;
810 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
811 if (!first && !dt9812[i].usb && dt9812[i].serial == 0)
812 first = &dt9812[i];
813 if (!best && dt9812[i].serial == dev->serial)
814 best = &dt9812[i];
817 if (!best)
818 best = first;
820 if (best) {
821 down(&best->mutex);
822 best->usb = dev;
823 dev->slot = best;
824 up(&best->mutex);
827 up(&dt9812_mutex);
829 return 0;
831 error:
832 if (dev)
833 kref_put(&dev->kref, dt9812_delete);
834 return retval;
837 static void dt9812_disconnect(struct usb_interface *interface)
839 struct usb_dt9812 *dev;
840 int minor = interface->minor;
842 down(&dt9812_mutex);
843 dev = usb_get_intfdata(interface);
844 if (dev->slot) {
845 down(&dev->slot->mutex);
846 dev->slot->usb = NULL;
847 up(&dev->slot->mutex);
848 dev->slot = NULL;
850 usb_set_intfdata(interface, NULL);
851 up(&dt9812_mutex);
853 /* queue final destruction */
854 kref_put(&dev->kref, dt9812_delete);
856 dev_info(&interface->dev, "USB Dt9812 #%d now disconnected\n", minor);
859 static struct usb_driver dt9812_usb_driver = {
860 .name = "dt9812",
861 .probe = dt9812_probe,
862 .disconnect = dt9812_disconnect,
863 .id_table = dt9812_table,
867 * Comedi functions
870 static int dt9812_comedi_open(struct comedi_device *dev)
872 int result = -ENODEV;
874 down(&devpriv->slot->mutex);
875 if (devpriv->slot->usb) {
876 /* We have an attached device, fill in current range info */
877 struct comedi_subdevice *s;
879 s = &dev->subdevices[0];
880 s->n_chan = 8;
881 s->maxdata = 1;
883 s = &dev->subdevices[1];
884 s->n_chan = 8;
885 s->maxdata = 1;
887 s = &dev->subdevices[2];
888 s->n_chan = 8;
889 switch (devpriv->slot->usb->device) {
890 case 0:{
891 s->maxdata = 4095;
892 s->range_table = &dt9812_10_ain_range;
894 break;
895 case 1:{
896 s->maxdata = 4095;
897 s->range_table = &dt9812_2pt5_ain_range;
899 break;
902 s = &dev->subdevices[3];
903 s->n_chan = 2;
904 switch (devpriv->slot->usb->device) {
905 case 0:{
906 s->maxdata = 4095;
907 s->range_table = &dt9812_10_aout_range;
909 break;
910 case 1:{
911 s->maxdata = 4095;
912 s->range_table = &dt9812_2pt5_aout_range;
914 break;
916 result = 0;
918 up(&devpriv->slot->mutex);
919 return result;
922 static int dt9812_di_rinsn(struct comedi_device *dev,
923 struct comedi_subdevice *s, struct comedi_insn *insn,
924 unsigned int *data)
926 int n;
927 u8 bits = 0;
929 dt9812_digital_in(devpriv->slot, &bits);
930 for (n = 0; n < insn->n; n++)
931 data[n] = ((1 << insn->chanspec) & bits) != 0;
932 return n;
935 static int dt9812_do_winsn(struct comedi_device *dev,
936 struct comedi_subdevice *s, struct comedi_insn *insn,
937 unsigned int *data)
939 int n;
940 u8 bits = 0;
942 dt9812_digital_out_shadow(devpriv->slot, &bits);
943 for (n = 0; n < insn->n; n++) {
944 u8 mask = 1 << insn->chanspec;
946 bits &= ~mask;
947 if (data[n])
948 bits |= mask;
950 dt9812_digital_out(devpriv->slot, bits);
951 return n;
954 static int dt9812_ai_rinsn(struct comedi_device *dev,
955 struct comedi_subdevice *s, struct comedi_insn *insn,
956 unsigned int *data)
958 int n;
960 for (n = 0; n < insn->n; n++) {
961 u16 value = 0;
963 dt9812_analog_in(devpriv->slot, insn->chanspec, &value,
964 DT9812_GAIN_1);
965 data[n] = value;
967 return n;
970 static int dt9812_ao_rinsn(struct comedi_device *dev,
971 struct comedi_subdevice *s, struct comedi_insn *insn,
972 unsigned int *data)
974 int n;
975 u16 value;
977 for (n = 0; n < insn->n; n++) {
978 value = 0;
979 dt9812_analog_out_shadow(devpriv->slot, insn->chanspec, &value);
980 data[n] = value;
982 return n;
985 static int dt9812_ao_winsn(struct comedi_device *dev,
986 struct comedi_subdevice *s, struct comedi_insn *insn,
987 unsigned int *data)
989 int n;
991 for (n = 0; n < insn->n; n++)
992 dt9812_analog_out(devpriv->slot, insn->chanspec, data[n]);
993 return n;
996 static int dt9812_attach(struct comedi_device *dev, struct comedi_devconfig *it)
998 int i;
999 struct comedi_subdevice *s;
1001 dev->board_name = "dt9812";
1003 if (alloc_private(dev, sizeof(struct comedi_dt9812)) < 0)
1004 return -ENOMEM;
1007 * Special open routine, since USB unit may be unattached at
1008 * comedi_config time, hence range can not be determined
1010 dev->open = dt9812_comedi_open;
1012 devpriv->serial = it->options[0];
1014 /* Allocate subdevices */
1015 if (alloc_subdevices(dev, 4) < 0)
1016 return -ENOMEM;
1018 /* digital input subdevice */
1019 s = dev->subdevices + 0;
1020 s->type = COMEDI_SUBD_DI;
1021 s->subdev_flags = SDF_READABLE;
1022 s->n_chan = 0;
1023 s->maxdata = 1;
1024 s->range_table = &range_digital;
1025 s->insn_read = &dt9812_di_rinsn;
1027 /* digital output subdevice */
1028 s = dev->subdevices + 1;
1029 s->type = COMEDI_SUBD_DO;
1030 s->subdev_flags = SDF_WRITEABLE;
1031 s->n_chan = 0;
1032 s->maxdata = 1;
1033 s->range_table = &range_digital;
1034 s->insn_write = &dt9812_do_winsn;
1036 /* analog input subdevice */
1037 s = dev->subdevices + 2;
1038 s->type = COMEDI_SUBD_AI;
1039 s->subdev_flags = SDF_READABLE | SDF_GROUND;
1040 s->n_chan = 0;
1041 s->maxdata = 1;
1042 s->range_table = NULL;
1043 s->insn_read = &dt9812_ai_rinsn;
1045 /* analog output subdevice */
1046 s = dev->subdevices + 3;
1047 s->type = COMEDI_SUBD_AO;
1048 s->subdev_flags = SDF_WRITEABLE;
1049 s->n_chan = 0;
1050 s->maxdata = 1;
1051 s->range_table = NULL;
1052 s->insn_write = &dt9812_ao_winsn;
1053 s->insn_read = &dt9812_ao_rinsn;
1055 printk(KERN_INFO "comedi%d: successfully attached to dt9812.\n",
1056 dev->minor);
1058 down(&dt9812_mutex);
1059 /* Find a slot for the comedi device */
1061 struct slot_dt9812 *first = NULL;
1062 struct slot_dt9812 *best = NULL;
1063 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1064 if (!first && !dt9812[i].comedi) {
1065 /* First free slot from comedi side */
1066 first = &dt9812[i];
1068 if (!best &&
1069 dt9812[i].usb &&
1070 dt9812[i].usb->serial == devpriv->serial) {
1071 /* We have an attaced device with matching ID */
1072 best = &dt9812[i];
1075 if (!best)
1076 best = first;
1077 if (best) {
1078 down(&best->mutex);
1079 best->comedi = devpriv;
1080 best->serial = devpriv->serial;
1081 devpriv->slot = best;
1082 up(&best->mutex);
1085 up(&dt9812_mutex);
1087 return 0;
1090 static int dt9812_detach(struct comedi_device *dev)
1092 return 0;
1095 static struct comedi_driver dt9812_comedi_driver = {
1096 .module = THIS_MODULE,
1097 .driver_name = "dt9812",
1098 .attach = dt9812_attach,
1099 .detach = dt9812_detach,
1102 static int __init usb_dt9812_init(void)
1104 int result, i;
1106 /* Initialize all driver slots */
1107 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1108 init_MUTEX(&dt9812[i].mutex);
1109 dt9812[i].serial = 0;
1110 dt9812[i].usb = NULL;
1111 dt9812[i].comedi = NULL;
1113 dt9812[12].serial = 0x0;
1115 /* register with the USB subsystem */
1116 result = usb_register(&dt9812_usb_driver);
1117 if (result) {
1118 printk(KERN_ERR KBUILD_MODNAME
1119 ": usb_register failed. Error number %d\n", result);
1120 return result;
1122 /* register with comedi */
1123 result = comedi_driver_register(&dt9812_comedi_driver);
1124 if (result) {
1125 usb_deregister(&dt9812_usb_driver);
1126 err("comedi_driver_register failed. Error number %d", result);
1129 return result;
1132 static void __exit usb_dt9812_exit(void)
1134 /* unregister with comedi */
1135 comedi_driver_unregister(&dt9812_comedi_driver);
1137 /* deregister this driver with the USB subsystem */
1138 usb_deregister(&dt9812_usb_driver);
1141 module_init(usb_dt9812_init);
1142 module_exit(usb_dt9812_exit);
1144 MODULE_AUTHOR("Anders Blomdell <anders.blomdell@control.lth.se>");
1145 MODULE_DESCRIPTION("Comedi DT9812 driver");
1146 MODULE_LICENSE("GPL");