1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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>
54
55#include "../comedidev.h"
56
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
60
61
62
63
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
80
81#define F020_MASK_ADC0CF_AMP0GN0 0x01
82#define F020_MASK_ADC0CF_AMP0GN1 0x02
83#define F020_MASK_ADC0CF_AMP0GN2 0x04
84
85#define F020_MASK_ADC0CN_AD0EN 0x80
86#define F020_MASK_ADC0CN_AD0INT 0x20
87#define F020_MASK_ADC0CN_AD0BUSY 0x10
88
89#define F020_MASK_DACxCN_DACxEN 0x80
90
91enum {
92
93 DT9812_DEVID_DT9812_10,
94 DT9812_DEVID_DT9812_2PT5,
95#if 0
96 DT9812_DEVID_DT9813,
97 DT9812_DEVID_DT9814
98#endif
99};
100
101enum dt9812_gain {
102 DT9812_GAIN_0PT25 = 1,
103 DT9812_GAIN_0PT5 = 2,
104 DT9812_GAIN_1 = 4,
105 DT9812_GAIN_2 = 8,
106 DT9812_GAIN_4 = 16,
107 DT9812_GAIN_8 = 32,
108 DT9812_GAIN_16 = 64,
109};
110
111enum {
112 DT9812_LEAST_USB_FIRMWARE_CMD_CODE = 0,
113
114 DT9812_W_FLASH_DATA = 0,
115
116 DT9812_R_FLASH_DATA = 1,
117
118
119
120
121
122
123 DT9812_R_SINGLE_BYTE_REG = 2,
124
125 DT9812_W_SINGLE_BYTE_REG = 3,
126
127 DT9812_R_MULTI_BYTE_REG = 4,
128
129 DT9812_W_MULTI_BYTE_REG = 5,
130
131 DT9812_RMW_SINGLE_BYTE_REG = 6,
132
133 DT9812_RMW_MULTI_BYTE_REG = 7,
134
135
136
137
138
139
140 DT9812_R_SINGLE_BYTE_SMBUS = 8,
141
142 DT9812_W_SINGLE_BYTE_SMBUS = 9,
143
144 DT9812_R_MULTI_BYTE_SMBUS = 10,
145
146 DT9812_W_MULTI_BYTE_SMBUS = 11,
147
148
149
150
151
152
153 DT9812_R_SINGLE_BYTE_DEV = 12,
154
155 DT9812_W_SINGLE_BYTE_DEV = 13,
156
157 DT9812_R_MULTI_BYTE_DEV = 14,
158
159 DT9812_W_MULTI_BYTE_DEV = 15,
160
161
162 DT9812_W_DAC_THRESHOLD = 16,
163
164
165 DT9812_W_INT_ON_CHANGE_MASK = 17,
166
167
168 DT9812_W_CGL = 18,
169
170 DT9812_R_MULTI_BYTE_USBMEM = 19,
171
172 DT9812_W_MULTI_BYTE_USBMEM = 20,
173
174
175 DT9812_START_SUBSYSTEM = 21,
176
177 DT9812_STOP_SUBSYSTEM = 22,
178
179
180 DT9812_CALIBRATE_POT = 23,
181
182 DT9812_W_DAC_FIFO_SIZE = 24,
183
184 DT9812_W_CGL_DAC = 25,
185
186 DT9812_R_SINGLE_VALUE_CMD = 26,
187
188 DT9812_W_SINGLE_VALUE_CMD = 27,
189
190 DT9812_MAX_USB_FIRMWARE_CMD_CODE,
191};
192
193struct dt9812_flash_data {
194 u16 numbytes;
195 u16 address;
196};
197
198#define DT9812_MAX_NUM_MULTI_BYTE_RDS \
199 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(u8))
200
201struct dt9812_read_multi {
202 u8 count;
203 u8 address[DT9812_MAX_NUM_MULTI_BYTE_RDS];
204};
205
206struct dt9812_write_byte {
207 u8 address;
208 u8 value;
209};
210
211#define DT9812_MAX_NUM_MULTI_BYTE_WRTS \
212 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / \
213 sizeof(struct dt9812_write_byte))
214
215struct dt9812_write_multi {
216 u8 count;
217 struct dt9812_write_byte write[DT9812_MAX_NUM_MULTI_BYTE_WRTS];
218};
219
220struct dt9812_rmw_byte {
221 u8 address;
222 u8 and_mask;
223 u8 or_value;
224};
225
226#define DT9812_MAX_NUM_MULTI_BYTE_RMWS \
227 ((DT9812_MAX_WRITE_CMD_PIPE_SIZE - 4 - 1) / sizeof(struct dt9812_rmw_byte))
228
229struct dt9812_rmw_multi {
230 u8 count;
231 struct dt9812_rmw_byte rmw[DT9812_MAX_NUM_MULTI_BYTE_RMWS];
232};
233
234struct dt9812_usb_cmd {
235 u32 cmd;
236 union {
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;
241 } u;
242#if 0
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;
251 CGL_INFO CglInfo;
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;
260#endif
261};
262
263#define DT9812_NUM_SLOTS 16
264
265static DECLARE_MUTEX(dt9812_mutex);
266
267static struct usb_device_id dt9812_table[] = {
268 {USB_DEVICE(0x0867, 0x9812)},
269 {}
270};
271
272MODULE_DEVICE_TABLE(usb, dt9812_table);
273
274struct usb_dt9812 {
275 struct slot_dt9812 *slot;
276 struct usb_device *udev;
277 struct usb_interface *interface;
278 u16 vendor;
279 u16 product;
280 u16 device;
281 u32 serial;
282 struct {
283 __u8 addr;
284 size_t size;
285 } message_pipe, command_write, command_read, write_stream, read_stream;
286 struct kref kref;
287 u16 analog_out_shadow[2];
288 u8 digital_out_shadow;
289};
290
291struct comedi_dt9812 {
292 struct slot_dt9812 *slot;
293 u32 serial;
294};
295
296struct slot_dt9812 {
297 struct semaphore mutex;
298 u32 serial;
299 struct usb_dt9812 *usb;
300 struct comedi_dt9812 *comedi;
301};
302
303static const struct comedi_lrange dt9812_10_ain_range = { 1, {
304 BIP_RANGE(10),
305 }
306};
307
308static const struct comedi_lrange dt9812_2pt5_ain_range = { 1, {
309 UNI_RANGE(2.5),
310 }
311};
312
313static const struct comedi_lrange dt9812_10_aout_range = { 1, {
314 BIP_RANGE(10),
315 }
316};
317
318static const struct comedi_lrange dt9812_2pt5_aout_range = { 1, {
319 UNI_RANGE(2.5),
320 }
321};
322
323static struct slot_dt9812 dt9812[DT9812_NUM_SLOTS];
324
325
326#define devpriv ((struct comedi_dt9812 *)dev->private)
327
328static inline struct usb_dt9812 *to_dt9812_dev(struct kref *d)
329{
330 return container_of(d, struct usb_dt9812, kref);
331}
332
333static void dt9812_delete(struct kref *kref)
334{
335 struct usb_dt9812 *dev = to_dt9812_dev(kref);
336
337 usb_put_dev(dev->udev);
338 kfree(dev);
339}
340
341static int dt9812_read_info(struct usb_dt9812 *dev, int offset, void *buf,
342 size_t buf_size)
343{
344 struct dt9812_usb_cmd cmd;
345 int count, retval;
346
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);
351
352
353 count = 32;
354 retval = usb_bulk_msg(dev->udev,
355 usb_sndbulkpipe(dev->udev,
356 dev->command_write.addr),
357 &cmd, 32, &count, HZ * 1);
358 if (retval)
359 return retval;
360 retval = usb_bulk_msg(dev->udev,
361 usb_rcvbulkpipe(dev->udev,
362 dev->command_read.addr),
363 buf, buf_size, &count, HZ * 1);
364 return retval;
365}
366
367static int dt9812_read_multiple_registers(struct usb_dt9812 *dev, int reg_count,
368 u8 * address, u8 * value)
369{
370 struct dt9812_usb_cmd cmd;
371 int i, count, retval;
372
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];
377
378
379 count = 32;
380 retval = usb_bulk_msg(dev->udev,
381 usb_sndbulkpipe(dev->udev,
382 dev->command_write.addr),
383 &cmd, 32, &count, HZ * 1);
384 if (retval)
385 return retval;
386 retval = usb_bulk_msg(dev->udev,
387 usb_rcvbulkpipe(dev->udev,
388 dev->command_read.addr),
389 value, reg_count, &count, HZ * 1);
390 return retval;
391}
392
393static int dt9812_write_multiple_registers(struct usb_dt9812 *dev,
394 int reg_count, u8 * address,
395 u8 * value)
396{
397 struct dt9812_usb_cmd cmd;
398 int i, count, retval;
399
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];
405 }
406
407 retval = usb_bulk_msg(dev->udev,
408 usb_sndbulkpipe(dev->udev,
409 dev->command_write.addr),
410 &cmd, 32, &count, HZ * 1);
411 return retval;
412}
413
414static int dt9812_rmw_multiple_registers(struct usb_dt9812 *dev, int reg_count,
415 struct dt9812_rmw_byte *rmw)
416{
417 struct dt9812_usb_cmd cmd;
418 int i, count, retval;
419
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];
424
425
426 retval = usb_bulk_msg(dev->udev,
427 usb_sndbulkpipe(dev->udev,
428 dev->command_write.addr),
429 &cmd, 32, &count, HZ * 1);
430 return retval;
431}
432
433static int dt9812_digital_in(struct slot_dt9812 *slot, u8 * bits)
434{
435 int result = -ENODEV;
436
437 down(&slot->mutex);
438 if (slot->usb) {
439 u8 reg[2] = { F020_SFR_P3, F020_SFR_P1 };
440 u8 value[2];
441
442 result = dt9812_read_multiple_registers(slot->usb, 2, reg,
443 value);
444 if (result == 0) {
445
446
447
448
449
450 *bits = (value[0] & 0x7f) | ((value[1] & 0x08) << 4);
451
452
453 }
454 }
455 up(&slot->mutex);
456
457 return result;
458}
459
460static int dt9812_digital_out(struct slot_dt9812 *slot, u8 bits)
461{
462 int result = -ENODEV;
463
464 down(&slot->mutex);
465 if (slot->usb) {
466 u8 reg[1];
467 u8 value[1];
468
469 reg[0] = F020_SFR_P2;
470 value[0] = bits;
471 result = dt9812_write_multiple_registers(slot->usb, 1, reg,
472 value);
473 slot->usb->digital_out_shadow = bits;
474 }
475 up(&slot->mutex);
476 return result;
477}
478
479static int dt9812_digital_out_shadow(struct slot_dt9812 *slot, u8 * bits)
480{
481 int result = -ENODEV;
482
483 down(&slot->mutex);
484 if (slot->usb) {
485 *bits = slot->usb->digital_out_shadow;
486 result = 0;
487 }
488 up(&slot->mutex);
489 return result;
490}
491
492static void dt9812_configure_mux(struct usb_dt9812 *dev,
493 struct dt9812_rmw_byte *rmw, int channel)
494{
495 if (dev->device == DT9812_DEVID_DT9812_10) {
496
497 rmw->address = F020_SFR_P1;
498 rmw->and_mask = 0xe0;
499 rmw->or_value = channel << 5;
500 } else {
501
502 rmw->address = F020_SFR_AMX0SL;
503 rmw->and_mask = 0xff;
504 rmw->or_value = channel & 0x07;
505 }
506}
507
508static void dt9812_configure_gain(struct usb_dt9812 *dev,
509 struct dt9812_rmw_byte *rmw,
510 enum dt9812_gain gain)
511{
512 if (dev->device == DT9812_DEVID_DT9812_10) {
513
514 gain <<= 1;
515 }
516
517 rmw->address = F020_SFR_ADC0CF;
518 rmw->and_mask = F020_MASK_ADC0CF_AMP0GN2 |
519 F020_MASK_ADC0CF_AMP0GN1 | F020_MASK_ADC0CF_AMP0GN0;
520 switch (gain) {
521
522
523
524
525
526
527
528
529 case DT9812_GAIN_0PT5:
530 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2 ||
531 F020_MASK_ADC0CF_AMP0GN1;
532 break;
533 case DT9812_GAIN_1:
534 rmw->or_value = 0x00;
535 break;
536 case DT9812_GAIN_2:
537 rmw->or_value = F020_MASK_ADC0CF_AMP0GN0;
538 break;
539 case DT9812_GAIN_4:
540 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1;
541 break;
542 case DT9812_GAIN_8:
543 rmw->or_value = F020_MASK_ADC0CF_AMP0GN1 ||
544 F020_MASK_ADC0CF_AMP0GN0;
545 break;
546 case DT9812_GAIN_16:
547 rmw->or_value = F020_MASK_ADC0CF_AMP0GN2;
548 break;
549 default:
550 err("Illegal gain %d\n", gain);
551
552 }
553}
554
555static int dt9812_analog_in(struct slot_dt9812 *slot, int channel, u16 * value,
556 enum dt9812_gain gain)
557{
558 struct dt9812_rmw_byte rmw[3];
559 u8 reg[3] = {
560 F020_SFR_ADC0CN,
561 F020_SFR_ADC0H,
562 F020_SFR_ADC0L
563 };
564 u8 val[3];
565 int result = -ENODEV;
566
567 down(&slot->mutex);
568 if (!slot->usb)
569 goto exit;
570
571
572 dt9812_configure_gain(slot->usb, &rmw[0], gain);
573
574
575 dt9812_configure_mux(slot->usb, &rmw[1], channel);
576
577
578 rmw[2].address = F020_SFR_ADC0CN;
579 rmw[2].and_mask = 0xff;
580 rmw[2].or_value = F020_MASK_ADC0CN_AD0EN | F020_MASK_ADC0CN_AD0BUSY;
581
582 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
583 if (result)
584 goto exit;
585
586
587 result = dt9812_read_multiple_registers(slot->usb, 3, reg, val);
588 if (result)
589 goto exit;
590
591
592
593
594
595
596
597
598
599 if ((val[0] & (F020_MASK_ADC0CN_AD0INT | F020_MASK_ADC0CN_AD0BUSY)) ==
600 F020_MASK_ADC0CN_AD0INT) {
601 switch (slot->usb->device) {
602 case DT9812_DEVID_DT9812_10:
603
604
605
606
607
608 *value = ((val[1] << 8) | val[2]) + 0x800;
609 break;
610 case DT9812_DEVID_DT9812_2PT5:
611 *value = (val[1] << 8) | val[2];
612 break;
613 }
614 }
615
616exit:
617 up(&slot->mutex);
618 return result;
619}
620
621static int dt9812_analog_out_shadow(struct slot_dt9812 *slot, int channel,
622 u16 * value)
623{
624 int result = -ENODEV;
625
626 down(&slot->mutex);
627 if (slot->usb) {
628 *value = slot->usb->analog_out_shadow[channel];
629 result = 0;
630 }
631 up(&slot->mutex);
632
633 return result;
634}
635
636static int dt9812_analog_out(struct slot_dt9812 *slot, int channel, u16 value)
637{
638 int result = -ENODEV;
639
640 down(&slot->mutex);
641 if (slot->usb) {
642 struct dt9812_rmw_byte rmw[3];
643
644 switch (channel) {
645 case 0:
646
647 rmw[0].address = F020_SFR_DAC0CN;
648 rmw[0].and_mask = 0xff;
649 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
650
651
652 rmw[1].address = F020_SFR_DAC0L;
653 rmw[1].and_mask = 0xff;
654 rmw[1].or_value = value & 0xff;
655
656
657
658 rmw[2].address = F020_SFR_DAC0H;
659 rmw[2].and_mask = 0xff;
660 rmw[2].or_value = (value >> 8) & 0xf;
661 break;
662
663 case 1:
664
665 rmw[0].address = F020_SFR_DAC1CN;
666 rmw[0].and_mask = 0xff;
667 rmw[0].or_value = F020_MASK_DACxCN_DACxEN;
668
669
670 rmw[1].address = F020_SFR_DAC1L;
671 rmw[1].and_mask = 0xff;
672 rmw[1].or_value = value & 0xff;
673
674
675
676 rmw[2].address = F020_SFR_DAC1H;
677 rmw[2].and_mask = 0xff;
678 rmw[2].or_value = (value >> 8) & 0xf;
679 break;
680 }
681 result = dt9812_rmw_multiple_registers(slot->usb, 3, rmw);
682 slot->usb->analog_out_shadow[channel] = value;
683 }
684 up(&slot->mutex);
685
686 return result;
687}
688
689
690
691
692
693static int dt9812_probe(struct usb_interface *interface,
694 const struct usb_device_id *id)
695{
696 int retval = -ENOMEM;
697 struct usb_dt9812 *dev = NULL;
698 struct usb_host_interface *iface_desc;
699 struct usb_endpoint_descriptor *endpoint;
700 int i;
701 u8 fw;
702
703
704 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
705 if (dev == NULL) {
706 dev_err(&interface->dev, "Out of memory\n");
707 goto error;
708 }
709 kref_init(&dev->kref);
710
711 dev->udev = usb_get_dev(interface_to_usbdev(interface));
712 dev->interface = interface;
713
714
715 iface_desc = interface->cur_altsetting;
716
717 if (iface_desc->desc.bNumEndpoints != 5) {
718 err("Wrong number of endpints.");
719 retval = -ENODEV;
720 goto error;
721 }
722
723 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
724 int direction = -1;
725 endpoint = &iface_desc->endpoint[i].desc;
726 switch (i) {
727 case 0:
728 direction = USB_DIR_IN;
729 dev->message_pipe.addr = endpoint->bEndpointAddress;
730 dev->message_pipe.size =
731 le16_to_cpu(endpoint->wMaxPacketSize);
732
733 break;
734 case 1:
735 direction = USB_DIR_OUT;
736 dev->command_write.addr = endpoint->bEndpointAddress;
737 dev->command_write.size =
738 le16_to_cpu(endpoint->wMaxPacketSize);
739 break;
740 case 2:
741 direction = USB_DIR_IN;
742 dev->command_read.addr = endpoint->bEndpointAddress;
743 dev->command_read.size =
744 le16_to_cpu(endpoint->wMaxPacketSize);
745 break;
746 case 3:
747 direction = USB_DIR_OUT;
748 dev->write_stream.addr = endpoint->bEndpointAddress;
749 dev->write_stream.size =
750 le16_to_cpu(endpoint->wMaxPacketSize);
751 break;
752 case 4:
753 direction = USB_DIR_IN;
754 dev->read_stream.addr = endpoint->bEndpointAddress;
755 dev->read_stream.size =
756 le16_to_cpu(endpoint->wMaxPacketSize);
757 break;
758 }
759 if ((endpoint->bEndpointAddress & USB_DIR_IN) != direction) {
760 dev_err(&interface->dev,
761 "Endpoint has wrong direction.\n");
762 retval = -ENODEV;
763 goto error;
764 }
765 }
766 if (dt9812_read_info(dev, 0, &fw, sizeof(fw)) != 0) {
767
768
769
770
771 usb_reset_configuration(dev->udev);
772 for (i = 0; i < 10; i++) {
773 retval = dt9812_read_info(dev, 1, &fw, sizeof(fw));
774 if (retval == 0) {
775 dev_info(&interface->dev,
776 "usb_reset_configuration succeded "
777 "after %d iterations\n", i);
778 break;
779 }
780 }
781 }
782
783 if (dt9812_read_info(dev, 1, &dev->vendor, sizeof(dev->vendor)) != 0) {
784 err("Failed to read vendor.");
785 retval = -ENODEV;
786 goto error;
787 }
788 if (dt9812_read_info(dev, 3, &dev->product, sizeof(dev->product)) != 0) {
789 err("Failed to read product.");
790 retval = -ENODEV;
791 goto error;
792 }
793 if (dt9812_read_info(dev, 5, &dev->device, sizeof(dev->device)) != 0) {
794 err("Failed to read device.");
795 retval = -ENODEV;
796 goto error;
797 }
798 if (dt9812_read_info(dev, 7, &dev->serial, sizeof(dev->serial)) != 0) {
799 err("Failed to read serial.");
800 retval = -ENODEV;
801 goto error;
802 }
803
804 dev->vendor = le16_to_cpu(dev->vendor);
805 dev->product = le16_to_cpu(dev->product);
806 dev->device = le16_to_cpu(dev->device);
807 dev->serial = le32_to_cpu(dev->serial);
808 switch (dev->device) {
809 case DT9812_DEVID_DT9812_10:
810 dev->analog_out_shadow[0] = 0x0800;
811 dev->analog_out_shadow[1] = 0x800;
812 break;
813 case DT9812_DEVID_DT9812_2PT5:
814 dev->analog_out_shadow[0] = 0x0000;
815 dev->analog_out_shadow[1] = 0x0000;
816 break;
817 }
818 dev->digital_out_shadow = 0;
819
820
821 usb_set_intfdata(interface, dev);
822
823
824 dev_info(&interface->dev, "USB DT9812 (%4.4x.%4.4x.%4.4x) #0x%8.8x\n",
825 dev->vendor, dev->product, dev->device, dev->serial);
826
827 down(&dt9812_mutex);
828 {
829
830 struct slot_dt9812 *first = NULL;
831 struct slot_dt9812 *best = NULL;
832
833 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
834 if (!first && !dt9812[i].usb && dt9812[i].serial == 0)
835 first = &dt9812[i];
836 if (!best && dt9812[i].serial == dev->serial)
837 best = &dt9812[i];
838 }
839
840 if (!best)
841 best = first;
842
843 if (best) {
844 down(&best->mutex);
845 best->usb = dev;
846 dev->slot = best;
847 up(&best->mutex);
848 }
849 }
850 up(&dt9812_mutex);
851
852 return 0;
853
854error:
855 if (dev)
856 kref_put(&dev->kref, dt9812_delete);
857 return retval;
858}
859
860static void dt9812_disconnect(struct usb_interface *interface)
861{
862 struct usb_dt9812 *dev;
863 int minor = interface->minor;
864
865 down(&dt9812_mutex);
866 dev = usb_get_intfdata(interface);
867 if (dev->slot) {
868 down(&dev->slot->mutex);
869 dev->slot->usb = NULL;
870 up(&dev->slot->mutex);
871 dev->slot = NULL;
872 }
873 usb_set_intfdata(interface, NULL);
874 up(&dt9812_mutex);
875
876
877 kref_put(&dev->kref, dt9812_delete);
878
879 dev_info(&interface->dev, "USB Dt9812 #%d now disconnected\n", minor);
880}
881
882static struct usb_driver dt9812_usb_driver = {
883 .name = "dt9812",
884 .probe = dt9812_probe,
885 .disconnect = dt9812_disconnect,
886 .id_table = dt9812_table,
887};
888
889
890
891
892
893static void dt9812_comedi_open(struct comedi_device *dev)
894{
895 down(&devpriv->slot->mutex);
896 if (devpriv->slot->usb) {
897
898 struct comedi_subdevice *s;
899
900 s = &dev->subdevices[0];
901 s->n_chan = 8;
902 s->maxdata = 1;
903
904 s = &dev->subdevices[1];
905 s->n_chan = 8;
906 s->maxdata = 1;
907
908 s = &dev->subdevices[2];
909 s->n_chan = 8;
910 switch (devpriv->slot->usb->device) {
911 case 0:{
912 s->maxdata = 4095;
913 s->range_table = &dt9812_10_ain_range;
914 }
915 break;
916 case 1:{
917 s->maxdata = 4095;
918 s->range_table = &dt9812_2pt5_ain_range;
919 }
920 break;
921 }
922
923 s = &dev->subdevices[3];
924 s->n_chan = 2;
925 switch (devpriv->slot->usb->device) {
926 case 0:{
927 s->maxdata = 4095;
928 s->range_table = &dt9812_10_aout_range;
929 }
930 break;
931 case 1:{
932 s->maxdata = 4095;
933 s->range_table = &dt9812_2pt5_aout_range;
934 }
935 break;
936 }
937 }
938 up(&devpriv->slot->mutex);
939}
940
941static int dt9812_di_rinsn(struct comedi_device *dev,
942 struct comedi_subdevice *s, struct comedi_insn *insn,
943 unsigned int *data)
944{
945 int n;
946 u8 bits = 0;
947
948 dt9812_digital_in(devpriv->slot, &bits);
949 for (n = 0; n < insn->n; n++)
950 data[n] = ((1 << insn->chanspec) & bits) != 0;
951 return n;
952}
953
954static int dt9812_do_winsn(struct comedi_device *dev,
955 struct comedi_subdevice *s, struct comedi_insn *insn,
956 unsigned int *data)
957{
958 int n;
959 u8 bits = 0;
960
961 dt9812_digital_out_shadow(devpriv->slot, &bits);
962 for (n = 0; n < insn->n; n++) {
963 u8 mask = 1 << insn->chanspec;
964
965 bits &= ~mask;
966 if (data[n])
967 bits |= mask;
968 }
969 dt9812_digital_out(devpriv->slot, bits);
970 return n;
971}
972
973static int dt9812_ai_rinsn(struct comedi_device *dev,
974 struct comedi_subdevice *s, struct comedi_insn *insn,
975 unsigned int *data)
976{
977 int n;
978
979 for (n = 0; n < insn->n; n++) {
980 u16 value = 0;
981
982 dt9812_analog_in(devpriv->slot, insn->chanspec, &value,
983 DT9812_GAIN_1);
984 data[n] = value;
985 }
986 return n;
987}
988
989static int dt9812_ao_rinsn(struct comedi_device *dev,
990 struct comedi_subdevice *s, struct comedi_insn *insn,
991 unsigned int *data)
992{
993 int n;
994 u16 value;
995
996 for (n = 0; n < insn->n; n++) {
997 value = 0;
998 dt9812_analog_out_shadow(devpriv->slot, insn->chanspec, &value);
999 data[n] = value;
1000 }
1001 return n;
1002}
1003
1004static int dt9812_ao_winsn(struct comedi_device *dev,
1005 struct comedi_subdevice *s, struct comedi_insn *insn,
1006 unsigned int *data)
1007{
1008 int n;
1009
1010 for (n = 0; n < insn->n; n++)
1011 dt9812_analog_out(devpriv->slot, insn->chanspec, data[n]);
1012 return n;
1013}
1014
1015static int dt9812_attach(struct comedi_device *dev, struct comedi_devconfig *it)
1016{
1017 int i;
1018 struct comedi_subdevice *s;
1019
1020 dev->board_name = "dt9812";
1021
1022 if (alloc_private(dev, sizeof(struct comedi_dt9812)) < 0)
1023 return -ENOMEM;
1024
1025
1026
1027
1028
1029 dev->open = dt9812_comedi_open;
1030
1031 devpriv->serial = it->options[0];
1032
1033
1034 if (alloc_subdevices(dev, 4) < 0)
1035 return -ENOMEM;
1036
1037
1038 s = dev->subdevices + 0;
1039 s->type = COMEDI_SUBD_DI;
1040 s->subdev_flags = SDF_READABLE;
1041 s->n_chan = 0;
1042 s->maxdata = 1;
1043 s->range_table = &range_digital;
1044 s->insn_read = &dt9812_di_rinsn;
1045
1046
1047 s = dev->subdevices + 1;
1048 s->type = COMEDI_SUBD_DO;
1049 s->subdev_flags = SDF_WRITEABLE;
1050 s->n_chan = 0;
1051 s->maxdata = 1;
1052 s->range_table = &range_digital;
1053 s->insn_write = &dt9812_do_winsn;
1054
1055
1056 s = dev->subdevices + 2;
1057 s->type = COMEDI_SUBD_AI;
1058 s->subdev_flags = SDF_READABLE | SDF_GROUND;
1059 s->n_chan = 0;
1060 s->maxdata = 1;
1061 s->range_table = NULL;
1062 s->insn_read = &dt9812_ai_rinsn;
1063
1064
1065 s = dev->subdevices + 3;
1066 s->type = COMEDI_SUBD_AO;
1067 s->subdev_flags = SDF_WRITEABLE;
1068 s->n_chan = 0;
1069 s->maxdata = 1;
1070 s->range_table = NULL;
1071 s->insn_write = &dt9812_ao_winsn;
1072 s->insn_read = &dt9812_ao_rinsn;
1073
1074 printk(KERN_INFO "comedi%d: successfully attached to dt9812.\n",
1075 dev->minor);
1076
1077 down(&dt9812_mutex);
1078
1079 {
1080 struct slot_dt9812 *first = NULL;
1081 struct slot_dt9812 *best = NULL;
1082 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1083 if (!first && !dt9812[i].comedi) {
1084
1085 first = &dt9812[i];
1086 }
1087 if (!best &&
1088 dt9812[i].usb &&
1089 dt9812[i].usb->serial == devpriv->serial) {
1090
1091 best = &dt9812[i];
1092 }
1093 }
1094 if (!best)
1095 best = first;
1096 if (best) {
1097 down(&best->mutex);
1098 best->comedi = devpriv;
1099 best->serial = devpriv->serial;
1100 devpriv->slot = best;
1101 up(&best->mutex);
1102 }
1103 }
1104 up(&dt9812_mutex);
1105
1106 return 0;
1107}
1108
1109static int dt9812_detach(struct comedi_device *dev)
1110{
1111 return 0;
1112}
1113
1114static struct comedi_driver dt9812_comedi_driver = {
1115 .module = THIS_MODULE,
1116 .driver_name = "dt9812",
1117 .attach = dt9812_attach,
1118 .detach = dt9812_detach,
1119};
1120
1121static int __init usb_dt9812_init(void)
1122{
1123 int result, i;
1124
1125
1126 for (i = 0; i < DT9812_NUM_SLOTS; i++) {
1127 init_MUTEX(&dt9812[i].mutex);
1128 dt9812[i].serial = 0;
1129 dt9812[i].usb = NULL;
1130 dt9812[i].comedi = NULL;
1131 }
1132 dt9812[12].serial = 0x0;
1133
1134
1135 result = usb_register(&dt9812_usb_driver);
1136 if (result) {
1137 printk(KERN_ERR KBUILD_MODNAME
1138 ": usb_register failed. Error number %d\n", result);
1139 return result;
1140 }
1141
1142 result = comedi_driver_register(&dt9812_comedi_driver);
1143 if (result) {
1144 usb_deregister(&dt9812_usb_driver);
1145 err("comedi_driver_register failed. Error number %d", result);
1146 }
1147
1148 return result;
1149}
1150
1151static void __exit usb_dt9812_exit(void)
1152{
1153
1154 comedi_driver_unregister(&dt9812_comedi_driver);
1155
1156
1157 usb_deregister(&dt9812_usb_driver);
1158}
1159
1160module_init(usb_dt9812_init);
1161module_exit(usb_dt9812_exit);
1162
1163MODULE_AUTHOR("Anders Blomdell <anders.blomdell@control.lth.se>");
1164MODULE_DESCRIPTION("Comedi DT9812 driver");
1165MODULE_LICENSE("GPL");
1166