2 comedi/drivers/usbdux.c
3 Copyright (C) 2011 Bernd Porr, Bernd.Porr@f2s.com
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Description: University of Stirling USB DAQ & INCITE Technology Limited
23 Devices: [ITL] USB-DUX (usbduxsigma.o)
24 Author: Bernd Porr <BerndPorr@f2s.com>
29 * I must give credit here to Chris Baugher who
30 * wrote the driver for AT-MIO-16d. I used some parts of this
31 * driver. I also must give credits to David Brownell
32 * who supported me with the USB development.
34 * Note: the raw data from the A/D converter is 24 bit big endian
35 * anything else is little endian to/from the dux board
39 * 0.1: initial version
40 * 0.2: all basic functions implemented, digital I/O only for one port
41 * 0.3: proper vendor ID and driver name
42 * 0.4: fixed D/A voltage range
43 * 0.5: various bug fixes, health check at startup
44 * 0.6: corrected wrong input range
47 /* generates loads of debug info */
48 /* #define NOISY_DUX_DEBUGBUG */
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/input.h>
55 #include <linux/usb.h>
56 #include <linux/fcntl.h>
57 #include <linux/compiler.h>
58 #include <linux/firmware.h>
59 #include "comedi_fc.h"
60 #include "../comedidev.h"
62 /* timeout for the USB-transfer in ms*/
63 #define BULK_TIMEOUT 1000
65 /* constants for "firmware" upload and download */
66 #define FIRMWARE "usbduxsigma_firmware.bin"
67 #define USBDUXSUB_FIRMWARE 0xA0
68 #define VENDOR_DIR_IN 0xC0
69 #define VENDOR_DIR_OUT 0x40
71 /* internal addresses of the 8051 processor */
72 #define USBDUXSUB_CPUCS 0xE600
75 * the minor device number, major is 180 only for debugging purposes and to
76 * upload special firmware (programming the eeprom etc) which is not
77 * compatible with the comedi framwork
79 #define USBDUXSUB_MINOR 32
81 /* max lenghth of the transfer-buffer for software upload */
84 /* Input endpoint number: ISO/IRQ */
87 /* Output endpoint number: ISO/IRQ */
90 /* This EP sends DUX commands to USBDUX */
91 #define COMMAND_OUT_EP 1
93 /* This EP receives the DUX commands from USBDUX */
94 #define COMMAND_IN_EP 8
96 /* Output endpoint for PWM */
99 /* 300Hz max frequ under PWM */
100 #define MIN_PWM_PERIOD ((long)(1E9/300))
102 /* Default PWM frequency */
103 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
105 /* Number of channels (16 AD and offset)*/
106 #define NUMCHANNELS 16
108 /* Size of one A/D value */
109 #define SIZEADIN ((sizeof(int32_t)))
112 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
115 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
118 #define SIZEINSNBUF 16
120 /* Number of DA channels */
121 #define NUMOUTCHANNELS 8
123 /* size of one value for the D/A converter: channel and value */
124 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(int16_t)))
127 * Size of the output-buffer in bytes
128 * Actually only the first 4 triplets are used but for the
129 * high speed mode we need to pad it to 8 (microframes).
131 #define SIZEOUTBUF ((8*SIZEDAOUT))
134 * Size of the buffer for the dux commands: just now max size is determined
135 * by the analogue out + command byte + panic bytes...
137 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
139 /* Number of in-URBs which receive the data: min=2 */
140 #define NUMOFINBUFFERSFULL 5
142 /* Number of out-URBs which send the data: min=2 */
143 #define NUMOFOUTBUFFERSFULL 5
145 /* Number of in-URBs which receive the data: min=5 */
146 /* must have more buffers due to buggy USB ctr */
147 #define NUMOFINBUFFERSHIGH 10
149 /* Number of out-URBs which send the data: min=5 */
150 /* must have more buffers due to buggy USB ctr */
151 #define NUMOFOUTBUFFERSHIGH 10
153 /* Total number of usbdux devices */
156 /* Analogue in subdevice */
159 /* Analogue out subdevice */
165 /* timer aka pwm output */
168 /* number of retries to get the right dux command */
171 /**************************************************/
172 /* comedi constants */
173 static const struct comedi_lrange range_usbdux_ai_range
= { 1, {
179 static const struct comedi_lrange range_usbdux_ao_range
= { 1, {
186 * private structure of one subdevice
190 * This is the structure which holds all the data of
191 * this driver one sub device just now: A/D
196 /* is it associated with a subdevice? */
198 /* pointer to the usb-device */
199 struct usb_device
*usbdev
;
200 /* actual number of in-buffers */
202 /* actual number of out-buffers */
204 /* ISO-transfer handling: buffers */
207 /* pwm-transfer handling */
210 unsigned int pwmPeriod
;
211 /* PWM internal delay for the GPIF in the FX2 */
213 /* size of the PWM buffer which holds the bit pattern */
215 /* input buffer for the ISO-transfer */
217 /* input buffer for single insn */
219 /* output buffer for single DA outputs */
221 /* interface number */
223 /* interface structure in 2.6 */
224 struct usb_interface
*interface
;
225 /* comedi device for the interrupt context */
226 struct comedi_device
*comedidev
;
227 /* is it USB_SPEED_HIGH or not? */
228 short int high_speed
;
229 /* asynchronous command is running */
230 short int ai_cmd_running
;
231 short int ao_cmd_running
;
233 short int pwm_cmd_running
;
234 /* continuous acquisition */
235 short int ai_continuous
;
236 short int ao_continuous
;
237 /* number of samples to acquire */
240 /* time between samples in units of the timer */
241 unsigned int ai_timer
;
242 unsigned int ao_timer
;
243 /* counter between acquisitions */
244 unsigned int ai_counter
;
245 unsigned int ao_counter
;
246 /* interval in frames/uframes */
247 unsigned int ai_interval
;
249 uint8_t *dac_commands
;
251 uint8_t *dux_commands
;
252 struct semaphore sem
;
256 * The pointer to the private usb-data of the driver is also the private data
257 * for the comedi-device. This has to be global as the usb subsystem needs
258 * global variables. The other reason is that this structure must be there
259 * _before_ any comedi command is issued. The usb subsystem must be initialised
260 * before comedi can access it.
262 static struct usbduxsub usbduxsub
[NUMUSBDUX
];
264 static DEFINE_SEMAPHORE(start_stop_sem
);
267 * Stops the data acquision
268 * It should be safe to call this function from any context
270 static int usbduxsub_unlink_InURBs(struct usbduxsub
*usbduxsub_tmp
)
275 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbIn
) {
276 for (i
= 0; i
< usbduxsub_tmp
->numOfInBuffers
; i
++) {
277 if (usbduxsub_tmp
->urbIn
[i
]) {
278 /* We wait here until all transfers have been
280 usb_kill_urb(usbduxsub_tmp
->urbIn
[i
]);
282 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
283 "comedi: usbdux: unlinked InURB %d, err=%d\n",
291 * This will stop a running acquisition operation
292 * Is called from within this driver from both the
293 * interrupt context and from comedi
295 static int usbdux_ai_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
299 if (!this_usbduxsub
) {
300 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
303 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ai_stop\n");
307 ret
= usbduxsub_unlink_InURBs(this_usbduxsub
);
310 this_usbduxsub
->ai_cmd_running
= 0;
316 * This will cancel a running acquisition operation.
317 * This is called by comedi but never from inside the driver.
319 static int usbdux_ai_cancel(struct comedi_device
*dev
,
320 struct comedi_subdevice
*s
)
322 struct usbduxsub
*this_usbduxsub
;
325 /* force unlink of all urbs */
326 this_usbduxsub
= dev
->private;
330 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ai_cancel\n");
332 /* prevent other CPUs from submitting new commands just now */
333 down(&this_usbduxsub
->sem
);
334 if (!(this_usbduxsub
->probed
)) {
335 up(&this_usbduxsub
->sem
);
338 /* unlink only if the urb really has been submitted */
339 res
= usbdux_ai_stop(this_usbduxsub
, this_usbduxsub
->ai_cmd_running
);
340 up(&this_usbduxsub
->sem
);
344 /* analogue IN - interrupt service routine */
345 static void usbduxsub_ai_IsocIrq(struct urb
*urb
)
348 struct usbduxsub
*this_usbduxsub
;
349 struct comedi_device
*this_comedidev
;
350 struct comedi_subdevice
*s
;
352 unsigned int dio_state
;
354 /* the context variable points to the comedi device */
355 this_comedidev
= urb
->context
;
356 /* the private structure of the subdevice is struct usbduxsub */
357 this_usbduxsub
= this_comedidev
->private;
358 /* subdevice which is the AD converter */
359 s
= &this_comedidev
->subdevices
[SUBDEV_AD
];
361 /* first we test if something unusual has just happened */
362 switch (urb
->status
) {
364 /* copy the result in the transfer buffer */
365 memcpy(this_usbduxsub
->inBuffer
,
366 urb
->transfer_buffer
, SIZEINBUF
);
369 /* error in the ISOchronous data */
370 /* we don't copy the data into the transfer buffer */
371 /* and recycle the last data byte */
372 dev_dbg(&urb
->dev
->dev
,
373 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
374 this_usbduxsub
->comedidev
->minor
);
382 /* happens after an unlink command */
383 if (this_usbduxsub
->ai_cmd_running
) {
384 /* we are still running a command */
385 /* tell this comedi */
386 s
->async
->events
|= COMEDI_CB_EOA
;
387 s
->async
->events
|= COMEDI_CB_ERROR
;
388 comedi_event(this_usbduxsub
->comedidev
, s
);
389 /* stop the transfer w/o unlink */
390 usbdux_ai_stop(this_usbduxsub
, 0);
395 /* a real error on the bus */
396 /* pass error to comedi if we are really running a command */
397 if (this_usbduxsub
->ai_cmd_running
) {
398 dev_err(&urb
->dev
->dev
,
399 "Non-zero urb status received in ai intr "
400 "context: %d\n", urb
->status
);
401 s
->async
->events
|= COMEDI_CB_EOA
;
402 s
->async
->events
|= COMEDI_CB_ERROR
;
403 comedi_event(this_usbduxsub
->comedidev
, s
);
404 /* don't do an unlink here */
405 usbdux_ai_stop(this_usbduxsub
, 0);
411 * at this point we are reasonably sure that nothing dodgy has happened
412 * are we running a command?
414 if (unlikely((!(this_usbduxsub
->ai_cmd_running
)))) {
416 * not running a command, do not continue execution if no
417 * asynchronous command is running in particular not resubmit
422 urb
->dev
= this_usbduxsub
->usbdev
;
424 /* resubmit the urb */
425 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
426 if (unlikely(err
< 0)) {
427 dev_err(&urb
->dev
->dev
,
428 "comedi_: urb resubmit failed in int-context!"
431 if (err
== -EL2NSYNC
)
432 dev_err(&urb
->dev
->dev
,
433 "buggy USB host controller or bug in IRQ "
435 s
->async
->events
|= COMEDI_CB_EOA
;
436 s
->async
->events
|= COMEDI_CB_ERROR
;
437 comedi_event(this_usbduxsub
->comedidev
, s
);
438 /* don't do an unlink here */
439 usbdux_ai_stop(this_usbduxsub
, 0);
443 /* get the state of the dio pins to allow external trigger */
444 dio_state
= be32_to_cpu(this_usbduxsub
->inBuffer
[0]);
446 this_usbduxsub
->ai_counter
--;
447 if (likely(this_usbduxsub
->ai_counter
> 0))
450 /* timer zero, transfer measurements to comedi */
451 this_usbduxsub
->ai_counter
= this_usbduxsub
->ai_timer
;
453 /* test, if we transmit only a fixed number of samples */
454 if (!(this_usbduxsub
->ai_continuous
)) {
455 /* not continuous, fixed number of samples */
456 this_usbduxsub
->ai_sample_count
--;
457 /* all samples received? */
458 if (this_usbduxsub
->ai_sample_count
< 0) {
459 /* prevent a resubmit next time */
460 usbdux_ai_stop(this_usbduxsub
, 0);
461 /* say comedi that the acquistion is over */
462 s
->async
->events
|= COMEDI_CB_EOA
;
463 comedi_event(this_usbduxsub
->comedidev
, s
);
467 /* get the data from the USB bus and hand it over to comedi */
468 n
= s
->async
->cmd
.chanlist_len
;
469 for (i
= 0; i
< n
; i
++) {
470 /* transfer data, note first byte is the DIO state */
471 v
= be32_to_cpu(this_usbduxsub
->inBuffer
[i
+1]);
472 /* strip status byte */
474 /* convert to unsigned */
476 /* write the byte to the buffer */
477 err
= cfc_write_array_to_buffer(s
, &v
, sizeof(uint32_t));
478 if (unlikely(err
== 0)) {
479 /* buffer overflow */
480 usbdux_ai_stop(this_usbduxsub
, 0);
484 /* tell comedi that data is there */
485 s
->async
->events
|= COMEDI_CB_BLOCK
| COMEDI_CB_EOS
;
486 comedi_event(this_usbduxsub
->comedidev
, s
);
489 static int usbduxsub_unlink_OutURBs(struct usbduxsub
*usbduxsub_tmp
)
494 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbOut
) {
495 for (i
= 0; i
< usbduxsub_tmp
->numOfOutBuffers
; i
++) {
496 if (usbduxsub_tmp
->urbOut
[i
])
497 usb_kill_urb(usbduxsub_tmp
->urbOut
[i
]);
499 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
500 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
507 /* This will cancel a running acquisition operation
510 static int usbdux_ao_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
516 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ao_cancel\n");
519 ret
= usbduxsub_unlink_OutURBs(this_usbduxsub
);
521 this_usbduxsub
->ao_cmd_running
= 0;
526 /* force unlink, is called by comedi */
527 static int usbdux_ao_cancel(struct comedi_device
*dev
,
528 struct comedi_subdevice
*s
)
530 struct usbduxsub
*this_usbduxsub
= dev
->private;
536 /* prevent other CPUs from submitting a command just now */
537 down(&this_usbduxsub
->sem
);
538 if (!(this_usbduxsub
->probed
)) {
539 up(&this_usbduxsub
->sem
);
542 /* unlink only if it is really running */
543 res
= usbdux_ao_stop(this_usbduxsub
, this_usbduxsub
->ao_cmd_running
);
544 up(&this_usbduxsub
->sem
);
548 static void usbduxsub_ao_IsocIrq(struct urb
*urb
)
552 struct usbduxsub
*this_usbduxsub
;
553 struct comedi_device
*this_comedidev
;
554 struct comedi_subdevice
*s
;
556 /* the context variable points to the subdevice */
557 this_comedidev
= urb
->context
;
558 /* the private structure of the subdevice is struct usbduxsub */
559 this_usbduxsub
= this_comedidev
->private;
561 s
= &this_comedidev
->subdevices
[SUBDEV_DA
];
563 switch (urb
->status
) {
572 /* after an unlink command, unplug, ... etc */
573 /* no unlink needed here. Already shutting down. */
574 if (this_usbduxsub
->ao_cmd_running
) {
575 s
->async
->events
|= COMEDI_CB_EOA
;
576 comedi_event(this_usbduxsub
->comedidev
, s
);
577 usbdux_ao_stop(this_usbduxsub
, 0);
583 if (this_usbduxsub
->ao_cmd_running
) {
584 dev_err(&urb
->dev
->dev
,
585 "comedi_: Non-zero urb status received in ao "
586 "intr context: %d\n", urb
->status
);
587 s
->async
->events
|= COMEDI_CB_ERROR
;
588 s
->async
->events
|= COMEDI_CB_EOA
;
589 comedi_event(this_usbduxsub
->comedidev
, s
);
590 /* we do an unlink if we are in the high speed mode */
591 usbdux_ao_stop(this_usbduxsub
, 0);
596 /* are we actually running? */
597 if (!(this_usbduxsub
->ao_cmd_running
))
600 /* normal operation: executing a command in this subdevice */
601 this_usbduxsub
->ao_counter
--;
602 if ((int)this_usbduxsub
->ao_counter
<= 0) {
604 this_usbduxsub
->ao_counter
= this_usbduxsub
->ao_timer
;
606 /* handle non continuous acquisition */
607 if (!(this_usbduxsub
->ao_continuous
)) {
608 /* fixed number of samples */
609 this_usbduxsub
->ao_sample_count
--;
610 if (this_usbduxsub
->ao_sample_count
< 0) {
611 /* all samples transmitted */
612 usbdux_ao_stop(this_usbduxsub
, 0);
613 s
->async
->events
|= COMEDI_CB_EOA
;
614 comedi_event(this_usbduxsub
->comedidev
, s
);
615 /* no resubmit of the urb */
619 /* transmit data to the USB bus */
620 ((uint8_t *) (urb
->transfer_buffer
))[0] =
621 s
->async
->cmd
.chanlist_len
;
622 for (i
= 0; i
< s
->async
->cmd
.chanlist_len
; i
++) {
624 if (i
>= NUMOUTCHANNELS
)
627 /* pointer to the DA */
629 (&(((uint8_t *) urb
->transfer_buffer
)[i
* 2 + 1]));
630 /* get the data from comedi */
631 ret
= comedi_buf_get(s
->async
, &temp
);
633 datap
[1] = this_usbduxsub
->dac_commands
[i
];
634 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
635 /* datap[0],datap[1],datap[2]); */
637 dev_err(&urb
->dev
->dev
,
638 "comedi: buffer underflow\n");
639 s
->async
->events
|= COMEDI_CB_EOA
;
640 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
642 /* transmit data to comedi */
643 s
->async
->events
|= COMEDI_CB_BLOCK
;
644 comedi_event(this_usbduxsub
->comedidev
, s
);
647 urb
->transfer_buffer_length
= SIZEOUTBUF
;
648 urb
->dev
= this_usbduxsub
->usbdev
;
650 if (this_usbduxsub
->ao_cmd_running
) {
651 if (this_usbduxsub
->high_speed
) {
658 urb
->number_of_packets
= 1;
659 urb
->iso_frame_desc
[0].offset
= 0;
660 urb
->iso_frame_desc
[0].length
= SIZEOUTBUF
;
661 urb
->iso_frame_desc
[0].status
= 0;
662 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
664 dev_err(&urb
->dev
->dev
,
665 "comedi_: ao urb resubm failed in int-cont. "
668 dev_err(&urb
->dev
->dev
,
669 "buggy USB host controller or bug in "
672 s
->async
->events
|= COMEDI_CB_EOA
;
673 s
->async
->events
|= COMEDI_CB_ERROR
;
674 comedi_event(this_usbduxsub
->comedidev
, s
);
675 /* don't do an unlink here */
676 usbdux_ao_stop(this_usbduxsub
, 0);
681 static int usbduxsub_start(struct usbduxsub
*usbduxsub
)
684 uint8_t local_transfer_buffer
[16];
687 local_transfer_buffer
[0] = 0;
688 errcode
= usb_control_msg(usbduxsub
->usbdev
,
689 /* create a pipe for a control transfer */
690 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
691 /* bRequest, "Firmware" */
699 /* address of the transfer buffer */
700 local_transfer_buffer
,
706 dev_err(&usbduxsub
->interface
->dev
,
707 "comedi_: control msg failed (start)\n");
713 static int usbduxsub_stop(struct usbduxsub
*usbduxsub
)
717 uint8_t local_transfer_buffer
[16];
720 local_transfer_buffer
[0] = 1;
721 errcode
= usb_control_msg(usbduxsub
->usbdev
,
722 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
723 /* bRequest, "Firmware" */
730 0x0000, local_transfer_buffer
,
736 dev_err(&usbduxsub
->interface
->dev
,
737 "comedi_: control msg failed (stop)\n");
743 static int usbduxsub_upload(struct usbduxsub
*usbduxsub
,
744 uint8_t *local_transfer_buffer
,
745 unsigned int startAddr
, unsigned int len
)
749 errcode
= usb_control_msg(usbduxsub
->usbdev
,
750 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
751 /* brequest, firmware */
759 /* our local safe buffer */
760 local_transfer_buffer
,
765 dev_dbg(&usbduxsub
->interface
->dev
, "comedi_: result=%d\n", errcode
);
767 dev_err(&usbduxsub
->interface
->dev
,
768 "comedi_: upload failed\n");
774 /* the FX2LP has twice as much as the standard FX2 */
775 #define FIRMWARE_MAX_LEN 0x4000
777 static int firmwareUpload(struct usbduxsub
*usbduxsub
,
778 const u8
*firmwareBinary
, int sizeFirmware
)
786 if (sizeFirmware
> FIRMWARE_MAX_LEN
) {
787 dev_err(&usbduxsub
->interface
->dev
,
788 "usbduxsigma firmware binary it too large for FX2.\n");
792 /* we generate a local buffer for the firmware */
793 fwBuf
= kmemdup(firmwareBinary
, sizeFirmware
, GFP_KERNEL
);
795 dev_err(&usbduxsub
->interface
->dev
,
796 "comedi_: mem alloc for firmware failed\n");
800 ret
= usbduxsub_stop(usbduxsub
);
802 dev_err(&usbduxsub
->interface
->dev
,
803 "comedi_: can not stop firmware\n");
808 ret
= usbduxsub_upload(usbduxsub
, fwBuf
, 0, sizeFirmware
);
810 dev_err(&usbduxsub
->interface
->dev
,
811 "comedi_: firmware upload failed\n");
815 ret
= usbduxsub_start(usbduxsub
);
817 dev_err(&usbduxsub
->interface
->dev
,
818 "comedi_: can not start firmware\n");
826 static int usbduxsub_submit_InURBs(struct usbduxsub
*usbduxsub
)
833 /* Submit all URBs and start the transfer on the bus */
834 for (i
= 0; i
< usbduxsub
->numOfInBuffers
; i
++) {
835 /* in case of a resubmission after an unlink... */
836 usbduxsub
->urbIn
[i
]->interval
= usbduxsub
->ai_interval
;
837 usbduxsub
->urbIn
[i
]->context
= usbduxsub
->comedidev
;
838 usbduxsub
->urbIn
[i
]->dev
= usbduxsub
->usbdev
;
839 usbduxsub
->urbIn
[i
]->status
= 0;
840 usbduxsub
->urbIn
[i
]->transfer_flags
= URB_ISO_ASAP
;
841 dev_dbg(&usbduxsub
->interface
->dev
,
842 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
843 usbduxsub
->comedidev
->minor
, i
,
844 (usbduxsub
->urbIn
[i
]->context
),
845 (usbduxsub
->urbIn
[i
]->dev
),
846 (usbduxsub
->urbIn
[i
]->interval
));
847 errFlag
= usb_submit_urb(usbduxsub
->urbIn
[i
], GFP_ATOMIC
);
849 dev_err(&usbduxsub
->interface
->dev
,
850 "comedi_: ai: usb_submit_urb(%d) error %d\n",
858 static int usbduxsub_submit_OutURBs(struct usbduxsub
*usbduxsub
)
865 for (i
= 0; i
< usbduxsub
->numOfOutBuffers
; i
++) {
866 dev_dbg(&usbduxsub
->interface
->dev
,
867 "comedi_: submitting out-urb[%d]\n", i
);
868 /* in case of a resubmission after an unlink... */
869 usbduxsub
->urbOut
[i
]->context
= usbduxsub
->comedidev
;
870 usbduxsub
->urbOut
[i
]->dev
= usbduxsub
->usbdev
;
871 usbduxsub
->urbOut
[i
]->status
= 0;
872 usbduxsub
->urbOut
[i
]->transfer_flags
= URB_ISO_ASAP
;
873 errFlag
= usb_submit_urb(usbduxsub
->urbOut
[i
], GFP_ATOMIC
);
875 dev_err(&usbduxsub
->interface
->dev
,
876 "comedi_: ao: usb_submit_urb(%d) error %d\n",
884 static int chanToInterval(int nChannels
)
896 static int usbdux_ai_cmdtest(struct comedi_device
*dev
,
897 struct comedi_subdevice
*s
,
898 struct comedi_cmd
*cmd
)
900 struct usbduxsub
*this_usbduxsub
= dev
->private;
902 unsigned int tmpTimer
;
904 if (!(this_usbduxsub
->probed
))
907 dev_dbg(&this_usbduxsub
->interface
->dev
,
908 "comedi%d: usbdux_ai_cmdtest\n", dev
->minor
);
910 /* Step 1 : check if triggers are trivially valid */
912 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_NOW
| TRIG_INT
);
913 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, TRIG_TIMER
);
914 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
915 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
916 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
921 /* Step 2a : make sure trigger sources are unique */
923 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
924 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
926 /* Step 2b : and mutually compatible */
931 /* step 3: make sure arguments are trivially compatible */
932 if (cmd
->start_arg
!= 0) {
937 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
938 /* internal trigger */
939 if (cmd
->scan_begin_arg
!= 0) {
940 cmd
->scan_begin_arg
= 0;
945 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
946 if (this_usbduxsub
->high_speed
) {
948 * In high speed mode microframes are possible.
949 * However, during one microframe we can roughly
950 * sample two channels. Thus, the more channels
951 * are in the channel list the more time we need.
953 i
= chanToInterval(cmd
->chanlist_len
);
954 if (cmd
->scan_begin_arg
< (1000000 / 8 * i
)) {
955 cmd
->scan_begin_arg
= 1000000 / 8 * i
;
958 /* now calc the real sampling rate with all the
961 ((unsigned int)(cmd
->scan_begin_arg
/ 125000)) *
963 if (cmd
->scan_begin_arg
!= tmpTimer
) {
964 cmd
->scan_begin_arg
= tmpTimer
;
969 /* 1kHz scans every USB frame */
970 if (cmd
->scan_begin_arg
< 1000000) {
971 cmd
->scan_begin_arg
= 1000000;
975 * calc the real sampling rate with the rounding errors
977 tmpTimer
= ((unsigned int)(cmd
->scan_begin_arg
/
979 if (cmd
->scan_begin_arg
!= tmpTimer
) {
980 cmd
->scan_begin_arg
= tmpTimer
;
985 /* the same argument */
986 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
987 cmd
->scan_end_arg
= cmd
->chanlist_len
;
991 if (cmd
->stop_src
== TRIG_COUNT
) {
992 /* any count is allowed */
995 if (cmd
->stop_arg
!= 0) {
1008 * creates the ADC command for the MAX1271
1009 * range is the range value from comedi
1011 static void create_adc_command(unsigned int chan
,
1016 (*muxsg0
) = (*muxsg0
) | (1 << chan
);
1018 (*muxsg1
) = (*muxsg1
) | (1 << (chan
-8));
1022 /* bulk transfers to usbdux */
1024 #define SENDADCOMMANDS 0
1025 #define SENDDACOMMANDS 1
1026 #define SENDDIOCONFIGCOMMAND 2
1027 #define SENDDIOBITSCOMMAND 3
1028 #define SENDSINGLEAD 4
1030 #define SENDPWMOFF 8
1032 static int send_dux_commands(struct usbduxsub
*this_usbduxsub
, int cmd_type
)
1036 this_usbduxsub
->dux_commands
[0] = cmd_type
;
1037 #ifdef NOISY_DUX_DEBUGBUG
1038 printk(KERN_DEBUG
"comedi%d: usbdux: dux_commands: ",
1039 this_usbduxsub
->comedidev
->minor
);
1040 for (result
= 0; result
< SIZEOFDUXBUFFER
; result
++)
1041 printk(" %02x", this_usbduxsub
->dux_commands
[result
]);
1044 result
= usb_bulk_msg(this_usbduxsub
->usbdev
,
1045 usb_sndbulkpipe(this_usbduxsub
->usbdev
,
1047 this_usbduxsub
->dux_commands
, SIZEOFDUXBUFFER
,
1048 &nsent
, BULK_TIMEOUT
);
1050 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1051 "could not transmit dux_command to the usb-device, "
1052 "err=%d\n", this_usbduxsub
->comedidev
->minor
, result
);
1057 static int receive_dux_commands(struct usbduxsub
*this_usbduxsub
, int command
)
1059 int result
= (-EFAULT
);
1063 for (i
= 0; i
< RETRIES
; i
++) {
1064 result
= usb_bulk_msg(this_usbduxsub
->usbdev
,
1065 usb_rcvbulkpipe(this_usbduxsub
->usbdev
,
1067 this_usbduxsub
->insnBuffer
, SIZEINSNBUF
,
1068 &nrec
, BULK_TIMEOUT
);
1070 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1071 "insn: USB error %d "
1072 "while receiving DUX command"
1073 "\n", this_usbduxsub
->comedidev
->minor
,
1077 if (this_usbduxsub
->insnBuffer
[0] == command
)
1080 /* this is only reached if the data has been requested a couple of
1082 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: insn: "
1083 "wrong data returned from firmware: want %d, got %d.\n",
1084 this_usbduxsub
->comedidev
->minor
, command
,
1085 this_usbduxsub
->insnBuffer
[0]);
1089 static int usbdux_ai_inttrig(struct comedi_device
*dev
,
1090 struct comedi_subdevice
*s
, unsigned int trignum
)
1093 struct usbduxsub
*this_usbduxsub
= dev
->private;
1094 if (!this_usbduxsub
)
1097 down(&this_usbduxsub
->sem
);
1098 if (!(this_usbduxsub
->probed
)) {
1099 up(&this_usbduxsub
->sem
);
1102 dev_dbg(&this_usbduxsub
->interface
->dev
,
1103 "comedi%d: usbdux_ai_inttrig\n", dev
->minor
);
1106 dev_err(&this_usbduxsub
->interface
->dev
,
1107 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1109 up(&this_usbduxsub
->sem
);
1112 if (!(this_usbduxsub
->ai_cmd_running
)) {
1113 this_usbduxsub
->ai_cmd_running
= 1;
1114 ret
= usbduxsub_submit_InURBs(this_usbduxsub
);
1116 dev_err(&this_usbduxsub
->interface
->dev
,
1117 "comedi%d: usbdux_ai_inttrig: "
1118 "urbSubmit: err=%d\n", dev
->minor
, ret
);
1119 this_usbduxsub
->ai_cmd_running
= 0;
1120 up(&this_usbduxsub
->sem
);
1123 s
->async
->inttrig
= NULL
;
1125 dev_err(&this_usbduxsub
->interface
->dev
,
1126 "comedi%d: ai_inttrig but acqu is already running\n",
1129 up(&this_usbduxsub
->sem
);
1133 static int usbdux_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1135 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
1138 struct usbduxsub
*this_usbduxsub
= dev
->private;
1144 if (!this_usbduxsub
)
1147 dev_dbg(&this_usbduxsub
->interface
->dev
,
1148 "comedi%d: usbdux_ai_cmd\n", dev
->minor
);
1150 /* block other CPUs from starting an ai_cmd */
1151 down(&this_usbduxsub
->sem
);
1153 if (!(this_usbduxsub
->probed
)) {
1154 up(&this_usbduxsub
->sem
);
1157 if (this_usbduxsub
->ai_cmd_running
) {
1158 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1159 "ai_cmd not possible. Another ai_cmd is running.\n",
1161 up(&this_usbduxsub
->sem
);
1164 /* set current channel of the running acquisition to zero */
1165 s
->async
->cur_chan
= 0;
1167 /* first the number of channels per time step */
1168 this_usbduxsub
->dux_commands
[1] = cmd
->chanlist_len
;
1171 this_usbduxsub
->dux_commands
[2] = 0x12;
1173 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
1174 this_usbduxsub
->dux_commands
[3] = 0x03;
1176 /* CONFIG3: differential channels off */
1177 this_usbduxsub
->dux_commands
[4] = 0x00;
1179 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
1180 chan
= CR_CHAN(cmd
->chanlist
[i
]);
1181 create_adc_command(chan
, &muxsg0
, &muxsg1
);
1182 if (i
>= NUMCHANNELS
) {
1183 dev_err(&this_usbduxsub
->interface
->dev
,
1184 "comedi%d: channel list too long\n",
1189 this_usbduxsub
->dux_commands
[5] = muxsg0
;
1190 this_usbduxsub
->dux_commands
[6] = muxsg1
;
1191 this_usbduxsub
->dux_commands
[7] = sysred
;
1193 dev_dbg(&this_usbduxsub
->interface
->dev
,
1194 "comedi %d: sending commands to the usb device: size=%u\n",
1195 dev
->minor
, NUMCHANNELS
);
1197 result
= send_dux_commands(this_usbduxsub
, SENDADCOMMANDS
);
1199 up(&this_usbduxsub
->sem
);
1203 if (this_usbduxsub
->high_speed
) {
1205 * every 2 channels get a time window of 125us. Thus, if we
1206 * sample all 16 channels we need 1ms. If we sample only one
1207 * channel we need only 125us
1209 this_usbduxsub
->ai_interval
=
1210 chanToInterval(cmd
->chanlist_len
);
1211 this_usbduxsub
->ai_timer
= cmd
->scan_begin_arg
/ (125000 *
1215 /* interval always 1ms */
1216 this_usbduxsub
->ai_interval
= 1;
1217 this_usbduxsub
->ai_timer
= cmd
->scan_begin_arg
/ 1000000;
1219 if (this_usbduxsub
->ai_timer
< 1) {
1220 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: ai_cmd: "
1221 "timer=%d, scan_begin_arg=%d. "
1222 "Not properly tested by cmdtest?\n", dev
->minor
,
1223 this_usbduxsub
->ai_timer
, cmd
->scan_begin_arg
);
1224 up(&this_usbduxsub
->sem
);
1227 this_usbduxsub
->ai_counter
= this_usbduxsub
->ai_timer
;
1229 if (cmd
->stop_src
== TRIG_COUNT
) {
1230 /* data arrives as one packet */
1231 this_usbduxsub
->ai_sample_count
= cmd
->stop_arg
;
1232 this_usbduxsub
->ai_continuous
= 0;
1234 /* continuous acquisition */
1235 this_usbduxsub
->ai_continuous
= 1;
1236 this_usbduxsub
->ai_sample_count
= 0;
1239 if (cmd
->start_src
== TRIG_NOW
) {
1240 /* enable this acquisition operation */
1241 this_usbduxsub
->ai_cmd_running
= 1;
1242 ret
= usbduxsub_submit_InURBs(this_usbduxsub
);
1244 this_usbduxsub
->ai_cmd_running
= 0;
1245 /* fixme: unlink here?? */
1246 up(&this_usbduxsub
->sem
);
1249 s
->async
->inttrig
= NULL
;
1252 /* don't enable the acquision operation */
1253 /* wait for an internal signal */
1254 s
->async
->inttrig
= usbdux_ai_inttrig
;
1256 up(&this_usbduxsub
->sem
);
1260 /* Mode 0 is used to get a single conversion on demand */
1261 static int usbdux_ai_insn_read(struct comedi_device
*dev
,
1262 struct comedi_subdevice
*s
,
1263 struct comedi_insn
*insn
, unsigned int *data
)
1269 struct usbduxsub
*this_usbduxsub
= dev
->private;
1274 if (!this_usbduxsub
)
1277 dev_dbg(&this_usbduxsub
->interface
->dev
,
1278 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1279 dev
->minor
, insn
->n
, insn
->subdev
);
1281 down(&this_usbduxsub
->sem
);
1282 if (!(this_usbduxsub
->probed
)) {
1283 up(&this_usbduxsub
->sem
);
1286 if (this_usbduxsub
->ai_cmd_running
) {
1287 dev_err(&this_usbduxsub
->interface
->dev
,
1288 "comedi%d: ai_insn_read not possible. "
1289 "Async Command is running.\n", dev
->minor
);
1290 up(&this_usbduxsub
->sem
);
1294 /* sample one channel */
1295 /* CONFIG0: chopper on */
1296 this_usbduxsub
->dux_commands
[1] = 0x16;
1298 /* CONFIG1: 2kHz sampling rate */
1299 this_usbduxsub
->dux_commands
[2] = 0x80;
1301 /* CONFIG3: differential channels off */
1302 this_usbduxsub
->dux_commands
[3] = 0x00;
1304 chan
= CR_CHAN(insn
->chanspec
);
1305 create_adc_command(chan
, &muxsg0
, &muxsg1
);
1307 this_usbduxsub
->dux_commands
[4] = muxsg0
;
1308 this_usbduxsub
->dux_commands
[5] = muxsg1
;
1309 this_usbduxsub
->dux_commands
[6] = sysred
;
1312 err
= send_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1314 up(&this_usbduxsub
->sem
);
1318 for (i
= 0; i
< insn
->n
; i
++) {
1319 err
= receive_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1321 up(&this_usbduxsub
->sem
);
1324 /* 32 bits big endian from the A/D converter */
1325 one
= be32_to_cpu(*((int32_t *)
1326 ((this_usbduxsub
->insnBuffer
)+1)));
1327 /* mask out the status byte */
1328 one
= one
& 0x00ffffff;
1329 /* turn it into an unsigned integer */
1330 one
= one
^ 0x00800000;
1333 up(&this_usbduxsub
->sem
);
1340 static int usbdux_getstatusinfo(struct comedi_device
*dev
, int chan
)
1342 struct usbduxsub
*this_usbduxsub
= dev
->private;
1347 if (!this_usbduxsub
)
1350 if (this_usbduxsub
->ai_cmd_running
) {
1351 dev_err(&this_usbduxsub
->interface
->dev
,
1352 "comedi%d: status read not possible. "
1353 "Async Command is running.\n", dev
->minor
);
1358 this_usbduxsub
->dux_commands
[1] = 0x12;
1360 /* CONFIG1: 2kHz sampling rate */
1361 this_usbduxsub
->dux_commands
[2] = 0x80;
1363 /* CONFIG3: differential channels off */
1364 this_usbduxsub
->dux_commands
[3] = 0x00;
1368 sysred
= sysred
| 1;
1369 } else if (chan
== 2) {
1371 sysred
= sysred
| 4;
1372 } else if (chan
== 3) {
1374 sysred
= sysred
| 8;
1375 } else if (chan
== 4) {
1377 sysred
= sysred
| 16;
1378 } else if (chan
== 5) {
1380 sysred
= sysred
| 32;
1383 this_usbduxsub
->dux_commands
[4] = 0;
1384 this_usbduxsub
->dux_commands
[5] = 0;
1385 this_usbduxsub
->dux_commands
[6] = sysred
;
1388 err
= send_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1392 err
= receive_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1396 /* 32 bits big endian from the A/D converter */
1397 one
= be32_to_cpu(*((int32_t *)((this_usbduxsub
->insnBuffer
)+1)));
1398 /* mask out the staus byte */
1399 one
= one
& 0x00ffffff;
1400 one
= one
^ 0x00800000;
1410 /************************************/
1413 static int usbdux_ao_insn_read(struct comedi_device
*dev
,
1414 struct comedi_subdevice
*s
,
1415 struct comedi_insn
*insn
, unsigned int *data
)
1418 int chan
= CR_CHAN(insn
->chanspec
);
1419 struct usbduxsub
*this_usbduxsub
= dev
->private;
1421 if (!this_usbduxsub
)
1424 down(&this_usbduxsub
->sem
);
1425 if (!(this_usbduxsub
->probed
)) {
1426 up(&this_usbduxsub
->sem
);
1429 for (i
= 0; i
< insn
->n
; i
++)
1430 data
[i
] = this_usbduxsub
->outBuffer
[chan
];
1432 up(&this_usbduxsub
->sem
);
1436 static int usbdux_ao_insn_write(struct comedi_device
*dev
,
1437 struct comedi_subdevice
*s
,
1438 struct comedi_insn
*insn
, unsigned int *data
)
1441 int chan
= CR_CHAN(insn
->chanspec
);
1442 struct usbduxsub
*this_usbduxsub
= dev
->private;
1444 if (!this_usbduxsub
)
1447 dev_dbg(&this_usbduxsub
->interface
->dev
,
1448 "comedi%d: ao_insn_write\n", dev
->minor
);
1450 down(&this_usbduxsub
->sem
);
1451 if (!(this_usbduxsub
->probed
)) {
1452 up(&this_usbduxsub
->sem
);
1455 if (this_usbduxsub
->ao_cmd_running
) {
1456 dev_err(&this_usbduxsub
->interface
->dev
,
1457 "comedi%d: ao_insn_write: "
1458 "ERROR: asynchronous ao_cmd is running\n", dev
->minor
);
1459 up(&this_usbduxsub
->sem
);
1463 for (i
= 0; i
< insn
->n
; i
++) {
1464 dev_dbg(&this_usbduxsub
->interface
->dev
,
1465 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1466 dev
->minor
, chan
, i
, data
[i
]);
1468 /* number of channels: 1 */
1469 this_usbduxsub
->dux_commands
[1] = 1;
1470 /* channel number */
1471 this_usbduxsub
->dux_commands
[2] = data
[i
];
1472 this_usbduxsub
->outBuffer
[chan
] = data
[i
];
1473 this_usbduxsub
->dux_commands
[3] = chan
;
1474 err
= send_dux_commands(this_usbduxsub
, SENDDACOMMANDS
);
1476 up(&this_usbduxsub
->sem
);
1480 up(&this_usbduxsub
->sem
);
1485 static int usbdux_ao_inttrig(struct comedi_device
*dev
,
1486 struct comedi_subdevice
*s
, unsigned int trignum
)
1489 struct usbduxsub
*this_usbduxsub
= dev
->private;
1491 if (!this_usbduxsub
)
1494 down(&this_usbduxsub
->sem
);
1496 if (!(this_usbduxsub
->probed
)) {
1501 dev_err(&this_usbduxsub
->interface
->dev
,
1502 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1507 if (!(this_usbduxsub
->ao_cmd_running
)) {
1508 this_usbduxsub
->ao_cmd_running
= 1;
1509 ret
= usbduxsub_submit_OutURBs(this_usbduxsub
);
1511 dev_err(&this_usbduxsub
->interface
->dev
,
1512 "comedi%d: usbdux_ao_inttrig: submitURB: "
1513 "err=%d\n", dev
->minor
, ret
);
1514 this_usbduxsub
->ao_cmd_running
= 0;
1517 s
->async
->inttrig
= NULL
;
1519 dev_err(&this_usbduxsub
->interface
->dev
,
1520 "comedi%d: ao_inttrig but acqu is already running.\n",
1525 up(&this_usbduxsub
->sem
);
1529 static int usbdux_ao_cmdtest(struct comedi_device
*dev
,
1530 struct comedi_subdevice
*s
,
1531 struct comedi_cmd
*cmd
)
1533 struct usbduxsub
*this_usbduxsub
= dev
->private;
1537 if (!this_usbduxsub
)
1540 if (!(this_usbduxsub
->probed
))
1543 dev_dbg(&this_usbduxsub
->interface
->dev
,
1544 "comedi%d: usbdux_ao_cmdtest\n", dev
->minor
);
1546 /* Step 1 : check if triggers are trivially valid */
1548 err
|= cfc_check_trigger_src(&cmd
->start_src
, TRIG_NOW
| TRIG_INT
);
1550 if (0) { /* (this_usbduxsub->high_speed) */
1552 * start immediately a new scan
1553 * the sampling rate is set by the coversion rate
1555 flags
= TRIG_FOLLOW
;
1557 /* start a new scan (output at once) with a timer */
1560 err
|= cfc_check_trigger_src(&cmd
->scan_begin_src
, flags
);
1562 err
|= cfc_check_trigger_src(&cmd
->convert_src
, TRIG_NOW
);
1563 err
|= cfc_check_trigger_src(&cmd
->scan_end_src
, TRIG_COUNT
);
1564 err
|= cfc_check_trigger_src(&cmd
->stop_src
, TRIG_COUNT
| TRIG_NONE
);
1569 /* Step 2a : make sure trigger sources are unique */
1571 err
|= cfc_check_trigger_is_unique(cmd
->start_src
);
1572 err
|= cfc_check_trigger_is_unique(cmd
->stop_src
);
1574 /* Step 2b : and mutually compatible */
1579 /* step 3: make sure arguments are trivially compatible */
1581 if (cmd
->start_arg
!= 0) {
1586 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
1587 /* internal trigger */
1588 if (cmd
->scan_begin_arg
!= 0) {
1589 cmd
->scan_begin_arg
= 0;
1594 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
1596 if (cmd
->scan_begin_arg
< 1000000) {
1597 cmd
->scan_begin_arg
= 1000000;
1601 /* not used now, is for later use */
1602 if (cmd
->convert_src
== TRIG_TIMER
) {
1603 if (cmd
->convert_arg
< 125000) {
1604 cmd
->convert_arg
= 125000;
1609 /* the same argument */
1610 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
1611 cmd
->scan_end_arg
= cmd
->chanlist_len
;
1615 if (cmd
->stop_src
== TRIG_COUNT
) {
1616 /* any count is allowed */
1619 if (cmd
->stop_arg
!= 0) {
1625 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi%d: err=%d, "
1626 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1627 "convert_arg=%d\n", dev
->minor
, err
, cmd
->scan_begin_src
,
1628 cmd
->scan_begin_arg
, cmd
->convert_src
, cmd
->convert_arg
);
1636 static int usbdux_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1638 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
1639 unsigned int chan
, gain
;
1641 struct usbduxsub
*this_usbduxsub
= dev
->private;
1643 if (!this_usbduxsub
)
1646 down(&this_usbduxsub
->sem
);
1647 if (!(this_usbduxsub
->probed
)) {
1648 up(&this_usbduxsub
->sem
);
1651 dev_dbg(&this_usbduxsub
->interface
->dev
,
1652 "comedi%d: %s\n", dev
->minor
, __func__
);
1654 /* set current channel of the running acquisition to zero */
1655 s
->async
->cur_chan
= 0;
1656 for (i
= 0; i
< cmd
->chanlist_len
; ++i
) {
1657 chan
= CR_CHAN(cmd
->chanlist
[i
]);
1658 gain
= CR_RANGE(cmd
->chanlist
[i
]);
1659 if (i
>= NUMOUTCHANNELS
) {
1660 dev_err(&this_usbduxsub
->interface
->dev
,
1661 "comedi%d: %s: channel list too long\n",
1662 dev
->minor
, __func__
);
1665 this_usbduxsub
->dac_commands
[i
] = chan
;
1666 dev_dbg(&this_usbduxsub
->interface
->dev
,
1667 "comedi%d: dac command for ch %d is %x\n",
1668 dev
->minor
, i
, this_usbduxsub
->dac_commands
[i
]);
1671 /* we count in steps of 1ms (125us) */
1672 /* 125us mode not used yet */
1673 if (0) { /* (this_usbduxsub->high_speed) */
1675 /* timing of the conversion itself: every 125 us */
1676 this_usbduxsub
->ao_timer
= cmd
->convert_arg
/ 125000;
1679 /* timing of the scan: we get all channels at once */
1680 this_usbduxsub
->ao_timer
= cmd
->scan_begin_arg
/ 1000000;
1681 dev_dbg(&this_usbduxsub
->interface
->dev
,
1682 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1683 "convert_src=%d, convert_arg=%d\n", dev
->minor
,
1684 cmd
->scan_begin_src
, cmd
->scan_begin_arg
,
1685 cmd
->convert_src
, cmd
->convert_arg
);
1686 dev_dbg(&this_usbduxsub
->interface
->dev
,
1687 "comedi%d: ao_timer=%d (ms)\n",
1688 dev
->minor
, this_usbduxsub
->ao_timer
);
1689 if (this_usbduxsub
->ao_timer
< 1) {
1690 dev_err(&this_usbduxsub
->interface
->dev
,
1691 "comedi%d: usbdux: ao_timer=%d, "
1692 "scan_begin_arg=%d. "
1693 "Not properly tested by cmdtest?\n",
1694 dev
->minor
, this_usbduxsub
->ao_timer
,
1695 cmd
->scan_begin_arg
);
1696 up(&this_usbduxsub
->sem
);
1700 this_usbduxsub
->ao_counter
= this_usbduxsub
->ao_timer
;
1702 if (cmd
->stop_src
== TRIG_COUNT
) {
1703 /* not continuous */
1705 /* high speed also scans everything at once */
1706 if (0) { /* (this_usbduxsub->high_speed) */
1707 this_usbduxsub
->ao_sample_count
=
1708 (cmd
->stop_arg
) * (cmd
->scan_end_arg
);
1710 /* there's no scan as the scan has been */
1711 /* perf inside the FX2 */
1712 /* data arrives as one packet */
1713 this_usbduxsub
->ao_sample_count
= cmd
->stop_arg
;
1715 this_usbduxsub
->ao_continuous
= 0;
1717 /* continuous acquisition */
1718 this_usbduxsub
->ao_continuous
= 1;
1719 this_usbduxsub
->ao_sample_count
= 0;
1722 if (cmd
->start_src
== TRIG_NOW
) {
1723 /* enable this acquisition operation */
1724 this_usbduxsub
->ao_cmd_running
= 1;
1725 ret
= usbduxsub_submit_OutURBs(this_usbduxsub
);
1727 this_usbduxsub
->ao_cmd_running
= 0;
1728 /* fixme: unlink here?? */
1729 up(&this_usbduxsub
->sem
);
1732 s
->async
->inttrig
= NULL
;
1735 /* submit the urbs later */
1736 /* wait for an internal signal */
1737 s
->async
->inttrig
= usbdux_ao_inttrig
;
1740 up(&this_usbduxsub
->sem
);
1744 static int usbdux_dio_insn_config(struct comedi_device
*dev
,
1745 struct comedi_subdevice
*s
,
1746 struct comedi_insn
*insn
, unsigned int *data
)
1748 int chan
= CR_CHAN(insn
->chanspec
);
1750 /* The input or output configuration of each digital line is
1751 * configured by a special insn_config instruction. chanspec
1752 * contains the channel to be changed, and data[0] contains the
1753 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1756 case INSN_CONFIG_DIO_OUTPUT
:
1757 s
->io_bits
|= 1 << chan
; /* 1 means Out */
1759 case INSN_CONFIG_DIO_INPUT
:
1760 s
->io_bits
&= ~(1 << chan
);
1762 case INSN_CONFIG_DIO_QUERY
:
1764 (s
->io_bits
& (1 << chan
)) ? COMEDI_OUTPUT
: COMEDI_INPUT
;
1770 /* we don't tell the firmware here as it would take 8 frames */
1771 /* to submit the information. We do it in the insn_bits. */
1775 static int usbdux_dio_insn_bits(struct comedi_device
*dev
,
1776 struct comedi_subdevice
*s
,
1777 struct comedi_insn
*insn
,
1781 struct usbduxsub
*this_usbduxsub
= dev
->private;
1784 if (!this_usbduxsub
)
1787 down(&this_usbduxsub
->sem
);
1789 if (!(this_usbduxsub
->probed
)) {
1790 up(&this_usbduxsub
->sem
);
1794 /* The insn data is a mask in data[0] and the new data
1795 * in data[1], each channel cooresponding to a bit. */
1796 s
->state
&= ~data
[0];
1797 s
->state
|= data
[0] & data
[1];
1798 /* The commands are 8 bits wide */
1799 this_usbduxsub
->dux_commands
[1] = (s
->io_bits
) & 0x000000FF;
1800 this_usbduxsub
->dux_commands
[4] = (s
->state
) & 0x000000FF;
1801 this_usbduxsub
->dux_commands
[2] = ((s
->io_bits
) & 0x0000FF00) >> 8;
1802 this_usbduxsub
->dux_commands
[5] = ((s
->state
) & 0x0000FF00) >> 8;
1803 this_usbduxsub
->dux_commands
[3] = ((s
->io_bits
) & 0x00FF0000) >> 16;
1804 this_usbduxsub
->dux_commands
[6] = ((s
->state
) & 0x00FF0000) >> 16;
1806 /* This command also tells the firmware to return */
1807 /* the digital input lines */
1808 err
= send_dux_commands(this_usbduxsub
, SENDDIOBITSCOMMAND
);
1810 up(&this_usbduxsub
->sem
);
1813 err
= receive_dux_commands(this_usbduxsub
, SENDDIOBITSCOMMAND
);
1815 up(&this_usbduxsub
->sem
);
1819 data
[1] = (((unsigned int)(this_usbduxsub
->insnBuffer
[1]))&0xff) |
1820 ((((unsigned int)(this_usbduxsub
->insnBuffer
[2]))&0xff) << 8) |
1821 ((((unsigned int)(this_usbduxsub
->insnBuffer
[3]))&0xff) << 16);
1825 up(&this_usbduxsub
->sem
);
1829 /***********************************/
1832 static int usbduxsub_unlink_PwmURBs(struct usbduxsub
*usbduxsub_tmp
)
1836 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbPwm
) {
1837 if (usbduxsub_tmp
->urbPwm
)
1838 usb_kill_urb(usbduxsub_tmp
->urbPwm
);
1839 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
1840 "comedi: unlinked PwmURB: res=%d\n", err
);
1845 /* This cancels a running acquisition operation
1848 static int usbdux_pwm_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
1852 if (!this_usbduxsub
)
1855 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: %s\n", __func__
);
1857 ret
= usbduxsub_unlink_PwmURBs(this_usbduxsub
);
1859 this_usbduxsub
->pwm_cmd_running
= 0;
1864 /* force unlink - is called by comedi */
1865 static int usbdux_pwm_cancel(struct comedi_device
*dev
,
1866 struct comedi_subdevice
*s
)
1868 struct usbduxsub
*this_usbduxsub
= dev
->private;
1871 /* unlink only if it is really running */
1872 res
= usbdux_pwm_stop(this_usbduxsub
, this_usbduxsub
->pwm_cmd_running
);
1874 dev_dbg(&this_usbduxsub
->interface
->dev
,
1875 "comedi %d: sending pwm off command to the usb device.\n",
1877 res
= send_dux_commands(this_usbduxsub
, SENDPWMOFF
);
1884 static void usbduxsub_pwm_irq(struct urb
*urb
)
1887 struct usbduxsub
*this_usbduxsub
;
1888 struct comedi_device
*this_comedidev
;
1889 struct comedi_subdevice
*s
;
1891 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1893 /* the context variable points to the subdevice */
1894 this_comedidev
= urb
->context
;
1895 /* the private structure of the subdevice is struct usbduxsub */
1896 this_usbduxsub
= this_comedidev
->private;
1898 s
= &this_comedidev
->subdevices
[SUBDEV_DA
];
1900 switch (urb
->status
) {
1910 * after an unlink command, unplug, ... etc
1911 * no unlink needed here. Already shutting down.
1913 if (this_usbduxsub
->pwm_cmd_running
)
1914 usbdux_pwm_stop(this_usbduxsub
, 0);
1920 if (this_usbduxsub
->pwm_cmd_running
) {
1921 dev_err(&this_usbduxsub
->interface
->dev
,
1922 "comedi_: Non-zero urb status received in "
1923 "pwm intr context: %d\n", urb
->status
);
1924 usbdux_pwm_stop(this_usbduxsub
, 0);
1929 /* are we actually running? */
1930 if (!(this_usbduxsub
->pwm_cmd_running
))
1933 urb
->transfer_buffer_length
= this_usbduxsub
->sizePwmBuf
;
1934 urb
->dev
= this_usbduxsub
->usbdev
;
1936 if (this_usbduxsub
->pwm_cmd_running
) {
1937 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
1939 dev_err(&this_usbduxsub
->interface
->dev
,
1940 "comedi_: pwm urb resubm failed in int-cont. "
1942 if (ret
== EL2NSYNC
)
1943 dev_err(&this_usbduxsub
->interface
->dev
,
1944 "buggy USB host controller or bug in "
1947 /* don't do an unlink here */
1948 usbdux_pwm_stop(this_usbduxsub
, 0);
1953 static int usbduxsub_submit_PwmURBs(struct usbduxsub
*usbduxsub
)
1960 dev_dbg(&usbduxsub
->interface
->dev
, "comedi_: submitting pwm-urb\n");
1962 /* in case of a resubmission after an unlink... */
1963 usb_fill_bulk_urb(usbduxsub
->urbPwm
,
1965 usb_sndbulkpipe(usbduxsub
->usbdev
, PWM_EP
),
1966 usbduxsub
->urbPwm
->transfer_buffer
,
1967 usbduxsub
->sizePwmBuf
, usbduxsub_pwm_irq
,
1968 usbduxsub
->comedidev
);
1970 errFlag
= usb_submit_urb(usbduxsub
->urbPwm
, GFP_ATOMIC
);
1972 dev_err(&usbduxsub
->interface
->dev
,
1973 "comedi_: usbduxsigma: pwm: usb_submit_urb error %d\n",
1980 static int usbdux_pwm_period(struct comedi_device
*dev
,
1981 struct comedi_subdevice
*s
, unsigned int period
)
1983 struct usbduxsub
*this_usbduxsub
= dev
->private;
1986 if (period
< MIN_PWM_PERIOD
) {
1987 dev_err(&this_usbduxsub
->interface
->dev
,
1988 "comedi%d: illegal period setting for pwm.\n",
1992 fx2delay
= period
/ ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1993 if (fx2delay
> 255) {
1994 dev_err(&this_usbduxsub
->interface
->dev
,
1995 "comedi%d: period %d for pwm is too low.\n",
1996 dev
->minor
, period
);
2000 this_usbduxsub
->pwmDelay
= fx2delay
;
2001 this_usbduxsub
->pwmPeriod
= period
;
2002 dev_dbg(&this_usbduxsub
->interface
->dev
, "%s: frequ=%d, period=%d\n",
2003 __func__
, period
, fx2delay
);
2007 /* is called from insn so there's no need to do all the sanity checks */
2008 static int usbdux_pwm_start(struct comedi_device
*dev
,
2009 struct comedi_subdevice
*s
)
2012 struct usbduxsub
*this_usbduxsub
= dev
->private;
2014 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi%d: %s\n",
2015 dev
->minor
, __func__
);
2017 if (this_usbduxsub
->pwm_cmd_running
) {
2018 /* already running */
2022 this_usbduxsub
->dux_commands
[1] = ((uint8_t) this_usbduxsub
->pwmDelay
);
2023 ret
= send_dux_commands(this_usbduxsub
, SENDPWMON
);
2027 /* initialise the buffer */
2028 for (i
= 0; i
< this_usbduxsub
->sizePwmBuf
; i
++)
2029 ((char *)(this_usbduxsub
->urbPwm
->transfer_buffer
))[i
] = 0;
2031 this_usbduxsub
->pwm_cmd_running
= 1;
2032 ret
= usbduxsub_submit_PwmURBs(this_usbduxsub
);
2034 this_usbduxsub
->pwm_cmd_running
= 0;
2040 /* generates the bit pattern for PWM with the optional sign bit */
2041 static int usbdux_pwm_pattern(struct comedi_device
*dev
,
2042 struct comedi_subdevice
*s
, int channel
,
2043 unsigned int value
, unsigned int sign
)
2045 struct usbduxsub
*this_usbduxsub
= dev
->private;
2052 if (!this_usbduxsub
)
2055 /* this is the DIO bit which carries the PWM data */
2056 pwm_mask
= (1 << channel
);
2057 /* this is the DIO bit which carries the optional direction bit */
2058 sgn_mask
= (16 << channel
);
2059 /* this is the buffer which will be filled with the with bit */
2060 /* pattern for one period */
2061 szbuf
= this_usbduxsub
->sizePwmBuf
;
2062 pBuf
= (char *)(this_usbduxsub
->urbPwm
->transfer_buffer
);
2063 for (i
= 0; i
< szbuf
; i
++) {
2066 c
= c
& (~pwm_mask
);
2067 /* set the bit as long as the index is lower than the value */
2070 /* set the optional sign bit for a relay */
2072 /* positive value */
2073 c
= c
& (~sgn_mask
);
2075 /* negative value */
2083 static int usbdux_pwm_write(struct comedi_device
*dev
,
2084 struct comedi_subdevice
*s
,
2085 struct comedi_insn
*insn
, unsigned int *data
)
2087 struct usbduxsub
*this_usbduxsub
= dev
->private;
2089 if (!this_usbduxsub
)
2092 if ((insn
->n
) != 1) {
2094 * doesn't make sense to have more than one value here because
2095 * it would just overwrite the PWM buffer a couple of times
2101 * the sign is set via a special INSN only, this gives us 8 bits for
2103 * relay sign 0 by default
2105 return usbdux_pwm_pattern(dev
, s
, CR_CHAN(insn
->chanspec
), data
[0], 0);
2108 static int usbdux_pwm_read(struct comedi_device
*x1
,
2109 struct comedi_subdevice
*x2
, struct comedi_insn
*x3
,
2116 /* switches on/off PWM */
2117 static int usbdux_pwm_config(struct comedi_device
*dev
,
2118 struct comedi_subdevice
*s
,
2119 struct comedi_insn
*insn
, unsigned int *data
)
2121 struct usbduxsub
*this_usbduxsub
= dev
->private;
2123 case INSN_CONFIG_ARM
:
2125 dev_dbg(&this_usbduxsub
->interface
->dev
,
2126 "comedi%d: %s: pwm on\n", dev
->minor
, __func__
);
2128 * if not zero the PWM is limited to a certain time which is
2129 * not supported here
2133 return usbdux_pwm_start(dev
, s
);
2134 case INSN_CONFIG_DISARM
:
2135 dev_dbg(&this_usbduxsub
->interface
->dev
,
2136 "comedi%d: %s: pwm off\n", dev
->minor
, __func__
);
2137 return usbdux_pwm_cancel(dev
, s
);
2138 case INSN_CONFIG_GET_PWM_STATUS
:
2140 * to check if the USB transmission has failed or in case PWM
2141 * was limited to n cycles to check if it has terminated
2143 data
[1] = this_usbduxsub
->pwm_cmd_running
;
2145 case INSN_CONFIG_PWM_SET_PERIOD
:
2146 dev_dbg(&this_usbduxsub
->interface
->dev
,
2147 "comedi%d: %s: setting period\n", dev
->minor
,
2149 return usbdux_pwm_period(dev
, s
, data
[1]);
2150 case INSN_CONFIG_PWM_GET_PERIOD
:
2151 data
[1] = this_usbduxsub
->pwmPeriod
;
2153 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
2154 /* value in the first byte and the sign in the second for a
2156 return usbdux_pwm_pattern(dev
, s
,
2157 /* the channel number */
2158 CR_CHAN(insn
->chanspec
),
2159 /* actual PWM data */
2163 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
2164 /* values are not kept in this driver, nothing to return */
2171 /*****************************************************************/
2173 static void tidy_up(struct usbduxsub
*usbduxsub_tmp
)
2179 dev_dbg(&usbduxsub_tmp
->interface
->dev
, "comedi_: tiding up\n");
2181 /* shows the usb subsystem that the driver is down */
2182 if (usbduxsub_tmp
->interface
)
2183 usb_set_intfdata(usbduxsub_tmp
->interface
, NULL
);
2185 usbduxsub_tmp
->probed
= 0;
2187 if (usbduxsub_tmp
->urbIn
) {
2188 if (usbduxsub_tmp
->ai_cmd_running
) {
2189 usbduxsub_tmp
->ai_cmd_running
= 0;
2190 usbduxsub_unlink_InURBs(usbduxsub_tmp
);
2192 for (i
= 0; i
< usbduxsub_tmp
->numOfInBuffers
; i
++) {
2193 kfree(usbduxsub_tmp
->urbIn
[i
]->transfer_buffer
);
2194 usbduxsub_tmp
->urbIn
[i
]->transfer_buffer
= NULL
;
2195 usb_kill_urb(usbduxsub_tmp
->urbIn
[i
]);
2196 usb_free_urb(usbduxsub_tmp
->urbIn
[i
]);
2197 usbduxsub_tmp
->urbIn
[i
] = NULL
;
2199 kfree(usbduxsub_tmp
->urbIn
);
2200 usbduxsub_tmp
->urbIn
= NULL
;
2202 if (usbduxsub_tmp
->urbOut
) {
2203 if (usbduxsub_tmp
->ao_cmd_running
) {
2204 usbduxsub_tmp
->ao_cmd_running
= 0;
2205 usbduxsub_unlink_OutURBs(usbduxsub_tmp
);
2207 for (i
= 0; i
< usbduxsub_tmp
->numOfOutBuffers
; i
++) {
2208 if (usbduxsub_tmp
->urbOut
[i
]->transfer_buffer
) {
2209 kfree(usbduxsub_tmp
->
2210 urbOut
[i
]->transfer_buffer
);
2211 usbduxsub_tmp
->urbOut
[i
]->transfer_buffer
=
2214 if (usbduxsub_tmp
->urbOut
[i
]) {
2215 usb_kill_urb(usbduxsub_tmp
->urbOut
[i
]);
2216 usb_free_urb(usbduxsub_tmp
->urbOut
[i
]);
2217 usbduxsub_tmp
->urbOut
[i
] = NULL
;
2220 kfree(usbduxsub_tmp
->urbOut
);
2221 usbduxsub_tmp
->urbOut
= NULL
;
2223 if (usbduxsub_tmp
->urbPwm
) {
2224 if (usbduxsub_tmp
->pwm_cmd_running
) {
2225 usbduxsub_tmp
->pwm_cmd_running
= 0;
2226 usbduxsub_unlink_PwmURBs(usbduxsub_tmp
);
2228 kfree(usbduxsub_tmp
->urbPwm
->transfer_buffer
);
2229 usbduxsub_tmp
->urbPwm
->transfer_buffer
= NULL
;
2230 usb_kill_urb(usbduxsub_tmp
->urbPwm
);
2231 usb_free_urb(usbduxsub_tmp
->urbPwm
);
2232 usbduxsub_tmp
->urbPwm
= NULL
;
2234 kfree(usbduxsub_tmp
->inBuffer
);
2235 usbduxsub_tmp
->inBuffer
= NULL
;
2236 kfree(usbduxsub_tmp
->insnBuffer
);
2237 usbduxsub_tmp
->insnBuffer
= NULL
;
2238 kfree(usbduxsub_tmp
->outBuffer
);
2239 usbduxsub_tmp
->outBuffer
= NULL
;
2240 kfree(usbduxsub_tmp
->dac_commands
);
2241 usbduxsub_tmp
->dac_commands
= NULL
;
2242 kfree(usbduxsub_tmp
->dux_commands
);
2243 usbduxsub_tmp
->dux_commands
= NULL
;
2244 usbduxsub_tmp
->ai_cmd_running
= 0;
2245 usbduxsub_tmp
->ao_cmd_running
= 0;
2246 usbduxsub_tmp
->pwm_cmd_running
= 0;
2249 static int usbduxsigma_attach_common(struct comedi_device
*dev
,
2250 struct usbduxsub
*uds
)
2253 struct comedi_subdevice
*s
;
2258 /* pointer back to the corresponding comedi device */
2259 uds
->comedidev
= dev
;
2260 dev
->board_name
= "usbduxsigma";
2261 /* set number of subdevices */
2262 if (uds
->high_speed
)
2263 n_subdevs
= 4; /* with pwm */
2265 n_subdevs
= 3; /* without pwm */
2266 ret
= comedi_alloc_subdevices(dev
, n_subdevs
);
2271 /* private structure is also simply the usb-structure */
2273 /* the first subdevice is the A/D converter */
2274 s
= &dev
->subdevices
[SUBDEV_AD
];
2275 /* the URBs get the comedi subdevice */
2276 /* which is responsible for reading */
2277 /* this is the subdevice which reads data */
2278 dev
->read_subdev
= s
;
2279 /* the subdevice receives as private structure the */
2283 s
->type
= COMEDI_SUBD_AI
;
2284 /* readable and ref is to ground, 32 bit wide data! */
2285 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
|
2286 SDF_CMD_READ
| SDF_LSAMPL
;
2287 /* 16 A/D channels */
2288 s
->n_chan
= NUMCHANNELS
;
2289 /* length of the channellist */
2290 s
->len_chanlist
= NUMCHANNELS
;
2291 /* callback functions */
2292 s
->insn_read
= usbdux_ai_insn_read
;
2293 s
->do_cmdtest
= usbdux_ai_cmdtest
;
2294 s
->do_cmd
= usbdux_ai_cmd
;
2295 s
->cancel
= usbdux_ai_cancel
;
2296 /* max value from the A/D converter (24bit) */
2297 s
->maxdata
= 0x00FFFFFF;
2298 /* range table to convert to physical units */
2299 s
->range_table
= (&range_usbdux_ai_range
);
2300 /* analog output subdevice */
2301 s
= &dev
->subdevices
[SUBDEV_DA
];
2303 s
->type
= COMEDI_SUBD_AO
;
2304 /* backward pointer */
2305 dev
->write_subdev
= s
;
2306 /* the subdevice receives as private structure the */
2310 s
->subdev_flags
= SDF_WRITABLE
| SDF_GROUND
| SDF_CMD_WRITE
;
2313 /* length of the channellist */
2314 s
->len_chanlist
= 4;
2315 /* 8 bit resolution */
2316 s
->maxdata
= 0x00ff;
2317 /* unipolar range */
2318 s
->range_table
= (&range_usbdux_ao_range
);
2320 s
->do_cmdtest
= usbdux_ao_cmdtest
;
2321 s
->do_cmd
= usbdux_ao_cmd
;
2322 s
->cancel
= usbdux_ao_cancel
;
2323 s
->insn_read
= usbdux_ao_insn_read
;
2324 s
->insn_write
= usbdux_ao_insn_write
;
2325 /* digital I/O subdevice */
2326 s
= &dev
->subdevices
[SUBDEV_DIO
];
2327 s
->type
= COMEDI_SUBD_DIO
;
2328 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
2329 /* 8 external and 16 internal channels */
2332 s
->range_table
= (&range_digital
);
2333 s
->insn_bits
= usbdux_dio_insn_bits
;
2334 s
->insn_config
= usbdux_dio_insn_config
;
2335 /* we don't use it */
2337 if (uds
->high_speed
) {
2338 /* timer / pwm subdevice */
2339 s
= &dev
->subdevices
[SUBDEV_PWM
];
2340 s
->type
= COMEDI_SUBD_PWM
;
2341 s
->subdev_flags
= SDF_WRITABLE
| SDF_PWM_HBRIDGE
;
2343 /* this defines the max duty cycle resolution */
2344 s
->maxdata
= uds
->sizePwmBuf
;
2345 s
->insn_write
= usbdux_pwm_write
;
2346 s
->insn_read
= usbdux_pwm_read
;
2347 s
->insn_config
= usbdux_pwm_config
;
2348 usbdux_pwm_period(dev
, s
, PWM_DEFAULT_PERIOD
);
2350 /* finally decide that it's attached */
2353 offset
= usbdux_getstatusinfo(dev
, 0);
2355 dev_err(&uds
->interface
->dev
,
2356 "Communication to USBDUXSIGMA failed! Check firmware and cabling.");
2357 dev_info(&uds
->interface
->dev
,
2358 "comedi%d: attached, ADC_zero = %x\n", dev
->minor
, offset
);
2362 static int usbduxsigma_attach_usb(struct comedi_device
*dev
,
2363 struct usb_interface
*uinterf
)
2366 struct usbduxsub
*uds
;
2368 dev
->private = NULL
;
2369 down(&start_stop_sem
);
2370 uds
= usb_get_intfdata(uinterf
);
2371 if (!uds
|| !uds
->probed
) {
2372 dev_err(dev
->class_dev
,
2373 "usbduxsigma: error: attach_usb failed, not connected\n");
2375 } else if (uds
->attached
) {
2376 dev_err(dev
->class_dev
,
2377 "usbduxsigma: error: attach_usb failed, already attached\n");
2380 ret
= usbduxsigma_attach_common(dev
, uds
);
2381 up(&start_stop_sem
);
2385 static void usbduxsigma_detach(struct comedi_device
*dev
)
2387 struct usbduxsub
*usb
= dev
->private;
2391 dev
->private = NULL
;
2393 usb
->comedidev
= NULL
;
2398 static struct comedi_driver usbduxsigma_driver
= {
2399 .driver_name
= "usbduxsigma",
2400 .module
= THIS_MODULE
,
2401 .attach_usb
= usbduxsigma_attach_usb
,
2402 .detach
= usbduxsigma_detach
,
2405 static void usbdux_firmware_request_complete_handler(const struct firmware
*fw
,
2408 struct usbduxsub
*usbduxsub_tmp
= context
;
2409 struct usb_interface
*uinterf
= usbduxsub_tmp
->interface
;
2413 dev_err(&uinterf
->dev
,
2414 "Firmware complete handler without firmware!\n");
2419 * we need to upload the firmware here because fw will be
2420 * freed once we've left this function
2422 ret
= firmwareUpload(usbduxsub_tmp
, fw
->data
, fw
->size
);
2425 dev_err(&uinterf
->dev
,
2426 "Could not upload firmware (err=%d)\n", ret
);
2429 comedi_usb_auto_config(uinterf
, &usbduxsigma_driver
);
2431 release_firmware(fw
);
2434 static int usbduxsigma_usb_probe(struct usb_interface
*uinterf
,
2435 const struct usb_device_id
*id
)
2437 struct usb_device
*udev
= interface_to_usbdev(uinterf
);
2438 struct device
*dev
= &uinterf
->dev
;
2443 dev_dbg(dev
, "comedi_: usbdux_: "
2444 "finding a free structure for the usb-device\n");
2446 down(&start_stop_sem
);
2447 /* look for a free place in the usbdux array */
2449 for (i
= 0; i
< NUMUSBDUX
; i
++) {
2450 if (!(usbduxsub
[i
].probed
)) {
2458 dev_err(dev
, "Too many usbduxsigma-devices connected.\n");
2459 up(&start_stop_sem
);
2462 dev_dbg(dev
, "comedi_: usbdux: "
2463 "usbduxsub[%d] is ready to connect to comedi.\n", index
);
2465 sema_init(&(usbduxsub
[index
].sem
), 1);
2466 /* save a pointer to the usb device */
2467 usbduxsub
[index
].usbdev
= udev
;
2469 /* save the interface itself */
2470 usbduxsub
[index
].interface
= uinterf
;
2471 /* get the interface number from the interface */
2472 usbduxsub
[index
].ifnum
= uinterf
->altsetting
->desc
.bInterfaceNumber
;
2473 /* hand the private data over to the usb subsystem */
2474 /* will be needed for disconnect */
2475 usb_set_intfdata(uinterf
, &(usbduxsub
[index
]));
2477 dev_dbg(dev
, "comedi_: usbdux: ifnum=%d\n", usbduxsub
[index
].ifnum
);
2479 /* test if it is high speed (USB 2.0) */
2480 usbduxsub
[index
].high_speed
=
2481 (usbduxsub
[index
].usbdev
->speed
== USB_SPEED_HIGH
);
2483 /* create space for the commands of the DA converter */
2484 usbduxsub
[index
].dac_commands
= kzalloc(NUMOUTCHANNELS
, GFP_KERNEL
);
2485 if (!usbduxsub
[index
].dac_commands
) {
2486 dev_err(dev
, "comedi_: usbduxsigma: "
2487 "error alloc space for dac commands\n");
2488 tidy_up(&(usbduxsub
[index
]));
2489 up(&start_stop_sem
);
2492 /* create space for the commands going to the usb device */
2493 usbduxsub
[index
].dux_commands
= kzalloc(SIZEOFDUXBUFFER
, GFP_KERNEL
);
2494 if (!usbduxsub
[index
].dux_commands
) {
2495 dev_err(dev
, "comedi_: usbduxsigma: "
2496 "error alloc space for dux commands\n");
2497 tidy_up(&(usbduxsub
[index
]));
2498 up(&start_stop_sem
);
2501 /* create space for the in buffer and set it to zero */
2502 usbduxsub
[index
].inBuffer
= kzalloc(SIZEINBUF
, GFP_KERNEL
);
2503 if (!(usbduxsub
[index
].inBuffer
)) {
2504 dev_err(dev
, "comedi_: usbduxsigma: "
2505 "could not alloc space for inBuffer\n");
2506 tidy_up(&(usbduxsub
[index
]));
2507 up(&start_stop_sem
);
2510 /* create space of the instruction buffer */
2511 usbduxsub
[index
].insnBuffer
= kzalloc(SIZEINSNBUF
, GFP_KERNEL
);
2512 if (!(usbduxsub
[index
].insnBuffer
)) {
2513 dev_err(dev
, "comedi_: usbduxsigma: "
2514 "could not alloc space for insnBuffer\n");
2515 tidy_up(&(usbduxsub
[index
]));
2516 up(&start_stop_sem
);
2519 /* create space for the outbuffer */
2520 usbduxsub
[index
].outBuffer
= kzalloc(SIZEOUTBUF
, GFP_KERNEL
);
2521 if (!(usbduxsub
[index
].outBuffer
)) {
2522 dev_err(dev
, "comedi_: usbduxsigma: "
2523 "could not alloc space for outBuffer\n");
2524 tidy_up(&(usbduxsub
[index
]));
2525 up(&start_stop_sem
);
2528 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2529 i
= usb_set_interface(usbduxsub
[index
].usbdev
,
2530 usbduxsub
[index
].ifnum
, 3);
2532 dev_err(dev
, "comedi_: usbduxsigma%d: "
2533 "could not set alternate setting 3 in high speed.\n",
2535 tidy_up(&(usbduxsub
[index
]));
2536 up(&start_stop_sem
);
2539 if (usbduxsub
[index
].high_speed
)
2540 usbduxsub
[index
].numOfInBuffers
= NUMOFINBUFFERSHIGH
;
2542 usbduxsub
[index
].numOfInBuffers
= NUMOFINBUFFERSFULL
;
2544 usbduxsub
[index
].urbIn
=
2545 kzalloc(sizeof(struct urb
*) * usbduxsub
[index
].numOfInBuffers
,
2547 if (!(usbduxsub
[index
].urbIn
)) {
2548 dev_err(dev
, "comedi_: usbduxsigma: "
2549 "Could not alloc. urbIn array\n");
2550 tidy_up(&(usbduxsub
[index
]));
2551 up(&start_stop_sem
);
2554 for (i
= 0; i
< usbduxsub
[index
].numOfInBuffers
; i
++) {
2555 /* one frame: 1ms */
2556 usbduxsub
[index
].urbIn
[i
] = usb_alloc_urb(1, GFP_KERNEL
);
2557 if (usbduxsub
[index
].urbIn
[i
] == NULL
) {
2558 dev_err(dev
, "comedi_: usbduxsigma%d: "
2559 "Could not alloc. urb(%d)\n", index
, i
);
2560 tidy_up(&(usbduxsub
[index
]));
2561 up(&start_stop_sem
);
2564 usbduxsub
[index
].urbIn
[i
]->dev
= usbduxsub
[index
].usbdev
;
2565 /* will be filled later with a pointer to the comedi-device */
2566 /* and ONLY then the urb should be submitted */
2567 usbduxsub
[index
].urbIn
[i
]->context
= NULL
;
2568 usbduxsub
[index
].urbIn
[i
]->pipe
=
2569 usb_rcvisocpipe(usbduxsub
[index
].usbdev
, ISOINEP
);
2570 usbduxsub
[index
].urbIn
[i
]->transfer_flags
= URB_ISO_ASAP
;
2571 usbduxsub
[index
].urbIn
[i
]->transfer_buffer
=
2572 kzalloc(SIZEINBUF
, GFP_KERNEL
);
2573 if (!(usbduxsub
[index
].urbIn
[i
]->transfer_buffer
)) {
2574 dev_err(dev
, "comedi_: usbduxsigma%d: "
2575 "could not alloc. transb.\n", index
);
2576 tidy_up(&(usbduxsub
[index
]));
2577 up(&start_stop_sem
);
2580 usbduxsub
[index
].urbIn
[i
]->complete
= usbduxsub_ai_IsocIrq
;
2581 usbduxsub
[index
].urbIn
[i
]->number_of_packets
= 1;
2582 usbduxsub
[index
].urbIn
[i
]->transfer_buffer_length
= SIZEINBUF
;
2583 usbduxsub
[index
].urbIn
[i
]->iso_frame_desc
[0].offset
= 0;
2584 usbduxsub
[index
].urbIn
[i
]->iso_frame_desc
[0].length
=
2589 if (usbduxsub
[index
].high_speed
)
2590 usbduxsub
[index
].numOfOutBuffers
= NUMOFOUTBUFFERSHIGH
;
2592 usbduxsub
[index
].numOfOutBuffers
= NUMOFOUTBUFFERSFULL
;
2594 usbduxsub
[index
].urbOut
=
2595 kzalloc(sizeof(struct urb
*) * usbduxsub
[index
].numOfOutBuffers
,
2597 if (!(usbduxsub
[index
].urbOut
)) {
2598 dev_err(dev
, "comedi_: usbduxsigma: "
2599 "Could not alloc. urbOut array\n");
2600 tidy_up(&(usbduxsub
[index
]));
2601 up(&start_stop_sem
);
2604 for (i
= 0; i
< usbduxsub
[index
].numOfOutBuffers
; i
++) {
2605 /* one frame: 1ms */
2606 usbduxsub
[index
].urbOut
[i
] = usb_alloc_urb(1, GFP_KERNEL
);
2607 if (usbduxsub
[index
].urbOut
[i
] == NULL
) {
2608 dev_err(dev
, "comedi_: usbduxsigma%d: "
2609 "Could not alloc. urb(%d)\n", index
, i
);
2610 tidy_up(&(usbduxsub
[index
]));
2611 up(&start_stop_sem
);
2614 usbduxsub
[index
].urbOut
[i
]->dev
= usbduxsub
[index
].usbdev
;
2615 /* will be filled later with a pointer to the comedi-device */
2616 /* and ONLY then the urb should be submitted */
2617 usbduxsub
[index
].urbOut
[i
]->context
= NULL
;
2618 usbduxsub
[index
].urbOut
[i
]->pipe
=
2619 usb_sndisocpipe(usbduxsub
[index
].usbdev
, ISOOUTEP
);
2620 usbduxsub
[index
].urbOut
[i
]->transfer_flags
= URB_ISO_ASAP
;
2621 usbduxsub
[index
].urbOut
[i
]->transfer_buffer
=
2622 kzalloc(SIZEOUTBUF
, GFP_KERNEL
);
2623 if (!(usbduxsub
[index
].urbOut
[i
]->transfer_buffer
)) {
2624 dev_err(dev
, "comedi_: usbduxsigma%d: "
2625 "could not alloc. transb.\n", index
);
2626 tidy_up(&(usbduxsub
[index
]));
2627 up(&start_stop_sem
);
2630 usbduxsub
[index
].urbOut
[i
]->complete
= usbduxsub_ao_IsocIrq
;
2631 usbduxsub
[index
].urbOut
[i
]->number_of_packets
= 1;
2632 usbduxsub
[index
].urbOut
[i
]->transfer_buffer_length
=
2634 usbduxsub
[index
].urbOut
[i
]->iso_frame_desc
[0].offset
= 0;
2635 usbduxsub
[index
].urbOut
[i
]->iso_frame_desc
[0].length
=
2637 if (usbduxsub
[index
].high_speed
) {
2639 usbduxsub
[index
].urbOut
[i
]->interval
= 8;
2642 usbduxsub
[index
].urbOut
[i
]->interval
= 1;
2647 if (usbduxsub
[index
].high_speed
) {
2648 /* max bulk ep size in high speed */
2649 usbduxsub
[index
].sizePwmBuf
= 512;
2650 usbduxsub
[index
].urbPwm
= usb_alloc_urb(0, GFP_KERNEL
);
2651 if (usbduxsub
[index
].urbPwm
== NULL
) {
2652 dev_err(dev
, "comedi_: usbduxsigma%d: "
2653 "Could not alloc. pwm urb\n", index
);
2654 tidy_up(&(usbduxsub
[index
]));
2655 up(&start_stop_sem
);
2658 usbduxsub
[index
].urbPwm
->transfer_buffer
=
2659 kzalloc(usbduxsub
[index
].sizePwmBuf
, GFP_KERNEL
);
2660 if (!(usbduxsub
[index
].urbPwm
->transfer_buffer
)) {
2661 dev_err(dev
, "comedi_: usbduxsigma%d: "
2662 "could not alloc. transb. for pwm\n", index
);
2663 tidy_up(&(usbduxsub
[index
]));
2664 up(&start_stop_sem
);
2668 usbduxsub
[index
].urbPwm
= NULL
;
2669 usbduxsub
[index
].sizePwmBuf
= 0;
2672 usbduxsub
[index
].ai_cmd_running
= 0;
2673 usbduxsub
[index
].ao_cmd_running
= 0;
2674 usbduxsub
[index
].pwm_cmd_running
= 0;
2676 /* we've reached the bottom of the function */
2677 usbduxsub
[index
].probed
= 1;
2678 up(&start_stop_sem
);
2680 ret
= request_firmware_nowait(THIS_MODULE
,
2686 usbdux_firmware_request_complete_handler
2690 dev_err(dev
, "Could not load firmware (err=%d)\n", ret
);
2694 dev_info(dev
, "comedi_: successfully initialised.\n");
2699 static void usbduxsigma_usb_disconnect(struct usb_interface
*intf
)
2701 struct usbduxsub
*usbduxsub_tmp
= usb_get_intfdata(intf
);
2702 struct usb_device
*udev
= interface_to_usbdev(intf
);
2704 if (!usbduxsub_tmp
) {
2706 "comedi_: disconnect called with null pointer.\n");
2709 if (usbduxsub_tmp
->usbdev
!= udev
) {
2710 dev_err(&intf
->dev
, "comedi_: BUG! wrong ptr!\n");
2713 if (usbduxsub_tmp
->ai_cmd_running
)
2714 /* we are still running a command */
2715 usbdux_ai_stop(usbduxsub_tmp
, 1);
2716 if (usbduxsub_tmp
->ao_cmd_running
)
2717 /* we are still running a command */
2718 usbdux_ao_stop(usbduxsub_tmp
, 1);
2719 comedi_usb_auto_unconfig(intf
);
2720 down(&start_stop_sem
);
2721 down(&usbduxsub_tmp
->sem
);
2722 tidy_up(usbduxsub_tmp
);
2723 up(&usbduxsub_tmp
->sem
);
2724 up(&start_stop_sem
);
2725 dev_info(&intf
->dev
, "comedi_: disconnected from the usb\n");
2728 static const struct usb_device_id usbduxsigma_usb_table
[] = {
2729 { USB_DEVICE(0x13d8, 0x0020) },
2730 { USB_DEVICE(0x13d8, 0x0021) },
2731 { USB_DEVICE(0x13d8, 0x0022) },
2734 MODULE_DEVICE_TABLE(usb
, usbduxsigma_usb_table
);
2736 static struct usb_driver usbduxsigma_usb_driver
= {
2737 .name
= "usbduxsigma",
2738 .probe
= usbduxsigma_usb_probe
,
2739 .disconnect
= usbduxsigma_usb_disconnect
,
2740 .id_table
= usbduxsigma_usb_table
,
2742 module_comedi_usb_driver(usbduxsigma_driver
, usbduxsigma_usb_driver
);
2744 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
2745 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com");
2746 MODULE_LICENSE("GPL");
2747 MODULE_FIRMWARE(FIRMWARE
);