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 */
179 /* interface structure in 2.6 */
180 struct usb_interface *interface;
181 /* is it USB_SPEED_HIGH or not? */
182 short int high_speed;
183 /* asynchronous command is running */
184 short int ai_cmd_running;
185 short int ao_cmd_running;
187 short int pwm_cmd_running;
188 /* continuous acquisition */
189 short int ai_continuous;
190 short int ao_continuous;
191 /* number of samples to acquire */
194 /* time between samples in units of the timer */
195 unsigned int ai_timer;
196 unsigned int ao_timer;
197 /* counter between acquisitions */
198 unsigned int ai_counter;
199 unsigned int ao_counter;
200 /* interval in frames/uframes */
201 unsigned int ai_interval;
203 uint8_t *dac_commands;
205 uint8_t *dux_commands;
206 struct semaphore sem;
209 static void usbdux_ai_stop(struct usbduxsigma_private *devpriv, int do_unlink)
214 for (i = 0; i < devpriv->numOfInBuffers; i++) {
215 if (devpriv->urbIn[i])
216 usb_kill_urb(devpriv->urbIn[i]);
220 devpriv->ai_cmd_running = 0;
223 static int usbdux_ai_cancel(struct comedi_device *dev,
224 struct comedi_subdevice *s)
226 struct usbduxsigma_private *devpriv = dev->private;
229 /* unlink only if it is really running */
230 usbdux_ai_stop(devpriv, devpriv->ai_cmd_running);
236 static void usbduxsub_ai_IsocIrq(struct urb *urb)
238 struct comedi_device *dev = urb->context;
239 struct usbduxsigma_private *devpriv = dev->private;
240 struct comedi_subdevice *s = dev->read_subdev;
241 unsigned int dio_state;
246 /* first we test if something unusual has just happened */
247 switch (urb->status) {
249 /* copy the result in the transfer buffer */
250 memcpy(devpriv->inBuffer, urb->transfer_buffer, SIZEINBUF);
254 * error in the ISOchronous data
255 * we don't copy the data into the transfer buffer
256 * and recycle the last data byte
258 dev_dbg(dev->class_dev,"CRC error in ISO IN stream\n");
266 /* happens after an unlink command */
267 if (devpriv->ai_cmd_running) {
268 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
269 /* we are still running a command, tell comedi */
270 s->async->events |= (COMEDI_CB_EOA | COMEDI_CB_ERROR);
271 comedi_event(dev, s);
277 * a real error on the bus
278 * pass error to comedi if we are really running a command
280 if (devpriv->ai_cmd_running) {
281 dev_err(dev->class_dev,
282 "%s: non-zero urb status (%d)\n",
283 __func__, urb->status);
284 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
285 s->async->events |= (COMEDI_CB_EOA | COMEDI_CB_ERROR);
286 comedi_event(dev, s);
291 if (unlikely(!devpriv->ai_cmd_running))
294 urb->dev = comedi_to_usb_dev(dev);
296 ret = usb_submit_urb(urb, GFP_ATOMIC);
297 if (unlikely(ret < 0)) {
298 dev_err(dev->class_dev, "%s: urb resubmit failed (%d)\n",
300 if (ret == -EL2NSYNC)
301 dev_err(dev->class_dev,
302 "buggy USB host controller or bug in IRQ handler\n");
303 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
304 s->async->events |= (COMEDI_CB_EOA | COMEDI_CB_ERROR);
305 comedi_event(dev, s);
309 /* get the state of the dio pins to allow external trigger */
310 dio_state = be32_to_cpu(devpriv->inBuffer[0]);
312 devpriv->ai_counter--;
313 if (likely(devpriv->ai_counter > 0))
316 /* timer zero, transfer measurements to comedi */
317 devpriv->ai_counter = devpriv->ai_timer;
319 if (!devpriv->ai_continuous) {
320 /* not continuous, fixed number of samples */
321 devpriv->ai_sample_count--;
322 if (devpriv->ai_sample_count < 0) {
323 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
324 /* acquistion is over, tell comedi */
325 s->async->events |= COMEDI_CB_EOA;
326 comedi_event(dev, s);
331 /* get the data from the USB bus and hand it over to comedi */
332 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
333 /* transfer data, note first byte is the DIO state */
334 val = be32_to_cpu(devpriv->inBuffer[i+1]);
335 val &= 0x00ffffff; /* strip status byte */
336 val ^= 0x00800000; /* convert to unsigned */
338 ret = cfc_write_array_to_buffer(s, &val, sizeof(uint32_t));
339 if (unlikely(ret == 0)) {
340 /* buffer overflow */
341 usbdux_ai_stop(devpriv, 0); /* w/o unlink */
345 /* tell comedi that data is there */
346 s->async->events |= (COMEDI_CB_BLOCK | COMEDI_CB_EOS);
347 comedi_event(dev, s);
350 static void usbdux_ao_stop(struct usbduxsigma_private *devpriv, int do_unlink)
355 for (i = 0; i < devpriv->numOfOutBuffers; i++) {
356 if (devpriv->urbOut[i])
357 usb_kill_urb(devpriv->urbOut[i]);
361 devpriv->ao_cmd_running = 0;
364 static int usbdux_ao_cancel(struct comedi_device *dev,
365 struct comedi_subdevice *s)
367 struct usbduxsigma_private *devpriv = dev->private;
370 /* unlink only if it is really running */
371 usbdux_ao_stop(devpriv, devpriv->ao_cmd_running);
377 static void usbduxsub_ao_IsocIrq(struct urb *urb)
379 struct comedi_device *dev = urb->context;
380 struct usbduxsigma_private *devpriv = dev->private;
381 struct comedi_subdevice *s = dev->write_subdev;
387 switch (urb->status) {
396 /* happens after an unlink command */
397 if (devpriv->ao_cmd_running) {
398 usbdux_ao_stop(devpriv, 0); /* w/o unlink */
399 s->async->events |= COMEDI_CB_EOA;
400 comedi_event(dev, s);
406 if (devpriv->ao_cmd_running) {
407 dev_err(dev->class_dev,
408 "%s: non-zero urb status (%d)\n",
409 __func__, urb->status);
410 usbdux_ao_stop(devpriv, 0); /* w/o unlink */
411 s->async->events |= (COMEDI_CB_ERROR | COMEDI_CB_EOA);
412 comedi_event(dev, s);
417 if (!devpriv->ao_cmd_running)
420 devpriv->ao_counter--;
421 if ((int)devpriv->ao_counter <= 0) {
422 /* timer zero, transfer from comedi */
423 devpriv->ao_counter = devpriv->ao_timer;
425 if (!devpriv->ao_continuous) {
426 /* not continuous, fixed number of samples */
427 devpriv->ao_sample_count--;
428 if (devpriv->ao_sample_count < 0) {
429 usbdux_ao_stop(devpriv, 0); /* w/o unlink */
430 /* acquistion is over, tell comedi */
431 s->async->events |= COMEDI_CB_EOA;
432 comedi_event(dev, s);
437 /* transmit data to the USB bus */
438 datap = urb->transfer_buffer;
439 len = s->async->cmd.chanlist_len;
441 for (i = 0; i < len; i++) {
444 if (i >= NUMOUTCHANNELS)
447 ret = comedi_buf_get(s->async, &val);
449 dev_err(dev->class_dev, "buffer underflow\n");
450 s->async->events |= (COMEDI_CB_EOA |
454 *datap++ = devpriv->dac_commands[i];
456 s->async->events |= COMEDI_CB_BLOCK;
457 comedi_event(dev, s);
461 urb->transfer_buffer_length = SIZEOUTBUF;
462 urb->dev = comedi_to_usb_dev(dev);
464 if (devpriv->high_speed)
465 urb->interval = 8; /* uframes */
467 urb->interval = 1; /* frames */
468 urb->number_of_packets = 1;
469 urb->iso_frame_desc[0].offset = 0;
470 urb->iso_frame_desc[0].length = SIZEOUTBUF;
471 urb->iso_frame_desc[0].status = 0;
472 ret = usb_submit_urb(urb, GFP_ATOMIC);
474 dev_err(dev->class_dev,
475 "%s: urb resubmit failed (%d)\n",
478 dev_err(dev->class_dev,
479 "buggy USB host controller or bug in IRQ handler\n");
480 usbdux_ao_stop(devpriv, 0); /* w/o unlink */
481 s->async->events |= (COMEDI_CB_EOA | COMEDI_CB_ERROR);
482 comedi_event(dev, s);
486 static int usbduxsigma_submit_urbs(struct comedi_device *dev,
487 struct urb **urbs, int num_urbs,
490 struct usb_device *usb = comedi_to_usb_dev(dev);
491 struct usbduxsigma_private *devpriv = dev->private;
496 /* Submit all URBs and start the transfer on the bus */
497 for (i = 0; i < num_urbs; i++) {
500 /* in case of a resubmission after an unlink... */
502 urb->interval = devpriv->ai_interval;
506 urb->transfer_flags = URB_ISO_ASAP;
508 ret = usb_submit_urb(urb, GFP_ATOMIC);
515 static int chanToInterval(int nChannels)
527 static int usbdux_ai_cmdtest(struct comedi_device *dev,
528 struct comedi_subdevice *s,
529 struct comedi_cmd *cmd)
531 struct usbduxsigma_private *this_usbduxsub = dev->private;
533 unsigned int tmpTimer;
535 /* Step 1 : check if triggers are trivially valid */
537 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
538 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
539 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
540 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
541 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
546 /* Step 2a : make sure trigger sources are unique */
548 err |= cfc_check_trigger_is_unique(cmd->start_src);
549 err |= cfc_check_trigger_is_unique(cmd->stop_src);
551 /* Step 2b : and mutually compatible */
556 /* Step 3: check if arguments are trivially valid */
558 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
560 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
561 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
563 if (cmd->scan_begin_src == TRIG_TIMER) {
564 if (this_usbduxsub->high_speed) {
566 * In high speed mode microframes are possible.
567 * However, during one microframe we can roughly
568 * sample two channels. Thus, the more channels
569 * are in the channel list the more time we need.
571 i = chanToInterval(cmd->chanlist_len);
572 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
574 /* now calc the real sampling rate with all the
577 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
581 /* 1kHz scans every USB frame */
582 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
585 * calc the real sampling rate with the rounding errors
587 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
590 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg,
594 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
596 if (cmd->stop_src == TRIG_COUNT) {
597 /* any count is allowed */
600 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
610 * creates the ADC command for the MAX1271
611 * range is the range value from comedi
613 static void create_adc_command(unsigned int chan,
618 (*muxsg0) = (*muxsg0) | (1 << chan);
620 (*muxsg1) = (*muxsg1) | (1 << (chan-8));
624 /* bulk transfers to usbdux */
626 #define SENDADCOMMANDS 0
627 #define SENDDACOMMANDS 1
628 #define SENDDIOCONFIGCOMMAND 2
629 #define SENDDIOBITSCOMMAND 3
630 #define SENDSINGLEAD 4
634 static int send_dux_commands(struct comedi_device *dev, int cmd_type)
636 struct usb_device *usb = comedi_to_usb_dev(dev);
637 struct usbduxsigma_private *devpriv = dev->private;
640 devpriv->dux_commands[0] = cmd_type;
642 return usb_bulk_msg(usb, usb_sndbulkpipe(usb, COMMAND_OUT_EP),
643 devpriv->dux_commands, SIZEOFDUXBUFFER,
644 &nsent, BULK_TIMEOUT);
647 static int receive_dux_commands(struct comedi_device *dev, int command)
649 struct usb_device *usb = comedi_to_usb_dev(dev);
650 struct usbduxsigma_private *devpriv = dev->private;
655 for (i = 0; i < RETRIES; i++) {
656 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, COMMAND_IN_EP),
657 devpriv->insnBuffer, SIZEINSNBUF,
658 &nrec, BULK_TIMEOUT);
662 if (devpriv->insnBuffer[0] == command)
666 * This is only reached if the data has been requested a
667 * couple of times and the command was not received.
672 static int usbduxsigma_ai_inttrig(struct comedi_device *dev,
673 struct comedi_subdevice *s,
674 unsigned int trignum)
676 struct usbduxsigma_private *devpriv= dev->private;
683 if (!devpriv->ai_cmd_running) {
684 ret = usbduxsigma_submit_urbs(dev, devpriv->urbIn,
685 devpriv->numOfInBuffers, 1);
690 devpriv->ai_cmd_running = 1;
691 s->async->inttrig = NULL;
698 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
700 struct usbduxsigma_private *this_usbduxsub = dev->private;
701 struct comedi_cmd *cmd = &s->async->cmd;
709 /* block other CPUs from starting an ai_cmd */
710 down(&this_usbduxsub->sem);
711 if (this_usbduxsub->ai_cmd_running) {
712 up(&this_usbduxsub->sem);
715 /* set current channel of the running acquisition to zero */
716 s->async->cur_chan = 0;
718 /* first the number of channels per time step */
719 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
722 this_usbduxsub->dux_commands[2] = 0x12;
724 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
725 this_usbduxsub->dux_commands[3] = 0x03;
727 /* CONFIG3: differential channels off */
728 this_usbduxsub->dux_commands[4] = 0x00;
730 for (i = 0; i < cmd->chanlist_len; i++) {
731 chan = CR_CHAN(cmd->chanlist[i]);
732 create_adc_command(chan, &muxsg0, &muxsg1);
734 this_usbduxsub->dux_commands[5] = muxsg0;
735 this_usbduxsub->dux_commands[6] = muxsg1;
736 this_usbduxsub->dux_commands[7] = sysred;
738 result = send_dux_commands(dev, SENDADCOMMANDS);
740 up(&this_usbduxsub->sem);
744 if (this_usbduxsub->high_speed) {
746 * every 2 channels get a time window of 125us. Thus, if we
747 * sample all 16 channels we need 1ms. If we sample only one
748 * channel we need only 125us
750 this_usbduxsub->ai_interval =
751 chanToInterval(cmd->chanlist_len);
752 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
756 /* interval always 1ms */
757 this_usbduxsub->ai_interval = 1;
758 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
760 if (this_usbduxsub->ai_timer < 1) {
761 up(&this_usbduxsub->sem);
764 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
766 if (cmd->stop_src == TRIG_COUNT) {
767 /* data arrives as one packet */
768 this_usbduxsub->ai_sample_count = cmd->stop_arg;
769 this_usbduxsub->ai_continuous = 0;
771 /* continuous acquisition */
772 this_usbduxsub->ai_continuous = 1;
773 this_usbduxsub->ai_sample_count = 0;
776 if (cmd->start_src == TRIG_NOW) {
777 /* enable this acquisition operation */
778 ret = usbduxsigma_submit_urbs(dev, this_usbduxsub->urbIn,
779 this_usbduxsub->numOfInBuffers,
782 up(&this_usbduxsub->sem);
785 this_usbduxsub->ai_cmd_running = 1;
786 s->async->inttrig = NULL;
789 /* don't enable the acquision operation */
790 /* wait for an internal signal */
791 s->async->inttrig = usbduxsigma_ai_inttrig;
793 up(&this_usbduxsub->sem);
797 /* Mode 0 is used to get a single conversion on demand */
798 static int usbdux_ai_insn_read(struct comedi_device *dev,
799 struct comedi_subdevice *s,
800 struct comedi_insn *insn, unsigned int *data)
802 struct usbduxsigma_private *this_usbduxsub = dev->private;
811 down(&this_usbduxsub->sem);
812 if (this_usbduxsub->ai_cmd_running) {
813 up(&this_usbduxsub->sem);
817 /* sample one channel */
818 /* CONFIG0: chopper on */
819 this_usbduxsub->dux_commands[1] = 0x16;
821 /* CONFIG1: 2kHz sampling rate */
822 this_usbduxsub->dux_commands[2] = 0x80;
824 /* CONFIG3: differential channels off */
825 this_usbduxsub->dux_commands[3] = 0x00;
827 chan = CR_CHAN(insn->chanspec);
828 create_adc_command(chan, &muxsg0, &muxsg1);
830 this_usbduxsub->dux_commands[4] = muxsg0;
831 this_usbduxsub->dux_commands[5] = muxsg1;
832 this_usbduxsub->dux_commands[6] = sysred;
835 err = send_dux_commands(dev, SENDSINGLEAD);
837 up(&this_usbduxsub->sem);
841 for (i = 0; i < insn->n; i++) {
842 err = receive_dux_commands(dev, SENDSINGLEAD);
844 up(&this_usbduxsub->sem);
847 /* 32 bits big endian from the A/D converter */
848 one = be32_to_cpu(*((int32_t *)
849 ((this_usbduxsub->insnBuffer)+1)));
850 /* mask out the status byte */
851 one = one & 0x00ffffff;
852 /* turn it into an unsigned integer */
853 one = one ^ 0x00800000;
856 up(&this_usbduxsub->sem);
860 /************************************/
863 static int usbdux_ao_insn_read(struct comedi_device *dev,
864 struct comedi_subdevice *s,
865 struct comedi_insn *insn, unsigned int *data)
867 struct usbduxsigma_private *this_usbduxsub = dev->private;
869 int chan = CR_CHAN(insn->chanspec);
871 down(&this_usbduxsub->sem);
872 for (i = 0; i < insn->n; i++)
873 data[i] = this_usbduxsub->outBuffer[chan];
875 up(&this_usbduxsub->sem);
879 static int usbdux_ao_insn_write(struct comedi_device *dev,
880 struct comedi_subdevice *s,
881 struct comedi_insn *insn, unsigned int *data)
883 struct usbduxsigma_private *this_usbduxsub = dev->private;
885 int chan = CR_CHAN(insn->chanspec);
887 down(&this_usbduxsub->sem);
888 if (this_usbduxsub->ao_cmd_running) {
889 up(&this_usbduxsub->sem);
893 for (i = 0; i < insn->n; i++) {
894 /* number of channels: 1 */
895 this_usbduxsub->dux_commands[1] = 1;
897 this_usbduxsub->dux_commands[2] = data[i];
898 this_usbduxsub->outBuffer[chan] = data[i];
899 this_usbduxsub->dux_commands[3] = chan;
900 err = send_dux_commands(dev, SENDDACOMMANDS);
902 up(&this_usbduxsub->sem);
906 up(&this_usbduxsub->sem);
911 static int usbduxsigma_ao_inttrig(struct comedi_device *dev,
912 struct comedi_subdevice *s,
913 unsigned int trignum)
915 struct usbduxsigma_private *devpriv = dev->private;
922 if (!devpriv->ao_cmd_running) {
923 ret = usbduxsigma_submit_urbs(dev, devpriv->urbOut,
924 devpriv->numOfOutBuffers, 0);
929 devpriv->ao_cmd_running = 1;
930 s->async->inttrig = NULL;
937 static int usbdux_ao_cmdtest(struct comedi_device *dev,
938 struct comedi_subdevice *s,
939 struct comedi_cmd *cmd)
944 /* Step 1 : check if triggers are trivially valid */
946 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
948 if (0) { /* (this_usbduxsub->high_speed) */
950 * start immediately a new scan
951 * the sampling rate is set by the coversion rate
955 /* start a new scan (output at once) with a timer */
958 err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
960 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
961 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
962 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
967 /* Step 2a : make sure trigger sources are unique */
969 err |= cfc_check_trigger_is_unique(cmd->start_src);
970 err |= cfc_check_trigger_is_unique(cmd->stop_src);
972 /* Step 2b : and mutually compatible */
977 /* Step 3: check if arguments are trivially valid */
979 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
981 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
982 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
984 if (cmd->scan_begin_src == TRIG_TIMER)
985 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
988 /* not used now, is for later use */
989 if (cmd->convert_src == TRIG_TIMER)
990 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
992 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
994 if (cmd->stop_src == TRIG_COUNT) {
995 /* any count is allowed */
998 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1007 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1009 struct usbduxsigma_private *this_usbduxsub = dev->private;
1010 struct comedi_cmd *cmd = &s->async->cmd;
1011 unsigned int chan, gain;
1014 down(&this_usbduxsub->sem);
1015 /* set current channel of the running acquisition to zero */
1016 s->async->cur_chan = 0;
1017 for (i = 0; i < cmd->chanlist_len; ++i) {
1018 chan = CR_CHAN(cmd->chanlist[i]);
1019 gain = CR_RANGE(cmd->chanlist[i]);
1020 this_usbduxsub->dac_commands[i] = chan;
1023 /* we count in steps of 1ms (125us) */
1024 /* 125us mode not used yet */
1025 if (0) { /* (this_usbduxsub->high_speed) */
1027 /* timing of the conversion itself: every 125 us */
1028 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1031 /* timing of the scan: we get all channels at once */
1032 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1033 if (this_usbduxsub->ao_timer < 1) {
1034 up(&this_usbduxsub->sem);
1038 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1040 if (cmd->stop_src == TRIG_COUNT) {
1041 /* not continuous */
1043 /* high speed also scans everything at once */
1044 if (0) { /* (this_usbduxsub->high_speed) */
1045 this_usbduxsub->ao_sample_count =
1046 (cmd->stop_arg) * (cmd->scan_end_arg);
1048 /* there's no scan as the scan has been */
1049 /* perf inside the FX2 */
1050 /* data arrives as one packet */
1051 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1053 this_usbduxsub->ao_continuous = 0;
1055 /* continuous acquisition */
1056 this_usbduxsub->ao_continuous = 1;
1057 this_usbduxsub->ao_sample_count = 0;
1060 if (cmd->start_src == TRIG_NOW) {
1061 /* enable this acquisition operation */
1062 ret = usbduxsigma_submit_urbs(dev, this_usbduxsub->urbOut,
1063 this_usbduxsub->numOfOutBuffers,
1066 up(&this_usbduxsub->sem);
1069 this_usbduxsub->ao_cmd_running = 1;
1070 s->async->inttrig = NULL;
1073 /* submit the urbs later */
1074 /* wait for an internal signal */
1075 s->async->inttrig = usbduxsigma_ao_inttrig;
1078 up(&this_usbduxsub->sem);
1082 static int usbduxsigma_dio_insn_config(struct comedi_device *dev,
1083 struct comedi_subdevice *s,
1084 struct comedi_insn *insn,
1087 unsigned int chan = CR_CHAN(insn->chanspec);
1088 unsigned int mask = 1 << chan;
1091 case INSN_CONFIG_DIO_OUTPUT:
1094 case INSN_CONFIG_DIO_INPUT:
1095 s->io_bits &= ~mask;
1097 case INSN_CONFIG_DIO_QUERY:
1098 data[1] = (s->io_bits & mask) ? COMEDI_OUTPUT : COMEDI_INPUT;
1106 * We don't tell the firmware here as it would take 8 frames
1107 * to submit the information. We do it in the (*insn_bits).
1112 static int usbduxsigma_dio_insn_bits(struct comedi_device *dev,
1113 struct comedi_subdevice *s,
1114 struct comedi_insn *insn,
1117 struct usbduxsigma_private *devpriv = dev->private;
1118 unsigned int mask = data[0];
1119 unsigned int bits = data[1];
1122 down(&devpriv->sem);
1125 s->state |= (bits & mask);
1127 devpriv->dux_commands[1] = s->io_bits & 0xff;
1128 devpriv->dux_commands[4] = s->state & 0xff;
1129 devpriv->dux_commands[2] = (s->io_bits >> 8) & 0xff;
1130 devpriv->dux_commands[5] = (s->state >> 8) & 0xff;
1131 devpriv->dux_commands[3] = (s->io_bits >> 16) & 0xff;
1132 devpriv->dux_commands[6] = (s->state >> 16) & 0xff;
1134 ret = send_dux_commands(dev, SENDDIOBITSCOMMAND);
1137 ret = receive_dux_commands(dev, SENDDIOBITSCOMMAND);
1141 s->state = devpriv->insnBuffer[1] |
1142 (devpriv->insnBuffer[2] << 8) |
1143 (devpriv->insnBuffer[3] << 16);
1154 static void usbdux_pwm_stop(struct usbduxsigma_private *devpriv, int do_unlink)
1157 if (devpriv->urbPwm)
1158 usb_kill_urb(devpriv->urbPwm);
1161 devpriv->pwm_cmd_running = 0;
1164 static int usbdux_pwm_cancel(struct comedi_device *dev,
1165 struct comedi_subdevice *s)
1167 struct usbduxsigma_private *devpriv = dev->private;
1169 /* unlink only if it is really running */
1170 usbdux_pwm_stop(devpriv, devpriv->pwm_cmd_running);
1172 return send_dux_commands(dev, SENDPWMOFF);
1175 static void usbduxsub_pwm_irq(struct urb *urb)
1177 struct comedi_device *dev = urb->context;
1178 struct usbduxsigma_private *devpriv = dev->private;
1181 switch (urb->status) {
1190 /* happens after an unlink command */
1191 if (devpriv->pwm_cmd_running)
1192 usbdux_pwm_stop(devpriv, 0); /* w/o unlink */
1197 if (devpriv->pwm_cmd_running) {
1198 dev_err(dev->class_dev,
1199 "%s: non-zero urb status (%d)\n",
1200 __func__, urb->status);
1201 usbdux_pwm_stop(devpriv, 0); /* w/o unlink */
1206 if (!devpriv->pwm_cmd_running)
1209 urb->transfer_buffer_length = devpriv->sizePwmBuf;
1210 urb->dev = comedi_to_usb_dev(dev);
1212 ret = usb_submit_urb(urb, GFP_ATOMIC);
1214 dev_err(dev->class_dev, "%s: urb resubmit failed (%d)\n",
1216 if (ret == EL2NSYNC)
1217 dev_err(dev->class_dev,
1218 "buggy USB host controller or bug in IRQ handler\n");
1219 usbdux_pwm_stop(devpriv, 0); /* w/o unlink */
1223 static int usbduxsigma_submit_pwm_urb(struct comedi_device *dev)
1225 struct usb_device *usb = comedi_to_usb_dev(dev);
1226 struct usbduxsigma_private *devpriv = dev->private;
1227 struct urb *urb = devpriv->urbPwm;
1229 /* in case of a resubmission after an unlink... */
1230 usb_fill_bulk_urb(urb, usb, usb_sndbulkpipe(usb, PWM_EP),
1231 urb->transfer_buffer, devpriv->sizePwmBuf,
1232 usbduxsub_pwm_irq, dev);
1234 return usb_submit_urb(urb, GFP_ATOMIC);
1237 static int usbdux_pwm_period(struct comedi_device *dev,
1238 struct comedi_subdevice *s, unsigned int period)
1240 struct usbduxsigma_private *this_usbduxsub = dev->private;
1243 if (period < MIN_PWM_PERIOD) {
1246 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1250 this_usbduxsub->pwmDelay = fx2delay;
1251 this_usbduxsub->pwmPeriod = period;
1255 static int usbduxsigma_pwm_start(struct comedi_device *dev,
1256 struct comedi_subdevice *s)
1258 struct usbduxsigma_private *devpriv = dev->private;
1261 if (devpriv->pwm_cmd_running)
1264 devpriv->dux_commands[1] = devpriv->pwmDelay;
1265 ret = send_dux_commands(dev, SENDPWMON);
1269 memset(devpriv->urbPwm->transfer_buffer, 0, devpriv->sizePwmBuf);
1271 ret = usbduxsigma_submit_pwm_urb(dev);
1274 devpriv->pwm_cmd_running = 1;
1279 static int usbduxsigma_pwm_pattern(struct comedi_device *dev,
1280 struct comedi_subdevice *s,
1285 struct usbduxsigma_private *devpriv = dev->private;
1286 char pwm_mask = (1 << chan); /* DIO bit for the PWM data */
1287 char sgn_mask = (16 << chan); /* DIO bit for the sign */
1288 char *buf = (char *)(devpriv->urbPwm->transfer_buffer);
1289 int szbuf = devpriv->sizePwmBuf;
1292 for (i = 0; i < szbuf; i++) {
1307 static int usbduxsigma_pwm_write(struct comedi_device *dev,
1308 struct comedi_subdevice *s,
1309 struct comedi_insn *insn,
1312 unsigned int chan = CR_CHAN(insn->chanspec);
1315 * It doesn't make sense to support more than one value here
1316 * because it would just overwrite the PWM buffer.
1322 * The sign is set via a special INSN only, this gives us 8 bits
1323 * for normal operation, sign is 0 by default.
1325 return usbduxsigma_pwm_pattern(dev, s, chan, data[0], 0);
1328 static int usbduxsigma_pwm_config(struct comedi_device *dev,
1329 struct comedi_subdevice *s,
1330 struct comedi_insn *insn,
1333 struct usbduxsigma_private *devpriv = dev->private;
1334 unsigned int chan = CR_CHAN(insn->chanspec);
1337 case INSN_CONFIG_ARM:
1339 * if not zero the PWM is limited to a certain time which is
1340 * not supported here
1344 return usbduxsigma_pwm_start(dev, s);
1345 case INSN_CONFIG_DISARM:
1346 return usbdux_pwm_cancel(dev, s);
1347 case INSN_CONFIG_GET_PWM_STATUS:
1348 data[1] = devpriv->pwm_cmd_running;
1350 case INSN_CONFIG_PWM_SET_PERIOD:
1351 return usbdux_pwm_period(dev, s, data[1]);
1352 case INSN_CONFIG_PWM_GET_PERIOD:
1353 data[1] = devpriv->pwmPeriod;
1355 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1358 * data[2] = sign (for a relay)
1360 return usbduxsigma_pwm_pattern(dev, s, chan,
1361 data[1], (data[2] != 0));
1362 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1363 /* values are not kept in this driver, nothing to return */
1369 static int usbduxsigma_getstatusinfo(struct comedi_device *dev, int chan)
1371 struct usbduxsigma_private *devpriv = dev->private;
1379 sysred = 0; /* ADC zero */
1382 sysred = 1; /* ADC offset */
1385 sysred = 4; /* VCC */
1388 sysred = 8; /* temperature */
1391 sysred = 16; /* gain */
1394 sysred = 32; /* ref */
1398 devpriv->dux_commands[1] = 0x12; /* CONFIG0 */
1399 devpriv->dux_commands[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
1400 devpriv->dux_commands[3] = 0x00; /* CONFIG3: diff. channels off */
1401 devpriv->dux_commands[4] = 0;
1402 devpriv->dux_commands[5] = 0;
1403 devpriv->dux_commands[6] = sysred;
1404 ret = send_dux_commands(dev, SENDSINGLEAD);
1408 ret = receive_dux_commands(dev, SENDSINGLEAD);
1412 /* 32 bits big endian from the A/D converter */
1413 val = be32_to_cpu(*((int32_t *)((devpriv->insnBuffer)+1)));
1414 val &= 0x00ffffff; /* strip status byte */
1415 val ^= 0x00800000; /* convert to unsigned */
1420 static int usbduxsigma_attach_common(struct comedi_device *dev)
1422 struct usbduxsigma_private *devpriv = dev->private;
1423 struct comedi_subdevice *s;
1428 down(&devpriv->sem);
1430 if (devpriv->high_speed)
1431 n_subdevs = 4; /* with pwm */
1433 n_subdevs = 3; /* without pwm */
1434 ret = comedi_alloc_subdevices(dev, n_subdevs);
1440 /* Analog Input subdevice */
1441 s = &dev->subdevices[0];
1442 dev->read_subdev = s;
1443 s->type = COMEDI_SUBD_AI;
1444 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ | SDF_LSAMPL;
1445 s->n_chan = NUMCHANNELS;
1446 s->len_chanlist = NUMCHANNELS;
1447 s->maxdata = 0x00ffffff;
1448 s->range_table = &range_usbdux_ai_range;
1449 s->insn_read = usbdux_ai_insn_read;
1450 s->do_cmdtest = usbdux_ai_cmdtest;
1451 s->do_cmd = usbdux_ai_cmd;
1452 s->cancel = usbdux_ai_cancel;
1454 /* Analog Output subdevice */
1455 s = &dev->subdevices[1];
1456 dev->write_subdev = s;
1457 s->type = COMEDI_SUBD_AO;
1458 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1460 s->len_chanlist = 4;
1461 s->maxdata = 0x00ff;
1462 s->range_table = &range_unipolar2_5;
1463 s->insn_write = usbdux_ao_insn_write;
1464 s->insn_read = usbdux_ao_insn_read;
1465 s->do_cmdtest = usbdux_ao_cmdtest;
1466 s->do_cmd = usbdux_ao_cmd;
1467 s->cancel = usbdux_ao_cancel;
1469 /* Digital I/O subdevice */
1470 s = &dev->subdevices[2];
1471 s->type = COMEDI_SUBD_DIO;
1472 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1475 s->range_table = &range_digital;
1476 s->insn_bits = usbduxsigma_dio_insn_bits;
1477 s->insn_config = usbduxsigma_dio_insn_config;
1479 if (devpriv->high_speed) {
1480 /* Timer / pwm subdevice */
1481 s = &dev->subdevices[3];
1482 s->type = COMEDI_SUBD_PWM;
1483 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
1485 s->maxdata = devpriv->sizePwmBuf;
1486 s->insn_write = usbduxsigma_pwm_write;
1487 s->insn_config = usbduxsigma_pwm_config;
1489 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
1494 offset = usbduxsigma_getstatusinfo(dev, 0);
1496 dev_err(dev->class_dev,
1497 "Communication to USBDUXSIGMA failed! Check firmware and cabling\n");
1499 dev_info(dev->class_dev, "attached, ADC_zero = %x\n", offset);
1504 static int usbduxsigma_firmware_upload(struct comedi_device *dev,
1505 const u8 *data, size_t size,
1506 unsigned long context)
1508 struct usb_device *usb = comedi_to_usb_dev(dev);
1516 if (size > FIRMWARE_MAX_LEN) {
1517 dev_err(dev->class_dev, "firmware binary too large for FX2\n");
1521 /* we generate a local buffer for the firmware */
1522 buf = kmemdup(data, size, GFP_KERNEL);
1526 /* we need a malloc'ed buffer for usb_control_msg() */
1527 tmp = kmalloc(1, GFP_KERNEL);
1533 /* stop the current firmware on the device */
1534 *tmp = 1; /* 7f92 to one */
1535 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1538 USBDUXSUB_CPUCS, 0x0000,
1542 dev_err(dev->class_dev, "can not stop firmware\n");
1546 /* upload the new firmware to the device */
1547 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1554 dev_err(dev->class_dev, "firmware upload failed\n");
1558 /* start the new firmware on the device */
1559 *tmp = 0; /* 7f92 to zero */
1560 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1563 USBDUXSUB_CPUCS, 0x0000,
1567 dev_err(dev->class_dev, "can not start firmware\n");
1575 static int usbduxsigma_alloc_usb_buffers(struct comedi_device *dev)
1577 struct usb_device *usb = comedi_to_usb_dev(dev);
1578 struct usbduxsigma_private *devpriv = dev->private;
1582 devpriv->dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
1583 devpriv->dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
1584 devpriv->inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1585 devpriv->insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
1586 devpriv->outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1587 devpriv->urbIn = kcalloc(devpriv->numOfInBuffers, sizeof(*urb),
1589 devpriv->urbOut = kcalloc(devpriv->numOfOutBuffers, sizeof(*urb),
1591 if (!devpriv->dac_commands || !devpriv->dux_commands ||
1592 !devpriv->inBuffer || !devpriv->insnBuffer ||
1593 !devpriv->outBuffer || !devpriv->urbIn || !devpriv->urbOut)
1596 for (i = 0; i < devpriv->numOfInBuffers; i++) {
1597 /* one frame: 1ms */
1598 urb = usb_alloc_urb(1, GFP_KERNEL);
1601 devpriv->urbIn[i] = urb;
1603 /* will be filled later with a pointer to the comedi-device */
1604 /* and ONLY then the urb should be submitted */
1605 urb->context = NULL;
1606 urb->pipe = usb_rcvisocpipe(usb, ISOINEP);
1607 urb->transfer_flags = URB_ISO_ASAP;
1608 urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1609 if (!urb->transfer_buffer)
1611 urb->complete = usbduxsub_ai_IsocIrq;
1612 urb->number_of_packets = 1;
1613 urb->transfer_buffer_length = SIZEINBUF;
1614 urb->iso_frame_desc[0].offset = 0;
1615 urb->iso_frame_desc[0].length = SIZEINBUF;
1618 for (i = 0; i < devpriv->numOfOutBuffers; i++) {
1619 /* one frame: 1ms */
1620 urb = usb_alloc_urb(1, GFP_KERNEL);
1623 devpriv->urbOut[i] = urb;
1625 /* will be filled later with a pointer to the comedi-device */
1626 /* and ONLY then the urb should be submitted */
1627 urb->context = NULL;
1628 urb->pipe = usb_sndisocpipe(usb, ISOOUTEP);
1629 urb->transfer_flags = URB_ISO_ASAP;
1630 urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1631 if (!urb->transfer_buffer)
1633 urb->complete = usbduxsub_ao_IsocIrq;
1634 urb->number_of_packets = 1;
1635 urb->transfer_buffer_length = SIZEOUTBUF;
1636 urb->iso_frame_desc[0].offset = 0;
1637 urb->iso_frame_desc[0].length = SIZEOUTBUF;
1638 if (devpriv->high_speed)
1639 urb->interval = 8; /* uframes */
1641 urb->interval = 1; /* frames */
1644 if (devpriv->high_speed) {
1645 /* max bulk ep size in high speed */
1646 devpriv->sizePwmBuf = 512;
1647 urb = usb_alloc_urb(0, GFP_KERNEL);
1650 devpriv->urbPwm = urb;
1651 urb->transfer_buffer = kzalloc(devpriv->sizePwmBuf, GFP_KERNEL);
1652 if (!urb->transfer_buffer)
1655 devpriv->urbPwm = NULL;
1656 devpriv->sizePwmBuf = 0;
1662 static void usbduxsigma_free_usb_buffers(struct comedi_device *dev)
1664 struct usbduxsigma_private *devpriv = dev->private;
1668 /* force unlink all urbs */
1669 usbdux_ai_stop(devpriv, 1);
1670 usbdux_ao_stop(devpriv, 1);
1671 usbdux_pwm_stop(devpriv, 1);
1673 urb = devpriv->urbPwm;
1675 kfree(urb->transfer_buffer);
1678 if (devpriv->urbOut) {
1679 for (i = 0; i < devpriv->numOfOutBuffers; i++) {
1680 urb = devpriv->urbOut[i];
1682 kfree(urb->transfer_buffer);
1686 kfree(devpriv->urbOut);
1688 if (devpriv->urbIn) {
1689 for (i = 0; i < devpriv->numOfInBuffers; i++) {
1690 urb = devpriv->urbIn[i];
1692 kfree(urb->transfer_buffer);
1696 kfree(devpriv->urbIn);
1698 kfree(devpriv->outBuffer);
1699 kfree(devpriv->insnBuffer);
1700 kfree(devpriv->inBuffer);
1701 kfree(devpriv->dux_commands);
1702 kfree(devpriv->dac_commands);
1705 static int usbduxsigma_auto_attach(struct comedi_device *dev,
1706 unsigned long context_unused)
1708 struct usb_interface *intf = comedi_to_usb_interface(dev);
1709 struct usb_device *usb = comedi_to_usb_dev(dev);
1710 struct usbduxsigma_private *devpriv;
1713 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1716 dev->private = devpriv;
1718 sema_init(&devpriv->sem, 1);
1719 devpriv->interface = intf;
1720 usb_set_intfdata(intf, devpriv);
1722 ret = usb_set_interface(usb,
1723 intf->altsetting->desc.bInterfaceNumber, 3);
1725 dev_err(dev->class_dev,
1726 "could not set alternate setting 3 in high speed\n");
1730 /* test if it is high speed (USB 2.0) */
1731 devpriv->high_speed = (usb->speed == USB_SPEED_HIGH);
1732 if (devpriv->high_speed) {
1733 devpriv->numOfInBuffers = NUMOFINBUFFERSHIGH;
1734 devpriv->numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
1736 devpriv->numOfInBuffers = NUMOFINBUFFERSFULL;
1737 devpriv->numOfOutBuffers = NUMOFOUTBUFFERSFULL;
1740 ret = usbduxsigma_alloc_usb_buffers(dev);
1744 ret = comedi_load_firmware(dev, &usb->dev, FIRMWARE,
1745 usbduxsigma_firmware_upload, 0);
1749 return usbduxsigma_attach_common(dev);
1752 static void usbduxsigma_detach(struct comedi_device *dev)
1754 struct usb_interface *intf = comedi_to_usb_interface(dev);
1755 struct usbduxsigma_private *devpriv = dev->private;
1760 usb_set_intfdata(intf, NULL);
1762 down(&devpriv->sem);
1763 usbduxsigma_free_usb_buffers(dev);
1767 static struct comedi_driver usbduxsigma_driver = {
1768 .driver_name = "usbduxsigma",
1769 .module = THIS_MODULE,
1770 .auto_attach = usbduxsigma_auto_attach,
1771 .detach = usbduxsigma_detach,
1774 static int usbduxsigma_usb_probe(struct usb_interface *intf,
1775 const struct usb_device_id *id)
1777 return comedi_usb_auto_config(intf, &usbduxsigma_driver, 0);
1780 static const struct usb_device_id usbduxsigma_usb_table[] = {
1781 { USB_DEVICE(0x13d8, 0x0020) },
1782 { USB_DEVICE(0x13d8, 0x0021) },
1783 { USB_DEVICE(0x13d8, 0x0022) },
1786 MODULE_DEVICE_TABLE(usb, usbduxsigma_usb_table);
1788 static struct usb_driver usbduxsigma_usb_driver = {
1789 .name = "usbduxsigma",
1790 .probe = usbduxsigma_usb_probe,
1791 .disconnect = comedi_usb_auto_unconfig,
1792 .id_table = usbduxsigma_usb_table,
1794 module_comedi_usb_driver(usbduxsigma_driver, usbduxsigma_usb_driver);
1796 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1797 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com");
1798 MODULE_LICENSE("GPL");
1799 MODULE_FIRMWARE(FIRMWARE);