2 * Copyright (C) 2004-2019 Bernd Porr, mail@berndporr.me.uk
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
17 * Description: University of Stirling USB DAQ & INCITE Technology Limited
18 * Devices: [ITL] USB-DUX-FAST (usbduxfast)
19 * Author: Bernd Porr <mail@berndporr.me.uk>
20 * Updated: 16 Nov 2019
25 * I must give credit here to Chris Baugher who
26 * wrote the driver for AT-MIO-16d. I used some parts of this
27 * driver. I also must give credits to David Brownell
28 * who supported me with the USB development.
34 * 1.0: Fixed a rounding error in usbduxfast_ai_cmdtest
35 * 0.9: Dropping the first data packet which seems to be from the last transfer.
36 * Buffer overflows in the FX2 are handed over to comedi.
37 * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
38 * Added insn command basically for testing. Sample rate is
40 * 0.99: Ian Abbott pointed out a bug which has been corrected. Thanks!
41 * 0.99a: added external trigger.
42 * 1.00: added firmware kernel request to the driver which fixed
43 * udev coldplug problem
46 #include <linux/kernel.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/input.h>
50 #include <linux/fcntl.h>
51 #include <linux/compiler.h>
52 #include "../comedi_usb.h"
55 * timeout for the USB-transfer
60 * constants for "firmware" upload and download
62 #define FIRMWARE "usbduxfast_firmware.bin"
63 #define FIRMWARE_MAX_LEN 0x2000
64 #define USBDUXFASTSUB_FIRMWARE 0xA0
65 #define VENDOR_DIR_IN 0xC0
66 #define VENDOR_DIR_OUT 0x40
69 * internal addresses of the 8051 processor
71 #define USBDUXFASTSUB_CPUCS 0xE600
74 * max lenghth of the transfer-buffer for software upload
79 * input endpoint number
84 * endpoint for the A/D channellist: bulk OUT
86 #define CHANNELLISTEP 4
91 #define NUMCHANNELS 32
94 * size of the waveform descriptor
99 * size of one A/D value
101 #define SIZEADIN (sizeof(s16))
104 * size of the input-buffer IN BYTES
106 #define SIZEINBUF 512
111 #define SIZEINSNBUF 512
114 * size of the buffer for the dux commands in bytes
116 #define SIZEOFDUXBUF 256
119 * number of in-URBs which receive the data: min=5
121 #define NUMOFINBUFFERSHIGH 10
124 * min delay steps for more than one channel
125 * basically when the mux gives up ;-)
127 * steps at 30MHz in the FX2
129 #define MIN_SAMPLING_PERIOD 9
132 * max number of 1/30MHz delay steps
134 #define MAX_SAMPLING_PERIOD 500
137 * number of received packets to ignore before we start handing data
138 * over to comedi, it's quad buffering and we have to ignore 4 packets
140 #define PACKETS_TO_IGNORE 4
145 static const struct comedi_lrange range_usbduxfast_ai_range = {
153 * private structure of one subdevice
155 * this is the structure which holds all the data of this driver
156 * one sub device just now: A/D
158 struct usbduxfast_private {
159 struct urb *urb; /* BULK-transfer handling: urb */
162 short int ai_cmd_running; /* asynchronous command is running */
163 int ignore; /* counter which ignores the first buffers */
168 * bulk transfers to usbduxfast
170 #define SENDADCOMMANDS 0
171 #define SENDINITEP6 1
173 static int usbduxfast_send_cmd(struct comedi_device *dev, int cmd_type)
175 struct usb_device *usb = comedi_to_usb_dev(dev);
176 struct usbduxfast_private *devpriv = dev->private;
180 devpriv->duxbuf[0] = cmd_type;
182 ret = usb_bulk_msg(usb, usb_sndbulkpipe(usb, CHANNELLISTEP),
183 devpriv->duxbuf, SIZEOFDUXBUF,
186 dev_err(dev->class_dev,
187 "could not transmit command to the usb-device, err=%d\n",
192 static void usbduxfast_cmd_data(struct comedi_device *dev, int index,
193 u8 len, u8 op, u8 out, u8 log)
195 struct usbduxfast_private *devpriv = dev->private;
197 /* Set the GPIF bytes, the first byte is the command byte */
198 devpriv->duxbuf[1 + 0x00 + index] = len;
199 devpriv->duxbuf[1 + 0x08 + index] = op;
200 devpriv->duxbuf[1 + 0x10 + index] = out;
201 devpriv->duxbuf[1 + 0x18 + index] = log;
204 static int usbduxfast_ai_stop(struct comedi_device *dev, int do_unlink)
206 struct usbduxfast_private *devpriv = dev->private;
209 devpriv->ai_cmd_running = 0;
211 if (do_unlink && devpriv->urb) {
212 /* kill the running transfer */
213 usb_kill_urb(devpriv->urb);
219 static int usbduxfast_ai_cancel(struct comedi_device *dev,
220 struct comedi_subdevice *s)
222 struct usbduxfast_private *devpriv = dev->private;
225 mutex_lock(&devpriv->mut);
226 ret = usbduxfast_ai_stop(dev, 1);
227 mutex_unlock(&devpriv->mut);
232 static void usbduxfast_ai_handle_urb(struct comedi_device *dev,
233 struct comedi_subdevice *s,
236 struct usbduxfast_private *devpriv = dev->private;
237 struct comedi_async *async = s->async;
238 struct comedi_cmd *cmd = &async->cmd;
241 if (devpriv->ignore) {
244 unsigned int nsamples;
246 nsamples = comedi_bytes_to_samples(s, urb->actual_length);
247 nsamples = comedi_nsamples_left(s, nsamples);
248 comedi_buf_write_samples(s, urb->transfer_buffer, nsamples);
250 if (cmd->stop_src == TRIG_COUNT &&
251 async->scans_done >= cmd->stop_arg)
252 async->events |= COMEDI_CB_EOA;
255 /* if command is still running, resubmit urb for BULK transfer */
256 if (!(async->events & COMEDI_CB_CANCEL_MASK)) {
257 urb->dev = comedi_to_usb_dev(dev);
259 ret = usb_submit_urb(urb, GFP_ATOMIC);
261 dev_err(dev->class_dev, "urb resubm failed: %d", ret);
262 async->events |= COMEDI_CB_ERROR;
267 static void usbduxfast_ai_interrupt(struct urb *urb)
269 struct comedi_device *dev = urb->context;
270 struct comedi_subdevice *s = dev->read_subdev;
271 struct comedi_async *async = s->async;
272 struct usbduxfast_private *devpriv = dev->private;
274 /* exit if not running a command, do not resubmit urb */
275 if (!devpriv->ai_cmd_running)
278 switch (urb->status) {
280 usbduxfast_ai_handle_urb(dev, s, urb);
287 /* after an unlink command, unplug, ... etc */
288 async->events |= COMEDI_CB_ERROR;
293 dev_err(dev->class_dev,
294 "non-zero urb status received in ai intr context: %d\n",
296 async->events |= COMEDI_CB_ERROR;
301 * comedi_handle_events() cannot be used in this driver. The (*cancel)
302 * operation would unlink the urb.
304 if (async->events & COMEDI_CB_CANCEL_MASK)
305 usbduxfast_ai_stop(dev, 0);
307 comedi_event(dev, s);
310 static int usbduxfast_submit_urb(struct comedi_device *dev)
312 struct usb_device *usb = comedi_to_usb_dev(dev);
313 struct usbduxfast_private *devpriv = dev->private;
316 usb_fill_bulk_urb(devpriv->urb, usb, usb_rcvbulkpipe(usb, BULKINEP),
317 devpriv->inbuf, SIZEINBUF,
318 usbduxfast_ai_interrupt, dev);
320 ret = usb_submit_urb(devpriv->urb, GFP_ATOMIC);
322 dev_err(dev->class_dev, "usb_submit_urb error %d\n", ret);
328 static int usbduxfast_ai_check_chanlist(struct comedi_device *dev,
329 struct comedi_subdevice *s,
330 struct comedi_cmd *cmd)
332 unsigned int gain0 = CR_RANGE(cmd->chanlist[0]);
335 if (cmd->chanlist_len > 3 && cmd->chanlist_len != 16) {
336 dev_err(dev->class_dev, "unsupported combination of channels\n");
340 for (i = 0; i < cmd->chanlist_len; ++i) {
341 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
342 unsigned int gain = CR_RANGE(cmd->chanlist[i]);
345 dev_err(dev->class_dev,
346 "channels are not consecutive\n");
349 if (gain != gain0 && cmd->chanlist_len > 3) {
350 dev_err(dev->class_dev,
351 "gain must be the same for all channels\n");
358 static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
359 struct comedi_subdevice *s,
360 struct comedi_cmd *cmd)
367 /* Step 1 : check if triggers are trivially valid */
369 err |= comedi_check_trigger_src(&cmd->start_src,
370 TRIG_NOW | TRIG_EXT | TRIG_INT);
371 err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_FOLLOW);
372 err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_TIMER);
373 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
374 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
379 /* Step 2a : make sure trigger sources are unique */
381 err |= comedi_check_trigger_is_unique(cmd->start_src);
382 err |= comedi_check_trigger_is_unique(cmd->stop_src);
384 /* Step 2b : and mutually compatible */
389 /* Step 3: check if arguments are trivially valid */
391 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
393 if (!cmd->chanlist_len)
396 /* external start trigger is only valid for 1 or 16 channels */
397 if (cmd->start_src == TRIG_EXT &&
398 cmd->chanlist_len != 1 && cmd->chanlist_len != 16)
401 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
405 * Validate the conversion timing:
406 * for 1 channel the timing in 30MHz "steps" is:
407 * steps <= MAX_SAMPLING_PERIOD
408 * for all other chanlist_len it is:
409 * MIN_SAMPLING_PERIOD <= steps <= MAX_SAMPLING_PERIOD
411 steps = (cmd->convert_arg * 30) / 1000;
412 if (cmd->chanlist_len != 1)
413 err2 |= comedi_check_trigger_arg_min(&steps,
414 MIN_SAMPLING_PERIOD);
416 err2 |= comedi_check_trigger_arg_min(&steps, 1);
417 err2 |= comedi_check_trigger_arg_max(&steps, MAX_SAMPLING_PERIOD);
420 arg = (steps * 1000) / 30;
421 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, arg);
424 if (cmd->stop_src == TRIG_COUNT)
425 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
427 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
432 /* Step 4: fix up any arguments */
434 /* Step 5: check channel list if it exists */
435 if (cmd->chanlist && cmd->chanlist_len > 0)
436 err |= usbduxfast_ai_check_chanlist(dev, s, cmd);
443 static int usbduxfast_ai_inttrig(struct comedi_device *dev,
444 struct comedi_subdevice *s,
445 unsigned int trig_num)
447 struct usbduxfast_private *devpriv = dev->private;
448 struct comedi_cmd *cmd = &s->async->cmd;
451 if (trig_num != cmd->start_arg)
454 mutex_lock(&devpriv->mut);
456 if (!devpriv->ai_cmd_running) {
457 devpriv->ai_cmd_running = 1;
458 ret = usbduxfast_submit_urb(dev);
460 dev_err(dev->class_dev, "urbSubmit: err=%d\n", ret);
461 devpriv->ai_cmd_running = 0;
462 mutex_unlock(&devpriv->mut);
465 s->async->inttrig = NULL;
467 dev_err(dev->class_dev, "ai is already running\n");
469 mutex_unlock(&devpriv->mut);
473 static int usbduxfast_ai_cmd(struct comedi_device *dev,
474 struct comedi_subdevice *s)
476 struct usbduxfast_private *devpriv = dev->private;
477 struct comedi_cmd *cmd = &s->async->cmd;
478 unsigned int rngmask = 0xff;
480 long steps, steps_tmp;
482 mutex_lock(&devpriv->mut);
483 if (devpriv->ai_cmd_running) {
489 * ignore the first buffers from the device if there
490 * is an error condition
492 devpriv->ignore = PACKETS_TO_IGNORE;
494 steps = (cmd->convert_arg * 30) / 1000;
496 switch (cmd->chanlist_len) {
502 if (CR_RANGE(cmd->chanlist[0]) > 0)
503 rngmask = 0xff - 0x04;
508 * for external trigger: looping in this state until
509 * the RDY0 pin becomes zero
512 /* we loop here until ready has been set */
513 if (cmd->start_src == TRIG_EXT) {
514 /* branch back to state 0 */
515 /* deceision state w/o data */
517 usbduxfast_cmd_data(dev, 0, 0x01, 0x01, rngmask, 0x00);
518 } else { /* we just proceed to state 1 */
519 usbduxfast_cmd_data(dev, 0, 0x01, 0x00, rngmask, 0x00);
522 if (steps < MIN_SAMPLING_PERIOD) {
523 /* for fast single channel aqu without mux */
526 * we just stay here at state 1 and rexecute
527 * the same state this gives us 30MHz sampling
531 /* branch back to state 1 */
532 /* deceision state with data */
534 usbduxfast_cmd_data(dev, 1,
535 0x89, 0x03, rngmask, 0xff);
538 * we loop through two states: data and delay
543 usbduxfast_cmd_data(dev, 1, steps - 1,
544 0x02, rngmask, 0x00);
546 /* branch back to state 1 */
547 /* deceision state w/o data */
549 usbduxfast_cmd_data(dev, 2,
550 0x09, 0x01, rngmask, 0xff);
554 * we loop through 3 states: 2x delay and 1x data
555 * this gives a min sampling rate of 60kHz
558 /* we have 1 state with duration 1 */
561 /* do the first part of the delay */
562 usbduxfast_cmd_data(dev, 1,
563 steps / 2, 0x00, rngmask, 0x00);
565 /* and the second part */
566 usbduxfast_cmd_data(dev, 2, steps - steps / 2,
567 0x00, rngmask, 0x00);
569 /* get the data and branch back */
571 /* branch back to state 1 */
572 /* deceision state w data */
574 usbduxfast_cmd_data(dev, 3,
575 0x09, 0x03, rngmask, 0xff);
582 * commit data to the FIFO
585 if (CR_RANGE(cmd->chanlist[0]) > 0)
586 rngmask = 0xff - 0x04;
591 usbduxfast_cmd_data(dev, 0, 0x01, 0x02, rngmask, 0x00);
593 /* we have 1 state with duration 1: state 0 */
594 steps_tmp = steps - 1;
596 if (CR_RANGE(cmd->chanlist[1]) > 0)
597 rngmask = 0xff - 0x04;
601 /* do the first part of the delay */
603 usbduxfast_cmd_data(dev, 1, steps_tmp / 2,
604 0x00, 0xfe & rngmask, 0x00);
606 /* and the second part */
607 usbduxfast_cmd_data(dev, 2, steps_tmp - steps_tmp / 2,
608 0x00, rngmask, 0x00);
611 usbduxfast_cmd_data(dev, 3, 0x01, 0x02, rngmask, 0x00);
614 * we have 2 states with duration 1: step 6 and
617 steps_tmp = steps - 2;
619 if (CR_RANGE(cmd->chanlist[0]) > 0)
620 rngmask = 0xff - 0x04;
624 /* do the first part of the delay */
626 usbduxfast_cmd_data(dev, 4, steps_tmp / 2,
627 0x00, (0xff - 0x02) & rngmask, 0x00);
629 /* and the second part */
630 usbduxfast_cmd_data(dev, 5, steps_tmp - steps_tmp / 2,
631 0x00, rngmask, 0x00);
633 usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
640 for (j = 0; j < 1; j++) {
643 if (CR_RANGE(cmd->chanlist[j]) > 0)
644 rngmask = 0xff - 0x04;
648 * commit data to the FIFO and do the first part
653 usbduxfast_cmd_data(dev, index, steps / 2,
654 0x02, rngmask, 0x00);
656 if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
657 rngmask = 0xff - 0x04;
661 /* do the second part of the delay */
664 usbduxfast_cmd_data(dev, index + 1, steps - steps / 2,
665 0x00, 0xfe & rngmask, 0x00);
668 /* 2 steps with duration 1: the idele step and step 6: */
669 steps_tmp = steps - 2;
671 /* commit data to the FIFO and do the first part of the delay */
673 usbduxfast_cmd_data(dev, 4, steps_tmp / 2,
674 0x02, rngmask, 0x00);
676 if (CR_RANGE(cmd->chanlist[0]) > 0)
677 rngmask = 0xff - 0x04;
681 /* do the second part of the delay */
684 usbduxfast_cmd_data(dev, 5, steps_tmp - steps_tmp / 2,
685 0x00, (0xff - 0x02) & rngmask, 0x00);
687 usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
691 if (CR_RANGE(cmd->chanlist[0]) > 0)
692 rngmask = 0xff - 0x04;
696 if (cmd->start_src == TRIG_EXT) {
698 * we loop here until ready has been set
701 /* branch back to state 0 */
702 /* deceision state w/o data */
705 usbduxfast_cmd_data(dev, 0, 0x01, 0x01,
706 (0xff - 0x02) & rngmask, 0x00);
709 * we just proceed to state 1
712 /* 30us reset pulse */
714 usbduxfast_cmd_data(dev, 0, 0xff, 0x00,
715 (0xff - 0x02) & rngmask, 0x00);
718 /* commit data to the FIFO */
720 usbduxfast_cmd_data(dev, 1, 0x01, 0x02, rngmask, 0x00);
722 /* we have 2 states with duration 1 */
725 /* do the first part of the delay */
726 usbduxfast_cmd_data(dev, 2, steps / 2,
727 0x00, 0xfe & rngmask, 0x00);
729 /* and the second part */
730 usbduxfast_cmd_data(dev, 3, steps - steps / 2,
731 0x00, rngmask, 0x00);
733 /* branch back to state 1 */
734 /* deceision state w/o data */
736 usbduxfast_cmd_data(dev, 4, 0x09, 0x01, rngmask, 0xff);
741 /* 0 means that the AD commands are sent */
742 ret = usbduxfast_send_cmd(dev, SENDADCOMMANDS);
746 if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
747 /* enable this acquisition operation */
748 devpriv->ai_cmd_running = 1;
749 ret = usbduxfast_submit_urb(dev);
751 devpriv->ai_cmd_running = 0;
752 /* fixme: unlink here?? */
755 s->async->inttrig = NULL;
756 } else { /* TRIG_INT */
757 s->async->inttrig = usbduxfast_ai_inttrig;
761 mutex_unlock(&devpriv->mut);
767 * Mode 0 is used to get a single conversion on demand.
769 static int usbduxfast_ai_insn_read(struct comedi_device *dev,
770 struct comedi_subdevice *s,
771 struct comedi_insn *insn,
774 struct usb_device *usb = comedi_to_usb_dev(dev);
775 struct usbduxfast_private *devpriv = dev->private;
776 unsigned int chan = CR_CHAN(insn->chanspec);
777 unsigned int range = CR_RANGE(insn->chanspec);
778 u8 rngmask = range ? (0xff - 0x04) : 0xff;
779 int i, j, n, actual_length;
782 mutex_lock(&devpriv->mut);
784 if (devpriv->ai_cmd_running) {
785 dev_err(dev->class_dev,
786 "ai_insn_read not possible, async cmd is running\n");
787 mutex_unlock(&devpriv->mut);
791 /* set command for the first channel */
793 /* commit data to the FIFO */
795 usbduxfast_cmd_data(dev, 0, 0x01, 0x02, rngmask, 0x00);
797 /* do the first part of the delay */
798 usbduxfast_cmd_data(dev, 1, 0x0c, 0x00, 0xfe & rngmask, 0x00);
799 usbduxfast_cmd_data(dev, 2, 0x01, 0x00, 0xfe & rngmask, 0x00);
800 usbduxfast_cmd_data(dev, 3, 0x01, 0x00, 0xfe & rngmask, 0x00);
801 usbduxfast_cmd_data(dev, 4, 0x01, 0x00, 0xfe & rngmask, 0x00);
804 usbduxfast_cmd_data(dev, 5, 0x0c, 0x00, rngmask, 0x00);
805 usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
807 ret = usbduxfast_send_cmd(dev, SENDADCOMMANDS);
809 mutex_unlock(&devpriv->mut);
813 for (i = 0; i < PACKETS_TO_IGNORE; i++) {
814 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, BULKINEP),
815 devpriv->inbuf, SIZEINBUF,
816 &actual_length, 10000);
818 dev_err(dev->class_dev, "insn timeout, no data\n");
819 mutex_unlock(&devpriv->mut);
824 for (i = 0; i < insn->n;) {
825 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, BULKINEP),
826 devpriv->inbuf, SIZEINBUF,
827 &actual_length, 10000);
829 dev_err(dev->class_dev, "insn data error: %d\n", ret);
830 mutex_unlock(&devpriv->mut);
833 n = actual_length / sizeof(u16);
835 dev_err(dev->class_dev, "insn data packet corrupted\n");
836 mutex_unlock(&devpriv->mut);
839 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
840 data[i] = ((u16 *)(devpriv->inbuf))[j];
845 mutex_unlock(&devpriv->mut);
850 static int usbduxfast_upload_firmware(struct comedi_device *dev,
851 const u8 *data, size_t size,
852 unsigned long context)
854 struct usb_device *usb = comedi_to_usb_dev(dev);
862 if (size > FIRMWARE_MAX_LEN) {
863 dev_err(dev->class_dev, "firmware binary too large for FX2\n");
867 /* we generate a local buffer for the firmware */
868 buf = kmemdup(data, size, GFP_KERNEL);
872 /* we need a malloc'ed buffer for usb_control_msg() */
873 tmp = kmalloc(1, GFP_KERNEL);
879 /* stop the current firmware on the device */
880 *tmp = 1; /* 7f92 to one */
881 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
882 USBDUXFASTSUB_FIRMWARE,
884 USBDUXFASTSUB_CPUCS, 0x0000,
888 dev_err(dev->class_dev, "can not stop firmware\n");
892 /* upload the new firmware to the device */
893 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
894 USBDUXFASTSUB_FIRMWARE,
900 dev_err(dev->class_dev, "firmware upload failed\n");
904 /* start the new firmware on the device */
905 *tmp = 0; /* 7f92 to zero */
906 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
907 USBDUXFASTSUB_FIRMWARE,
909 USBDUXFASTSUB_CPUCS, 0x0000,
913 dev_err(dev->class_dev, "can not start firmware\n");
921 static int usbduxfast_auto_attach(struct comedi_device *dev,
922 unsigned long context_unused)
924 struct usb_interface *intf = comedi_to_usb_interface(dev);
925 struct usb_device *usb = comedi_to_usb_dev(dev);
926 struct usbduxfast_private *devpriv;
927 struct comedi_subdevice *s;
930 if (usb->speed != USB_SPEED_HIGH) {
931 dev_err(dev->class_dev,
932 "This driver needs USB 2.0 to operate. Aborting...\n");
936 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
940 mutex_init(&devpriv->mut);
941 usb_set_intfdata(intf, devpriv);
943 devpriv->duxbuf = kmalloc(SIZEOFDUXBUF, GFP_KERNEL);
944 if (!devpriv->duxbuf)
947 ret = usb_set_interface(usb,
948 intf->altsetting->desc.bInterfaceNumber, 1);
950 dev_err(dev->class_dev,
951 "could not switch to alternate setting 1\n");
955 devpriv->urb = usb_alloc_urb(0, GFP_KERNEL);
959 devpriv->inbuf = kmalloc(SIZEINBUF, GFP_KERNEL);
963 ret = comedi_load_firmware(dev, &usb->dev, FIRMWARE,
964 usbduxfast_upload_firmware, 0);
968 ret = comedi_alloc_subdevices(dev, 1);
972 /* Analog Input subdevice */
973 s = &dev->subdevices[0];
974 dev->read_subdev = s;
975 s->type = COMEDI_SUBD_AI;
976 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
978 s->maxdata = 0x1000; /* 12-bit + 1 overflow bit */
979 s->range_table = &range_usbduxfast_ai_range;
980 s->insn_read = usbduxfast_ai_insn_read;
981 s->len_chanlist = s->n_chan;
982 s->do_cmdtest = usbduxfast_ai_cmdtest;
983 s->do_cmd = usbduxfast_ai_cmd;
984 s->cancel = usbduxfast_ai_cancel;
989 static void usbduxfast_detach(struct comedi_device *dev)
991 struct usb_interface *intf = comedi_to_usb_interface(dev);
992 struct usbduxfast_private *devpriv = dev->private;
997 mutex_lock(&devpriv->mut);
999 usb_set_intfdata(intf, NULL);
1002 /* waits until a running transfer is over */
1003 usb_kill_urb(devpriv->urb);
1005 kfree(devpriv->inbuf);
1006 usb_free_urb(devpriv->urb);
1009 kfree(devpriv->duxbuf);
1011 mutex_unlock(&devpriv->mut);
1014 static struct comedi_driver usbduxfast_driver = {
1015 .driver_name = "usbduxfast",
1016 .module = THIS_MODULE,
1017 .auto_attach = usbduxfast_auto_attach,
1018 .detach = usbduxfast_detach,
1021 static int usbduxfast_usb_probe(struct usb_interface *intf,
1022 const struct usb_device_id *id)
1024 return comedi_usb_auto_config(intf, &usbduxfast_driver, 0);
1027 static const struct usb_device_id usbduxfast_usb_table[] = {
1028 /* { USB_DEVICE(0x4b4, 0x8613) }, testing */
1029 { USB_DEVICE(0x13d8, 0x0010) }, /* real ID */
1030 { USB_DEVICE(0x13d8, 0x0011) }, /* real ID */
1033 MODULE_DEVICE_TABLE(usb, usbduxfast_usb_table);
1035 static struct usb_driver usbduxfast_usb_driver = {
1036 .name = "usbduxfast",
1037 .probe = usbduxfast_usb_probe,
1038 .disconnect = comedi_usb_auto_unconfig,
1039 .id_table = usbduxfast_usb_table,
1041 module_comedi_usb_driver(usbduxfast_driver, usbduxfast_usb_driver);
1043 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1044 MODULE_DESCRIPTION("USB-DUXfast, BerndPorr@f2s.com");
1045 MODULE_LICENSE("GPL");
1046 MODULE_FIRMWARE(FIRMWARE);