3 * Copyright (C) 2011-2015 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: 20 July 2015
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
42 * 0.7: rewrite code that urb->interval is always 1
45 #include <linux/kernel.h>
46 #include <linux/module.h>
47 #include <linux/slab.h>
48 #include <linux/input.h>
49 #include <linux/fcntl.h>
50 #include <linux/compiler.h>
51 #include <asm/unaligned.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(u32)))
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(u8) + 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 9
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
;
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;
202 mutex_lock(&devpriv
->mut
);
203 /* unlink only if it is really running */
204 usbduxsigma_ai_stop(dev
, devpriv
->ai_cmd_running
);
205 mutex_unlock(&devpriv
->mut
);
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 if ((urb
->actual_length
> 0) && (urb
->status
!= -EXDEV
)) {
222 devpriv
->ai_counter
--;
223 if (devpriv
->ai_counter
== 0) {
224 devpriv
->ai_counter
= devpriv
->ai_timer
;
227 * Get the data from the USB bus and hand it over
228 * to comedi. Note, first byte is the DIO state.
230 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
231 val
= be32_to_cpu(devpriv
->in_buf
[i
+ 1]);
232 val
&= 0x00ffffff; /* strip status byte */
233 val
= comedi_offset_munge(s
, val
);
234 if (!comedi_buf_write_samples(s
, &val
, 1))
238 if (cmd
->stop_src
== TRIG_COUNT
&&
239 async
->scans_done
>= cmd
->stop_arg
)
240 async
->events
|= COMEDI_CB_EOA
;
244 /* if command is still running, resubmit urb */
245 if (!(async
->events
& COMEDI_CB_CANCEL_MASK
)) {
246 urb
->dev
= comedi_to_usb_dev(dev
);
247 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
249 dev_err(dev
->class_dev
, "urb resubmit failed (%d)\n",
251 if (ret
== -EL2NSYNC
)
252 dev_err(dev
->class_dev
,
253 "buggy USB host controller or bug in IRQ handler\n");
254 async
->events
|= COMEDI_CB_ERROR
;
259 static void usbduxsigma_ai_urb_complete(struct urb
*urb
)
261 struct comedi_device
*dev
= urb
->context
;
262 struct usbduxsigma_private
*devpriv
= dev
->private;
263 struct comedi_subdevice
*s
= dev
->read_subdev
;
264 struct comedi_async
*async
= s
->async
;
266 /* exit if not running a command, do not resubmit urb */
267 if (!devpriv
->ai_cmd_running
)
270 switch (urb
->status
) {
272 /* copy the result in the transfer buffer */
273 memcpy(devpriv
->in_buf
, urb
->transfer_buffer
, SIZEINBUF
);
274 usbduxsigma_ai_handle_urb(dev
, s
, urb
);
279 * error in the ISOchronous data
280 * we don't copy the data into the transfer buffer
281 * and recycle the last data byte
283 dev_dbg(dev
->class_dev
, "CRC error in ISO IN stream\n");
284 usbduxsigma_ai_handle_urb(dev
, s
, urb
);
291 /* happens after an unlink command */
292 async
->events
|= COMEDI_CB_ERROR
;
297 dev_err(dev
->class_dev
, "non-zero urb status (%d)\n",
299 async
->events
|= COMEDI_CB_ERROR
;
304 * comedi_handle_events() cannot be used in this driver. The (*cancel)
305 * operation would unlink the urb.
307 if (async
->events
& COMEDI_CB_CANCEL_MASK
)
308 usbduxsigma_ai_stop(dev
, 0);
310 comedi_event(dev
, s
);
313 static void usbduxsigma_ao_stop(struct comedi_device
*dev
, int do_unlink
)
315 struct usbduxsigma_private
*devpriv
= dev
->private;
317 if (do_unlink
&& devpriv
->ao_urbs
)
318 usbduxsigma_unlink_urbs(devpriv
->ao_urbs
, devpriv
->n_ao_urbs
);
320 devpriv
->ao_cmd_running
= 0;
323 static int usbduxsigma_ao_cancel(struct comedi_device
*dev
,
324 struct comedi_subdevice
*s
)
326 struct usbduxsigma_private
*devpriv
= dev
->private;
328 mutex_lock(&devpriv
->mut
);
329 /* unlink only if it is really running */
330 usbduxsigma_ao_stop(dev
, devpriv
->ao_cmd_running
);
331 mutex_unlock(&devpriv
->mut
);
336 static void usbduxsigma_ao_handle_urb(struct comedi_device
*dev
,
337 struct comedi_subdevice
*s
,
340 struct usbduxsigma_private
*devpriv
= dev
->private;
341 struct comedi_async
*async
= s
->async
;
342 struct comedi_cmd
*cmd
= &async
->cmd
;
347 devpriv
->ao_counter
--;
348 if (devpriv
->ao_counter
== 0) {
349 devpriv
->ao_counter
= devpriv
->ao_timer
;
351 if (cmd
->stop_src
== TRIG_COUNT
&&
352 async
->scans_done
>= cmd
->stop_arg
) {
353 async
->events
|= COMEDI_CB_EOA
;
357 /* transmit data to the USB bus */
358 datap
= urb
->transfer_buffer
;
359 *datap
++ = cmd
->chanlist_len
;
360 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
361 unsigned int chan
= CR_CHAN(cmd
->chanlist
[i
]);
364 if (!comedi_buf_read_samples(s
, &val
, 1)) {
365 dev_err(dev
->class_dev
, "buffer underflow\n");
366 async
->events
|= COMEDI_CB_OVERFLOW
;
372 s
->readback
[chan
] = val
;
376 /* if command is still running, resubmit urb */
377 if (!(async
->events
& COMEDI_CB_CANCEL_MASK
)) {
378 urb
->transfer_buffer_length
= SIZEOUTBUF
;
379 urb
->dev
= comedi_to_usb_dev(dev
);
381 urb
->interval
= 1; /* (u)frames */
382 urb
->number_of_packets
= 1;
383 urb
->iso_frame_desc
[0].offset
= 0;
384 urb
->iso_frame_desc
[0].length
= SIZEOUTBUF
;
385 urb
->iso_frame_desc
[0].status
= 0;
386 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
388 dev_err(dev
->class_dev
, "urb resubmit failed (%d)\n",
390 if (ret
== -EL2NSYNC
)
391 dev_err(dev
->class_dev
,
392 "buggy USB host controller or bug in IRQ handler\n");
393 async
->events
|= COMEDI_CB_ERROR
;
398 static void usbduxsigma_ao_urb_complete(struct urb
*urb
)
400 struct comedi_device
*dev
= urb
->context
;
401 struct usbduxsigma_private
*devpriv
= dev
->private;
402 struct comedi_subdevice
*s
= dev
->write_subdev
;
403 struct comedi_async
*async
= s
->async
;
405 /* exit if not running a command, do not resubmit urb */
406 if (!devpriv
->ao_cmd_running
)
409 switch (urb
->status
) {
411 usbduxsigma_ao_handle_urb(dev
, s
, urb
);
418 /* happens after an unlink command */
419 async
->events
|= COMEDI_CB_ERROR
;
424 dev_err(dev
->class_dev
, "non-zero urb status (%d)\n",
426 async
->events
|= COMEDI_CB_ERROR
;
431 * comedi_handle_events() cannot be used in this driver. The (*cancel)
432 * operation would unlink the urb.
434 if (async
->events
& COMEDI_CB_CANCEL_MASK
)
435 usbduxsigma_ao_stop(dev
, 0);
437 comedi_event(dev
, s
);
440 static int usbduxsigma_submit_urbs(struct comedi_device
*dev
,
441 struct urb
**urbs
, int num_urbs
,
444 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
449 /* Submit all URBs and start the transfer on the bus */
450 for (i
= 0; i
< num_urbs
; i
++) {
453 /* in case of a resubmission after an unlink... */
459 urb
->transfer_flags
= URB_ISO_ASAP
;
461 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
468 static int usbduxsigma_chans_to_interval(int num_chan
)
477 static int usbduxsigma_ai_cmdtest(struct comedi_device
*dev
,
478 struct comedi_subdevice
*s
,
479 struct comedi_cmd
*cmd
)
481 struct usbduxsigma_private
*devpriv
= dev
->private;
482 int high_speed
= devpriv
->high_speed
;
483 int interval
= usbduxsigma_chans_to_interval(cmd
->chanlist_len
);
487 /* Step 1 : check if triggers are trivially valid */
489 err
|= comedi_check_trigger_src(&cmd
->start_src
, TRIG_NOW
| TRIG_INT
);
490 err
|= comedi_check_trigger_src(&cmd
->scan_begin_src
, TRIG_TIMER
);
491 err
|= comedi_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
492 err
|= comedi_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
493 err
|= comedi_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
498 /* Step 2a : make sure trigger sources are unique */
500 err
|= comedi_check_trigger_is_unique(cmd
->start_src
);
501 err
|= comedi_check_trigger_is_unique(cmd
->stop_src
);
503 /* Step 2b : and mutually compatible */
508 /* Step 3: check if arguments are trivially valid */
510 err
|= comedi_check_trigger_arg_is(&cmd
->start_arg
, 0);
514 * In high speed mode microframes are possible.
515 * However, during one microframe we can roughly
516 * sample two channels. Thus, the more channels
517 * are in the channel list the more time we need.
519 err
|= comedi_check_trigger_arg_min(&cmd
->scan_begin_arg
,
520 (125000 * interval
));
523 /* 1kHz scans every USB frame */
524 err
|= comedi_check_trigger_arg_min(&cmd
->scan_begin_arg
,
528 err
|= comedi_check_trigger_arg_is(&cmd
->scan_end_arg
,
531 if (cmd
->stop_src
== TRIG_COUNT
)
532 err
|= comedi_check_trigger_arg_min(&cmd
->stop_arg
, 1);
534 err
|= comedi_check_trigger_arg_is(&cmd
->stop_arg
, 0);
539 /* Step 4: fix up any arguments */
541 tmp
= rounddown(cmd
->scan_begin_arg
, high_speed
? 125000 : 1000000);
542 err
|= comedi_check_trigger_arg_is(&cmd
->scan_begin_arg
, tmp
);
551 * creates the ADC command for the MAX1271
552 * range is the range value from comedi
554 static void create_adc_command(unsigned int chan
,
555 u8
*muxsg0
, u8
*muxsg1
)
558 (*muxsg0
) = (*muxsg0
) | (1 << chan
);
560 (*muxsg1
) = (*muxsg1
) | (1 << (chan
- 8));
563 static int usbbuxsigma_send_cmd(struct comedi_device
*dev
, int cmd_type
)
565 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
566 struct usbduxsigma_private
*devpriv
= dev
->private;
569 devpriv
->dux_commands
[0] = cmd_type
;
571 return usb_bulk_msg(usb
, usb_sndbulkpipe(usb
, 1),
572 devpriv
->dux_commands
, SIZEOFDUXBUFFER
,
573 &nsent
, BULK_TIMEOUT
);
576 static int usbduxsigma_receive_cmd(struct comedi_device
*dev
, int command
)
578 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
579 struct usbduxsigma_private
*devpriv
= dev
->private;
584 for (i
= 0; i
< RETRIES
; i
++) {
585 ret
= usb_bulk_msg(usb
, usb_rcvbulkpipe(usb
, 8),
586 devpriv
->insn_buf
, SIZEINSNBUF
,
587 &nrec
, BULK_TIMEOUT
);
591 if (devpriv
->insn_buf
[0] == command
)
595 * This is only reached if the data has been requested a
596 * couple of times and the command was not received.
601 static int usbduxsigma_ai_inttrig(struct comedi_device
*dev
,
602 struct comedi_subdevice
*s
,
603 unsigned int trig_num
)
605 struct usbduxsigma_private
*devpriv
= dev
->private;
606 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
609 if (trig_num
!= cmd
->start_arg
)
612 mutex_lock(&devpriv
->mut
);
613 if (!devpriv
->ai_cmd_running
) {
614 devpriv
->ai_cmd_running
= 1;
615 ret
= usbduxsigma_submit_urbs(dev
, devpriv
->ai_urbs
,
616 devpriv
->n_ai_urbs
, 1);
618 devpriv
->ai_cmd_running
= 0;
619 mutex_unlock(&devpriv
->mut
);
622 s
->async
->inttrig
= NULL
;
624 mutex_unlock(&devpriv
->mut
);
629 static int usbduxsigma_ai_cmd(struct comedi_device
*dev
,
630 struct comedi_subdevice
*s
)
632 struct usbduxsigma_private
*devpriv
= dev
->private;
633 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
634 unsigned int len
= cmd
->chanlist_len
;
641 mutex_lock(&devpriv
->mut
);
643 if (devpriv
->high_speed
) {
645 * every 2 channels get a time window of 125us. Thus, if we
646 * sample all 16 channels we need 1ms. If we sample only one
647 * channel we need only 125us
649 unsigned int interval
= usbduxsigma_chans_to_interval(len
);
651 devpriv
->ai_interval
= interval
;
652 devpriv
->ai_timer
= cmd
->scan_begin_arg
/ (125000 * interval
);
654 /* interval always 1ms */
655 devpriv
->ai_interval
= 1;
656 devpriv
->ai_timer
= cmd
->scan_begin_arg
/ 1000000;
659 for (i
= 0; i
< len
; i
++) {
660 unsigned int chan
= CR_CHAN(cmd
->chanlist
[i
]);
662 create_adc_command(chan
, &muxsg0
, &muxsg1
);
665 devpriv
->dux_commands
[1] = devpriv
->ai_interval
;
666 devpriv
->dux_commands
[2] = len
; /* num channels per time step */
667 devpriv
->dux_commands
[3] = 0x12; /* CONFIG0 */
668 devpriv
->dux_commands
[4] = 0x03; /* CONFIG1: 23kHz sample, delay 0us */
669 devpriv
->dux_commands
[5] = 0x00; /* CONFIG3: diff. channels off */
670 devpriv
->dux_commands
[6] = muxsg0
;
671 devpriv
->dux_commands
[7] = muxsg1
;
672 devpriv
->dux_commands
[8] = sysred
;
674 ret
= usbbuxsigma_send_cmd(dev
, USBBUXSIGMA_AD_CMD
);
676 mutex_unlock(&devpriv
->mut
);
680 devpriv
->ai_counter
= devpriv
->ai_timer
;
682 if (cmd
->start_src
== TRIG_NOW
) {
683 /* enable this acquisition operation */
684 devpriv
->ai_cmd_running
= 1;
685 ret
= usbduxsigma_submit_urbs(dev
, devpriv
->ai_urbs
,
686 devpriv
->n_ai_urbs
, 1);
688 devpriv
->ai_cmd_running
= 0;
689 mutex_unlock(&devpriv
->mut
);
692 s
->async
->inttrig
= NULL
;
693 } else { /* TRIG_INT */
694 s
->async
->inttrig
= usbduxsigma_ai_inttrig
;
697 mutex_unlock(&devpriv
->mut
);
702 static int usbduxsigma_ai_insn_read(struct comedi_device
*dev
,
703 struct comedi_subdevice
*s
,
704 struct comedi_insn
*insn
,
707 struct usbduxsigma_private
*devpriv
= dev
->private;
708 unsigned int chan
= CR_CHAN(insn
->chanspec
);
715 mutex_lock(&devpriv
->mut
);
716 if (devpriv
->ai_cmd_running
) {
717 mutex_unlock(&devpriv
->mut
);
721 create_adc_command(chan
, &muxsg0
, &muxsg1
);
723 /* Mode 0 is used to get a single conversion on demand */
724 devpriv
->dux_commands
[1] = 0x16; /* CONFIG0: chopper on */
725 devpriv
->dux_commands
[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
726 devpriv
->dux_commands
[3] = 0x00; /* CONFIG3: diff. channels off */
727 devpriv
->dux_commands
[4] = muxsg0
;
728 devpriv
->dux_commands
[5] = muxsg1
;
729 devpriv
->dux_commands
[6] = sysred
;
732 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_SINGLE_AD_CMD
);
734 mutex_unlock(&devpriv
->mut
);
738 for (i
= 0; i
< insn
->n
; i
++) {
741 ret
= usbduxsigma_receive_cmd(dev
, USBDUXSIGMA_SINGLE_AD_CMD
);
743 mutex_unlock(&devpriv
->mut
);
747 /* 32 bits big endian from the A/D converter */
748 val
= be32_to_cpu(get_unaligned((__be32
749 *)(devpriv
->insn_buf
+ 1)));
750 val
&= 0x00ffffff; /* strip status byte */
751 data
[i
] = comedi_offset_munge(s
, val
);
753 mutex_unlock(&devpriv
->mut
);
758 static int usbduxsigma_ao_insn_read(struct comedi_device
*dev
,
759 struct comedi_subdevice
*s
,
760 struct comedi_insn
*insn
,
763 struct usbduxsigma_private
*devpriv
= dev
->private;
766 mutex_lock(&devpriv
->mut
);
767 ret
= comedi_readback_insn_read(dev
, s
, insn
, data
);
768 mutex_unlock(&devpriv
->mut
);
773 static int usbduxsigma_ao_insn_write(struct comedi_device
*dev
,
774 struct comedi_subdevice
*s
,
775 struct comedi_insn
*insn
,
778 struct usbduxsigma_private
*devpriv
= dev
->private;
779 unsigned int chan
= CR_CHAN(insn
->chanspec
);
783 mutex_lock(&devpriv
->mut
);
784 if (devpriv
->ao_cmd_running
) {
785 mutex_unlock(&devpriv
->mut
);
789 for (i
= 0; i
< insn
->n
; i
++) {
790 devpriv
->dux_commands
[1] = 1; /* num channels */
791 devpriv
->dux_commands
[2] = data
[i
]; /* value */
792 devpriv
->dux_commands
[3] = chan
; /* channel number */
793 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_DA_CMD
);
795 mutex_unlock(&devpriv
->mut
);
798 s
->readback
[chan
] = data
[i
];
800 mutex_unlock(&devpriv
->mut
);
805 static int usbduxsigma_ao_inttrig(struct comedi_device
*dev
,
806 struct comedi_subdevice
*s
,
807 unsigned int trig_num
)
809 struct usbduxsigma_private
*devpriv
= dev
->private;
810 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
813 if (trig_num
!= cmd
->start_arg
)
816 mutex_lock(&devpriv
->mut
);
817 if (!devpriv
->ao_cmd_running
) {
818 devpriv
->ao_cmd_running
= 1;
819 ret
= usbduxsigma_submit_urbs(dev
, devpriv
->ao_urbs
,
820 devpriv
->n_ao_urbs
, 0);
822 devpriv
->ao_cmd_running
= 0;
823 mutex_unlock(&devpriv
->mut
);
826 s
->async
->inttrig
= NULL
;
828 mutex_unlock(&devpriv
->mut
);
833 static int usbduxsigma_ao_cmdtest(struct comedi_device
*dev
,
834 struct comedi_subdevice
*s
,
835 struct comedi_cmd
*cmd
)
837 struct usbduxsigma_private
*devpriv
= dev
->private;
841 /* Step 1 : check if triggers are trivially valid */
843 err
|= comedi_check_trigger_src(&cmd
->start_src
, TRIG_NOW
| TRIG_INT
);
846 * For now, always use "scan" timing with all channels updated at once
847 * (cmd->scan_begin_src == TRIG_TIMER, cmd->convert_src == TRIG_NOW).
849 * In a future version, "convert" timing with channels updated
850 * indivually may be supported in high speed mode
851 * (cmd->scan_begin_src == TRIG_FOLLOW, cmd->convert_src == TRIG_TIMER).
853 err
|= comedi_check_trigger_src(&cmd
->scan_begin_src
, TRIG_TIMER
);
854 err
|= comedi_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
855 err
|= comedi_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
856 err
|= comedi_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
859 mutex_unlock(&devpriv
->mut
);
863 /* Step 2a : make sure trigger sources are unique */
865 err
|= comedi_check_trigger_is_unique(cmd
->start_src
);
866 err
|= comedi_check_trigger_is_unique(cmd
->stop_src
);
868 /* Step 2b : and mutually compatible */
873 /* Step 3: check if arguments are trivially valid */
875 err
|= comedi_check_trigger_arg_is(&cmd
->start_arg
, 0);
877 err
|= comedi_check_trigger_arg_min(&cmd
->scan_begin_arg
, 1000000);
879 err
|= comedi_check_trigger_arg_is(&cmd
->scan_end_arg
,
882 if (cmd
->stop_src
== TRIG_COUNT
)
883 err
|= comedi_check_trigger_arg_min(&cmd
->stop_arg
, 1);
885 err
|= comedi_check_trigger_arg_is(&cmd
->stop_arg
, 0);
890 /* Step 4: fix up any arguments */
892 tmp
= rounddown(cmd
->scan_begin_arg
, 1000000);
893 err
|= comedi_check_trigger_arg_is(&cmd
->scan_begin_arg
, tmp
);
901 static int usbduxsigma_ao_cmd(struct comedi_device
*dev
,
902 struct comedi_subdevice
*s
)
904 struct usbduxsigma_private
*devpriv
= dev
->private;
905 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
908 mutex_lock(&devpriv
->mut
);
911 * For now, only "scan" timing is supported. A future version may
912 * support "convert" timing in high speed mode.
914 * Timing of the scan: every 1ms all channels updated at once.
916 devpriv
->ao_timer
= cmd
->scan_begin_arg
/ 1000000;
918 devpriv
->ao_counter
= devpriv
->ao_timer
;
920 if (cmd
->start_src
== TRIG_NOW
) {
921 /* enable this acquisition operation */
922 devpriv
->ao_cmd_running
= 1;
923 ret
= usbduxsigma_submit_urbs(dev
, devpriv
->ao_urbs
,
924 devpriv
->n_ao_urbs
, 0);
926 devpriv
->ao_cmd_running
= 0;
927 mutex_unlock(&devpriv
->mut
);
930 s
->async
->inttrig
= NULL
;
931 } else { /* TRIG_INT */
932 s
->async
->inttrig
= usbduxsigma_ao_inttrig
;
935 mutex_unlock(&devpriv
->mut
);
940 static int usbduxsigma_dio_insn_config(struct comedi_device
*dev
,
941 struct comedi_subdevice
*s
,
942 struct comedi_insn
*insn
,
947 ret
= comedi_dio_insn_config(dev
, s
, insn
, data
, 0);
952 * We don't tell the firmware here as it would take 8 frames
953 * to submit the information. We do it in the (*insn_bits).
958 static int usbduxsigma_dio_insn_bits(struct comedi_device
*dev
,
959 struct comedi_subdevice
*s
,
960 struct comedi_insn
*insn
,
963 struct usbduxsigma_private
*devpriv
= dev
->private;
966 mutex_lock(&devpriv
->mut
);
968 comedi_dio_update_state(s
, data
);
970 /* Always update the hardware. See the (*insn_config). */
971 devpriv
->dux_commands
[1] = s
->io_bits
& 0xff;
972 devpriv
->dux_commands
[4] = s
->state
& 0xff;
973 devpriv
->dux_commands
[2] = (s
->io_bits
>> 8) & 0xff;
974 devpriv
->dux_commands
[5] = (s
->state
>> 8) & 0xff;
975 devpriv
->dux_commands
[3] = (s
->io_bits
>> 16) & 0xff;
976 devpriv
->dux_commands
[6] = (s
->state
>> 16) & 0xff;
978 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_DIO_BITS_CMD
);
981 ret
= usbduxsigma_receive_cmd(dev
, USBDUXSIGMA_DIO_BITS_CMD
);
985 s
->state
= devpriv
->insn_buf
[1] |
986 (devpriv
->insn_buf
[2] << 8) |
987 (devpriv
->insn_buf
[3] << 16);
993 mutex_unlock(&devpriv
->mut
);
998 static void usbduxsigma_pwm_stop(struct comedi_device
*dev
, int do_unlink
)
1000 struct usbduxsigma_private
*devpriv
= dev
->private;
1003 if (devpriv
->pwm_urb
)
1004 usb_kill_urb(devpriv
->pwm_urb
);
1007 devpriv
->pwm_cmd_running
= 0;
1010 static int usbduxsigma_pwm_cancel(struct comedi_device
*dev
,
1011 struct comedi_subdevice
*s
)
1013 struct usbduxsigma_private
*devpriv
= dev
->private;
1015 /* unlink only if it is really running */
1016 usbduxsigma_pwm_stop(dev
, devpriv
->pwm_cmd_running
);
1018 return usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_PWM_OFF_CMD
);
1021 static void usbduxsigma_pwm_urb_complete(struct urb
*urb
)
1023 struct comedi_device
*dev
= urb
->context
;
1024 struct usbduxsigma_private
*devpriv
= dev
->private;
1027 switch (urb
->status
) {
1036 /* happens after an unlink command */
1037 if (devpriv
->pwm_cmd_running
)
1038 usbduxsigma_pwm_stop(dev
, 0); /* w/o unlink */
1043 if (devpriv
->pwm_cmd_running
) {
1044 dev_err(dev
->class_dev
, "non-zero urb status (%d)\n",
1046 usbduxsigma_pwm_stop(dev
, 0); /* w/o unlink */
1051 if (!devpriv
->pwm_cmd_running
)
1054 urb
->transfer_buffer_length
= devpriv
->pwm_buf_sz
;
1055 urb
->dev
= comedi_to_usb_dev(dev
);
1057 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
1059 dev_err(dev
->class_dev
, "urb resubmit failed (%d)\n", ret
);
1060 if (ret
== -EL2NSYNC
)
1061 dev_err(dev
->class_dev
,
1062 "buggy USB host controller or bug in IRQ handler\n");
1063 usbduxsigma_pwm_stop(dev
, 0); /* w/o unlink */
1067 static int usbduxsigma_submit_pwm_urb(struct comedi_device
*dev
)
1069 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
1070 struct usbduxsigma_private
*devpriv
= dev
->private;
1071 struct urb
*urb
= devpriv
->pwm_urb
;
1073 /* in case of a resubmission after an unlink... */
1074 usb_fill_bulk_urb(urb
, usb
, usb_sndbulkpipe(usb
, 4),
1075 urb
->transfer_buffer
, devpriv
->pwm_buf_sz
,
1076 usbduxsigma_pwm_urb_complete
, dev
);
1078 return usb_submit_urb(urb
, GFP_ATOMIC
);
1081 static int usbduxsigma_pwm_period(struct comedi_device
*dev
,
1082 struct comedi_subdevice
*s
,
1083 unsigned int period
)
1085 struct usbduxsigma_private
*devpriv
= dev
->private;
1088 if (period
< MIN_PWM_PERIOD
)
1091 fx2delay
= (period
/ (6 * 512 * 1000 / 33)) - 6;
1095 devpriv
->pwm_delay
= fx2delay
;
1096 devpriv
->pwm_period
= period
;
1100 static int usbduxsigma_pwm_start(struct comedi_device
*dev
,
1101 struct comedi_subdevice
*s
)
1103 struct usbduxsigma_private
*devpriv
= dev
->private;
1106 if (devpriv
->pwm_cmd_running
)
1109 devpriv
->dux_commands
[1] = devpriv
->pwm_delay
;
1110 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_PWM_ON_CMD
);
1114 memset(devpriv
->pwm_urb
->transfer_buffer
, 0, devpriv
->pwm_buf_sz
);
1116 devpriv
->pwm_cmd_running
= 1;
1117 ret
= usbduxsigma_submit_pwm_urb(dev
);
1119 devpriv
->pwm_cmd_running
= 0;
1126 static void usbduxsigma_pwm_pattern(struct comedi_device
*dev
,
1127 struct comedi_subdevice
*s
,
1132 struct usbduxsigma_private
*devpriv
= dev
->private;
1133 char pwm_mask
= (1 << chan
); /* DIO bit for the PWM data */
1134 char sgn_mask
= (16 << chan
); /* DIO bit for the sign */
1135 char *buf
= (char *)(devpriv
->pwm_urb
->transfer_buffer
);
1136 int szbuf
= devpriv
->pwm_buf_sz
;
1139 for (i
= 0; i
< szbuf
; i
++) {
1153 static int usbduxsigma_pwm_write(struct comedi_device
*dev
,
1154 struct comedi_subdevice
*s
,
1155 struct comedi_insn
*insn
,
1158 unsigned int chan
= CR_CHAN(insn
->chanspec
);
1161 * It doesn't make sense to support more than one value here
1162 * because it would just overwrite the PWM buffer.
1168 * The sign is set via a special INSN only, this gives us 8 bits
1169 * for normal operation, sign is 0 by default.
1171 usbduxsigma_pwm_pattern(dev
, s
, chan
, data
[0], 0);
1176 static int usbduxsigma_pwm_config(struct comedi_device
*dev
,
1177 struct comedi_subdevice
*s
,
1178 struct comedi_insn
*insn
,
1181 struct usbduxsigma_private
*devpriv
= dev
->private;
1182 unsigned int chan
= CR_CHAN(insn
->chanspec
);
1185 case INSN_CONFIG_ARM
:
1187 * if not zero the PWM is limited to a certain time which is
1188 * not supported here
1192 return usbduxsigma_pwm_start(dev
, s
);
1193 case INSN_CONFIG_DISARM
:
1194 return usbduxsigma_pwm_cancel(dev
, s
);
1195 case INSN_CONFIG_GET_PWM_STATUS
:
1196 data
[1] = devpriv
->pwm_cmd_running
;
1198 case INSN_CONFIG_PWM_SET_PERIOD
:
1199 return usbduxsigma_pwm_period(dev
, s
, data
[1]);
1200 case INSN_CONFIG_PWM_GET_PERIOD
:
1201 data
[1] = devpriv
->pwm_period
;
1203 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1206 * data[2] = sign (for a relay)
1208 usbduxsigma_pwm_pattern(dev
, s
, chan
, data
[1], (data
[2] != 0));
1210 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1211 /* values are not kept in this driver, nothing to return */
1217 static int usbduxsigma_getstatusinfo(struct comedi_device
*dev
, int chan
)
1219 struct comedi_subdevice
*s
= dev
->read_subdev
;
1220 struct usbduxsigma_private
*devpriv
= dev
->private;
1228 sysred
= 0; /* ADC zero */
1231 sysred
= 1; /* ADC offset */
1234 sysred
= 4; /* VCC */
1237 sysred
= 8; /* temperature */
1240 sysred
= 16; /* gain */
1243 sysred
= 32; /* ref */
1247 devpriv
->dux_commands
[1] = 0x12; /* CONFIG0 */
1248 devpriv
->dux_commands
[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
1249 devpriv
->dux_commands
[3] = 0x00; /* CONFIG3: diff. channels off */
1250 devpriv
->dux_commands
[4] = 0;
1251 devpriv
->dux_commands
[5] = 0;
1252 devpriv
->dux_commands
[6] = sysred
;
1253 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_SINGLE_AD_CMD
);
1257 ret
= usbduxsigma_receive_cmd(dev
, USBDUXSIGMA_SINGLE_AD_CMD
);
1261 /* 32 bits big endian from the A/D converter */
1262 val
= be32_to_cpu(get_unaligned((__be32
*)(devpriv
->insn_buf
+ 1)));
1263 val
&= 0x00ffffff; /* strip status byte */
1265 return (int)comedi_offset_munge(s
, val
);
1268 static int usbduxsigma_firmware_upload(struct comedi_device
*dev
,
1269 const u8
*data
, size_t size
,
1270 unsigned long context
)
1272 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
1280 if (size
> FIRMWARE_MAX_LEN
) {
1281 dev_err(dev
->class_dev
, "firmware binary too large for FX2\n");
1285 /* we generate a local buffer for the firmware */
1286 buf
= kmemdup(data
, size
, GFP_KERNEL
);
1290 /* we need a malloc'ed buffer for usb_control_msg() */
1291 tmp
= kmalloc(1, GFP_KERNEL
);
1297 /* stop the current firmware on the device */
1298 *tmp
= 1; /* 7f92 to one */
1299 ret
= usb_control_msg(usb
, usb_sndctrlpipe(usb
, 0),
1302 USBDUXSUB_CPUCS
, 0x0000,
1306 dev_err(dev
->class_dev
, "can not stop firmware\n");
1310 /* upload the new firmware to the device */
1311 ret
= usb_control_msg(usb
, usb_sndctrlpipe(usb
, 0),
1318 dev_err(dev
->class_dev
, "firmware upload failed\n");
1322 /* start the new firmware on the device */
1323 *tmp
= 0; /* 7f92 to zero */
1324 ret
= usb_control_msg(usb
, usb_sndctrlpipe(usb
, 0),
1327 USBDUXSUB_CPUCS
, 0x0000,
1331 dev_err(dev
->class_dev
, "can not start firmware\n");
1339 static int usbduxsigma_alloc_usb_buffers(struct comedi_device
*dev
)
1341 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
1342 struct usbduxsigma_private
*devpriv
= dev
->private;
1346 devpriv
->dux_commands
= kzalloc(SIZEOFDUXBUFFER
, GFP_KERNEL
);
1347 devpriv
->in_buf
= kzalloc(SIZEINBUF
, GFP_KERNEL
);
1348 devpriv
->insn_buf
= kzalloc(SIZEINSNBUF
, GFP_KERNEL
);
1349 devpriv
->ai_urbs
= kcalloc(devpriv
->n_ai_urbs
, sizeof(urb
), GFP_KERNEL
);
1350 devpriv
->ao_urbs
= kcalloc(devpriv
->n_ao_urbs
, sizeof(urb
), GFP_KERNEL
);
1351 if (!devpriv
->dux_commands
|| !devpriv
->in_buf
|| !devpriv
->insn_buf
||
1352 !devpriv
->ai_urbs
|| !devpriv
->ao_urbs
)
1355 for (i
= 0; i
< devpriv
->n_ai_urbs
; i
++) {
1356 /* one frame: 1ms */
1357 urb
= usb_alloc_urb(1, GFP_KERNEL
);
1360 devpriv
->ai_urbs
[i
] = urb
;
1362 /* will be filled later with a pointer to the comedi-device */
1363 /* and ONLY then the urb should be submitted */
1364 urb
->context
= NULL
;
1365 urb
->pipe
= usb_rcvisocpipe(usb
, 6);
1366 urb
->transfer_flags
= URB_ISO_ASAP
;
1367 urb
->transfer_buffer
= kzalloc(SIZEINBUF
, GFP_KERNEL
);
1368 if (!urb
->transfer_buffer
)
1370 urb
->complete
= usbduxsigma_ai_urb_complete
;
1371 urb
->number_of_packets
= 1;
1372 urb
->transfer_buffer_length
= SIZEINBUF
;
1373 urb
->iso_frame_desc
[0].offset
= 0;
1374 urb
->iso_frame_desc
[0].length
= SIZEINBUF
;
1377 for (i
= 0; i
< devpriv
->n_ao_urbs
; i
++) {
1378 /* one frame: 1ms */
1379 urb
= usb_alloc_urb(1, GFP_KERNEL
);
1382 devpriv
->ao_urbs
[i
] = urb
;
1384 /* will be filled later with a pointer to the comedi-device */
1385 /* and ONLY then the urb should be submitted */
1386 urb
->context
= NULL
;
1387 urb
->pipe
= usb_sndisocpipe(usb
, 2);
1388 urb
->transfer_flags
= URB_ISO_ASAP
;
1389 urb
->transfer_buffer
= kzalloc(SIZEOUTBUF
, GFP_KERNEL
);
1390 if (!urb
->transfer_buffer
)
1392 urb
->complete
= usbduxsigma_ao_urb_complete
;
1393 urb
->number_of_packets
= 1;
1394 urb
->transfer_buffer_length
= SIZEOUTBUF
;
1395 urb
->iso_frame_desc
[0].offset
= 0;
1396 urb
->iso_frame_desc
[0].length
= SIZEOUTBUF
;
1397 urb
->interval
= 1; /* (u)frames */
1400 if (devpriv
->pwm_buf_sz
) {
1401 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1404 devpriv
->pwm_urb
= urb
;
1406 urb
->transfer_buffer
= kzalloc(devpriv
->pwm_buf_sz
,
1408 if (!urb
->transfer_buffer
)
1415 static void usbduxsigma_free_usb_buffers(struct comedi_device
*dev
)
1417 struct usbduxsigma_private
*devpriv
= dev
->private;
1421 urb
= devpriv
->pwm_urb
;
1423 kfree(urb
->transfer_buffer
);
1426 if (devpriv
->ao_urbs
) {
1427 for (i
= 0; i
< devpriv
->n_ao_urbs
; i
++) {
1428 urb
= devpriv
->ao_urbs
[i
];
1430 kfree(urb
->transfer_buffer
);
1434 kfree(devpriv
->ao_urbs
);
1436 if (devpriv
->ai_urbs
) {
1437 for (i
= 0; i
< devpriv
->n_ai_urbs
; i
++) {
1438 urb
= devpriv
->ai_urbs
[i
];
1440 kfree(urb
->transfer_buffer
);
1444 kfree(devpriv
->ai_urbs
);
1446 kfree(devpriv
->insn_buf
);
1447 kfree(devpriv
->in_buf
);
1448 kfree(devpriv
->dux_commands
);
1451 static int usbduxsigma_auto_attach(struct comedi_device
*dev
,
1452 unsigned long context_unused
)
1454 struct usb_interface
*intf
= comedi_to_usb_interface(dev
);
1455 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
1456 struct usbduxsigma_private
*devpriv
;
1457 struct comedi_subdevice
*s
;
1461 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
1465 mutex_init(&devpriv
->mut
);
1467 usb_set_intfdata(intf
, devpriv
);
1469 devpriv
->high_speed
= (usb
->speed
== USB_SPEED_HIGH
);
1470 if (devpriv
->high_speed
) {
1471 devpriv
->n_ai_urbs
= NUMOFINBUFFERSHIGH
;
1472 devpriv
->n_ao_urbs
= NUMOFOUTBUFFERSHIGH
;
1473 devpriv
->pwm_buf_sz
= 512;
1475 devpriv
->n_ai_urbs
= NUMOFINBUFFERSFULL
;
1476 devpriv
->n_ao_urbs
= NUMOFOUTBUFFERSFULL
;
1479 ret
= usbduxsigma_alloc_usb_buffers(dev
);
1483 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
1484 ret
= usb_set_interface(usb
, intf
->altsetting
->desc
.bInterfaceNumber
,
1487 dev_err(dev
->class_dev
,
1488 "could not set alternate setting 3 in high speed\n");
1492 ret
= comedi_load_firmware(dev
, &usb
->dev
, FIRMWARE
,
1493 usbduxsigma_firmware_upload
, 0);
1497 ret
= comedi_alloc_subdevices(dev
, (devpriv
->high_speed
) ? 4 : 3);
1501 /* Analog Input subdevice */
1502 s
= &dev
->subdevices
[0];
1503 dev
->read_subdev
= s
;
1504 s
->type
= COMEDI_SUBD_AI
;
1505 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
| SDF_CMD_READ
| SDF_LSAMPL
;
1506 s
->n_chan
= NUMCHANNELS
;
1507 s
->len_chanlist
= NUMCHANNELS
;
1508 s
->maxdata
= 0x00ffffff;
1509 s
->range_table
= &usbduxsigma_ai_range
;
1510 s
->insn_read
= usbduxsigma_ai_insn_read
;
1511 s
->do_cmdtest
= usbduxsigma_ai_cmdtest
;
1512 s
->do_cmd
= usbduxsigma_ai_cmd
;
1513 s
->cancel
= usbduxsigma_ai_cancel
;
1515 /* Analog Output subdevice */
1516 s
= &dev
->subdevices
[1];
1517 dev
->write_subdev
= s
;
1518 s
->type
= COMEDI_SUBD_AO
;
1519 s
->subdev_flags
= SDF_WRITABLE
| SDF_GROUND
| SDF_CMD_WRITE
;
1521 s
->len_chanlist
= s
->n_chan
;
1522 s
->maxdata
= 0x00ff;
1523 s
->range_table
= &range_unipolar2_5
;
1524 s
->insn_write
= usbduxsigma_ao_insn_write
;
1525 s
->insn_read
= usbduxsigma_ao_insn_read
;
1526 s
->do_cmdtest
= usbduxsigma_ao_cmdtest
;
1527 s
->do_cmd
= usbduxsigma_ao_cmd
;
1528 s
->cancel
= usbduxsigma_ao_cancel
;
1530 ret
= comedi_alloc_subdev_readback(s
);
1534 /* Digital I/O subdevice */
1535 s
= &dev
->subdevices
[2];
1536 s
->type
= COMEDI_SUBD_DIO
;
1537 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
1540 s
->range_table
= &range_digital
;
1541 s
->insn_bits
= usbduxsigma_dio_insn_bits
;
1542 s
->insn_config
= usbduxsigma_dio_insn_config
;
1544 if (devpriv
->high_speed
) {
1545 /* Timer / pwm subdevice */
1546 s
= &dev
->subdevices
[3];
1547 s
->type
= COMEDI_SUBD_PWM
;
1548 s
->subdev_flags
= SDF_WRITABLE
| SDF_PWM_HBRIDGE
;
1550 s
->maxdata
= devpriv
->pwm_buf_sz
;
1551 s
->insn_write
= usbduxsigma_pwm_write
;
1552 s
->insn_config
= usbduxsigma_pwm_config
;
1554 usbduxsigma_pwm_period(dev
, s
, PWM_DEFAULT_PERIOD
);
1557 offset
= usbduxsigma_getstatusinfo(dev
, 0);
1559 dev_err(dev
->class_dev
,
1560 "Communication to USBDUXSIGMA failed! Check firmware and cabling.\n");
1564 dev_info(dev
->class_dev
, "ADC_zero = %x\n", offset
);
1569 static void usbduxsigma_detach(struct comedi_device
*dev
)
1571 struct usb_interface
*intf
= comedi_to_usb_interface(dev
);
1572 struct usbduxsigma_private
*devpriv
= dev
->private;
1574 usb_set_intfdata(intf
, NULL
);
1579 mutex_lock(&devpriv
->mut
);
1581 /* force unlink all urbs */
1582 usbduxsigma_ai_stop(dev
, 1);
1583 usbduxsigma_ao_stop(dev
, 1);
1584 usbduxsigma_pwm_stop(dev
, 1);
1586 usbduxsigma_free_usb_buffers(dev
);
1588 mutex_unlock(&devpriv
->mut
);
1591 static struct comedi_driver usbduxsigma_driver
= {
1592 .driver_name
= "usbduxsigma",
1593 .module
= THIS_MODULE
,
1594 .auto_attach
= usbduxsigma_auto_attach
,
1595 .detach
= usbduxsigma_detach
,
1598 static int usbduxsigma_usb_probe(struct usb_interface
*intf
,
1599 const struct usb_device_id
*id
)
1601 return comedi_usb_auto_config(intf
, &usbduxsigma_driver
, 0);
1604 static const struct usb_device_id usbduxsigma_usb_table
[] = {
1605 { USB_DEVICE(0x13d8, 0x0020) },
1606 { USB_DEVICE(0x13d8, 0x0021) },
1607 { USB_DEVICE(0x13d8, 0x0022) },
1610 MODULE_DEVICE_TABLE(usb
, usbduxsigma_usb_table
);
1612 static struct usb_driver usbduxsigma_usb_driver
= {
1613 .name
= "usbduxsigma",
1614 .probe
= usbduxsigma_usb_probe
,
1615 .disconnect
= comedi_usb_auto_unconfig
,
1616 .id_table
= usbduxsigma_usb_table
,
1618 module_comedi_usb_driver(usbduxsigma_driver
, usbduxsigma_usb_driver
);
1620 MODULE_AUTHOR("Bernd Porr, mail@berndporr.me.uk");
1621 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- mail@berndporr.me.uk");
1622 MODULE_LICENSE("GPL");
1623 MODULE_FIRMWARE(FIRMWARE
);