2 comedi/drivers/usbdux.c
3 Copyright (C) 2003-2007 Bernd Porr, Bernd.Porr@f2s.com
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 Description: University of Stirling USB DAQ & INCITE Technology Limited
23 Devices: [ITL] USB-DUX (usbdux.o)
24 Author: Bernd Porr <BerndPorr@f2s.com>
27 Configuration options:
28 You have to upload firmware with the -i option. The
29 firmware is usually installed under /usr/share/usb or
30 /usr/local/share/usb or /lib/firmware.
32 Connection scheme for the counter at the digital port:
33 0=/CLK0, 1=UP/DOWN0, 2=RESET0, 4=/CLK1, 5=UP/DOWN1, 6=RESET1.
34 The sampling rate of the counter is approximately 500Hz.
36 Please note that under USB2.0 the length of the channel list determines
37 the max sampling rate. If you sample only one channel you get 8kHz
38 sampling rate. If you sample two channels you get 4kHz and so on.
41 * I must give credit here to Chris Baugher who
42 * wrote the driver for AT-MIO-16d. I used some parts of this
43 * driver. I also must give credits to David Brownell
44 * who supported me with the USB development.
50 * 0.94: D/A output should work now with any channel list combinations
51 * 0.95: .owner commented out for kernel vers below 2.4.19
52 * sanity checks in ai/ao_cmd
53 * 0.96: trying to get it working with 2.6, moved all memory alloc to comedi's
54 * attach final USB IDs
55 * moved memory allocation completely to the corresponding comedi
56 * functions firmware upload is by fxload and no longer by comedi (due to
58 * 0.97: USB IDs received, adjusted table
59 * 0.98: SMP, locking, memroy alloc: moved all usb memory alloc
60 * to the usb subsystem and moved all comedi related memory
62 * | kernel | registration | usbdux-usb | usbdux-comedi | comedi |
63 * 0.99: USB 2.0: changed protocol to isochronous transfer
64 * IRQ transfer is too buggy and too risky in 2.0
65 * for the high speed ISO transfer is now a working version
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
71 * Firmware vers 1.00 is needed for this.
72 * Two 16 bit up/down/reset counter with a sampling rate of 1kHz
73 * And loads of cleaning up, in particular streamlining the
75 * 1.1: moved EP4 transfers to EP1 to make space for a PWM output on EP4
76 * 1.2: added PWM suport via EP4
77 * 2.0: PWM seems to be stable and is not interfering with the other functions
78 * 2.1: changed PWM API
79 * 2.2: added firmware kernel request to fix an udev problem
80 * 2.3: corrected a bug in bulk timeouts which were far too short
81 * 2.4: fixed a bug which causes the driver to hang when it ran out of data.
82 * Thanks to Jan-Matthias Braun and Ian to spot the bug and fix it.
86 /* generates loads of debug info */
87 /* #define NOISY_DUX_DEBUGBUG */
89 #include <linux/kernel.h>
90 #include <linux/module.h>
91 #include <linux/init.h>
92 #include <linux/slab.h>
93 #include <linux/input.h>
94 #include <linux/usb.h>
95 #include <linux/fcntl.h>
96 #include <linux/compiler.h>
97 #include <linux/firmware.h>
99 #include "../comedidev.h"
101 #include "comedi_fc.h"
103 /* timeout for the USB-transfer in ms*/
104 #define BULK_TIMEOUT 1000
106 /* constants for "firmware" upload and download */
107 #define FIRMWARE "usbdux_firmware.bin"
108 #define USBDUXSUB_FIRMWARE 0xA0
109 #define VENDOR_DIR_IN 0xC0
110 #define VENDOR_DIR_OUT 0x40
112 /* internal addresses of the 8051 processor */
113 #define USBDUXSUB_CPUCS 0xE600
116 * the minor device number, major is 180 only for debugging purposes and to
117 * upload special firmware (programming the eeprom etc) which is not compatible
118 * with the comedi framwork
120 #define USBDUXSUB_MINOR 32
122 /* max lenghth of the transfer-buffer for software upload */
123 #define TB_LEN 0x2000
125 /* Input endpoint number: ISO/IRQ */
128 /* Output endpoint number: ISO/IRQ */
131 /* This EP sends DUX commands to USBDUX */
132 #define COMMAND_OUT_EP 1
134 /* This EP receives the DUX commands from USBDUX */
135 #define COMMAND_IN_EP 8
137 /* Output endpoint for PWM */
140 /* 300Hz max frequ under PWM */
141 #define MIN_PWM_PERIOD ((long)(1E9/300))
143 /* Default PWM frequency */
144 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
146 /* Number of channels */
147 #define NUMCHANNELS 8
149 /* Size of one A/D value */
150 #define SIZEADIN ((sizeof(int16_t)))
153 * Size of the input-buffer IN BYTES
154 * Always multiple of 8 for 8 microframes which is needed in the highspeed mode
156 #define SIZEINBUF ((8*SIZEADIN))
159 #define SIZEINSNBUF 16
161 /* Number of DA channels */
162 #define NUMOUTCHANNELS 8
164 /* size of one value for the D/A converter: channel and value */
165 #define SIZEDAOUT ((sizeof(int8_t)+sizeof(int16_t)))
168 * Size of the output-buffer in bytes
169 * Actually only the first 4 triplets are used but for the
170 * high speed mode we need to pad it to 8 (microframes).
172 #define SIZEOUTBUF ((8*SIZEDAOUT))
175 * Size of the buffer for the dux commands: just now max size is determined
176 * by the analogue out + command byte + panic bytes...
178 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
180 /* Number of in-URBs which receive the data: min=2 */
181 #define NUMOFINBUFFERSFULL 5
183 /* Number of out-URBs which send the data: min=2 */
184 #define NUMOFOUTBUFFERSFULL 5
186 /* Number of in-URBs which receive the data: min=5 */
187 /* must have more buffers due to buggy USB ctr */
188 #define NUMOFINBUFFERSHIGH 10
190 /* Number of out-URBs which send the data: min=5 */
191 /* must have more buffers due to buggy USB ctr */
192 #define NUMOFOUTBUFFERSHIGH 10
194 /* Total number of usbdux devices */
197 /* Analogue in subdevice */
200 /* Analogue out subdevice */
207 #define SUBDEV_COUNTER 3
209 /* timer aka pwm output */
212 /* number of retries to get the right dux command */
215 /**************************************************/
216 /* comedi constants */
217 static const struct comedi_lrange range_usbdux_ai_range = { 4, {
229 static const struct comedi_lrange range_usbdux_ao_range = { 2, {
238 * private structure of one subdevice
242 * This is the structure which holds all the data of
243 * this driver one sub device just now: A/D
248 /* is it associated with a subdevice? */
250 /* pointer to the usb-device */
251 struct usb_device *usbdev;
252 /* actual number of in-buffers */
254 /* actual number of out-buffers */
256 /* ISO-transfer handling: buffers */
259 /* pwm-transfer handling */
262 unsigned int pwmPeriod;
263 /* PWM internal delay for the GPIF in the FX2 */
265 /* size of the PWM buffer which holds the bit pattern */
267 /* input buffer for the ISO-transfer */
269 /* input buffer for single insn */
271 /* output buffer for single DA outputs */
273 /* interface number */
275 /* interface structure in 2.6 */
276 struct usb_interface *interface;
277 /* comedi device for the interrupt context */
278 struct comedi_device *comedidev;
279 /* is it USB_SPEED_HIGH or not? */
280 short int high_speed;
281 /* asynchronous command is running */
282 short int ai_cmd_running;
283 short int ao_cmd_running;
285 short int pwm_cmd_running;
286 /* continous acquisition */
287 short int ai_continous;
288 short int ao_continous;
289 /* number of samples to acquire */
292 /* time between samples in units of the timer */
293 unsigned int ai_timer;
294 unsigned int ao_timer;
295 /* counter between aquisitions */
296 unsigned int ai_counter;
297 unsigned int ao_counter;
298 /* interval in frames/uframes */
299 unsigned int ai_interval;
301 int8_t *dac_commands;
303 int8_t *dux_commands;
304 struct semaphore sem;
308 * The pointer to the private usb-data of the driver is also the private data
309 * for the comedi-device. This has to be global as the usb subsystem needs
310 * global variables. The other reason is that this structure must be there
311 * _before_ any comedi command is issued. The usb subsystem must be initialised
312 * before comedi can access it.
314 static struct usbduxsub usbduxsub[NUMUSBDUX];
316 static DEFINE_SEMAPHORE(start_stop_sem);
319 * Stops the data acquision
320 * It should be safe to call this function from any context
322 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
327 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
328 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
329 if (usbduxsub_tmp->urbIn[i]) {
330 /* We wait here until all transfers have been
332 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
334 dev_dbg(&usbduxsub_tmp->interface->dev,
335 "comedi: usbdux: unlinked InURB %d, err=%d\n",
343 * This will stop a running acquisition operation
344 * Is called from within this driver from both the
345 * interrupt context and from comedi
347 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
351 if (!this_usbduxsub) {
352 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
355 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
359 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
362 this_usbduxsub->ai_cmd_running = 0;
368 * This will cancel a running acquisition operation.
369 * This is called by comedi but never from inside the driver.
371 static int usbdux_ai_cancel(struct comedi_device *dev,
372 struct comedi_subdevice *s)
374 struct usbduxsub *this_usbduxsub;
377 /* force unlink of all urbs */
378 this_usbduxsub = dev->private;
382 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
384 /* prevent other CPUs from submitting new commands just now */
385 down(&this_usbduxsub->sem);
386 if (!(this_usbduxsub->probed)) {
387 up(&this_usbduxsub->sem);
390 /* unlink only if the urb really has been submitted */
391 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
392 up(&this_usbduxsub->sem);
396 /* analogue IN - interrupt service routine */
397 static void usbduxsub_ai_IsocIrq(struct urb *urb)
400 struct usbduxsub *this_usbduxsub;
401 struct comedi_device *this_comedidev;
402 struct comedi_subdevice *s;
404 /* the context variable points to the subdevice */
405 this_comedidev = urb->context;
406 /* the private structure of the subdevice is struct usbduxsub */
407 this_usbduxsub = this_comedidev->private;
408 /* subdevice which is the AD converter */
409 s = &this_comedidev->subdevices[SUBDEV_AD];
411 /* first we test if something unusual has just happened */
412 switch (urb->status) {
414 /* copy the result in the transfer buffer */
415 memcpy(this_usbduxsub->inBuffer,
416 urb->transfer_buffer, SIZEINBUF);
419 /* error in the ISOchronous data */
420 /* we don't copy the data into the transfer buffer */
421 /* and recycle the last data byte */
422 dev_dbg(&urb->dev->dev,
423 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
424 this_usbduxsub->comedidev->minor);
432 /* happens after an unlink command */
433 if (this_usbduxsub->ai_cmd_running) {
434 /* we are still running a command */
435 /* tell this comedi */
436 s->async->events |= COMEDI_CB_EOA;
437 s->async->events |= COMEDI_CB_ERROR;
438 comedi_event(this_usbduxsub->comedidev, s);
439 /* stop the transfer w/o unlink */
440 usbdux_ai_stop(this_usbduxsub, 0);
445 /* a real error on the bus */
446 /* pass error to comedi if we are really running a command */
447 if (this_usbduxsub->ai_cmd_running) {
448 dev_err(&urb->dev->dev,
449 "Non-zero urb status received in ai intr "
450 "context: %d\n", urb->status);
451 s->async->events |= COMEDI_CB_EOA;
452 s->async->events |= COMEDI_CB_ERROR;
453 comedi_event(this_usbduxsub->comedidev, s);
454 /* don't do an unlink here */
455 usbdux_ai_stop(this_usbduxsub, 0);
461 * at this point we are reasonably sure that nothing dodgy has happened
462 * are we running a command?
464 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
466 * not running a command, do not continue execution if no
467 * asynchronous command is running in particular not resubmit
472 urb->dev = this_usbduxsub->usbdev;
474 /* resubmit the urb */
475 err = usb_submit_urb(urb, GFP_ATOMIC);
476 if (unlikely(err < 0)) {
477 dev_err(&urb->dev->dev,
478 "comedi_: urb resubmit failed in int-context! err=%d\n",
480 if (err == -EL2NSYNC)
481 dev_err(&urb->dev->dev,
482 "buggy USB host controller or bug in IRQ "
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 continuous, 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->inBuffer[i]) ^ 0x800);
524 le16_to_cpu(this_usbduxsub->inBuffer[i]));
526 if (unlikely(err == 0)) {
527 /* buffer overflow */
528 usbdux_ai_stop(this_usbduxsub, 0);
532 /* tell comedi that data is there */
533 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
534 comedi_event(this_usbduxsub->comedidev, s);
537 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
542 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
543 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
544 if (usbduxsub_tmp->urbOut[i])
545 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
547 dev_dbg(&usbduxsub_tmp->interface->dev,
548 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
555 /* This will cancel a running acquisition operation
558 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
564 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
567 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
569 this_usbduxsub->ao_cmd_running = 0;
574 /* force unlink, is called by comedi */
575 static int usbdux_ao_cancel(struct comedi_device *dev,
576 struct comedi_subdevice *s)
578 struct usbduxsub *this_usbduxsub = dev->private;
584 /* prevent other CPUs from submitting a command just now */
585 down(&this_usbduxsub->sem);
586 if (!(this_usbduxsub->probed)) {
587 up(&this_usbduxsub->sem);
590 /* unlink only if it is really running */
591 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
592 up(&this_usbduxsub->sem);
596 static void usbduxsub_ao_IsocIrq(struct urb *urb)
600 struct usbduxsub *this_usbduxsub;
601 struct comedi_device *this_comedidev;
602 struct comedi_subdevice *s;
604 /* the context variable points to the subdevice */
605 this_comedidev = urb->context;
606 /* the private structure of the subdevice is struct usbduxsub */
607 this_usbduxsub = this_comedidev->private;
609 s = &this_comedidev->subdevices[SUBDEV_DA];
611 switch (urb->status) {
620 /* after an unlink command, unplug, ... etc */
621 /* no unlink needed here. Already shutting down. */
622 if (this_usbduxsub->ao_cmd_running) {
623 s->async->events |= COMEDI_CB_EOA;
624 comedi_event(this_usbduxsub->comedidev, s);
625 usbdux_ao_stop(this_usbduxsub, 0);
631 if (this_usbduxsub->ao_cmd_running) {
632 dev_err(&urb->dev->dev,
633 "comedi_: Non-zero urb status received in ao "
634 "intr context: %d\n", urb->status);
635 s->async->events |= COMEDI_CB_ERROR;
636 s->async->events |= COMEDI_CB_EOA;
637 comedi_event(this_usbduxsub->comedidev, s);
638 /* we do an unlink if we are in the high speed mode */
639 usbdux_ao_stop(this_usbduxsub, 0);
644 /* are we actually running? */
645 if (!(this_usbduxsub->ao_cmd_running))
648 /* normal operation: executing a command in this subdevice */
649 this_usbduxsub->ao_counter--;
650 if ((int)this_usbduxsub->ao_counter <= 0) {
652 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
654 /* handle non continous acquisition */
655 if (!(this_usbduxsub->ao_continous)) {
656 /* fixed number of samples */
657 this_usbduxsub->ao_sample_count--;
658 if (this_usbduxsub->ao_sample_count < 0) {
659 /* all samples transmitted */
660 usbdux_ao_stop(this_usbduxsub, 0);
661 s->async->events |= COMEDI_CB_EOA;
662 comedi_event(this_usbduxsub->comedidev, s);
663 /* no resubmit of the urb */
667 /* transmit data to the USB bus */
668 ((uint8_t *) (urb->transfer_buffer))[0] =
669 s->async->cmd.chanlist_len;
670 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
672 if (i >= NUMOUTCHANNELS)
675 /* pointer to the DA */
677 (&(((int8_t *) urb->transfer_buffer)[i * 3 + 1]));
678 /* get the data from comedi */
679 ret = comedi_buf_get(s->async, &temp);
681 datap[1] = temp >> 8;
682 datap[2] = this_usbduxsub->dac_commands[i];
683 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
684 /* datap[0],datap[1],datap[2]); */
686 dev_err(&urb->dev->dev,
687 "comedi: buffer underflow\n");
688 s->async->events |= COMEDI_CB_EOA;
689 s->async->events |= COMEDI_CB_OVERFLOW;
691 /* transmit data to comedi */
692 s->async->events |= COMEDI_CB_BLOCK;
693 comedi_event(this_usbduxsub->comedidev, s);
696 urb->transfer_buffer_length = SIZEOUTBUF;
697 urb->dev = this_usbduxsub->usbdev;
699 if (this_usbduxsub->ao_cmd_running) {
700 if (this_usbduxsub->high_speed) {
707 urb->number_of_packets = 1;
708 urb->iso_frame_desc[0].offset = 0;
709 urb->iso_frame_desc[0].length = SIZEOUTBUF;
710 urb->iso_frame_desc[0].status = 0;
711 ret = usb_submit_urb(urb, GFP_ATOMIC);
713 dev_err(&urb->dev->dev,
714 "comedi_: ao urb resubm failed in int-cont. "
717 dev_err(&urb->dev->dev,
718 "buggy USB host controller or bug in "
721 s->async->events |= COMEDI_CB_EOA;
722 s->async->events |= COMEDI_CB_ERROR;
723 comedi_event(this_usbduxsub->comedidev, s);
724 /* don't do an unlink here */
725 usbdux_ao_stop(this_usbduxsub, 0);
730 static int usbduxsub_start(struct usbduxsub *usbduxsub)
733 uint8_t local_transfer_buffer[16];
736 local_transfer_buffer[0] = 0;
737 errcode = usb_control_msg(usbduxsub->usbdev,
738 /* create a pipe for a control transfer */
739 usb_sndctrlpipe(usbduxsub->usbdev, 0),
740 /* bRequest, "Firmware" */
748 /* address of the transfer buffer */
749 local_transfer_buffer,
755 dev_err(&usbduxsub->interface->dev,
756 "comedi_: control msg failed (start)\n");
762 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
766 uint8_t local_transfer_buffer[16];
769 local_transfer_buffer[0] = 1;
770 errcode = usb_control_msg(usbduxsub->usbdev,
771 usb_sndctrlpipe(usbduxsub->usbdev, 0),
772 /* bRequest, "Firmware" */
779 0x0000, local_transfer_buffer,
785 dev_err(&usbduxsub->interface->dev,
786 "comedi_: control msg failed (stop)\n");
792 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
793 uint8_t *local_transfer_buffer,
794 unsigned int startAddr, unsigned int len)
798 errcode = usb_control_msg(usbduxsub->usbdev,
799 usb_sndctrlpipe(usbduxsub->usbdev, 0),
800 /* brequest, firmware */
808 /* our local safe buffer */
809 local_transfer_buffer,
814 dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
816 dev_err(&usbduxsub->interface->dev, "comedi_: upload failed\n");
822 #define FIRMWARE_MAX_LEN 0x2000
824 static int firmwareUpload(struct usbduxsub *usbduxsub,
825 const u8 *firmwareBinary, int sizeFirmware)
833 if (sizeFirmware > FIRMWARE_MAX_LEN) {
834 dev_err(&usbduxsub->interface->dev,
835 "usbdux firmware binary it too large for FX2.\n");
839 /* we generate a local buffer for the firmware */
840 fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
842 dev_err(&usbduxsub->interface->dev,
843 "comedi_: mem alloc for firmware failed\n");
847 ret = usbduxsub_stop(usbduxsub);
849 dev_err(&usbduxsub->interface->dev,
850 "comedi_: can not stop firmware\n");
855 ret = usbduxsub_upload(usbduxsub, fwBuf, 0, sizeFirmware);
857 dev_err(&usbduxsub->interface->dev,
858 "comedi_: firmware upload failed\n");
862 ret = usbduxsub_start(usbduxsub);
864 dev_err(&usbduxsub->interface->dev,
865 "comedi_: can not start firmware\n");
873 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
880 /* Submit all URBs and start the transfer on the bus */
881 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
882 /* in case of a resubmission after an unlink... */
883 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
884 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
885 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
886 usbduxsub->urbIn[i]->status = 0;
887 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
888 dev_dbg(&usbduxsub->interface->dev,
889 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
890 usbduxsub->comedidev->minor, i,
891 (usbduxsub->urbIn[i]->context),
892 (usbduxsub->urbIn[i]->dev),
893 (usbduxsub->urbIn[i]->interval));
894 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
896 dev_err(&usbduxsub->interface->dev,
897 "comedi_: ai: usb_submit_urb(%d) error %d\n",
905 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
912 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
913 dev_dbg(&usbduxsub->interface->dev,
914 "comedi_: submitting out-urb[%d]\n", i);
915 /* in case of a resubmission after an unlink... */
916 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
917 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
918 usbduxsub->urbOut[i]->status = 0;
919 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
920 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
922 dev_err(&usbduxsub->interface->dev,
923 "comedi_: ao: usb_submit_urb(%d) error %d\n",
931 static int usbdux_ai_cmdtest(struct comedi_device *dev,
932 struct comedi_subdevice *s, struct comedi_cmd *cmd)
934 struct usbduxsub *this_usbduxsub = dev->private;
936 unsigned int tmpTimer;
938 if (!(this_usbduxsub->probed))
941 dev_dbg(&this_usbduxsub->interface->dev,
942 "comedi%d: usbdux_ai_cmdtest\n", dev->minor);
944 /* Step 1 : check if triggers are trivially valid */
946 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
947 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
948 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
949 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
950 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
955 /* Step 2a : make sure trigger sources are unique */
957 err |= cfc_check_trigger_is_unique(cmd->start_src);
958 err |= cfc_check_trigger_is_unique(cmd->stop_src);
960 /* Step 2b : and mutually compatible */
965 /* step 3: make sure arguments are trivially compatible */
966 if (cmd->start_arg != 0) {
971 if (cmd->scan_begin_src == TRIG_FOLLOW) {
972 /* internal trigger */
973 if (cmd->scan_begin_arg != 0) {
974 cmd->scan_begin_arg = 0;
979 if (cmd->scan_begin_src == TRIG_TIMER) {
980 if (this_usbduxsub->high_speed) {
982 * In high speed mode microframes are possible.
983 * However, during one microframe we can roughly
984 * sample one channel. Thus, the more channels
985 * are in the channel list the more time we need.
988 /* find a power of 2 for the number of channels */
989 while (i < (cmd->chanlist_len))
992 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
993 cmd->scan_begin_arg = 1000000 / 8 * i;
996 /* now calc the real sampling rate with all the
999 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
1001 if (cmd->scan_begin_arg != tmpTimer) {
1002 cmd->scan_begin_arg = tmpTimer;
1007 /* 1kHz scans every USB frame */
1008 if (cmd->scan_begin_arg < 1000000) {
1009 cmd->scan_begin_arg = 1000000;
1013 * calc the real sampling rate with the rounding errors
1015 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
1016 1000000)) * 1000000;
1017 if (cmd->scan_begin_arg != tmpTimer) {
1018 cmd->scan_begin_arg = tmpTimer;
1023 /* the same argument */
1024 if (cmd->scan_end_arg != cmd->chanlist_len) {
1025 cmd->scan_end_arg = cmd->chanlist_len;
1029 if (cmd->stop_src == TRIG_COUNT) {
1030 /* any count is allowed */
1033 if (cmd->stop_arg != 0) {
1046 * creates the ADC command for the MAX1271
1047 * range is the range value from comedi
1049 static int8_t create_adc_command(unsigned int chan, int range)
1051 int8_t p = (range <= 1);
1052 int8_t r = ((range % 2) == 0);
1053 return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
1056 /* bulk transfers to usbdux */
1058 #define SENDADCOMMANDS 0
1059 #define SENDDACOMMANDS 1
1060 #define SENDDIOCONFIGCOMMAND 2
1061 #define SENDDIOBITSCOMMAND 3
1062 #define SENDSINGLEAD 4
1063 #define READCOUNTERCOMMAND 5
1064 #define WRITECOUNTERCOMMAND 6
1066 #define SENDPWMOFF 8
1068 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1072 this_usbduxsub->dux_commands[0] = cmd_type;
1073 #ifdef NOISY_DUX_DEBUGBUG
1074 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1075 this_usbduxsub->comedidev->minor);
1076 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1077 printk(" %02x", this_usbduxsub->dux_commands[result]);
1080 result = usb_bulk_msg(this_usbduxsub->usbdev,
1081 usb_sndbulkpipe(this_usbduxsub->usbdev,
1083 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1084 &nsent, BULK_TIMEOUT);
1086 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1087 "could not transmit dux_command to the usb-device, "
1088 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1093 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1095 int result = (-EFAULT);
1099 for (i = 0; i < RETRIES; i++) {
1100 result = usb_bulk_msg(this_usbduxsub->usbdev,
1101 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1103 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1104 &nrec, BULK_TIMEOUT);
1106 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1107 "insn: USB error %d while receiving DUX command"
1108 "\n", this_usbduxsub->comedidev->minor, result);
1111 if (le16_to_cpu(this_usbduxsub->insnBuffer[0]) == command)
1114 /* this is only reached if the data has been requested a couple of
1116 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1117 "wrong data returned from firmware: want cmd %d, got cmd %d.\n",
1118 this_usbduxsub->comedidev->minor, command,
1119 le16_to_cpu(this_usbduxsub->insnBuffer[0]));
1123 static int usbdux_ai_inttrig(struct comedi_device *dev,
1124 struct comedi_subdevice *s, unsigned int trignum)
1127 struct usbduxsub *this_usbduxsub = dev->private;
1128 if (!this_usbduxsub)
1131 down(&this_usbduxsub->sem);
1132 if (!(this_usbduxsub->probed)) {
1133 up(&this_usbduxsub->sem);
1136 dev_dbg(&this_usbduxsub->interface->dev,
1137 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1140 dev_err(&this_usbduxsub->interface->dev,
1141 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1143 up(&this_usbduxsub->sem);
1146 if (!(this_usbduxsub->ai_cmd_running)) {
1147 this_usbduxsub->ai_cmd_running = 1;
1148 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1150 dev_err(&this_usbduxsub->interface->dev,
1151 "comedi%d: usbdux_ai_inttrig: "
1152 "urbSubmit: err=%d\n", dev->minor, ret);
1153 this_usbduxsub->ai_cmd_running = 0;
1154 up(&this_usbduxsub->sem);
1157 s->async->inttrig = NULL;
1159 dev_err(&this_usbduxsub->interface->dev,
1160 "comedi%d: ai_inttrig but acqu is already running\n",
1163 up(&this_usbduxsub->sem);
1167 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1169 struct comedi_cmd *cmd = &s->async->cmd;
1170 unsigned int chan, range;
1172 struct usbduxsub *this_usbduxsub = dev->private;
1175 if (!this_usbduxsub)
1178 dev_dbg(&this_usbduxsub->interface->dev,
1179 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1181 /* block other CPUs from starting an ai_cmd */
1182 down(&this_usbduxsub->sem);
1184 if (!(this_usbduxsub->probed)) {
1185 up(&this_usbduxsub->sem);
1188 if (this_usbduxsub->ai_cmd_running) {
1189 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1190 "ai_cmd not possible. Another ai_cmd is running.\n",
1192 up(&this_usbduxsub->sem);
1195 /* set current channel of the running acquisition to zero */
1196 s->async->cur_chan = 0;
1198 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1199 for (i = 0; i < cmd->chanlist_len; ++i) {
1200 chan = CR_CHAN(cmd->chanlist[i]);
1201 range = CR_RANGE(cmd->chanlist[i]);
1202 if (i >= NUMCHANNELS) {
1203 dev_err(&this_usbduxsub->interface->dev,
1204 "comedi%d: channel list too long\n",
1208 this_usbduxsub->dux_commands[i + 2] =
1209 create_adc_command(chan, range);
1212 dev_dbg(&this_usbduxsub->interface->dev,
1213 "comedi %d: sending commands to the usb device: size=%u\n",
1214 dev->minor, NUMCHANNELS);
1216 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1218 up(&this_usbduxsub->sem);
1222 if (this_usbduxsub->high_speed) {
1224 * every channel gets a time window of 125us. Thus, if we
1225 * sample all 8 channels we need 1ms. If we sample only one
1226 * channel we need only 125us
1228 this_usbduxsub->ai_interval = 1;
1229 /* find a power of 2 for the interval */
1230 while ((this_usbduxsub->ai_interval) < (cmd->chanlist_len)) {
1231 this_usbduxsub->ai_interval =
1232 (this_usbduxsub->ai_interval) * 2;
1234 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1238 /* interval always 1ms */
1239 this_usbduxsub->ai_interval = 1;
1240 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1242 if (this_usbduxsub->ai_timer < 1) {
1243 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1244 "timer=%d, scan_begin_arg=%d. "
1245 "Not properly tested by cmdtest?\n", dev->minor,
1246 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1247 up(&this_usbduxsub->sem);
1250 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1252 if (cmd->stop_src == TRIG_COUNT) {
1253 /* data arrives as one packet */
1254 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1255 this_usbduxsub->ai_continous = 0;
1257 /* continous acquisition */
1258 this_usbduxsub->ai_continous = 1;
1259 this_usbduxsub->ai_sample_count = 0;
1262 if (cmd->start_src == TRIG_NOW) {
1263 /* enable this acquisition operation */
1264 this_usbduxsub->ai_cmd_running = 1;
1265 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1267 this_usbduxsub->ai_cmd_running = 0;
1268 /* fixme: unlink here?? */
1269 up(&this_usbduxsub->sem);
1272 s->async->inttrig = NULL;
1275 /* don't enable the acquision operation */
1276 /* wait for an internal signal */
1277 s->async->inttrig = usbdux_ai_inttrig;
1279 up(&this_usbduxsub->sem);
1283 /* Mode 0 is used to get a single conversion on demand */
1284 static int usbdux_ai_insn_read(struct comedi_device *dev,
1285 struct comedi_subdevice *s,
1286 struct comedi_insn *insn, unsigned int *data)
1289 unsigned int one = 0;
1292 struct usbduxsub *this_usbduxsub = dev->private;
1294 if (!this_usbduxsub)
1297 dev_dbg(&this_usbduxsub->interface->dev,
1298 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1299 dev->minor, insn->n, insn->subdev);
1301 down(&this_usbduxsub->sem);
1302 if (!(this_usbduxsub->probed)) {
1303 up(&this_usbduxsub->sem);
1306 if (this_usbduxsub->ai_cmd_running) {
1307 dev_err(&this_usbduxsub->interface->dev,
1308 "comedi%d: ai_insn_read not possible. "
1309 "Async Command is running.\n", dev->minor);
1310 up(&this_usbduxsub->sem);
1314 /* sample one channel */
1315 chan = CR_CHAN(insn->chanspec);
1316 range = CR_RANGE(insn->chanspec);
1317 /* set command for the first channel */
1318 this_usbduxsub->dux_commands[1] = create_adc_command(chan, range);
1321 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1323 up(&this_usbduxsub->sem);
1327 for (i = 0; i < insn->n; i++) {
1328 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1330 up(&this_usbduxsub->sem);
1333 one = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1334 if (CR_RANGE(insn->chanspec) <= 1)
1339 up(&this_usbduxsub->sem);
1343 /************************************/
1346 static int usbdux_ao_insn_read(struct comedi_device *dev,
1347 struct comedi_subdevice *s,
1348 struct comedi_insn *insn, unsigned int *data)
1351 int chan = CR_CHAN(insn->chanspec);
1352 struct usbduxsub *this_usbduxsub = dev->private;
1354 if (!this_usbduxsub)
1357 down(&this_usbduxsub->sem);
1358 if (!(this_usbduxsub->probed)) {
1359 up(&this_usbduxsub->sem);
1362 for (i = 0; i < insn->n; i++)
1363 data[i] = this_usbduxsub->outBuffer[chan];
1365 up(&this_usbduxsub->sem);
1369 static int usbdux_ao_insn_write(struct comedi_device *dev,
1370 struct comedi_subdevice *s,
1371 struct comedi_insn *insn, unsigned int *data)
1374 int chan = CR_CHAN(insn->chanspec);
1375 struct usbduxsub *this_usbduxsub = dev->private;
1377 if (!this_usbduxsub)
1380 dev_dbg(&this_usbduxsub->interface->dev,
1381 "comedi%d: ao_insn_write\n", dev->minor);
1383 down(&this_usbduxsub->sem);
1384 if (!(this_usbduxsub->probed)) {
1385 up(&this_usbduxsub->sem);
1388 if (this_usbduxsub->ao_cmd_running) {
1389 dev_err(&this_usbduxsub->interface->dev,
1390 "comedi%d: ao_insn_write: "
1391 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1392 up(&this_usbduxsub->sem);
1396 for (i = 0; i < insn->n; i++) {
1397 dev_dbg(&this_usbduxsub->interface->dev,
1398 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1399 dev->minor, chan, i, data[i]);
1401 /* number of channels: 1 */
1402 this_usbduxsub->dux_commands[1] = 1;
1403 /* one 16 bit value */
1404 *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
1405 cpu_to_le16(data[i]);
1406 this_usbduxsub->outBuffer[chan] = data[i];
1407 /* channel number */
1408 this_usbduxsub->dux_commands[4] = (chan << 6);
1409 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1411 up(&this_usbduxsub->sem);
1415 up(&this_usbduxsub->sem);
1420 static int usbdux_ao_inttrig(struct comedi_device *dev,
1421 struct comedi_subdevice *s, unsigned int trignum)
1424 struct usbduxsub *this_usbduxsub = dev->private;
1426 if (!this_usbduxsub)
1429 down(&this_usbduxsub->sem);
1430 if (!(this_usbduxsub->probed)) {
1431 up(&this_usbduxsub->sem);
1435 dev_err(&this_usbduxsub->interface->dev,
1436 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1438 up(&this_usbduxsub->sem);
1441 if (!(this_usbduxsub->ao_cmd_running)) {
1442 this_usbduxsub->ao_cmd_running = 1;
1443 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1445 dev_err(&this_usbduxsub->interface->dev,
1446 "comedi%d: usbdux_ao_inttrig: submitURB: "
1447 "err=%d\n", dev->minor, ret);
1448 this_usbduxsub->ao_cmd_running = 0;
1449 up(&this_usbduxsub->sem);
1452 s->async->inttrig = NULL;
1454 dev_err(&this_usbduxsub->interface->dev,
1455 "comedi%d: ao_inttrig but acqu is already running.\n",
1458 up(&this_usbduxsub->sem);
1462 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1463 struct comedi_subdevice *s, struct comedi_cmd *cmd)
1465 struct usbduxsub *this_usbduxsub = dev->private;
1469 if (!this_usbduxsub)
1472 if (!(this_usbduxsub->probed))
1475 dev_dbg(&this_usbduxsub->interface->dev,
1476 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1478 /* Step 1 : check if triggers are trivially valid */
1480 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
1482 if (0) { /* (this_usbduxsub->high_speed) */
1483 /* the sampling rate is set by the coversion rate */
1484 flags = TRIG_FOLLOW;
1486 /* start a new scan (output at once) with a timer */
1489 err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
1491 if (0) { /* (this_usbduxsub->high_speed) */
1493 * in usb-2.0 only one conversion it transmitted
1499 * all conversion events happen simultaneously with
1504 err |= cfc_check_trigger_src(&cmd->convert_src, flags);
1506 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
1507 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
1512 /* Step 2a : make sure trigger sources are unique */
1514 err |= cfc_check_trigger_is_unique(cmd->start_src);
1515 err |= cfc_check_trigger_is_unique(cmd->stop_src);
1517 /* Step 2b : and mutually compatible */
1522 /* step 3: make sure arguments are trivially compatible */
1524 if (cmd->start_arg != 0) {
1529 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1530 /* internal trigger */
1531 if (cmd->scan_begin_arg != 0) {
1532 cmd->scan_begin_arg = 0;
1537 if (cmd->scan_begin_src == TRIG_TIMER) {
1539 if (cmd->scan_begin_arg < 1000000) {
1540 cmd->scan_begin_arg = 1000000;
1544 /* not used now, is for later use */
1545 if (cmd->convert_src == TRIG_TIMER) {
1546 if (cmd->convert_arg < 125000) {
1547 cmd->convert_arg = 125000;
1552 /* the same argument */
1553 if (cmd->scan_end_arg != cmd->chanlist_len) {
1554 cmd->scan_end_arg = cmd->chanlist_len;
1558 if (cmd->stop_src == TRIG_COUNT) {
1559 /* any count is allowed */
1562 if (cmd->stop_arg != 0) {
1568 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: err=%d, "
1569 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1570 "convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src,
1571 cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1579 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1581 struct comedi_cmd *cmd = &s->async->cmd;
1582 unsigned int chan, gain;
1584 struct usbduxsub *this_usbduxsub = dev->private;
1586 if (!this_usbduxsub)
1589 down(&this_usbduxsub->sem);
1590 if (!(this_usbduxsub->probed)) {
1591 up(&this_usbduxsub->sem);
1594 dev_dbg(&this_usbduxsub->interface->dev,
1595 "comedi%d: %s\n", dev->minor, __func__);
1597 /* set current channel of the running acquisition to zero */
1598 s->async->cur_chan = 0;
1599 for (i = 0; i < cmd->chanlist_len; ++i) {
1600 chan = CR_CHAN(cmd->chanlist[i]);
1601 gain = CR_RANGE(cmd->chanlist[i]);
1602 if (i >= NUMOUTCHANNELS) {
1603 dev_err(&this_usbduxsub->interface->dev,
1604 "comedi%d: %s: channel list too long\n",
1605 dev->minor, __func__);
1608 this_usbduxsub->dac_commands[i] = (chan << 6);
1609 dev_dbg(&this_usbduxsub->interface->dev,
1610 "comedi%d: dac command for ch %d is %x\n",
1611 dev->minor, i, this_usbduxsub->dac_commands[i]);
1614 /* we count in steps of 1ms (125us) */
1615 /* 125us mode not used yet */
1616 if (0) { /* (this_usbduxsub->high_speed) */
1618 /* timing of the conversion itself: every 125 us */
1619 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1622 /* timing of the scan: we get all channels at once */
1623 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1624 dev_dbg(&this_usbduxsub->interface->dev,
1625 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1626 "convert_src=%d, convert_arg=%d\n", dev->minor,
1627 cmd->scan_begin_src, cmd->scan_begin_arg,
1628 cmd->convert_src, cmd->convert_arg);
1629 dev_dbg(&this_usbduxsub->interface->dev,
1630 "comedi%d: ao_timer=%d (ms)\n",
1631 dev->minor, this_usbduxsub->ao_timer);
1632 if (this_usbduxsub->ao_timer < 1) {
1633 dev_err(&this_usbduxsub->interface->dev,
1634 "comedi%d: usbdux: ao_timer=%d, "
1635 "scan_begin_arg=%d. "
1636 "Not properly tested by cmdtest?\n",
1637 dev->minor, this_usbduxsub->ao_timer,
1638 cmd->scan_begin_arg);
1639 up(&this_usbduxsub->sem);
1643 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1645 if (cmd->stop_src == TRIG_COUNT) {
1646 /* not continuous */
1648 /* high speed also scans everything at once */
1649 if (0) { /* (this_usbduxsub->high_speed) */
1650 this_usbduxsub->ao_sample_count =
1651 (cmd->stop_arg) * (cmd->scan_end_arg);
1653 /* there's no scan as the scan has been */
1654 /* perf inside the FX2 */
1655 /* data arrives as one packet */
1656 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1658 this_usbduxsub->ao_continous = 0;
1660 /* continous acquisition */
1661 this_usbduxsub->ao_continous = 1;
1662 this_usbduxsub->ao_sample_count = 0;
1665 if (cmd->start_src == TRIG_NOW) {
1666 /* enable this acquisition operation */
1667 this_usbduxsub->ao_cmd_running = 1;
1668 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1670 this_usbduxsub->ao_cmd_running = 0;
1671 /* fixme: unlink here?? */
1672 up(&this_usbduxsub->sem);
1675 s->async->inttrig = NULL;
1678 /* submit the urbs later */
1679 /* wait for an internal signal */
1680 s->async->inttrig = usbdux_ao_inttrig;
1683 up(&this_usbduxsub->sem);
1687 static int usbdux_dio_insn_config(struct comedi_device *dev,
1688 struct comedi_subdevice *s,
1689 struct comedi_insn *insn, unsigned int *data)
1691 int chan = CR_CHAN(insn->chanspec);
1693 /* The input or output configuration of each digital line is
1694 * configured by a special insn_config instruction. chanspec
1695 * contains the channel to be changed, and data[0] contains the
1696 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1699 case INSN_CONFIG_DIO_OUTPUT:
1700 s->io_bits |= 1 << chan; /* 1 means Out */
1702 case INSN_CONFIG_DIO_INPUT:
1703 s->io_bits &= ~(1 << chan);
1705 case INSN_CONFIG_DIO_QUERY:
1707 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1713 /* we don't tell the firmware here as it would take 8 frames */
1714 /* to submit the information. We do it in the insn_bits. */
1718 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1719 struct comedi_subdevice *s,
1720 struct comedi_insn *insn, unsigned int *data)
1723 struct usbduxsub *this_usbduxsub = dev->private;
1726 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 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1747 up(&this_usbduxsub->sem);
1750 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1752 up(&this_usbduxsub->sem);
1756 data[1] = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1757 up(&this_usbduxsub->sem);
1761 /* reads the 4 counters, only two are used just now */
1762 static int usbdux_counter_read(struct comedi_device *dev,
1763 struct comedi_subdevice *s,
1764 struct comedi_insn *insn, unsigned int *data)
1766 struct usbduxsub *this_usbduxsub = dev->private;
1767 int chan = insn->chanspec;
1770 if (!this_usbduxsub)
1773 down(&this_usbduxsub->sem);
1775 if (!(this_usbduxsub->probed)) {
1776 up(&this_usbduxsub->sem);
1780 err = send_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1782 up(&this_usbduxsub->sem);
1786 err = receive_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1788 up(&this_usbduxsub->sem);
1792 data[0] = le16_to_cpu(this_usbduxsub->insnBuffer[chan + 1]);
1793 up(&this_usbduxsub->sem);
1797 static int usbdux_counter_write(struct comedi_device *dev,
1798 struct comedi_subdevice *s,
1799 struct comedi_insn *insn, unsigned int *data)
1801 struct usbduxsub *this_usbduxsub = dev->private;
1804 if (!this_usbduxsub)
1807 down(&this_usbduxsub->sem);
1809 if (!(this_usbduxsub->probed)) {
1810 up(&this_usbduxsub->sem);
1814 this_usbduxsub->dux_commands[1] = insn->chanspec;
1815 *((int16_t *) (this_usbduxsub->dux_commands + 2)) = cpu_to_le16(*data);
1817 err = send_dux_commands(this_usbduxsub, WRITECOUNTERCOMMAND);
1819 up(&this_usbduxsub->sem);
1823 up(&this_usbduxsub->sem);
1828 static int usbdux_counter_config(struct comedi_device *dev,
1829 struct comedi_subdevice *s,
1830 struct comedi_insn *insn, unsigned int *data)
1832 /* nothing to do so far */
1836 /***********************************/
1839 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1843 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1844 if (usbduxsub_tmp->urbPwm)
1845 usb_kill_urb(usbduxsub_tmp->urbPwm);
1846 dev_dbg(&usbduxsub_tmp->interface->dev,
1847 "comedi: unlinked PwmURB: res=%d\n", err);
1852 /* This cancels a running acquisition operation
1855 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1859 if (!this_usbduxsub)
1862 dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1864 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1866 this_usbduxsub->pwm_cmd_running = 0;
1871 /* force unlink - is called by comedi */
1872 static int usbdux_pwm_cancel(struct comedi_device *dev,
1873 struct comedi_subdevice *s)
1875 struct usbduxsub *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 dev_dbg(&this_usbduxsub->interface->dev,
1882 "comedi %d: sending pwm off command to the usb device.\n",
1885 return send_dux_commands(this_usbduxsub, SENDPWMOFF);
1888 static void usbduxsub_pwm_irq(struct urb *urb)
1891 struct usbduxsub *this_usbduxsub;
1892 struct comedi_device *this_comedidev;
1893 struct comedi_subdevice *s;
1895 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1897 /* the context variable points to the subdevice */
1898 this_comedidev = urb->context;
1899 /* the private structure of the subdevice is struct usbduxsub */
1900 this_usbduxsub = this_comedidev->private;
1902 s = &this_comedidev->subdevices[SUBDEV_DA];
1904 switch (urb->status) {
1914 * after an unlink command, unplug, ... etc
1915 * no unlink needed here. Already shutting down.
1917 if (this_usbduxsub->pwm_cmd_running)
1918 usbdux_pwm_stop(this_usbduxsub, 0);
1924 if (this_usbduxsub->pwm_cmd_running) {
1925 dev_err(&this_usbduxsub->interface->dev,
1926 "comedi_: Non-zero urb status received in "
1927 "pwm intr context: %d\n", urb->status);
1928 usbdux_pwm_stop(this_usbduxsub, 0);
1933 /* are we actually running? */
1934 if (!(this_usbduxsub->pwm_cmd_running))
1937 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1938 urb->dev = this_usbduxsub->usbdev;
1940 if (this_usbduxsub->pwm_cmd_running) {
1941 ret = usb_submit_urb(urb, GFP_ATOMIC);
1943 dev_err(&this_usbduxsub->interface->dev,
1944 "comedi_: pwm urb resubm failed in int-cont. "
1946 if (ret == EL2NSYNC)
1947 dev_err(&this_usbduxsub->interface->dev,
1948 "buggy USB host controller or bug in "
1951 /* don't do an unlink here */
1952 usbdux_pwm_stop(this_usbduxsub, 0);
1957 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
1964 dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
1966 /* in case of a resubmission after an unlink... */
1967 usb_fill_bulk_urb(usbduxsub->urbPwm,
1969 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
1970 usbduxsub->urbPwm->transfer_buffer,
1971 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq,
1972 usbduxsub->comedidev);
1974 errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
1976 dev_err(&usbduxsub->interface->dev,
1977 "comedi_: usbdux: pwm: usb_submit_urb error %d\n",
1984 static int usbdux_pwm_period(struct comedi_device *dev,
1985 struct comedi_subdevice *s, unsigned int period)
1987 struct usbduxsub *this_usbduxsub = dev->private;
1990 if (period < MIN_PWM_PERIOD) {
1991 dev_err(&this_usbduxsub->interface->dev,
1992 "comedi%d: illegal period setting for pwm.\n",
1996 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1997 if (fx2delay > 255) {
1998 dev_err(&this_usbduxsub->interface->dev,
1999 "comedi%d: period %d for pwm is too low.\n",
2000 dev->minor, period);
2004 this_usbduxsub->pwmDelay = fx2delay;
2005 this_usbduxsub->pwmPeriod = period;
2006 dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
2007 __func__, period, fx2delay);
2011 /* is called from insn so there's no need to do all the sanity checks */
2012 static int usbdux_pwm_start(struct comedi_device *dev,
2013 struct comedi_subdevice *s)
2016 struct usbduxsub *this_usbduxsub = dev->private;
2018 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
2019 dev->minor, __func__);
2021 if (this_usbduxsub->pwm_cmd_running) {
2022 /* already running */
2026 this_usbduxsub->dux_commands[1] = ((int8_t) this_usbduxsub->pwmDelay);
2027 ret = send_dux_commands(this_usbduxsub, SENDPWMON);
2031 /* initialise the buffer */
2032 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
2033 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
2035 this_usbduxsub->pwm_cmd_running = 1;
2036 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
2038 this_usbduxsub->pwm_cmd_running = 0;
2044 /* generates the bit pattern for PWM with the optional sign bit */
2045 static int usbdux_pwm_pattern(struct comedi_device *dev,
2046 struct comedi_subdevice *s, int channel,
2047 unsigned int value, unsigned int sign)
2049 struct usbduxsub *this_usbduxsub = dev->private;
2056 if (!this_usbduxsub)
2059 /* this is the DIO bit which carries the PWM data */
2060 pwm_mask = (1 << channel);
2061 /* this is the DIO bit which carries the optional direction bit */
2062 sgn_mask = (16 << channel);
2063 /* this is the buffer which will be filled with the with bit */
2064 /* pattern for one period */
2065 szbuf = this_usbduxsub->sizePwmBuf;
2066 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2067 for (i = 0; i < szbuf; i++) {
2070 c = c & (~pwm_mask);
2071 /* set the bit as long as the index is lower than the value */
2074 /* set the optional sign bit for a relay */
2076 /* positive value */
2077 c = c & (~sgn_mask);
2079 /* negative value */
2087 static int usbdux_pwm_write(struct comedi_device *dev,
2088 struct comedi_subdevice *s,
2089 struct comedi_insn *insn, unsigned int *data)
2091 struct usbduxsub *this_usbduxsub = dev->private;
2093 if (!this_usbduxsub)
2096 if ((insn->n) != 1) {
2098 * doesn't make sense to have more than one value here because
2099 * it would just overwrite the PWM buffer a couple of times
2105 * the sign is set via a special INSN only, this gives us 8 bits for
2107 * relay sign 0 by default
2109 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
2112 static int usbdux_pwm_read(struct comedi_device *x1,
2113 struct comedi_subdevice *x2, struct comedi_insn *x3,
2120 /* switches on/off PWM */
2121 static int usbdux_pwm_config(struct comedi_device *dev,
2122 struct comedi_subdevice *s,
2123 struct comedi_insn *insn, unsigned int *data)
2125 struct usbduxsub *this_usbduxsub = dev->private;
2127 case INSN_CONFIG_ARM:
2129 dev_dbg(&this_usbduxsub->interface->dev,
2130 "comedi%d: %s: pwm on\n", dev->minor, __func__);
2132 * if not zero the PWM is limited to a certain time which is
2133 * not supported here
2137 return usbdux_pwm_start(dev, s);
2138 case INSN_CONFIG_DISARM:
2139 dev_dbg(&this_usbduxsub->interface->dev,
2140 "comedi%d: %s: pwm off\n", dev->minor, __func__);
2141 return usbdux_pwm_cancel(dev, s);
2142 case INSN_CONFIG_GET_PWM_STATUS:
2144 * to check if the USB transmission has failed or in case PWM
2145 * was limited to n cycles to check if it has terminated
2147 data[1] = this_usbduxsub->pwm_cmd_running;
2149 case INSN_CONFIG_PWM_SET_PERIOD:
2150 dev_dbg(&this_usbduxsub->interface->dev,
2151 "comedi%d: %s: setting period\n", dev->minor, __func__);
2152 return usbdux_pwm_period(dev, s, data[1]);
2153 case INSN_CONFIG_PWM_GET_PERIOD:
2154 data[1] = this_usbduxsub->pwmPeriod;
2156 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2157 /* value in the first byte and the sign in the second for a
2159 return usbdux_pwm_pattern(dev, s,
2160 /* the channel number */
2161 CR_CHAN(insn->chanspec),
2162 /* actual PWM data */
2166 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2167 /* values are not kept in this driver, nothing to return here */
2174 /*****************************************************************/
2176 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2182 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2184 /* shows the usb subsystem that the driver is down */
2185 if (usbduxsub_tmp->interface)
2186 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2188 usbduxsub_tmp->probed = 0;
2190 if (usbduxsub_tmp->urbIn) {
2191 if (usbduxsub_tmp->ai_cmd_running) {
2192 usbduxsub_tmp->ai_cmd_running = 0;
2193 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2195 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2196 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2197 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2198 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2199 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2200 usbduxsub_tmp->urbIn[i] = NULL;
2202 kfree(usbduxsub_tmp->urbIn);
2203 usbduxsub_tmp->urbIn = NULL;
2205 if (usbduxsub_tmp->urbOut) {
2206 if (usbduxsub_tmp->ao_cmd_running) {
2207 usbduxsub_tmp->ao_cmd_running = 0;
2208 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2210 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2211 kfree(usbduxsub_tmp->urbOut[i]->transfer_buffer);
2212 usbduxsub_tmp->urbOut[i]->transfer_buffer = NULL;
2213 if (usbduxsub_tmp->urbOut[i]) {
2214 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2215 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2216 usbduxsub_tmp->urbOut[i] = NULL;
2219 kfree(usbduxsub_tmp->urbOut);
2220 usbduxsub_tmp->urbOut = NULL;
2222 if (usbduxsub_tmp->urbPwm) {
2223 if (usbduxsub_tmp->pwm_cmd_running) {
2224 usbduxsub_tmp->pwm_cmd_running = 0;
2225 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2227 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2228 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2229 usb_kill_urb(usbduxsub_tmp->urbPwm);
2230 usb_free_urb(usbduxsub_tmp->urbPwm);
2231 usbduxsub_tmp->urbPwm = NULL;
2233 kfree(usbduxsub_tmp->inBuffer);
2234 usbduxsub_tmp->inBuffer = NULL;
2235 kfree(usbduxsub_tmp->insnBuffer);
2236 usbduxsub_tmp->insnBuffer = NULL;
2237 kfree(usbduxsub_tmp->outBuffer);
2238 usbduxsub_tmp->outBuffer = NULL;
2239 kfree(usbduxsub_tmp->dac_commands);
2240 usbduxsub_tmp->dac_commands = NULL;
2241 kfree(usbduxsub_tmp->dux_commands);
2242 usbduxsub_tmp->dux_commands = NULL;
2243 usbduxsub_tmp->ai_cmd_running = 0;
2244 usbduxsub_tmp->ao_cmd_running = 0;
2245 usbduxsub_tmp->pwm_cmd_running = 0;
2248 static int usbdux_attach_common(struct comedi_device *dev,
2249 struct usbduxsub *udev)
2252 struct comedi_subdevice *s = NULL;
2256 /* pointer back to the corresponding comedi device */
2257 udev->comedidev = dev;
2259 dev->board_name = "usbdux";
2261 /* set number of subdevices */
2262 if (udev->high_speed) {
2270 ret = comedi_alloc_subdevices(dev, n_subdevs);
2276 /* private structure is also simply the usb-structure */
2277 dev->private = udev;
2279 /* the first subdevice is the A/D converter */
2280 s = &dev->subdevices[SUBDEV_AD];
2281 /* the URBs get the comedi subdevice */
2282 /* which is responsible for reading */
2283 /* this is the subdevice which reads data */
2284 dev->read_subdev = s;
2285 /* the subdevice receives as private structure the */
2289 s->type = COMEDI_SUBD_AI;
2290 /* readable and ref is to ground */
2291 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
2294 /* length of the channellist */
2295 s->len_chanlist = 8;
2296 /* callback functions */
2297 s->insn_read = usbdux_ai_insn_read;
2298 s->do_cmdtest = usbdux_ai_cmdtest;
2299 s->do_cmd = usbdux_ai_cmd;
2300 s->cancel = usbdux_ai_cancel;
2301 /* max value from the A/D converter (12bit) */
2303 /* range table to convert to physical units */
2304 s->range_table = (&range_usbdux_ai_range);
2307 s = &dev->subdevices[SUBDEV_DA];
2309 s->type = COMEDI_SUBD_AO;
2310 /* backward pointer */
2311 dev->write_subdev = s;
2312 /* the subdevice receives as private structure the */
2316 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2319 /* length of the channellist */
2320 s->len_chanlist = 4;
2321 /* 12 bit resolution */
2322 s->maxdata = 0x0fff;
2324 s->range_table = (&range_usbdux_ao_range);
2326 s->do_cmdtest = usbdux_ao_cmdtest;
2327 s->do_cmd = usbdux_ao_cmd;
2328 s->cancel = usbdux_ao_cancel;
2329 s->insn_read = usbdux_ao_insn_read;
2330 s->insn_write = usbdux_ao_insn_write;
2333 s = &dev->subdevices[SUBDEV_DIO];
2334 s->type = COMEDI_SUBD_DIO;
2335 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2338 s->range_table = (&range_digital);
2339 s->insn_bits = usbdux_dio_insn_bits;
2340 s->insn_config = usbdux_dio_insn_config;
2341 /* we don't use it */
2345 s = &dev->subdevices[SUBDEV_COUNTER];
2346 s->type = COMEDI_SUBD_COUNTER;
2347 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2349 s->maxdata = 0xFFFF;
2350 s->insn_read = usbdux_counter_read;
2351 s->insn_write = usbdux_counter_write;
2352 s->insn_config = usbdux_counter_config;
2354 if (udev->high_speed) {
2356 s = &dev->subdevices[SUBDEV_PWM];
2357 s->type = COMEDI_SUBD_PWM;
2358 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2360 /* this defines the max duty cycle resolution */
2361 s->maxdata = udev->sizePwmBuf;
2362 s->insn_write = usbdux_pwm_write;
2363 s->insn_read = usbdux_pwm_read;
2364 s->insn_config = usbdux_pwm_config;
2365 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2367 /* finally decide that it's attached */
2372 dev_info(&udev->interface->dev, "comedi%d: attached to usbdux.\n",
2378 static int usbdux_attach_usb(struct comedi_device *dev,
2379 struct usb_interface *uinterf)
2382 struct usbduxsub *this_usbduxsub;
2384 dev->private = NULL;
2386 down(&start_stop_sem);
2387 this_usbduxsub = usb_get_intfdata(uinterf);
2388 if (!this_usbduxsub || !this_usbduxsub->probed) {
2390 "comedi%d: usbdux: error: attach_usb failed, not connected\n",
2393 } else if (this_usbduxsub->attached) {
2395 "comedi%d: usbdux: error: attach_usb failed, already attached\n",
2399 ret = usbdux_attach_common(dev, this_usbduxsub);
2400 up(&start_stop_sem);
2404 static void usbdux_detach(struct comedi_device *dev)
2406 struct usbduxsub *usb = dev->private;
2410 dev->private = NULL;
2412 usb->comedidev = NULL;
2417 static struct comedi_driver usbdux_driver = {
2418 .driver_name = "usbdux",
2419 .module = THIS_MODULE,
2420 .attach_usb = usbdux_attach_usb,
2421 .detach = usbdux_detach,
2424 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2427 struct usbduxsub *usbduxsub_tmp = context;
2428 struct usb_interface *uinterf = usbduxsub_tmp->interface;
2432 dev_err(&uinterf->dev,
2433 "Firmware complete handler without firmware!\n");
2438 * we need to upload the firmware here because fw will be
2439 * freed once we've left this function
2441 ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2444 dev_err(&uinterf->dev,
2445 "Could not upload firmware (err=%d)\n", ret);
2448 comedi_usb_auto_config(uinterf, &usbdux_driver);
2450 release_firmware(fw);
2453 static int usbdux_usb_probe(struct usb_interface *uinterf,
2454 const struct usb_device_id *id)
2456 struct usb_device *udev = interface_to_usbdev(uinterf);
2457 struct device *dev = &uinterf->dev;
2462 dev_dbg(dev, "comedi_: usbdux_: "
2463 "finding a free structure for the usb-device\n");
2465 down(&start_stop_sem);
2466 /* look for a free place in the usbdux array */
2468 for (i = 0; i < NUMUSBDUX; i++) {
2469 if (!(usbduxsub[i].probed)) {
2477 dev_err(dev, "Too many usbdux-devices connected.\n");
2478 up(&start_stop_sem);
2481 dev_dbg(dev, "comedi_: usbdux: "
2482 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2484 sema_init(&(usbduxsub[index].sem), 1);
2485 /* save a pointer to the usb device */
2486 usbduxsub[index].usbdev = udev;
2488 /* 2.6: save the interface itself */
2489 usbduxsub[index].interface = uinterf;
2490 /* get the interface number from the interface */
2491 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2492 /* hand the private data over to the usb subsystem */
2493 /* will be needed for disconnect */
2494 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2496 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2498 /* test if it is high speed (USB 2.0) */
2499 usbduxsub[index].high_speed =
2500 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2502 /* create space for the commands of the DA converter */
2503 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2504 if (!usbduxsub[index].dac_commands) {
2505 dev_err(dev, "comedi_: usbdux: "
2506 "error alloc space for dac commands\n");
2507 tidy_up(&(usbduxsub[index]));
2508 up(&start_stop_sem);
2511 /* create space for the commands going to the usb device */
2512 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2513 if (!usbduxsub[index].dux_commands) {
2514 dev_err(dev, "comedi_: usbdux: "
2515 "error alloc space for dux commands\n");
2516 tidy_up(&(usbduxsub[index]));
2517 up(&start_stop_sem);
2520 /* create space for the in buffer and set it to zero */
2521 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2522 if (!(usbduxsub[index].inBuffer)) {
2523 dev_err(dev, "comedi_: usbdux: "
2524 "could not alloc space for inBuffer\n");
2525 tidy_up(&(usbduxsub[index]));
2526 up(&start_stop_sem);
2529 /* create space of the instruction buffer */
2530 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2531 if (!(usbduxsub[index].insnBuffer)) {
2532 dev_err(dev, "comedi_: usbdux: "
2533 "could not alloc space for insnBuffer\n");
2534 tidy_up(&(usbduxsub[index]));
2535 up(&start_stop_sem);
2538 /* create space for the outbuffer */
2539 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2540 if (!(usbduxsub[index].outBuffer)) {
2541 dev_err(dev, "comedi_: usbdux: "
2542 "could not alloc space for outBuffer\n");
2543 tidy_up(&(usbduxsub[index]));
2544 up(&start_stop_sem);
2547 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2548 i = usb_set_interface(usbduxsub[index].usbdev,
2549 usbduxsub[index].ifnum, 3);
2551 dev_err(dev, "comedi_: usbdux%d: "
2552 "could not set alternate setting 3 in high speed.\n",
2554 tidy_up(&(usbduxsub[index]));
2555 up(&start_stop_sem);
2558 if (usbduxsub[index].high_speed)
2559 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2561 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2563 usbduxsub[index].urbIn =
2564 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2566 if (!(usbduxsub[index].urbIn)) {
2567 dev_err(dev, "comedi_: usbdux: Could not alloc. urbIn array\n");
2568 tidy_up(&(usbduxsub[index]));
2569 up(&start_stop_sem);
2572 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2573 /* one frame: 1ms */
2574 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2575 if (usbduxsub[index].urbIn[i] == NULL) {
2576 dev_err(dev, "comedi_: usbdux%d: "
2577 "Could not alloc. urb(%d)\n", index, i);
2578 tidy_up(&(usbduxsub[index]));
2579 up(&start_stop_sem);
2582 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2583 /* will be filled later with a pointer to the comedi-device */
2584 /* and ONLY then the urb should be submitted */
2585 usbduxsub[index].urbIn[i]->context = NULL;
2586 usbduxsub[index].urbIn[i]->pipe =
2587 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2588 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2589 usbduxsub[index].urbIn[i]->transfer_buffer =
2590 kzalloc(SIZEINBUF, GFP_KERNEL);
2591 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2592 dev_err(dev, "comedi_: usbdux%d: "
2593 "could not alloc. transb.\n", index);
2594 tidy_up(&(usbduxsub[index]));
2595 up(&start_stop_sem);
2598 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2599 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2600 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2601 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2602 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length = SIZEINBUF;
2606 if (usbduxsub[index].high_speed)
2607 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2609 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2611 usbduxsub[index].urbOut =
2612 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2614 if (!(usbduxsub[index].urbOut)) {
2615 dev_err(dev, "comedi_: usbdux: "
2616 "Could not alloc. urbOut array\n");
2617 tidy_up(&(usbduxsub[index]));
2618 up(&start_stop_sem);
2621 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2622 /* one frame: 1ms */
2623 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2624 if (usbduxsub[index].urbOut[i] == NULL) {
2625 dev_err(dev, "comedi_: usbdux%d: "
2626 "Could not alloc. urb(%d)\n", index, i);
2627 tidy_up(&(usbduxsub[index]));
2628 up(&start_stop_sem);
2631 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2632 /* will be filled later with a pointer to the comedi-device */
2633 /* and ONLY then the urb should be submitted */
2634 usbduxsub[index].urbOut[i]->context = NULL;
2635 usbduxsub[index].urbOut[i]->pipe =
2636 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2637 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2638 usbduxsub[index].urbOut[i]->transfer_buffer =
2639 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2640 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2641 dev_err(dev, "comedi_: usbdux%d: "
2642 "could not alloc. transb.\n", index);
2643 tidy_up(&(usbduxsub[index]));
2644 up(&start_stop_sem);
2647 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2648 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2649 usbduxsub[index].urbOut[i]->transfer_buffer_length = SIZEOUTBUF;
2650 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2651 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2653 if (usbduxsub[index].high_speed) {
2655 usbduxsub[index].urbOut[i]->interval = 8;
2658 usbduxsub[index].urbOut[i]->interval = 1;
2663 if (usbduxsub[index].high_speed) {
2664 /* max bulk ep size in high speed */
2665 usbduxsub[index].sizePwmBuf = 512;
2666 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2667 if (usbduxsub[index].urbPwm == NULL) {
2668 dev_err(dev, "comedi_: usbdux%d: "
2669 "Could not alloc. pwm urb\n", index);
2670 tidy_up(&(usbduxsub[index]));
2671 up(&start_stop_sem);
2674 usbduxsub[index].urbPwm->transfer_buffer =
2675 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2676 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2677 dev_err(dev, "comedi_: usbdux%d: "
2678 "could not alloc. transb. for pwm\n", index);
2679 tidy_up(&(usbduxsub[index]));
2680 up(&start_stop_sem);
2684 usbduxsub[index].urbPwm = NULL;
2685 usbduxsub[index].sizePwmBuf = 0;
2688 usbduxsub[index].ai_cmd_running = 0;
2689 usbduxsub[index].ao_cmd_running = 0;
2690 usbduxsub[index].pwm_cmd_running = 0;
2692 /* we've reached the bottom of the function */
2693 usbduxsub[index].probed = 1;
2694 up(&start_stop_sem);
2696 ret = request_firmware_nowait(THIS_MODULE,
2702 usbdux_firmware_request_complete_handler);
2705 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2709 dev_info(dev, "comedi_: usbdux%d "
2710 "has been successfully initialised.\n", index);
2715 static void usbdux_usb_disconnect(struct usb_interface *intf)
2717 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2718 struct usb_device *udev = interface_to_usbdev(intf);
2720 if (!usbduxsub_tmp) {
2722 "comedi_: disconnect called with null pointer.\n");
2725 if (usbduxsub_tmp->usbdev != udev) {
2726 dev_err(&intf->dev, "comedi_: BUG! called with wrong ptr!!!\n");
2729 comedi_usb_auto_unconfig(intf);
2730 down(&start_stop_sem);
2731 down(&usbduxsub_tmp->sem);
2732 tidy_up(usbduxsub_tmp);
2733 up(&usbduxsub_tmp->sem);
2734 up(&start_stop_sem);
2735 dev_dbg(&intf->dev, "comedi_: disconnected from the usb\n");
2738 static const struct usb_device_id usbdux_usb_table[] = {
2739 { USB_DEVICE(0x13d8, 0x0001) },
2740 { USB_DEVICE(0x13d8, 0x0002) },
2744 MODULE_DEVICE_TABLE(usb, usbdux_usb_table);
2746 static struct usb_driver usbdux_usb_driver = {
2748 .probe = usbdux_usb_probe,
2749 .disconnect = usbdux_usb_disconnect,
2750 .id_table = usbdux_usb_table,
2752 module_comedi_usb_driver(usbdux_driver, usbdux_usb_driver);
2754 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
2755 MODULE_DESCRIPTION("Stirling/ITL USB-DUX -- Bernd.Porr@f2s.com");
2756 MODULE_LICENSE("GPL");
2757 MODULE_FIRMWARE(FIRMWARE);