2 * Copyright (C) 2004-2014 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: 10 Oct 2014
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 * 0.9: Dropping the first data packet which seems to be from the last transfer.
35 * Buffer overflows in the FX2 are handed over to comedi.
36 * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
37 * Added insn command basically for testing. Sample rate is
39 * 0.99: Ian Abbott pointed out a bug which has been corrected. Thanks!
40 * 0.99a: added external trigger.
41 * 1.00: added firmware kernel request to the driver which fixed
42 * udev coldplug problem
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/slab.h>
48 #include <linux/input.h>
49 #include <linux/fcntl.h>
50 #include <linux/compiler.h>
51 #include "../comedi_usb.h"
54 * timeout for the USB-transfer
59 * constants for "firmware" upload and download
61 #define FIRMWARE "usbduxfast_firmware.bin"
62 #define FIRMWARE_MAX_LEN 0x2000
63 #define USBDUXFASTSUB_FIRMWARE 0xA0
64 #define VENDOR_DIR_IN 0xC0
65 #define VENDOR_DIR_OUT 0x40
68 * internal addresses of the 8051 processor
70 #define USBDUXFASTSUB_CPUCS 0xE600
73 * max lenghth of the transfer-buffer for software upload
78 * input endpoint number
83 * endpoint for the A/D channellist: bulk OUT
85 #define CHANNELLISTEP 4
90 #define NUMCHANNELS 32
93 * size of the waveform descriptor
98 * size of one A/D value
100 #define SIZEADIN (sizeof(int16_t))
103 * size of the input-buffer IN BYTES
105 #define SIZEINBUF 512
110 #define SIZEINSNBUF 512
113 * size of the buffer for the dux commands in bytes
115 #define SIZEOFDUXBUF 256
118 * number of in-URBs which receive the data: min=5
120 #define NUMOFINBUFFERSHIGH 10
123 * min delay steps for more than one channel
124 * basically when the mux gives up ;-)
126 * steps at 30MHz in the FX2
128 #define MIN_SAMPLING_PERIOD 9
131 * max number of 1/30MHz delay steps
133 #define MAX_SAMPLING_PERIOD 500
136 * number of received packets to ignore before we start handing data
137 * over to comedi, it's quad buffering and we have to ignore 4 packets
139 #define PACKETS_TO_IGNORE 4
144 static const struct comedi_lrange range_usbduxfast_ai_range = {
152 * private structure of one subdevice
154 * this is the structure which holds all the data of this driver
155 * one sub device just now: A/D
157 struct usbduxfast_private {
158 struct urb *urb; /* BULK-transfer handling: urb */
161 short int ai_cmd_running; /* asynchronous command is running */
162 int ignore; /* counter which ignores the first
164 struct semaphore sem;
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 uint8_t len, uint8_t op, uint8_t out,
196 struct usbduxfast_private *devpriv = dev->private;
198 /* Set the GPIF bytes, the first byte is the command byte */
199 devpriv->duxbuf[1 + 0x00 + index] = len;
200 devpriv->duxbuf[1 + 0x08 + index] = op;
201 devpriv->duxbuf[1 + 0x10 + index] = out;
202 devpriv->duxbuf[1 + 0x18 + index] = log;
205 static int usbduxfast_ai_stop(struct comedi_device *dev, int do_unlink)
207 struct usbduxfast_private *devpriv = dev->private;
210 devpriv->ai_cmd_running = 0;
212 if (do_unlink && devpriv->urb) {
213 /* kill the running transfer */
214 usb_kill_urb(devpriv->urb);
220 static int usbduxfast_ai_cancel(struct comedi_device *dev,
221 struct comedi_subdevice *s)
223 struct usbduxfast_private *devpriv = dev->private;
230 ret = usbduxfast_ai_stop(dev, 1);
236 static void usbduxfast_ai_handle_urb(struct comedi_device *dev,
237 struct comedi_subdevice *s,
240 struct usbduxfast_private *devpriv = dev->private;
241 struct comedi_async *async = s->async;
242 struct comedi_cmd *cmd = &async->cmd;
245 if (devpriv->ignore) {
248 unsigned int nsamples;
250 nsamples = comedi_bytes_to_samples(s, urb->actual_length);
251 nsamples = comedi_nsamples_left(s, nsamples);
252 comedi_buf_write_samples(s, urb->transfer_buffer, nsamples);
254 if (cmd->stop_src == TRIG_COUNT &&
255 async->scans_done >= cmd->stop_arg)
256 async->events |= COMEDI_CB_EOA;
259 /* if command is still running, resubmit urb for BULK transfer */
260 if (!(async->events & COMEDI_CB_CANCEL_MASK)) {
261 urb->dev = comedi_to_usb_dev(dev);
263 ret = usb_submit_urb(urb, GFP_ATOMIC);
265 dev_err(dev->class_dev, "urb resubm failed: %d", ret);
266 async->events |= COMEDI_CB_ERROR;
271 static void usbduxfast_ai_interrupt(struct urb *urb)
273 struct comedi_device *dev = urb->context;
274 struct comedi_subdevice *s = dev->read_subdev;
275 struct comedi_async *async = s->async;
276 struct usbduxfast_private *devpriv = dev->private;
278 /* exit if not running a command, do not resubmit urb */
279 if (!devpriv->ai_cmd_running)
282 switch (urb->status) {
284 usbduxfast_ai_handle_urb(dev, s, urb);
291 /* after an unlink command, unplug, ... etc */
292 async->events |= COMEDI_CB_ERROR;
297 dev_err(dev->class_dev,
298 "non-zero urb status received in ai intr context: %d\n",
300 async->events |= COMEDI_CB_ERROR;
305 * comedi_handle_events() cannot be used in this driver. The (*cancel)
306 * operation would unlink the urb.
308 if (async->events & COMEDI_CB_CANCEL_MASK)
309 usbduxfast_ai_stop(dev, 0);
311 comedi_event(dev, s);
314 static int usbduxfast_submit_urb(struct comedi_device *dev)
316 struct usb_device *usb = comedi_to_usb_dev(dev);
317 struct usbduxfast_private *devpriv = dev->private;
323 usb_fill_bulk_urb(devpriv->urb, usb, usb_rcvbulkpipe(usb, BULKINEP),
324 devpriv->inbuf, SIZEINBUF,
325 usbduxfast_ai_interrupt, dev);
327 ret = usb_submit_urb(devpriv->urb, GFP_ATOMIC);
329 dev_err(dev->class_dev, "usb_submit_urb error %d\n", ret);
335 static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
336 struct comedi_subdevice *s,
337 struct comedi_cmd *cmd)
341 int min_sample_period;
343 /* Step 1 : check if triggers are trivially valid */
345 err |= comedi_check_trigger_src(&cmd->start_src,
346 TRIG_NOW | TRIG_EXT | TRIG_INT);
347 err |= comedi_check_trigger_src(&cmd->scan_begin_src,
348 TRIG_FOLLOW | TRIG_EXT);
349 err |= comedi_check_trigger_src(&cmd->convert_src,
350 TRIG_TIMER | TRIG_EXT);
351 err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
352 err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
357 /* Step 2a : make sure trigger sources are unique */
359 err |= comedi_check_trigger_is_unique(cmd->start_src);
360 err |= comedi_check_trigger_is_unique(cmd->scan_begin_src);
361 err |= comedi_check_trigger_is_unique(cmd->convert_src);
362 err |= comedi_check_trigger_is_unique(cmd->stop_src);
364 /* Step 2b : and mutually compatible */
366 /* can't have external stop and start triggers at once */
367 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
373 /* Step 3: check if arguments are trivially valid */
375 err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
377 if (!cmd->chanlist_len)
380 err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
383 if (cmd->chanlist_len == 1)
384 min_sample_period = 1;
386 min_sample_period = MIN_SAMPLING_PERIOD;
388 if (cmd->convert_src == TRIG_TIMER) {
389 steps = cmd->convert_arg * 30;
390 if (steps < (min_sample_period * 1000))
391 steps = min_sample_period * 1000;
393 if (steps > (MAX_SAMPLING_PERIOD * 1000))
394 steps = MAX_SAMPLING_PERIOD * 1000;
398 err |= comedi_check_trigger_arg_is(&cmd->convert_arg, tmp);
402 switch (cmd->stop_src) {
404 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
407 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
410 * TRIG_EXT doesn't care since it doesn't trigger
411 * off a numbered channel
420 /* step 4: fix up any arguments */
425 static int usbduxfast_ai_inttrig(struct comedi_device *dev,
426 struct comedi_subdevice *s,
427 unsigned int trig_num)
429 struct usbduxfast_private *devpriv = dev->private;
430 struct comedi_cmd *cmd = &s->async->cmd;
436 if (trig_num != cmd->start_arg)
441 if (!devpriv->ai_cmd_running) {
442 devpriv->ai_cmd_running = 1;
443 ret = usbduxfast_submit_urb(dev);
445 dev_err(dev->class_dev, "urbSubmit: err=%d\n", ret);
446 devpriv->ai_cmd_running = 0;
450 s->async->inttrig = NULL;
452 dev_err(dev->class_dev, "ai is already running\n");
458 static int usbduxfast_ai_cmd(struct comedi_device *dev,
459 struct comedi_subdevice *s)
461 struct usbduxfast_private *devpriv = dev->private;
462 struct comedi_cmd *cmd = &s->async->cmd;
463 unsigned int chan, gain, rngmask = 0xff;
466 long steps, steps_tmp;
472 if (devpriv->ai_cmd_running) {
473 dev_err(dev->class_dev, "ai_cmd not possible\n");
479 * ignore the first buffers from the device if there
480 * is an error condition
482 devpriv->ignore = PACKETS_TO_IGNORE;
484 gain = CR_RANGE(cmd->chanlist[0]);
485 for (i = 0; i < cmd->chanlist_len; ++i) {
486 chan = CR_CHAN(cmd->chanlist[i]);
488 dev_err(dev->class_dev,
489 "channels are not consecutive\n");
493 if ((gain != CR_RANGE(cmd->chanlist[i]))
494 && (cmd->chanlist_len > 3)) {
495 dev_err(dev->class_dev,
496 "gain must be the same for all channels\n");
500 if (i >= NUMCHANNELS) {
501 dev_err(dev->class_dev, "chanlist too long\n");
506 if (cmd->convert_src == TRIG_TIMER)
507 steps = (cmd->convert_arg * 30) / 1000;
509 if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
510 dev_err(dev->class_dev,
511 "steps=%ld, scan_begin_arg=%d. Not properly tested by cmdtest?\n",
512 steps, cmd->scan_begin_arg);
516 if (steps > MAX_SAMPLING_PERIOD) {
517 dev_err(dev->class_dev, "sampling rate too low\n");
521 if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
522 && (cmd->chanlist_len != 16)) {
523 dev_err(dev->class_dev,
524 "TRIG_EXT only with 1 or 16 channels possible\n");
529 switch (cmd->chanlist_len) {
535 if (CR_RANGE(cmd->chanlist[0]) > 0)
536 rngmask = 0xff - 0x04;
541 * for external trigger: looping in this state until
542 * the RDY0 pin becomes zero
545 /* we loop here until ready has been set */
546 if (cmd->start_src == TRIG_EXT) {
547 /* branch back to state 0 */
548 /* deceision state w/o data */
550 usbduxfast_cmd_data(dev, 0, 0x01, 0x01, rngmask, 0x00);
551 } else { /* we just proceed to state 1 */
552 usbduxfast_cmd_data(dev, 0, 0x01, 0x00, rngmask, 0x00);
555 if (steps < MIN_SAMPLING_PERIOD) {
556 /* for fast single channel aqu without mux */
559 * we just stay here at state 1 and rexecute
560 * the same state this gives us 30MHz sampling
564 /* branch back to state 1 */
565 /* deceision state with data */
567 usbduxfast_cmd_data(dev, 1,
568 0x89, 0x03, rngmask, 0xff);
571 * we loop through two states: data and delay
576 usbduxfast_cmd_data(dev, 1, steps - 1,
577 0x02, rngmask, 0x00);
579 /* branch back to state 1 */
580 /* deceision state w/o data */
582 usbduxfast_cmd_data(dev, 2,
583 0x09, 0x01, rngmask, 0xff);
587 * we loop through 3 states: 2x delay and 1x data
588 * this gives a min sampling rate of 60kHz
591 /* we have 1 state with duration 1 */
594 /* do the first part of the delay */
595 usbduxfast_cmd_data(dev, 1,
596 steps / 2, 0x00, rngmask, 0x00);
598 /* and the second part */
599 usbduxfast_cmd_data(dev, 2, steps - steps / 2,
600 0x00, rngmask, 0x00);
602 /* get the data and branch back */
604 /* branch back to state 1 */
605 /* deceision state w data */
607 usbduxfast_cmd_data(dev, 3,
608 0x09, 0x03, rngmask, 0xff);
615 * commit data to the FIFO
618 if (CR_RANGE(cmd->chanlist[0]) > 0)
619 rngmask = 0xff - 0x04;
624 usbduxfast_cmd_data(dev, 0, 0x01, 0x02, rngmask, 0x00);
626 /* we have 1 state with duration 1: state 0 */
627 steps_tmp = steps - 1;
629 if (CR_RANGE(cmd->chanlist[1]) > 0)
630 rngmask = 0xff - 0x04;
634 /* do the first part of the delay */
636 usbduxfast_cmd_data(dev, 1, steps_tmp / 2,
637 0x00, 0xfe & rngmask, 0x00);
639 /* and the second part */
640 usbduxfast_cmd_data(dev, 2, steps_tmp - steps_tmp / 2,
641 0x00, rngmask, 0x00);
644 usbduxfast_cmd_data(dev, 3, 0x01, 0x02, rngmask, 0x00);
647 * we have 2 states with duration 1: step 6 and
650 steps_tmp = steps - 2;
652 if (CR_RANGE(cmd->chanlist[0]) > 0)
653 rngmask = 0xff - 0x04;
657 /* do the first part of the delay */
659 usbduxfast_cmd_data(dev, 4, steps_tmp / 2,
660 0x00, (0xff - 0x02) & rngmask, 0x00);
662 /* and the second part */
663 usbduxfast_cmd_data(dev, 5, steps_tmp - steps_tmp / 2,
664 0x00, rngmask, 0x00);
666 usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
673 for (j = 0; j < 1; j++) {
676 if (CR_RANGE(cmd->chanlist[j]) > 0)
677 rngmask = 0xff - 0x04;
681 * commit data to the FIFO and do the first part
686 usbduxfast_cmd_data(dev, index, steps / 2,
687 0x02, rngmask, 0x00);
689 if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
690 rngmask = 0xff - 0x04;
694 /* do the second part of the delay */
697 usbduxfast_cmd_data(dev, index + 1, steps - steps / 2,
698 0x00, 0xfe & rngmask, 0x00);
701 /* 2 steps with duration 1: the idele step and step 6: */
702 steps_tmp = steps - 2;
704 /* commit data to the FIFO and do the first part of the delay */
706 usbduxfast_cmd_data(dev, 4, steps_tmp / 2,
707 0x02, rngmask, 0x00);
709 if (CR_RANGE(cmd->chanlist[0]) > 0)
710 rngmask = 0xff - 0x04;
714 /* do the second part of the delay */
717 usbduxfast_cmd_data(dev, 5, steps_tmp - steps_tmp / 2,
718 0x00, (0xff - 0x02) & rngmask, 0x00);
720 usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
724 if (CR_RANGE(cmd->chanlist[0]) > 0)
725 rngmask = 0xff - 0x04;
729 if (cmd->start_src == TRIG_EXT) {
731 * we loop here until ready has been set
734 /* branch back to state 0 */
735 /* deceision state w/o data */
738 usbduxfast_cmd_data(dev, 0, 0x01, 0x01,
739 (0xff - 0x02) & rngmask, 0x00);
742 * we just proceed to state 1
745 /* 30us reset pulse */
747 usbduxfast_cmd_data(dev, 0, 0xff, 0x00,
748 (0xff - 0x02) & rngmask, 0x00);
751 /* commit data to the FIFO */
753 usbduxfast_cmd_data(dev, 1, 0x01, 0x02, rngmask, 0x00);
755 /* we have 2 states with duration 1 */
758 /* do the first part of the delay */
759 usbduxfast_cmd_data(dev, 2, steps / 2,
760 0x00, 0xfe & rngmask, 0x00);
762 /* and the second part */
763 usbduxfast_cmd_data(dev, 3, steps - steps / 2,
764 0x00, rngmask, 0x00);
766 /* branch back to state 1 */
767 /* deceision state w/o data */
769 usbduxfast_cmd_data(dev, 4, 0x09, 0x01, rngmask, 0xff);
774 dev_err(dev->class_dev, "unsupported combination of channels\n");
779 /* 0 means that the AD commands are sent */
780 result = usbduxfast_send_cmd(dev, SENDADCOMMANDS);
786 if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
787 /* enable this acquisition operation */
788 devpriv->ai_cmd_running = 1;
789 ret = usbduxfast_submit_urb(dev);
791 devpriv->ai_cmd_running = 0;
792 /* fixme: unlink here?? */
796 s->async->inttrig = NULL;
797 } else { /* TRIG_INT */
798 s->async->inttrig = usbduxfast_ai_inttrig;
806 * Mode 0 is used to get a single conversion on demand.
808 static int usbduxfast_ai_insn_read(struct comedi_device *dev,
809 struct comedi_subdevice *s,
810 struct comedi_insn *insn,
813 struct usb_device *usb = comedi_to_usb_dev(dev);
814 struct usbduxfast_private *devpriv = dev->private;
815 unsigned int chan = CR_CHAN(insn->chanspec);
816 unsigned int range = CR_RANGE(insn->chanspec);
817 uint8_t rngmask = range ? (0xff - 0x04) : 0xff;
818 int i, j, n, actual_length;
823 if (devpriv->ai_cmd_running) {
824 dev_err(dev->class_dev,
825 "ai_insn_read not possible, async cmd is running\n");
830 /* set command for the first channel */
832 /* commit data to the FIFO */
834 usbduxfast_cmd_data(dev, 0, 0x01, 0x02, rngmask, 0x00);
836 /* do the first part of the delay */
837 usbduxfast_cmd_data(dev, 1, 0x0c, 0x00, 0xfe & rngmask, 0x00);
838 usbduxfast_cmd_data(dev, 2, 0x01, 0x00, 0xfe & rngmask, 0x00);
839 usbduxfast_cmd_data(dev, 3, 0x01, 0x00, 0xfe & rngmask, 0x00);
840 usbduxfast_cmd_data(dev, 4, 0x01, 0x00, 0xfe & rngmask, 0x00);
843 usbduxfast_cmd_data(dev, 5, 0x0c, 0x00, rngmask, 0x00);
844 usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
846 ret = usbduxfast_send_cmd(dev, SENDADCOMMANDS);
852 for (i = 0; i < PACKETS_TO_IGNORE; i++) {
853 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, BULKINEP),
854 devpriv->inbuf, SIZEINBUF,
855 &actual_length, 10000);
857 dev_err(dev->class_dev, "insn timeout, no data\n");
863 for (i = 0; i < insn->n;) {
864 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, BULKINEP),
865 devpriv->inbuf, SIZEINBUF,
866 &actual_length, 10000);
868 dev_err(dev->class_dev, "insn data error: %d\n", ret);
872 n = actual_length / sizeof(uint16_t);
874 dev_err(dev->class_dev, "insn data packet corrupted\n");
878 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
879 data[i] = ((uint16_t *) (devpriv->inbuf))[j];
889 static int usbduxfast_attach_common(struct comedi_device *dev)
891 struct usbduxfast_private *devpriv = dev->private;
892 struct comedi_subdevice *s;
897 ret = comedi_alloc_subdevices(dev, 1);
903 /* Analog Input subdevice */
904 s = &dev->subdevices[0];
905 dev->read_subdev = s;
906 s->type = COMEDI_SUBD_AI;
907 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
909 s->len_chanlist = 16;
910 s->insn_read = usbduxfast_ai_insn_read;
911 s->do_cmdtest = usbduxfast_ai_cmdtest;
912 s->do_cmd = usbduxfast_ai_cmd;
913 s->cancel = usbduxfast_ai_cancel;
915 s->range_table = &range_usbduxfast_ai_range;
922 static int usbduxfast_upload_firmware(struct comedi_device *dev,
923 const u8 *data, size_t size,
924 unsigned long context)
926 struct usb_device *usb = comedi_to_usb_dev(dev);
934 if (size > FIRMWARE_MAX_LEN) {
935 dev_err(dev->class_dev, "firmware binary too large for FX2\n");
939 /* we generate a local buffer for the firmware */
940 buf = kmemdup(data, size, GFP_KERNEL);
944 /* we need a malloc'ed buffer for usb_control_msg() */
945 tmp = kmalloc(1, GFP_KERNEL);
951 /* stop the current firmware on the device */
952 *tmp = 1; /* 7f92 to one */
953 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
954 USBDUXFASTSUB_FIRMWARE,
956 USBDUXFASTSUB_CPUCS, 0x0000,
960 dev_err(dev->class_dev, "can not stop firmware\n");
964 /* upload the new firmware to the device */
965 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
966 USBDUXFASTSUB_FIRMWARE,
972 dev_err(dev->class_dev, "firmware upload failed\n");
976 /* start the new firmware on the device */
977 *tmp = 0; /* 7f92 to zero */
978 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
979 USBDUXFASTSUB_FIRMWARE,
981 USBDUXFASTSUB_CPUCS, 0x0000,
985 dev_err(dev->class_dev, "can not start firmware\n");
993 static int usbduxfast_auto_attach(struct comedi_device *dev,
994 unsigned long context_unused)
996 struct usb_interface *intf = comedi_to_usb_interface(dev);
997 struct usb_device *usb = comedi_to_usb_dev(dev);
998 struct usbduxfast_private *devpriv;
1001 if (usb->speed != USB_SPEED_HIGH) {
1002 dev_err(dev->class_dev,
1003 "This driver needs USB 2.0 to operate. Aborting...\n");
1007 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1011 sema_init(&devpriv->sem, 1);
1012 usb_set_intfdata(intf, devpriv);
1014 devpriv->duxbuf = kmalloc(SIZEOFDUXBUF, GFP_KERNEL);
1015 if (!devpriv->duxbuf)
1018 ret = usb_set_interface(usb,
1019 intf->altsetting->desc.bInterfaceNumber, 1);
1021 dev_err(dev->class_dev,
1022 "could not switch to alternate setting 1\n");
1026 devpriv->urb = usb_alloc_urb(0, GFP_KERNEL);
1027 if (!devpriv->urb) {
1028 dev_err(dev->class_dev, "Could not alloc. urb\n");
1032 devpriv->inbuf = kmalloc(SIZEINBUF, GFP_KERNEL);
1033 if (!devpriv->inbuf)
1036 ret = comedi_load_firmware(dev, &usb->dev, FIRMWARE,
1037 usbduxfast_upload_firmware, 0);
1041 return usbduxfast_attach_common(dev);
1044 static void usbduxfast_detach(struct comedi_device *dev)
1046 struct usb_interface *intf = comedi_to_usb_interface(dev);
1047 struct usbduxfast_private *devpriv = dev->private;
1052 down(&devpriv->sem);
1054 usb_set_intfdata(intf, NULL);
1057 /* waits until a running transfer is over */
1058 usb_kill_urb(devpriv->urb);
1060 kfree(devpriv->inbuf);
1061 devpriv->inbuf = NULL;
1063 usb_free_urb(devpriv->urb);
1064 devpriv->urb = NULL;
1067 kfree(devpriv->duxbuf);
1068 devpriv->duxbuf = NULL;
1070 devpriv->ai_cmd_running = 0;
1075 static struct comedi_driver usbduxfast_driver = {
1076 .driver_name = "usbduxfast",
1077 .module = THIS_MODULE,
1078 .auto_attach = usbduxfast_auto_attach,
1079 .detach = usbduxfast_detach,
1082 static int usbduxfast_usb_probe(struct usb_interface *intf,
1083 const struct usb_device_id *id)
1085 return comedi_usb_auto_config(intf, &usbduxfast_driver, 0);
1088 static const struct usb_device_id usbduxfast_usb_table[] = {
1089 /* { USB_DEVICE(0x4b4, 0x8613) }, testing */
1090 { USB_DEVICE(0x13d8, 0x0010) }, /* real ID */
1091 { USB_DEVICE(0x13d8, 0x0011) }, /* real ID */
1094 MODULE_DEVICE_TABLE(usb, usbduxfast_usb_table);
1096 static struct usb_driver usbduxfast_usb_driver = {
1097 .name = "usbduxfast",
1098 .probe = usbduxfast_usb_probe,
1099 .disconnect = comedi_usb_auto_unconfig,
1100 .id_table = usbduxfast_usb_table,
1102 module_comedi_usb_driver(usbduxfast_driver, usbduxfast_usb_driver);
1104 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1105 MODULE_DESCRIPTION("USB-DUXfast, BerndPorr@f2s.com");
1106 MODULE_LICENSE("GPL");
1107 MODULE_FIRMWARE(FIRMWARE);