2 comedi/drivers/usbdux.c
3 Copyright (C) 2011 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 (usbduxsigma.o)
19 Author: Bernd Porr <BerndPorr@f2s.com>
24 * I must give credit here to Chris Baugher who
25 * wrote the driver for AT-MIO-16d. I used some parts of this
26 * driver. I also must give credits to David Brownell
27 * who supported me with the USB development.
29 * Note: the raw data from the A/D converter is 24 bit big endian
30 * anything else is little endian to/from the dux board
34 * 0.1: initial version
35 * 0.2: all basic functions implemented, digital I/O only for one port
36 * 0.3: proper vendor ID and driver name
37 * 0.4: fixed D/A voltage range
38 * 0.5: various bug fixes, health check at startup
39 * 0.6: corrected wrong input range
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/init.h>
45 #include <linux/slab.h>
46 #include <linux/input.h>
47 #include <linux/usb.h>
48 #include <linux/fcntl.h>
49 #include <linux/compiler.h>
51 #include "comedi_fc.h"
52 #include "../comedidev.h"
54 /* timeout for the USB-transfer in ms*/
55 #define BULK_TIMEOUT 1000
57 /* constants for "firmware" upload and download */
58 #define FIRMWARE "usbduxsigma_firmware.bin"
59 #define FIRMWARE_MAX_LEN 0x4000
60 #define USBDUXSUB_FIRMWARE 0xa0
61 #define VENDOR_DIR_IN 0xc0
62 #define VENDOR_DIR_OUT 0x40
64 /* internal addresses of the 8051 processor */
65 #define USBDUXSUB_CPUCS 0xE600
68 * the minor device number, major is 180 only for debugging purposes and to
69 * upload special firmware (programming the eeprom etc) which is not
70 * compatible with the comedi framwork
72 #define USBDUXSUB_MINOR 32
74 /* max lenghth of the transfer-buffer for software upload */
77 /* Input endpoint number: ISO/IRQ */
80 /* Output endpoint number: ISO/IRQ */
83 /* This EP sends DUX commands to USBDUX */
84 #define COMMAND_OUT_EP 1
86 /* This EP receives the DUX commands from USBDUX */
87 #define COMMAND_IN_EP 8
89 /* Output endpoint for PWM */
92 /* 300Hz max frequ under PWM */
93 #define MIN_PWM_PERIOD ((long)(1E9/300))
95 /* Default PWM frequency */
96 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
98 /* Number of channels (16 AD and offset)*/
99 #define NUMCHANNELS 16
101 /* Size of one A/D value */
102 #define SIZEADIN ((sizeof(int32_t)))
105 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
108 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
111 #define SIZEINSNBUF 16
113 /* Number of DA channels */
114 #define NUMOUTCHANNELS 8
116 /* size of one value for the D/A converter: channel and value */
117 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(int16_t)))
120 * Size of the output-buffer in bytes
121 * Actually only the first 4 triplets are used but for the
122 * high speed mode we need to pad it to 8 (microframes).
124 #define SIZEOUTBUF ((8*SIZEDAOUT))
127 * Size of the buffer for the dux commands: just now max size is determined
128 * by the analogue out + command byte + panic bytes...
130 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
132 /* Number of in-URBs which receive the data: min=2 */
133 #define NUMOFINBUFFERSFULL 5
135 /* Number of out-URBs which send the data: min=2 */
136 #define NUMOFOUTBUFFERSFULL 5
138 /* Number of in-URBs which receive the data: min=5 */
139 /* must have more buffers due to buggy USB ctr */
140 #define NUMOFINBUFFERSHIGH 10
142 /* Number of out-URBs which send the data: min=5 */
143 /* must have more buffers due to buggy USB ctr */
144 #define NUMOFOUTBUFFERSHIGH 10
146 /* number of retries to get the right dux command */
149 /**************************************************/
150 /* comedi constants */
151 static const struct comedi_lrange range_usbdux_ai_range = { 1, {
157 struct usbduxsigma_private {
158 /* actual number of in-buffers */
160 /* actual number of out-buffers */
162 /* ISO-transfer handling: buffers */
165 /* pwm-transfer handling */
168 unsigned int pwmPeriod;
169 /* PWM internal delay for the GPIF in the FX2 */
171 /* size of the PWM buffer which holds the bit pattern */
173 /* input buffer for the ISO-transfer */
175 /* input buffer for single insn */
177 /* output buffer for single DA outputs */
180 unsigned high_speed:1;
181 unsigned ai_cmd_running:1;
182 unsigned ai_continuous:1;
183 unsigned ao_cmd_running:1;
184 unsigned ao_continuous:1;
185 unsigned pwm_cmd_running:1;
187 /* number of samples to acquire */
190 /* time between samples in units of the timer */
191 unsigned int ai_timer;
192 unsigned int ao_timer;
193 /* counter between acquisitions */
194 unsigned int ai_counter;
195 unsigned int ao_counter;
196 /* interval in frames/uframes */
197 unsigned int ai_interval;
199 uint8_t *dac_commands;
201 uint8_t *dux_commands;
202 struct semaphore sem;
205 static void usbdux_ai_stop(struct usbduxsigma_private *devpriv, int do_unlink)
210 for (i = 0; i < devpriv->numOfInBuffers; i++) {
211 if (devpriv->urbIn[i])
212 usb_kill_urb(devpriv->urbIn[i]);
216 devpriv->ai_cmd_running = 0;
219 static int usbdux_ai_cancel(struct comedi_device *dev,
220 struct comedi_subdevice *s)
222 struct usbduxsigma_private *devpriv = dev->private;
225 /* unlink only if it is really running */
226 usbdux_ai_stop(devpriv, devpriv->ai_cmd_running);
232 static void usbduxsub_ai_IsocIrq(struct urb *urb)
234 struct comedi_device *dev = urb->context;
235 struct usbduxsigma_private *devpriv = dev->private;
236 struct comedi_subdevice *s = dev->read_subdev;
237 unsigned int dio_state;
242 /* first we test if something unusual has just happened */
243 switch (urb->status) {
245 /* copy the result in the transfer buffer */
246 memcpy(devpriv->inBuffer, urb->transfer_buffer, SIZEINBUF);
250 * error in the ISOchronous data
251 * we don't copy the data into the transfer buffer
252 * and recycle the last data byte
254 dev_dbg(dev->class_dev,"CRC error in ISO IN stream\n");
262 /* happens after an unlink command */
263 if (devpriv->ai_cmd_running) {
264 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
265 /* we are still running a command, tell comedi */
266 s->async->events |= (COMEDI_CB_EOA | COMEDI_CB_ERROR);
267 comedi_event(dev, s);
273 * a real error on the bus
274 * pass error to comedi if we are really running a command
276 if (devpriv->ai_cmd_running) {
277 dev_err(dev->class_dev,
278 "%s: non-zero urb status (%d)\n",
279 __func__, urb->status);
280 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
281 s->async->events |= (COMEDI_CB_EOA | COMEDI_CB_ERROR);
282 comedi_event(dev, s);
287 if (unlikely(!devpriv->ai_cmd_running))
290 urb->dev = comedi_to_usb_dev(dev);
292 ret = usb_submit_urb(urb, GFP_ATOMIC);
293 if (unlikely(ret < 0)) {
294 dev_err(dev->class_dev, "%s: urb resubmit failed (%d)\n",
296 if (ret == -EL2NSYNC)
297 dev_err(dev->class_dev,
298 "buggy USB host controller or bug in IRQ handler\n");
299 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
300 s->async->events |= (COMEDI_CB_EOA | COMEDI_CB_ERROR);
301 comedi_event(dev, s);
305 /* get the state of the dio pins to allow external trigger */
306 dio_state = be32_to_cpu(devpriv->inBuffer[0]);
308 devpriv->ai_counter--;
309 if (likely(devpriv->ai_counter > 0))
312 /* timer zero, transfer measurements to comedi */
313 devpriv->ai_counter = devpriv->ai_timer;
315 if (!devpriv->ai_continuous) {
316 /* not continuous, fixed number of samples */
317 devpriv->ai_sample_count--;
318 if (devpriv->ai_sample_count < 0) {
319 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
320 /* acquistion is over, tell comedi */
321 s->async->events |= COMEDI_CB_EOA;
322 comedi_event(dev, s);
327 /* get the data from the USB bus and hand it over to comedi */
328 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
329 /* transfer data, note first byte is the DIO state */
330 val = be32_to_cpu(devpriv->inBuffer[i+1]);
331 val &= 0x00ffffff; /* strip status byte */
332 val ^= 0x00800000; /* convert to unsigned */
334 ret = cfc_write_array_to_buffer(s, &val, sizeof(uint32_t));
335 if (unlikely(ret == 0)) {
336 /* buffer overflow */
337 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
341 /* tell comedi that data is there */
342 s->async->events |= (COMEDI_CB_BLOCK | COMEDI_CB_EOS);
343 comedi_event(dev, s);
346 static void usbdux_ao_stop(struct usbduxsigma_private *devpriv, int do_unlink)
351 for (i = 0; i < devpriv->numOfOutBuffers; i++) {
352 if (devpriv->urbOut[i])
353 usb_kill_urb(devpriv->urbOut[i]);
357 devpriv->ao_cmd_running = 0;
360 static int usbdux_ao_cancel(struct comedi_device *dev,
361 struct comedi_subdevice *s)
363 struct usbduxsigma_private *devpriv = dev->private;
366 /* unlink only if it is really running */
367 usbdux_ao_stop(devpriv, devpriv->ao_cmd_running);
373 static void usbduxsub_ao_IsocIrq(struct urb *urb)
375 struct comedi_device *dev = urb->context;
376 struct usbduxsigma_private *devpriv = dev->private;
377 struct comedi_subdevice *s = dev->write_subdev;
383 switch (urb->status) {
392 /* happens after an unlink command */
393 if (devpriv->ao_cmd_running) {
394 usbdux_ao_stop(devpriv, 0); /* w/o unlink */
395 s->async->events |= COMEDI_CB_EOA;
396 comedi_event(dev, s);
402 if (devpriv->ao_cmd_running) {
403 dev_err(dev->class_dev,
404 "%s: non-zero urb status (%d)\n",
405 __func__, urb->status);
406 usbdux_ao_stop(devpriv, 0); /* w/o unlink */
407 s->async->events |= (COMEDI_CB_ERROR | COMEDI_CB_EOA);
408 comedi_event(dev, s);
413 if (!devpriv->ao_cmd_running)
416 devpriv->ao_counter--;
417 if ((int)devpriv->ao_counter <= 0) {
418 /* timer zero, transfer from comedi */
419 devpriv->ao_counter = devpriv->ao_timer;
421 if (!devpriv->ao_continuous) {
422 /* not continuous, fixed number of samples */
423 devpriv->ao_sample_count--;
424 if (devpriv->ao_sample_count < 0) {
425 usbdux_ao_stop(devpriv, 0); /* w/o unlink */
426 /* acquistion is over, tell comedi */
427 s->async->events |= COMEDI_CB_EOA;
428 comedi_event(dev, s);
433 /* transmit data to the USB bus */
434 datap = urb->transfer_buffer;
435 len = s->async->cmd.chanlist_len;
437 for (i = 0; i < len; i++) {
440 if (i >= NUMOUTCHANNELS)
443 ret = comedi_buf_get(s->async, &val);
445 dev_err(dev->class_dev, "buffer underflow\n");
446 s->async->events |= (COMEDI_CB_EOA |
450 *datap++ = devpriv->dac_commands[i];
452 s->async->events |= COMEDI_CB_BLOCK;
453 comedi_event(dev, s);
457 urb->transfer_buffer_length = SIZEOUTBUF;
458 urb->dev = comedi_to_usb_dev(dev);
460 if (devpriv->high_speed)
461 urb->interval = 8; /* uframes */
463 urb->interval = 1; /* frames */
464 urb->number_of_packets = 1;
465 urb->iso_frame_desc[0].offset = 0;
466 urb->iso_frame_desc[0].length = SIZEOUTBUF;
467 urb->iso_frame_desc[0].status = 0;
468 ret = usb_submit_urb(urb, GFP_ATOMIC);
470 dev_err(dev->class_dev,
471 "%s: urb resubmit failed (%d)\n",
474 dev_err(dev->class_dev,
475 "buggy USB host controller or bug in IRQ handler\n");
476 usbdux_ao_stop(devpriv, 0); /* w/o unlink */
477 s->async->events |= (COMEDI_CB_EOA | COMEDI_CB_ERROR);
478 comedi_event(dev, s);
482 static int usbduxsigma_submit_urbs(struct comedi_device *dev,
483 struct urb **urbs, int num_urbs,
486 struct usb_device *usb = comedi_to_usb_dev(dev);
487 struct usbduxsigma_private *devpriv = dev->private;
492 /* Submit all URBs and start the transfer on the bus */
493 for (i = 0; i < num_urbs; i++) {
496 /* in case of a resubmission after an unlink... */
498 urb->interval = devpriv->ai_interval;
502 urb->transfer_flags = URB_ISO_ASAP;
504 ret = usb_submit_urb(urb, GFP_ATOMIC);
511 static int chanToInterval(int nChannels)
523 static int usbdux_ai_cmdtest(struct comedi_device *dev,
524 struct comedi_subdevice *s,
525 struct comedi_cmd *cmd)
527 struct usbduxsigma_private *this_usbduxsub = dev->private;
529 unsigned int tmpTimer;
531 /* Step 1 : check if triggers are trivially valid */
533 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
534 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
535 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
536 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
537 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
542 /* Step 2a : make sure trigger sources are unique */
544 err |= cfc_check_trigger_is_unique(cmd->start_src);
545 err |= cfc_check_trigger_is_unique(cmd->stop_src);
547 /* Step 2b : and mutually compatible */
552 /* Step 3: check if arguments are trivially valid */
554 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
556 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
557 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
559 if (cmd->scan_begin_src == TRIG_TIMER) {
560 if (this_usbduxsub->high_speed) {
562 * In high speed mode microframes are possible.
563 * However, during one microframe we can roughly
564 * sample two channels. Thus, the more channels
565 * are in the channel list the more time we need.
567 i = chanToInterval(cmd->chanlist_len);
568 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
570 /* now calc the real sampling rate with all the
573 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
577 /* 1kHz scans every USB frame */
578 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
581 * calc the real sampling rate with the rounding errors
583 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
586 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg,
590 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
592 if (cmd->stop_src == TRIG_COUNT) {
593 /* any count is allowed */
596 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
606 * creates the ADC command for the MAX1271
607 * range is the range value from comedi
609 static void create_adc_command(unsigned int chan,
614 (*muxsg0) = (*muxsg0) | (1 << chan);
616 (*muxsg1) = (*muxsg1) | (1 << (chan-8));
620 /* bulk transfers to usbdux */
622 #define SENDADCOMMANDS 0
623 #define SENDDACOMMANDS 1
624 #define SENDDIOCONFIGCOMMAND 2
625 #define SENDDIOBITSCOMMAND 3
626 #define SENDSINGLEAD 4
630 static int send_dux_commands(struct comedi_device *dev, int cmd_type)
632 struct usb_device *usb = comedi_to_usb_dev(dev);
633 struct usbduxsigma_private *devpriv = dev->private;
636 devpriv->dux_commands[0] = cmd_type;
638 return usb_bulk_msg(usb, usb_sndbulkpipe(usb, COMMAND_OUT_EP),
639 devpriv->dux_commands, SIZEOFDUXBUFFER,
640 &nsent, BULK_TIMEOUT);
643 static int receive_dux_commands(struct comedi_device *dev, int command)
645 struct usb_device *usb = comedi_to_usb_dev(dev);
646 struct usbduxsigma_private *devpriv = dev->private;
651 for (i = 0; i < RETRIES; i++) {
652 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, COMMAND_IN_EP),
653 devpriv->insnBuffer, SIZEINSNBUF,
654 &nrec, BULK_TIMEOUT);
658 if (devpriv->insnBuffer[0] == command)
662 * This is only reached if the data has been requested a
663 * couple of times and the command was not received.
668 static int usbduxsigma_ai_inttrig(struct comedi_device *dev,
669 struct comedi_subdevice *s,
670 unsigned int trignum)
672 struct usbduxsigma_private *devpriv= dev->private;
679 if (!devpriv->ai_cmd_running) {
680 ret = usbduxsigma_submit_urbs(dev, devpriv->urbIn,
681 devpriv->numOfInBuffers, 1);
686 devpriv->ai_cmd_running = 1;
687 s->async->inttrig = NULL;
694 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
696 struct usbduxsigma_private *this_usbduxsub = dev->private;
697 struct comedi_cmd *cmd = &s->async->cmd;
705 /* block other CPUs from starting an ai_cmd */
706 down(&this_usbduxsub->sem);
707 if (this_usbduxsub->ai_cmd_running) {
708 up(&this_usbduxsub->sem);
711 /* set current channel of the running acquisition to zero */
712 s->async->cur_chan = 0;
714 /* first the number of channels per time step */
715 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
718 this_usbduxsub->dux_commands[2] = 0x12;
720 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
721 this_usbduxsub->dux_commands[3] = 0x03;
723 /* CONFIG3: differential channels off */
724 this_usbduxsub->dux_commands[4] = 0x00;
726 for (i = 0; i < cmd->chanlist_len; i++) {
727 chan = CR_CHAN(cmd->chanlist[i]);
728 create_adc_command(chan, &muxsg0, &muxsg1);
730 this_usbduxsub->dux_commands[5] = muxsg0;
731 this_usbduxsub->dux_commands[6] = muxsg1;
732 this_usbduxsub->dux_commands[7] = sysred;
734 result = send_dux_commands(dev, SENDADCOMMANDS);
736 up(&this_usbduxsub->sem);
740 if (this_usbduxsub->high_speed) {
742 * every 2 channels get a time window of 125us. Thus, if we
743 * sample all 16 channels we need 1ms. If we sample only one
744 * channel we need only 125us
746 this_usbduxsub->ai_interval =
747 chanToInterval(cmd->chanlist_len);
748 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
752 /* interval always 1ms */
753 this_usbduxsub->ai_interval = 1;
754 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
756 if (this_usbduxsub->ai_timer < 1) {
757 up(&this_usbduxsub->sem);
760 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
762 if (cmd->stop_src == TRIG_COUNT) {
763 /* data arrives as one packet */
764 this_usbduxsub->ai_sample_count = cmd->stop_arg;
765 this_usbduxsub->ai_continuous = 0;
767 /* continuous acquisition */
768 this_usbduxsub->ai_continuous = 1;
769 this_usbduxsub->ai_sample_count = 0;
772 if (cmd->start_src == TRIG_NOW) {
773 /* enable this acquisition operation */
774 ret = usbduxsigma_submit_urbs(dev, this_usbduxsub->urbIn,
775 this_usbduxsub->numOfInBuffers,
778 up(&this_usbduxsub->sem);
781 this_usbduxsub->ai_cmd_running = 1;
782 s->async->inttrig = NULL;
785 /* don't enable the acquision operation */
786 /* wait for an internal signal */
787 s->async->inttrig = usbduxsigma_ai_inttrig;
789 up(&this_usbduxsub->sem);
793 /* Mode 0 is used to get a single conversion on demand */
794 static int usbdux_ai_insn_read(struct comedi_device *dev,
795 struct comedi_subdevice *s,
796 struct comedi_insn *insn, unsigned int *data)
798 struct usbduxsigma_private *this_usbduxsub = dev->private;
807 down(&this_usbduxsub->sem);
808 if (this_usbduxsub->ai_cmd_running) {
809 up(&this_usbduxsub->sem);
813 /* sample one channel */
814 /* CONFIG0: chopper on */
815 this_usbduxsub->dux_commands[1] = 0x16;
817 /* CONFIG1: 2kHz sampling rate */
818 this_usbduxsub->dux_commands[2] = 0x80;
820 /* CONFIG3: differential channels off */
821 this_usbduxsub->dux_commands[3] = 0x00;
823 chan = CR_CHAN(insn->chanspec);
824 create_adc_command(chan, &muxsg0, &muxsg1);
826 this_usbduxsub->dux_commands[4] = muxsg0;
827 this_usbduxsub->dux_commands[5] = muxsg1;
828 this_usbduxsub->dux_commands[6] = sysred;
831 err = send_dux_commands(dev, SENDSINGLEAD);
833 up(&this_usbduxsub->sem);
837 for (i = 0; i < insn->n; i++) {
838 err = receive_dux_commands(dev, SENDSINGLEAD);
840 up(&this_usbduxsub->sem);
843 /* 32 bits big endian from the A/D converter */
844 one = be32_to_cpu(*((int32_t *)
845 ((this_usbduxsub->insnBuffer)+1)));
846 /* mask out the status byte */
847 one = one & 0x00ffffff;
848 /* turn it into an unsigned integer */
849 one = one ^ 0x00800000;
852 up(&this_usbduxsub->sem);
856 /************************************/
859 static int usbdux_ao_insn_read(struct comedi_device *dev,
860 struct comedi_subdevice *s,
861 struct comedi_insn *insn, unsigned int *data)
863 struct usbduxsigma_private *this_usbduxsub = dev->private;
865 int chan = CR_CHAN(insn->chanspec);
867 down(&this_usbduxsub->sem);
868 for (i = 0; i < insn->n; i++)
869 data[i] = this_usbduxsub->outBuffer[chan];
871 up(&this_usbduxsub->sem);
875 static int usbdux_ao_insn_write(struct comedi_device *dev,
876 struct comedi_subdevice *s,
877 struct comedi_insn *insn, unsigned int *data)
879 struct usbduxsigma_private *this_usbduxsub = dev->private;
881 int chan = CR_CHAN(insn->chanspec);
883 down(&this_usbduxsub->sem);
884 if (this_usbduxsub->ao_cmd_running) {
885 up(&this_usbduxsub->sem);
889 for (i = 0; i < insn->n; i++) {
890 /* number of channels: 1 */
891 this_usbduxsub->dux_commands[1] = 1;
893 this_usbduxsub->dux_commands[2] = data[i];
894 this_usbduxsub->outBuffer[chan] = data[i];
895 this_usbduxsub->dux_commands[3] = chan;
896 err = send_dux_commands(dev, SENDDACOMMANDS);
898 up(&this_usbduxsub->sem);
902 up(&this_usbduxsub->sem);
907 static int usbduxsigma_ao_inttrig(struct comedi_device *dev,
908 struct comedi_subdevice *s,
909 unsigned int trignum)
911 struct usbduxsigma_private *devpriv = dev->private;
918 if (!devpriv->ao_cmd_running) {
919 ret = usbduxsigma_submit_urbs(dev, devpriv->urbOut,
920 devpriv->numOfOutBuffers, 0);
925 devpriv->ao_cmd_running = 1;
926 s->async->inttrig = NULL;
933 static int usbdux_ao_cmdtest(struct comedi_device *dev,
934 struct comedi_subdevice *s,
935 struct comedi_cmd *cmd)
940 /* Step 1 : check if triggers are trivially valid */
942 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
944 if (0) { /* (this_usbduxsub->high_speed) */
946 * start immediately a new scan
947 * the sampling rate is set by the coversion rate
951 /* start a new scan (output at once) with a timer */
954 err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
956 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
957 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
958 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
963 /* Step 2a : make sure trigger sources are unique */
965 err |= cfc_check_trigger_is_unique(cmd->start_src);
966 err |= cfc_check_trigger_is_unique(cmd->stop_src);
968 /* Step 2b : and mutually compatible */
973 /* Step 3: check if arguments are trivially valid */
975 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
977 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
978 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
980 if (cmd->scan_begin_src == TRIG_TIMER)
981 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
984 /* not used now, is for later use */
985 if (cmd->convert_src == TRIG_TIMER)
986 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
988 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
990 if (cmd->stop_src == TRIG_COUNT) {
991 /* any count is allowed */
994 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1003 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1005 struct usbduxsigma_private *this_usbduxsub = dev->private;
1006 struct comedi_cmd *cmd = &s->async->cmd;
1007 unsigned int chan, gain;
1010 down(&this_usbduxsub->sem);
1011 /* set current channel of the running acquisition to zero */
1012 s->async->cur_chan = 0;
1013 for (i = 0; i < cmd->chanlist_len; ++i) {
1014 chan = CR_CHAN(cmd->chanlist[i]);
1015 gain = CR_RANGE(cmd->chanlist[i]);
1016 this_usbduxsub->dac_commands[i] = chan;
1019 /* we count in steps of 1ms (125us) */
1020 /* 125us mode not used yet */
1021 if (0) { /* (this_usbduxsub->high_speed) */
1023 /* timing of the conversion itself: every 125 us */
1024 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1027 /* timing of the scan: we get all channels at once */
1028 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1029 if (this_usbduxsub->ao_timer < 1) {
1030 up(&this_usbduxsub->sem);
1034 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1036 if (cmd->stop_src == TRIG_COUNT) {
1037 /* not continuous */
1039 /* high speed also scans everything at once */
1040 if (0) { /* (this_usbduxsub->high_speed) */
1041 this_usbduxsub->ao_sample_count =
1042 (cmd->stop_arg) * (cmd->scan_end_arg);
1044 /* there's no scan as the scan has been */
1045 /* perf inside the FX2 */
1046 /* data arrives as one packet */
1047 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1049 this_usbduxsub->ao_continuous = 0;
1051 /* continuous acquisition */
1052 this_usbduxsub->ao_continuous = 1;
1053 this_usbduxsub->ao_sample_count = 0;
1056 if (cmd->start_src == TRIG_NOW) {
1057 /* enable this acquisition operation */
1058 ret = usbduxsigma_submit_urbs(dev, this_usbduxsub->urbOut,
1059 this_usbduxsub->numOfOutBuffers,
1062 up(&this_usbduxsub->sem);
1065 this_usbduxsub->ao_cmd_running = 1;
1066 s->async->inttrig = NULL;
1069 /* submit the urbs later */
1070 /* wait for an internal signal */
1071 s->async->inttrig = usbduxsigma_ao_inttrig;
1074 up(&this_usbduxsub->sem);
1078 static int usbduxsigma_dio_insn_config(struct comedi_device *dev,
1079 struct comedi_subdevice *s,
1080 struct comedi_insn *insn,
1083 unsigned int chan = CR_CHAN(insn->chanspec);
1084 unsigned int mask = 1 << chan;
1087 case INSN_CONFIG_DIO_OUTPUT:
1090 case INSN_CONFIG_DIO_INPUT:
1091 s->io_bits &= ~mask;
1093 case INSN_CONFIG_DIO_QUERY:
1094 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
1102 * We don't tell the firmware here as it would take 8 frames
1103 * to submit the information. We do it in the (*insn_bits).
1108 static int usbduxsigma_dio_insn_bits(struct comedi_device *dev,
1109 struct comedi_subdevice *s,
1110 struct comedi_insn *insn,
1113 struct usbduxsigma_private *devpriv = dev->private;
1114 unsigned int mask = data[0];
1115 unsigned int bits = data[1];
1118 down(&devpriv->sem);
1121 s->state |= (bits & mask);
1123 devpriv->dux_commands[1] = s->io_bits & 0xff;
1124 devpriv->dux_commands[4] = s->state & 0xff;
1125 devpriv->dux_commands[2] = (s->io_bits >> 8) & 0xff;
1126 devpriv->dux_commands[5] = (s->state >> 8) & 0xff;
1127 devpriv->dux_commands[3] = (s->io_bits >> 16) & 0xff;
1128 devpriv->dux_commands[6] = (s->state >> 16) & 0xff;
1130 ret = send_dux_commands(dev, SENDDIOBITSCOMMAND);
1133 ret = receive_dux_commands(dev, SENDDIOBITSCOMMAND);
1137 s->state = devpriv->insnBuffer[1] |
1138 (devpriv->insnBuffer[2] << 8) |
1139 (devpriv->insnBuffer[3] << 16);
1150 static void usbduxsigma_pwm_stop(struct comedi_device *dev, int do_unlink)
1152 struct usbduxsigma_private *devpriv = dev->private;
1155 if (devpriv->urbPwm)
1156 usb_kill_urb(devpriv->urbPwm);
1159 devpriv->pwm_cmd_running = 0;
1162 static int usbduxsigma_pwm_cancel(struct comedi_device *dev,
1163 struct comedi_subdevice *s)
1165 struct usbduxsigma_private *devpriv = dev->private;
1167 /* unlink only if it is really running */
1168 usbduxsigma_pwm_stop(dev, devpriv->pwm_cmd_running);
1170 return send_dux_commands(dev, SENDPWMOFF);
1173 static void usbduxsigma_pwm_irq(struct urb *urb)
1175 struct comedi_device *dev = urb->context;
1176 struct usbduxsigma_private *devpriv = dev->private;
1179 switch (urb->status) {
1188 /* happens after an unlink command */
1189 if (devpriv->pwm_cmd_running)
1190 usbduxsigma_pwm_stop(dev, 0); /* w/o unlink */
1195 if (devpriv->pwm_cmd_running) {
1196 dev_err(dev->class_dev,
1197 "%s: non-zero urb status (%d)\n",
1198 __func__, urb->status);
1199 usbduxsigma_pwm_stop(dev, 0); /* w/o unlink */
1204 if (!devpriv->pwm_cmd_running)
1207 urb->transfer_buffer_length = devpriv->sizePwmBuf;
1208 urb->dev = comedi_to_usb_dev(dev);
1210 ret = usb_submit_urb(urb, GFP_ATOMIC);
1212 dev_err(dev->class_dev, "%s: urb resubmit failed (%d)\n",
1214 if (ret == EL2NSYNC)
1215 dev_err(dev->class_dev,
1216 "buggy USB host controller or bug in IRQ handler\n");
1217 usbduxsigma_pwm_stop(dev, 0); /* w/o unlink */
1221 static int usbduxsigma_submit_pwm_urb(struct comedi_device *dev)
1223 struct usb_device *usb = comedi_to_usb_dev(dev);
1224 struct usbduxsigma_private *devpriv = dev->private;
1225 struct urb *urb = devpriv->urbPwm;
1227 /* in case of a resubmission after an unlink... */
1228 usb_fill_bulk_urb(urb, usb, usb_sndbulkpipe(usb, PWM_EP),
1229 urb->transfer_buffer, devpriv->sizePwmBuf,
1230 usbduxsigma_pwm_irq, dev);
1232 return usb_submit_urb(urb, GFP_ATOMIC);
1235 static int usbduxsigma_pwm_period(struct comedi_device *dev,
1236 struct comedi_subdevice *s,
1237 unsigned int period)
1239 struct usbduxsigma_private *devpriv = dev->private;
1242 if (period < MIN_PWM_PERIOD) {
1245 fx2delay = (period / (6 * 512 * 1000 / 33)) - 6;
1249 devpriv->pwmDelay = fx2delay;
1250 devpriv->pwmPeriod = period;
1254 static int usbduxsigma_pwm_start(struct comedi_device *dev,
1255 struct comedi_subdevice *s)
1257 struct usbduxsigma_private *devpriv = dev->private;
1260 if (devpriv->pwm_cmd_running)
1263 devpriv->dux_commands[1] = devpriv->pwmDelay;
1264 ret = send_dux_commands(dev, SENDPWMON);
1268 memset(devpriv->urbPwm->transfer_buffer, 0, devpriv->sizePwmBuf);
1270 ret = usbduxsigma_submit_pwm_urb(dev);
1273 devpriv->pwm_cmd_running = 1;
1278 static int usbduxsigma_pwm_pattern(struct comedi_device *dev,
1279 struct comedi_subdevice *s,
1284 struct usbduxsigma_private *devpriv = dev->private;
1285 char pwm_mask = (1 << chan); /* DIO bit for the PWM data */
1286 char sgn_mask = (16 << chan); /* DIO bit for the sign */
1287 char *buf = (char *)(devpriv->urbPwm->transfer_buffer);
1288 int szbuf = devpriv->sizePwmBuf;
1291 for (i = 0; i < szbuf; i++) {
1306 static int usbduxsigma_pwm_write(struct comedi_device *dev,
1307 struct comedi_subdevice *s,
1308 struct comedi_insn *insn,
1311 unsigned int chan = CR_CHAN(insn->chanspec);
1314 * It doesn't make sense to support more than one value here
1315 * because it would just overwrite the PWM buffer.
1321 * The sign is set via a special INSN only, this gives us 8 bits
1322 * for normal operation, sign is 0 by default.
1324 return usbduxsigma_pwm_pattern(dev, s, chan, data[0], 0);
1327 static int usbduxsigma_pwm_config(struct comedi_device *dev,
1328 struct comedi_subdevice *s,
1329 struct comedi_insn *insn,
1332 struct usbduxsigma_private *devpriv = dev->private;
1333 unsigned int chan = CR_CHAN(insn->chanspec);
1336 case INSN_CONFIG_ARM:
1338 * if not zero the PWM is limited to a certain time which is
1339 * not supported here
1343 return usbduxsigma_pwm_start(dev, s);
1344 case INSN_CONFIG_DISARM:
1345 return usbduxsigma_pwm_cancel(dev, s);
1346 case INSN_CONFIG_GET_PWM_STATUS:
1347 data[1] = devpriv->pwm_cmd_running;
1349 case INSN_CONFIG_PWM_SET_PERIOD:
1350 return usbduxsigma_pwm_period(dev, s, data[1]);
1351 case INSN_CONFIG_PWM_GET_PERIOD:
1352 data[1] = devpriv->pwmPeriod;
1354 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1357 * data[2] = sign (for a relay)
1359 return usbduxsigma_pwm_pattern(dev, s, chan,
1360 data[1], (data[2] != 0));
1361 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1362 /* values are not kept in this driver, nothing to return */
1368 static int usbduxsigma_getstatusinfo(struct comedi_device *dev, int chan)
1370 struct usbduxsigma_private *devpriv = dev->private;
1378 sysred = 0; /* ADC zero */
1381 sysred = 1; /* ADC offset */
1384 sysred = 4; /* VCC */
1387 sysred = 8; /* temperature */
1390 sysred = 16; /* gain */
1393 sysred = 32; /* ref */
1397 devpriv->dux_commands[1] = 0x12; /* CONFIG0 */
1398 devpriv->dux_commands[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
1399 devpriv->dux_commands[3] = 0x00; /* CONFIG3: diff. channels off */
1400 devpriv->dux_commands[4] = 0;
1401 devpriv->dux_commands[5] = 0;
1402 devpriv->dux_commands[6] = sysred;
1403 ret = send_dux_commands(dev, SENDSINGLEAD);
1407 ret = receive_dux_commands(dev, SENDSINGLEAD);
1411 /* 32 bits big endian from the A/D converter */
1412 val = be32_to_cpu(*((int32_t *)((devpriv->insnBuffer)+1)));
1413 val &= 0x00ffffff; /* strip status byte */
1414 val ^= 0x00800000; /* convert to unsigned */
1419 static int usbduxsigma_attach_common(struct comedi_device *dev)
1421 struct usbduxsigma_private *devpriv = dev->private;
1422 struct comedi_subdevice *s;
1427 down(&devpriv->sem);
1429 if (devpriv->high_speed)
1430 n_subdevs = 4; /* with pwm */
1432 n_subdevs = 3; /* without pwm */
1433 ret = comedi_alloc_subdevices(dev, n_subdevs);
1439 /* Analog Input subdevice */
1440 s = &dev->subdevices[0];
1441 dev->read_subdev = s;
1442 s->type = COMEDI_SUBD_AI;
1443 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ | SDF_LSAMPL;
1444 s->n_chan = NUMCHANNELS;
1445 s->len_chanlist = NUMCHANNELS;
1446 s->maxdata = 0x00ffffff;
1447 s->range_table = &range_usbdux_ai_range;
1448 s->insn_read = usbdux_ai_insn_read;
1449 s->do_cmdtest = usbdux_ai_cmdtest;
1450 s->do_cmd = usbdux_ai_cmd;
1451 s->cancel = usbdux_ai_cancel;
1453 /* Analog Output subdevice */
1454 s = &dev->subdevices[1];
1455 dev->write_subdev = s;
1456 s->type = COMEDI_SUBD_AO;
1457 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1459 s->len_chanlist = 4;
1460 s->maxdata = 0x00ff;
1461 s->range_table = &range_unipolar2_5;
1462 s->insn_write = usbdux_ao_insn_write;
1463 s->insn_read = usbdux_ao_insn_read;
1464 s->do_cmdtest = usbdux_ao_cmdtest;
1465 s->do_cmd = usbdux_ao_cmd;
1466 s->cancel = usbdux_ao_cancel;
1468 /* Digital I/O subdevice */
1469 s = &dev->subdevices[2];
1470 s->type = COMEDI_SUBD_DIO;
1471 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1474 s->range_table = &range_digital;
1475 s->insn_bits = usbduxsigma_dio_insn_bits;
1476 s->insn_config = usbduxsigma_dio_insn_config;
1478 if (devpriv->high_speed) {
1479 /* Timer / pwm subdevice */
1480 s = &dev->subdevices[3];
1481 s->type = COMEDI_SUBD_PWM;
1482 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
1484 s->maxdata = devpriv->sizePwmBuf;
1485 s->insn_write = usbduxsigma_pwm_write;
1486 s->insn_config = usbduxsigma_pwm_config;
1488 usbduxsigma_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
1493 offset = usbduxsigma_getstatusinfo(dev, 0);
1495 dev_err(dev->class_dev,
1496 "Communication to USBDUXSIGMA failed! Check firmware and cabling\n");
1498 dev_info(dev->class_dev, "attached, ADC_zero = %x\n", offset);
1503 static int usbduxsigma_firmware_upload(struct comedi_device *dev,
1504 const u8 *data, size_t size,
1505 unsigned long context)
1507 struct usb_device *usb = comedi_to_usb_dev(dev);
1515 if (size > FIRMWARE_MAX_LEN) {
1516 dev_err(dev->class_dev, "firmware binary too large for FX2\n");
1520 /* we generate a local buffer for the firmware */
1521 buf = kmemdup(data, size, GFP_KERNEL);
1525 /* we need a malloc'ed buffer for usb_control_msg() */
1526 tmp = kmalloc(1, GFP_KERNEL);
1532 /* stop the current firmware on the device */
1533 *tmp = 1; /* 7f92 to one */
1534 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1537 USBDUXSUB_CPUCS, 0x0000,
1541 dev_err(dev->class_dev, "can not stop firmware\n");
1545 /* upload the new firmware to the device */
1546 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1553 dev_err(dev->class_dev, "firmware upload failed\n");
1557 /* start the new firmware on the device */
1558 *tmp = 0; /* 7f92 to zero */
1559 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1562 USBDUXSUB_CPUCS, 0x0000,
1566 dev_err(dev->class_dev, "can not start firmware\n");
1574 static int usbduxsigma_alloc_usb_buffers(struct comedi_device *dev)
1576 struct usb_device *usb = comedi_to_usb_dev(dev);
1577 struct usbduxsigma_private *devpriv = dev->private;
1581 devpriv->dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
1582 devpriv->dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
1583 devpriv->inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1584 devpriv->insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
1585 devpriv->outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1586 devpriv->urbIn = kcalloc(devpriv->numOfInBuffers, sizeof(*urb),
1588 devpriv->urbOut = kcalloc(devpriv->numOfOutBuffers, sizeof(*urb),
1590 if (!devpriv->dac_commands || !devpriv->dux_commands ||
1591 !devpriv->inBuffer || !devpriv->insnBuffer ||
1592 !devpriv->outBuffer || !devpriv->urbIn || !devpriv->urbOut)
1595 for (i = 0; i < devpriv->numOfInBuffers; i++) {
1596 /* one frame: 1ms */
1597 urb = usb_alloc_urb(1, GFP_KERNEL);
1600 devpriv->urbIn[i] = urb;
1602 /* will be filled later with a pointer to the comedi-device */
1603 /* and ONLY then the urb should be submitted */
1604 urb->context = NULL;
1605 urb->pipe = usb_rcvisocpipe(usb, ISOINEP);
1606 urb->transfer_flags = URB_ISO_ASAP;
1607 urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1608 if (!urb->transfer_buffer)
1610 urb->complete = usbduxsub_ai_IsocIrq;
1611 urb->number_of_packets = 1;
1612 urb->transfer_buffer_length = SIZEINBUF;
1613 urb->iso_frame_desc[0].offset = 0;
1614 urb->iso_frame_desc[0].length = SIZEINBUF;
1617 for (i = 0; i < devpriv->numOfOutBuffers; i++) {
1618 /* one frame: 1ms */
1619 urb = usb_alloc_urb(1, GFP_KERNEL);
1622 devpriv->urbOut[i] = urb;
1624 /* will be filled later with a pointer to the comedi-device */
1625 /* and ONLY then the urb should be submitted */
1626 urb->context = NULL;
1627 urb->pipe = usb_sndisocpipe(usb, ISOOUTEP);
1628 urb->transfer_flags = URB_ISO_ASAP;
1629 urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1630 if (!urb->transfer_buffer)
1632 urb->complete = usbduxsub_ao_IsocIrq;
1633 urb->number_of_packets = 1;
1634 urb->transfer_buffer_length = SIZEOUTBUF;
1635 urb->iso_frame_desc[0].offset = 0;
1636 urb->iso_frame_desc[0].length = SIZEOUTBUF;
1637 if (devpriv->high_speed)
1638 urb->interval = 8; /* uframes */
1640 urb->interval = 1; /* frames */
1643 if (devpriv->high_speed) {
1644 /* max bulk ep size in high speed */
1645 devpriv->sizePwmBuf = 512;
1646 urb = usb_alloc_urb(0, GFP_KERNEL);
1649 devpriv->urbPwm = urb;
1650 urb->transfer_buffer = kzalloc(devpriv->sizePwmBuf, GFP_KERNEL);
1651 if (!urb->transfer_buffer)
1654 devpriv->urbPwm = NULL;
1655 devpriv->sizePwmBuf = 0;
1661 static void usbduxsigma_free_usb_buffers(struct comedi_device *dev)
1663 struct usbduxsigma_private *devpriv = dev->private;
1667 /* force unlink all urbs */
1668 usbdux_ai_stop(devpriv, 1);
1669 usbdux_ao_stop(devpriv, 1);
1670 usbduxsigma_pwm_stop(dev, 1);
1672 urb = devpriv->urbPwm;
1674 kfree(urb->transfer_buffer);
1677 if (devpriv->urbOut) {
1678 for (i = 0; i < devpriv->numOfOutBuffers; i++) {
1679 urb = devpriv->urbOut[i];
1681 kfree(urb->transfer_buffer);
1685 kfree(devpriv->urbOut);
1687 if (devpriv->urbIn) {
1688 for (i = 0; i < devpriv->numOfInBuffers; i++) {
1689 urb = devpriv->urbIn[i];
1691 kfree(urb->transfer_buffer);
1695 kfree(devpriv->urbIn);
1697 kfree(devpriv->outBuffer);
1698 kfree(devpriv->insnBuffer);
1699 kfree(devpriv->inBuffer);
1700 kfree(devpriv->dux_commands);
1701 kfree(devpriv->dac_commands);
1704 static int usbduxsigma_auto_attach(struct comedi_device *dev,
1705 unsigned long context_unused)
1707 struct usb_interface *intf = comedi_to_usb_interface(dev);
1708 struct usb_device *usb = comedi_to_usb_dev(dev);
1709 struct usbduxsigma_private *devpriv;
1712 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1715 dev->private = devpriv;
1717 sema_init(&devpriv->sem, 1);
1718 usb_set_intfdata(intf, devpriv);
1720 ret = usb_set_interface(usb,
1721 intf->altsetting->desc.bInterfaceNumber, 3);
1723 dev_err(dev->class_dev,
1724 "could not set alternate setting 3 in high speed\n");
1728 /* test if it is high speed (USB 2.0) */
1729 devpriv->high_speed = (usb->speed == USB_SPEED_HIGH);
1730 if (devpriv->high_speed) {
1731 devpriv->numOfInBuffers = NUMOFINBUFFERSHIGH;
1732 devpriv->numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
1734 devpriv->numOfInBuffers = NUMOFINBUFFERSFULL;
1735 devpriv->numOfOutBuffers = NUMOFOUTBUFFERSFULL;
1738 ret = usbduxsigma_alloc_usb_buffers(dev);
1742 ret = comedi_load_firmware(dev, &usb->dev, FIRMWARE,
1743 usbduxsigma_firmware_upload, 0);
1747 return usbduxsigma_attach_common(dev);
1750 static void usbduxsigma_detach(struct comedi_device *dev)
1752 struct usb_interface *intf = comedi_to_usb_interface(dev);
1753 struct usbduxsigma_private *devpriv = dev->private;
1758 usb_set_intfdata(intf, NULL);
1760 down(&devpriv->sem);
1761 usbduxsigma_free_usb_buffers(dev);
1765 static struct comedi_driver usbduxsigma_driver = {
1766 .driver_name = "usbduxsigma",
1767 .module = THIS_MODULE,
1768 .auto_attach = usbduxsigma_auto_attach,
1769 .detach = usbduxsigma_detach,
1772 static int usbduxsigma_usb_probe(struct usb_interface *intf,
1773 const struct usb_device_id *id)
1775 return comedi_usb_auto_config(intf, &usbduxsigma_driver, 0);
1778 static const struct usb_device_id usbduxsigma_usb_table[] = {
1779 { USB_DEVICE(0x13d8, 0x0020) },
1780 { USB_DEVICE(0x13d8, 0x0021) },
1781 { USB_DEVICE(0x13d8, 0x0022) },
1784 MODULE_DEVICE_TABLE(usb, usbduxsigma_usb_table);
1786 static struct usb_driver usbduxsigma_usb_driver = {
1787 .name = "usbduxsigma",
1788 .probe = usbduxsigma_usb_probe,
1789 .disconnect = comedi_usb_auto_unconfig,
1790 .id_table = usbduxsigma_usb_table,
1792 module_comedi_usb_driver(usbduxsigma_driver, usbduxsigma_usb_driver);
1794 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1795 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com");
1796 MODULE_LICENSE("GPL");
1797 MODULE_FIRMWARE(FIRMWARE);