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 */
98 DT9812_GAIN_0PT25
= 1,
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 */
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
{
194 #define DT9812_MAX_NUM_MULTI_BYTE_RDS \
195 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
197 struct dt9812_read_multi
{
199 u8 address
[DT9812_MAX_NUM_MULTI_BYTE_RDS
];
202 struct dt9812_write_byte
{
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
{
213 struct dt9812_write_byte write
[DT9812_MAX_NUM_MULTI_BYTE_WRTS
];
216 struct dt9812_rmw_byte
{
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
{
227 struct dt9812_rmw_byte rmw
[DT9812_MAX_NUM_MULTI_BYTE_RMWS
];
230 struct dt9812_usb_cmd
{
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
;
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
);
252 struct slot_dt9812
*slot
;
253 struct usb_device
*udev
;
254 struct usb_interface
*interface
;
262 } message_pipe
, command_write
, command_read
, write_stream
, read_stream
;
264 u16 analog_out_shadow
[2];
265 u8 digital_out_shadow
;
268 struct comedi_dt9812
{
269 struct slot_dt9812
*slot
;
274 struct semaphore mutex
;
276 struct usb_dt9812
*usb
;
277 struct comedi_dt9812
*comedi
;
280 static const struct comedi_lrange dt9812_10_ain_range
= { 1, {
285 static const struct comedi_lrange dt9812_2pt5_ain_range
= { 1, {
290 static const struct comedi_lrange dt9812_10_aout_range
= { 1, {
295 static const struct comedi_lrange dt9812_2pt5_aout_range
= { 1, {
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
);
318 static int dt9812_read_info(struct usb_dt9812
*dev
, int offset
, void *buf
,
321 struct dt9812_usb_cmd cmd
;
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!! */
331 retval
= usb_bulk_msg(dev
->udev
,
332 usb_sndbulkpipe(dev
->udev
,
333 dev
->command_write
.addr
),
334 &cmd
, 32, &count
, HZ
* 1);
337 retval
= usb_bulk_msg(dev
->udev
,
338 usb_rcvbulkpipe(dev
->udev
,
339 dev
->command_read
.addr
),
340 buf
, buf_size
, &count
, HZ
* 1);
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!! */
357 retval
= usb_bulk_msg(dev
->udev
,
358 usb_sndbulkpipe(dev
->udev
,
359 dev
->command_write
.addr
),
360 &cmd
, 32, &count
, HZ
* 1);
363 retval
= usb_bulk_msg(dev
->udev
,
364 usb_rcvbulkpipe(dev
->udev
,
365 dev
->command_read
.addr
),
366 value
, reg_count
, &count
, HZ
* 1);
370 static int dt9812_write_multiple_registers(struct usb_dt9812
*dev
,
371 int reg_count
, u8
* address
,
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);
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);
410 static int dt9812_digital_in(struct slot_dt9812
*slot
, u8
* bits
)
412 int result
= -ENODEV
;
416 u8 reg
[2] = { F020_SFR_P3
, F020_SFR_P1
};
419 result
= dt9812_read_multiple_registers(slot
->usb
, 2, reg
,
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
427 *bits
= (value
[0] & 0x7f) | ((value
[1] & 0x08) << 4);
428 /* printk("%2.2x, %2.2x -> %2.2x\n",
429 value[0], value[1], *bits); */
437 static int dt9812_digital_out(struct slot_dt9812
*slot
, u8 bits
)
439 int result
= -ENODEV
;
446 reg
[0] = F020_SFR_P2
;
448 result
= dt9812_write_multiple_registers(slot
->usb
, 1, reg
,
450 slot
->usb
->digital_out_shadow
= bits
;
456 static int dt9812_digital_out_shadow(struct slot_dt9812
*slot
, u8
* bits
)
458 int result
= -ENODEV
;
462 *bits
= slot
->usb
->digital_out_shadow
;
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;
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 */
494 rmw
->address
= F020_SFR_ADC0CF
;
495 rmw
->and_mask
= F020_MASK_ADC0CF_AMP0GN2
|
496 F020_MASK_ADC0CF_AMP0GN1
| F020_MASK_ADC0CF_AMP0GN0
;
506 case DT9812_GAIN_0PT5
:
507 rmw
->or_value
= F020_MASK_ADC0CF_AMP0GN2
||
508 F020_MASK_ADC0CF_AMP0GN1
;
511 rmw
->or_value
= 0x00;
514 rmw
->or_value
= F020_MASK_ADC0CF_AMP0GN0
;
517 rmw
->or_value
= F020_MASK_ADC0CF_AMP0GN1
;
520 rmw
->or_value
= F020_MASK_ADC0CF_AMP0GN1
||
521 F020_MASK_ADC0CF_AMP0GN0
;
524 rmw
->or_value
= F020_MASK_ADC0CF_AMP0GN2
;
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];
542 int result
= -ENODEV
;
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
);
563 /* read the status and ADC */
564 result
= dt9812_read_multiple_registers(slot
->usb
, 3, reg
, val
);
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
585 *value
= ((val
[1] << 8) | val
[2]) + 0x800;
587 case DT9812_DEVID_DT9812_2PT5
:
588 *value
= (val
[1] << 8) | val
[2];
598 static int dt9812_analog_out_shadow(struct slot_dt9812
*slot
, int channel
,
601 int result
= -ENODEV
;
605 *value
= slot
->usb
->analog_out_shadow
[channel
];
613 static int dt9812_analog_out(struct slot_dt9812
*slot
, int channel
, u16 value
)
615 int result
= -ENODEV
;
619 struct dt9812_rmw_byte rmw
[3];
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
635 rmw
[2].address
= F020_SFR_DAC0H
;
636 rmw
[2].and_mask
= 0xff;
637 rmw
[2].or_value
= (value
>> 8) & 0xf;
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
653 rmw
[2].address
= F020_SFR_DAC1H
;
654 rmw
[2].and_mask
= 0xff;
655 rmw
[2].or_value
= (value
>> 8) & 0xf;
658 result
= dt9812_rmw_multiple_registers(slot
->usb
, 3, rmw
);
659 slot
->usb
->analog_out_shadow
[channel
] = value
;
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
;
680 /* allocate memory for our device state and initialize it */
681 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
683 dev_err(&interface
->dev
, "Out of memory\n");
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.");
700 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
) {
702 endpoint
= &iface_desc
->endpoint
[i
].desc
;
705 direction
= USB_DIR_IN
;
706 dev
->message_pipe
.addr
= endpoint
->bEndpointAddress
;
707 dev
->message_pipe
.size
=
708 le16_to_cpu(endpoint
->wMaxPacketSize
);
712 direction
= USB_DIR_OUT
;
713 dev
->command_write
.addr
= endpoint
->bEndpointAddress
;
714 dev
->command_write
.size
=
715 le16_to_cpu(endpoint
->wMaxPacketSize
);
718 direction
= USB_DIR_IN
;
719 dev
->command_read
.addr
= endpoint
->bEndpointAddress
;
720 dev
->command_read
.size
=
721 le16_to_cpu(endpoint
->wMaxPacketSize
);
724 direction
= USB_DIR_OUT
;
725 dev
->write_stream
.addr
= endpoint
->bEndpointAddress
;
726 dev
->write_stream
.size
=
727 le16_to_cpu(endpoint
->wMaxPacketSize
);
730 direction
= USB_DIR_IN
;
731 dev
->read_stream
.addr
= endpoint
->bEndpointAddress
;
732 dev
->read_stream
.size
=
733 le16_to_cpu(endpoint
->wMaxPacketSize
);
736 if ((endpoint
->bEndpointAddress
& USB_DIR_IN
) != direction
) {
737 dev_err(&interface
->dev
,
738 "Endpoint has wrong direction.\n");
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
));
752 dev_info(&interface
->dev
,
753 "usb_reset_configuration succeded "
754 "after %d iterations\n", i
);
760 if (dt9812_read_info(dev
, 1, &dev
->vendor
, sizeof(dev
->vendor
)) != 0) {
761 err("Failed to read vendor.");
765 if (dt9812_read_info(dev
, 3, &dev
->product
, sizeof(dev
->product
)) != 0) {
766 err("Failed to read product.");
770 if (dt9812_read_info(dev
, 5, &dev
->device
, sizeof(dev
->device
)) != 0) {
771 err("Failed to read device.");
775 if (dt9812_read_info(dev
, 7, &dev
->serial
, sizeof(dev
->serial
)) != 0) {
776 err("Failed to read serial.");
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;
790 case DT9812_DEVID_DT9812_2PT5
:
791 dev
->analog_out_shadow
[0] = 0x0000;
792 dev
->analog_out_shadow
[1] = 0x0000;
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
);
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)
813 if (!best
&& dt9812
[i
].serial
== dev
->serial
)
833 kref_put(&dev
->kref
, dt9812_delete
);
837 static void dt9812_disconnect(struct usb_interface
*interface
)
839 struct usb_dt9812
*dev
;
840 int minor
= interface
->minor
;
843 dev
= usb_get_intfdata(interface
);
845 down(&dev
->slot
->mutex
);
846 dev
->slot
->usb
= NULL
;
847 up(&dev
->slot
->mutex
);
850 usb_set_intfdata(interface
, NULL
);
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
= {
861 .probe
= dt9812_probe
,
862 .disconnect
= dt9812_disconnect
,
863 .id_table
= dt9812_table
,
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];
883 s
= &dev
->subdevices
[1];
887 s
= &dev
->subdevices
[2];
889 switch (devpriv
->slot
->usb
->device
) {
892 s
->range_table
= &dt9812_10_ain_range
;
897 s
->range_table
= &dt9812_2pt5_ain_range
;
902 s
= &dev
->subdevices
[3];
904 switch (devpriv
->slot
->usb
->device
) {
907 s
->range_table
= &dt9812_10_aout_range
;
912 s
->range_table
= &dt9812_2pt5_aout_range
;
918 up(&devpriv
->slot
->mutex
);
922 static int dt9812_di_rinsn(struct comedi_device
*dev
,
923 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
929 dt9812_digital_in(devpriv
->slot
, &bits
);
930 for (n
= 0; n
< insn
->n
; n
++)
931 data
[n
] = ((1 << insn
->chanspec
) & bits
) != 0;
935 static int dt9812_do_winsn(struct comedi_device
*dev
,
936 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
942 dt9812_digital_out_shadow(devpriv
->slot
, &bits
);
943 for (n
= 0; n
< insn
->n
; n
++) {
944 u8 mask
= 1 << insn
->chanspec
;
950 dt9812_digital_out(devpriv
->slot
, bits
);
954 static int dt9812_ai_rinsn(struct comedi_device
*dev
,
955 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
960 for (n
= 0; n
< insn
->n
; n
++) {
963 dt9812_analog_in(devpriv
->slot
, insn
->chanspec
, &value
,
970 static int dt9812_ao_rinsn(struct comedi_device
*dev
,
971 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
977 for (n
= 0; n
< insn
->n
; n
++) {
979 dt9812_analog_out_shadow(devpriv
->slot
, insn
->chanspec
, &value
);
985 static int dt9812_ao_winsn(struct comedi_device
*dev
,
986 struct comedi_subdevice
*s
, struct comedi_insn
*insn
,
991 for (n
= 0; n
< insn
->n
; n
++)
992 dt9812_analog_out(devpriv
->slot
, insn
->chanspec
, data
[n
]);
996 static int dt9812_attach(struct comedi_device
*dev
, struct comedi_devconfig
*it
)
999 struct comedi_subdevice
*s
;
1001 dev
->board_name
= "dt9812";
1003 if (alloc_private(dev
, sizeof(struct comedi_dt9812
)) < 0)
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)
1018 /* digital input subdevice */
1019 s
= dev
->subdevices
+ 0;
1020 s
->type
= COMEDI_SUBD_DI
;
1021 s
->subdev_flags
= SDF_READABLE
;
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
;
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
;
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
;
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",
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 */
1070 dt9812
[i
].usb
->serial
== devpriv
->serial
) {
1071 /* We have an attaced device with matching ID */
1079 best
->comedi
= devpriv
;
1080 best
->serial
= devpriv
->serial
;
1081 devpriv
->slot
= best
;
1090 static int dt9812_detach(struct comedi_device
*dev
)
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)
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
);
1118 printk(KERN_ERR KBUILD_MODNAME
1119 ": usb_register failed. Error number %d\n", result
);
1122 /* register with comedi */
1123 result
= comedi_driver_register(&dt9812_comedi_driver
);
1125 usb_deregister(&dt9812_usb_driver
);
1126 err("comedi_driver_register failed. Error number %d", 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");