1 #define DRIVER_VERSION "v0.6"
2 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
3 #define DRIVER_DESC "Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com"
5 comedi/drivers/usbdux.c
6 Copyright (C) 2011 Bernd Porr, Bernd.Porr@f2s.com
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 Description: University of Stirling USB DAQ & INCITE Technology Limited
26 Devices: [ITL] USB-DUX (usbduxsigma.o)
27 Author: Bernd Porr <BerndPorr@f2s.com>
32 * I must give credit here to Chris Baugher who
33 * wrote the driver for AT-MIO-16d. I used some parts of this
34 * driver. I also must give credits to David Brownell
35 * who supported me with the USB development.
37 * Note: the raw data from the A/D converter is 24 bit big endian
38 * anything else is little endian to/from the dux board
42 * 0.1: initial version
43 * 0.2: all basic functions implemented, digital I/O only for one port
44 * 0.3: proper vendor ID and driver name
45 * 0.4: fixed D/A voltage range
46 * 0.5: various bug fixes, health check at startup
47 * 0.6: corrected wrong input range
50 /* generates loads of debug info */
51 /* #define NOISY_DUX_DEBUGBUG */
53 #include <linux/kernel.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/input.h>
58 #include <linux/usb.h>
59 #include <linux/fcntl.h>
60 #include <linux/compiler.h>
61 #include <linux/firmware.h>
62 #include "comedi_fc.h"
63 #include "../comedidev.h"
65 #define BOARDNAME "usbduxsigma"
67 /* timeout for the USB-transfer in ms*/
68 #define BULK_TIMEOUT 1000
70 /* constants for "firmware" upload and download */
71 #define USBDUXSUB_FIRMWARE 0xA0
72 #define VENDOR_DIR_IN 0xC0
73 #define VENDOR_DIR_OUT 0x40
75 /* internal addresses of the 8051 processor */
76 #define USBDUXSUB_CPUCS 0xE600
79 * the minor device number, major is 180 only for debugging purposes and to
80 * upload special firmware (programming the eeprom etc) which is not
81 * compatible with the comedi framwork
83 #define USBDUXSUB_MINOR 32
85 /* max lenghth of the transfer-buffer for software upload */
88 /* Input endpoint number: ISO/IRQ */
91 /* Output endpoint number: ISO/IRQ */
94 /* This EP sends DUX commands to USBDUX */
95 #define COMMAND_OUT_EP 1
97 /* This EP receives the DUX commands from USBDUX */
98 #define COMMAND_IN_EP 8
100 /* Output endpoint for PWM */
103 /* 300Hz max frequ under PWM */
104 #define MIN_PWM_PERIOD ((long)(1E9/300))
106 /* Default PWM frequency */
107 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
109 /* Number of channels (16 AD and offset)*/
110 #define NUMCHANNELS 16
112 /* Size of one A/D value */
113 #define SIZEADIN ((sizeof(int32_t)))
116 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
119 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
122 #define SIZEINSNBUF 16
124 /* Number of DA channels */
125 #define NUMOUTCHANNELS 8
127 /* size of one value for the D/A converter: channel and value */
128 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(int16_t)))
131 * Size of the output-buffer in bytes
132 * Actually only the first 4 triplets are used but for the
133 * high speed mode we need to pad it to 8 (microframes).
135 #define SIZEOUTBUF ((8*SIZEDAOUT))
138 * Size of the buffer for the dux commands: just now max size is determined
139 * by the analogue out + command byte + panic bytes...
141 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
143 /* Number of in-URBs which receive the data: min=2 */
144 #define NUMOFINBUFFERSFULL 5
146 /* Number of out-URBs which send the data: min=2 */
147 #define NUMOFOUTBUFFERSFULL 5
149 /* Number of in-URBs which receive the data: min=5 */
150 /* must have more buffers due to buggy USB ctr */
151 #define NUMOFINBUFFERSHIGH 10
153 /* Number of out-URBs which send the data: min=5 */
154 /* must have more buffers due to buggy USB ctr */
155 #define NUMOFOUTBUFFERSHIGH 10
157 /* Total number of usbdux devices */
160 /* Analogue in subdevice */
163 /* Analogue out subdevice */
169 /* timer aka pwm output */
172 /* number of retries to get the right dux command */
175 /**************************************************/
176 /* comedi constants */
177 static const struct comedi_lrange range_usbdux_ai_range
= { 1, {
183 static const struct comedi_lrange range_usbdux_ao_range
= { 1, {
190 * private structure of one subdevice
194 * This is the structure which holds all the data of
195 * this driver one sub device just now: A/D
200 /* is it associated with a subdevice? */
202 /* pointer to the usb-device */
203 struct usb_device
*usbdev
;
204 /* actual number of in-buffers */
206 /* actual number of out-buffers */
208 /* ISO-transfer handling: buffers */
211 /* pwm-transfer handling */
214 unsigned int pwmPeriod
;
215 /* PWM internal delay for the GPIF in the FX2 */
217 /* size of the PWM buffer which holds the bit pattern */
219 /* input buffer for the ISO-transfer */
221 /* input buffer for single insn */
223 /* output buffer for single DA outputs */
225 /* interface number */
227 /* interface structure in 2.6 */
228 struct usb_interface
*interface
;
229 /* comedi device for the interrupt context */
230 struct comedi_device
*comedidev
;
231 /* is it USB_SPEED_HIGH or not? */
232 short int high_speed
;
233 /* asynchronous command is running */
234 short int ai_cmd_running
;
235 short int ao_cmd_running
;
237 short int pwm_cmd_running
;
238 /* continuous acquisition */
239 short int ai_continuous
;
240 short int ao_continuous
;
241 /* number of samples to acquire */
244 /* time between samples in units of the timer */
245 unsigned int ai_timer
;
246 unsigned int ao_timer
;
247 /* counter between acquisitions */
248 unsigned int ai_counter
;
249 unsigned int ao_counter
;
250 /* interval in frames/uframes */
251 unsigned int ai_interval
;
253 uint8_t *dac_commands
;
255 uint8_t *dux_commands
;
256 struct semaphore sem
;
260 * The pointer to the private usb-data of the driver is also the private data
261 * for the comedi-device. This has to be global as the usb subsystem needs
262 * global variables. The other reason is that this structure must be there
263 * _before_ any comedi command is issued. The usb subsystem must be initialised
264 * before comedi can access it.
266 static struct usbduxsub usbduxsub
[NUMUSBDUX
];
268 static DEFINE_SEMAPHORE(start_stop_sem
);
270 static struct comedi_driver driver_usbduxsigma
; /* see below for initializer */
273 * Stops the data acquision
274 * It should be safe to call this function from any context
276 static int usbduxsub_unlink_InURBs(struct usbduxsub
*usbduxsub_tmp
)
281 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbIn
) {
282 for (i
= 0; i
< usbduxsub_tmp
->numOfInBuffers
; i
++) {
283 if (usbduxsub_tmp
->urbIn
[i
]) {
284 /* We wait here until all transfers have been
286 usb_kill_urb(usbduxsub_tmp
->urbIn
[i
]);
288 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
289 "comedi: usbdux: unlinked InURB %d, err=%d\n",
297 * This will stop a running acquisition operation
298 * Is called from within this driver from both the
299 * interrupt context and from comedi
301 static int usbdux_ai_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
305 if (!this_usbduxsub
) {
306 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
309 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ai_stop\n");
313 ret
= usbduxsub_unlink_InURBs(this_usbduxsub
);
316 this_usbduxsub
->ai_cmd_running
= 0;
322 * This will cancel a running acquisition operation.
323 * This is called by comedi but never from inside the driver.
325 static int usbdux_ai_cancel(struct comedi_device
*dev
,
326 struct comedi_subdevice
*s
)
328 struct usbduxsub
*this_usbduxsub
;
331 /* force unlink of all urbs */
332 this_usbduxsub
= dev
->private;
336 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ai_cancel\n");
338 /* prevent other CPUs from submitting new commands just now */
339 down(&this_usbduxsub
->sem
);
340 if (!(this_usbduxsub
->probed
)) {
341 up(&this_usbduxsub
->sem
);
344 /* unlink only if the urb really has been submitted */
345 res
= usbdux_ai_stop(this_usbduxsub
, this_usbduxsub
->ai_cmd_running
);
346 up(&this_usbduxsub
->sem
);
350 /* analogue IN - interrupt service routine */
351 static void usbduxsub_ai_IsocIrq(struct urb
*urb
)
354 struct usbduxsub
*this_usbduxsub
;
355 struct comedi_device
*this_comedidev
;
356 struct comedi_subdevice
*s
;
358 unsigned int dio_state
;
360 /* the context variable points to the comedi device */
361 this_comedidev
= urb
->context
;
362 /* the private structure of the subdevice is struct usbduxsub */
363 this_usbduxsub
= this_comedidev
->private;
364 /* subdevice which is the AD converter */
365 s
= this_comedidev
->subdevices
+ SUBDEV_AD
;
367 /* first we test if something unusual has just happened */
368 switch (urb
->status
) {
370 /* copy the result in the transfer buffer */
371 memcpy(this_usbduxsub
->inBuffer
,
372 urb
->transfer_buffer
, SIZEINBUF
);
375 /* error in the ISOchronous data */
376 /* we don't copy the data into the transfer buffer */
377 /* and recycle the last data byte */
378 dev_dbg(&urb
->dev
->dev
,
379 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
380 this_usbduxsub
->comedidev
->minor
);
388 /* happens after an unlink command */
389 if (this_usbduxsub
->ai_cmd_running
) {
390 /* we are still running a command */
391 /* tell this comedi */
392 s
->async
->events
|= COMEDI_CB_EOA
;
393 s
->async
->events
|= COMEDI_CB_ERROR
;
394 comedi_event(this_usbduxsub
->comedidev
, s
);
395 /* stop the transfer w/o unlink */
396 usbdux_ai_stop(this_usbduxsub
, 0);
401 /* a real error on the bus */
402 /* pass error to comedi if we are really running a command */
403 if (this_usbduxsub
->ai_cmd_running
) {
404 dev_err(&urb
->dev
->dev
,
405 "Non-zero urb status received in ai intr "
406 "context: %d\n", urb
->status
);
407 s
->async
->events
|= COMEDI_CB_EOA
;
408 s
->async
->events
|= COMEDI_CB_ERROR
;
409 comedi_event(this_usbduxsub
->comedidev
, s
);
410 /* don't do an unlink here */
411 usbdux_ai_stop(this_usbduxsub
, 0);
417 * at this point we are reasonably sure that nothing dodgy has happened
418 * are we running a command?
420 if (unlikely((!(this_usbduxsub
->ai_cmd_running
)))) {
422 * not running a command, do not continue execution if no
423 * asynchronous command is running in particular not resubmit
428 urb
->dev
= this_usbduxsub
->usbdev
;
430 /* resubmit the urb */
431 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
432 if (unlikely(err
< 0)) {
433 dev_err(&urb
->dev
->dev
,
434 "comedi_: urb resubmit failed in int-context!"
437 if (err
== -EL2NSYNC
)
438 dev_err(&urb
->dev
->dev
,
439 "buggy USB host controller or bug in IRQ "
441 s
->async
->events
|= COMEDI_CB_EOA
;
442 s
->async
->events
|= COMEDI_CB_ERROR
;
443 comedi_event(this_usbduxsub
->comedidev
, s
);
444 /* don't do an unlink here */
445 usbdux_ai_stop(this_usbduxsub
, 0);
449 /* get the state of the dio pins to allow external trigger */
450 dio_state
= be32_to_cpu(this_usbduxsub
->inBuffer
[0]);
452 this_usbduxsub
->ai_counter
--;
453 if (likely(this_usbduxsub
->ai_counter
> 0))
456 /* timer zero, transfer measurements to comedi */
457 this_usbduxsub
->ai_counter
= this_usbduxsub
->ai_timer
;
459 /* test, if we transmit only a fixed number of samples */
460 if (!(this_usbduxsub
->ai_continuous
)) {
461 /* not continuous, fixed number of samples */
462 this_usbduxsub
->ai_sample_count
--;
463 /* all samples received? */
464 if (this_usbduxsub
->ai_sample_count
< 0) {
465 /* prevent a resubmit next time */
466 usbdux_ai_stop(this_usbduxsub
, 0);
467 /* say comedi that the acquistion is over */
468 s
->async
->events
|= COMEDI_CB_EOA
;
469 comedi_event(this_usbduxsub
->comedidev
, s
);
473 /* get the data from the USB bus and hand it over to comedi */
474 n
= s
->async
->cmd
.chanlist_len
;
475 for (i
= 0; i
< n
; i
++) {
476 /* transfer data, note first byte is the DIO state */
477 v
= be32_to_cpu(this_usbduxsub
->inBuffer
[i
+1]);
478 /* strip status byte */
480 /* convert to unsigned */
482 /* write the byte to the buffer */
483 err
= cfc_write_array_to_buffer(s
, &v
, sizeof(uint32_t));
484 if (unlikely(err
== 0)) {
485 /* buffer overflow */
486 usbdux_ai_stop(this_usbduxsub
, 0);
490 /* tell comedi that data is there */
491 s
->async
->events
|= COMEDI_CB_BLOCK
| COMEDI_CB_EOS
;
492 comedi_event(this_usbduxsub
->comedidev
, s
);
495 static int usbduxsub_unlink_OutURBs(struct usbduxsub
*usbduxsub_tmp
)
500 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbOut
) {
501 for (i
= 0; i
< usbduxsub_tmp
->numOfOutBuffers
; i
++) {
502 if (usbduxsub_tmp
->urbOut
[i
])
503 usb_kill_urb(usbduxsub_tmp
->urbOut
[i
]);
505 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
506 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
513 /* This will cancel a running acquisition operation
516 static int usbdux_ao_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
522 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ao_cancel\n");
525 ret
= usbduxsub_unlink_OutURBs(this_usbduxsub
);
527 this_usbduxsub
->ao_cmd_running
= 0;
532 /* force unlink, is called by comedi */
533 static int usbdux_ao_cancel(struct comedi_device
*dev
,
534 struct comedi_subdevice
*s
)
536 struct usbduxsub
*this_usbduxsub
= dev
->private;
542 /* prevent other CPUs from submitting a command just now */
543 down(&this_usbduxsub
->sem
);
544 if (!(this_usbduxsub
->probed
)) {
545 up(&this_usbduxsub
->sem
);
548 /* unlink only if it is really running */
549 res
= usbdux_ao_stop(this_usbduxsub
, this_usbduxsub
->ao_cmd_running
);
550 up(&this_usbduxsub
->sem
);
554 static void usbduxsub_ao_IsocIrq(struct urb
*urb
)
558 struct usbduxsub
*this_usbduxsub
;
559 struct comedi_device
*this_comedidev
;
560 struct comedi_subdevice
*s
;
562 /* the context variable points to the subdevice */
563 this_comedidev
= urb
->context
;
564 /* the private structure of the subdevice is struct usbduxsub */
565 this_usbduxsub
= this_comedidev
->private;
567 s
= this_comedidev
->subdevices
+ SUBDEV_DA
;
569 switch (urb
->status
) {
578 /* after an unlink command, unplug, ... etc */
579 /* no unlink needed here. Already shutting down. */
580 if (this_usbduxsub
->ao_cmd_running
) {
581 s
->async
->events
|= COMEDI_CB_EOA
;
582 comedi_event(this_usbduxsub
->comedidev
, s
);
583 usbdux_ao_stop(this_usbduxsub
, 0);
589 if (this_usbduxsub
->ao_cmd_running
) {
590 dev_err(&urb
->dev
->dev
,
591 "comedi_: Non-zero urb status received in ao "
592 "intr context: %d\n", urb
->status
);
593 s
->async
->events
|= COMEDI_CB_ERROR
;
594 s
->async
->events
|= COMEDI_CB_EOA
;
595 comedi_event(this_usbduxsub
->comedidev
, s
);
596 /* we do an unlink if we are in the high speed mode */
597 usbdux_ao_stop(this_usbduxsub
, 0);
602 /* are we actually running? */
603 if (!(this_usbduxsub
->ao_cmd_running
))
606 /* normal operation: executing a command in this subdevice */
607 this_usbduxsub
->ao_counter
--;
608 if ((int)this_usbduxsub
->ao_counter
<= 0) {
610 this_usbduxsub
->ao_counter
= this_usbduxsub
->ao_timer
;
612 /* handle non continuous acquisition */
613 if (!(this_usbduxsub
->ao_continuous
)) {
614 /* fixed number of samples */
615 this_usbduxsub
->ao_sample_count
--;
616 if (this_usbduxsub
->ao_sample_count
< 0) {
617 /* all samples transmitted */
618 usbdux_ao_stop(this_usbduxsub
, 0);
619 s
->async
->events
|= COMEDI_CB_EOA
;
620 comedi_event(this_usbduxsub
->comedidev
, s
);
621 /* no resubmit of the urb */
625 /* transmit data to the USB bus */
626 ((uint8_t *) (urb
->transfer_buffer
))[0] =
627 s
->async
->cmd
.chanlist_len
;
628 for (i
= 0; i
< s
->async
->cmd
.chanlist_len
; i
++) {
630 if (i
>= NUMOUTCHANNELS
)
633 /* pointer to the DA */
635 (&(((uint8_t *) urb
->transfer_buffer
)[i
* 2 + 1]));
636 /* get the data from comedi */
637 ret
= comedi_buf_get(s
->async
, &temp
);
639 datap
[1] = this_usbduxsub
->dac_commands
[i
];
640 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
641 /* datap[0],datap[1],datap[2]); */
643 dev_err(&urb
->dev
->dev
,
644 "comedi: buffer underflow\n");
645 s
->async
->events
|= COMEDI_CB_EOA
;
646 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
648 /* transmit data to comedi */
649 s
->async
->events
|= COMEDI_CB_BLOCK
;
650 comedi_event(this_usbduxsub
->comedidev
, s
);
653 urb
->transfer_buffer_length
= SIZEOUTBUF
;
654 urb
->dev
= this_usbduxsub
->usbdev
;
656 if (this_usbduxsub
->ao_cmd_running
) {
657 if (this_usbduxsub
->high_speed
) {
664 urb
->number_of_packets
= 1;
665 urb
->iso_frame_desc
[0].offset
= 0;
666 urb
->iso_frame_desc
[0].length
= SIZEOUTBUF
;
667 urb
->iso_frame_desc
[0].status
= 0;
668 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
670 dev_err(&urb
->dev
->dev
,
671 "comedi_: ao urb resubm failed in int-cont. "
674 dev_err(&urb
->dev
->dev
,
675 "buggy USB host controller or bug in "
678 s
->async
->events
|= COMEDI_CB_EOA
;
679 s
->async
->events
|= COMEDI_CB_ERROR
;
680 comedi_event(this_usbduxsub
->comedidev
, s
);
681 /* don't do an unlink here */
682 usbdux_ao_stop(this_usbduxsub
, 0);
687 static int usbduxsub_start(struct usbduxsub
*usbduxsub
)
690 uint8_t local_transfer_buffer
[16];
693 local_transfer_buffer
[0] = 0;
694 errcode
= usb_control_msg(usbduxsub
->usbdev
,
695 /* create a pipe for a control transfer */
696 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
697 /* bRequest, "Firmware" */
705 /* address of the transfer buffer */
706 local_transfer_buffer
,
712 dev_err(&usbduxsub
->interface
->dev
,
713 "comedi_: control msg failed (start)\n");
719 static int usbduxsub_stop(struct usbduxsub
*usbduxsub
)
723 uint8_t local_transfer_buffer
[16];
726 local_transfer_buffer
[0] = 1;
727 errcode
= usb_control_msg(usbduxsub
->usbdev
,
728 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
729 /* bRequest, "Firmware" */
736 0x0000, local_transfer_buffer
,
742 dev_err(&usbduxsub
->interface
->dev
,
743 "comedi_: control msg failed (stop)\n");
749 static int usbduxsub_upload(struct usbduxsub
*usbduxsub
,
750 uint8_t *local_transfer_buffer
,
751 unsigned int startAddr
, unsigned int len
)
755 errcode
= usb_control_msg(usbduxsub
->usbdev
,
756 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
757 /* brequest, firmware */
765 /* our local safe buffer */
766 local_transfer_buffer
,
771 dev_dbg(&usbduxsub
->interface
->dev
, "comedi_: result=%d\n", errcode
);
773 dev_err(&usbduxsub
->interface
->dev
,
774 "comedi_: upload failed\n");
780 /* the FX2LP has twice as much as the standard FX2 */
781 #define FIRMWARE_MAX_LEN 0x4000
783 static int firmwareUpload(struct usbduxsub
*usbduxsub
,
784 const u8
*firmwareBinary
, int sizeFirmware
)
792 if (sizeFirmware
> FIRMWARE_MAX_LEN
) {
793 dev_err(&usbduxsub
->interface
->dev
,
794 "usbduxsigma firmware binary it too large for FX2.\n");
798 /* we generate a local buffer for the firmware */
799 fwBuf
= kmemdup(firmwareBinary
, sizeFirmware
, GFP_KERNEL
);
801 dev_err(&usbduxsub
->interface
->dev
,
802 "comedi_: mem alloc for firmware failed\n");
806 ret
= usbduxsub_stop(usbduxsub
);
808 dev_err(&usbduxsub
->interface
->dev
,
809 "comedi_: can not stop firmware\n");
814 ret
= usbduxsub_upload(usbduxsub
, fwBuf
, 0, sizeFirmware
);
816 dev_err(&usbduxsub
->interface
->dev
,
817 "comedi_: firmware upload failed\n");
821 ret
= usbduxsub_start(usbduxsub
);
823 dev_err(&usbduxsub
->interface
->dev
,
824 "comedi_: can not start firmware\n");
832 static int usbduxsub_submit_InURBs(struct usbduxsub
*usbduxsub
)
839 /* Submit all URBs and start the transfer on the bus */
840 for (i
= 0; i
< usbduxsub
->numOfInBuffers
; i
++) {
841 /* in case of a resubmission after an unlink... */
842 usbduxsub
->urbIn
[i
]->interval
= usbduxsub
->ai_interval
;
843 usbduxsub
->urbIn
[i
]->context
= usbduxsub
->comedidev
;
844 usbduxsub
->urbIn
[i
]->dev
= usbduxsub
->usbdev
;
845 usbduxsub
->urbIn
[i
]->status
= 0;
846 usbduxsub
->urbIn
[i
]->transfer_flags
= URB_ISO_ASAP
;
847 dev_dbg(&usbduxsub
->interface
->dev
,
848 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
849 usbduxsub
->comedidev
->minor
, i
,
850 (usbduxsub
->urbIn
[i
]->context
),
851 (usbduxsub
->urbIn
[i
]->dev
),
852 (usbduxsub
->urbIn
[i
]->interval
));
853 errFlag
= usb_submit_urb(usbduxsub
->urbIn
[i
], GFP_ATOMIC
);
855 dev_err(&usbduxsub
->interface
->dev
,
856 "comedi_: ai: usb_submit_urb(%d) error %d\n",
864 static int usbduxsub_submit_OutURBs(struct usbduxsub
*usbduxsub
)
871 for (i
= 0; i
< usbduxsub
->numOfOutBuffers
; i
++) {
872 dev_dbg(&usbduxsub
->interface
->dev
,
873 "comedi_: submitting out-urb[%d]\n", i
);
874 /* in case of a resubmission after an unlink... */
875 usbduxsub
->urbOut
[i
]->context
= usbduxsub
->comedidev
;
876 usbduxsub
->urbOut
[i
]->dev
= usbduxsub
->usbdev
;
877 usbduxsub
->urbOut
[i
]->status
= 0;
878 usbduxsub
->urbOut
[i
]->transfer_flags
= URB_ISO_ASAP
;
879 errFlag
= usb_submit_urb(usbduxsub
->urbOut
[i
], GFP_ATOMIC
);
881 dev_err(&usbduxsub
->interface
->dev
,
882 "comedi_: ao: usb_submit_urb(%d) error %d\n",
890 static int chanToInterval(int nChannels
)
902 static int usbdux_ai_cmdtest(struct comedi_device
*dev
,
903 struct comedi_subdevice
*s
,
904 struct comedi_cmd
*cmd
)
907 unsigned int tmpTimer
;
908 struct usbduxsub
*this_usbduxsub
= dev
->private;
910 if (!(this_usbduxsub
->probed
))
913 dev_dbg(&this_usbduxsub
->interface
->dev
,
914 "comedi%d: usbdux_ai_cmdtest\n", dev
->minor
);
916 /* make sure triggers are valid */
917 /* Only immediate triggers are allowed */
918 tmp
= cmd
->start_src
;
919 cmd
->start_src
&= TRIG_NOW
| TRIG_INT
;
920 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
923 /* trigger should happen timed */
924 tmp
= cmd
->scan_begin_src
;
925 /* start a new _scan_ with a timer */
926 cmd
->scan_begin_src
&= TRIG_TIMER
;
927 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
930 /* scanning is continuous */
931 tmp
= cmd
->convert_src
;
932 cmd
->convert_src
&= TRIG_NOW
;
933 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
936 /* issue a trigger when scan is finished and start a new scan */
937 tmp
= cmd
->scan_end_src
;
938 cmd
->scan_end_src
&= TRIG_COUNT
;
939 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
942 /* trigger at the end of count events or not, stop condition or not */
944 cmd
->stop_src
&= TRIG_COUNT
| TRIG_NONE
;
945 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
952 * step 2: make sure trigger sources are unique and mutually compatible
953 * note that mutual compatibility is not an issue here
955 if (cmd
->scan_begin_src
!= TRIG_FOLLOW
&&
956 cmd
->scan_begin_src
!= TRIG_EXT
&&
957 cmd
->scan_begin_src
!= TRIG_TIMER
)
959 if (cmd
->stop_src
!= TRIG_COUNT
&& cmd
->stop_src
!= TRIG_NONE
)
965 /* step 3: make sure arguments are trivially compatible */
966 if (cmd
->start_arg
!= 0) {
971 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
972 /* internal trigger */
973 if (cmd
->scan_begin_arg
!= 0) {
974 cmd
->scan_begin_arg
= 0;
979 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
980 if (this_usbduxsub
->high_speed
) {
982 * In high speed mode microframes are possible.
983 * However, during one microframe we can roughly
984 * sample two channels. Thus, the more channels
985 * are in the channel list the more time we need.
987 i
= chanToInterval(cmd
->chanlist_len
);
988 if (cmd
->scan_begin_arg
< (1000000 / 8 * i
)) {
989 cmd
->scan_begin_arg
= 1000000 / 8 * i
;
992 /* now calc the real sampling rate with all the
995 ((unsigned int)(cmd
->scan_begin_arg
/ 125000)) *
997 if (cmd
->scan_begin_arg
!= tmpTimer
) {
998 cmd
->scan_begin_arg
= tmpTimer
;
1003 /* 1kHz scans every USB frame */
1004 if (cmd
->scan_begin_arg
< 1000000) {
1005 cmd
->scan_begin_arg
= 1000000;
1009 * calc the real sampling rate with the rounding errors
1011 tmpTimer
= ((unsigned int)(cmd
->scan_begin_arg
/
1012 1000000)) * 1000000;
1013 if (cmd
->scan_begin_arg
!= tmpTimer
) {
1014 cmd
->scan_begin_arg
= tmpTimer
;
1019 /* the same argument */
1020 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
1021 cmd
->scan_end_arg
= cmd
->chanlist_len
;
1025 if (cmd
->stop_src
== TRIG_COUNT
) {
1026 /* any count is allowed */
1029 if (cmd
->stop_arg
!= 0) {
1042 * creates the ADC command for the MAX1271
1043 * range is the range value from comedi
1045 static void create_adc_command(unsigned int chan
,
1050 (*muxsg0
) = (*muxsg0
) | (1 << chan
);
1052 (*muxsg1
) = (*muxsg1
) | (1 << (chan
-8));
1056 /* bulk transfers to usbdux */
1058 #define SENDADCOMMANDS 0
1059 #define SENDDACOMMANDS 1
1060 #define SENDDIOCONFIGCOMMAND 2
1061 #define SENDDIOBITSCOMMAND 3
1062 #define SENDSINGLEAD 4
1064 #define SENDPWMOFF 8
1066 static int send_dux_commands(struct usbduxsub
*this_usbduxsub
, int cmd_type
)
1070 this_usbduxsub
->dux_commands
[0] = cmd_type
;
1071 #ifdef NOISY_DUX_DEBUGBUG
1072 printk(KERN_DEBUG
"comedi%d: usbdux: dux_commands: ",
1073 this_usbduxsub
->comedidev
->minor
);
1074 for (result
= 0; result
< SIZEOFDUXBUFFER
; result
++)
1075 printk(" %02x", this_usbduxsub
->dux_commands
[result
]);
1078 result
= usb_bulk_msg(this_usbduxsub
->usbdev
,
1079 usb_sndbulkpipe(this_usbduxsub
->usbdev
,
1081 this_usbduxsub
->dux_commands
, SIZEOFDUXBUFFER
,
1082 &nsent
, BULK_TIMEOUT
);
1084 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1085 "could not transmit dux_command to the usb-device, "
1086 "err=%d\n", this_usbduxsub
->comedidev
->minor
, result
);
1091 static int receive_dux_commands(struct usbduxsub
*this_usbduxsub
, int command
)
1093 int result
= (-EFAULT
);
1097 for (i
= 0; i
< RETRIES
; i
++) {
1098 result
= usb_bulk_msg(this_usbduxsub
->usbdev
,
1099 usb_rcvbulkpipe(this_usbduxsub
->usbdev
,
1101 this_usbduxsub
->insnBuffer
, SIZEINSNBUF
,
1102 &nrec
, BULK_TIMEOUT
);
1104 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1105 "insn: USB error %d "
1106 "while receiving DUX command"
1107 "\n", this_usbduxsub
->comedidev
->minor
,
1111 if (this_usbduxsub
->insnBuffer
[0] == command
)
1114 /* this is only reached if the data has been requested a couple of
1116 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: insn: "
1117 "wrong data returned from firmware: want %d, got %d.\n",
1118 this_usbduxsub
->comedidev
->minor
, command
,
1119 this_usbduxsub
->insnBuffer
[0]);
1123 static int usbdux_ai_inttrig(struct comedi_device
*dev
,
1124 struct comedi_subdevice
*s
, unsigned int trignum
)
1127 struct usbduxsub
*this_usbduxsub
= dev
->private;
1128 if (!this_usbduxsub
)
1131 down(&this_usbduxsub
->sem
);
1132 if (!(this_usbduxsub
->probed
)) {
1133 up(&this_usbduxsub
->sem
);
1136 dev_dbg(&this_usbduxsub
->interface
->dev
,
1137 "comedi%d: usbdux_ai_inttrig\n", dev
->minor
);
1140 dev_err(&this_usbduxsub
->interface
->dev
,
1141 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1143 up(&this_usbduxsub
->sem
);
1146 if (!(this_usbduxsub
->ai_cmd_running
)) {
1147 this_usbduxsub
->ai_cmd_running
= 1;
1148 ret
= usbduxsub_submit_InURBs(this_usbduxsub
);
1150 dev_err(&this_usbduxsub
->interface
->dev
,
1151 "comedi%d: usbdux_ai_inttrig: "
1152 "urbSubmit: err=%d\n", dev
->minor
, ret
);
1153 this_usbduxsub
->ai_cmd_running
= 0;
1154 up(&this_usbduxsub
->sem
);
1157 s
->async
->inttrig
= NULL
;
1159 dev_err(&this_usbduxsub
->interface
->dev
,
1160 "comedi%d: ai_inttrig but acqu is already running\n",
1163 up(&this_usbduxsub
->sem
);
1167 static int usbdux_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1169 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
1172 struct usbduxsub
*this_usbduxsub
= dev
->private;
1178 if (!this_usbduxsub
)
1181 dev_dbg(&this_usbduxsub
->interface
->dev
,
1182 "comedi%d: usbdux_ai_cmd\n", dev
->minor
);
1184 /* block other CPUs from starting an ai_cmd */
1185 down(&this_usbduxsub
->sem
);
1187 if (!(this_usbduxsub
->probed
)) {
1188 up(&this_usbduxsub
->sem
);
1191 if (this_usbduxsub
->ai_cmd_running
) {
1192 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1193 "ai_cmd not possible. Another ai_cmd is running.\n",
1195 up(&this_usbduxsub
->sem
);
1198 /* set current channel of the running acquisition to zero */
1199 s
->async
->cur_chan
= 0;
1201 /* first the number of channels per time step */
1202 this_usbduxsub
->dux_commands
[1] = cmd
->chanlist_len
;
1205 this_usbduxsub
->dux_commands
[2] = 0x12;
1207 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
1208 this_usbduxsub
->dux_commands
[3] = 0x03;
1210 /* CONFIG3: differential channels off */
1211 this_usbduxsub
->dux_commands
[4] = 0x00;
1213 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
1214 chan
= CR_CHAN(cmd
->chanlist
[i
]);
1215 create_adc_command(chan
, &muxsg0
, &muxsg1
);
1216 if (i
>= NUMCHANNELS
) {
1217 dev_err(&this_usbduxsub
->interface
->dev
,
1218 "comedi%d: channel list too long\n",
1223 this_usbduxsub
->dux_commands
[5] = muxsg0
;
1224 this_usbduxsub
->dux_commands
[6] = muxsg1
;
1225 this_usbduxsub
->dux_commands
[7] = sysred
;
1227 dev_dbg(&this_usbduxsub
->interface
->dev
,
1228 "comedi %d: sending commands to the usb device: size=%u\n",
1229 dev
->minor
, NUMCHANNELS
);
1231 result
= send_dux_commands(this_usbduxsub
, SENDADCOMMANDS
);
1233 up(&this_usbduxsub
->sem
);
1237 if (this_usbduxsub
->high_speed
) {
1239 * every 2 channels get a time window of 125us. Thus, if we
1240 * sample all 16 channels we need 1ms. If we sample only one
1241 * channel we need only 125us
1243 this_usbduxsub
->ai_interval
=
1244 chanToInterval(cmd
->chanlist_len
);
1245 this_usbduxsub
->ai_timer
= cmd
->scan_begin_arg
/ (125000 *
1249 /* interval always 1ms */
1250 this_usbduxsub
->ai_interval
= 1;
1251 this_usbduxsub
->ai_timer
= cmd
->scan_begin_arg
/ 1000000;
1253 if (this_usbduxsub
->ai_timer
< 1) {
1254 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: ai_cmd: "
1255 "timer=%d, scan_begin_arg=%d. "
1256 "Not properly tested by cmdtest?\n", dev
->minor
,
1257 this_usbduxsub
->ai_timer
, cmd
->scan_begin_arg
);
1258 up(&this_usbduxsub
->sem
);
1261 this_usbduxsub
->ai_counter
= this_usbduxsub
->ai_timer
;
1263 if (cmd
->stop_src
== TRIG_COUNT
) {
1264 /* data arrives as one packet */
1265 this_usbduxsub
->ai_sample_count
= cmd
->stop_arg
;
1266 this_usbduxsub
->ai_continuous
= 0;
1268 /* continuous acquisition */
1269 this_usbduxsub
->ai_continuous
= 1;
1270 this_usbduxsub
->ai_sample_count
= 0;
1273 if (cmd
->start_src
== TRIG_NOW
) {
1274 /* enable this acquisition operation */
1275 this_usbduxsub
->ai_cmd_running
= 1;
1276 ret
= usbduxsub_submit_InURBs(this_usbduxsub
);
1278 this_usbduxsub
->ai_cmd_running
= 0;
1279 /* fixme: unlink here?? */
1280 up(&this_usbduxsub
->sem
);
1283 s
->async
->inttrig
= NULL
;
1286 /* don't enable the acquision operation */
1287 /* wait for an internal signal */
1288 s
->async
->inttrig
= usbdux_ai_inttrig
;
1290 up(&this_usbduxsub
->sem
);
1294 /* Mode 0 is used to get a single conversion on demand */
1295 static int usbdux_ai_insn_read(struct comedi_device
*dev
,
1296 struct comedi_subdevice
*s
,
1297 struct comedi_insn
*insn
, unsigned int *data
)
1303 struct usbduxsub
*this_usbduxsub
= dev
->private;
1308 if (!this_usbduxsub
)
1311 dev_dbg(&this_usbduxsub
->interface
->dev
,
1312 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1313 dev
->minor
, insn
->n
, insn
->subdev
);
1315 down(&this_usbduxsub
->sem
);
1316 if (!(this_usbduxsub
->probed
)) {
1317 up(&this_usbduxsub
->sem
);
1320 if (this_usbduxsub
->ai_cmd_running
) {
1321 dev_err(&this_usbduxsub
->interface
->dev
,
1322 "comedi%d: ai_insn_read not possible. "
1323 "Async Command is running.\n", dev
->minor
);
1324 up(&this_usbduxsub
->sem
);
1328 /* sample one channel */
1329 /* CONFIG0: chopper on */
1330 this_usbduxsub
->dux_commands
[1] = 0x16;
1332 /* CONFIG1: 2kHz sampling rate */
1333 this_usbduxsub
->dux_commands
[2] = 0x80;
1335 /* CONFIG3: differential channels off */
1336 this_usbduxsub
->dux_commands
[3] = 0x00;
1338 chan
= CR_CHAN(insn
->chanspec
);
1339 create_adc_command(chan
, &muxsg0
, &muxsg1
);
1341 this_usbduxsub
->dux_commands
[4] = muxsg0
;
1342 this_usbduxsub
->dux_commands
[5] = muxsg1
;
1343 this_usbduxsub
->dux_commands
[6] = sysred
;
1346 err
= send_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1348 up(&this_usbduxsub
->sem
);
1352 for (i
= 0; i
< insn
->n
; i
++) {
1353 err
= receive_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1355 up(&this_usbduxsub
->sem
);
1358 /* 32 bits big endian from the A/D converter */
1359 one
= be32_to_cpu(*((int32_t *)
1360 ((this_usbduxsub
->insnBuffer
)+1)));
1361 /* mask out the staus byte */
1362 one
= one
& 0x00ffffff;
1363 /* turn it into an unsigned integer */
1364 one
= one
^ 0x00800000;
1367 up(&this_usbduxsub
->sem
);
1374 static int usbdux_getstatusinfo(struct comedi_device
*dev
, int chan
)
1376 struct usbduxsub
*this_usbduxsub
= dev
->private;
1381 if (!this_usbduxsub
)
1384 if (this_usbduxsub
->ai_cmd_running
) {
1385 dev_err(&this_usbduxsub
->interface
->dev
,
1386 "comedi%d: status read not possible. "
1387 "Async Command is running.\n", dev
->minor
);
1392 this_usbduxsub
->dux_commands
[1] = 0x12;
1394 /* CONFIG1: 2kHz sampling rate */
1395 this_usbduxsub
->dux_commands
[2] = 0x80;
1397 /* CONFIG3: differential channels off */
1398 this_usbduxsub
->dux_commands
[3] = 0x00;
1402 sysred
= sysred
| 1;
1403 } else if (chan
== 2) {
1405 sysred
= sysred
| 4;
1406 } else if (chan
== 3) {
1408 sysred
= sysred
| 8;
1409 } else if (chan
== 4) {
1411 sysred
= sysred
| 16;
1412 } else if (chan
== 5) {
1414 sysred
= sysred
| 32;
1417 this_usbduxsub
->dux_commands
[4] = 0;
1418 this_usbduxsub
->dux_commands
[5] = 0;
1419 this_usbduxsub
->dux_commands
[6] = sysred
;
1422 err
= send_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1426 err
= receive_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1430 /* 32 bits big endian from the A/D converter */
1431 one
= be32_to_cpu(*((int32_t *)((this_usbduxsub
->insnBuffer
)+1)));
1432 /* mask out the staus byte */
1433 one
= one
& 0x00ffffff;
1434 one
= one
^ 0x00800000;
1444 /************************************/
1447 static int usbdux_ao_insn_read(struct comedi_device
*dev
,
1448 struct comedi_subdevice
*s
,
1449 struct comedi_insn
*insn
, unsigned int *data
)
1452 int chan
= CR_CHAN(insn
->chanspec
);
1453 struct usbduxsub
*this_usbduxsub
= dev
->private;
1455 if (!this_usbduxsub
)
1458 down(&this_usbduxsub
->sem
);
1459 if (!(this_usbduxsub
->probed
)) {
1460 up(&this_usbduxsub
->sem
);
1463 for (i
= 0; i
< insn
->n
; i
++)
1464 data
[i
] = this_usbduxsub
->outBuffer
[chan
];
1466 up(&this_usbduxsub
->sem
);
1470 static int usbdux_ao_insn_write(struct comedi_device
*dev
,
1471 struct comedi_subdevice
*s
,
1472 struct comedi_insn
*insn
, unsigned int *data
)
1475 int chan
= CR_CHAN(insn
->chanspec
);
1476 struct usbduxsub
*this_usbduxsub
= dev
->private;
1478 if (!this_usbduxsub
)
1481 dev_dbg(&this_usbduxsub
->interface
->dev
,
1482 "comedi%d: ao_insn_write\n", dev
->minor
);
1484 down(&this_usbduxsub
->sem
);
1485 if (!(this_usbduxsub
->probed
)) {
1486 up(&this_usbduxsub
->sem
);
1489 if (this_usbduxsub
->ao_cmd_running
) {
1490 dev_err(&this_usbduxsub
->interface
->dev
,
1491 "comedi%d: ao_insn_write: "
1492 "ERROR: asynchronous ao_cmd is running\n", dev
->minor
);
1493 up(&this_usbduxsub
->sem
);
1497 for (i
= 0; i
< insn
->n
; i
++) {
1498 dev_dbg(&this_usbduxsub
->interface
->dev
,
1499 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1500 dev
->minor
, chan
, i
, data
[i
]);
1502 /* number of channels: 1 */
1503 this_usbduxsub
->dux_commands
[1] = 1;
1504 /* channel number */
1505 this_usbduxsub
->dux_commands
[2] = data
[i
];
1506 this_usbduxsub
->outBuffer
[chan
] = data
[i
];
1507 this_usbduxsub
->dux_commands
[3] = chan
;
1508 err
= send_dux_commands(this_usbduxsub
, SENDDACOMMANDS
);
1510 up(&this_usbduxsub
->sem
);
1514 up(&this_usbduxsub
->sem
);
1519 static int usbdux_ao_inttrig(struct comedi_device
*dev
,
1520 struct comedi_subdevice
*s
, unsigned int trignum
)
1523 struct usbduxsub
*this_usbduxsub
= dev
->private;
1525 if (!this_usbduxsub
)
1528 down(&this_usbduxsub
->sem
);
1530 if (!(this_usbduxsub
->probed
)) {
1535 dev_err(&this_usbduxsub
->interface
->dev
,
1536 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1541 if (!(this_usbduxsub
->ao_cmd_running
)) {
1542 this_usbduxsub
->ao_cmd_running
= 1;
1543 ret
= usbduxsub_submit_OutURBs(this_usbduxsub
);
1545 dev_err(&this_usbduxsub
->interface
->dev
,
1546 "comedi%d: usbdux_ao_inttrig: submitURB: "
1547 "err=%d\n", dev
->minor
, ret
);
1548 this_usbduxsub
->ao_cmd_running
= 0;
1551 s
->async
->inttrig
= NULL
;
1553 dev_err(&this_usbduxsub
->interface
->dev
,
1554 "comedi%d: ao_inttrig but acqu is already running.\n",
1559 up(&this_usbduxsub
->sem
);
1563 static int usbdux_ao_cmdtest(struct comedi_device
*dev
,
1564 struct comedi_subdevice
*s
,
1565 struct comedi_cmd
*cmd
)
1568 struct usbduxsub
*this_usbduxsub
= dev
->private;
1570 if (!this_usbduxsub
)
1573 if (!(this_usbduxsub
->probed
))
1576 dev_dbg(&this_usbduxsub
->interface
->dev
,
1577 "comedi%d: usbdux_ao_cmdtest\n", dev
->minor
);
1579 /* make sure triggers are valid */
1580 /* Only immediate triggers are allowed */
1581 tmp
= cmd
->start_src
;
1582 cmd
->start_src
&= TRIG_NOW
| TRIG_INT
;
1583 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
1586 /* trigger should happen timed */
1587 tmp
= cmd
->scan_begin_src
;
1588 /* just now we scan also in the high speed mode every frame */
1589 /* this is due to ehci driver limitations */
1590 if (0) { /* (this_usbduxsub->high_speed) */
1591 /* start immediately a new scan */
1592 /* the sampling rate is set by the coversion rate */
1593 cmd
->scan_begin_src
&= TRIG_FOLLOW
;
1595 /* start a new scan (output at once) with a timer */
1596 cmd
->scan_begin_src
&= TRIG_TIMER
;
1598 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
1601 /* scanning is continuous */
1602 tmp
= cmd
->convert_src
;
1604 /* all conversion events happen simultaneously */
1605 cmd
->convert_src
&= TRIG_NOW
;
1607 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
1610 /* issue a trigger when scan is finished and start a new scan */
1611 tmp
= cmd
->scan_end_src
;
1612 cmd
->scan_end_src
&= TRIG_COUNT
;
1613 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
1616 /* trigger at the end of count events or not, stop condition or not */
1617 tmp
= cmd
->stop_src
;
1618 cmd
->stop_src
&= TRIG_COUNT
| TRIG_NONE
;
1619 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
1626 * step 2: make sure trigger sources
1627 * are unique and mutually compatible
1628 * note that mutual compatibility is not an issue here
1630 if (cmd
->scan_begin_src
!= TRIG_FOLLOW
&&
1631 cmd
->scan_begin_src
!= TRIG_EXT
&&
1632 cmd
->scan_begin_src
!= TRIG_TIMER
)
1634 if (cmd
->stop_src
!= TRIG_COUNT
&& cmd
->stop_src
!= TRIG_NONE
)
1640 /* step 3: make sure arguments are trivially compatible */
1642 if (cmd
->start_arg
!= 0) {
1647 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
1648 /* internal trigger */
1649 if (cmd
->scan_begin_arg
!= 0) {
1650 cmd
->scan_begin_arg
= 0;
1655 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
1657 if (cmd
->scan_begin_arg
< 1000000) {
1658 cmd
->scan_begin_arg
= 1000000;
1662 /* not used now, is for later use */
1663 if (cmd
->convert_src
== TRIG_TIMER
) {
1664 if (cmd
->convert_arg
< 125000) {
1665 cmd
->convert_arg
= 125000;
1670 /* the same argument */
1671 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
1672 cmd
->scan_end_arg
= cmd
->chanlist_len
;
1676 if (cmd
->stop_src
== TRIG_COUNT
) {
1677 /* any count is allowed */
1680 if (cmd
->stop_arg
!= 0) {
1686 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi%d: err=%d, "
1687 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1688 "convert_arg=%d\n", dev
->minor
, err
, cmd
->scan_begin_src
,
1689 cmd
->scan_begin_arg
, cmd
->convert_src
, cmd
->convert_arg
);
1697 static int usbdux_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1699 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
1700 unsigned int chan
, gain
;
1702 struct usbduxsub
*this_usbduxsub
= dev
->private;
1704 if (!this_usbduxsub
)
1707 down(&this_usbduxsub
->sem
);
1708 if (!(this_usbduxsub
->probed
)) {
1709 up(&this_usbduxsub
->sem
);
1712 dev_dbg(&this_usbduxsub
->interface
->dev
,
1713 "comedi%d: %s\n", dev
->minor
, __func__
);
1715 /* set current channel of the running acquisition to zero */
1716 s
->async
->cur_chan
= 0;
1717 for (i
= 0; i
< cmd
->chanlist_len
; ++i
) {
1718 chan
= CR_CHAN(cmd
->chanlist
[i
]);
1719 gain
= CR_RANGE(cmd
->chanlist
[i
]);
1720 if (i
>= NUMOUTCHANNELS
) {
1721 dev_err(&this_usbduxsub
->interface
->dev
,
1722 "comedi%d: %s: channel list too long\n",
1723 dev
->minor
, __func__
);
1726 this_usbduxsub
->dac_commands
[i
] = chan
;
1727 dev_dbg(&this_usbduxsub
->interface
->dev
,
1728 "comedi%d: dac command for ch %d is %x\n",
1729 dev
->minor
, i
, this_usbduxsub
->dac_commands
[i
]);
1732 /* we count in steps of 1ms (125us) */
1733 /* 125us mode not used yet */
1734 if (0) { /* (this_usbduxsub->high_speed) */
1736 /* timing of the conversion itself: every 125 us */
1737 this_usbduxsub
->ao_timer
= cmd
->convert_arg
/ 125000;
1740 /* timing of the scan: we get all channels at once */
1741 this_usbduxsub
->ao_timer
= cmd
->scan_begin_arg
/ 1000000;
1742 dev_dbg(&this_usbduxsub
->interface
->dev
,
1743 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1744 "convert_src=%d, convert_arg=%d\n", dev
->minor
,
1745 cmd
->scan_begin_src
, cmd
->scan_begin_arg
,
1746 cmd
->convert_src
, cmd
->convert_arg
);
1747 dev_dbg(&this_usbduxsub
->interface
->dev
,
1748 "comedi%d: ao_timer=%d (ms)\n",
1749 dev
->minor
, this_usbduxsub
->ao_timer
);
1750 if (this_usbduxsub
->ao_timer
< 1) {
1751 dev_err(&this_usbduxsub
->interface
->dev
,
1752 "comedi%d: usbdux: ao_timer=%d, "
1753 "scan_begin_arg=%d. "
1754 "Not properly tested by cmdtest?\n",
1755 dev
->minor
, this_usbduxsub
->ao_timer
,
1756 cmd
->scan_begin_arg
);
1757 up(&this_usbduxsub
->sem
);
1761 this_usbduxsub
->ao_counter
= this_usbduxsub
->ao_timer
;
1763 if (cmd
->stop_src
== TRIG_COUNT
) {
1764 /* not continuous */
1766 /* high speed also scans everything at once */
1767 if (0) { /* (this_usbduxsub->high_speed) */
1768 this_usbduxsub
->ao_sample_count
=
1769 (cmd
->stop_arg
) * (cmd
->scan_end_arg
);
1771 /* there's no scan as the scan has been */
1772 /* perf inside the FX2 */
1773 /* data arrives as one packet */
1774 this_usbduxsub
->ao_sample_count
= cmd
->stop_arg
;
1776 this_usbduxsub
->ao_continuous
= 0;
1778 /* continuous acquisition */
1779 this_usbduxsub
->ao_continuous
= 1;
1780 this_usbduxsub
->ao_sample_count
= 0;
1783 if (cmd
->start_src
== TRIG_NOW
) {
1784 /* enable this acquisition operation */
1785 this_usbduxsub
->ao_cmd_running
= 1;
1786 ret
= usbduxsub_submit_OutURBs(this_usbduxsub
);
1788 this_usbduxsub
->ao_cmd_running
= 0;
1789 /* fixme: unlink here?? */
1790 up(&this_usbduxsub
->sem
);
1793 s
->async
->inttrig
= NULL
;
1796 /* submit the urbs later */
1797 /* wait for an internal signal */
1798 s
->async
->inttrig
= usbdux_ao_inttrig
;
1801 up(&this_usbduxsub
->sem
);
1805 static int usbdux_dio_insn_config(struct comedi_device
*dev
,
1806 struct comedi_subdevice
*s
,
1807 struct comedi_insn
*insn
, unsigned int *data
)
1809 int chan
= CR_CHAN(insn
->chanspec
);
1811 /* The input or output configuration of each digital line is
1812 * configured by a special insn_config instruction. chanspec
1813 * contains the channel to be changed, and data[0] contains the
1814 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1817 case INSN_CONFIG_DIO_OUTPUT
:
1818 s
->io_bits
|= 1 << chan
; /* 1 means Out */
1820 case INSN_CONFIG_DIO_INPUT
:
1821 s
->io_bits
&= ~(1 << chan
);
1823 case INSN_CONFIG_DIO_QUERY
:
1825 (s
->io_bits
& (1 << chan
)) ? COMEDI_OUTPUT
: COMEDI_INPUT
;
1831 /* we don't tell the firmware here as it would take 8 frames */
1832 /* to submit the information. We do it in the insn_bits. */
1836 static int usbdux_dio_insn_bits(struct comedi_device
*dev
,
1837 struct comedi_subdevice
*s
,
1838 struct comedi_insn
*insn
,
1842 struct usbduxsub
*this_usbduxsub
= dev
->private;
1845 if (!this_usbduxsub
)
1851 down(&this_usbduxsub
->sem
);
1853 if (!(this_usbduxsub
->probed
)) {
1854 up(&this_usbduxsub
->sem
);
1858 /* The insn data is a mask in data[0] and the new data
1859 * in data[1], each channel cooresponding to a bit. */
1860 s
->state
&= ~data
[0];
1861 s
->state
|= data
[0] & data
[1];
1862 /* The commands are 8 bits wide */
1863 this_usbduxsub
->dux_commands
[1] = (s
->io_bits
) & 0x000000FF;
1864 this_usbduxsub
->dux_commands
[4] = (s
->state
) & 0x000000FF;
1865 this_usbduxsub
->dux_commands
[2] = ((s
->io_bits
) & 0x0000FF00) >> 8;
1866 this_usbduxsub
->dux_commands
[5] = ((s
->state
) & 0x0000FF00) >> 8;
1867 this_usbduxsub
->dux_commands
[3] = ((s
->io_bits
) & 0x00FF0000) >> 16;
1868 this_usbduxsub
->dux_commands
[6] = ((s
->state
) & 0x00FF0000) >> 16;
1870 /* This command also tells the firmware to return */
1871 /* the digital input lines */
1872 err
= send_dux_commands(this_usbduxsub
, SENDDIOBITSCOMMAND
);
1874 up(&this_usbduxsub
->sem
);
1877 err
= receive_dux_commands(this_usbduxsub
, SENDDIOBITSCOMMAND
);
1879 up(&this_usbduxsub
->sem
);
1883 data
[1] = (((unsigned int)(this_usbduxsub
->insnBuffer
[1]))&0xff) |
1884 ((((unsigned int)(this_usbduxsub
->insnBuffer
[2]))&0xff) << 8) |
1885 ((((unsigned int)(this_usbduxsub
->insnBuffer
[3]))&0xff) << 16);
1889 up(&this_usbduxsub
->sem
);
1893 /***********************************/
1896 static int usbduxsub_unlink_PwmURBs(struct usbduxsub
*usbduxsub_tmp
)
1900 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbPwm
) {
1901 if (usbduxsub_tmp
->urbPwm
)
1902 usb_kill_urb(usbduxsub_tmp
->urbPwm
);
1903 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
1904 "comedi: unlinked PwmURB: res=%d\n", err
);
1909 /* This cancels a running acquisition operation
1912 static int usbdux_pwm_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
1916 if (!this_usbduxsub
)
1919 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: %s\n", __func__
);
1921 ret
= usbduxsub_unlink_PwmURBs(this_usbduxsub
);
1923 this_usbduxsub
->pwm_cmd_running
= 0;
1928 /* force unlink - is called by comedi */
1929 static int usbdux_pwm_cancel(struct comedi_device
*dev
,
1930 struct comedi_subdevice
*s
)
1932 struct usbduxsub
*this_usbduxsub
= dev
->private;
1935 /* unlink only if it is really running */
1936 res
= usbdux_pwm_stop(this_usbduxsub
, this_usbduxsub
->pwm_cmd_running
);
1938 dev_dbg(&this_usbduxsub
->interface
->dev
,
1939 "comedi %d: sending pwm off command to the usb device.\n",
1941 res
= send_dux_commands(this_usbduxsub
, SENDPWMOFF
);
1948 static void usbduxsub_pwm_irq(struct urb
*urb
)
1951 struct usbduxsub
*this_usbduxsub
;
1952 struct comedi_device
*this_comedidev
;
1953 struct comedi_subdevice
*s
;
1955 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1957 /* the context variable points to the subdevice */
1958 this_comedidev
= urb
->context
;
1959 /* the private structure of the subdevice is struct usbduxsub */
1960 this_usbduxsub
= this_comedidev
->private;
1962 s
= this_comedidev
->subdevices
+ SUBDEV_DA
;
1964 switch (urb
->status
) {
1974 * after an unlink command, unplug, ... etc
1975 * no unlink needed here. Already shutting down.
1977 if (this_usbduxsub
->pwm_cmd_running
)
1978 usbdux_pwm_stop(this_usbduxsub
, 0);
1984 if (this_usbduxsub
->pwm_cmd_running
) {
1985 dev_err(&this_usbduxsub
->interface
->dev
,
1986 "comedi_: Non-zero urb status received in "
1987 "pwm intr context: %d\n", urb
->status
);
1988 usbdux_pwm_stop(this_usbduxsub
, 0);
1993 /* are we actually running? */
1994 if (!(this_usbduxsub
->pwm_cmd_running
))
1997 urb
->transfer_buffer_length
= this_usbduxsub
->sizePwmBuf
;
1998 urb
->dev
= this_usbduxsub
->usbdev
;
2000 if (this_usbduxsub
->pwm_cmd_running
) {
2001 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
2003 dev_err(&this_usbduxsub
->interface
->dev
,
2004 "comedi_: pwm urb resubm failed in int-cont. "
2006 if (ret
== EL2NSYNC
)
2007 dev_err(&this_usbduxsub
->interface
->dev
,
2008 "buggy USB host controller or bug in "
2011 /* don't do an unlink here */
2012 usbdux_pwm_stop(this_usbduxsub
, 0);
2017 static int usbduxsub_submit_PwmURBs(struct usbduxsub
*usbduxsub
)
2024 dev_dbg(&usbduxsub
->interface
->dev
, "comedi_: submitting pwm-urb\n");
2026 /* in case of a resubmission after an unlink... */
2027 usb_fill_bulk_urb(usbduxsub
->urbPwm
,
2029 usb_sndbulkpipe(usbduxsub
->usbdev
, PWM_EP
),
2030 usbduxsub
->urbPwm
->transfer_buffer
,
2031 usbduxsub
->sizePwmBuf
, usbduxsub_pwm_irq
,
2032 usbduxsub
->comedidev
);
2034 errFlag
= usb_submit_urb(usbduxsub
->urbPwm
, GFP_ATOMIC
);
2036 dev_err(&usbduxsub
->interface
->dev
,
2037 "comedi_: usbduxsigma: pwm: usb_submit_urb error %d\n",
2044 static int usbdux_pwm_period(struct comedi_device
*dev
,
2045 struct comedi_subdevice
*s
, unsigned int period
)
2047 struct usbduxsub
*this_usbduxsub
= dev
->private;
2050 if (period
< MIN_PWM_PERIOD
) {
2051 dev_err(&this_usbduxsub
->interface
->dev
,
2052 "comedi%d: illegal period setting for pwm.\n",
2056 fx2delay
= period
/ ((int)(6 * 512 * (1.0 / 0.033))) - 6;
2057 if (fx2delay
> 255) {
2058 dev_err(&this_usbduxsub
->interface
->dev
,
2059 "comedi%d: period %d for pwm is too low.\n",
2060 dev
->minor
, period
);
2064 this_usbduxsub
->pwmDelay
= fx2delay
;
2065 this_usbduxsub
->pwmPeriod
= period
;
2066 dev_dbg(&this_usbduxsub
->interface
->dev
, "%s: frequ=%d, period=%d\n",
2067 __func__
, period
, fx2delay
);
2071 /* is called from insn so there's no need to do all the sanity checks */
2072 static int usbdux_pwm_start(struct comedi_device
*dev
,
2073 struct comedi_subdevice
*s
)
2076 struct usbduxsub
*this_usbduxsub
= dev
->private;
2078 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi%d: %s\n",
2079 dev
->minor
, __func__
);
2081 if (this_usbduxsub
->pwm_cmd_running
) {
2082 /* already running */
2086 this_usbduxsub
->dux_commands
[1] = ((uint8_t) this_usbduxsub
->pwmDelay
);
2087 ret
= send_dux_commands(this_usbduxsub
, SENDPWMON
);
2091 /* initialise the buffer */
2092 for (i
= 0; i
< this_usbduxsub
->sizePwmBuf
; i
++)
2093 ((char *)(this_usbduxsub
->urbPwm
->transfer_buffer
))[i
] = 0;
2095 this_usbduxsub
->pwm_cmd_running
= 1;
2096 ret
= usbduxsub_submit_PwmURBs(this_usbduxsub
);
2098 this_usbduxsub
->pwm_cmd_running
= 0;
2104 /* generates the bit pattern for PWM with the optional sign bit */
2105 static int usbdux_pwm_pattern(struct comedi_device
*dev
,
2106 struct comedi_subdevice
*s
, int channel
,
2107 unsigned int value
, unsigned int sign
)
2109 struct usbduxsub
*this_usbduxsub
= dev
->private;
2116 if (!this_usbduxsub
)
2119 /* this is the DIO bit which carries the PWM data */
2120 pwm_mask
= (1 << channel
);
2121 /* this is the DIO bit which carries the optional direction bit */
2122 sgn_mask
= (16 << channel
);
2123 /* this is the buffer which will be filled with the with bit */
2124 /* pattern for one period */
2125 szbuf
= this_usbduxsub
->sizePwmBuf
;
2126 pBuf
= (char *)(this_usbduxsub
->urbPwm
->transfer_buffer
);
2127 for (i
= 0; i
< szbuf
; i
++) {
2130 c
= c
& (~pwm_mask
);
2131 /* set the bit as long as the index is lower than the value */
2134 /* set the optional sign bit for a relay */
2136 /* positive value */
2137 c
= c
& (~sgn_mask
);
2139 /* negative value */
2147 static int usbdux_pwm_write(struct comedi_device
*dev
,
2148 struct comedi_subdevice
*s
,
2149 struct comedi_insn
*insn
, unsigned int *data
)
2151 struct usbduxsub
*this_usbduxsub
= dev
->private;
2153 if (!this_usbduxsub
)
2156 if ((insn
->n
) != 1) {
2158 * doesn't make sense to have more than one value here because
2159 * it would just overwrite the PWM buffer a couple of times
2165 * the sign is set via a special INSN only, this gives us 8 bits for
2167 * relay sign 0 by default
2169 return usbdux_pwm_pattern(dev
, s
, CR_CHAN(insn
->chanspec
), data
[0], 0);
2172 static int usbdux_pwm_read(struct comedi_device
*x1
,
2173 struct comedi_subdevice
*x2
, struct comedi_insn
*x3
,
2180 /* switches on/off PWM */
2181 static int usbdux_pwm_config(struct comedi_device
*dev
,
2182 struct comedi_subdevice
*s
,
2183 struct comedi_insn
*insn
, unsigned int *data
)
2185 struct usbduxsub
*this_usbduxsub
= dev
->private;
2187 case INSN_CONFIG_ARM
:
2189 dev_dbg(&this_usbduxsub
->interface
->dev
,
2190 "comedi%d: %s: pwm on\n", dev
->minor
, __func__
);
2192 * if not zero the PWM is limited to a certain time which is
2193 * not supported here
2197 return usbdux_pwm_start(dev
, s
);
2198 case INSN_CONFIG_DISARM
:
2199 dev_dbg(&this_usbduxsub
->interface
->dev
,
2200 "comedi%d: %s: pwm off\n", dev
->minor
, __func__
);
2201 return usbdux_pwm_cancel(dev
, s
);
2202 case INSN_CONFIG_GET_PWM_STATUS
:
2204 * to check if the USB transmission has failed or in case PWM
2205 * was limited to n cycles to check if it has terminated
2207 data
[1] = this_usbduxsub
->pwm_cmd_running
;
2209 case INSN_CONFIG_PWM_SET_PERIOD
:
2210 dev_dbg(&this_usbduxsub
->interface
->dev
,
2211 "comedi%d: %s: setting period\n", dev
->minor
,
2213 return usbdux_pwm_period(dev
, s
, data
[1]);
2214 case INSN_CONFIG_PWM_GET_PERIOD
:
2215 data
[1] = this_usbduxsub
->pwmPeriod
;
2217 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
2218 /* value in the first byte and the sign in the second for a
2220 return usbdux_pwm_pattern(dev
, s
,
2221 /* the channel number */
2222 CR_CHAN(insn
->chanspec
),
2223 /* actual PWM data */
2227 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
2228 /* values are not kept in this driver, nothing to return */
2235 /*****************************************************************/
2237 static void tidy_up(struct usbduxsub
*usbduxsub_tmp
)
2243 dev_dbg(&usbduxsub_tmp
->interface
->dev
, "comedi_: tiding up\n");
2245 /* shows the usb subsystem that the driver is down */
2246 if (usbduxsub_tmp
->interface
)
2247 usb_set_intfdata(usbduxsub_tmp
->interface
, NULL
);
2249 usbduxsub_tmp
->probed
= 0;
2251 if (usbduxsub_tmp
->urbIn
) {
2252 if (usbduxsub_tmp
->ai_cmd_running
) {
2253 usbduxsub_tmp
->ai_cmd_running
= 0;
2254 usbduxsub_unlink_InURBs(usbduxsub_tmp
);
2256 for (i
= 0; i
< usbduxsub_tmp
->numOfInBuffers
; i
++) {
2257 kfree(usbduxsub_tmp
->urbIn
[i
]->transfer_buffer
);
2258 usbduxsub_tmp
->urbIn
[i
]->transfer_buffer
= NULL
;
2259 usb_kill_urb(usbduxsub_tmp
->urbIn
[i
]);
2260 usb_free_urb(usbduxsub_tmp
->urbIn
[i
]);
2261 usbduxsub_tmp
->urbIn
[i
] = NULL
;
2263 kfree(usbduxsub_tmp
->urbIn
);
2264 usbduxsub_tmp
->urbIn
= NULL
;
2266 if (usbduxsub_tmp
->urbOut
) {
2267 if (usbduxsub_tmp
->ao_cmd_running
) {
2268 usbduxsub_tmp
->ao_cmd_running
= 0;
2269 usbduxsub_unlink_OutURBs(usbduxsub_tmp
);
2271 for (i
= 0; i
< usbduxsub_tmp
->numOfOutBuffers
; i
++) {
2272 if (usbduxsub_tmp
->urbOut
[i
]->transfer_buffer
) {
2273 kfree(usbduxsub_tmp
->
2274 urbOut
[i
]->transfer_buffer
);
2275 usbduxsub_tmp
->urbOut
[i
]->transfer_buffer
=
2278 if (usbduxsub_tmp
->urbOut
[i
]) {
2279 usb_kill_urb(usbduxsub_tmp
->urbOut
[i
]);
2280 usb_free_urb(usbduxsub_tmp
->urbOut
[i
]);
2281 usbduxsub_tmp
->urbOut
[i
] = NULL
;
2284 kfree(usbduxsub_tmp
->urbOut
);
2285 usbduxsub_tmp
->urbOut
= NULL
;
2287 if (usbduxsub_tmp
->urbPwm
) {
2288 if (usbduxsub_tmp
->pwm_cmd_running
) {
2289 usbduxsub_tmp
->pwm_cmd_running
= 0;
2290 usbduxsub_unlink_PwmURBs(usbduxsub_tmp
);
2292 kfree(usbduxsub_tmp
->urbPwm
->transfer_buffer
);
2293 usbduxsub_tmp
->urbPwm
->transfer_buffer
= NULL
;
2294 usb_kill_urb(usbduxsub_tmp
->urbPwm
);
2295 usb_free_urb(usbduxsub_tmp
->urbPwm
);
2296 usbduxsub_tmp
->urbPwm
= NULL
;
2298 kfree(usbduxsub_tmp
->inBuffer
);
2299 usbduxsub_tmp
->inBuffer
= NULL
;
2300 kfree(usbduxsub_tmp
->insnBuffer
);
2301 usbduxsub_tmp
->insnBuffer
= NULL
;
2302 kfree(usbduxsub_tmp
->outBuffer
);
2303 usbduxsub_tmp
->outBuffer
= NULL
;
2304 kfree(usbduxsub_tmp
->dac_commands
);
2305 usbduxsub_tmp
->dac_commands
= NULL
;
2306 kfree(usbduxsub_tmp
->dux_commands
);
2307 usbduxsub_tmp
->dux_commands
= NULL
;
2308 usbduxsub_tmp
->ai_cmd_running
= 0;
2309 usbduxsub_tmp
->ao_cmd_running
= 0;
2310 usbduxsub_tmp
->pwm_cmd_running
= 0;
2313 static void usbdux_firmware_request_complete_handler(const struct firmware
*fw
,
2316 struct usbduxsub
*usbduxsub_tmp
= context
;
2317 struct usb_interface
*uinterf
= usbduxsub_tmp
->interface
;
2321 dev_err(&uinterf
->dev
,
2322 "Firmware complete handler without firmware!\n");
2327 * we need to upload the firmware here because fw will be
2328 * freed once we've left this function
2330 ret
= firmwareUpload(usbduxsub_tmp
, fw
->data
, fw
->size
);
2333 dev_err(&uinterf
->dev
,
2334 "Could not upload firmware (err=%d)\n", ret
);
2337 comedi_usb_auto_config(uinterf
, &driver_usbduxsigma
);
2339 release_firmware(fw
);
2342 /* allocate memory for the urbs and initialise them */
2343 static int usbduxsigma_probe(struct usb_interface
*uinterf
,
2344 const struct usb_device_id
*id
)
2346 struct usb_device
*udev
= interface_to_usbdev(uinterf
);
2347 struct device
*dev
= &uinterf
->dev
;
2352 dev_dbg(dev
, "comedi_: usbdux_: "
2353 "finding a free structure for the usb-device\n");
2355 down(&start_stop_sem
);
2356 /* look for a free place in the usbdux array */
2358 for (i
= 0; i
< NUMUSBDUX
; i
++) {
2359 if (!(usbduxsub
[i
].probed
)) {
2367 dev_err(dev
, "Too many usbduxsigma-devices connected.\n");
2368 up(&start_stop_sem
);
2371 dev_dbg(dev
, "comedi_: usbdux: "
2372 "usbduxsub[%d] is ready to connect to comedi.\n", index
);
2374 sema_init(&(usbduxsub
[index
].sem
), 1);
2375 /* save a pointer to the usb device */
2376 usbduxsub
[index
].usbdev
= udev
;
2378 /* save the interface itself */
2379 usbduxsub
[index
].interface
= uinterf
;
2380 /* get the interface number from the interface */
2381 usbduxsub
[index
].ifnum
= uinterf
->altsetting
->desc
.bInterfaceNumber
;
2382 /* hand the private data over to the usb subsystem */
2383 /* will be needed for disconnect */
2384 usb_set_intfdata(uinterf
, &(usbduxsub
[index
]));
2386 dev_dbg(dev
, "comedi_: usbdux: ifnum=%d\n", usbduxsub
[index
].ifnum
);
2388 /* test if it is high speed (USB 2.0) */
2389 usbduxsub
[index
].high_speed
=
2390 (usbduxsub
[index
].usbdev
->speed
== USB_SPEED_HIGH
);
2392 /* create space for the commands of the DA converter */
2393 usbduxsub
[index
].dac_commands
= kzalloc(NUMOUTCHANNELS
, GFP_KERNEL
);
2394 if (!usbduxsub
[index
].dac_commands
) {
2395 dev_err(dev
, "comedi_: usbduxsigma: "
2396 "error alloc space for dac commands\n");
2397 tidy_up(&(usbduxsub
[index
]));
2398 up(&start_stop_sem
);
2401 /* create space for the commands going to the usb device */
2402 usbduxsub
[index
].dux_commands
= kzalloc(SIZEOFDUXBUFFER
, GFP_KERNEL
);
2403 if (!usbduxsub
[index
].dux_commands
) {
2404 dev_err(dev
, "comedi_: usbduxsigma: "
2405 "error alloc space for dux commands\n");
2406 tidy_up(&(usbduxsub
[index
]));
2407 up(&start_stop_sem
);
2410 /* create space for the in buffer and set it to zero */
2411 usbduxsub
[index
].inBuffer
= kzalloc(SIZEINBUF
, GFP_KERNEL
);
2412 if (!(usbduxsub
[index
].inBuffer
)) {
2413 dev_err(dev
, "comedi_: usbduxsigma: "
2414 "could not alloc space for inBuffer\n");
2415 tidy_up(&(usbduxsub
[index
]));
2416 up(&start_stop_sem
);
2419 /* create space of the instruction buffer */
2420 usbduxsub
[index
].insnBuffer
= kzalloc(SIZEINSNBUF
, GFP_KERNEL
);
2421 if (!(usbduxsub
[index
].insnBuffer
)) {
2422 dev_err(dev
, "comedi_: usbduxsigma: "
2423 "could not alloc space for insnBuffer\n");
2424 tidy_up(&(usbduxsub
[index
]));
2425 up(&start_stop_sem
);
2428 /* create space for the outbuffer */
2429 usbduxsub
[index
].outBuffer
= kzalloc(SIZEOUTBUF
, GFP_KERNEL
);
2430 if (!(usbduxsub
[index
].outBuffer
)) {
2431 dev_err(dev
, "comedi_: usbduxsigma: "
2432 "could not alloc space for outBuffer\n");
2433 tidy_up(&(usbduxsub
[index
]));
2434 up(&start_stop_sem
);
2437 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2438 i
= usb_set_interface(usbduxsub
[index
].usbdev
,
2439 usbduxsub
[index
].ifnum
, 3);
2441 dev_err(dev
, "comedi_: usbduxsigma%d: "
2442 "could not set alternate setting 3 in high speed.\n",
2444 tidy_up(&(usbduxsub
[index
]));
2445 up(&start_stop_sem
);
2448 if (usbduxsub
[index
].high_speed
)
2449 usbduxsub
[index
].numOfInBuffers
= NUMOFINBUFFERSHIGH
;
2451 usbduxsub
[index
].numOfInBuffers
= NUMOFINBUFFERSFULL
;
2453 usbduxsub
[index
].urbIn
=
2454 kzalloc(sizeof(struct urb
*) * usbduxsub
[index
].numOfInBuffers
,
2456 if (!(usbduxsub
[index
].urbIn
)) {
2457 dev_err(dev
, "comedi_: usbduxsigma: "
2458 "Could not alloc. urbIn array\n");
2459 tidy_up(&(usbduxsub
[index
]));
2460 up(&start_stop_sem
);
2463 for (i
= 0; i
< usbduxsub
[index
].numOfInBuffers
; i
++) {
2464 /* one frame: 1ms */
2465 usbduxsub
[index
].urbIn
[i
] = usb_alloc_urb(1, GFP_KERNEL
);
2466 if (usbduxsub
[index
].urbIn
[i
] == NULL
) {
2467 dev_err(dev
, "comedi_: usbduxsigma%d: "
2468 "Could not alloc. urb(%d)\n", index
, i
);
2469 tidy_up(&(usbduxsub
[index
]));
2470 up(&start_stop_sem
);
2473 usbduxsub
[index
].urbIn
[i
]->dev
= usbduxsub
[index
].usbdev
;
2474 /* will be filled later with a pointer to the comedi-device */
2475 /* and ONLY then the urb should be submitted */
2476 usbduxsub
[index
].urbIn
[i
]->context
= NULL
;
2477 usbduxsub
[index
].urbIn
[i
]->pipe
=
2478 usb_rcvisocpipe(usbduxsub
[index
].usbdev
, ISOINEP
);
2479 usbduxsub
[index
].urbIn
[i
]->transfer_flags
= URB_ISO_ASAP
;
2480 usbduxsub
[index
].urbIn
[i
]->transfer_buffer
=
2481 kzalloc(SIZEINBUF
, GFP_KERNEL
);
2482 if (!(usbduxsub
[index
].urbIn
[i
]->transfer_buffer
)) {
2483 dev_err(dev
, "comedi_: usbduxsigma%d: "
2484 "could not alloc. transb.\n", index
);
2485 tidy_up(&(usbduxsub
[index
]));
2486 up(&start_stop_sem
);
2489 usbduxsub
[index
].urbIn
[i
]->complete
= usbduxsub_ai_IsocIrq
;
2490 usbduxsub
[index
].urbIn
[i
]->number_of_packets
= 1;
2491 usbduxsub
[index
].urbIn
[i
]->transfer_buffer_length
= SIZEINBUF
;
2492 usbduxsub
[index
].urbIn
[i
]->iso_frame_desc
[0].offset
= 0;
2493 usbduxsub
[index
].urbIn
[i
]->iso_frame_desc
[0].length
=
2498 if (usbduxsub
[index
].high_speed
)
2499 usbduxsub
[index
].numOfOutBuffers
= NUMOFOUTBUFFERSHIGH
;
2501 usbduxsub
[index
].numOfOutBuffers
= NUMOFOUTBUFFERSFULL
;
2503 usbduxsub
[index
].urbOut
=
2504 kzalloc(sizeof(struct urb
*) * usbduxsub
[index
].numOfOutBuffers
,
2506 if (!(usbduxsub
[index
].urbOut
)) {
2507 dev_err(dev
, "comedi_: usbduxsigma: "
2508 "Could not alloc. urbOut array\n");
2509 tidy_up(&(usbduxsub
[index
]));
2510 up(&start_stop_sem
);
2513 for (i
= 0; i
< usbduxsub
[index
].numOfOutBuffers
; i
++) {
2514 /* one frame: 1ms */
2515 usbduxsub
[index
].urbOut
[i
] = usb_alloc_urb(1, GFP_KERNEL
);
2516 if (usbduxsub
[index
].urbOut
[i
] == NULL
) {
2517 dev_err(dev
, "comedi_: usbduxsigma%d: "
2518 "Could not alloc. urb(%d)\n", index
, i
);
2519 tidy_up(&(usbduxsub
[index
]));
2520 up(&start_stop_sem
);
2523 usbduxsub
[index
].urbOut
[i
]->dev
= usbduxsub
[index
].usbdev
;
2524 /* will be filled later with a pointer to the comedi-device */
2525 /* and ONLY then the urb should be submitted */
2526 usbduxsub
[index
].urbOut
[i
]->context
= NULL
;
2527 usbduxsub
[index
].urbOut
[i
]->pipe
=
2528 usb_sndisocpipe(usbduxsub
[index
].usbdev
, ISOOUTEP
);
2529 usbduxsub
[index
].urbOut
[i
]->transfer_flags
= URB_ISO_ASAP
;
2530 usbduxsub
[index
].urbOut
[i
]->transfer_buffer
=
2531 kzalloc(SIZEOUTBUF
, GFP_KERNEL
);
2532 if (!(usbduxsub
[index
].urbOut
[i
]->transfer_buffer
)) {
2533 dev_err(dev
, "comedi_: usbduxsigma%d: "
2534 "could not alloc. transb.\n", index
);
2535 tidy_up(&(usbduxsub
[index
]));
2536 up(&start_stop_sem
);
2539 usbduxsub
[index
].urbOut
[i
]->complete
= usbduxsub_ao_IsocIrq
;
2540 usbduxsub
[index
].urbOut
[i
]->number_of_packets
= 1;
2541 usbduxsub
[index
].urbOut
[i
]->transfer_buffer_length
=
2543 usbduxsub
[index
].urbOut
[i
]->iso_frame_desc
[0].offset
= 0;
2544 usbduxsub
[index
].urbOut
[i
]->iso_frame_desc
[0].length
=
2546 if (usbduxsub
[index
].high_speed
) {
2548 usbduxsub
[index
].urbOut
[i
]->interval
= 8;
2551 usbduxsub
[index
].urbOut
[i
]->interval
= 1;
2556 if (usbduxsub
[index
].high_speed
) {
2557 /* max bulk ep size in high speed */
2558 usbduxsub
[index
].sizePwmBuf
= 512;
2559 usbduxsub
[index
].urbPwm
= usb_alloc_urb(0, GFP_KERNEL
);
2560 if (usbduxsub
[index
].urbPwm
== NULL
) {
2561 dev_err(dev
, "comedi_: usbduxsigma%d: "
2562 "Could not alloc. pwm urb\n", index
);
2563 tidy_up(&(usbduxsub
[index
]));
2564 up(&start_stop_sem
);
2567 usbduxsub
[index
].urbPwm
->transfer_buffer
=
2568 kzalloc(usbduxsub
[index
].sizePwmBuf
, GFP_KERNEL
);
2569 if (!(usbduxsub
[index
].urbPwm
->transfer_buffer
)) {
2570 dev_err(dev
, "comedi_: usbduxsigma%d: "
2571 "could not alloc. transb. for pwm\n", index
);
2572 tidy_up(&(usbduxsub
[index
]));
2573 up(&start_stop_sem
);
2577 usbduxsub
[index
].urbPwm
= NULL
;
2578 usbduxsub
[index
].sizePwmBuf
= 0;
2581 usbduxsub
[index
].ai_cmd_running
= 0;
2582 usbduxsub
[index
].ao_cmd_running
= 0;
2583 usbduxsub
[index
].pwm_cmd_running
= 0;
2585 /* we've reached the bottom of the function */
2586 usbduxsub
[index
].probed
= 1;
2587 up(&start_stop_sem
);
2589 ret
= request_firmware_nowait(THIS_MODULE
,
2591 "usbduxsigma_firmware.bin",
2595 usbdux_firmware_request_complete_handler
2599 dev_err(dev
, "Could not load firmware (err=%d)\n", ret
);
2603 dev_info(dev
, "comedi_: successfully initialised.\n");
2608 static void usbduxsigma_disconnect(struct usb_interface
*intf
)
2610 struct usbduxsub
*usbduxsub_tmp
= usb_get_intfdata(intf
);
2611 struct usb_device
*udev
= interface_to_usbdev(intf
);
2613 if (!usbduxsub_tmp
) {
2615 "comedi_: disconnect called with null pointer.\n");
2618 if (usbduxsub_tmp
->usbdev
!= udev
) {
2619 dev_err(&intf
->dev
, "comedi_: BUG! wrong ptr!\n");
2622 if (usbduxsub_tmp
->ai_cmd_running
)
2623 /* we are still running a command */
2624 usbdux_ai_stop(usbduxsub_tmp
, 1);
2625 if (usbduxsub_tmp
->ao_cmd_running
)
2626 /* we are still running a command */
2627 usbdux_ao_stop(usbduxsub_tmp
, 1);
2628 comedi_usb_auto_unconfig(intf
);
2629 down(&start_stop_sem
);
2630 down(&usbduxsub_tmp
->sem
);
2631 tidy_up(usbduxsub_tmp
);
2632 up(&usbduxsub_tmp
->sem
);
2633 up(&start_stop_sem
);
2634 dev_info(&intf
->dev
, "comedi_: disconnected from the usb\n");
2637 /* is called when comedi-config is called */
2638 static int usbduxsigma_attach(struct comedi_device
*dev
,
2639 struct comedi_devconfig
*it
)
2644 struct usbduxsub
*udev
;
2648 struct comedi_subdevice
*s
= NULL
;
2649 dev
->private = NULL
;
2651 down(&start_stop_sem
);
2652 /* find a valid device which has been detected by the probe function of
2655 for (i
= 0; i
< NUMUSBDUX
; i
++) {
2656 if ((usbduxsub
[i
].probed
) && (!usbduxsub
[i
].attached
)) {
2663 printk(KERN_ERR
"comedi%d: usbduxsigma: error: attach failed,"
2664 "dev not connected to the usb bus.\n", dev
->minor
);
2665 up(&start_stop_sem
);
2669 udev
= &usbduxsub
[index
];
2671 /* pointer back to the corresponding comedi device */
2672 udev
->comedidev
= dev
;
2674 /* trying to upload the firmware into the FX2 */
2675 if (comedi_aux_data(it
->options
, 0) &&
2676 it
->options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
2677 firmwareUpload(udev
, comedi_aux_data(it
->options
, 0),
2678 it
->options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]);
2681 dev
->board_name
= BOARDNAME
;
2683 /* set number of subdevices */
2684 if (udev
->high_speed
) {
2686 dev
->n_subdevices
= 4;
2689 dev
->n_subdevices
= 3;
2692 /* allocate space for the subdevices */
2693 ret
= alloc_subdevices(dev
, dev
->n_subdevices
);
2695 dev_err(&udev
->interface
->dev
,
2696 "comedi%d: no space for subdev\n", dev
->minor
);
2698 up(&start_stop_sem
);
2702 /* private structure is also simply the usb-structure */
2703 dev
->private = udev
;
2705 /* the first subdevice is the A/D converter */
2706 s
= dev
->subdevices
+ SUBDEV_AD
;
2707 /* the URBs get the comedi subdevice */
2708 /* which is responsible for reading */
2709 /* this is the subdevice which reads data */
2710 dev
->read_subdev
= s
;
2711 /* the subdevice receives as private structure the */
2715 s
->type
= COMEDI_SUBD_AI
;
2716 /* readable and ref is to ground, 32 bit wide data! */
2717 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
|
2718 SDF_CMD_READ
| SDF_LSAMPL
;
2719 /* 16 A/D channels */
2720 s
->n_chan
= NUMCHANNELS
;
2721 /* length of the channellist */
2722 s
->len_chanlist
= NUMCHANNELS
;
2723 /* callback functions */
2724 s
->insn_read
= usbdux_ai_insn_read
;
2725 s
->do_cmdtest
= usbdux_ai_cmdtest
;
2726 s
->do_cmd
= usbdux_ai_cmd
;
2727 s
->cancel
= usbdux_ai_cancel
;
2728 /* max value from the A/D converter (24bit) */
2729 s
->maxdata
= 0x00FFFFFF;
2730 /* range table to convert to physical units */
2731 s
->range_table
= (&range_usbdux_ai_range
);
2734 s
= dev
->subdevices
+ SUBDEV_DA
;
2736 s
->type
= COMEDI_SUBD_AO
;
2737 /* backward pointer */
2738 dev
->write_subdev
= s
;
2739 /* the subdevice receives as private structure the */
2743 s
->subdev_flags
= SDF_WRITABLE
| SDF_GROUND
| SDF_CMD_WRITE
;
2746 /* length of the channellist */
2747 s
->len_chanlist
= 4;
2748 /* 8 bit resolution */
2749 s
->maxdata
= 0x00ff;
2750 /* unipolar range */
2751 s
->range_table
= (&range_usbdux_ao_range
);
2753 s
->do_cmdtest
= usbdux_ao_cmdtest
;
2754 s
->do_cmd
= usbdux_ao_cmd
;
2755 s
->cancel
= usbdux_ao_cancel
;
2756 s
->insn_read
= usbdux_ao_insn_read
;
2757 s
->insn_write
= usbdux_ao_insn_write
;
2760 s
= dev
->subdevices
+ SUBDEV_DIO
;
2761 s
->type
= COMEDI_SUBD_DIO
;
2762 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
2763 /* 8 external and 16 internal channels */
2766 s
->range_table
= (&range_digital
);
2767 s
->insn_bits
= usbdux_dio_insn_bits
;
2768 s
->insn_config
= usbdux_dio_insn_config
;
2769 /* we don't use it */
2772 if (udev
->high_speed
) {
2774 s
= dev
->subdevices
+ SUBDEV_PWM
;
2775 s
->type
= COMEDI_SUBD_PWM
;
2776 s
->subdev_flags
= SDF_WRITABLE
| SDF_PWM_HBRIDGE
;
2778 /* this defines the max duty cycle resolution */
2779 s
->maxdata
= udev
->sizePwmBuf
;
2780 s
->insn_write
= usbdux_pwm_write
;
2781 s
->insn_read
= usbdux_pwm_read
;
2782 s
->insn_config
= usbdux_pwm_config
;
2783 usbdux_pwm_period(dev
, s
, PWM_DEFAULT_PERIOD
);
2785 /* finally decide that it's attached */
2790 up(&start_stop_sem
);
2792 offset
= usbdux_getstatusinfo(dev
, 0);
2794 dev_err(&udev
->interface
->dev
,
2795 "Communication to USBDUXSIGMA failed!"
2796 "Check firmware and cabling.");
2798 dev_info(&udev
->interface
->dev
,
2799 "comedi%d: attached, ADC_zero = %x", dev
->minor
, offset
);
2804 static void usbduxsigma_detach(struct comedi_device
*dev
)
2806 struct usbduxsub
*usb
= dev
->private;
2810 dev
->private = NULL
;
2812 usb
->comedidev
= NULL
;
2817 /* main driver struct */
2818 static struct comedi_driver driver_usbduxsigma
= {
2819 .driver_name
= "usbduxsigma",
2820 .module
= THIS_MODULE
,
2821 .attach
= usbduxsigma_attach
,
2822 .detach
= usbduxsigma_detach
,
2825 /* Table with the USB-devices */
2826 static const struct usb_device_id usbduxsigma_table
[] = {
2827 {USB_DEVICE(0x13d8, 0x0020)},
2828 {USB_DEVICE(0x13d8, 0x0021)},
2829 {USB_DEVICE(0x13d8, 0x0022)},
2830 {} /* Terminating entry */
2833 MODULE_DEVICE_TABLE(usb
, usbduxsigma_table
);
2835 /* The usbduxsub-driver */
2836 static struct usb_driver usbduxsigma_driver
= {
2838 .probe
= usbduxsigma_probe
,
2839 .disconnect
= usbduxsigma_disconnect
,
2840 .id_table
= usbduxsigma_table
,
2843 /* Can't use the nice macro as I have also to initialise the USB */
2845 /* registering the usb-system _and_ the comedi-driver */
2846 static int __init
init_usbduxsigma(void)
2848 printk(KERN_INFO KBUILD_MODNAME
": "
2849 DRIVER_VERSION
":" DRIVER_DESC
"\n");
2850 usb_register(&usbduxsigma_driver
);
2851 comedi_driver_register(&driver_usbduxsigma
);
2855 /* deregistering the comedi driver and the usb-subsystem */
2856 static void __exit
exit_usbduxsigma(void)
2858 comedi_driver_unregister(&driver_usbduxsigma
);
2859 usb_deregister(&usbduxsigma_driver
);
2862 module_init(init_usbduxsigma
);
2863 module_exit(exit_usbduxsigma
);
2865 MODULE_AUTHOR(DRIVER_AUTHOR
);
2866 MODULE_DESCRIPTION(DRIVER_DESC
);
2867 MODULE_LICENSE("GPL");