1 #define DRIVER_VERSION "v2.2"
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
82 * 2.2: added firmware kernel request to fix an udev problem
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/smp_lock.h>
96 #include <linux/fcntl.h>
97 #include <linux/compiler.h>
98 #include <linux/firmware.h>
100 #include "../comedidev.h"
102 #define BOARDNAME "usbdux"
104 /* timeout for the USB-transfer */
107 /* constants for "firmware" upload and download */
108 #define USBDUXSUB_FIRMWARE 0xA0
109 #define VENDOR_DIR_IN 0xC0
110 #define VENDOR_DIR_OUT 0x40
112 /* internal adresses 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, {
219 BIP_RANGE(4.096 / 2),
225 static const struct comedi_lrange range_usbdux_ao_range = { 2, {
232 * private structure of one subdevice
236 * This is the structure which holds all the data of
237 * this driver one sub device just now: A/D
242 /* is it associated with a subdevice? */
244 /* pointer to the usb-device */
245 struct usb_device *usbdev;
246 /* actual number of in-buffers */
248 /* actual number of out-buffers */
250 /* ISO-transfer handling: buffers */
253 /* pwm-transfer handling */
256 unsigned int pwmPeriod;
257 /* PWM internal delay for the GPIF in the FX2 */
259 /* size of the PWM buffer which holds the bit pattern */
261 /* input buffer for the ISO-transfer */
263 /* input buffer for single insn */
265 /* output buffer for single DA outputs */
267 /* interface number */
269 /* interface structure in 2.6 */
270 struct usb_interface *interface;
271 /* comedi device for the interrupt context */
272 struct comedi_device *comedidev;
273 /* is it USB_SPEED_HIGH or not? */
274 short int high_speed;
275 /* asynchronous command is running */
276 short int ai_cmd_running;
277 short int ao_cmd_running;
279 short int pwm_cmd_running;
280 /* continous aquisition */
281 short int ai_continous;
282 short int ao_continous;
283 /* number of samples to aquire */
286 /* time between samples in units of the timer */
287 unsigned int ai_timer;
288 unsigned int ao_timer;
289 /* counter between aquisitions */
290 unsigned int ai_counter;
291 unsigned int ao_counter;
292 /* interval in frames/uframes */
293 unsigned int ai_interval;
295 int8_t *dac_commands;
297 int8_t *dux_commands;
298 struct semaphore sem;
302 * The pointer to the private usb-data of the driver is also the private data
303 * for the comedi-device. This has to be global as the usb subsystem needs
304 * global variables. The other reason is that this structure must be there
305 * _before_ any comedi command is issued. The usb subsystem must be initialised
306 * before comedi can access it.
308 static struct usbduxsub usbduxsub[NUMUSBDUX];
310 static DECLARE_MUTEX(start_stop_sem);
313 * Stops the data acquision
314 * It should be safe to call this function from any context
316 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
321 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
322 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
323 if (usbduxsub_tmp->urbIn[i]) {
324 /* We wait here until all transfers have been
326 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
328 dev_dbg(&usbduxsub_tmp->interface->dev,
329 "comedi: usbdux: unlinked InURB %d, err=%d\n",
337 * This will stop a running acquisition operation
338 * Is called from within this driver from both the
339 * interrupt context and from comedi
341 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
345 if (!this_usbduxsub) {
346 dev_err(&this_usbduxsub->interface->dev,
347 "comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
350 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
354 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
357 this_usbduxsub->ai_cmd_running = 0;
363 * This will cancel a running acquisition operation.
364 * This is called by comedi but never from inside the driver.
366 static int usbdux_ai_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
368 struct usbduxsub *this_usbduxsub;
371 /* force unlink of all urbs */
372 this_usbduxsub = dev->private;
376 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
378 /* prevent other CPUs from submitting new commands just now */
379 down(&this_usbduxsub->sem);
380 if (!(this_usbduxsub->probed)) {
381 up(&this_usbduxsub->sem);
384 /* unlink only if the urb really has been submitted */
385 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
386 up(&this_usbduxsub->sem);
390 /* analogue IN - interrupt service routine */
391 static void usbduxsub_ai_IsocIrq(struct urb *urb)
394 struct usbduxsub *this_usbduxsub;
395 struct comedi_device *this_comedidev;
396 struct comedi_subdevice *s;
398 /* the context variable points to the subdevice */
399 this_comedidev = urb->context;
400 /* the private structure of the subdevice is struct usbduxsub */
401 this_usbduxsub = this_comedidev->private;
402 /* subdevice which is the AD converter */
403 s = this_comedidev->subdevices + SUBDEV_AD;
405 /* first we test if something unusual has just happened */
406 switch (urb->status) {
408 /* copy the result in the transfer buffer */
409 memcpy(this_usbduxsub->inBuffer,
410 urb->transfer_buffer, SIZEINBUF);
413 /* error in the ISOchronous data */
414 /* we don't copy the data into the transfer buffer */
415 /* and recycle the last data byte */
416 dev_dbg(&urb->dev->dev,
417 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
418 this_usbduxsub->comedidev->minor);
426 /* happens after an unlink command */
427 if (this_usbduxsub->ai_cmd_running) {
428 /* we are still running a command */
429 /* tell this comedi */
430 s->async->events |= COMEDI_CB_EOA;
431 s->async->events |= COMEDI_CB_ERROR;
432 comedi_event(this_usbduxsub->comedidev, s);
433 /* stop the transfer w/o unlink */
434 usbdux_ai_stop(this_usbduxsub, 0);
439 /* a real error on the bus */
440 /* pass error to comedi if we are really running a command */
441 if (this_usbduxsub->ai_cmd_running) {
442 dev_err(&urb->dev->dev,
443 "Non-zero urb status received in ai intr "
444 "context: %d\n", urb->status);
445 s->async->events |= COMEDI_CB_EOA;
446 s->async->events |= COMEDI_CB_ERROR;
447 comedi_event(this_usbduxsub->comedidev, s);
448 /* don't do an unlink here */
449 usbdux_ai_stop(this_usbduxsub, 0);
455 * at this point we are reasonably sure that nothing dodgy has happened
456 * are we running a command?
458 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
460 * not running a command, do not continue execution if no
461 * asynchronous command is running in particular not resubmit
466 urb->dev = this_usbduxsub->usbdev;
468 /* resubmit the urb */
469 err = usb_submit_urb(urb, GFP_ATOMIC);
470 if (unlikely(err < 0)) {
471 dev_err(&urb->dev->dev,
472 "comedi_: urb resubmit failed in int-context! err=%d\n",
474 if (err == -EL2NSYNC)
475 dev_err(&urb->dev->dev,
476 "buggy USB host controller or bug in IRQ "
478 s->async->events |= COMEDI_CB_EOA;
479 s->async->events |= COMEDI_CB_ERROR;
480 comedi_event(this_usbduxsub->comedidev, s);
481 /* don't do an unlink here */
482 usbdux_ai_stop(this_usbduxsub, 0);
486 this_usbduxsub->ai_counter--;
487 if (likely(this_usbduxsub->ai_counter > 0))
490 /* timer zero, transfer measurements to comedi */
491 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
493 /* test, if we transmit only a fixed number of samples */
494 if (!(this_usbduxsub->ai_continous)) {
495 /* not continous, fixed number of samples */
496 this_usbduxsub->ai_sample_count--;
497 /* all samples received? */
498 if (this_usbduxsub->ai_sample_count < 0) {
499 /* prevent a resubmit next time */
500 usbdux_ai_stop(this_usbduxsub, 0);
501 /* say comedi that the acquistion is over */
502 s->async->events |= COMEDI_CB_EOA;
503 comedi_event(this_usbduxsub->comedidev, s);
507 /* get the data from the USB bus and hand it over to comedi */
508 n = s->async->cmd.chanlist_len;
509 for (i = 0; i < n; i++) {
511 if (CR_RANGE(s->async->cmd.chanlist[i]) <= 1) {
514 le16_to_cpu(this_usbduxsub->
515 inBuffer[i]) ^ 0x800);
519 le16_to_cpu(this_usbduxsub->inBuffer[i]));
522 /* tell comedi that data is there */
523 comedi_event(this_usbduxsub->comedidev, s);
526 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
531 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
532 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
533 if (usbduxsub_tmp->urbOut[i])
534 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
536 dev_dbg(&usbduxsub_tmp->interface->dev,
537 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
544 /* This will cancel a running acquisition operation
547 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
553 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
556 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
558 this_usbduxsub->ao_cmd_running = 0;
563 /* force unlink, is called by comedi */
564 static int usbdux_ao_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
566 struct usbduxsub *this_usbduxsub = dev->private;
572 /* prevent other CPUs from submitting a command just now */
573 down(&this_usbduxsub->sem);
574 if (!(this_usbduxsub->probed)) {
575 up(&this_usbduxsub->sem);
578 /* unlink only if it is really running */
579 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
580 up(&this_usbduxsub->sem);
584 static void usbduxsub_ao_IsocIrq(struct urb *urb)
588 struct usbduxsub *this_usbduxsub;
589 struct comedi_device *this_comedidev;
590 struct comedi_subdevice *s;
592 /* the context variable points to the subdevice */
593 this_comedidev = urb->context;
594 /* the private structure of the subdevice is struct usbduxsub */
595 this_usbduxsub = this_comedidev->private;
597 s = this_comedidev->subdevices + SUBDEV_DA;
599 switch (urb->status) {
608 /* after an unlink command, unplug, ... etc */
609 /* no unlink needed here. Already shutting down. */
610 if (this_usbduxsub->ao_cmd_running) {
611 s->async->events |= COMEDI_CB_EOA;
612 comedi_event(this_usbduxsub->comedidev, s);
613 usbdux_ao_stop(this_usbduxsub, 0);
619 if (this_usbduxsub->ao_cmd_running) {
620 dev_err(&urb->dev->dev,
621 "comedi_: Non-zero urb status received in ao "
622 "intr context: %d\n", urb->status);
623 s->async->events |= COMEDI_CB_ERROR;
624 s->async->events |= COMEDI_CB_EOA;
625 comedi_event(this_usbduxsub->comedidev, s);
626 /* we do an unlink if we are in the high speed mode */
627 usbdux_ao_stop(this_usbduxsub, 0);
632 /* are we actually running? */
633 if (!(this_usbduxsub->ao_cmd_running))
636 /* normal operation: executing a command in this subdevice */
637 this_usbduxsub->ao_counter--;
638 if (this_usbduxsub->ao_counter <= 0) {
640 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
642 /* handle non continous aquisition */
643 if (!(this_usbduxsub->ao_continous)) {
644 /* fixed number of samples */
645 this_usbduxsub->ao_sample_count--;
646 if (this_usbduxsub->ao_sample_count < 0) {
647 /* all samples transmitted */
648 usbdux_ao_stop(this_usbduxsub, 0);
649 s->async->events |= COMEDI_CB_EOA;
650 comedi_event(this_usbduxsub->comedidev, s);
651 /* no resubmit of the urb */
655 /* transmit data to the USB bus */
656 ((uint8_t *) (urb->transfer_buffer))[0] =
657 s->async->cmd.chanlist_len;
658 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
660 if (i >= NUMOUTCHANNELS)
663 /* pointer to the DA */
665 (&(((int8_t *)urb->transfer_buffer)[i * 3 + 1]));
666 /* get the data from comedi */
667 ret = comedi_buf_get(s->async, &temp);
669 datap[1] = temp >> 8;
670 datap[2] = this_usbduxsub->dac_commands[i];
671 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
672 /* datap[0],datap[1],datap[2]); */
674 dev_err(&urb->dev->dev,
675 "comedi: buffer underflow\n");
676 s->async->events |= COMEDI_CB_EOA;
677 s->async->events |= COMEDI_CB_OVERFLOW;
679 /* transmit data to comedi */
680 s->async->events |= COMEDI_CB_BLOCK;
681 comedi_event(this_usbduxsub->comedidev, s);
684 urb->transfer_buffer_length = SIZEOUTBUF;
685 urb->dev = this_usbduxsub->usbdev;
687 if (this_usbduxsub->ao_cmd_running) {
688 if (this_usbduxsub->high_speed) {
695 urb->number_of_packets = 1;
696 urb->iso_frame_desc[0].offset = 0;
697 urb->iso_frame_desc[0].length = SIZEOUTBUF;
698 urb->iso_frame_desc[0].status = 0;
699 ret = usb_submit_urb(urb, GFP_ATOMIC);
701 dev_err(&urb->dev->dev,
702 "comedi_: ao urb resubm failed in int-cont. "
705 dev_err(&urb->dev->dev,
706 "buggy USB host controller or bug in "
709 s->async->events |= COMEDI_CB_EOA;
710 s->async->events |= COMEDI_CB_ERROR;
711 comedi_event(this_usbduxsub->comedidev, s);
712 /* don't do an unlink here */
713 usbdux_ao_stop(this_usbduxsub, 0);
718 static int usbduxsub_start(struct usbduxsub *usbduxsub)
721 uint8_t local_transfer_buffer[16];
724 local_transfer_buffer[0] = 0;
725 errcode = usb_control_msg(usbduxsub->usbdev,
726 /* create a pipe for a control transfer */
727 usb_sndctrlpipe(usbduxsub->usbdev, 0),
728 /* bRequest, "Firmware" */
736 /* address of the transfer buffer */
737 local_transfer_buffer,
743 dev_err(&usbduxsub->interface->dev,
744 "comedi_: control msg failed (start)\n");
750 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
754 uint8_t local_transfer_buffer[16];
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");
780 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
781 uint8_t *local_transfer_buffer,
782 unsigned int startAddr, unsigned int len)
786 errcode = usb_control_msg(usbduxsub->usbdev,
787 usb_sndctrlpipe(usbduxsub->usbdev, 0),
788 /* brequest, firmware */
796 /* our local safe buffer */
797 local_transfer_buffer,
802 dev_dbg(&usbduxsub->interface->dev,
803 "comedi_: result=%d\n", errcode);
805 dev_err(&usbduxsub->interface->dev,
806 "comedi_: upload failed\n");
812 static int firmwareUpload(struct usbduxsub *usbduxsub, uint8_t *firmwareBinary,
820 ret = usbduxsub_stop(usbduxsub);
822 dev_err(&usbduxsub->interface->dev,
823 "comedi_: can not stop firmware\n");
826 ret = usbduxsub_upload(usbduxsub, firmwareBinary, 0, sizeFirmware);
828 dev_err(&usbduxsub->interface->dev,
829 "comedi_: firmware upload failed\n");
832 ret = usbduxsub_start(usbduxsub);
834 dev_err(&usbduxsub->interface->dev,
835 "comedi_: can not start firmware\n");
841 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
848 /* Submit all URBs and start the transfer on the bus */
849 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
850 /* in case of a resubmission after an unlink... */
851 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
852 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
853 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
854 usbduxsub->urbIn[i]->status = 0;
855 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
856 dev_dbg(&usbduxsub->interface->dev,
857 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
858 usbduxsub->comedidev->minor, i,
859 (usbduxsub->urbIn[i]->context),
860 (usbduxsub->urbIn[i]->dev),
861 (usbduxsub->urbIn[i]->interval));
862 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
864 dev_err(&usbduxsub->interface->dev,
865 "comedi_: ai: usb_submit_urb(%d) error %d\n",
873 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
880 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
881 dev_dbg(&usbduxsub->interface->dev,
882 "comedi_: submitting out-urb[%d]\n", i);
883 /* in case of a resubmission after an unlink... */
884 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
885 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
886 usbduxsub->urbOut[i]->status = 0;
887 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
888 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
890 dev_err(&usbduxsub->interface->dev,
891 "comedi_: ao: usb_submit_urb(%d) error %d\n",
899 static int usbdux_ai_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
900 struct comedi_cmd *cmd)
903 unsigned int tmpTimer;
904 struct usbduxsub *this_usbduxsub = dev->private;
906 if (!(this_usbduxsub->probed))
909 dev_dbg(&this_usbduxsub->interface->dev,
910 "comedi%d: usbdux_ai_cmdtest\n", dev->minor);
912 /* make sure triggers are valid */
913 /* Only immediate triggers are allowed */
914 tmp = cmd->start_src;
915 cmd->start_src &= TRIG_NOW | TRIG_INT;
916 if (!cmd->start_src || tmp != cmd->start_src)
919 /* trigger should happen timed */
920 tmp = cmd->scan_begin_src;
921 /* start a new _scan_ with a timer */
922 cmd->scan_begin_src &= TRIG_TIMER;
923 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
926 /* scanning is continous */
927 tmp = cmd->convert_src;
928 cmd->convert_src &= TRIG_NOW;
929 if (!cmd->convert_src || tmp != cmd->convert_src)
932 /* issue a trigger when scan is finished and start a new scan */
933 tmp = cmd->scan_end_src;
934 cmd->scan_end_src &= TRIG_COUNT;
935 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
938 /* trigger at the end of count events or not, stop condition or not */
940 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
941 if (!cmd->stop_src || tmp != cmd->stop_src)
948 * step 2: make sure trigger sources are unique and mutually compatible
949 * note that mutual compatiblity is not an issue here
951 if (cmd->scan_begin_src != TRIG_FOLLOW &&
952 cmd->scan_begin_src != TRIG_EXT &&
953 cmd->scan_begin_src != TRIG_TIMER)
955 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
961 /* step 3: make sure arguments are trivially compatible */
962 if (cmd->start_arg != 0) {
967 if (cmd->scan_begin_src == TRIG_FOLLOW) {
968 /* internal trigger */
969 if (cmd->scan_begin_arg != 0) {
970 cmd->scan_begin_arg = 0;
975 if (cmd->scan_begin_src == TRIG_TIMER) {
976 if (this_usbduxsub->high_speed) {
978 * In high speed mode microframes are possible.
979 * However, during one microframe we can roughly
980 * sample one channel. Thus, the more channels
981 * are in the channel list the more time we need.
984 /* find a power of 2 for the number of channels */
985 while (i < (cmd->chanlist_len))
988 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
989 cmd->scan_begin_arg = 1000000 / 8 * i;
992 /* now calc the real sampling rate with all the
995 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
997 if (cmd->scan_begin_arg != tmpTimer) {
998 cmd->scan_begin_arg = tmpTimer;
1003 /* 1kHz scans every USB frame */
1004 if (cmd->scan_begin_arg < 1000000) {
1005 cmd->scan_begin_arg = 1000000;
1009 * calc the real sampling rate with the rounding errors
1011 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
1012 1000000)) * 1000000;
1013 if (cmd->scan_begin_arg != tmpTimer) {
1014 cmd->scan_begin_arg = tmpTimer;
1019 /* the same argument */
1020 if (cmd->scan_end_arg != cmd->chanlist_len) {
1021 cmd->scan_end_arg = cmd->chanlist_len;
1025 if (cmd->stop_src == TRIG_COUNT) {
1026 /* any count is allowed */
1029 if (cmd->stop_arg != 0) {
1042 * creates the ADC command for the MAX1271
1043 * range is the range value from comedi
1045 static int8_t create_adc_command(unsigned int chan, int range)
1047 int8_t p = (range <= 1);
1048 int8_t r = ((range % 2) == 0);
1049 return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
1052 /* bulk transfers to usbdux */
1054 #define SENDADCOMMANDS 0
1055 #define SENDDACOMMANDS 1
1056 #define SENDDIOCONFIGCOMMAND 2
1057 #define SENDDIOBITSCOMMAND 3
1058 #define SENDSINGLEAD 4
1059 #define READCOUNTERCOMMAND 5
1060 #define WRITECOUNTERCOMMAND 6
1062 #define SENDPWMOFF 8
1064 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1068 this_usbduxsub->dux_commands[0] = cmd_type;
1069 #ifdef NOISY_DUX_DEBUGBUG
1070 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1071 this_usbduxsub->comedidev->minor);
1072 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1073 printk(" %02x", this_usbduxsub->dux_commands[result]);
1076 result = usb_bulk_msg(this_usbduxsub->usbdev,
1077 usb_sndbulkpipe(this_usbduxsub->usbdev,
1079 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1082 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1083 "could not transmit dux_command to the usb-device, "
1084 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1089 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1091 int result = (-EFAULT);
1095 for (i = 0; i < RETRIES; i++) {
1096 result = usb_bulk_msg(this_usbduxsub->usbdev,
1097 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1099 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1102 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1103 "insn: USB error %d while receiving DUX command"
1104 "\n", this_usbduxsub->comedidev->minor, result);
1107 if (le16_to_cpu(this_usbduxsub->insnBuffer[0]) == command)
1110 /* this is only reached if the data has been requested a couple of
1112 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1113 "wrong data returned from firmware: want cmd %d, got cmd %d.\n",
1114 this_usbduxsub->comedidev->minor, command,
1115 le16_to_cpu(this_usbduxsub->insnBuffer[0]));
1119 static int usbdux_ai_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
1120 unsigned int trignum)
1123 struct usbduxsub *this_usbduxsub = dev->private;
1124 if (!this_usbduxsub)
1127 down(&this_usbduxsub->sem);
1128 if (!(this_usbduxsub->probed)) {
1129 up(&this_usbduxsub->sem);
1132 dev_dbg(&this_usbduxsub->interface->dev,
1133 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1136 dev_err(&this_usbduxsub->interface->dev,
1137 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1139 up(&this_usbduxsub->sem);
1142 if (!(this_usbduxsub->ai_cmd_running)) {
1143 this_usbduxsub->ai_cmd_running = 1;
1144 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1146 dev_err(&this_usbduxsub->interface->dev,
1147 "comedi%d: usbdux_ai_inttrig: "
1148 "urbSubmit: err=%d\n", dev->minor, ret);
1149 this_usbduxsub->ai_cmd_running = 0;
1150 up(&this_usbduxsub->sem);
1153 s->async->inttrig = NULL;
1155 dev_err(&this_usbduxsub->interface->dev,
1156 "comedi%d: ai_inttrig but acqu is already running\n",
1159 up(&this_usbduxsub->sem);
1163 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1165 struct comedi_cmd *cmd = &s->async->cmd;
1166 unsigned int chan, range;
1168 struct usbduxsub *this_usbduxsub = dev->private;
1171 if (!this_usbduxsub)
1174 dev_dbg(&this_usbduxsub->interface->dev,
1175 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1177 /* block other CPUs from starting an ai_cmd */
1178 down(&this_usbduxsub->sem);
1180 if (!(this_usbduxsub->probed)) {
1181 up(&this_usbduxsub->sem);
1184 if (this_usbduxsub->ai_cmd_running) {
1185 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1186 "ai_cmd not possible. Another ai_cmd is running.\n",
1188 up(&this_usbduxsub->sem);
1191 /* set current channel of the running aquisition to zero */
1192 s->async->cur_chan = 0;
1194 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1195 for (i = 0; i < cmd->chanlist_len; ++i) {
1196 chan = CR_CHAN(cmd->chanlist[i]);
1197 range = CR_RANGE(cmd->chanlist[i]);
1198 if (i >= NUMCHANNELS) {
1199 dev_err(&this_usbduxsub->interface->dev,
1200 "comedi%d: channel list too long\n",
1204 this_usbduxsub->dux_commands[i + 2] =
1205 create_adc_command(chan, range);
1208 dev_dbg(&this_usbduxsub->interface->dev,
1209 "comedi %d: sending commands to the usb device: size=%u\n",
1210 dev->minor, NUMCHANNELS);
1212 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1214 up(&this_usbduxsub->sem);
1218 if (this_usbduxsub->high_speed) {
1220 * every channel gets a time window of 125us. Thus, if we
1221 * sample all 8 channels we need 1ms. If we sample only one
1222 * channel we need only 125us
1224 this_usbduxsub->ai_interval = 1;
1225 /* find a power of 2 for the interval */
1226 while ((this_usbduxsub->ai_interval) < (cmd->chanlist_len)) {
1227 this_usbduxsub->ai_interval =
1228 (this_usbduxsub->ai_interval) * 2;
1230 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1231 (this_usbduxsub->ai_interval));
1233 /* interval always 1ms */
1234 this_usbduxsub->ai_interval = 1;
1235 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1237 if (this_usbduxsub->ai_timer < 1) {
1238 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1239 "timer=%d, scan_begin_arg=%d. "
1240 "Not properly tested by cmdtest?\n", dev->minor,
1241 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1242 up(&this_usbduxsub->sem);
1245 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1247 if (cmd->stop_src == TRIG_COUNT) {
1248 /* data arrives as one packet */
1249 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1250 this_usbduxsub->ai_continous = 0;
1252 /* continous aquisition */
1253 this_usbduxsub->ai_continous = 1;
1254 this_usbduxsub->ai_sample_count = 0;
1257 if (cmd->start_src == TRIG_NOW) {
1258 /* enable this acquisition operation */
1259 this_usbduxsub->ai_cmd_running = 1;
1260 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1262 this_usbduxsub->ai_cmd_running = 0;
1263 /* fixme: unlink here?? */
1264 up(&this_usbduxsub->sem);
1267 s->async->inttrig = NULL;
1270 /* don't enable the acquision operation */
1271 /* wait for an internal signal */
1272 s->async->inttrig = usbdux_ai_inttrig;
1274 up(&this_usbduxsub->sem);
1278 /* Mode 0 is used to get a single conversion on demand */
1279 static int usbdux_ai_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
1280 struct comedi_insn *insn, unsigned int *data)
1283 unsigned int one = 0;
1286 struct usbduxsub *this_usbduxsub = dev->private;
1288 if (!this_usbduxsub)
1291 dev_dbg(&this_usbduxsub->interface->dev,
1292 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1293 dev->minor, insn->n, insn->subdev);
1295 down(&this_usbduxsub->sem);
1296 if (!(this_usbduxsub->probed)) {
1297 up(&this_usbduxsub->sem);
1300 if (this_usbduxsub->ai_cmd_running) {
1301 dev_err(&this_usbduxsub->interface->dev,
1302 "comedi%d: ai_insn_read not possible. "
1303 "Async Command is running.\n", dev->minor);
1304 up(&this_usbduxsub->sem);
1308 /* sample one channel */
1309 chan = CR_CHAN(insn->chanspec);
1310 range = CR_RANGE(insn->chanspec);
1311 /* set command for the first channel */
1312 this_usbduxsub->dux_commands[1] = create_adc_command(chan, range);
1315 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1317 up(&this_usbduxsub->sem);
1321 for (i = 0; i < insn->n; i++) {
1322 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1324 up(&this_usbduxsub->sem);
1327 one = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1328 if (CR_RANGE(insn->chanspec) <= 1)
1333 up(&this_usbduxsub->sem);
1337 /************************************/
1340 static int usbdux_ao_insn_read(struct comedi_device *dev, struct comedi_subdevice *s,
1341 struct comedi_insn *insn, unsigned int *data)
1344 int chan = CR_CHAN(insn->chanspec);
1345 struct usbduxsub *this_usbduxsub = dev->private;
1347 if (!this_usbduxsub)
1350 down(&this_usbduxsub->sem);
1351 if (!(this_usbduxsub->probed)) {
1352 up(&this_usbduxsub->sem);
1355 for (i = 0; i < insn->n; i++)
1356 data[i] = this_usbduxsub->outBuffer[chan];
1358 up(&this_usbduxsub->sem);
1362 static int usbdux_ao_insn_write(struct comedi_device *dev, struct comedi_subdevice *s,
1363 struct comedi_insn *insn, unsigned int *data)
1366 int chan = CR_CHAN(insn->chanspec);
1367 struct usbduxsub *this_usbduxsub = dev->private;
1369 if (!this_usbduxsub)
1372 dev_dbg(&this_usbduxsub->interface->dev,
1373 "comedi%d: ao_insn_write\n", dev->minor);
1375 down(&this_usbduxsub->sem);
1376 if (!(this_usbduxsub->probed)) {
1377 up(&this_usbduxsub->sem);
1380 if (this_usbduxsub->ao_cmd_running) {
1381 dev_err(&this_usbduxsub->interface->dev,
1382 "comedi%d: ao_insn_write: "
1383 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1384 up(&this_usbduxsub->sem);
1388 for (i = 0; i < insn->n; i++) {
1389 dev_dbg(&this_usbduxsub->interface->dev,
1390 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1391 dev->minor, chan, i, data[i]);
1393 /* number of channels: 1 */
1394 this_usbduxsub->dux_commands[1] = 1;
1395 /* one 16 bit value */
1396 *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
1397 cpu_to_le16(data[i]);
1398 this_usbduxsub->outBuffer[chan] = data[i];
1399 /* channel number */
1400 this_usbduxsub->dux_commands[4] = (chan << 6);
1401 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1403 up(&this_usbduxsub->sem);
1407 up(&this_usbduxsub->sem);
1412 static int usbdux_ao_inttrig(struct comedi_device *dev, struct comedi_subdevice *s,
1413 unsigned int trignum)
1416 struct usbduxsub *this_usbduxsub = dev->private;
1418 if (!this_usbduxsub)
1421 down(&this_usbduxsub->sem);
1422 if (!(this_usbduxsub->probed)) {
1423 up(&this_usbduxsub->sem);
1427 dev_err(&this_usbduxsub->interface->dev,
1428 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1432 if (!(this_usbduxsub->ao_cmd_running)) {
1433 this_usbduxsub->ao_cmd_running = 1;
1434 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1436 dev_err(&this_usbduxsub->interface->dev,
1437 "comedi%d: usbdux_ao_inttrig: submitURB: "
1438 "err=%d\n", dev->minor, ret);
1439 this_usbduxsub->ao_cmd_running = 0;
1440 up(&this_usbduxsub->sem);
1443 s->async->inttrig = NULL;
1445 dev_err(&this_usbduxsub->interface->dev,
1446 "comedi%d: ao_inttrig but acqu is already running.\n",
1449 up(&this_usbduxsub->sem);
1453 static int usbdux_ao_cmdtest(struct comedi_device *dev, struct comedi_subdevice *s,
1454 struct comedi_cmd *cmd)
1457 struct usbduxsub *this_usbduxsub = dev->private;
1459 if (!this_usbduxsub)
1462 if (!(this_usbduxsub->probed))
1465 dev_dbg(&this_usbduxsub->interface->dev,
1466 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1468 /* make sure triggers are valid */
1469 /* Only immediate triggers are allowed */
1470 tmp = cmd->start_src;
1471 cmd->start_src &= TRIG_NOW | TRIG_INT;
1472 if (!cmd->start_src || tmp != cmd->start_src)
1475 /* trigger should happen timed */
1476 tmp = cmd->scan_begin_src;
1477 /* just now we scan also in the high speed mode every frame */
1478 /* this is due to ehci driver limitations */
1479 if (0) { /* (this_usbduxsub->high_speed) */
1480 /* start immidiately a new scan */
1481 /* the sampling rate is set by the coversion rate */
1482 cmd->scan_begin_src &= TRIG_FOLLOW;
1484 /* start a new scan (output at once) with a timer */
1485 cmd->scan_begin_src &= TRIG_TIMER;
1487 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1490 /* scanning is continous */
1491 tmp = cmd->convert_src;
1492 /* we always output at 1kHz just now all channels at once */
1493 if (0) { /* (this_usbduxsub->high_speed) */
1495 * in usb-2.0 only one conversion it tranmitted but with 8kHz/n
1497 cmd->convert_src &= TRIG_TIMER;
1499 /* all conversion events happen simultaneously with a rate of
1501 cmd->convert_src &= TRIG_NOW;
1503 if (!cmd->convert_src || tmp != cmd->convert_src)
1506 /* issue a trigger when scan is finished and start a new scan */
1507 tmp = cmd->scan_end_src;
1508 cmd->scan_end_src &= TRIG_COUNT;
1509 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1512 /* trigger at the end of count events or not, stop condition or not */
1513 tmp = cmd->stop_src;
1514 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1515 if (!cmd->stop_src || tmp != cmd->stop_src)
1522 * step 2: make sure trigger sources are unique and mutually compatible
1523 * note that mutual compatiblity is not an issue here
1525 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1526 cmd->scan_begin_src != TRIG_EXT &&
1527 cmd->scan_begin_src != TRIG_TIMER)
1529 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1535 /* step 3: make sure arguments are trivially compatible */
1537 if (cmd->start_arg != 0) {
1542 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1543 /* internal trigger */
1544 if (cmd->scan_begin_arg != 0) {
1545 cmd->scan_begin_arg = 0;
1550 if (cmd->scan_begin_src == TRIG_TIMER) {
1552 if (cmd->scan_begin_arg < 1000000) {
1553 cmd->scan_begin_arg = 1000000;
1557 /* not used now, is for later use */
1558 if (cmd->convert_src == TRIG_TIMER) {
1559 if (cmd->convert_arg < 125000) {
1560 cmd->convert_arg = 125000;
1565 /* the same argument */
1566 if (cmd->scan_end_arg != cmd->chanlist_len) {
1567 cmd->scan_end_arg = cmd->chanlist_len;
1571 if (cmd->stop_src == TRIG_COUNT) {
1572 /* any count is allowed */
1575 if (cmd->stop_arg != 0) {
1581 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: err=%d, "
1582 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1583 "convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src,
1584 cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1592 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1594 struct comedi_cmd *cmd = &s->async->cmd;
1595 unsigned int chan, gain;
1597 struct usbduxsub *this_usbduxsub = dev->private;
1599 if (!this_usbduxsub)
1602 down(&this_usbduxsub->sem);
1603 if (!(this_usbduxsub->probed)) {
1604 up(&this_usbduxsub->sem);
1607 dev_dbg(&this_usbduxsub->interface->dev,
1608 "comedi%d: %s\n", dev->minor, __func__);
1610 /* set current channel of the running aquisition to zero */
1611 s->async->cur_chan = 0;
1612 for (i = 0; i < cmd->chanlist_len; ++i) {
1613 chan = CR_CHAN(cmd->chanlist[i]);
1614 gain = CR_RANGE(cmd->chanlist[i]);
1615 if (i >= NUMOUTCHANNELS) {
1616 dev_err(&this_usbduxsub->interface->dev,
1617 "comedi%d: %s: channel list too long\n",
1618 dev->minor, __func__);
1621 this_usbduxsub->dac_commands[i] = (chan << 6);
1622 dev_dbg(&this_usbduxsub->interface->dev,
1623 "comedi%d: dac command for ch %d is %x\n",
1624 dev->minor, i, this_usbduxsub->dac_commands[i]);
1627 /* we count in steps of 1ms (125us) */
1628 /* 125us mode not used yet */
1629 if (0) { /* (this_usbduxsub->high_speed) */
1631 /* timing of the conversion itself: every 125 us */
1632 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1635 /* timing of the scan: we get all channels at once */
1636 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1637 dev_dbg(&this_usbduxsub->interface->dev,
1638 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1639 "convert_src=%d, convert_arg=%d\n", dev->minor,
1640 cmd->scan_begin_src, cmd->scan_begin_arg,
1641 cmd->convert_src, cmd->convert_arg);
1642 dev_dbg(&this_usbduxsub->interface->dev,
1643 "comedi%d: ao_timer=%d (ms)\n",
1644 dev->minor, this_usbduxsub->ao_timer);
1645 if (this_usbduxsub->ao_timer < 1) {
1646 dev_err(&this_usbduxsub->interface->dev,
1647 "comedi%d: usbdux: ao_timer=%d, "
1648 "scan_begin_arg=%d. "
1649 "Not properly tested by cmdtest?\n",
1650 dev->minor, this_usbduxsub->ao_timer,
1651 cmd->scan_begin_arg);
1652 up(&this_usbduxsub->sem);
1656 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1658 if (cmd->stop_src == TRIG_COUNT) {
1661 /* high speed also scans everything at once */
1662 if (0) { /* (this_usbduxsub->high_speed) */
1663 this_usbduxsub->ao_sample_count =
1664 (cmd->stop_arg) * (cmd->scan_end_arg);
1666 /* there's no scan as the scan has been */
1667 /* perf inside the FX2 */
1668 /* data arrives as one packet */
1669 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1671 this_usbduxsub->ao_continous = 0;
1673 /* continous aquisition */
1674 this_usbduxsub->ao_continous = 1;
1675 this_usbduxsub->ao_sample_count = 0;
1678 if (cmd->start_src == TRIG_NOW) {
1679 /* enable this acquisition operation */
1680 this_usbduxsub->ao_cmd_running = 1;
1681 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1683 this_usbduxsub->ao_cmd_running = 0;
1684 /* fixme: unlink here?? */
1685 up(&this_usbduxsub->sem);
1688 s->async->inttrig = NULL;
1691 /* submit the urbs later */
1692 /* wait for an internal signal */
1693 s->async->inttrig = usbdux_ao_inttrig;
1696 up(&this_usbduxsub->sem);
1700 static int usbdux_dio_insn_config(struct comedi_device *dev, struct comedi_subdevice *s,
1701 struct comedi_insn *insn, unsigned int *data)
1703 int chan = CR_CHAN(insn->chanspec);
1705 /* The input or output configuration of each digital line is
1706 * configured by a special insn_config instruction. chanspec
1707 * contains the channel to be changed, and data[0] contains the
1708 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1711 case INSN_CONFIG_DIO_OUTPUT:
1712 s->io_bits |= 1 << chan; /* 1 means Out */
1714 case INSN_CONFIG_DIO_INPUT:
1715 s->io_bits &= ~(1 << chan);
1717 case INSN_CONFIG_DIO_QUERY:
1720 io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1726 /* we don't tell the firmware here as it would take 8 frames */
1727 /* to submit the information. We do it in the insn_bits. */
1731 static int usbdux_dio_insn_bits(struct comedi_device *dev, struct comedi_subdevice *s,
1732 struct comedi_insn *insn, unsigned int *data)
1735 struct usbduxsub *this_usbduxsub = dev->private;
1738 if (!this_usbduxsub)
1745 down(&this_usbduxsub->sem);
1747 if (!(this_usbduxsub->probed)) {
1748 up(&this_usbduxsub->sem);
1752 /* The insn data is a mask in data[0] and the new data
1753 * in data[1], each channel cooresponding to a bit. */
1754 s->state &= ~data[0];
1755 s->state |= data[0] & data[1];
1756 this_usbduxsub->dux_commands[1] = s->io_bits;
1757 this_usbduxsub->dux_commands[2] = s->state;
1759 /* This command also tells the firmware to return */
1760 /* the digital input lines */
1761 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1763 up(&this_usbduxsub->sem);
1766 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1768 up(&this_usbduxsub->sem);
1772 data[1] = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1773 up(&this_usbduxsub->sem);
1777 /* reads the 4 counters, only two are used just now */
1778 static int usbdux_counter_read(struct comedi_device *dev, struct comedi_subdevice *s,
1779 struct comedi_insn *insn, unsigned int *data)
1781 struct usbduxsub *this_usbduxsub = dev->private;
1782 int chan = insn->chanspec;
1785 if (!this_usbduxsub)
1788 down(&this_usbduxsub->sem);
1790 if (!(this_usbduxsub->probed)) {
1791 up(&this_usbduxsub->sem);
1795 err = send_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1797 up(&this_usbduxsub->sem);
1801 err = receive_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1803 up(&this_usbduxsub->sem);
1807 data[0] = le16_to_cpu(this_usbduxsub->insnBuffer[chan + 1]);
1808 up(&this_usbduxsub->sem);
1812 static int usbdux_counter_write(struct comedi_device *dev, struct comedi_subdevice *s,
1813 struct comedi_insn *insn, unsigned int *data)
1815 struct usbduxsub *this_usbduxsub = dev->private;
1818 if (!this_usbduxsub)
1821 down(&this_usbduxsub->sem);
1823 if (!(this_usbduxsub->probed)) {
1824 up(&this_usbduxsub->sem);
1828 this_usbduxsub->dux_commands[1] = insn->chanspec;
1829 *((int16_t *) (this_usbduxsub->dux_commands + 2)) = cpu_to_le16(*data);
1831 err = send_dux_commands(this_usbduxsub, WRITECOUNTERCOMMAND);
1833 up(&this_usbduxsub->sem);
1837 up(&this_usbduxsub->sem);
1842 static int usbdux_counter_config(struct comedi_device *dev, struct comedi_subdevice *s,
1843 struct comedi_insn *insn, unsigned int *data)
1845 /* nothing to do so far */
1849 /***********************************/
1852 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1856 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1857 if (usbduxsub_tmp->urbPwm)
1858 usb_kill_urb(usbduxsub_tmp->urbPwm);
1859 dev_dbg(&usbduxsub_tmp->interface->dev,
1860 "comedi: unlinked PwmURB: res=%d\n", err);
1865 /* This cancels a running acquisition operation
1868 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1872 if (!this_usbduxsub)
1875 dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1877 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1880 this_usbduxsub->pwm_cmd_running = 0;
1885 /* force unlink - is called by comedi */
1886 static int usbdux_pwm_cancel(struct comedi_device *dev, struct comedi_subdevice *s)
1888 struct usbduxsub *this_usbduxsub = dev->private;
1891 /* unlink only if it is really running */
1892 res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1894 dev_dbg(&this_usbduxsub->interface->dev,
1895 "comedi %d: sending pwm off command to the usb device.\n",
1897 res = send_dux_commands(this_usbduxsub, SENDPWMOFF);
1904 static void usbduxsub_pwm_irq(struct urb *urb)
1907 struct usbduxsub *this_usbduxsub;
1908 struct comedi_device *this_comedidev;
1909 struct comedi_subdevice *s;
1911 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1913 /* the context variable points to the subdevice */
1914 this_comedidev = urb->context;
1915 /* the private structure of the subdevice is struct usbduxsub */
1916 this_usbduxsub = this_comedidev->private;
1918 s = this_comedidev->subdevices + SUBDEV_DA;
1920 switch (urb->status) {
1930 * after an unlink command, unplug, ... etc
1931 * no unlink needed here. Already shutting down.
1933 if (this_usbduxsub->pwm_cmd_running)
1934 usbdux_pwm_stop(this_usbduxsub, 0);
1940 if (this_usbduxsub->pwm_cmd_running) {
1941 dev_err(&this_usbduxsub->interface->dev,
1942 "comedi_: Non-zero urb status received in "
1943 "pwm intr context: %d\n", urb->status);
1944 usbdux_pwm_stop(this_usbduxsub, 0);
1949 /* are we actually running? */
1950 if (!(this_usbduxsub->pwm_cmd_running))
1953 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1954 urb->dev = this_usbduxsub->usbdev;
1956 if (this_usbduxsub->pwm_cmd_running) {
1957 ret = usb_submit_urb(urb, GFP_ATOMIC);
1959 dev_err(&this_usbduxsub->interface->dev,
1960 "comedi_: pwm urb resubm failed in int-cont. "
1962 if (ret == EL2NSYNC)
1963 dev_err(&this_usbduxsub->interface->dev,
1964 "buggy USB host controller or bug in "
1967 /* don't do an unlink here */
1968 usbdux_pwm_stop(this_usbduxsub, 0);
1973 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
1980 dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
1982 /* in case of a resubmission after an unlink... */
1983 usb_fill_bulk_urb(usbduxsub->urbPwm,
1985 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
1986 usbduxsub->urbPwm->transfer_buffer,
1987 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq, usbduxsub->comedidev);
1989 errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
1991 dev_err(&usbduxsub->interface->dev,
1992 "comedi_: usbdux: pwm: usb_submit_urb error %d\n",
1999 static int usbdux_pwm_period(struct comedi_device *dev, struct comedi_subdevice *s,
2000 unsigned int period)
2002 struct usbduxsub *this_usbduxsub = dev->private;
2005 if (period < MIN_PWM_PERIOD) {
2006 dev_err(&this_usbduxsub->interface->dev,
2007 "comedi%d: illegal period setting for pwm.\n",
2011 fx2delay = period / ((int)(6*512*(1.0/0.033))) - 6;
2012 if (fx2delay > 255) {
2013 dev_err(&this_usbduxsub->interface->dev,
2014 "comedi%d: period %d for pwm is too low.\n",
2015 dev->minor, period);
2019 this_usbduxsub->pwmDelay = fx2delay;
2020 this_usbduxsub->pwmPeriod = period;
2021 dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
2022 __func__, period, fx2delay);
2026 /* is called from insn so there's no need to do all the sanity checks */
2027 static int usbdux_pwm_start(struct comedi_device *dev, struct comedi_subdevice *s)
2030 struct usbduxsub *this_usbduxsub = dev->private;
2032 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
2033 dev->minor, __func__);
2035 if (this_usbduxsub->pwm_cmd_running) {
2036 /* already running */
2040 this_usbduxsub->dux_commands[1] = ((int8_t) this_usbduxsub->pwmDelay);
2041 ret = send_dux_commands(this_usbduxsub, SENDPWMON);
2045 /* initalise the buffer */
2046 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
2047 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
2049 this_usbduxsub->pwm_cmd_running = 1;
2050 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
2052 this_usbduxsub->pwm_cmd_running = 0;
2058 /* generates the bit pattern for PWM with the optional sign bit */
2059 static int usbdux_pwm_pattern(struct comedi_device *dev, struct comedi_subdevice *s,
2060 int channel, unsigned int value, unsigned int sign)
2062 struct usbduxsub *this_usbduxsub = dev->private;
2069 if (!this_usbduxsub)
2072 /* this is the DIO bit which carries the PWM data */
2073 pwm_mask = (1 << channel);
2074 /* this is the DIO bit which carries the optional direction bit */
2075 sgn_mask = (16 << channel);
2076 /* this is the buffer which will be filled with the with bit */
2077 /* pattern for one period */
2078 szbuf = this_usbduxsub->sizePwmBuf;
2079 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2080 for (i = 0; i < szbuf; i++) {
2083 c = c & (~pwm_mask);
2084 /* set the bit as long as the index is lower than the value */
2087 /* set the optional sign bit for a relay */
2089 /* positive value */
2090 c = c & (~sgn_mask);
2092 /* negative value */
2100 static int usbdux_pwm_write(struct comedi_device *dev, struct comedi_subdevice *s,
2101 struct comedi_insn *insn, unsigned int *data)
2103 struct usbduxsub *this_usbduxsub = dev->private;
2105 if (!this_usbduxsub)
2108 if ((insn->n) != 1) {
2110 * doesn't make sense to have more than one value here because
2111 * it would just overwrite the PWM buffer a couple of times
2117 * the sign is set via a special INSN only, this gives us 8 bits for
2119 * relay sign 0 by default
2121 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec),
2125 static int usbdux_pwm_read(struct comedi_device *x1, struct comedi_subdevice *x2,
2126 struct comedi_insn *x3, unsigned int *x4)
2132 /* switches on/off PWM */
2133 static int usbdux_pwm_config(struct comedi_device *dev, struct comedi_subdevice *s,
2134 struct comedi_insn *insn, unsigned int *data)
2136 struct usbduxsub *this_usbduxsub = dev->private;
2138 case INSN_CONFIG_ARM:
2140 dev_dbg(&this_usbduxsub->interface->dev,
2141 "comedi%d: %s: pwm on\n", dev->minor, __func__);
2143 * if not zero the PWM is limited to a certain time which is
2144 * not supported here
2148 return usbdux_pwm_start(dev, s);
2149 case INSN_CONFIG_DISARM:
2150 dev_dbg(&this_usbduxsub->interface->dev,
2151 "comedi%d: %s: pwm off\n", dev->minor, __func__);
2152 return usbdux_pwm_cancel(dev, s);
2153 case INSN_CONFIG_GET_PWM_STATUS:
2155 * to check if the USB transmission has failed or in case PWM
2156 * was limited to n cycles to check if it has terminated
2158 data[1] = this_usbduxsub->pwm_cmd_running;
2160 case INSN_CONFIG_PWM_SET_PERIOD:
2161 dev_dbg(&this_usbduxsub->interface->dev,
2162 "comedi%d: %s: setting period\n", dev->minor, __func__);
2163 return usbdux_pwm_period(dev, s, data[1]);
2164 case INSN_CONFIG_PWM_GET_PERIOD:
2165 data[1] = this_usbduxsub->pwmPeriod;
2167 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2168 /* value in the first byte and the sign in the second for a
2170 return usbdux_pwm_pattern(dev, s,
2171 /* the channel number */
2172 CR_CHAN(insn->chanspec),
2173 /* actual PWM data */
2177 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2178 /* values are not kept in this driver, nothing to return here */
2185 /*****************************************************************/
2187 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2193 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2195 /* shows the usb subsystem that the driver is down */
2196 if (usbduxsub_tmp->interface)
2197 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2199 usbduxsub_tmp->probed = 0;
2201 if (usbduxsub_tmp->urbIn) {
2202 if (usbduxsub_tmp->ai_cmd_running) {
2203 usbduxsub_tmp->ai_cmd_running = 0;
2204 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2206 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2207 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2208 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2209 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2210 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2211 usbduxsub_tmp->urbIn[i] = NULL;
2213 kfree(usbduxsub_tmp->urbIn);
2214 usbduxsub_tmp->urbIn = NULL;
2216 if (usbduxsub_tmp->urbOut) {
2217 if (usbduxsub_tmp->ao_cmd_running) {
2218 usbduxsub_tmp->ao_cmd_running = 0;
2219 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2221 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2222 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2223 kfree(usbduxsub_tmp->urbOut[i]->
2225 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2228 if (usbduxsub_tmp->urbOut[i]) {
2229 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2230 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2231 usbduxsub_tmp->urbOut[i] = NULL;
2234 kfree(usbduxsub_tmp->urbOut);
2235 usbduxsub_tmp->urbOut = NULL;
2237 if (usbduxsub_tmp->urbPwm) {
2238 if (usbduxsub_tmp->pwm_cmd_running) {
2239 usbduxsub_tmp->pwm_cmd_running = 0;
2240 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2242 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2243 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2244 usb_kill_urb(usbduxsub_tmp->urbPwm);
2245 usb_free_urb(usbduxsub_tmp->urbPwm);
2246 usbduxsub_tmp->urbPwm = NULL;
2248 kfree(usbduxsub_tmp->inBuffer);
2249 usbduxsub_tmp->inBuffer = NULL;
2250 kfree(usbduxsub_tmp->insnBuffer);
2251 usbduxsub_tmp->insnBuffer = NULL;
2252 kfree(usbduxsub_tmp->inBuffer);
2253 usbduxsub_tmp->inBuffer = NULL;
2254 kfree(usbduxsub_tmp->dac_commands);
2255 usbduxsub_tmp->dac_commands = NULL;
2256 kfree(usbduxsub_tmp->dux_commands);
2257 usbduxsub_tmp->dux_commands = NULL;
2258 usbduxsub_tmp->ai_cmd_running = 0;
2259 usbduxsub_tmp->ao_cmd_running = 0;
2260 usbduxsub_tmp->pwm_cmd_running = 0;
2263 static unsigned hex2unsigned(char *h)
2268 hi = h[0] - 'A' + 0x0a;
2273 lo = h[1] - 'A' + 0x0a;
2277 return hi * 0x10 + lo;
2281 #define FIRMWARE_MAX_LEN 0x2000
2283 /* taken from David Brownell's fxload and adjusted for this driver */
2284 static int read_firmware(struct usbduxsub *usbduxsub, const void *firmwarePtr,
2287 struct device *dev = &usbduxsub->interface->dev;
2289 unsigned char *fp = (char *)firmwarePtr;
2290 unsigned char *firmwareBinary;
2294 firmwareBinary = kzalloc(FIRMWARE_MAX_LEN, GFP_KERNEL);
2295 if (!firmwareBinary) {
2296 dev_err(dev, "comedi_: mem alloc for firmware failed\n");
2308 while ((i < size) && (fp[i] != 13) && (fp[i] != 10)) {
2312 if (j >= sizeof(buf)) {
2313 dev_err(dev, "comedi_: bogus firmware file!\n");
2314 kfree(firmwareBinary);
2318 /* get rid of LF/CR/... */
2319 while ((i < size) && ((fp[i] == 13) || (fp[i] == 10)
2325 /* dev_dbg(dev, "comedi_: buf=%s\n", buf); */
2329 * "# comment-till-end-of-line", for copyrights etc
2334 if (buf[0] != ':') {
2335 dev_err(dev, "comedi_: upload: not an ihex record: %s",
2337 kfree(firmwareBinary);
2341 /* Read the length field (up to 16 bytes) */
2342 len = hex2unsigned(buf + 1);
2344 /* Read the target offset */
2345 off = (hex2unsigned(buf + 3) * 0x0100) + hex2unsigned(buf + 5);
2347 if ((off + len) > maxAddr)
2348 maxAddr = off + len;
2351 if (maxAddr >= FIRMWARE_MAX_LEN) {
2352 dev_err(dev, "comedi_: firmware upload goes "
2353 "beyond FX2 RAM boundaries.\n");
2354 kfree(firmwareBinary);
2357 /* dev_dbg(dev, "comedi_: off=%x, len=%x:\n", off, len); */
2359 /* Read the record type */
2360 type = hex2unsigned(buf + 7);
2362 /* If this is an EOF record, then make it so. */
2368 dev_err(dev, "comedi_: unsupported record type: %u\n",
2370 kfree(firmwareBinary);
2374 for (idx = 0, cp = buf + 9; idx < len; idx += 1, cp += 2) {
2375 firmwareBinary[idx + off] = hex2unsigned(cp);
2376 /*printk("%02x ",firmwareBinary[idx+off]); */
2381 dev_err(dev, "comedi_: unexpected end of hex file\n");
2386 res = firmwareUpload(usbduxsub, firmwareBinary, maxAddr + 1);
2387 kfree(firmwareBinary);
2391 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2394 struct usbduxsub *usbduxsub_tmp = context;
2395 struct usb_device *usbdev = usbduxsub_tmp->usbdev;
2399 dev_err(&usbdev->dev,
2400 "Firmware complete handler without firmware!\n");
2405 * we need to upload the firmware here because fw will be
2406 * freed once we've left this function
2408 ret = read_firmware(usbduxsub_tmp, fw->data, fw->size);
2411 dev_err(&usbdev->dev,
2412 "Could not upload firmware (err=%d)\n",
2416 comedi_usb_auto_config(usbdev, BOARDNAME);
2419 /* allocate memory for the urbs and initialise them */
2420 static int usbduxsub_probe(struct usb_interface *uinterf,
2421 const struct usb_device_id *id)
2423 struct usb_device *udev = interface_to_usbdev(uinterf);
2424 struct device *dev = &uinterf->dev;
2429 dev_dbg(dev, "comedi_: usbdux_: "
2430 "finding a free structure for the usb-device\n");
2432 down(&start_stop_sem);
2433 /* look for a free place in the usbdux array */
2435 for (i = 0; i < NUMUSBDUX; i++) {
2436 if (!(usbduxsub[i].probed)) {
2444 dev_err(dev, "Too many usbdux-devices connected.\n");
2445 up(&start_stop_sem);
2448 dev_dbg(dev, "comedi_: usbdux: "
2449 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2451 init_MUTEX(&(usbduxsub[index].sem));
2452 /* save a pointer to the usb device */
2453 usbduxsub[index].usbdev = udev;
2455 /* 2.6: save the interface itself */
2456 usbduxsub[index].interface = uinterf;
2457 /* get the interface number from the interface */
2458 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2459 /* hand the private data over to the usb subsystem */
2460 /* will be needed for disconnect */
2461 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2463 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2465 /* test if it is high speed (USB 2.0) */
2466 usbduxsub[index].high_speed =
2467 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2469 /* create space for the commands of the DA converter */
2470 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2471 if (!usbduxsub[index].dac_commands) {
2472 dev_err(dev, "comedi_: usbdux: "
2473 "error alloc space for dac commands\n");
2474 tidy_up(&(usbduxsub[index]));
2475 up(&start_stop_sem);
2478 /* create space for the commands going to the usb device */
2479 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2480 if (!usbduxsub[index].dux_commands) {
2481 dev_err(dev, "comedi_: usbdux: "
2482 "error alloc space for dac commands\n");
2483 tidy_up(&(usbduxsub[index]));
2484 up(&start_stop_sem);
2487 /* create space for the in buffer and set it to zero */
2488 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2489 if (!(usbduxsub[index].inBuffer)) {
2490 dev_err(dev, "comedi_: usbdux: "
2491 "could not alloc space for inBuffer\n");
2492 tidy_up(&(usbduxsub[index]));
2493 up(&start_stop_sem);
2496 /* create space of the instruction buffer */
2497 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2498 if (!(usbduxsub[index].insnBuffer)) {
2499 dev_err(dev, "comedi_: usbdux: "
2500 "could not alloc space for insnBuffer\n");
2501 tidy_up(&(usbduxsub[index]));
2502 up(&start_stop_sem);
2505 /* create space for the outbuffer */
2506 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2507 if (!(usbduxsub[index].outBuffer)) {
2508 dev_err(dev, "comedi_: usbdux: "
2509 "could not alloc space for outBuffer\n");
2510 tidy_up(&(usbduxsub[index]));
2511 up(&start_stop_sem);
2514 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2515 i = usb_set_interface(usbduxsub[index].usbdev,
2516 usbduxsub[index].ifnum, 3);
2518 dev_err(dev, "comedi_: usbdux%d: "
2519 "could not set alternate setting 3 in high speed.\n",
2521 tidy_up(&(usbduxsub[index]));
2522 up(&start_stop_sem);
2525 if (usbduxsub[index].high_speed)
2526 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2528 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2530 usbduxsub[index].urbIn =
2531 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2533 if (!(usbduxsub[index].urbIn)) {
2534 dev_err(dev, "comedi_: usbdux: Could not alloc. urbIn array\n");
2535 tidy_up(&(usbduxsub[index]));
2536 up(&start_stop_sem);
2539 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2540 /* one frame: 1ms */
2541 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2542 if (usbduxsub[index].urbIn[i] == NULL) {
2543 dev_err(dev, "comedi_: usbdux%d: "
2544 "Could not alloc. urb(%d)\n", index, i);
2545 tidy_up(&(usbduxsub[index]));
2546 up(&start_stop_sem);
2549 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2550 /* will be filled later with a pointer to the comedi-device */
2551 /* and ONLY then the urb should be submitted */
2552 usbduxsub[index].urbIn[i]->context = NULL;
2553 usbduxsub[index].urbIn[i]->pipe =
2554 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2555 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2556 usbduxsub[index].urbIn[i]->transfer_buffer =
2557 kzalloc(SIZEINBUF, GFP_KERNEL);
2558 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2559 dev_err(dev, "comedi_: usbdux%d: "
2560 "could not alloc. transb.\n", index);
2561 tidy_up(&(usbduxsub[index]));
2562 up(&start_stop_sem);
2565 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2566 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2567 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2568 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2569 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length = SIZEINBUF;
2573 if (usbduxsub[index].high_speed)
2574 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2576 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2578 usbduxsub[index].urbOut =
2579 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2581 if (!(usbduxsub[index].urbOut)) {
2582 dev_err(dev, "comedi_: usbdux: "
2583 "Could not alloc. urbOut array\n");
2584 tidy_up(&(usbduxsub[index]));
2585 up(&start_stop_sem);
2588 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2589 /* one frame: 1ms */
2590 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2591 if (usbduxsub[index].urbOut[i] == NULL) {
2592 dev_err(dev, "comedi_: usbdux%d: "
2593 "Could not alloc. urb(%d)\n", index, i);
2594 tidy_up(&(usbduxsub[index]));
2595 up(&start_stop_sem);
2598 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2599 /* will be filled later with a pointer to the comedi-device */
2600 /* and ONLY then the urb should be submitted */
2601 usbduxsub[index].urbOut[i]->context = NULL;
2602 usbduxsub[index].urbOut[i]->pipe =
2603 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2604 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2605 usbduxsub[index].urbOut[i]->transfer_buffer =
2606 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2607 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2608 dev_err(dev, "comedi_: usbdux%d: "
2609 "could not alloc. transb.\n", index);
2610 tidy_up(&(usbduxsub[index]));
2611 up(&start_stop_sem);
2614 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2615 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2616 usbduxsub[index].urbOut[i]->transfer_buffer_length = SIZEOUTBUF;
2617 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2618 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2620 if (usbduxsub[index].high_speed) {
2622 usbduxsub[index].urbOut[i]->interval = 8;
2625 usbduxsub[index].urbOut[i]->interval = 1;
2630 if (usbduxsub[index].high_speed) {
2631 /* max bulk ep size in high speed */
2632 usbduxsub[index].sizePwmBuf = 512;
2633 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2634 if (usbduxsub[index].urbPwm == NULL) {
2635 dev_err(dev, "comedi_: usbdux%d: "
2636 "Could not alloc. pwm urb\n", index);
2637 tidy_up(&(usbduxsub[index]));
2638 up(&start_stop_sem);
2641 usbduxsub[index].urbPwm->transfer_buffer =
2642 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2643 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2644 dev_err(dev, "comedi_: usbdux%d: "
2645 "could not alloc. transb. for pwm\n", index);
2646 tidy_up(&(usbduxsub[index]));
2647 up(&start_stop_sem);
2651 usbduxsub[index].urbPwm = NULL;
2652 usbduxsub[index].sizePwmBuf = 0;
2655 usbduxsub[index].ai_cmd_running = 0;
2656 usbduxsub[index].ao_cmd_running = 0;
2657 usbduxsub[index].pwm_cmd_running = 0;
2659 /* we've reached the bottom of the function */
2660 usbduxsub[index].probed = 1;
2661 up(&start_stop_sem);
2663 ret = request_firmware_nowait(THIS_MODULE,
2665 "usbdux_firmware.hex",
2668 usbdux_firmware_request_complete_handler);
2671 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2675 dev_info(dev, "comedi_: usbdux%d "
2676 "has been successfully initialised.\n", index);
2681 static void usbduxsub_disconnect(struct usb_interface *intf)
2683 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2684 struct usb_device *udev = interface_to_usbdev(intf);
2686 if (!usbduxsub_tmp) {
2688 "comedi_: disconnect called with null pointer.\n");
2691 if (usbduxsub_tmp->usbdev != udev) {
2693 "comedi_: BUG! called with wrong ptr!!!\n");
2696 comedi_usb_auto_unconfig(udev);
2697 down(&start_stop_sem);
2698 down(&usbduxsub_tmp->sem);
2699 tidy_up(usbduxsub_tmp);
2700 up(&usbduxsub_tmp->sem);
2701 up(&start_stop_sem);
2702 dev_dbg(&intf->dev, "comedi_: disconnected from the usb\n");
2705 /* is called when comedi-config is called */
2706 static int usbdux_attach(struct comedi_device *dev, struct comedi_devconfig *it)
2711 struct usbduxsub *udev;
2713 struct comedi_subdevice *s = NULL;
2714 dev->private = NULL;
2716 down(&start_stop_sem);
2717 /* find a valid device which has been detected by the probe function of
2720 for (i = 0; i < NUMUSBDUX; i++) {
2721 if ((usbduxsub[i].probed) && (!usbduxsub[i].attached)) {
2728 printk(KERN_ERR "comedi%d: usbdux: error: attach failed, no "
2729 "usbdux devs connected to the usb bus.\n", dev->minor);
2730 up(&start_stop_sem);
2734 udev = &usbduxsub[index];
2736 /* pointer back to the corresponding comedi device */
2737 udev->comedidev = dev;
2739 /* trying to upload the firmware into the chip */
2740 if (comedi_aux_data(it->options, 0) &&
2741 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
2742 read_firmware(udev, comedi_aux_data(it->options, 0),
2743 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
2746 dev->board_name = BOARDNAME;
2748 /* set number of subdevices */
2749 if (udev->high_speed) {
2751 dev->n_subdevices = 5;
2754 dev->n_subdevices = 4;
2757 /* allocate space for the subdevices */
2758 ret = alloc_subdevices(dev, dev->n_subdevices);
2760 dev_err(&udev->interface->dev,
2761 "comedi%d: error alloc space for subdev\n", dev->minor);
2762 up(&start_stop_sem);
2766 dev_info(&udev->interface->dev,
2767 "comedi%d: usb-device %d is attached to comedi.\n",
2769 /* private structure is also simply the usb-structure */
2770 dev->private = udev;
2772 /* the first subdevice is the A/D converter */
2773 s = dev->subdevices + SUBDEV_AD;
2774 /* the URBs get the comedi subdevice */
2775 /* which is responsible for reading */
2776 /* this is the subdevice which reads data */
2777 dev->read_subdev = s;
2778 /* the subdevice receives as private structure the */
2782 s->type = COMEDI_SUBD_AI;
2783 /* readable and ref is to ground */
2784 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
2787 /* length of the channellist */
2788 s->len_chanlist = 8;
2789 /* callback functions */
2790 s->insn_read = usbdux_ai_insn_read;
2791 s->do_cmdtest = usbdux_ai_cmdtest;
2792 s->do_cmd = usbdux_ai_cmd;
2793 s->cancel = usbdux_ai_cancel;
2794 /* max value from the A/D converter (12bit) */
2796 /* range table to convert to physical units */
2797 s->range_table = (&range_usbdux_ai_range);
2800 s = dev->subdevices + SUBDEV_DA;
2802 s->type = COMEDI_SUBD_AO;
2803 /* backward pointer */
2804 dev->write_subdev = s;
2805 /* the subdevice receives as private structure the */
2809 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2812 /* length of the channellist */
2813 s->len_chanlist = 4;
2814 /* 12 bit resolution */
2815 s->maxdata = 0x0fff;
2817 s->range_table = (&range_usbdux_ao_range);
2819 s->do_cmdtest = usbdux_ao_cmdtest;
2820 s->do_cmd = usbdux_ao_cmd;
2821 s->cancel = usbdux_ao_cancel;
2822 s->insn_read = usbdux_ao_insn_read;
2823 s->insn_write = usbdux_ao_insn_write;
2826 s = dev->subdevices + SUBDEV_DIO;
2827 s->type = COMEDI_SUBD_DIO;
2828 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2831 s->range_table = (&range_digital);
2832 s->insn_bits = usbdux_dio_insn_bits;
2833 s->insn_config = usbdux_dio_insn_config;
2834 /* we don't use it */
2838 s = dev->subdevices + SUBDEV_COUNTER;
2839 s->type = COMEDI_SUBD_COUNTER;
2840 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2842 s->maxdata = 0xFFFF;
2843 s->insn_read = usbdux_counter_read;
2844 s->insn_write = usbdux_counter_write;
2845 s->insn_config = usbdux_counter_config;
2847 if (udev->high_speed) {
2849 s = dev->subdevices + SUBDEV_PWM;
2850 s->type = COMEDI_SUBD_PWM;
2851 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2853 /* this defines the max duty cycle resolution */
2854 s->maxdata = udev->sizePwmBuf;
2855 s->insn_write = usbdux_pwm_write;
2856 s->insn_read = usbdux_pwm_read;
2857 s->insn_config = usbdux_pwm_config;
2858 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2860 /* finally decide that it's attached */
2865 up(&start_stop_sem);
2867 dev_info(&udev->interface->dev, "comedi%d: attached to usbdux.\n",
2873 static int usbdux_detach(struct comedi_device *dev)
2875 struct usbduxsub *usbduxsub_tmp;
2879 "comedi?: usbdux: detach without dev variable...\n");
2883 usbduxsub_tmp = dev->private;
2884 if (!usbduxsub_tmp) {
2886 "comedi?: usbdux: detach without ptr to usbduxsub[]\n");
2890 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi%d: detach usb device\n",
2893 down(&usbduxsub_tmp->sem);
2894 /* Don't allow detach to free the private structure */
2895 /* It's one entry of of usbduxsub[] */
2896 dev->private = NULL;
2897 usbduxsub_tmp->attached = 0;
2898 usbduxsub_tmp->comedidev = NULL;
2899 dev_dbg(&usbduxsub_tmp->interface->dev,
2900 "comedi%d: detach: successfully removed\n", dev->minor);
2901 up(&usbduxsub_tmp->sem);
2905 /* main driver struct */
2906 static struct comedi_driver driver_usbdux = {
2907 .driver_name = "usbdux",
2908 .module = THIS_MODULE,
2909 .attach = usbdux_attach,
2910 .detach = usbdux_detach,
2913 /* Table with the USB-devices: just now only testing IDs */
2914 static struct usb_device_id usbduxsub_table[] = {
2915 {USB_DEVICE(0x13d8, 0x0001) },
2916 {USB_DEVICE(0x13d8, 0x0002) },
2917 {} /* Terminating entry */
2920 MODULE_DEVICE_TABLE(usb, usbduxsub_table);
2922 /* The usbduxsub-driver */
2923 static struct usb_driver usbduxsub_driver = {
2925 .probe = usbduxsub_probe,
2926 .disconnect = usbduxsub_disconnect,
2927 .id_table = usbduxsub_table,
2930 /* Can't use the nice macro as I have also to initialise the USB */
2932 /* registering the usb-system _and_ the comedi-driver */
2933 static int __init init_usbdux(void)
2935 printk(KERN_INFO KBUILD_MODNAME ": "
2936 DRIVER_VERSION ":" DRIVER_DESC "\n");
2937 usb_register(&usbduxsub_driver);
2938 comedi_driver_register(&driver_usbdux);
2942 /* deregistering the comedi driver and the usb-subsystem */
2943 static void __exit exit_usbdux(void)
2945 comedi_driver_unregister(&driver_usbdux);
2946 usb_deregister(&usbduxsub_driver);
2949 module_init(init_usbdux);
2950 module_exit(exit_usbdux);
2952 MODULE_AUTHOR(DRIVER_AUTHOR);
2953 MODULE_DESCRIPTION(DRIVER_DESC);
2954 MODULE_LICENSE("GPL");