2 comedi/drivers/usbdux.c
3 Copyright (C) 2003-2007 Bernd Porr, Bernd.Porr@f2s.com
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
17 Description: University of Stirling USB DAQ & INCITE Technology Limited
18 Devices: [ITL] USB-DUX (usbdux.o)
19 Author: Bernd Porr <BerndPorr@f2s.com>
22 Configuration options:
23 You have to upload firmware with the -i option. The
24 firmware is usually installed under /usr/share/usb or
25 /usr/local/share/usb or /lib/firmware.
27 Connection scheme for the counter at the digital port:
28 0=/CLK0, 1=UP/DOWN0, 2=RESET0, 4=/CLK1, 5=UP/DOWN1, 6=RESET1.
29 The sampling rate of the counter is approximately 500Hz.
31 Please note that under USB2.0 the length of the channel list determines
32 the max sampling rate. If you sample only one channel you get 8kHz
33 sampling rate. If you sample two channels you get 4kHz and so on.
36 * I must give credit here to Chris Baugher who
37 * wrote the driver for AT-MIO-16d. I used some parts of this
38 * driver. I also must give credits to David Brownell
39 * who supported me with the USB development.
45 * 0.94: D/A output should work now with any channel list combinations
46 * 0.95: .owner commented out for kernel vers below 2.4.19
47 * sanity checks in ai/ao_cmd
48 * 0.96: trying to get it working with 2.6, moved all memory alloc to comedi's
49 * attach final USB IDs
50 * moved memory allocation completely to the corresponding comedi
51 * functions firmware upload is by fxload and no longer by comedi (due to
53 * 0.97: USB IDs received, adjusted table
54 * 0.98: SMP, locking, memory alloc: moved all usb memory alloc
55 * to the usb subsystem and moved all comedi related memory
57 * | kernel | registration | usbdux-usb | usbdux-comedi | comedi |
58 * 0.99: USB 2.0: changed protocol to isochronous transfer
59 * IRQ transfer is too buggy and too risky in 2.0
60 * for the high speed ISO transfer is now a working version
62 * 0.99b: Increased the iso transfer buffer for high sp.to 10 buffers. Some VIA
63 * chipsets miss out IRQs. Deeper buffering is needed.
64 * 1.00: full USB 2.0 support for the A/D converter. Now: max 8kHz sampling
66 * Firmware vers 1.00 is needed for this.
67 * Two 16 bit up/down/reset counter with a sampling rate of 1kHz
68 * And loads of cleaning up, in particular streamlining the
70 * 1.1: moved EP4 transfers to EP1 to make space for a PWM output on EP4
71 * 1.2: added PWM support via EP4
72 * 2.0: PWM seems to be stable and is not interfering with the other functions
73 * 2.1: changed PWM API
74 * 2.2: added firmware kernel request to fix an udev problem
75 * 2.3: corrected a bug in bulk timeouts which were far too short
76 * 2.4: fixed a bug which causes the driver to hang when it ran out of data.
77 * Thanks to Jan-Matthias Braun and Ian to spot the bug and fix it.
81 #include <linux/kernel.h>
82 #include <linux/module.h>
83 #include <linux/init.h>
84 #include <linux/slab.h>
85 #include <linux/input.h>
86 #include <linux/usb.h>
87 #include <linux/fcntl.h>
88 #include <linux/compiler.h>
90 #include "../comedidev.h"
92 #include "comedi_fc.h"
94 /* constants for firmware upload and download */
95 #define USBDUX_FIRMWARE "usbdux_firmware.bin"
96 #define USBDUX_FIRMWARE_MAX_LEN 0x2000
97 #define USBDUX_FIRMWARE_CMD 0xa0
98 #define VENDOR_DIR_IN 0xc0
99 #define VENDOR_DIR_OUT 0x40
100 #define USBDUX_CPU_CS 0xe600
102 /* timeout for the USB-transfer in ms */
103 #define BULK_TIMEOUT 1000
105 /* 300Hz max frequ under PWM */
106 #define MIN_PWM_PERIOD ((long)(1E9/300))
108 /* Default PWM frequency */
109 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
111 /* Number of channels */
112 #define NUMCHANNELS 8
114 /* Size of one A/D value */
115 #define SIZEADIN ((sizeof(int16_t)))
118 * Size of the input-buffer IN BYTES
119 * Always multiple of 8 for 8 microframes which is needed in the highspeed mode
121 #define SIZEINBUF ((8*SIZEADIN))
124 #define SIZEINSNBUF 16
126 /* Number of DA channels */
127 #define NUMOUTCHANNELS 8
129 /* size of one value for the D/A converter: channel and value */
130 #define SIZEDAOUT ((sizeof(int8_t)+sizeof(int16_t)))
133 * Size of the output-buffer in bytes
134 * Actually only the first 4 triplets are used but for the
135 * high speed mode we need to pad it to 8 (microframes).
137 #define SIZEOUTBUF ((8*SIZEDAOUT))
140 * Size of the buffer for the dux commands: just now max size is determined
141 * by the analogue out + command byte + panic bytes...
143 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
145 /* Number of in-URBs which receive the data: min=2 */
146 #define NUMOFINBUFFERSFULL 5
148 /* Number of out-URBs which send the data: min=2 */
149 #define NUMOFOUTBUFFERSFULL 5
151 /* Number of in-URBs which receive the data: min=5 */
152 /* must have more buffers due to buggy USB ctr */
153 #define NUMOFINBUFFERSHIGH 10
155 /* Number of out-URBs which send the data: min=5 */
156 /* must have more buffers due to buggy USB ctr */
157 #define NUMOFOUTBUFFERSHIGH 10
159 /* number of retries to get the right dux command */
162 static const struct comedi_lrange range_usbdux_ai_range = {
165 BIP_RANGE(4.096 / 2),
171 static const struct comedi_lrange range_usbdux_ao_range = {
178 struct usbdux_private {
179 /* actual number of in-buffers */
181 /* actual number of out-buffers */
183 /* ISO-transfer handling: buffers */
185 struct urb **urb_out;
186 /* pwm-transfer handling */
189 unsigned int pwm_period;
190 /* PWM internal delay for the GPIF in the FX2 */
192 /* size of the PWM buffer which holds the bit pattern */
194 /* input buffer for the ISO-transfer */
196 /* input buffer for single insn */
197 int16_t *insn_buffer;
198 /* output buffer for single DA outputs */
201 unsigned int high_speed:1;
202 unsigned int ai_cmd_running:1;
203 unsigned int ai_continous:1;
204 unsigned int ao_cmd_running:1;
205 unsigned int ao_continous:1;
206 unsigned int pwm_cmd_running:1;
208 /* number of samples to acquire */
211 /* time between samples in units of the timer */
212 unsigned int ai_timer;
213 unsigned int ao_timer;
214 /* counter between aquisitions */
215 unsigned int ai_counter;
216 unsigned int ao_counter;
217 /* interval in frames/uframes */
218 unsigned int ai_interval;
220 int8_t *dac_commands;
222 int8_t *dux_commands;
223 struct semaphore sem;
226 static void usbduxsub_unlink_inurbs(struct comedi_device *dev)
228 struct usbdux_private *devpriv = dev->private;
231 if (devpriv->urb_in) {
232 for (i = 0; i < devpriv->num_in_buffers; i++)
233 usb_kill_urb(devpriv->urb_in[i]);
237 static void usbdux_ai_stop(struct comedi_device *dev, int do_unlink)
239 struct usbdux_private *devpriv = dev->private;
242 usbduxsub_unlink_inurbs(dev);
244 devpriv->ai_cmd_running = 0;
247 static int usbdux_ai_cancel(struct comedi_device *dev,
248 struct comedi_subdevice *s)
250 struct usbdux_private *devpriv = dev->private;
252 /* prevent other CPUs from submitting new commands just now */
254 /* unlink only if the urb really has been submitted */
255 usbdux_ai_stop(dev, devpriv->ai_cmd_running);
261 /* analogue IN - interrupt service routine */
262 static void usbduxsub_ai_isoc_irq(struct urb *urb)
264 struct comedi_device *dev = urb->context;
265 struct comedi_subdevice *s = dev->read_subdev;
266 struct usbdux_private *devpriv = dev->private;
269 /* first we test if something unusual has just happened */
270 switch (urb->status) {
272 /* copy the result in the transfer buffer */
273 memcpy(devpriv->in_buffer, urb->transfer_buffer, SIZEINBUF);
276 /* error in the ISOchronous data */
277 /* we don't copy the data into the transfer buffer */
278 /* and recycle the last data byte */
279 dev_dbg(dev->class_dev, "CRC error in ISO IN stream\n");
286 /* happens after an unlink command */
287 if (devpriv->ai_cmd_running) {
288 s->async->events |= COMEDI_CB_EOA;
289 s->async->events |= COMEDI_CB_ERROR;
290 comedi_event(dev, s);
291 /* stop the transfer w/o unlink */
292 usbdux_ai_stop(dev, 0);
297 /* a real error on the bus */
298 /* pass error to comedi if we are really running a command */
299 if (devpriv->ai_cmd_running) {
300 dev_err(dev->class_dev,
301 "Non-zero urb status received in ai intr context: %d\n",
303 s->async->events |= COMEDI_CB_EOA;
304 s->async->events |= COMEDI_CB_ERROR;
305 comedi_event(dev, s);
306 /* don't do an unlink here */
307 usbdux_ai_stop(dev, 0);
313 * at this point we are reasonably sure that nothing dodgy has happened
314 * are we running a command?
316 if (unlikely(!devpriv->ai_cmd_running)) {
318 * not running a command, do not continue execution if no
319 * asynchronous command is running in particular not resubmit
324 urb->dev = comedi_to_usb_dev(dev);
326 /* resubmit the urb */
327 err = usb_submit_urb(urb, GFP_ATOMIC);
328 if (unlikely(err < 0)) {
329 dev_err(dev->class_dev,
330 "urb resubmit failed in int-context! err=%d\n", err);
331 if (err == -EL2NSYNC)
332 dev_err(dev->class_dev,
333 "buggy USB host controller or bug in IRQ handler!\n");
334 s->async->events |= COMEDI_CB_EOA;
335 s->async->events |= COMEDI_CB_ERROR;
336 comedi_event(dev, s);
337 /* don't do an unlink here */
338 usbdux_ai_stop(dev, 0);
342 devpriv->ai_counter--;
343 if (likely(devpriv->ai_counter > 0))
346 /* timer zero, transfer measurements to comedi */
347 devpriv->ai_counter = devpriv->ai_timer;
349 /* test, if we transmit only a fixed number of samples */
350 if (!devpriv->ai_continous) {
351 /* not continuous, fixed number of samples */
352 devpriv->ai_sample_count--;
353 /* all samples received? */
354 if (devpriv->ai_sample_count < 0) {
355 /* prevent a resubmit next time */
356 usbdux_ai_stop(dev, 0);
357 /* say comedi that the acquistion is over */
358 s->async->events |= COMEDI_CB_EOA;
359 comedi_event(dev, s);
363 /* get the data from the USB bus and hand it over to comedi */
364 n = s->async->cmd.chanlist_len;
365 for (i = 0; i < n; i++) {
367 if (CR_RANGE(s->async->cmd.chanlist[i]) <= 1) {
368 err = comedi_buf_put(s->async,
369 le16_to_cpu(devpriv->in_buffer[i]) ^ 0x800);
371 err = comedi_buf_put(s->async,
372 le16_to_cpu(devpriv->in_buffer[i]));
374 if (unlikely(err == 0)) {
375 /* buffer overflow */
376 usbdux_ai_stop(dev, 0);
380 /* tell comedi that data is there */
381 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
382 comedi_event(dev, s);
385 static void usbduxsub_unlink_outurbs(struct comedi_device *dev)
387 struct usbdux_private *devpriv = dev->private;
390 if (devpriv->urb_out) {
391 for (i = 0; i < devpriv->num_out_buffers; i++)
392 usb_kill_urb(devpriv->urb_out[i]);
396 static void usbdux_ao_stop(struct comedi_device *dev, int do_unlink)
398 struct usbdux_private *devpriv = dev->private;
401 usbduxsub_unlink_outurbs(dev);
403 devpriv->ao_cmd_running = 0;
406 static int usbdux_ao_cancel(struct comedi_device *dev,
407 struct comedi_subdevice *s)
409 struct usbdux_private *devpriv = dev->private;
411 /* prevent other CPUs from submitting a command just now */
413 /* unlink only if it is really running */
414 usbdux_ao_stop(dev, devpriv->ao_cmd_running);
420 static void usbduxsub_ao_isoc_irq(struct urb *urb)
422 struct comedi_device *dev = urb->context;
423 struct comedi_subdevice *s = dev->write_subdev;
424 struct usbdux_private *devpriv = dev->private;
428 switch (urb->status) {
437 /* after an unlink command, unplug, ... etc */
438 /* no unlink needed here. Already shutting down. */
439 if (devpriv->ao_cmd_running) {
440 s->async->events |= COMEDI_CB_EOA;
441 comedi_event(dev, s);
442 usbdux_ao_stop(dev, 0);
448 if (devpriv->ao_cmd_running) {
449 dev_err(dev->class_dev,
450 "Non-zero urb status received in ao intr context: %d\n",
452 s->async->events |= COMEDI_CB_ERROR;
453 s->async->events |= COMEDI_CB_EOA;
454 comedi_event(dev, s);
455 /* we do an unlink if we are in the high speed mode */
456 usbdux_ao_stop(dev, 0);
461 /* are we actually running? */
462 if (!devpriv->ao_cmd_running)
465 /* normal operation: executing a command in this subdevice */
466 devpriv->ao_counter--;
467 if ((int)devpriv->ao_counter <= 0) {
469 devpriv->ao_counter = devpriv->ao_timer;
471 /* handle non continous acquisition */
472 if (!devpriv->ao_continous) {
473 /* fixed number of samples */
474 devpriv->ao_sample_count--;
475 if (devpriv->ao_sample_count < 0) {
476 /* all samples transmitted */
477 usbdux_ao_stop(dev, 0);
478 s->async->events |= COMEDI_CB_EOA;
479 comedi_event(dev, s);
480 /* no resubmit of the urb */
484 /* transmit data to the USB bus */
485 ((uint8_t *) (urb->transfer_buffer))[0] =
486 s->async->cmd.chanlist_len;
487 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
489 if (i >= NUMOUTCHANNELS)
492 /* pointer to the DA */
494 (&(((int8_t *) urb->transfer_buffer)[i * 3 + 1]));
495 /* get the data from comedi */
496 ret = comedi_buf_get(s->async, &temp);
498 datap[1] = temp >> 8;
499 datap[2] = devpriv->dac_commands[i];
501 dev_err(&urb->dev->dev,
502 "comedi: buffer underflow\n");
503 s->async->events |= COMEDI_CB_EOA;
504 s->async->events |= COMEDI_CB_OVERFLOW;
506 /* transmit data to comedi */
507 s->async->events |= COMEDI_CB_BLOCK;
508 comedi_event(dev, s);
511 urb->transfer_buffer_length = SIZEOUTBUF;
512 urb->dev = comedi_to_usb_dev(dev);
514 if (devpriv->ao_cmd_running) {
515 if (devpriv->high_speed)
516 urb->interval = 8; /* uframes */
518 urb->interval = 1; /* frames */
519 urb->number_of_packets = 1;
520 urb->iso_frame_desc[0].offset = 0;
521 urb->iso_frame_desc[0].length = SIZEOUTBUF;
522 urb->iso_frame_desc[0].status = 0;
523 ret = usb_submit_urb(urb, GFP_ATOMIC);
525 dev_err(dev->class_dev,
526 "ao urb resubm failed in int-cont. ret=%d",
529 dev_err(dev->class_dev,
530 "buggy USB host controller or bug in IRQ handling!\n");
532 s->async->events |= COMEDI_CB_EOA;
533 s->async->events |= COMEDI_CB_ERROR;
534 comedi_event(dev, s);
535 /* don't do an unlink here */
536 usbdux_ao_stop(dev, 0);
541 static int usbduxsub_submit_inurbs(struct comedi_device *dev)
543 struct usb_device *usb = comedi_to_usb_dev(dev);
544 struct usbdux_private *devpriv = dev->private;
549 /* Submit all URBs and start the transfer on the bus */
550 for (i = 0; i < devpriv->num_in_buffers; i++) {
551 urb = devpriv->urb_in[i];
553 /* in case of a resubmission after an unlink... */
554 urb->interval = devpriv->ai_interval;
558 urb->transfer_flags = URB_ISO_ASAP;
560 ret = usb_submit_urb(urb, GFP_ATOMIC);
567 static int usbduxsub_submit_outurbs(struct comedi_device *dev)
569 struct usb_device *usb = comedi_to_usb_dev(dev);
570 struct usbdux_private *devpriv = dev->private;
575 for (i = 0; i < devpriv->num_out_buffers; i++) {
576 urb = devpriv->urb_out[i];
578 /* in case of a resubmission after an unlink... */
582 urb->transfer_flags = URB_ISO_ASAP;
584 ret = usb_submit_urb(urb, GFP_ATOMIC);
591 static int usbdux_ai_cmdtest(struct comedi_device *dev,
592 struct comedi_subdevice *s, struct comedi_cmd *cmd)
594 struct usbdux_private *this_usbduxsub = dev->private;
596 unsigned int tmp_timer;
598 /* Step 1 : check if triggers are trivially valid */
600 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
601 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
602 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
603 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
604 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
609 /* Step 2a : make sure trigger sources are unique */
611 err |= cfc_check_trigger_is_unique(cmd->start_src);
612 err |= cfc_check_trigger_is_unique(cmd->stop_src);
614 /* Step 2b : and mutually compatible */
619 /* Step 3: check if arguments are trivially valid */
621 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
623 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
624 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
626 if (cmd->scan_begin_src == TRIG_TIMER) {
627 if (this_usbduxsub->high_speed) {
629 * In high speed mode microframes are possible.
630 * However, during one microframe we can roughly
631 * sample one channel. Thus, the more channels
632 * are in the channel list the more time we need.
635 /* find a power of 2 for the number of channels */
636 while (i < (cmd->chanlist_len))
639 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
641 /* now calc the real sampling rate with all the
644 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
648 /* 1kHz scans every USB frame */
649 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
652 * calc the real sampling rate with the rounding errors
654 tmp_timer = ((unsigned int)(cmd->scan_begin_arg /
657 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg,
661 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
663 if (cmd->stop_src == TRIG_COUNT) {
664 /* any count is allowed */
667 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
677 * creates the ADC command for the MAX1271
678 * range is the range value from comedi
680 static int8_t create_adc_command(unsigned int chan, int range)
682 int8_t p = (range <= 1);
683 int8_t r = ((range % 2) == 0);
684 return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
687 /* bulk transfers to usbdux */
689 #define SENDADCOMMANDS 0
690 #define SENDDACOMMANDS 1
691 #define SENDDIOCONFIGCOMMAND 2
692 #define SENDDIOBITSCOMMAND 3
693 #define SENDSINGLEAD 4
694 #define READCOUNTERCOMMAND 5
695 #define WRITECOUNTERCOMMAND 6
699 static int send_dux_commands(struct comedi_device *dev, int cmd_type)
701 struct usb_device *usb = comedi_to_usb_dev(dev);
702 struct usbdux_private *devpriv = dev->private;
705 devpriv->dux_commands[0] = cmd_type;
707 return usb_bulk_msg(usb, usb_sndbulkpipe(usb, 1),
708 devpriv->dux_commands, SIZEOFDUXBUFFER,
709 &nsent, BULK_TIMEOUT);
712 static int receive_dux_commands(struct comedi_device *dev, int command)
714 struct usb_device *usb = comedi_to_usb_dev(dev);
715 struct usbdux_private *devpriv = dev->private;
720 for (i = 0; i < RETRIES; i++) {
721 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, 8),
722 devpriv->insn_buffer, SIZEINSNBUF,
723 &nrec, BULK_TIMEOUT);
726 if (le16_to_cpu(devpriv->insn_buffer[0]) == command)
729 /* command not received */
733 static int usbdux_ai_inttrig(struct comedi_device *dev,
734 struct comedi_subdevice *s,
735 unsigned int trignum)
737 struct usbdux_private *devpriv = dev->private;
745 if (!devpriv->ai_cmd_running) {
746 devpriv->ai_cmd_running = 1;
747 ret = usbduxsub_submit_inurbs(dev);
749 devpriv->ai_cmd_running = 0;
752 s->async->inttrig = NULL;
762 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
764 struct usbdux_private *devpriv = dev->private;
765 struct comedi_cmd *cmd = &s->async->cmd;
766 int len = cmd->chanlist_len;
770 /* block other CPUs from starting an ai_cmd */
773 if (devpriv->ai_cmd_running)
776 /* set current channel of the running acquisition to zero */
777 s->async->cur_chan = 0;
779 devpriv->dux_commands[1] = len;
780 for (i = 0; i < len; ++i) {
781 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
782 unsigned int range = CR_RANGE(cmd->chanlist[i]);
784 if (i >= NUMCHANNELS)
787 devpriv->dux_commands[i + 2] = create_adc_command(chan, range);
790 ret = send_dux_commands(dev, SENDADCOMMANDS);
794 if (devpriv->high_speed) {
796 * every channel gets a time window of 125us. Thus, if we
797 * sample all 8 channels we need 1ms. If we sample only one
798 * channel we need only 125us
800 devpriv->ai_interval = 1;
801 /* find a power of 2 for the interval */
802 while (devpriv->ai_interval < len)
803 devpriv->ai_interval *= 2;
805 devpriv->ai_timer = cmd->scan_begin_arg /
806 (125000 * devpriv->ai_interval);
808 /* interval always 1ms */
809 devpriv->ai_interval = 1;
810 devpriv->ai_timer = cmd->scan_begin_arg / 1000000;
812 if (devpriv->ai_timer < 1) {
817 devpriv->ai_counter = devpriv->ai_timer;
819 if (cmd->stop_src == TRIG_COUNT) {
820 /* data arrives as one packet */
821 devpriv->ai_sample_count = cmd->stop_arg;
822 devpriv->ai_continous = 0;
824 /* continous acquisition */
825 devpriv->ai_continous = 1;
826 devpriv->ai_sample_count = 0;
829 if (cmd->start_src == TRIG_NOW) {
830 /* enable this acquisition operation */
831 devpriv->ai_cmd_running = 1;
832 ret = usbduxsub_submit_inurbs(dev);
834 devpriv->ai_cmd_running = 0;
835 /* fixme: unlink here?? */
838 s->async->inttrig = NULL;
841 /* don't enable the acquision operation */
842 /* wait for an internal signal */
843 s->async->inttrig = usbdux_ai_inttrig;
852 /* Mode 0 is used to get a single conversion on demand */
853 static int usbdux_ai_insn_read(struct comedi_device *dev,
854 struct comedi_subdevice *s,
855 struct comedi_insn *insn,
858 struct usbdux_private *devpriv = dev->private;
859 unsigned int chan = CR_CHAN(insn->chanspec);
860 unsigned int range = CR_RANGE(insn->chanspec);
867 if (devpriv->ai_cmd_running)
870 /* set command for the first channel */
871 devpriv->dux_commands[1] = create_adc_command(chan, range);
874 ret = send_dux_commands(dev, SENDSINGLEAD);
878 for (i = 0; i < insn->n; i++) {
879 ret = receive_dux_commands(dev, SENDSINGLEAD);
883 val = le16_to_cpu(devpriv->insn_buffer[1]);
885 /* bipolar data is two's-complement */
886 if (comedi_range_is_bipolar(s, range))
887 val ^= ((s->maxdata + 1) >> 1);
895 return ret ? ret : insn->n;
898 static int usbdux_ao_insn_read(struct comedi_device *dev,
899 struct comedi_subdevice *s,
900 struct comedi_insn *insn,
903 struct usbdux_private *devpriv = dev->private;
904 unsigned int chan = CR_CHAN(insn->chanspec);
908 for (i = 0; i < insn->n; i++)
909 data[i] = devpriv->out_buffer[chan];
915 static int usbdux_ao_insn_write(struct comedi_device *dev,
916 struct comedi_subdevice *s,
917 struct comedi_insn *insn,
920 struct usbdux_private *devpriv = dev->private;
921 unsigned int chan = CR_CHAN(insn->chanspec);
922 unsigned int val = devpriv->out_buffer[chan];
923 int16_t *p = (int16_t *)&devpriv->dux_commands[2];
929 if (devpriv->ao_cmd_running)
932 /* number of channels: 1 */
933 devpriv->dux_commands[1] = 1;
935 devpriv->dux_commands[4] = chan << 6;
937 for (i = 0; i < insn->n; i++) {
940 /* one 16 bit value */
941 *p = cpu_to_le16(val);
943 ret = send_dux_commands(dev, SENDDACOMMANDS);
947 devpriv->out_buffer[chan] = val;
952 return ret ? ret : insn->n;
955 static int usbdux_ao_inttrig(struct comedi_device *dev,
956 struct comedi_subdevice *s,
957 unsigned int trignum)
959 struct usbdux_private *devpriv = dev->private;
967 if (!devpriv->ao_cmd_running) {
968 devpriv->ao_cmd_running = 1;
969 ret = usbduxsub_submit_outurbs(dev);
971 devpriv->ao_cmd_running = 0;
974 s->async->inttrig = NULL;
984 static int usbdux_ao_cmdtest(struct comedi_device *dev,
985 struct comedi_subdevice *s, struct comedi_cmd *cmd)
987 struct usbdux_private *this_usbduxsub = dev->private;
994 /* Step 1 : check if triggers are trivially valid */
996 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
998 if (0) { /* (this_usbduxsub->high_speed) */
999 /* the sampling rate is set by the coversion rate */
1000 flags = TRIG_FOLLOW;
1002 /* start a new scan (output at once) with a timer */
1005 err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
1007 if (0) { /* (this_usbduxsub->high_speed) */
1009 * in usb-2.0 only one conversion it transmitted
1015 * all conversion events happen simultaneously with
1020 err |= cfc_check_trigger_src(&cmd->convert_src, flags);
1022 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
1023 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
1028 /* Step 2a : make sure trigger sources are unique */
1030 err |= cfc_check_trigger_is_unique(cmd->start_src);
1031 err |= cfc_check_trigger_is_unique(cmd->stop_src);
1033 /* Step 2b : and mutually compatible */
1038 /* Step 3: check if arguments are trivially valid */
1040 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
1042 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
1043 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
1045 if (cmd->scan_begin_src == TRIG_TIMER)
1046 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
1049 /* not used now, is for later use */
1050 if (cmd->convert_src == TRIG_TIMER)
1051 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
1053 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
1055 if (cmd->stop_src == TRIG_COUNT) {
1056 /* any count is allowed */
1059 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1068 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1070 struct usbdux_private *devpriv = dev->private;
1071 struct comedi_cmd *cmd = &s->async->cmd;
1075 down(&devpriv->sem);
1077 if (devpriv->ao_cmd_running)
1080 /* set current channel of the running acquisition to zero */
1081 s->async->cur_chan = 0;
1083 for (i = 0; i < cmd->chanlist_len; ++i) {
1084 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
1086 if (i >= NUMOUTCHANNELS)
1089 devpriv->dac_commands[i] = chan << 6;
1092 /* we count in steps of 1ms (125us) */
1093 /* 125us mode not used yet */
1094 if (0) { /* (devpriv->high_speed) */
1096 /* timing of the conversion itself: every 125 us */
1097 devpriv->ao_timer = cmd->convert_arg / 125000;
1100 /* timing of the scan: we get all channels at once */
1101 devpriv->ao_timer = cmd->scan_begin_arg / 1000000;
1102 if (devpriv->ao_timer < 1) {
1108 devpriv->ao_counter = devpriv->ao_timer;
1110 if (cmd->stop_src == TRIG_COUNT) {
1111 /* not continuous */
1113 /* high speed also scans everything at once */
1114 if (0) { /* (devpriv->high_speed) */
1115 devpriv->ao_sample_count = cmd->stop_arg *
1118 /* there's no scan as the scan has been */
1119 /* perf inside the FX2 */
1120 /* data arrives as one packet */
1121 devpriv->ao_sample_count = cmd->stop_arg;
1123 devpriv->ao_continous = 0;
1125 /* continous acquisition */
1126 devpriv->ao_continous = 1;
1127 devpriv->ao_sample_count = 0;
1130 if (cmd->start_src == TRIG_NOW) {
1131 /* enable this acquisition operation */
1132 devpriv->ao_cmd_running = 1;
1133 ret = usbduxsub_submit_outurbs(dev);
1135 devpriv->ao_cmd_running = 0;
1136 /* fixme: unlink here?? */
1139 s->async->inttrig = NULL;
1142 /* submit the urbs later */
1143 /* wait for an internal signal */
1144 s->async->inttrig = usbdux_ao_inttrig;
1153 static int usbdux_dio_insn_config(struct comedi_device *dev,
1154 struct comedi_subdevice *s,
1155 struct comedi_insn *insn,
1158 unsigned int mask = 1 << CR_CHAN(insn->chanspec);
1161 case INSN_CONFIG_DIO_OUTPUT:
1164 case INSN_CONFIG_DIO_INPUT:
1165 s->io_bits &= ~mask;
1167 case INSN_CONFIG_DIO_QUERY:
1168 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
1176 * We don't tell the firmware here as it would take 8 frames
1177 * to submit the information. We do it in the insn_bits.
1182 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1183 struct comedi_subdevice *s,
1184 struct comedi_insn *insn,
1188 struct usbdux_private *devpriv = dev->private;
1189 unsigned int mask = data[0];
1190 unsigned int bits = data[1];
1193 down(&devpriv->sem);
1196 s->state |= (bits & mask);
1198 devpriv->dux_commands[1] = s->io_bits;
1199 devpriv->dux_commands[2] = s->state;
1202 * This command also tells the firmware to return
1203 * the digital input lines.
1205 ret = send_dux_commands(dev, SENDDIOBITSCOMMAND);
1208 ret = receive_dux_commands(dev, SENDDIOBITSCOMMAND);
1212 data[1] = le16_to_cpu(devpriv->insn_buffer[1]);
1217 return ret ? ret : insn->n;
1220 static int usbdux_counter_read(struct comedi_device *dev,
1221 struct comedi_subdevice *s,
1222 struct comedi_insn *insn,
1225 struct usbdux_private *devpriv = dev->private;
1226 unsigned int chan = CR_CHAN(insn->chanspec);
1230 down(&devpriv->sem);
1232 for (i = 0; i < insn->n; i++) {
1233 ret = send_dux_commands(dev, READCOUNTERCOMMAND);
1235 goto counter_read_exit;
1236 ret = receive_dux_commands(dev, READCOUNTERCOMMAND);
1238 goto counter_read_exit;
1240 data[i] = le16_to_cpu(devpriv->insn_buffer[chan + 1]);
1246 return ret ? ret : insn->n;
1249 static int usbdux_counter_write(struct comedi_device *dev,
1250 struct comedi_subdevice *s,
1251 struct comedi_insn *insn,
1254 struct usbdux_private *devpriv = dev->private;
1255 unsigned int chan = CR_CHAN(insn->chanspec);
1256 int16_t *p = (int16_t *)&devpriv->dux_commands[2];
1260 down(&devpriv->sem);
1262 devpriv->dux_commands[1] = chan;
1264 for (i = 0; i < insn->n; i++) {
1265 *p = cpu_to_le16(data[i]);
1267 ret = send_dux_commands(dev, WRITECOUNTERCOMMAND);
1274 return ret ? ret : insn->n;
1277 static int usbdux_counter_config(struct comedi_device *dev,
1278 struct comedi_subdevice *s,
1279 struct comedi_insn *insn, unsigned int *data)
1281 /* nothing to do so far */
1285 static void usbduxsub_unlink_pwm_urbs(struct comedi_device *dev)
1287 struct usbdux_private *devpriv = dev->private;
1289 usb_kill_urb(devpriv->urb_pwm);
1292 static void usbdux_pwm_stop(struct comedi_device *dev, int do_unlink)
1294 struct usbdux_private *devpriv = dev->private;
1297 usbduxsub_unlink_pwm_urbs(dev);
1299 devpriv->pwm_cmd_running = 0;
1302 static int usbdux_pwm_cancel(struct comedi_device *dev,
1303 struct comedi_subdevice *s)
1305 struct usbdux_private *devpriv = dev->private;
1308 down(&devpriv->sem);
1309 /* unlink only if it is really running */
1310 usbdux_pwm_stop(dev, devpriv->pwm_cmd_running);
1311 ret = send_dux_commands(dev, SENDPWMOFF);
1317 static void usbduxsub_pwm_irq(struct urb *urb)
1319 struct comedi_device *dev = urb->context;
1320 struct usbdux_private *devpriv = dev->private;
1323 switch (urb->status) {
1333 * after an unlink command, unplug, ... etc
1334 * no unlink needed here. Already shutting down.
1336 if (devpriv->pwm_cmd_running)
1337 usbdux_pwm_stop(dev, 0);
1343 if (devpriv->pwm_cmd_running) {
1344 dev_err(dev->class_dev,
1345 "Non-zero urb status received in pwm intr context: %d\n",
1347 usbdux_pwm_stop(dev, 0);
1352 /* are we actually running? */
1353 if (!devpriv->pwm_cmd_running)
1356 urb->transfer_buffer_length = devpriv->size_pwm_buf;
1357 urb->dev = comedi_to_usb_dev(dev);
1359 if (devpriv->pwm_cmd_running) {
1360 ret = usb_submit_urb(urb, GFP_ATOMIC);
1362 dev_err(dev->class_dev,
1363 "pwm urb resubm failed in int-cont. ret=%d",
1365 if (ret == EL2NSYNC)
1366 dev_err(dev->class_dev,
1367 "buggy USB host controller or bug in IRQ handling!\n");
1369 /* don't do an unlink here */
1370 usbdux_pwm_stop(dev, 0);
1375 static int usbduxsub_submit_pwm_urbs(struct comedi_device *dev)
1377 struct usb_device *usb = comedi_to_usb_dev(dev);
1378 struct usbdux_private *devpriv = dev->private;
1379 struct urb *urb = devpriv->urb_pwm;
1381 /* in case of a resubmission after an unlink... */
1382 usb_fill_bulk_urb(urb, usb, usb_sndbulkpipe(usb, 4),
1383 urb->transfer_buffer,
1384 devpriv->size_pwm_buf,
1388 return usb_submit_urb(urb, GFP_ATOMIC);
1391 static int usbdux_pwm_period(struct comedi_device *dev,
1392 struct comedi_subdevice *s, unsigned int period)
1394 struct usbdux_private *this_usbduxsub = dev->private;
1397 if (period < MIN_PWM_PERIOD) {
1400 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1404 this_usbduxsub->pwn_delay = fx2delay;
1405 this_usbduxsub->pwm_period = period;
1410 static int usbdux_pwm_start(struct comedi_device *dev,
1411 struct comedi_subdevice *s)
1413 struct usbdux_private *devpriv = dev->private;
1416 down(&devpriv->sem);
1418 if (devpriv->pwm_cmd_running)
1419 goto pwm_start_exit;
1421 devpriv->dux_commands[1] = devpriv->pwn_delay;
1422 ret = send_dux_commands(dev, SENDPWMON);
1424 goto pwm_start_exit;
1426 /* initialise the buffer */
1427 memset(devpriv->urb_pwm->transfer_buffer, 0, devpriv->size_pwm_buf);
1429 devpriv->pwm_cmd_running = 1;
1430 ret = usbduxsub_submit_pwm_urbs(dev);
1432 devpriv->pwm_cmd_running = 0;
1440 /* generates the bit pattern for PWM with the optional sign bit */
1441 static int usbdux_pwm_pattern(struct comedi_device *dev,
1442 struct comedi_subdevice *s, int channel,
1443 unsigned int value, unsigned int sign)
1445 struct usbdux_private *this_usbduxsub = dev->private;
1452 if (!this_usbduxsub)
1455 /* this is the DIO bit which carries the PWM data */
1456 pwm_mask = (1 << channel);
1457 /* this is the DIO bit which carries the optional direction bit */
1458 sgn_mask = (16 << channel);
1459 /* this is the buffer which will be filled with the with bit */
1460 /* pattern for one period */
1461 szbuf = this_usbduxsub->size_pwm_buf;
1462 p_buf = (char *)(this_usbduxsub->urb_pwm->transfer_buffer);
1463 for (i = 0; i < szbuf; i++) {
1466 c = c & (~pwm_mask);
1467 /* set the bit as long as the index is lower than the value */
1470 /* set the optional sign bit for a relay */
1472 /* positive value */
1473 c = c & (~sgn_mask);
1475 /* negative value */
1483 static int usbdux_pwm_write(struct comedi_device *dev,
1484 struct comedi_subdevice *s,
1485 struct comedi_insn *insn, unsigned int *data)
1487 struct usbdux_private *this_usbduxsub = dev->private;
1489 if (!this_usbduxsub)
1492 if ((insn->n) != 1) {
1494 * doesn't make sense to have more than one value here because
1495 * it would just overwrite the PWM buffer a couple of times
1501 * the sign is set via a special INSN only, this gives us 8 bits for
1503 * relay sign 0 by default
1505 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
1508 static int usbdux_pwm_read(struct comedi_device *x1,
1509 struct comedi_subdevice *x2, struct comedi_insn *x3,
1516 /* switches on/off PWM */
1517 static int usbdux_pwm_config(struct comedi_device *dev,
1518 struct comedi_subdevice *s,
1519 struct comedi_insn *insn, unsigned int *data)
1521 struct usbdux_private *this_usbduxsub = dev->private;
1523 case INSN_CONFIG_ARM:
1526 * if not zero the PWM is limited to a certain time which is
1527 * not supported here
1531 return usbdux_pwm_start(dev, s);
1532 case INSN_CONFIG_DISARM:
1533 return usbdux_pwm_cancel(dev, s);
1534 case INSN_CONFIG_GET_PWM_STATUS:
1536 * to check if the USB transmission has failed or in case PWM
1537 * was limited to n cycles to check if it has terminated
1539 data[1] = this_usbduxsub->pwm_cmd_running;
1541 case INSN_CONFIG_PWM_SET_PERIOD:
1542 return usbdux_pwm_period(dev, s, data[1]);
1543 case INSN_CONFIG_PWM_GET_PERIOD:
1544 data[1] = this_usbduxsub->pwm_period;
1546 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1547 /* value in the first byte and the sign in the second for a
1549 return usbdux_pwm_pattern(dev, s,
1550 /* the channel number */
1551 CR_CHAN(insn->chanspec),
1552 /* actual PWM data */
1556 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1557 /* values are not kept in this driver, nothing to return here */
1563 static int usbdux_firmware_upload(struct comedi_device *dev,
1564 const u8 *data, size_t size,
1565 unsigned long context)
1567 struct usb_device *usb = comedi_to_usb_dev(dev);
1575 if (size > USBDUX_FIRMWARE_MAX_LEN) {
1576 dev_err(dev->class_dev,
1577 "usbdux firmware binary it too large for FX2.\n");
1581 /* we generate a local buffer for the firmware */
1582 buf = kmemdup(data, size, GFP_KERNEL);
1586 /* we need a malloc'ed buffer for usb_control_msg() */
1587 tmp = kmalloc(1, GFP_KERNEL);
1593 /* stop the current firmware on the device */
1594 *tmp = 1; /* 7f92 to one */
1595 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1596 USBDUX_FIRMWARE_CMD,
1598 USBDUX_CPU_CS, 0x0000,
1602 dev_err(dev->class_dev, "can not stop firmware\n");
1606 /* upload the new firmware to the device */
1607 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1608 USBDUX_FIRMWARE_CMD,
1614 dev_err(dev->class_dev, "firmware upload failed\n");
1618 /* start the new firmware on the device */
1619 *tmp = 0; /* 7f92 to zero */
1620 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1621 USBDUX_FIRMWARE_CMD,
1623 USBDUX_CPU_CS, 0x0000,
1627 dev_err(dev->class_dev, "can not start firmware\n");
1635 static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
1637 struct usb_device *usb = comedi_to_usb_dev(dev);
1638 struct usbdux_private *devpriv = dev->private;
1642 /* create space for the commands of the DA converter */
1643 devpriv->dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
1644 if (!devpriv->dac_commands)
1647 /* create space for the commands going to the usb device */
1648 devpriv->dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
1649 if (!devpriv->dux_commands)
1652 /* create space for the in buffer and set it to zero */
1653 devpriv->in_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1654 if (!devpriv->in_buffer)
1657 /* create space of the instruction buffer */
1658 devpriv->insn_buffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
1659 if (!devpriv->insn_buffer)
1662 /* create space for the outbuffer */
1663 devpriv->out_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1664 if (!devpriv->out_buffer)
1668 devpriv->urb_in = kcalloc(devpriv->num_in_buffers, sizeof(*urb),
1670 if (!devpriv->urb_in)
1673 for (i = 0; i < devpriv->num_in_buffers; i++) {
1674 /* one frame: 1ms */
1675 urb = usb_alloc_urb(1, GFP_KERNEL);
1678 devpriv->urb_in[i] = urb;
1681 /* will be filled later with a pointer to the comedi-device */
1682 /* and ONLY then the urb should be submitted */
1683 urb->context = NULL;
1684 urb->pipe = usb_rcvisocpipe(usb, 6);
1685 urb->transfer_flags = URB_ISO_ASAP;
1686 urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1687 if (!urb->transfer_buffer)
1690 urb->complete = usbduxsub_ai_isoc_irq;
1691 urb->number_of_packets = 1;
1692 urb->transfer_buffer_length = SIZEINBUF;
1693 urb->iso_frame_desc[0].offset = 0;
1694 urb->iso_frame_desc[0].length = SIZEINBUF;
1698 devpriv->urb_out = kcalloc(devpriv->num_out_buffers, sizeof(*urb),
1700 if (!devpriv->urb_out)
1703 for (i = 0; i < devpriv->num_out_buffers; i++) {
1704 /* one frame: 1ms */
1705 urb = usb_alloc_urb(1, GFP_KERNEL);
1708 devpriv->urb_out[i] = urb;
1711 /* will be filled later with a pointer to the comedi-device */
1712 /* and ONLY then the urb should be submitted */
1713 urb->context = NULL;
1714 urb->pipe = usb_sndisocpipe(usb, 2);
1715 urb->transfer_flags = URB_ISO_ASAP;
1716 urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1717 if (!urb->transfer_buffer)
1720 urb->complete = usbduxsub_ao_isoc_irq;
1721 urb->number_of_packets = 1;
1722 urb->transfer_buffer_length = SIZEOUTBUF;
1723 urb->iso_frame_desc[0].offset = 0;
1724 urb->iso_frame_desc[0].length = SIZEOUTBUF;
1725 if (devpriv->high_speed)
1726 urb->interval = 8; /* uframes */
1728 urb->interval = 1; /* frames */
1732 if (devpriv->size_pwm_buf) {
1733 urb = usb_alloc_urb(0, GFP_KERNEL);
1736 devpriv->urb_pwm = urb;
1738 /* max bulk ep size in high speed */
1739 urb->transfer_buffer = kzalloc(devpriv->size_pwm_buf,
1741 if (!urb->transfer_buffer)
1748 static void usbdux_free_usb_buffers(struct usbdux_private *devpriv)
1753 urb = devpriv->urb_pwm;
1755 kfree(urb->transfer_buffer);
1759 if (devpriv->urb_out) {
1760 for (i = 0; i < devpriv->num_out_buffers; i++) {
1761 urb = devpriv->urb_out[i];
1763 kfree(urb->transfer_buffer);
1768 kfree(devpriv->urb_out);
1770 if (devpriv->urb_in) {
1771 for (i = 0; i < devpriv->num_in_buffers; i++) {
1772 urb = devpriv->urb_in[i];
1774 kfree(urb->transfer_buffer);
1779 kfree(devpriv->urb_in);
1781 kfree(devpriv->out_buffer);
1782 kfree(devpriv->insn_buffer);
1783 kfree(devpriv->in_buffer);
1784 kfree(devpriv->dux_commands);
1785 kfree(devpriv->dac_commands);
1788 static int usbdux_auto_attach(struct comedi_device *dev,
1789 unsigned long context_unused)
1791 struct usb_interface *intf = comedi_to_usb_interface(dev);
1792 struct usb_device *usb = comedi_to_usb_dev(dev);
1793 struct usbdux_private *devpriv;
1794 struct comedi_subdevice *s;
1797 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1801 sema_init(&devpriv->sem, 1);
1803 usb_set_intfdata(intf, devpriv);
1805 devpriv->high_speed = (usb->speed == USB_SPEED_HIGH);
1806 if (devpriv->high_speed) {
1807 devpriv->num_in_buffers = NUMOFINBUFFERSHIGH;
1808 devpriv->num_out_buffers = NUMOFOUTBUFFERSHIGH;
1809 devpriv->size_pwm_buf = 512;
1811 devpriv->num_in_buffers = NUMOFINBUFFERSFULL;
1812 devpriv->num_out_buffers = NUMOFOUTBUFFERSFULL;
1815 ret = usbdux_alloc_usb_buffers(dev);
1819 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
1820 ret = usb_set_interface(usb, intf->altsetting->desc.bInterfaceNumber,
1823 dev_err(dev->class_dev,
1824 "could not set alternate setting 3 in high speed\n");
1828 ret = comedi_load_firmware(dev, &usb->dev, USBDUX_FIRMWARE,
1829 usbdux_firmware_upload, 0);
1833 ret = comedi_alloc_subdevices(dev, (devpriv->high_speed) ? 5 : 4);
1837 /* Analog Input subdevice */
1838 s = &dev->subdevices[0];
1839 dev->read_subdev = s;
1840 s->type = COMEDI_SUBD_AI;
1841 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1843 s->maxdata = 0x0fff;
1844 s->len_chanlist = 8;
1845 s->range_table = &range_usbdux_ai_range;
1846 s->insn_read = usbdux_ai_insn_read;
1847 s->do_cmdtest = usbdux_ai_cmdtest;
1848 s->do_cmd = usbdux_ai_cmd;
1849 s->cancel = usbdux_ai_cancel;
1851 /* Analog Output subdevice */
1852 s = &dev->subdevices[1];
1853 dev->write_subdev = s;
1854 s->type = COMEDI_SUBD_AO;
1855 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1857 s->maxdata = 0x0fff;
1858 s->len_chanlist = 4;
1859 s->range_table = &range_usbdux_ao_range;
1860 s->do_cmdtest = usbdux_ao_cmdtest;
1861 s->do_cmd = usbdux_ao_cmd;
1862 s->cancel = usbdux_ao_cancel;
1863 s->insn_read = usbdux_ao_insn_read;
1864 s->insn_write = usbdux_ao_insn_write;
1866 /* Digital I/O subdevice */
1867 s = &dev->subdevices[2];
1868 s->type = COMEDI_SUBD_DIO;
1869 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1872 s->range_table = &range_digital;
1873 s->insn_bits = usbdux_dio_insn_bits;
1874 s->insn_config = usbdux_dio_insn_config;
1876 /* Counter subdevice */
1877 s = &dev->subdevices[3];
1878 s->type = COMEDI_SUBD_COUNTER;
1879 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
1881 s->maxdata = 0xffff;
1882 s->insn_read = usbdux_counter_read;
1883 s->insn_write = usbdux_counter_write;
1884 s->insn_config = usbdux_counter_config;
1886 if (devpriv->high_speed) {
1888 s = &dev->subdevices[4];
1889 s->type = COMEDI_SUBD_PWM;
1890 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
1892 s->maxdata = devpriv->size_pwm_buf;
1893 s->insn_write = usbdux_pwm_write;
1894 s->insn_read = usbdux_pwm_read;
1895 s->insn_config = usbdux_pwm_config;
1897 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
1903 static void usbdux_detach(struct comedi_device *dev)
1905 struct usb_interface *intf = comedi_to_usb_interface(dev);
1906 struct usbdux_private *devpriv = dev->private;
1909 down(&devpriv->sem);
1911 usb_set_intfdata(intf, NULL);
1913 /* stop and unlink any submitted urbs */
1914 usbdux_pwm_stop(dev, devpriv->pwm_cmd_running);
1915 usbdux_ao_stop(dev, devpriv->ao_cmd_running);
1916 usbdux_ai_stop(dev, devpriv->ai_cmd_running);
1918 usbdux_free_usb_buffers(devpriv);
1924 static struct comedi_driver usbdux_driver = {
1925 .driver_name = "usbdux",
1926 .module = THIS_MODULE,
1927 .auto_attach = usbdux_auto_attach,
1928 .detach = usbdux_detach,
1931 static int usbdux_usb_probe(struct usb_interface *intf,
1932 const struct usb_device_id *id)
1934 return comedi_usb_auto_config(intf, &usbdux_driver, 0);
1937 static const struct usb_device_id usbdux_usb_table[] = {
1938 { USB_DEVICE(0x13d8, 0x0001) },
1939 { USB_DEVICE(0x13d8, 0x0002) },
1942 MODULE_DEVICE_TABLE(usb, usbdux_usb_table);
1944 static struct usb_driver usbdux_usb_driver = {
1946 .probe = usbdux_usb_probe,
1947 .disconnect = comedi_usb_auto_unconfig,
1948 .id_table = usbdux_usb_table,
1950 module_comedi_usb_driver(usbdux_driver, usbdux_usb_driver);
1952 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1953 MODULE_DESCRIPTION("Stirling/ITL USB-DUX -- Bernd.Porr@f2s.com");
1954 MODULE_LICENSE("GPL");
1955 MODULE_FIRMWARE(USBDUX_FIRMWARE);