2 comedi/drivers/usbdux.c
3 Copyright (C) 2011 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 (usbduxsigma.o)
24 Author: Bernd Porr <BerndPorr@f2s.com>
29 * I must give credit here to Chris Baugher who
30 * wrote the driver for AT-MIO-16d. I used some parts of this
31 * driver. I also must give credits to David Brownell
32 * who supported me with the USB development.
34 * Note: the raw data from the A/D converter is 24 bit big endian
35 * anything else is little endian to/from the dux board
39 * 0.1: initial version
40 * 0.2: all basic functions implemented, digital I/O only for one port
41 * 0.3: proper vendor ID and driver name
42 * 0.4: fixed D/A voltage range
43 * 0.5: various bug fixes, health check at startup
44 * 0.6: corrected wrong input range
47 /* generates loads of debug info */
48 /* #define NOISY_DUX_DEBUGBUG */
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/input.h>
55 #include <linux/usb.h>
56 #include <linux/fcntl.h>
57 #include <linux/compiler.h>
58 #include <linux/firmware.h>
59 #include "comedi_fc.h"
60 #include "../comedidev.h"
62 /* timeout for the USB-transfer in ms*/
63 #define BULK_TIMEOUT 1000
65 /* constants for "firmware" upload and download */
66 #define FIRMWARE "usbduxsigma_firmware.bin"
67 #define USBDUXSUB_FIRMWARE 0xA0
68 #define VENDOR_DIR_IN 0xC0
69 #define VENDOR_DIR_OUT 0x40
71 /* internal addresses of the 8051 processor */
72 #define USBDUXSUB_CPUCS 0xE600
75 * the minor device number, major is 180 only for debugging purposes and to
76 * upload special firmware (programming the eeprom etc) which is not
77 * compatible with the comedi framwork
79 #define USBDUXSUB_MINOR 32
81 /* max lenghth of the transfer-buffer for software upload */
84 /* Input endpoint number: ISO/IRQ */
87 /* Output endpoint number: ISO/IRQ */
90 /* This EP sends DUX commands to USBDUX */
91 #define COMMAND_OUT_EP 1
93 /* This EP receives the DUX commands from USBDUX */
94 #define COMMAND_IN_EP 8
96 /* Output endpoint for PWM */
99 /* 300Hz max frequ under PWM */
100 #define MIN_PWM_PERIOD ((long)(1E9/300))
102 /* Default PWM frequency */
103 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
105 /* Number of channels (16 AD and offset)*/
106 #define NUMCHANNELS 16
108 /* Size of one A/D value */
109 #define SIZEADIN ((sizeof(int32_t)))
112 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
115 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
118 #define SIZEINSNBUF 16
120 /* Number of DA channels */
121 #define NUMOUTCHANNELS 8
123 /* size of one value for the D/A converter: channel and value */
124 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(int16_t)))
127 * Size of the output-buffer in bytes
128 * Actually only the first 4 triplets are used but for the
129 * high speed mode we need to pad it to 8 (microframes).
131 #define SIZEOUTBUF ((8*SIZEDAOUT))
134 * Size of the buffer for the dux commands: just now max size is determined
135 * by the analogue out + command byte + panic bytes...
137 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
139 /* Number of in-URBs which receive the data: min=2 */
140 #define NUMOFINBUFFERSFULL 5
142 /* Number of out-URBs which send the data: min=2 */
143 #define NUMOFOUTBUFFERSFULL 5
145 /* Number of in-URBs which receive the data: min=5 */
146 /* must have more buffers due to buggy USB ctr */
147 #define NUMOFINBUFFERSHIGH 10
149 /* Number of out-URBs which send the data: min=5 */
150 /* must have more buffers due to buggy USB ctr */
151 #define NUMOFOUTBUFFERSHIGH 10
153 /* Total number of usbdux devices */
156 /* Analogue in subdevice */
159 /* Analogue out subdevice */
165 /* timer aka pwm output */
168 /* number of retries to get the right dux command */
171 /**************************************************/
172 /* comedi constants */
173 static const struct comedi_lrange range_usbdux_ai_range = { 1, {
180 * private structure of one subdevice
184 * This is the structure which holds all the data of
185 * this driver one sub device just now: A/D
190 /* is it associated with a subdevice? */
192 /* pointer to the usb-device */
193 struct usb_device *usbdev;
194 /* actual number of in-buffers */
196 /* actual number of out-buffers */
198 /* ISO-transfer handling: buffers */
201 /* pwm-transfer handling */
204 unsigned int pwmPeriod;
205 /* PWM internal delay for the GPIF in the FX2 */
207 /* size of the PWM buffer which holds the bit pattern */
209 /* input buffer for the ISO-transfer */
211 /* input buffer for single insn */
213 /* output buffer for single DA outputs */
215 /* interface number */
217 /* interface structure in 2.6 */
218 struct usb_interface *interface;
219 /* comedi device for the interrupt context */
220 struct comedi_device *comedidev;
221 /* is it USB_SPEED_HIGH or not? */
222 short int high_speed;
223 /* asynchronous command is running */
224 short int ai_cmd_running;
225 short int ao_cmd_running;
227 short int pwm_cmd_running;
228 /* continuous acquisition */
229 short int ai_continuous;
230 short int ao_continuous;
231 /* number of samples to acquire */
234 /* time between samples in units of the timer */
235 unsigned int ai_timer;
236 unsigned int ao_timer;
237 /* counter between acquisitions */
238 unsigned int ai_counter;
239 unsigned int ao_counter;
240 /* interval in frames/uframes */
241 unsigned int ai_interval;
243 uint8_t *dac_commands;
245 uint8_t *dux_commands;
246 struct semaphore sem;
250 * The pointer to the private usb-data of the driver is also the private data
251 * for the comedi-device. This has to be global as the usb subsystem needs
252 * global variables. The other reason is that this structure must be there
253 * _before_ any comedi command is issued. The usb subsystem must be initialised
254 * before comedi can access it.
256 static struct usbduxsub usbduxsub[NUMUSBDUX];
258 static DEFINE_SEMAPHORE(start_stop_sem);
261 * Stops the data acquision
262 * It should be safe to call this function from any context
264 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
269 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
270 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
271 if (usbduxsub_tmp->urbIn[i]) {
272 /* We wait here until all transfers have been
274 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
276 dev_dbg(&usbduxsub_tmp->interface->dev,
277 "comedi: usbdux: unlinked InURB %d, err=%d\n",
285 * This will stop a running acquisition operation
286 * Is called from within this driver from both the
287 * interrupt context and from comedi
289 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
293 if (!this_usbduxsub) {
294 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
297 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
301 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
304 this_usbduxsub->ai_cmd_running = 0;
310 * This will cancel a running acquisition operation.
311 * This is called by comedi but never from inside the driver.
313 static int usbdux_ai_cancel(struct comedi_device *dev,
314 struct comedi_subdevice *s)
316 struct usbduxsub *this_usbduxsub;
319 /* force unlink of all urbs */
320 this_usbduxsub = dev->private;
324 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
326 /* prevent other CPUs from submitting new commands just now */
327 down(&this_usbduxsub->sem);
328 if (!(this_usbduxsub->probed)) {
329 up(&this_usbduxsub->sem);
332 /* unlink only if the urb really has been submitted */
333 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
334 up(&this_usbduxsub->sem);
338 /* analogue IN - interrupt service routine */
339 static void usbduxsub_ai_IsocIrq(struct urb *urb)
342 struct usbduxsub *this_usbduxsub;
343 struct comedi_device *this_comedidev;
344 struct comedi_subdevice *s;
346 unsigned int dio_state;
348 /* the context variable points to the comedi device */
349 this_comedidev = urb->context;
350 /* the private structure of the subdevice is struct usbduxsub */
351 this_usbduxsub = this_comedidev->private;
352 /* subdevice which is the AD converter */
353 s = &this_comedidev->subdevices[SUBDEV_AD];
355 /* first we test if something unusual has just happened */
356 switch (urb->status) {
358 /* copy the result in the transfer buffer */
359 memcpy(this_usbduxsub->inBuffer,
360 urb->transfer_buffer, SIZEINBUF);
363 /* error in the ISOchronous data */
364 /* we don't copy the data into the transfer buffer */
365 /* and recycle the last data byte */
366 dev_dbg(&urb->dev->dev,
367 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
368 this_usbduxsub->comedidev->minor);
376 /* happens after an unlink command */
377 if (this_usbduxsub->ai_cmd_running) {
378 /* we are still running a command */
379 /* tell this comedi */
380 s->async->events |= COMEDI_CB_EOA;
381 s->async->events |= COMEDI_CB_ERROR;
382 comedi_event(this_usbduxsub->comedidev, s);
383 /* stop the transfer w/o unlink */
384 usbdux_ai_stop(this_usbduxsub, 0);
389 /* a real error on the bus */
390 /* pass error to comedi if we are really running a command */
391 if (this_usbduxsub->ai_cmd_running) {
392 dev_err(&urb->dev->dev,
393 "Non-zero urb status received in ai intr "
394 "context: %d\n", urb->status);
395 s->async->events |= COMEDI_CB_EOA;
396 s->async->events |= COMEDI_CB_ERROR;
397 comedi_event(this_usbduxsub->comedidev, s);
398 /* don't do an unlink here */
399 usbdux_ai_stop(this_usbduxsub, 0);
405 * at this point we are reasonably sure that nothing dodgy has happened
406 * are we running a command?
408 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
410 * not running a command, do not continue execution if no
411 * asynchronous command is running in particular not resubmit
416 urb->dev = this_usbduxsub->usbdev;
418 /* resubmit the urb */
419 err = usb_submit_urb(urb, GFP_ATOMIC);
420 if (unlikely(err < 0)) {
421 dev_err(&urb->dev->dev,
422 "comedi_: urb resubmit failed in int-context!"
425 if (err == -EL2NSYNC)
426 dev_err(&urb->dev->dev,
427 "buggy USB host controller or bug in IRQ "
429 s->async->events |= COMEDI_CB_EOA;
430 s->async->events |= COMEDI_CB_ERROR;
431 comedi_event(this_usbduxsub->comedidev, s);
432 /* don't do an unlink here */
433 usbdux_ai_stop(this_usbduxsub, 0);
437 /* get the state of the dio pins to allow external trigger */
438 dio_state = be32_to_cpu(this_usbduxsub->inBuffer[0]);
440 this_usbduxsub->ai_counter--;
441 if (likely(this_usbduxsub->ai_counter > 0))
444 /* timer zero, transfer measurements to comedi */
445 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
447 /* test, if we transmit only a fixed number of samples */
448 if (!(this_usbduxsub->ai_continuous)) {
449 /* not continuous, fixed number of samples */
450 this_usbduxsub->ai_sample_count--;
451 /* all samples received? */
452 if (this_usbduxsub->ai_sample_count < 0) {
453 /* prevent a resubmit next time */
454 usbdux_ai_stop(this_usbduxsub, 0);
455 /* say comedi that the acquistion is over */
456 s->async->events |= COMEDI_CB_EOA;
457 comedi_event(this_usbduxsub->comedidev, s);
461 /* get the data from the USB bus and hand it over to comedi */
462 n = s->async->cmd.chanlist_len;
463 for (i = 0; i < n; i++) {
464 /* transfer data, note first byte is the DIO state */
465 v = be32_to_cpu(this_usbduxsub->inBuffer[i+1]);
466 /* strip status byte */
468 /* convert to unsigned */
470 /* write the byte to the buffer */
471 err = cfc_write_array_to_buffer(s, &v, sizeof(uint32_t));
472 if (unlikely(err == 0)) {
473 /* buffer overflow */
474 usbdux_ai_stop(this_usbduxsub, 0);
478 /* tell comedi that data is there */
479 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
480 comedi_event(this_usbduxsub->comedidev, s);
483 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
488 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
489 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
490 if (usbduxsub_tmp->urbOut[i])
491 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
493 dev_dbg(&usbduxsub_tmp->interface->dev,
494 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
501 /* This will cancel a running acquisition operation
504 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
510 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
513 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
515 this_usbduxsub->ao_cmd_running = 0;
520 /* force unlink, is called by comedi */
521 static int usbdux_ao_cancel(struct comedi_device *dev,
522 struct comedi_subdevice *s)
524 struct usbduxsub *this_usbduxsub = dev->private;
530 /* prevent other CPUs from submitting a command just now */
531 down(&this_usbduxsub->sem);
532 if (!(this_usbduxsub->probed)) {
533 up(&this_usbduxsub->sem);
536 /* unlink only if it is really running */
537 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
538 up(&this_usbduxsub->sem);
542 static void usbduxsub_ao_IsocIrq(struct urb *urb)
546 struct usbduxsub *this_usbduxsub;
547 struct comedi_device *this_comedidev;
548 struct comedi_subdevice *s;
550 /* the context variable points to the subdevice */
551 this_comedidev = urb->context;
552 /* the private structure of the subdevice is struct usbduxsub */
553 this_usbduxsub = this_comedidev->private;
555 s = &this_comedidev->subdevices[SUBDEV_DA];
557 switch (urb->status) {
566 /* after an unlink command, unplug, ... etc */
567 /* no unlink needed here. Already shutting down. */
568 if (this_usbduxsub->ao_cmd_running) {
569 s->async->events |= COMEDI_CB_EOA;
570 comedi_event(this_usbduxsub->comedidev, s);
571 usbdux_ao_stop(this_usbduxsub, 0);
577 if (this_usbduxsub->ao_cmd_running) {
578 dev_err(&urb->dev->dev,
579 "comedi_: Non-zero urb status received in ao "
580 "intr context: %d\n", urb->status);
581 s->async->events |= COMEDI_CB_ERROR;
582 s->async->events |= COMEDI_CB_EOA;
583 comedi_event(this_usbduxsub->comedidev, s);
584 /* we do an unlink if we are in the high speed mode */
585 usbdux_ao_stop(this_usbduxsub, 0);
590 /* are we actually running? */
591 if (!(this_usbduxsub->ao_cmd_running))
594 /* normal operation: executing a command in this subdevice */
595 this_usbduxsub->ao_counter--;
596 if ((int)this_usbduxsub->ao_counter <= 0) {
598 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
600 /* handle non continuous acquisition */
601 if (!(this_usbduxsub->ao_continuous)) {
602 /* fixed number of samples */
603 this_usbduxsub->ao_sample_count--;
604 if (this_usbduxsub->ao_sample_count < 0) {
605 /* all samples transmitted */
606 usbdux_ao_stop(this_usbduxsub, 0);
607 s->async->events |= COMEDI_CB_EOA;
608 comedi_event(this_usbduxsub->comedidev, s);
609 /* no resubmit of the urb */
613 /* transmit data to the USB bus */
614 ((uint8_t *) (urb->transfer_buffer))[0] =
615 s->async->cmd.chanlist_len;
616 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
618 if (i >= NUMOUTCHANNELS)
621 /* pointer to the DA */
623 (&(((uint8_t *) urb->transfer_buffer)[i * 2 + 1]));
624 /* get the data from comedi */
625 ret = comedi_buf_get(s->async, &temp);
627 datap[1] = this_usbduxsub->dac_commands[i];
628 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
629 /* datap[0],datap[1],datap[2]); */
631 dev_err(&urb->dev->dev,
632 "comedi: buffer underflow\n");
633 s->async->events |= COMEDI_CB_EOA;
634 s->async->events |= COMEDI_CB_OVERFLOW;
636 /* transmit data to comedi */
637 s->async->events |= COMEDI_CB_BLOCK;
638 comedi_event(this_usbduxsub->comedidev, s);
641 urb->transfer_buffer_length = SIZEOUTBUF;
642 urb->dev = this_usbduxsub->usbdev;
644 if (this_usbduxsub->ao_cmd_running) {
645 if (this_usbduxsub->high_speed) {
652 urb->number_of_packets = 1;
653 urb->iso_frame_desc[0].offset = 0;
654 urb->iso_frame_desc[0].length = SIZEOUTBUF;
655 urb->iso_frame_desc[0].status = 0;
656 ret = usb_submit_urb(urb, GFP_ATOMIC);
658 dev_err(&urb->dev->dev,
659 "comedi_: ao urb resubm failed in int-cont. "
662 dev_err(&urb->dev->dev,
663 "buggy USB host controller or bug in "
666 s->async->events |= COMEDI_CB_EOA;
667 s->async->events |= COMEDI_CB_ERROR;
668 comedi_event(this_usbduxsub->comedidev, s);
669 /* don't do an unlink here */
670 usbdux_ao_stop(this_usbduxsub, 0);
675 static int usbduxsub_start(struct usbduxsub *usbduxsub)
678 uint8_t *local_transfer_buffer;
680 local_transfer_buffer = kmalloc(16, GFP_KERNEL);
681 if (!local_transfer_buffer)
685 local_transfer_buffer[0] = 0;
686 errcode = usb_control_msg(usbduxsub->usbdev,
687 /* create a pipe for a control transfer */
688 usb_sndctrlpipe(usbduxsub->usbdev, 0),
689 /* bRequest, "Firmware" */
697 /* address of the transfer buffer */
698 local_transfer_buffer,
704 dev_err(&usbduxsub->interface->dev,
705 "comedi_: control msg failed (start)\n");
707 kfree(local_transfer_buffer);
711 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
714 uint8_t *local_transfer_buffer;
716 local_transfer_buffer = kmalloc(16, GFP_KERNEL);
717 if (!local_transfer_buffer)
721 local_transfer_buffer[0] = 1;
722 errcode = usb_control_msg(usbduxsub->usbdev,
723 usb_sndctrlpipe(usbduxsub->usbdev, 0),
724 /* bRequest, "Firmware" */
731 0x0000, local_transfer_buffer,
737 dev_err(&usbduxsub->interface->dev,
738 "comedi_: control msg failed (stop)\n");
740 kfree(local_transfer_buffer);
744 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
745 uint8_t *local_transfer_buffer,
746 unsigned int startAddr, unsigned int len)
750 errcode = usb_control_msg(usbduxsub->usbdev,
751 usb_sndctrlpipe(usbduxsub->usbdev, 0),
752 /* brequest, firmware */
760 /* our local safe buffer */
761 local_transfer_buffer,
766 dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
768 dev_err(&usbduxsub->interface->dev,
769 "comedi_: upload failed\n");
775 /* the FX2LP has twice as much as the standard FX2 */
776 #define FIRMWARE_MAX_LEN 0x4000
778 static int firmwareUpload(struct usbduxsub *usbduxsub,
779 const u8 *firmwareBinary, int sizeFirmware)
787 if (sizeFirmware > FIRMWARE_MAX_LEN) {
788 dev_err(&usbduxsub->interface->dev,
789 "usbduxsigma firmware binary it too large for FX2.\n");
793 /* we generate a local buffer for the firmware */
794 fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
796 dev_err(&usbduxsub->interface->dev,
797 "comedi_: mem alloc for firmware failed\n");
801 ret = usbduxsub_stop(usbduxsub);
803 dev_err(&usbduxsub->interface->dev,
804 "comedi_: can not stop firmware\n");
809 ret = usbduxsub_upload(usbduxsub, fwBuf, 0, sizeFirmware);
811 dev_err(&usbduxsub->interface->dev,
812 "comedi_: firmware upload failed\n");
816 ret = usbduxsub_start(usbduxsub);
818 dev_err(&usbduxsub->interface->dev,
819 "comedi_: can not start firmware\n");
827 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
834 /* Submit all URBs and start the transfer on the bus */
835 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
836 /* in case of a resubmission after an unlink... */
837 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
838 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
839 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
840 usbduxsub->urbIn[i]->status = 0;
841 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
842 dev_dbg(&usbduxsub->interface->dev,
843 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
844 usbduxsub->comedidev->minor, i,
845 (usbduxsub->urbIn[i]->context),
846 (usbduxsub->urbIn[i]->dev),
847 (usbduxsub->urbIn[i]->interval));
848 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
850 dev_err(&usbduxsub->interface->dev,
851 "comedi_: ai: usb_submit_urb(%d) error %d\n",
859 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
866 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
867 dev_dbg(&usbduxsub->interface->dev,
868 "comedi_: submitting out-urb[%d]\n", i);
869 /* in case of a resubmission after an unlink... */
870 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
871 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
872 usbduxsub->urbOut[i]->status = 0;
873 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
874 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
876 dev_err(&usbduxsub->interface->dev,
877 "comedi_: ao: usb_submit_urb(%d) error %d\n",
885 static int chanToInterval(int nChannels)
897 static int usbdux_ai_cmdtest(struct comedi_device *dev,
898 struct comedi_subdevice *s,
899 struct comedi_cmd *cmd)
901 struct usbduxsub *this_usbduxsub = dev->private;
903 unsigned int tmpTimer;
905 if (!(this_usbduxsub->probed))
908 /* Step 1 : check if triggers are trivially valid */
910 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
911 err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
912 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
913 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
914 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
919 /* Step 2a : make sure trigger sources are unique */
921 err |= cfc_check_trigger_is_unique(cmd->start_src);
922 err |= cfc_check_trigger_is_unique(cmd->stop_src);
924 /* Step 2b : and mutually compatible */
929 /* Step 3: check if arguments are trivially valid */
931 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
933 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
934 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
936 if (cmd->scan_begin_src == TRIG_TIMER) {
937 if (this_usbduxsub->high_speed) {
939 * In high speed mode microframes are possible.
940 * However, during one microframe we can roughly
941 * sample two channels. Thus, the more channels
942 * are in the channel list the more time we need.
944 i = chanToInterval(cmd->chanlist_len);
945 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
947 /* now calc the real sampling rate with all the
950 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
954 /* 1kHz scans every USB frame */
955 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
958 * calc the real sampling rate with the rounding errors
960 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
963 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg,
967 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
969 if (cmd->stop_src == TRIG_COUNT) {
970 /* any count is allowed */
973 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
983 * creates the ADC command for the MAX1271
984 * range is the range value from comedi
986 static void create_adc_command(unsigned int chan,
991 (*muxsg0) = (*muxsg0) | (1 << chan);
993 (*muxsg1) = (*muxsg1) | (1 << (chan-8));
997 /* bulk transfers to usbdux */
999 #define SENDADCOMMANDS 0
1000 #define SENDDACOMMANDS 1
1001 #define SENDDIOCONFIGCOMMAND 2
1002 #define SENDDIOBITSCOMMAND 3
1003 #define SENDSINGLEAD 4
1005 #define SENDPWMOFF 8
1007 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1011 this_usbduxsub->dux_commands[0] = cmd_type;
1012 #ifdef NOISY_DUX_DEBUGBUG
1013 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1014 this_usbduxsub->comedidev->minor);
1015 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1016 printk(" %02x", this_usbduxsub->dux_commands[result]);
1019 result = usb_bulk_msg(this_usbduxsub->usbdev,
1020 usb_sndbulkpipe(this_usbduxsub->usbdev,
1022 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1023 &nsent, BULK_TIMEOUT);
1025 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1026 "could not transmit dux_command to the usb-device, "
1027 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1032 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1034 int result = (-EFAULT);
1038 for (i = 0; i < RETRIES; i++) {
1039 result = usb_bulk_msg(this_usbduxsub->usbdev,
1040 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1042 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1043 &nrec, BULK_TIMEOUT);
1045 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1046 "insn: USB error %d "
1047 "while receiving DUX command"
1048 "\n", this_usbduxsub->comedidev->minor,
1052 if (this_usbduxsub->insnBuffer[0] == command)
1055 /* this is only reached if the data has been requested a couple of
1057 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1058 "wrong data returned from firmware: want %d, got %d.\n",
1059 this_usbduxsub->comedidev->minor, command,
1060 this_usbduxsub->insnBuffer[0]);
1064 static int usbdux_ai_inttrig(struct comedi_device *dev,
1065 struct comedi_subdevice *s, unsigned int trignum)
1068 struct usbduxsub *this_usbduxsub = dev->private;
1069 if (!this_usbduxsub)
1072 down(&this_usbduxsub->sem);
1073 if (!(this_usbduxsub->probed)) {
1074 up(&this_usbduxsub->sem);
1077 dev_dbg(&this_usbduxsub->interface->dev,
1078 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1081 dev_err(&this_usbduxsub->interface->dev,
1082 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1084 up(&this_usbduxsub->sem);
1087 if (!(this_usbduxsub->ai_cmd_running)) {
1088 this_usbduxsub->ai_cmd_running = 1;
1089 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1091 dev_err(&this_usbduxsub->interface->dev,
1092 "comedi%d: usbdux_ai_inttrig: "
1093 "urbSubmit: err=%d\n", dev->minor, ret);
1094 this_usbduxsub->ai_cmd_running = 0;
1095 up(&this_usbduxsub->sem);
1098 s->async->inttrig = NULL;
1100 dev_err(&this_usbduxsub->interface->dev,
1101 "comedi%d: ai_inttrig but acqu is already running\n",
1104 up(&this_usbduxsub->sem);
1108 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1110 struct comedi_cmd *cmd = &s->async->cmd;
1113 struct usbduxsub *this_usbduxsub = dev->private;
1119 if (!this_usbduxsub)
1122 dev_dbg(&this_usbduxsub->interface->dev,
1123 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1125 /* block other CPUs from starting an ai_cmd */
1126 down(&this_usbduxsub->sem);
1128 if (!(this_usbduxsub->probed)) {
1129 up(&this_usbduxsub->sem);
1132 if (this_usbduxsub->ai_cmd_running) {
1133 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1134 "ai_cmd not possible. Another ai_cmd is running.\n",
1136 up(&this_usbduxsub->sem);
1139 /* set current channel of the running acquisition to zero */
1140 s->async->cur_chan = 0;
1142 /* first the number of channels per time step */
1143 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1146 this_usbduxsub->dux_commands[2] = 0x12;
1148 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
1149 this_usbduxsub->dux_commands[3] = 0x03;
1151 /* CONFIG3: differential channels off */
1152 this_usbduxsub->dux_commands[4] = 0x00;
1154 for (i = 0; i < cmd->chanlist_len; i++) {
1155 chan = CR_CHAN(cmd->chanlist[i]);
1156 create_adc_command(chan, &muxsg0, &muxsg1);
1157 if (i >= NUMCHANNELS) {
1158 dev_err(&this_usbduxsub->interface->dev,
1159 "comedi%d: channel list too long\n",
1164 this_usbduxsub->dux_commands[5] = muxsg0;
1165 this_usbduxsub->dux_commands[6] = muxsg1;
1166 this_usbduxsub->dux_commands[7] = sysred;
1168 dev_dbg(&this_usbduxsub->interface->dev,
1169 "comedi %d: sending commands to the usb device: size=%u\n",
1170 dev->minor, NUMCHANNELS);
1172 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1174 up(&this_usbduxsub->sem);
1178 if (this_usbduxsub->high_speed) {
1180 * every 2 channels get a time window of 125us. Thus, if we
1181 * sample all 16 channels we need 1ms. If we sample only one
1182 * channel we need only 125us
1184 this_usbduxsub->ai_interval =
1185 chanToInterval(cmd->chanlist_len);
1186 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1190 /* interval always 1ms */
1191 this_usbduxsub->ai_interval = 1;
1192 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1194 if (this_usbduxsub->ai_timer < 1) {
1195 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1196 "timer=%d, scan_begin_arg=%d. "
1197 "Not properly tested by cmdtest?\n", dev->minor,
1198 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1199 up(&this_usbduxsub->sem);
1202 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1204 if (cmd->stop_src == TRIG_COUNT) {
1205 /* data arrives as one packet */
1206 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1207 this_usbduxsub->ai_continuous = 0;
1209 /* continuous acquisition */
1210 this_usbduxsub->ai_continuous = 1;
1211 this_usbduxsub->ai_sample_count = 0;
1214 if (cmd->start_src == TRIG_NOW) {
1215 /* enable this acquisition operation */
1216 this_usbduxsub->ai_cmd_running = 1;
1217 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1219 this_usbduxsub->ai_cmd_running = 0;
1220 /* fixme: unlink here?? */
1221 up(&this_usbduxsub->sem);
1224 s->async->inttrig = NULL;
1227 /* don't enable the acquision operation */
1228 /* wait for an internal signal */
1229 s->async->inttrig = usbdux_ai_inttrig;
1231 up(&this_usbduxsub->sem);
1235 /* Mode 0 is used to get a single conversion on demand */
1236 static int usbdux_ai_insn_read(struct comedi_device *dev,
1237 struct comedi_subdevice *s,
1238 struct comedi_insn *insn, unsigned int *data)
1244 struct usbduxsub *this_usbduxsub = dev->private;
1249 if (!this_usbduxsub)
1252 dev_dbg(&this_usbduxsub->interface->dev,
1253 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1254 dev->minor, insn->n, insn->subdev);
1256 down(&this_usbduxsub->sem);
1257 if (!(this_usbduxsub->probed)) {
1258 up(&this_usbduxsub->sem);
1261 if (this_usbduxsub->ai_cmd_running) {
1262 dev_err(&this_usbduxsub->interface->dev,
1263 "comedi%d: ai_insn_read not possible. "
1264 "Async Command is running.\n", dev->minor);
1265 up(&this_usbduxsub->sem);
1269 /* sample one channel */
1270 /* CONFIG0: chopper on */
1271 this_usbduxsub->dux_commands[1] = 0x16;
1273 /* CONFIG1: 2kHz sampling rate */
1274 this_usbduxsub->dux_commands[2] = 0x80;
1276 /* CONFIG3: differential channels off */
1277 this_usbduxsub->dux_commands[3] = 0x00;
1279 chan = CR_CHAN(insn->chanspec);
1280 create_adc_command(chan, &muxsg0, &muxsg1);
1282 this_usbduxsub->dux_commands[4] = muxsg0;
1283 this_usbduxsub->dux_commands[5] = muxsg1;
1284 this_usbduxsub->dux_commands[6] = sysred;
1287 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1289 up(&this_usbduxsub->sem);
1293 for (i = 0; i < insn->n; i++) {
1294 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1296 up(&this_usbduxsub->sem);
1299 /* 32 bits big endian from the A/D converter */
1300 one = be32_to_cpu(*((int32_t *)
1301 ((this_usbduxsub->insnBuffer)+1)));
1302 /* mask out the status byte */
1303 one = one & 0x00ffffff;
1304 /* turn it into an unsigned integer */
1305 one = one ^ 0x00800000;
1308 up(&this_usbduxsub->sem);
1315 static int usbdux_getstatusinfo(struct comedi_device *dev, int chan)
1317 struct usbduxsub *this_usbduxsub = dev->private;
1322 if (!this_usbduxsub)
1325 if (this_usbduxsub->ai_cmd_running) {
1326 dev_err(&this_usbduxsub->interface->dev,
1327 "comedi%d: status read not possible. "
1328 "Async Command is running.\n", dev->minor);
1333 this_usbduxsub->dux_commands[1] = 0x12;
1335 /* CONFIG1: 2kHz sampling rate */
1336 this_usbduxsub->dux_commands[2] = 0x80;
1338 /* CONFIG3: differential channels off */
1339 this_usbduxsub->dux_commands[3] = 0x00;
1343 sysred = sysred | 1;
1344 } else if (chan == 2) {
1346 sysred = sysred | 4;
1347 } else if (chan == 3) {
1349 sysred = sysred | 8;
1350 } else if (chan == 4) {
1352 sysred = sysred | 16;
1353 } else if (chan == 5) {
1355 sysred = sysred | 32;
1358 this_usbduxsub->dux_commands[4] = 0;
1359 this_usbduxsub->dux_commands[5] = 0;
1360 this_usbduxsub->dux_commands[6] = sysred;
1363 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1367 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1371 /* 32 bits big endian from the A/D converter */
1372 one = be32_to_cpu(*((int32_t *)((this_usbduxsub->insnBuffer)+1)));
1373 /* mask out the status byte */
1374 one = one & 0x00ffffff;
1375 one = one ^ 0x00800000;
1385 /************************************/
1388 static int usbdux_ao_insn_read(struct comedi_device *dev,
1389 struct comedi_subdevice *s,
1390 struct comedi_insn *insn, unsigned int *data)
1393 int chan = CR_CHAN(insn->chanspec);
1394 struct usbduxsub *this_usbduxsub = dev->private;
1396 if (!this_usbduxsub)
1399 down(&this_usbduxsub->sem);
1400 if (!(this_usbduxsub->probed)) {
1401 up(&this_usbduxsub->sem);
1404 for (i = 0; i < insn->n; i++)
1405 data[i] = this_usbduxsub->outBuffer[chan];
1407 up(&this_usbduxsub->sem);
1411 static int usbdux_ao_insn_write(struct comedi_device *dev,
1412 struct comedi_subdevice *s,
1413 struct comedi_insn *insn, unsigned int *data)
1416 int chan = CR_CHAN(insn->chanspec);
1417 struct usbduxsub *this_usbduxsub = dev->private;
1419 if (!this_usbduxsub)
1422 dev_dbg(&this_usbduxsub->interface->dev,
1423 "comedi%d: ao_insn_write\n", dev->minor);
1425 down(&this_usbduxsub->sem);
1426 if (!(this_usbduxsub->probed)) {
1427 up(&this_usbduxsub->sem);
1430 if (this_usbduxsub->ao_cmd_running) {
1431 dev_err(&this_usbduxsub->interface->dev,
1432 "comedi%d: ao_insn_write: "
1433 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1434 up(&this_usbduxsub->sem);
1438 for (i = 0; i < insn->n; i++) {
1439 dev_dbg(&this_usbduxsub->interface->dev,
1440 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1441 dev->minor, chan, i, data[i]);
1443 /* number of channels: 1 */
1444 this_usbduxsub->dux_commands[1] = 1;
1445 /* channel number */
1446 this_usbduxsub->dux_commands[2] = data[i];
1447 this_usbduxsub->outBuffer[chan] = data[i];
1448 this_usbduxsub->dux_commands[3] = chan;
1449 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1451 up(&this_usbduxsub->sem);
1455 up(&this_usbduxsub->sem);
1460 static int usbdux_ao_inttrig(struct comedi_device *dev,
1461 struct comedi_subdevice *s, unsigned int trignum)
1464 struct usbduxsub *this_usbduxsub = dev->private;
1466 if (!this_usbduxsub)
1469 down(&this_usbduxsub->sem);
1471 if (!(this_usbduxsub->probed)) {
1476 dev_err(&this_usbduxsub->interface->dev,
1477 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1482 if (!(this_usbduxsub->ao_cmd_running)) {
1483 this_usbduxsub->ao_cmd_running = 1;
1484 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1486 dev_err(&this_usbduxsub->interface->dev,
1487 "comedi%d: usbdux_ao_inttrig: submitURB: "
1488 "err=%d\n", dev->minor, ret);
1489 this_usbduxsub->ao_cmd_running = 0;
1492 s->async->inttrig = NULL;
1494 dev_err(&this_usbduxsub->interface->dev,
1495 "comedi%d: ao_inttrig but acqu is already running.\n",
1500 up(&this_usbduxsub->sem);
1504 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1505 struct comedi_subdevice *s,
1506 struct comedi_cmd *cmd)
1508 struct usbduxsub *this_usbduxsub = dev->private;
1512 if (!this_usbduxsub)
1515 if (!(this_usbduxsub->probed))
1518 dev_dbg(&this_usbduxsub->interface->dev,
1519 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1521 /* Step 1 : check if triggers are trivially valid */
1523 err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
1525 if (0) { /* (this_usbduxsub->high_speed) */
1527 * start immediately a new scan
1528 * the sampling rate is set by the coversion rate
1530 flags = TRIG_FOLLOW;
1532 /* start a new scan (output at once) with a timer */
1535 err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
1537 err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
1538 err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
1539 err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
1544 /* Step 2a : make sure trigger sources are unique */
1546 err |= cfc_check_trigger_is_unique(cmd->start_src);
1547 err |= cfc_check_trigger_is_unique(cmd->stop_src);
1549 /* Step 2b : and mutually compatible */
1554 /* Step 3: check if arguments are trivially valid */
1556 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
1558 if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
1559 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
1561 if (cmd->scan_begin_src == TRIG_TIMER)
1562 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
1565 /* not used now, is for later use */
1566 if (cmd->convert_src == TRIG_TIMER)
1567 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
1569 err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
1571 if (cmd->stop_src == TRIG_COUNT) {
1572 /* any count is allowed */
1575 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1584 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1586 struct comedi_cmd *cmd = &s->async->cmd;
1587 unsigned int chan, gain;
1589 struct usbduxsub *this_usbduxsub = dev->private;
1591 if (!this_usbduxsub)
1594 down(&this_usbduxsub->sem);
1595 if (!(this_usbduxsub->probed)) {
1596 up(&this_usbduxsub->sem);
1599 dev_dbg(&this_usbduxsub->interface->dev,
1600 "comedi%d: %s\n", dev->minor, __func__);
1602 /* set current channel of the running acquisition to zero */
1603 s->async->cur_chan = 0;
1604 for (i = 0; i < cmd->chanlist_len; ++i) {
1605 chan = CR_CHAN(cmd->chanlist[i]);
1606 gain = CR_RANGE(cmd->chanlist[i]);
1607 if (i >= NUMOUTCHANNELS) {
1608 dev_err(&this_usbduxsub->interface->dev,
1609 "comedi%d: %s: channel list too long\n",
1610 dev->minor, __func__);
1613 this_usbduxsub->dac_commands[i] = chan;
1614 dev_dbg(&this_usbduxsub->interface->dev,
1615 "comedi%d: dac command for ch %d is %x\n",
1616 dev->minor, i, this_usbduxsub->dac_commands[i]);
1619 /* we count in steps of 1ms (125us) */
1620 /* 125us mode not used yet */
1621 if (0) { /* (this_usbduxsub->high_speed) */
1623 /* timing of the conversion itself: every 125 us */
1624 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1627 /* timing of the scan: we get all channels at once */
1628 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1629 dev_dbg(&this_usbduxsub->interface->dev,
1630 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1631 "convert_src=%d, convert_arg=%d\n", dev->minor,
1632 cmd->scan_begin_src, cmd->scan_begin_arg,
1633 cmd->convert_src, cmd->convert_arg);
1634 dev_dbg(&this_usbduxsub->interface->dev,
1635 "comedi%d: ao_timer=%d (ms)\n",
1636 dev->minor, this_usbduxsub->ao_timer);
1637 if (this_usbduxsub->ao_timer < 1) {
1638 dev_err(&this_usbduxsub->interface->dev,
1639 "comedi%d: usbdux: ao_timer=%d, "
1640 "scan_begin_arg=%d. "
1641 "Not properly tested by cmdtest?\n",
1642 dev->minor, this_usbduxsub->ao_timer,
1643 cmd->scan_begin_arg);
1644 up(&this_usbduxsub->sem);
1648 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1650 if (cmd->stop_src == TRIG_COUNT) {
1651 /* not continuous */
1653 /* high speed also scans everything at once */
1654 if (0) { /* (this_usbduxsub->high_speed) */
1655 this_usbduxsub->ao_sample_count =
1656 (cmd->stop_arg) * (cmd->scan_end_arg);
1658 /* there's no scan as the scan has been */
1659 /* perf inside the FX2 */
1660 /* data arrives as one packet */
1661 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1663 this_usbduxsub->ao_continuous = 0;
1665 /* continuous acquisition */
1666 this_usbduxsub->ao_continuous = 1;
1667 this_usbduxsub->ao_sample_count = 0;
1670 if (cmd->start_src == TRIG_NOW) {
1671 /* enable this acquisition operation */
1672 this_usbduxsub->ao_cmd_running = 1;
1673 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1675 this_usbduxsub->ao_cmd_running = 0;
1676 /* fixme: unlink here?? */
1677 up(&this_usbduxsub->sem);
1680 s->async->inttrig = NULL;
1683 /* submit the urbs later */
1684 /* wait for an internal signal */
1685 s->async->inttrig = usbdux_ao_inttrig;
1688 up(&this_usbduxsub->sem);
1692 static int usbdux_dio_insn_config(struct comedi_device *dev,
1693 struct comedi_subdevice *s,
1694 struct comedi_insn *insn, unsigned int *data)
1696 int chan = CR_CHAN(insn->chanspec);
1698 /* The input or output configuration of each digital line is
1699 * configured by a special insn_config instruction. chanspec
1700 * contains the channel to be changed, and data[0] contains the
1701 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1704 case INSN_CONFIG_DIO_OUTPUT:
1705 s->io_bits |= 1 << chan; /* 1 means Out */
1707 case INSN_CONFIG_DIO_INPUT:
1708 s->io_bits &= ~(1 << chan);
1710 case INSN_CONFIG_DIO_QUERY:
1712 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1718 /* we don't tell the firmware here as it would take 8 frames */
1719 /* to submit the information. We do it in the insn_bits. */
1723 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1724 struct comedi_subdevice *s,
1725 struct comedi_insn *insn,
1729 struct usbduxsub *this_usbduxsub = dev->private;
1732 if (!this_usbduxsub)
1735 down(&this_usbduxsub->sem);
1737 if (!(this_usbduxsub->probed)) {
1738 up(&this_usbduxsub->sem);
1742 /* The insn data is a mask in data[0] and the new data
1743 * in data[1], each channel cooresponding to a bit. */
1744 s->state &= ~data[0];
1745 s->state |= data[0] & data[1];
1746 /* The commands are 8 bits wide */
1747 this_usbduxsub->dux_commands[1] = (s->io_bits) & 0x000000FF;
1748 this_usbduxsub->dux_commands[4] = (s->state) & 0x000000FF;
1749 this_usbduxsub->dux_commands[2] = ((s->io_bits) & 0x0000FF00) >> 8;
1750 this_usbduxsub->dux_commands[5] = ((s->state) & 0x0000FF00) >> 8;
1751 this_usbduxsub->dux_commands[3] = ((s->io_bits) & 0x00FF0000) >> 16;
1752 this_usbduxsub->dux_commands[6] = ((s->state) & 0x00FF0000) >> 16;
1754 /* This command also tells the firmware to return */
1755 /* the digital input lines */
1756 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1758 up(&this_usbduxsub->sem);
1761 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1763 up(&this_usbduxsub->sem);
1767 data[1] = (((unsigned int)(this_usbduxsub->insnBuffer[1]))&0xff) |
1768 ((((unsigned int)(this_usbduxsub->insnBuffer[2]))&0xff) << 8) |
1769 ((((unsigned int)(this_usbduxsub->insnBuffer[3]))&0xff) << 16);
1773 up(&this_usbduxsub->sem);
1777 /***********************************/
1780 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1784 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1785 if (usbduxsub_tmp->urbPwm)
1786 usb_kill_urb(usbduxsub_tmp->urbPwm);
1787 dev_dbg(&usbduxsub_tmp->interface->dev,
1788 "comedi: unlinked PwmURB: res=%d\n", err);
1793 /* This cancels a running acquisition operation
1796 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1800 if (!this_usbduxsub)
1803 dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1805 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1807 this_usbduxsub->pwm_cmd_running = 0;
1812 /* force unlink - is called by comedi */
1813 static int usbdux_pwm_cancel(struct comedi_device *dev,
1814 struct comedi_subdevice *s)
1816 struct usbduxsub *this_usbduxsub = dev->private;
1819 /* unlink only if it is really running */
1820 res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1822 dev_dbg(&this_usbduxsub->interface->dev,
1823 "comedi %d: sending pwm off command to the usb device.\n",
1825 res = send_dux_commands(this_usbduxsub, SENDPWMOFF);
1832 static void usbduxsub_pwm_irq(struct urb *urb)
1835 struct usbduxsub *this_usbduxsub;
1836 struct comedi_device *this_comedidev;
1837 struct comedi_subdevice *s;
1839 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1841 /* the context variable points to the subdevice */
1842 this_comedidev = urb->context;
1843 /* the private structure of the subdevice is struct usbduxsub */
1844 this_usbduxsub = this_comedidev->private;
1846 s = &this_comedidev->subdevices[SUBDEV_DA];
1848 switch (urb->status) {
1858 * after an unlink command, unplug, ... etc
1859 * no unlink needed here. Already shutting down.
1861 if (this_usbduxsub->pwm_cmd_running)
1862 usbdux_pwm_stop(this_usbduxsub, 0);
1868 if (this_usbduxsub->pwm_cmd_running) {
1869 dev_err(&this_usbduxsub->interface->dev,
1870 "comedi_: Non-zero urb status received in "
1871 "pwm intr context: %d\n", urb->status);
1872 usbdux_pwm_stop(this_usbduxsub, 0);
1877 /* are we actually running? */
1878 if (!(this_usbduxsub->pwm_cmd_running))
1881 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1882 urb->dev = this_usbduxsub->usbdev;
1884 if (this_usbduxsub->pwm_cmd_running) {
1885 ret = usb_submit_urb(urb, GFP_ATOMIC);
1887 dev_err(&this_usbduxsub->interface->dev,
1888 "comedi_: pwm urb resubm failed in int-cont. "
1890 if (ret == EL2NSYNC)
1891 dev_err(&this_usbduxsub->interface->dev,
1892 "buggy USB host controller or bug in "
1895 /* don't do an unlink here */
1896 usbdux_pwm_stop(this_usbduxsub, 0);
1901 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
1908 dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
1910 /* in case of a resubmission after an unlink... */
1911 usb_fill_bulk_urb(usbduxsub->urbPwm,
1913 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
1914 usbduxsub->urbPwm->transfer_buffer,
1915 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq,
1916 usbduxsub->comedidev);
1918 errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
1920 dev_err(&usbduxsub->interface->dev,
1921 "comedi_: usbduxsigma: pwm: usb_submit_urb error %d\n",
1928 static int usbdux_pwm_period(struct comedi_device *dev,
1929 struct comedi_subdevice *s, unsigned int period)
1931 struct usbduxsub *this_usbduxsub = dev->private;
1934 if (period < MIN_PWM_PERIOD) {
1935 dev_err(&this_usbduxsub->interface->dev,
1936 "comedi%d: illegal period setting for pwm.\n",
1940 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1941 if (fx2delay > 255) {
1942 dev_err(&this_usbduxsub->interface->dev,
1943 "comedi%d: period %d for pwm is too low.\n",
1944 dev->minor, period);
1948 this_usbduxsub->pwmDelay = fx2delay;
1949 this_usbduxsub->pwmPeriod = period;
1950 dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
1951 __func__, period, fx2delay);
1955 /* is called from insn so there's no need to do all the sanity checks */
1956 static int usbdux_pwm_start(struct comedi_device *dev,
1957 struct comedi_subdevice *s)
1960 struct usbduxsub *this_usbduxsub = dev->private;
1962 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
1963 dev->minor, __func__);
1965 if (this_usbduxsub->pwm_cmd_running) {
1966 /* already running */
1970 this_usbduxsub->dux_commands[1] = ((uint8_t) this_usbduxsub->pwmDelay);
1971 ret = send_dux_commands(this_usbduxsub, SENDPWMON);
1975 /* initialise the buffer */
1976 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
1977 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
1979 this_usbduxsub->pwm_cmd_running = 1;
1980 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
1982 this_usbduxsub->pwm_cmd_running = 0;
1988 /* generates the bit pattern for PWM with the optional sign bit */
1989 static int usbdux_pwm_pattern(struct comedi_device *dev,
1990 struct comedi_subdevice *s, int channel,
1991 unsigned int value, unsigned int sign)
1993 struct usbduxsub *this_usbduxsub = dev->private;
2000 if (!this_usbduxsub)
2003 /* this is the DIO bit which carries the PWM data */
2004 pwm_mask = (1 << channel);
2005 /* this is the DIO bit which carries the optional direction bit */
2006 sgn_mask = (16 << channel);
2007 /* this is the buffer which will be filled with the with bit */
2008 /* pattern for one period */
2009 szbuf = this_usbduxsub->sizePwmBuf;
2010 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2011 for (i = 0; i < szbuf; i++) {
2014 c = c & (~pwm_mask);
2015 /* set the bit as long as the index is lower than the value */
2018 /* set the optional sign bit for a relay */
2020 /* positive value */
2021 c = c & (~sgn_mask);
2023 /* negative value */
2031 static int usbdux_pwm_write(struct comedi_device *dev,
2032 struct comedi_subdevice *s,
2033 struct comedi_insn *insn, unsigned int *data)
2035 struct usbduxsub *this_usbduxsub = dev->private;
2037 if (!this_usbduxsub)
2040 if ((insn->n) != 1) {
2042 * doesn't make sense to have more than one value here because
2043 * it would just overwrite the PWM buffer a couple of times
2049 * the sign is set via a special INSN only, this gives us 8 bits for
2051 * relay sign 0 by default
2053 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
2056 static int usbdux_pwm_read(struct comedi_device *x1,
2057 struct comedi_subdevice *x2, struct comedi_insn *x3,
2064 /* switches on/off PWM */
2065 static int usbdux_pwm_config(struct comedi_device *dev,
2066 struct comedi_subdevice *s,
2067 struct comedi_insn *insn, unsigned int *data)
2069 struct usbduxsub *this_usbduxsub = dev->private;
2071 case INSN_CONFIG_ARM:
2073 dev_dbg(&this_usbduxsub->interface->dev,
2074 "comedi%d: %s: pwm on\n", dev->minor, __func__);
2076 * if not zero the PWM is limited to a certain time which is
2077 * not supported here
2081 return usbdux_pwm_start(dev, s);
2082 case INSN_CONFIG_DISARM:
2083 dev_dbg(&this_usbduxsub->interface->dev,
2084 "comedi%d: %s: pwm off\n", dev->minor, __func__);
2085 return usbdux_pwm_cancel(dev, s);
2086 case INSN_CONFIG_GET_PWM_STATUS:
2088 * to check if the USB transmission has failed or in case PWM
2089 * was limited to n cycles to check if it has terminated
2091 data[1] = this_usbduxsub->pwm_cmd_running;
2093 case INSN_CONFIG_PWM_SET_PERIOD:
2094 dev_dbg(&this_usbduxsub->interface->dev,
2095 "comedi%d: %s: setting period\n", dev->minor,
2097 return usbdux_pwm_period(dev, s, data[1]);
2098 case INSN_CONFIG_PWM_GET_PERIOD:
2099 data[1] = this_usbduxsub->pwmPeriod;
2101 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2102 /* value in the first byte and the sign in the second for a
2104 return usbdux_pwm_pattern(dev, s,
2105 /* the channel number */
2106 CR_CHAN(insn->chanspec),
2107 /* actual PWM data */
2111 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2112 /* values are not kept in this driver, nothing to return */
2119 /*****************************************************************/
2121 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2127 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2129 /* shows the usb subsystem that the driver is down */
2130 if (usbduxsub_tmp->interface)
2131 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2133 usbduxsub_tmp->probed = 0;
2135 if (usbduxsub_tmp->urbIn) {
2136 if (usbduxsub_tmp->ai_cmd_running) {
2137 usbduxsub_tmp->ai_cmd_running = 0;
2138 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2140 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2141 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2142 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2143 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2144 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2145 usbduxsub_tmp->urbIn[i] = NULL;
2147 kfree(usbduxsub_tmp->urbIn);
2148 usbduxsub_tmp->urbIn = NULL;
2150 if (usbduxsub_tmp->urbOut) {
2151 if (usbduxsub_tmp->ao_cmd_running) {
2152 usbduxsub_tmp->ao_cmd_running = 0;
2153 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2155 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2156 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2157 kfree(usbduxsub_tmp->
2158 urbOut[i]->transfer_buffer);
2159 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2162 if (usbduxsub_tmp->urbOut[i]) {
2163 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2164 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2165 usbduxsub_tmp->urbOut[i] = NULL;
2168 kfree(usbduxsub_tmp->urbOut);
2169 usbduxsub_tmp->urbOut = NULL;
2171 if (usbduxsub_tmp->urbPwm) {
2172 if (usbduxsub_tmp->pwm_cmd_running) {
2173 usbduxsub_tmp->pwm_cmd_running = 0;
2174 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2176 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2177 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2178 usb_kill_urb(usbduxsub_tmp->urbPwm);
2179 usb_free_urb(usbduxsub_tmp->urbPwm);
2180 usbduxsub_tmp->urbPwm = NULL;
2182 kfree(usbduxsub_tmp->inBuffer);
2183 usbduxsub_tmp->inBuffer = NULL;
2184 kfree(usbduxsub_tmp->insnBuffer);
2185 usbduxsub_tmp->insnBuffer = NULL;
2186 kfree(usbduxsub_tmp->outBuffer);
2187 usbduxsub_tmp->outBuffer = NULL;
2188 kfree(usbduxsub_tmp->dac_commands);
2189 usbduxsub_tmp->dac_commands = NULL;
2190 kfree(usbduxsub_tmp->dux_commands);
2191 usbduxsub_tmp->dux_commands = NULL;
2192 usbduxsub_tmp->ai_cmd_running = 0;
2193 usbduxsub_tmp->ao_cmd_running = 0;
2194 usbduxsub_tmp->pwm_cmd_running = 0;
2197 static int usbduxsigma_attach_common(struct comedi_device *dev,
2198 struct usbduxsub *uds)
2201 struct comedi_subdevice *s;
2206 /* pointer back to the corresponding comedi device */
2207 uds->comedidev = dev;
2209 /* set number of subdevices */
2210 if (uds->high_speed)
2211 n_subdevs = 4; /* with pwm */
2213 n_subdevs = 3; /* without pwm */
2214 ret = comedi_alloc_subdevices(dev, n_subdevs);
2219 /* private structure is also simply the usb-structure */
2221 /* the first subdevice is the A/D converter */
2222 s = &dev->subdevices[SUBDEV_AD];
2223 /* the URBs get the comedi subdevice */
2224 /* which is responsible for reading */
2225 /* this is the subdevice which reads data */
2226 dev->read_subdev = s;
2227 /* the subdevice receives as private structure the */
2231 s->type = COMEDI_SUBD_AI;
2232 /* readable and ref is to ground, 32 bit wide data! */
2233 s->subdev_flags = SDF_READABLE | SDF_GROUND |
2234 SDF_CMD_READ | SDF_LSAMPL;
2235 /* 16 A/D channels */
2236 s->n_chan = NUMCHANNELS;
2237 /* length of the channellist */
2238 s->len_chanlist = NUMCHANNELS;
2239 /* callback functions */
2240 s->insn_read = usbdux_ai_insn_read;
2241 s->do_cmdtest = usbdux_ai_cmdtest;
2242 s->do_cmd = usbdux_ai_cmd;
2243 s->cancel = usbdux_ai_cancel;
2244 /* max value from the A/D converter (24bit) */
2245 s->maxdata = 0x00FFFFFF;
2246 /* range table to convert to physical units */
2247 s->range_table = (&range_usbdux_ai_range);
2248 /* analog output subdevice */
2249 s = &dev->subdevices[SUBDEV_DA];
2251 s->type = COMEDI_SUBD_AO;
2252 /* backward pointer */
2253 dev->write_subdev = s;
2254 /* the subdevice receives as private structure the */
2258 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2261 /* length of the channellist */
2262 s->len_chanlist = 4;
2263 /* 8 bit resolution */
2264 s->maxdata = 0x00ff;
2265 /* unipolar range */
2266 s->range_table = &range_unipolar2_5;
2268 s->do_cmdtest = usbdux_ao_cmdtest;
2269 s->do_cmd = usbdux_ao_cmd;
2270 s->cancel = usbdux_ao_cancel;
2271 s->insn_read = usbdux_ao_insn_read;
2272 s->insn_write = usbdux_ao_insn_write;
2273 /* digital I/O subdevice */
2274 s = &dev->subdevices[SUBDEV_DIO];
2275 s->type = COMEDI_SUBD_DIO;
2276 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2277 /* 8 external and 16 internal channels */
2280 s->range_table = (&range_digital);
2281 s->insn_bits = usbdux_dio_insn_bits;
2282 s->insn_config = usbdux_dio_insn_config;
2283 /* we don't use it */
2285 if (uds->high_speed) {
2286 /* timer / pwm subdevice */
2287 s = &dev->subdevices[SUBDEV_PWM];
2288 s->type = COMEDI_SUBD_PWM;
2289 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2291 /* this defines the max duty cycle resolution */
2292 s->maxdata = uds->sizePwmBuf;
2293 s->insn_write = usbdux_pwm_write;
2294 s->insn_read = usbdux_pwm_read;
2295 s->insn_config = usbdux_pwm_config;
2296 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2298 /* finally decide that it's attached */
2301 offset = usbdux_getstatusinfo(dev, 0);
2303 dev_err(&uds->interface->dev,
2304 "Communication to USBDUXSIGMA failed! Check firmware and cabling.");
2305 dev_info(&uds->interface->dev,
2306 "comedi%d: attached, ADC_zero = %x\n", dev->minor, offset);
2310 static int usbduxsigma_auto_attach(struct comedi_device *dev,
2311 unsigned long context_unused)
2313 struct usb_interface *uinterf = comedi_to_usb_interface(dev);
2315 struct usbduxsub *uds;
2317 dev->private = NULL;
2318 down(&start_stop_sem);
2319 uds = usb_get_intfdata(uinterf);
2320 if (!uds || !uds->probed) {
2321 dev_err(dev->class_dev,
2322 "usbduxsigma: error: auto_attach failed, not connected\n");
2324 } else if (uds->attached) {
2325 dev_err(dev->class_dev,
2326 "usbduxsigma: error: auto_attach failed, already attached\n");
2329 ret = usbduxsigma_attach_common(dev, uds);
2330 up(&start_stop_sem);
2334 static void usbduxsigma_detach(struct comedi_device *dev)
2336 struct usbduxsub *usb = dev->private;
2340 dev->private = NULL;
2342 usb->comedidev = NULL;
2347 static struct comedi_driver usbduxsigma_driver = {
2348 .driver_name = "usbduxsigma",
2349 .module = THIS_MODULE,
2350 .auto_attach = usbduxsigma_auto_attach,
2351 .detach = usbduxsigma_detach,
2354 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2357 struct usbduxsub *usbduxsub_tmp = context;
2358 struct usb_interface *uinterf = usbduxsub_tmp->interface;
2362 dev_err(&uinterf->dev,
2363 "Firmware complete handler without firmware!\n");
2368 * we need to upload the firmware here because fw will be
2369 * freed once we've left this function
2371 ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2374 dev_err(&uinterf->dev,
2375 "Could not upload firmware (err=%d)\n", ret);
2378 comedi_usb_auto_config(uinterf, &usbduxsigma_driver, 0);
2380 release_firmware(fw);
2383 static int usbduxsigma_usb_probe(struct usb_interface *uinterf,
2384 const struct usb_device_id *id)
2386 struct usb_device *udev = interface_to_usbdev(uinterf);
2387 struct device *dev = &uinterf->dev;
2392 dev_dbg(dev, "comedi_: usbdux_: "
2393 "finding a free structure for the usb-device\n");
2395 down(&start_stop_sem);
2396 /* look for a free place in the usbdux array */
2398 for (i = 0; i < NUMUSBDUX; i++) {
2399 if (!(usbduxsub[i].probed)) {
2407 dev_err(dev, "Too many usbduxsigma-devices connected.\n");
2408 up(&start_stop_sem);
2411 dev_dbg(dev, "comedi_: usbdux: "
2412 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2414 sema_init(&(usbduxsub[index].sem), 1);
2415 /* save a pointer to the usb device */
2416 usbduxsub[index].usbdev = udev;
2418 /* save the interface itself */
2419 usbduxsub[index].interface = uinterf;
2420 /* get the interface number from the interface */
2421 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2422 /* hand the private data over to the usb subsystem */
2423 /* will be needed for disconnect */
2424 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2426 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2428 /* test if it is high speed (USB 2.0) */
2429 usbduxsub[index].high_speed =
2430 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2432 /* create space for the commands of the DA converter */
2433 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2434 if (!usbduxsub[index].dac_commands) {
2435 tidy_up(&(usbduxsub[index]));
2436 up(&start_stop_sem);
2439 /* create space for the commands going to the usb device */
2440 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2441 if (!usbduxsub[index].dux_commands) {
2442 tidy_up(&(usbduxsub[index]));
2443 up(&start_stop_sem);
2446 /* create space for the in buffer and set it to zero */
2447 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2448 if (!(usbduxsub[index].inBuffer)) {
2449 tidy_up(&(usbduxsub[index]));
2450 up(&start_stop_sem);
2453 /* create space of the instruction buffer */
2454 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2455 if (!(usbduxsub[index].insnBuffer)) {
2456 tidy_up(&(usbduxsub[index]));
2457 up(&start_stop_sem);
2460 /* create space for the outbuffer */
2461 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2462 if (!(usbduxsub[index].outBuffer)) {
2463 tidy_up(&(usbduxsub[index]));
2464 up(&start_stop_sem);
2467 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2468 i = usb_set_interface(usbduxsub[index].usbdev,
2469 usbduxsub[index].ifnum, 3);
2471 dev_err(dev, "comedi_: usbduxsigma%d: "
2472 "could not set alternate setting 3 in high speed.\n",
2474 tidy_up(&(usbduxsub[index]));
2475 up(&start_stop_sem);
2478 if (usbduxsub[index].high_speed)
2479 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2481 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2483 usbduxsub[index].urbIn = kcalloc(usbduxsub[index].numOfInBuffers,
2484 sizeof(struct urb *),
2486 if (!(usbduxsub[index].urbIn)) {
2487 tidy_up(&(usbduxsub[index]));
2488 up(&start_stop_sem);
2491 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2492 /* one frame: 1ms */
2493 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2494 if (usbduxsub[index].urbIn[i] == NULL) {
2495 dev_err(dev, "comedi_: usbduxsigma%d: "
2496 "Could not alloc. urb(%d)\n", index, i);
2497 tidy_up(&(usbduxsub[index]));
2498 up(&start_stop_sem);
2501 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2502 /* will be filled later with a pointer to the comedi-device */
2503 /* and ONLY then the urb should be submitted */
2504 usbduxsub[index].urbIn[i]->context = NULL;
2505 usbduxsub[index].urbIn[i]->pipe =
2506 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2507 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2508 usbduxsub[index].urbIn[i]->transfer_buffer =
2509 kzalloc(SIZEINBUF, GFP_KERNEL);
2510 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2511 tidy_up(&(usbduxsub[index]));
2512 up(&start_stop_sem);
2515 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2516 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2517 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2518 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2519 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length =
2524 if (usbduxsub[index].high_speed)
2525 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2527 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2529 usbduxsub[index].urbOut = kcalloc(usbduxsub[index].numOfOutBuffers,
2530 sizeof(struct urb *), GFP_KERNEL);
2531 if (!(usbduxsub[index].urbOut)) {
2532 tidy_up(&(usbduxsub[index]));
2533 up(&start_stop_sem);
2536 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2537 /* one frame: 1ms */
2538 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2539 if (usbduxsub[index].urbOut[i] == NULL) {
2540 dev_err(dev, "comedi_: usbduxsigma%d: "
2541 "Could not alloc. urb(%d)\n", index, i);
2542 tidy_up(&(usbduxsub[index]));
2543 up(&start_stop_sem);
2546 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2547 /* will be filled later with a pointer to the comedi-device */
2548 /* and ONLY then the urb should be submitted */
2549 usbduxsub[index].urbOut[i]->context = NULL;
2550 usbduxsub[index].urbOut[i]->pipe =
2551 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2552 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2553 usbduxsub[index].urbOut[i]->transfer_buffer =
2554 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2555 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2556 tidy_up(&(usbduxsub[index]));
2557 up(&start_stop_sem);
2560 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2561 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2562 usbduxsub[index].urbOut[i]->transfer_buffer_length =
2564 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2565 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2567 if (usbduxsub[index].high_speed) {
2569 usbduxsub[index].urbOut[i]->interval = 8;
2572 usbduxsub[index].urbOut[i]->interval = 1;
2577 if (usbduxsub[index].high_speed) {
2578 /* max bulk ep size in high speed */
2579 usbduxsub[index].sizePwmBuf = 512;
2580 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2581 if (usbduxsub[index].urbPwm == NULL) {
2582 dev_err(dev, "comedi_: usbduxsigma%d: "
2583 "Could not alloc. pwm urb\n", index);
2584 tidy_up(&(usbduxsub[index]));
2585 up(&start_stop_sem);
2588 usbduxsub[index].urbPwm->transfer_buffer =
2589 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2590 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2591 tidy_up(&(usbduxsub[index]));
2592 up(&start_stop_sem);
2596 usbduxsub[index].urbPwm = NULL;
2597 usbduxsub[index].sizePwmBuf = 0;
2600 usbduxsub[index].ai_cmd_running = 0;
2601 usbduxsub[index].ao_cmd_running = 0;
2602 usbduxsub[index].pwm_cmd_running = 0;
2604 /* we've reached the bottom of the function */
2605 usbduxsub[index].probed = 1;
2606 up(&start_stop_sem);
2608 ret = request_firmware_nowait(THIS_MODULE,
2614 usbdux_firmware_request_complete_handler
2618 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2622 dev_info(dev, "comedi_: successfully initialised.\n");
2627 static void usbduxsigma_usb_disconnect(struct usb_interface *intf)
2629 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2630 struct usb_device *udev = interface_to_usbdev(intf);
2632 if (!usbduxsub_tmp) {
2634 "comedi_: disconnect called with null pointer.\n");
2637 if (usbduxsub_tmp->usbdev != udev) {
2638 dev_err(&intf->dev, "comedi_: BUG! wrong ptr!\n");
2641 if (usbduxsub_tmp->ai_cmd_running)
2642 /* we are still running a command */
2643 usbdux_ai_stop(usbduxsub_tmp, 1);
2644 if (usbduxsub_tmp->ao_cmd_running)
2645 /* we are still running a command */
2646 usbdux_ao_stop(usbduxsub_tmp, 1);
2647 comedi_usb_auto_unconfig(intf);
2648 down(&start_stop_sem);
2649 down(&usbduxsub_tmp->sem);
2650 tidy_up(usbduxsub_tmp);
2651 up(&usbduxsub_tmp->sem);
2652 up(&start_stop_sem);
2653 dev_info(&intf->dev, "comedi_: disconnected from the usb\n");
2656 static const struct usb_device_id usbduxsigma_usb_table[] = {
2657 { USB_DEVICE(0x13d8, 0x0020) },
2658 { USB_DEVICE(0x13d8, 0x0021) },
2659 { USB_DEVICE(0x13d8, 0x0022) },
2662 MODULE_DEVICE_TABLE(usb, usbduxsigma_usb_table);
2664 static struct usb_driver usbduxsigma_usb_driver = {
2665 .name = "usbduxsigma",
2666 .probe = usbduxsigma_usb_probe,
2667 .disconnect = usbduxsigma_usb_disconnect,
2668 .id_table = usbduxsigma_usb_table,
2670 module_comedi_usb_driver(usbduxsigma_driver, usbduxsigma_usb_driver);
2672 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
2673 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com");
2674 MODULE_LICENSE("GPL");
2675 MODULE_FIRMWARE(FIRMWARE);