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.
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)
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
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
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 */
96 DT9812_DEVID_DT9813, /* 16 2 4 4 1 +/- 10V */
97 DT9812_DEVID_DT9814 /* 24 2 0 0 1 +/- 10V */
102 DT9812_GAIN_0PT25 = 1,
103 DT9812_GAIN_0PT5 = 2,
112 DT9812_LEAST_USB_FIRMWARE_CMD_CODE = 0,
113 /* Write Flash memory */
114 DT9812_W_FLASH_DATA = 0,
115 /* Read Flash memory misc config info */
116 DT9812_R_FLASH_DATA = 1,
119 * Register read/write commands for processor
122 /* Read a single byte of USB memory */
123 DT9812_R_SINGLE_BYTE_REG = 2,
124 /* Write a single byte of USB memory */
125 DT9812_W_SINGLE_BYTE_REG = 3,
126 /* Multiple Reads of USB memory */
127 DT9812_R_MULTI_BYTE_REG = 4,
128 /* Multiple Writes of USB memory */
129 DT9812_W_MULTI_BYTE_REG = 5,
130 /* Read, (AND) with mask, OR value, then write (single) */
131 DT9812_RMW_SINGLE_BYTE_REG = 6,
132 /* Read, (AND) with mask, OR value, then write (multiple) */
133 DT9812_RMW_MULTI_BYTE_REG = 7,
136 * Register read/write commands for SMBus
139 /* Read a single byte of SMBus */
140 DT9812_R_SINGLE_BYTE_SMBUS = 8,
141 /* Write a single byte of SMBus */
142 DT9812_W_SINGLE_BYTE_SMBUS = 9,
143 /* Multiple Reads of SMBus */
144 DT9812_R_MULTI_BYTE_SMBUS = 10,
145 /* Multiple Writes of SMBus */
146 DT9812_W_MULTI_BYTE_SMBUS = 11,
149 * Register read/write commands for a device
152 /* Read a single byte of a device */
153 DT9812_R_SINGLE_BYTE_DEV = 12,
154 /* Write a single byte of a device */
155 DT9812_W_SINGLE_BYTE_DEV = 13,
156 /* Multiple Reads of a device */
157 DT9812_R_MULTI_BYTE_DEV = 14,
158 /* Multiple Writes of a device */
159 DT9812_W_MULTI_BYTE_DEV = 15,
161 /* Not sure if we'll need this */
162 DT9812_W_DAC_THRESHOLD = 16,
164 /* Set interrupt on change mask */
165 DT9812_W_INT_ON_CHANGE_MASK = 17,
167 /* Write (or Clear) the CGL for the ADC */
169 /* Multiple Reads of USB memory */
170 DT9812_R_MULTI_BYTE_USBMEM = 19,
171 /* Multiple Writes to USB memory */
172 DT9812_W_MULTI_BYTE_USBMEM = 20,
174 /* Issue a start command to a given subsystem */
175 DT9812_START_SUBSYSTEM = 21,
176 /* Issue a stop command to a given subsystem */
177 DT9812_STOP_SUBSYSTEM = 22,
179 /* calibrate the board using CAL_POT_CMD */
180 DT9812_CALIBRATE_POT = 23,
181 /* set the DAC FIFO size */
182 DT9812_W_DAC_FIFO_SIZE = 24,
183 /* Write or Clear the CGL for the DAC */
184 DT9812_W_CGL_DAC = 25,
185 /* Read a single value from a subsystem */
186 DT9812_R_SINGLE_VALUE_CMD = 26,
187 /* Write a single value to a subsystem */
188 DT9812_W_SINGLE_VALUE_CMD = 27,
189 /* Valid DT9812_USB_FIRMWARE_CMD_CODE's will be less than this number */
190 DT9812_MAX_USB_FIRMWARE_CMD_CODE,
193 struct dt9812_flash_data {
198 #define DT9812_MAX_NUM_MULTI_BYTE_RDS \
199 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
201 struct dt9812_read_multi {
203 u8 address[DT9812_MAX_NUM_MULTI_BYTE_RDS];
206 struct dt9812_write_byte {
211 #define DT9812_MAX_NUM_MULTI_BYTE_WRTS \
212 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
213 sizeof(struct dt9812_write_byte))
215 struct dt9812_write_multi {
217 struct dt9812_write_byte write[DT9812_MAX_NUM_MULTI_BYTE_WRTS];
220 struct dt9812_rmw_byte {
226 #define DT9812_MAX_NUM_MULTI_BYTE_RMWS \
227 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(struct dt9812_rmw_byte))
229 struct dt9812_rmw_multi {
231 struct dt9812_rmw_byte rmw[DT9812_MAX_NUM_MULTI_BYTE_RMWS];
234 struct dt9812_usb_cmd {
237 struct dt9812_flash_data flash_data_info;
238 struct dt9812_read_multi read_multi_info;
239 struct dt9812_write_multi write_multi_info;
240 struct dt9812_rmw_multi rmw_multi_info;
243 WRITE_BYTE_INFO WriteByteInfo;
244 READ_BYTE_INFO ReadByteInfo;
245 WRITE_MULTI_INFO WriteMultiInfo;
246 READ_MULTI_INFO ReadMultiInfo;
247 RMW_BYTE_INFO RMWByteInfo;
248 RMW_MULTI_INFO RMWMultiInfo;
249 DAC_THRESHOLD_INFO DacThresholdInfo;
250 INT_ON_CHANGE_MASK_INFO IntOnChangeMaskInfo;
252 SUBSYSTEM_INFO SubsystemInfo;
253 CAL_POT_CMD CalPotCmd;
254 WRITE_DEV_BYTE_INFO WriteDevByteInfo;
255 READ_DEV_BYTE_INFO ReadDevByteInfo;
256 WRITE_DEV_MULTI_INFO WriteDevMultiInfo;
257 READ_DEV_MULTI_INFO ReadDevMultiInfo;
258 READ_SINGLE_VALUE_INFO ReadSingleValueInfo;
259 WRITE_SINGLE_VALUE_INFO WriteSingleValueInfo;
263 #define DT9812_NUM_SLOTS 16
265 static DECLARE_MUTEX(dt9812_mutex);
267 static struct usb_device_id dt9812_table[] = {
268 {USB_DEVICE(0x0867, 0x9812)},
269 { } /* Terminating entry */
272 MODULE_DEVICE_TABLE(usb, dt9812_table);
275 struct slot_dt9812 *slot;
276 struct usb_device *udev;
277 struct usb_interface *interface;
285 } message_pipe, command_write, command_read, write_stream, read_stream;
287 u16 analog_out_shadow[2];
288 u8 digital_out_shadow;
291 struct comedi_dt9812 {
292 struct slot_dt9812 *slot;
297 struct semaphore mutex;
299 struct usb_dt9812 *usb;
300 struct comedi_dt9812 *comedi;
303 static const struct comedi_lrange dt9812_10_ain_range = { 1, {
308 static const struct comedi_lrange dt9812_2pt5_ain_range = { 1, {
313 static const struct comedi_lrange dt9812_10_aout_range = { 1, {
318 static const struct comedi_lrange dt9812_2pt5_aout_range = { 1, {
323 static struct slot_dt9812 dt9812[DT9812_NUM_SLOTS];
325 /* Useful shorthand access to private data */
326 #define devpriv ((struct comedi_dt9812 *)dev->private)
328 static inline struct usb_dt9812 *to_dt9812_dev(struct kref *d)
330 return container_of(d, struct usb_dt9812, kref);
333 static void dt9812_delete(struct kref *kref)
335 struct usb_dt9812 *dev = to_dt9812_dev(kref);
337 usb_put_dev(dev->udev);
341 static int dt9812_read_info(struct usb_dt9812 *dev, int offset, void *buf,
344 struct dt9812_usb_cmd cmd;
347 cmd.cmd = cpu_to_le32(DT9812_R_FLASH_DATA);
348 cmd.u.flash_data_info.address =
349 cpu_to_le16(DT9812_DIAGS_BOARD_INFO_ADDR + offset);
350 cmd.u.flash_data_info.numbytes = cpu_to_le16(buf_size);
352 /* DT9812 only responds to 32 byte writes!! */
354 retval = usb_bulk_msg(dev->udev,
355 usb_sndbulkpipe(dev->udev,
356 dev->command_write.addr),
357 &cmd, 32, &count, HZ * 1);
360 retval = usb_bulk_msg(dev->udev,
361 usb_rcvbulkpipe(dev->udev,
362 dev->command_read.addr),
363 buf, buf_size, &count, HZ * 1);
367 static int dt9812_read_multiple_registers(struct usb_dt9812 *dev, int reg_count,
368 u8 *address, u8 *value)
370 struct dt9812_usb_cmd cmd;
371 int i, count, retval;
373 cmd.cmd = cpu_to_le32(DT9812_R_MULTI_BYTE_REG);
374 cmd.u.read_multi_info.count = reg_count;
375 for (i = 0; i < reg_count; i++)
376 cmd.u.read_multi_info.address[i] = address[i];
378 /* DT9812 only responds to 32 byte writes!! */
380 retval = usb_bulk_msg(dev->udev,
381 usb_sndbulkpipe(dev->udev,
382 dev->command_write.addr),
383 &cmd, 32, &count, HZ * 1);
386 retval = usb_bulk_msg(dev->udev,
387 usb_rcvbulkpipe(dev->udev,
388 dev->command_read.addr),
389 value, reg_count, &count, HZ * 1);
393 static int dt9812_write_multiple_registers(struct usb_dt9812 *dev,
394 int reg_count, u8 *address,
397 struct dt9812_usb_cmd cmd;
398 int i, count, retval;
400 cmd.cmd = cpu_to_le32(DT9812_W_MULTI_BYTE_REG);
401 cmd.u.read_multi_info.count = reg_count;
402 for (i = 0; i < reg_count; i++) {
403 cmd.u.write_multi_info.write[i].address = address[i];
404 cmd.u.write_multi_info.write[i].value = value[i];
406 /* DT9812 only responds to 32 byte writes!! */
407 retval = usb_bulk_msg(dev->udev,
408 usb_sndbulkpipe(dev->udev,
409 dev->command_write.addr),
410 &cmd, 32, &count, HZ * 1);
414 static int dt9812_rmw_multiple_registers(struct usb_dt9812 *dev, int reg_count,
415 struct dt9812_rmw_byte *rmw)
417 struct dt9812_usb_cmd cmd;
418 int i, count, retval;
420 cmd.cmd = cpu_to_le32(DT9812_RMW_MULTI_BYTE_REG);
421 cmd.u.rmw_multi_info.count = reg_count;
422 for (i = 0; i < reg_count; i++)
423 cmd.u.rmw_multi_info.rmw[i] = rmw[i];
425 /* DT9812 only responds to 32 byte writes!! */
426 retval = usb_bulk_msg(dev->udev,
427 usb_sndbulkpipe(dev->udev,
428 dev->command_write.addr),
429 &cmd, 32, &count, HZ * 1);
433 static int dt9812_digital_in(struct slot_dt9812 *slot, u8 *bits)
435 int result = -ENODEV;
439 u8 reg[2] = { F020_SFR_P3, F020_SFR_P1 };
442 result = dt9812_read_multiple_registers(slot->usb, 2, reg,
446 * bits 0-6 in F020_SFR_P3 are bits 0-6 in the digital
447 * input port bit 3 in F020_SFR_P1 is bit 7 in the
450 *bits = (value[0] & 0x7f) | ((value[1] & 0x08) << 4);
451 /* printk("%2.2x, %2.2x -> %2.2x\n",
452 value[0], value[1], *bits); */
460 static int dt9812_digital_out(struct slot_dt9812 *slot, u8 bits)
462 int result = -ENODEV;
469 reg[0] = F020_SFR_P2;
471 result = dt9812_write_multiple_registers(slot->usb, 1, reg,
473 slot->usb->digital_out_shadow = bits;
479 static int dt9812_digital_out_shadow(struct slot_dt9812 *slot, u8 *bits)
481 int result = -ENODEV;
485 *bits = slot->usb->digital_out_shadow;
492 static void dt9812_configure_mux(struct usb_dt9812 *dev,
493 struct dt9812_rmw_byte *rmw, int channel)
495 if (dev->device == DT9812_DEVID_DT9812_10) {
496 /* In the DT9812/10V MUX is selected by P1.5-7 */
497 rmw->address = F020_SFR_P1;
498 rmw->and_mask = 0xe0;
499 rmw->or_value = channel << 5;
501 /* In the DT9812/2.5V, internal mux is selected by bits 0:2 */
502 rmw->address = F020_SFR_AMX0SL;
503 rmw->and_mask = 0xff;
504 rmw->or_value = channel & 0x07;
508 static void dt9812_configure_gain(struct usb_dt9812 *dev,
509 struct dt9812_rmw_byte *rmw,
510 enum dt9812_gain gain)
512 if (dev->device == DT9812_DEVID_DT9812_10) {
513 /* In the DT9812/10V, there is an external gain of 0.5 */
517 rmw->address = F020_SFR_ADC0CF;
518 rmw->and_mask = F020_MASK_ADC0CF_AMP0GN2 |
519 F020_MASK_ADC0CF_AMP0GN1 |
520 F020_MASK_ADC0CF_AMP0GN0;
530 case DT9812_GAIN_0PT5:
531 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2 ||
532 F020_MASK_ADC0CF_AMP0GN1;
535 rmw->or_value = 0x00;
538 rmw->or_value = F020_MASK_ADC0CF_AMP0GN0;
541 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1;
544 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1 ||
545 F020_MASK_ADC0CF_AMP0GN0;
548 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2;
551 err("Illegal gain %d\n", gain);
556 static int dt9812_analog_in(struct slot_dt9812 *slot, int channel, u16 *value,
557 enum dt9812_gain gain)
559 struct dt9812_rmw_byte rmw[3];
566 int result = -ENODEV;
572 /* 1 select the gain */
573 dt9812_configure_gain(slot->usb, &rmw[0], gain);
575 /* 2 set the MUX to select the channel */
576 dt9812_configure_mux(slot->usb, &rmw[1], channel);
578 /* 3 start conversion */
579 rmw[2].address = F020_SFR_ADC0CN;
580 rmw[2].and_mask = 0xff;
581 rmw[2].or_value = F020_MASK_ADC0CN_AD0EN | F020_MASK_ADC0CN_AD0BUSY;
583 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
587 /* read the status and ADC */
588 result = dt9812_read_multiple_registers(slot->usb, 3, reg, val);
592 * An ADC conversion takes 16 SAR clocks cycles, i.e. about 9us.
593 * Therefore, between the instant that AD0BUSY was set via
594 * dt9812_rmw_multiple_registers and the read of AD0BUSY via
595 * dt9812_read_multiple_registers, the conversion should be complete
596 * since these two operations require two USB transactions each taking
597 * at least a millisecond to complete. However, lets make sure that
598 * conversion is finished.
600 if ((val[0] & (F020_MASK_ADC0CN_AD0INT | F020_MASK_ADC0CN_AD0BUSY)) ==
601 F020_MASK_ADC0CN_AD0INT) {
602 switch (slot->usb->device) {
603 case DT9812_DEVID_DT9812_10:
605 * For DT9812-10V the personality module set the
606 * encoding to 2's complement. Hence, convert it before
609 *value = ((val[1] << 8) | val[2]) + 0x800;
611 case DT9812_DEVID_DT9812_2PT5:
612 *value = (val[1] << 8) | val[2];
622 static int dt9812_analog_out_shadow(struct slot_dt9812 *slot, int channel,
625 int result = -ENODEV;
629 *value = slot->usb->analog_out_shadow[channel];
637 static int dt9812_analog_out(struct slot_dt9812 *slot, int channel, u16 value)
639 int result = -ENODEV;
643 struct dt9812_rmw_byte rmw[3];
647 /* 1. Set DAC mode */
648 rmw[0].address = F020_SFR_DAC0CN;
649 rmw[0].and_mask = 0xff;
650 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
652 /* 2 load low byte of DAC value first */
653 rmw[1].address = F020_SFR_DAC0L;
654 rmw[1].and_mask = 0xff;
655 rmw[1].or_value = value & 0xff;
657 /* 3 load high byte of DAC value next to latch the
659 rmw[2].address = F020_SFR_DAC0H;
660 rmw[2].and_mask = 0xff;
661 rmw[2].or_value = (value >> 8) & 0xf;
665 /* 1. Set DAC mode */
666 rmw[0].address = F020_SFR_DAC1CN;
667 rmw[0].and_mask = 0xff;
668 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
670 /* 2 load low byte of DAC value first */
671 rmw[1].address = F020_SFR_DAC1L;
672 rmw[1].and_mask = 0xff;
673 rmw[1].or_value = value & 0xff;
675 /* 3 load high byte of DAC value next to latch the
677 rmw[2].address = F020_SFR_DAC1H;
678 rmw[2].and_mask = 0xff;
679 rmw[2].or_value = (value >> 8) & 0xf;
682 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
683 slot->usb->analog_out_shadow[channel] = value;
691 * USB framework functions
694 static int dt9812_probe(struct usb_interface *interface,
695 const struct usb_device_id *id)
697 int retval = -ENOMEM;
698 struct usb_dt9812 *dev = NULL;
699 struct usb_host_interface *iface_desc;
700 struct usb_endpoint_descriptor *endpoint;
704 /* allocate memory for our device state and initialize it */
705 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
707 dev_err(&interface->dev, "Out of memory\n");
710 kref_init(&dev->kref);
712 dev->udev = usb_get_dev(interface_to_usbdev(interface));
713 dev->interface = interface;
715 /* Check endpoints */
716 iface_desc = interface->cur_altsetting;
718 if (iface_desc->desc.bNumEndpoints != 5) {
719 err("Wrong number of endpints.");
724 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
726 endpoint = &iface_desc->endpoint[i].desc;
729 direction = USB_DIR_IN;
730 dev->message_pipe.addr = endpoint->bEndpointAddress;
731 dev->message_pipe.size =
732 le16_to_cpu(endpoint->wMaxPacketSize);
736 direction = USB_DIR_OUT;
737 dev->command_write.addr = endpoint->bEndpointAddress;
738 dev->command_write.size =
739 le16_to_cpu(endpoint->wMaxPacketSize);
742 direction = USB_DIR_IN;
743 dev->command_read.addr = endpoint->bEndpointAddress;
744 dev->command_read.size =
745 le16_to_cpu(endpoint->wMaxPacketSize);
748 direction = USB_DIR_OUT;
749 dev->write_stream.addr = endpoint->bEndpointAddress;
750 dev->write_stream.size =
751 le16_to_cpu(endpoint->wMaxPacketSize);
754 direction = USB_DIR_IN;
755 dev->read_stream.addr = endpoint->bEndpointAddress;
756 dev->read_stream.size =
757 le16_to_cpu(endpoint->wMaxPacketSize);
760 if ((endpoint->bEndpointAddress & USB_DIR_IN) != direction) {
761 dev_err(&interface->dev,
762 "Endpoint has wrong direction.\n");
767 if (dt9812_read_info(dev, 0, &fw, sizeof(fw)) != 0) {
769 * Seems like a configuration reset is necessary if driver is
770 * reloaded while device is attached
772 usb_reset_configuration(dev->udev);
773 for (i = 0; i < 10; i++) {
774 retval = dt9812_read_info(dev, 1, &fw, sizeof(fw));
776 dev_info(&interface->dev,
777 "usb_reset_configuration succeded "
778 "after %d iterations\n", i);
784 if (dt9812_read_info(dev, 1, &dev->vendor, sizeof(dev->vendor)) != 0) {
785 err("Failed to read vendor.");
789 if (dt9812_read_info(dev, 3, &dev->product,
790 sizeof(dev->product)) != 0) {
791 err("Failed to read product.");
795 if (dt9812_read_info(dev, 5, &dev->device, sizeof(dev->device)) != 0) {
796 err("Failed to read device.");
800 if (dt9812_read_info(dev, 7, &dev->serial, sizeof(dev->serial)) != 0) {
801 err("Failed to read serial.");
806 dev->vendor = le16_to_cpu(dev->vendor);
807 dev->product = le16_to_cpu(dev->product);
808 dev->device = le16_to_cpu(dev->device);
809 dev->serial = le32_to_cpu(dev->serial);
810 switch (dev->device) {
811 case DT9812_DEVID_DT9812_10:
812 dev->analog_out_shadow[0] = 0x0800;
813 dev->analog_out_shadow[1] = 0x800;
815 case DT9812_DEVID_DT9812_2PT5:
816 dev->analog_out_shadow[0] = 0x0000;
817 dev->analog_out_shadow[1] = 0x0000;
820 dev->digital_out_shadow = 0;
822 /* save our data pointer in this interface device */
823 usb_set_intfdata(interface, dev);
825 /* let the user know what node this device is now attached to */
826 dev_info(&interface->dev, "USB DT9812 (%4.4x.%4.4x.%4.4x) #0x%8.8x\n",
827 dev->vendor, dev->product, dev->device, dev->serial);
831 /* Find a slot for the USB device */
832 struct slot_dt9812 *first = NULL;
833 struct slot_dt9812 *best = NULL;
835 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
836 if (!first && !dt9812[i].usb && dt9812[i].serial == 0)
838 if (!best && dt9812[i].serial == dev->serial)
858 kref_put(&dev->kref, dt9812_delete);
862 static void dt9812_disconnect(struct usb_interface *interface)
864 struct usb_dt9812 *dev;
865 int minor = interface->minor;
868 dev = usb_get_intfdata(interface);
870 down(&dev->slot->mutex);
871 dev->slot->usb = NULL;
872 up(&dev->slot->mutex);
875 usb_set_intfdata(interface, NULL);
878 /* queue final destruction */
879 kref_put(&dev->kref, dt9812_delete);
881 dev_info(&interface->dev, "USB Dt9812 #%d now disconnected\n", minor);
884 static struct usb_driver dt9812_usb_driver = {
886 .probe = dt9812_probe,
887 .disconnect = dt9812_disconnect,
888 .id_table = dt9812_table,
895 static void dt9812_comedi_open(struct comedi_device *dev)
897 down(&devpriv->slot->mutex);
898 if (devpriv->slot->usb) {
899 /* We have an attached device, fill in current range info */
900 struct comedi_subdevice *s;
902 s = &dev->subdevices[0];
906 s = &dev->subdevices[1];
910 s = &dev->subdevices[2];
912 switch (devpriv->slot->usb->device) {
915 s->range_table = &dt9812_10_ain_range;
920 s->range_table = &dt9812_2pt5_ain_range;
925 s = &dev->subdevices[3];
927 switch (devpriv->slot->usb->device) {
930 s->range_table = &dt9812_10_aout_range;
935 s->range_table = &dt9812_2pt5_aout_range;
940 up(&devpriv->slot->mutex);
943 static int dt9812_di_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
944 struct comedi_insn *insn, unsigned int *data)
949 dt9812_digital_in(devpriv->slot, &bits);
950 for (n = 0; n < insn->n; n++)
951 data[n] = ((1 << insn->chanspec) & bits) != 0;
955 static int dt9812_do_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
956 struct comedi_insn *insn, unsigned int *data)
961 dt9812_digital_out_shadow(devpriv->slot, &bits);
962 for (n = 0; n < insn->n; n++) {
963 u8 mask = 1 << insn->chanspec;
969 dt9812_digital_out(devpriv->slot, bits);
973 static int dt9812_ai_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
974 struct comedi_insn *insn, unsigned int *data)
978 for (n = 0; n < insn->n; n++) {
981 dt9812_analog_in(devpriv->slot, insn->chanspec, &value,
988 static int dt9812_ao_rinsn(struct comedi_device *dev, struct comedi_subdevice *s,
989 struct comedi_insn *insn, unsigned int *data)
994 for (n = 0; n < insn->n; n++) {
996 dt9812_analog_out_shadow(devpriv->slot, insn->chanspec, &value);
1002 static int dt9812_ao_winsn(struct comedi_device *dev, struct comedi_subdevice *s,
1003 struct comedi_insn *insn, unsigned int *data)
1007 for (n = 0; n < insn->n; n++)
1008 dt9812_analog_out(devpriv->slot, insn->chanspec, data[n]);
1012 static int dt9812_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1015 struct comedi_subdevice *s;
1017 dev->board_name = "dt9812";
1019 if (alloc_private(dev, sizeof(struct comedi_dt9812)) < 0)
1023 * Special open routine, since USB unit may be unattached at
1024 * comedi_config time, hence range can not be determined
1026 dev->open = dt9812_comedi_open;
1028 devpriv->serial = it->options[0];
1030 /* Allocate subdevices */
1031 if (alloc_subdevices(dev, 4) < 0)
1034 /* digital input subdevice */
1035 s = dev->subdevices + 0;
1036 s->type = COMEDI_SUBD_DI;
1037 s->subdev_flags = SDF_READABLE;
1040 s->range_table = &range_digital;
1041 s->insn_read = &dt9812_di_rinsn;
1043 /* digital output subdevice */
1044 s = dev->subdevices + 1;
1045 s->type = COMEDI_SUBD_DO;
1046 s->subdev_flags = SDF_WRITEABLE;
1049 s->range_table = &range_digital;
1050 s->insn_write = &dt9812_do_winsn;
1052 /* analog input subdevice */
1053 s = dev->subdevices + 2;
1054 s->type = COMEDI_SUBD_AI;
1055 s->subdev_flags = SDF_READABLE | SDF_GROUND;
1058 s->range_table = NULL;
1059 s->insn_read = &dt9812_ai_rinsn;
1061 /* analog output subdevice */
1062 s = dev->subdevices + 3;
1063 s->type = COMEDI_SUBD_AO;
1064 s->subdev_flags = SDF_WRITEABLE;
1067 s->range_table = NULL;
1068 s->insn_write = &dt9812_ao_winsn;
1069 s->insn_read = &dt9812_ao_rinsn;
1071 printk(KERN_INFO "comedi%d: successfully attached to dt9812.\n",
1074 down(&dt9812_mutex);
1075 /* Find a slot for the comedi device */
1077 struct slot_dt9812 *first = NULL;
1078 struct slot_dt9812 *best = NULL;
1079 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1080 if (!first && !dt9812[i].comedi) {
1081 /* First free slot from comedi side */
1086 dt9812[i].usb->serial == devpriv->serial) {
1087 /* We have an attaced device with matching ID */
1095 best->comedi = devpriv;
1096 best->serial = devpriv->serial;
1097 devpriv->slot = best;
1106 static int dt9812_detach(struct comedi_device *dev)
1111 static struct comedi_driver dt9812_comedi_driver = {
1112 .module = THIS_MODULE,
1113 .driver_name = "dt9812",
1114 .attach = dt9812_attach,
1115 .detach = dt9812_detach,
1118 static int __init usb_dt9812_init(void)
1122 /* Initialize all driver slots */
1123 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1124 init_MUTEX(&dt9812[i].mutex);
1125 dt9812[i].serial = 0;
1126 dt9812[i].usb = NULL;
1127 dt9812[i].comedi = NULL;
1129 dt9812[12].serial = 0x0;
1131 /* register with the USB subsystem */
1132 result = usb_register(&dt9812_usb_driver);
1134 printk(KERN_ERR KBUILD_MODNAME
1135 ": usb_register failed. Error number %d\n", result);
1138 /* register with comedi */
1139 result = comedi_driver_register(&dt9812_comedi_driver);
1141 usb_deregister(&dt9812_usb_driver);
1142 err("comedi_driver_register failed. Error number %d", result);
1148 static void __exit usb_dt9812_exit(void)
1150 /* unregister with comedi */
1151 comedi_driver_unregister(&dt9812_comedi_driver);
1153 /* deregister this driver with the USB subsystem */
1154 usb_deregister(&dt9812_usb_driver);
1157 module_init(usb_dt9812_init);
1158 module_exit(usb_dt9812_exit);
1160 MODULE_AUTHOR("Anders Blomdell <anders.blomdell@control.lth.se>");
1161 MODULE_DESCRIPTION("Comedi DT9812 driver");
1162 MODULE_LICENSE("GPL");