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
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 /* continous aquisition */
239 short int ai_continous
;
240 short int ao_continous
;
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 aquisitions */
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
);
271 * Stops the data acquision
272 * It should be safe to call this function from any context
274 static int usbduxsub_unlink_InURBs(struct usbduxsub
*usbduxsub_tmp
)
279 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbIn
) {
280 for (i
= 0; i
< usbduxsub_tmp
->numOfInBuffers
; i
++) {
281 if (usbduxsub_tmp
->urbIn
[i
]) {
282 /* We wait here until all transfers have been
284 usb_kill_urb(usbduxsub_tmp
->urbIn
[i
]);
286 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
287 "comedi: usbdux: unlinked InURB %d, err=%d\n",
295 * This will stop a running acquisition operation
296 * Is called from within this driver from both the
297 * interrupt context and from comedi
299 static int usbdux_ai_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
303 if (!this_usbduxsub
) {
304 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
307 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ai_stop\n");
311 ret
= usbduxsub_unlink_InURBs(this_usbduxsub
);
314 this_usbduxsub
->ai_cmd_running
= 0;
320 * This will cancel a running acquisition operation.
321 * This is called by comedi but never from inside the driver.
323 static int usbdux_ai_cancel(struct comedi_device
*dev
,
324 struct comedi_subdevice
*s
)
326 struct usbduxsub
*this_usbduxsub
;
329 /* force unlink of all urbs */
330 this_usbduxsub
= dev
->private;
334 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ai_cancel\n");
336 /* prevent other CPUs from submitting new commands just now */
337 down(&this_usbduxsub
->sem
);
338 if (!(this_usbduxsub
->probed
)) {
339 up(&this_usbduxsub
->sem
);
342 /* unlink only if the urb really has been submitted */
343 res
= usbdux_ai_stop(this_usbduxsub
, this_usbduxsub
->ai_cmd_running
);
344 up(&this_usbduxsub
->sem
);
348 /* analogue IN - interrupt service routine */
349 static void usbduxsub_ai_IsocIrq(struct urb
*urb
)
352 struct usbduxsub
*this_usbduxsub
;
353 struct comedi_device
*this_comedidev
;
354 struct comedi_subdevice
*s
;
356 unsigned int dio_state
;
358 /* the context variable points to the comedi device */
359 this_comedidev
= urb
->context
;
360 /* the private structure of the subdevice is struct usbduxsub */
361 this_usbduxsub
= this_comedidev
->private;
362 /* subdevice which is the AD converter */
363 s
= this_comedidev
->subdevices
+ SUBDEV_AD
;
365 /* first we test if something unusual has just happened */
366 switch (urb
->status
) {
368 /* copy the result in the transfer buffer */
369 memcpy(this_usbduxsub
->inBuffer
,
370 urb
->transfer_buffer
, SIZEINBUF
);
373 /* error in the ISOchronous data */
374 /* we don't copy the data into the transfer buffer */
375 /* and recycle the last data byte */
376 dev_dbg(&urb
->dev
->dev
,
377 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
378 this_usbduxsub
->comedidev
->minor
);
386 /* happens after an unlink command */
387 if (this_usbduxsub
->ai_cmd_running
) {
388 /* we are still running a command */
389 /* tell this comedi */
390 s
->async
->events
|= COMEDI_CB_EOA
;
391 s
->async
->events
|= COMEDI_CB_ERROR
;
392 comedi_event(this_usbduxsub
->comedidev
, s
);
393 /* stop the transfer w/o unlink */
394 usbdux_ai_stop(this_usbduxsub
, 0);
399 /* a real error on the bus */
400 /* pass error to comedi if we are really running a command */
401 if (this_usbduxsub
->ai_cmd_running
) {
402 dev_err(&urb
->dev
->dev
,
403 "Non-zero urb status received in ai intr "
404 "context: %d\n", urb
->status
);
405 s
->async
->events
|= COMEDI_CB_EOA
;
406 s
->async
->events
|= COMEDI_CB_ERROR
;
407 comedi_event(this_usbduxsub
->comedidev
, s
);
408 /* don't do an unlink here */
409 usbdux_ai_stop(this_usbduxsub
, 0);
415 * at this point we are reasonably sure that nothing dodgy has happened
416 * are we running a command?
418 if (unlikely((!(this_usbduxsub
->ai_cmd_running
)))) {
420 * not running a command, do not continue execution if no
421 * asynchronous command is running in particular not resubmit
426 urb
->dev
= this_usbduxsub
->usbdev
;
428 /* resubmit the urb */
429 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
430 if (unlikely(err
< 0)) {
431 dev_err(&urb
->dev
->dev
,
432 "comedi_: urb resubmit failed in int-context!"
435 if (err
== -EL2NSYNC
)
436 dev_err(&urb
->dev
->dev
,
437 "buggy USB host controller or bug in IRQ "
439 s
->async
->events
|= COMEDI_CB_EOA
;
440 s
->async
->events
|= COMEDI_CB_ERROR
;
441 comedi_event(this_usbduxsub
->comedidev
, s
);
442 /* don't do an unlink here */
443 usbdux_ai_stop(this_usbduxsub
, 0);
447 /* get the state of the dio pins to allow external trigger */
448 dio_state
= be32_to_cpu(this_usbduxsub
->inBuffer
[0]);
450 this_usbduxsub
->ai_counter
--;
451 if (likely(this_usbduxsub
->ai_counter
> 0))
454 /* timer zero, transfer measurements to comedi */
455 this_usbduxsub
->ai_counter
= this_usbduxsub
->ai_timer
;
457 /* test, if we transmit only a fixed number of samples */
458 if (!(this_usbduxsub
->ai_continous
)) {
459 /* not continous, fixed number of samples */
460 this_usbduxsub
->ai_sample_count
--;
461 /* all samples received? */
462 if (this_usbduxsub
->ai_sample_count
< 0) {
463 /* prevent a resubmit next time */
464 usbdux_ai_stop(this_usbduxsub
, 0);
465 /* say comedi that the acquistion is over */
466 s
->async
->events
|= COMEDI_CB_EOA
;
467 comedi_event(this_usbduxsub
->comedidev
, s
);
471 /* get the data from the USB bus and hand it over to comedi */
472 n
= s
->async
->cmd
.chanlist_len
;
473 for (i
= 0; i
< n
; i
++) {
474 /* transfer data, note first byte is the DIO state */
475 v
= be32_to_cpu(this_usbduxsub
->inBuffer
[i
+1]);
476 /* strip status byte */
478 /* convert to unsigned */
480 /* write the byte to the buffer */
481 err
= cfc_write_array_to_buffer(s
, &v
, sizeof(uint32_t));
482 if (unlikely(err
== 0)) {
483 /* buffer overflow */
484 usbdux_ai_stop(this_usbduxsub
, 0);
488 /* tell comedi that data is there */
489 s
->async
->events
|= COMEDI_CB_BLOCK
| COMEDI_CB_EOS
;
490 comedi_event(this_usbduxsub
->comedidev
, s
);
493 static int usbduxsub_unlink_OutURBs(struct usbduxsub
*usbduxsub_tmp
)
498 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbOut
) {
499 for (i
= 0; i
< usbduxsub_tmp
->numOfOutBuffers
; i
++) {
500 if (usbduxsub_tmp
->urbOut
[i
])
501 usb_kill_urb(usbduxsub_tmp
->urbOut
[i
]);
503 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
504 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
511 /* This will cancel a running acquisition operation
514 static int usbdux_ao_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
520 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: usbdux_ao_cancel\n");
523 ret
= usbduxsub_unlink_OutURBs(this_usbduxsub
);
525 this_usbduxsub
->ao_cmd_running
= 0;
530 /* force unlink, is called by comedi */
531 static int usbdux_ao_cancel(struct comedi_device
*dev
,
532 struct comedi_subdevice
*s
)
534 struct usbduxsub
*this_usbduxsub
= dev
->private;
540 /* prevent other CPUs from submitting a command just now */
541 down(&this_usbduxsub
->sem
);
542 if (!(this_usbduxsub
->probed
)) {
543 up(&this_usbduxsub
->sem
);
546 /* unlink only if it is really running */
547 res
= usbdux_ao_stop(this_usbduxsub
, this_usbduxsub
->ao_cmd_running
);
548 up(&this_usbduxsub
->sem
);
552 static void usbduxsub_ao_IsocIrq(struct urb
*urb
)
556 struct usbduxsub
*this_usbduxsub
;
557 struct comedi_device
*this_comedidev
;
558 struct comedi_subdevice
*s
;
560 /* the context variable points to the subdevice */
561 this_comedidev
= urb
->context
;
562 /* the private structure of the subdevice is struct usbduxsub */
563 this_usbduxsub
= this_comedidev
->private;
565 s
= this_comedidev
->subdevices
+ SUBDEV_DA
;
567 switch (urb
->status
) {
576 /* after an unlink command, unplug, ... etc */
577 /* no unlink needed here. Already shutting down. */
578 if (this_usbduxsub
->ao_cmd_running
) {
579 s
->async
->events
|= COMEDI_CB_EOA
;
580 comedi_event(this_usbduxsub
->comedidev
, s
);
581 usbdux_ao_stop(this_usbduxsub
, 0);
587 if (this_usbduxsub
->ao_cmd_running
) {
588 dev_err(&urb
->dev
->dev
,
589 "comedi_: Non-zero urb status received in ao "
590 "intr context: %d\n", urb
->status
);
591 s
->async
->events
|= COMEDI_CB_ERROR
;
592 s
->async
->events
|= COMEDI_CB_EOA
;
593 comedi_event(this_usbduxsub
->comedidev
, s
);
594 /* we do an unlink if we are in the high speed mode */
595 usbdux_ao_stop(this_usbduxsub
, 0);
600 /* are we actually running? */
601 if (!(this_usbduxsub
->ao_cmd_running
))
604 /* normal operation: executing a command in this subdevice */
605 this_usbduxsub
->ao_counter
--;
606 if ((int)this_usbduxsub
->ao_counter
<= 0) {
608 this_usbduxsub
->ao_counter
= this_usbduxsub
->ao_timer
;
610 /* handle non continous aquisition */
611 if (!(this_usbduxsub
->ao_continous
)) {
612 /* fixed number of samples */
613 this_usbduxsub
->ao_sample_count
--;
614 if (this_usbduxsub
->ao_sample_count
< 0) {
615 /* all samples transmitted */
616 usbdux_ao_stop(this_usbduxsub
, 0);
617 s
->async
->events
|= COMEDI_CB_EOA
;
618 comedi_event(this_usbduxsub
->comedidev
, s
);
619 /* no resubmit of the urb */
623 /* transmit data to the USB bus */
624 ((uint8_t *) (urb
->transfer_buffer
))[0] =
625 s
->async
->cmd
.chanlist_len
;
626 for (i
= 0; i
< s
->async
->cmd
.chanlist_len
; i
++) {
628 if (i
>= NUMOUTCHANNELS
)
631 /* pointer to the DA */
633 (&(((uint8_t *) urb
->transfer_buffer
)[i
* 2 + 1]));
634 /* get the data from comedi */
635 ret
= comedi_buf_get(s
->async
, &temp
);
637 datap
[1] = this_usbduxsub
->dac_commands
[i
];
638 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
639 /* datap[0],datap[1],datap[2]); */
641 dev_err(&urb
->dev
->dev
,
642 "comedi: buffer underflow\n");
643 s
->async
->events
|= COMEDI_CB_EOA
;
644 s
->async
->events
|= COMEDI_CB_OVERFLOW
;
646 /* transmit data to comedi */
647 s
->async
->events
|= COMEDI_CB_BLOCK
;
648 comedi_event(this_usbduxsub
->comedidev
, s
);
651 urb
->transfer_buffer_length
= SIZEOUTBUF
;
652 urb
->dev
= this_usbduxsub
->usbdev
;
654 if (this_usbduxsub
->ao_cmd_running
) {
655 if (this_usbduxsub
->high_speed
) {
662 urb
->number_of_packets
= 1;
663 urb
->iso_frame_desc
[0].offset
= 0;
664 urb
->iso_frame_desc
[0].length
= SIZEOUTBUF
;
665 urb
->iso_frame_desc
[0].status
= 0;
666 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
668 dev_err(&urb
->dev
->dev
,
669 "comedi_: ao urb resubm failed in int-cont. "
672 dev_err(&urb
->dev
->dev
,
673 "buggy USB host controller or bug in "
676 s
->async
->events
|= COMEDI_CB_EOA
;
677 s
->async
->events
|= COMEDI_CB_ERROR
;
678 comedi_event(this_usbduxsub
->comedidev
, s
);
679 /* don't do an unlink here */
680 usbdux_ao_stop(this_usbduxsub
, 0);
685 static int usbduxsub_start(struct usbduxsub
*usbduxsub
)
688 uint8_t local_transfer_buffer
[16];
691 local_transfer_buffer
[0] = 0;
692 errcode
= usb_control_msg(usbduxsub
->usbdev
,
693 /* create a pipe for a control transfer */
694 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
695 /* bRequest, "Firmware" */
703 /* address of the transfer buffer */
704 local_transfer_buffer
,
710 dev_err(&usbduxsub
->interface
->dev
,
711 "comedi_: control msg failed (start)\n");
717 static int usbduxsub_stop(struct usbduxsub
*usbduxsub
)
721 uint8_t local_transfer_buffer
[16];
724 local_transfer_buffer
[0] = 1;
725 errcode
= usb_control_msg(usbduxsub
->usbdev
,
726 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
727 /* bRequest, "Firmware" */
734 0x0000, local_transfer_buffer
,
740 dev_err(&usbduxsub
->interface
->dev
,
741 "comedi_: control msg failed (stop)\n");
747 static int usbduxsub_upload(struct usbduxsub
*usbduxsub
,
748 uint8_t *local_transfer_buffer
,
749 unsigned int startAddr
, unsigned int len
)
753 errcode
= usb_control_msg(usbduxsub
->usbdev
,
754 usb_sndctrlpipe(usbduxsub
->usbdev
, 0),
755 /* brequest, firmware */
763 /* our local safe buffer */
764 local_transfer_buffer
,
769 dev_dbg(&usbduxsub
->interface
->dev
, "comedi_: result=%d\n", errcode
);
771 dev_err(&usbduxsub
->interface
->dev
,
772 "comedi_: upload failed\n");
778 /* the FX2LP has twice as much as the standard FX2 */
779 #define FIRMWARE_MAX_LEN 0x4000
781 static int firmwareUpload(struct usbduxsub
*usbduxsub
,
782 const u8
*firmwareBinary
, int sizeFirmware
)
790 if (sizeFirmware
> FIRMWARE_MAX_LEN
) {
791 dev_err(&usbduxsub
->interface
->dev
,
792 "usbduxsigma firmware binary it too large for FX2.\n");
796 /* we generate a local buffer for the firmware */
797 fwBuf
= kmemdup(firmwareBinary
, sizeFirmware
, GFP_KERNEL
);
799 dev_err(&usbduxsub
->interface
->dev
,
800 "comedi_: mem alloc for firmware failed\n");
804 ret
= usbduxsub_stop(usbduxsub
);
806 dev_err(&usbduxsub
->interface
->dev
,
807 "comedi_: can not stop firmware\n");
812 ret
= usbduxsub_upload(usbduxsub
, fwBuf
, 0, sizeFirmware
);
814 dev_err(&usbduxsub
->interface
->dev
,
815 "comedi_: firmware upload failed\n");
819 ret
= usbduxsub_start(usbduxsub
);
821 dev_err(&usbduxsub
->interface
->dev
,
822 "comedi_: can not start firmware\n");
830 static int usbduxsub_submit_InURBs(struct usbduxsub
*usbduxsub
)
837 /* Submit all URBs and start the transfer on the bus */
838 for (i
= 0; i
< usbduxsub
->numOfInBuffers
; i
++) {
839 /* in case of a resubmission after an unlink... */
840 usbduxsub
->urbIn
[i
]->interval
= usbduxsub
->ai_interval
;
841 usbduxsub
->urbIn
[i
]->context
= usbduxsub
->comedidev
;
842 usbduxsub
->urbIn
[i
]->dev
= usbduxsub
->usbdev
;
843 usbduxsub
->urbIn
[i
]->status
= 0;
844 usbduxsub
->urbIn
[i
]->transfer_flags
= URB_ISO_ASAP
;
845 dev_dbg(&usbduxsub
->interface
->dev
,
846 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
847 usbduxsub
->comedidev
->minor
, i
,
848 (usbduxsub
->urbIn
[i
]->context
),
849 (usbduxsub
->urbIn
[i
]->dev
),
850 (usbduxsub
->urbIn
[i
]->interval
));
851 errFlag
= usb_submit_urb(usbduxsub
->urbIn
[i
], GFP_ATOMIC
);
853 dev_err(&usbduxsub
->interface
->dev
,
854 "comedi_: ai: usb_submit_urb(%d) error %d\n",
862 static int usbduxsub_submit_OutURBs(struct usbduxsub
*usbduxsub
)
869 for (i
= 0; i
< usbduxsub
->numOfOutBuffers
; i
++) {
870 dev_dbg(&usbduxsub
->interface
->dev
,
871 "comedi_: submitting out-urb[%d]\n", i
);
872 /* in case of a resubmission after an unlink... */
873 usbduxsub
->urbOut
[i
]->context
= usbduxsub
->comedidev
;
874 usbduxsub
->urbOut
[i
]->dev
= usbduxsub
->usbdev
;
875 usbduxsub
->urbOut
[i
]->status
= 0;
876 usbduxsub
->urbOut
[i
]->transfer_flags
= URB_ISO_ASAP
;
877 errFlag
= usb_submit_urb(usbduxsub
->urbOut
[i
], GFP_ATOMIC
);
879 dev_err(&usbduxsub
->interface
->dev
,
880 "comedi_: ao: usb_submit_urb(%d) error %d\n",
888 static int chanToInterval(int nChannels
)
900 static int usbdux_ai_cmdtest(struct comedi_device
*dev
,
901 struct comedi_subdevice
*s
,
902 struct comedi_cmd
*cmd
)
905 unsigned int tmpTimer
;
906 struct usbduxsub
*this_usbduxsub
= dev
->private;
908 if (!(this_usbduxsub
->probed
))
911 dev_dbg(&this_usbduxsub
->interface
->dev
,
912 "comedi%d: usbdux_ai_cmdtest\n", dev
->minor
);
914 /* make sure triggers are valid */
915 /* Only immediate triggers are allowed */
916 tmp
= cmd
->start_src
;
917 cmd
->start_src
&= TRIG_NOW
| TRIG_INT
;
918 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
921 /* trigger should happen timed */
922 tmp
= cmd
->scan_begin_src
;
923 /* start a new _scan_ with a timer */
924 cmd
->scan_begin_src
&= TRIG_TIMER
;
925 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
928 /* scanning is continous */
929 tmp
= cmd
->convert_src
;
930 cmd
->convert_src
&= TRIG_NOW
;
931 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
934 /* issue a trigger when scan is finished and start a new scan */
935 tmp
= cmd
->scan_end_src
;
936 cmd
->scan_end_src
&= TRIG_COUNT
;
937 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
940 /* trigger at the end of count events or not, stop condition or not */
942 cmd
->stop_src
&= TRIG_COUNT
| TRIG_NONE
;
943 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
950 * step 2: make sure trigger sources are unique and mutually compatible
951 * note that mutual compatibility is not an issue here
953 if (cmd
->scan_begin_src
!= TRIG_FOLLOW
&&
954 cmd
->scan_begin_src
!= TRIG_EXT
&&
955 cmd
->scan_begin_src
!= TRIG_TIMER
)
957 if (cmd
->stop_src
!= TRIG_COUNT
&& cmd
->stop_src
!= TRIG_NONE
)
963 /* step 3: make sure arguments are trivially compatible */
964 if (cmd
->start_arg
!= 0) {
969 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
970 /* internal trigger */
971 if (cmd
->scan_begin_arg
!= 0) {
972 cmd
->scan_begin_arg
= 0;
977 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
978 if (this_usbduxsub
->high_speed
) {
980 * In high speed mode microframes are possible.
981 * However, during one microframe we can roughly
982 * sample two channels. Thus, the more channels
983 * are in the channel list the more time we need.
985 i
= chanToInterval(cmd
->chanlist_len
);
986 if (cmd
->scan_begin_arg
< (1000000 / 8 * i
)) {
987 cmd
->scan_begin_arg
= 1000000 / 8 * i
;
990 /* now calc the real sampling rate with all the
993 ((unsigned int)(cmd
->scan_begin_arg
/ 125000)) *
995 if (cmd
->scan_begin_arg
!= tmpTimer
) {
996 cmd
->scan_begin_arg
= tmpTimer
;
1001 /* 1kHz scans every USB frame */
1002 if (cmd
->scan_begin_arg
< 1000000) {
1003 cmd
->scan_begin_arg
= 1000000;
1007 * calc the real sampling rate with the rounding errors
1009 tmpTimer
= ((unsigned int)(cmd
->scan_begin_arg
/
1010 1000000)) * 1000000;
1011 if (cmd
->scan_begin_arg
!= tmpTimer
) {
1012 cmd
->scan_begin_arg
= tmpTimer
;
1017 /* the same argument */
1018 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
1019 cmd
->scan_end_arg
= cmd
->chanlist_len
;
1023 if (cmd
->stop_src
== TRIG_COUNT
) {
1024 /* any count is allowed */
1027 if (cmd
->stop_arg
!= 0) {
1040 * creates the ADC command for the MAX1271
1041 * range is the range value from comedi
1043 static void create_adc_command(unsigned int chan
,
1048 (*muxsg0
) = (*muxsg0
) | (1 << chan
);
1050 (*muxsg1
) = (*muxsg1
) | (1 << (chan
-8));
1054 /* bulk transfers to usbdux */
1056 #define SENDADCOMMANDS 0
1057 #define SENDDACOMMANDS 1
1058 #define SENDDIOCONFIGCOMMAND 2
1059 #define SENDDIOBITSCOMMAND 3
1060 #define SENDSINGLEAD 4
1062 #define SENDPWMOFF 8
1064 static int send_dux_commands(struct usbduxsub
*this_usbduxsub
, int cmd_type
)
1068 this_usbduxsub
->dux_commands
[0] = cmd_type
;
1069 #ifdef NOISY_DUX_DEBUGBUG
1070 printk(KERN_DEBUG
"comedi%d: usbdux: dux_commands: ",
1071 this_usbduxsub
->comedidev
->minor
);
1072 for (result
= 0; result
< SIZEOFDUXBUFFER
; result
++)
1073 printk(" %02x", this_usbduxsub
->dux_commands
[result
]);
1076 result
= usb_bulk_msg(this_usbduxsub
->usbdev
,
1077 usb_sndbulkpipe(this_usbduxsub
->usbdev
,
1079 this_usbduxsub
->dux_commands
, SIZEOFDUXBUFFER
,
1080 &nsent
, BULK_TIMEOUT
);
1082 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1083 "could not transmit dux_command to the usb-device, "
1084 "err=%d\n", this_usbduxsub
->comedidev
->minor
, result
);
1089 static int receive_dux_commands(struct usbduxsub
*this_usbduxsub
, int command
)
1091 int result
= (-EFAULT
);
1095 for (i
= 0; i
< RETRIES
; i
++) {
1096 result
= usb_bulk_msg(this_usbduxsub
->usbdev
,
1097 usb_rcvbulkpipe(this_usbduxsub
->usbdev
,
1099 this_usbduxsub
->insnBuffer
, SIZEINSNBUF
,
1100 &nrec
, BULK_TIMEOUT
);
1102 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1103 "insn: USB error %d "
1104 "while receiving DUX command"
1105 "\n", this_usbduxsub
->comedidev
->minor
,
1109 if (this_usbduxsub
->insnBuffer
[0] == command
)
1112 /* this is only reached if the data has been requested a couple of
1114 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: insn: "
1115 "wrong data returned from firmware: want %d, got %d.\n",
1116 this_usbduxsub
->comedidev
->minor
, command
,
1117 this_usbduxsub
->insnBuffer
[0]);
1121 static int usbdux_ai_inttrig(struct comedi_device
*dev
,
1122 struct comedi_subdevice
*s
, unsigned int trignum
)
1125 struct usbduxsub
*this_usbduxsub
= dev
->private;
1126 if (!this_usbduxsub
)
1129 down(&this_usbduxsub
->sem
);
1130 if (!(this_usbduxsub
->probed
)) {
1131 up(&this_usbduxsub
->sem
);
1134 dev_dbg(&this_usbduxsub
->interface
->dev
,
1135 "comedi%d: usbdux_ai_inttrig\n", dev
->minor
);
1138 dev_err(&this_usbduxsub
->interface
->dev
,
1139 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1141 up(&this_usbduxsub
->sem
);
1144 if (!(this_usbduxsub
->ai_cmd_running
)) {
1145 this_usbduxsub
->ai_cmd_running
= 1;
1146 ret
= usbduxsub_submit_InURBs(this_usbduxsub
);
1148 dev_err(&this_usbduxsub
->interface
->dev
,
1149 "comedi%d: usbdux_ai_inttrig: "
1150 "urbSubmit: err=%d\n", dev
->minor
, ret
);
1151 this_usbduxsub
->ai_cmd_running
= 0;
1152 up(&this_usbduxsub
->sem
);
1155 s
->async
->inttrig
= NULL
;
1157 dev_err(&this_usbduxsub
->interface
->dev
,
1158 "comedi%d: ai_inttrig but acqu is already running\n",
1161 up(&this_usbduxsub
->sem
);
1165 static int usbdux_ai_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1167 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
1170 struct usbduxsub
*this_usbduxsub
= dev
->private;
1176 if (!this_usbduxsub
)
1179 dev_dbg(&this_usbduxsub
->interface
->dev
,
1180 "comedi%d: usbdux_ai_cmd\n", dev
->minor
);
1182 /* block other CPUs from starting an ai_cmd */
1183 down(&this_usbduxsub
->sem
);
1185 if (!(this_usbduxsub
->probed
)) {
1186 up(&this_usbduxsub
->sem
);
1189 if (this_usbduxsub
->ai_cmd_running
) {
1190 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: "
1191 "ai_cmd not possible. Another ai_cmd is running.\n",
1193 up(&this_usbduxsub
->sem
);
1196 /* set current channel of the running aquisition to zero */
1197 s
->async
->cur_chan
= 0;
1199 /* first the number of channels per time step */
1200 this_usbduxsub
->dux_commands
[1] = cmd
->chanlist_len
;
1203 this_usbduxsub
->dux_commands
[2] = 0x12;
1205 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
1206 this_usbduxsub
->dux_commands
[3] = 0x03;
1208 /* CONFIG3: differential channels off */
1209 this_usbduxsub
->dux_commands
[4] = 0x00;
1211 for (i
= 0; i
< cmd
->chanlist_len
; i
++) {
1212 chan
= CR_CHAN(cmd
->chanlist
[i
]);
1213 create_adc_command(chan
, &muxsg0
, &muxsg1
);
1214 if (i
>= NUMCHANNELS
) {
1215 dev_err(&this_usbduxsub
->interface
->dev
,
1216 "comedi%d: channel list too long\n",
1221 this_usbduxsub
->dux_commands
[5] = muxsg0
;
1222 this_usbduxsub
->dux_commands
[6] = muxsg1
;
1223 this_usbduxsub
->dux_commands
[7] = sysred
;
1225 dev_dbg(&this_usbduxsub
->interface
->dev
,
1226 "comedi %d: sending commands to the usb device: size=%u\n",
1227 dev
->minor
, NUMCHANNELS
);
1229 result
= send_dux_commands(this_usbduxsub
, SENDADCOMMANDS
);
1231 up(&this_usbduxsub
->sem
);
1235 if (this_usbduxsub
->high_speed
) {
1237 * every 2 channels get a time window of 125us. Thus, if we
1238 * sample all 16 channels we need 1ms. If we sample only one
1239 * channel we need only 125us
1241 this_usbduxsub
->ai_interval
=
1242 chanToInterval(cmd
->chanlist_len
);
1243 this_usbduxsub
->ai_timer
= cmd
->scan_begin_arg
/ (125000 *
1247 /* interval always 1ms */
1248 this_usbduxsub
->ai_interval
= 1;
1249 this_usbduxsub
->ai_timer
= cmd
->scan_begin_arg
/ 1000000;
1251 if (this_usbduxsub
->ai_timer
< 1) {
1252 dev_err(&this_usbduxsub
->interface
->dev
, "comedi%d: ai_cmd: "
1253 "timer=%d, scan_begin_arg=%d. "
1254 "Not properly tested by cmdtest?\n", dev
->minor
,
1255 this_usbduxsub
->ai_timer
, cmd
->scan_begin_arg
);
1256 up(&this_usbduxsub
->sem
);
1259 this_usbduxsub
->ai_counter
= this_usbduxsub
->ai_timer
;
1261 if (cmd
->stop_src
== TRIG_COUNT
) {
1262 /* data arrives as one packet */
1263 this_usbduxsub
->ai_sample_count
= cmd
->stop_arg
;
1264 this_usbduxsub
->ai_continous
= 0;
1266 /* continous aquisition */
1267 this_usbduxsub
->ai_continous
= 1;
1268 this_usbduxsub
->ai_sample_count
= 0;
1271 if (cmd
->start_src
== TRIG_NOW
) {
1272 /* enable this acquisition operation */
1273 this_usbduxsub
->ai_cmd_running
= 1;
1274 ret
= usbduxsub_submit_InURBs(this_usbduxsub
);
1276 this_usbduxsub
->ai_cmd_running
= 0;
1277 /* fixme: unlink here?? */
1278 up(&this_usbduxsub
->sem
);
1281 s
->async
->inttrig
= NULL
;
1284 /* don't enable the acquision operation */
1285 /* wait for an internal signal */
1286 s
->async
->inttrig
= usbdux_ai_inttrig
;
1288 up(&this_usbduxsub
->sem
);
1292 /* Mode 0 is used to get a single conversion on demand */
1293 static int usbdux_ai_insn_read(struct comedi_device
*dev
,
1294 struct comedi_subdevice
*s
,
1295 struct comedi_insn
*insn
, unsigned int *data
)
1301 struct usbduxsub
*this_usbduxsub
= dev
->private;
1306 if (!this_usbduxsub
)
1309 dev_dbg(&this_usbduxsub
->interface
->dev
,
1310 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1311 dev
->minor
, insn
->n
, insn
->subdev
);
1313 down(&this_usbduxsub
->sem
);
1314 if (!(this_usbduxsub
->probed
)) {
1315 up(&this_usbduxsub
->sem
);
1318 if (this_usbduxsub
->ai_cmd_running
) {
1319 dev_err(&this_usbduxsub
->interface
->dev
,
1320 "comedi%d: ai_insn_read not possible. "
1321 "Async Command is running.\n", dev
->minor
);
1322 up(&this_usbduxsub
->sem
);
1326 /* sample one channel */
1327 /* CONFIG0: chopper on */
1328 this_usbduxsub
->dux_commands
[1] = 0x16;
1330 /* CONFIG1: 2kHz sampling rate */
1331 this_usbduxsub
->dux_commands
[2] = 0x80;
1333 /* CONFIG3: differential channels off */
1334 this_usbduxsub
->dux_commands
[3] = 0x00;
1336 chan
= CR_CHAN(insn
->chanspec
);
1337 create_adc_command(chan
, &muxsg0
, &muxsg1
);
1339 this_usbduxsub
->dux_commands
[4] = muxsg0
;
1340 this_usbduxsub
->dux_commands
[5] = muxsg1
;
1341 this_usbduxsub
->dux_commands
[6] = sysred
;
1344 err
= send_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1346 up(&this_usbduxsub
->sem
);
1350 for (i
= 0; i
< insn
->n
; i
++) {
1351 err
= receive_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1353 up(&this_usbduxsub
->sem
);
1356 /* 32 bits big endian from the A/D converter */
1357 one
= be32_to_cpu(*((int32_t *)
1358 ((this_usbduxsub
->insnBuffer
)+1)));
1359 /* mask out the staus byte */
1360 one
= one
& 0x00ffffff;
1361 /* turn it into an unsigned integer */
1362 one
= one
^ 0x00800000;
1365 up(&this_usbduxsub
->sem
);
1372 static int usbdux_getstatusinfo(struct comedi_device
*dev
, int chan
)
1374 struct usbduxsub
*this_usbduxsub
= dev
->private;
1379 if (!this_usbduxsub
)
1382 if (this_usbduxsub
->ai_cmd_running
) {
1383 dev_err(&this_usbduxsub
->interface
->dev
,
1384 "comedi%d: status read not possible. "
1385 "Async Command is running.\n", dev
->minor
);
1390 this_usbduxsub
->dux_commands
[1] = 0x12;
1392 /* CONFIG1: 2kHz sampling rate */
1393 this_usbduxsub
->dux_commands
[2] = 0x80;
1395 /* CONFIG3: differential channels off */
1396 this_usbduxsub
->dux_commands
[3] = 0x00;
1400 sysred
= sysred
| 1;
1401 } else if (chan
== 2) {
1403 sysred
= sysred
| 4;
1404 } else if (chan
== 3) {
1406 sysred
= sysred
| 8;
1407 } else if (chan
== 4) {
1409 sysred
= sysred
| 16;
1410 } else if (chan
== 5) {
1412 sysred
= sysred
| 32;
1415 this_usbduxsub
->dux_commands
[4] = 0;
1416 this_usbduxsub
->dux_commands
[5] = 0;
1417 this_usbduxsub
->dux_commands
[6] = sysred
;
1420 err
= send_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1424 err
= receive_dux_commands(this_usbduxsub
, SENDSINGLEAD
);
1428 /* 32 bits big endian from the A/D converter */
1429 one
= be32_to_cpu(*((int32_t *)((this_usbduxsub
->insnBuffer
)+1)));
1430 /* mask out the staus byte */
1431 one
= one
& 0x00ffffff;
1432 one
= one
^ 0x00800000;
1442 /************************************/
1445 static int usbdux_ao_insn_read(struct comedi_device
*dev
,
1446 struct comedi_subdevice
*s
,
1447 struct comedi_insn
*insn
, unsigned int *data
)
1450 int chan
= CR_CHAN(insn
->chanspec
);
1451 struct usbduxsub
*this_usbduxsub
= dev
->private;
1453 if (!this_usbduxsub
)
1456 down(&this_usbduxsub
->sem
);
1457 if (!(this_usbduxsub
->probed
)) {
1458 up(&this_usbduxsub
->sem
);
1461 for (i
= 0; i
< insn
->n
; i
++)
1462 data
[i
] = this_usbduxsub
->outBuffer
[chan
];
1464 up(&this_usbduxsub
->sem
);
1468 static int usbdux_ao_insn_write(struct comedi_device
*dev
,
1469 struct comedi_subdevice
*s
,
1470 struct comedi_insn
*insn
, unsigned int *data
)
1473 int chan
= CR_CHAN(insn
->chanspec
);
1474 struct usbduxsub
*this_usbduxsub
= dev
->private;
1476 if (!this_usbduxsub
)
1479 dev_dbg(&this_usbduxsub
->interface
->dev
,
1480 "comedi%d: ao_insn_write\n", dev
->minor
);
1482 down(&this_usbduxsub
->sem
);
1483 if (!(this_usbduxsub
->probed
)) {
1484 up(&this_usbduxsub
->sem
);
1487 if (this_usbduxsub
->ao_cmd_running
) {
1488 dev_err(&this_usbduxsub
->interface
->dev
,
1489 "comedi%d: ao_insn_write: "
1490 "ERROR: asynchronous ao_cmd is running\n", dev
->minor
);
1491 up(&this_usbduxsub
->sem
);
1495 for (i
= 0; i
< insn
->n
; i
++) {
1496 dev_dbg(&this_usbduxsub
->interface
->dev
,
1497 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1498 dev
->minor
, chan
, i
, data
[i
]);
1500 /* number of channels: 1 */
1501 this_usbduxsub
->dux_commands
[1] = 1;
1502 /* channel number */
1503 this_usbduxsub
->dux_commands
[2] = data
[i
];
1504 this_usbduxsub
->outBuffer
[chan
] = data
[i
];
1505 this_usbduxsub
->dux_commands
[3] = chan
;
1506 err
= send_dux_commands(this_usbduxsub
, SENDDACOMMANDS
);
1508 up(&this_usbduxsub
->sem
);
1512 up(&this_usbduxsub
->sem
);
1517 static int usbdux_ao_inttrig(struct comedi_device
*dev
,
1518 struct comedi_subdevice
*s
, unsigned int trignum
)
1521 struct usbduxsub
*this_usbduxsub
= dev
->private;
1523 if (!this_usbduxsub
)
1526 down(&this_usbduxsub
->sem
);
1527 if (!(this_usbduxsub
->probed
)) {
1528 up(&this_usbduxsub
->sem
);
1532 dev_err(&this_usbduxsub
->interface
->dev
,
1533 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1537 if (!(this_usbduxsub
->ao_cmd_running
)) {
1538 this_usbduxsub
->ao_cmd_running
= 1;
1539 ret
= usbduxsub_submit_OutURBs(this_usbduxsub
);
1541 dev_err(&this_usbduxsub
->interface
->dev
,
1542 "comedi%d: usbdux_ao_inttrig: submitURB: "
1543 "err=%d\n", dev
->minor
, ret
);
1544 this_usbduxsub
->ao_cmd_running
= 0;
1545 up(&this_usbduxsub
->sem
);
1548 s
->async
->inttrig
= NULL
;
1550 dev_err(&this_usbduxsub
->interface
->dev
,
1551 "comedi%d: ao_inttrig but acqu is already running.\n",
1554 up(&this_usbduxsub
->sem
);
1558 static int usbdux_ao_cmdtest(struct comedi_device
*dev
,
1559 struct comedi_subdevice
*s
,
1560 struct comedi_cmd
*cmd
)
1563 struct usbduxsub
*this_usbduxsub
= dev
->private;
1565 if (!this_usbduxsub
)
1568 if (!(this_usbduxsub
->probed
))
1571 dev_dbg(&this_usbduxsub
->interface
->dev
,
1572 "comedi%d: usbdux_ao_cmdtest\n", dev
->minor
);
1574 /* make sure triggers are valid */
1575 /* Only immediate triggers are allowed */
1576 tmp
= cmd
->start_src
;
1577 cmd
->start_src
&= TRIG_NOW
| TRIG_INT
;
1578 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
1581 /* trigger should happen timed */
1582 tmp
= cmd
->scan_begin_src
;
1583 /* just now we scan also in the high speed mode every frame */
1584 /* this is due to ehci driver limitations */
1585 if (0) { /* (this_usbduxsub->high_speed) */
1586 /* start immidiately a new scan */
1587 /* the sampling rate is set by the coversion rate */
1588 cmd
->scan_begin_src
&= TRIG_FOLLOW
;
1590 /* start a new scan (output at once) with a timer */
1591 cmd
->scan_begin_src
&= TRIG_TIMER
;
1593 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
1596 /* scanning is continous */
1597 tmp
= cmd
->convert_src
;
1599 /* all conversion events happen simultaneously */
1600 cmd
->convert_src
&= TRIG_NOW
;
1602 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
1605 /* issue a trigger when scan is finished and start a new scan */
1606 tmp
= cmd
->scan_end_src
;
1607 cmd
->scan_end_src
&= TRIG_COUNT
;
1608 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
1611 /* trigger at the end of count events or not, stop condition or not */
1612 tmp
= cmd
->stop_src
;
1613 cmd
->stop_src
&= TRIG_COUNT
| TRIG_NONE
;
1614 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
1621 * step 2: make sure trigger sources
1622 * are unique and mutually compatible
1623 * note that mutual compatibility is not an issue here
1625 if (cmd
->scan_begin_src
!= TRIG_FOLLOW
&&
1626 cmd
->scan_begin_src
!= TRIG_EXT
&&
1627 cmd
->scan_begin_src
!= TRIG_TIMER
)
1629 if (cmd
->stop_src
!= TRIG_COUNT
&& cmd
->stop_src
!= TRIG_NONE
)
1635 /* step 3: make sure arguments are trivially compatible */
1637 if (cmd
->start_arg
!= 0) {
1642 if (cmd
->scan_begin_src
== TRIG_FOLLOW
) {
1643 /* internal trigger */
1644 if (cmd
->scan_begin_arg
!= 0) {
1645 cmd
->scan_begin_arg
= 0;
1650 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
1652 if (cmd
->scan_begin_arg
< 1000000) {
1653 cmd
->scan_begin_arg
= 1000000;
1657 /* not used now, is for later use */
1658 if (cmd
->convert_src
== TRIG_TIMER
) {
1659 if (cmd
->convert_arg
< 125000) {
1660 cmd
->convert_arg
= 125000;
1665 /* the same argument */
1666 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
1667 cmd
->scan_end_arg
= cmd
->chanlist_len
;
1671 if (cmd
->stop_src
== TRIG_COUNT
) {
1672 /* any count is allowed */
1675 if (cmd
->stop_arg
!= 0) {
1681 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi%d: err=%d, "
1682 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1683 "convert_arg=%d\n", dev
->minor
, err
, cmd
->scan_begin_src
,
1684 cmd
->scan_begin_arg
, cmd
->convert_src
, cmd
->convert_arg
);
1692 static int usbdux_ao_cmd(struct comedi_device
*dev
, struct comedi_subdevice
*s
)
1694 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
1695 unsigned int chan
, gain
;
1697 struct usbduxsub
*this_usbduxsub
= dev
->private;
1699 if (!this_usbduxsub
)
1702 down(&this_usbduxsub
->sem
);
1703 if (!(this_usbduxsub
->probed
)) {
1704 up(&this_usbduxsub
->sem
);
1707 dev_dbg(&this_usbduxsub
->interface
->dev
,
1708 "comedi%d: %s\n", dev
->minor
, __func__
);
1710 /* set current channel of the running aquisition to zero */
1711 s
->async
->cur_chan
= 0;
1712 for (i
= 0; i
< cmd
->chanlist_len
; ++i
) {
1713 chan
= CR_CHAN(cmd
->chanlist
[i
]);
1714 gain
= CR_RANGE(cmd
->chanlist
[i
]);
1715 if (i
>= NUMOUTCHANNELS
) {
1716 dev_err(&this_usbduxsub
->interface
->dev
,
1717 "comedi%d: %s: channel list too long\n",
1718 dev
->minor
, __func__
);
1721 this_usbduxsub
->dac_commands
[i
] = chan
;
1722 dev_dbg(&this_usbduxsub
->interface
->dev
,
1723 "comedi%d: dac command for ch %d is %x\n",
1724 dev
->minor
, i
, this_usbduxsub
->dac_commands
[i
]);
1727 /* we count in steps of 1ms (125us) */
1728 /* 125us mode not used yet */
1729 if (0) { /* (this_usbduxsub->high_speed) */
1731 /* timing of the conversion itself: every 125 us */
1732 this_usbduxsub
->ao_timer
= cmd
->convert_arg
/ 125000;
1735 /* timing of the scan: we get all channels at once */
1736 this_usbduxsub
->ao_timer
= cmd
->scan_begin_arg
/ 1000000;
1737 dev_dbg(&this_usbduxsub
->interface
->dev
,
1738 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1739 "convert_src=%d, convert_arg=%d\n", dev
->minor
,
1740 cmd
->scan_begin_src
, cmd
->scan_begin_arg
,
1741 cmd
->convert_src
, cmd
->convert_arg
);
1742 dev_dbg(&this_usbduxsub
->interface
->dev
,
1743 "comedi%d: ao_timer=%d (ms)\n",
1744 dev
->minor
, this_usbduxsub
->ao_timer
);
1745 if (this_usbduxsub
->ao_timer
< 1) {
1746 dev_err(&this_usbduxsub
->interface
->dev
,
1747 "comedi%d: usbdux: ao_timer=%d, "
1748 "scan_begin_arg=%d. "
1749 "Not properly tested by cmdtest?\n",
1750 dev
->minor
, this_usbduxsub
->ao_timer
,
1751 cmd
->scan_begin_arg
);
1752 up(&this_usbduxsub
->sem
);
1756 this_usbduxsub
->ao_counter
= this_usbduxsub
->ao_timer
;
1758 if (cmd
->stop_src
== TRIG_COUNT
) {
1761 /* high speed also scans everything at once */
1762 if (0) { /* (this_usbduxsub->high_speed) */
1763 this_usbduxsub
->ao_sample_count
=
1764 (cmd
->stop_arg
) * (cmd
->scan_end_arg
);
1766 /* there's no scan as the scan has been */
1767 /* perf inside the FX2 */
1768 /* data arrives as one packet */
1769 this_usbduxsub
->ao_sample_count
= cmd
->stop_arg
;
1771 this_usbduxsub
->ao_continous
= 0;
1773 /* continous aquisition */
1774 this_usbduxsub
->ao_continous
= 1;
1775 this_usbduxsub
->ao_sample_count
= 0;
1778 if (cmd
->start_src
== TRIG_NOW
) {
1779 /* enable this acquisition operation */
1780 this_usbduxsub
->ao_cmd_running
= 1;
1781 ret
= usbduxsub_submit_OutURBs(this_usbduxsub
);
1783 this_usbduxsub
->ao_cmd_running
= 0;
1784 /* fixme: unlink here?? */
1785 up(&this_usbduxsub
->sem
);
1788 s
->async
->inttrig
= NULL
;
1791 /* submit the urbs later */
1792 /* wait for an internal signal */
1793 s
->async
->inttrig
= usbdux_ao_inttrig
;
1796 up(&this_usbduxsub
->sem
);
1800 static int usbdux_dio_insn_config(struct comedi_device
*dev
,
1801 struct comedi_subdevice
*s
,
1802 struct comedi_insn
*insn
, unsigned int *data
)
1804 int chan
= CR_CHAN(insn
->chanspec
);
1806 /* The input or output configuration of each digital line is
1807 * configured by a special insn_config instruction. chanspec
1808 * contains the channel to be changed, and data[0] contains the
1809 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1812 case INSN_CONFIG_DIO_OUTPUT
:
1813 s
->io_bits
|= 1 << chan
; /* 1 means Out */
1815 case INSN_CONFIG_DIO_INPUT
:
1816 s
->io_bits
&= ~(1 << chan
);
1818 case INSN_CONFIG_DIO_QUERY
:
1820 (s
->io_bits
& (1 << chan
)) ? COMEDI_OUTPUT
: COMEDI_INPUT
;
1826 /* we don't tell the firmware here as it would take 8 frames */
1827 /* to submit the information. We do it in the insn_bits. */
1831 static int usbdux_dio_insn_bits(struct comedi_device
*dev
,
1832 struct comedi_subdevice
*s
,
1833 struct comedi_insn
*insn
,
1837 struct usbduxsub
*this_usbduxsub
= dev
->private;
1840 if (!this_usbduxsub
)
1846 down(&this_usbduxsub
->sem
);
1848 if (!(this_usbduxsub
->probed
)) {
1849 up(&this_usbduxsub
->sem
);
1853 /* The insn data is a mask in data[0] and the new data
1854 * in data[1], each channel cooresponding to a bit. */
1855 s
->state
&= ~data
[0];
1856 s
->state
|= data
[0] & data
[1];
1857 /* The commands are 8 bits wide */
1858 this_usbduxsub
->dux_commands
[1] = (s
->io_bits
) & 0x000000FF;
1859 this_usbduxsub
->dux_commands
[4] = (s
->state
) & 0x000000FF;
1860 this_usbduxsub
->dux_commands
[2] = ((s
->io_bits
) & 0x0000FF00) >> 8;
1861 this_usbduxsub
->dux_commands
[5] = ((s
->state
) & 0x0000FF00) >> 8;
1862 this_usbduxsub
->dux_commands
[3] = ((s
->io_bits
) & 0x00FF0000) >> 16;
1863 this_usbduxsub
->dux_commands
[6] = ((s
->state
) & 0x00FF0000) >> 16;
1865 /* This command also tells the firmware to return */
1866 /* the digital input lines */
1867 err
= send_dux_commands(this_usbduxsub
, SENDDIOBITSCOMMAND
);
1869 up(&this_usbduxsub
->sem
);
1872 err
= receive_dux_commands(this_usbduxsub
, SENDDIOBITSCOMMAND
);
1874 up(&this_usbduxsub
->sem
);
1878 data
[1] = (((unsigned int)(this_usbduxsub
->insnBuffer
[1]))&0xff) |
1879 ((((unsigned int)(this_usbduxsub
->insnBuffer
[2]))&0xff) << 8) |
1880 ((((unsigned int)(this_usbduxsub
->insnBuffer
[3]))&0xff) << 16);
1884 up(&this_usbduxsub
->sem
);
1888 /***********************************/
1891 static int usbduxsub_unlink_PwmURBs(struct usbduxsub
*usbduxsub_tmp
)
1895 if (usbduxsub_tmp
&& usbduxsub_tmp
->urbPwm
) {
1896 if (usbduxsub_tmp
->urbPwm
)
1897 usb_kill_urb(usbduxsub_tmp
->urbPwm
);
1898 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
1899 "comedi: unlinked PwmURB: res=%d\n", err
);
1904 /* This cancels a running acquisition operation
1907 static int usbdux_pwm_stop(struct usbduxsub
*this_usbduxsub
, int do_unlink
)
1911 if (!this_usbduxsub
)
1914 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi: %s\n", __func__
);
1916 ret
= usbduxsub_unlink_PwmURBs(this_usbduxsub
);
1918 this_usbduxsub
->pwm_cmd_running
= 0;
1923 /* force unlink - is called by comedi */
1924 static int usbdux_pwm_cancel(struct comedi_device
*dev
,
1925 struct comedi_subdevice
*s
)
1927 struct usbduxsub
*this_usbduxsub
= dev
->private;
1930 /* unlink only if it is really running */
1931 res
= usbdux_pwm_stop(this_usbduxsub
, this_usbduxsub
->pwm_cmd_running
);
1933 dev_dbg(&this_usbduxsub
->interface
->dev
,
1934 "comedi %d: sending pwm off command to the usb device.\n",
1936 res
= send_dux_commands(this_usbduxsub
, SENDPWMOFF
);
1943 static void usbduxsub_pwm_irq(struct urb
*urb
)
1946 struct usbduxsub
*this_usbduxsub
;
1947 struct comedi_device
*this_comedidev
;
1948 struct comedi_subdevice
*s
;
1950 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1952 /* the context variable points to the subdevice */
1953 this_comedidev
= urb
->context
;
1954 /* the private structure of the subdevice is struct usbduxsub */
1955 this_usbduxsub
= this_comedidev
->private;
1957 s
= this_comedidev
->subdevices
+ SUBDEV_DA
;
1959 switch (urb
->status
) {
1969 * after an unlink command, unplug, ... etc
1970 * no unlink needed here. Already shutting down.
1972 if (this_usbduxsub
->pwm_cmd_running
)
1973 usbdux_pwm_stop(this_usbduxsub
, 0);
1979 if (this_usbduxsub
->pwm_cmd_running
) {
1980 dev_err(&this_usbduxsub
->interface
->dev
,
1981 "comedi_: Non-zero urb status received in "
1982 "pwm intr context: %d\n", urb
->status
);
1983 usbdux_pwm_stop(this_usbduxsub
, 0);
1988 /* are we actually running? */
1989 if (!(this_usbduxsub
->pwm_cmd_running
))
1992 urb
->transfer_buffer_length
= this_usbduxsub
->sizePwmBuf
;
1993 urb
->dev
= this_usbduxsub
->usbdev
;
1995 if (this_usbduxsub
->pwm_cmd_running
) {
1996 ret
= usb_submit_urb(urb
, GFP_ATOMIC
);
1998 dev_err(&this_usbduxsub
->interface
->dev
,
1999 "comedi_: pwm urb resubm failed in int-cont. "
2001 if (ret
== EL2NSYNC
)
2002 dev_err(&this_usbduxsub
->interface
->dev
,
2003 "buggy USB host controller or bug in "
2006 /* don't do an unlink here */
2007 usbdux_pwm_stop(this_usbduxsub
, 0);
2012 static int usbduxsub_submit_PwmURBs(struct usbduxsub
*usbduxsub
)
2019 dev_dbg(&usbduxsub
->interface
->dev
, "comedi_: submitting pwm-urb\n");
2021 /* in case of a resubmission after an unlink... */
2022 usb_fill_bulk_urb(usbduxsub
->urbPwm
,
2024 usb_sndbulkpipe(usbduxsub
->usbdev
, PWM_EP
),
2025 usbduxsub
->urbPwm
->transfer_buffer
,
2026 usbduxsub
->sizePwmBuf
, usbduxsub_pwm_irq
,
2027 usbduxsub
->comedidev
);
2029 errFlag
= usb_submit_urb(usbduxsub
->urbPwm
, GFP_ATOMIC
);
2031 dev_err(&usbduxsub
->interface
->dev
,
2032 "comedi_: usbduxsigma: pwm: usb_submit_urb error %d\n",
2039 static int usbdux_pwm_period(struct comedi_device
*dev
,
2040 struct comedi_subdevice
*s
, unsigned int period
)
2042 struct usbduxsub
*this_usbduxsub
= dev
->private;
2045 if (period
< MIN_PWM_PERIOD
) {
2046 dev_err(&this_usbduxsub
->interface
->dev
,
2047 "comedi%d: illegal period setting for pwm.\n",
2051 fx2delay
= period
/ ((int)(6 * 512 * (1.0 / 0.033))) - 6;
2052 if (fx2delay
> 255) {
2053 dev_err(&this_usbduxsub
->interface
->dev
,
2054 "comedi%d: period %d for pwm is too low.\n",
2055 dev
->minor
, period
);
2059 this_usbduxsub
->pwmDelay
= fx2delay
;
2060 this_usbduxsub
->pwmPeriod
= period
;
2061 dev_dbg(&this_usbduxsub
->interface
->dev
, "%s: frequ=%d, period=%d\n",
2062 __func__
, period
, fx2delay
);
2066 /* is called from insn so there's no need to do all the sanity checks */
2067 static int usbdux_pwm_start(struct comedi_device
*dev
,
2068 struct comedi_subdevice
*s
)
2071 struct usbduxsub
*this_usbduxsub
= dev
->private;
2073 dev_dbg(&this_usbduxsub
->interface
->dev
, "comedi%d: %s\n",
2074 dev
->minor
, __func__
);
2076 if (this_usbduxsub
->pwm_cmd_running
) {
2077 /* already running */
2081 this_usbduxsub
->dux_commands
[1] = ((uint8_t) this_usbduxsub
->pwmDelay
);
2082 ret
= send_dux_commands(this_usbduxsub
, SENDPWMON
);
2086 /* initialise the buffer */
2087 for (i
= 0; i
< this_usbduxsub
->sizePwmBuf
; i
++)
2088 ((char *)(this_usbduxsub
->urbPwm
->transfer_buffer
))[i
] = 0;
2090 this_usbduxsub
->pwm_cmd_running
= 1;
2091 ret
= usbduxsub_submit_PwmURBs(this_usbduxsub
);
2093 this_usbduxsub
->pwm_cmd_running
= 0;
2099 /* generates the bit pattern for PWM with the optional sign bit */
2100 static int usbdux_pwm_pattern(struct comedi_device
*dev
,
2101 struct comedi_subdevice
*s
, int channel
,
2102 unsigned int value
, unsigned int sign
)
2104 struct usbduxsub
*this_usbduxsub
= dev
->private;
2111 if (!this_usbduxsub
)
2114 /* this is the DIO bit which carries the PWM data */
2115 pwm_mask
= (1 << channel
);
2116 /* this is the DIO bit which carries the optional direction bit */
2117 sgn_mask
= (16 << channel
);
2118 /* this is the buffer which will be filled with the with bit */
2119 /* pattern for one period */
2120 szbuf
= this_usbduxsub
->sizePwmBuf
;
2121 pBuf
= (char *)(this_usbduxsub
->urbPwm
->transfer_buffer
);
2122 for (i
= 0; i
< szbuf
; i
++) {
2125 c
= c
& (~pwm_mask
);
2126 /* set the bit as long as the index is lower than the value */
2129 /* set the optional sign bit for a relay */
2131 /* positive value */
2132 c
= c
& (~sgn_mask
);
2134 /* negative value */
2142 static int usbdux_pwm_write(struct comedi_device
*dev
,
2143 struct comedi_subdevice
*s
,
2144 struct comedi_insn
*insn
, unsigned int *data
)
2146 struct usbduxsub
*this_usbduxsub
= dev
->private;
2148 if (!this_usbduxsub
)
2151 if ((insn
->n
) != 1) {
2153 * doesn't make sense to have more than one value here because
2154 * it would just overwrite the PWM buffer a couple of times
2160 * the sign is set via a special INSN only, this gives us 8 bits for
2162 * relay sign 0 by default
2164 return usbdux_pwm_pattern(dev
, s
, CR_CHAN(insn
->chanspec
), data
[0], 0);
2167 static int usbdux_pwm_read(struct comedi_device
*x1
,
2168 struct comedi_subdevice
*x2
, struct comedi_insn
*x3
,
2175 /* switches on/off PWM */
2176 static int usbdux_pwm_config(struct comedi_device
*dev
,
2177 struct comedi_subdevice
*s
,
2178 struct comedi_insn
*insn
, unsigned int *data
)
2180 struct usbduxsub
*this_usbduxsub
= dev
->private;
2182 case INSN_CONFIG_ARM
:
2184 dev_dbg(&this_usbduxsub
->interface
->dev
,
2185 "comedi%d: %s: pwm on\n", dev
->minor
, __func__
);
2187 * if not zero the PWM is limited to a certain time which is
2188 * not supported here
2192 return usbdux_pwm_start(dev
, s
);
2193 case INSN_CONFIG_DISARM
:
2194 dev_dbg(&this_usbduxsub
->interface
->dev
,
2195 "comedi%d: %s: pwm off\n", dev
->minor
, __func__
);
2196 return usbdux_pwm_cancel(dev
, s
);
2197 case INSN_CONFIG_GET_PWM_STATUS
:
2199 * to check if the USB transmission has failed or in case PWM
2200 * was limited to n cycles to check if it has terminated
2202 data
[1] = this_usbduxsub
->pwm_cmd_running
;
2204 case INSN_CONFIG_PWM_SET_PERIOD
:
2205 dev_dbg(&this_usbduxsub
->interface
->dev
,
2206 "comedi%d: %s: setting period\n", dev
->minor
,
2208 return usbdux_pwm_period(dev
, s
, data
[1]);
2209 case INSN_CONFIG_PWM_GET_PERIOD
:
2210 data
[1] = this_usbduxsub
->pwmPeriod
;
2212 case INSN_CONFIG_PWM_SET_H_BRIDGE
:
2213 /* value in the first byte and the sign in the second for a
2215 return usbdux_pwm_pattern(dev
, s
,
2216 /* the channel number */
2217 CR_CHAN(insn
->chanspec
),
2218 /* actual PWM data */
2222 case INSN_CONFIG_PWM_GET_H_BRIDGE
:
2223 /* values are not kept in this driver, nothing to return */
2230 /*****************************************************************/
2232 static void tidy_up(struct usbduxsub
*usbduxsub_tmp
)
2238 dev_dbg(&usbduxsub_tmp
->interface
->dev
, "comedi_: tiding up\n");
2240 /* shows the usb subsystem that the driver is down */
2241 if (usbduxsub_tmp
->interface
)
2242 usb_set_intfdata(usbduxsub_tmp
->interface
, NULL
);
2244 usbduxsub_tmp
->probed
= 0;
2246 if (usbduxsub_tmp
->urbIn
) {
2247 if (usbduxsub_tmp
->ai_cmd_running
) {
2248 usbduxsub_tmp
->ai_cmd_running
= 0;
2249 usbduxsub_unlink_InURBs(usbduxsub_tmp
);
2251 for (i
= 0; i
< usbduxsub_tmp
->numOfInBuffers
; i
++) {
2252 kfree(usbduxsub_tmp
->urbIn
[i
]->transfer_buffer
);
2253 usbduxsub_tmp
->urbIn
[i
]->transfer_buffer
= NULL
;
2254 usb_kill_urb(usbduxsub_tmp
->urbIn
[i
]);
2255 usb_free_urb(usbduxsub_tmp
->urbIn
[i
]);
2256 usbduxsub_tmp
->urbIn
[i
] = NULL
;
2258 kfree(usbduxsub_tmp
->urbIn
);
2259 usbduxsub_tmp
->urbIn
= NULL
;
2261 if (usbduxsub_tmp
->urbOut
) {
2262 if (usbduxsub_tmp
->ao_cmd_running
) {
2263 usbduxsub_tmp
->ao_cmd_running
= 0;
2264 usbduxsub_unlink_OutURBs(usbduxsub_tmp
);
2266 for (i
= 0; i
< usbduxsub_tmp
->numOfOutBuffers
; i
++) {
2267 if (usbduxsub_tmp
->urbOut
[i
]->transfer_buffer
) {
2268 kfree(usbduxsub_tmp
->
2269 urbOut
[i
]->transfer_buffer
);
2270 usbduxsub_tmp
->urbOut
[i
]->transfer_buffer
=
2273 if (usbduxsub_tmp
->urbOut
[i
]) {
2274 usb_kill_urb(usbduxsub_tmp
->urbOut
[i
]);
2275 usb_free_urb(usbduxsub_tmp
->urbOut
[i
]);
2276 usbduxsub_tmp
->urbOut
[i
] = NULL
;
2279 kfree(usbduxsub_tmp
->urbOut
);
2280 usbduxsub_tmp
->urbOut
= NULL
;
2282 if (usbduxsub_tmp
->urbPwm
) {
2283 if (usbduxsub_tmp
->pwm_cmd_running
) {
2284 usbduxsub_tmp
->pwm_cmd_running
= 0;
2285 usbduxsub_unlink_PwmURBs(usbduxsub_tmp
);
2287 kfree(usbduxsub_tmp
->urbPwm
->transfer_buffer
);
2288 usbduxsub_tmp
->urbPwm
->transfer_buffer
= NULL
;
2289 usb_kill_urb(usbduxsub_tmp
->urbPwm
);
2290 usb_free_urb(usbduxsub_tmp
->urbPwm
);
2291 usbduxsub_tmp
->urbPwm
= NULL
;
2293 kfree(usbduxsub_tmp
->inBuffer
);
2294 usbduxsub_tmp
->inBuffer
= NULL
;
2295 kfree(usbduxsub_tmp
->insnBuffer
);
2296 usbduxsub_tmp
->insnBuffer
= NULL
;
2297 kfree(usbduxsub_tmp
->outBuffer
);
2298 usbduxsub_tmp
->outBuffer
= NULL
;
2299 kfree(usbduxsub_tmp
->dac_commands
);
2300 usbduxsub_tmp
->dac_commands
= NULL
;
2301 kfree(usbduxsub_tmp
->dux_commands
);
2302 usbduxsub_tmp
->dux_commands
= NULL
;
2303 usbduxsub_tmp
->ai_cmd_running
= 0;
2304 usbduxsub_tmp
->ao_cmd_running
= 0;
2305 usbduxsub_tmp
->pwm_cmd_running
= 0;
2308 static void usbdux_firmware_request_complete_handler(const struct firmware
*fw
,
2311 struct usbduxsub
*usbduxsub_tmp
= context
;
2312 struct usb_device
*usbdev
= usbduxsub_tmp
->usbdev
;
2316 dev_err(&usbdev
->dev
,
2317 "Firmware complete handler without firmware!\n");
2322 * we need to upload the firmware here because fw will be
2323 * freed once we've left this function
2325 ret
= firmwareUpload(usbduxsub_tmp
, fw
->data
, fw
->size
);
2328 dev_err(&usbdev
->dev
,
2329 "Could not upload firmware (err=%d)\n", ret
);
2332 comedi_usb_auto_config(usbdev
, BOARDNAME
);
2334 release_firmware(fw
);
2337 /* allocate memory for the urbs and initialise them */
2338 static int usbduxsigma_probe(struct usb_interface
*uinterf
,
2339 const struct usb_device_id
*id
)
2341 struct usb_device
*udev
= interface_to_usbdev(uinterf
);
2342 struct device
*dev
= &uinterf
->dev
;
2347 dev_dbg(dev
, "comedi_: usbdux_: "
2348 "finding a free structure for the usb-device\n");
2350 down(&start_stop_sem
);
2351 /* look for a free place in the usbdux array */
2353 for (i
= 0; i
< NUMUSBDUX
; i
++) {
2354 if (!(usbduxsub
[i
].probed
)) {
2362 dev_err(dev
, "Too many usbduxsigma-devices connected.\n");
2363 up(&start_stop_sem
);
2366 dev_dbg(dev
, "comedi_: usbdux: "
2367 "usbduxsub[%d] is ready to connect to comedi.\n", index
);
2369 sema_init(&(usbduxsub
[index
].sem
), 1);
2370 /* save a pointer to the usb device */
2371 usbduxsub
[index
].usbdev
= udev
;
2373 /* save the interface itself */
2374 usbduxsub
[index
].interface
= uinterf
;
2375 /* get the interface number from the interface */
2376 usbduxsub
[index
].ifnum
= uinterf
->altsetting
->desc
.bInterfaceNumber
;
2377 /* hand the private data over to the usb subsystem */
2378 /* will be needed for disconnect */
2379 usb_set_intfdata(uinterf
, &(usbduxsub
[index
]));
2381 dev_dbg(dev
, "comedi_: usbdux: ifnum=%d\n", usbduxsub
[index
].ifnum
);
2383 /* test if it is high speed (USB 2.0) */
2384 usbduxsub
[index
].high_speed
=
2385 (usbduxsub
[index
].usbdev
->speed
== USB_SPEED_HIGH
);
2387 /* create space for the commands of the DA converter */
2388 usbduxsub
[index
].dac_commands
= kzalloc(NUMOUTCHANNELS
, GFP_KERNEL
);
2389 if (!usbduxsub
[index
].dac_commands
) {
2390 dev_err(dev
, "comedi_: usbduxsigma: "
2391 "error alloc space for dac commands\n");
2392 tidy_up(&(usbduxsub
[index
]));
2393 up(&start_stop_sem
);
2396 /* create space for the commands going to the usb device */
2397 usbduxsub
[index
].dux_commands
= kzalloc(SIZEOFDUXBUFFER
, GFP_KERNEL
);
2398 if (!usbduxsub
[index
].dux_commands
) {
2399 dev_err(dev
, "comedi_: usbduxsigma: "
2400 "error alloc space for dux commands\n");
2401 tidy_up(&(usbduxsub
[index
]));
2402 up(&start_stop_sem
);
2405 /* create space for the in buffer and set it to zero */
2406 usbduxsub
[index
].inBuffer
= kzalloc(SIZEINBUF
, GFP_KERNEL
);
2407 if (!(usbduxsub
[index
].inBuffer
)) {
2408 dev_err(dev
, "comedi_: usbduxsigma: "
2409 "could not alloc space for inBuffer\n");
2410 tidy_up(&(usbduxsub
[index
]));
2411 up(&start_stop_sem
);
2414 /* create space of the instruction buffer */
2415 usbduxsub
[index
].insnBuffer
= kzalloc(SIZEINSNBUF
, GFP_KERNEL
);
2416 if (!(usbduxsub
[index
].insnBuffer
)) {
2417 dev_err(dev
, "comedi_: usbduxsigma: "
2418 "could not alloc space for insnBuffer\n");
2419 tidy_up(&(usbduxsub
[index
]));
2420 up(&start_stop_sem
);
2423 /* create space for the outbuffer */
2424 usbduxsub
[index
].outBuffer
= kzalloc(SIZEOUTBUF
, GFP_KERNEL
);
2425 if (!(usbduxsub
[index
].outBuffer
)) {
2426 dev_err(dev
, "comedi_: usbduxsigma: "
2427 "could not alloc space for outBuffer\n");
2428 tidy_up(&(usbduxsub
[index
]));
2429 up(&start_stop_sem
);
2432 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2433 i
= usb_set_interface(usbduxsub
[index
].usbdev
,
2434 usbduxsub
[index
].ifnum
, 3);
2436 dev_err(dev
, "comedi_: usbduxsigma%d: "
2437 "could not set alternate setting 3 in high speed.\n",
2439 tidy_up(&(usbduxsub
[index
]));
2440 up(&start_stop_sem
);
2443 if (usbduxsub
[index
].high_speed
)
2444 usbduxsub
[index
].numOfInBuffers
= NUMOFINBUFFERSHIGH
;
2446 usbduxsub
[index
].numOfInBuffers
= NUMOFINBUFFERSFULL
;
2448 usbduxsub
[index
].urbIn
=
2449 kzalloc(sizeof(struct urb
*) * usbduxsub
[index
].numOfInBuffers
,
2451 if (!(usbduxsub
[index
].urbIn
)) {
2452 dev_err(dev
, "comedi_: usbduxsigma: "
2453 "Could not alloc. urbIn array\n");
2454 tidy_up(&(usbduxsub
[index
]));
2455 up(&start_stop_sem
);
2458 for (i
= 0; i
< usbduxsub
[index
].numOfInBuffers
; i
++) {
2459 /* one frame: 1ms */
2460 usbduxsub
[index
].urbIn
[i
] = usb_alloc_urb(1, GFP_KERNEL
);
2461 if (usbduxsub
[index
].urbIn
[i
] == NULL
) {
2462 dev_err(dev
, "comedi_: usbduxsigma%d: "
2463 "Could not alloc. urb(%d)\n", index
, i
);
2464 tidy_up(&(usbduxsub
[index
]));
2465 up(&start_stop_sem
);
2468 usbduxsub
[index
].urbIn
[i
]->dev
= usbduxsub
[index
].usbdev
;
2469 /* will be filled later with a pointer to the comedi-device */
2470 /* and ONLY then the urb should be submitted */
2471 usbduxsub
[index
].urbIn
[i
]->context
= NULL
;
2472 usbduxsub
[index
].urbIn
[i
]->pipe
=
2473 usb_rcvisocpipe(usbduxsub
[index
].usbdev
, ISOINEP
);
2474 usbduxsub
[index
].urbIn
[i
]->transfer_flags
= URB_ISO_ASAP
;
2475 usbduxsub
[index
].urbIn
[i
]->transfer_buffer
=
2476 kzalloc(SIZEINBUF
, GFP_KERNEL
);
2477 if (!(usbduxsub
[index
].urbIn
[i
]->transfer_buffer
)) {
2478 dev_err(dev
, "comedi_: usbduxsigma%d: "
2479 "could not alloc. transb.\n", index
);
2480 tidy_up(&(usbduxsub
[index
]));
2481 up(&start_stop_sem
);
2484 usbduxsub
[index
].urbIn
[i
]->complete
= usbduxsub_ai_IsocIrq
;
2485 usbduxsub
[index
].urbIn
[i
]->number_of_packets
= 1;
2486 usbduxsub
[index
].urbIn
[i
]->transfer_buffer_length
= SIZEINBUF
;
2487 usbduxsub
[index
].urbIn
[i
]->iso_frame_desc
[0].offset
= 0;
2488 usbduxsub
[index
].urbIn
[i
]->iso_frame_desc
[0].length
=
2493 if (usbduxsub
[index
].high_speed
)
2494 usbduxsub
[index
].numOfOutBuffers
= NUMOFOUTBUFFERSHIGH
;
2496 usbduxsub
[index
].numOfOutBuffers
= NUMOFOUTBUFFERSFULL
;
2498 usbduxsub
[index
].urbOut
=
2499 kzalloc(sizeof(struct urb
*) * usbduxsub
[index
].numOfOutBuffers
,
2501 if (!(usbduxsub
[index
].urbOut
)) {
2502 dev_err(dev
, "comedi_: usbduxsigma: "
2503 "Could not alloc. urbOut array\n");
2504 tidy_up(&(usbduxsub
[index
]));
2505 up(&start_stop_sem
);
2508 for (i
= 0; i
< usbduxsub
[index
].numOfOutBuffers
; i
++) {
2509 /* one frame: 1ms */
2510 usbduxsub
[index
].urbOut
[i
] = usb_alloc_urb(1, GFP_KERNEL
);
2511 if (usbduxsub
[index
].urbOut
[i
] == NULL
) {
2512 dev_err(dev
, "comedi_: usbduxsigma%d: "
2513 "Could not alloc. urb(%d)\n", index
, i
);
2514 tidy_up(&(usbduxsub
[index
]));
2515 up(&start_stop_sem
);
2518 usbduxsub
[index
].urbOut
[i
]->dev
= usbduxsub
[index
].usbdev
;
2519 /* will be filled later with a pointer to the comedi-device */
2520 /* and ONLY then the urb should be submitted */
2521 usbduxsub
[index
].urbOut
[i
]->context
= NULL
;
2522 usbduxsub
[index
].urbOut
[i
]->pipe
=
2523 usb_sndisocpipe(usbduxsub
[index
].usbdev
, ISOOUTEP
);
2524 usbduxsub
[index
].urbOut
[i
]->transfer_flags
= URB_ISO_ASAP
;
2525 usbduxsub
[index
].urbOut
[i
]->transfer_buffer
=
2526 kzalloc(SIZEOUTBUF
, GFP_KERNEL
);
2527 if (!(usbduxsub
[index
].urbOut
[i
]->transfer_buffer
)) {
2528 dev_err(dev
, "comedi_: usbduxsigma%d: "
2529 "could not alloc. transb.\n", index
);
2530 tidy_up(&(usbduxsub
[index
]));
2531 up(&start_stop_sem
);
2534 usbduxsub
[index
].urbOut
[i
]->complete
= usbduxsub_ao_IsocIrq
;
2535 usbduxsub
[index
].urbOut
[i
]->number_of_packets
= 1;
2536 usbduxsub
[index
].urbOut
[i
]->transfer_buffer_length
=
2538 usbduxsub
[index
].urbOut
[i
]->iso_frame_desc
[0].offset
= 0;
2539 usbduxsub
[index
].urbOut
[i
]->iso_frame_desc
[0].length
=
2541 if (usbduxsub
[index
].high_speed
) {
2543 usbduxsub
[index
].urbOut
[i
]->interval
= 8;
2546 usbduxsub
[index
].urbOut
[i
]->interval
= 1;
2551 if (usbduxsub
[index
].high_speed
) {
2552 /* max bulk ep size in high speed */
2553 usbduxsub
[index
].sizePwmBuf
= 512;
2554 usbduxsub
[index
].urbPwm
= usb_alloc_urb(0, GFP_KERNEL
);
2555 if (usbduxsub
[index
].urbPwm
== NULL
) {
2556 dev_err(dev
, "comedi_: usbduxsigma%d: "
2557 "Could not alloc. pwm urb\n", index
);
2558 tidy_up(&(usbduxsub
[index
]));
2559 up(&start_stop_sem
);
2562 usbduxsub
[index
].urbPwm
->transfer_buffer
=
2563 kzalloc(usbduxsub
[index
].sizePwmBuf
, GFP_KERNEL
);
2564 if (!(usbduxsub
[index
].urbPwm
->transfer_buffer
)) {
2565 dev_err(dev
, "comedi_: usbduxsigma%d: "
2566 "could not alloc. transb. for pwm\n", index
);
2567 tidy_up(&(usbduxsub
[index
]));
2568 up(&start_stop_sem
);
2572 usbduxsub
[index
].urbPwm
= NULL
;
2573 usbduxsub
[index
].sizePwmBuf
= 0;
2576 usbduxsub
[index
].ai_cmd_running
= 0;
2577 usbduxsub
[index
].ao_cmd_running
= 0;
2578 usbduxsub
[index
].pwm_cmd_running
= 0;
2580 /* we've reached the bottom of the function */
2581 usbduxsub
[index
].probed
= 1;
2582 up(&start_stop_sem
);
2584 ret
= request_firmware_nowait(THIS_MODULE
,
2586 "usbduxsigma_firmware.bin",
2590 usbdux_firmware_request_complete_handler
2594 dev_err(dev
, "Could not load firmware (err=%d)\n", ret
);
2598 dev_info(dev
, "comedi_: successfully initialised.\n");
2603 static void usbduxsigma_disconnect(struct usb_interface
*intf
)
2605 struct usbduxsub
*usbduxsub_tmp
= usb_get_intfdata(intf
);
2606 struct usb_device
*udev
= interface_to_usbdev(intf
);
2608 if (!usbduxsub_tmp
) {
2610 "comedi_: disconnect called with null pointer.\n");
2613 if (usbduxsub_tmp
->usbdev
!= udev
) {
2614 dev_err(&intf
->dev
, "comedi_: BUG! wrong ptr!\n");
2617 if (usbduxsub_tmp
->ai_cmd_running
)
2618 /* we are still running a command */
2619 usbdux_ai_stop(usbduxsub_tmp
, 1);
2620 if (usbduxsub_tmp
->ao_cmd_running
)
2621 /* we are still running a command */
2622 usbdux_ao_stop(usbduxsub_tmp
, 1);
2623 comedi_usb_auto_unconfig(udev
);
2624 down(&start_stop_sem
);
2625 down(&usbduxsub_tmp
->sem
);
2626 tidy_up(usbduxsub_tmp
);
2627 up(&usbduxsub_tmp
->sem
);
2628 up(&start_stop_sem
);
2629 dev_info(&intf
->dev
, "comedi_: disconnected from the usb\n");
2632 /* is called when comedi-config is called */
2633 static int usbduxsigma_attach(struct comedi_device
*dev
,
2634 struct comedi_devconfig
*it
)
2639 struct usbduxsub
*udev
;
2643 struct comedi_subdevice
*s
= NULL
;
2644 dev
->private = NULL
;
2646 down(&start_stop_sem
);
2647 /* find a valid device which has been detected by the probe function of
2650 for (i
= 0; i
< NUMUSBDUX
; i
++) {
2651 if ((usbduxsub
[i
].probed
) && (!usbduxsub
[i
].attached
)) {
2658 printk(KERN_ERR
"comedi%d: usbduxsigma: error: attach failed,"
2659 "dev not connected to the usb bus.\n", dev
->minor
);
2660 up(&start_stop_sem
);
2664 udev
= &usbduxsub
[index
];
2666 /* pointer back to the corresponding comedi device */
2667 udev
->comedidev
= dev
;
2669 /* trying to upload the firmware into the FX2 */
2670 if (comedi_aux_data(it
->options
, 0) &&
2671 it
->options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
2672 firmwareUpload(udev
, comedi_aux_data(it
->options
, 0),
2673 it
->options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]);
2676 dev
->board_name
= BOARDNAME
;
2678 /* set number of subdevices */
2679 if (udev
->high_speed
) {
2681 dev
->n_subdevices
= 4;
2684 dev
->n_subdevices
= 3;
2687 /* allocate space for the subdevices */
2688 ret
= alloc_subdevices(dev
, dev
->n_subdevices
);
2690 dev_err(&udev
->interface
->dev
,
2691 "comedi%d: no space for subdev\n", dev
->minor
);
2692 up(&start_stop_sem
);
2696 /* private structure is also simply the usb-structure */
2697 dev
->private = udev
;
2699 /* the first subdevice is the A/D converter */
2700 s
= dev
->subdevices
+ SUBDEV_AD
;
2701 /* the URBs get the comedi subdevice */
2702 /* which is responsible for reading */
2703 /* this is the subdevice which reads data */
2704 dev
->read_subdev
= s
;
2705 /* the subdevice receives as private structure the */
2709 s
->type
= COMEDI_SUBD_AI
;
2710 /* readable and ref is to ground, 32 bit wide data! */
2711 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
|
2712 SDF_CMD_READ
| SDF_LSAMPL
;
2713 /* 16 A/D channels */
2714 s
->n_chan
= NUMCHANNELS
;
2715 /* length of the channellist */
2716 s
->len_chanlist
= NUMCHANNELS
;
2717 /* callback functions */
2718 s
->insn_read
= usbdux_ai_insn_read
;
2719 s
->do_cmdtest
= usbdux_ai_cmdtest
;
2720 s
->do_cmd
= usbdux_ai_cmd
;
2721 s
->cancel
= usbdux_ai_cancel
;
2722 /* max value from the A/D converter (24bit) */
2723 s
->maxdata
= 0x00FFFFFF;
2724 /* range table to convert to physical units */
2725 s
->range_table
= (&range_usbdux_ai_range
);
2728 s
= dev
->subdevices
+ SUBDEV_DA
;
2730 s
->type
= COMEDI_SUBD_AO
;
2731 /* backward pointer */
2732 dev
->write_subdev
= s
;
2733 /* the subdevice receives as private structure the */
2737 s
->subdev_flags
= SDF_WRITABLE
| SDF_GROUND
| SDF_CMD_WRITE
;
2740 /* length of the channellist */
2741 s
->len_chanlist
= 4;
2742 /* 8 bit resolution */
2743 s
->maxdata
= 0x00ff;
2744 /* unipolar range */
2745 s
->range_table
= (&range_usbdux_ao_range
);
2747 s
->do_cmdtest
= usbdux_ao_cmdtest
;
2748 s
->do_cmd
= usbdux_ao_cmd
;
2749 s
->cancel
= usbdux_ao_cancel
;
2750 s
->insn_read
= usbdux_ao_insn_read
;
2751 s
->insn_write
= usbdux_ao_insn_write
;
2754 s
= dev
->subdevices
+ SUBDEV_DIO
;
2755 s
->type
= COMEDI_SUBD_DIO
;
2756 s
->subdev_flags
= SDF_READABLE
| SDF_WRITABLE
;
2757 /* 8 external and 16 internal channels */
2760 s
->range_table
= (&range_digital
);
2761 s
->insn_bits
= usbdux_dio_insn_bits
;
2762 s
->insn_config
= usbdux_dio_insn_config
;
2763 /* we don't use it */
2766 if (udev
->high_speed
) {
2768 s
= dev
->subdevices
+ SUBDEV_PWM
;
2769 s
->type
= COMEDI_SUBD_PWM
;
2770 s
->subdev_flags
= SDF_WRITABLE
| SDF_PWM_HBRIDGE
;
2772 /* this defines the max duty cycle resolution */
2773 s
->maxdata
= udev
->sizePwmBuf
;
2774 s
->insn_write
= usbdux_pwm_write
;
2775 s
->insn_read
= usbdux_pwm_read
;
2776 s
->insn_config
= usbdux_pwm_config
;
2777 usbdux_pwm_period(dev
, s
, PWM_DEFAULT_PERIOD
);
2779 /* finally decide that it's attached */
2784 up(&start_stop_sem
);
2786 offset
= usbdux_getstatusinfo(dev
, 0);
2788 dev_err(&udev
->interface
->dev
,
2789 "Communication to USBDUXSIGMA failed!"
2790 "Check firmware and cabling.");
2792 dev_info(&udev
->interface
->dev
,
2793 "comedi%d: attached, ADC_zero = %x", dev
->minor
, offset
);
2798 static int usbduxsigma_detach(struct comedi_device
*dev
)
2800 struct usbduxsub
*usbduxsub_tmp
;
2804 "comedi? usbduxsigma detach: dev=NULL\n");
2808 usbduxsub_tmp
= dev
->private;
2809 if (!usbduxsub_tmp
) {
2811 "comedi?: usbduxsigma detach: private=NULL\n");
2815 dev_dbg(&usbduxsub_tmp
->interface
->dev
,
2816 "comedi%d: detach usb device\n",
2819 down(&usbduxsub_tmp
->sem
);
2820 /* Don't allow detach to free the private structure */
2821 /* It's one entry of of usbduxsub[] */
2822 dev
->private = NULL
;
2823 usbduxsub_tmp
->attached
= 0;
2824 usbduxsub_tmp
->comedidev
= NULL
;
2825 dev_info(&usbduxsub_tmp
->interface
->dev
,
2826 "comedi%d: successfully detached.\n", dev
->minor
);
2827 up(&usbduxsub_tmp
->sem
);
2831 /* main driver struct */
2832 static struct comedi_driver driver_usbduxsigma
= {
2833 .driver_name
= "usbduxsigma",
2834 .module
= THIS_MODULE
,
2835 .attach
= usbduxsigma_attach
,
2836 .detach
= usbduxsigma_detach
,
2839 /* Table with the USB-devices */
2840 static const struct usb_device_id usbduxsigma_table
[] = {
2841 {USB_DEVICE(0x13d8, 0x0020)},
2842 {USB_DEVICE(0x13d8, 0x0021)},
2843 {USB_DEVICE(0x13d8, 0x0022)},
2844 {} /* Terminating entry */
2847 MODULE_DEVICE_TABLE(usb
, usbduxsigma_table
);
2849 /* The usbduxsub-driver */
2850 static struct usb_driver usbduxsigma_driver
= {
2852 .probe
= usbduxsigma_probe
,
2853 .disconnect
= usbduxsigma_disconnect
,
2854 .id_table
= usbduxsigma_table
,
2857 /* Can't use the nice macro as I have also to initialise the USB */
2859 /* registering the usb-system _and_ the comedi-driver */
2860 static int __init
init_usbduxsigma(void)
2862 printk(KERN_INFO KBUILD_MODNAME
": "
2863 DRIVER_VERSION
":" DRIVER_DESC
"\n");
2864 usb_register(&usbduxsigma_driver
);
2865 comedi_driver_register(&driver_usbduxsigma
);
2869 /* deregistering the comedi driver and the usb-subsystem */
2870 static void __exit
exit_usbduxsigma(void)
2872 comedi_driver_unregister(&driver_usbduxsigma
);
2873 usb_deregister(&usbduxsigma_driver
);
2876 module_init(init_usbduxsigma
);
2877 module_exit(exit_usbduxsigma
);
2879 MODULE_AUTHOR(DRIVER_AUTHOR
);
2880 MODULE_DESCRIPTION(DRIVER_DESC
);
2881 MODULE_LICENSE("GPL");