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: ITL USB-DUXfast
26 Devices: [ITL] USB-DUX (usbduxfast.o)
27 Author: Bernd Porr <BerndPorr@f2s.com>
33 * I must give credit here to Chris Baugher who
34 * wrote the driver for AT-MIO-16d. I used some parts of this
35 * driver. I also must give credits to David Brownell
36 * who supported me with the USB development.
42 * 0.9: Dropping the first data packet which seems to be from the last transfer.
43 * Buffer overflows in the FX2 are handed over to comedi.
44 * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
45 * Added insn command basically for testing. Sample rate is 1MHz/16ch=62.5kHz
46 * 0.99: Ian Abbott pointed out a bug which has been corrected. Thanks!
47 * 0.99a: added external trigger.
48 * 1.00: added firmware kernel request to the driver which fixed
49 * udev coldplug problem
52 #include <linux/kernel.h>
53 #include <linux/firmware.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/input.h>
58 #include <linux/usb.h>
59 #include <linux/fcntl.h>
60 #include <linux/compiler.h>
61 #include "comedi_fc.h"
62 #include <linux/comedidev.h>
63 #include <linux/usb.h>
65 // (un)comment this if you want to have debug info.
66 //#define CONFIG_COMEDI_DEBUG
67 #undef CONFIG_COMEDI_DEBUG
69 #define BOARDNAME "usbduxfast"
71 // timeout for the USB-transfer
72 #define BULK_TIMEOUT 1000
74 // constants for "firmware" upload and download
75 #define USBDUXFASTSUB_FIRMWARE 0xA0
76 #define VENDOR_DIR_IN 0xC0
77 #define VENDOR_DIR_OUT 0x40
79 // internal adresses of the 8051 processor
80 #define USBDUXFASTSUB_CPUCS 0xE600
82 // max lenghth of the transfer-buffer for software upload
85 // Input endpoint number
88 // Endpoint for the A/D channellist: bulk OUT
89 #define CHANNELLISTEP 4
92 #define NUMCHANNELS 32
94 // size of the waveform descriptor
97 // Size of one A/D value
98 #define SIZEADIN ((sizeof(int16_t)))
100 // Size of the input-buffer IN BYTES
101 #define SIZEINBUF 512
104 #define SIZEINSNBUF 512
106 // Size of the buffer for the dux commands
107 #define SIZEOFDUXBUFFER 256 // bytes
109 // Number of in-URBs which receive the data: min=5
110 #define NUMOFINBUFFERSHIGH 10
112 // Total number of usbduxfast devices
113 #define NUMUSBDUXFAST 16
115 // Number of subdevices
116 #define N_SUBDEVICES 1
118 // Analogue in subdevice
121 // min delay steps for more than one channel
122 // basically when the mux gives up. ;-)
123 #define MIN_SAMPLING_PERIOD 9 // steps at 30MHz in the FX2
125 // Max number of 1/30MHz delay steps:
126 #define MAX_SAMPLING_PERIOD 500
128 // Number of received packets to ignore before we start handing data over to comedi.
129 // It's quad buffering and we have to ignore 4 packets.
130 #define PACKETS_TO_IGNORE 4
132 /////////////////////////////////////////////
134 static const comedi_lrange range_usbduxfast_ai_range = { 2, {
141 * private structure of one subdevice
144 // This is the structure which holds all the data of this driver
145 // one sub device just now: A/D
149 // is it associated with a subdevice?
151 // pointer to the usb-device
152 struct usb_device *usbdev;
153 // BULK-transfer handling: urb
155 int8_t *transfer_buffer;
156 // input buffer for single insn
160 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
161 // interface structure in 2.6
162 struct usb_interface *interface;
164 // comedi device for the interrupt context
165 comedi_device *comedidev;
166 // asynchronous command is running
167 short int ai_cmd_running;
168 // continous aquisition
169 short int ai_continous;
170 // number of samples to aquire
171 long int ai_sample_count;
173 uint8_t *dux_commands;
174 // counter which ignores the first buffers
179 // The pointer to the private usb-data of the driver
180 // is also the private data for the comedi-device.
181 // This has to be global as the usb subsystem needs
182 // global variables. The other reason is that this
183 // structure must be there _before_ any comedi
184 // command is issued. The usb subsystem must be
185 // initialised before comedi can access it.
186 static usbduxfastsub_t usbduxfastsub[NUMUSBDUXFAST];
188 static DEFINE_MUTEX(start_stop_mutex);
190 // bulk transfers to usbduxfast
192 #define SENDADCOMMANDS 0
193 #define SENDINITEP6 1
195 static int send_dux_commands(usbduxfastsub_t * this_usbduxfastsub, int cmd_type)
198 this_usbduxfastsub->dux_commands[0] = cmd_type;
199 #ifdef CONFIG_COMEDI_DEBUG
201 printk("comedi%d: usbduxfast: dux_commands: ",
202 this_usbduxfastsub->comedidev->minor);
203 for (i = 0; i < SIZEOFDUXBUFFER; i++) {
204 printk(" %02x", this_usbduxfastsub->dux_commands[i]);
208 result = USB_BULK_MSG(this_usbduxfastsub->usbdev,
209 usb_sndbulkpipe(this_usbduxfastsub->usbdev,
211 this_usbduxfastsub->dux_commands,
212 SIZEOFDUXBUFFER, &nsent, BULK_TIMEOUT);
214 printk("comedi%d: could not transmit dux_commands to the usb-device, err=%d\n", this_usbduxfastsub->comedidev->minor, result);
219 // Stops the data acquision
220 // It should be safe to call this function from any context
221 static int usbduxfastsub_unlink_InURBs(usbduxfastsub_t * usbduxfastsub_tmp)
226 if (usbduxfastsub_tmp && usbduxfastsub_tmp->urbIn) {
227 usbduxfastsub_tmp->ai_cmd_running = 0;
228 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
229 j = usb_unlink_urb(usbduxfastsub_tmp->urbIn);
234 // waits until a running transfer is over
235 usb_kill_urb(usbduxfastsub_tmp->urbIn);
239 #ifdef CONFIG_COMEDI_DEBUG
240 printk("comedi: usbduxfast: unlinked InURB: res=%d\n", j);
245 /* This will stop a running acquisition operation */
246 // Is called from within this driver from both the
247 // interrupt context and from comedi
248 static int usbduxfast_ai_stop(usbduxfastsub_t * this_usbduxfastsub,
253 if (!this_usbduxfastsub) {
254 printk("comedi?: usbduxfast_ai_stop: this_usbduxfastsub=NULL!\n");
257 #ifdef CONFIG_COMEDI_DEBUG
258 printk("comedi: usbduxfast_ai_stop\n");
261 this_usbduxfastsub->ai_cmd_running = 0;
265 ret = usbduxfastsub_unlink_InURBs(this_usbduxfastsub);
271 // This will cancel a running acquisition operation.
272 // This is called by comedi but never from inside the
274 static int usbduxfast_ai_cancel(comedi_device * dev, comedi_subdevice * s)
276 usbduxfastsub_t *this_usbduxfastsub;
279 // force unlink of all urbs
280 #ifdef CONFIG_COMEDI_DEBUG
281 printk("comedi: usbduxfast_ai_cancel\n");
283 this_usbduxfastsub = dev->private;
284 if (!this_usbduxfastsub) {
285 printk("comedi: usbduxfast_ai_cancel: this_usbduxfastsub=NULL\n");
288 mutex_lock(&this_usbduxfastsub->mutex);
289 if (!(this_usbduxfastsub->probed)) {
290 mutex_unlock(&this_usbduxfastsub->mutex);
294 res = usbduxfast_ai_stop(this_usbduxfastsub, 1);
295 mutex_unlock(&this_usbduxfastsub->mutex);
301 // interrupt service routine
302 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
303 static void usbduxfastsub_ai_Irq(struct urb *urb)
305 static void usbduxfastsub_ai_Irq(struct urb *urb PT_REGS_ARG)
309 usbduxfastsub_t *this_usbduxfastsub;
310 comedi_device *this_comedidev;
317 printk("comedi_: usbduxfast_: ao int-handler called with urb=NULL!\n");
320 // the context variable points to the subdevice
321 this_comedidev = urb->context;
322 if (!this_comedidev) {
323 printk("comedi_: usbduxfast_: urb context is a NULL pointer!\n");
326 // the private structure of the subdevice is usbduxfastsub_t
327 this_usbduxfastsub = this_comedidev->private;
328 if (!this_usbduxfastsub) {
329 printk("comedi_: usbduxfast_: private of comedi subdev is a NULL pointer!\n");
332 // are we running a command?
333 if (unlikely(!(this_usbduxfastsub->ai_cmd_running))) {
334 // not running a command
335 // do not continue execution if no asynchronous command is running
336 // in particular not resubmit
340 if (unlikely(!(this_usbduxfastsub->attached))) {
341 // no comedi device there
344 // subdevice which is the AD converter
345 s = this_comedidev->subdevices + SUBDEV_AD;
347 // first we test if something unusual has just happened
348 switch (urb->status) {
352 // happens after an unlink command or when the device is plugged out
358 s->async->events |= COMEDI_CB_EOA;
359 s->async->events |= COMEDI_CB_ERROR;
360 comedi_event(this_usbduxfastsub->comedidev, s);
361 // stop the transfer w/o unlink
362 usbduxfast_ai_stop(this_usbduxfastsub, 0);
366 printk("comedi%d: usbduxfast: non-zero urb status received in ai intr context: %d\n", this_usbduxfastsub->comedidev->minor, urb->status);
367 s->async->events |= COMEDI_CB_EOA;
368 s->async->events |= COMEDI_CB_ERROR;
369 comedi_event(this_usbduxfastsub->comedidev, s);
370 usbduxfast_ai_stop(this_usbduxfastsub, 0);
374 p = urb->transfer_buffer;
375 if (!this_usbduxfastsub->ignore) {
376 if (!(this_usbduxfastsub->ai_continous)) {
377 // not continous, fixed number of samples
378 n = urb->actual_length / sizeof(uint16_t);
379 if (unlikely(this_usbduxfastsub->ai_sample_count < n)) {
380 // we have send only a fraction of the bytes received
381 cfc_write_array_to_buffer(s,
382 urb->transfer_buffer,
383 this_usbduxfastsub->ai_sample_count *
385 usbduxfast_ai_stop(this_usbduxfastsub, 0);
386 // say comedi that the acquistion is over
387 s->async->events |= COMEDI_CB_EOA;
388 comedi_event(this_usbduxfastsub->comedidev, s);
391 this_usbduxfastsub->ai_sample_count -= n;
393 // write the full buffer to comedi
394 err = cfc_write_array_to_buffer(s,
395 urb->transfer_buffer, urb->actual_length);
397 if (unlikely(err == 0)) {
398 /* buffer overflow */
399 usbduxfast_ai_stop(this_usbduxfastsub, 0);
403 // tell comedi that data is there
404 comedi_event(this_usbduxfastsub->comedidev, s);
407 // ignore this packet
408 this_usbduxfastsub->ignore--;
411 // command is still running
412 // resubmit urb for BULK transfer
413 urb->dev = this_usbduxfastsub->usbdev;
415 if ((err = USB_SUBMIT_URB(urb)) < 0) {
416 printk("comedi%d: usbduxfast: urb resubm failed: %d",
417 this_usbduxfastsub->comedidev->minor, err);
418 s->async->events |= COMEDI_CB_EOA;
419 s->async->events |= COMEDI_CB_ERROR;
420 comedi_event(this_usbduxfastsub->comedidev, s);
421 usbduxfast_ai_stop(this_usbduxfastsub, 0);
425 static int usbduxfastsub_start(usbduxfastsub_t * usbduxfastsub)
428 unsigned char local_transfer_buffer[16];
431 local_transfer_buffer[0] = 0;
432 errcode = USB_CONTROL_MSG(usbduxfastsub->usbdev,
433 // create a pipe for a control transfer
434 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
435 // bRequest, "Firmware"
436 USBDUXFASTSUB_FIRMWARE,
443 // address of the transfer buffer
444 local_transfer_buffer,
450 printk("comedi_: usbduxfast_: control msg failed (start)\n");
456 static int usbduxfastsub_stop(usbduxfastsub_t * usbduxfastsub)
460 unsigned char local_transfer_buffer[16];
462 local_transfer_buffer[0] = 1;
463 errcode = USB_CONTROL_MSG
464 (usbduxfastsub->usbdev,
465 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
466 // bRequest, "Firmware"
467 USBDUXFASTSUB_FIRMWARE,
473 0x0000, local_transfer_buffer,
479 printk("comedi_: usbduxfast: control msg failed (stop)\n");
485 static int usbduxfastsub_upload(usbduxfastsub_t * usbduxfastsub,
486 unsigned char *local_transfer_buffer,
487 unsigned int startAddr, unsigned int len)
491 errcode = USB_CONTROL_MSG
492 (usbduxfastsub->usbdev,
493 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
494 // brequest, firmware
495 USBDUXFASTSUB_FIRMWARE,
502 // our local safe buffer
503 local_transfer_buffer,
509 printk("comedi_: usbduxfast: uppload failed\n");
515 int firmwareUpload(usbduxfastsub_t * usbduxfastsub,
516 unsigned char *firmwareBinary, int sizeFirmware)
520 if (!firmwareBinary) {
523 ret = usbduxfastsub_stop(usbduxfastsub);
525 printk("comedi_: usbduxfast: can not stop firmware\n");
528 ret = usbduxfastsub_upload(usbduxfastsub,
529 firmwareBinary, 0, sizeFirmware);
531 printk("comedi_: usbduxfast: firmware upload failed\n");
534 ret = usbduxfastsub_start(usbduxfastsub);
536 printk("comedi_: usbduxfast: can not start firmware\n");
542 int usbduxfastsub_submit_InURBs(usbduxfastsub_t * usbduxfastsub)
546 if (!usbduxfastsub) {
549 usb_fill_bulk_urb(usbduxfastsub->urbIn,
550 usbduxfastsub->usbdev,
551 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
552 usbduxfastsub->transfer_buffer,
553 SIZEINBUF, usbduxfastsub_ai_Irq, usbduxfastsub->comedidev);
555 #ifdef CONFIG_COMEDI_DEBUG
556 printk("comedi%d: usbduxfast: submitting in-urb: %x,%x\n",
557 usbduxfastsub->comedidev->minor,
558 (int)(usbduxfastsub->urbIn->context),
559 (int)(usbduxfastsub->urbIn->dev));
561 errFlag = USB_SUBMIT_URB(usbduxfastsub->urbIn);
563 printk("comedi_: usbduxfast: ai: ");
564 printk("USB_SUBMIT_URB");
565 printk(" error %d\n", errFlag);
571 static int usbduxfast_ai_cmdtest(comedi_device * dev,
572 comedi_subdevice * s, comedi_cmd * cmd)
574 int err = 0, stop_mask = 0;
575 long int steps, tmp = 0;
577 usbduxfastsub_t *this_usbduxfastsub = dev->private;
578 if (!(this_usbduxfastsub->probed)) {
581 #ifdef CONFIG_COMEDI_DEBUG
582 printk("comedi%d: usbduxfast_ai_cmdtest\n", dev->minor);
583 printk("comedi%d: usbduxfast: convert_arg=%u scan_begin_arg=%u\n",
584 dev->minor, cmd->convert_arg, cmd->scan_begin_arg);
586 /* step 1: make sure trigger sources are trivially valid */
588 tmp = cmd->start_src;
589 cmd->start_src &= TRIG_NOW | TRIG_EXT | TRIG_INT;
590 if (!cmd->start_src || tmp != cmd->start_src)
593 tmp = cmd->scan_begin_src;
594 cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT;
595 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
598 tmp = cmd->convert_src;
599 cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
600 if (!cmd->convert_src || tmp != cmd->convert_src)
603 tmp = cmd->scan_end_src;
604 cmd->scan_end_src &= TRIG_COUNT;
605 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
609 stop_mask = TRIG_COUNT | TRIG_NONE;
610 cmd->stop_src &= stop_mask;
611 if (!cmd->stop_src || tmp != cmd->stop_src)
617 /* step 2: make sure trigger sources are unique and mutually compatible */
619 if (cmd->start_src != TRIG_NOW &&
620 cmd->start_src != TRIG_EXT && cmd->start_src != TRIG_INT)
622 if (cmd->scan_begin_src != TRIG_TIMER &&
623 cmd->scan_begin_src != TRIG_FOLLOW &&
624 cmd->scan_begin_src != TRIG_EXT)
626 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
628 if (cmd->stop_src != TRIG_COUNT &&
629 cmd->stop_src != TRIG_EXT && cmd->stop_src != TRIG_NONE)
632 // can't have external stop and start triggers at once
633 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
639 /* step 3: make sure arguments are trivially compatible */
641 if (cmd->start_src == TRIG_NOW && cmd->start_arg != 0) {
646 if (!cmd->chanlist_len) {
649 if (cmd->scan_end_arg != cmd->chanlist_len) {
650 cmd->scan_end_arg = cmd->chanlist_len;
654 if (cmd->chanlist_len == 1) {
657 minSamplPer = MIN_SAMPLING_PERIOD;
660 if (cmd->convert_src == TRIG_TIMER) {
661 steps = cmd->convert_arg * 30;
662 if (steps < (minSamplPer * 1000)) {
663 steps = minSamplPer * 1000;
665 if (steps > (MAX_SAMPLING_PERIOD * 1000)) {
666 steps = MAX_SAMPLING_PERIOD * 1000;
670 if (cmd->convert_arg != tmp) {
671 cmd->convert_arg = tmp;
676 if (cmd->scan_begin_src == TRIG_TIMER) {
680 switch (cmd->stop_src) {
682 if (!cmd->stop_arg) {
688 if (cmd->stop_arg != 0) {
693 // TRIG_EXT doesn't care since it doesn't trigger off a numbered channel
701 /* step 4: fix up any arguments */
707 static int usbduxfast_ai_inttrig(comedi_device * dev,
708 comedi_subdevice * s, unsigned int trignum)
711 usbduxfastsub_t *this_usbduxfastsub = dev->private;
712 if (!this_usbduxfastsub) {
715 mutex_lock(&this_usbduxfastsub->mutex);
716 if (!(this_usbduxfastsub->probed)) {
717 mutex_unlock(&this_usbduxfastsub->mutex);
720 #ifdef CONFIG_COMEDI_DEBUG
721 printk("comedi%d: usbduxfast_ai_inttrig\n", dev->minor);
725 printk("comedi%d: usbduxfast_ai_inttrig: invalid trignum\n",
727 mutex_unlock(&this_usbduxfastsub->mutex);
730 if (!(this_usbduxfastsub->ai_cmd_running)) {
731 this_usbduxfastsub->ai_cmd_running = 1;
732 ret = usbduxfastsub_submit_InURBs(this_usbduxfastsub);
734 printk("comedi%d: usbduxfast_ai_inttrig: urbSubmit: err=%d\n", dev->minor, ret);
735 this_usbduxfastsub->ai_cmd_running = 0;
736 mutex_unlock(&this_usbduxfastsub->mutex);
739 s->async->inttrig = NULL;
741 printk("comedi%d: ai_inttrig but acqu is already running\n",
744 mutex_unlock(&this_usbduxfastsub->mutex);
748 // offsets for the GPIF bytes
749 // the first byte is the command byte
750 #define LENBASE 1+0x00
751 #define OPBASE 1+0x08
752 #define OUTBASE 1+0x10
753 #define LOGBASE 1+0x18
755 static int usbduxfast_ai_cmd(comedi_device * dev, comedi_subdevice * s)
757 comedi_cmd *cmd = &s->async->cmd;
758 unsigned int chan, gain, rngmask = 0xff;
760 usbduxfastsub_t *this_usbduxfastsub = dev->private;
762 long steps, steps_tmp;
764 #ifdef CONFIG_COMEDI_DEBUG
765 printk("comedi%d: usbduxfast_ai_cmd\n", dev->minor);
767 if (!this_usbduxfastsub) {
770 mutex_lock(&this_usbduxfastsub->mutex);
771 if (!(this_usbduxfastsub->probed)) {
772 mutex_unlock(&this_usbduxfastsub->mutex);
775 if (this_usbduxfastsub->ai_cmd_running) {
776 printk("comedi%d: ai_cmd not possible. Another ai_cmd is running.\n", dev->minor);
777 mutex_unlock(&this_usbduxfastsub->mutex);
780 // set current channel of the running aquisition to zero
781 s->async->cur_chan = 0;
783 // ignore the first buffers from the device if there is an error condition
784 this_usbduxfastsub->ignore = PACKETS_TO_IGNORE;
786 if (cmd->chanlist_len > 0) {
787 gain = CR_RANGE(cmd->chanlist[0]);
788 for (i = 0; i < cmd->chanlist_len; ++i) {
789 chan = CR_CHAN(cmd->chanlist[i]);
791 printk("comedi%d: cmd is accepting only consecutive channels.\n", dev->minor);
792 mutex_unlock(&this_usbduxfastsub->mutex);
795 if ((gain != CR_RANGE(cmd->chanlist[i]))
796 && (cmd->chanlist_len > 3)) {
797 printk("comedi%d: the gain must be the same for all channels.\n", dev->minor);
798 mutex_unlock(&this_usbduxfastsub->mutex);
801 if (i >= NUMCHANNELS) {
802 printk("comedi%d: channel list too long\n",
809 if (cmd->scan_begin_src == TRIG_TIMER) {
810 printk("comedi%d: usbduxfast: scan_begin_src==TRIG_TIMER not valid.\n", dev->minor);
811 mutex_unlock(&this_usbduxfastsub->mutex);
814 if (cmd->convert_src == TRIG_TIMER) {
815 steps = (cmd->convert_arg * 30) / 1000;
817 if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
818 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);
819 mutex_unlock(&this_usbduxfastsub->mutex);
822 if (steps > MAX_SAMPLING_PERIOD) {
823 printk("comedi%d: usbduxfast: ai_cmd: sampling rate too low.\n",
825 mutex_unlock(&this_usbduxfastsub->mutex);
828 if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
829 && (cmd->chanlist_len != 16)) {
830 printk("comedi%d: usbduxfast: ai_cmd: TRIG_EXT only with 1 or 16 channels possible.\n", dev->minor);
831 mutex_unlock(&this_usbduxfastsub->mutex);
834 #ifdef CONFIG_COMEDI_DEBUG
835 printk("comedi%d: usbduxfast: steps=%ld, convert_arg=%u, ai_timer=%u\n",
837 steps, cmd->convert_arg, this_usbduxfastsub->ai_timer);
840 switch (cmd->chanlist_len) {
843 if (CR_RANGE(cmd->chanlist[0]) > 0)
844 rngmask = 0xff - 0x04;
848 // for external trigger: looping in this state until the RDY0 pin
850 if (cmd->start_src == TRIG_EXT) { // we loop here until ready has been set
851 this_usbduxfastsub->dux_commands[LENBASE + 0] = 0x01; // branch back to state 0
852 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x01; // deceision state w/o data
853 this_usbduxfastsub->dux_commands[OUTBASE + 0] =
855 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0x00; // RDY0 = 0
856 } else { // we just proceed to state 1
857 this_usbduxfastsub->dux_commands[LENBASE + 0] = 1;
858 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0;
859 this_usbduxfastsub->dux_commands[OUTBASE + 0] =
861 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
864 if (steps < MIN_SAMPLING_PERIOD) {
865 // for fast single channel aqu without mux
867 // we just stay here at state 1 and rexecute the same state
868 // this gives us 30MHz sampling rate
869 this_usbduxfastsub->dux_commands[LENBASE + 1] = 0x89; // branch back to state 1
870 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x03; // deceision state with data
871 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
873 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0xFF; // doesn't matter
875 // we loop through two states: data and delay: max rate is 15Mhz
876 this_usbduxfastsub->dux_commands[LENBASE + 1] =
878 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x02; // data
879 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
881 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0; // doesn't matter
883 this_usbduxfastsub->dux_commands[LENBASE + 2] = 0x09; // branch back to state 1
884 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0x01; // deceision state w/o data
885 this_usbduxfastsub->dux_commands[OUTBASE + 2] =
887 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0xFF; // doesn't matter
890 // we loop through 3 states: 2x delay and 1x data. This gives a min
891 // sampling rate of 60kHz.
893 // we have 1 state with duration 1
896 // do the first part of the delay
897 this_usbduxfastsub->dux_commands[LENBASE + 1] =
899 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0;
900 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
902 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
904 // and the second part
905 this_usbduxfastsub->dux_commands[LENBASE + 2] =
907 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
908 this_usbduxfastsub->dux_commands[OUTBASE + 2] =
910 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
912 // get the data and branch back
913 this_usbduxfastsub->dux_commands[LENBASE + 3] = 0x09; // branch back to state 1
914 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0x03; // deceision state w data
915 this_usbduxfastsub->dux_commands[OUTBASE + 3] =
917 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0xFF; // doesn't matter
923 // commit data to the FIFO
924 if (CR_RANGE(cmd->chanlist[0]) > 0)
925 rngmask = 0xff - 0x04;
928 this_usbduxfastsub->dux_commands[LENBASE + 0] = 1;
929 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x02; // data
930 this_usbduxfastsub->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
931 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
933 // we have 1 state with duration 1: state 0
934 steps_tmp = steps - 1;
936 if (CR_RANGE(cmd->chanlist[1]) > 0)
937 rngmask = 0xff - 0x04;
940 // do the first part of the delay
941 this_usbduxfastsub->dux_commands[LENBASE + 1] = steps_tmp / 2;
942 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0;
943 this_usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFE & rngmask; //count
944 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
946 // and the second part
947 this_usbduxfastsub->dux_commands[LENBASE + 2] =
948 steps_tmp - steps_tmp / 2;
949 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
950 this_usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
951 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
953 this_usbduxfastsub->dux_commands[LENBASE + 3] = 1;
954 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0x02; // data
955 this_usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
956 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
958 // we have 2 states with duration 1: step 6 and the IDLE state
959 steps_tmp = steps - 2;
961 if (CR_RANGE(cmd->chanlist[0]) > 0)
962 rngmask = 0xff - 0x04;
965 // do the first part of the delay
966 this_usbduxfastsub->dux_commands[LENBASE + 4] = steps_tmp / 2;
967 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0;
968 this_usbduxfastsub->dux_commands[OUTBASE + 4] = (0xFF - 0x02) & rngmask; //reset
969 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
971 // and the second part
972 this_usbduxfastsub->dux_commands[LENBASE + 5] =
973 steps_tmp - steps_tmp / 2;
974 this_usbduxfastsub->dux_commands[OPBASE + 5] = 0;
975 this_usbduxfastsub->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
976 this_usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
978 this_usbduxfastsub->dux_commands[LENBASE + 6] = 1;
979 this_usbduxfastsub->dux_commands[OPBASE + 6] = 0;
980 this_usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
981 this_usbduxfastsub->dux_commands[LOGBASE + 6] = 0;
986 for (j = 0; j < 1; j++) {
987 if (CR_RANGE(cmd->chanlist[j]) > 0)
988 rngmask = 0xff - 0x04;
991 // commit data to the FIFO and do the first part of the delay
992 this_usbduxfastsub->dux_commands[LENBASE + j * 2] =
994 this_usbduxfastsub->dux_commands[OPBASE + j * 2] = 0x02; // data
995 this_usbduxfastsub->dux_commands[OUTBASE + j * 2] = 0xFF & rngmask; // no change
996 this_usbduxfastsub->dux_commands[LOGBASE + j * 2] = 0;
998 if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
999 rngmask = 0xff - 0x04;
1002 // do the second part of the delay
1003 this_usbduxfastsub->dux_commands[LENBASE + j * 2 + 1] =
1005 this_usbduxfastsub->dux_commands[OPBASE + j * 2 + 1] = 0; // no data
1006 this_usbduxfastsub->dux_commands[OUTBASE + j * 2 + 1] = 0xFE & rngmask; //count
1007 this_usbduxfastsub->dux_commands[LOGBASE + j * 2 + 1] =
1011 // 2 steps with duration 1: the idele step and step 6:
1012 steps_tmp = steps - 2;
1013 // commit data to the FIFO and do the first part of the delay
1014 this_usbduxfastsub->dux_commands[LENBASE + 4] = steps_tmp / 2;
1015 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0x02; // data
1016 this_usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFF & rngmask; // no change
1017 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1019 if (CR_RANGE(cmd->chanlist[0]) > 0)
1020 rngmask = 0xff - 0x04;
1023 // do the second part of the delay
1024 this_usbduxfastsub->dux_commands[LENBASE + 5] =
1025 steps_tmp - steps_tmp / 2;
1026 this_usbduxfastsub->dux_commands[OPBASE + 5] = 0; // no data
1027 this_usbduxfastsub->dux_commands[OUTBASE + 5] = (0xFF - 0x02) & rngmask; // reset
1028 this_usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1030 this_usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1031 this_usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1032 this_usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1033 this_usbduxfastsub->dux_commands[LOGBASE + 6] = 0;
1036 if (CR_RANGE(cmd->chanlist[0]) > 0)
1037 rngmask = 0xff - 0x04;
1040 if (cmd->start_src == TRIG_EXT) { // we loop here until ready has been set
1041 this_usbduxfastsub->dux_commands[LENBASE + 0] = 0x01; // branch back to state 0
1042 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x01; // deceision state w/o data
1043 this_usbduxfastsub->dux_commands[OUTBASE + 0] = (0xFF - 0x02) & rngmask; // reset
1044 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0x00; // RDY0 = 0
1045 } else { // we just proceed to state 1
1046 this_usbduxfastsub->dux_commands[LENBASE + 0] = 255; // 30us reset pulse
1047 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0;
1048 this_usbduxfastsub->dux_commands[OUTBASE + 0] = (0xFF - 0x02) & rngmask; // reset
1049 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1052 // commit data to the FIFO
1053 this_usbduxfastsub->dux_commands[LENBASE + 1] = 1;
1054 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x02; // data
1055 this_usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
1056 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
1058 // we have 2 states with duration 1
1061 // do the first part of the delay
1062 this_usbduxfastsub->dux_commands[LENBASE + 2] = steps / 2;
1063 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
1064 this_usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1065 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
1067 // and the second part
1068 this_usbduxfastsub->dux_commands[LENBASE + 3] =
1070 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0;
1071 this_usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
1072 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
1074 this_usbduxfastsub->dux_commands[LENBASE + 4] = 0x09; // branch back to state 1
1075 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0x01; // deceision state w/o data
1076 this_usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1077 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0xFF; // doesn't matter
1082 printk("comedi %d: unsupported combination of channels\n",
1084 mutex_unlock(&this_usbduxfastsub->mutex);
1088 #ifdef CONFIG_COMEDI_DEBUG
1089 printk("comedi %d: sending commands to the usb device\n", dev->minor);
1091 // 0 means that the AD commands are sent
1092 result = send_dux_commands(this_usbduxfastsub, SENDADCOMMANDS);
1094 printk("comedi%d: adc command could not be submitted. Aborting...\n", dev->minor);
1095 mutex_unlock(&this_usbduxfastsub->mutex);
1098 if (cmd->stop_src == TRIG_COUNT) {
1099 this_usbduxfastsub->ai_sample_count =
1100 (cmd->stop_arg) * (cmd->scan_end_arg);
1101 if (usbduxfastsub->ai_sample_count < 1) {
1102 printk("comedi%d: (cmd->stop_arg)*(cmd->scan_end_arg)<1, aborting.\n", dev->minor);
1103 mutex_unlock(&this_usbduxfastsub->mutex);
1106 this_usbduxfastsub->ai_continous = 0;
1108 // continous aquisition
1109 this_usbduxfastsub->ai_continous = 1;
1110 this_usbduxfastsub->ai_sample_count = 0;
1113 if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
1114 // enable this acquisition operation
1115 this_usbduxfastsub->ai_cmd_running = 1;
1116 ret = usbduxfastsub_submit_InURBs(this_usbduxfastsub);
1118 this_usbduxfastsub->ai_cmd_running = 0;
1119 // fixme: unlink here??
1120 mutex_unlock(&this_usbduxfastsub->mutex);
1123 s->async->inttrig = NULL;
1126 // don't enable the acquision operation
1127 // wait for an internal signal
1128 s->async->inttrig = usbduxfast_ai_inttrig;
1130 mutex_unlock(&this_usbduxfastsub->mutex);
1135 /* Mode 0 is used to get a single conversion on demand */
1136 static int usbduxfast_ai_insn_read(comedi_device * dev,
1137 comedi_subdevice * s, comedi_insn * insn, lsampl_t * data)
1139 int i, j, n, actual_length;
1140 int chan, range, rngmask;
1142 usbduxfastsub_t *usbduxfastsub = dev->private;
1144 if (!usbduxfastsub) {
1145 printk("comedi%d: ai_insn_read: no usb dev.\n", dev->minor);
1148 #ifdef CONFIG_COMEDI_DEBUG
1149 printk("comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1150 dev->minor, insn->n, insn->subdev);
1152 mutex_lock(&usbduxfastsub->mutex);
1153 if (!(usbduxfastsub->probed)) {
1154 mutex_unlock(&usbduxfastsub->mutex);
1157 if (usbduxfastsub->ai_cmd_running) {
1158 printk("comedi%d: ai_insn_read not possible. Async Command is running.\n", dev->minor);
1159 mutex_unlock(&usbduxfastsub->mutex);
1162 // sample one channel
1163 chan = CR_CHAN(insn->chanspec);
1164 range = CR_RANGE(insn->chanspec);
1165 // set command for the first channel
1168 rngmask = 0xff - 0x04;
1171 // commit data to the FIFO
1172 usbduxfastsub->dux_commands[LENBASE + 0] = 1;
1173 usbduxfastsub->dux_commands[OPBASE + 0] = 0x02; // data
1174 usbduxfastsub->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
1175 usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1177 // do the first part of the delay
1178 usbduxfastsub->dux_commands[LENBASE + 1] = 12;
1179 usbduxfastsub->dux_commands[OPBASE + 1] = 0;
1180 usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
1181 usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
1183 usbduxfastsub->dux_commands[LENBASE + 2] = 1;
1184 usbduxfastsub->dux_commands[OPBASE + 2] = 0;
1185 usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1186 usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
1188 usbduxfastsub->dux_commands[LENBASE + 3] = 1;
1189 usbduxfastsub->dux_commands[OPBASE + 3] = 0;
1190 usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFE & rngmask;
1191 usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
1193 usbduxfastsub->dux_commands[LENBASE + 4] = 1;
1194 usbduxfastsub->dux_commands[OPBASE + 4] = 0;
1195 usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFE & rngmask;
1196 usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1199 usbduxfastsub->dux_commands[LENBASE + 5] = 12;
1200 usbduxfastsub->dux_commands[OPBASE + 5] = 0;
1201 usbduxfastsub->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1202 usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1204 usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1205 usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1206 usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1207 usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1209 #ifdef CONFIG_COMEDI_DEBUG
1210 printk("comedi %d: sending commands to the usb device\n", dev->minor);
1212 // 0 means that the AD commands are sent
1213 err = send_dux_commands(usbduxfastsub, SENDADCOMMANDS);
1215 printk("comedi%d: adc command could not be submitted. Aborting...\n", dev->minor);
1216 mutex_unlock(&usbduxfastsub->mutex);
1219 #ifdef CONFIG_COMEDI_DEBUG
1220 printk("comedi%d: usbduxfast: submitting in-urb: %x,%x\n",
1221 usbduxfastsub->comedidev->minor,
1222 (int)(usbduxfastsub->urbIn->context),
1223 (int)(usbduxfastsub->urbIn->dev));
1225 for (i = 0; i < PACKETS_TO_IGNORE; i++) {
1226 err = USB_BULK_MSG(usbduxfastsub->usbdev,
1227 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
1228 usbduxfastsub->transfer_buffer,
1229 SIZEINBUF, &actual_length, BULK_TIMEOUT);
1231 printk("comedi%d: insn timeout. No data.\n",
1233 mutex_unlock(&usbduxfastsub->mutex);
1238 for (i = 0; i < insn->n;) {
1239 err = USB_BULK_MSG(usbduxfastsub->usbdev,
1240 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
1241 usbduxfastsub->transfer_buffer,
1242 SIZEINBUF, &actual_length, BULK_TIMEOUT);
1244 printk("comedi%d: insn data error: %d\n",
1246 mutex_unlock(&usbduxfastsub->mutex);
1249 n = actual_length / sizeof(uint16_t);
1250 if ((n % 16) != 0) {
1251 printk("comedi%d: insn data packet corrupted.\n",
1253 mutex_unlock(&usbduxfastsub->mutex);
1256 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
1258 ((uint16_t *) (usbduxfastsub->
1259 transfer_buffer))[j];
1263 mutex_unlock(&usbduxfastsub->mutex);
1267 static unsigned hex2unsigned(char *h)
1271 hi = h[0] - 'A' + 0x0a;
1276 lo = h[1] - 'A' + 0x0a;
1280 return hi * 0x10 + lo;
1284 #define FIRMWARE_MAX_LEN 0x2000
1286 // taken from David Brownell's fxload and adjusted for this driver
1287 static int read_firmware(usbduxfastsub_t * usbduxfastsub,
1288 const void *firmwarePtr,
1292 unsigned char *fp = (char *)firmwarePtr;
1293 unsigned char *firmwareBinary = NULL;
1297 firmwareBinary = kmalloc(FIRMWARE_MAX_LEN, GFP_KERNEL);
1298 if (!firmwareBinary) {
1299 printk("comedi_: usbduxfast: mem alloc for firmware failed\n");
1311 while ((i < size) && (fp[i] != 13) && (fp[i] != 10)) {
1315 if (j >= sizeof(buf)) {
1316 printk("comedi_: usbduxfast: bogus firmware file!\n");
1320 // get rid of LF/CR/...
1321 while ((i < size) && ((fp[i] == 13) || (fp[i] == 10)
1327 //printk("comedi_: buf=%s\n",buf);
1329 /* EXTENSION: "# comment-till-end-of-line", for copyrights etc */
1333 if (buf[0] != ':') {
1334 printk("comedi_: usbduxfast: upload: not an ihex record: %s", buf);
1338 /* Read the length field (up to 16 bytes) */
1339 len = hex2unsigned(buf + 1);
1341 /* Read the target offset */
1342 off = (hex2unsigned(buf + 3) * 0x0100) + hex2unsigned(buf + 5);
1344 if ((off + len) > maxAddr) {
1345 maxAddr = off + len;
1348 if (maxAddr >= FIRMWARE_MAX_LEN) {
1349 printk("comedi_: usbduxfast: firmware upload goes beyond FX2 RAM boundaries.");
1352 //printk("comedi_: usbduxfast: off=%x, len=%x:",off,len);
1354 /* Read the record type */
1355 type = hex2unsigned(buf + 7);
1357 /* If this is an EOF record, then make it so. */
1363 printk("comedi_: usbduxfast: unsupported record type: %u\n", type);
1367 for (idx = 0, cp = buf + 9; idx < len; idx += 1, cp += 2) {
1368 firmwareBinary[idx + off] = hex2unsigned(cp);
1369 //printk("%02x ",firmwareBinary[idx+off]);
1374 printk("comedi_: usbduxfast: unexpected end of hex file\n");
1379 res = firmwareUpload(usbduxfastsub, firmwareBinary, maxAddr + 1);
1380 kfree(firmwareBinary);
1384 static void tidy_up(usbduxfastsub_t * usbduxfastsub_tmp)
1386 #ifdef CONFIG_COMEDI_DEBUG
1387 printk("comedi_: usbduxfast: tiding up\n");
1389 if (!usbduxfastsub_tmp) {
1392 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1393 // shows the usb subsystem that the driver is down
1394 if (usbduxfastsub_tmp->interface) {
1395 usb_set_intfdata(usbduxfastsub_tmp->interface, NULL);
1399 usbduxfastsub_tmp->probed = 0;
1401 if (usbduxfastsub_tmp->urbIn) {
1402 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)
1403 // waits until a running transfer is over
1404 // thus, under 2.4 hotplugging while a command
1405 // is running is not safe
1406 usb_kill_urb(usbduxfastsub_tmp->urbIn);
1408 if (usbduxfastsub_tmp->transfer_buffer) {
1409 kfree(usbduxfastsub_tmp->transfer_buffer);
1410 usbduxfastsub_tmp->transfer_buffer = NULL;
1412 usb_free_urb(usbduxfastsub_tmp->urbIn);
1413 usbduxfastsub_tmp->urbIn = NULL;
1415 if (usbduxfastsub_tmp->insnBuffer) {
1416 kfree(usbduxfastsub_tmp->insnBuffer);
1417 usbduxfastsub_tmp->insnBuffer = NULL;
1419 if (usbduxfastsub_tmp->dux_commands) {
1420 kfree(usbduxfastsub_tmp->dux_commands);
1421 usbduxfastsub_tmp->dux_commands = NULL;
1423 usbduxfastsub_tmp->ai_cmd_running = 0;
1426 static void usbduxfast_firmware_request_complete_handler(
1427 const struct firmware *fw,
1430 usbduxfastsub_t * usbduxfastsub_tmp = (usbduxfastsub_t *)context;
1431 struct usb_device *usbdev = usbduxfastsub_tmp->usbdev;
1438 // we need to upload the firmware here because fw will be
1439 // freed one we've left this function
1440 ret=read_firmware(usbduxfastsub_tmp,
1445 dev_err(&usbdev->dev,
1446 "Could not upload firmware (err=%d)\n",
1451 comedi_usb_auto_config(usbdev, BOARDNAME);
1454 // allocate memory for the urbs and initialise them
1455 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1456 static void *usbduxfastsub_probe(struct usb_device *udev,
1457 unsigned int interfnum,
1458 const struct usb_device_id *id)
1461 static int usbduxfastsub_probe(struct usb_interface *uinterf,
1462 const struct usb_device_id *id)
1464 struct usb_device *udev = interface_to_usbdev(uinterf);
1470 if (udev->speed != USB_SPEED_HIGH) {
1471 printk("comedi_: usbduxfast_: This driver needs USB 2.0 to operate. Aborting...\n");
1472 return PROBE_ERR_RETURN(-ENODEV);
1474 #ifdef CONFIG_COMEDI_DEBUG
1475 printk("comedi_: usbduxfast_: finding a free structure for the usb-device\n");
1477 mutex_lock(&start_stop_mutex);
1478 // look for a free place in the usbduxfast array
1480 for (i = 0; i < NUMUSBDUXFAST; i++) {
1481 if (!(usbduxfastsub[i].probed)) {
1489 printk("Too many usbduxfast-devices connected.\n");
1490 mutex_unlock(&start_stop_mutex);
1491 return PROBE_ERR_RETURN(-EMFILE);
1493 #ifdef CONFIG_COMEDI_DEBUG
1494 printk("comedi_: usbduxfast: usbduxfastsub[%d] is ready to connect to comedi.\n", index);
1497 mutex_init(&(usbduxfastsub[index].mutex));
1498 // save a pointer to the usb device
1499 usbduxfastsub[index].usbdev = udev;
1501 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1502 // save the interface number
1503 usbduxfastsub[index].ifnum = interfnum;
1505 // 2.6: save the interface itself
1506 usbduxfastsub[index].interface = uinterf;
1507 // get the interface number from the interface
1508 usbduxfastsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
1509 // hand the private data over to the usb subsystem
1510 // will be needed for disconnect
1511 usb_set_intfdata(uinterf, &(usbduxfastsub[index]));
1514 #ifdef CONFIG_COMEDI_DEBUG
1515 printk("comedi_: usbduxfast: ifnum=%d\n", usbduxfastsub[index].ifnum);
1517 // create space for the commands going to the usb device
1518 usbduxfastsub[index].dux_commands = kmalloc(SIZEOFDUXBUFFER,
1520 if (!usbduxfastsub[index].dux_commands) {
1521 printk("comedi_: usbduxfast: error alloc space for dac commands\n");
1522 tidy_up(&(usbduxfastsub[index]));
1523 mutex_unlock(&start_stop_mutex);
1524 return PROBE_ERR_RETURN(-ENOMEM);
1526 // create space of the instruction buffer
1527 usbduxfastsub[index].insnBuffer = kmalloc(SIZEINSNBUF, GFP_KERNEL);
1528 if (!(usbduxfastsub[index].insnBuffer)) {
1529 printk("comedi_: usbduxfast: could not alloc space for insnBuffer\n");
1530 tidy_up(&(usbduxfastsub[index]));
1531 mutex_unlock(&start_stop_mutex);
1532 return PROBE_ERR_RETURN(-ENOMEM);
1534 // setting to alternate setting 1: enabling bulk ep
1535 i = usb_set_interface(usbduxfastsub[index].usbdev,
1536 usbduxfastsub[index].ifnum, 1);
1538 printk("comedi_: usbduxfast%d: could not switch to alternate setting 1.\n", index);
1539 tidy_up(&(usbduxfastsub[index]));
1540 mutex_unlock(&start_stop_mutex);
1541 return PROBE_ERR_RETURN(-ENODEV);
1543 usbduxfastsub[index].urbIn = USB_ALLOC_URB(0);
1544 if (usbduxfastsub[index].urbIn == NULL) {
1545 printk("comedi_: usbduxfast%d: Could not alloc. urb\n", index);
1546 tidy_up(&(usbduxfastsub[index]));
1547 mutex_unlock(&start_stop_mutex);
1548 return PROBE_ERR_RETURN(-ENOMEM);
1550 usbduxfastsub[index].transfer_buffer = kmalloc(SIZEINBUF, GFP_KERNEL);
1551 if (!(usbduxfastsub[index].transfer_buffer)) {
1552 printk("comedi_: usbduxfast%d: could not alloc. transb.\n",
1554 tidy_up(&(usbduxfastsub[index]));
1555 mutex_unlock(&start_stop_mutex);
1556 return PROBE_ERR_RETURN(-ENOMEM);
1558 // we've reached the bottom of the function
1559 usbduxfastsub[index].probed = 1;
1560 mutex_unlock(&start_stop_mutex);
1562 ret = request_firmware_nowait(THIS_MODULE,
1564 "usbduxfast_firmware.hex",
1567 usbduxfastsub + index,
1568 usbduxfast_firmware_request_complete_handler);
1572 "could not load firmware (err=%d)\n",
1577 printk("comedi_: usbduxfast%d has been successfully initialized.\n",
1580 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1581 return (void *)(&usbduxfastsub[index]);
1588 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1589 static void usbduxfastsub_disconnect(struct usb_device *udev, void *ptr)
1591 usbduxfastsub_t *usbduxfastsub_tmp = (usbduxfastsub_t *) ptr;
1593 static void usbduxfastsub_disconnect(struct usb_interface *intf)
1595 usbduxfastsub_t *usbduxfastsub_tmp = usb_get_intfdata(intf);
1596 struct usb_device *udev = interface_to_usbdev(intf);
1598 if (!usbduxfastsub_tmp) {
1599 printk("comedi_: usbduxfast: disconnect called with null pointer.\n");
1602 if (usbduxfastsub_tmp->usbdev != udev) {
1603 printk("comedi_: usbduxfast: BUG! called with wrong ptr!!!\n");
1607 comedi_usb_auto_unconfig(udev);
1609 mutex_lock(&start_stop_mutex);
1610 mutex_lock(&usbduxfastsub_tmp->mutex);
1611 tidy_up(usbduxfastsub_tmp);
1612 mutex_unlock(&usbduxfastsub_tmp->mutex);
1613 mutex_unlock(&start_stop_mutex);
1614 #ifdef CONFIG_COMEDI_DEBUG
1615 printk("comedi_: usbduxfast: disconnected from the usb\n");
1619 // is called when comedi-config is called
1620 static int usbduxfast_attach(comedi_device * dev, comedi_devconfig * it)
1625 comedi_subdevice *s = NULL;
1626 dev->private = NULL;
1628 mutex_lock(&start_stop_mutex);
1629 // find a valid device which has been detected by the probe function of the usb
1631 for (i = 0; i < NUMUSBDUXFAST; i++) {
1632 if ((usbduxfastsub[i].probed) && (!usbduxfastsub[i].attached)) {
1639 printk("comedi%d: usbduxfast: error: attach failed, no usbduxfast devs connected to the usb bus.\n", dev->minor);
1640 mutex_unlock(&start_stop_mutex);
1644 mutex_lock(&(usbduxfastsub[index].mutex));
1645 // pointer back to the corresponding comedi device
1646 usbduxfastsub[index].comedidev = dev;
1648 // trying to upload the firmware into the chip
1649 if (comedi_aux_data(it->options, 0) &&
1650 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
1651 read_firmware(&usbduxfastsub[index],
1652 comedi_aux_data(it->options, 0),
1653 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
1656 dev->board_name = BOARDNAME;
1658 /* set number of subdevices */
1659 dev->n_subdevices = N_SUBDEVICES;
1661 // allocate space for the subdevices
1662 if ((ret = alloc_subdevices(dev, N_SUBDEVICES)) < 0) {
1663 printk("comedi%d: usbduxfast: error alloc space for subdev\n",
1665 mutex_unlock(&start_stop_mutex);
1669 printk("comedi%d: usbduxfast: usb-device %d is attached to comedi.\n",
1671 // private structure is also simply the usb-structure
1672 dev->private = usbduxfastsub + index;
1673 // the first subdevice is the A/D converter
1674 s = dev->subdevices + SUBDEV_AD;
1675 // the URBs get the comedi subdevice
1676 // which is responsible for reading
1677 // this is the subdevice which reads data
1678 dev->read_subdev = s;
1679 // the subdevice receives as private structure the
1683 s->type = COMEDI_SUBD_AI;
1684 // readable and ref is to ground
1685 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1688 // length of the channellist
1689 s->len_chanlist = 16;
1690 // callback functions
1691 s->insn_read = usbduxfast_ai_insn_read;
1692 s->do_cmdtest = usbduxfast_ai_cmdtest;
1693 s->do_cmd = usbduxfast_ai_cmd;
1694 s->cancel = usbduxfast_ai_cancel;
1695 // max value from the A/D converter (12bit+1 bit for overflow)
1696 s->maxdata = 0x1000;
1697 // range table to convert to physical units
1698 s->range_table = &range_usbduxfast_ai_range;
1700 // finally decide that it's attached
1701 usbduxfastsub[index].attached = 1;
1703 mutex_unlock(&(usbduxfastsub[index].mutex));
1705 mutex_unlock(&start_stop_mutex);
1707 printk("comedi%d: successfully attached to usbduxfast.\n", dev->minor);
1712 static int usbduxfast_detach(comedi_device * dev)
1714 usbduxfastsub_t *usbduxfastsub_tmp;
1716 #ifdef CONFIG_COMEDI_DEBUG
1717 printk("comedi%d: usbduxfast: detach usb device\n", dev->minor);
1721 printk("comedi?: usbduxfast: detach without dev variable...\n");
1725 usbduxfastsub_tmp = dev->private;
1726 if (!usbduxfastsub_tmp) {
1727 printk("comedi?: usbduxfast: detach without ptr to usbduxfastsub[]\n");
1731 mutex_lock(&usbduxfastsub_tmp->mutex);
1732 mutex_lock(&start_stop_mutex);
1733 // Don't allow detach to free the private structure
1734 // It's one entry of of usbduxfastsub[]
1735 dev->private = NULL;
1736 usbduxfastsub_tmp->attached = 0;
1737 usbduxfastsub_tmp->comedidev = NULL;
1738 #ifdef CONFIG_COMEDI_DEBUG
1739 printk("comedi%d: usbduxfast: detach: successfully removed\n",
1742 mutex_unlock(&start_stop_mutex);
1743 mutex_unlock(&usbduxfastsub_tmp->mutex);
1747 /* main driver struct */
1748 static comedi_driver driver_usbduxfast = {
1749 driver_name:"usbduxfast",
1751 attach:usbduxfast_attach,
1752 detach:usbduxfast_detach,
1755 static void init_usb_devices(void)
1758 #ifdef CONFIG_COMEDI_DEBUG
1759 printk("comedi_: usbduxfast: setting all possible devs to invalid\n");
1761 // all devices entries are invalid to begin with
1762 // they will become valid by the probe function
1763 // and then finally by the attach-function
1764 for (index = 0; index < NUMUSBDUXFAST; index++) {
1765 memset(&(usbduxfastsub[index]), 0x00,
1766 sizeof(usbduxfastsub[index]));
1767 mutex_init(&(usbduxfastsub[index].mutex));
1771 static void uninit_usb_devices(void)
1775 for (index = 0; index < NUMUSBDUXFAST; index++) {
1776 mutex_destroy(&(usbduxfastsub[index].mutex));
1780 // Table with the USB-devices: just now only testing IDs
1781 static struct usb_device_id usbduxfastsub_table[] = {
1782 // { USB_DEVICE(0x4b4, 0x8613), //testing
1784 {USB_DEVICE(0x13d8, 0x0010) //real ID
1786 {USB_DEVICE(0x13d8, 0x0011) //real ID
1788 {} /* Terminating entry */
1791 MODULE_DEVICE_TABLE(usb, usbduxfastsub_table);
1793 // The usbduxfastsub-driver
1794 static struct usb_driver usbduxfastsub_driver = {
1795 #ifdef COMEDI_HAVE_USB_DRIVER_OWNER
1799 probe:usbduxfastsub_probe,
1800 disconnect:usbduxfastsub_disconnect,
1801 id_table:usbduxfastsub_table,
1804 // Can't use the nice macro as I have also to initialise the USB
1806 // registering the usb-system _and_ the comedi-driver
1807 static int init_usbduxfast(void)
1809 printk(KERN_INFO KBUILD_MODNAME ": "
1810 DRIVER_VERSION ":" DRIVER_DESC "\n");
1812 usb_register(&usbduxfastsub_driver);
1813 comedi_driver_register(&driver_usbduxfast);
1817 // deregistering the comedi driver and the usb-subsystem
1818 static void exit_usbduxfast(void)
1820 comedi_driver_unregister(&driver_usbduxfast);
1821 usb_deregister(&usbduxfastsub_driver);
1822 uninit_usb_devices();
1825 module_init(init_usbduxfast);
1826 module_exit(exit_usbduxfast);
1828 MODULE_AUTHOR(DRIVER_AUTHOR);
1829 MODULE_DESCRIPTION(DRIVER_DESC);
1830 MODULE_LICENSE("GPL");