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 (usbduxfast)
27 Author: Bernd Porr <tech@linux-usb-daq.co.uk>
34 number of channels: 16
38 range = 0 : [-0.75 V,0.75 V]
39 range = 1 : [-0.5 V,0.5 V]
41 The channel-list allows 1,2,3 and 16 channels.
42 start: now|ext|int (external trigger via pin at HD-D connector)
43 scan_begin: follow|timer|ext
44 convert: timer|ext (contains the sampling interval. Min interval
45 for single channel acquisition is 33us
46 and for multiplexed acquisition 300us)
50 Configuration options:
51 The device requires firmware which is usually
52 uploaded automatically by udev/hotplug at the moment
53 the driver module is being loaded.
54 In case udev/hotplug is not enabled you need to upload
55 the firmware with comedi_config -i usbduxfast_firmware.bin
56 The firmware is usually installed under /lib/firmware
57 or can be downloaded form http://www.linux-usb-daq.co.uk.
61 * I must give credit here to Chris Baugher who
62 * wrote the driver for AT-MIO-16d. I used some parts of this
63 * driver. I also must give credits to David Brownell
64 * who supported me with the USB development.
70 * 0.9: Dropping the first data packet which seems to be from the last transfer.
71 * Buffer overflows in the FX2 are handed over to comedi.
72 * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
73 * Added insn command basically for testing. Sample rate is 1MHz/16ch=62.5kHz
74 * 0.99: Ian Abbott pointed out a bug which has been corrected. Thanks!
75 * 0.99a: added external trigger.
76 * 1.00: added firmware kernel request to the driver which fixed
77 * udev coldplug problem
80 #include <linux/kernel.h>
81 #include <linux/firmware.h>
82 #include <linux/module.h>
83 #include <linux/init.h>
84 #include <linux/slab.h>
85 #include <linux/input.h>
86 #include <linux/usb.h>
87 #include <linux/fcntl.h>
88 #include <linux/compiler.h>
89 #include "comedi_fc.h"
90 #include <linux/comedidev.h>
91 #include <linux/usb.h>
93 // (un)comment this if you want to have debug info.
94 //#define CONFIG_COMEDI_DEBUG
95 #undef CONFIG_COMEDI_DEBUG
97 #define BOARDNAME "usbduxfast"
99 // timeout for the USB-transfer
100 #define BULK_TIMEOUT 1000
102 // constants for "firmware" upload and download
103 #define USBDUXFASTSUB_FIRMWARE 0xA0
104 #define VENDOR_DIR_IN 0xC0
105 #define VENDOR_DIR_OUT 0x40
107 // internal adresses of the 8051 processor
108 #define USBDUXFASTSUB_CPUCS 0xE600
110 // max lenghth of the transfer-buffer for software upload
111 #define TB_LEN 0x2000
113 // Input endpoint number
116 // Endpoint for the A/D channellist: bulk OUT
117 #define CHANNELLISTEP 4
119 // Number of channels
120 #define NUMCHANNELS 32
122 // size of the waveform descriptor
123 #define WAVESIZE 0x20
125 // Size of one A/D value
126 #define SIZEADIN ((sizeof(int16_t)))
128 // Size of the input-buffer IN BYTES
129 #define SIZEINBUF 512
132 #define SIZEINSNBUF 512
134 // Size of the buffer for the dux commands
135 #define SIZEOFDUXBUFFER 256 // bytes
137 // Number of in-URBs which receive the data: min=5
138 #define NUMOFINBUFFERSHIGH 10
140 // Total number of usbduxfast devices
141 #define NUMUSBDUXFAST 16
143 // Number of subdevices
144 #define N_SUBDEVICES 1
146 // Analogue in subdevice
149 // min delay steps for more than one channel
150 // basically when the mux gives up. ;-)
151 #define MIN_SAMPLING_PERIOD 9 // steps at 30MHz in the FX2
153 // Max number of 1/30MHz delay steps:
154 #define MAX_SAMPLING_PERIOD 500
156 // Number of received packets to ignore before we start handing data over to comedi.
157 // It's quad buffering and we have to ignore 4 packets.
158 #define PACKETS_TO_IGNORE 4
160 /////////////////////////////////////////////
162 static const comedi_lrange range_usbduxfast_ai_range = { 2, {
169 * private structure of one subdevice
172 // This is the structure which holds all the data of this driver
173 // one sub device just now: A/D
177 // is it associated with a subdevice?
179 // pointer to the usb-device
180 struct usb_device *usbdev;
181 // BULK-transfer handling: urb
183 int8_t *transfer_buffer;
184 // input buffer for single insn
188 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
189 // interface structure in 2.6
190 struct usb_interface *interface;
192 // comedi device for the interrupt context
193 comedi_device *comedidev;
194 // asynchronous command is running
195 short int ai_cmd_running;
196 // continous aquisition
197 short int ai_continous;
198 // number of samples to aquire
199 long int ai_sample_count;
201 uint8_t *dux_commands;
202 // counter which ignores the first buffers
207 // The pointer to the private usb-data of the driver
208 // is also the private data for the comedi-device.
209 // This has to be global as the usb subsystem needs
210 // global variables. The other reason is that this
211 // structure must be there _before_ any comedi
212 // command is issued. The usb subsystem must be
213 // initialised before comedi can access it.
214 static usbduxfastsub_t usbduxfastsub[NUMUSBDUXFAST];
216 static DEFINE_MUTEX(start_stop_mutex);
218 // bulk transfers to usbduxfast
220 #define SENDADCOMMANDS 0
221 #define SENDINITEP6 1
223 static int send_dux_commands(usbduxfastsub_t * this_usbduxfastsub, int cmd_type)
226 this_usbduxfastsub->dux_commands[0] = cmd_type;
227 #ifdef CONFIG_COMEDI_DEBUG
229 printk("comedi%d: usbduxfast: dux_commands: ",
230 this_usbduxfastsub->comedidev->minor);
231 for (i = 0; i < SIZEOFDUXBUFFER; i++) {
232 printk(" %02x", this_usbduxfastsub->dux_commands[i]);
236 result = USB_BULK_MSG(this_usbduxfastsub->usbdev,
237 usb_sndbulkpipe(this_usbduxfastsub->usbdev,
239 this_usbduxfastsub->dux_commands,
240 SIZEOFDUXBUFFER, &nsent, BULK_TIMEOUT);
242 printk("comedi%d: could not transmit dux_commands to the usb-device, err=%d\n", this_usbduxfastsub->comedidev->minor, result);
247 // Stops the data acquision
248 // It should be safe to call this function from any context
249 static int usbduxfastsub_unlink_InURBs(usbduxfastsub_t * usbduxfastsub_tmp)
254 if (usbduxfastsub_tmp && usbduxfastsub_tmp->urbIn) {
255 usbduxfastsub_tmp->ai_cmd_running = 0;
256 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
257 j = usb_unlink_urb(usbduxfastsub_tmp->urbIn);
262 // waits until a running transfer is over
263 usb_kill_urb(usbduxfastsub_tmp->urbIn);
267 #ifdef CONFIG_COMEDI_DEBUG
268 printk("comedi: usbduxfast: unlinked InURB: res=%d\n", j);
273 /* This will stop a running acquisition operation */
274 // Is called from within this driver from both the
275 // interrupt context and from comedi
276 static int usbduxfast_ai_stop(usbduxfastsub_t * this_usbduxfastsub,
281 if (!this_usbduxfastsub) {
282 printk("comedi?: usbduxfast_ai_stop: this_usbduxfastsub=NULL!\n");
285 #ifdef CONFIG_COMEDI_DEBUG
286 printk("comedi: usbduxfast_ai_stop\n");
289 this_usbduxfastsub->ai_cmd_running = 0;
293 ret = usbduxfastsub_unlink_InURBs(this_usbduxfastsub);
299 // This will cancel a running acquisition operation.
300 // This is called by comedi but never from inside the
302 static int usbduxfast_ai_cancel(comedi_device * dev, comedi_subdevice * s)
304 usbduxfastsub_t *this_usbduxfastsub;
307 // force unlink of all urbs
308 #ifdef CONFIG_COMEDI_DEBUG
309 printk("comedi: usbduxfast_ai_cancel\n");
311 this_usbduxfastsub = dev->private;
312 if (!this_usbduxfastsub) {
313 printk("comedi: usbduxfast_ai_cancel: this_usbduxfastsub=NULL\n");
316 mutex_lock(&this_usbduxfastsub->mutex);
317 if (!(this_usbduxfastsub->probed)) {
318 mutex_unlock(&this_usbduxfastsub->mutex);
322 res = usbduxfast_ai_stop(this_usbduxfastsub, 1);
323 mutex_unlock(&this_usbduxfastsub->mutex);
329 // interrupt service routine
330 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
331 static void usbduxfastsub_ai_Irq(struct urb *urb)
333 static void usbduxfastsub_ai_Irq(struct urb *urb PT_REGS_ARG)
337 usbduxfastsub_t *this_usbduxfastsub;
338 comedi_device *this_comedidev;
345 printk("comedi_: usbduxfast_: ao int-handler called with urb=NULL!\n");
348 // the context variable points to the subdevice
349 this_comedidev = urb->context;
350 if (!this_comedidev) {
351 printk("comedi_: usbduxfast_: urb context is a NULL pointer!\n");
354 // the private structure of the subdevice is usbduxfastsub_t
355 this_usbduxfastsub = this_comedidev->private;
356 if (!this_usbduxfastsub) {
357 printk("comedi_: usbduxfast_: private of comedi subdev is a NULL pointer!\n");
360 // are we running a command?
361 if (unlikely(!(this_usbduxfastsub->ai_cmd_running))) {
362 // not running a command
363 // do not continue execution if no asynchronous command is running
364 // in particular not resubmit
368 if (unlikely(!(this_usbduxfastsub->attached))) {
369 // no comedi device there
372 // subdevice which is the AD converter
373 s = this_comedidev->subdevices + SUBDEV_AD;
375 // first we test if something unusual has just happened
376 switch (urb->status) {
380 // happens after an unlink command or when the device is plugged out
386 s->async->events |= COMEDI_CB_EOA;
387 s->async->events |= COMEDI_CB_ERROR;
388 comedi_event(this_usbduxfastsub->comedidev, s);
389 // stop the transfer w/o unlink
390 usbduxfast_ai_stop(this_usbduxfastsub, 0);
394 printk("comedi%d: usbduxfast: non-zero urb status received in ai intr context: %d\n", this_usbduxfastsub->comedidev->minor, urb->status);
395 s->async->events |= COMEDI_CB_EOA;
396 s->async->events |= COMEDI_CB_ERROR;
397 comedi_event(this_usbduxfastsub->comedidev, s);
398 usbduxfast_ai_stop(this_usbduxfastsub, 0);
402 p = urb->transfer_buffer;
403 if (!this_usbduxfastsub->ignore) {
404 if (!(this_usbduxfastsub->ai_continous)) {
405 // not continous, fixed number of samples
406 n = urb->actual_length / sizeof(uint16_t);
407 if (unlikely(this_usbduxfastsub->ai_sample_count < n)) {
408 // we have send only a fraction of the bytes received
409 cfc_write_array_to_buffer(s,
410 urb->transfer_buffer,
411 this_usbduxfastsub->ai_sample_count *
413 usbduxfast_ai_stop(this_usbduxfastsub, 0);
414 // say comedi that the acquistion is over
415 s->async->events |= COMEDI_CB_EOA;
416 comedi_event(this_usbduxfastsub->comedidev, s);
419 this_usbduxfastsub->ai_sample_count -= n;
421 // write the full buffer to comedi
422 err = cfc_write_array_to_buffer(s,
423 urb->transfer_buffer, urb->actual_length);
425 if (unlikely(err == 0)) {
426 /* buffer overflow */
427 usbduxfast_ai_stop(this_usbduxfastsub, 0);
431 // tell comedi that data is there
432 comedi_event(this_usbduxfastsub->comedidev, s);
435 // ignore this packet
436 this_usbduxfastsub->ignore--;
439 // command is still running
440 // resubmit urb for BULK transfer
441 urb->dev = this_usbduxfastsub->usbdev;
443 if ((err = USB_SUBMIT_URB(urb)) < 0) {
444 printk("comedi%d: usbduxfast: urb resubm failed: %d",
445 this_usbduxfastsub->comedidev->minor, err);
446 s->async->events |= COMEDI_CB_EOA;
447 s->async->events |= COMEDI_CB_ERROR;
448 comedi_event(this_usbduxfastsub->comedidev, s);
449 usbduxfast_ai_stop(this_usbduxfastsub, 0);
453 static int usbduxfastsub_start(usbduxfastsub_t * usbduxfastsub)
456 unsigned char local_transfer_buffer[16];
459 local_transfer_buffer[0] = 0;
460 errcode = USB_CONTROL_MSG(usbduxfastsub->usbdev,
461 // create a pipe for a control transfer
462 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
463 // bRequest, "Firmware"
464 USBDUXFASTSUB_FIRMWARE,
471 // address of the transfer buffer
472 local_transfer_buffer,
478 printk("comedi_: usbduxfast_: control msg failed (start)\n");
484 static int usbduxfastsub_stop(usbduxfastsub_t * usbduxfastsub)
488 unsigned char local_transfer_buffer[16];
490 local_transfer_buffer[0] = 1;
491 errcode = USB_CONTROL_MSG
492 (usbduxfastsub->usbdev,
493 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
494 // bRequest, "Firmware"
495 USBDUXFASTSUB_FIRMWARE,
501 0x0000, local_transfer_buffer,
507 printk("comedi_: usbduxfast: control msg failed (stop)\n");
513 static int usbduxfastsub_upload(usbduxfastsub_t * usbduxfastsub,
514 unsigned char *local_transfer_buffer,
515 unsigned int startAddr, unsigned int len)
519 errcode = USB_CONTROL_MSG
520 (usbduxfastsub->usbdev,
521 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
522 // brequest, firmware
523 USBDUXFASTSUB_FIRMWARE,
530 // our local safe buffer
531 local_transfer_buffer,
537 printk("comedi_: usbduxfast: uppload failed\n");
543 int firmwareUpload(usbduxfastsub_t * usbduxfastsub,
544 unsigned char *firmwareBinary, int sizeFirmware)
548 if (!firmwareBinary) {
551 ret = usbduxfastsub_stop(usbduxfastsub);
553 printk("comedi_: usbduxfast: can not stop firmware\n");
556 ret = usbduxfastsub_upload(usbduxfastsub,
557 firmwareBinary, 0, sizeFirmware);
559 printk("comedi_: usbduxfast: firmware upload failed\n");
562 ret = usbduxfastsub_start(usbduxfastsub);
564 printk("comedi_: usbduxfast: can not start firmware\n");
570 int usbduxfastsub_submit_InURBs(usbduxfastsub_t * usbduxfastsub)
574 if (!usbduxfastsub) {
577 usb_fill_bulk_urb(usbduxfastsub->urbIn,
578 usbduxfastsub->usbdev,
579 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
580 usbduxfastsub->transfer_buffer,
581 SIZEINBUF, usbduxfastsub_ai_Irq, usbduxfastsub->comedidev);
583 #ifdef CONFIG_COMEDI_DEBUG
584 printk("comedi%d: usbduxfast: submitting in-urb: %x,%x\n",
585 usbduxfastsub->comedidev->minor,
586 (int)(usbduxfastsub->urbIn->context),
587 (int)(usbduxfastsub->urbIn->dev));
589 errFlag = USB_SUBMIT_URB(usbduxfastsub->urbIn);
591 printk("comedi_: usbduxfast: ai: ");
592 printk("USB_SUBMIT_URB");
593 printk(" error %d\n", errFlag);
599 static int usbduxfast_ai_cmdtest(comedi_device * dev,
600 comedi_subdevice * s, comedi_cmd * cmd)
602 int err = 0, stop_mask = 0;
603 long int steps, tmp = 0;
605 usbduxfastsub_t *this_usbduxfastsub = dev->private;
606 if (!(this_usbduxfastsub->probed)) {
609 #ifdef CONFIG_COMEDI_DEBUG
610 printk("comedi%d: usbduxfast_ai_cmdtest\n", dev->minor);
611 printk("comedi%d: usbduxfast: convert_arg=%u scan_begin_arg=%u\n",
612 dev->minor, cmd->convert_arg, cmd->scan_begin_arg);
614 /* step 1: make sure trigger sources are trivially valid */
616 tmp = cmd->start_src;
617 cmd->start_src &= TRIG_NOW | TRIG_EXT | TRIG_INT;
618 if (!cmd->start_src || tmp != cmd->start_src)
621 tmp = cmd->scan_begin_src;
622 cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT;
623 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
626 tmp = cmd->convert_src;
627 cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
628 if (!cmd->convert_src || tmp != cmd->convert_src)
631 tmp = cmd->scan_end_src;
632 cmd->scan_end_src &= TRIG_COUNT;
633 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
637 stop_mask = TRIG_COUNT | TRIG_NONE;
638 cmd->stop_src &= stop_mask;
639 if (!cmd->stop_src || tmp != cmd->stop_src)
645 /* step 2: make sure trigger sources are unique and mutually compatible */
647 if (cmd->start_src != TRIG_NOW &&
648 cmd->start_src != TRIG_EXT && cmd->start_src != TRIG_INT)
650 if (cmd->scan_begin_src != TRIG_TIMER &&
651 cmd->scan_begin_src != TRIG_FOLLOW &&
652 cmd->scan_begin_src != TRIG_EXT)
654 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
656 if (cmd->stop_src != TRIG_COUNT &&
657 cmd->stop_src != TRIG_EXT && cmd->stop_src != TRIG_NONE)
660 // can't have external stop and start triggers at once
661 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
667 /* step 3: make sure arguments are trivially compatible */
669 if (cmd->start_src == TRIG_NOW && cmd->start_arg != 0) {
674 if (!cmd->chanlist_len) {
677 if (cmd->scan_end_arg != cmd->chanlist_len) {
678 cmd->scan_end_arg = cmd->chanlist_len;
682 if (cmd->chanlist_len == 1) {
685 minSamplPer = MIN_SAMPLING_PERIOD;
688 if (cmd->convert_src == TRIG_TIMER) {
689 steps = cmd->convert_arg * 30;
690 if (steps < (minSamplPer * 1000)) {
691 steps = minSamplPer * 1000;
693 if (steps > (MAX_SAMPLING_PERIOD * 1000)) {
694 steps = MAX_SAMPLING_PERIOD * 1000;
698 if (cmd->convert_arg != tmp) {
699 cmd->convert_arg = tmp;
704 if (cmd->scan_begin_src == TRIG_TIMER) {
708 switch (cmd->stop_src) {
710 if (!cmd->stop_arg) {
716 if (cmd->stop_arg != 0) {
721 // TRIG_EXT doesn't care since it doesn't trigger off a numbered channel
729 /* step 4: fix up any arguments */
735 static int usbduxfast_ai_inttrig(comedi_device * dev,
736 comedi_subdevice * s, unsigned int trignum)
739 usbduxfastsub_t *this_usbduxfastsub = dev->private;
740 if (!this_usbduxfastsub) {
743 mutex_lock(&this_usbduxfastsub->mutex);
744 if (!(this_usbduxfastsub->probed)) {
745 mutex_unlock(&this_usbduxfastsub->mutex);
748 #ifdef CONFIG_COMEDI_DEBUG
749 printk("comedi%d: usbduxfast_ai_inttrig\n", dev->minor);
753 printk("comedi%d: usbduxfast_ai_inttrig: invalid trignum\n",
755 mutex_unlock(&this_usbduxfastsub->mutex);
758 if (!(this_usbduxfastsub->ai_cmd_running)) {
759 this_usbduxfastsub->ai_cmd_running = 1;
760 ret = usbduxfastsub_submit_InURBs(this_usbduxfastsub);
762 printk("comedi%d: usbduxfast_ai_inttrig: urbSubmit: err=%d\n", dev->minor, ret);
763 this_usbduxfastsub->ai_cmd_running = 0;
764 mutex_unlock(&this_usbduxfastsub->mutex);
767 s->async->inttrig = NULL;
769 printk("comedi%d: ai_inttrig but acqu is already running\n",
772 mutex_unlock(&this_usbduxfastsub->mutex);
776 // offsets for the GPIF bytes
777 // the first byte is the command byte
778 #define LENBASE 1+0x00
779 #define OPBASE 1+0x08
780 #define OUTBASE 1+0x10
781 #define LOGBASE 1+0x18
783 static int usbduxfast_ai_cmd(comedi_device * dev, comedi_subdevice * s)
785 comedi_cmd *cmd = &s->async->cmd;
786 unsigned int chan, gain, rngmask = 0xff;
788 usbduxfastsub_t *this_usbduxfastsub = dev->private;
790 long steps, steps_tmp;
792 #ifdef CONFIG_COMEDI_DEBUG
793 printk("comedi%d: usbduxfast_ai_cmd\n", dev->minor);
795 if (!this_usbduxfastsub) {
798 mutex_lock(&this_usbduxfastsub->mutex);
799 if (!(this_usbduxfastsub->probed)) {
800 mutex_unlock(&this_usbduxfastsub->mutex);
803 if (this_usbduxfastsub->ai_cmd_running) {
804 printk("comedi%d: ai_cmd not possible. Another ai_cmd is running.\n", dev->minor);
805 mutex_unlock(&this_usbduxfastsub->mutex);
808 // set current channel of the running aquisition to zero
809 s->async->cur_chan = 0;
811 // ignore the first buffers from the device if there is an error condition
812 this_usbduxfastsub->ignore = PACKETS_TO_IGNORE;
814 if (cmd->chanlist_len > 0) {
815 gain = CR_RANGE(cmd->chanlist[0]);
816 for (i = 0; i < cmd->chanlist_len; ++i) {
817 chan = CR_CHAN(cmd->chanlist[i]);
819 printk("comedi%d: cmd is accepting only consecutive channels.\n", dev->minor);
820 mutex_unlock(&this_usbduxfastsub->mutex);
823 if ((gain != CR_RANGE(cmd->chanlist[i]))
824 && (cmd->chanlist_len > 3)) {
825 printk("comedi%d: the gain must be the same for all channels.\n", dev->minor);
826 mutex_unlock(&this_usbduxfastsub->mutex);
829 if (i >= NUMCHANNELS) {
830 printk("comedi%d: channel list too long\n",
837 if (cmd->scan_begin_src == TRIG_TIMER) {
838 printk("comedi%d: usbduxfast: scan_begin_src==TRIG_TIMER not valid.\n", dev->minor);
839 mutex_unlock(&this_usbduxfastsub->mutex);
842 if (cmd->convert_src == TRIG_TIMER) {
843 steps = (cmd->convert_arg * 30) / 1000;
845 if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
846 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);
847 mutex_unlock(&this_usbduxfastsub->mutex);
850 if (steps > MAX_SAMPLING_PERIOD) {
851 printk("comedi%d: usbduxfast: ai_cmd: sampling rate too low.\n",
853 mutex_unlock(&this_usbduxfastsub->mutex);
856 if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
857 && (cmd->chanlist_len != 16)) {
858 printk("comedi%d: usbduxfast: ai_cmd: TRIG_EXT only with 1 or 16 channels possible.\n", dev->minor);
859 mutex_unlock(&this_usbduxfastsub->mutex);
862 #ifdef CONFIG_COMEDI_DEBUG
863 printk("comedi%d: usbduxfast: steps=%ld, convert_arg=%u, ai_timer=%u\n",
865 steps, cmd->convert_arg, this_usbduxfastsub->ai_timer);
868 switch (cmd->chanlist_len) {
871 if (CR_RANGE(cmd->chanlist[0]) > 0)
872 rngmask = 0xff - 0x04;
876 // for external trigger: looping in this state until the RDY0 pin
878 if (cmd->start_src == TRIG_EXT) { // we loop here until ready has been set
879 this_usbduxfastsub->dux_commands[LENBASE + 0] = 0x01; // branch back to state 0
880 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x01; // deceision state w/o data
881 this_usbduxfastsub->dux_commands[OUTBASE + 0] =
883 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0x00; // RDY0 = 0
884 } else { // we just proceed to state 1
885 this_usbduxfastsub->dux_commands[LENBASE + 0] = 1;
886 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0;
887 this_usbduxfastsub->dux_commands[OUTBASE + 0] =
889 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
892 if (steps < MIN_SAMPLING_PERIOD) {
893 // for fast single channel aqu without mux
895 // we just stay here at state 1 and rexecute the same state
896 // this gives us 30MHz sampling rate
897 this_usbduxfastsub->dux_commands[LENBASE + 1] = 0x89; // branch back to state 1
898 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x03; // deceision state with data
899 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
901 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0xFF; // doesn't matter
903 // we loop through two states: data and delay: max rate is 15Mhz
904 this_usbduxfastsub->dux_commands[LENBASE + 1] =
906 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x02; // data
907 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
909 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0; // doesn't matter
911 this_usbduxfastsub->dux_commands[LENBASE + 2] = 0x09; // branch back to state 1
912 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0x01; // deceision state w/o data
913 this_usbduxfastsub->dux_commands[OUTBASE + 2] =
915 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0xFF; // doesn't matter
918 // we loop through 3 states: 2x delay and 1x data. This gives a min
919 // sampling rate of 60kHz.
921 // we have 1 state with duration 1
924 // do the first part of the delay
925 this_usbduxfastsub->dux_commands[LENBASE + 1] =
927 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0;
928 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
930 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
932 // and the second part
933 this_usbduxfastsub->dux_commands[LENBASE + 2] =
935 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
936 this_usbduxfastsub->dux_commands[OUTBASE + 2] =
938 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
940 // get the data and branch back
941 this_usbduxfastsub->dux_commands[LENBASE + 3] = 0x09; // branch back to state 1
942 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0x03; // deceision state w data
943 this_usbduxfastsub->dux_commands[OUTBASE + 3] =
945 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0xFF; // doesn't matter
951 // commit data to the FIFO
952 if (CR_RANGE(cmd->chanlist[0]) > 0)
953 rngmask = 0xff - 0x04;
956 this_usbduxfastsub->dux_commands[LENBASE + 0] = 1;
957 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x02; // data
958 this_usbduxfastsub->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
959 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
961 // we have 1 state with duration 1: state 0
962 steps_tmp = steps - 1;
964 if (CR_RANGE(cmd->chanlist[1]) > 0)
965 rngmask = 0xff - 0x04;
968 // do the first part of the delay
969 this_usbduxfastsub->dux_commands[LENBASE + 1] = steps_tmp / 2;
970 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0;
971 this_usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFE & rngmask; //count
972 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
974 // and the second part
975 this_usbduxfastsub->dux_commands[LENBASE + 2] =
976 steps_tmp - steps_tmp / 2;
977 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
978 this_usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
979 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
981 this_usbduxfastsub->dux_commands[LENBASE + 3] = 1;
982 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0x02; // data
983 this_usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
984 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
986 // we have 2 states with duration 1: step 6 and the IDLE state
987 steps_tmp = steps - 2;
989 if (CR_RANGE(cmd->chanlist[0]) > 0)
990 rngmask = 0xff - 0x04;
993 // do the first part of the delay
994 this_usbduxfastsub->dux_commands[LENBASE + 4] = steps_tmp / 2;
995 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0;
996 this_usbduxfastsub->dux_commands[OUTBASE + 4] = (0xFF - 0x02) & rngmask; //reset
997 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
999 // and the second part
1000 this_usbduxfastsub->dux_commands[LENBASE + 5] =
1001 steps_tmp - steps_tmp / 2;
1002 this_usbduxfastsub->dux_commands[OPBASE + 5] = 0;
1003 this_usbduxfastsub->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1004 this_usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1006 this_usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1007 this_usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1008 this_usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1009 this_usbduxfastsub->dux_commands[LOGBASE + 6] = 0;
1014 for (j = 0; j < 1; j++) {
1015 if (CR_RANGE(cmd->chanlist[j]) > 0)
1016 rngmask = 0xff - 0x04;
1019 // commit data to the FIFO and do the first part of the delay
1020 this_usbduxfastsub->dux_commands[LENBASE + j * 2] =
1022 this_usbduxfastsub->dux_commands[OPBASE + j * 2] = 0x02; // data
1023 this_usbduxfastsub->dux_commands[OUTBASE + j * 2] = 0xFF & rngmask; // no change
1024 this_usbduxfastsub->dux_commands[LOGBASE + j * 2] = 0;
1026 if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
1027 rngmask = 0xff - 0x04;
1030 // do the second part of the delay
1031 this_usbduxfastsub->dux_commands[LENBASE + j * 2 + 1] =
1033 this_usbduxfastsub->dux_commands[OPBASE + j * 2 + 1] = 0; // no data
1034 this_usbduxfastsub->dux_commands[OUTBASE + j * 2 + 1] = 0xFE & rngmask; //count
1035 this_usbduxfastsub->dux_commands[LOGBASE + j * 2 + 1] =
1039 // 2 steps with duration 1: the idele step and step 6:
1040 steps_tmp = steps - 2;
1041 // commit data to the FIFO and do the first part of the delay
1042 this_usbduxfastsub->dux_commands[LENBASE + 4] = steps_tmp / 2;
1043 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0x02; // data
1044 this_usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFF & rngmask; // no change
1045 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1047 if (CR_RANGE(cmd->chanlist[0]) > 0)
1048 rngmask = 0xff - 0x04;
1051 // do the second part of the delay
1052 this_usbduxfastsub->dux_commands[LENBASE + 5] =
1053 steps_tmp - steps_tmp / 2;
1054 this_usbduxfastsub->dux_commands[OPBASE + 5] = 0; // no data
1055 this_usbduxfastsub->dux_commands[OUTBASE + 5] = (0xFF - 0x02) & rngmask; // reset
1056 this_usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1058 this_usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1059 this_usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1060 this_usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1061 this_usbduxfastsub->dux_commands[LOGBASE + 6] = 0;
1064 if (CR_RANGE(cmd->chanlist[0]) > 0)
1065 rngmask = 0xff - 0x04;
1068 if (cmd->start_src == TRIG_EXT) { // we loop here until ready has been set
1069 this_usbduxfastsub->dux_commands[LENBASE + 0] = 0x01; // branch back to state 0
1070 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x01; // deceision state w/o data
1071 this_usbduxfastsub->dux_commands[OUTBASE + 0] = (0xFF - 0x02) & rngmask; // reset
1072 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0x00; // RDY0 = 0
1073 } else { // we just proceed to state 1
1074 this_usbduxfastsub->dux_commands[LENBASE + 0] = 255; // 30us reset pulse
1075 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0;
1076 this_usbduxfastsub->dux_commands[OUTBASE + 0] = (0xFF - 0x02) & rngmask; // reset
1077 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1080 // commit data to the FIFO
1081 this_usbduxfastsub->dux_commands[LENBASE + 1] = 1;
1082 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x02; // data
1083 this_usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
1084 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
1086 // we have 2 states with duration 1
1089 // do the first part of the delay
1090 this_usbduxfastsub->dux_commands[LENBASE + 2] = steps / 2;
1091 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
1092 this_usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1093 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
1095 // and the second part
1096 this_usbduxfastsub->dux_commands[LENBASE + 3] =
1098 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0;
1099 this_usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
1100 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
1102 this_usbduxfastsub->dux_commands[LENBASE + 4] = 0x09; // branch back to state 1
1103 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0x01; // deceision state w/o data
1104 this_usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1105 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0xFF; // doesn't matter
1110 printk("comedi %d: unsupported combination of channels\n",
1112 mutex_unlock(&this_usbduxfastsub->mutex);
1116 #ifdef CONFIG_COMEDI_DEBUG
1117 printk("comedi %d: sending commands to the usb device\n", dev->minor);
1119 // 0 means that the AD commands are sent
1120 result = send_dux_commands(this_usbduxfastsub, SENDADCOMMANDS);
1122 printk("comedi%d: adc command could not be submitted. Aborting...\n", dev->minor);
1123 mutex_unlock(&this_usbduxfastsub->mutex);
1126 if (cmd->stop_src == TRIG_COUNT) {
1127 this_usbduxfastsub->ai_sample_count =
1128 (cmd->stop_arg) * (cmd->scan_end_arg);
1129 if (usbduxfastsub->ai_sample_count < 1) {
1130 printk("comedi%d: (cmd->stop_arg)*(cmd->scan_end_arg)<1, aborting.\n", dev->minor);
1131 mutex_unlock(&this_usbduxfastsub->mutex);
1134 this_usbduxfastsub->ai_continous = 0;
1136 // continous aquisition
1137 this_usbduxfastsub->ai_continous = 1;
1138 this_usbduxfastsub->ai_sample_count = 0;
1141 if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
1142 // enable this acquisition operation
1143 this_usbduxfastsub->ai_cmd_running = 1;
1144 ret = usbduxfastsub_submit_InURBs(this_usbduxfastsub);
1146 this_usbduxfastsub->ai_cmd_running = 0;
1147 // fixme: unlink here??
1148 mutex_unlock(&this_usbduxfastsub->mutex);
1151 s->async->inttrig = NULL;
1154 // don't enable the acquision operation
1155 // wait for an internal signal
1156 s->async->inttrig = usbduxfast_ai_inttrig;
1158 mutex_unlock(&this_usbduxfastsub->mutex);
1163 /* Mode 0 is used to get a single conversion on demand */
1164 static int usbduxfast_ai_insn_read(comedi_device * dev,
1165 comedi_subdevice * s, comedi_insn * insn, lsampl_t * data)
1167 int i, j, n, actual_length;
1168 int chan, range, rngmask;
1170 usbduxfastsub_t *usbduxfastsub = dev->private;
1172 if (!usbduxfastsub) {
1173 printk("comedi%d: ai_insn_read: no usb dev.\n", dev->minor);
1176 #ifdef CONFIG_COMEDI_DEBUG
1177 printk("comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1178 dev->minor, insn->n, insn->subdev);
1180 mutex_lock(&usbduxfastsub->mutex);
1181 if (!(usbduxfastsub->probed)) {
1182 mutex_unlock(&usbduxfastsub->mutex);
1185 if (usbduxfastsub->ai_cmd_running) {
1186 printk("comedi%d: ai_insn_read not possible. Async Command is running.\n", dev->minor);
1187 mutex_unlock(&usbduxfastsub->mutex);
1190 // sample one channel
1191 chan = CR_CHAN(insn->chanspec);
1192 range = CR_RANGE(insn->chanspec);
1193 // set command for the first channel
1196 rngmask = 0xff - 0x04;
1199 // commit data to the FIFO
1200 usbduxfastsub->dux_commands[LENBASE + 0] = 1;
1201 usbduxfastsub->dux_commands[OPBASE + 0] = 0x02; // data
1202 usbduxfastsub->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
1203 usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1205 // do the first part of the delay
1206 usbduxfastsub->dux_commands[LENBASE + 1] = 12;
1207 usbduxfastsub->dux_commands[OPBASE + 1] = 0;
1208 usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
1209 usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
1211 usbduxfastsub->dux_commands[LENBASE + 2] = 1;
1212 usbduxfastsub->dux_commands[OPBASE + 2] = 0;
1213 usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1214 usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
1216 usbduxfastsub->dux_commands[LENBASE + 3] = 1;
1217 usbduxfastsub->dux_commands[OPBASE + 3] = 0;
1218 usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFE & rngmask;
1219 usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
1221 usbduxfastsub->dux_commands[LENBASE + 4] = 1;
1222 usbduxfastsub->dux_commands[OPBASE + 4] = 0;
1223 usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFE & rngmask;
1224 usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1227 usbduxfastsub->dux_commands[LENBASE + 5] = 12;
1228 usbduxfastsub->dux_commands[OPBASE + 5] = 0;
1229 usbduxfastsub->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1230 usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1232 usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1233 usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1234 usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1235 usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1237 #ifdef CONFIG_COMEDI_DEBUG
1238 printk("comedi %d: sending commands to the usb device\n", dev->minor);
1240 // 0 means that the AD commands are sent
1241 err = send_dux_commands(usbduxfastsub, SENDADCOMMANDS);
1243 printk("comedi%d: adc command could not be submitted. Aborting...\n", dev->minor);
1244 mutex_unlock(&usbduxfastsub->mutex);
1247 #ifdef CONFIG_COMEDI_DEBUG
1248 printk("comedi%d: usbduxfast: submitting in-urb: %x,%x\n",
1249 usbduxfastsub->comedidev->minor,
1250 (int)(usbduxfastsub->urbIn->context),
1251 (int)(usbduxfastsub->urbIn->dev));
1253 for (i = 0; i < PACKETS_TO_IGNORE; i++) {
1254 err = USB_BULK_MSG(usbduxfastsub->usbdev,
1255 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
1256 usbduxfastsub->transfer_buffer,
1257 SIZEINBUF, &actual_length, BULK_TIMEOUT);
1259 printk("comedi%d: insn timeout. No data.\n",
1261 mutex_unlock(&usbduxfastsub->mutex);
1266 for (i = 0; i < insn->n;) {
1267 err = USB_BULK_MSG(usbduxfastsub->usbdev,
1268 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
1269 usbduxfastsub->transfer_buffer,
1270 SIZEINBUF, &actual_length, BULK_TIMEOUT);
1272 printk("comedi%d: insn data error: %d\n",
1274 mutex_unlock(&usbduxfastsub->mutex);
1277 n = actual_length / sizeof(uint16_t);
1278 if ((n % 16) != 0) {
1279 printk("comedi%d: insn data packet corrupted.\n",
1281 mutex_unlock(&usbduxfastsub->mutex);
1284 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
1286 ((uint16_t *) (usbduxfastsub->
1287 transfer_buffer))[j];
1291 mutex_unlock(&usbduxfastsub->mutex);
1295 static unsigned hex2unsigned(char *h)
1299 hi = h[0] - 'A' + 0x0a;
1304 lo = h[1] - 'A' + 0x0a;
1308 return hi * 0x10 + lo;
1312 #define FIRMWARE_MAX_LEN 0x2000
1314 // taken from David Brownell's fxload and adjusted for this driver
1315 static int read_firmware(usbduxfastsub_t * usbduxfastsub,
1316 const void *firmwarePtr,
1320 unsigned char *fp = (char *)firmwarePtr;
1321 unsigned char *firmwareBinary = NULL;
1325 firmwareBinary = kmalloc(FIRMWARE_MAX_LEN, GFP_KERNEL);
1326 if (!firmwareBinary) {
1327 printk("comedi_: usbduxfast: mem alloc for firmware failed\n");
1339 while ((i < size) && (fp[i] != 13) && (fp[i] != 10)) {
1343 if (j >= sizeof(buf)) {
1344 printk("comedi_: usbduxfast: bogus firmware file!\n");
1348 // get rid of LF/CR/...
1349 while ((i < size) && ((fp[i] == 13) || (fp[i] == 10)
1355 //printk("comedi_: buf=%s\n",buf);
1357 /* EXTENSION: "# comment-till-end-of-line", for copyrights etc */
1361 if (buf[0] != ':') {
1362 printk("comedi_: usbduxfast: upload: not an ihex record: %s", buf);
1366 /* Read the length field (up to 16 bytes) */
1367 len = hex2unsigned(buf + 1);
1369 /* Read the target offset */
1370 off = (hex2unsigned(buf + 3) * 0x0100) + hex2unsigned(buf + 5);
1372 if ((off + len) > maxAddr) {
1373 maxAddr = off + len;
1376 if (maxAddr >= FIRMWARE_MAX_LEN) {
1377 printk("comedi_: usbduxfast: firmware upload goes beyond FX2 RAM boundaries.");
1380 //printk("comedi_: usbduxfast: off=%x, len=%x:",off,len);
1382 /* Read the record type */
1383 type = hex2unsigned(buf + 7);
1385 /* If this is an EOF record, then make it so. */
1391 printk("comedi_: usbduxfast: unsupported record type: %u\n", type);
1395 for (idx = 0, cp = buf + 9; idx < len; idx += 1, cp += 2) {
1396 firmwareBinary[idx + off] = hex2unsigned(cp);
1397 //printk("%02x ",firmwareBinary[idx+off]);
1402 printk("comedi_: usbduxfast: unexpected end of hex file\n");
1407 res = firmwareUpload(usbduxfastsub, firmwareBinary, maxAddr + 1);
1408 kfree(firmwareBinary);
1412 static void tidy_up(usbduxfastsub_t * usbduxfastsub_tmp)
1414 #ifdef CONFIG_COMEDI_DEBUG
1415 printk("comedi_: usbduxfast: tiding up\n");
1417 if (!usbduxfastsub_tmp) {
1420 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1421 // shows the usb subsystem that the driver is down
1422 if (usbduxfastsub_tmp->interface) {
1423 usb_set_intfdata(usbduxfastsub_tmp->interface, NULL);
1427 usbduxfastsub_tmp->probed = 0;
1429 if (usbduxfastsub_tmp->urbIn) {
1430 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)
1431 // waits until a running transfer is over
1432 // thus, under 2.4 hotplugging while a command
1433 // is running is not safe
1434 usb_kill_urb(usbduxfastsub_tmp->urbIn);
1436 if (usbduxfastsub_tmp->transfer_buffer) {
1437 kfree(usbduxfastsub_tmp->transfer_buffer);
1438 usbduxfastsub_tmp->transfer_buffer = NULL;
1440 usb_free_urb(usbduxfastsub_tmp->urbIn);
1441 usbduxfastsub_tmp->urbIn = NULL;
1443 if (usbduxfastsub_tmp->insnBuffer) {
1444 kfree(usbduxfastsub_tmp->insnBuffer);
1445 usbduxfastsub_tmp->insnBuffer = NULL;
1447 if (usbduxfastsub_tmp->dux_commands) {
1448 kfree(usbduxfastsub_tmp->dux_commands);
1449 usbduxfastsub_tmp->dux_commands = NULL;
1451 usbduxfastsub_tmp->ai_cmd_running = 0;
1454 static void usbduxfast_firmware_request_complete_handler(
1455 const struct firmware *fw,
1458 usbduxfastsub_t * usbduxfastsub_tmp = (usbduxfastsub_t *)context;
1459 struct usb_device *usbdev = usbduxfastsub_tmp->usbdev;
1466 // we need to upload the firmware here because fw will be
1467 // freed one we've left this function
1468 ret=read_firmware(usbduxfastsub_tmp,
1473 dev_err(&usbdev->dev,
1474 "Could not upload firmware (err=%d)\n",
1479 comedi_usb_auto_config(usbdev, BOARDNAME);
1482 // allocate memory for the urbs and initialise them
1483 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1484 static void *usbduxfastsub_probe(struct usb_device *udev,
1485 unsigned int interfnum,
1486 const struct usb_device_id *id)
1489 static int usbduxfastsub_probe(struct usb_interface *uinterf,
1490 const struct usb_device_id *id)
1492 struct usb_device *udev = interface_to_usbdev(uinterf);
1498 if (udev->speed != USB_SPEED_HIGH) {
1499 printk("comedi_: usbduxfast_: This driver needs USB 2.0 to operate. Aborting...\n");
1500 return PROBE_ERR_RETURN(-ENODEV);
1502 #ifdef CONFIG_COMEDI_DEBUG
1503 printk("comedi_: usbduxfast_: finding a free structure for the usb-device\n");
1505 mutex_lock(&start_stop_mutex);
1506 // look for a free place in the usbduxfast array
1508 for (i = 0; i < NUMUSBDUXFAST; i++) {
1509 if (!(usbduxfastsub[i].probed)) {
1517 printk("Too many usbduxfast-devices connected.\n");
1518 mutex_unlock(&start_stop_mutex);
1519 return PROBE_ERR_RETURN(-EMFILE);
1521 #ifdef CONFIG_COMEDI_DEBUG
1522 printk("comedi_: usbduxfast: usbduxfastsub[%d] is ready to connect to comedi.\n", index);
1525 mutex_init(&(usbduxfastsub[index].mutex));
1526 // save a pointer to the usb device
1527 usbduxfastsub[index].usbdev = udev;
1529 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1530 // save the interface number
1531 usbduxfastsub[index].ifnum = interfnum;
1533 // 2.6: save the interface itself
1534 usbduxfastsub[index].interface = uinterf;
1535 // get the interface number from the interface
1536 usbduxfastsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
1537 // hand the private data over to the usb subsystem
1538 // will be needed for disconnect
1539 usb_set_intfdata(uinterf, &(usbduxfastsub[index]));
1542 #ifdef CONFIG_COMEDI_DEBUG
1543 printk("comedi_: usbduxfast: ifnum=%d\n", usbduxfastsub[index].ifnum);
1545 // create space for the commands going to the usb device
1546 usbduxfastsub[index].dux_commands = kmalloc(SIZEOFDUXBUFFER,
1548 if (!usbduxfastsub[index].dux_commands) {
1549 printk("comedi_: usbduxfast: error alloc space for dac commands\n");
1550 tidy_up(&(usbduxfastsub[index]));
1551 mutex_unlock(&start_stop_mutex);
1552 return PROBE_ERR_RETURN(-ENOMEM);
1554 // create space of the instruction buffer
1555 usbduxfastsub[index].insnBuffer = kmalloc(SIZEINSNBUF, GFP_KERNEL);
1556 if (!(usbduxfastsub[index].insnBuffer)) {
1557 printk("comedi_: usbduxfast: could not alloc space for insnBuffer\n");
1558 tidy_up(&(usbduxfastsub[index]));
1559 mutex_unlock(&start_stop_mutex);
1560 return PROBE_ERR_RETURN(-ENOMEM);
1562 // setting to alternate setting 1: enabling bulk ep
1563 i = usb_set_interface(usbduxfastsub[index].usbdev,
1564 usbduxfastsub[index].ifnum, 1);
1566 printk("comedi_: usbduxfast%d: could not switch to alternate setting 1.\n", index);
1567 tidy_up(&(usbduxfastsub[index]));
1568 mutex_unlock(&start_stop_mutex);
1569 return PROBE_ERR_RETURN(-ENODEV);
1571 usbduxfastsub[index].urbIn = USB_ALLOC_URB(0);
1572 if (usbduxfastsub[index].urbIn == NULL) {
1573 printk("comedi_: usbduxfast%d: Could not alloc. urb\n", index);
1574 tidy_up(&(usbduxfastsub[index]));
1575 mutex_unlock(&start_stop_mutex);
1576 return PROBE_ERR_RETURN(-ENOMEM);
1578 usbduxfastsub[index].transfer_buffer = kmalloc(SIZEINBUF, GFP_KERNEL);
1579 if (!(usbduxfastsub[index].transfer_buffer)) {
1580 printk("comedi_: usbduxfast%d: could not alloc. transb.\n",
1582 tidy_up(&(usbduxfastsub[index]));
1583 mutex_unlock(&start_stop_mutex);
1584 return PROBE_ERR_RETURN(-ENOMEM);
1586 // we've reached the bottom of the function
1587 usbduxfastsub[index].probed = 1;
1588 mutex_unlock(&start_stop_mutex);
1590 ret = request_firmware_nowait(THIS_MODULE,
1592 "usbduxfast_firmware.hex",
1595 usbduxfastsub + index,
1596 usbduxfast_firmware_request_complete_handler);
1600 "could not load firmware (err=%d)\n",
1605 printk("comedi_: usbduxfast%d has been successfully initialized.\n",
1608 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1609 return (void *)(&usbduxfastsub[index]);
1616 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1617 static void usbduxfastsub_disconnect(struct usb_device *udev, void *ptr)
1619 usbduxfastsub_t *usbduxfastsub_tmp = (usbduxfastsub_t *) ptr;
1621 static void usbduxfastsub_disconnect(struct usb_interface *intf)
1623 usbduxfastsub_t *usbduxfastsub_tmp = usb_get_intfdata(intf);
1624 struct usb_device *udev = interface_to_usbdev(intf);
1626 if (!usbduxfastsub_tmp) {
1627 printk("comedi_: usbduxfast: disconnect called with null pointer.\n");
1630 if (usbduxfastsub_tmp->usbdev != udev) {
1631 printk("comedi_: usbduxfast: BUG! called with wrong ptr!!!\n");
1635 comedi_usb_auto_unconfig(udev);
1637 mutex_lock(&start_stop_mutex);
1638 mutex_lock(&usbduxfastsub_tmp->mutex);
1639 tidy_up(usbduxfastsub_tmp);
1640 mutex_unlock(&usbduxfastsub_tmp->mutex);
1641 mutex_unlock(&start_stop_mutex);
1642 #ifdef CONFIG_COMEDI_DEBUG
1643 printk("comedi_: usbduxfast: disconnected from the usb\n");
1647 // is called when comedi-config is called
1648 static int usbduxfast_attach(comedi_device * dev, comedi_devconfig * it)
1653 comedi_subdevice *s = NULL;
1654 dev->private = NULL;
1656 mutex_lock(&start_stop_mutex);
1657 // find a valid device which has been detected by the probe function of the usb
1659 for (i = 0; i < NUMUSBDUXFAST; i++) {
1660 if ((usbduxfastsub[i].probed) && (!usbduxfastsub[i].attached)) {
1667 printk("comedi%d: usbduxfast: error: attach failed, no usbduxfast devs connected to the usb bus.\n", dev->minor);
1668 mutex_unlock(&start_stop_mutex);
1672 mutex_lock(&(usbduxfastsub[index].mutex));
1673 // pointer back to the corresponding comedi device
1674 usbduxfastsub[index].comedidev = dev;
1676 // trying to upload the firmware into the chip
1677 if (comedi_aux_data(it->options, 0) &&
1678 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
1679 read_firmware(&usbduxfastsub[index],
1680 comedi_aux_data(it->options, 0),
1681 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
1684 dev->board_name = BOARDNAME;
1686 /* set number of subdevices */
1687 dev->n_subdevices = N_SUBDEVICES;
1689 // allocate space for the subdevices
1690 if ((ret = alloc_subdevices(dev, N_SUBDEVICES)) < 0) {
1691 printk("comedi%d: usbduxfast: error alloc space for subdev\n",
1693 mutex_unlock(&start_stop_mutex);
1697 printk("comedi%d: usbduxfast: usb-device %d is attached to comedi.\n",
1699 // private structure is also simply the usb-structure
1700 dev->private = usbduxfastsub + index;
1701 // the first subdevice is the A/D converter
1702 s = dev->subdevices + SUBDEV_AD;
1703 // the URBs get the comedi subdevice
1704 // which is responsible for reading
1705 // this is the subdevice which reads data
1706 dev->read_subdev = s;
1707 // the subdevice receives as private structure the
1711 s->type = COMEDI_SUBD_AI;
1712 // readable and ref is to ground
1713 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1716 // length of the channellist
1717 s->len_chanlist = 16;
1718 // callback functions
1719 s->insn_read = usbduxfast_ai_insn_read;
1720 s->do_cmdtest = usbduxfast_ai_cmdtest;
1721 s->do_cmd = usbduxfast_ai_cmd;
1722 s->cancel = usbduxfast_ai_cancel;
1723 // max value from the A/D converter (12bit+1 bit for overflow)
1724 s->maxdata = 0x1000;
1725 // range table to convert to physical units
1726 s->range_table = &range_usbduxfast_ai_range;
1728 // finally decide that it's attached
1729 usbduxfastsub[index].attached = 1;
1731 mutex_unlock(&(usbduxfastsub[index].mutex));
1733 mutex_unlock(&start_stop_mutex);
1735 printk("comedi%d: successfully attached to usbduxfast.\n", dev->minor);
1740 static int usbduxfast_detach(comedi_device * dev)
1742 usbduxfastsub_t *usbduxfastsub_tmp;
1744 #ifdef CONFIG_COMEDI_DEBUG
1745 printk("comedi%d: usbduxfast: detach usb device\n", dev->minor);
1749 printk("comedi?: usbduxfast: detach without dev variable...\n");
1753 usbduxfastsub_tmp = dev->private;
1754 if (!usbduxfastsub_tmp) {
1755 printk("comedi?: usbduxfast: detach without ptr to usbduxfastsub[]\n");
1759 mutex_lock(&usbduxfastsub_tmp->mutex);
1760 mutex_lock(&start_stop_mutex);
1761 // Don't allow detach to free the private structure
1762 // It's one entry of of usbduxfastsub[]
1763 dev->private = NULL;
1764 usbduxfastsub_tmp->attached = 0;
1765 usbduxfastsub_tmp->comedidev = NULL;
1766 #ifdef CONFIG_COMEDI_DEBUG
1767 printk("comedi%d: usbduxfast: detach: successfully removed\n",
1770 mutex_unlock(&start_stop_mutex);
1771 mutex_unlock(&usbduxfastsub_tmp->mutex);
1775 /* main driver struct */
1776 static comedi_driver driver_usbduxfast = {
1777 driver_name:"usbduxfast",
1779 attach:usbduxfast_attach,
1780 detach:usbduxfast_detach,
1783 static void init_usb_devices(void)
1786 #ifdef CONFIG_COMEDI_DEBUG
1787 printk("comedi_: usbduxfast: setting all possible devs to invalid\n");
1789 // all devices entries are invalid to begin with
1790 // they will become valid by the probe function
1791 // and then finally by the attach-function
1792 for (index = 0; index < NUMUSBDUXFAST; index++) {
1793 memset(&(usbduxfastsub[index]), 0x00,
1794 sizeof(usbduxfastsub[index]));
1795 mutex_init(&(usbduxfastsub[index].mutex));
1799 static void uninit_usb_devices(void)
1803 for (index = 0; index < NUMUSBDUXFAST; index++) {
1804 mutex_destroy(&(usbduxfastsub[index].mutex));
1808 // Table with the USB-devices: just now only testing IDs
1809 static struct usb_device_id usbduxfastsub_table[] = {
1810 // { USB_DEVICE(0x4b4, 0x8613), //testing
1812 {USB_DEVICE(0x13d8, 0x0010) //real ID
1814 {USB_DEVICE(0x13d8, 0x0011) //real ID
1816 {} /* Terminating entry */
1819 MODULE_DEVICE_TABLE(usb, usbduxfastsub_table);
1821 // The usbduxfastsub-driver
1822 static struct usb_driver usbduxfastsub_driver = {
1823 #ifdef COMEDI_HAVE_USB_DRIVER_OWNER
1827 probe:usbduxfastsub_probe,
1828 disconnect:usbduxfastsub_disconnect,
1829 id_table:usbduxfastsub_table,
1832 // Can't use the nice macro as I have also to initialise the USB
1834 // registering the usb-system _and_ the comedi-driver
1835 static int init_usbduxfast(void)
1837 printk(KERN_INFO KBUILD_MODNAME ": "
1838 DRIVER_VERSION ":" DRIVER_DESC "\n");
1840 usb_register(&usbduxfastsub_driver);
1841 comedi_driver_register(&driver_usbduxfast);
1845 // deregistering the comedi driver and the usb-subsystem
1846 static void exit_usbduxfast(void)
1848 comedi_driver_unregister(&driver_usbduxfast);
1849 usb_deregister(&usbduxfastsub_driver);
1850 uninit_usb_devices();
1853 module_init(init_usbduxfast);
1854 module_exit(exit_usbduxfast);
1856 MODULE_AUTHOR(DRIVER_AUTHOR);
1857 MODULE_DESCRIPTION(DRIVER_DESC);
1858 MODULE_LICENSE("GPL");