1 #define DRIVER_VERSION "v0.99a"
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.
50 #include <linux/kernel.h>
51 #include <linux/module.h>
52 #include <linux/init.h>
53 #include <linux/slab.h>
54 #include <linux/input.h>
55 #include <linux/usb.h>
56 #include <linux/smp_lock.h>
57 #include <linux/fcntl.h>
58 #include <linux/compiler.h>
59 #include "comedi_fc.h"
60 #include "../comedidev.h"
62 // (un)comment this if you want to have debug info.
63 //#define CONFIG_COMEDI_DEBUG
64 #undef CONFIG_COMEDI_DEBUG
66 #define BOARDNAME "usbduxfast"
68 // timeout for the USB-transfer
71 // constants for "firmware" upload and download
72 #define USBDUXFASTSUB_FIRMWARE 0xA0
73 #define VENDOR_DIR_IN 0xC0
74 #define VENDOR_DIR_OUT 0x40
76 // internal adresses of the 8051 processor
77 #define USBDUXFASTSUB_CPUCS 0xE600
79 // max lenghth of the transfer-buffer for software upload
82 // Input endpoint number
85 // Endpoint for the A/D channellist: bulk OUT
86 #define CHANNELLISTEP 4
89 #define NUMCHANNELS 32
91 // size of the waveform descriptor
94 // Size of one A/D value
95 #define SIZEADIN ((sizeof(int16_t)))
97 // Size of the input-buffer IN BYTES
101 #define SIZEINSNBUF 512
103 // Size of the buffer for the dux commands
104 #define SIZEOFDUXBUFFER 256 // bytes
106 // Number of in-URBs which receive the data: min=5
107 #define NUMOFINBUFFERSHIGH 10
109 // Total number of usbduxfast devices
110 #define NUMUSBDUXFAST 16
112 // Number of subdevices
113 #define N_SUBDEVICES 1
115 // Analogue in subdevice
118 // min delay steps for more than one channel
119 // basically when the mux gives up. ;-)
120 #define MIN_SAMPLING_PERIOD 9 // steps at 30MHz in the FX2
122 // Max number of 1/30MHz delay steps:
123 #define MAX_SAMPLING_PERIOD 500
125 // Number of received packets to ignore before we start handing data over to comedi.
126 // It's quad buffering and we have to ignore 4 packets.
127 #define PACKETS_TO_IGNORE 4
129 /////////////////////////////////////////////
131 static const comedi_lrange range_usbduxfast_ai_range = { 2, {
138 * private structure of one subdevice
141 // This is the structure which holds all the data of this driver
142 // one sub device just now: A/D
146 // is it associated with a subdevice?
148 // pointer to the usb-device
149 struct usb_device *usbdev;
150 // BULK-transfer handling: urb
152 int8_t *transfer_buffer;
153 // input buffer for single insn
157 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
158 // interface structure in 2.6
159 struct usb_interface *interface;
161 // comedi device for the interrupt context
162 comedi_device *comedidev;
163 // asynchronous command is running
164 short int ai_cmd_running;
165 // continous aquisition
166 short int ai_continous;
167 // number of samples to aquire
168 long int ai_sample_count;
170 uint8_t *dux_commands;
171 // counter which ignores the first buffers
173 struct semaphore sem;
176 // The pointer to the private usb-data of the driver
177 // is also the private data for the comedi-device.
178 // This has to be global as the usb subsystem needs
179 // global variables. The other reason is that this
180 // structure must be there _before_ any comedi
181 // command is issued. The usb subsystem must be
182 // initialised before comedi can access it.
183 static usbduxfastsub_t usbduxfastsub[NUMUSBDUXFAST];
185 static DECLARE_MUTEX(start_stop_sem);
187 // bulk transfers to usbduxfast
189 #define SENDADCOMMANDS 0
190 #define SENDINITEP6 1
192 static int send_dux_commands(usbduxfastsub_t * this_usbduxfastsub, int cmd_type)
195 this_usbduxfastsub->dux_commands[0] = cmd_type;
196 #ifdef CONFIG_COMEDI_DEBUG
198 printk("comedi%d: usbduxfast: dux_commands: ",
199 this_usbduxfastsub->comedidev->minor);
200 for (i = 0; i < SIZEOFDUXBUFFER; i++) {
201 printk(" %02x", this_usbduxfastsub->dux_commands[i]);
205 result = usb_bulk_msg(this_usbduxfastsub->usbdev,
206 usb_sndbulkpipe(this_usbduxfastsub->usbdev,
208 this_usbduxfastsub->dux_commands, SIZEOFDUXBUFFER,
211 printk("comedi%d: could not transmit dux_commands to the usb-device, err=%d\n", this_usbduxfastsub->comedidev->minor, result);
216 // Stops the data acquision
217 // It should be safe to call this function from any context
218 static int usbduxfastsub_unlink_InURBs(usbduxfastsub_t * usbduxfastsub_tmp)
223 if (usbduxfastsub_tmp && usbduxfastsub_tmp->urbIn) {
224 usbduxfastsub_tmp->ai_cmd_running = 0;
225 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,8)
226 j = usb_unlink_urb(usbduxfastsub_tmp->urbIn);
231 // waits until a running transfer is over
232 usb_kill_urb(usbduxfastsub_tmp->urbIn);
236 #ifdef CONFIG_COMEDI_DEBUG
237 printk("comedi: usbduxfast: unlinked InURB: res=%d\n", j);
242 /* This will stop a running acquisition operation */
243 // Is called from within this driver from both the
244 // interrupt context and from comedi
245 static int usbduxfast_ai_stop(usbduxfastsub_t * this_usbduxfastsub,
250 if (!this_usbduxfastsub) {
251 printk("comedi?: usbduxfast_ai_stop: this_usbduxfastsub=NULL!\n");
254 #ifdef CONFIG_COMEDI_DEBUG
255 printk("comedi: usbduxfast_ai_stop\n");
258 this_usbduxfastsub->ai_cmd_running = 0;
262 ret = usbduxfastsub_unlink_InURBs(this_usbduxfastsub);
268 // This will cancel a running acquisition operation.
269 // This is called by comedi but never from inside the
271 static int usbduxfast_ai_cancel(comedi_device * dev, comedi_subdevice * s)
273 usbduxfastsub_t *this_usbduxfastsub;
276 // force unlink of all urbs
277 #ifdef CONFIG_COMEDI_DEBUG
278 printk("comedi: usbduxfast_ai_cancel\n");
280 this_usbduxfastsub = dev->private;
281 if (!this_usbduxfastsub) {
282 printk("comedi: usbduxfast_ai_cancel: this_usbduxfastsub=NULL\n");
285 down(&this_usbduxfastsub->sem);
286 if (!(this_usbduxfastsub->probed)) {
287 up(&this_usbduxfastsub->sem);
291 res = usbduxfast_ai_stop(this_usbduxfastsub, 1);
292 up(&this_usbduxfastsub->sem);
298 // interrupt service routine
299 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
300 static void usbduxfastsub_ai_Irq(struct urb *urb)
302 static void usbduxfastsub_ai_Irq(struct urb *urb PT_REGS_ARG)
306 usbduxfastsub_t *this_usbduxfastsub;
307 comedi_device *this_comedidev;
314 printk("comedi_: usbduxfast_: ao int-handler called with urb=NULL!\n");
317 // the context variable points to the subdevice
318 this_comedidev = urb->context;
319 if (!this_comedidev) {
320 printk("comedi_: usbduxfast_: urb context is a NULL pointer!\n");
323 // the private structure of the subdevice is usbduxfastsub_t
324 this_usbduxfastsub = this_comedidev->private;
325 if (!this_usbduxfastsub) {
326 printk("comedi_: usbduxfast_: private of comedi subdev is a NULL pointer!\n");
329 // are we running a command?
330 if (unlikely(!(this_usbduxfastsub->ai_cmd_running))) {
331 // not running a command
332 // do not continue execution if no asynchronous command is running
333 // in particular not resubmit
337 if (unlikely(!(this_usbduxfastsub->attached))) {
338 // no comedi device there
341 // subdevice which is the AD converter
342 s = this_comedidev->subdevices + SUBDEV_AD;
344 // first we test if something unusual has just happened
345 switch (urb->status) {
349 // happens after an unlink command or when the device is plugged out
355 s->async->events |= COMEDI_CB_EOA;
356 s->async->events |= COMEDI_CB_ERROR;
357 comedi_event(this_usbduxfastsub->comedidev, s);
358 // stop the transfer w/o unlink
359 usbduxfast_ai_stop(this_usbduxfastsub, 0);
363 printk("comedi%d: usbduxfast: non-zero urb status received in ai intr context: %d\n", this_usbduxfastsub->comedidev->minor, urb->status);
364 s->async->events |= COMEDI_CB_EOA;
365 s->async->events |= COMEDI_CB_ERROR;
366 comedi_event(this_usbduxfastsub->comedidev, s);
367 usbduxfast_ai_stop(this_usbduxfastsub, 0);
371 p = urb->transfer_buffer;
372 if (!this_usbduxfastsub->ignore) {
373 if (!(this_usbduxfastsub->ai_continous)) {
374 // not continous, fixed number of samples
375 n = urb->actual_length / sizeof(uint16_t);
376 if (unlikely(this_usbduxfastsub->ai_sample_count < n)) {
377 // we have send only a fraction of the bytes received
378 cfc_write_array_to_buffer(s,
379 urb->transfer_buffer,
380 this_usbduxfastsub->ai_sample_count *
382 usbduxfast_ai_stop(this_usbduxfastsub, 0);
383 // say comedi that the acquistion is over
384 s->async->events |= COMEDI_CB_EOA;
385 comedi_event(this_usbduxfastsub->comedidev, s);
388 this_usbduxfastsub->ai_sample_count -= n;
390 // write the full buffer to comedi
391 cfc_write_array_to_buffer(s,
392 urb->transfer_buffer, urb->actual_length);
394 // tell comedi that data is there
395 comedi_event(this_usbduxfastsub->comedidev, s);
398 // ignore this packet
399 this_usbduxfastsub->ignore--;
402 // command is still running
403 // resubmit urb for BULK transfer
404 urb->dev = this_usbduxfastsub->usbdev;
406 err = usb_submit_urb(urb, GFP_ATOMIC);
408 printk("comedi%d: usbduxfast: urb resubm failed: %d",
409 this_usbduxfastsub->comedidev->minor, err);
410 s->async->events |= COMEDI_CB_EOA;
411 s->async->events |= COMEDI_CB_ERROR;
412 comedi_event(this_usbduxfastsub->comedidev, s);
413 usbduxfast_ai_stop(this_usbduxfastsub, 0);
417 static int usbduxfastsub_start(usbduxfastsub_t * usbduxfastsub)
420 unsigned char local_transfer_buffer[16];
422 if (usbduxfastsub->probed) {
424 local_transfer_buffer[0] = 0;
425 errcode = usb_control_msg(usbduxfastsub->usbdev,
426 // create a pipe for a control transfer
427 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
428 // bRequest, "Firmware"
429 USBDUXFASTSUB_FIRMWARE,
436 // address of the transfer buffer
437 local_transfer_buffer,
443 printk("comedi_: usbduxfast_: control msg failed (start)\n");
450 static int usbduxfastsub_stop(usbduxfastsub_t * usbduxfastsub)
454 unsigned char local_transfer_buffer[16];
455 if (usbduxfastsub->probed) {
457 local_transfer_buffer[0] = 1;
458 errcode = usb_control_msg(usbduxfastsub->usbdev,
459 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
460 // bRequest, "Firmware"
461 USBDUXFASTSUB_FIRMWARE,
467 0x0000, local_transfer_buffer,
473 printk("comedi_: usbduxfast: control msg failed (stop)\n");
480 static int usbduxfastsub_upload(usbduxfastsub_t * usbduxfastsub,
481 unsigned char *local_transfer_buffer,
482 unsigned int startAddr, unsigned int len)
486 if (usbduxfastsub->probed) {
487 #ifdef CONFIG_COMEDI_DEBUG
488 printk("comedi%d: usbduxfast: uploading %d bytes",
489 usbduxfastsub->comedidev->minor, len);
490 printk(" to addr %d, first byte=%d.\n",
491 startAddr, local_transfer_buffer[0]);
493 errcode = usb_control_msg(usbduxfastsub->usbdev,
494 usb_sndctrlpipe(usbduxfastsub->usbdev, 0),
495 // brequest, firmware
496 USBDUXFASTSUB_FIRMWARE,
503 // our local safe buffer
504 local_transfer_buffer,
509 #ifdef CONFIG_COMEDI_DEBUG
510 printk("comedi_: usbduxfast: result=%d\n", errcode);
513 printk("comedi_: usbduxfast: uppload failed\n");
517 // no device on the bus for this index
523 int firmwareUpload(usbduxfastsub_t * usbduxfastsub,
524 unsigned char *firmwareBinary, int sizeFirmware)
528 if (!firmwareBinary) {
531 ret = usbduxfastsub_stop(usbduxfastsub);
533 printk("comedi_: usbduxfast: can not stop firmware\n");
536 ret = usbduxfastsub_upload(usbduxfastsub,
537 firmwareBinary, 0, sizeFirmware);
539 printk("comedi_: usbduxfast: firmware upload failed\n");
542 ret = usbduxfastsub_start(usbduxfastsub);
544 printk("comedi_: usbduxfast: can not start firmware\n");
550 int usbduxfastsub_submit_InURBs(usbduxfastsub_t * usbduxfastsub)
554 if (!usbduxfastsub) {
557 usb_fill_bulk_urb(usbduxfastsub->urbIn,
558 usbduxfastsub->usbdev,
559 usb_rcvbulkpipe(usbduxfastsub->usbdev, BULKINEP),
560 usbduxfastsub->transfer_buffer,
561 SIZEINBUF, usbduxfastsub_ai_Irq, usbduxfastsub->comedidev);
563 #ifdef CONFIG_COMEDI_DEBUG
564 printk("comedi%d: usbduxfast: submitting in-urb: %x,%x\n",
565 usbduxfastsub->comedidev->minor,
566 (int)(usbduxfastsub->urbIn->context),
567 (int)(usbduxfastsub->urbIn->dev));
569 errFlag = usb_submit_urb(usbduxfastsub->urbIn, GFP_ATOMIC);
571 printk("comedi_: usbduxfast: ai: usb_submit_urb error %d\n",
578 static int usbduxfast_ai_cmdtest(comedi_device * dev,
579 comedi_subdevice * s, comedi_cmd * cmd)
581 int err = 0, stop_mask = 0;
582 long int steps, tmp = 0;
584 usbduxfastsub_t *this_usbduxfastsub = dev->private;
585 if (!(this_usbduxfastsub->probed)) {
588 #ifdef CONFIG_COMEDI_DEBUG
589 printk("comedi%d: usbduxfast_ai_cmdtest\n", dev->minor);
590 printk("comedi%d: usbduxfast: convert_arg=%u scan_begin_arg=%u\n",
591 dev->minor, cmd->convert_arg, cmd->scan_begin_arg);
593 /* step 1: make sure trigger sources are trivially valid */
595 tmp = cmd->start_src;
596 cmd->start_src &= TRIG_NOW | TRIG_EXT | TRIG_INT;
597 if (!cmd->start_src || tmp != cmd->start_src)
600 tmp = cmd->scan_begin_src;
601 cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT;
602 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
605 tmp = cmd->convert_src;
606 cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
607 if (!cmd->convert_src || tmp != cmd->convert_src)
610 tmp = cmd->scan_end_src;
611 cmd->scan_end_src &= TRIG_COUNT;
612 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
616 stop_mask = TRIG_COUNT | TRIG_NONE;
617 cmd->stop_src &= stop_mask;
618 if (!cmd->stop_src || tmp != cmd->stop_src)
624 /* step 2: make sure trigger sources are unique and mutually compatible */
626 if (cmd->start_src != TRIG_NOW &&
627 cmd->start_src != TRIG_EXT && cmd->start_src != TRIG_INT)
629 if (cmd->scan_begin_src != TRIG_TIMER &&
630 cmd->scan_begin_src != TRIG_FOLLOW &&
631 cmd->scan_begin_src != TRIG_EXT)
633 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
635 if (cmd->stop_src != TRIG_COUNT &&
636 cmd->stop_src != TRIG_EXT && cmd->stop_src != TRIG_NONE)
639 // can't have external stop and start triggers at once
640 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
646 /* step 3: make sure arguments are trivially compatible */
648 if (cmd->start_src == TRIG_NOW && cmd->start_arg != 0) {
653 if (!cmd->chanlist_len) {
656 if (cmd->scan_end_arg != cmd->chanlist_len) {
657 cmd->scan_end_arg = cmd->chanlist_len;
661 if (cmd->chanlist_len == 1) {
664 minSamplPer = MIN_SAMPLING_PERIOD;
667 if (cmd->convert_src == TRIG_TIMER) {
668 steps = cmd->convert_arg * 30;
669 if (steps < (minSamplPer * 1000)) {
670 steps = minSamplPer * 1000;
672 if (steps > (MAX_SAMPLING_PERIOD * 1000)) {
673 steps = MAX_SAMPLING_PERIOD * 1000;
677 if (cmd->convert_arg != tmp) {
678 cmd->convert_arg = tmp;
683 if (cmd->scan_begin_src == TRIG_TIMER) {
687 switch (cmd->stop_src) {
689 if (!cmd->stop_arg) {
695 if (cmd->stop_arg != 0) {
700 // TRIG_EXT doesn't care since it doesn't trigger off a numbered channel
708 /* step 4: fix up any arguments */
714 static int usbduxfast_ai_inttrig(comedi_device * dev,
715 comedi_subdevice * s, unsigned int trignum)
718 usbduxfastsub_t *this_usbduxfastsub = dev->private;
719 if (!this_usbduxfastsub) {
722 down(&this_usbduxfastsub->sem);
723 if (!(this_usbduxfastsub->probed)) {
724 up(&this_usbduxfastsub->sem);
727 #ifdef CONFIG_COMEDI_DEBUG
728 printk("comedi%d: usbduxfast_ai_inttrig\n", dev->minor);
732 printk("comedi%d: usbduxfast_ai_inttrig: invalid trignum\n",
734 up(&this_usbduxfastsub->sem);
737 if (!(this_usbduxfastsub->ai_cmd_running)) {
738 this_usbduxfastsub->ai_cmd_running = 1;
739 ret = usbduxfastsub_submit_InURBs(this_usbduxfastsub);
741 printk("comedi%d: usbduxfast_ai_inttrig: urbSubmit: err=%d\n", dev->minor, ret);
742 this_usbduxfastsub->ai_cmd_running = 0;
743 up(&this_usbduxfastsub->sem);
746 s->async->inttrig = NULL;
748 printk("comedi%d: ai_inttrig but acqu is already running\n",
751 up(&this_usbduxfastsub->sem);
755 // offsets for the GPIF bytes
756 // the first byte is the command byte
757 #define LENBASE 1+0x00
758 #define OPBASE 1+0x08
759 #define OUTBASE 1+0x10
760 #define LOGBASE 1+0x18
762 static int usbduxfast_ai_cmd(comedi_device * dev, comedi_subdevice * s)
764 comedi_cmd *cmd = &s->async->cmd;
765 unsigned int chan, gain, rngmask = 0xff;
767 usbduxfastsub_t *this_usbduxfastsub = dev->private;
769 long steps, steps_tmp;
771 #ifdef CONFIG_COMEDI_DEBUG
772 printk("comedi%d: usbduxfast_ai_cmd\n", dev->minor);
774 if (!this_usbduxfastsub) {
777 down(&this_usbduxfastsub->sem);
778 if (!(this_usbduxfastsub->probed)) {
779 up(&this_usbduxfastsub->sem);
782 if (this_usbduxfastsub->ai_cmd_running) {
783 printk("comedi%d: ai_cmd not possible. Another ai_cmd is running.\n", dev->minor);
784 up(&this_usbduxfastsub->sem);
787 // set current channel of the running aquisition to zero
788 s->async->cur_chan = 0;
790 // ignore the first buffers from the device if there is an error condition
791 this_usbduxfastsub->ignore = PACKETS_TO_IGNORE;
793 if (cmd->chanlist_len > 0) {
794 gain = CR_RANGE(cmd->chanlist[0]);
795 for (i = 0; i < cmd->chanlist_len; ++i) {
796 chan = CR_CHAN(cmd->chanlist[i]);
798 printk("comedi%d: cmd is accepting only consecutive channels.\n", dev->minor);
799 up(&this_usbduxfastsub->sem);
802 if ((gain != CR_RANGE(cmd->chanlist[i]))
803 && (cmd->chanlist_len > 3)) {
804 printk("comedi%d: the gain must be the same for all channels.\n", dev->minor);
805 up(&this_usbduxfastsub->sem);
808 if (i >= NUMCHANNELS) {
809 printk("comedi%d: channel list too long\n",
816 if (cmd->scan_begin_src == TRIG_TIMER) {
817 printk("comedi%d: usbduxfast: scan_begin_src==TRIG_TIMER not valid.\n", dev->minor);
818 up(&this_usbduxfastsub->sem);
821 if (cmd->convert_src == TRIG_TIMER) {
822 steps = (cmd->convert_arg * 30) / 1000;
824 if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
825 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);
826 up(&this_usbduxfastsub->sem);
829 if (steps > MAX_SAMPLING_PERIOD) {
830 printk("comedi%d: usbduxfast: ai_cmd: sampling rate too low.\n",
832 up(&this_usbduxfastsub->sem);
835 if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
836 && (cmd->chanlist_len != 16)) {
837 printk("comedi%d: usbduxfast: ai_cmd: TRIG_EXT only with 1 or 16 channels possible.\n", dev->minor);
838 up(&this_usbduxfastsub->sem);
841 #ifdef CONFIG_COMEDI_DEBUG
842 printk("comedi%d: usbduxfast: steps=%ld, convert_arg=%u, ai_timer=%u\n",
844 steps, cmd->convert_arg, this_usbduxfastsub->ai_timer);
847 switch (cmd->chanlist_len) {
850 if (CR_RANGE(cmd->chanlist[0]) > 0)
851 rngmask = 0xff - 0x04;
855 // for external trigger: looping in this state until the RDY0 pin
857 if (cmd->start_src == TRIG_EXT) { // we loop here until ready has been set
858 this_usbduxfastsub->dux_commands[LENBASE + 0] = 0x01; // branch back to state 0
859 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x01; // deceision state w/o data
860 this_usbduxfastsub->dux_commands[OUTBASE + 0] =
862 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0x00; // RDY0 = 0
863 } else { // we just proceed to state 1
864 this_usbduxfastsub->dux_commands[LENBASE + 0] = 1;
865 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0;
866 this_usbduxfastsub->dux_commands[OUTBASE + 0] =
868 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
871 if (steps < MIN_SAMPLING_PERIOD) {
872 // for fast single channel aqu without mux
874 // we just stay here at state 1 and rexecute the same state
875 // this gives us 30MHz sampling rate
876 this_usbduxfastsub->dux_commands[LENBASE + 1] = 0x89; // branch back to state 1
877 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x03; // deceision state with data
878 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
880 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0xFF; // doesn't matter
882 // we loop through two states: data and delay: max rate is 15Mhz
883 this_usbduxfastsub->dux_commands[LENBASE + 1] =
885 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x02; // data
886 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
888 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0; // doesn't matter
890 this_usbduxfastsub->dux_commands[LENBASE + 2] = 0x09; // branch back to state 1
891 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0x01; // deceision state w/o data
892 this_usbduxfastsub->dux_commands[OUTBASE + 2] =
894 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0xFF; // doesn't matter
897 // we loop through 3 states: 2x delay and 1x data. This gives a min
898 // sampling rate of 60kHz.
900 // we have 1 state with duration 1
903 // do the first part of the delay
904 this_usbduxfastsub->dux_commands[LENBASE + 1] =
906 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0;
907 this_usbduxfastsub->dux_commands[OUTBASE + 1] =
909 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
911 // and the second part
912 this_usbduxfastsub->dux_commands[LENBASE + 2] =
914 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
915 this_usbduxfastsub->dux_commands[OUTBASE + 2] =
917 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
919 // get the data and branch back
920 this_usbduxfastsub->dux_commands[LENBASE + 3] = 0x09; // branch back to state 1
921 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0x03; // deceision state w data
922 this_usbduxfastsub->dux_commands[OUTBASE + 3] =
924 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0xFF; // doesn't matter
930 // commit data to the FIFO
931 if (CR_RANGE(cmd->chanlist[0]) > 0)
932 rngmask = 0xff - 0x04;
935 this_usbduxfastsub->dux_commands[LENBASE + 0] = 1;
936 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x02; // data
937 this_usbduxfastsub->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
938 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
940 // we have 1 state with duration 1: state 0
941 steps_tmp = steps - 1;
943 if (CR_RANGE(cmd->chanlist[1]) > 0)
944 rngmask = 0xff - 0x04;
947 // do the first part of the delay
948 this_usbduxfastsub->dux_commands[LENBASE + 1] = steps_tmp / 2;
949 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0;
950 this_usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFE & rngmask; //count
951 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
953 // and the second part
954 this_usbduxfastsub->dux_commands[LENBASE + 2] =
955 steps_tmp - steps_tmp / 2;
956 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
957 this_usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
958 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
960 this_usbduxfastsub->dux_commands[LENBASE + 3] = 1;
961 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0x02; // data
962 this_usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
963 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
965 // we have 2 states with duration 1: step 6 and the IDLE state
966 steps_tmp = steps - 2;
968 if (CR_RANGE(cmd->chanlist[0]) > 0)
969 rngmask = 0xff - 0x04;
972 // do the first part of the delay
973 this_usbduxfastsub->dux_commands[LENBASE + 4] = steps_tmp / 2;
974 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0;
975 this_usbduxfastsub->dux_commands[OUTBASE + 4] = (0xFF - 0x02) & rngmask; //reset
976 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
978 // and the second part
979 this_usbduxfastsub->dux_commands[LENBASE + 5] =
980 steps_tmp - steps_tmp / 2;
981 this_usbduxfastsub->dux_commands[OPBASE + 5] = 0;
982 this_usbduxfastsub->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
983 this_usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
985 this_usbduxfastsub->dux_commands[LENBASE + 6] = 1;
986 this_usbduxfastsub->dux_commands[OPBASE + 6] = 0;
987 this_usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
988 this_usbduxfastsub->dux_commands[LOGBASE + 6] = 0;
993 for (j = 0; j < 1; j++) {
994 if (CR_RANGE(cmd->chanlist[j]) > 0)
995 rngmask = 0xff - 0x04;
998 // commit data to the FIFO and do the first part of the delay
999 this_usbduxfastsub->dux_commands[LENBASE + j * 2] =
1001 this_usbduxfastsub->dux_commands[OPBASE + j * 2] = 0x02; // data
1002 this_usbduxfastsub->dux_commands[OUTBASE + j * 2] = 0xFF & rngmask; // no change
1003 this_usbduxfastsub->dux_commands[LOGBASE + j * 2] = 0;
1005 if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
1006 rngmask = 0xff - 0x04;
1009 // do the second part of the delay
1010 this_usbduxfastsub->dux_commands[LENBASE + j * 2 + 1] =
1012 this_usbduxfastsub->dux_commands[OPBASE + j * 2 + 1] = 0; // no data
1013 this_usbduxfastsub->dux_commands[OUTBASE + j * 2 + 1] = 0xFE & rngmask; //count
1014 this_usbduxfastsub->dux_commands[LOGBASE + j * 2 + 1] =
1018 // 2 steps with duration 1: the idele step and step 6:
1019 steps_tmp = steps - 2;
1020 // commit data to the FIFO and do the first part of the delay
1021 this_usbduxfastsub->dux_commands[LENBASE + 4] = steps_tmp / 2;
1022 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0x02; // data
1023 this_usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFF & rngmask; // no change
1024 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1026 if (CR_RANGE(cmd->chanlist[0]) > 0)
1027 rngmask = 0xff - 0x04;
1030 // do the second part of the delay
1031 this_usbduxfastsub->dux_commands[LENBASE + 5] =
1032 steps_tmp - steps_tmp / 2;
1033 this_usbduxfastsub->dux_commands[OPBASE + 5] = 0; // no data
1034 this_usbduxfastsub->dux_commands[OUTBASE + 5] = (0xFF - 0x02) & rngmask; // reset
1035 this_usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1037 this_usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1038 this_usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1039 this_usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1040 this_usbduxfastsub->dux_commands[LOGBASE + 6] = 0;
1043 if (CR_RANGE(cmd->chanlist[0]) > 0)
1044 rngmask = 0xff - 0x04;
1047 if (cmd->start_src == TRIG_EXT) { // we loop here until ready has been set
1048 this_usbduxfastsub->dux_commands[LENBASE + 0] = 0x01; // branch back to state 0
1049 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0x01; // deceision state w/o data
1050 this_usbduxfastsub->dux_commands[OUTBASE + 0] = (0xFF - 0x02) & rngmask; // reset
1051 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0x00; // RDY0 = 0
1052 } else { // we just proceed to state 1
1053 this_usbduxfastsub->dux_commands[LENBASE + 0] = 255; // 30us reset pulse
1054 this_usbduxfastsub->dux_commands[OPBASE + 0] = 0;
1055 this_usbduxfastsub->dux_commands[OUTBASE + 0] = (0xFF - 0x02) & rngmask; // reset
1056 this_usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1059 // commit data to the FIFO
1060 this_usbduxfastsub->dux_commands[LENBASE + 1] = 1;
1061 this_usbduxfastsub->dux_commands[OPBASE + 1] = 0x02; // data
1062 this_usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
1063 this_usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
1065 // we have 2 states with duration 1
1068 // do the first part of the delay
1069 this_usbduxfastsub->dux_commands[LENBASE + 2] = steps / 2;
1070 this_usbduxfastsub->dux_commands[OPBASE + 2] = 0;
1071 this_usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1072 this_usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
1074 // and the second part
1075 this_usbduxfastsub->dux_commands[LENBASE + 3] =
1077 this_usbduxfastsub->dux_commands[OPBASE + 3] = 0;
1078 this_usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
1079 this_usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
1081 this_usbduxfastsub->dux_commands[LENBASE + 4] = 0x09; // branch back to state 1
1082 this_usbduxfastsub->dux_commands[OPBASE + 4] = 0x01; // deceision state w/o data
1083 this_usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1084 this_usbduxfastsub->dux_commands[LOGBASE + 4] = 0xFF; // doesn't matter
1089 printk("comedi %d: unsupported combination of channels\n",
1091 up(&this_usbduxfastsub->sem);
1095 #ifdef CONFIG_COMEDI_DEBUG
1096 printk("comedi %d: sending commands to the usb device\n", dev->minor);
1098 // 0 means that the AD commands are sent
1099 result = send_dux_commands(this_usbduxfastsub, SENDADCOMMANDS);
1101 printk("comedi%d: adc command could not be submitted. Aborting...\n", dev->minor);
1102 up(&this_usbduxfastsub->sem);
1105 if (cmd->stop_src == TRIG_COUNT) {
1106 this_usbduxfastsub->ai_sample_count =
1107 (cmd->stop_arg) * (cmd->scan_end_arg);
1108 if (usbduxfastsub->ai_sample_count < 1) {
1109 printk("comedi%d: (cmd->stop_arg)*(cmd->scan_end_arg)<1, aborting.\n", dev->minor);
1110 up(&this_usbduxfastsub->sem);
1113 this_usbduxfastsub->ai_continous = 0;
1115 // continous aquisition
1116 this_usbduxfastsub->ai_continous = 1;
1117 this_usbduxfastsub->ai_sample_count = 0;
1120 if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
1121 // enable this acquisition operation
1122 this_usbduxfastsub->ai_cmd_running = 1;
1123 ret = usbduxfastsub_submit_InURBs(this_usbduxfastsub);
1125 this_usbduxfastsub->ai_cmd_running = 0;
1126 // fixme: unlink here??
1127 up(&this_usbduxfastsub->sem);
1130 s->async->inttrig = NULL;
1133 // don't enable the acquision operation
1134 // wait for an internal signal
1135 s->async->inttrig = usbduxfast_ai_inttrig;
1137 up(&this_usbduxfastsub->sem);
1142 /* Mode 0 is used to get a single conversion on demand */
1143 static int usbduxfast_ai_insn_read(comedi_device * dev,
1144 comedi_subdevice * s, comedi_insn * insn, lsampl_t * data)
1146 int i, j, n, actual_length;
1147 int chan, range, rngmask;
1149 usbduxfastsub_t *usbduxfastsub = dev->private;
1151 if (!usbduxfastsub) {
1152 printk("comedi%d: ai_insn_read: no usb dev.\n", dev->minor);
1155 #ifdef CONFIG_COMEDI_DEBUG
1156 printk("comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1157 dev->minor, insn->n, insn->subdev);
1159 down(&usbduxfastsub->sem);
1160 if (!(usbduxfastsub->probed)) {
1161 up(&usbduxfastsub->sem);
1164 if (usbduxfastsub->ai_cmd_running) {
1165 printk("comedi%d: ai_insn_read not possible. Async Command is running.\n", dev->minor);
1166 up(&usbduxfastsub->sem);
1169 // sample one channel
1170 chan = CR_CHAN(insn->chanspec);
1171 range = CR_RANGE(insn->chanspec);
1172 // set command for the first channel
1175 rngmask = 0xff - 0x04;
1178 // commit data to the FIFO
1179 usbduxfastsub->dux_commands[LENBASE + 0] = 1;
1180 usbduxfastsub->dux_commands[OPBASE + 0] = 0x02; // data
1181 usbduxfastsub->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
1182 usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1184 // do the first part of the delay
1185 usbduxfastsub->dux_commands[LENBASE + 1] = 12;
1186 usbduxfastsub->dux_commands[OPBASE + 1] = 0;
1187 usbduxfastsub->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
1188 usbduxfastsub->dux_commands[LOGBASE + 1] = 0;
1190 usbduxfastsub->dux_commands[LENBASE + 2] = 1;
1191 usbduxfastsub->dux_commands[OPBASE + 2] = 0;
1192 usbduxfastsub->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1193 usbduxfastsub->dux_commands[LOGBASE + 2] = 0;
1195 usbduxfastsub->dux_commands[LENBASE + 3] = 1;
1196 usbduxfastsub->dux_commands[OPBASE + 3] = 0;
1197 usbduxfastsub->dux_commands[OUTBASE + 3] = 0xFE & rngmask;
1198 usbduxfastsub->dux_commands[LOGBASE + 3] = 0;
1200 usbduxfastsub->dux_commands[LENBASE + 4] = 1;
1201 usbduxfastsub->dux_commands[OPBASE + 4] = 0;
1202 usbduxfastsub->dux_commands[OUTBASE + 4] = 0xFE & rngmask;
1203 usbduxfastsub->dux_commands[LOGBASE + 4] = 0;
1206 usbduxfastsub->dux_commands[LENBASE + 5] = 12;
1207 usbduxfastsub->dux_commands[OPBASE + 5] = 0;
1208 usbduxfastsub->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1209 usbduxfastsub->dux_commands[LOGBASE + 5] = 0;
1211 usbduxfastsub->dux_commands[LENBASE + 6] = 1;
1212 usbduxfastsub->dux_commands[OPBASE + 6] = 0;
1213 usbduxfastsub->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1214 usbduxfastsub->dux_commands[LOGBASE + 0] = 0;
1216 #ifdef CONFIG_COMEDI_DEBUG
1217 printk("comedi %d: sending commands to the usb device\n", dev->minor);
1219 // 0 means that the AD commands are sent
1220 err = send_dux_commands(usbduxfastsub, SENDADCOMMANDS);
1222 printk("comedi%d: adc command could not be submitted. Aborting...\n", dev->minor);
1223 up(&usbduxfastsub->sem);
1226 #ifdef CONFIG_COMEDI_DEBUG
1227 printk("comedi%d: usbduxfast: submitting in-urb: %x,%x\n",
1228 usbduxfastsub->comedidev->minor,
1229 (int)(usbduxfastsub->urbIn->context),
1230 (int)(usbduxfastsub->urbIn->dev));
1232 for (i = 0; i < PACKETS_TO_IGNORE; i++) {
1233 err = usb_bulk_msg(usbduxfastsub->usbdev,
1234 usb_rcvbulkpipe(usbduxfastsub->usbdev,
1236 usbduxfastsub->transfer_buffer, SIZEINBUF,
1237 &actual_length, 10000);
1239 printk("comedi%d: insn timeout. No data.\n",
1241 up(&usbduxfastsub->sem);
1246 for (i = 0; i < insn->n;) {
1247 err = usb_bulk_msg(usbduxfastsub->usbdev,
1248 usb_rcvbulkpipe(usbduxfastsub->usbdev,
1250 usbduxfastsub->transfer_buffer, SIZEINBUF,
1251 &actual_length, 10000);
1253 printk("comedi%d: insn data error: %d\n",
1255 up(&usbduxfastsub->sem);
1258 n = actual_length / sizeof(uint16_t);
1259 if ((n % 16) != 0) {
1260 printk("comedi%d: insn data packet corrupted.\n",
1262 up(&usbduxfastsub->sem);
1265 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
1267 ((uint16_t *) (usbduxfastsub->
1268 transfer_buffer))[j];
1272 up(&usbduxfastsub->sem);
1276 static unsigned hex2unsigned(char *h)
1280 hi = h[0] - 'A' + 0x0a;
1285 lo = h[1] - 'A' + 0x0a;
1289 return hi * 0x10 + lo;
1293 #define FIRMWARE_MAX_LEN 0x2000
1295 // taken from David Brownell's fxload and adjusted for this driver
1296 static int read_firmware(usbduxfastsub_t * usbduxfastsub, void *firmwarePtr,
1300 unsigned char *fp = (char *)firmwarePtr;
1301 unsigned char *firmwareBinary = NULL;
1305 firmwareBinary = kmalloc(FIRMWARE_MAX_LEN, GFP_KERNEL);
1306 if (!firmwareBinary) {
1307 printk("comedi_: usbduxfast: mem alloc for firmware failed\n");
1319 while ((i < size) && (fp[i] != 13) && (fp[i] != 10)) {
1323 if (j >= sizeof(buf)) {
1324 printk("comedi_: usbduxfast: bogus firmware file!\n");
1328 // get rid of LF/CR/...
1329 while ((i < size) && ((fp[i] == 13) || (fp[i] == 10)
1335 //printk("comedi_: buf=%s\n",buf);
1337 /* EXTENSION: "# comment-till-end-of-line", for copyrights etc */
1341 if (buf[0] != ':') {
1342 printk("comedi_: usbduxfast: upload: not an ihex record: %s", buf);
1346 /* Read the length field (up to 16 bytes) */
1347 len = hex2unsigned(buf + 1);
1349 /* Read the target offset */
1350 off = (hex2unsigned(buf + 3) * 0x0100) + hex2unsigned(buf + 5);
1352 if ((off + len) > maxAddr) {
1353 maxAddr = off + len;
1356 if (maxAddr >= FIRMWARE_MAX_LEN) {
1357 printk("comedi_: usbduxfast: firmware upload goes beyond FX2 RAM boundaries.");
1360 //printk("comedi_: usbduxfast: off=%x, len=%x:",off,len);
1362 /* Read the record type */
1363 type = hex2unsigned(buf + 7);
1365 /* If this is an EOF record, then make it so. */
1371 printk("comedi_: usbduxfast: unsupported record type: %u\n", type);
1375 for (idx = 0, cp = buf + 9; idx < len; idx += 1, cp += 2) {
1376 firmwareBinary[idx + off] = hex2unsigned(cp);
1377 //printk("%02x ",firmwareBinary[idx+off]);
1382 printk("comedi_: usbduxfast: unexpected end of hex file\n");
1387 res = firmwareUpload(usbduxfastsub, firmwareBinary, maxAddr + 1);
1388 kfree(firmwareBinary);
1392 static void tidy_up(usbduxfastsub_t * usbduxfastsub_tmp)
1394 #ifdef CONFIG_COMEDI_DEBUG
1395 printk("comedi_: usbduxfast: tiding up\n");
1397 if (!usbduxfastsub_tmp) {
1400 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
1401 // shows the usb subsystem that the driver is down
1402 if (usbduxfastsub_tmp->interface) {
1403 usb_set_intfdata(usbduxfastsub_tmp->interface, NULL);
1407 usbduxfastsub_tmp->probed = 0;
1409 if (usbduxfastsub_tmp->urbIn) {
1410 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)
1411 // waits until a running transfer is over
1412 // thus, under 2.4 hotplugging while a command
1413 // is running is not safe
1414 usb_kill_urb(usbduxfastsub_tmp->urbIn);
1416 if (usbduxfastsub_tmp->transfer_buffer) {
1417 kfree(usbduxfastsub_tmp->transfer_buffer);
1418 usbduxfastsub_tmp->transfer_buffer = NULL;
1420 usb_free_urb(usbduxfastsub_tmp->urbIn);
1421 usbduxfastsub_tmp->urbIn = NULL;
1423 if (usbduxfastsub_tmp->insnBuffer) {
1424 kfree(usbduxfastsub_tmp->insnBuffer);
1425 usbduxfastsub_tmp->insnBuffer = NULL;
1427 if (usbduxfastsub_tmp->dux_commands) {
1428 kfree(usbduxfastsub_tmp->dux_commands);
1429 usbduxfastsub_tmp->dux_commands = NULL;
1431 usbduxfastsub_tmp->ai_cmd_running = 0;
1434 // allocate memory for the urbs and initialise them
1435 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1436 static void *usbduxfastsub_probe(struct usb_device *udev,
1437 unsigned int interfnum, const struct usb_device_id *id)
1440 static int usbduxfastsub_probe(struct usb_interface *uinterf,
1441 const struct usb_device_id *id)
1443 struct usb_device *udev = interface_to_usbdev(uinterf);
1448 if (udev->speed != USB_SPEED_HIGH) {
1449 printk("comedi_: usbduxfast_: This driver needs USB 2.0 to operate. Aborting...\n");
1452 #ifdef CONFIG_COMEDI_DEBUG
1453 printk("comedi_: usbduxfast_: finding a free structure for the usb-device\n");
1455 down(&start_stop_sem);
1456 // look for a free place in the usbduxfast array
1458 for (i = 0; i < NUMUSBDUXFAST; i++) {
1459 if (!(usbduxfastsub[i].probed)) {
1467 printk("Too many usbduxfast-devices connected.\n");
1468 up(&start_stop_sem);
1471 #ifdef CONFIG_COMEDI_DEBUG
1472 printk("comedi_: usbduxfast: usbduxfastsub[%d] is ready to connect to comedi.\n", index);
1475 init_MUTEX(&(usbduxfastsub[index].sem));
1476 // save a pointer to the usb device
1477 usbduxfastsub[index].usbdev = udev;
1479 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1480 // save the interface number
1481 usbduxfastsub[index].ifnum = interfnum;
1483 // 2.6: save the interface itself
1484 usbduxfastsub[index].interface = uinterf;
1485 // get the interface number from the interface
1486 usbduxfastsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
1487 // hand the private data over to the usb subsystem
1488 // will be needed for disconnect
1489 usb_set_intfdata(uinterf, &(usbduxfastsub[index]));
1492 #ifdef CONFIG_COMEDI_DEBUG
1493 printk("comedi_: usbduxfast: ifnum=%d\n", usbduxfastsub[index].ifnum);
1495 // create space for the commands going to the usb device
1496 usbduxfastsub[index].dux_commands = kmalloc(SIZEOFDUXBUFFER,
1498 if (!usbduxfastsub[index].dux_commands) {
1499 printk("comedi_: usbduxfast: error alloc space for dac commands\n");
1500 tidy_up(&(usbduxfastsub[index]));
1501 up(&start_stop_sem);
1504 // create space of the instruction buffer
1505 usbduxfastsub[index].insnBuffer = kmalloc(SIZEINSNBUF, GFP_KERNEL);
1506 if (!(usbduxfastsub[index].insnBuffer)) {
1507 printk("comedi_: usbduxfast: could not alloc space for insnBuffer\n");
1508 tidy_up(&(usbduxfastsub[index]));
1509 up(&start_stop_sem);
1512 // setting to alternate setting 1: enabling bulk ep
1513 i = usb_set_interface(usbduxfastsub[index].usbdev,
1514 usbduxfastsub[index].ifnum, 1);
1516 printk("comedi_: usbduxfast%d: could not switch to alternate setting 1.\n", index);
1517 tidy_up(&(usbduxfastsub[index]));
1518 up(&start_stop_sem);
1521 usbduxfastsub[index].urbIn = usb_alloc_urb(0, GFP_KERNEL);
1522 if (usbduxfastsub[index].urbIn == NULL) {
1523 printk("comedi_: usbduxfast%d: Could not alloc. urb\n", index);
1524 tidy_up(&(usbduxfastsub[index]));
1525 up(&start_stop_sem);
1528 usbduxfastsub[index].transfer_buffer = kmalloc(SIZEINBUF, GFP_KERNEL);
1529 if (!(usbduxfastsub[index].transfer_buffer)) {
1530 printk("comedi_: usbduxfast%d: could not alloc. transb.\n",
1532 tidy_up(&(usbduxfastsub[index]));
1533 up(&start_stop_sem);
1536 // we've reached the bottom of the function
1537 usbduxfastsub[index].probed = 1;
1538 up(&start_stop_sem);
1539 printk("comedi_: usbduxfast%d has been successfully initialized.\n",
1541 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1542 return (void *)(&usbduxfastsub[index]);
1549 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
1550 static void usbduxfastsub_disconnect(struct usb_device *udev, void *ptr)
1552 usbduxfastsub_t *usbduxfastsub_tmp = (usbduxfastsub_t *) ptr;
1554 static void usbduxfastsub_disconnect(struct usb_interface *intf)
1556 usbduxfastsub_t *usbduxfastsub_tmp = usb_get_intfdata(intf);
1557 struct usb_device *udev = interface_to_usbdev(intf);
1559 if (!usbduxfastsub_tmp) {
1560 printk("comedi_: usbduxfast: disconnect called with null pointer.\n");
1563 if (usbduxfastsub_tmp->usbdev != udev) {
1564 printk("comedi_: usbduxfast: BUG! called with wrong ptr!!!\n");
1567 down(&start_stop_sem);
1568 down(&usbduxfastsub_tmp->sem);
1569 tidy_up(usbduxfastsub_tmp);
1570 up(&usbduxfastsub_tmp->sem);
1571 up(&start_stop_sem);
1572 #ifdef CONFIG_COMEDI_DEBUG
1573 printk("comedi_: usbduxfast: disconnected from the usb\n");
1577 // is called when comedi-config is called
1578 static int usbduxfast_attach(comedi_device * dev, comedi_devconfig * it)
1583 comedi_subdevice *s = NULL;
1584 dev->private = NULL;
1586 down(&start_stop_sem);
1587 // find a valid device which has been detected by the probe function of the usb
1589 for (i = 0; i < NUMUSBDUXFAST; i++) {
1590 if ((usbduxfastsub[i].probed) && (!usbduxfastsub[i].attached)) {
1597 printk("comedi%d: usbduxfast: error: attach failed, no usbduxfast devs connected to the usb bus.\n", dev->minor);
1598 up(&start_stop_sem);
1602 down(&(usbduxfastsub[index].sem));
1603 // pointer back to the corresponding comedi device
1604 usbduxfastsub[index].comedidev = dev;
1606 // trying to upload the firmware into the chip
1607 if (comedi_aux_data(it->options, 0) &&
1608 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
1609 read_firmware(usbduxfastsub,
1610 comedi_aux_data(it->options, 0),
1611 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
1614 dev->board_name = BOARDNAME;
1616 /* set number of subdevices */
1617 dev->n_subdevices = N_SUBDEVICES;
1619 // allocate space for the subdevices
1620 if ((ret = alloc_subdevices(dev, N_SUBDEVICES)) < 0) {
1621 printk("comedi%d: usbduxfast: error alloc space for subdev\n",
1623 up(&start_stop_sem);
1627 printk("comedi%d: usbduxfast: usb-device %d is attached to comedi.\n",
1629 // private structure is also simply the usb-structure
1630 dev->private = usbduxfastsub + index;
1631 // the first subdevice is the A/D converter
1632 s = dev->subdevices + SUBDEV_AD;
1633 // the URBs get the comedi subdevice
1634 // which is responsible for reading
1635 // this is the subdevice which reads data
1636 dev->read_subdev = s;
1637 // the subdevice receives as private structure the
1641 s->type = COMEDI_SUBD_AI;
1642 // readable and ref is to ground
1643 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1646 // length of the channellist
1647 s->len_chanlist = 16;
1648 // callback functions
1649 s->insn_read = usbduxfast_ai_insn_read;
1650 s->do_cmdtest = usbduxfast_ai_cmdtest;
1651 s->do_cmd = usbduxfast_ai_cmd;
1652 s->cancel = usbduxfast_ai_cancel;
1653 // max value from the A/D converter (12bit+1 bit for overflow)
1654 s->maxdata = 0x1000;
1655 // range table to convert to physical units
1656 s->range_table = &range_usbduxfast_ai_range;
1658 // finally decide that it's attached
1659 usbduxfastsub[index].attached = 1;
1661 up(&(usbduxfastsub[index].sem));
1663 up(&start_stop_sem);
1665 printk("comedi%d: successfully attached to usbduxfast.\n", dev->minor);
1670 static int usbduxfast_detach(comedi_device * dev)
1672 usbduxfastsub_t *usbduxfastsub_tmp;
1674 #ifdef CONFIG_COMEDI_DEBUG
1675 printk("comedi%d: usbduxfast: detach usb device\n", dev->minor);
1679 printk("comedi?: usbduxfast: detach without dev variable...\n");
1683 usbduxfastsub_tmp = dev->private;
1684 if (!usbduxfastsub_tmp) {
1685 printk("comedi?: usbduxfast: detach without ptr to usbduxfastsub[]\n");
1689 down(&usbduxfastsub_tmp->sem);
1690 down(&start_stop_sem);
1691 // Don't allow detach to free the private structure
1692 // It's one entry of of usbduxfastsub[]
1693 dev->private = NULL;
1694 usbduxfastsub_tmp->attached = 0;
1695 usbduxfastsub_tmp->comedidev = NULL;
1696 #ifdef CONFIG_COMEDI_DEBUG
1697 printk("comedi%d: usbduxfast: detach: successfully removed\n",
1700 up(&start_stop_sem);
1701 up(&usbduxfastsub_tmp->sem);
1705 /* main driver struct */
1706 static comedi_driver driver_usbduxfast = {
1707 driver_name:"usbduxfast",
1709 attach:usbduxfast_attach,
1710 detach:usbduxfast_detach,
1713 static void init_usb_devices(void)
1716 #ifdef CONFIG_COMEDI_DEBUG
1717 printk("comedi_: usbduxfast: setting all possible devs to invalid\n");
1719 // all devices entries are invalid to begin with
1720 // they will become valid by the probe function
1721 // and then finally by the attach-function
1722 for (index = 0; index < NUMUSBDUXFAST; index++) {
1723 memset(&(usbduxfastsub[index]), 0x00,
1724 sizeof(usbduxfastsub[index]));
1725 init_MUTEX(&(usbduxfastsub[index].sem));
1729 // Table with the USB-devices: just now only testing IDs
1730 static struct usb_device_id usbduxfastsub_table[] = {
1731 // { USB_DEVICE(0x4b4, 0x8613), //testing
1733 {USB_DEVICE(0x13d8, 0x0010) //real ID
1735 {USB_DEVICE(0x13d8, 0x0011) //real ID
1737 {} /* Terminating entry */
1740 MODULE_DEVICE_TABLE(usb, usbduxfastsub_table);
1742 // The usbduxfastsub-driver
1743 static struct usb_driver usbduxfastsub_driver = {
1744 #ifdef COMEDI_HAVE_USB_DRIVER_OWNER
1748 probe:usbduxfastsub_probe,
1749 disconnect:usbduxfastsub_disconnect,
1750 id_table:usbduxfastsub_table,
1753 // Can't use the nice macro as I have also to initialise the USB
1755 // registering the usb-system _and_ the comedi-driver
1756 static int init_usbduxfast(void)
1758 printk(KERN_INFO KBUILD_MODNAME ": "
1759 DRIVER_VERSION ":" DRIVER_DESC "\n");
1761 usb_register(&usbduxfastsub_driver);
1762 comedi_driver_register(&driver_usbduxfast);
1766 // deregistering the comedi driver and the usb-subsystem
1767 static void exit_usbduxfast(void)
1769 comedi_driver_unregister(&driver_usbduxfast);
1770 usb_deregister(&usbduxfastsub_driver);
1773 module_init(init_usbduxfast);
1774 module_exit(exit_usbduxfast);
1776 MODULE_AUTHOR(DRIVER_AUTHOR);
1777 MODULE_DESCRIPTION(DRIVER_DESC);
1778 MODULE_LICENSE("GPL");