3 * Copyright (C) 2011-2014 Bernd Porr, mail@berndporr.me.uk
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.
18 * Description: University of Stirling USB DAQ & INCITE Technology Limited
19 * Devices: [ITL] USB-DUX-SIGMA (usbduxsigma)
20 * Author: Bernd Porr <mail@berndporr.me.uk>
21 * Updated: 10 Oct 2014
26 * I must give credit here to Chris Baugher who
27 * wrote the driver for AT-MIO-16d. I used some parts of this
28 * driver. I also must give credits to David Brownell
29 * who supported me with the USB development.
31 * Note: the raw data from the A/D converter is 24 bit big endian
32 * anything else is little endian to/from the dux board
36 * 0.1: initial version
37 * 0.2: all basic functions implemented, digital I/O only for one port
38 * 0.3: proper vendor ID and driver name
39 * 0.4: fixed D/A voltage range
40 * 0.5: various bug fixes, health check at startup
41 * 0.6: corrected wrong input range
44 #include <linux/kernel.h>
45 #include <linux/module.h>
46 #include <linux/slab.h>
47 #include <linux/input.h>
48 #include <linux/fcntl.h>
49 #include <linux/compiler.h>
50 #include <asm/unaligned.h>
52 #include "comedi_fc.h"
53 #include "../comedi_usb.h"
55 /* timeout for the USB-transfer in ms*/
56 #define BULK_TIMEOUT 1000
58 /* constants for "firmware" upload and download */
59 #define FIRMWARE "usbduxsigma_firmware.bin"
60 #define FIRMWARE_MAX_LEN 0x4000
61 #define USBDUXSUB_FIRMWARE 0xa0
62 #define VENDOR_DIR_IN 0xc0
63 #define VENDOR_DIR_OUT 0x40
65 /* internal addresses of the 8051 processor */
66 #define USBDUXSUB_CPUCS 0xE600
68 /* 300Hz max frequ under PWM */
69 #define MIN_PWM_PERIOD ((long)(1E9/300))
71 /* Default PWM frequency */
72 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
74 /* Number of channels (16 AD and offset)*/
75 #define NUMCHANNELS 16
77 /* Size of one A/D value */
78 #define SIZEADIN ((sizeof(uint32_t)))
81 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
84 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
87 #define SIZEINSNBUF 16
89 /* Number of DA channels */
90 #define NUMOUTCHANNELS 8
92 /* size of one value for the D/A converter: channel and value */
93 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(uint16_t)))
96 * Size of the output-buffer in bytes
97 * Actually only the first 4 triplets are used but for the
98 * high speed mode we need to pad it to 8 (microframes).
100 #define SIZEOUTBUF ((8*SIZEDAOUT))
103 * Size of the buffer for the dux commands: just now max size is determined
104 * by the analogue out + command byte + panic bytes...
106 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
108 /* Number of in-URBs which receive the data: min=2 */
109 #define NUMOFINBUFFERSFULL 5
111 /* Number of out-URBs which send the data: min=2 */
112 #define NUMOFOUTBUFFERSFULL 5
114 /* Number of in-URBs which receive the data: min=5 */
115 /* must have more buffers due to buggy USB ctr */
116 #define NUMOFINBUFFERSHIGH 10
118 /* Number of out-URBs which send the data: min=5 */
119 /* must have more buffers due to buggy USB ctr */
120 #define NUMOFOUTBUFFERSHIGH 10
122 /* number of retries to get the right dux command */
125 /* bulk transfer commands to usbduxsigma */
126 #define USBBUXSIGMA_AD_CMD 0
127 #define USBDUXSIGMA_DA_CMD 1
128 #define USBDUXSIGMA_DIO_CFG_CMD 2
129 #define USBDUXSIGMA_DIO_BITS_CMD 3
130 #define USBDUXSIGMA_SINGLE_AD_CMD 4
131 #define USBDUXSIGMA_PWM_ON_CMD 7
132 #define USBDUXSIGMA_PWM_OFF_CMD 8
134 static const struct comedi_lrange usbduxsigma_ai_range = {
136 BIP_RANGE(2.5 * 0x800000 / 0x780000 / 2.0)
140 struct usbduxsigma_private {
141 /* actual number of in-buffers */
143 /* actual number of out-buffers */
145 /* ISO-transfer handling: buffers */
146 struct urb **ai_urbs;
147 struct urb **ao_urbs;
148 /* pwm-transfer handling */
151 unsigned int pwm_period;
152 /* PWM internal delay for the GPIF in the FX2 */
154 /* size of the PWM buffer which holds the bit pattern */
156 /* input buffer for the ISO-transfer */
158 /* input buffer for single insn */
161 unsigned high_speed:1;
162 unsigned ai_cmd_running:1;
163 unsigned ao_cmd_running:1;
164 unsigned pwm_cmd_running:1;
166 /* time between samples in units of the timer */
167 unsigned int ai_timer;
168 unsigned int ao_timer;
169 /* counter between acquisitions */
170 unsigned int ai_counter;
171 unsigned int ao_counter;
172 /* interval in frames/uframes */
173 unsigned int ai_interval;
175 uint8_t *dux_commands;
176 struct semaphore sem;
179 static void usbduxsigma_unlink_urbs(struct urb **urbs, int num_urbs)
183 for (i = 0; i < num_urbs; i++)
184 usb_kill_urb(urbs[i]);
187 static void usbduxsigma_ai_stop(struct comedi_device *dev, int do_unlink)
189 struct usbduxsigma_private *devpriv = dev->private;
191 if (do_unlink && devpriv->ai_urbs)
192 usbduxsigma_unlink_urbs(devpriv->ai_urbs, devpriv->n_ai_urbs);
194 devpriv->ai_cmd_running = 0;
197 static int usbduxsigma_ai_cancel(struct comedi_device *dev,
198 struct comedi_subdevice *s)
200 struct usbduxsigma_private *devpriv = dev->private;
203 /* unlink only if it is really running */
204 usbduxsigma_ai_stop(dev, devpriv->ai_cmd_running);
210 static void usbduxsigma_ai_handle_urb(struct comedi_device *dev,
211 struct comedi_subdevice *s,
214 struct usbduxsigma_private *devpriv = dev->private;
215 struct comedi_async *async = s->async;
216 struct comedi_cmd *cmd = &async->cmd;
221 devpriv->ai_counter--;
222 if (devpriv->ai_counter == 0) {
223 devpriv->ai_counter = devpriv->ai_timer;
225 /* get the data from the USB bus and hand it over to comedi */
226 for (i = 0; i < cmd->chanlist_len; i++) {
227 /* transfer data, note first byte is the DIO state */
228 val = be32_to_cpu(devpriv->in_buf[i+1]);
229 val &= 0x00ffffff; /* strip status byte */
230 val ^= 0x00800000; /* convert to unsigned */
232 if (!comedi_buf_write_samples(s, &val, 1))
236 if (cmd->stop_src == TRIG_COUNT &&
237 async->scans_done >= cmd->stop_arg)
238 async->events |= COMEDI_CB_EOA;
241 /* if command is still running, resubmit urb */
242 if (!(async->events & COMEDI_CB_CANCEL_MASK)) {
243 urb->dev = comedi_to_usb_dev(dev);
244 ret = usb_submit_urb(urb, GFP_ATOMIC);
246 dev_err(dev->class_dev,
247 "%s: urb resubmit failed (%d)\n",
249 if (ret == -EL2NSYNC)
250 dev_err(dev->class_dev,
251 "buggy USB host controller or bug in IRQ handler\n");
252 async->events |= COMEDI_CB_ERROR;
257 static void usbduxsigma_ai_urb_complete(struct urb *urb)
259 struct comedi_device *dev = urb->context;
260 struct usbduxsigma_private *devpriv = dev->private;
261 struct comedi_subdevice *s = dev->read_subdev;
262 struct comedi_async *async = s->async;
264 /* exit if not running a command, do not resubmit urb */
265 if (!devpriv->ai_cmd_running)
268 switch (urb->status) {
270 /* copy the result in the transfer buffer */
271 memcpy(devpriv->in_buf, urb->transfer_buffer, SIZEINBUF);
272 usbduxsigma_ai_handle_urb(dev, s, urb);
277 * error in the ISOchronous data
278 * we don't copy the data into the transfer buffer
279 * and recycle the last data byte
281 dev_dbg(dev->class_dev, "CRC error in ISO IN stream\n");
282 usbduxsigma_ai_handle_urb(dev, s, urb);
289 /* happens after an unlink command */
290 async->events |= COMEDI_CB_ERROR;
295 dev_err(dev->class_dev, "%s: non-zero urb status (%d)\n",
296 __func__, urb->status);
297 async->events |= COMEDI_CB_ERROR;
302 * comedi_handle_events() cannot be used in this driver. The (*cancel)
303 * operation would unlink the urb.
305 if (async->events & COMEDI_CB_CANCEL_MASK)
306 usbduxsigma_ai_stop(dev, 0);
308 comedi_event(dev, s);
311 static void usbduxsigma_ao_stop(struct comedi_device *dev, int do_unlink)
313 struct usbduxsigma_private *devpriv = dev->private;
315 if (do_unlink && devpriv->ao_urbs)
316 usbduxsigma_unlink_urbs(devpriv->ao_urbs, devpriv->n_ao_urbs);
318 devpriv->ao_cmd_running = 0;
321 static int usbduxsigma_ao_cancel(struct comedi_device *dev,
322 struct comedi_subdevice *s)
324 struct usbduxsigma_private *devpriv = dev->private;
327 /* unlink only if it is really running */
328 usbduxsigma_ao_stop(dev, devpriv->ao_cmd_running);
334 static void usbduxsigma_ao_handle_urb(struct comedi_device *dev,
335 struct comedi_subdevice *s,
338 struct usbduxsigma_private *devpriv = dev->private;
339 struct comedi_async *async = s->async;
340 struct comedi_cmd *cmd = &async->cmd;
345 devpriv->ao_counter--;
346 if (devpriv->ao_counter == 0) {
347 devpriv->ao_counter = devpriv->ao_timer;
349 if (cmd->stop_src == TRIG_COUNT &&
350 async->scans_done >= cmd->stop_arg) {
351 async->events |= COMEDI_CB_EOA;
355 /* transmit data to the USB bus */
356 datap = urb->transfer_buffer;
357 *datap++ = cmd->chanlist_len;
358 for (i = 0; i < cmd->chanlist_len; i++) {
359 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
362 if (!comedi_buf_read_samples(s, &val, 1)) {
363 dev_err(dev->class_dev, "buffer underflow\n");
364 async->events |= COMEDI_CB_OVERFLOW;
370 s->readback[chan] = val;
374 /* if command is still running, resubmit urb */
375 if (!(async->events & COMEDI_CB_CANCEL_MASK)) {
376 urb->transfer_buffer_length = SIZEOUTBUF;
377 urb->dev = comedi_to_usb_dev(dev);
379 if (devpriv->high_speed)
380 urb->interval = 8; /* uframes */
382 urb->interval = 1; /* frames */
383 urb->number_of_packets = 1;
384 urb->iso_frame_desc[0].offset = 0;
385 urb->iso_frame_desc[0].length = SIZEOUTBUF;
386 urb->iso_frame_desc[0].status = 0;
387 ret = usb_submit_urb(urb, GFP_ATOMIC);
389 dev_err(dev->class_dev,
390 "%s: urb resubmit failed (%d)\n",
392 if (ret == -EL2NSYNC)
393 dev_err(dev->class_dev,
394 "buggy USB host controller or bug in IRQ handler\n");
395 async->events |= COMEDI_CB_ERROR;
400 static void usbduxsigma_ao_urb_complete(struct urb *urb)
402 struct comedi_device *dev = urb->context;
403 struct usbduxsigma_private *devpriv = dev->private;
404 struct comedi_subdevice *s = dev->write_subdev;
405 struct comedi_async *async = s->async;
407 /* exit if not running a command, do not resubmit urb */
408 if (!devpriv->ao_cmd_running)
411 switch (urb->status) {
413 usbduxsigma_ao_handle_urb(dev, s, urb);
420 /* happens after an unlink command */
421 async->events |= COMEDI_CB_ERROR;
426 dev_err(dev->class_dev, "%s: non-zero urb status (%d)\n",
427 __func__, urb->status);
428 async->events |= COMEDI_CB_ERROR;
433 * comedi_handle_events() cannot be used in this driver. The (*cancel)
434 * operation would unlink the urb.
436 if (async->events & COMEDI_CB_CANCEL_MASK)
437 usbduxsigma_ao_stop(dev, 0);
439 comedi_event(dev, s);
442 static int usbduxsigma_submit_urbs(struct comedi_device *dev,
443 struct urb **urbs, int num_urbs,
446 struct usb_device *usb = comedi_to_usb_dev(dev);
447 struct usbduxsigma_private *devpriv = dev->private;
452 /* Submit all URBs and start the transfer on the bus */
453 for (i = 0; i < num_urbs; i++) {
456 /* in case of a resubmission after an unlink... */
458 urb->interval = devpriv->ai_interval;
462 urb->transfer_flags = URB_ISO_ASAP;
464 ret = usb_submit_urb(urb, GFP_ATOMIC);
471 static int usbduxsigma_chans_to_interval(int num_chan)
480 static int usbduxsigma_ai_cmdtest(struct comedi_device *dev,
481 struct comedi_subdevice *s,
482 struct comedi_cmd *cmd)
484 struct usbduxsigma_private *devpriv = dev->private;
485 int high_speed = devpriv->high_speed;
486 int interval = usbduxsigma_chans_to_interval(cmd->chanlist_len);
489 /* Step 1 : check if triggers are trivially valid */
491 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
492 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
493 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
494 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
495 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
500 /* Step 2a : make sure trigger sources are unique */
502 err |= cfc_check_trigger_is_unique(cmd->start_src);
503 err |= cfc_check_trigger_is_unique(cmd->stop_src);
505 /* Step 2b : and mutually compatible */
510 /* Step 3: check if arguments are trivially valid */
512 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
514 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
515 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
517 if (cmd->scan_begin_src == TRIG_TIMER) {
522 * In high speed mode microframes are possible.
523 * However, during one microframe we can roughly
524 * sample two channels. Thus, the more channels
525 * are in the channel list the more time we need.
527 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
528 (1000000 / 8 * interval));
530 tmp = (cmd->scan_begin_arg / 125000) * 125000;
533 /* 1kHz scans every USB frame */
534 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
537 tmp = (cmd->scan_begin_arg / 1000000) * 1000000;
539 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, tmp);
542 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
544 if (cmd->stop_src == TRIG_COUNT)
545 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
547 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
552 /* Step 4: fix up any arguments */
556 * every 2 channels get a time window of 125us. Thus, if we
557 * sample all 16 channels we need 1ms. If we sample only one
558 * channel we need only 125us
560 devpriv->ai_interval = interval;
561 devpriv->ai_timer = cmd->scan_begin_arg / (125000 * interval);
563 /* interval always 1ms */
564 devpriv->ai_interval = 1;
565 devpriv->ai_timer = cmd->scan_begin_arg / 1000000;
567 if (devpriv->ai_timer < 1)
577 * creates the ADC command for the MAX1271
578 * range is the range value from comedi
580 static void create_adc_command(unsigned int chan,
585 (*muxsg0) = (*muxsg0) | (1 << chan);
587 (*muxsg1) = (*muxsg1) | (1 << (chan-8));
590 static int usbbuxsigma_send_cmd(struct comedi_device *dev, int cmd_type)
592 struct usb_device *usb = comedi_to_usb_dev(dev);
593 struct usbduxsigma_private *devpriv = dev->private;
596 devpriv->dux_commands[0] = cmd_type;
598 return usb_bulk_msg(usb, usb_sndbulkpipe(usb, 1),
599 devpriv->dux_commands, SIZEOFDUXBUFFER,
600 &nsent, BULK_TIMEOUT);
603 static int usbduxsigma_receive_cmd(struct comedi_device *dev, int command)
605 struct usb_device *usb = comedi_to_usb_dev(dev);
606 struct usbduxsigma_private *devpriv = dev->private;
611 for (i = 0; i < RETRIES; i++) {
612 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, 8),
613 devpriv->insn_buf, SIZEINSNBUF,
614 &nrec, BULK_TIMEOUT);
618 if (devpriv->insn_buf[0] == command)
622 * This is only reached if the data has been requested a
623 * couple of times and the command was not received.
628 static int usbduxsigma_ai_inttrig(struct comedi_device *dev,
629 struct comedi_subdevice *s,
630 unsigned int trig_num)
632 struct usbduxsigma_private *devpriv = dev->private;
633 struct comedi_cmd *cmd = &s->async->cmd;
636 if (trig_num != cmd->start_arg)
640 if (!devpriv->ai_cmd_running) {
641 devpriv->ai_cmd_running = 1;
642 ret = usbduxsigma_submit_urbs(dev, devpriv->ai_urbs,
643 devpriv->n_ai_urbs, 1);
645 devpriv->ai_cmd_running = 0;
649 s->async->inttrig = NULL;
656 static int usbduxsigma_ai_cmd(struct comedi_device *dev,
657 struct comedi_subdevice *s)
659 struct usbduxsigma_private *devpriv = dev->private;
660 struct comedi_cmd *cmd = &s->async->cmd;
661 unsigned int len = cmd->chanlist_len;
670 for (i = 0; i < len; i++) {
671 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
673 create_adc_command(chan, &muxsg0, &muxsg1);
676 devpriv->dux_commands[1] = len; /* num channels per time step */
677 devpriv->dux_commands[2] = 0x12; /* CONFIG0 */
678 devpriv->dux_commands[3] = 0x03; /* CONFIG1: 23kHz sample, delay 0us */
679 devpriv->dux_commands[4] = 0x00; /* CONFIG3: diff. channels off */
680 devpriv->dux_commands[5] = muxsg0;
681 devpriv->dux_commands[6] = muxsg1;
682 devpriv->dux_commands[7] = sysred;
684 ret = usbbuxsigma_send_cmd(dev, USBBUXSIGMA_AD_CMD);
690 devpriv->ai_counter = devpriv->ai_timer;
692 if (cmd->start_src == TRIG_NOW) {
693 /* enable this acquisition operation */
694 devpriv->ai_cmd_running = 1;
695 ret = usbduxsigma_submit_urbs(dev, devpriv->ai_urbs,
696 devpriv->n_ai_urbs, 1);
698 devpriv->ai_cmd_running = 0;
702 s->async->inttrig = NULL;
703 } else { /* TRIG_INT */
704 s->async->inttrig = usbduxsigma_ai_inttrig;
712 static int usbduxsigma_ai_insn_read(struct comedi_device *dev,
713 struct comedi_subdevice *s,
714 struct comedi_insn *insn,
717 struct usbduxsigma_private *devpriv = dev->private;
718 unsigned int chan = CR_CHAN(insn->chanspec);
726 if (devpriv->ai_cmd_running) {
731 create_adc_command(chan, &muxsg0, &muxsg1);
733 /* Mode 0 is used to get a single conversion on demand */
734 devpriv->dux_commands[1] = 0x16; /* CONFIG0: chopper on */
735 devpriv->dux_commands[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
736 devpriv->dux_commands[3] = 0x00; /* CONFIG3: diff. channels off */
737 devpriv->dux_commands[4] = muxsg0;
738 devpriv->dux_commands[5] = muxsg1;
739 devpriv->dux_commands[6] = sysred;
742 ret = usbbuxsigma_send_cmd(dev, USBDUXSIGMA_SINGLE_AD_CMD);
748 for (i = 0; i < insn->n; i++) {
751 ret = usbduxsigma_receive_cmd(dev, USBDUXSIGMA_SINGLE_AD_CMD);
757 /* 32 bits big endian from the A/D converter */
758 val = be32_to_cpu(get_unaligned((__be32
759 *)(devpriv->insn_buf + 1)));
760 val &= 0x00ffffff; /* strip status byte */
761 val ^= 0x00800000; /* convert to unsigned */
770 static int usbduxsigma_ao_insn_read(struct comedi_device *dev,
771 struct comedi_subdevice *s,
772 struct comedi_insn *insn,
775 struct usbduxsigma_private *devpriv = dev->private;
779 ret = comedi_readback_insn_read(dev, s, insn, data);
785 static int usbduxsigma_ao_insn_write(struct comedi_device *dev,
786 struct comedi_subdevice *s,
787 struct comedi_insn *insn,
790 struct usbduxsigma_private *devpriv = dev->private;
791 unsigned int chan = CR_CHAN(insn->chanspec);
796 if (devpriv->ao_cmd_running) {
801 for (i = 0; i < insn->n; i++) {
802 devpriv->dux_commands[1] = 1; /* num channels */
803 devpriv->dux_commands[2] = data[i]; /* value */
804 devpriv->dux_commands[3] = chan; /* channel number */
805 ret = usbbuxsigma_send_cmd(dev, USBDUXSIGMA_DA_CMD);
810 s->readback[chan] = data[i];
817 static int usbduxsigma_ao_inttrig(struct comedi_device *dev,
818 struct comedi_subdevice *s,
819 unsigned int trig_num)
821 struct usbduxsigma_private *devpriv = dev->private;
822 struct comedi_cmd *cmd = &s->async->cmd;
825 if (trig_num != cmd->start_arg)
829 if (!devpriv->ao_cmd_running) {
830 devpriv->ao_cmd_running = 1;
831 ret = usbduxsigma_submit_urbs(dev, devpriv->ao_urbs,
832 devpriv->n_ao_urbs, 0);
834 devpriv->ao_cmd_running = 0;
838 s->async->inttrig = NULL;
845 static int usbduxsigma_ao_cmdtest(struct comedi_device *dev,
846 struct comedi_subdevice *s,
847 struct comedi_cmd *cmd)
849 struct usbduxsigma_private *devpriv = dev->private;
854 /* high speed conversions are not used yet */
855 high_speed = 0; /* (devpriv->high_speed) */
857 /* Step 1 : check if triggers are trivially valid */
859 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
863 * start immediately a new scan
864 * the sampling rate is set by the coversion rate
868 /* start a new scan (output at once) with a timer */
871 err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
873 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
874 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
875 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
882 /* Step 2a : make sure trigger sources are unique */
884 err |= cfc_check_trigger_is_unique(cmd->start_src);
885 err |= cfc_check_trigger_is_unique(cmd->stop_src);
887 /* Step 2b : and mutually compatible */
892 /* Step 3: check if arguments are trivially valid */
894 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
896 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
897 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
899 if (cmd->scan_begin_src == TRIG_TIMER)
900 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
903 /* not used now, is for later use */
904 if (cmd->convert_src == TRIG_TIMER)
905 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
907 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
909 if (cmd->stop_src == TRIG_COUNT)
910 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
912 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
917 /* Step 4: fix up any arguments */
919 /* we count in timer steps */
921 /* timing of the conversion itself: every 125 us */
922 devpriv->ao_timer = cmd->convert_arg / 125000;
925 * timing of the scan: every 1ms
926 * we get all channels at once
928 devpriv->ao_timer = cmd->scan_begin_arg / 1000000;
930 if (devpriv->ao_timer < 1)
939 static int usbduxsigma_ao_cmd(struct comedi_device *dev,
940 struct comedi_subdevice *s)
942 struct usbduxsigma_private *devpriv = dev->private;
943 struct comedi_cmd *cmd = &s->async->cmd;
948 devpriv->ao_counter = devpriv->ao_timer;
950 if (cmd->start_src == TRIG_NOW) {
951 /* enable this acquisition operation */
952 devpriv->ao_cmd_running = 1;
953 ret = usbduxsigma_submit_urbs(dev, devpriv->ao_urbs,
954 devpriv->n_ao_urbs, 0);
956 devpriv->ao_cmd_running = 0;
960 s->async->inttrig = NULL;
961 } else { /* TRIG_INT */
962 s->async->inttrig = usbduxsigma_ao_inttrig;
970 static int usbduxsigma_dio_insn_config(struct comedi_device *dev,
971 struct comedi_subdevice *s,
972 struct comedi_insn *insn,
977 ret = comedi_dio_insn_config(dev, s, insn, data, 0);
982 * We don't tell the firmware here as it would take 8 frames
983 * to submit the information. We do it in the (*insn_bits).
988 static int usbduxsigma_dio_insn_bits(struct comedi_device *dev,
989 struct comedi_subdevice *s,
990 struct comedi_insn *insn,
993 struct usbduxsigma_private *devpriv = dev->private;
998 comedi_dio_update_state(s, data);
1000 /* Always update the hardware. See the (*insn_config). */
1001 devpriv->dux_commands[1] = s->io_bits & 0xff;
1002 devpriv->dux_commands[4] = s->state & 0xff;
1003 devpriv->dux_commands[2] = (s->io_bits >> 8) & 0xff;
1004 devpriv->dux_commands[5] = (s->state >> 8) & 0xff;
1005 devpriv->dux_commands[3] = (s->io_bits >> 16) & 0xff;
1006 devpriv->dux_commands[6] = (s->state >> 16) & 0xff;
1008 ret = usbbuxsigma_send_cmd(dev, USBDUXSIGMA_DIO_BITS_CMD);
1011 ret = usbduxsigma_receive_cmd(dev, USBDUXSIGMA_DIO_BITS_CMD);
1015 s->state = devpriv->insn_buf[1] |
1016 (devpriv->insn_buf[2] << 8) |
1017 (devpriv->insn_buf[3] << 16);
1028 static void usbduxsigma_pwm_stop(struct comedi_device *dev, int do_unlink)
1030 struct usbduxsigma_private *devpriv = dev->private;
1033 if (devpriv->pwm_urb)
1034 usb_kill_urb(devpriv->pwm_urb);
1037 devpriv->pwm_cmd_running = 0;
1040 static int usbduxsigma_pwm_cancel(struct comedi_device *dev,
1041 struct comedi_subdevice *s)
1043 struct usbduxsigma_private *devpriv = dev->private;
1045 /* unlink only if it is really running */
1046 usbduxsigma_pwm_stop(dev, devpriv->pwm_cmd_running);
1048 return usbbuxsigma_send_cmd(dev, USBDUXSIGMA_PWM_OFF_CMD);
1051 static void usbduxsigma_pwm_urb_complete(struct urb *urb)
1053 struct comedi_device *dev = urb->context;
1054 struct usbduxsigma_private *devpriv = dev->private;
1057 switch (urb->status) {
1066 /* happens after an unlink command */
1067 if (devpriv->pwm_cmd_running)
1068 usbduxsigma_pwm_stop(dev, 0); /* w/o unlink */
1073 if (devpriv->pwm_cmd_running) {
1074 dev_err(dev->class_dev,
1075 "%s: non-zero urb status (%d)\n",
1076 __func__, urb->status);
1077 usbduxsigma_pwm_stop(dev, 0); /* w/o unlink */
1082 if (!devpriv->pwm_cmd_running)
1085 urb->transfer_buffer_length = devpriv->pwm_buf_sz;
1086 urb->dev = comedi_to_usb_dev(dev);
1088 ret = usb_submit_urb(urb, GFP_ATOMIC);
1090 dev_err(dev->class_dev, "%s: urb resubmit failed (%d)\n",
1092 if (ret == -EL2NSYNC)
1093 dev_err(dev->class_dev,
1094 "buggy USB host controller or bug in IRQ handler\n");
1095 usbduxsigma_pwm_stop(dev, 0); /* w/o unlink */
1099 static int usbduxsigma_submit_pwm_urb(struct comedi_device *dev)
1101 struct usb_device *usb = comedi_to_usb_dev(dev);
1102 struct usbduxsigma_private *devpriv = dev->private;
1103 struct urb *urb = devpriv->pwm_urb;
1105 /* in case of a resubmission after an unlink... */
1106 usb_fill_bulk_urb(urb, usb, usb_sndbulkpipe(usb, 4),
1107 urb->transfer_buffer, devpriv->pwm_buf_sz,
1108 usbduxsigma_pwm_urb_complete, dev);
1110 return usb_submit_urb(urb, GFP_ATOMIC);
1113 static int usbduxsigma_pwm_period(struct comedi_device *dev,
1114 struct comedi_subdevice *s,
1115 unsigned int period)
1117 struct usbduxsigma_private *devpriv = dev->private;
1120 if (period < MIN_PWM_PERIOD)
1123 fx2delay = (period / (6 * 512 * 1000 / 33)) - 6;
1127 devpriv->pwm_delay = fx2delay;
1128 devpriv->pwm_period = period;
1132 static int usbduxsigma_pwm_start(struct comedi_device *dev,
1133 struct comedi_subdevice *s)
1135 struct usbduxsigma_private *devpriv = dev->private;
1138 if (devpriv->pwm_cmd_running)
1141 devpriv->dux_commands[1] = devpriv->pwm_delay;
1142 ret = usbbuxsigma_send_cmd(dev, USBDUXSIGMA_PWM_ON_CMD);
1146 memset(devpriv->pwm_urb->transfer_buffer, 0, devpriv->pwm_buf_sz);
1148 devpriv->pwm_cmd_running = 1;
1149 ret = usbduxsigma_submit_pwm_urb(dev);
1151 devpriv->pwm_cmd_running = 0;
1158 static void usbduxsigma_pwm_pattern(struct comedi_device *dev,
1159 struct comedi_subdevice *s,
1164 struct usbduxsigma_private *devpriv = dev->private;
1165 char pwm_mask = (1 << chan); /* DIO bit for the PWM data */
1166 char sgn_mask = (16 << chan); /* DIO bit for the sign */
1167 char *buf = (char *)(devpriv->pwm_urb->transfer_buffer);
1168 int szbuf = devpriv->pwm_buf_sz;
1171 for (i = 0; i < szbuf; i++) {
1185 static int usbduxsigma_pwm_write(struct comedi_device *dev,
1186 struct comedi_subdevice *s,
1187 struct comedi_insn *insn,
1190 unsigned int chan = CR_CHAN(insn->chanspec);
1193 * It doesn't make sense to support more than one value here
1194 * because it would just overwrite the PWM buffer.
1200 * The sign is set via a special INSN only, this gives us 8 bits
1201 * for normal operation, sign is 0 by default.
1203 usbduxsigma_pwm_pattern(dev, s, chan, data[0], 0);
1208 static int usbduxsigma_pwm_config(struct comedi_device *dev,
1209 struct comedi_subdevice *s,
1210 struct comedi_insn *insn,
1213 struct usbduxsigma_private *devpriv = dev->private;
1214 unsigned int chan = CR_CHAN(insn->chanspec);
1217 case INSN_CONFIG_ARM:
1219 * if not zero the PWM is limited to a certain time which is
1220 * not supported here
1224 return usbduxsigma_pwm_start(dev, s);
1225 case INSN_CONFIG_DISARM:
1226 return usbduxsigma_pwm_cancel(dev, s);
1227 case INSN_CONFIG_GET_PWM_STATUS:
1228 data[1] = devpriv->pwm_cmd_running;
1230 case INSN_CONFIG_PWM_SET_PERIOD:
1231 return usbduxsigma_pwm_period(dev, s, data[1]);
1232 case INSN_CONFIG_PWM_GET_PERIOD:
1233 data[1] = devpriv->pwm_period;
1235 case INSN_CONFIG_PWM_SET_H_BRIDGE:
1238 * data[2] = sign (for a relay)
1240 usbduxsigma_pwm_pattern(dev, s, chan, data[1], (data[2] != 0));
1242 case INSN_CONFIG_PWM_GET_H_BRIDGE:
1243 /* values are not kept in this driver, nothing to return */
1249 static int usbduxsigma_getstatusinfo(struct comedi_device *dev, int chan)
1251 struct usbduxsigma_private *devpriv = dev->private;
1259 sysred = 0; /* ADC zero */
1262 sysred = 1; /* ADC offset */
1265 sysred = 4; /* VCC */
1268 sysred = 8; /* temperature */
1271 sysred = 16; /* gain */
1274 sysred = 32; /* ref */
1278 devpriv->dux_commands[1] = 0x12; /* CONFIG0 */
1279 devpriv->dux_commands[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
1280 devpriv->dux_commands[3] = 0x00; /* CONFIG3: diff. channels off */
1281 devpriv->dux_commands[4] = 0;
1282 devpriv->dux_commands[5] = 0;
1283 devpriv->dux_commands[6] = sysred;
1284 ret = usbbuxsigma_send_cmd(dev, USBDUXSIGMA_SINGLE_AD_CMD);
1288 ret = usbduxsigma_receive_cmd(dev, USBDUXSIGMA_SINGLE_AD_CMD);
1292 /* 32 bits big endian from the A/D converter */
1293 val = be32_to_cpu(get_unaligned((__be32 *)(devpriv->insn_buf + 1)));
1294 val &= 0x00ffffff; /* strip status byte */
1295 val ^= 0x00800000; /* convert to unsigned */
1300 static int usbduxsigma_firmware_upload(struct comedi_device *dev,
1301 const u8 *data, size_t size,
1302 unsigned long context)
1304 struct usb_device *usb = comedi_to_usb_dev(dev);
1312 if (size > FIRMWARE_MAX_LEN) {
1313 dev_err(dev->class_dev, "firmware binary too large for FX2\n");
1317 /* we generate a local buffer for the firmware */
1318 buf = kmemdup(data, size, GFP_KERNEL);
1322 /* we need a malloc'ed buffer for usb_control_msg() */
1323 tmp = kmalloc(1, GFP_KERNEL);
1329 /* stop the current firmware on the device */
1330 *tmp = 1; /* 7f92 to one */
1331 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1334 USBDUXSUB_CPUCS, 0x0000,
1338 dev_err(dev->class_dev, "can not stop firmware\n");
1342 /* upload the new firmware to the device */
1343 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1350 dev_err(dev->class_dev, "firmware upload failed\n");
1354 /* start the new firmware on the device */
1355 *tmp = 0; /* 7f92 to zero */
1356 ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1359 USBDUXSUB_CPUCS, 0x0000,
1363 dev_err(dev->class_dev, "can not start firmware\n");
1371 static int usbduxsigma_alloc_usb_buffers(struct comedi_device *dev)
1373 struct usb_device *usb = comedi_to_usb_dev(dev);
1374 struct usbduxsigma_private *devpriv = dev->private;
1378 devpriv->dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
1379 devpriv->in_buf = kzalloc(SIZEINBUF, GFP_KERNEL);
1380 devpriv->insn_buf = kzalloc(SIZEINSNBUF, GFP_KERNEL);
1381 devpriv->ai_urbs = kcalloc(devpriv->n_ai_urbs, sizeof(urb), GFP_KERNEL);
1382 devpriv->ao_urbs = kcalloc(devpriv->n_ao_urbs, sizeof(urb), GFP_KERNEL);
1383 if (!devpriv->dux_commands || !devpriv->in_buf || !devpriv->insn_buf ||
1384 !devpriv->ai_urbs || !devpriv->ao_urbs)
1387 for (i = 0; i < devpriv->n_ai_urbs; i++) {
1388 /* one frame: 1ms */
1389 urb = usb_alloc_urb(1, GFP_KERNEL);
1392 devpriv->ai_urbs[i] = urb;
1394 /* will be filled later with a pointer to the comedi-device */
1395 /* and ONLY then the urb should be submitted */
1396 urb->context = NULL;
1397 urb->pipe = usb_rcvisocpipe(usb, 6);
1398 urb->transfer_flags = URB_ISO_ASAP;
1399 urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1400 if (!urb->transfer_buffer)
1402 urb->complete = usbduxsigma_ai_urb_complete;
1403 urb->number_of_packets = 1;
1404 urb->transfer_buffer_length = SIZEINBUF;
1405 urb->iso_frame_desc[0].offset = 0;
1406 urb->iso_frame_desc[0].length = SIZEINBUF;
1409 for (i = 0; i < devpriv->n_ao_urbs; i++) {
1410 /* one frame: 1ms */
1411 urb = usb_alloc_urb(1, GFP_KERNEL);
1414 devpriv->ao_urbs[i] = urb;
1416 /* will be filled later with a pointer to the comedi-device */
1417 /* and ONLY then the urb should be submitted */
1418 urb->context = NULL;
1419 urb->pipe = usb_sndisocpipe(usb, 2);
1420 urb->transfer_flags = URB_ISO_ASAP;
1421 urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1422 if (!urb->transfer_buffer)
1424 urb->complete = usbduxsigma_ao_urb_complete;
1425 urb->number_of_packets = 1;
1426 urb->transfer_buffer_length = SIZEOUTBUF;
1427 urb->iso_frame_desc[0].offset = 0;
1428 urb->iso_frame_desc[0].length = SIZEOUTBUF;
1429 if (devpriv->high_speed)
1430 urb->interval = 8; /* uframes */
1432 urb->interval = 1; /* frames */
1435 if (devpriv->pwm_buf_sz) {
1436 urb = usb_alloc_urb(0, GFP_KERNEL);
1439 devpriv->pwm_urb = urb;
1441 urb->transfer_buffer = kzalloc(devpriv->pwm_buf_sz,
1443 if (!urb->transfer_buffer)
1450 static void usbduxsigma_free_usb_buffers(struct comedi_device *dev)
1452 struct usbduxsigma_private *devpriv = dev->private;
1456 urb = devpriv->pwm_urb;
1458 kfree(urb->transfer_buffer);
1461 if (devpriv->ao_urbs) {
1462 for (i = 0; i < devpriv->n_ao_urbs; i++) {
1463 urb = devpriv->ao_urbs[i];
1465 kfree(urb->transfer_buffer);
1469 kfree(devpriv->ao_urbs);
1471 if (devpriv->ai_urbs) {
1472 for (i = 0; i < devpriv->n_ai_urbs; i++) {
1473 urb = devpriv->ai_urbs[i];
1475 kfree(urb->transfer_buffer);
1479 kfree(devpriv->ai_urbs);
1481 kfree(devpriv->insn_buf);
1482 kfree(devpriv->in_buf);
1483 kfree(devpriv->dux_commands);
1486 static int usbduxsigma_auto_attach(struct comedi_device *dev,
1487 unsigned long context_unused)
1489 struct usb_interface *intf = comedi_to_usb_interface(dev);
1490 struct usb_device *usb = comedi_to_usb_dev(dev);
1491 struct usbduxsigma_private *devpriv;
1492 struct comedi_subdevice *s;
1496 devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1500 sema_init(&devpriv->sem, 1);
1502 usb_set_intfdata(intf, devpriv);
1504 devpriv->high_speed = (usb->speed == USB_SPEED_HIGH);
1505 if (devpriv->high_speed) {
1506 devpriv->n_ai_urbs = NUMOFINBUFFERSHIGH;
1507 devpriv->n_ao_urbs = NUMOFOUTBUFFERSHIGH;
1508 devpriv->pwm_buf_sz = 512;
1510 devpriv->n_ai_urbs = NUMOFINBUFFERSFULL;
1511 devpriv->n_ao_urbs = NUMOFOUTBUFFERSFULL;
1514 ret = usbduxsigma_alloc_usb_buffers(dev);
1518 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
1519 ret = usb_set_interface(usb, intf->altsetting->desc.bInterfaceNumber,
1522 dev_err(dev->class_dev,
1523 "could not set alternate setting 3 in high speed\n");
1527 ret = comedi_load_firmware(dev, &usb->dev, FIRMWARE,
1528 usbduxsigma_firmware_upload, 0);
1532 ret = comedi_alloc_subdevices(dev, (devpriv->high_speed) ? 4 : 3);
1536 /* Analog Input subdevice */
1537 s = &dev->subdevices[0];
1538 dev->read_subdev = s;
1539 s->type = COMEDI_SUBD_AI;
1540 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ | SDF_LSAMPL;
1541 s->n_chan = NUMCHANNELS;
1542 s->len_chanlist = NUMCHANNELS;
1543 s->maxdata = 0x00ffffff;
1544 s->range_table = &usbduxsigma_ai_range;
1545 s->insn_read = usbduxsigma_ai_insn_read;
1546 s->do_cmdtest = usbduxsigma_ai_cmdtest;
1547 s->do_cmd = usbduxsigma_ai_cmd;
1548 s->cancel = usbduxsigma_ai_cancel;
1550 /* Analog Output subdevice */
1551 s = &dev->subdevices[1];
1552 dev->write_subdev = s;
1553 s->type = COMEDI_SUBD_AO;
1554 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1556 s->len_chanlist = s->n_chan;
1557 s->maxdata = 0x00ff;
1558 s->range_table = &range_unipolar2_5;
1559 s->insn_write = usbduxsigma_ao_insn_write;
1560 s->insn_read = usbduxsigma_ao_insn_read;
1561 s->do_cmdtest = usbduxsigma_ao_cmdtest;
1562 s->do_cmd = usbduxsigma_ao_cmd;
1563 s->cancel = usbduxsigma_ao_cancel;
1565 ret = comedi_alloc_subdev_readback(s);
1569 /* Digital I/O subdevice */
1570 s = &dev->subdevices[2];
1571 s->type = COMEDI_SUBD_DIO;
1572 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1575 s->range_table = &range_digital;
1576 s->insn_bits = usbduxsigma_dio_insn_bits;
1577 s->insn_config = usbduxsigma_dio_insn_config;
1579 if (devpriv->high_speed) {
1580 /* Timer / pwm subdevice */
1581 s = &dev->subdevices[3];
1582 s->type = COMEDI_SUBD_PWM;
1583 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
1585 s->maxdata = devpriv->pwm_buf_sz;
1586 s->insn_write = usbduxsigma_pwm_write;
1587 s->insn_config = usbduxsigma_pwm_config;
1589 usbduxsigma_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
1592 offset = usbduxsigma_getstatusinfo(dev, 0);
1594 dev_err(dev->class_dev,
1595 "Communication to USBDUXSIGMA failed! Check firmware and cabling.\n");
1599 dev_info(dev->class_dev, "ADC_zero = %x\n", offset);
1604 static void usbduxsigma_detach(struct comedi_device *dev)
1606 struct usb_interface *intf = comedi_to_usb_interface(dev);
1607 struct usbduxsigma_private *devpriv = dev->private;
1609 usb_set_intfdata(intf, NULL);
1614 down(&devpriv->sem);
1616 /* force unlink all urbs */
1617 usbduxsigma_ai_stop(dev, 1);
1618 usbduxsigma_ao_stop(dev, 1);
1619 usbduxsigma_pwm_stop(dev, 1);
1621 usbduxsigma_free_usb_buffers(dev);
1626 static struct comedi_driver usbduxsigma_driver = {
1627 .driver_name = "usbduxsigma",
1628 .module = THIS_MODULE,
1629 .auto_attach = usbduxsigma_auto_attach,
1630 .detach = usbduxsigma_detach,
1633 static int usbduxsigma_usb_probe(struct usb_interface *intf,
1634 const struct usb_device_id *id)
1636 return comedi_usb_auto_config(intf, &usbduxsigma_driver, 0);
1639 static const struct usb_device_id usbduxsigma_usb_table[] = {
1640 { USB_DEVICE(0x13d8, 0x0020) },
1641 { USB_DEVICE(0x13d8, 0x0021) },
1642 { USB_DEVICE(0x13d8, 0x0022) },
1645 MODULE_DEVICE_TABLE(usb, usbduxsigma_usb_table);
1647 static struct usb_driver usbduxsigma_usb_driver = {
1648 .name = "usbduxsigma",
1649 .probe = usbduxsigma_usb_probe,
1650 .disconnect = comedi_usb_auto_unconfig,
1651 .id_table = usbduxsigma_usb_table,
1653 module_comedi_usb_driver(usbduxsigma_driver, usbduxsigma_usb_driver);
1655 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1656 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com");
1657 MODULE_LICENSE("GPL");
1658 MODULE_FIRMWARE(FIRMWARE);