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 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
713 "ai_cmd not possible. Another ai_cmd is running.\n",
715 up(&this_usbduxsub->sem);
718 /* set current channel of the running acquisition to zero */
719 s->async->cur_chan = 0;
721 /* first the number of channels per time step */
722 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
725 this_usbduxsub->dux_commands[2] = 0x12;
727 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
728 this_usbduxsub->dux_commands[3] = 0x03;
730 /* CONFIG3: differential channels off */
731 this_usbduxsub->dux_commands[4] = 0x00;
733 for (i = 0; i < cmd->chanlist_len; i++) {
734 chan = CR_CHAN(cmd->chanlist[i]);
735 create_adc_command(chan, &muxsg0, &muxsg1);
737 this_usbduxsub->dux_commands[5] = muxsg0;
738 this_usbduxsub->dux_commands[6] = muxsg1;
739 this_usbduxsub->dux_commands[7] = sysred;
741 result = send_dux_commands(dev, SENDADCOMMANDS);
743 up(&this_usbduxsub->sem);
747 if (this_usbduxsub->high_speed) {
749 * every 2 channels get a time window of 125us. Thus, if we
750 * sample all 16 channels we need 1ms. If we sample only one
751 * channel we need only 125us
753 this_usbduxsub->ai_interval =
754 chanToInterval(cmd->chanlist_len);
755 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
759 /* interval always 1ms */
760 this_usbduxsub->ai_interval = 1;
761 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
763 if (this_usbduxsub->ai_timer < 1) {
764 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
765 "timer=%d, scan_begin_arg=%d. "
766 "Not properly tested by cmdtest?\n", dev->minor,
767 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
768 up(&this_usbduxsub->sem);
771 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
773 if (cmd->stop_src == TRIG_COUNT) {
774 /* data arrives as one packet */
775 this_usbduxsub->ai_sample_count = cmd->stop_arg;
776 this_usbduxsub->ai_continuous = 0;
778 /* continuous acquisition */
779 this_usbduxsub->ai_continuous = 1;
780 this_usbduxsub->ai_sample_count = 0;
783 if (cmd->start_src == TRIG_NOW) {
784 /* enable this acquisition operation */
785 ret = usbduxsigma_submit_urbs(dev, this_usbduxsub->urbIn,
786 this_usbduxsub->numOfInBuffers,
789 up(&this_usbduxsub->sem);
792 this_usbduxsub->ai_cmd_running = 1;
793 s->async->inttrig = NULL;
796 /* don't enable the acquision operation */
797 /* wait for an internal signal */
798 s->async->inttrig = usbduxsigma_ai_inttrig;
800 up(&this_usbduxsub->sem);
804 /* Mode 0 is used to get a single conversion on demand */
805 static int usbdux_ai_insn_read(struct comedi_device *dev,
806 struct comedi_subdevice *s,
807 struct comedi_insn *insn, unsigned int *data)
809 struct usbduxsigma_private *this_usbduxsub = dev->private;
818 down(&this_usbduxsub->sem);
819 if (this_usbduxsub->ai_cmd_running) {
820 dev_err(&this_usbduxsub->interface->dev,
821 "comedi%d: ai_insn_read not possible. "
822 "Async Command is running.\n", dev->minor);
823 up(&this_usbduxsub->sem);
827 /* sample one channel */
828 /* CONFIG0: chopper on */
829 this_usbduxsub->dux_commands[1] = 0x16;
831 /* CONFIG1: 2kHz sampling rate */
832 this_usbduxsub->dux_commands[2] = 0x80;
834 /* CONFIG3: differential channels off */
835 this_usbduxsub->dux_commands[3] = 0x00;
837 chan = CR_CHAN(insn->chanspec);
838 create_adc_command(chan, &muxsg0, &muxsg1);
840 this_usbduxsub->dux_commands[4] = muxsg0;
841 this_usbduxsub->dux_commands[5] = muxsg1;
842 this_usbduxsub->dux_commands[6] = sysred;
845 err = send_dux_commands(dev, SENDSINGLEAD);
847 up(&this_usbduxsub->sem);
851 for (i = 0; i < insn->n; i++) {
852 err = receive_dux_commands(dev, SENDSINGLEAD);
854 up(&this_usbduxsub->sem);
857 /* 32 bits big endian from the A/D converter */
858 one = be32_to_cpu(*((int32_t *)
859 ((this_usbduxsub->insnBuffer)+1)));
860 /* mask out the status byte */
861 one = one & 0x00ffffff;
862 /* turn it into an unsigned integer */
863 one = one ^ 0x00800000;
866 up(&this_usbduxsub->sem);
870 /************************************/
873 static int usbdux_ao_insn_read(struct comedi_device *dev,
874 struct comedi_subdevice *s,
875 struct comedi_insn *insn, unsigned int *data)
877 struct usbduxsigma_private *this_usbduxsub = dev->private;
879 int chan = CR_CHAN(insn->chanspec);
881 down(&this_usbduxsub->sem);
882 for (i = 0; i < insn->n; i++)
883 data[i] = this_usbduxsub->outBuffer[chan];
885 up(&this_usbduxsub->sem);
889 static int usbdux_ao_insn_write(struct comedi_device *dev,
890 struct comedi_subdevice *s,
891 struct comedi_insn *insn, unsigned int *data)
893 struct usbduxsigma_private *this_usbduxsub = dev->private;
895 int chan = CR_CHAN(insn->chanspec);
897 down(&this_usbduxsub->sem);
898 if (this_usbduxsub->ao_cmd_running) {
899 dev_err(&this_usbduxsub->interface->dev,
900 "comedi%d: ao_insn_write: "
901 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
902 up(&this_usbduxsub->sem);
906 for (i = 0; i < insn->n; i++) {
907 dev_dbg(&this_usbduxsub->interface->dev,
908 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
909 dev->minor, chan, i, data[i]);
911 /* number of channels: 1 */
912 this_usbduxsub->dux_commands[1] = 1;
914 this_usbduxsub->dux_commands[2] = data[i];
915 this_usbduxsub->outBuffer[chan] = data[i];
916 this_usbduxsub->dux_commands[3] = chan;
917 err = send_dux_commands(dev, SENDDACOMMANDS);
919 up(&this_usbduxsub->sem);
923 up(&this_usbduxsub->sem);
928 static int usbduxsigma_ao_inttrig(struct comedi_device *dev,
929 struct comedi_subdevice *s,
930 unsigned int trignum)
932 struct usbduxsigma_private *devpriv = dev->private;
939 if (!devpriv->ao_cmd_running) {
940 ret = usbduxsigma_submit_urbs(dev, devpriv->urbOut,
941 devpriv->numOfOutBuffers, 0);
946 devpriv->ao_cmd_running = 1;
947 s->async->inttrig = NULL;
954 static int usbdux_ao_cmdtest(struct comedi_device *dev,
955 struct comedi_subdevice *s,
956 struct comedi_cmd *cmd)
961 /* Step 1 : check if triggers are trivially valid */
963 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
965 if (0) { /* (this_usbduxsub->high_speed) */
967 * start immediately a new scan
968 * the sampling rate is set by the coversion rate
972 /* start a new scan (output at once) with a timer */
975 err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
977 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
978 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
979 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
984 /* Step 2a : make sure trigger sources are unique */
986 err |= cfc_check_trigger_is_unique(cmd->start_src);
987 err |= cfc_check_trigger_is_unique(cmd->stop_src);
989 /* Step 2b : and mutually compatible */
994 /* Step 3: check if arguments are trivially valid */
996 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
998 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
999 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
1001 if (cmd->scan_begin_src == TRIG_TIMER)
1002 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
1005 /* not used now, is for later use */
1006 if (cmd->convert_src == TRIG_TIMER)
1007 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
1009 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
1011 if (cmd->stop_src == TRIG_COUNT) {
1012 /* any count is allowed */
1015 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1024 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1026 struct usbduxsigma_private *this_usbduxsub = dev->private;
1027 struct comedi_cmd *cmd = &s->async->cmd;
1028 unsigned int chan, gain;
1031 down(&this_usbduxsub->sem);
1032 /* set current channel of the running acquisition to zero */
1033 s->async->cur_chan = 0;
1034 for (i = 0; i < cmd->chanlist_len; ++i) {
1035 chan = CR_CHAN(cmd->chanlist[i]);
1036 gain = CR_RANGE(cmd->chanlist[i]);
1037 this_usbduxsub->dac_commands[i] = chan;
1038 dev_dbg(&this_usbduxsub->interface->dev,
1039 "comedi%d: dac command for ch %d is %x\n",
1040 dev->minor, i, this_usbduxsub->dac_commands[i]);
1043 /* we count in steps of 1ms (125us) */
1044 /* 125us mode not used yet */
1045 if (0) { /* (this_usbduxsub->high_speed) */
1047 /* timing of the conversion itself: every 125 us */
1048 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1051 /* timing of the scan: we get all channels at once */
1052 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1053 dev_dbg(&this_usbduxsub->interface->dev,
1054 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1055 "convert_src=%d, convert_arg=%d\n", dev->minor,
1056 cmd->scan_begin_src, cmd->scan_begin_arg,
1057 cmd->convert_src, cmd->convert_arg);
1058 dev_dbg(&this_usbduxsub->interface->dev,
1059 "comedi%d: ao_timer=%d (ms)\n",
1060 dev->minor, this_usbduxsub->ao_timer);
1061 if (this_usbduxsub->ao_timer < 1) {
1062 dev_err(&this_usbduxsub->interface->dev,
1063 "comedi%d: usbdux: ao_timer=%d, "
1064 "scan_begin_arg=%d. "
1065 "Not properly tested by cmdtest?\n",
1066 dev->minor, this_usbduxsub->ao_timer,
1067 cmd->scan_begin_arg);
1068 up(&this_usbduxsub->sem);
1072 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1074 if (cmd->stop_src == TRIG_COUNT) {
1075 /* not continuous */
1077 /* high speed also scans everything at once */
1078 if (0) { /* (this_usbduxsub->high_speed) */
1079 this_usbduxsub->ao_sample_count =
1080 (cmd->stop_arg) * (cmd->scan_end_arg);
1082 /* there's no scan as the scan has been */
1083 /* perf inside the FX2 */
1084 /* data arrives as one packet */
1085 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1087 this_usbduxsub->ao_continuous = 0;
1089 /* continuous acquisition */
1090 this_usbduxsub->ao_continuous = 1;
1091 this_usbduxsub->ao_sample_count = 0;
1094 if (cmd->start_src == TRIG_NOW) {
1095 /* enable this acquisition operation */
1096 ret = usbduxsigma_submit_urbs(dev, this_usbduxsub->urbOut,
1097 this_usbduxsub->numOfOutBuffers,
1100 up(&this_usbduxsub->sem);
1103 this_usbduxsub->ao_cmd_running = 1;
1104 s->async->inttrig = NULL;
1107 /* submit the urbs later */
1108 /* wait for an internal signal */
1109 s->async->inttrig = usbduxsigma_ao_inttrig;
1112 up(&this_usbduxsub->sem);
1116 static int usbdux_dio_insn_config(struct comedi_device *dev,
1117 struct comedi_subdevice *s,
1118 struct comedi_insn *insn, unsigned int *data)
1120 int chan = CR_CHAN(insn->chanspec);
1122 /* The input or output configuration of each digital line is
1123 * configured by a special insn_config instruction. chanspec
1124 * contains the channel to be changed, and data[0] contains the
1125 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1128 case INSN_CONFIG_DIO_OUTPUT:
1129 s->io_bits |= 1 << chan; /* 1 means Out */
1131 case INSN_CONFIG_DIO_INPUT:
1132 s->io_bits &= ~(1 << chan);
1134 case INSN_CONFIG_DIO_QUERY:
1136 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1142 /* we don't tell the firmware here as it would take 8 frames */
1143 /* to submit the information. We do it in the insn_bits. */
1147 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1148 struct comedi_subdevice *s,
1149 struct comedi_insn *insn,
1152 struct usbduxsigma_private *this_usbduxsub = dev->private;
1155 down(&this_usbduxsub->sem);
1157 /* The insn data is a mask in data[0] and the new data
1158 * in data[1], each channel cooresponding to a bit. */
1159 s->state &= ~data[0];
1160 s->state |= data[0] & data[1];
1161 /* The commands are 8 bits wide */
1162 this_usbduxsub->dux_commands[1] = (s->io_bits) & 0x000000FF;
1163 this_usbduxsub->dux_commands[4] = (s->state) & 0x000000FF;
1164 this_usbduxsub->dux_commands[2] = ((s->io_bits) & 0x0000FF00) >> 8;
1165 this_usbduxsub->dux_commands[5] = ((s->state) & 0x0000FF00) >> 8;
1166 this_usbduxsub->dux_commands[3] = ((s->io_bits) & 0x00FF0000) >> 16;
1167 this_usbduxsub->dux_commands[6] = ((s->state) & 0x00FF0000) >> 16;
1169 /* This command also tells the firmware to return */
1170 /* the digital input lines */
1171 err = send_dux_commands(dev, SENDDIOBITSCOMMAND);
1173 up(&this_usbduxsub->sem);
1176 err = receive_dux_commands(dev, SENDDIOBITSCOMMAND);
1178 up(&this_usbduxsub->sem);
1182 data[1] = (((unsigned int)(this_usbduxsub->insnBuffer[1]))&0xff) |
1183 ((((unsigned int)(this_usbduxsub->insnBuffer[2]))&0xff) << 8) |
1184 ((((unsigned int)(this_usbduxsub->insnBuffer[3]))&0xff) << 16);
1188 up(&this_usbduxsub->sem);
1192 static void usbdux_pwm_stop(struct usbduxsigma_private *devpriv, int do_unlink)
1195 if (devpriv->urbPwm)
1196 usb_kill_urb(devpriv->urbPwm);
1199 devpriv->pwm_cmd_running = 0;
1202 static int usbdux_pwm_cancel(struct comedi_device *dev,
1203 struct comedi_subdevice *s)
1205 struct usbduxsigma_private *devpriv = dev->private;
1207 /* unlink only if it is really running */
1208 usbdux_pwm_stop(devpriv, devpriv->pwm_cmd_running);
1210 return send_dux_commands(dev, SENDPWMOFF);
1213 static void usbduxsub_pwm_irq(struct urb *urb)
1215 struct comedi_device *dev = urb->context;
1216 struct usbduxsigma_private *devpriv = dev->private;
1219 switch (urb->status) {
1228 /* happens after an unlink command */
1229 if (devpriv->pwm_cmd_running)
1230 usbdux_pwm_stop(devpriv, 0); /* w/o unlink */
1235 if (devpriv->pwm_cmd_running) {
1236 dev_err(dev->class_dev,
1237 "%s: non-zero urb status (%d)\n",
1238 __func__, urb->status);
1239 usbdux_pwm_stop(devpriv, 0); /* w/o unlink */
1244 if (!devpriv->pwm_cmd_running)
1247 urb->transfer_buffer_length = devpriv->sizePwmBuf;
1248 urb->dev = comedi_to_usb_dev(dev);
1250 ret = usb_submit_urb(urb, GFP_ATOMIC);
1252 dev_err(dev->class_dev, "%s: urb resubmit failed (%d)\n",
1254 if (ret == EL2NSYNC)
1255 dev_err(dev->class_dev,
1256 "buggy USB host controller or bug in IRQ handler\n");
1257 usbdux_pwm_stop(devpriv, 0); /* w/o unlink */
1261 static int usbduxsigma_submit_pwm_urb(struct comedi_device *dev)
1263 struct usb_device *usb = comedi_to_usb_dev(dev);
1264 struct usbduxsigma_private *devpriv = dev->private;
1265 struct urb *urb = devpriv->urbPwm;
1267 /* in case of a resubmission after an unlink... */
1268 usb_fill_bulk_urb(urb, usb, usb_sndbulkpipe(usb, PWM_EP),
1269 urb->transfer_buffer, devpriv->sizePwmBuf,
1270 usbduxsub_pwm_irq, dev);
1272 return usb_submit_urb(urb, GFP_ATOMIC);
1275 static int usbdux_pwm_period(struct comedi_device *dev,
1276 struct comedi_subdevice *s, unsigned int period)
1278 struct usbduxsigma_private *this_usbduxsub = dev->private;
1281 if (period < MIN_PWM_PERIOD) {
1282 dev_err(&this_usbduxsub->interface->dev,
1283 "comedi%d: illegal period setting for pwm.\n",
1287 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1288 if (fx2delay > 255) {
1289 dev_err(&this_usbduxsub->interface->dev,
1290 "comedi%d: period %d for pwm is too low.\n",
1291 dev->minor, period);
1295 this_usbduxsub->pwmDelay = fx2delay;
1296 this_usbduxsub->pwmPeriod = period;
1300 /* is called from insn so there's no need to do all the sanity checks */
1301 static int usbdux_pwm_start(struct comedi_device *dev,
1302 struct comedi_subdevice *s)
1304 struct usbduxsigma_private *this_usbduxsub = dev->private;
1307 if (this_usbduxsub->pwm_cmd_running) {
1308 /* already running */
1312 this_usbduxsub->dux_commands[1] = ((uint8_t) this_usbduxsub->pwmDelay);
1313 ret = send_dux_commands(dev, SENDPWMON);
1317 /* initialise the buffer */
1318 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
1319 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
1321 ret = usbduxsigma_submit_pwm_urb(dev);
1324 this_usbduxsub->pwm_cmd_running = 1;
1329 static int usbduxsigma_pwm_pattern(struct comedi_device *dev,
1330 struct comedi_subdevice *s,
1335 struct usbduxsigma_private *devpriv = dev->private;
1336 char pwm_mask = (1 << chan); /* DIO bit for the PWM data */
1337 char sgn_mask = (16 << chan); /* DIO bit for the sign */
1338 char *buf = (char *)(devpriv->urbPwm->transfer_buffer);
1339 int szbuf = devpriv->sizePwmBuf;
1342 for (i = 0; i < szbuf; i++) {
1357 static int usbduxsigma_pwm_write(struct comedi_device *dev,
1358 struct comedi_subdevice *s,
1359 struct comedi_insn *insn,
1362 unsigned int chan = CR_CHAN(insn->chanspec);
1365 * It doesn't make sense to support more than one value here
1366 * because it would just overwrite the PWM buffer.
1372 * The sign is set via a special INSN only, this gives us 8 bits
1373 * for normal operation, sign is 0 by default.
1375 return usbduxsigma_pwm_pattern(dev, s, chan, data[0], 0);
1378 static int usbduxsigma_pwm_config(struct comedi_device *dev,
1379 struct comedi_subdevice *s,
1380 struct comedi_insn *insn,
1383 struct usbduxsigma_private *devpriv = dev->private;
1384 unsigned int chan = CR_CHAN(insn->chanspec);
1387 case INSN_CONFIG_ARM:
1389 * if not zero the PWM is limited to a certain time which is
1390 * not supported here
1394 return usbdux_pwm_start(dev, s);
1395 case INSN_CONFIG_DISARM:
1396 return usbdux_pwm_cancel(dev, s);
1397 case INSN_CONFIG_GET_PWM_STATUS:
1398 data[1] = devpriv->pwm_cmd_running;
1400 case INSN_CONFIG_PWM_SET_PERIOD:
1401 return usbdux_pwm_period(dev, s, data[1]);
1402 case INSN_CONFIG_PWM_GET_PERIOD:
1403 data[1] = devpriv->pwmPeriod;
1405 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1408 * data[2] = sign (for a relay)
1410 return usbduxsigma_pwm_pattern(dev, s, chan,
1411 data[1], (data[2] != 0));
1412 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1413 /* values are not kept in this driver, nothing to return */
1419 static int usbduxsigma_getstatusinfo(struct comedi_device *dev, int chan)
1421 struct usbduxsigma_private *devpriv = dev->private;
1429 sysred = 0; /* ADC zero */
1432 sysred = 1; /* ADC offset */
1435 sysred = 4; /* VCC */
1438 sysred = 8; /* temperature */
1441 sysred = 16; /* gain */
1444 sysred = 32; /* ref */
1448 devpriv->dux_commands[1] = 0x12; /* CONFIG0 */
1449 devpriv->dux_commands[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
1450 devpriv->dux_commands[3] = 0x00; /* CONFIG3: diff. channels off */
1451 devpriv->dux_commands[4] = 0;
1452 devpriv->dux_commands[5] = 0;
1453 devpriv->dux_commands[6] = sysred;
1454 ret = send_dux_commands(dev, SENDSINGLEAD);
1458 ret = receive_dux_commands(dev, SENDSINGLEAD);
1462 /* 32 bits big endian from the A/D converter */
1463 val = be32_to_cpu(*((int32_t *)((devpriv->insnBuffer)+1)));
1464 val &= 0x00ffffff; /* strip status byte */
1465 val ^= 0x00800000; /* convert to unsigned */
1470 static int usbduxsigma_attach_common(struct comedi_device *dev)
1472 struct usbduxsigma_private *devpriv = dev->private;
1473 struct comedi_subdevice *s;
1478 down(&devpriv->sem);
1480 if (devpriv->high_speed)
1481 n_subdevs = 4; /* with pwm */
1483 n_subdevs = 3; /* without pwm */
1484 ret = comedi_alloc_subdevices(dev, n_subdevs);
1490 /* Analog Input subdevice */
1491 s = &dev->subdevices[0];
1492 dev->read_subdev = s;
1493 s->type = COMEDI_SUBD_AI;
1494 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ | SDF_LSAMPL;
1495 s->n_chan = NUMCHANNELS;
1496 s->len_chanlist = NUMCHANNELS;
1497 s->maxdata = 0x00ffffff;
1498 s->range_table = &range_usbdux_ai_range;
1499 s->insn_read = usbdux_ai_insn_read;
1500 s->do_cmdtest = usbdux_ai_cmdtest;
1501 s->do_cmd = usbdux_ai_cmd;
1502 s->cancel = usbdux_ai_cancel;
1504 /* Analog Output subdevice */
1505 s = &dev->subdevices[1];
1506 dev->write_subdev = s;
1507 s->type = COMEDI_SUBD_AO;
1508 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1510 s->len_chanlist = 4;
1511 s->maxdata = 0x00ff;
1512 s->range_table = &range_unipolar2_5;
1513 s->insn_write = usbdux_ao_insn_write;
1514 s->insn_read = usbdux_ao_insn_read;
1515 s->do_cmdtest = usbdux_ao_cmdtest;
1516 s->do_cmd = usbdux_ao_cmd;
1517 s->cancel = usbdux_ao_cancel;
1519 /* Digital I/O subdevice */
1520 s = &dev->subdevices[2];
1521 s->type = COMEDI_SUBD_DIO;
1522 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1525 s->range_table = &range_digital;
1526 s->insn_bits = usbdux_dio_insn_bits;
1527 s->insn_config = usbdux_dio_insn_config;
1529 if (devpriv->high_speed) {
1530 /* Timer / pwm subdevice */
1531 s = &dev->subdevices[3];
1532 s->type = COMEDI_SUBD_PWM;
1533 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
1535 s->maxdata = devpriv->sizePwmBuf;
1536 s->insn_write = usbduxsigma_pwm_write;
1537 s->insn_config = usbduxsigma_pwm_config;
1539 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
1544 offset = usbduxsigma_getstatusinfo(dev, 0);
1546 dev_err(dev->class_dev,
1547 "Communication to USBDUXSIGMA failed! Check firmware and cabling\n");
1549 dev_info(dev->class_dev, "attached, ADC_zero = %x\n", offset);
1554 static int usbduxsigma_firmware_upload(struct comedi_device *dev,
1555 const u8 *data, size_t size,
1556 unsigned long context)
1558 struct usb_device *usb = comedi_to_usb_dev(dev);
1566 if (size > FIRMWARE_MAX_LEN) {
1567 dev_err(dev->class_dev, "firmware binary too large for FX2\n");
1571 /* we generate a local buffer for the firmware */
1572 buf = kmemdup(data, size, GFP_KERNEL);
1576 /* we need a malloc'ed buffer for usb_control_msg() */
1577 tmp = kmalloc(1, GFP_KERNEL);
1583 /* stop the current firmware on the device */
1584 *tmp = 1; /* 7f92 to one */
1585 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1588 USBDUXSUB_CPUCS, 0x0000,
1592 dev_err(dev->class_dev, "can not stop firmware\n");
1596 /* upload the new firmware to the device */
1597 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1604 dev_err(dev->class_dev, "firmware upload failed\n");
1608 /* start the new firmware on the device */
1609 *tmp = 0; /* 7f92 to zero */
1610 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1613 USBDUXSUB_CPUCS, 0x0000,
1617 dev_err(dev->class_dev, "can not start firmware\n");
1625 static int usbduxsigma_alloc_usb_buffers(struct comedi_device *dev)
1627 struct usb_device *usb = comedi_to_usb_dev(dev);
1628 struct usbduxsigma_private *devpriv = dev->private;
1632 devpriv->dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
1633 devpriv->dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
1634 devpriv->inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1635 devpriv->insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
1636 devpriv->outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1637 devpriv->urbIn = kcalloc(devpriv->numOfInBuffers, sizeof(*urb),
1639 devpriv->urbOut = kcalloc(devpriv->numOfOutBuffers, sizeof(*urb),
1641 if (!devpriv->dac_commands || !devpriv->dux_commands ||
1642 !devpriv->inBuffer || !devpriv->insnBuffer ||
1643 !devpriv->outBuffer || !devpriv->urbIn || !devpriv->urbOut)
1646 for (i = 0; i < devpriv->numOfInBuffers; i++) {
1647 /* one frame: 1ms */
1648 urb = usb_alloc_urb(1, GFP_KERNEL);
1651 devpriv->urbIn[i] = urb;
1653 /* will be filled later with a pointer to the comedi-device */
1654 /* and ONLY then the urb should be submitted */
1655 urb->context = NULL;
1656 urb->pipe = usb_rcvisocpipe(usb, ISOINEP);
1657 urb->transfer_flags = URB_ISO_ASAP;
1658 urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1659 if (!urb->transfer_buffer)
1661 urb->complete = usbduxsub_ai_IsocIrq;
1662 urb->number_of_packets = 1;
1663 urb->transfer_buffer_length = SIZEINBUF;
1664 urb->iso_frame_desc[0].offset = 0;
1665 urb->iso_frame_desc[0].length = SIZEINBUF;
1668 for (i = 0; i < devpriv->numOfOutBuffers; i++) {
1669 /* one frame: 1ms */
1670 urb = usb_alloc_urb(1, GFP_KERNEL);
1673 devpriv->urbOut[i] = urb;
1675 /* will be filled later with a pointer to the comedi-device */
1676 /* and ONLY then the urb should be submitted */
1677 urb->context = NULL;
1678 urb->pipe = usb_sndisocpipe(usb, ISOOUTEP);
1679 urb->transfer_flags = URB_ISO_ASAP;
1680 urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1681 if (!urb->transfer_buffer)
1683 urb->complete = usbduxsub_ao_IsocIrq;
1684 urb->number_of_packets = 1;
1685 urb->transfer_buffer_length = SIZEOUTBUF;
1686 urb->iso_frame_desc[0].offset = 0;
1687 urb->iso_frame_desc[0].length = SIZEOUTBUF;
1688 if (devpriv->high_speed)
1689 urb->interval = 8; /* uframes */
1691 urb->interval = 1; /* frames */
1694 if (devpriv->high_speed) {
1695 /* max bulk ep size in high speed */
1696 devpriv->sizePwmBuf = 512;
1697 urb = usb_alloc_urb(0, GFP_KERNEL);
1700 devpriv->urbPwm = urb;
1701 urb->transfer_buffer = kzalloc(devpriv->sizePwmBuf, GFP_KERNEL);
1702 if (!urb->transfer_buffer)
1705 devpriv->urbPwm = NULL;
1706 devpriv->sizePwmBuf = 0;
1712 static void usbduxsigma_free_usb_buffers(struct comedi_device *dev)
1714 struct usbduxsigma_private *devpriv = dev->private;
1718 /* force unlink all urbs */
1719 usbdux_ai_stop(devpriv, 1);
1720 usbdux_ao_stop(devpriv, 1);
1721 usbdux_pwm_stop(devpriv, 1);
1723 urb = devpriv->urbPwm;
1725 kfree(urb->transfer_buffer);
1728 if (devpriv->urbOut) {
1729 for (i = 0; i < devpriv->numOfOutBuffers; i++) {
1730 urb = devpriv->urbOut[i];
1732 kfree(urb->transfer_buffer);
1736 kfree(devpriv->urbOut);
1738 if (devpriv->urbIn) {
1739 for (i = 0; i < devpriv->numOfInBuffers; i++) {
1740 urb = devpriv->urbIn[i];
1742 kfree(urb->transfer_buffer);
1746 kfree(devpriv->urbIn);
1748 kfree(devpriv->outBuffer);
1749 kfree(devpriv->insnBuffer);
1750 kfree(devpriv->inBuffer);
1751 kfree(devpriv->dux_commands);
1752 kfree(devpriv->dac_commands);
1755 static int usbduxsigma_auto_attach(struct comedi_device *dev,
1756 unsigned long context_unused)
1758 struct usb_interface *intf = comedi_to_usb_interface(dev);
1759 struct usb_device *usb = comedi_to_usb_dev(dev);
1760 struct usbduxsigma_private *devpriv;
1763 devpriv = kzalloc(sizeof(*devpriv), GFP_KERNEL);
1766 dev->private = devpriv;
1768 sema_init(&devpriv->sem, 1);
1769 devpriv->interface = intf;
1770 usb_set_intfdata(intf, devpriv);
1772 ret = usb_set_interface(usb,
1773 intf->altsetting->desc.bInterfaceNumber, 3);
1775 dev_err(dev->class_dev,
1776 "could not set alternate setting 3 in high speed\n");
1780 /* test if it is high speed (USB 2.0) */
1781 devpriv->high_speed = (usb->speed == USB_SPEED_HIGH);
1782 if (devpriv->high_speed) {
1783 devpriv->numOfInBuffers = NUMOFINBUFFERSHIGH;
1784 devpriv->numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
1786 devpriv->numOfInBuffers = NUMOFINBUFFERSFULL;
1787 devpriv->numOfOutBuffers = NUMOFOUTBUFFERSFULL;
1790 ret = usbduxsigma_alloc_usb_buffers(dev);
1794 ret = comedi_load_firmware(dev, &usb->dev, FIRMWARE,
1795 usbduxsigma_firmware_upload, 0);
1799 return usbduxsigma_attach_common(dev);
1802 static void usbduxsigma_detach(struct comedi_device *dev)
1804 struct usb_interface *intf = comedi_to_usb_interface(dev);
1805 struct usbduxsigma_private *devpriv = dev->private;
1810 usb_set_intfdata(intf, NULL);
1812 down(&devpriv->sem);
1813 usbduxsigma_free_usb_buffers(dev);
1817 static struct comedi_driver usbduxsigma_driver = {
1818 .driver_name = "usbduxsigma",
1819 .module = THIS_MODULE,
1820 .auto_attach = usbduxsigma_auto_attach,
1821 .detach = usbduxsigma_detach,
1824 static int usbduxsigma_usb_probe(struct usb_interface *intf,
1825 const struct usb_device_id *id)
1827 return comedi_usb_auto_config(intf, &usbduxsigma_driver, 0);
1830 static const struct usb_device_id usbduxsigma_usb_table[] = {
1831 { USB_DEVICE(0x13d8, 0x0020) },
1832 { USB_DEVICE(0x13d8, 0x0021) },
1833 { USB_DEVICE(0x13d8, 0x0022) },
1836 MODULE_DEVICE_TABLE(usb, usbduxsigma_usb_table);
1838 static struct usb_driver usbduxsigma_usb_driver = {
1839 .name = "usbduxsigma",
1840 .probe = usbduxsigma_usb_probe,
1841 .disconnect = comedi_usb_auto_unconfig,
1842 .id_table = usbduxsigma_usb_table,
1844 module_comedi_usb_driver(usbduxsigma_driver, usbduxsigma_usb_driver);
1846 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1847 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com");
1848 MODULE_LICENSE("GPL");
1849 MODULE_FIRMWARE(FIRMWARE);