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.
18 * Description: University of Stirling USB DAQ & INCITE Technology Limited
19 * Devices: (ITL) USB-DUX [usbduxsigma]
20 * Author: Bernd Porr <BerndPorr@f2s.com>
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/usb.h>
49 #include <linux/fcntl.h>
50 #include <linux/compiler.h>
52 #include "comedi_fc.h"
53 #include "../comedidev.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 #define USBDUXSIGMA_NUM_AO_CHAN 4
79 /* Size of one A/D value */
80 #define SIZEADIN ((sizeof(uint32_t)))
83 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
86 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
89 #define SIZEINSNBUF 16
91 /* Number of DA channels */
92 #define NUMOUTCHANNELS 8
94 /* size of one value for the D/A converter: channel and value */
95 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(uint16_t)))
98 * Size of the output-buffer in bytes
99 * Actually only the first 4 triplets are used but for the
100 * high speed mode we need to pad it to 8 (microframes).
102 #define SIZEOUTBUF ((8*SIZEDAOUT))
105 * Size of the buffer for the dux commands: just now max size is determined
106 * by the analogue out + command byte + panic bytes...
108 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
110 /* Number of in-URBs which receive the data: min=2 */
111 #define NUMOFINBUFFERSFULL 5
113 /* Number of out-URBs which send the data: min=2 */
114 #define NUMOFOUTBUFFERSFULL 5
116 /* Number of in-URBs which receive the data: min=5 */
117 /* must have more buffers due to buggy USB ctr */
118 #define NUMOFINBUFFERSHIGH 10
120 /* Number of out-URBs which send the data: min=5 */
121 /* must have more buffers due to buggy USB ctr */
122 #define NUMOFOUTBUFFERSHIGH 10
124 /* number of retries to get the right dux command */
127 /* bulk transfer commands to usbduxsigma */
128 #define USBBUXSIGMA_AD_CMD 0
129 #define USBDUXSIGMA_DA_CMD 1
130 #define USBDUXSIGMA_DIO_CFG_CMD 2
131 #define USBDUXSIGMA_DIO_BITS_CMD 3
132 #define USBDUXSIGMA_SINGLE_AD_CMD 4
133 #define USBDUXSIGMA_PWM_ON_CMD 7
134 #define USBDUXSIGMA_PWM_OFF_CMD 8
136 static const struct comedi_lrange usbduxsigma_ai_range
= {
138 BIP_RANGE(2.65 / 2.0)
142 struct usbduxsigma_private
{
143 /* actual number of in-buffers */
145 /* actual number of out-buffers */
147 /* ISO-transfer handling: buffers */
148 struct urb
**ai_urbs
;
149 struct urb
**ao_urbs
;
150 /* pwm-transfer handling */
153 unsigned int pwm_period
;
154 /* PWM internal delay for the GPIF in the FX2 */
156 /* size of the PWM buffer which holds the bit pattern */
158 /* input buffer for the ISO-transfer */
160 /* input buffer for single insn */
163 uint8_t ao_chanlist
[USBDUXSIGMA_NUM_AO_CHAN
];
164 unsigned int ao_readback
[USBDUXSIGMA_NUM_AO_CHAN
];
166 unsigned high_speed
:1;
167 unsigned ai_cmd_running
:1;
168 unsigned ai_continuous
:1;
169 unsigned ao_cmd_running
:1;
170 unsigned ao_continuous
:1;
171 unsigned pwm_cmd_running
:1;
173 /* number of samples to acquire */
176 /* time between samples in units of the timer */
177 unsigned int ai_timer
;
178 unsigned int ao_timer
;
179 /* counter between acquisitions */
180 unsigned int ai_counter
;
181 unsigned int ao_counter
;
182 /* interval in frames/uframes */
183 unsigned int ai_interval
;
185 uint8_t *dux_commands
;
186 struct semaphore sem
;
189 static void usbduxsigma_unlink_urbs(struct urb
**urbs
, int num_urbs
)
193 for (i
= 0; i
< num_urbs
; i
++)
194 usb_kill_urb(urbs
[i
]);
197 static void usbduxsigma_ai_stop(struct comedi_device
*dev
, int do_unlink
)
199 struct usbduxsigma_private
*devpriv
= dev
->private;
201 if (do_unlink
&& devpriv
->ai_urbs
)
202 usbduxsigma_unlink_urbs(devpriv
->ai_urbs
, devpriv
->n_ai_urbs
);
204 devpriv
->ai_cmd_running
= 0;
207 static int usbduxsigma_ai_cancel(struct comedi_device
*dev
,
208 struct comedi_subdevice
*s
)
210 struct usbduxsigma_private
*devpriv
= dev
->private;
213 /* unlink only if it is really running */
214 usbduxsigma_ai_stop(dev
, devpriv
->ai_cmd_running
);
220 static void usbduxsigma_ai_urb_complete(struct urb
*urb
)
222 struct comedi_device
*dev
= urb
->context
;
223 struct usbduxsigma_private
*devpriv
= dev
->private;
224 struct comedi_subdevice
*s
= dev
->read_subdev
;
225 unsigned int dio_state
;
230 /* first we test if something unusual has just happened */
231 switch (urb
->status
) {
233 /* copy the result in the transfer buffer */
234 memcpy(devpriv
->in_buf
, urb
->transfer_buffer
, SIZEINBUF
);
238 * error in the ISOchronous data
239 * we don't copy the data into the transfer buffer
240 * and recycle the last data byte
242 dev_dbg(dev
->class_dev
, "CRC error in ISO IN stream\n");
250 /* happens after an unlink command */
251 if (devpriv
->ai_cmd_running
) {
252 usbduxsigma_ai_stop(dev
, 0); /* w/o unlink */
253 /* we are still running a command, tell comedi */
254 s
->async
->events
|= (COMEDI_CB_EOA
| COMEDI_CB_ERROR
);
255 comedi_event(dev
, s
);
261 * a real error on the bus
262 * pass error to comedi if we are really running a command
264 if (devpriv
->ai_cmd_running
) {
265 dev_err(dev
->class_dev
,
266 "%s: non-zero urb status (%d)\n",
267 __func__
, urb
->status
);
268 usbduxsigma_ai_stop(dev
, 0); /* w/o unlink */
269 s
->async
->events
|= (COMEDI_CB_EOA
| COMEDI_CB_ERROR
);
270 comedi_event(dev
, s
);
275 if (unlikely(!devpriv
->ai_cmd_running
))
278 urb
->dev
= comedi_to_usb_dev(dev
);
280 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
281 if (unlikely(ret
< 0)) {
282 dev_err(dev
->class_dev
, "%s: urb resubmit failed (%d)\n",
284 if (ret
== -EL2NSYNC
)
285 dev_err(dev
->class_dev
,
286 "buggy USB host controller or bug in IRQ handler\n");
287 usbduxsigma_ai_stop(dev
, 0); /* w/o unlink */
288 s
->async
->events
|= (COMEDI_CB_EOA
| COMEDI_CB_ERROR
);
289 comedi_event(dev
, s
);
293 /* get the state of the dio pins to allow external trigger */
294 dio_state
= be32_to_cpu(devpriv
->in_buf
[0]);
296 devpriv
->ai_counter
--;
297 if (likely(devpriv
->ai_counter
> 0))
300 /* timer zero, transfer measurements to comedi */
301 devpriv
->ai_counter
= devpriv
->ai_timer
;
303 if (!devpriv
->ai_continuous
) {
304 /* not continuous, fixed number of samples */
305 devpriv
->ai_sample_count
--;
306 if (devpriv
->ai_sample_count
< 0) {
307 usbduxsigma_ai_stop(dev
, 0); /* w/o unlink */
308 /* acquistion is over, tell comedi */
309 s
->async
->events
|= COMEDI_CB_EOA
;
310 comedi_event(dev
, s
);
315 /* get the data from the USB bus and hand it over to comedi */
316 for (i
= 0; i
< s
->async
->cmd
.chanlist_len
; i
++) {
317 /* transfer data, note first byte is the DIO state */
318 val
= be32_to_cpu(devpriv
->in_buf
[i
+1]);
319 val
&= 0x00ffffff; /* strip status byte */
320 val
^= 0x00800000; /* convert to unsigned */
322 ret
= cfc_write_array_to_buffer(s
, &val
, sizeof(uint32_t));
323 if (unlikely(ret
== 0)) {
324 /* buffer overflow */
325 usbduxsigma_ai_stop(dev
, 0); /* w/o unlink */
329 /* tell comedi that data is there */
330 s
->async
->events
|= (COMEDI_CB_BLOCK
| COMEDI_CB_EOS
);
331 comedi_event(dev
, s
);
334 static void usbduxsigma_ao_stop(struct comedi_device
*dev
, int do_unlink
)
336 struct usbduxsigma_private
*devpriv
= dev
->private;
338 if (do_unlink
&& devpriv
->ao_urbs
)
339 usbduxsigma_unlink_urbs(devpriv
->ao_urbs
, devpriv
->n_ao_urbs
);
341 devpriv
->ao_cmd_running
= 0;
344 static int usbduxsigma_ao_cancel(struct comedi_device
*dev
,
345 struct comedi_subdevice
*s
)
347 struct usbduxsigma_private
*devpriv
= dev
->private;
350 /* unlink only if it is really running */
351 usbduxsigma_ao_stop(dev
, devpriv
->ao_cmd_running
);
357 static void usbduxsigma_ao_urb_complete(struct urb
*urb
)
359 struct comedi_device
*dev
= urb
->context
;
360 struct usbduxsigma_private
*devpriv
= dev
->private;
361 struct comedi_subdevice
*s
= dev
->write_subdev
;
367 switch (urb
->status
) {
376 /* happens after an unlink command */
377 if (devpriv
->ao_cmd_running
) {
378 usbduxsigma_ao_stop(dev
, 0); /* w/o unlink */
379 s
->async
->events
|= COMEDI_CB_EOA
;
380 comedi_event(dev
, s
);
386 if (devpriv
->ao_cmd_running
) {
387 dev_err(dev
->class_dev
,
388 "%s: non-zero urb status (%d)\n",
389 __func__
, urb
->status
);
390 usbduxsigma_ao_stop(dev
, 0); /* w/o unlink */
391 s
->async
->events
|= (COMEDI_CB_ERROR
| COMEDI_CB_EOA
);
392 comedi_event(dev
, s
);
397 if (!devpriv
->ao_cmd_running
)
400 devpriv
->ao_counter
--;
401 if ((int)devpriv
->ao_counter
<= 0) {
402 /* timer zero, transfer from comedi */
403 devpriv
->ao_counter
= devpriv
->ao_timer
;
405 if (!devpriv
->ao_continuous
) {
406 /* not continuous, fixed number of samples */
407 devpriv
->ao_sample_count
--;
408 if (devpriv
->ao_sample_count
< 0) {
409 usbduxsigma_ao_stop(dev
, 0); /* w/o unlink */
410 /* acquistion is over, tell comedi */
411 s
->async
->events
|= COMEDI_CB_EOA
;
412 comedi_event(dev
, s
);
417 /* transmit data to the USB bus */
418 datap
= urb
->transfer_buffer
;
419 len
= s
->async
->cmd
.chanlist_len
;
421 for (i
= 0; i
< len
; i
++) {
422 unsigned int chan
= devpriv
->ao_chanlist
[i
];
425 ret
= comedi_buf_get(s
->async
, &val
);
427 dev_err(dev
->class_dev
, "buffer underflow\n");
428 s
->async
->events
|= (COMEDI_CB_EOA
|
433 devpriv
->ao_readback
[chan
] = val
;
435 s
->async
->events
|= COMEDI_CB_BLOCK
;
436 comedi_event(dev
, s
);
440 urb
->transfer_buffer_length
= SIZEOUTBUF
;
441 urb
->dev
= comedi_to_usb_dev(dev
);
443 if (devpriv
->high_speed
)
444 urb
->interval
= 8; /* uframes */
446 urb
->interval
= 1; /* frames */
447 urb
->number_of_packets
= 1;
448 urb
->iso_frame_desc
[0].offset
= 0;
449 urb
->iso_frame_desc
[0].length
= SIZEOUTBUF
;
450 urb
->iso_frame_desc
[0].status
= 0;
451 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
453 dev_err(dev
->class_dev
,
454 "%s: urb resubmit failed (%d)\n",
457 dev_err(dev
->class_dev
,
458 "buggy USB host controller or bug in IRQ handler\n");
459 usbduxsigma_ao_stop(dev
, 0); /* w/o unlink */
460 s
->async
->events
|= (COMEDI_CB_EOA
| COMEDI_CB_ERROR
);
461 comedi_event(dev
, s
);
465 static int usbduxsigma_submit_urbs(struct comedi_device
*dev
,
466 struct urb
**urbs
, int num_urbs
,
469 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
470 struct usbduxsigma_private
*devpriv
= dev
->private;
475 /* Submit all URBs and start the transfer on the bus */
476 for (i
= 0; i
< num_urbs
; i
++) {
479 /* in case of a resubmission after an unlink... */
481 urb
->interval
= devpriv
->ai_interval
;
485 urb
->transfer_flags
= URB_ISO_ASAP
;
487 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
494 static int usbduxsigma_chans_to_interval(int num_chan
)
503 static int usbduxsigma_ai_cmdtest(struct comedi_device
*dev
,
504 struct comedi_subdevice
*s
,
505 struct comedi_cmd
*cmd
)
507 struct usbduxsigma_private
*devpriv
= dev
->private;
508 int high_speed
= devpriv
->high_speed
;
509 int interval
= usbduxsigma_chans_to_interval(cmd
->chanlist_len
);
512 /* Step 1 : check if triggers are trivially valid */
514 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_NOW
| TRIG_INT
);
515 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, TRIG_TIMER
);
516 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
517 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
518 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
523 /* Step 2a : make sure trigger sources are unique */
525 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
526 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
528 /* Step 2b : and mutually compatible */
533 /* Step 3: check if arguments are trivially valid */
535 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
537 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) /* internal trigger */
538 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, 0);
540 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
545 * In high speed mode microframes are possible.
546 * However, during one microframe we can roughly
547 * sample two channels. Thus, the more channels
548 * are in the channel list the more time we need.
550 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
551 (1000000 / 8 * interval
));
553 tmp
= (cmd
->scan_begin_arg
/ 125000) * 125000;
556 /* 1kHz scans every USB frame */
557 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
560 tmp
= (cmd
->scan_begin_arg
/ 1000000) * 1000000;
562 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, tmp
);
565 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
567 if (cmd
->stop_src
== TRIG_COUNT
) {
568 /* any count is allowed */
571 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
577 /* Step 4: fix up any arguments */
581 * every 2 channels get a time window of 125us. Thus, if we
582 * sample all 16 channels we need 1ms. If we sample only one
583 * channel we need only 125us
585 devpriv
->ai_interval
= interval
;
586 devpriv
->ai_timer
= cmd
->scan_begin_arg
/ (125000 * interval
);
588 /* interval always 1ms */
589 devpriv
->ai_interval
= 1;
590 devpriv
->ai_timer
= cmd
->scan_begin_arg
/ 1000000;
592 if (devpriv
->ai_timer
< 1)
595 if (cmd
->stop_src
== TRIG_COUNT
) {
596 /* data arrives as one packet */
597 devpriv
->ai_sample_count
= cmd
->stop_arg
;
598 devpriv
->ai_continuous
= 0;
600 /* continuous acquisition */
601 devpriv
->ai_continuous
= 1;
602 devpriv
->ai_sample_count
= 0;
612 * creates the ADC command for the MAX1271
613 * range is the range value from comedi
615 static void create_adc_command(unsigned int chan
,
620 (*muxsg0
) = (*muxsg0
) | (1 << chan
);
622 (*muxsg1
) = (*muxsg1
) | (1 << (chan
-8));
625 static int usbbuxsigma_send_cmd(struct comedi_device
*dev
, int cmd_type
)
627 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
628 struct usbduxsigma_private
*devpriv
= dev
->private;
631 devpriv
->dux_commands
[0] = cmd_type
;
633 return usb_bulk_msg(usb
, usb_sndbulkpipe(usb
, 1),
634 devpriv
->dux_commands
, SIZEOFDUXBUFFER
,
635 &nsent
, BULK_TIMEOUT
);
638 static int usbduxsigma_receive_cmd(struct comedi_device
*dev
, int command
)
640 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
641 struct usbduxsigma_private
*devpriv
= dev
->private;
646 for (i
= 0; i
< RETRIES
; i
++) {
647 ret
= usb_bulk_msg(usb
, usb_rcvbulkpipe(usb
, 8),
648 devpriv
->insn_buf
, SIZEINSNBUF
,
649 &nrec
, BULK_TIMEOUT
);
653 if (devpriv
->insn_buf
[0] == command
)
657 * This is only reached if the data has been requested a
658 * couple of times and the command was not received.
663 static int usbduxsigma_ai_inttrig(struct comedi_device
*dev
,
664 struct comedi_subdevice
*s
,
665 unsigned int trignum
)
667 struct usbduxsigma_private
*devpriv
= dev
->private;
674 if (!devpriv
->ai_cmd_running
) {
675 devpriv
->ai_cmd_running
= 1;
676 ret
= usbduxsigma_submit_urbs(dev
, devpriv
->ai_urbs
,
677 devpriv
->n_ai_urbs
, 1);
679 devpriv
->ai_cmd_running
= 0;
683 s
->async
->inttrig
= NULL
;
690 static int usbduxsigma_ai_cmd(struct comedi_device
*dev
,
691 struct comedi_subdevice
*s
)
693 struct usbduxsigma_private
*devpriv
= dev
->private;
694 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
695 unsigned int len
= cmd
->chanlist_len
;
704 /* set current channel of the running acquisition to zero */
705 s
->async
->cur_chan
= 0;
706 for (i
= 0; i
< len
; i
++) {
707 unsigned int chan
= CR_CHAN(cmd
->chanlist
[i
]);
709 create_adc_command(chan
, &muxsg0
, &muxsg1
);
712 devpriv
->dux_commands
[1] = len
; /* num channels per time step */
713 devpriv
->dux_commands
[2] = 0x12; /* CONFIG0 */
714 devpriv
->dux_commands
[3] = 0x03; /* CONFIG1: 23kHz sample, delay 0us */
715 devpriv
->dux_commands
[4] = 0x00; /* CONFIG3: diff. channels off */
716 devpriv
->dux_commands
[5] = muxsg0
;
717 devpriv
->dux_commands
[6] = muxsg1
;
718 devpriv
->dux_commands
[7] = sysred
;
720 ret
= usbbuxsigma_send_cmd(dev
, USBBUXSIGMA_AD_CMD
);
726 devpriv
->ai_counter
= devpriv
->ai_timer
;
728 if (cmd
->start_src
== TRIG_NOW
) {
729 /* enable this acquisition operation */
730 devpriv
->ai_cmd_running
= 1;
731 ret
= usbduxsigma_submit_urbs(dev
, devpriv
->ai_urbs
,
732 devpriv
->n_ai_urbs
, 1);
734 devpriv
->ai_cmd_running
= 0;
738 s
->async
->inttrig
= NULL
;
739 } else { /* TRIG_INT */
740 /* wait for an internal signal and submit the urbs later */
741 s
->async
->inttrig
= usbduxsigma_ai_inttrig
;
749 static int usbduxsigma_ai_insn_read(struct comedi_device
*dev
,
750 struct comedi_subdevice
*s
,
751 struct comedi_insn
*insn
,
754 struct usbduxsigma_private
*devpriv
= dev
->private;
755 unsigned int chan
= CR_CHAN(insn
->chanspec
);
763 if (devpriv
->ai_cmd_running
) {
768 create_adc_command(chan
, &muxsg0
, &muxsg1
);
770 /* Mode 0 is used to get a single conversion on demand */
771 devpriv
->dux_commands
[1] = 0x16; /* CONFIG0: chopper on */
772 devpriv
->dux_commands
[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
773 devpriv
->dux_commands
[3] = 0x00; /* CONFIG3: diff. channels off */
774 devpriv
->dux_commands
[4] = muxsg0
;
775 devpriv
->dux_commands
[5] = muxsg1
;
776 devpriv
->dux_commands
[6] = sysred
;
779 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_SINGLE_AD_CMD
);
785 for (i
= 0; i
< insn
->n
; i
++) {
788 ret
= usbduxsigma_receive_cmd(dev
, USBDUXSIGMA_SINGLE_AD_CMD
);
794 /* 32 bits big endian from the A/D converter */
795 val
= be32_to_cpu(*((uint32_t *)((devpriv
->insn_buf
) + 1)));
796 val
&= 0x00ffffff; /* strip status byte */
797 val
^= 0x00800000; /* convert to unsigned */
806 static int usbduxsigma_ao_insn_read(struct comedi_device
*dev
,
807 struct comedi_subdevice
*s
,
808 struct comedi_insn
*insn
,
811 struct usbduxsigma_private
*devpriv
= dev
->private;
812 unsigned int chan
= CR_CHAN(insn
->chanspec
);
816 for (i
= 0; i
< insn
->n
; i
++)
817 data
[i
] = devpriv
->ao_readback
[chan
];
823 static int usbduxsigma_ao_insn_write(struct comedi_device
*dev
,
824 struct comedi_subdevice
*s
,
825 struct comedi_insn
*insn
,
828 struct usbduxsigma_private
*devpriv
= dev
->private;
829 unsigned int chan
= CR_CHAN(insn
->chanspec
);
834 if (devpriv
->ao_cmd_running
) {
839 for (i
= 0; i
< insn
->n
; i
++) {
840 devpriv
->dux_commands
[1] = 1; /* num channels */
841 devpriv
->dux_commands
[2] = data
[i
]; /* value */
842 devpriv
->dux_commands
[3] = chan
; /* channel number */
843 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_DA_CMD
);
848 devpriv
->ao_readback
[chan
] = data
[i
];
855 static int usbduxsigma_ao_inttrig(struct comedi_device
*dev
,
856 struct comedi_subdevice
*s
,
857 unsigned int trignum
)
859 struct usbduxsigma_private
*devpriv
= dev
->private;
866 if (!devpriv
->ao_cmd_running
) {
867 devpriv
->ao_cmd_running
= 1;
868 ret
= usbduxsigma_submit_urbs(dev
, devpriv
->ao_urbs
,
869 devpriv
->n_ao_urbs
, 0);
871 devpriv
->ao_cmd_running
= 0;
875 s
->async
->inttrig
= NULL
;
882 static int usbduxsigma_ao_cmdtest(struct comedi_device
*dev
,
883 struct comedi_subdevice
*s
,
884 struct comedi_cmd
*cmd
)
886 struct usbduxsigma_private
*devpriv
= dev
->private;
891 /* high speed conversions are not used yet */
892 high_speed
= 0; /* (devpriv->high_speed) */
894 /* Step 1 : check if triggers are trivially valid */
896 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_NOW
| TRIG_INT
);
900 * start immediately a new scan
901 * the sampling rate is set by the coversion rate
905 /* start a new scan (output at once) with a timer */
908 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, flags
);
910 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
911 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
912 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
919 /* Step 2a : make sure trigger sources are unique */
921 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
922 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
924 /* Step 2b : and mutually compatible */
929 /* Step 3: check if arguments are trivially valid */
931 err
|= cfc_check_trigger_arg_is(&cmd
->start_arg
, 0);
933 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) /* internal trigger */
934 err
|= cfc_check_trigger_arg_is(&cmd
->scan_begin_arg
, 0);
936 if (cmd
->scan_begin_src
== TRIG_TIMER
)
937 err
|= cfc_check_trigger_arg_min(&cmd
->scan_begin_arg
,
940 /* not used now, is for later use */
941 if (cmd
->convert_src
== TRIG_TIMER
)
942 err
|= cfc_check_trigger_arg_min(&cmd
->convert_arg
, 125000);
944 err
|= cfc_check_trigger_arg_is(&cmd
->scan_end_arg
, cmd
->chanlist_len
);
946 if (cmd
->stop_src
== TRIG_COUNT
) {
947 /* any count is allowed */
950 err
|= cfc_check_trigger_arg_is(&cmd
->stop_arg
, 0);
956 /* Step 4: fix up any arguments */
958 /* we count in timer steps */
960 /* timing of the conversion itself: every 125 us */
961 devpriv
->ao_timer
= cmd
->convert_arg
/ 125000;
964 * timing of the scan: every 1ms
965 * we get all channels at once
967 devpriv
->ao_timer
= cmd
->scan_begin_arg
/ 1000000;
969 if (devpriv
->ao_timer
< 1)
972 if (cmd
->stop_src
== TRIG_COUNT
) {
973 /* not continuous, use counter */
975 /* high speed also scans everything at once */
976 devpriv
->ao_sample_count
= cmd
->stop_arg
*
980 * There's no scan as the scan has been
981 * handled inside the FX2. Data arrives as
984 devpriv
->ao_sample_count
= cmd
->stop_arg
;
986 devpriv
->ao_continuous
= 0;
988 /* continuous acquisition */
989 devpriv
->ao_continuous
= 1;
990 devpriv
->ao_sample_count
= 0;
999 static int usbduxsigma_ao_cmd(struct comedi_device
*dev
,
1000 struct comedi_subdevice
*s
)
1002 struct usbduxsigma_private
*devpriv
= dev
->private;
1003 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
1007 down(&devpriv
->sem
);
1009 /* set current channel of the running acquisition to zero */
1010 s
->async
->cur_chan
= 0;
1011 for (i
= 0; i
< cmd
->chanlist_len
; ++i
)
1012 devpriv
->ao_chanlist
[i
] = CR_CHAN(cmd
->chanlist
[i
]);
1014 devpriv
->ao_counter
= devpriv
->ao_timer
;
1016 if (cmd
->start_src
== TRIG_NOW
) {
1017 /* enable this acquisition operation */
1018 devpriv
->ao_cmd_running
= 1;
1019 ret
= usbduxsigma_submit_urbs(dev
, devpriv
->ao_urbs
,
1020 devpriv
->n_ao_urbs
, 0);
1022 devpriv
->ao_cmd_running
= 0;
1026 s
->async
->inttrig
= NULL
;
1027 } else { /* TRIG_INT */
1028 /* wait for an internal signal and submit the urbs later */
1029 s
->async
->inttrig
= usbduxsigma_ao_inttrig
;
1037 static int usbduxsigma_dio_insn_config(struct comedi_device
*dev
,
1038 struct comedi_subdevice
*s
,
1039 struct comedi_insn
*insn
,
1044 ret
= comedi_dio_insn_config(dev
, s
, insn
, data
, 0);
1049 * We don't tell the firmware here as it would take 8 frames
1050 * to submit the information. We do it in the (*insn_bits).
1055 static int usbduxsigma_dio_insn_bits(struct comedi_device
*dev
,
1056 struct comedi_subdevice
*s
,
1057 struct comedi_insn
*insn
,
1060 struct usbduxsigma_private
*devpriv
= dev
->private;
1063 down(&devpriv
->sem
);
1065 comedi_dio_update_state(s
, data
);
1067 /* Always update the hardware. See the (*insn_config). */
1068 devpriv
->dux_commands
[1] = s
->io_bits
& 0xff;
1069 devpriv
->dux_commands
[4] = s
->state
& 0xff;
1070 devpriv
->dux_commands
[2] = (s
->io_bits
>> 8) & 0xff;
1071 devpriv
->dux_commands
[5] = (s
->state
>> 8) & 0xff;
1072 devpriv
->dux_commands
[3] = (s
->io_bits
>> 16) & 0xff;
1073 devpriv
->dux_commands
[6] = (s
->state
>> 16) & 0xff;
1075 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_DIO_BITS_CMD
);
1078 ret
= usbduxsigma_receive_cmd(dev
, USBDUXSIGMA_DIO_BITS_CMD
);
1082 s
->state
= devpriv
->insn_buf
[1] |
1083 (devpriv
->insn_buf
[2] << 8) |
1084 (devpriv
->insn_buf
[3] << 16);
1095 static void usbduxsigma_pwm_stop(struct comedi_device
*dev
, int do_unlink
)
1097 struct usbduxsigma_private
*devpriv
= dev
->private;
1100 if (devpriv
->pwm_urb
)
1101 usb_kill_urb(devpriv
->pwm_urb
);
1104 devpriv
->pwm_cmd_running
= 0;
1107 static int usbduxsigma_pwm_cancel(struct comedi_device
*dev
,
1108 struct comedi_subdevice
*s
)
1110 struct usbduxsigma_private
*devpriv
= dev
->private;
1112 /* unlink only if it is really running */
1113 usbduxsigma_pwm_stop(dev
, devpriv
->pwm_cmd_running
);
1115 return usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_PWM_OFF_CMD
);
1118 static void usbduxsigma_pwm_urb_complete(struct urb
*urb
)
1120 struct comedi_device
*dev
= urb
->context
;
1121 struct usbduxsigma_private
*devpriv
= dev
->private;
1124 switch (urb
->status
) {
1133 /* happens after an unlink command */
1134 if (devpriv
->pwm_cmd_running
)
1135 usbduxsigma_pwm_stop(dev
, 0); /* w/o unlink */
1140 if (devpriv
->pwm_cmd_running
) {
1141 dev_err(dev
->class_dev
,
1142 "%s: non-zero urb status (%d)\n",
1143 __func__
, urb
->status
);
1144 usbduxsigma_pwm_stop(dev
, 0); /* w/o unlink */
1149 if (!devpriv
->pwm_cmd_running
)
1152 urb
->transfer_buffer_length
= devpriv
->pwm_buf_sz
;
1153 urb
->dev
= comedi_to_usb_dev(dev
);
1155 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
1157 dev_err(dev
->class_dev
, "%s: urb resubmit failed (%d)\n",
1159 if (ret
== EL2NSYNC
)
1160 dev_err(dev
->class_dev
,
1161 "buggy USB host controller or bug in IRQ handler\n");
1162 usbduxsigma_pwm_stop(dev
, 0); /* w/o unlink */
1166 static int usbduxsigma_submit_pwm_urb(struct comedi_device
*dev
)
1168 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
1169 struct usbduxsigma_private
*devpriv
= dev
->private;
1170 struct urb
*urb
= devpriv
->pwm_urb
;
1172 /* in case of a resubmission after an unlink... */
1173 usb_fill_bulk_urb(urb
, usb
, usb_sndbulkpipe(usb
, 4),
1174 urb
->transfer_buffer
, devpriv
->pwm_buf_sz
,
1175 usbduxsigma_pwm_urb_complete
, dev
);
1177 return usb_submit_urb(urb
, GFP_ATOMIC
);
1180 static int usbduxsigma_pwm_period(struct comedi_device
*dev
,
1181 struct comedi_subdevice
*s
,
1182 unsigned int period
)
1184 struct usbduxsigma_private
*devpriv
= dev
->private;
1187 if (period
< MIN_PWM_PERIOD
) {
1190 fx2delay
= (period
/ (6 * 512 * 1000 / 33)) - 6;
1194 devpriv
->pwm_delay
= fx2delay
;
1195 devpriv
->pwm_period
= period
;
1199 static int usbduxsigma_pwm_start(struct comedi_device
*dev
,
1200 struct comedi_subdevice
*s
)
1202 struct usbduxsigma_private
*devpriv
= dev
->private;
1205 if (devpriv
->pwm_cmd_running
)
1208 devpriv
->dux_commands
[1] = devpriv
->pwm_delay
;
1209 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_PWM_ON_CMD
);
1213 memset(devpriv
->pwm_urb
->transfer_buffer
, 0, devpriv
->pwm_buf_sz
);
1215 devpriv
->pwm_cmd_running
= 1;
1216 ret
= usbduxsigma_submit_pwm_urb(dev
);
1218 devpriv
->pwm_cmd_running
= 0;
1225 static void usbduxsigma_pwm_pattern(struct comedi_device
*dev
,
1226 struct comedi_subdevice
*s
,
1231 struct usbduxsigma_private
*devpriv
= dev
->private;
1232 char pwm_mask
= (1 << chan
); /* DIO bit for the PWM data */
1233 char sgn_mask
= (16 << chan
); /* DIO bit for the sign */
1234 char *buf
= (char *)(devpriv
->pwm_urb
->transfer_buffer
);
1235 int szbuf
= devpriv
->pwm_buf_sz
;
1238 for (i
= 0; i
< szbuf
; i
++) {
1252 static int usbduxsigma_pwm_write(struct comedi_device
*dev
,
1253 struct comedi_subdevice
*s
,
1254 struct comedi_insn
*insn
,
1257 unsigned int chan
= CR_CHAN(insn
->chanspec
);
1260 * It doesn't make sense to support more than one value here
1261 * because it would just overwrite the PWM buffer.
1267 * The sign is set via a special INSN only, this gives us 8 bits
1268 * for normal operation, sign is 0 by default.
1270 usbduxsigma_pwm_pattern(dev
, s
, chan
, data
[0], 0);
1275 static int usbduxsigma_pwm_config(struct comedi_device
*dev
,
1276 struct comedi_subdevice
*s
,
1277 struct comedi_insn
*insn
,
1280 struct usbduxsigma_private
*devpriv
= dev
->private;
1281 unsigned int chan
= CR_CHAN(insn
->chanspec
);
1284 case INSN_CONFIG_ARM
:
1286 * if not zero the PWM is limited to a certain time which is
1287 * not supported here
1291 return usbduxsigma_pwm_start(dev
, s
);
1292 case INSN_CONFIG_DISARM
:
1293 return usbduxsigma_pwm_cancel(dev
, s
);
1294 case INSN_CONFIG_GET_PWM_STATUS
:
1295 data
[1] = devpriv
->pwm_cmd_running
;
1297 case INSN_CONFIG_PWM_SET_PERIOD
:
1298 return usbduxsigma_pwm_period(dev
, s
, data
[1]);
1299 case INSN_CONFIG_PWM_GET_PERIOD
:
1300 data
[1] = devpriv
->pwm_period
;
1302 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
1305 * data[2] = sign (for a relay)
1307 usbduxsigma_pwm_pattern(dev
, s
, chan
, data
[1], (data
[2] != 0));
1309 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
1310 /* values are not kept in this driver, nothing to return */
1316 static int usbduxsigma_getstatusinfo(struct comedi_device
*dev
, int chan
)
1318 struct usbduxsigma_private
*devpriv
= dev
->private;
1326 sysred
= 0; /* ADC zero */
1329 sysred
= 1; /* ADC offset */
1332 sysred
= 4; /* VCC */
1335 sysred
= 8; /* temperature */
1338 sysred
= 16; /* gain */
1341 sysred
= 32; /* ref */
1345 devpriv
->dux_commands
[1] = 0x12; /* CONFIG0 */
1346 devpriv
->dux_commands
[2] = 0x80; /* CONFIG1: 2kHz sampling rate */
1347 devpriv
->dux_commands
[3] = 0x00; /* CONFIG3: diff. channels off */
1348 devpriv
->dux_commands
[4] = 0;
1349 devpriv
->dux_commands
[5] = 0;
1350 devpriv
->dux_commands
[6] = sysred
;
1351 ret
= usbbuxsigma_send_cmd(dev
, USBDUXSIGMA_SINGLE_AD_CMD
);
1355 ret
= usbduxsigma_receive_cmd(dev
, USBDUXSIGMA_SINGLE_AD_CMD
);
1359 /* 32 bits big endian from the A/D converter */
1360 val
= be32_to_cpu(*((uint32_t *)((devpriv
->insn_buf
)+1)));
1361 val
&= 0x00ffffff; /* strip status byte */
1362 val
^= 0x00800000; /* convert to unsigned */
1367 static int usbduxsigma_firmware_upload(struct comedi_device
*dev
,
1368 const u8
*data
, size_t size
,
1369 unsigned long context
)
1371 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
1379 if (size
> FIRMWARE_MAX_LEN
) {
1380 dev_err(dev
->class_dev
, "firmware binary too large for FX2\n");
1384 /* we generate a local buffer for the firmware */
1385 buf
= kmemdup(data
, size
, GFP_KERNEL
);
1389 /* we need a malloc'ed buffer for usb_control_msg() */
1390 tmp
= kmalloc(1, GFP_KERNEL
);
1396 /* stop the current firmware on the device */
1397 *tmp
= 1; /* 7f92 to one */
1398 ret
= usb_control_msg(usb
, usb_sndctrlpipe(usb
, 0),
1401 USBDUXSUB_CPUCS
, 0x0000,
1405 dev_err(dev
->class_dev
, "can not stop firmware\n");
1409 /* upload the new firmware to the device */
1410 ret
= usb_control_msg(usb
, usb_sndctrlpipe(usb
, 0),
1417 dev_err(dev
->class_dev
, "firmware upload failed\n");
1421 /* start the new firmware on the device */
1422 *tmp
= 0; /* 7f92 to zero */
1423 ret
= usb_control_msg(usb
, usb_sndctrlpipe(usb
, 0),
1426 USBDUXSUB_CPUCS
, 0x0000,
1430 dev_err(dev
->class_dev
, "can not start firmware\n");
1438 static int usbduxsigma_alloc_usb_buffers(struct comedi_device
*dev
)
1440 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
1441 struct usbduxsigma_private
*devpriv
= dev
->private;
1445 devpriv
->dux_commands
= kzalloc(SIZEOFDUXBUFFER
, GFP_KERNEL
);
1446 devpriv
->in_buf
= kzalloc(SIZEINBUF
, GFP_KERNEL
);
1447 devpriv
->insn_buf
= kzalloc(SIZEINSNBUF
, GFP_KERNEL
);
1448 devpriv
->ai_urbs
= kcalloc(devpriv
->n_ai_urbs
, sizeof(*urb
),
1450 devpriv
->ao_urbs
= kcalloc(devpriv
->n_ao_urbs
, sizeof(*urb
),
1452 if (!devpriv
->dux_commands
|| !devpriv
->in_buf
|| !devpriv
->insn_buf
||
1453 !devpriv
->ai_urbs
|| !devpriv
->ao_urbs
)
1456 for (i
= 0; i
< devpriv
->n_ai_urbs
; i
++) {
1457 /* one frame: 1ms */
1458 urb
= usb_alloc_urb(1, GFP_KERNEL
);
1461 devpriv
->ai_urbs
[i
] = urb
;
1463 /* will be filled later with a pointer to the comedi-device */
1464 /* and ONLY then the urb should be submitted */
1465 urb
->context
= NULL
;
1466 urb
->pipe
= usb_rcvisocpipe(usb
, 6);
1467 urb
->transfer_flags
= URB_ISO_ASAP
;
1468 urb
->transfer_buffer
= kzalloc(SIZEINBUF
, GFP_KERNEL
);
1469 if (!urb
->transfer_buffer
)
1471 urb
->complete
= usbduxsigma_ai_urb_complete
;
1472 urb
->number_of_packets
= 1;
1473 urb
->transfer_buffer_length
= SIZEINBUF
;
1474 urb
->iso_frame_desc
[0].offset
= 0;
1475 urb
->iso_frame_desc
[0].length
= SIZEINBUF
;
1478 for (i
= 0; i
< devpriv
->n_ao_urbs
; i
++) {
1479 /* one frame: 1ms */
1480 urb
= usb_alloc_urb(1, GFP_KERNEL
);
1483 devpriv
->ao_urbs
[i
] = urb
;
1485 /* will be filled later with a pointer to the comedi-device */
1486 /* and ONLY then the urb should be submitted */
1487 urb
->context
= NULL
;
1488 urb
->pipe
= usb_sndisocpipe(usb
, 2);
1489 urb
->transfer_flags
= URB_ISO_ASAP
;
1490 urb
->transfer_buffer
= kzalloc(SIZEOUTBUF
, GFP_KERNEL
);
1491 if (!urb
->transfer_buffer
)
1493 urb
->complete
= usbduxsigma_ao_urb_complete
;
1494 urb
->number_of_packets
= 1;
1495 urb
->transfer_buffer_length
= SIZEOUTBUF
;
1496 urb
->iso_frame_desc
[0].offset
= 0;
1497 urb
->iso_frame_desc
[0].length
= SIZEOUTBUF
;
1498 if (devpriv
->high_speed
)
1499 urb
->interval
= 8; /* uframes */
1501 urb
->interval
= 1; /* frames */
1504 if (devpriv
->pwm_buf_sz
) {
1505 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1508 devpriv
->pwm_urb
= urb
;
1510 urb
->transfer_buffer
= kzalloc(devpriv
->pwm_buf_sz
,
1512 if (!urb
->transfer_buffer
)
1519 static void usbduxsigma_free_usb_buffers(struct comedi_device
*dev
)
1521 struct usbduxsigma_private
*devpriv
= dev
->private;
1525 urb
= devpriv
->pwm_urb
;
1527 kfree(urb
->transfer_buffer
);
1530 if (devpriv
->ao_urbs
) {
1531 for (i
= 0; i
< devpriv
->n_ao_urbs
; i
++) {
1532 urb
= devpriv
->ao_urbs
[i
];
1534 kfree(urb
->transfer_buffer
);
1538 kfree(devpriv
->ao_urbs
);
1540 if (devpriv
->ai_urbs
) {
1541 for (i
= 0; i
< devpriv
->n_ai_urbs
; i
++) {
1542 urb
= devpriv
->ai_urbs
[i
];
1544 kfree(urb
->transfer_buffer
);
1548 kfree(devpriv
->ai_urbs
);
1550 kfree(devpriv
->insn_buf
);
1551 kfree(devpriv
->in_buf
);
1552 kfree(devpriv
->dux_commands
);
1555 static int usbduxsigma_auto_attach(struct comedi_device
*dev
,
1556 unsigned long context_unused
)
1558 struct usb_interface
*intf
= comedi_to_usb_interface(dev
);
1559 struct usb_device
*usb
= comedi_to_usb_dev(dev
);
1560 struct usbduxsigma_private
*devpriv
;
1561 struct comedi_subdevice
*s
;
1565 devpriv
= comedi_alloc_devpriv(dev
, sizeof(*devpriv
));
1569 sema_init(&devpriv
->sem
, 1);
1571 usb_set_intfdata(intf
, devpriv
);
1573 devpriv
->high_speed
= (usb
->speed
== USB_SPEED_HIGH
);
1574 if (devpriv
->high_speed
) {
1575 devpriv
->n_ai_urbs
= NUMOFINBUFFERSHIGH
;
1576 devpriv
->n_ao_urbs
= NUMOFOUTBUFFERSHIGH
;
1577 devpriv
->pwm_buf_sz
= 512;
1579 devpriv
->n_ai_urbs
= NUMOFINBUFFERSFULL
;
1580 devpriv
->n_ao_urbs
= NUMOFOUTBUFFERSFULL
;
1583 ret
= usbduxsigma_alloc_usb_buffers(dev
);
1587 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
1588 ret
= usb_set_interface(usb
, intf
->altsetting
->desc
.bInterfaceNumber
,
1591 dev_err(dev
->class_dev
,
1592 "could not set alternate setting 3 in high speed\n");
1596 ret
= comedi_load_firmware(dev
, &usb
->dev
, FIRMWARE
,
1597 usbduxsigma_firmware_upload
, 0);
1601 ret
= comedi_alloc_subdevices(dev
, (devpriv
->high_speed
) ? 4 : 3);
1605 /* Analog Input subdevice */
1606 s
= &dev
->subdevices
[0];
1607 dev
->read_subdev
= s
;
1608 s
->type
= COMEDI_SUBD_AI
;
1609 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
| SDF_CMD_READ
| SDF_LSAMPL
;
1610 s
->n_chan
= NUMCHANNELS
;
1611 s
->len_chanlist
= NUMCHANNELS
;
1612 s
->maxdata
= 0x00ffffff;
1613 s
->range_table
= &usbduxsigma_ai_range
;
1614 s
->insn_read
= usbduxsigma_ai_insn_read
;
1615 s
->do_cmdtest
= usbduxsigma_ai_cmdtest
;
1616 s
->do_cmd
= usbduxsigma_ai_cmd
;
1617 s
->cancel
= usbduxsigma_ai_cancel
;
1619 /* Analog Output subdevice */
1620 s
= &dev
->subdevices
[1];
1621 dev
->write_subdev
= s
;
1622 s
->type
= COMEDI_SUBD_AO
;
1623 s
->subdev_flags
= SDF_WRITABLE
| SDF_GROUND
| SDF_CMD_WRITE
;
1624 s
->n_chan
= USBDUXSIGMA_NUM_AO_CHAN
;
1625 s
->len_chanlist
= s
->n_chan
;
1626 s
->maxdata
= 0x00ff;
1627 s
->range_table
= &range_unipolar2_5
;
1628 s
->insn_write
= usbduxsigma_ao_insn_write
;
1629 s
->insn_read
= usbduxsigma_ao_insn_read
;
1630 s
->do_cmdtest
= usbduxsigma_ao_cmdtest
;
1631 s
->do_cmd
= usbduxsigma_ao_cmd
;
1632 s
->cancel
= usbduxsigma_ao_cancel
;
1634 /* Digital I/O subdevice */
1635 s
= &dev
->subdevices
[2];
1636 s
->type
= COMEDI_SUBD_DIO
;
1637 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
1640 s
->range_table
= &range_digital
;
1641 s
->insn_bits
= usbduxsigma_dio_insn_bits
;
1642 s
->insn_config
= usbduxsigma_dio_insn_config
;
1644 if (devpriv
->high_speed
) {
1645 /* Timer / pwm subdevice */
1646 s
= &dev
->subdevices
[3];
1647 s
->type
= COMEDI_SUBD_PWM
;
1648 s
->subdev_flags
= SDF_WRITABLE
| SDF_PWM_HBRIDGE
;
1650 s
->maxdata
= devpriv
->pwm_buf_sz
;
1651 s
->insn_write
= usbduxsigma_pwm_write
;
1652 s
->insn_config
= usbduxsigma_pwm_config
;
1654 usbduxsigma_pwm_period(dev
, s
, PWM_DEFAULT_PERIOD
);
1657 offset
= usbduxsigma_getstatusinfo(dev
, 0);
1659 dev_err(dev
->class_dev
,
1660 "Communication to USBDUXSIGMA failed! Check firmware and cabling\n");
1662 dev_info(dev
->class_dev
, "attached, ADC_zero = %x\n", offset
);
1667 static void usbduxsigma_detach(struct comedi_device
*dev
)
1669 struct usb_interface
*intf
= comedi_to_usb_interface(dev
);
1670 struct usbduxsigma_private
*devpriv
= dev
->private;
1672 usb_set_intfdata(intf
, NULL
);
1677 down(&devpriv
->sem
);
1679 /* force unlink all urbs */
1680 usbduxsigma_ai_stop(dev
, 1);
1681 usbduxsigma_ao_stop(dev
, 1);
1682 usbduxsigma_pwm_stop(dev
, 1);
1684 usbduxsigma_free_usb_buffers(dev
);
1689 static struct comedi_driver usbduxsigma_driver
= {
1690 .driver_name
= "usbduxsigma",
1691 .module
= THIS_MODULE
,
1692 .auto_attach
= usbduxsigma_auto_attach
,
1693 .detach
= usbduxsigma_detach
,
1696 static int usbduxsigma_usb_probe(struct usb_interface
*intf
,
1697 const struct usb_device_id
*id
)
1699 return comedi_usb_auto_config(intf
, &usbduxsigma_driver
, 0);
1702 static const struct usb_device_id usbduxsigma_usb_table
[] = {
1703 { USB_DEVICE(0x13d8, 0x0020) },
1704 { USB_DEVICE(0x13d8, 0x0021) },
1705 { USB_DEVICE(0x13d8, 0x0022) },
1708 MODULE_DEVICE_TABLE(usb
, usbduxsigma_usb_table
);
1710 static struct usb_driver usbduxsigma_usb_driver
= {
1711 .name
= "usbduxsigma",
1712 .probe
= usbduxsigma_usb_probe
,
1713 .disconnect
= comedi_usb_auto_unconfig
,
1714 .id_table
= usbduxsigma_usb_table
,
1716 module_comedi_usb_driver(usbduxsigma_driver
, usbduxsigma_usb_driver
);
1718 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1719 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com");
1720 MODULE_LICENSE("GPL");
1721 MODULE_FIRMWARE(FIRMWARE
);