1 #define DRIVER_VERSION "v1.0"
2 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
3 #define DRIVER_DESC "USB-DUXfast, BerndPorr@f2s.com"
5 comedi/drivers/usbduxfast.c
6 Copyright (C) 2004 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-FAST of INCITE Technology Limited
26 Devices: [ITL] USB-DUX-FAST (usbduxfast)
27 Author: Bernd Porr <tech@linux-usb-daq.co.uk>
31 The device has one subdevice for analogue input.
33 number of channels: 16
37 range = 0 : [-0.75 V,0.75 V]
38 range = 1 : [-0.5 V,0.5 V]
40 The channel-list allows 1,2,3 and 16 channels.
41 start: now|ext|int (external trigger via pin at HD-D connector)
42 scan_begin: follow|timer|ext
43 convert: timer|ext (contains the sampling interval. Min interval
44 for single channel acquisition is 33us
45 and for multiplexed acquisition 300us)
49 Configuration options:
50 The device requires firmware which is usually
51 uploaded automatically by udev/hotplug at the moment
52 the driver module is being loaded.
53 In case udev/hotplug is not enabled you need to upload
54 the firmware with comedi_config -i usbduxfast_firmware.bin
55 The firmware is usually installed under /lib/firmware
56 or can be downloaded form http://www.linux-usb-daq.co.uk.
60 * I must give credit here to Chris Baugher who
61 * wrote the driver for AT-MIO-16d. I used some parts of this
62 * driver. I also must give credits to David Brownell
63 * who supported me with the USB development.
69 * 0.9: Dropping the first data packet which seems to be from the last transfer.
70 * Buffer overflows in the FX2 are handed over to comedi.
71 * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
72 * Added insn command basically for testing. Sample rate is 1MHz/16ch=62.5kHz
73 * 0.99: Ian Abbott pointed out a bug which has been corrected. Thanks!
74 * 0.99a: added external trigger.
75 * 1.00: added firmware kernel request to the driver which fixed
76 * udev coldplug problem
79 #include <linux/kernel.h>
80 #include <linux/firmware.h>
81 #include <linux/module.h>
82 #include <linux/init.h>
83 #include <linux/slab.h>
84 #include <linux/input.h>
85 #include <linux/usb.h>
86 #include <linux/fcntl.h>
87 #include <linux/compiler.h>
88 #include "comedi_fc.h"
89 #include <linux/comedidev.h>
90 #include <linux/usb.h>
92 // (un)comment this if you want to have debug info.
93 //#define CONFIG_COMEDI_DEBUG
94 #undef CONFIG_COMEDI_DEBUG
96 #define BOARDNAME "usbduxfast"
98 // timeout for the USB-transfer
99 #define BULK_TIMEOUT 1000
101 // constants for "firmware" upload and download
102 #define USBDUXFASTSUB_FIRMWARE 0xA0
103 #define VENDOR_DIR_IN 0xC0
104 #define VENDOR_DIR_OUT 0x40
106 // internal adresses of the 8051 processor
107 #define USBDUXFASTSUB_CPUCS 0xE600
109 // max lenghth of the transfer-buffer for software upload
110 #define TB_LEN 0x2000
112 // Input endpoint number
115 // Endpoint for the A/D channellist: bulk OUT
116 #define CHANNELLISTEP 4
118 // Number of channels
119 #define NUMCHANNELS 32
121 // size of the waveform descriptor
122 #define WAVESIZE 0x20
124 // Size of one A/D value
125 #define SIZEADIN ((sizeof(int16_t)))
127 // Size of the input-buffer IN BYTES
128 #define SIZEINBUF 512
131 #define SIZEINSNBUF 512
133 // Size of the buffer for the dux commands
134 #define SIZEOFDUXBUFFER 256 // bytes
136 // Number of in-URBs which receive the data: min=5
137 #define NUMOFINBUFFERSHIGH 10
139 // Total number of usbduxfast devices
140 #define NUMUSBDUXFAST 16
142 // Number of subdevices
143 #define N_SUBDEVICES 1
145 // Analogue in subdevice
148 // min delay steps for more than one channel
149 // basically when the mux gives up. ;-)
150 #define MIN_SAMPLING_PERIOD 9 // steps at 30MHz in the FX2
152 // Max number of 1/30MHz delay steps:
153 #define MAX_SAMPLING_PERIOD 500
155 // Number of received packets to ignore before we start handing data over to comedi.
156 // It's quad buffering and we have to ignore 4 packets.
157 #define PACKETS_TO_IGNORE 4
159 /////////////////////////////////////////////
161 static const comedi_lrange range_usbduxfast_ai_range = { 2, {
168 * private structure of one subdevice
171 // This is the structure which holds all the data of this driver
172 // one sub device just now: A/D
176 // is it associated with a subdevice?
178 // pointer to the usb-device
179 struct usb_device *usbdev;
180 // BULK-transfer handling: urb
182 int8_t *transfer_buffer;
183 // input buffer for single insn
187 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
188 // interface structure in 2.6
189 struct usb_interface *interface;
191 // comedi device for the interrupt context
192 comedi_device *comedidev;
193 // asynchronous command is running
194 short int ai_cmd_running;
195 // continous aquisition
196 short int ai_continous;
197 // number of samples to aquire
198 long int ai_sample_count;
200 uint8_t *dux_commands;
201 // counter which ignores the first buffers
206 // The pointer to the private usb-data of the driver
207 // is also the private data for the comedi-device.
208 // This has to be global as the usb subsystem needs
209 // global variables. The other reason is that this
210 // structure must be there _before_ any comedi
211 // command is issued. The usb subsystem must be
212 // initialised before comedi can access it.
213 static usbduxfastsub_t usbduxfastsub[NUMUSBDUXFAST];
215 static DEFINE_MUTEX(start_stop_mutex);
217 // bulk transfers to usbduxfast
219 #define SENDADCOMMANDS 0
220 #define SENDINITEP6 1
222 static int send_dux_commands(usbduxfastsub_t * this_usbduxfastsub, int cmd_type)
225 this_usbduxfastsub->dux_commands[0] = cmd_type;
226 #ifdef CONFIG_COMEDI_DEBUG
228 printk("comedi%d: usbduxfast: dux_commands: ",
229 this_usbduxfastsub->comedidev->minor);
230 for (i = 0; i < SIZEOFDUXBUFFER; i++) {
231 printk(" %02x", this_usbduxfastsub->dux_commands[i]);
235 result = USB_BULK_MSG(this_usbduxfastsub->usbdev,
236 usb_sndbulkpipe(this_usbduxfastsub->usbdev,
238 this_usbduxfastsub->dux_commands,
239 SIZEOFDUXBUFFER, &nsent, BULK_TIMEOUT);
241 printk("comedi%d: could not transmit dux_commands to the usb-device, err=%d\n", this_usbduxfastsub->comedidev->minor, result);
246 // Stops the data acquision
247 // It should be safe to call this function from any context
248 static int usbduxfastsub_unlink_InURBs(usbduxfastsub_t * usbduxfastsub_tmp)
253 if (usbduxfastsub_tmp && usbduxfastsub_tmp->urbIn) {
254 usbduxfastsub_tmp->ai_cmd_running = 0;
255 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
256 j = usb_unlink_urb(usbduxfastsub_tmp->urbIn);
261 // waits until a running transfer is over
262 usb_kill_urb(usbduxfastsub_tmp->urbIn);
266 #ifdef CONFIG_COMEDI_DEBUG
267 printk("comedi: usbduxfast: unlinked InURB: res=%d\n", j);
272 /* This will stop a running acquisition operation */
273 // Is called from within this driver from both the
274 // interrupt context and from comedi
275 static int usbduxfast_ai_stop(usbduxfastsub_t * this_usbduxfastsub,
280 if (!this_usbduxfastsub) {
281 printk("comedi?: usbduxfast_ai_stop: this_usbduxfastsub=NULL!\n");
284 #ifdef CONFIG_COMEDI_DEBUG
285 printk("comedi: usbduxfast_ai_stop\n");
288 this_usbduxfastsub->ai_cmd_running = 0;
292 ret = usbduxfastsub_unlink_InURBs(this_usbduxfastsub);
298 // This will cancel a running acquisition operation.
299 // This is called by comedi but never from inside the
301 static int usbduxfast_ai_cancel(comedi_device * dev, comedi_subdevice * s)
303 usbduxfastsub_t *this_usbduxfastsub;
306 // force unlink of all urbs
307 #ifdef CONFIG_COMEDI_DEBUG
308 printk("comedi: usbduxfast_ai_cancel\n");
310 this_usbduxfastsub = dev->private;
311 if (!this_usbduxfastsub) {
312 printk("comedi: usbduxfast_ai_cancel: this_usbduxfastsub=NULL\n");
315 mutex_lock(&this_usbduxfastsub->mutex);
316 if (!(this_usbduxfastsub->probed)) {
317 mutex_unlock(&this_usbduxfastsub->mutex);
321 res = usbduxfast_ai_stop(this_usbduxfastsub, 1);
322 mutex_unlock(&this_usbduxfastsub->mutex);
328 // interrupt service routine
329 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
330 static void usbduxfastsub_ai_Irq(struct urb *urb)
332 static void usbduxfastsub_ai_Irq(struct urb *urb PT_REGS_ARG)
336 usbduxfastsub_t *this_usbduxfastsub;
337 comedi_device *this_comedidev;
344 printk("comedi_: usbduxfast_: ao int-handler called with urb=NULL!\n");
347 // the context variable points to the subdevice
348 this_comedidev = urb->context;
349 if (!this_comedidev) {
350 printk("comedi_: usbduxfast_: urb context is a NULL pointer!\n");
353 // the private structure of the subdevice is usbduxfastsub_t
354 this_usbduxfastsub = this_comedidev->private;
355 if (!this_usbduxfastsub) {
356 printk("comedi_: usbduxfast_: private of comedi subdev is a NULL pointer!\n");
359 // are we running a command?
360 if (unlikely(!(this_usbduxfastsub->ai_cmd_running))) {
361 // not running a command
362 // do not continue execution if no asynchronous command is running
363 // in particular not resubmit
367 if (unlikely(!(this_usbduxfastsub->attached))) {
368 // no comedi device there
371 // subdevice which is the AD converter
372 s = this_comedidev->subdevices + SUBDEV_AD;
374 // first we test if something unusual has just happened
375 switch (urb->status) {
379 // happens after an unlink command or when the device is plugged out
385 s->async->events |= COMEDI_CB_EOA;
386 s->async->events |= COMEDI_CB_ERROR;
387 comedi_event(this_usbduxfastsub->comedidev, s);
388 // stop the transfer w/o unlink
389 usbduxfast_ai_stop(this_usbduxfastsub, 0);
393 printk("comedi%d: usbduxfast: non-zero urb status received in ai intr context: %d\n", this_usbduxfastsub->comedidev->minor, urb->status);
394 s->async->events |= COMEDI_CB_EOA;
395 s->async->events |= COMEDI_CB_ERROR;
396 comedi_event(this_usbduxfastsub->comedidev, s);
397 usbduxfast_ai_stop(this_usbduxfastsub, 0);
401 p = urb->transfer_buffer;
402 if (!this_usbduxfastsub->ignore) {
403 if (!(this_usbduxfastsub->ai_continous)) {
404 // not continous, fixed number of samples
405 n = urb->actual_length / sizeof(uint16_t);
406 if (unlikely(this_usbduxfastsub->ai_sample_count < n)) {
407 // we have send only a fraction of the bytes received
408 cfc_write_array_to_buffer(s,
409 urb->transfer_buffer,
410 this_usbduxfastsub->ai_sample_count *
412 usbduxfast_ai_stop(this_usbduxfastsub, 0);
413 // say comedi that the acquistion is over
414 s->async->events |= COMEDI_CB_EOA;
415 comedi_event(this_usbduxfastsub->comedidev, s);
418 this_usbduxfastsub->ai_sample_count -= n;
420 // write the full buffer to comedi
421 err = cfc_write_array_to_buffer(s,
422 urb->transfer_buffer, urb->actual_length);
424 if (unlikely(err == 0)) {
425 /* buffer overflow */
426 usbduxfast_ai_stop(this_usbduxfastsub, 0);
430 // tell comedi that data is there
431 comedi_event(this_usbduxfastsub->comedidev, s);
434 // ignore this packet
435 this_usbduxfastsub->ignore--;
438 // command is still running
439 // resubmit urb for BULK transfer
440 urb->dev = this_usbduxfastsub->usbdev;
442 if ((err = USB_SUBMIT_URB(urb)) < 0) {
443 printk("comedi%d: usbduxfast: urb resubm failed: %d",
444 this_usbduxfastsub->comedidev->minor, err);
445 s->async->events |= COMEDI_CB_EOA;
446 s->async->events |= COMEDI_CB_ERROR;
447 comedi_event(this_usbduxfastsub->comedidev, s);
448 usbduxfast_ai_stop(this_usbduxfastsub, 0);
452 static int usbduxfastsub_start(usbduxfastsub_t * usbduxfastsub)
455 unsigned char *local_transfer_buffer;
457 local_transfer_buffer = kmalloc(16, GFP_KERNEL);
458 if (!local_transfer_buffer)
462 local_transfer_buffer[0] = 0;
463 errcode = USB_CONTROL_MSG(usbduxfastsub->usbdev,
464 // create a pipe for a control transfer
465 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
466 // bRequest, "Firmware"
467 USBDUXFASTSUB_FIRMWARE,
474 // address of the transfer buffer
475 local_transfer_buffer,
481 printk("comedi_: usbduxfast_: control msg failed (start)\n");
483 kfree(local_transfer_buffer);
487 static int usbduxfastsub_stop(usbduxfastsub_t * usbduxfastsub)
490 unsigned char *local_transfer_buffer;
492 local_transfer_buffer = kmalloc(16, GFP_KERNEL);
493 if (!local_transfer_buffer)
497 local_transfer_buffer[0] = 1;
498 errcode = USB_CONTROL_MSG
499 (usbduxfastsub->usbdev,
500 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
501 // bRequest, "Firmware"
502 USBDUXFASTSUB_FIRMWARE,
508 0x0000, local_transfer_buffer,
514 printk("comedi_: usbduxfast: control msg failed (stop)\n");
516 kfree(local_transfer_buffer);
520 static int usbduxfastsub_upload(usbduxfastsub_t * usbduxfastsub,
521 unsigned char *local_transfer_buffer,
522 unsigned int startAddr, unsigned int len)
526 errcode = USB_CONTROL_MSG
527 (usbduxfastsub->usbdev,
528 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
529 // brequest, firmware
530 USBDUXFASTSUB_FIRMWARE,
537 // our local safe buffer
538 local_transfer_buffer,
544 printk("comedi_: usbduxfast: uppload failed\n");
550 int firmwareUpload(usbduxfastsub_t * usbduxfastsub,
551 unsigned char *firmwareBinary, int sizeFirmware)
555 if (!firmwareBinary) {
558 ret = usbduxfastsub_stop(usbduxfastsub);
560 printk("comedi_: usbduxfast: can not stop firmware\n");
563 ret = usbduxfastsub_upload(usbduxfastsub,
564 firmwareBinary, 0, sizeFirmware);
566 printk("comedi_: usbduxfast: firmware upload failed\n");
569 ret = usbduxfastsub_start(usbduxfastsub);
571 printk("comedi_: usbduxfast: can not start firmware\n");
577 int usbduxfastsub_submit_InURBs(usbduxfastsub_t * usbduxfastsub)
581 if (!usbduxfastsub) {
584 usb_fill_bulk_urb(usbduxfastsub->urbIn,
585 usbduxfastsub->usbdev,
586 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
587 usbduxfastsub->transfer_buffer,
588 SIZEINBUF, usbduxfastsub_ai_Irq, usbduxfastsub->comedidev);
590 #ifdef CONFIG_COMEDI_DEBUG
591 printk("comedi%d: usbduxfast: submitting in-urb: %x,%x\n",
592 usbduxfastsub->comedidev->minor,
593 (int)(usbduxfastsub->urbIn->context),
594 (int)(usbduxfastsub->urbIn->dev));
596 errFlag = USB_SUBMIT_URB(usbduxfastsub->urbIn);
598 printk("comedi_: usbduxfast: ai: ");
599 printk("USB_SUBMIT_URB");
600 printk(" error %d\n", errFlag);
606 static int usbduxfast_ai_cmdtest(comedi_device * dev,
607 comedi_subdevice * s, comedi_cmd * cmd)
609 int err = 0, stop_mask = 0;
610 long int steps, tmp = 0;
612 usbduxfastsub_t *this_usbduxfastsub = dev->private;
613 if (!(this_usbduxfastsub->probed)) {
616 #ifdef CONFIG_COMEDI_DEBUG
617 printk("comedi%d: usbduxfast_ai_cmdtest\n", dev->minor);
618 printk("comedi%d: usbduxfast: convert_arg=%u scan_begin_arg=%u\n",
619 dev->minor, cmd->convert_arg, cmd->scan_begin_arg);
621 /* step 1: make sure trigger sources are trivially valid */
623 tmp = cmd->start_src;
624 cmd->start_src &= TRIG_NOW | TRIG_EXT | TRIG_INT;
625 if (!cmd->start_src || tmp != cmd->start_src)
628 tmp = cmd->scan_begin_src;
629 cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT;
630 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
633 tmp = cmd->convert_src;
634 cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
635 if (!cmd->convert_src || tmp != cmd->convert_src)
638 tmp = cmd->scan_end_src;
639 cmd->scan_end_src &= TRIG_COUNT;
640 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
644 stop_mask = TRIG_COUNT | TRIG_NONE;
645 cmd->stop_src &= stop_mask;
646 if (!cmd->stop_src || tmp != cmd->stop_src)
652 /* step 2: make sure trigger sources are unique and mutually compatible */
654 if (cmd->start_src != TRIG_NOW &&
655 cmd->start_src != TRIG_EXT && cmd->start_src != TRIG_INT)
657 if (cmd->scan_begin_src != TRIG_TIMER &&
658 cmd->scan_begin_src != TRIG_FOLLOW &&
659 cmd->scan_begin_src != TRIG_EXT)
661 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
663 if (cmd->stop_src != TRIG_COUNT &&
664 cmd->stop_src != TRIG_EXT && cmd->stop_src != TRIG_NONE)
667 // can't have external stop and start triggers at once
668 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
674 /* step 3: make sure arguments are trivially compatible */
676 if (cmd->start_src == TRIG_NOW && cmd->start_arg != 0) {
681 if (!cmd->chanlist_len) {
684 if (cmd->scan_end_arg != cmd->chanlist_len) {
685 cmd->scan_end_arg = cmd->chanlist_len;
689 if (cmd->chanlist_len == 1) {
692 minSamplPer = MIN_SAMPLING_PERIOD;
695 if (cmd->convert_src == TRIG_TIMER) {
696 steps = cmd->convert_arg * 30;
697 if (steps < (minSamplPer * 1000)) {
698 steps = minSamplPer * 1000;
700 if (steps > (MAX_SAMPLING_PERIOD * 1000)) {
701 steps = MAX_SAMPLING_PERIOD * 1000;
705 if (cmd->convert_arg != tmp) {
706 cmd->convert_arg = tmp;
711 if (cmd->scan_begin_src == TRIG_TIMER) {
715 switch (cmd->stop_src) {
717 if (!cmd->stop_arg) {
723 if (cmd->stop_arg != 0) {
728 // TRIG_EXT doesn't care since it doesn't trigger off a numbered channel
736 /* step 4: fix up any arguments */
742 static int usbduxfast_ai_inttrig(comedi_device * dev,
743 comedi_subdevice * s, unsigned int trignum)
746 usbduxfastsub_t *this_usbduxfastsub = dev->private;
747 if (!this_usbduxfastsub) {
750 mutex_lock(&this_usbduxfastsub->mutex);
751 if (!(this_usbduxfastsub->probed)) {
752 mutex_unlock(&this_usbduxfastsub->mutex);
755 #ifdef CONFIG_COMEDI_DEBUG
756 printk("comedi%d: usbduxfast_ai_inttrig\n", dev->minor);
760 printk("comedi%d: usbduxfast_ai_inttrig: invalid trignum\n",
762 mutex_unlock(&this_usbduxfastsub->mutex);
765 if (!(this_usbduxfastsub->ai_cmd_running)) {
766 this_usbduxfastsub->ai_cmd_running = 1;
767 ret = usbduxfastsub_submit_InURBs(this_usbduxfastsub);
769 printk("comedi%d: usbduxfast_ai_inttrig: urbSubmit: err=%d\n", dev->minor, ret);
770 this_usbduxfastsub->ai_cmd_running = 0;
771 mutex_unlock(&this_usbduxfastsub->mutex);
774 s->async->inttrig = NULL;
776 printk("comedi%d: ai_inttrig but acqu is already running\n",
779 mutex_unlock(&this_usbduxfastsub->mutex);
783 // offsets for the GPIF bytes
784 // the first byte is the command byte
785 #define LENBASE 1+0x00
786 #define OPBASE 1+0x08
787 #define OUTBASE 1+0x10
788 #define LOGBASE 1+0x18
790 static int usbduxfast_ai_cmd(comedi_device * dev, comedi_subdevice * s)
792 comedi_cmd *cmd = &s->async->cmd;
793 unsigned int chan, gain, rngmask = 0xff;
795 usbduxfastsub_t *this_usbduxfastsub = dev->private;
797 long steps, steps_tmp;
799 #ifdef CONFIG_COMEDI_DEBUG
800 printk("comedi%d: usbduxfast_ai_cmd\n", dev->minor);
802 if (!this_usbduxfastsub) {
805 mutex_lock(&this_usbduxfastsub->mutex);
806 if (!(this_usbduxfastsub->probed)) {
807 mutex_unlock(&this_usbduxfastsub->mutex);
810 if (this_usbduxfastsub->ai_cmd_running) {
811 printk("comedi%d: ai_cmd not possible. Another ai_cmd is running.\n", dev->minor);
812 mutex_unlock(&this_usbduxfastsub->mutex);
815 // set current channel of the running aquisition to zero
816 s->async->cur_chan = 0;
818 // ignore the first buffers from the device if there is an error condition
819 this_usbduxfastsub->ignore = PACKETS_TO_IGNORE;
821 if (cmd->chanlist_len > 0) {
822 gain = CR_RANGE(cmd->chanlist[0]);
823 for (i = 0; i < cmd->chanlist_len; ++i) {
824 chan = CR_CHAN(cmd->chanlist[i]);
826 printk("comedi%d: cmd is accepting only consecutive channels.\n", dev->minor);
827 mutex_unlock(&this_usbduxfastsub->mutex);
830 if ((gain != CR_RANGE(cmd->chanlist[i]))
831 && (cmd->chanlist_len > 3)) {
832 printk("comedi%d: the gain must be the same for all channels.\n", dev->minor);
833 mutex_unlock(&this_usbduxfastsub->mutex);
836 if (i >= NUMCHANNELS) {
837 printk("comedi%d: channel list too long\n",
844 if (cmd->scan_begin_src == TRIG_TIMER) {
845 printk("comedi%d: usbduxfast: scan_begin_src==TRIG_TIMER not valid.\n", dev->minor);
846 mutex_unlock(&this_usbduxfastsub->mutex);
849 if (cmd->convert_src == TRIG_TIMER) {
850 steps = (cmd->convert_arg * 30) / 1000;
852 if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
853 printk("comedi%d: usbduxfast: ai_cmd: steps=%ld, scan_begin_arg=%d. Not properly tested by cmdtest?\n", dev->minor, steps, cmd->scan_begin_arg);
854 mutex_unlock(&this_usbduxfastsub->mutex);
857 if (steps > MAX_SAMPLING_PERIOD) {
858 printk("comedi%d: usbduxfast: ai_cmd: sampling rate too low.\n",
860 mutex_unlock(&this_usbduxfastsub->mutex);
863 if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
864 && (cmd->chanlist_len != 16)) {
865 printk("comedi%d: usbduxfast: ai_cmd: TRIG_EXT only with 1 or 16 channels possible.\n", dev->minor);
866 mutex_unlock(&this_usbduxfastsub->mutex);
869 #ifdef CONFIG_COMEDI_DEBUG
870 printk("comedi%d: usbduxfast: steps=%ld, convert_arg=%u, ai_timer=%u\n",
872 steps, cmd->convert_arg, this_usbduxfastsub->ai_timer);
875 switch (cmd->chanlist_len) {
878 if (CR_RANGE(cmd->chanlist[0]) > 0)
879 rngmask = 0xff - 0x04;
883 // for external trigger: looping in this state until the RDY0 pin
885 if (cmd->start_src == TRIG_EXT) { // we loop here until ready has been set
886 this_usbduxfastsub->dux_commands[LENBASE + 0] = 0x01; // branch back to state 0
887 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x01; // deceision state w/o data
888 this_usbduxfastsub->dux_commands[OUTBASE + 0] =
890 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0x00; // RDY0 = 0
891 } else { // we just proceed to state 1
892 this_usbduxfastsub->dux_commands[LENBASE + 0] = 1;
893 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0;
894 this_usbduxfastsub->dux_commands[OUTBASE + 0] =
896 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
899 if (steps < MIN_SAMPLING_PERIOD) {
900 // for fast single channel aqu without mux
902 // we just stay here at state 1 and rexecute the same state
903 // this gives us 30MHz sampling rate
904 this_usbduxfastsub->dux_commands[LENBASE + 1] = 0x89; // branch back to state 1
905 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x03; // deceision state with data
906 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
908 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0xFF; // doesn't matter
910 // we loop through two states: data and delay: max rate is 15Mhz
911 this_usbduxfastsub->dux_commands[LENBASE + 1] =
913 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x02; // data
914 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
916 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0; // doesn't matter
918 this_usbduxfastsub->dux_commands[LENBASE + 2] = 0x09; // branch back to state 1
919 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0x01; // deceision state w/o data
920 this_usbduxfastsub->dux_commands[OUTBASE + 2] =
922 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0xFF; // doesn't matter
925 // we loop through 3 states: 2x delay and 1x data. This gives a min
926 // sampling rate of 60kHz.
928 // we have 1 state with duration 1
931 // do the first part of the delay
932 this_usbduxfastsub->dux_commands[LENBASE + 1] =
934 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0;
935 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
937 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
939 // and the second part
940 this_usbduxfastsub->dux_commands[LENBASE + 2] =
942 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
943 this_usbduxfastsub->dux_commands[OUTBASE + 2] =
945 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
947 // get the data and branch back
948 this_usbduxfastsub->dux_commands[LENBASE + 3] = 0x09; // branch back to state 1
949 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0x03; // deceision state w data
950 this_usbduxfastsub->dux_commands[OUTBASE + 3] =
952 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0xFF; // doesn't matter
958 // commit data to the FIFO
959 if (CR_RANGE(cmd->chanlist[0]) > 0)
960 rngmask = 0xff - 0x04;
963 this_usbduxfastsub->dux_commands[LENBASE + 0] = 1;
964 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x02; // data
965 this_usbduxfastsub->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
966 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
968 // we have 1 state with duration 1: state 0
969 steps_tmp = steps - 1;
971 if (CR_RANGE(cmd->chanlist[1]) > 0)
972 rngmask = 0xff - 0x04;
975 // do the first part of the delay
976 this_usbduxfastsub->dux_commands[LENBASE + 1] = steps_tmp / 2;
977 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0;
978 this_usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFE & rngmask; //count
979 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
981 // and the second part
982 this_usbduxfastsub->dux_commands[LENBASE + 2] =
983 steps_tmp - steps_tmp / 2;
984 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
985 this_usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
986 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
988 this_usbduxfastsub->dux_commands[LENBASE + 3] = 1;
989 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0x02; // data
990 this_usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
991 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
993 // we have 2 states with duration 1: step 6 and the IDLE state
994 steps_tmp = steps - 2;
996 if (CR_RANGE(cmd->chanlist[0]) > 0)
997 rngmask = 0xff - 0x04;
1000 // do the first part of the delay
1001 this_usbduxfastsub->dux_commands[LENBASE + 4] = steps_tmp / 2;
1002 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0;
1003 this_usbduxfastsub->dux_commands[OUTBASE + 4] = (0xFF - 0x02) & rngmask; //reset
1004 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1006 // and the second part
1007 this_usbduxfastsub->dux_commands[LENBASE + 5] =
1008 steps_tmp - steps_tmp / 2;
1009 this_usbduxfastsub->dux_commands[OPBASE + 5] = 0;
1010 this_usbduxfastsub->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1011 this_usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1013 this_usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1014 this_usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1015 this_usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1016 this_usbduxfastsub->dux_commands[LOGBASE + 6] = 0;
1021 for (j = 0; j < 1; j++) {
1022 if (CR_RANGE(cmd->chanlist[j]) > 0)
1023 rngmask = 0xff - 0x04;
1026 // commit data to the FIFO and do the first part of the delay
1027 this_usbduxfastsub->dux_commands[LENBASE + j * 2] =
1029 this_usbduxfastsub->dux_commands[OPBASE + j * 2] = 0x02; // data
1030 this_usbduxfastsub->dux_commands[OUTBASE + j * 2] = 0xFF & rngmask; // no change
1031 this_usbduxfastsub->dux_commands[LOGBASE + j * 2] = 0;
1033 if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
1034 rngmask = 0xff - 0x04;
1037 // do the second part of the delay
1038 this_usbduxfastsub->dux_commands[LENBASE + j * 2 + 1] =
1040 this_usbduxfastsub->dux_commands[OPBASE + j * 2 + 1] = 0; // no data
1041 this_usbduxfastsub->dux_commands[OUTBASE + j * 2 + 1] = 0xFE & rngmask; //count
1042 this_usbduxfastsub->dux_commands[LOGBASE + j * 2 + 1] =
1046 // 2 steps with duration 1: the idele step and step 6:
1047 steps_tmp = steps - 2;
1048 // commit data to the FIFO and do the first part of the delay
1049 this_usbduxfastsub->dux_commands[LENBASE + 4] = steps_tmp / 2;
1050 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0x02; // data
1051 this_usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFF & rngmask; // no change
1052 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1054 if (CR_RANGE(cmd->chanlist[0]) > 0)
1055 rngmask = 0xff - 0x04;
1058 // do the second part of the delay
1059 this_usbduxfastsub->dux_commands[LENBASE + 5] =
1060 steps_tmp - steps_tmp / 2;
1061 this_usbduxfastsub->dux_commands[OPBASE + 5] = 0; // no data
1062 this_usbduxfastsub->dux_commands[OUTBASE + 5] = (0xFF - 0x02) & rngmask; // reset
1063 this_usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1065 this_usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1066 this_usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1067 this_usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1068 this_usbduxfastsub->dux_commands[LOGBASE + 6] = 0;
1071 if (CR_RANGE(cmd->chanlist[0]) > 0)
1072 rngmask = 0xff - 0x04;
1075 if (cmd->start_src == TRIG_EXT) { // we loop here until ready has been set
1076 this_usbduxfastsub->dux_commands[LENBASE + 0] = 0x01; // branch back to state 0
1077 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x01; // deceision state w/o data
1078 this_usbduxfastsub->dux_commands[OUTBASE + 0] = (0xFF - 0x02) & rngmask; // reset
1079 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0x00; // RDY0 = 0
1080 } else { // we just proceed to state 1
1081 this_usbduxfastsub->dux_commands[LENBASE + 0] = 255; // 30us reset pulse
1082 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0;
1083 this_usbduxfastsub->dux_commands[OUTBASE + 0] = (0xFF - 0x02) & rngmask; // reset
1084 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1087 // commit data to the FIFO
1088 this_usbduxfastsub->dux_commands[LENBASE + 1] = 1;
1089 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x02; // data
1090 this_usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
1091 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
1093 // we have 2 states with duration 1
1096 // do the first part of the delay
1097 this_usbduxfastsub->dux_commands[LENBASE + 2] = steps / 2;
1098 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
1099 this_usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1100 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
1102 // and the second part
1103 this_usbduxfastsub->dux_commands[LENBASE + 3] =
1105 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0;
1106 this_usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
1107 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
1109 this_usbduxfastsub->dux_commands[LENBASE + 4] = 0x09; // branch back to state 1
1110 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0x01; // deceision state w/o data
1111 this_usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1112 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0xFF; // doesn't matter
1117 printk("comedi %d: unsupported combination of channels\n",
1119 mutex_unlock(&this_usbduxfastsub->mutex);
1123 #ifdef CONFIG_COMEDI_DEBUG
1124 printk("comedi %d: sending commands to the usb device\n", dev->minor);
1126 // 0 means that the AD commands are sent
1127 result = send_dux_commands(this_usbduxfastsub, SENDADCOMMANDS);
1129 printk("comedi%d: adc command could not be submitted. Aborting...\n", dev->minor);
1130 mutex_unlock(&this_usbduxfastsub->mutex);
1133 if (cmd->stop_src == TRIG_COUNT) {
1134 this_usbduxfastsub->ai_sample_count =
1135 (cmd->stop_arg) * (cmd->scan_end_arg);
1136 if (usbduxfastsub->ai_sample_count < 1) {
1137 printk("comedi%d: (cmd->stop_arg)*(cmd->scan_end_arg)<1, aborting.\n", dev->minor);
1138 mutex_unlock(&this_usbduxfastsub->mutex);
1141 this_usbduxfastsub->ai_continous = 0;
1143 // continous aquisition
1144 this_usbduxfastsub->ai_continous = 1;
1145 this_usbduxfastsub->ai_sample_count = 0;
1148 if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
1149 // enable this acquisition operation
1150 this_usbduxfastsub->ai_cmd_running = 1;
1151 ret = usbduxfastsub_submit_InURBs(this_usbduxfastsub);
1153 this_usbduxfastsub->ai_cmd_running = 0;
1154 // fixme: unlink here??
1155 mutex_unlock(&this_usbduxfastsub->mutex);
1158 s->async->inttrig = NULL;
1161 // don't enable the acquision operation
1162 // wait for an internal signal
1163 s->async->inttrig = usbduxfast_ai_inttrig;
1165 mutex_unlock(&this_usbduxfastsub->mutex);
1170 /* Mode 0 is used to get a single conversion on demand */
1171 static int usbduxfast_ai_insn_read(comedi_device * dev,
1172 comedi_subdevice * s, comedi_insn * insn, lsampl_t * data)
1174 int i, j, n, actual_length;
1175 int chan, range, rngmask;
1177 usbduxfastsub_t *usbduxfastsub = dev->private;
1179 if (!usbduxfastsub) {
1180 printk("comedi%d: ai_insn_read: no usb dev.\n", dev->minor);
1183 #ifdef CONFIG_COMEDI_DEBUG
1184 printk("comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1185 dev->minor, insn->n, insn->subdev);
1187 mutex_lock(&usbduxfastsub->mutex);
1188 if (!(usbduxfastsub->probed)) {
1189 mutex_unlock(&usbduxfastsub->mutex);
1192 if (usbduxfastsub->ai_cmd_running) {
1193 printk("comedi%d: ai_insn_read not possible. Async Command is running.\n", dev->minor);
1194 mutex_unlock(&usbduxfastsub->mutex);
1197 // sample one channel
1198 chan = CR_CHAN(insn->chanspec);
1199 range = CR_RANGE(insn->chanspec);
1200 // set command for the first channel
1203 rngmask = 0xff - 0x04;
1206 // commit data to the FIFO
1207 usbduxfastsub->dux_commands[LENBASE + 0] = 1;
1208 usbduxfastsub->dux_commands[OPBASE + 0] = 0x02; // data
1209 usbduxfastsub->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
1210 usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1212 // do the first part of the delay
1213 usbduxfastsub->dux_commands[LENBASE + 1] = 12;
1214 usbduxfastsub->dux_commands[OPBASE + 1] = 0;
1215 usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
1216 usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
1218 usbduxfastsub->dux_commands[LENBASE + 2] = 1;
1219 usbduxfastsub->dux_commands[OPBASE + 2] = 0;
1220 usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1221 usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
1223 usbduxfastsub->dux_commands[LENBASE + 3] = 1;
1224 usbduxfastsub->dux_commands[OPBASE + 3] = 0;
1225 usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFE & rngmask;
1226 usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
1228 usbduxfastsub->dux_commands[LENBASE + 4] = 1;
1229 usbduxfastsub->dux_commands[OPBASE + 4] = 0;
1230 usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFE & rngmask;
1231 usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1234 usbduxfastsub->dux_commands[LENBASE + 5] = 12;
1235 usbduxfastsub->dux_commands[OPBASE + 5] = 0;
1236 usbduxfastsub->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1237 usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1239 usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1240 usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1241 usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1242 usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1244 #ifdef CONFIG_COMEDI_DEBUG
1245 printk("comedi %d: sending commands to the usb device\n", dev->minor);
1247 // 0 means that the AD commands are sent
1248 err = send_dux_commands(usbduxfastsub, SENDADCOMMANDS);
1250 printk("comedi%d: adc command could not be submitted. Aborting...\n", dev->minor);
1251 mutex_unlock(&usbduxfastsub->mutex);
1254 #ifdef CONFIG_COMEDI_DEBUG
1255 printk("comedi%d: usbduxfast: submitting in-urb: %x,%x\n",
1256 usbduxfastsub->comedidev->minor,
1257 (int)(usbduxfastsub->urbIn->context),
1258 (int)(usbduxfastsub->urbIn->dev));
1260 for (i = 0; i < PACKETS_TO_IGNORE; i++) {
1261 err = USB_BULK_MSG(usbduxfastsub->usbdev,
1262 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
1263 usbduxfastsub->transfer_buffer,
1264 SIZEINBUF, &actual_length, BULK_TIMEOUT);
1266 printk("comedi%d: insn timeout. No data.\n",
1268 mutex_unlock(&usbduxfastsub->mutex);
1273 for (i = 0; i < insn->n;) {
1274 err = USB_BULK_MSG(usbduxfastsub->usbdev,
1275 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
1276 usbduxfastsub->transfer_buffer,
1277 SIZEINBUF, &actual_length, BULK_TIMEOUT);
1279 printk("comedi%d: insn data error: %d\n",
1281 mutex_unlock(&usbduxfastsub->mutex);
1284 n = actual_length / sizeof(uint16_t);
1285 if ((n % 16) != 0) {
1286 printk("comedi%d: insn data packet corrupted.\n",
1288 mutex_unlock(&usbduxfastsub->mutex);
1291 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
1293 ((uint16_t *) (usbduxfastsub->
1294 transfer_buffer))[j];
1298 mutex_unlock(&usbduxfastsub->mutex);
1302 static unsigned hex2unsigned(char *h)
1306 hi = h[0] - 'A' + 0x0a;
1311 lo = h[1] - 'A' + 0x0a;
1315 return hi * 0x10 + lo;
1319 #define FIRMWARE_MAX_LEN 0x2000
1321 // taken from David Brownell's fxload and adjusted for this driver
1322 static int read_firmware(usbduxfastsub_t * usbduxfastsub,
1323 const void *firmwarePtr,
1327 unsigned char *fp = (char *)firmwarePtr;
1328 unsigned char *firmwareBinary = NULL;
1332 firmwareBinary = kmalloc(FIRMWARE_MAX_LEN, GFP_KERNEL);
1333 if (!firmwareBinary) {
1334 printk("comedi_: usbduxfast: mem alloc for firmware failed\n");
1346 while ((i < size) && (fp[i] != 13) && (fp[i] != 10)) {
1350 if (j >= sizeof(buf)) {
1351 printk("comedi_: usbduxfast: bogus firmware file!\n");
1355 // get rid of LF/CR/...
1356 while ((i < size) && ((fp[i] == 13) || (fp[i] == 10)
1362 //printk("comedi_: buf=%s\n",buf);
1364 /* EXTENSION: "# comment-till-end-of-line", for copyrights etc */
1368 if (buf[0] != ':') {
1369 printk("comedi_: usbduxfast: upload: not an ihex record: %s", buf);
1373 /* Read the length field (up to 16 bytes) */
1374 len = hex2unsigned(buf + 1);
1376 /* Read the target offset */
1377 off = (hex2unsigned(buf + 3) * 0x0100) + hex2unsigned(buf + 5);
1379 if ((off + len) > maxAddr) {
1380 maxAddr = off + len;
1383 if (maxAddr >= FIRMWARE_MAX_LEN) {
1384 printk("comedi_: usbduxfast: firmware upload goes beyond FX2 RAM boundaries.");
1387 //printk("comedi_: usbduxfast: off=%x, len=%x:",off,len);
1389 /* Read the record type */
1390 type = hex2unsigned(buf + 7);
1392 /* If this is an EOF record, then make it so. */
1398 printk("comedi_: usbduxfast: unsupported record type: %u\n", type);
1402 for (idx = 0, cp = buf + 9; idx < len; idx += 1, cp += 2) {
1403 firmwareBinary[idx + off] = hex2unsigned(cp);
1404 //printk("%02x ",firmwareBinary[idx+off]);
1409 printk("comedi_: usbduxfast: unexpected end of hex file\n");
1414 res = firmwareUpload(usbduxfastsub, firmwareBinary, maxAddr + 1);
1415 kfree(firmwareBinary);
1419 static void tidy_up(usbduxfastsub_t * usbduxfastsub_tmp)
1421 #ifdef CONFIG_COMEDI_DEBUG
1422 printk("comedi_: usbduxfast: tiding up\n");
1424 if (!usbduxfastsub_tmp) {
1427 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1428 // shows the usb subsystem that the driver is down
1429 if (usbduxfastsub_tmp->interface) {
1430 usb_set_intfdata(usbduxfastsub_tmp->interface, NULL);
1434 usbduxfastsub_tmp->probed = 0;
1436 if (usbduxfastsub_tmp->urbIn) {
1437 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)
1438 // waits until a running transfer is over
1439 // thus, under 2.4 hotplugging while a command
1440 // is running is not safe
1441 usb_kill_urb(usbduxfastsub_tmp->urbIn);
1443 if (usbduxfastsub_tmp->transfer_buffer) {
1444 kfree(usbduxfastsub_tmp->transfer_buffer);
1445 usbduxfastsub_tmp->transfer_buffer = NULL;
1447 usb_free_urb(usbduxfastsub_tmp->urbIn);
1448 usbduxfastsub_tmp->urbIn = NULL;
1450 if (usbduxfastsub_tmp->insnBuffer) {
1451 kfree(usbduxfastsub_tmp->insnBuffer);
1452 usbduxfastsub_tmp->insnBuffer = NULL;
1454 if (usbduxfastsub_tmp->dux_commands) {
1455 kfree(usbduxfastsub_tmp->dux_commands);
1456 usbduxfastsub_tmp->dux_commands = NULL;
1458 usbduxfastsub_tmp->ai_cmd_running = 0;
1461 static void usbduxfast_firmware_request_complete_handler(
1462 const struct firmware *fw,
1465 usbduxfastsub_t * usbduxfastsub_tmp = (usbduxfastsub_t *)context;
1466 struct usb_device *usbdev = usbduxfastsub_tmp->usbdev;
1473 // we need to upload the firmware here because fw will be
1474 // freed one we've left this function
1475 ret=read_firmware(usbduxfastsub_tmp,
1480 dev_err(&usbdev->dev,
1481 "Could not upload firmware (err=%d)\n",
1486 comedi_usb_auto_config(usbdev, BOARDNAME);
1489 * in more recent versions the completion handler
1490 * had to release the firmware whereas in older
1491 * versions this has been done by the caller
1493 COMEDI_RELEASE_FIRMWARE_NOWAIT(fw);
1496 // allocate memory for the urbs and initialise them
1497 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1498 static void *usbduxfastsub_probe(struct usb_device *udev,
1499 unsigned int interfnum,
1500 const struct usb_device_id *id)
1503 static int usbduxfastsub_probe(struct usb_interface *uinterf,
1504 const struct usb_device_id *id)
1506 struct usb_device *udev = interface_to_usbdev(uinterf);
1512 if (udev->speed != USB_SPEED_HIGH) {
1513 printk("comedi_: usbduxfast_: This driver needs USB 2.0 to operate. Aborting...\n");
1514 return PROBE_ERR_RETURN(-ENODEV);
1516 #ifdef CONFIG_COMEDI_DEBUG
1517 printk("comedi_: usbduxfast_: finding a free structure for the usb-device\n");
1519 mutex_lock(&start_stop_mutex);
1520 // look for a free place in the usbduxfast array
1522 for (i = 0; i < NUMUSBDUXFAST; i++) {
1523 if (!(usbduxfastsub[i].probed)) {
1531 printk("Too many usbduxfast-devices connected.\n");
1532 mutex_unlock(&start_stop_mutex);
1533 return PROBE_ERR_RETURN(-EMFILE);
1535 #ifdef CONFIG_COMEDI_DEBUG
1536 printk("comedi_: usbduxfast: usbduxfastsub[%d] is ready to connect to comedi.\n", index);
1539 mutex_init(&(usbduxfastsub[index].mutex));
1540 // save a pointer to the usb device
1541 usbduxfastsub[index].usbdev = udev;
1543 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1544 // save the interface number
1545 usbduxfastsub[index].ifnum = interfnum;
1547 // 2.6: save the interface itself
1548 usbduxfastsub[index].interface = uinterf;
1549 // get the interface number from the interface
1550 usbduxfastsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
1551 // hand the private data over to the usb subsystem
1552 // will be needed for disconnect
1553 usb_set_intfdata(uinterf, &(usbduxfastsub[index]));
1556 #ifdef CONFIG_COMEDI_DEBUG
1557 printk("comedi_: usbduxfast: ifnum=%d\n", usbduxfastsub[index].ifnum);
1559 // create space for the commands going to the usb device
1560 usbduxfastsub[index].dux_commands = kmalloc(SIZEOFDUXBUFFER,
1562 if (!usbduxfastsub[index].dux_commands) {
1563 printk("comedi_: usbduxfast: error alloc space for dac commands\n");
1564 tidy_up(&(usbduxfastsub[index]));
1565 mutex_unlock(&start_stop_mutex);
1566 return PROBE_ERR_RETURN(-ENOMEM);
1568 // create space of the instruction buffer
1569 usbduxfastsub[index].insnBuffer = kmalloc(SIZEINSNBUF, GFP_KERNEL);
1570 if (!(usbduxfastsub[index].insnBuffer)) {
1571 printk("comedi_: usbduxfast: could not alloc space for insnBuffer\n");
1572 tidy_up(&(usbduxfastsub[index]));
1573 mutex_unlock(&start_stop_mutex);
1574 return PROBE_ERR_RETURN(-ENOMEM);
1576 // setting to alternate setting 1: enabling bulk ep
1577 i = usb_set_interface(usbduxfastsub[index].usbdev,
1578 usbduxfastsub[index].ifnum, 1);
1580 printk("comedi_: usbduxfast%d: could not switch to alternate setting 1.\n", index);
1581 tidy_up(&(usbduxfastsub[index]));
1582 mutex_unlock(&start_stop_mutex);
1583 return PROBE_ERR_RETURN(-ENODEV);
1585 usbduxfastsub[index].urbIn = USB_ALLOC_URB(0);
1586 if (usbduxfastsub[index].urbIn == NULL) {
1587 printk("comedi_: usbduxfast%d: Could not alloc. urb\n", index);
1588 tidy_up(&(usbduxfastsub[index]));
1589 mutex_unlock(&start_stop_mutex);
1590 return PROBE_ERR_RETURN(-ENOMEM);
1592 usbduxfastsub[index].transfer_buffer = kmalloc(SIZEINBUF, GFP_KERNEL);
1593 if (!(usbduxfastsub[index].transfer_buffer)) {
1594 printk("comedi_: usbduxfast%d: could not alloc. transb.\n",
1596 tidy_up(&(usbduxfastsub[index]));
1597 mutex_unlock(&start_stop_mutex);
1598 return PROBE_ERR_RETURN(-ENOMEM);
1600 // we've reached the bottom of the function
1601 usbduxfastsub[index].probed = 1;
1602 mutex_unlock(&start_stop_mutex);
1604 ret = request_firmware_nowait(THIS_MODULE,
1606 "usbduxfast_firmware.hex",
1609 usbduxfastsub + index,
1610 usbduxfast_firmware_request_complete_handler);
1614 "could not load firmware (err=%d)\n",
1619 printk("comedi_: usbduxfast%d has been successfully initialized.\n",
1622 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1623 return (void *)(&usbduxfastsub[index]);
1630 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1631 static void usbduxfastsub_disconnect(struct usb_device *udev, void *ptr)
1633 usbduxfastsub_t *usbduxfastsub_tmp = (usbduxfastsub_t *) ptr;
1635 static void usbduxfastsub_disconnect(struct usb_interface *intf)
1637 usbduxfastsub_t *usbduxfastsub_tmp = usb_get_intfdata(intf);
1638 struct usb_device *udev = interface_to_usbdev(intf);
1640 if (!usbduxfastsub_tmp) {
1641 printk("comedi_: usbduxfast: disconnect called with null pointer.\n");
1644 if (usbduxfastsub_tmp->usbdev != udev) {
1645 printk("comedi_: usbduxfast: BUG! called with wrong ptr!!!\n");
1649 comedi_usb_auto_unconfig(udev);
1651 mutex_lock(&start_stop_mutex);
1652 mutex_lock(&usbduxfastsub_tmp->mutex);
1653 tidy_up(usbduxfastsub_tmp);
1654 mutex_unlock(&usbduxfastsub_tmp->mutex);
1655 mutex_unlock(&start_stop_mutex);
1656 #ifdef CONFIG_COMEDI_DEBUG
1657 printk("comedi_: usbduxfast: disconnected from the usb\n");
1661 // is called when comedi-config is called
1662 static int usbduxfast_attach(comedi_device * dev, comedi_devconfig * it)
1667 comedi_subdevice *s = NULL;
1668 dev->private = NULL;
1670 mutex_lock(&start_stop_mutex);
1671 // find a valid device which has been detected by the probe function of the usb
1673 for (i = 0; i < NUMUSBDUXFAST; i++) {
1674 if ((usbduxfastsub[i].probed) && (!usbduxfastsub[i].attached)) {
1681 printk("comedi%d: usbduxfast: error: attach failed, no usbduxfast devs connected to the usb bus.\n", dev->minor);
1682 mutex_unlock(&start_stop_mutex);
1686 mutex_lock(&(usbduxfastsub[index].mutex));
1687 // pointer back to the corresponding comedi device
1688 usbduxfastsub[index].comedidev = dev;
1690 // trying to upload the firmware into the chip
1691 if (comedi_aux_data(it->options, 0) &&
1692 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
1693 read_firmware(&usbduxfastsub[index],
1694 comedi_aux_data(it->options, 0),
1695 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
1698 dev->board_name = BOARDNAME;
1700 /* set number of subdevices */
1701 dev->n_subdevices = N_SUBDEVICES;
1703 // allocate space for the subdevices
1704 if ((ret = alloc_subdevices(dev, N_SUBDEVICES)) < 0) {
1705 printk("comedi%d: usbduxfast: error alloc space for subdev\n",
1707 mutex_unlock(&start_stop_mutex);
1711 printk("comedi%d: usbduxfast: usb-device %d is attached to comedi.\n",
1713 // private structure is also simply the usb-structure
1714 dev->private = usbduxfastsub + index;
1715 // the first subdevice is the A/D converter
1716 s = dev->subdevices + SUBDEV_AD;
1717 // the URBs get the comedi subdevice
1718 // which is responsible for reading
1719 // this is the subdevice which reads data
1720 dev->read_subdev = s;
1721 // the subdevice receives as private structure the
1725 s->type = COMEDI_SUBD_AI;
1726 // readable and ref is to ground
1727 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1730 // length of the channellist
1731 s->len_chanlist = 16;
1732 // callback functions
1733 s->insn_read = usbduxfast_ai_insn_read;
1734 s->do_cmdtest = usbduxfast_ai_cmdtest;
1735 s->do_cmd = usbduxfast_ai_cmd;
1736 s->cancel = usbduxfast_ai_cancel;
1737 // max value from the A/D converter (12bit+1 bit for overflow)
1738 s->maxdata = 0x1000;
1739 // range table to convert to physical units
1740 s->range_table = &range_usbduxfast_ai_range;
1742 // finally decide that it's attached
1743 usbduxfastsub[index].attached = 1;
1745 mutex_unlock(&(usbduxfastsub[index].mutex));
1747 mutex_unlock(&start_stop_mutex);
1749 printk("comedi%d: successfully attached to usbduxfast.\n", dev->minor);
1754 static int usbduxfast_detach(comedi_device * dev)
1756 usbduxfastsub_t *usbduxfastsub_tmp;
1758 #ifdef CONFIG_COMEDI_DEBUG
1759 printk("comedi%d: usbduxfast: detach usb device\n", dev->minor);
1763 printk("comedi?: usbduxfast: detach without dev variable...\n");
1767 usbduxfastsub_tmp = dev->private;
1768 if (!usbduxfastsub_tmp) {
1769 printk("comedi?: usbduxfast: detach without ptr to usbduxfastsub[]\n");
1773 mutex_lock(&usbduxfastsub_tmp->mutex);
1774 mutex_lock(&start_stop_mutex);
1775 // Don't allow detach to free the private structure
1776 // It's one entry of of usbduxfastsub[]
1777 dev->private = NULL;
1778 usbduxfastsub_tmp->attached = 0;
1779 usbduxfastsub_tmp->comedidev = NULL;
1780 #ifdef CONFIG_COMEDI_DEBUG
1781 printk("comedi%d: usbduxfast: detach: successfully removed\n",
1784 mutex_unlock(&start_stop_mutex);
1785 mutex_unlock(&usbduxfastsub_tmp->mutex);
1789 /* main driver struct */
1790 static comedi_driver driver_usbduxfast = {
1791 driver_name:"usbduxfast",
1793 attach:usbduxfast_attach,
1794 detach:usbduxfast_detach,
1797 static void init_usb_devices(void)
1800 #ifdef CONFIG_COMEDI_DEBUG
1801 printk("comedi_: usbduxfast: setting all possible devs to invalid\n");
1803 // all devices entries are invalid to begin with
1804 // they will become valid by the probe function
1805 // and then finally by the attach-function
1806 for (index = 0; index < NUMUSBDUXFAST; index++) {
1807 memset(&(usbduxfastsub[index]), 0x00,
1808 sizeof(usbduxfastsub[index]));
1809 mutex_init(&(usbduxfastsub[index].mutex));
1813 static void uninit_usb_devices(void)
1817 for (index = 0; index < NUMUSBDUXFAST; index++) {
1818 mutex_destroy(&(usbduxfastsub[index].mutex));
1822 // Table with the USB-devices: just now only testing IDs
1823 static struct usb_device_id usbduxfastsub_table[] = {
1824 // { USB_DEVICE(0x4b4, 0x8613), //testing
1826 {USB_DEVICE(0x13d8, 0x0010) //real ID
1828 {USB_DEVICE(0x13d8, 0x0011) //real ID
1830 {} /* Terminating entry */
1833 MODULE_DEVICE_TABLE(usb, usbduxfastsub_table);
1835 // The usbduxfastsub-driver
1836 static struct usb_driver usbduxfastsub_driver = {
1837 #ifdef COMEDI_HAVE_USB_DRIVER_OWNER
1841 probe:usbduxfastsub_probe,
1842 disconnect:usbduxfastsub_disconnect,
1843 id_table:usbduxfastsub_table,
1846 // Can't use the nice macro as I have also to initialise the USB
1848 // registering the usb-system _and_ the comedi-driver
1849 static int init_usbduxfast(void)
1851 printk(KERN_INFO KBUILD_MODNAME ": "
1852 DRIVER_VERSION ":" DRIVER_DESC "\n");
1854 usb_register(&usbduxfastsub_driver);
1855 comedi_driver_register(&driver_usbduxfast);
1859 // deregistering the comedi driver and the usb-subsystem
1860 static void exit_usbduxfast(void)
1862 comedi_driver_unregister(&driver_usbduxfast);
1863 usb_deregister(&usbduxfastsub_driver);
1864 uninit_usb_devices();
1867 module_init(init_usbduxfast);
1868 module_exit(exit_usbduxfast);
1870 MODULE_AUTHOR(DRIVER_AUTHOR);
1871 MODULE_DESCRIPTION(DRIVER_DESC);
1872 MODULE_LICENSE("GPL");