1 #define DRIVER_VERSION "v2.1"
2 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
3 #define DRIVER_DESC "Stirling/ITL USB-DUX -- Bernd.Porr@f2s.com"
5 comedi/drivers/usbdux.c
6 Copyright (C) 2003-2007 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 (usbdux.o)
27 Author: Bernd Porr <BerndPorr@f2s.com>
30 Configuration options:
31 You have to upload firmware with the -i option. The
32 firmware is usually installed under /usr/share/usb or
33 /usr/local/share/usb or /lib/firmware.
35 Connection scheme for the counter at the digital port:
36 0=/CLK0, 1=UP/DOWN0, 2=RESET0, 4=/CLK1, 5=UP/DOWN1, 6=RESET1.
37 The sampling rate of the counter is approximately 500Hz.
39 Please note that under USB2.0 the length of the channel list determines
40 the max sampling rate. If you sample only one channel you get 8kHz
41 sampling rate. If you sample two channels you get 4kHz and so on.
44 * I must give credit here to Chris Baugher who
45 * wrote the driver for AT-MIO-16d. I used some parts of this
46 * driver. I also must give credits to David Brownell
47 * who supported me with the USB development.
53 * 0.94: D/A output should work now with any channel list combinations
54 * 0.95: .owner commented out for kernel vers below 2.4.19
55 * sanity checks in ai/ao_cmd
56 * 0.96: trying to get it working with 2.6, moved all memory alloc to comedi's attach final USB IDs
57 * moved memory allocation completely to the corresponding comedi functions
58 * firmware upload is by fxload and no longer by comedi (due to enumeration)
59 * 0.97: USB IDs received, adjusted table
60 * 0.98: SMP, locking, memroy alloc: moved all usb memory alloc
61 * to the usb subsystem and moved all comedi related memory
63 * | kernel | registration | usbdux-usb | usbdux-comedi | comedi |
64 * 0.99: USB 2.0: changed protocol to isochronous transfer
65 * IRQ transfer is too buggy and too risky in 2.0
66 * for the high speed ISO transfer is now a working version available
67 * 0.99b: Increased the iso transfer buffer for high sp.to 10 buffers. Some VIA
68 * chipsets miss out IRQs. Deeper buffering is needed.
69 * 1.00: full USB 2.0 support for the A/D converter. Now: max 8kHz sampling rate.
70 * Firmware vers 1.00 is needed for this.
71 * Two 16 bit up/down/reset counter with a sampling rate of 1kHz
72 * And loads of cleaning up, in particular streamlining the
74 * 1.1: moved EP4 transfers to EP1 to make space for a PWM output on EP4
75 * 1.2: added PWM suport via EP4
76 * 2.0: PWM seems to be stable and is not interfering with the other functions
77 * 2.1: changed PWM API
81 /* generates loads of debug info */
82 /* #define NOISY_DUX_DEBUGBUG */
84 #include <linux/kernel.h>
85 #include <linux/module.h>
86 #include <linux/init.h>
87 #include <linux/slab.h>
88 #include <linux/input.h>
89 #include <linux/usb.h>
90 #include <linux/smp_lock.h>
91 #include <linux/fcntl.h>
92 #include <linux/compiler.h>
94 #include "../comedidev.h"
97 #define BOARDNAME "usbdux"
99 /* timeout for the USB-transfer */
102 /* constants for "firmware" upload and download */
103 #define USBDUXSUB_FIRMWARE 0xA0
104 #define VENDOR_DIR_IN 0xC0
105 #define VENDOR_DIR_OUT 0x40
107 /* internal adresses of the 8051 processor */
108 #define USBDUXSUB_CPUCS 0xE600
111 * the minor device number, major is 180 only for debugging purposes and to
112 * upload special firmware (programming the eeprom etc) which is not compatible
113 * with the comedi framwork
115 #define USBDUXSUB_MINOR 32
117 /* max lenghth of the transfer-buffer for software upload */
118 #define TB_LEN 0x2000
120 /* Input endpoint number: ISO/IRQ */
123 /* Output endpoint number: ISO/IRQ */
126 /* This EP sends DUX commands to USBDUX */
127 #define COMMAND_OUT_EP 1
129 /* This EP receives the DUX commands from USBDUX */
130 #define COMMAND_IN_EP 8
132 /* Output endpoint for PWM */
135 /* 300Hz max frequ under PWM */
136 #define MIN_PWM_PERIOD ((long)(1E9/300))
138 /* Default PWM frequency */
139 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
141 /* Number of channels */
142 #define NUMCHANNELS 8
144 /* Size of one A/D value */
145 #define SIZEADIN ((sizeof(int16_t)))
147 /* Size of the input-buffer IN BYTES */
148 /* Always multiple of 8 for 8 microframes which is needed in the highspeed mode */
149 #define SIZEINBUF ((8*SIZEADIN))
152 #define SIZEINSNBUF 16
154 /* Number of DA channels */
155 #define NUMOUTCHANNELS 8
157 /* size of one value for the D/A converter: channel and value */
158 #define SIZEDAOUT ((sizeof(int8_t)+sizeof(int16_t)))
161 * Size of the output-buffer in bytes
162 * Actually only the first 4 triplets are used but for the
163 * high speed mode we need to pad it to 8 (microframes).
165 #define SIZEOUTBUF ((8*SIZEDAOUT))
168 * Size of the buffer for the dux commands: just now max size is determined
169 * by the analogue out + command byte + panic bytes...
171 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
173 /* Number of in-URBs which receive the data: min=2 */
174 #define NUMOFINBUFFERSFULL 5
176 /* Number of out-URBs which send the data: min=2 */
177 #define NUMOFOUTBUFFERSFULL 5
179 /* Number of in-URBs which receive the data: min=5 */
180 /* must have more buffers due to buggy USB ctr */
181 #define NUMOFINBUFFERSHIGH 10
183 /* Number of out-URBs which send the data: min=5 */
184 /* must have more buffers due to buggy USB ctr */
185 #define NUMOFOUTBUFFERSHIGH 10
187 /* Total number of usbdux devices */
190 /* Analogue in subdevice */
193 /* Analogue out subdevice */
200 #define SUBDEV_COUNTER 3
202 /* timer aka pwm output */
205 /* number of retries to get the right dux command */
208 /**************************************************/
209 /* comedi constants */
210 static const comedi_lrange range_usbdux_ai_range = { 4, {
212 BIP_RANGE(4.096 / 2),
218 static const comedi_lrange range_usbdux_ao_range = { 2, {
225 * private structure of one subdevice
229 * This is the structure which holds all the data of
230 * this driver one sub device just now: A/D
235 /* is it associated with a subdevice? */
237 /* pointer to the usb-device */
238 struct usb_device *usbdev;
239 /* actual number of in-buffers */
241 /* actual number of out-buffers */
243 /* ISO-transfer handling: buffers */
246 /* pwm-transfer handling */
250 /* PWM internal delay for the GPIF in the FX2 */
252 /* size of the PWM buffer which holds the bit pattern */
254 /* input buffer for the ISO-transfer */
256 /* input buffer for single insn */
258 /* output buffer for single DA outputs */
260 /* interface number */
262 /* interface structure in 2.6 */
263 struct usb_interface *interface;
264 /* comedi device for the interrupt context */
265 comedi_device *comedidev;
266 /* is it USB_SPEED_HIGH or not? */
267 short int high_speed;
268 /* asynchronous command is running */
269 short int ai_cmd_running;
270 short int ao_cmd_running;
272 short int pwm_cmd_running;
273 /* continous aquisition */
274 short int ai_continous;
275 short int ao_continous;
276 /* number of samples to aquire */
279 /* time between samples in units of the timer */
280 unsigned int ai_timer;
281 unsigned int ao_timer;
282 /* counter between aquisitions */
283 unsigned int ai_counter;
284 unsigned int ao_counter;
285 /* interval in frames/uframes */
286 unsigned int ai_interval;
288 int8_t *dac_commands;
290 int8_t *dux_commands;
291 struct semaphore sem;
295 * The pointer to the private usb-data of the driver is also the private data
296 * for the comedi-device. This has to be global as the usb subsystem needs
297 * global variables. The other reason is that this structure must be there
298 * _before_ any comedi command is issued. The usb subsystem must be initialised
299 * before comedi can access it.
301 static usbduxsub_t usbduxsub[NUMUSBDUX];
303 static DECLARE_MUTEX(start_stop_sem);
306 * Stops the data acquision
307 * It should be safe to call this function from any context
309 static int usbduxsub_unlink_InURBs(usbduxsub_t *usbduxsub_tmp)
314 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
315 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
316 if (usbduxsub_tmp->urbIn[i]) {
317 /* We wait here until all transfers have been
319 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
321 #ifdef NOISY_DUX_DEBUGBUG
322 printk("comedi: usbdux: unlinked InURB %d, err=%d\n",
331 * This will stop a running acquisition operation
332 * Is called from within this driver from both the
333 * interrupt context and from comedi
335 static int usbdux_ai_stop(usbduxsub_t *this_usbduxsub, int do_unlink)
339 if (!this_usbduxsub) {
340 printk("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
343 #ifdef NOISY_DUX_DEBUGBUG
344 printk("comedi: usbdux_ai_stop\n");
349 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
352 this_usbduxsub->ai_cmd_running = 0;
358 * This will cancel a running acquisition operation.
359 * This is called by comedi but never from inside the driver.
361 static int usbdux_ai_cancel(comedi_device * dev, comedi_subdevice * s)
363 usbduxsub_t *this_usbduxsub;
366 /* force unlink of all urbs */
367 #ifdef NOISY_DUX_DEBUGBUG
368 printk("comedi: usbdux_ai_cancel\n");
370 this_usbduxsub = dev->private;
371 if (!this_usbduxsub) {
372 printk("comedi: usbdux_ai_cancel: this_usbduxsub=NULL\n");
375 /* prevent other CPUs from submitting new commands just now */
376 down(&this_usbduxsub->sem);
377 if (!(this_usbduxsub->probed)) {
378 up(&this_usbduxsub->sem);
381 /* unlink only if the urb really has been submitted */
382 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
383 up(&this_usbduxsub->sem);
387 /* analogue IN - interrupt service routine */
388 static void usbduxsub_ai_IsocIrq(struct urb *urb)
391 usbduxsub_t *this_usbduxsub;
392 comedi_device *this_comedidev;
396 /* is the urb there? */
398 printk("comedi_: usbdux_: ao int-handler called with urb=NULL!\n");
401 /* the context variable points to the subdevice */
402 this_comedidev = urb->context;
403 if (unlikely(!this_comedidev)) {
404 printk("comedi_: usbdux_: BUG! urb context is a NULL pointer!\n");
407 /* the private structure of the subdevice is usbduxsub_t */
408 this_usbduxsub = this_comedidev->private;
409 if (unlikely(!this_usbduxsub)) {
410 printk("comedi_: usbdux_: BUG! private of comedi subdev is a NULL pointer!\n");
413 /* subdevice which is the AD converter */
414 s = this_comedidev->subdevices + SUBDEV_AD;
416 /* first we test if something unusual has just happened */
417 switch (urb->status) {
419 /* copy the result in the transfer buffer */
420 memcpy(this_usbduxsub->inBuffer,
421 urb->transfer_buffer, SIZEINBUF);
424 /* error in the ISOchronous data */
425 /* we don't copy the data into the transfer buffer */
426 /* and recycle the last data byte */
427 #ifdef CONFIG_COMEDI_DEBUG
428 printk("comedi%d: usbdux: CRC error in ISO IN stream.\n",
429 this_usbduxsub->comedidev->minor);
438 /* happens after an unlink command */
439 if (this_usbduxsub->ai_cmd_running) {
440 /* we are still running a command */
441 /* tell this comedi */
442 s->async->events |= COMEDI_CB_EOA;
443 s->async->events |= COMEDI_CB_ERROR;
444 comedi_event(this_usbduxsub->comedidev, s);
445 /* stop the transfer w/o unlink */
446 usbdux_ai_stop(this_usbduxsub, 0);
451 /* a real error on the bus */
452 /* pass error to comedi if we are really running a command */
453 if (this_usbduxsub->ai_cmd_running) {
454 printk("Non-zero urb status received in ai intr context: %d\n", urb->status);
455 s->async->events |= COMEDI_CB_EOA;
456 s->async->events |= COMEDI_CB_ERROR;
457 comedi_event(this_usbduxsub->comedidev, s);
458 /* don't do an unlink here */
459 usbdux_ai_stop(this_usbduxsub, 0);
464 /* at this point we are reasonably sure that nothing dodgy has happened */
465 /* are we running a command? */
466 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
468 * not running a command, do not continue execution if no
469 * asynchronous command is running in particular not resubmit
474 urb->dev = this_usbduxsub->usbdev;
476 /* resubmit the urb */
477 err = USB_SUBMIT_URB(urb);
478 if (unlikely(err < 0)) {
479 printk("comedi_: usbdux_: urb resubmit failed in int-context! err=%d ", err);
480 if (err == -EL2NSYNC)
481 printk("--> buggy USB host controller or bug in IRQ handler!\n");
484 s->async->events |= COMEDI_CB_EOA;
485 s->async->events |= COMEDI_CB_ERROR;
486 comedi_event(this_usbduxsub->comedidev, s);
487 /* don't do an unlink here */
488 usbdux_ai_stop(this_usbduxsub, 0);
492 this_usbduxsub->ai_counter--;
493 if (likely(this_usbduxsub->ai_counter > 0))
496 /* timer zero, transfer measurements to comedi */
497 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
499 /* test, if we transmit only a fixed number of samples */
500 if (!(this_usbduxsub->ai_continous)) {
501 /* not continous, fixed number of samples */
502 this_usbduxsub->ai_sample_count--;
503 /* all samples received? */
504 if (this_usbduxsub->ai_sample_count < 0) {
505 /* prevent a resubmit next time */
506 usbdux_ai_stop(this_usbduxsub, 0);
507 /* say comedi that the acquistion is over */
508 s->async->events |= COMEDI_CB_EOA;
509 comedi_event(this_usbduxsub->comedidev, s);
513 /* get the data from the USB bus and hand it over to comedi */
514 n = s->async->cmd.chanlist_len;
515 for (i = 0; i < n; i++) {
517 if (CR_RANGE(s->async->cmd.chanlist[i]) <= 1) {
520 le16_to_cpu(this_usbduxsub->
521 inBuffer[i]) ^ 0x800);
525 le16_to_cpu(this_usbduxsub->inBuffer[i]));
528 /* tell comedi that data is there */
529 comedi_event(this_usbduxsub->comedidev, s);
532 static int usbduxsub_unlink_OutURBs(usbduxsub_t *usbduxsub_tmp)
537 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
538 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
539 if (usbduxsub_tmp->urbOut[i])
540 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
542 #ifdef NOISY_DUX_DEBUGBUG
543 printk("comedi: usbdux: unlinked OutURB %d: res=%d\n",
551 /* This will cancel a running acquisition operation
554 static int usbdux_ao_stop(usbduxsub_t *this_usbduxsub, int do_unlink)
558 if (!this_usbduxsub) {
559 #ifdef NOISY_DUX_DEBUGBUG
560 printk("comedi?: usbdux_ao_stop: this_usbduxsub=NULL!\n");
564 #ifdef NOISY_DUX_DEBUGBUG
565 printk("comedi: usbdux_ao_cancel\n");
568 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
570 this_usbduxsub->ao_cmd_running = 0;
575 /* force unlink, is called by comedi */
576 static int usbdux_ao_cancel(comedi_device *dev, comedi_subdevice *s)
578 usbduxsub_t *this_usbduxsub = dev->private;
581 if (!this_usbduxsub) {
582 printk("comedi: usbdux_ao_cancel: this_usbduxsub=NULL\n");
585 /* prevent other CPUs from submitting a command just now */
586 down(&this_usbduxsub->sem);
587 if (!(this_usbduxsub->probed)) {
588 up(&this_usbduxsub->sem);
591 /* unlink only if it is really running */
592 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
593 up(&this_usbduxsub->sem);
597 static void usbduxsub_ao_IsocIrq(struct urb *urb)
601 usbduxsub_t *this_usbduxsub;
602 comedi_device *this_comedidev;
606 printk("comedi_: usbdux_: ao urb handler called with NULL ptr.\n");
609 /* the context variable points to the subdevice */
610 this_comedidev = urb->context;
611 if (!this_comedidev) {
612 printk("comedi_: usbdux_: ao urb int-context is a NULL pointer.\n");
615 /* the private structure of the subdevice is usbduxsub_t */
616 this_usbduxsub = this_comedidev->private;
617 if (!this_usbduxsub) {
618 printk("comedi_: usbdux_: private data structure of ao subdev is NULL p.\n");
622 s = this_comedidev->subdevices + SUBDEV_DA;
624 switch (urb->status) {
633 /* after an unlink command, unplug, ... etc */
634 /* no unlink needed here. Already shutting down. */
635 if (this_usbduxsub->ao_cmd_running) {
636 s->async->events |= COMEDI_CB_EOA;
637 comedi_event(this_usbduxsub->comedidev, s);
638 usbdux_ao_stop(this_usbduxsub, 0);
644 if (this_usbduxsub->ao_cmd_running) {
645 printk("comedi_: usbdux_: Non-zero urb status received in ao intr context: %d\n", urb->status);
646 s->async->events |= COMEDI_CB_ERROR;
647 s->async->events |= COMEDI_CB_EOA;
648 comedi_event(this_usbduxsub->comedidev, s);
649 /* we do an unlink if we are in the high speed mode */
650 usbdux_ao_stop(this_usbduxsub, 0);
655 /* are we actually running? */
656 if (!(this_usbduxsub->ao_cmd_running))
659 /* normal operation: executing a command in this subdevice */
660 this_usbduxsub->ao_counter--;
661 if (this_usbduxsub->ao_counter <= 0) {
663 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
665 /* handle non continous aquisition */
666 if (!(this_usbduxsub->ao_continous)) {
667 /* fixed number of samples */
668 this_usbduxsub->ao_sample_count--;
669 if (this_usbduxsub->ao_sample_count < 0) {
670 /* all samples transmitted */
671 usbdux_ao_stop(this_usbduxsub, 0);
672 s->async->events |= COMEDI_CB_EOA;
673 comedi_event(this_usbduxsub->comedidev, s);
674 /* no resubmit of the urb */
678 /* transmit data to the USB bus */
679 ((uint8_t *) (urb->transfer_buffer))[0] =
680 s->async->cmd.chanlist_len;
681 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
683 if (i >= NUMOUTCHANNELS)
686 /* pointer to the DA */
687 datap = (&(((int8_t *) urb->transfer_buffer)[i * 3 + 1]));
688 /* get the data from comedi */
689 ret = comedi_buf_get(s->async, &temp);
691 datap[1] = temp >> 8;
692 datap[2] = this_usbduxsub->dac_commands[i];
693 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
694 /* datap[0],datap[1],datap[2]); */
696 printk("comedi: usbdux: buffer underflow\n");
697 s->async->events |= COMEDI_CB_EOA;
698 s->async->events |= COMEDI_CB_OVERFLOW;
700 /* transmit data to comedi */
701 s->async->events |= COMEDI_CB_BLOCK;
702 comedi_event(this_usbduxsub->comedidev, s);
705 urb->transfer_buffer_length = SIZEOUTBUF;
706 urb->dev = this_usbduxsub->usbdev;
708 if (this_usbduxsub->ao_cmd_running) {
709 if (this_usbduxsub->high_speed) {
716 urb->number_of_packets = 1;
717 urb->iso_frame_desc[0].offset = 0;
718 urb->iso_frame_desc[0].length = SIZEOUTBUF;
719 urb->iso_frame_desc[0].status = 0;
720 if ((ret = USB_SUBMIT_URB(urb)) < 0) {
721 printk("comedi_: usbdux_: ao urb resubm failed in int-cont.");
722 printk("ret=%d", ret);
724 printk("--> buggy USB host controller or bug in IRQ handling!\n");
728 s->async->events |= COMEDI_CB_EOA;
729 s->async->events |= COMEDI_CB_ERROR;
730 comedi_event(this_usbduxsub->comedidev, s);
731 /* don't do an unlink here */
732 usbdux_ao_stop(this_usbduxsub, 0);
737 static int usbduxsub_start(usbduxsub_t *usbduxsub)
740 uint8_t local_transfer_buffer[16];
742 if (usbduxsub->probed) {
744 local_transfer_buffer[0] = 0;
745 errcode = USB_CONTROL_MSG(usbduxsub->usbdev,
746 /* create a pipe for a control transfer */
747 usb_sndctrlpipe(usbduxsub->usbdev, 0),
748 /* bRequest, "Firmware" */
756 /* address of the transfer buffer */
757 local_transfer_buffer,
763 printk("comedi_: usbdux_: control msg failed (start)\n");
770 static int usbduxsub_stop(usbduxsub_t *usbduxsub)
774 uint8_t local_transfer_buffer[16];
775 if (usbduxsub->probed) {
777 local_transfer_buffer[0] = 1;
778 errcode = USB_CONTROL_MSG
780 usb_sndctrlpipe(usbduxsub->usbdev, 0),
781 /* bRequest, "Firmware" */
788 0x0000, local_transfer_buffer,
794 printk("comedi_: usbdux: control msg failed (stop)\n");
801 static int usbduxsub_upload(usbduxsub_t *usbduxsub,
802 uint8_t *local_transfer_buffer,
803 unsigned int startAddr, unsigned int len)
807 if (usbduxsub->probed) {
808 #ifdef CONFIG_COMEDI_DEBUG
809 printk("comedi%d: usbdux: uploading %d bytes",
810 usbduxsub->comedidev->minor, len);
811 printk(" to addr %d, first byte=%d.\n",
812 startAddr, local_transfer_buffer[0]);
814 errcode = USB_CONTROL_MSG
816 usb_sndctrlpipe(usbduxsub->usbdev, 0),
817 /* brequest, firmware */
825 /* our local safe buffer */
826 local_transfer_buffer,
831 #ifdef NOISY_DUX_DEBUGBUG
832 printk("comedi_: usbdux: result=%d\n", errcode);
835 printk("comedi_: usbdux: uppload failed\n");
839 /* no device on the bus for this index */
845 static int firmwareUpload(usbduxsub_t *usbduxsub, uint8_t *firmwareBinary,
850 if (!firmwareBinary) {
853 ret = usbduxsub_stop(usbduxsub);
855 printk("comedi_: usbdux: can not stop firmware\n");
858 ret = usbduxsub_upload(usbduxsub, firmwareBinary, 0, sizeFirmware);
860 printk("comedi_: usbdux: firmware upload failed\n");
863 ret = usbduxsub_start(usbduxsub);
865 printk("comedi_: usbdux: can not start firmware\n");
871 static int usbduxsub_submit_InURBs(usbduxsub_t *usbduxsub)
878 /* Submit all URBs and start the transfer on the bus */
879 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
880 /* in case of a resubmission after an unlink... */
881 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
882 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
883 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
884 usbduxsub->urbIn[i]->status = 0;
885 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
886 #ifdef NOISY_DUX_DEBUGBUG
887 printk("comedi%d: usbdux: submitting in-urb[%d]: %p,%p intv=%d\n", usbduxsub->comedidev->minor, i, (usbduxsub->urbIn[i]->context), (usbduxsub->urbIn[i]->dev), (usbduxsub->urbIn[i]->interval));
889 errFlag = USB_SUBMIT_URB(usbduxsub->urbIn[i]);
891 printk("comedi_: usbdux: ai: ");
892 printk("USB_SUBMIT_URB(%d)", i);
893 printk(" error %d\n", errFlag);
900 static int usbduxsub_submit_OutURBs(usbduxsub_t *usbduxsub)
907 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
908 #ifdef NOISY_DUX_DEBUGBUG
909 printk("comedi_: usbdux: submitting out-urb[%d]\n", i);
911 /* in case of a resubmission after an unlink... */
912 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
913 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
914 usbduxsub->urbOut[i]->status = 0;
915 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
916 errFlag = USB_SUBMIT_URB(usbduxsub->urbOut[i]);
918 printk("comedi_: usbdux: ao: ");
919 printk("USB_SUBMIT_URB(%d)", i);
920 printk(" error %d\n", errFlag);
927 static int usbdux_ai_cmdtest(comedi_device *dev, comedi_subdevice *s,
931 unsigned int tmpTimer;
932 usbduxsub_t *this_usbduxsub = dev->private;
933 if (!(this_usbduxsub->probed)) {
936 #ifdef NOISY_DUX_DEBUGBUG
937 printk("comedi%d: usbdux_ai_cmdtest\n", dev->minor);
939 /* make sure triggers are valid */
940 /* Only immediate triggers are allowed */
941 tmp = cmd->start_src;
942 cmd->start_src &= TRIG_NOW | TRIG_INT;
943 if (!cmd->start_src || tmp != cmd->start_src)
946 /* trigger should happen timed */
947 tmp = cmd->scan_begin_src;
948 /* start a new _scan_ with a timer */
949 cmd->scan_begin_src &= TRIG_TIMER;
950 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
953 /* scanning is continous */
954 tmp = cmd->convert_src;
955 cmd->convert_src &= TRIG_NOW;
956 if (!cmd->convert_src || tmp != cmd->convert_src)
959 /* issue a trigger when scan is finished and start a new scan */
960 tmp = cmd->scan_end_src;
961 cmd->scan_end_src &= TRIG_COUNT;
962 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
965 /* trigger at the end of count events or not, stop condition or not */
967 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
968 if (!cmd->stop_src || tmp != cmd->stop_src)
974 /* step 2: make sure trigger sources are unique and mutually compatible */
975 /* note that mutual compatiblity is not an issue here */
976 if (cmd->scan_begin_src != TRIG_FOLLOW &&
977 cmd->scan_begin_src != TRIG_EXT &&
978 cmd->scan_begin_src != TRIG_TIMER)
980 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
986 /* step 3: make sure arguments are trivially compatible */
988 if (cmd->start_arg != 0) {
993 if (cmd->scan_begin_src == TRIG_FOLLOW) {
994 /* internal trigger */
995 if (cmd->scan_begin_arg != 0) {
996 cmd->scan_begin_arg = 0;
1001 if (cmd->scan_begin_src == TRIG_TIMER) {
1002 if (this_usbduxsub->high_speed) {
1004 * In high speed mode microframes are possible.
1005 * However, during one microframe we can roughly
1006 * sample one channel. Thus, the more channels
1007 * are in the channel list the more time we need.
1010 /* find a power of 2 for the number of channels */
1011 while (i < (cmd->chanlist_len)) {
1014 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
1015 cmd->scan_begin_arg = 1000000 / 8 * i;
1018 /* now calc the real sampling rate with all the
1019 * rounding errors */
1021 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
1023 if (cmd->scan_begin_arg != tmpTimer) {
1024 cmd->scan_begin_arg = tmpTimer;
1029 /* 1kHz scans every USB frame */
1030 if (cmd->scan_begin_arg < 1000000) {
1031 cmd->scan_begin_arg = 1000000;
1034 /* calc the real sampling rate with the rounding errors */
1036 ((unsigned int)(cmd->scan_begin_arg /
1037 1000000)) * 1000000;
1038 if (cmd->scan_begin_arg != tmpTimer) {
1039 cmd->scan_begin_arg = tmpTimer;
1044 /* the same argument */
1045 if (cmd->scan_end_arg != cmd->chanlist_len) {
1046 cmd->scan_end_arg = cmd->chanlist_len;
1050 if (cmd->stop_src == TRIG_COUNT) {
1051 /* any count is allowed */
1054 if (cmd->stop_arg != 0) {
1067 * creates the ADC command for the MAX1271
1068 * range is the range value from comedi
1070 static int8_t create_adc_command(unsigned int chan, int range)
1072 int8_t p = (range <= 1);
1073 int8_t r = ((range % 2) == 0);
1074 return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
1077 /* bulk transfers to usbdux */
1079 #define SENDADCOMMANDS 0
1080 #define SENDDACOMMANDS 1
1081 #define SENDDIOCONFIGCOMMAND 2
1082 #define SENDDIOBITSCOMMAND 3
1083 #define SENDSINGLEAD 4
1084 #define READCOUNTERCOMMAND 5
1085 #define WRITECOUNTERCOMMAND 6
1087 #define SENDPWMOFF 8
1089 static int send_dux_commands(usbduxsub_t *this_usbduxsub, int cmd_type)
1093 this_usbduxsub->dux_commands[0] = cmd_type;
1094 #ifdef NOISY_DUX_DEBUGBUG
1095 printk("comedi%d: usbdux: dux_commands: ",
1096 this_usbduxsub->comedidev->minor);
1097 for (result = 0; result < SIZEOFDUXBUFFER; result++) {
1098 printk(" %02x", this_usbduxsub->dux_commands[result]);
1102 result = USB_BULK_MSG(this_usbduxsub->usbdev,
1103 usb_sndbulkpipe(this_usbduxsub->usbdev,
1105 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER, &nsent, 10 * HZ);
1107 printk("comedi%d: could not transmit dux_command to the usb-device, err=%d\n", this_usbduxsub->comedidev->minor, result);
1112 static int receive_dux_commands(usbduxsub_t *this_usbduxsub, int command)
1114 int result = (-EFAULT);
1118 for (i = 0; i < RETRIES; i++) {
1119 result = USB_BULK_MSG(this_usbduxsub->usbdev,
1120 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1122 this_usbduxsub->insnBuffer, SIZEINSNBUF, &nrec, 1 * HZ);
1124 printk("comedi%d: insn: USB error %d while receiving DUX command\n", this_usbduxsub->comedidev->minor, result);
1127 if (le16_to_cpu(this_usbduxsub->insnBuffer[0]) == command) {
1131 /* this is only reached if the data has been requested a couple of times */
1132 printk("comedi%d: insn: wrong data returned from firmware: want cmd %d, got cmd %d.\n", this_usbduxsub->comedidev->minor, command, le16_to_cpu(this_usbduxsub->insnBuffer[0]));
1136 static int usbdux_ai_inttrig(comedi_device *dev, comedi_subdevice *s,
1137 unsigned int trignum)
1140 usbduxsub_t *this_usbduxsub = dev->private;
1141 if (!this_usbduxsub) {
1144 down(&this_usbduxsub->sem);
1145 if (!(this_usbduxsub->probed)) {
1146 up(&this_usbduxsub->sem);
1149 #ifdef NOISY_DUX_DEBUGBUG
1150 printk("comedi%d: usbdux_ai_inttrig\n", dev->minor);
1154 printk("comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1156 up(&this_usbduxsub->sem);
1159 if (!(this_usbduxsub->ai_cmd_running)) {
1160 this_usbduxsub->ai_cmd_running = 1;
1161 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1163 printk("comedi%d: usbdux_ai_inttrig: urbSubmit: err=%d\n", dev->minor, ret);
1164 this_usbduxsub->ai_cmd_running = 0;
1165 up(&this_usbduxsub->sem);
1168 s->async->inttrig = NULL;
1170 printk("comedi%d: ai_inttrig but acqu is already running\n",
1173 up(&this_usbduxsub->sem);
1177 static int usbdux_ai_cmd(comedi_device *dev, comedi_subdevice *s)
1179 comedi_cmd *cmd = &s->async->cmd;
1180 unsigned int chan, range;
1182 usbduxsub_t *this_usbduxsub = dev->private;
1185 #ifdef NOISY_DUX_DEBUGBUG
1186 printk("comedi%d: usbdux_ai_cmd\n", dev->minor);
1188 if (!this_usbduxsub)
1191 /* block other CPUs from starting an ai_cmd */
1192 down(&this_usbduxsub->sem);
1194 if (!(this_usbduxsub->probed)) {
1195 up(&this_usbduxsub->sem);
1198 if (this_usbduxsub->ai_cmd_running) {
1199 printk("comedi%d: ai_cmd not possible. Another ai_cmd is running.\n", dev->minor);
1200 up(&this_usbduxsub->sem);
1203 /* set current channel of the running aquisition to zero */
1204 s->async->cur_chan = 0;
1206 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1207 for (i = 0; i < cmd->chanlist_len; ++i) {
1208 chan = CR_CHAN(cmd->chanlist[i]);
1209 range = CR_RANGE(cmd->chanlist[i]);
1210 if (i >= NUMCHANNELS) {
1211 printk("comedi%d: channel list too long\n", dev->minor);
1214 this_usbduxsub->dux_commands[i + 2] =
1215 create_adc_command(chan, range);
1218 #ifdef NOISY_DUX_DEBUGBUG
1219 printk("comedi %d: sending commands to the usb device: ", dev->minor);
1220 printk("size=%u\n", NUMCHANNELS);
1222 if ((result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS)) < 0) {
1223 up(&this_usbduxsub->sem);
1227 if (this_usbduxsub->high_speed) {
1229 * every channel gets a time window of 125us. Thus, if we
1230 * sample all 8 channels we need 1ms. If we sample only one
1231 * channel we need only 125us
1233 this_usbduxsub->ai_interval = 1;
1234 /* find a power of 2 for the interval */
1235 while ((this_usbduxsub->ai_interval) < (cmd->chanlist_len)) {
1236 this_usbduxsub->ai_interval =
1237 (this_usbduxsub->ai_interval) * 2;
1239 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1240 (this_usbduxsub->ai_interval));
1242 /* interval always 1ms */
1243 this_usbduxsub->ai_interval = 1;
1244 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1246 if (this_usbduxsub->ai_timer < 1) {
1247 printk("comedi%d: usbdux: ai_cmd: timer=%d, scan_begin_arg=%d. Not properly tested by cmdtest?\n", dev->minor, this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1248 up(&this_usbduxsub->sem);
1251 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1253 if (cmd->stop_src == TRIG_COUNT) {
1254 /* data arrives as one packet */
1255 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1256 this_usbduxsub->ai_continous = 0;
1258 /* continous aquisition */
1259 this_usbduxsub->ai_continous = 1;
1260 this_usbduxsub->ai_sample_count = 0;
1263 if (cmd->start_src == TRIG_NOW) {
1264 /* enable this acquisition operation */
1265 this_usbduxsub->ai_cmd_running = 1;
1266 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1268 this_usbduxsub->ai_cmd_running = 0;
1269 /* fixme: unlink here?? */
1270 up(&this_usbduxsub->sem);
1273 s->async->inttrig = NULL;
1276 /* don't enable the acquision operation */
1277 /* wait for an internal signal */
1278 s->async->inttrig = usbdux_ai_inttrig;
1280 up(&this_usbduxsub->sem);
1284 /* Mode 0 is used to get a single conversion on demand */
1285 static int usbdux_ai_insn_read(comedi_device *dev, comedi_subdevice *s,
1286 comedi_insn *insn, lsampl_t *data)
1292 usbduxsub_t *this_usbduxsub = dev->private;
1294 if (!this_usbduxsub) {
1295 printk("comedi%d: ai_insn_read: no usb dev.\n", dev->minor);
1298 #ifdef NOISY_DUX_DEBUGBUG
1299 printk("comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1300 dev->minor, insn->n, insn->subdev);
1302 down(&this_usbduxsub->sem);
1303 if (!(this_usbduxsub->probed)) {
1304 up(&this_usbduxsub->sem);
1307 if (this_usbduxsub->ai_cmd_running) {
1308 printk("comedi%d: ai_insn_read not possible. Async Command is running.\n", dev->minor);
1309 up(&this_usbduxsub->sem);
1313 /* sample one channel */
1314 chan = CR_CHAN(insn->chanspec);
1315 range = CR_RANGE(insn->chanspec);
1316 /* set command for the first channel */
1317 this_usbduxsub->dux_commands[1] = create_adc_command(chan, range);
1320 if ((err = send_dux_commands(this_usbduxsub, SENDSINGLEAD)) < 0) {
1321 up(&this_usbduxsub->sem);
1325 for (i = 0; i < insn->n; i++) {
1326 if ((err = receive_dux_commands(this_usbduxsub,
1327 SENDSINGLEAD)) < 0) {
1328 up(&this_usbduxsub->sem);
1331 one = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1332 if (CR_RANGE(insn->chanspec) <= 1)
1337 up(&this_usbduxsub->sem);
1341 /************************************/
1344 static int usbdux_ao_insn_read(comedi_device *dev, comedi_subdevice *s,
1345 comedi_insn *insn, lsampl_t *data)
1348 int chan = CR_CHAN(insn->chanspec);
1349 usbduxsub_t *this_usbduxsub = dev->private;
1351 if (!this_usbduxsub)
1354 down(&this_usbduxsub->sem);
1355 if (!(this_usbduxsub->probed)) {
1356 up(&this_usbduxsub->sem);
1359 for (i = 0; i < insn->n; i++)
1360 data[i] = this_usbduxsub->outBuffer[chan];
1362 up(&this_usbduxsub->sem);
1366 static int usbdux_ao_insn_write(comedi_device *dev, comedi_subdevice *s,
1367 comedi_insn *insn, lsampl_t *data)
1370 int chan = CR_CHAN(insn->chanspec);
1371 usbduxsub_t *this_usbduxsub = dev->private;
1373 #ifdef NOISY_DUX_DEBUGBUG
1374 printk("comedi%d: ao_insn_write\n", dev->minor);
1376 if (!this_usbduxsub)
1379 down(&this_usbduxsub->sem);
1380 if (!(this_usbduxsub->probed)) {
1381 up(&this_usbduxsub->sem);
1384 if (this_usbduxsub->ao_cmd_running) {
1385 printk("comedi%d: ao_insn_write: ERROR: asynchronous ao_cmd is running\n", dev->minor);
1386 up(&this_usbduxsub->sem);
1390 for (i = 0; i < insn->n; i++) {
1391 #ifdef NOISY_DUX_DEBUGBUG
1392 printk("comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1393 dev->minor, chan, i, data[i]);
1395 /* number of channels: 1 */
1396 this_usbduxsub->dux_commands[1] = 1;
1397 /* one 16 bit value */
1398 *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
1399 cpu_to_le16(data[i]);
1400 this_usbduxsub->outBuffer[chan] = data[i];
1401 /* channel number */
1402 this_usbduxsub->dux_commands[4] = (chan << 6);
1403 if ((err = send_dux_commands(this_usbduxsub,
1404 SENDDACOMMANDS)) < 0) {
1405 up(&this_usbduxsub->sem);
1409 up(&this_usbduxsub->sem);
1414 static int usbdux_ao_inttrig(comedi_device *dev, comedi_subdevice *s,
1415 unsigned int trignum)
1418 usbduxsub_t *this_usbduxsub = dev->private;
1420 if (!this_usbduxsub)
1423 down(&this_usbduxsub->sem);
1424 if (!(this_usbduxsub->probed)) {
1425 up(&this_usbduxsub->sem);
1429 printk("comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1433 if (!(this_usbduxsub->ao_cmd_running)) {
1434 this_usbduxsub->ao_cmd_running = 1;
1435 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1437 printk("comedi%d: usbdux_ao_inttrig: submitURB: err=%d\n", dev->minor, ret);
1438 this_usbduxsub->ao_cmd_running = 0;
1439 up(&this_usbduxsub->sem);
1442 s->async->inttrig = NULL;
1444 printk("comedi%d: ao_inttrig but acqu is already running.\n",
1447 up(&this_usbduxsub->sem);
1451 static int usbdux_ao_cmdtest(comedi_device *dev, comedi_subdevice *s,
1455 usbduxsub_t *this_usbduxsub = dev->private;
1457 if (!this_usbduxsub)
1460 if (!(this_usbduxsub->probed))
1463 #ifdef NOISY_DUX_DEBUGBUG
1464 printk("comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1466 /* make sure triggers are valid */
1467 /* Only immediate triggers are allowed */
1468 tmp = cmd->start_src;
1469 cmd->start_src &= TRIG_NOW | TRIG_INT;
1470 if (!cmd->start_src || tmp != cmd->start_src)
1473 /* trigger should happen timed */
1474 tmp = cmd->scan_begin_src;
1475 /* just now we scan also in the high speed mode every frame */
1476 /* this is due to ehci driver limitations */
1477 if (0) { /* (this_usbduxsub->high_speed) */
1478 /* start immidiately a new scan */
1479 /* the sampling rate is set by the coversion rate */
1480 cmd->scan_begin_src &= TRIG_FOLLOW;
1482 /* start a new scan (output at once) with a timer */
1483 cmd->scan_begin_src &= TRIG_TIMER;
1485 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1488 /* scanning is continous */
1489 tmp = cmd->convert_src;
1490 /* we always output at 1kHz just now all channels at once */
1491 if (0) { /* (this_usbduxsub->high_speed) */
1492 /* in usb-2.0 only one conversion it tranmitted but with 8kHz/n */
1493 cmd->convert_src &= TRIG_TIMER;
1495 /* all conversion events happen simultaneously with a rate of 1kHz/n */
1496 cmd->convert_src &= TRIG_NOW;
1498 if (!cmd->convert_src || tmp != cmd->convert_src)
1501 /* issue a trigger when scan is finished and start a new scan */
1502 tmp = cmd->scan_end_src;
1503 cmd->scan_end_src &= TRIG_COUNT;
1504 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1507 /* trigger at the end of count events or not, stop condition or not */
1508 tmp = cmd->stop_src;
1509 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1510 if (!cmd->stop_src || tmp != cmd->stop_src)
1516 /* step 2: make sure trigger sources are unique and mutually compatible */
1517 /* note that mutual compatiblity is not an issue here */
1518 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1519 cmd->scan_begin_src != TRIG_EXT &&
1520 cmd->scan_begin_src != TRIG_TIMER)
1522 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1528 /* step 3: make sure arguments are trivially compatible */
1530 if (cmd->start_arg != 0) {
1535 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1536 /* internal trigger */
1537 if (cmd->scan_begin_arg != 0) {
1538 cmd->scan_begin_arg = 0;
1543 if (cmd->scan_begin_src == TRIG_TIMER) {
1545 if (cmd->scan_begin_arg < 1000000) {
1546 cmd->scan_begin_arg = 1000000;
1550 /* not used now, is for later use */
1551 if (cmd->convert_src == TRIG_TIMER) {
1552 if (cmd->convert_arg < 125000) {
1553 cmd->convert_arg = 125000;
1558 /* the same argument */
1559 if (cmd->scan_end_arg != cmd->chanlist_len) {
1560 cmd->scan_end_arg = cmd->chanlist_len;
1564 if (cmd->stop_src == TRIG_COUNT) {
1565 /* any count is allowed */
1568 if (cmd->stop_arg != 0) {
1574 #ifdef NOISY_DUX_DEBUGBUG
1575 printk("comedi%d: err=%d, scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src, cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1584 static int usbdux_ao_cmd(comedi_device *dev, comedi_subdevice *s)
1586 comedi_cmd *cmd = &s->async->cmd;
1587 unsigned int chan, gain;
1589 usbduxsub_t *this_usbduxsub = dev->private;
1591 if (!this_usbduxsub)
1594 down(&this_usbduxsub->sem);
1595 if (!(this_usbduxsub->probed)) {
1596 up(&this_usbduxsub->sem);
1599 #ifdef NOISY_DUX_DEBUGBUG
1600 printk("comedi%d: usbdux_ao_cmd\n", dev->minor);
1603 /* set current channel of the running aquisition to zero */
1604 s->async->cur_chan = 0;
1605 for (i = 0; i < cmd->chanlist_len; ++i) {
1606 chan = CR_CHAN(cmd->chanlist[i]);
1607 gain = CR_RANGE(cmd->chanlist[i]);
1608 if (i >= NUMOUTCHANNELS) {
1609 printk("comedi%d: usbdux_ao_cmd: channel list too long\n", dev->minor);
1612 this_usbduxsub->dac_commands[i] = (chan << 6);
1613 #ifdef NOISY_DUX_DEBUGBUG
1614 printk("comedi%d: dac command for ch %d is %x\n",
1615 dev->minor, i, this_usbduxsub->dac_commands[i]);
1619 /* we count in steps of 1ms (125us) */
1620 /* 125us mode not used yet */
1621 if (0) { /* (this_usbduxsub->high_speed) */
1623 /* timing of the conversion itself: every 125 us */
1624 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1627 /* timing of the scan: we get all channels at once */
1628 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1629 #ifdef NOISY_DUX_DEBUGBUG
1630 printk("comedi%d: usbdux: scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, convert_arg=%d\n", dev->minor, cmd->scan_begin_src, cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1631 printk("comedi%d: usbdux: ao_timer=%d (ms)\n",
1632 dev->minor, this_usbduxsub->ao_timer);
1634 if (this_usbduxsub->ao_timer < 1) {
1635 printk("comedi%d: usbdux: ao_timer=%d, scan_begin_arg=%d. Not properly tested by cmdtest?\n", dev->minor, this_usbduxsub->ao_timer, cmd->scan_begin_arg);
1636 up(&this_usbduxsub->sem);
1640 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1642 if (cmd->stop_src == TRIG_COUNT) {
1645 /* high speed also scans everything at once */
1646 if (0) { /* (this_usbduxsub->high_speed) */
1647 this_usbduxsub->ao_sample_count =
1648 (cmd->stop_arg) * (cmd->scan_end_arg);
1650 /* there's no scan as the scan has been */
1651 /* perf inside the FX2 */
1652 /* data arrives as one packet */
1653 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1655 this_usbduxsub->ao_continous = 0;
1657 /* continous aquisition */
1658 this_usbduxsub->ao_continous = 1;
1659 this_usbduxsub->ao_sample_count = 0;
1662 if (cmd->start_src == TRIG_NOW) {
1663 /* enable this acquisition operation */
1664 this_usbduxsub->ao_cmd_running = 1;
1665 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1667 this_usbduxsub->ao_cmd_running = 0;
1668 /* fixme: unlink here?? */
1669 up(&this_usbduxsub->sem);
1672 s->async->inttrig = NULL;
1675 /* submit the urbs later */
1676 /* wait for an internal signal */
1677 s->async->inttrig = usbdux_ao_inttrig;
1680 up(&this_usbduxsub->sem);
1684 static int usbdux_dio_insn_config(comedi_device *dev, comedi_subdevice *s,
1685 comedi_insn *insn, lsampl_t *data)
1687 int chan = CR_CHAN(insn->chanspec);
1689 /* The input or output configuration of each digital line is
1690 * configured by a special insn_config instruction. chanspec
1691 * contains the channel to be changed, and data[0] contains the
1692 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1695 case INSN_CONFIG_DIO_OUTPUT:
1696 s->io_bits |= 1 << chan; /* 1 means Out */
1698 case INSN_CONFIG_DIO_INPUT:
1699 s->io_bits &= ~(1 << chan);
1701 case INSN_CONFIG_DIO_QUERY:
1704 io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1710 /* we don't tell the firmware here as it would take 8 frames */
1711 /* to submit the information. We do it in the insn_bits. */
1715 static int usbdux_dio_insn_bits(comedi_device *dev, comedi_subdevice *s,
1716 comedi_insn *insn, lsampl_t *data)
1719 usbduxsub_t *this_usbduxsub = dev->private;
1722 if (!this_usbduxsub)
1729 down(&this_usbduxsub->sem);
1731 if (!(this_usbduxsub->probed)) {
1732 up(&this_usbduxsub->sem);
1736 /* The insn data is a mask in data[0] and the new data
1737 * in data[1], each channel cooresponding to a bit. */
1738 s->state &= ~data[0];
1739 s->state |= data[0] & data[1];
1740 this_usbduxsub->dux_commands[1] = s->io_bits;
1741 this_usbduxsub->dux_commands[2] = s->state;
1743 /* This command also tells the firmware to return */
1744 /* the digital input lines */
1745 if ((err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND)) < 0) {
1746 up(&this_usbduxsub->sem);
1749 if ((err = receive_dux_commands(this_usbduxsub,
1750 SENDDIOBITSCOMMAND)) < 0) {
1751 up(&this_usbduxsub->sem);
1755 data[1] = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1756 up(&this_usbduxsub->sem);
1760 /* reads the 4 counters, only two are used just now */
1761 static int usbdux_counter_read(comedi_device *dev, comedi_subdevice *s,
1762 comedi_insn *insn, lsampl_t *data)
1764 usbduxsub_t *this_usbduxsub = dev->private;
1765 int chan = insn->chanspec;
1768 if (!this_usbduxsub)
1771 down(&this_usbduxsub->sem);
1773 if (!(this_usbduxsub->probed)) {
1774 up(&this_usbduxsub->sem);
1778 if ((err = send_dux_commands(this_usbduxsub, READCOUNTERCOMMAND)) < 0) {
1779 up(&this_usbduxsub->sem);
1783 if ((err = receive_dux_commands(this_usbduxsub,
1784 READCOUNTERCOMMAND)) < 0) {
1785 up(&this_usbduxsub->sem);
1789 data[0] = le16_to_cpu(this_usbduxsub->insnBuffer[chan + 1]);
1790 up(&this_usbduxsub->sem);
1794 static int usbdux_counter_write(comedi_device *dev, comedi_subdevice *s,
1795 comedi_insn *insn, lsampl_t *data)
1797 usbduxsub_t *this_usbduxsub = dev->private;
1800 if (!this_usbduxsub)
1803 down(&this_usbduxsub->sem);
1805 if (!(this_usbduxsub->probed)) {
1806 up(&this_usbduxsub->sem);
1810 this_usbduxsub->dux_commands[1] = insn->chanspec;
1811 *((int16_t *) (this_usbduxsub->dux_commands + 2)) = cpu_to_le16(*data);
1813 if ((err = send_dux_commands(this_usbduxsub, WRITECOUNTERCOMMAND)) < 0) {
1814 up(&this_usbduxsub->sem);
1818 up(&this_usbduxsub->sem);
1823 static int usbdux_counter_config(comedi_device *dev, comedi_subdevice *s,
1824 comedi_insn *insn, lsampl_t *data)
1826 /* nothing to do so far */
1830 /***********************************/
1833 static int usbduxsub_unlink_PwmURBs(usbduxsub_t *usbduxsub_tmp)
1837 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1838 if (usbduxsub_tmp->urbPwm)
1839 usb_kill_urb(usbduxsub_tmp->urbPwm);
1840 #ifdef NOISY_DUX_DEBUGBUG
1841 printk("comedi: usbdux: unlinked PwmURB: res=%d\n", err);
1847 /* This cancels a running acquisition operation
1850 static int usbdux_pwm_stop(usbduxsub_t *this_usbduxsub, int do_unlink)
1854 if (!this_usbduxsub) {
1855 #ifdef NOISY_DUX_DEBUGBUG
1856 printk("comedi?: usbdux_pwm_stop: this_usbduxsub=NULL!\n");
1860 #ifdef NOISY_DUX_DEBUGBUG
1861 printk("comedi: usbdux_pwm_cancel\n");
1864 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1867 this_usbduxsub->pwm_cmd_running = 0;
1872 /* force unlink - is called by comedi */
1873 static int usbdux_pwm_cancel(comedi_device *dev, comedi_subdevice *s)
1875 usbduxsub_t *this_usbduxsub = dev->private;
1878 /* unlink only if it is really running */
1879 res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1881 #ifdef NOISY_DUX_DEBUGBUG
1882 printk("comedi %d: sending pwm off command to the usb device.\n",
1885 if ((res = send_dux_commands(this_usbduxsub, SENDPWMOFF)) < 0)
1891 static void usbduxsub_pwm_irq(struct urb *urb)
1894 usbduxsub_t *this_usbduxsub;
1895 comedi_device *this_comedidev;
1896 comedi_subdevice *s;
1898 /* printk("PWM: IRQ\n"); */
1901 printk("comedi_: usbdux_: pwm urb handler called with NULL ptr.\n");
1904 /* the context variable points to the subdevice */
1905 this_comedidev = urb->context;
1906 if (!this_comedidev) {
1907 printk("comedi_: usbdux_: pwm urb int-context is a NULL pointer.\n");
1910 /* the private structure of the subdevice is usbduxsub_t */
1911 this_usbduxsub = this_comedidev->private;
1912 if (!this_usbduxsub) {
1913 printk("comedi_: usbdux_: private data structure of pwm subdev is NULL p.\n");
1917 s = this_comedidev->subdevices + SUBDEV_DA;
1919 switch (urb->status) {
1929 * after an unlink command, unplug, ... etc
1930 * no unlink needed here. Already shutting down.
1932 if (this_usbduxsub->pwm_cmd_running)
1933 usbdux_pwm_stop(this_usbduxsub, 0);
1939 if (this_usbduxsub->pwm_cmd_running) {
1940 printk("comedi_: usbdux_: Non-zero urb status received in pwm intr context: %d\n", urb->status);
1941 usbdux_pwm_stop(this_usbduxsub, 0);
1946 /* are we actually running? */
1947 if (!(this_usbduxsub->pwm_cmd_running))
1950 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1951 urb->dev = this_usbduxsub->usbdev;
1953 if (this_usbduxsub->pwm_cmd_running) {
1954 if ((ret = USB_SUBMIT_URB(urb)) < 0) {
1955 printk("comedi_: usbdux_: pwm urb resubm failed in int-cont.");
1956 printk("ret=%d", ret);
1957 if (ret == EL2NSYNC)
1958 printk("--> buggy USB host controller or bug in IRQ handling!\n");
1962 /* don't do an unlink here */
1963 usbdux_pwm_stop(this_usbduxsub, 0);
1968 static int usbduxsub_submit_PwmURBs(usbduxsub_t *usbduxsub)
1975 #ifdef NOISY_DUX_DEBUGBUG
1976 printk("comedi_: usbdux: submitting pwm-urb\n");
1978 /* in case of a resubmission after an unlink... */
1980 usb_fill_bulk_urb(usbduxsub->urbPwm,
1982 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
1983 usbduxsub->urbPwm->transfer_buffer,
1984 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq, usbduxsub->comedidev);
1986 errFlag = USB_SUBMIT_URB(usbduxsub->urbPwm);
1988 printk("comedi_: usbdux: pwm: ");
1989 printk("USB_SUBMIT_URB");
1990 printk(" error %d\n", errFlag);
1996 static int usbdux_pwm_period(comedi_device *dev, comedi_subdevice *s,
1999 usbduxsub_t *this_usbduxsub = dev->private;
2002 if (period < MIN_PWM_PERIOD) {
2003 printk("comedi%d: illegal period setting for pwm.\n", dev->minor);
2006 fx2delay = period / ((int)(6*512*(1.0/0.033))) - 6;
2007 if (fx2delay > 255) {
2008 printk("comedi%d: period %d for pwm is too low.\n",
2009 dev->minor, period);
2013 this_usbduxsub->pwmDelay = fx2delay;
2014 this_usbduxsub->pwmPeriod = period;
2015 #ifdef NOISY_DUX_DEBUGBUG
2016 printk("usbdux_pwm_period: frequ=%d, period=%d\n", period, fx2delay);
2022 /* is called from insn so there's no need to do all the sanity checks */
2023 static int usbdux_pwm_start(comedi_device * dev, comedi_subdevice * s)
2026 usbduxsub_t *this_usbduxsub = dev->private;
2028 #ifdef NOISY_DUX_DEBUGBUG
2029 printk("comedi%d: usbdux_pwm_start\n", dev->minor);
2031 if (this_usbduxsub->pwm_cmd_running) {
2032 /* already running */
2036 this_usbduxsub->dux_commands[1] = ((int8_t) this_usbduxsub->pwmDelay);
2037 if ((ret = send_dux_commands(this_usbduxsub, SENDPWMON)) < 0) {
2040 /* initalise the buffer */
2041 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++) {
2042 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
2045 this_usbduxsub->pwm_cmd_running = 1;
2046 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
2048 this_usbduxsub->pwm_cmd_running = 0;
2055 /* generates the bit pattern for PWM with the optional sign bit */
2056 static int usbdux_pwm_pattern(comedi_device * dev, comedi_subdevice * s,
2057 int channel, lsampl_t value, lsampl_t sign)
2059 usbduxsub_t *this_usbduxsub = dev->private;
2062 char pwm_mask,sgn_mask,c;
2064 if (!this_usbduxsub) {
2067 /* this is the DIO bit which carries the PWM data */
2068 pwm_mask = (1 << channel);
2069 /* this is the DIO bit which carries the optional direction bit */
2070 sgn_mask = (16 << channel);
2071 /* this is the buffer which will be filled with the with bit */
2072 /* pattern for one period */
2073 szbuf = this_usbduxsub->sizePwmBuf;
2074 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2075 for (i = 0; i < szbuf; i++) {
2078 c = c & (~pwm_mask);
2079 /* set the bit as long as the index is lower than the value */
2082 /* set the optional sign bit for a relay */
2084 /* positive value */
2085 c = c & (~sgn_mask);
2087 /* negative value */
2095 static int usbdux_pwm_write(comedi_device * dev, comedi_subdevice * s,
2096 comedi_insn * insn, lsampl_t * data)
2098 usbduxsub_t *this_usbduxsub = dev->private;
2100 if (!this_usbduxsub) {
2105 /* doesn't make sense to have more than one value here */
2106 /* because it would just overwrite the PWM buffer a couple of times */
2110 /* the sign is set via a special INSN only, this gives us 8 bits for */
2111 /* normal operation */
2112 /* relay sign 0 by default */
2113 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec),
2117 static int usbdux_pwm_read(comedi_device *x1, comedi_subdevice *x2,
2118 comedi_insn *x3, lsampl_t *x4)
2124 /* switches on/off PWM */
2125 static int usbdux_pwm_config(comedi_device *dev, comedi_subdevice *s,
2126 comedi_insn *insn, lsampl_t *data)
2128 usbduxsub_t *this_usbduxsub = dev->private;
2130 case INSN_CONFIG_ARM:
2131 #ifdef NOISY_DUX_DEBUGBUG
2133 printk("comedi%d: pwm_insn_config: pwm on\n",
2137 * if not zero the PWM is limited to a certain time which is
2138 * not supported here
2142 return usbdux_pwm_start(dev, s);
2143 case INSN_CONFIG_DISARM:
2144 #ifdef NOISY_DUX_DEBUGBUG
2145 printk("comedi%d: pwm_insn_config: pwm off\n",
2148 return usbdux_pwm_cancel(dev, s);
2149 case INSN_CONFIG_GET_PWM_STATUS:
2151 * to check if the USB transmission has failed or in case PWM
2152 * was limited to n cycles to check if it has terminated
2154 data[1] = this_usbduxsub->pwm_cmd_running;
2156 case INSN_CONFIG_PWM_SET_PERIOD:
2157 #ifdef NOISY_DUX_DEBUGBUG
2158 printk("comedi%d: pwm_insn_config: setting period\n",
2161 return usbdux_pwm_period(dev, s, data[1]);
2162 case INSN_CONFIG_PWM_GET_PERIOD:
2163 data[1] = this_usbduxsub->pwmPeriod;
2165 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2166 /* value in the first byte and the sign in the second for a
2168 return usbdux_pwm_pattern(dev, s,
2169 /* the channel number */
2170 CR_CHAN(insn->chanspec),
2171 /* actual PWM data */
2175 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2176 /* values are not kept in this driver, nothing to return here */
2183 /*****************************************************************/
2185 static void tidy_up(usbduxsub_t *usbduxsub_tmp)
2189 #ifdef CONFIG_COMEDI_DEBUG
2190 printk("comedi_: usbdux: tiding up\n");
2195 /* shows the usb subsystem that the driver is down */
2196 if (usbduxsub_tmp->interface)
2197 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2199 usbduxsub_tmp->probed = 0;
2201 if (usbduxsub_tmp->urbIn) {
2202 if (usbduxsub_tmp->ai_cmd_running) {
2203 usbduxsub_tmp->ai_cmd_running = 0;
2204 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2206 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2207 if (usbduxsub_tmp->urbIn[i]->transfer_buffer) {
2208 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2209 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2211 if (usbduxsub_tmp->urbIn[i]) {
2212 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2213 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2214 usbduxsub_tmp->urbIn[i] = NULL;
2217 kfree(usbduxsub_tmp->urbIn);
2218 usbduxsub_tmp->urbIn = NULL;
2220 if (usbduxsub_tmp->urbOut) {
2221 if (usbduxsub_tmp->ao_cmd_running) {
2222 usbduxsub_tmp->ao_cmd_running = 0;
2223 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2225 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2226 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2227 kfree(usbduxsub_tmp->urbOut[i]->
2229 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2232 if (usbduxsub_tmp->urbOut[i]) {
2233 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2234 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2235 usbduxsub_tmp->urbOut[i] = NULL;
2238 kfree(usbduxsub_tmp->urbOut);
2239 usbduxsub_tmp->urbOut = NULL;
2241 if (usbduxsub_tmp->urbPwm) {
2242 if (usbduxsub_tmp->pwm_cmd_running) {
2243 usbduxsub_tmp->pwm_cmd_running = 0;
2244 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2246 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2247 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2248 usb_kill_urb(usbduxsub_tmp->urbPwm);
2249 usb_free_urb(usbduxsub_tmp->urbPwm);
2250 usbduxsub_tmp->urbPwm = NULL;
2252 kfree(usbduxsub_tmp->inBuffer);
2253 usbduxsub_tmp->inBuffer = NULL;
2254 kfree(usbduxsub_tmp->insnBuffer);
2255 usbduxsub_tmp->insnBuffer = NULL;
2256 kfree(usbduxsub_tmp->inBuffer);
2257 usbduxsub_tmp->inBuffer = NULL;
2258 kfree(usbduxsub_tmp->dac_commands);
2259 usbduxsub_tmp->dac_commands = NULL;
2260 kfree(usbduxsub_tmp->dux_commands);
2261 usbduxsub_tmp->dux_commands = NULL;
2262 usbduxsub_tmp->ai_cmd_running = 0;
2263 usbduxsub_tmp->ao_cmd_running = 0;
2264 usbduxsub_tmp->pwm_cmd_running = 0;
2267 static unsigned hex2unsigned(char *h)
2272 hi = h[0] - 'A' + 0x0a;
2277 lo = h[1] - 'A' + 0x0a;
2281 return hi * 0x10 + lo;
2285 #define FIRMWARE_MAX_LEN 0x2000
2287 /* taken from David Brownell's fxload and adjusted for this driver */
2288 static int read_firmware(usbduxsub_t *usbduxsub, void *firmwarePtr, long size)
2291 unsigned char *fp = (char *)firmwarePtr;
2292 unsigned char *firmwareBinary = NULL;
2296 firmwareBinary = kzalloc(FIRMWARE_MAX_LEN, GFP_KERNEL);
2297 if (!firmwareBinary) {
2298 printk("comedi_: usbdux: mem alloc for firmware failed\n");
2310 while ((i < size) && (fp[i] != 13) && (fp[i] != 10)) {
2314 if (j >= sizeof(buf)) {
2315 printk("comedi_: usbdux: bogus firmware file!\n");
2319 /* get rid of LF/CR/... */
2320 while ((i < size) && ((fp[i] == 13) || (fp[i] == 10)
2326 /*printk("comedi_: buf=%s\n",buf); */
2328 /* EXTENSION: "# comment-till-end-of-line", for copyrights etc */
2332 if (buf[0] != ':') {
2333 printk("comedi_: usbdux: upload: not an ihex record: %s", buf);
2337 /* Read the length field (up to 16 bytes) */
2338 len = hex2unsigned(buf + 1);
2340 /* Read the target offset */
2341 off = (hex2unsigned(buf + 3) * 0x0100) + hex2unsigned(buf + 5);
2343 if ((off + len) > maxAddr)
2344 maxAddr = off + len;
2347 if (maxAddr >= FIRMWARE_MAX_LEN) {
2348 printk("comedi_: usbdux: firmware upload goes beyond FX2 RAM boundaries.");
2351 /*printk("comedi_: usbdux: off=%x, len=%x:",off,len); */
2353 /* Read the record type */
2354 type = hex2unsigned(buf + 7);
2356 /* If this is an EOF record, then make it so. */
2362 printk("comedi_: usbdux: unsupported record type: %u\n",
2367 for (idx = 0, cp = buf + 9; idx < len; idx += 1, cp += 2) {
2368 firmwareBinary[idx + off] = hex2unsigned(cp);
2369 /*printk("%02x ",firmwareBinary[idx+off]); */
2374 printk("comedi_: usbdux: unexpected end of hex file\n");
2379 res = firmwareUpload(usbduxsub, firmwareBinary, maxAddr + 1);
2380 kfree(firmwareBinary);
2384 /* allocate memory for the urbs and initialise them */
2385 static int usbduxsub_probe(struct usb_interface *uinterf,
2386 const struct usb_device_id *id)
2388 struct usb_device *udev = interface_to_usbdev(uinterf);
2392 #ifdef CONFIG_COMEDI_DEBUG
2393 printk("comedi_: usbdux_: finding a free structure for the usb-device\n");
2395 down(&start_stop_sem);
2396 /* look for a free place in the usbdux array */
2398 for (i = 0; i < NUMUSBDUX; i++) {
2399 if (!(usbduxsub[i].probed)) {
2407 printk("Too many usbdux-devices connected.\n");
2408 up(&start_stop_sem);
2409 return PROBE_ERR_RETURN(-EMFILE);
2411 #ifdef CONFIG_COMEDI_DEBUG
2412 printk("comedi_: usbdux: usbduxsub[%d] is ready to connect to comedi.\n", index);
2415 init_MUTEX(&(usbduxsub[index].sem));
2416 /* save a pointer to the usb device */
2417 usbduxsub[index].usbdev = udev;
2419 /* 2.6: save the interface itself */
2420 usbduxsub[index].interface = uinterf;
2421 /* get the interface number from the interface */
2422 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2423 /* hand the private data over to the usb subsystem */
2424 /* will be needed for disconnect */
2425 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2427 #ifdef CONFIG_COMEDI_DEBUG
2428 printk("comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2430 /* test if it is high speed (USB 2.0) */
2431 usbduxsub[index].high_speed =
2432 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2434 /* create space for the commands of the DA converter */
2435 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2436 if (!usbduxsub[index].dac_commands) {
2437 printk("comedi_: usbdux: error alloc space for dac commands\n");
2438 tidy_up(&(usbduxsub[index]));
2439 up(&start_stop_sem);
2440 return PROBE_ERR_RETURN(-ENOMEM);
2442 /* create space for the commands going to the usb device */
2443 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2444 if (!usbduxsub[index].dux_commands) {
2445 printk("comedi_: usbdux: error alloc space for dac commands\n");
2446 tidy_up(&(usbduxsub[index]));
2447 up(&start_stop_sem);
2448 return PROBE_ERR_RETURN(-ENOMEM);
2450 /* create space for the in buffer and set it to zero */
2451 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2452 if (!(usbduxsub[index].inBuffer)) {
2453 printk("comedi_: usbdux: could not alloc space for inBuffer\n");
2454 tidy_up(&(usbduxsub[index]));
2455 up(&start_stop_sem);
2456 return PROBE_ERR_RETURN(-ENOMEM);
2458 /* create space of the instruction buffer */
2459 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2460 if (!(usbduxsub[index].insnBuffer)) {
2461 printk("comedi_: usbdux: could not alloc space for insnBuffer\n");
2462 tidy_up(&(usbduxsub[index]));
2463 up(&start_stop_sem);
2464 return PROBE_ERR_RETURN(-ENOMEM);
2466 /* create space for the outbuffer */
2467 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2468 if (!(usbduxsub[index].outBuffer)) {
2469 printk("comedi_: usbdux: could not alloc space for outBuffer\n");
2470 tidy_up(&(usbduxsub[index]));
2471 up(&start_stop_sem);
2472 return PROBE_ERR_RETURN(-ENOMEM);
2474 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2475 i = usb_set_interface(usbduxsub[index].usbdev,
2476 usbduxsub[index].ifnum, 3);
2478 printk("comedi_: usbdux%d: could not set alternate setting 3 in high speed.\n", index);
2479 tidy_up(&(usbduxsub[index]));
2480 up(&start_stop_sem);
2481 return PROBE_ERR_RETURN(-ENODEV);
2483 if (usbduxsub[index].high_speed)
2484 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2486 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2488 usbduxsub[index].urbIn =
2489 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2491 if (!(usbduxsub[index].urbIn)) {
2492 printk("comedi_: usbdux: Could not alloc. urbIn array\n");
2493 tidy_up(&(usbduxsub[index]));
2494 up(&start_stop_sem);
2495 return PROBE_ERR_RETURN(-ENOMEM);
2497 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2498 /* one frame: 1ms */
2499 usbduxsub[index].urbIn[i] = USB_ALLOC_URB(1);
2500 if (usbduxsub[index].urbIn[i] == NULL) {
2501 printk("comedi_: usbdux%d: Could not alloc. urb(%d)\n",
2503 tidy_up(&(usbduxsub[index]));
2504 up(&start_stop_sem);
2505 return PROBE_ERR_RETURN(-ENOMEM);
2507 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2508 /* will be filled later with a pointer to the comedi-device */
2509 /* and ONLY then the urb should be submitted */
2510 usbduxsub[index].urbIn[i]->context = NULL;
2511 usbduxsub[index].urbIn[i]->pipe =
2512 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2513 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2514 usbduxsub[index].urbIn[i]->transfer_buffer =
2515 kzalloc(SIZEINBUF, GFP_KERNEL);
2516 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2517 printk("comedi_: usbdux%d: could not alloc. transb.\n",
2519 tidy_up(&(usbduxsub[index]));
2520 up(&start_stop_sem);
2521 return PROBE_ERR_RETURN(-ENOMEM);
2523 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2524 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2525 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2526 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2527 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length = SIZEINBUF;
2531 if (usbduxsub[index].high_speed)
2532 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2534 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2536 usbduxsub[index].urbOut =
2537 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2539 if (!(usbduxsub[index].urbOut)) {
2540 printk("comedi_: usbdux: Could not alloc. urbOut array\n");
2541 tidy_up(&(usbduxsub[index]));
2542 up(&start_stop_sem);
2543 return PROBE_ERR_RETURN(-ENOMEM);
2545 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2546 /* one frame: 1ms */
2547 usbduxsub[index].urbOut[i] = USB_ALLOC_URB(1);
2548 if (usbduxsub[index].urbOut[i] == NULL) {
2549 printk("comedi_: usbdux%d: Could not alloc. urb(%d)\n",
2551 tidy_up(&(usbduxsub[index]));
2552 up(&start_stop_sem);
2553 return PROBE_ERR_RETURN(-ENOMEM);
2555 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2556 /* will be filled later with a pointer to the comedi-device */
2557 /* and ONLY then the urb should be submitted */
2558 usbduxsub[index].urbOut[i]->context = NULL;
2559 usbduxsub[index].urbOut[i]->pipe =
2560 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2561 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2562 usbduxsub[index].urbOut[i]->transfer_buffer =
2563 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2564 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2565 printk("comedi_: usbdux%d: could not alloc. transb.\n",
2567 tidy_up(&(usbduxsub[index]));
2568 up(&start_stop_sem);
2569 return PROBE_ERR_RETURN(-ENOMEM);
2571 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2572 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2573 usbduxsub[index].urbOut[i]->transfer_buffer_length = SIZEOUTBUF;
2574 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2575 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2577 if (usbduxsub[index].high_speed) {
2579 usbduxsub[index].urbOut[i]->interval = 8;
2582 usbduxsub[index].urbOut[i]->interval = 1;
2587 if (usbduxsub[index].high_speed) {
2588 usbduxsub[index].sizePwmBuf = 512; /* max bulk ep size in high speed */
2589 usbduxsub[index].urbPwm = USB_ALLOC_URB(0);
2590 if (usbduxsub[index].urbPwm == NULL) {
2591 printk("comedi_: usbdux%d: Could not alloc. pwm urb\n",
2593 tidy_up(&(usbduxsub[index]));
2594 up(&start_stop_sem);
2595 return PROBE_ERR_RETURN(-ENOMEM);
2597 usbduxsub[index].urbPwm->transfer_buffer =
2598 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2599 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2600 printk("comedi_: usbdux%d: could not alloc. transb. for pwm\n", index);
2601 tidy_up(&(usbduxsub[index]));
2602 up(&start_stop_sem);
2603 return PROBE_ERR_RETURN(-ENOMEM);
2606 usbduxsub[index].urbPwm = NULL;
2607 usbduxsub[index].sizePwmBuf = 0;
2610 usbduxsub[index].ai_cmd_running = 0;
2611 usbduxsub[index].ao_cmd_running = 0;
2612 usbduxsub[index].pwm_cmd_running = 0;
2614 /* we've reached the bottom of the function */
2615 usbduxsub[index].probed = 1;
2616 up(&start_stop_sem);
2617 printk("comedi_: usbdux%d has been successfully initialised.\n", index);
2622 static void usbduxsub_disconnect(struct usb_interface *intf)
2624 usbduxsub_t *usbduxsub_tmp = usb_get_intfdata(intf);
2625 struct usb_device *udev = interface_to_usbdev(intf);
2627 if (!usbduxsub_tmp) {
2628 printk("comedi_: usbdux: disconnect called with null pointer.\n");
2631 if (usbduxsub_tmp->usbdev != udev) {
2632 printk("comedi_: usbdux: BUG! called with wrong ptr!!!\n");
2635 down(&start_stop_sem);
2636 down(&usbduxsub_tmp->sem);
2637 tidy_up(usbduxsub_tmp);
2638 up(&usbduxsub_tmp->sem);
2639 up(&start_stop_sem);
2640 #ifdef CONFIG_COMEDI_DEBUG
2641 printk("comedi_: usbdux: disconnected from the usb\n");
2645 /* is called when comedi-config is called */
2646 static int usbdux_attach(comedi_device *dev, comedi_devconfig *it)
2651 comedi_subdevice *s = NULL;
2652 dev->private = NULL;
2654 down(&start_stop_sem);
2655 /* find a valid device which has been detected by the probe function of the usb */
2657 for (i = 0; i < NUMUSBDUX; i++) {
2658 if ((usbduxsub[i].probed) && (!usbduxsub[i].attached)) {
2665 printk("comedi%d: usbdux: error: attach failed, no usbdux devs connected to the usb bus.\n", dev->minor);
2666 up(&start_stop_sem);
2670 down(&(usbduxsub[index].sem));
2671 /* pointer back to the corresponding comedi device */
2672 usbduxsub[index].comedidev = dev;
2674 /* trying to upload the firmware into the chip */
2675 if (comedi_aux_data(it->options, 0) &&
2676 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
2677 read_firmware(usbduxsub + index,
2678 comedi_aux_data(it->options, 0),
2679 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
2682 dev->board_name = BOARDNAME;
2684 /* set number of subdevices */
2685 if (usbduxsub[index].high_speed) {
2687 dev->n_subdevices = 5;
2690 dev->n_subdevices = 4;
2693 /* allocate space for the subdevices */
2694 if ((ret = alloc_subdevices(dev, dev->n_subdevices)) < 0) {
2695 printk("comedi%d: usbdux: error alloc space for subdev\n",
2697 up(&start_stop_sem);
2701 printk("comedi%d: usbdux: usb-device %d is attached to comedi.\n",
2703 /* private structure is also simply the usb-structure */
2704 dev->private = usbduxsub + index;
2706 /* the first subdevice is the A/D converter */
2707 s = dev->subdevices + SUBDEV_AD;
2708 /* the URBs get the comedi subdevice */
2709 /* which is responsible for reading */
2710 /* this is the subdevice which reads data */
2711 dev->read_subdev = s;
2712 /* the subdevice receives as private structure the */
2716 s->type = COMEDI_SUBD_AI;
2717 /* readable and ref is to ground */
2718 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
2721 /* length of the channellist */
2722 s->len_chanlist = 8;
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 (12bit) */
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 /* 12 bit resolution */
2749 s->maxdata = 0x0fff;
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;
2765 s->range_table = (&range_digital);
2766 s->insn_bits = usbdux_dio_insn_bits;
2767 s->insn_config = usbdux_dio_insn_config;
2768 /* we don't use it */
2772 s = dev->subdevices + SUBDEV_COUNTER;
2773 s->type = COMEDI_SUBD_COUNTER;
2774 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2776 s->maxdata = 0xFFFF;
2777 s->insn_read = usbdux_counter_read;
2778 s->insn_write = usbdux_counter_write;
2779 s->insn_config = usbdux_counter_config;
2781 if (usbduxsub[index].high_speed) {
2783 s = dev->subdevices + SUBDEV_PWM;
2784 s->type = COMEDI_SUBD_PWM;
2785 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2787 /* this defines the max duty cycle resolution */
2788 s->maxdata = usbduxsub[index].sizePwmBuf;
2789 s->insn_write = usbdux_pwm_write;
2790 s->insn_read = usbdux_pwm_read;
2791 s->insn_config = usbdux_pwm_config;
2792 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2794 /* finally decide that it's attached */
2795 usbduxsub[index].attached = 1;
2797 up(&(usbduxsub[index].sem));
2799 up(&start_stop_sem);
2801 printk("comedi%d: attached to usbdux.\n", dev->minor);
2806 static int usbdux_detach(comedi_device *dev)
2808 usbduxsub_t *usbduxsub_tmp;
2810 #ifdef CONFIG_COMEDI_DEBUG
2811 printk("comedi%d: usbdux: detach usb device\n", dev->minor);
2815 printk("comedi?: usbdux: detach without dev variable...\n");
2819 usbduxsub_tmp = dev->private;
2820 if (!usbduxsub_tmp) {
2821 printk("comedi?: usbdux: detach without ptr to usbduxsub[]\n");
2825 down(&usbduxsub_tmp->sem);
2826 /* Don't allow detach to free the private structure */
2827 /* It's one entry of of usbduxsub[] */
2828 dev->private = NULL;
2829 usbduxsub_tmp->attached = 0;
2830 usbduxsub_tmp->comedidev = NULL;
2831 #ifdef CONFIG_COMEDI_DEBUG
2832 printk("comedi%d: usbdux: detach: successfully removed\n", dev->minor);
2834 up(&usbduxsub_tmp->sem);
2838 /* main driver struct */
2839 static comedi_driver driver_usbdux = {
2840 .driver_name = "usbdux",
2841 .module = THIS_MODULE,
2842 .attach = usbdux_attach,
2843 .detach = usbdux_detach,
2846 static void init_usb_devices(void)
2849 #ifdef CONFIG_COMEDI_DEBUG
2850 printk("comedi_: usbdux: setting all possible devs to invalid\n");
2852 /* all devices entries are invalid to begin with */
2853 /* they will become valid by the probe function */
2854 /* and then finally by the attach-function */
2855 for (index = 0; index < NUMUSBDUX; index++) {
2856 memset(&(usbduxsub[index]), 0x00, sizeof(usbduxsub[index]));
2857 init_MUTEX(&(usbduxsub[index].sem));
2861 /* Table with the USB-devices: just now only testing IDs */
2862 static struct usb_device_id usbduxsub_table[] = {
2863 {USB_DEVICE(0x13d8, 0x0001) },
2864 {USB_DEVICE(0x13d8, 0x0002) },
2865 {} /* Terminating entry */
2868 MODULE_DEVICE_TABLE(usb, usbduxsub_table);
2870 /* The usbduxsub-driver */
2871 static struct usb_driver usbduxsub_driver = {
2873 .probe = usbduxsub_probe,
2874 .disconnect = usbduxsub_disconnect,
2875 .id_table = usbduxsub_table,
2878 /* Can't use the nice macro as I have also to initialise the USB */
2880 /* registering the usb-system _and_ the comedi-driver */
2881 static int init_usbdux(void)
2883 printk(KERN_INFO KBUILD_MODNAME ": "
2884 DRIVER_VERSION ":" DRIVER_DESC "\n");
2886 usb_register(&usbduxsub_driver);
2887 comedi_driver_register(&driver_usbdux);
2891 /* deregistering the comedi driver and the usb-subsystem */
2892 static void exit_usbdux(void)
2894 comedi_driver_unregister(&driver_usbdux);
2895 usb_deregister(&usbduxsub_driver);
2898 module_init(init_usbdux);
2899 module_exit(exit_usbdux);
2901 MODULE_AUTHOR(DRIVER_AUTHOR);
2902 MODULE_DESCRIPTION(DRIVER_DESC);
2903 MODULE_LICENSE("GPL");