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
57 * attach final USB IDs
58 * moved memory allocation completely to the corresponding comedi
59 * functions firmware upload is by fxload and no longer by comedi (due to
61 * 0.97: USB IDs received, adjusted table
62 * 0.98: SMP, locking, memroy alloc: moved all usb memory alloc
63 * to the usb subsystem and moved all comedi related memory
65 * | kernel | registration | usbdux-usb | usbdux-comedi | comedi |
66 * 0.99: USB 2.0: changed protocol to isochronous transfer
67 * IRQ transfer is too buggy and too risky in 2.0
68 * for the high speed ISO transfer is now a working version
70 * 0.99b: Increased the iso transfer buffer for high sp.to 10 buffers. Some VIA
71 * chipsets miss out IRQs. Deeper buffering is needed.
72 * 1.00: full USB 2.0 support for the A/D converter. Now: max 8kHz sampling
74 * Firmware vers 1.00 is needed for this.
75 * Two 16 bit up/down/reset counter with a sampling rate of 1kHz
76 * And loads of cleaning up, in particular streamlining the
78 * 1.1: moved EP4 transfers to EP1 to make space for a PWM output on EP4
79 * 1.2: added PWM suport via EP4
80 * 2.0: PWM seems to be stable and is not interfering with the other functions
81 * 2.1: changed PWM API
85 /* generates loads of debug info */
86 /* #define NOISY_DUX_DEBUGBUG */
88 #include <linux/kernel.h>
89 #include <linux/module.h>
90 #include <linux/init.h>
91 #include <linux/slab.h>
92 #include <linux/input.h>
93 #include <linux/usb.h>
94 #include <linux/smp_lock.h>
95 #include <linux/fcntl.h>
96 #include <linux/compiler.h>
98 #include "../comedidev.h"
100 #define BOARDNAME "usbdux"
102 /* timeout for the USB-transfer */
105 /* constants for "firmware" upload and download */
106 #define USBDUXSUB_FIRMWARE 0xA0
107 #define VENDOR_DIR_IN 0xC0
108 #define VENDOR_DIR_OUT 0x40
110 /* internal adresses of the 8051 processor */
111 #define USBDUXSUB_CPUCS 0xE600
114 * the minor device number, major is 180 only for debugging purposes and to
115 * upload special firmware (programming the eeprom etc) which is not compatible
116 * with the comedi framwork
118 #define USBDUXSUB_MINOR 32
120 /* max lenghth of the transfer-buffer for software upload */
121 #define TB_LEN 0x2000
123 /* Input endpoint number: ISO/IRQ */
126 /* Output endpoint number: ISO/IRQ */
129 /* This EP sends DUX commands to USBDUX */
130 #define COMMAND_OUT_EP 1
132 /* This EP receives the DUX commands from USBDUX */
133 #define COMMAND_IN_EP 8
135 /* Output endpoint for PWM */
138 /* 300Hz max frequ under PWM */
139 #define MIN_PWM_PERIOD ((long)(1E9/300))
141 /* Default PWM frequency */
142 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
144 /* Number of channels */
145 #define NUMCHANNELS 8
147 /* Size of one A/D value */
148 #define SIZEADIN ((sizeof(int16_t)))
151 * Size of the input-buffer IN BYTES
152 * Always multiple of 8 for 8 microframes which is needed in the highspeed mode
154 #define SIZEINBUF ((8*SIZEADIN))
157 #define SIZEINSNBUF 16
159 /* Number of DA channels */
160 #define NUMOUTCHANNELS 8
162 /* size of one value for the D/A converter: channel and value */
163 #define SIZEDAOUT ((sizeof(int8_t)+sizeof(int16_t)))
166 * Size of the output-buffer in bytes
167 * Actually only the first 4 triplets are used but for the
168 * high speed mode we need to pad it to 8 (microframes).
170 #define SIZEOUTBUF ((8*SIZEDAOUT))
173 * Size of the buffer for the dux commands: just now max size is determined
174 * by the analogue out + command byte + panic bytes...
176 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
178 /* Number of in-URBs which receive the data: min=2 */
179 #define NUMOFINBUFFERSFULL 5
181 /* Number of out-URBs which send the data: min=2 */
182 #define NUMOFOUTBUFFERSFULL 5
184 /* Number of in-URBs which receive the data: min=5 */
185 /* must have more buffers due to buggy USB ctr */
186 #define NUMOFINBUFFERSHIGH 10
188 /* Number of out-URBs which send the data: min=5 */
189 /* must have more buffers due to buggy USB ctr */
190 #define NUMOFOUTBUFFERSHIGH 10
192 /* Total number of usbdux devices */
195 /* Analogue in subdevice */
198 /* Analogue out subdevice */
205 #define SUBDEV_COUNTER 3
207 /* timer aka pwm output */
210 /* number of retries to get the right dux command */
213 /**************************************************/
214 /* comedi constants */
215 static const comedi_lrange range_usbdux_ai_range = { 4, {
217 BIP_RANGE(4.096 / 2),
223 static const comedi_lrange range_usbdux_ao_range = { 2, {
230 * private structure of one subdevice
234 * This is the structure which holds all the data of
235 * this driver one sub device just now: A/D
240 /* is it associated with a subdevice? */
242 /* pointer to the usb-device */
243 struct usb_device *usbdev;
244 /* actual number of in-buffers */
246 /* actual number of out-buffers */
248 /* ISO-transfer handling: buffers */
251 /* pwm-transfer handling */
255 /* PWM internal delay for the GPIF in the FX2 */
257 /* size of the PWM buffer which holds the bit pattern */
259 /* input buffer for the ISO-transfer */
261 /* input buffer for single insn */
263 /* output buffer for single DA outputs */
265 /* interface number */
267 /* interface structure in 2.6 */
268 struct usb_interface *interface;
269 /* comedi device for the interrupt context */
270 comedi_device *comedidev;
271 /* is it USB_SPEED_HIGH or not? */
272 short int high_speed;
273 /* asynchronous command is running */
274 short int ai_cmd_running;
275 short int ao_cmd_running;
277 short int pwm_cmd_running;
278 /* continous aquisition */
279 short int ai_continous;
280 short int ao_continous;
281 /* number of samples to aquire */
284 /* time between samples in units of the timer */
285 unsigned int ai_timer;
286 unsigned int ao_timer;
287 /* counter between aquisitions */
288 unsigned int ai_counter;
289 unsigned int ao_counter;
290 /* interval in frames/uframes */
291 unsigned int ai_interval;
293 int8_t *dac_commands;
295 int8_t *dux_commands;
296 struct semaphore sem;
300 * The pointer to the private usb-data of the driver is also the private data
301 * for the comedi-device. This has to be global as the usb subsystem needs
302 * global variables. The other reason is that this structure must be there
303 * _before_ any comedi command is issued. The usb subsystem must be initialised
304 * before comedi can access it.
306 static struct usbduxsub usbduxsub[NUMUSBDUX];
308 static DECLARE_MUTEX(start_stop_sem);
311 * Stops the data acquision
312 * It should be safe to call this function from any context
314 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
319 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
320 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
321 if (usbduxsub_tmp->urbIn[i]) {
322 /* We wait here until all transfers have been
324 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
326 dev_dbg(&usbduxsub_tmp->interface->dev,
327 "comedi: usbdux: unlinked InURB %d, err=%d\n",
335 * This will stop a running acquisition operation
336 * Is called from within this driver from both the
337 * interrupt context and from comedi
339 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
343 if (!this_usbduxsub) {
344 dev_err(&this_usbduxsub->interface->dev,
345 "comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
348 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
352 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
355 this_usbduxsub->ai_cmd_running = 0;
361 * This will cancel a running acquisition operation.
362 * This is called by comedi but never from inside the driver.
364 static int usbdux_ai_cancel(comedi_device *dev, comedi_subdevice *s)
366 struct usbduxsub *this_usbduxsub;
369 /* force unlink of all urbs */
370 this_usbduxsub = dev->private;
374 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
376 /* prevent other CPUs from submitting new commands just now */
377 down(&this_usbduxsub->sem);
378 if (!(this_usbduxsub->probed)) {
379 up(&this_usbduxsub->sem);
382 /* unlink only if the urb really has been submitted */
383 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
384 up(&this_usbduxsub->sem);
388 /* analogue IN - interrupt service routine */
389 static void usbduxsub_ai_IsocIrq(struct urb *urb)
392 struct usbduxsub *this_usbduxsub;
393 comedi_device *this_comedidev;
396 /* the context variable points to the subdevice */
397 this_comedidev = urb->context;
398 /* the private structure of the subdevice is struct usbduxsub */
399 this_usbduxsub = this_comedidev->private;
400 /* subdevice which is the AD converter */
401 s = this_comedidev->subdevices + SUBDEV_AD;
403 /* first we test if something unusual has just happened */
404 switch (urb->status) {
406 /* copy the result in the transfer buffer */
407 memcpy(this_usbduxsub->inBuffer,
408 urb->transfer_buffer, SIZEINBUF);
411 /* error in the ISOchronous data */
412 /* we don't copy the data into the transfer buffer */
413 /* and recycle the last data byte */
414 dev_dbg(&urb->dev->dev,
415 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
416 this_usbduxsub->comedidev->minor);
424 /* happens after an unlink command */
425 if (this_usbduxsub->ai_cmd_running) {
426 /* we are still running a command */
427 /* tell this comedi */
428 s->async->events |= COMEDI_CB_EOA;
429 s->async->events |= COMEDI_CB_ERROR;
430 comedi_event(this_usbduxsub->comedidev, s);
431 /* stop the transfer w/o unlink */
432 usbdux_ai_stop(this_usbduxsub, 0);
437 /* a real error on the bus */
438 /* pass error to comedi if we are really running a command */
439 if (this_usbduxsub->ai_cmd_running) {
440 dev_err(&urb->dev->dev,
441 "Non-zero urb status received in ai intr "
442 "context: %d\n", urb->status);
443 s->async->events |= COMEDI_CB_EOA;
444 s->async->events |= COMEDI_CB_ERROR;
445 comedi_event(this_usbduxsub->comedidev, s);
446 /* don't do an unlink here */
447 usbdux_ai_stop(this_usbduxsub, 0);
453 * at this point we are reasonably sure that nothing dodgy has happened
454 * are we running a command?
456 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
458 * not running a command, do not continue execution if no
459 * asynchronous command is running in particular not resubmit
464 urb->dev = this_usbduxsub->usbdev;
466 /* resubmit the urb */
467 err = usb_submit_urb(urb, GFP_ATOMIC);
468 if (unlikely(err < 0)) {
469 dev_err(&urb->dev->dev,
470 "comedi_: urb resubmit failed in int-context! err=%d\n",
472 if (err == -EL2NSYNC)
473 dev_err(&urb->dev->dev,
474 "buggy USB host controller or bug in IRQ "
476 s->async->events |= COMEDI_CB_EOA;
477 s->async->events |= COMEDI_CB_ERROR;
478 comedi_event(this_usbduxsub->comedidev, s);
479 /* don't do an unlink here */
480 usbdux_ai_stop(this_usbduxsub, 0);
484 this_usbduxsub->ai_counter--;
485 if (likely(this_usbduxsub->ai_counter > 0))
488 /* timer zero, transfer measurements to comedi */
489 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
491 /* test, if we transmit only a fixed number of samples */
492 if (!(this_usbduxsub->ai_continous)) {
493 /* not continous, fixed number of samples */
494 this_usbduxsub->ai_sample_count--;
495 /* all samples received? */
496 if (this_usbduxsub->ai_sample_count < 0) {
497 /* prevent a resubmit next time */
498 usbdux_ai_stop(this_usbduxsub, 0);
499 /* say comedi that the acquistion is over */
500 s->async->events |= COMEDI_CB_EOA;
501 comedi_event(this_usbduxsub->comedidev, s);
505 /* get the data from the USB bus and hand it over to comedi */
506 n = s->async->cmd.chanlist_len;
507 for (i = 0; i < n; i++) {
509 if (CR_RANGE(s->async->cmd.chanlist[i]) <= 1) {
512 le16_to_cpu(this_usbduxsub->
513 inBuffer[i]) ^ 0x800);
517 le16_to_cpu(this_usbduxsub->inBuffer[i]));
520 /* tell comedi that data is there */
521 comedi_event(this_usbduxsub->comedidev, s);
524 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
529 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
530 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
531 if (usbduxsub_tmp->urbOut[i])
532 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
534 dev_dbg(&usbduxsub_tmp->interface->dev,
535 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
542 /* This will cancel a running acquisition operation
545 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
551 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
554 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
556 this_usbduxsub->ao_cmd_running = 0;
561 /* force unlink, is called by comedi */
562 static int usbdux_ao_cancel(comedi_device *dev, comedi_subdevice *s)
564 struct usbduxsub *this_usbduxsub = dev->private;
570 /* prevent other CPUs from submitting a command just now */
571 down(&this_usbduxsub->sem);
572 if (!(this_usbduxsub->probed)) {
573 up(&this_usbduxsub->sem);
576 /* unlink only if it is really running */
577 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
578 up(&this_usbduxsub->sem);
582 static void usbduxsub_ao_IsocIrq(struct urb *urb)
586 struct usbduxsub *this_usbduxsub;
587 comedi_device *this_comedidev;
590 /* the context variable points to the subdevice */
591 this_comedidev = urb->context;
592 /* the private structure of the subdevice is struct usbduxsub */
593 this_usbduxsub = this_comedidev->private;
595 s = this_comedidev->subdevices + SUBDEV_DA;
597 switch (urb->status) {
606 /* after an unlink command, unplug, ... etc */
607 /* no unlink needed here. Already shutting down. */
608 if (this_usbduxsub->ao_cmd_running) {
609 s->async->events |= COMEDI_CB_EOA;
610 comedi_event(this_usbduxsub->comedidev, s);
611 usbdux_ao_stop(this_usbduxsub, 0);
617 if (this_usbduxsub->ao_cmd_running) {
618 dev_err(&urb->dev->dev,
619 "comedi_: Non-zero urb status received in ao "
620 "intr context: %d\n", urb->status);
621 s->async->events |= COMEDI_CB_ERROR;
622 s->async->events |= COMEDI_CB_EOA;
623 comedi_event(this_usbduxsub->comedidev, s);
624 /* we do an unlink if we are in the high speed mode */
625 usbdux_ao_stop(this_usbduxsub, 0);
630 /* are we actually running? */
631 if (!(this_usbduxsub->ao_cmd_running))
634 /* normal operation: executing a command in this subdevice */
635 this_usbduxsub->ao_counter--;
636 if (this_usbduxsub->ao_counter <= 0) {
638 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
640 /* handle non continous aquisition */
641 if (!(this_usbduxsub->ao_continous)) {
642 /* fixed number of samples */
643 this_usbduxsub->ao_sample_count--;
644 if (this_usbduxsub->ao_sample_count < 0) {
645 /* all samples transmitted */
646 usbdux_ao_stop(this_usbduxsub, 0);
647 s->async->events |= COMEDI_CB_EOA;
648 comedi_event(this_usbduxsub->comedidev, s);
649 /* no resubmit of the urb */
653 /* transmit data to the USB bus */
654 ((uint8_t *) (urb->transfer_buffer))[0] =
655 s->async->cmd.chanlist_len;
656 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
658 if (i >= NUMOUTCHANNELS)
661 /* pointer to the DA */
663 (&(((int8_t *)urb->transfer_buffer)[i * 3 + 1]));
664 /* get the data from comedi */
665 ret = comedi_buf_get(s->async, &temp);
667 datap[1] = temp >> 8;
668 datap[2] = this_usbduxsub->dac_commands[i];
669 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
670 /* datap[0],datap[1],datap[2]); */
672 dev_err(&urb->dev->dev,
673 "comedi: buffer underflow\n");
674 s->async->events |= COMEDI_CB_EOA;
675 s->async->events |= COMEDI_CB_OVERFLOW;
677 /* transmit data to comedi */
678 s->async->events |= COMEDI_CB_BLOCK;
679 comedi_event(this_usbduxsub->comedidev, s);
682 urb->transfer_buffer_length = SIZEOUTBUF;
683 urb->dev = this_usbduxsub->usbdev;
685 if (this_usbduxsub->ao_cmd_running) {
686 if (this_usbduxsub->high_speed) {
693 urb->number_of_packets = 1;
694 urb->iso_frame_desc[0].offset = 0;
695 urb->iso_frame_desc[0].length = SIZEOUTBUF;
696 urb->iso_frame_desc[0].status = 0;
697 ret = usb_submit_urb(urb, GFP_ATOMIC);
699 dev_err(&urb->dev->dev,
700 "comedi_: ao urb resubm failed in int-cont. "
703 dev_err(&urb->dev->dev,
704 "buggy USB host controller or bug in "
707 s->async->events |= COMEDI_CB_EOA;
708 s->async->events |= COMEDI_CB_ERROR;
709 comedi_event(this_usbduxsub->comedidev, s);
710 /* don't do an unlink here */
711 usbdux_ao_stop(this_usbduxsub, 0);
716 static int usbduxsub_start(struct usbduxsub *usbduxsub)
719 uint8_t local_transfer_buffer[16];
721 if (usbduxsub->probed) {
723 local_transfer_buffer[0] = 0;
724 errcode = usb_control_msg(usbduxsub->usbdev,
725 /* create a pipe for a control transfer */
726 usb_sndctrlpipe(usbduxsub->usbdev, 0),
727 /* bRequest, "Firmware" */
735 /* address of the transfer buffer */
736 local_transfer_buffer,
742 dev_err(&usbduxsub->interface->dev,
743 "comedi_: control msg failed (start)\n");
750 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
754 uint8_t local_transfer_buffer[16];
755 if (usbduxsub->probed) {
757 local_transfer_buffer[0] = 1;
758 errcode = usb_control_msg(usbduxsub->usbdev,
759 usb_sndctrlpipe(usbduxsub->usbdev, 0),
760 /* bRequest, "Firmware" */
767 0x0000, local_transfer_buffer,
773 dev_err(&usbduxsub->interface->dev,
774 "comedi_: control msg failed (stop)\n");
781 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
782 uint8_t *local_transfer_buffer,
783 unsigned int startAddr, unsigned int len)
787 if (usbduxsub->probed) {
788 dev_dbg(&usbduxsub->interface->dev,
789 "comedi%d: usbdux: uploading %d bytes"
790 " to addr %d, first byte=%d.\n",
791 usbduxsub->comedidev->minor, len,
792 startAddr, local_transfer_buffer[0]);
793 errcode = usb_control_msg(usbduxsub->usbdev,
794 usb_sndctrlpipe(usbduxsub->usbdev, 0),
795 /* brequest, firmware */
803 /* our local safe buffer */
804 local_transfer_buffer,
809 dev_dbg(&usbduxsub->interface->dev,
810 "comedi_: result=%d\n", errcode);
812 dev_err(&usbduxsub->interface->dev,
813 "comedi_: upload failed\n");
817 /* no device on the bus for this index */
823 static int firmwareUpload(struct usbduxsub *usbduxsub, uint8_t *firmwareBinary,
831 ret = usbduxsub_stop(usbduxsub);
833 dev_err(&usbduxsub->interface->dev,
834 "comedi_: can not stop firmware\n");
837 ret = usbduxsub_upload(usbduxsub, firmwareBinary, 0, sizeFirmware);
839 dev_err(&usbduxsub->interface->dev,
840 "comedi_: firmware upload failed\n");
843 ret = usbduxsub_start(usbduxsub);
845 dev_err(&usbduxsub->interface->dev,
846 "comedi_: can not start firmware\n");
852 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
859 /* Submit all URBs and start the transfer on the bus */
860 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
861 /* in case of a resubmission after an unlink... */
862 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
863 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
864 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
865 usbduxsub->urbIn[i]->status = 0;
866 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
867 dev_dbg(&usbduxsub->interface->dev,
868 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
869 usbduxsub->comedidev->minor, i,
870 (usbduxsub->urbIn[i]->context),
871 (usbduxsub->urbIn[i]->dev),
872 (usbduxsub->urbIn[i]->interval));
873 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
875 dev_err(&usbduxsub->interface->dev,
876 "comedi_: ai: usb_submit_urb(%d) error %d\n",
884 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
891 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
892 dev_dbg(&usbduxsub->interface->dev,
893 "comedi_: submitting out-urb[%d]\n", i);
894 /* in case of a resubmission after an unlink... */
895 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
896 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
897 usbduxsub->urbOut[i]->status = 0;
898 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
899 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
901 dev_err(&usbduxsub->interface->dev,
902 "comedi_: ao: usb_submit_urb(%d) error %d\n",
910 static int usbdux_ai_cmdtest(comedi_device *dev, comedi_subdevice *s,
914 unsigned int tmpTimer;
915 struct usbduxsub *this_usbduxsub = dev->private;
917 if (!(this_usbduxsub->probed))
920 dev_dbg(&this_usbduxsub->interface->dev,
921 "comedi%d: usbdux_ai_cmdtest\n", dev->minor);
923 /* make sure triggers are valid */
924 /* Only immediate triggers are allowed */
925 tmp = cmd->start_src;
926 cmd->start_src &= TRIG_NOW | TRIG_INT;
927 if (!cmd->start_src || tmp != cmd->start_src)
930 /* trigger should happen timed */
931 tmp = cmd->scan_begin_src;
932 /* start a new _scan_ with a timer */
933 cmd->scan_begin_src &= TRIG_TIMER;
934 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
937 /* scanning is continous */
938 tmp = cmd->convert_src;
939 cmd->convert_src &= TRIG_NOW;
940 if (!cmd->convert_src || tmp != cmd->convert_src)
943 /* issue a trigger when scan is finished and start a new scan */
944 tmp = cmd->scan_end_src;
945 cmd->scan_end_src &= TRIG_COUNT;
946 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
949 /* trigger at the end of count events or not, stop condition or not */
951 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
952 if (!cmd->stop_src || tmp != cmd->stop_src)
959 * step 2: make sure trigger sources are unique and mutually compatible
960 * note that mutual compatiblity is not an issue here
962 if (cmd->scan_begin_src != TRIG_FOLLOW &&
963 cmd->scan_begin_src != TRIG_EXT &&
964 cmd->scan_begin_src != TRIG_TIMER)
966 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
972 /* step 3: make sure arguments are trivially compatible */
973 if (cmd->start_arg != 0) {
978 if (cmd->scan_begin_src == TRIG_FOLLOW) {
979 /* internal trigger */
980 if (cmd->scan_begin_arg != 0) {
981 cmd->scan_begin_arg = 0;
986 if (cmd->scan_begin_src == TRIG_TIMER) {
987 if (this_usbduxsub->high_speed) {
989 * In high speed mode microframes are possible.
990 * However, during one microframe we can roughly
991 * sample one channel. Thus, the more channels
992 * are in the channel list the more time we need.
995 /* find a power of 2 for the number of channels */
996 while (i < (cmd->chanlist_len))
999 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
1000 cmd->scan_begin_arg = 1000000 / 8 * i;
1003 /* now calc the real sampling rate with all the
1004 * rounding errors */
1006 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
1008 if (cmd->scan_begin_arg != tmpTimer) {
1009 cmd->scan_begin_arg = tmpTimer;
1014 /* 1kHz scans every USB frame */
1015 if (cmd->scan_begin_arg < 1000000) {
1016 cmd->scan_begin_arg = 1000000;
1020 * calc the real sampling rate with the rounding errors
1022 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
1023 1000000)) * 1000000;
1024 if (cmd->scan_begin_arg != tmpTimer) {
1025 cmd->scan_begin_arg = tmpTimer;
1030 /* the same argument */
1031 if (cmd->scan_end_arg != cmd->chanlist_len) {
1032 cmd->scan_end_arg = cmd->chanlist_len;
1036 if (cmd->stop_src == TRIG_COUNT) {
1037 /* any count is allowed */
1040 if (cmd->stop_arg != 0) {
1053 * creates the ADC command for the MAX1271
1054 * range is the range value from comedi
1056 static int8_t create_adc_command(unsigned int chan, int range)
1058 int8_t p = (range <= 1);
1059 int8_t r = ((range % 2) == 0);
1060 return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
1063 /* bulk transfers to usbdux */
1065 #define SENDADCOMMANDS 0
1066 #define SENDDACOMMANDS 1
1067 #define SENDDIOCONFIGCOMMAND 2
1068 #define SENDDIOBITSCOMMAND 3
1069 #define SENDSINGLEAD 4
1070 #define READCOUNTERCOMMAND 5
1071 #define WRITECOUNTERCOMMAND 6
1073 #define SENDPWMOFF 8
1075 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1079 this_usbduxsub->dux_commands[0] = cmd_type;
1080 #ifdef NOISY_DUX_DEBUGBUG
1081 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1082 this_usbduxsub->comedidev->minor);
1083 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1084 printk(" %02x", this_usbduxsub->dux_commands[result]);
1087 result = usb_bulk_msg(this_usbduxsub->usbdev,
1088 usb_sndbulkpipe(this_usbduxsub->usbdev,
1090 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1093 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1094 "could not transmit dux_command to the usb-device, "
1095 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1100 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1102 int result = (-EFAULT);
1106 for (i = 0; i < RETRIES; i++) {
1107 result = usb_bulk_msg(this_usbduxsub->usbdev,
1108 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1110 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1113 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1114 "insn: USB error %d while receiving DUX command"
1115 "\n", this_usbduxsub->comedidev->minor, result);
1118 if (le16_to_cpu(this_usbduxsub->insnBuffer[0]) == command)
1121 /* this is only reached if the data has been requested a couple of
1123 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1124 "wrong data returned from firmware: want cmd %d, got cmd %d.\n",
1125 this_usbduxsub->comedidev->minor, command,
1126 le16_to_cpu(this_usbduxsub->insnBuffer[0]));
1130 static int usbdux_ai_inttrig(comedi_device *dev, comedi_subdevice *s,
1131 unsigned int trignum)
1134 struct usbduxsub *this_usbduxsub = dev->private;
1135 if (!this_usbduxsub)
1138 down(&this_usbduxsub->sem);
1139 if (!(this_usbduxsub->probed)) {
1140 up(&this_usbduxsub->sem);
1143 dev_dbg(&this_usbduxsub->interface->dev,
1144 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1147 dev_err(&this_usbduxsub->interface->dev,
1148 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1150 up(&this_usbduxsub->sem);
1153 if (!(this_usbduxsub->ai_cmd_running)) {
1154 this_usbduxsub->ai_cmd_running = 1;
1155 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1157 dev_err(&this_usbduxsub->interface->dev,
1158 "comedi%d: usbdux_ai_inttrig: "
1159 "urbSubmit: err=%d\n", dev->minor, ret);
1160 this_usbduxsub->ai_cmd_running = 0;
1161 up(&this_usbduxsub->sem);
1164 s->async->inttrig = NULL;
1166 dev_err(&this_usbduxsub->interface->dev,
1167 "comedi%d: ai_inttrig but acqu is already running\n",
1170 up(&this_usbduxsub->sem);
1174 static int usbdux_ai_cmd(comedi_device *dev, comedi_subdevice *s)
1176 comedi_cmd *cmd = &s->async->cmd;
1177 unsigned int chan, range;
1179 struct usbduxsub *this_usbduxsub = dev->private;
1182 if (!this_usbduxsub)
1185 dev_dbg(&this_usbduxsub->interface->dev,
1186 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1188 /* block other CPUs from starting an ai_cmd */
1189 down(&this_usbduxsub->sem);
1191 if (!(this_usbduxsub->probed)) {
1192 up(&this_usbduxsub->sem);
1195 if (this_usbduxsub->ai_cmd_running) {
1196 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1197 "ai_cmd not possible. Another ai_cmd is running.\n",
1199 up(&this_usbduxsub->sem);
1202 /* set current channel of the running aquisition to zero */
1203 s->async->cur_chan = 0;
1205 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1206 for (i = 0; i < cmd->chanlist_len; ++i) {
1207 chan = CR_CHAN(cmd->chanlist[i]);
1208 range = CR_RANGE(cmd->chanlist[i]);
1209 if (i >= NUMCHANNELS) {
1210 dev_err(&this_usbduxsub->interface->dev,
1211 "comedi%d: channel list too long\n",
1215 this_usbduxsub->dux_commands[i + 2] =
1216 create_adc_command(chan, range);
1219 dev_dbg(&this_usbduxsub->interface->dev,
1220 "comedi %d: sending commands to the usb device: size=%u\n",
1221 dev->minor, NUMCHANNELS);
1223 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1225 up(&this_usbduxsub->sem);
1229 if (this_usbduxsub->high_speed) {
1231 * every channel gets a time window of 125us. Thus, if we
1232 * sample all 8 channels we need 1ms. If we sample only one
1233 * channel we need only 125us
1235 this_usbduxsub->ai_interval = 1;
1236 /* find a power of 2 for the interval */
1237 while ((this_usbduxsub->ai_interval) < (cmd->chanlist_len)) {
1238 this_usbduxsub->ai_interval =
1239 (this_usbduxsub->ai_interval) * 2;
1241 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1242 (this_usbduxsub->ai_interval));
1244 /* interval always 1ms */
1245 this_usbduxsub->ai_interval = 1;
1246 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1248 if (this_usbduxsub->ai_timer < 1) {
1249 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1250 "timer=%d, scan_begin_arg=%d. "
1251 "Not properly tested by cmdtest?\n", dev->minor,
1252 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1253 up(&this_usbduxsub->sem);
1256 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1258 if (cmd->stop_src == TRIG_COUNT) {
1259 /* data arrives as one packet */
1260 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1261 this_usbduxsub->ai_continous = 0;
1263 /* continous aquisition */
1264 this_usbduxsub->ai_continous = 1;
1265 this_usbduxsub->ai_sample_count = 0;
1268 if (cmd->start_src == TRIG_NOW) {
1269 /* enable this acquisition operation */
1270 this_usbduxsub->ai_cmd_running = 1;
1271 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1273 this_usbduxsub->ai_cmd_running = 0;
1274 /* fixme: unlink here?? */
1275 up(&this_usbduxsub->sem);
1278 s->async->inttrig = NULL;
1281 /* don't enable the acquision operation */
1282 /* wait for an internal signal */
1283 s->async->inttrig = usbdux_ai_inttrig;
1285 up(&this_usbduxsub->sem);
1289 /* Mode 0 is used to get a single conversion on demand */
1290 static int usbdux_ai_insn_read(comedi_device *dev, comedi_subdevice *s,
1291 comedi_insn *insn, lsampl_t *data)
1297 struct usbduxsub *this_usbduxsub = dev->private;
1299 if (!this_usbduxsub)
1302 dev_dbg(&this_usbduxsub->interface->dev,
1303 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1304 dev->minor, insn->n, insn->subdev);
1306 down(&this_usbduxsub->sem);
1307 if (!(this_usbduxsub->probed)) {
1308 up(&this_usbduxsub->sem);
1311 if (this_usbduxsub->ai_cmd_running) {
1312 dev_err(&this_usbduxsub->interface->dev,
1313 "comedi%d: ai_insn_read not possible. "
1314 "Async Command is running.\n", dev->minor);
1315 up(&this_usbduxsub->sem);
1319 /* sample one channel */
1320 chan = CR_CHAN(insn->chanspec);
1321 range = CR_RANGE(insn->chanspec);
1322 /* set command for the first channel */
1323 this_usbduxsub->dux_commands[1] = create_adc_command(chan, range);
1326 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1328 up(&this_usbduxsub->sem);
1332 for (i = 0; i < insn->n; i++) {
1333 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1335 up(&this_usbduxsub->sem);
1338 one = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1339 if (CR_RANGE(insn->chanspec) <= 1)
1344 up(&this_usbduxsub->sem);
1348 /************************************/
1351 static int usbdux_ao_insn_read(comedi_device *dev, comedi_subdevice *s,
1352 comedi_insn *insn, lsampl_t *data)
1355 int chan = CR_CHAN(insn->chanspec);
1356 struct usbduxsub *this_usbduxsub = dev->private;
1358 if (!this_usbduxsub)
1361 down(&this_usbduxsub->sem);
1362 if (!(this_usbduxsub->probed)) {
1363 up(&this_usbduxsub->sem);
1366 for (i = 0; i < insn->n; i++)
1367 data[i] = this_usbduxsub->outBuffer[chan];
1369 up(&this_usbduxsub->sem);
1373 static int usbdux_ao_insn_write(comedi_device *dev, comedi_subdevice *s,
1374 comedi_insn *insn, lsampl_t *data)
1377 int chan = CR_CHAN(insn->chanspec);
1378 struct usbduxsub *this_usbduxsub = dev->private;
1380 if (!this_usbduxsub)
1383 dev_dbg(&this_usbduxsub->interface->dev,
1384 "comedi%d: ao_insn_write\n", dev->minor);
1386 down(&this_usbduxsub->sem);
1387 if (!(this_usbduxsub->probed)) {
1388 up(&this_usbduxsub->sem);
1391 if (this_usbduxsub->ao_cmd_running) {
1392 dev_err(&this_usbduxsub->interface->dev,
1393 "comedi%d: ao_insn_write: "
1394 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1395 up(&this_usbduxsub->sem);
1399 for (i = 0; i < insn->n; i++) {
1400 dev_dbg(&this_usbduxsub->interface->dev,
1401 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1402 dev->minor, chan, i, data[i]);
1404 /* number of channels: 1 */
1405 this_usbduxsub->dux_commands[1] = 1;
1406 /* one 16 bit value */
1407 *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
1408 cpu_to_le16(data[i]);
1409 this_usbduxsub->outBuffer[chan] = data[i];
1410 /* channel number */
1411 this_usbduxsub->dux_commands[4] = (chan << 6);
1412 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1414 up(&this_usbduxsub->sem);
1418 up(&this_usbduxsub->sem);
1423 static int usbdux_ao_inttrig(comedi_device *dev, comedi_subdevice *s,
1424 unsigned int trignum)
1427 struct usbduxsub *this_usbduxsub = dev->private;
1429 if (!this_usbduxsub)
1432 down(&this_usbduxsub->sem);
1433 if (!(this_usbduxsub->probed)) {
1434 up(&this_usbduxsub->sem);
1438 dev_err(&this_usbduxsub->interface->dev,
1439 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1443 if (!(this_usbduxsub->ao_cmd_running)) {
1444 this_usbduxsub->ao_cmd_running = 1;
1445 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1447 dev_err(&this_usbduxsub->interface->dev,
1448 "comedi%d: usbdux_ao_inttrig: submitURB: "
1449 "err=%d\n", dev->minor, ret);
1450 this_usbduxsub->ao_cmd_running = 0;
1451 up(&this_usbduxsub->sem);
1454 s->async->inttrig = NULL;
1456 dev_err(&this_usbduxsub->interface->dev,
1457 "comedi%d: ao_inttrig but acqu is already running.\n",
1460 up(&this_usbduxsub->sem);
1464 static int usbdux_ao_cmdtest(comedi_device *dev, comedi_subdevice *s,
1468 struct usbduxsub *this_usbduxsub = dev->private;
1470 if (!this_usbduxsub)
1473 if (!(this_usbduxsub->probed))
1476 dev_dbg(&this_usbduxsub->interface->dev,
1477 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1479 /* make sure triggers are valid */
1480 /* Only immediate triggers are allowed */
1481 tmp = cmd->start_src;
1482 cmd->start_src &= TRIG_NOW | TRIG_INT;
1483 if (!cmd->start_src || tmp != cmd->start_src)
1486 /* trigger should happen timed */
1487 tmp = cmd->scan_begin_src;
1488 /* just now we scan also in the high speed mode every frame */
1489 /* this is due to ehci driver limitations */
1490 if (0) { /* (this_usbduxsub->high_speed) */
1491 /* start immidiately a new scan */
1492 /* the sampling rate is set by the coversion rate */
1493 cmd->scan_begin_src &= TRIG_FOLLOW;
1495 /* start a new scan (output at once) with a timer */
1496 cmd->scan_begin_src &= TRIG_TIMER;
1498 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1501 /* scanning is continous */
1502 tmp = cmd->convert_src;
1503 /* we always output at 1kHz just now all channels at once */
1504 if (0) { /* (this_usbduxsub->high_speed) */
1506 * in usb-2.0 only one conversion it tranmitted but with 8kHz/n
1508 cmd->convert_src &= TRIG_TIMER;
1510 /* all conversion events happen simultaneously with a rate of
1512 cmd->convert_src &= TRIG_NOW;
1514 if (!cmd->convert_src || tmp != cmd->convert_src)
1517 /* issue a trigger when scan is finished and start a new scan */
1518 tmp = cmd->scan_end_src;
1519 cmd->scan_end_src &= TRIG_COUNT;
1520 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1523 /* trigger at the end of count events or not, stop condition or not */
1524 tmp = cmd->stop_src;
1525 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1526 if (!cmd->stop_src || tmp != cmd->stop_src)
1533 * step 2: make sure trigger sources are unique and mutually compatible
1534 * note that mutual compatiblity is not an issue here
1536 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1537 cmd->scan_begin_src != TRIG_EXT &&
1538 cmd->scan_begin_src != TRIG_TIMER)
1540 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1546 /* step 3: make sure arguments are trivially compatible */
1548 if (cmd->start_arg != 0) {
1553 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1554 /* internal trigger */
1555 if (cmd->scan_begin_arg != 0) {
1556 cmd->scan_begin_arg = 0;
1561 if (cmd->scan_begin_src == TRIG_TIMER) {
1563 if (cmd->scan_begin_arg < 1000000) {
1564 cmd->scan_begin_arg = 1000000;
1568 /* not used now, is for later use */
1569 if (cmd->convert_src == TRIG_TIMER) {
1570 if (cmd->convert_arg < 125000) {
1571 cmd->convert_arg = 125000;
1576 /* the same argument */
1577 if (cmd->scan_end_arg != cmd->chanlist_len) {
1578 cmd->scan_end_arg = cmd->chanlist_len;
1582 if (cmd->stop_src == TRIG_COUNT) {
1583 /* any count is allowed */
1586 if (cmd->stop_arg != 0) {
1592 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: err=%d, "
1593 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1594 "convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src,
1595 cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1603 static int usbdux_ao_cmd(comedi_device *dev, comedi_subdevice *s)
1605 comedi_cmd *cmd = &s->async->cmd;
1606 unsigned int chan, gain;
1608 struct usbduxsub *this_usbduxsub = dev->private;
1610 if (!this_usbduxsub)
1613 down(&this_usbduxsub->sem);
1614 if (!(this_usbduxsub->probed)) {
1615 up(&this_usbduxsub->sem);
1618 dev_dbg(&this_usbduxsub->interface->dev,
1619 "comedi%d: %s\n", dev->minor, __func__);
1621 /* set current channel of the running aquisition to zero */
1622 s->async->cur_chan = 0;
1623 for (i = 0; i < cmd->chanlist_len; ++i) {
1624 chan = CR_CHAN(cmd->chanlist[i]);
1625 gain = CR_RANGE(cmd->chanlist[i]);
1626 if (i >= NUMOUTCHANNELS) {
1627 dev_err(&this_usbduxsub->interface->dev,
1628 "comedi%d: %s: channel list too long\n",
1629 dev->minor, __func__);
1632 this_usbduxsub->dac_commands[i] = (chan << 6);
1633 dev_dbg(&this_usbduxsub->interface->dev,
1634 "comedi%d: dac command for ch %d is %x\n",
1635 dev->minor, i, this_usbduxsub->dac_commands[i]);
1638 /* we count in steps of 1ms (125us) */
1639 /* 125us mode not used yet */
1640 if (0) { /* (this_usbduxsub->high_speed) */
1642 /* timing of the conversion itself: every 125 us */
1643 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1646 /* timing of the scan: we get all channels at once */
1647 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1648 dev_dbg(&this_usbduxsub->interface->dev,
1649 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1650 "convert_src=%d, convert_arg=%d\n", dev->minor,
1651 cmd->scan_begin_src, cmd->scan_begin_arg,
1652 cmd->convert_src, cmd->convert_arg);
1653 dev_dbg(&this_usbduxsub->interface->dev,
1654 "comedi%d: ao_timer=%d (ms)\n",
1655 dev->minor, this_usbduxsub->ao_timer);
1656 if (this_usbduxsub->ao_timer < 1) {
1657 dev_err(&this_usbduxsub->interface->dev,
1658 "comedi%d: usbdux: ao_timer=%d, "
1659 "scan_begin_arg=%d. "
1660 "Not properly tested by cmdtest?\n",
1661 dev->minor, this_usbduxsub->ao_timer,
1662 cmd->scan_begin_arg);
1663 up(&this_usbduxsub->sem);
1667 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1669 if (cmd->stop_src == TRIG_COUNT) {
1672 /* high speed also scans everything at once */
1673 if (0) { /* (this_usbduxsub->high_speed) */
1674 this_usbduxsub->ao_sample_count =
1675 (cmd->stop_arg) * (cmd->scan_end_arg);
1677 /* there's no scan as the scan has been */
1678 /* perf inside the FX2 */
1679 /* data arrives as one packet */
1680 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1682 this_usbduxsub->ao_continous = 0;
1684 /* continous aquisition */
1685 this_usbduxsub->ao_continous = 1;
1686 this_usbduxsub->ao_sample_count = 0;
1689 if (cmd->start_src == TRIG_NOW) {
1690 /* enable this acquisition operation */
1691 this_usbduxsub->ao_cmd_running = 1;
1692 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1694 this_usbduxsub->ao_cmd_running = 0;
1695 /* fixme: unlink here?? */
1696 up(&this_usbduxsub->sem);
1699 s->async->inttrig = NULL;
1702 /* submit the urbs later */
1703 /* wait for an internal signal */
1704 s->async->inttrig = usbdux_ao_inttrig;
1707 up(&this_usbduxsub->sem);
1711 static int usbdux_dio_insn_config(comedi_device *dev, comedi_subdevice *s,
1712 comedi_insn *insn, lsampl_t *data)
1714 int chan = CR_CHAN(insn->chanspec);
1716 /* The input or output configuration of each digital line is
1717 * configured by a special insn_config instruction. chanspec
1718 * contains the channel to be changed, and data[0] contains the
1719 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1722 case INSN_CONFIG_DIO_OUTPUT:
1723 s->io_bits |= 1 << chan; /* 1 means Out */
1725 case INSN_CONFIG_DIO_INPUT:
1726 s->io_bits &= ~(1 << chan);
1728 case INSN_CONFIG_DIO_QUERY:
1731 io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1737 /* we don't tell the firmware here as it would take 8 frames */
1738 /* to submit the information. We do it in the insn_bits. */
1742 static int usbdux_dio_insn_bits(comedi_device *dev, comedi_subdevice *s,
1743 comedi_insn *insn, lsampl_t *data)
1746 struct usbduxsub *this_usbduxsub = dev->private;
1749 if (!this_usbduxsub)
1756 down(&this_usbduxsub->sem);
1758 if (!(this_usbduxsub->probed)) {
1759 up(&this_usbduxsub->sem);
1763 /* The insn data is a mask in data[0] and the new data
1764 * in data[1], each channel cooresponding to a bit. */
1765 s->state &= ~data[0];
1766 s->state |= data[0] & data[1];
1767 this_usbduxsub->dux_commands[1] = s->io_bits;
1768 this_usbduxsub->dux_commands[2] = s->state;
1770 /* This command also tells the firmware to return */
1771 /* the digital input lines */
1772 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1774 up(&this_usbduxsub->sem);
1777 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1779 up(&this_usbduxsub->sem);
1783 data[1] = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1784 up(&this_usbduxsub->sem);
1788 /* reads the 4 counters, only two are used just now */
1789 static int usbdux_counter_read(comedi_device *dev, comedi_subdevice *s,
1790 comedi_insn *insn, lsampl_t *data)
1792 struct usbduxsub *this_usbduxsub = dev->private;
1793 int chan = insn->chanspec;
1796 if (!this_usbduxsub)
1799 down(&this_usbduxsub->sem);
1801 if (!(this_usbduxsub->probed)) {
1802 up(&this_usbduxsub->sem);
1806 err = send_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1808 up(&this_usbduxsub->sem);
1812 err = receive_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1814 up(&this_usbduxsub->sem);
1818 data[0] = le16_to_cpu(this_usbduxsub->insnBuffer[chan + 1]);
1819 up(&this_usbduxsub->sem);
1823 static int usbdux_counter_write(comedi_device *dev, comedi_subdevice *s,
1824 comedi_insn *insn, lsampl_t *data)
1826 struct usbduxsub *this_usbduxsub = dev->private;
1829 if (!this_usbduxsub)
1832 down(&this_usbduxsub->sem);
1834 if (!(this_usbduxsub->probed)) {
1835 up(&this_usbduxsub->sem);
1839 this_usbduxsub->dux_commands[1] = insn->chanspec;
1840 *((int16_t *) (this_usbduxsub->dux_commands + 2)) = cpu_to_le16(*data);
1842 err = send_dux_commands(this_usbduxsub, WRITECOUNTERCOMMAND);
1844 up(&this_usbduxsub->sem);
1848 up(&this_usbduxsub->sem);
1853 static int usbdux_counter_config(comedi_device *dev, comedi_subdevice *s,
1854 comedi_insn *insn, lsampl_t *data)
1856 /* nothing to do so far */
1860 /***********************************/
1863 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1867 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1868 if (usbduxsub_tmp->urbPwm)
1869 usb_kill_urb(usbduxsub_tmp->urbPwm);
1870 dev_dbg(&usbduxsub_tmp->interface->dev,
1871 "comedi: unlinked PwmURB: res=%d\n", err);
1876 /* This cancels a running acquisition operation
1879 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1883 if (!this_usbduxsub)
1886 dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1888 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1891 this_usbduxsub->pwm_cmd_running = 0;
1896 /* force unlink - is called by comedi */
1897 static int usbdux_pwm_cancel(comedi_device *dev, comedi_subdevice *s)
1899 struct usbduxsub *this_usbduxsub = dev->private;
1902 /* unlink only if it is really running */
1903 res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1905 dev_dbg(&this_usbduxsub->interface->dev,
1906 "comedi %d: sending pwm off command to the usb device.\n",
1908 res = send_dux_commands(this_usbduxsub, SENDPWMOFF);
1915 static void usbduxsub_pwm_irq(struct urb *urb)
1918 struct usbduxsub *this_usbduxsub;
1919 comedi_device *this_comedidev;
1920 comedi_subdevice *s;
1922 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1924 /* the context variable points to the subdevice */
1925 this_comedidev = urb->context;
1926 /* the private structure of the subdevice is struct usbduxsub */
1927 this_usbduxsub = this_comedidev->private;
1929 s = this_comedidev->subdevices + SUBDEV_DA;
1931 switch (urb->status) {
1941 * after an unlink command, unplug, ... etc
1942 * no unlink needed here. Already shutting down.
1944 if (this_usbduxsub->pwm_cmd_running)
1945 usbdux_pwm_stop(this_usbduxsub, 0);
1951 if (this_usbduxsub->pwm_cmd_running) {
1952 dev_err(&this_usbduxsub->interface->dev,
1953 "comedi_: Non-zero urb status received in "
1954 "pwm intr context: %d\n", urb->status);
1955 usbdux_pwm_stop(this_usbduxsub, 0);
1960 /* are we actually running? */
1961 if (!(this_usbduxsub->pwm_cmd_running))
1964 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1965 urb->dev = this_usbduxsub->usbdev;
1967 if (this_usbduxsub->pwm_cmd_running) {
1968 ret = usb_submit_urb(urb, GFP_ATOMIC);
1970 dev_err(&this_usbduxsub->interface->dev,
1971 "comedi_: pwm urb resubm failed in int-cont. "
1973 if (ret == EL2NSYNC)
1974 dev_err(&this_usbduxsub->interface->dev,
1975 "buggy USB host controller or bug in "
1978 /* don't do an unlink here */
1979 usbdux_pwm_stop(this_usbduxsub, 0);
1984 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
1991 dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
1993 /* in case of a resubmission after an unlink... */
1994 usb_fill_bulk_urb(usbduxsub->urbPwm,
1996 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
1997 usbduxsub->urbPwm->transfer_buffer,
1998 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq, usbduxsub->comedidev);
2000 errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
2002 dev_err(&usbduxsub->interface->dev,
2003 "comedi_: usbdux: pwm: usb_submit_urb error %d\n",
2010 static int usbdux_pwm_period(comedi_device *dev, comedi_subdevice *s,
2013 struct usbduxsub *this_usbduxsub = dev->private;
2016 if (period < MIN_PWM_PERIOD) {
2017 dev_err(&this_usbduxsub->interface->dev,
2018 "comedi%d: illegal period setting for pwm.\n",
2022 fx2delay = period / ((int)(6*512*(1.0/0.033))) - 6;
2023 if (fx2delay > 255) {
2024 dev_err(&this_usbduxsub->interface->dev,
2025 "comedi%d: period %d for pwm is too low.\n",
2026 dev->minor, period);
2030 this_usbduxsub->pwmDelay = fx2delay;
2031 this_usbduxsub->pwmPeriod = period;
2032 dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
2033 __func__, period, fx2delay);
2037 /* is called from insn so there's no need to do all the sanity checks */
2038 static int usbdux_pwm_start(comedi_device *dev, comedi_subdevice *s)
2041 struct usbduxsub *this_usbduxsub = dev->private;
2043 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
2044 dev->minor, __func__);
2046 if (this_usbduxsub->pwm_cmd_running) {
2047 /* already running */
2051 this_usbduxsub->dux_commands[1] = ((int8_t) this_usbduxsub->pwmDelay);
2052 ret = send_dux_commands(this_usbduxsub, SENDPWMON);
2056 /* initalise the buffer */
2057 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
2058 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
2060 this_usbduxsub->pwm_cmd_running = 1;
2061 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
2063 this_usbduxsub->pwm_cmd_running = 0;
2069 /* generates the bit pattern for PWM with the optional sign bit */
2070 static int usbdux_pwm_pattern(comedi_device *dev, comedi_subdevice *s,
2071 int channel, lsampl_t value, lsampl_t sign)
2073 struct usbduxsub *this_usbduxsub = dev->private;
2080 if (!this_usbduxsub)
2083 /* this is the DIO bit which carries the PWM data */
2084 pwm_mask = (1 << channel);
2085 /* this is the DIO bit which carries the optional direction bit */
2086 sgn_mask = (16 << channel);
2087 /* this is the buffer which will be filled with the with bit */
2088 /* pattern for one period */
2089 szbuf = this_usbduxsub->sizePwmBuf;
2090 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2091 for (i = 0; i < szbuf; i++) {
2094 c = c & (~pwm_mask);
2095 /* set the bit as long as the index is lower than the value */
2098 /* set the optional sign bit for a relay */
2100 /* positive value */
2101 c = c & (~sgn_mask);
2103 /* negative value */
2111 static int usbdux_pwm_write(comedi_device *dev, comedi_subdevice *s,
2112 comedi_insn *insn, lsampl_t *data)
2114 struct usbduxsub *this_usbduxsub = dev->private;
2116 if (!this_usbduxsub)
2119 if ((insn->n) != 1) {
2121 * doesn't make sense to have more than one value here because
2122 * it would just overwrite the PWM buffer a couple of times
2128 * the sign is set via a special INSN only, this gives us 8 bits for
2130 * relay sign 0 by default
2132 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec),
2136 static int usbdux_pwm_read(comedi_device *x1, comedi_subdevice *x2,
2137 comedi_insn *x3, lsampl_t *x4)
2143 /* switches on/off PWM */
2144 static int usbdux_pwm_config(comedi_device *dev, comedi_subdevice *s,
2145 comedi_insn *insn, lsampl_t *data)
2147 struct usbduxsub *this_usbduxsub = dev->private;
2149 case INSN_CONFIG_ARM:
2151 dev_dbg(&this_usbduxsub->interface->dev,
2152 "comedi%d: %s: pwm on\n", dev->minor, __func__);
2154 * if not zero the PWM is limited to a certain time which is
2155 * not supported here
2159 return usbdux_pwm_start(dev, s);
2160 case INSN_CONFIG_DISARM:
2161 dev_dbg(&this_usbduxsub->interface->dev,
2162 "comedi%d: %s: pwm off\n", dev->minor, __func__);
2163 return usbdux_pwm_cancel(dev, s);
2164 case INSN_CONFIG_GET_PWM_STATUS:
2166 * to check if the USB transmission has failed or in case PWM
2167 * was limited to n cycles to check if it has terminated
2169 data[1] = this_usbduxsub->pwm_cmd_running;
2171 case INSN_CONFIG_PWM_SET_PERIOD:
2172 dev_dbg(&this_usbduxsub->interface->dev,
2173 "comedi%d: %s: setting period\n", dev->minor, __func__);
2174 return usbdux_pwm_period(dev, s, data[1]);
2175 case INSN_CONFIG_PWM_GET_PERIOD:
2176 data[1] = this_usbduxsub->pwmPeriod;
2178 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2179 /* value in the first byte and the sign in the second for a
2181 return usbdux_pwm_pattern(dev, s,
2182 /* the channel number */
2183 CR_CHAN(insn->chanspec),
2184 /* actual PWM data */
2188 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2189 /* values are not kept in this driver, nothing to return here */
2196 /*****************************************************************/
2198 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2204 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2206 /* shows the usb subsystem that the driver is down */
2207 if (usbduxsub_tmp->interface)
2208 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2210 usbduxsub_tmp->probed = 0;
2212 if (usbduxsub_tmp->urbIn) {
2213 if (usbduxsub_tmp->ai_cmd_running) {
2214 usbduxsub_tmp->ai_cmd_running = 0;
2215 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2217 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2218 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2219 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2220 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2221 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2222 usbduxsub_tmp->urbIn[i] = NULL;
2224 kfree(usbduxsub_tmp->urbIn);
2225 usbduxsub_tmp->urbIn = NULL;
2227 if (usbduxsub_tmp->urbOut) {
2228 if (usbduxsub_tmp->ao_cmd_running) {
2229 usbduxsub_tmp->ao_cmd_running = 0;
2230 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2232 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2233 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2234 kfree(usbduxsub_tmp->urbOut[i]->
2236 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2239 if (usbduxsub_tmp->urbOut[i]) {
2240 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2241 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2242 usbduxsub_tmp->urbOut[i] = NULL;
2245 kfree(usbduxsub_tmp->urbOut);
2246 usbduxsub_tmp->urbOut = NULL;
2248 if (usbduxsub_tmp->urbPwm) {
2249 if (usbduxsub_tmp->pwm_cmd_running) {
2250 usbduxsub_tmp->pwm_cmd_running = 0;
2251 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2253 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2254 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2255 usb_kill_urb(usbduxsub_tmp->urbPwm);
2256 usb_free_urb(usbduxsub_tmp->urbPwm);
2257 usbduxsub_tmp->urbPwm = NULL;
2259 kfree(usbduxsub_tmp->inBuffer);
2260 usbduxsub_tmp->inBuffer = NULL;
2261 kfree(usbduxsub_tmp->insnBuffer);
2262 usbduxsub_tmp->insnBuffer = NULL;
2263 kfree(usbduxsub_tmp->inBuffer);
2264 usbduxsub_tmp->inBuffer = NULL;
2265 kfree(usbduxsub_tmp->dac_commands);
2266 usbduxsub_tmp->dac_commands = NULL;
2267 kfree(usbduxsub_tmp->dux_commands);
2268 usbduxsub_tmp->dux_commands = NULL;
2269 usbduxsub_tmp->ai_cmd_running = 0;
2270 usbduxsub_tmp->ao_cmd_running = 0;
2271 usbduxsub_tmp->pwm_cmd_running = 0;
2274 static unsigned hex2unsigned(char *h)
2279 hi = h[0] - 'A' + 0x0a;
2284 lo = h[1] - 'A' + 0x0a;
2288 return hi * 0x10 + lo;
2292 #define FIRMWARE_MAX_LEN 0x2000
2294 /* taken from David Brownell's fxload and adjusted for this driver */
2295 static int read_firmware(struct usbduxsub *usbduxsub, void *firmwarePtr,
2298 struct device *dev = &usbduxsub->interface->dev;
2300 unsigned char *fp = (char *)firmwarePtr;
2301 unsigned char *firmwareBinary = NULL;
2305 firmwareBinary = kzalloc(FIRMWARE_MAX_LEN, GFP_KERNEL);
2306 if (!firmwareBinary) {
2307 dev_err(dev, "comedi_: mem alloc for firmware failed\n");
2319 while ((i < size) && (fp[i] != 13) && (fp[i] != 10)) {
2323 if (j >= sizeof(buf)) {
2324 dev_err(dev, "comedi_: bogus firmware file!\n");
2328 /* get rid of LF/CR/... */
2329 while ((i < size) && ((fp[i] == 13) || (fp[i] == 10)
2335 /* dev_dbg(dev, "comedi_: buf=%s\n", buf); */
2339 * "# comment-till-end-of-line", for copyrights etc
2344 if (buf[0] != ':') {
2345 dev_err(dev, "comedi_: upload: not an ihex record: %s",
2350 /* Read the length field (up to 16 bytes) */
2351 len = hex2unsigned(buf + 1);
2353 /* Read the target offset */
2354 off = (hex2unsigned(buf + 3) * 0x0100) + hex2unsigned(buf + 5);
2356 if ((off + len) > maxAddr)
2357 maxAddr = off + len;
2360 if (maxAddr >= FIRMWARE_MAX_LEN) {
2361 dev_err(dev, "comedi_: firmware upload goes "
2362 "beyond FX2 RAM boundaries.\n");
2365 /* dev_dbg(dev, "comedi_: off=%x, len=%x:\n", off, len); */
2367 /* Read the record type */
2368 type = hex2unsigned(buf + 7);
2370 /* If this is an EOF record, then make it so. */
2376 dev_err(dev, "comedi_: unsupported record type: %u\n",
2381 for (idx = 0, cp = buf + 9; idx < len; idx += 1, cp += 2) {
2382 firmwareBinary[idx + off] = hex2unsigned(cp);
2383 /*printk("%02x ",firmwareBinary[idx+off]); */
2388 dev_err(dev, "comedi_: unexpected end of hex file\n");
2393 res = firmwareUpload(usbduxsub, firmwareBinary, maxAddr + 1);
2394 kfree(firmwareBinary);
2398 /* allocate memory for the urbs and initialise them */
2399 static int usbduxsub_probe(struct usb_interface *uinterf,
2400 const struct usb_device_id *id)
2402 struct usb_device *udev = interface_to_usbdev(uinterf);
2403 struct device *dev = &uinterf->dev;
2407 dev_dbg(dev, "comedi_: usbdux_: "
2408 "finding a free structure for the usb-device\n");
2410 down(&start_stop_sem);
2411 /* look for a free place in the usbdux array */
2413 for (i = 0; i < NUMUSBDUX; i++) {
2414 if (!(usbduxsub[i].probed)) {
2422 dev_err(dev, "Too many usbdux-devices connected.\n");
2423 up(&start_stop_sem);
2426 dev_dbg(dev, "comedi_: usbdux: "
2427 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2429 init_MUTEX(&(usbduxsub[index].sem));
2430 /* save a pointer to the usb device */
2431 usbduxsub[index].usbdev = udev;
2433 /* 2.6: save the interface itself */
2434 usbduxsub[index].interface = uinterf;
2435 /* get the interface number from the interface */
2436 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2437 /* hand the private data over to the usb subsystem */
2438 /* will be needed for disconnect */
2439 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2441 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2443 /* test if it is high speed (USB 2.0) */
2444 usbduxsub[index].high_speed =
2445 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2447 /* create space for the commands of the DA converter */
2448 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2449 if (!usbduxsub[index].dac_commands) {
2450 dev_err(dev, "comedi_: usbdux: "
2451 "error alloc space for dac commands\n");
2452 tidy_up(&(usbduxsub[index]));
2453 up(&start_stop_sem);
2456 /* create space for the commands going to the usb device */
2457 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2458 if (!usbduxsub[index].dux_commands) {
2459 dev_err(dev, "comedi_: usbdux: "
2460 "error alloc space for dac commands\n");
2461 tidy_up(&(usbduxsub[index]));
2462 up(&start_stop_sem);
2465 /* create space for the in buffer and set it to zero */
2466 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2467 if (!(usbduxsub[index].inBuffer)) {
2468 dev_err(dev, "comedi_: usbdux: "
2469 "could not alloc space for inBuffer\n");
2470 tidy_up(&(usbduxsub[index]));
2471 up(&start_stop_sem);
2474 /* create space of the instruction buffer */
2475 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2476 if (!(usbduxsub[index].insnBuffer)) {
2477 dev_err(dev, "comedi_: usbdux: "
2478 "could not alloc space for insnBuffer\n");
2479 tidy_up(&(usbduxsub[index]));
2480 up(&start_stop_sem);
2483 /* create space for the outbuffer */
2484 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2485 if (!(usbduxsub[index].outBuffer)) {
2486 dev_err(dev, "comedi_: usbdux: "
2487 "could not alloc space for outBuffer\n");
2488 tidy_up(&(usbduxsub[index]));
2489 up(&start_stop_sem);
2492 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2493 i = usb_set_interface(usbduxsub[index].usbdev,
2494 usbduxsub[index].ifnum, 3);
2496 dev_err(dev, "comedi_: usbdux%d: "
2497 "could not set alternate setting 3 in high speed.\n",
2499 tidy_up(&(usbduxsub[index]));
2500 up(&start_stop_sem);
2503 if (usbduxsub[index].high_speed)
2504 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2506 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2508 usbduxsub[index].urbIn =
2509 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2511 if (!(usbduxsub[index].urbIn)) {
2512 dev_err(dev, "comedi_: usbdux: Could not alloc. urbIn array\n");
2513 tidy_up(&(usbduxsub[index]));
2514 up(&start_stop_sem);
2517 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2518 /* one frame: 1ms */
2519 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2520 if (usbduxsub[index].urbIn[i] == NULL) {
2521 dev_err(dev, "comedi_: usbdux%d: "
2522 "Could not alloc. urb(%d)\n", index, i);
2523 tidy_up(&(usbduxsub[index]));
2524 up(&start_stop_sem);
2527 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2528 /* will be filled later with a pointer to the comedi-device */
2529 /* and ONLY then the urb should be submitted */
2530 usbduxsub[index].urbIn[i]->context = NULL;
2531 usbduxsub[index].urbIn[i]->pipe =
2532 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2533 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2534 usbduxsub[index].urbIn[i]->transfer_buffer =
2535 kzalloc(SIZEINBUF, GFP_KERNEL);
2536 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2537 dev_err(dev, "comedi_: usbdux%d: "
2538 "could not alloc. transb.\n", index);
2539 tidy_up(&(usbduxsub[index]));
2540 up(&start_stop_sem);
2543 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2544 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2545 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2546 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2547 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length = SIZEINBUF;
2551 if (usbduxsub[index].high_speed)
2552 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2554 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2556 usbduxsub[index].urbOut =
2557 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2559 if (!(usbduxsub[index].urbOut)) {
2560 dev_err(dev, "comedi_: usbdux: "
2561 "Could not alloc. urbOut array\n");
2562 tidy_up(&(usbduxsub[index]));
2563 up(&start_stop_sem);
2566 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2567 /* one frame: 1ms */
2568 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2569 if (usbduxsub[index].urbOut[i] == NULL) {
2570 dev_err(dev, "comedi_: usbdux%d: "
2571 "Could not alloc. urb(%d)\n", index, i);
2572 tidy_up(&(usbduxsub[index]));
2573 up(&start_stop_sem);
2576 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2577 /* will be filled later with a pointer to the comedi-device */
2578 /* and ONLY then the urb should be submitted */
2579 usbduxsub[index].urbOut[i]->context = NULL;
2580 usbduxsub[index].urbOut[i]->pipe =
2581 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2582 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2583 usbduxsub[index].urbOut[i]->transfer_buffer =
2584 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2585 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2586 dev_err(dev, "comedi_: usbdux%d: "
2587 "could not alloc. transb.\n", index);
2588 tidy_up(&(usbduxsub[index]));
2589 up(&start_stop_sem);
2592 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2593 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2594 usbduxsub[index].urbOut[i]->transfer_buffer_length = SIZEOUTBUF;
2595 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2596 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2598 if (usbduxsub[index].high_speed) {
2600 usbduxsub[index].urbOut[i]->interval = 8;
2603 usbduxsub[index].urbOut[i]->interval = 1;
2608 if (usbduxsub[index].high_speed) {
2609 /* max bulk ep size in high speed */
2610 usbduxsub[index].sizePwmBuf = 512;
2611 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2612 if (usbduxsub[index].urbPwm == NULL) {
2613 dev_err(dev, "comedi_: usbdux%d: "
2614 "Could not alloc. pwm urb\n", index);
2615 tidy_up(&(usbduxsub[index]));
2616 up(&start_stop_sem);
2619 usbduxsub[index].urbPwm->transfer_buffer =
2620 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2621 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2622 dev_err(dev, "comedi_: usbdux%d: "
2623 "could not alloc. transb. for pwm\n", index);
2624 tidy_up(&(usbduxsub[index]));
2625 up(&start_stop_sem);
2629 usbduxsub[index].urbPwm = NULL;
2630 usbduxsub[index].sizePwmBuf = 0;
2633 usbduxsub[index].ai_cmd_running = 0;
2634 usbduxsub[index].ao_cmd_running = 0;
2635 usbduxsub[index].pwm_cmd_running = 0;
2637 /* we've reached the bottom of the function */
2638 usbduxsub[index].probed = 1;
2639 up(&start_stop_sem);
2640 dev_info(dev, "comedi_: usbdux%d "
2641 "has been successfully initialised.\n", index);
2646 static void usbduxsub_disconnect(struct usb_interface *intf)
2648 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2649 struct usb_device *udev = interface_to_usbdev(intf);
2651 if (!usbduxsub_tmp) {
2653 "comedi_: disconnect called with null pointer.\n");
2656 if (usbduxsub_tmp->usbdev != udev) {
2658 "comedi_: BUG! called with wrong ptr!!!\n");
2661 down(&start_stop_sem);
2662 down(&usbduxsub_tmp->sem);
2663 tidy_up(usbduxsub_tmp);
2664 up(&usbduxsub_tmp->sem);
2665 up(&start_stop_sem);
2666 dev_dbg(&intf->dev, "comedi_: disconnected from the usb\n");
2669 /* is called when comedi-config is called */
2670 static int usbdux_attach(comedi_device *dev, comedi_devconfig *it)
2675 struct usbduxsub *udev;
2677 comedi_subdevice *s = NULL;
2678 dev->private = NULL;
2680 down(&start_stop_sem);
2681 /* find a valid device which has been detected by the probe function of
2684 for (i = 0; i < NUMUSBDUX; i++) {
2685 if ((usbduxsub[i].probed) && (!usbduxsub[i].attached)) {
2692 printk(KERN_ERR "comedi%d: usbdux: error: attach failed, no "
2693 "usbdux devs connected to the usb bus.\n", dev->minor);
2694 up(&start_stop_sem);
2698 udev = &usbduxsub[index];
2700 /* pointer back to the corresponding comedi device */
2701 udev->comedidev = dev;
2703 /* trying to upload the firmware into the chip */
2704 if (comedi_aux_data(it->options, 0) &&
2705 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
2706 read_firmware(udev, comedi_aux_data(it->options, 0),
2707 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
2710 dev->board_name = BOARDNAME;
2712 /* set number of subdevices */
2713 if (udev->high_speed) {
2715 dev->n_subdevices = 5;
2718 dev->n_subdevices = 4;
2721 /* allocate space for the subdevices */
2722 ret = alloc_subdevices(dev, dev->n_subdevices);
2724 dev_err(&udev->interface->dev,
2725 "comedi%d: error alloc space for subdev\n", dev->minor);
2726 up(&start_stop_sem);
2730 dev_info(&udev->interface->dev,
2731 "comedi%d: usb-device %d is attached to comedi.\n",
2733 /* private structure is also simply the usb-structure */
2734 dev->private = udev;
2736 /* the first subdevice is the A/D converter */
2737 s = dev->subdevices + SUBDEV_AD;
2738 /* the URBs get the comedi subdevice */
2739 /* which is responsible for reading */
2740 /* this is the subdevice which reads data */
2741 dev->read_subdev = s;
2742 /* the subdevice receives as private structure the */
2746 s->type = COMEDI_SUBD_AI;
2747 /* readable and ref is to ground */
2748 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
2751 /* length of the channellist */
2752 s->len_chanlist = 8;
2753 /* callback functions */
2754 s->insn_read = usbdux_ai_insn_read;
2755 s->do_cmdtest = usbdux_ai_cmdtest;
2756 s->do_cmd = usbdux_ai_cmd;
2757 s->cancel = usbdux_ai_cancel;
2758 /* max value from the A/D converter (12bit) */
2760 /* range table to convert to physical units */
2761 s->range_table = (&range_usbdux_ai_range);
2764 s = dev->subdevices + SUBDEV_DA;
2766 s->type = COMEDI_SUBD_AO;
2767 /* backward pointer */
2768 dev->write_subdev = s;
2769 /* the subdevice receives as private structure the */
2773 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2776 /* length of the channellist */
2777 s->len_chanlist = 4;
2778 /* 12 bit resolution */
2779 s->maxdata = 0x0fff;
2781 s->range_table = (&range_usbdux_ao_range);
2783 s->do_cmdtest = usbdux_ao_cmdtest;
2784 s->do_cmd = usbdux_ao_cmd;
2785 s->cancel = usbdux_ao_cancel;
2786 s->insn_read = usbdux_ao_insn_read;
2787 s->insn_write = usbdux_ao_insn_write;
2790 s = dev->subdevices + SUBDEV_DIO;
2791 s->type = COMEDI_SUBD_DIO;
2792 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2795 s->range_table = (&range_digital);
2796 s->insn_bits = usbdux_dio_insn_bits;
2797 s->insn_config = usbdux_dio_insn_config;
2798 /* we don't use it */
2802 s = dev->subdevices + SUBDEV_COUNTER;
2803 s->type = COMEDI_SUBD_COUNTER;
2804 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2806 s->maxdata = 0xFFFF;
2807 s->insn_read = usbdux_counter_read;
2808 s->insn_write = usbdux_counter_write;
2809 s->insn_config = usbdux_counter_config;
2811 if (udev->high_speed) {
2813 s = dev->subdevices + SUBDEV_PWM;
2814 s->type = COMEDI_SUBD_PWM;
2815 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2817 /* this defines the max duty cycle resolution */
2818 s->maxdata = udev->sizePwmBuf;
2819 s->insn_write = usbdux_pwm_write;
2820 s->insn_read = usbdux_pwm_read;
2821 s->insn_config = usbdux_pwm_config;
2822 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2824 /* finally decide that it's attached */
2829 up(&start_stop_sem);
2831 dev_info(&udev->interface->dev, "comedi%d: attached to usbdux.\n",
2837 static int usbdux_detach(comedi_device *dev)
2839 struct usbduxsub *usbduxsub_tmp;
2843 "comedi?: usbdux: detach without dev variable...\n");
2847 usbduxsub_tmp = dev->private;
2848 if (!usbduxsub_tmp) {
2850 "comedi?: usbdux: detach without ptr to usbduxsub[]\n");
2854 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi%d: detach usb device\n",
2857 down(&usbduxsub_tmp->sem);
2858 /* Don't allow detach to free the private structure */
2859 /* It's one entry of of usbduxsub[] */
2860 dev->private = NULL;
2861 usbduxsub_tmp->attached = 0;
2862 usbduxsub_tmp->comedidev = NULL;
2863 dev_dbg(&usbduxsub_tmp->interface->dev,
2864 "comedi%d: detach: successfully removed\n", dev->minor);
2865 up(&usbduxsub_tmp->sem);
2869 /* main driver struct */
2870 static comedi_driver driver_usbdux = {
2871 .driver_name = "usbdux",
2872 .module = THIS_MODULE,
2873 .attach = usbdux_attach,
2874 .detach = usbdux_detach,
2877 static void init_usb_devices(void)
2881 /* all devices entries are invalid to begin with */
2882 /* they will become valid by the probe function */
2883 /* and then finally by the attach-function */
2884 for (index = 0; index < NUMUSBDUX; index++) {
2885 memset(&(usbduxsub[index]), 0x00, sizeof(usbduxsub[index]));
2886 init_MUTEX(&(usbduxsub[index].sem));
2890 /* Table with the USB-devices: just now only testing IDs */
2891 static struct usb_device_id usbduxsub_table[] = {
2892 {USB_DEVICE(0x13d8, 0x0001) },
2893 {USB_DEVICE(0x13d8, 0x0002) },
2894 {} /* Terminating entry */
2897 MODULE_DEVICE_TABLE(usb, usbduxsub_table);
2899 /* The usbduxsub-driver */
2900 static struct usb_driver usbduxsub_driver = {
2902 .probe = usbduxsub_probe,
2903 .disconnect = usbduxsub_disconnect,
2904 .id_table = usbduxsub_table,
2907 /* Can't use the nice macro as I have also to initialise the USB */
2909 /* registering the usb-system _and_ the comedi-driver */
2910 static int init_usbdux(void)
2912 printk(KERN_INFO KBUILD_MODNAME ": "
2913 DRIVER_VERSION ":" DRIVER_DESC "\n");
2915 usb_register(&usbduxsub_driver);
2916 comedi_driver_register(&driver_usbdux);
2920 /* deregistering the comedi driver and the usb-subsystem */
2921 static void exit_usbdux(void)
2923 comedi_driver_unregister(&driver_usbdux);
2924 usb_deregister(&usbduxsub_driver);
2927 module_init(init_usbdux);
2928 module_exit(exit_usbdux);
2930 MODULE_AUTHOR(DRIVER_AUTHOR);
2931 MODULE_DESCRIPTION(DRIVER_DESC);
2932 MODULE_LICENSE("GPL");