1 #define DRIVER_VERSION "v0.5"
2 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
3 #define DRIVER_DESC "Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com"
5 comedi/drivers/usbdux.c
6 Copyright (C) 2011 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: Driver for USB-DUX-SIGMA of INCITE Technology Limited
26 Devices: [ITL] USB-DUX-SIGMA (usbduxsigma)
27 Author: Bernd Porr <tech@linux-usb-daq.co.uk>
31 The following subdevices are available
34 number of channels: 16
35 max data value: 16777215 (0xfffff, 24bits)
37 all channels: [-1.325 V,1.325 V]
40 scan_begin: timer (contains the sampling interval. min 250us)
49 all channels: [0 V,2.5 V]
52 scan_begin: timer (contains the sampling interval. min 1ms)
58 number of channels: 24 (first 8 bits on the D connector, 16 bits int.)
61 number of channels: 8 or 4 + polarity output for H-bridge
62 (see INSN_CONFIG_PWM_SET_H_BRIDGE where
63 the first byte is the value and the
67 Configuration options:
68 The device requires firmware which is usually
69 uploaded automatically by udev/hotplug at the moment
70 the driver module is loaded.
71 In case udev/hotplug is not enabled you need to upload
72 the firmware with comedi_config -i usbdux_firmware.bin.
73 The firmware is usually installed under /lib/firmware
74 or can be downloaded form http://www.linux-usb-daq.co.uk.
77 * I must give credit here to Chris Baugher who
78 * wrote the driver for AT-MIO-16d. I used some parts of this
79 * driver. I also must give credits to David Brownell
80 * who supported me with the USB development.
82 * Note: the raw data from the A/D converter is 24 bit big endian
83 * anything else is little endian to/from the dux board
88 * 0.2: all basic functions implemented, digital I/O only for one port
89 * 0.3: proper vendor ID and driver name
90 * 0.4: fixed D/A voltage range
91 * 0.5: various bug fixes, health check at startup
94 /* generates loads of debug info */
95 /* #define NOISY_DUX_DEBUGBUG */
97 // #define dev_dbg(dev, format, arg...) printk(format , ## arg)
100 #include <linux/kernel.h>
101 #include <linux/module.h>
102 #include <linux/init.h>
103 #include <linux/slab.h>
104 #include <linux/input.h>
105 #include <linux/usb.h>
106 #include <linux/fcntl.h>
107 #include <linux/compiler.h>
108 #include <linux/firmware.h>
109 #include "comedi_fc.h"
110 #include <linux/comedidev.h>
111 #include <linux/usb.h>
113 #define BOARDNAME "usbduxsigma"
115 /* timeout for the USB-transfer in ms*/
116 #define BULK_TIMEOUT 1000
118 /* constants for "firmware" upload and download */
119 #define USBDUXSUB_FIRMWARE 0xA0
120 #define VENDOR_DIR_IN 0xC0
121 #define VENDOR_DIR_OUT 0x40
123 /* internal addresses of the 8051 processor */
124 #define USBDUXSUB_CPUCS 0xE600
127 * the minor device number, major is 180 only for debugging purposes and to
128 * upload special firmware (programming the eeprom etc) which is not
129 * compatible with the comedi framwork
131 #define USBDUXSUB_MINOR 32
133 /* max lenghth of the transfer-buffer for software upload */
134 #define TB_LEN 0x2000
136 /* Input endpoint number: ISO/IRQ */
139 /* Output endpoint number: ISO/IRQ */
142 /* This EP sends DUX commands to USBDUX */
143 #define COMMAND_OUT_EP 1
145 /* This EP receives the DUX commands from USBDUX */
146 #define COMMAND_IN_EP 8
148 /* Output endpoint for PWM */
151 /* 300Hz max frequ under PWM */
152 #define MIN_PWM_PERIOD ((long)(1E9/300))
154 /* Default PWM frequency */
155 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
157 /* Number of channels (16 AD and offset)*/
158 #define NUMCHANNELS 16
160 /* Size of one A/D value */
161 #define SIZEADIN ((sizeof(int32_t)))
164 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
167 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
170 #define SIZEINSNBUF 16
172 /* Number of DA channels */
173 #define NUMOUTCHANNELS 8
175 /* size of one value for the D/A converter: channel and value */
176 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(int16_t)))
179 * Size of the output-buffer in bytes
180 * Actually only the first 4 triplets are used but for the
181 * high speed mode we need to pad it to 8 (microframes).
183 #define SIZEOUTBUF ((8*SIZEDAOUT))
186 * Size of the buffer for the dux commands: just now max size is determined
187 * by the analogue out + command byte + panic bytes...
189 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
191 /* Number of in-URBs which receive the data: min=2 */
192 #define NUMOFINBUFFERSFULL 5
194 /* Number of out-URBs which send the data: min=2 */
195 #define NUMOFOUTBUFFERSFULL 5
197 /* Number of in-URBs which receive the data: min=5 */
198 /* must have more buffers due to buggy USB ctr */
199 #define NUMOFINBUFFERSHIGH 10
201 /* Number of out-URBs which send the data: min=5 */
202 /* must have more buffers due to buggy USB ctr */
203 #define NUMOFOUTBUFFERSHIGH 10
205 /* Total number of usbdux devices */
208 /* Analogue in subdevice */
211 /* Analogue out subdevice */
217 /* timer aka pwm output */
220 /* number of retries to get the right dux command */
224 /**************************************************/
225 /* comedi constants */
226 static const comedi_lrange range_usbdux_ai_range = { 1, {
232 static const comedi_lrange range_usbdux_ao_range = { 1, {
239 * private structure of one subdevice
243 * This is the structure which holds all the data of
244 * this driver one sub device just now: A/D
249 /* is it associated with a subdevice? */
251 /* pointer to the usb-device */
252 struct usb_device *usbdev;
253 /* actual number of in-buffers */
255 /* actual number of out-buffers */
257 /* ISO-transfer handling: buffers */
260 /* pwm-transfer handling */
263 unsigned int pwmPeriod;
264 /* PWM internal delay for the GPIF in the FX2 */
266 /* size of the PWM buffer which holds the bit pattern */
268 /* input buffer for the ISO-transfer */
270 /* input buffer for single insn */
272 /* output buffer for single DA outputs */
274 /* interface number */
276 /* interface structure in 2.6 */
277 struct usb_interface *interface;
278 /* comedi device for the interrupt context */
279 comedi_device *comedidev;
280 /* is it USB_SPEED_HIGH or not? */
281 short int high_speed;
282 /* asynchronous command is running */
283 short int ai_cmd_running;
284 short int ao_cmd_running;
286 short int pwm_cmd_running;
287 /* continous aquisition */
288 short int ai_continous;
289 short int ao_continous;
290 /* number of samples to acquire */
293 /* time between samples in units of the timer */
294 unsigned int ai_timer;
295 unsigned int ao_timer;
296 /* counter between aquisitions */
297 unsigned int ai_counter;
298 unsigned int ao_counter;
299 /* interval in frames/uframes */
300 unsigned int ai_interval;
302 uint8_t *dac_commands;
304 uint8_t *dux_commands;
309 * The pointer to the private usb-data of the driver is also the private data
310 * for the comedi-device. This has to be global as the usb subsystem needs
311 * global variables. The other reason is that this structure must be there
312 * _before_ any comedi command is issued. The usb subsystem must be initialised
313 * before comedi can access it.
315 static struct usbduxsub usbduxsub[NUMUSBDUX];
317 static DEFINE_MUTEX(start_stop_sem);
320 * Stops the data acquision
321 * It should be safe to call this function from any context
323 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
328 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
329 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
330 if (usbduxsub_tmp->urbIn[i]) {
331 /* We wait here until all transfers have been
333 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
335 dev_dbg(&usbduxsub_tmp->interface->dev,
336 "comedi: usbdux: unlinked InURB %d, err=%d\n",
344 * This will stop a running acquisition operation
345 * Is called from within this driver from both the
346 * interrupt context and from comedi
348 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
352 if (!this_usbduxsub) {
353 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
356 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
360 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
363 this_usbduxsub->ai_cmd_running = 0;
369 * This will cancel a running acquisition operation.
370 * This is called by comedi but never from inside the driver.
372 static int usbdux_ai_cancel(comedi_device *dev,
375 struct usbduxsub *this_usbduxsub;
378 /* force unlink of all urbs */
379 this_usbduxsub = dev->private;
383 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
385 /* prevent other CPUs from submitting new commands just now */
386 mutex_lock(&this_usbduxsub->sem);
387 if (!(this_usbduxsub->probed)) {
388 mutex_unlock(&this_usbduxsub->sem);
391 /* unlink only if the urb really has been submitted */
392 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
393 mutex_unlock(&this_usbduxsub->sem);
397 /* analogue IN - interrupt service routine */
398 static void usbduxsub_ai_IsocIrq(struct urb *urb)
401 struct usbduxsub *this_usbduxsub;
402 comedi_device *this_comedidev;
405 unsigned int dio_state;
407 /* the context variable points to the comedi device */
408 this_comedidev = urb->context;
409 /* the private structure of the subdevice is struct usbduxsub */
410 this_usbduxsub = this_comedidev->private;
411 /* subdevice which is the AD converter */
412 s = this_comedidev->subdevices + SUBDEV_AD;
414 /* first we test if something unusual has just happened */
415 switch (urb->status) {
417 /* copy the result in the transfer buffer */
418 memcpy(this_usbduxsub->inBuffer,
419 urb->transfer_buffer, SIZEINBUF);
422 /* error in the ISOchronous data */
423 /* we don't copy the data into the transfer buffer */
424 /* and recycle the last data byte */
425 dev_dbg(&urb->dev->dev,
426 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
427 this_usbduxsub->comedidev->minor);
434 /* happens after an unlink command */
435 if (this_usbduxsub->ai_cmd_running) {
436 /* we are still running a command */
437 /* tell this comedi */
438 s->async->events |= COMEDI_CB_EOA;
439 s->async->events |= COMEDI_CB_ERROR;
440 comedi_event(this_usbduxsub->comedidev, s);
441 /* stop the transfer w/o unlink */
442 usbdux_ai_stop(this_usbduxsub, 0);
447 /* a real error on the bus */
448 /* pass error to comedi if we are really running a command */
449 if (this_usbduxsub->ai_cmd_running) {
450 dev_err(&urb->dev->dev,
451 "Non-zero urb status received in ai intr "
452 "context: %d\n", urb->status);
453 s->async->events |= COMEDI_CB_EOA;
454 s->async->events |= COMEDI_CB_ERROR;
455 comedi_event(this_usbduxsub->comedidev, s);
456 /* don't do an unlink here */
457 usbdux_ai_stop(this_usbduxsub, 0);
463 * at this point we are reasonably sure that nothing dodgy has happened
464 * are we running a command?
466 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
468 * not running a command, do not continue execution if no
469 * asynchronous command is running in particular not resubmit
474 urb->dev = this_usbduxsub->usbdev;
476 /* resubmit the urb */
477 err = usb_submit_urb(urb, GFP_ATOMIC);
478 if (unlikely(err < 0)) {
479 dev_err(&urb->dev->dev,
480 "comedi_: urb resubmit failed in int-context!"
483 if (err == -EL2NSYNC)
484 dev_err(&urb->dev->dev,
485 "buggy USB host controller or bug in IRQ "
487 s->async->events |= COMEDI_CB_EOA;
488 s->async->events |= COMEDI_CB_ERROR;
489 comedi_event(this_usbduxsub->comedidev, s);
490 /* don't do an unlink here */
491 usbdux_ai_stop(this_usbduxsub, 0);
495 /* get the state of the dio pins to allow external trigger */
496 dio_state = be32_to_cpu(this_usbduxsub->inBuffer[0]);
498 this_usbduxsub->ai_counter--;
499 if (likely(this_usbduxsub->ai_counter > 0))
502 /* timer zero, transfer measurements to comedi */
503 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
505 /* test, if we transmit only a fixed number of samples */
506 if (!(this_usbduxsub->ai_continous)) {
507 /* not continous, fixed number of samples */
508 this_usbduxsub->ai_sample_count--;
509 /* all samples received? */
510 if (this_usbduxsub->ai_sample_count < 0) {
511 /* prevent a resubmit next time */
512 usbdux_ai_stop(this_usbduxsub, 0);
513 /* say comedi that the acquistion is over */
514 s->async->events |= COMEDI_CB_EOA;
515 comedi_event(this_usbduxsub->comedidev, s);
519 /* get the data from the USB bus and hand it over to comedi */
520 n = s->async->cmd.chanlist_len;
521 for (i = 0; i < n; i++) {
522 /* transfer data, note first byte is the DIO state */
523 v = be32_to_cpu(this_usbduxsub->inBuffer[i+1]);
524 /* strip status byte */
526 /* convert to unsigned */
528 /* write the byte to the buffer */
529 err = cfc_write_array_to_buffer(s, &v, sizeof(uint32_t));
530 if (unlikely(err == 0)) {
531 /* buffer overflow */
532 usbdux_ai_stop(this_usbduxsub, 0);
536 /* tell comedi that data is there */
537 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
538 comedi_event(this_usbduxsub->comedidev, s);
541 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
546 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
547 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
548 if (usbduxsub_tmp->urbOut[i])
549 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
551 dev_dbg(&usbduxsub_tmp->interface->dev,
552 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
559 /* This will cancel a running acquisition operation
562 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
568 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
571 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
573 this_usbduxsub->ao_cmd_running = 0;
578 /* force unlink, is called by comedi */
579 static int usbdux_ao_cancel(comedi_device *dev,
582 struct usbduxsub *this_usbduxsub = dev->private;
588 /* prevent other CPUs from submitting a command just now */
589 mutex_lock(&this_usbduxsub->sem);
590 if (!(this_usbduxsub->probed)) {
591 mutex_unlock(&this_usbduxsub->sem);
594 /* unlink only if it is really running */
595 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
596 mutex_unlock(&this_usbduxsub->sem);
600 static void usbduxsub_ao_IsocIrq(struct urb *urb)
604 struct usbduxsub *this_usbduxsub;
605 comedi_device *this_comedidev;
608 /* the context variable points to the subdevice */
609 this_comedidev = urb->context;
610 /* the private structure of the subdevice is struct usbduxsub */
611 this_usbduxsub = this_comedidev->private;
613 s = this_comedidev->subdevices + SUBDEV_DA;
615 switch (urb->status) {
624 /* after an unlink command, unplug, ... etc */
625 /* no unlink needed here. Already shutting down. */
626 if (this_usbduxsub->ao_cmd_running) {
627 s->async->events |= COMEDI_CB_EOA;
628 comedi_event(this_usbduxsub->comedidev, s);
629 usbdux_ao_stop(this_usbduxsub, 0);
635 if (this_usbduxsub->ao_cmd_running) {
636 dev_err(&urb->dev->dev,
637 "comedi_: Non-zero urb status received in ao "
638 "intr context: %d\n", urb->status);
639 s->async->events |= COMEDI_CB_ERROR;
640 s->async->events |= COMEDI_CB_EOA;
641 comedi_event(this_usbduxsub->comedidev, s);
642 /* we do an unlink if we are in the high speed mode */
643 usbdux_ao_stop(this_usbduxsub, 0);
648 /* are we actually running? */
649 if (!(this_usbduxsub->ao_cmd_running))
652 /* normal operation: executing a command in this subdevice */
653 this_usbduxsub->ao_counter--;
654 if ((int)this_usbduxsub->ao_counter <= 0) {
656 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
658 /* handle non continous aquisition */
659 if (!(this_usbduxsub->ao_continous)) {
660 /* fixed number of samples */
661 this_usbduxsub->ao_sample_count--;
662 if (this_usbduxsub->ao_sample_count < 0) {
663 /* all samples transmitted */
664 usbdux_ao_stop(this_usbduxsub, 0);
665 s->async->events |= COMEDI_CB_EOA;
666 comedi_event(this_usbduxsub->comedidev, s);
667 /* no resubmit of the urb */
671 /* transmit data to the USB bus */
672 ((uint8_t *) (urb->transfer_buffer))[0] =
673 s->async->cmd.chanlist_len;
674 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
676 if (i >= NUMOUTCHANNELS)
679 /* pointer to the DA */
681 (&(((uint8_t *) urb->transfer_buffer)[i * 2 + 1]));
682 /* get the data from comedi */
683 ret = comedi_buf_get(s->async, &temp);
685 datap[1] = this_usbduxsub->dac_commands[i];
686 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
687 /* datap[0],datap[1],datap[2]); */
689 dev_err(&urb->dev->dev,
690 "comedi: buffer underflow\n");
691 s->async->events |= COMEDI_CB_EOA;
692 s->async->events |= COMEDI_CB_OVERFLOW;
694 /* transmit data to comedi */
695 s->async->events |= COMEDI_CB_BLOCK;
696 comedi_event(this_usbduxsub->comedidev, s);
699 urb->transfer_buffer_length = SIZEOUTBUF;
700 urb->dev = this_usbduxsub->usbdev;
702 if (this_usbduxsub->ao_cmd_running) {
703 if (this_usbduxsub->high_speed) {
710 urb->number_of_packets = 1;
711 urb->iso_frame_desc[0].offset = 0;
712 urb->iso_frame_desc[0].length = SIZEOUTBUF;
713 urb->iso_frame_desc[0].status = 0;
714 ret = usb_submit_urb(urb, GFP_ATOMIC);
716 dev_err(&urb->dev->dev,
717 "comedi_: ao urb resubm failed in int-cont. "
720 dev_err(&urb->dev->dev,
721 "buggy USB host controller or bug in "
724 s->async->events |= COMEDI_CB_EOA;
725 s->async->events |= COMEDI_CB_ERROR;
726 comedi_event(this_usbduxsub->comedidev, s);
727 /* don't do an unlink here */
728 usbdux_ao_stop(this_usbduxsub, 0);
733 static int usbduxsub_start(struct usbduxsub *usbduxsub)
736 uint8_t local_transfer_buffer[16];
739 local_transfer_buffer[0] = 0;
740 errcode = usb_control_msg(usbduxsub->usbdev,
741 /* create a pipe for a control transfer */
742 usb_sndctrlpipe(usbduxsub->usbdev, 0),
743 /* bRequest, "Firmware" */
751 /* address of the transfer buffer */
752 local_transfer_buffer,
758 dev_err(&usbduxsub->interface->dev,
759 "comedi_: control msg failed (start)\n");
765 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
769 uint8_t local_transfer_buffer[16];
772 local_transfer_buffer[0] = 1;
773 errcode = usb_control_msg(usbduxsub->usbdev,
774 usb_sndctrlpipe(usbduxsub->usbdev, 0),
775 /* bRequest, "Firmware" */
782 0x0000, local_transfer_buffer,
788 dev_err(&usbduxsub->interface->dev,
789 "comedi_: control msg failed (stop)\n");
795 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
796 uint8_t *local_transfer_buffer,
797 unsigned int startAddr, unsigned int len)
801 errcode = usb_control_msg(usbduxsub->usbdev,
802 usb_sndctrlpipe(usbduxsub->usbdev, 0),
803 /* brequest, firmware */
811 /* our local safe buffer */
812 local_transfer_buffer,
817 dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
819 dev_err(&usbduxsub->interface->dev,
820 "comedi_: upload failed\n");
826 /* the FX2LP has twice as much as the standard FX2 */
827 #define FIRMWARE_MAX_LEN 0x4000
829 static int firmwareUpload(struct usbduxsub *usbduxsub,
830 const u8 *firmwareBinary, int sizeFirmware)
838 if (sizeFirmware > FIRMWARE_MAX_LEN) {
839 dev_err(&usbduxsub->interface->dev,
840 "usbduxsigma firmware binary it too large for FX2.\n");
844 /* we generate a local buffer for the firmware */
845 fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
847 dev_err(&usbduxsub->interface->dev,
848 "comedi_: mem alloc for firmware failed\n");
852 ret = usbduxsub_stop(usbduxsub);
854 dev_err(&usbduxsub->interface->dev,
855 "comedi_: can not stop firmware\n");
860 ret = usbduxsub_upload(usbduxsub, fwBuf, 0, sizeFirmware);
862 dev_err(&usbduxsub->interface->dev,
863 "comedi_: firmware upload failed\n");
867 ret = usbduxsub_start(usbduxsub);
869 dev_err(&usbduxsub->interface->dev,
870 "comedi_: can not start firmware\n");
878 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
885 /* Submit all URBs and start the transfer on the bus */
886 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
887 /* in case of a resubmission after an unlink... */
888 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
889 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
890 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
891 usbduxsub->urbIn[i]->status = 0;
892 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
893 dev_dbg(&usbduxsub->interface->dev,
894 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
895 usbduxsub->comedidev->minor, i,
896 (usbduxsub->urbIn[i]->context),
897 (usbduxsub->urbIn[i]->dev),
898 (usbduxsub->urbIn[i]->interval));
899 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
901 dev_err(&usbduxsub->interface->dev,
902 "comedi_: ai: usb_submit_urb(%d) error %d\n",
910 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
917 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
918 dev_dbg(&usbduxsub->interface->dev,
919 "comedi_: submitting out-urb[%d]\n", i);
920 /* in case of a resubmission after an unlink... */
921 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
922 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
923 usbduxsub->urbOut[i]->status = 0;
924 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
925 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
927 dev_err(&usbduxsub->interface->dev,
928 "comedi_: ao: usb_submit_urb(%d) error %d\n",
936 static int chanToInterval(int nChannels)
948 static int usbdux_ai_cmdtest(comedi_device *dev,
953 unsigned int tmpTimer;
954 struct usbduxsub *this_usbduxsub = dev->private;
956 if (!(this_usbduxsub->probed))
959 dev_dbg(&this_usbduxsub->interface->dev,
960 "comedi%d: usbdux_ai_cmdtest\n", dev->minor);
962 /* make sure triggers are valid */
963 /* Only immediate triggers are allowed */
964 tmp = cmd->start_src;
965 cmd->start_src &= TRIG_NOW | TRIG_INT;
966 if (!cmd->start_src || tmp != cmd->start_src)
969 /* trigger should happen timed */
970 tmp = cmd->scan_begin_src;
971 /* start a new _scan_ with a timer */
972 cmd->scan_begin_src &= TRIG_TIMER;
973 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
976 /* scanning is continous */
977 tmp = cmd->convert_src;
978 cmd->convert_src &= TRIG_NOW;
979 if (!cmd->convert_src || tmp != cmd->convert_src)
982 /* issue a trigger when scan is finished and start a new scan */
983 tmp = cmd->scan_end_src;
984 cmd->scan_end_src &= TRIG_COUNT;
985 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
988 /* trigger at the end of count events or not, stop condition or not */
990 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
991 if (!cmd->stop_src || tmp != cmd->stop_src)
998 * step 2: make sure trigger sources are unique and mutually compatible
999 * note that mutual compatibility is not an issue here
1001 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1002 cmd->scan_begin_src != TRIG_EXT &&
1003 cmd->scan_begin_src != TRIG_TIMER)
1005 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1011 /* step 3: make sure arguments are trivially compatible */
1012 if (cmd->start_arg != 0) {
1017 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1018 /* internal trigger */
1019 if (cmd->scan_begin_arg != 0) {
1020 cmd->scan_begin_arg = 0;
1025 if (cmd->scan_begin_src == TRIG_TIMER) {
1026 if (this_usbduxsub->high_speed) {
1028 * In high speed mode microframes are possible.
1029 * However, during one microframe we can roughly
1030 * sample two channels. Thus, the more channels
1031 * are in the channel list the more time we need.
1033 i = chanToInterval(cmd->chanlist_len);
1034 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
1035 cmd->scan_begin_arg = 1000000 / 8 * i;
1038 /* now calc the real sampling rate with all the
1039 * rounding errors */
1041 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
1043 if (cmd->scan_begin_arg != tmpTimer) {
1044 cmd->scan_begin_arg = tmpTimer;
1049 /* 1kHz scans every USB frame */
1050 if (cmd->scan_begin_arg < 1000000) {
1051 cmd->scan_begin_arg = 1000000;
1055 * calc the real sampling rate with the rounding errors
1057 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
1058 1000000)) * 1000000;
1059 if (cmd->scan_begin_arg != tmpTimer) {
1060 cmd->scan_begin_arg = tmpTimer;
1065 /* the same argument */
1066 if (cmd->scan_end_arg != cmd->chanlist_len) {
1067 cmd->scan_end_arg = cmd->chanlist_len;
1071 if (cmd->stop_src == TRIG_COUNT) {
1072 /* any count is allowed */
1075 if (cmd->stop_arg != 0) {
1088 * creates the ADC command for the MAX1271
1089 * range is the range value from comedi
1091 static void create_adc_command(unsigned int chan,
1096 (*muxsg0) = (*muxsg0) | (1 << chan);
1098 (*muxsg1) = (*muxsg1) | (1 << (chan-8));
1102 /* bulk transfers to usbdux */
1104 #define SENDADCOMMANDS 0
1105 #define SENDDACOMMANDS 1
1106 #define SENDDIOCONFIGCOMMAND 2
1107 #define SENDDIOBITSCOMMAND 3
1108 #define SENDSINGLEAD 4
1110 #define SENDPWMOFF 8
1112 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1116 this_usbduxsub->dux_commands[0] = cmd_type;
1117 #ifdef NOISY_DUX_DEBUGBUG
1118 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1119 this_usbduxsub->comedidev->minor);
1120 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1121 printk(" %02x", this_usbduxsub->dux_commands[result]);
1124 result = usb_bulk_msg(this_usbduxsub->usbdev,
1125 usb_sndbulkpipe(this_usbduxsub->usbdev,
1127 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1128 &nsent, BULK_TIMEOUT);
1130 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1131 "could not transmit dux_command to the usb-device, "
1132 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1137 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1139 int result = (-EFAULT);
1143 for (i = 0; i < RETRIES; i++) {
1144 result = usb_bulk_msg(this_usbduxsub->usbdev,
1145 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1147 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1148 &nrec, BULK_TIMEOUT);
1150 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1151 "insn: USB error %d "
1152 "while receiving DUX command"
1153 "\n", this_usbduxsub->comedidev->minor,
1157 if (this_usbduxsub->insnBuffer[0] == command)
1160 /* this is only reached if the data has been requested a couple of
1162 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1163 "wrong data returned from firmware: want %d, got %d.\n",
1164 this_usbduxsub->comedidev->minor, command,
1165 this_usbduxsub->insnBuffer[0]);
1169 static int usbdux_ai_inttrig(comedi_device *dev,
1170 comedi_subdevice *s, unsigned int trignum)
1173 struct usbduxsub *this_usbduxsub = dev->private;
1174 if (!this_usbduxsub)
1177 mutex_lock(&this_usbduxsub->sem);
1178 if (!(this_usbduxsub->probed)) {
1179 mutex_unlock(&this_usbduxsub->sem);
1182 dev_dbg(&this_usbduxsub->interface->dev,
1183 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1186 dev_err(&this_usbduxsub->interface->dev,
1187 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1189 mutex_unlock(&this_usbduxsub->sem);
1192 if (!(this_usbduxsub->ai_cmd_running)) {
1193 this_usbduxsub->ai_cmd_running = 1;
1194 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1196 dev_err(&this_usbduxsub->interface->dev,
1197 "comedi%d: usbdux_ai_inttrig: "
1198 "urbSubmit: err=%d\n", dev->minor, ret);
1199 this_usbduxsub->ai_cmd_running = 0;
1200 mutex_unlock(&this_usbduxsub->sem);
1203 s->async->inttrig = NULL;
1205 dev_err(&this_usbduxsub->interface->dev,
1206 "comedi%d: ai_inttrig but acqu is already running\n",
1209 mutex_unlock(&this_usbduxsub->sem);
1213 static int usbdux_ai_cmd(comedi_device *dev, comedi_subdevice *s)
1215 comedi_cmd *cmd = &s->async->cmd;
1218 struct usbduxsub *this_usbduxsub = dev->private;
1224 if (!this_usbduxsub)
1227 dev_dbg(&this_usbduxsub->interface->dev,
1228 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1230 /* block other CPUs from starting an ai_cmd */
1231 mutex_lock(&this_usbduxsub->sem);
1233 if (!(this_usbduxsub->probed)) {
1234 mutex_unlock(&this_usbduxsub->sem);
1237 if (this_usbduxsub->ai_cmd_running) {
1238 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1239 "ai_cmd not possible. Another ai_cmd is running.\n",
1241 mutex_unlock(&this_usbduxsub->sem);
1244 /* set current channel of the running aquisition to zero */
1245 s->async->cur_chan = 0;
1247 /* first the number of channels per time step */
1248 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1251 this_usbduxsub->dux_commands[2] = 0x12;
1253 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
1254 this_usbduxsub->dux_commands[3] = 0x03;
1256 /* CONFIG3: differential channels off */
1257 this_usbduxsub->dux_commands[4] = 0x00;
1259 for (i = 0; i < cmd->chanlist_len; i++) {
1260 chan = CR_CHAN(cmd->chanlist[i]);
1261 create_adc_command(chan, &muxsg0, &muxsg1);
1262 if (i >= NUMCHANNELS) {
1263 dev_err(&this_usbduxsub->interface->dev,
1264 "comedi%d: channel list too long\n",
1269 this_usbduxsub->dux_commands[5] = muxsg0;
1270 this_usbduxsub->dux_commands[6] = muxsg1;
1271 this_usbduxsub->dux_commands[7] = sysred;
1273 dev_dbg(&this_usbduxsub->interface->dev,
1274 "comedi %d: sending commands to the usb device: size=%u\n",
1275 dev->minor, NUMCHANNELS);
1277 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1279 mutex_unlock(&this_usbduxsub->sem);
1283 if (this_usbduxsub->high_speed) {
1285 * every 2 channels get a time window of 125us. Thus, if we
1286 * sample all 16 channels we need 1ms. If we sample only one
1287 * channel we need only 125us
1289 this_usbduxsub->ai_interval =
1290 chanToInterval(cmd->chanlist_len);
1291 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1295 /* interval always 1ms */
1296 this_usbduxsub->ai_interval = 1;
1297 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1299 if (this_usbduxsub->ai_timer < 1) {
1300 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1301 "timer=%d, scan_begin_arg=%d. "
1302 "Not properly tested by cmdtest?\n", dev->minor,
1303 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1304 mutex_unlock(&this_usbduxsub->sem);
1307 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1309 if (cmd->stop_src == TRIG_COUNT) {
1310 /* data arrives as one packet */
1311 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1312 this_usbduxsub->ai_continous = 0;
1314 /* continous aquisition */
1315 this_usbduxsub->ai_continous = 1;
1316 this_usbduxsub->ai_sample_count = 0;
1319 if (cmd->start_src == TRIG_NOW) {
1320 /* enable this acquisition operation */
1321 this_usbduxsub->ai_cmd_running = 1;
1322 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1324 this_usbduxsub->ai_cmd_running = 0;
1325 /* fixme: unlink here?? */
1326 mutex_unlock(&this_usbduxsub->sem);
1329 s->async->inttrig = NULL;
1332 /* don't enable the acquision operation */
1333 /* wait for an internal signal */
1334 s->async->inttrig = usbdux_ai_inttrig;
1336 mutex_unlock(&this_usbduxsub->sem);
1340 /* Mode 0 is used to get a single conversion on demand */
1341 static int usbdux_ai_insn_read(comedi_device *dev,
1342 comedi_subdevice *s,
1343 comedi_insn *insn, unsigned int *data)
1349 struct usbduxsub *this_usbduxsub = dev->private;
1354 if (!this_usbduxsub)
1357 dev_dbg(&this_usbduxsub->interface->dev,
1358 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1359 dev->minor, insn->n, insn->subdev);
1361 mutex_lock(&this_usbduxsub->sem);
1362 if (!(this_usbduxsub->probed)) {
1363 mutex_unlock(&this_usbduxsub->sem);
1366 if (this_usbduxsub->ai_cmd_running) {
1367 dev_err(&this_usbduxsub->interface->dev,
1368 "comedi%d: ai_insn_read not possible. "
1369 "Async Command is running.\n", dev->minor);
1370 mutex_unlock(&this_usbduxsub->sem);
1374 /* sample one channel */
1375 /* CONFIG0: chopper on */
1376 this_usbduxsub->dux_commands[1] = 0x16;
1378 /* CONFIG1: 2kHz sampling rate */
1379 this_usbduxsub->dux_commands[2] = 0x80;
1381 /* CONFIG3: differential channels off */
1382 this_usbduxsub->dux_commands[3] = 0x00;
1384 chan = CR_CHAN(insn->chanspec);
1385 create_adc_command(chan, &muxsg0, &muxsg1);
1387 this_usbduxsub->dux_commands[4] = muxsg0;
1388 this_usbduxsub->dux_commands[5] = muxsg1;
1389 this_usbduxsub->dux_commands[6] = sysred;
1392 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1394 mutex_unlock(&this_usbduxsub->sem);
1398 for (i = 0; i < insn->n; i++) {
1399 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1401 mutex_unlock(&this_usbduxsub->sem);
1404 /* 32 bits big endian from the A/D converter */
1405 one = be32_to_cpu(*((int32_t *)
1406 ((this_usbduxsub->insnBuffer)+1)));
1407 /* mask out the staus byte */
1408 one = one & 0x00ffffff;
1409 /* turn it into an unsigned integer */
1410 one = one ^ 0x00800000;
1413 mutex_unlock(&this_usbduxsub->sem);
1420 static int usbdux_getstatusinfo(comedi_device *dev, int chan)
1422 struct usbduxsub *this_usbduxsub = dev->private;
1427 if (!this_usbduxsub)
1430 if (this_usbduxsub->ai_cmd_running) {
1431 dev_err(&this_usbduxsub->interface->dev,
1432 "comedi%d: status read not possible. "
1433 "Async Command is running.\n", dev->minor);
1438 this_usbduxsub->dux_commands[1] = 0x12;
1440 /* CONFIG1: 2kHz sampling rate */
1441 this_usbduxsub->dux_commands[2] = 0x80;
1443 /* CONFIG3: differential channels off */
1444 this_usbduxsub->dux_commands[3] = 0x00;
1448 sysred = sysred | 1;
1449 } else if (chan == 2) {
1451 sysred = sysred | 4;
1452 } else if (chan == 3) {
1454 sysred = sysred | 8;
1455 } else if (chan == 4) {
1457 sysred = sysred | 16;
1458 } else if (chan == 5) {
1460 sysred = sysred | 32;
1463 this_usbduxsub->dux_commands[4] = 0;
1464 this_usbduxsub->dux_commands[5] = 0;
1465 this_usbduxsub->dux_commands[6] = sysred;
1468 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1472 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1476 /* 32 bits big endian from the A/D converter */
1477 one = be32_to_cpu(*((int32_t *)((this_usbduxsub->insnBuffer)+1)));
1478 /* mask out the staus byte */
1479 one = one & 0x00ffffff;
1480 one = one ^ 0x00800000;
1490 /************************************/
1493 static int usbdux_ao_insn_read(comedi_device *dev,
1494 comedi_subdevice *s,
1495 comedi_insn *insn, unsigned int *data)
1498 int chan = CR_CHAN(insn->chanspec);
1499 struct usbduxsub *this_usbduxsub = dev->private;
1501 if (!this_usbduxsub)
1504 mutex_lock(&this_usbduxsub->sem);
1505 if (!(this_usbduxsub->probed)) {
1506 mutex_unlock(&this_usbduxsub->sem);
1509 for (i = 0; i < insn->n; i++)
1510 data[i] = this_usbduxsub->outBuffer[chan];
1512 mutex_unlock(&this_usbduxsub->sem);
1516 static int usbdux_ao_insn_write(comedi_device *dev,
1517 comedi_subdevice *s,
1518 comedi_insn *insn, unsigned int *data)
1521 int chan = CR_CHAN(insn->chanspec);
1522 struct usbduxsub *this_usbduxsub = dev->private;
1524 if (!this_usbduxsub)
1527 dev_dbg(&this_usbduxsub->interface->dev,
1528 "comedi%d: ao_insn_write\n", dev->minor);
1530 mutex_lock(&this_usbduxsub->sem);
1531 if (!(this_usbduxsub->probed)) {
1532 mutex_unlock(&this_usbduxsub->sem);
1535 if (this_usbduxsub->ao_cmd_running) {
1536 dev_err(&this_usbduxsub->interface->dev,
1537 "comedi%d: ao_insn_write: "
1538 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1539 mutex_unlock(&this_usbduxsub->sem);
1543 for (i = 0; i < insn->n; i++) {
1544 dev_dbg(&this_usbduxsub->interface->dev,
1545 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1546 dev->minor, chan, i, data[i]);
1548 /* number of channels: 1 */
1549 this_usbduxsub->dux_commands[1] = 1;
1550 /* channel number */
1551 this_usbduxsub->dux_commands[2] = data[i];
1552 this_usbduxsub->outBuffer[chan] = data[i];
1553 this_usbduxsub->dux_commands[3] = chan;
1554 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1556 mutex_unlock(&this_usbduxsub->sem);
1560 mutex_unlock(&this_usbduxsub->sem);
1565 static int usbdux_ao_inttrig(comedi_device *dev,
1566 comedi_subdevice *s, unsigned int trignum)
1569 struct usbduxsub *this_usbduxsub = dev->private;
1571 if (!this_usbduxsub)
1574 mutex_lock(&this_usbduxsub->sem);
1575 if (!(this_usbduxsub->probed)) {
1576 mutex_unlock(&this_usbduxsub->sem);
1580 dev_err(&this_usbduxsub->interface->dev,
1581 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1583 mutex_unlock(&this_usbduxsub->sem);
1586 if (!(this_usbduxsub->ao_cmd_running)) {
1587 this_usbduxsub->ao_cmd_running = 1;
1588 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1590 dev_err(&this_usbduxsub->interface->dev,
1591 "comedi%d: usbdux_ao_inttrig: submitURB: "
1592 "err=%d\n", dev->minor, ret);
1593 this_usbduxsub->ao_cmd_running = 0;
1594 mutex_unlock(&this_usbduxsub->sem);
1597 s->async->inttrig = NULL;
1599 dev_err(&this_usbduxsub->interface->dev,
1600 "comedi%d: ao_inttrig but acqu is already running.\n",
1603 mutex_unlock(&this_usbduxsub->sem);
1607 static int usbdux_ao_cmdtest(comedi_device *dev,
1608 comedi_subdevice *s,
1612 struct usbduxsub *this_usbduxsub = dev->private;
1614 if (!this_usbduxsub)
1617 if (!(this_usbduxsub->probed))
1620 dev_dbg(&this_usbduxsub->interface->dev,
1621 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1623 /* make sure triggers are valid */
1624 /* Only immediate triggers are allowed */
1625 tmp = cmd->start_src;
1626 cmd->start_src &= TRIG_NOW | TRIG_INT;
1627 if (!cmd->start_src || tmp != cmd->start_src)
1630 /* trigger should happen timed */
1631 tmp = cmd->scan_begin_src;
1632 /* just now we scan also in the high speed mode every frame */
1633 /* this is due to ehci driver limitations */
1634 if (0) { /* (this_usbduxsub->high_speed) */
1635 /* start immidiately a new scan */
1636 /* the sampling rate is set by the coversion rate */
1637 cmd->scan_begin_src &= TRIG_FOLLOW;
1639 /* start a new scan (output at once) with a timer */
1640 cmd->scan_begin_src &= TRIG_TIMER;
1642 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1645 /* scanning is continous */
1646 tmp = cmd->convert_src;
1648 /* all conversion events happen simultaneously */
1649 cmd->convert_src &= TRIG_NOW;
1651 if (!cmd->convert_src || tmp != cmd->convert_src)
1654 /* issue a trigger when scan is finished and start a new scan */
1655 tmp = cmd->scan_end_src;
1656 cmd->scan_end_src &= TRIG_COUNT;
1657 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1660 /* trigger at the end of count events or not, stop condition or not */
1661 tmp = cmd->stop_src;
1662 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1663 if (!cmd->stop_src || tmp != cmd->stop_src)
1670 * step 2: make sure trigger sources
1671 * are unique and mutually compatible
1672 * note that mutual compatibility is not an issue here
1674 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1675 cmd->scan_begin_src != TRIG_EXT &&
1676 cmd->scan_begin_src != TRIG_TIMER)
1678 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1684 /* step 3: make sure arguments are trivially compatible */
1686 if (cmd->start_arg != 0) {
1691 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1692 /* internal trigger */
1693 if (cmd->scan_begin_arg != 0) {
1694 cmd->scan_begin_arg = 0;
1699 if (cmd->scan_begin_src == TRIG_TIMER) {
1701 if (cmd->scan_begin_arg < 1000000) {
1702 cmd->scan_begin_arg = 1000000;
1706 /* not used now, is for later use */
1707 if (cmd->convert_src == TRIG_TIMER) {
1708 if (cmd->convert_arg < 125000) {
1709 cmd->convert_arg = 125000;
1714 /* the same argument */
1715 if (cmd->scan_end_arg != cmd->chanlist_len) {
1716 cmd->scan_end_arg = cmd->chanlist_len;
1720 if (cmd->stop_src == TRIG_COUNT) {
1721 /* any count is allowed */
1724 if (cmd->stop_arg != 0) {
1730 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: err=%d, "
1731 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1732 "convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src,
1733 cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1741 static int usbdux_ao_cmd(comedi_device *dev, comedi_subdevice *s)
1743 comedi_cmd *cmd = &s->async->cmd;
1744 unsigned int chan, gain;
1746 struct usbduxsub *this_usbduxsub = dev->private;
1748 if (!this_usbduxsub)
1751 mutex_lock(&this_usbduxsub->sem);
1752 if (!(this_usbduxsub->probed)) {
1753 mutex_unlock(&this_usbduxsub->sem);
1756 dev_dbg(&this_usbduxsub->interface->dev,
1757 "comedi%d: %s\n", dev->minor, __func__);
1759 /* set current channel of the running aquisition to zero */
1760 s->async->cur_chan = 0;
1761 for (i = 0; i < cmd->chanlist_len; ++i) {
1762 chan = CR_CHAN(cmd->chanlist[i]);
1763 gain = CR_RANGE(cmd->chanlist[i]);
1764 if (i >= NUMOUTCHANNELS) {
1765 dev_err(&this_usbduxsub->interface->dev,
1766 "comedi%d: %s: channel list too long\n",
1767 dev->minor, __func__);
1770 this_usbduxsub->dac_commands[i] = chan;
1771 dev_dbg(&this_usbduxsub->interface->dev,
1772 "comedi%d: dac command for ch %d is %x\n",
1773 dev->minor, i, this_usbduxsub->dac_commands[i]);
1776 /* we count in steps of 1ms (125us) */
1777 /* 125us mode not used yet */
1778 if (0) { /* (this_usbduxsub->high_speed) */
1780 /* timing of the conversion itself: every 125 us */
1781 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1784 /* timing of the scan: we get all channels at once */
1785 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1786 dev_dbg(&this_usbduxsub->interface->dev,
1787 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1788 "convert_src=%d, convert_arg=%d\n", dev->minor,
1789 cmd->scan_begin_src, cmd->scan_begin_arg,
1790 cmd->convert_src, cmd->convert_arg);
1791 dev_dbg(&this_usbduxsub->interface->dev,
1792 "comedi%d: ao_timer=%d (ms)\n",
1793 dev->minor, this_usbduxsub->ao_timer);
1794 if (this_usbduxsub->ao_timer < 1) {
1795 dev_err(&this_usbduxsub->interface->dev,
1796 "comedi%d: usbdux: ao_timer=%d, "
1797 "scan_begin_arg=%d. "
1798 "Not properly tested by cmdtest?\n",
1799 dev->minor, this_usbduxsub->ao_timer,
1800 cmd->scan_begin_arg);
1801 mutex_unlock(&this_usbduxsub->sem);
1805 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1807 if (cmd->stop_src == TRIG_COUNT) {
1810 /* high speed also scans everything at once */
1811 if (0) { /* (this_usbduxsub->high_speed) */
1812 this_usbduxsub->ao_sample_count =
1813 (cmd->stop_arg) * (cmd->scan_end_arg);
1815 /* there's no scan as the scan has been */
1816 /* perf inside the FX2 */
1817 /* data arrives as one packet */
1818 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1820 this_usbduxsub->ao_continous = 0;
1822 /* continous aquisition */
1823 this_usbduxsub->ao_continous = 1;
1824 this_usbduxsub->ao_sample_count = 0;
1827 if (cmd->start_src == TRIG_NOW) {
1828 /* enable this acquisition operation */
1829 this_usbduxsub->ao_cmd_running = 1;
1830 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1832 this_usbduxsub->ao_cmd_running = 0;
1833 /* fixme: unlink here?? */
1834 mutex_unlock(&this_usbduxsub->sem);
1837 s->async->inttrig = NULL;
1840 /* submit the urbs later */
1841 /* wait for an internal signal */
1842 s->async->inttrig = usbdux_ao_inttrig;
1845 mutex_unlock(&this_usbduxsub->sem);
1849 static int usbdux_dio_insn_config(comedi_device *dev,
1850 comedi_subdevice *s,
1851 comedi_insn *insn, unsigned int *data)
1853 int chan = CR_CHAN(insn->chanspec);
1855 /* The input or output configuration of each digital line is
1856 * configured by a special insn_config instruction. chanspec
1857 * contains the channel to be changed, and data[0] contains the
1858 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1861 case INSN_CONFIG_DIO_OUTPUT:
1862 s->io_bits |= 1 << chan; /* 1 means Out */
1864 case INSN_CONFIG_DIO_INPUT:
1865 s->io_bits &= ~(1 << chan);
1867 case INSN_CONFIG_DIO_QUERY:
1869 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1875 /* we don't tell the firmware here as it would take 8 frames */
1876 /* to submit the information. We do it in the insn_bits. */
1880 static int usbdux_dio_insn_bits(comedi_device *dev,
1881 comedi_subdevice *s,
1886 struct usbduxsub *this_usbduxsub = dev->private;
1889 if (!this_usbduxsub)
1895 mutex_lock(&this_usbduxsub->sem);
1897 if (!(this_usbduxsub->probed)) {
1898 mutex_unlock(&this_usbduxsub->sem);
1902 /* The insn data is a mask in data[0] and the new data
1903 * in data[1], each channel cooresponding to a bit. */
1904 s->state &= ~data[0];
1905 s->state |= data[0] & data[1];
1906 /* The commands are 8 bits wide */
1907 this_usbduxsub->dux_commands[1] = (s->io_bits) & 0x000000FF;
1908 this_usbduxsub->dux_commands[4] = (s->state) & 0x000000FF;
1909 this_usbduxsub->dux_commands[2] = ((s->io_bits) & 0x0000FF00) >> 8;
1910 this_usbduxsub->dux_commands[5] = ((s->state) & 0x0000FF00) >> 8;
1911 this_usbduxsub->dux_commands[3] = ((s->io_bits) & 0x00FF0000) >> 16;
1912 this_usbduxsub->dux_commands[6] = ((s->state) & 0x00FF0000) >> 16;
1914 /* This command also tells the firmware to return */
1915 /* the digital input lines */
1916 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1918 mutex_unlock(&this_usbduxsub->sem);
1921 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1923 mutex_unlock(&this_usbduxsub->sem);
1927 data[1] = (((unsigned int)(this_usbduxsub->insnBuffer[1]))&0xff) |
1928 ((((unsigned int)(this_usbduxsub->insnBuffer[2]))&0xff) << 8) |
1929 ((((unsigned int)(this_usbduxsub->insnBuffer[3]))&0xff) << 16);
1933 mutex_unlock(&this_usbduxsub->sem);
1937 /***********************************/
1940 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1944 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1945 if (usbduxsub_tmp->urbPwm)
1946 usb_kill_urb(usbduxsub_tmp->urbPwm);
1947 dev_dbg(&usbduxsub_tmp->interface->dev,
1948 "comedi: unlinked PwmURB: res=%d\n", err);
1953 /* This cancels a running acquisition operation
1956 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1960 if (!this_usbduxsub)
1963 dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1965 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1967 this_usbduxsub->pwm_cmd_running = 0;
1972 /* force unlink - is called by comedi */
1973 static int usbdux_pwm_cancel(comedi_device *dev,
1974 comedi_subdevice *s)
1976 struct usbduxsub *this_usbduxsub = dev->private;
1979 /* unlink only if it is really running */
1980 res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1982 dev_dbg(&this_usbduxsub->interface->dev,
1983 "comedi %d: sending pwm off command to the usb device.\n",
1985 res = send_dux_commands(this_usbduxsub, SENDPWMOFF);
1992 static void usbduxsub_pwm_irq(struct urb *urb)
1995 struct usbduxsub *this_usbduxsub;
1996 comedi_device *this_comedidev;
1997 comedi_subdevice *s;
1999 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
2001 /* the context variable points to the subdevice */
2002 this_comedidev = urb->context;
2003 /* the private structure of the subdevice is struct usbduxsub */
2004 this_usbduxsub = this_comedidev->private;
2006 s = this_comedidev->subdevices + SUBDEV_DA;
2008 switch (urb->status) {
2018 * after an unlink command, unplug, ... etc
2019 * no unlink needed here. Already shutting down.
2021 if (this_usbduxsub->pwm_cmd_running)
2022 usbdux_pwm_stop(this_usbduxsub, 0);
2028 if (this_usbduxsub->pwm_cmd_running) {
2029 dev_err(&this_usbduxsub->interface->dev,
2030 "comedi_: Non-zero urb status received in "
2031 "pwm intr context: %d\n", urb->status);
2032 usbdux_pwm_stop(this_usbduxsub, 0);
2037 /* are we actually running? */
2038 if (!(this_usbduxsub->pwm_cmd_running))
2041 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
2042 urb->dev = this_usbduxsub->usbdev;
2044 if (this_usbduxsub->pwm_cmd_running) {
2045 ret = usb_submit_urb(urb, GFP_ATOMIC);
2047 dev_err(&this_usbduxsub->interface->dev,
2048 "comedi_: pwm urb resubm failed in int-cont. "
2050 if (ret == EL2NSYNC)
2051 dev_err(&this_usbduxsub->interface->dev,
2052 "buggy USB host controller or bug in "
2055 /* don't do an unlink here */
2056 usbdux_pwm_stop(this_usbduxsub, 0);
2061 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
2068 dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
2070 /* in case of a resubmission after an unlink... */
2071 usb_fill_bulk_urb(usbduxsub->urbPwm,
2073 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
2074 usbduxsub->urbPwm->transfer_buffer,
2075 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq,
2076 usbduxsub->comedidev);
2078 errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
2080 dev_err(&usbduxsub->interface->dev,
2081 "comedi_: usbduxsigma: pwm: usb_submit_urb error %d\n",
2088 static int usbdux_pwm_period(comedi_device *dev,
2089 comedi_subdevice *s, unsigned int period)
2091 struct usbduxsub *this_usbduxsub = dev->private;
2094 if (period < MIN_PWM_PERIOD) {
2095 dev_err(&this_usbduxsub->interface->dev,
2096 "comedi%d: illegal period setting for pwm.\n",
2100 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
2101 if (fx2delay > 255) {
2102 dev_err(&this_usbduxsub->interface->dev,
2103 "comedi%d: period %d for pwm is too low.\n",
2104 dev->minor, period);
2108 this_usbduxsub->pwmDelay = fx2delay;
2109 this_usbduxsub->pwmPeriod = period;
2110 dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
2111 __func__, period, fx2delay);
2115 /* is called from insn so there's no need to do all the sanity checks */
2116 static int usbdux_pwm_start(comedi_device *dev,
2117 comedi_subdevice *s)
2120 struct usbduxsub *this_usbduxsub = dev->private;
2122 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
2123 dev->minor, __func__);
2125 if (this_usbduxsub->pwm_cmd_running) {
2126 /* already running */
2130 this_usbduxsub->dux_commands[1] = ((uint8_t) this_usbduxsub->pwmDelay);
2131 ret = send_dux_commands(this_usbduxsub, SENDPWMON);
2135 /* initialise the buffer */
2136 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
2137 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
2139 this_usbduxsub->pwm_cmd_running = 1;
2140 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
2142 this_usbduxsub->pwm_cmd_running = 0;
2148 /* generates the bit pattern for PWM with the optional sign bit */
2149 static int usbdux_pwm_pattern(comedi_device *dev,
2150 comedi_subdevice *s, int channel,
2151 unsigned int value, unsigned int sign)
2153 struct usbduxsub *this_usbduxsub = dev->private;
2160 if (!this_usbduxsub)
2163 /* this is the DIO bit which carries the PWM data */
2164 pwm_mask = (1 << channel);
2165 /* this is the DIO bit which carries the optional direction bit */
2166 sgn_mask = (16 << channel);
2167 /* this is the buffer which will be filled with the with bit */
2168 /* pattern for one period */
2169 szbuf = this_usbduxsub->sizePwmBuf;
2170 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2171 for (i = 0; i < szbuf; i++) {
2174 c = c & (~pwm_mask);
2175 /* set the bit as long as the index is lower than the value */
2178 /* set the optional sign bit for a relay */
2180 /* positive value */
2181 c = c & (~sgn_mask);
2183 /* negative value */
2191 static int usbdux_pwm_write(comedi_device *dev,
2192 comedi_subdevice *s,
2193 comedi_insn *insn, unsigned int *data)
2195 struct usbduxsub *this_usbduxsub = dev->private;
2197 if (!this_usbduxsub)
2200 if ((insn->n) != 1) {
2202 * doesn't make sense to have more than one value here because
2203 * it would just overwrite the PWM buffer a couple of times
2209 * the sign is set via a special INSN only, this gives us 8 bits for
2211 * relay sign 0 by default
2213 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
2216 static int usbdux_pwm_read(comedi_device *x1,
2217 comedi_subdevice *x2, comedi_insn *x3,
2224 /* switches on/off PWM */
2225 static int usbdux_pwm_config(comedi_device *dev,
2226 comedi_subdevice *s,
2227 comedi_insn *insn, unsigned int *data)
2229 struct usbduxsub *this_usbduxsub = dev->private;
2231 case INSN_CONFIG_ARM:
2233 dev_dbg(&this_usbduxsub->interface->dev,
2234 "comedi%d: %s: pwm on\n", dev->minor, __func__);
2236 * if not zero the PWM is limited to a certain time which is
2237 * not supported here
2241 return usbdux_pwm_start(dev, s);
2242 case INSN_CONFIG_DISARM:
2243 dev_dbg(&this_usbduxsub->interface->dev,
2244 "comedi%d: %s: pwm off\n", dev->minor, __func__);
2245 return usbdux_pwm_cancel(dev, s);
2246 case INSN_CONFIG_GET_PWM_STATUS:
2248 * to check if the USB transmission has failed or in case PWM
2249 * was limited to n cycles to check if it has terminated
2251 data[1] = this_usbduxsub->pwm_cmd_running;
2253 case INSN_CONFIG_PWM_SET_PERIOD:
2254 dev_dbg(&this_usbduxsub->interface->dev,
2255 "comedi%d: %s: setting period\n", dev->minor,
2257 return usbdux_pwm_period(dev, s, data[1]);
2258 case INSN_CONFIG_PWM_GET_PERIOD:
2259 data[1] = this_usbduxsub->pwmPeriod;
2261 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2262 /* value in the first byte and the sign in the second for a
2264 return usbdux_pwm_pattern(dev, s,
2265 /* the channel number */
2266 CR_CHAN(insn->chanspec),
2267 /* actual PWM data */
2271 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2272 /* values are not kept in this driver, nothing to return */
2279 /*****************************************************************/
2281 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2287 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2289 /* shows the usb subsystem that the driver is down */
2290 if (usbduxsub_tmp->interface)
2291 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2293 usbduxsub_tmp->probed = 0;
2295 if (usbduxsub_tmp->urbIn) {
2296 if (usbduxsub_tmp->ai_cmd_running) {
2297 usbduxsub_tmp->ai_cmd_running = 0;
2298 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2300 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2301 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2302 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2303 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2304 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2305 usbduxsub_tmp->urbIn[i] = NULL;
2307 kfree(usbduxsub_tmp->urbIn);
2308 usbduxsub_tmp->urbIn = NULL;
2310 if (usbduxsub_tmp->urbOut) {
2311 if (usbduxsub_tmp->ao_cmd_running) {
2312 usbduxsub_tmp->ao_cmd_running = 0;
2313 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2315 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2316 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2317 kfree(usbduxsub_tmp->
2318 urbOut[i]->transfer_buffer);
2319 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2322 if (usbduxsub_tmp->urbOut[i]) {
2323 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2324 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2325 usbduxsub_tmp->urbOut[i] = NULL;
2328 kfree(usbduxsub_tmp->urbOut);
2329 usbduxsub_tmp->urbOut = NULL;
2331 if (usbduxsub_tmp->urbPwm) {
2332 if (usbduxsub_tmp->pwm_cmd_running) {
2333 usbduxsub_tmp->pwm_cmd_running = 0;
2334 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2336 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2337 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2338 usb_kill_urb(usbduxsub_tmp->urbPwm);
2339 usb_free_urb(usbduxsub_tmp->urbPwm);
2340 usbduxsub_tmp->urbPwm = NULL;
2342 kfree(usbduxsub_tmp->inBuffer);
2343 usbduxsub_tmp->inBuffer = NULL;
2344 kfree(usbduxsub_tmp->insnBuffer);
2345 usbduxsub_tmp->insnBuffer = NULL;
2346 kfree(usbduxsub_tmp->outBuffer);
2347 usbduxsub_tmp->outBuffer = NULL;
2348 kfree(usbduxsub_tmp->dac_commands);
2349 usbduxsub_tmp->dac_commands = NULL;
2350 kfree(usbduxsub_tmp->dux_commands);
2351 usbduxsub_tmp->dux_commands = NULL;
2352 usbduxsub_tmp->ai_cmd_running = 0;
2353 usbduxsub_tmp->ao_cmd_running = 0;
2354 usbduxsub_tmp->pwm_cmd_running = 0;
2357 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2360 struct usbduxsub *usbduxsub_tmp = context;
2361 struct usb_device *usbdev = usbduxsub_tmp->usbdev;
2365 dev_err(&usbdev->dev,
2366 "Firmware complete handler without firmware!\n");
2371 * we need to upload the firmware here because fw will be
2372 * freed once we've left this function
2374 ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2377 dev_err(&usbdev->dev,
2378 "Could not upload firmware (err=%d)\n", ret);
2381 comedi_usb_auto_config(usbdev, BOARDNAME);
2384 * in more recent versions the completion handler
2385 * had to release the firmware whereas in older
2386 * versions this has been done by the caller
2388 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,32)
2389 release_firmware(fw);
2395 /* allocate memory for the urbs and initialise them */
2396 static int usbduxsigma_probe(struct usb_interface *uinterf,
2397 const struct usb_device_id *id)
2399 struct usb_device *udev = interface_to_usbdev(uinterf);
2400 struct device *dev = &uinterf->dev;
2405 dev_dbg(dev, "comedi_: usbdux_: "
2406 "finding a free structure for the usb-device\n");
2408 mutex_lock(&start_stop_sem);
2409 /* look for a free place in the usbdux array */
2411 for (i = 0; i < NUMUSBDUX; i++) {
2412 if (!(usbduxsub[i].probed)) {
2420 dev_err(dev, "Too many usbduxsigma-devices connected.\n");
2421 mutex_unlock(&start_stop_sem);
2424 dev_dbg(dev, "comedi_: usbdux: "
2425 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2427 mutex_init(&(usbduxsub[index].sem));
2429 /* save a pointer to the usb device */
2430 usbduxsub[index].usbdev = udev;
2432 /* save the interface itself */
2433 usbduxsub[index].interface = uinterf;
2434 /* get the interface number from the interface */
2435 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2436 /* hand the private data over to the usb subsystem */
2437 /* will be needed for disconnect */
2438 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2440 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2442 /* test if it is high speed (USB 2.0) */
2443 usbduxsub[index].high_speed =
2444 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2446 /* create space for the commands of the DA converter */
2447 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2448 if (!usbduxsub[index].dac_commands) {
2449 dev_err(dev, "comedi_: usbduxsigma: "
2450 "error alloc space for dac commands\n");
2451 tidy_up(&(usbduxsub[index]));
2452 mutex_unlock(&start_stop_sem);
2455 /* create space for the commands going to the usb device */
2456 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2457 if (!usbduxsub[index].dux_commands) {
2458 dev_err(dev, "comedi_: usbduxsigma: "
2459 "error alloc space for dux commands\n");
2460 tidy_up(&(usbduxsub[index]));
2461 mutex_unlock(&start_stop_sem);
2464 /* create space for the in buffer and set it to zero */
2465 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2466 if (!(usbduxsub[index].inBuffer)) {
2467 dev_err(dev, "comedi_: usbduxsigma: "
2468 "could not alloc space for inBuffer\n");
2469 tidy_up(&(usbduxsub[index]));
2470 mutex_unlock(&start_stop_sem);
2473 /* create space of the instruction buffer */
2474 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2475 if (!(usbduxsub[index].insnBuffer)) {
2476 dev_err(dev, "comedi_: usbduxsigma: "
2477 "could not alloc space for insnBuffer\n");
2478 tidy_up(&(usbduxsub[index]));
2479 mutex_unlock(&start_stop_sem);
2482 /* create space for the outbuffer */
2483 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2484 if (!(usbduxsub[index].outBuffer)) {
2485 dev_err(dev, "comedi_: usbduxsigma: "
2486 "could not alloc space for outBuffer\n");
2487 tidy_up(&(usbduxsub[index]));
2488 mutex_unlock(&start_stop_sem);
2491 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2492 i = usb_set_interface(usbduxsub[index].usbdev,
2493 usbduxsub[index].ifnum, 3);
2495 dev_err(dev, "comedi_: usbduxsigma%d: "
2496 "could not set alternate setting 3 in high speed.\n",
2498 tidy_up(&(usbduxsub[index]));
2499 mutex_unlock(&start_stop_sem);
2502 if (usbduxsub[index].high_speed)
2503 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2505 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2507 usbduxsub[index].urbIn =
2508 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2510 if (!(usbduxsub[index].urbIn)) {
2511 dev_err(dev, "comedi_: usbduxsigma: "
2512 "Could not alloc. urbIn array\n");
2513 tidy_up(&(usbduxsub[index]));
2514 mutex_unlock(&start_stop_sem);
2517 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2518 /* one frame: 1ms */
2519 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2520 if (usbduxsub[index].urbIn[i] == NULL) {
2521 dev_err(dev, "comedi_: usbduxsigma%d: "
2522 "Could not alloc. urb(%d)\n", index, i);
2523 tidy_up(&(usbduxsub[index]));
2524 mutex_unlock(&start_stop_sem);
2527 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2528 /* will be filled later with a pointer to the comedi-device */
2529 /* and ONLY then the urb should be submitted */
2530 usbduxsub[index].urbIn[i]->context = NULL;
2531 usbduxsub[index].urbIn[i]->pipe =
2532 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2533 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2534 usbduxsub[index].urbIn[i]->transfer_buffer =
2535 kzalloc(SIZEINBUF, GFP_KERNEL);
2536 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2537 dev_err(dev, "comedi_: usbduxsigma%d: "
2538 "could not alloc. transb.\n", index);
2539 tidy_up(&(usbduxsub[index]));
2540 mutex_unlock(&start_stop_sem);
2543 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2544 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2545 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2546 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2547 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length =
2552 if (usbduxsub[index].high_speed)
2553 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2555 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2557 usbduxsub[index].urbOut =
2558 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2560 if (!(usbduxsub[index].urbOut)) {
2561 dev_err(dev, "comedi_: usbduxsigma: "
2562 "Could not alloc. urbOut array\n");
2563 tidy_up(&(usbduxsub[index]));
2564 mutex_unlock(&start_stop_sem);
2567 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2568 /* one frame: 1ms */
2569 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2570 if (usbduxsub[index].urbOut[i] == NULL) {
2571 dev_err(dev, "comedi_: usbduxsigma%d: "
2572 "Could not alloc. urb(%d)\n", index, i);
2573 tidy_up(&(usbduxsub[index]));
2574 mutex_unlock(&start_stop_sem);
2577 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2578 /* will be filled later with a pointer to the comedi-device */
2579 /* and ONLY then the urb should be submitted */
2580 usbduxsub[index].urbOut[i]->context = NULL;
2581 usbduxsub[index].urbOut[i]->pipe =
2582 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2583 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2584 usbduxsub[index].urbOut[i]->transfer_buffer =
2585 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2586 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2587 dev_err(dev, "comedi_: usbduxsigma%d: "
2588 "could not alloc. transb.\n", index);
2589 tidy_up(&(usbduxsub[index]));
2590 mutex_unlock(&start_stop_sem);
2593 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2594 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2595 usbduxsub[index].urbOut[i]->transfer_buffer_length =
2597 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2598 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2600 if (usbduxsub[index].high_speed) {
2602 usbduxsub[index].urbOut[i]->interval = 8;
2605 usbduxsub[index].urbOut[i]->interval = 1;
2610 if (usbduxsub[index].high_speed) {
2611 /* max bulk ep size in high speed */
2612 usbduxsub[index].sizePwmBuf = 512;
2613 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2614 if (usbduxsub[index].urbPwm == NULL) {
2615 dev_err(dev, "comedi_: usbduxsigma%d: "
2616 "Could not alloc. pwm urb\n", index);
2617 tidy_up(&(usbduxsub[index]));
2618 mutex_unlock(&start_stop_sem);
2621 usbduxsub[index].urbPwm->transfer_buffer =
2622 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2623 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2624 dev_err(dev, "comedi_: usbduxsigma%d: "
2625 "could not alloc. transb. for pwm\n", index);
2626 tidy_up(&(usbduxsub[index]));
2627 mutex_unlock(&start_stop_sem);
2631 usbduxsub[index].urbPwm = NULL;
2632 usbduxsub[index].sizePwmBuf = 0;
2635 usbduxsub[index].ai_cmd_running = 0;
2636 usbduxsub[index].ao_cmd_running = 0;
2637 usbduxsub[index].pwm_cmd_running = 0;
2639 /* we've reached the bottom of the function */
2640 usbduxsub[index].probed = 1;
2641 mutex_unlock(&start_stop_sem);
2643 ret = request_firmware_nowait(THIS_MODULE,
2645 "usbduxsigma_firmware.bin",
2649 usbdux_firmware_request_complete_handler
2653 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2657 dev_info(dev, "comedi_: successfully initialised.\n");
2662 static void usbduxsigma_disconnect(struct usb_interface *intf)
2664 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2665 struct usb_device *udev = interface_to_usbdev(intf);
2667 if (!usbduxsub_tmp) {
2669 "comedi_: disconnect called with null pointer.\n");
2672 if (usbduxsub_tmp->usbdev != udev) {
2673 dev_err(&intf->dev, "comedi_: BUG! wrong ptr!\n");
2676 if (usbduxsub_tmp->ai_cmd_running)
2677 /* we are still running a command */
2678 usbdux_ai_stop(usbduxsub_tmp, 1);
2679 if (usbduxsub_tmp->ao_cmd_running)
2680 /* we are still running a command */
2681 usbdux_ao_stop(usbduxsub_tmp, 1);
2682 comedi_usb_auto_unconfig(udev);
2683 mutex_lock(&start_stop_sem);
2684 mutex_lock(&usbduxsub_tmp->sem);
2685 tidy_up(usbduxsub_tmp);
2686 mutex_unlock(&usbduxsub_tmp->sem);
2687 mutex_unlock(&start_stop_sem);
2688 dev_info(&intf->dev, "comedi_: disconnected from the usb\n");
2691 /* is called when comedi-config is called */
2692 static int usbduxsigma_attach(comedi_device *dev,
2693 comedi_devconfig *it)
2698 struct usbduxsub *udev;
2702 comedi_subdevice *s = NULL;
2703 dev->private = NULL;
2705 mutex_lock(&start_stop_sem);
2706 /* find a valid device which has been detected by the probe function of
2709 for (i = 0; i < NUMUSBDUX; i++) {
2710 if ((usbduxsub[i].probed) && (!usbduxsub[i].attached)) {
2717 printk(KERN_ERR "comedi%d: usbduxsigma: error: attach failed,"
2718 "dev not connected to the usb bus.\n", dev->minor);
2719 mutex_unlock(&start_stop_sem);
2723 udev = &usbduxsub[index];
2724 mutex_lock(&udev->sem);
2725 /* pointer back to the corresponding comedi device */
2726 udev->comedidev = dev;
2728 /* trying to upload the firmware into the FX2 */
2729 if (comedi_aux_data(it->options, 0) &&
2730 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
2731 firmwareUpload(udev, comedi_aux_data(it->options, 0),
2732 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
2735 dev->board_name = BOARDNAME;
2737 /* set number of subdevices */
2738 if (udev->high_speed) {
2740 dev->n_subdevices = 4;
2743 dev->n_subdevices = 3;
2746 /* allocate space for the subdevices */
2747 ret = alloc_subdevices(dev, dev->n_subdevices);
2749 dev_err(&udev->interface->dev,
2750 "comedi%d: no space for subdev\n", dev->minor);
2751 mutex_unlock(&start_stop_sem);
2755 /* private structure is also simply the usb-structure */
2756 dev->private = udev;
2758 /* the first subdevice is the A/D converter */
2759 s = dev->subdevices + SUBDEV_AD;
2760 /* the URBs get the comedi subdevice */
2761 /* which is responsible for reading */
2762 /* this is the subdevice which reads data */
2763 dev->read_subdev = s;
2764 /* the subdevice receives as private structure the */
2768 s->type = COMEDI_SUBD_AI;
2769 /* readable and ref is to ground, 32 bit wide data! */
2770 s->subdev_flags = SDF_READABLE | SDF_GROUND |
2771 SDF_CMD_READ | SDF_LSAMPL;
2772 /* 16 A/D channels */
2773 s->n_chan = NUMCHANNELS;
2774 /* length of the channellist */
2775 s->len_chanlist = NUMCHANNELS;
2776 /* callback functions */
2777 s->insn_read = usbdux_ai_insn_read;
2778 s->do_cmdtest = usbdux_ai_cmdtest;
2779 s->do_cmd = usbdux_ai_cmd;
2780 s->cancel = usbdux_ai_cancel;
2781 /* max value from the A/D converter (24bit) */
2782 s->maxdata = 0x00FFFFFF;
2783 /* range table to convert to physical units */
2784 s->range_table = (&range_usbdux_ai_range);
2787 s = dev->subdevices + SUBDEV_DA;
2789 s->type = COMEDI_SUBD_AO;
2790 /* backward pointer */
2791 dev->write_subdev = s;
2792 /* the subdevice receives as private structure the */
2796 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2799 /* length of the channellist */
2800 s->len_chanlist = 4;
2801 /* 8 bit resolution */
2802 s->maxdata = 0x00ff;
2803 /* unipolar range */
2804 s->range_table = (&range_usbdux_ao_range);
2806 s->do_cmdtest = usbdux_ao_cmdtest;
2807 s->do_cmd = usbdux_ao_cmd;
2808 s->cancel = usbdux_ao_cancel;
2809 s->insn_read = usbdux_ao_insn_read;
2810 s->insn_write = usbdux_ao_insn_write;
2813 s = dev->subdevices + SUBDEV_DIO;
2814 s->type = COMEDI_SUBD_DIO;
2815 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2816 /* 8 external and 16 internal channels */
2819 s->range_table = (&range_digital);
2820 s->insn_bits = usbdux_dio_insn_bits;
2821 s->insn_config = usbdux_dio_insn_config;
2822 /* we don't use it */
2825 if (udev->high_speed) {
2827 s = dev->subdevices + SUBDEV_PWM;
2828 s->type = COMEDI_SUBD_PWM;
2829 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2831 /* this defines the max duty cycle resolution */
2832 s->maxdata = udev->sizePwmBuf;
2833 s->insn_write = usbdux_pwm_write;
2834 s->insn_read = usbdux_pwm_read;
2835 s->insn_config = usbdux_pwm_config;
2836 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2838 /* finally decide that it's attached */
2841 mutex_unlock(&udev->sem);
2843 mutex_unlock(&start_stop_sem);
2845 offset = usbdux_getstatusinfo(dev, 0);
2847 dev_err(&udev->interface->dev,
2848 "Communication to USBDUXSIGMA failed!"
2849 "Check firmware and cabling.");
2851 dev_info(&udev->interface->dev,
2852 "comedi%d: attached, ADC_zero = %x", dev->minor, offset);
2857 static int usbduxsigma_detach(comedi_device *dev)
2859 struct usbduxsub *usbduxsub_tmp;
2863 "comedi? usbduxsigma detach: dev=NULL\n");
2867 usbduxsub_tmp = dev->private;
2868 if (!usbduxsub_tmp) {
2870 "comedi?: usbduxsigma detach: private=NULL\n");
2874 dev_dbg(&usbduxsub_tmp->interface->dev,
2875 "comedi%d: detach usb device\n",
2878 mutex_lock(&usbduxsub_tmp->sem);
2879 /* Don't allow detach to free the private structure */
2880 /* It's one entry of of usbduxsub[] */
2881 dev->private = NULL;
2882 usbduxsub_tmp->attached = 0;
2883 usbduxsub_tmp->comedidev = NULL;
2884 dev_info(&usbduxsub_tmp->interface->dev,
2885 "comedi%d: successfully detached.\n", dev->minor);
2886 mutex_unlock(&usbduxsub_tmp->sem);
2890 /* main driver struct */
2891 static comedi_driver driver_usbduxsigma = {
2892 .driver_name = "usbduxsigma",
2893 .module = THIS_MODULE,
2894 .attach = usbduxsigma_attach,
2895 .detach = usbduxsigma_detach,
2898 /* Table with the USB-devices */
2899 static const struct usb_device_id usbduxsigma_table[] = {
2900 {USB_DEVICE(0x13d8, 0x0020)},
2901 {USB_DEVICE(0x13d8, 0x0021)},
2902 {USB_DEVICE(0x13d8, 0x0022)},
2903 {} /* Terminating entry */
2906 MODULE_DEVICE_TABLE(usb, usbduxsigma_table);
2908 /* The usbduxsub-driver */
2909 static struct usb_driver usbduxsigma_driver = {
2911 .probe = usbduxsigma_probe,
2912 .disconnect = usbduxsigma_disconnect,
2913 .id_table = usbduxsigma_table,
2916 /* Can't use the nice macro as I have also to initialise the USB */
2918 /* registering the usb-system _and_ the comedi-driver */
2919 static int __init init_usbduxsigma(void)
2921 printk(KERN_INFO KBUILD_MODNAME ": "
2922 DRIVER_VERSION ":" DRIVER_DESC "\n");
2923 usb_register(&usbduxsigma_driver);
2924 comedi_driver_register(&driver_usbduxsigma);
2928 /* deregistering the comedi driver and the usb-subsystem */
2929 static void __exit exit_usbduxsigma(void)
2931 comedi_driver_unregister(&driver_usbduxsigma);
2932 usb_deregister(&usbduxsigma_driver);
2935 module_init(init_usbduxsigma);
2936 module_exit(exit_usbduxsigma);
2938 MODULE_AUTHOR(DRIVER_AUTHOR);
2939 MODULE_DESCRIPTION(DRIVER_DESC);
2940 MODULE_LICENSE("GPL");