2 * Copyright (C) 2004 Bernd Porr, Bernd.Porr@f2s.com
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * I must give credit here to Chris Baugher who
21 * wrote the driver for AT-MIO-16d. I used some parts of this
22 * driver. I also must give credits to David Brownell
23 * who supported me with the USB development.
29 * 0.9: Dropping the first data packet which seems to be from the last transfer.
30 * Buffer overflows in the FX2 are handed over to comedi.
31 * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
32 * Added insn command basically for testing. Sample rate is
34 * 0.99: Ian Abbott pointed out a bug which has been corrected. Thanks!
35 * 0.99a: added external trigger.
36 * 1.00: added firmware kernel request to the driver which fixed
37 * udev coldplug problem
40 #include <linux/kernel.h>
41 #include <linux/firmware.h>
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/slab.h>
45 #include <linux/input.h>
46 #include <linux/usb.h>
47 #include <linux/fcntl.h>
48 #include <linux/compiler.h>
49 #include "comedi_fc.h"
50 #include "../comedidev.h"
52 #define DRIVER_VERSION "v1.0"
53 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
54 #define DRIVER_DESC "USB-DUXfast, BerndPorr@f2s.com"
55 #define BOARDNAME "usbduxfast"
58 * timeout for the USB-transfer
63 * constants for "firmware" upload and download
65 #define USBDUXFASTSUB_FIRMWARE 0xA0
66 #define VENDOR_DIR_IN 0xC0
67 #define VENDOR_DIR_OUT 0x40
70 * internal addresses of the 8051 processor
72 #define USBDUXFASTSUB_CPUCS 0xE600
75 * max lenghth of the transfer-buffer for software upload
80 * input endpoint number
85 * endpoint for the A/D channellist: bulk OUT
87 #define CHANNELLISTEP 4
92 #define NUMCHANNELS 32
95 * size of the waveform descriptor
100 * size of one A/D value
102 #define SIZEADIN (sizeof(int16_t))
105 * size of the input-buffer IN BYTES
107 #define SIZEINBUF 512
112 #define SIZEINSNBUF 512
115 * size of the buffer for the dux commands in bytes
117 #define SIZEOFDUXBUFFER 256
120 * number of in-URBs which receive the data: min=5
122 #define NUMOFINBUFFERSHIGH 10
125 * total number of usbduxfast devices
127 #define NUMUSBDUXFAST 16
130 * number of subdevices
132 #define N_SUBDEVICES 1
135 * analogue in subdevice
140 * min delay steps for more than one channel
141 * basically when the mux gives up ;-)
143 * steps at 30MHz in the FX2
145 #define MIN_SAMPLING_PERIOD 9
148 * max number of 1/30MHz delay steps
150 #define MAX_SAMPLING_PERIOD 500
153 * number of received packets to ignore before we start handing data
154 * over to comedi, it's quad buffering and we have to ignore 4 packets
156 #define PACKETS_TO_IGNORE 4
161 static const struct comedi_lrange range_usbduxfast_ai_range
= {
162 2, {BIP_RANGE(0.75), BIP_RANGE(0.5)}
166 * private structure of one subdevice
168 * this is the structure which holds all the data of this driver
169 * one sub device just now: A/D
171 struct usbduxfastsub_s
{
172 int attached
; /* is attached? */
173 int probed
; /* is it associated with a subdevice? */
174 struct usb_device
*usbdev
; /* pointer to the usb-device */
175 struct urb
*urbIn
; /* BULK-transfer handling: urb */
176 int8_t *transfer_buffer
;
177 int16_t *insnBuffer
; /* input buffer for single insn */
178 int ifnum
; /* interface number */
179 struct usb_interface
*interface
; /* interface structure */
180 /* comedi device for the interrupt context */
181 struct comedi_device
*comedidev
;
182 short int ai_cmd_running
; /* asynchronous command is running */
183 short int ai_continous
; /* continous aquisition */
184 long int ai_sample_count
; /* number of samples to acquire */
185 uint8_t *dux_commands
; /* commands */
186 int ignore
; /* counter which ignores the first
188 struct semaphore sem
;
192 * The pointer to the private usb-data of the driver
193 * is also the private data for the comedi-device.
194 * This has to be global as the usb subsystem needs
195 * global variables. The other reason is that this
196 * structure must be there _before_ any comedi
197 * command is issued. The usb subsystem must be
198 * initialised before comedi can access it.
200 static struct usbduxfastsub_s usbduxfastsub
[NUMUSBDUXFAST
];
202 static DECLARE_MUTEX(start_stop_sem
);
205 * bulk transfers to usbduxfast
207 #define SENDADCOMMANDS 0
208 #define SENDINITEP6 1
210 static int send_dux_commands(struct usbduxfastsub_s
*udfs
, int cmd_type
)
214 udfs
->dux_commands
[0] = cmd_type
;
216 #ifdef CONFIG_COMEDI_DEBUG
217 printk(KERN_DEBUG
"comedi%d: usbduxfast: dux_commands: ",
218 udfs
->comedidev
->minor
);
219 for (tmp
= 0; tmp
< SIZEOFDUXBUFFER
; tmp
++)
220 printk(" %02x", udfs
->dux_commands
[tmp
]);
224 tmp
= usb_bulk_msg(udfs
->usbdev
,
225 usb_sndbulkpipe(udfs
->usbdev
, CHANNELLISTEP
),
226 udfs
->dux_commands
, SIZEOFDUXBUFFER
, &nsent
, 10000);
228 printk(KERN_ERR
"comedi%d: could not transmit dux_commands to"
229 "the usb-device, err=%d\n", udfs
->comedidev
->minor
, tmp
);
234 * Stops the data acquision.
235 * It should be safe to call this function from any context.
237 static int usbduxfastsub_unlink_InURBs(struct usbduxfastsub_s
*udfs
)
242 if (udfs
&& udfs
->urbIn
) {
243 udfs
->ai_cmd_running
= 0;
244 /* waits until a running transfer is over */
245 usb_kill_urb(udfs
->urbIn
);
248 #ifdef CONFIG_COMEDI_DEBUG
249 printk(KERN_DEBUG
"comedi: usbduxfast: unlinked InURB: res=%d\n", j
);
255 * This will stop a running acquisition operation.
256 * Is called from within this driver from both the
257 * interrupt context and from comedi.
259 static int usbduxfast_ai_stop(struct usbduxfastsub_s
*udfs
, int do_unlink
)
264 printk(KERN_ERR
"comedi?: usbduxfast_ai_stop: udfs=NULL!\n");
267 #ifdef CONFIG_COMEDI_DEBUG
268 printk(KERN_DEBUG
"comedi: usbduxfast_ai_stop\n");
271 udfs
->ai_cmd_running
= 0;
275 ret
= usbduxfastsub_unlink_InURBs(udfs
);
281 * This will cancel a running acquisition operation.
282 * This is called by comedi but never from inside the driver.
284 static int usbduxfast_ai_cancel(struct comedi_device
*dev
,
285 struct comedi_subdevice
*s
)
287 struct usbduxfastsub_s
*udfs
;
290 /* force unlink of all urbs */
291 #ifdef CONFIG_COMEDI_DEBUG
292 printk(KERN_DEBUG
"comedi: usbduxfast_ai_cancel\n");
296 printk(KERN_ERR
"comedi: usbduxfast_ai_cancel: udfs=NULL\n");
305 ret
= usbduxfast_ai_stop(udfs
, 1);
313 * interrupt service routine
315 static void usbduxfastsub_ai_Irq(struct urb
*urb
)
318 struct usbduxfastsub_s
*udfs
;
319 struct comedi_device
*this_comedidev
;
320 struct comedi_subdevice
*s
;
323 /* sanity checks - is the urb there? */
325 printk(KERN_ERR
"comedi_: usbduxfast_: ao int-handler called "
329 /* the context variable points to the subdevice */
330 this_comedidev
= urb
->context
;
331 if (!this_comedidev
) {
332 printk(KERN_ERR
"comedi_: usbduxfast_: urb context is a NULL "
336 /* the private structure of the subdevice is usbduxfastsub_s */
337 udfs
= this_comedidev
->private;
339 printk(KERN_ERR
"comedi_: usbduxfast_: private of comedi "
340 "subdev is a NULL pointer!\n");
343 /* are we running a command? */
344 if (unlikely(!udfs
->ai_cmd_running
)) {
346 * not running a command
347 * do not continue execution if no asynchronous command
348 * is running in particular not resubmit
353 if (unlikely(!udfs
->attached
)) {
354 /* no comedi device there */
357 /* subdevice which is the AD converter */
358 s
= this_comedidev
->subdevices
+ SUBDEV_AD
;
360 /* first we test if something unusual has just happened */
361 switch (urb
->status
) {
366 * happens after an unlink command or when the device
373 /* tell this comedi */
374 s
->async
->events
|= COMEDI_CB_EOA
;
375 s
->async
->events
|= COMEDI_CB_ERROR
;
376 comedi_event(udfs
->comedidev
, s
);
377 /* stop the transfer w/o unlink */
378 usbduxfast_ai_stop(udfs
, 0);
382 printk("comedi%d: usbduxfast: non-zero urb status received in "
383 "ai intr context: %d\n",
384 udfs
->comedidev
->minor
, urb
->status
);
385 s
->async
->events
|= COMEDI_CB_EOA
;
386 s
->async
->events
|= COMEDI_CB_ERROR
;
387 comedi_event(udfs
->comedidev
, s
);
388 usbduxfast_ai_stop(udfs
, 0);
392 p
= urb
->transfer_buffer
;
394 if (!udfs
->ai_continous
) {
395 /* not continous, fixed number of samples */
396 n
= urb
->actual_length
/ sizeof(uint16_t);
397 if (unlikely(udfs
->ai_sample_count
< n
)) {
399 * we have send only a fraction of the bytes
402 cfc_write_array_to_buffer(s
,
403 urb
->transfer_buffer
,
404 udfs
->ai_sample_count
406 usbduxfast_ai_stop(udfs
, 0);
407 /* tell comedi that the acquistion is over */
408 s
->async
->events
|= COMEDI_CB_EOA
;
409 comedi_event(udfs
->comedidev
, s
);
412 udfs
->ai_sample_count
-= n
;
414 /* write the full buffer to comedi */
415 err
= cfc_write_array_to_buffer(s
, urb
->transfer_buffer
,
417 if (unlikely(err
== 0)) {
418 /* buffer overflow */
419 usbduxfast_ai_stop(udfs
, 0);
423 /* tell comedi that data is there */
424 comedi_event(udfs
->comedidev
, s
);
427 /* ignore this packet */
432 * command is still running
433 * resubmit urb for BULK transfer
435 urb
->dev
= udfs
->usbdev
;
437 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
439 printk(KERN_ERR
"comedi%d: usbduxfast: urb resubm failed: %d",
440 udfs
->comedidev
->minor
, err
);
441 s
->async
->events
|= COMEDI_CB_EOA
;
442 s
->async
->events
|= COMEDI_CB_ERROR
;
443 comedi_event(udfs
->comedidev
, s
);
444 usbduxfast_ai_stop(udfs
, 0);
448 static int usbduxfastsub_start(struct usbduxfastsub_s
*udfs
)
451 unsigned char local_transfer_buffer
[16];
454 local_transfer_buffer
[0] = 0;
455 /* bRequest, "Firmware" */
456 ret
= usb_control_msg(udfs
->usbdev
, usb_sndctrlpipe(udfs
->usbdev
, 0), USBDUXFASTSUB_FIRMWARE
,
457 VENDOR_DIR_OUT
, /* bmRequestType */
458 USBDUXFASTSUB_CPUCS
, /* Value */
460 /* address of the transfer buffer */
461 local_transfer_buffer
,
463 EZTIMEOUT
); /* Timeout */
465 printk("comedi_: usbduxfast_: control msg failed (start)\n");
472 static int usbduxfastsub_stop(struct usbduxfastsub_s
*udfs
)
475 unsigned char local_transfer_buffer
[16];
478 local_transfer_buffer
[0] = 1;
479 /* bRequest, "Firmware" */
480 ret
= usb_control_msg(udfs
->usbdev
, usb_sndctrlpipe(udfs
->usbdev
, 0), USBDUXFASTSUB_FIRMWARE
,
481 VENDOR_DIR_OUT
, /* bmRequestType */
482 USBDUXFASTSUB_CPUCS
, /* Value */
484 local_transfer_buffer
, 1, /* Length */
485 EZTIMEOUT
); /* Timeout */
487 printk(KERN_ERR
"comedi_: usbduxfast: control msg failed "
495 static int usbduxfastsub_upload(struct usbduxfastsub_s
*udfs
,
496 unsigned char *local_transfer_buffer
,
497 unsigned int startAddr
, unsigned int len
)
501 #ifdef CONFIG_COMEDI_DEBUG
502 printk(KERN_DEBUG
"comedi: usbduxfast: uploading %d bytes", len
);
503 printk(KERN_DEBUG
" to addr %d, first byte=%d.\n",
504 startAddr
, local_transfer_buffer
[0]);
506 /* brequest, firmware */
507 ret
= usb_control_msg(udfs
->usbdev
, usb_sndctrlpipe(udfs
->usbdev
, 0), USBDUXFASTSUB_FIRMWARE
,
508 VENDOR_DIR_OUT
, /* bmRequestType */
509 startAddr
, /* value */
511 /* our local safe buffer */
512 local_transfer_buffer
,
514 EZTIMEOUT
); /* timeout */
516 #ifdef CONFIG_COMEDI_DEBUG
517 printk(KERN_DEBUG
"comedi_: usbduxfast: result=%d\n", ret
);
521 printk(KERN_ERR
"comedi_: usbduxfast: uppload failed\n");
528 static int usbduxfastsub_submit_InURBs(struct usbduxfastsub_s
*udfs
)
535 usb_fill_bulk_urb(udfs
->urbIn
, udfs
->usbdev
,
536 usb_rcvbulkpipe(udfs
->usbdev
, BULKINEP
),
537 udfs
->transfer_buffer
,
538 SIZEINBUF
, usbduxfastsub_ai_Irq
, udfs
->comedidev
);
540 #ifdef CONFIG_COMEDI_DEBUG
541 printk(KERN_DEBUG
"comedi%d: usbduxfast: submitting in-urb: "
542 "0x%p,0x%p\n", udfs
->comedidev
->minor
, udfs
->urbIn
->context
,
545 ret
= usb_submit_urb(udfs
->urbIn
, GFP_ATOMIC
);
547 printk(KERN_ERR
"comedi_: usbduxfast: ai: usb_submit_urb error"
554 static int usbduxfast_ai_cmdtest(struct comedi_device
*dev
,
555 struct comedi_subdevice
*s
,
556 struct comedi_cmd
*cmd
)
558 int err
= 0, stop_mask
= 0;
561 struct usbduxfastsub_s
*udfs
= dev
->private;
566 #ifdef CONFIG_COMEDI_DEBUG
567 printk(KERN_DEBUG
"comedi%d: usbduxfast_ai_cmdtest\n", dev
->minor
);
568 printk(KERN_DEBUG
"comedi%d: usbduxfast: convert_arg=%u "
569 "scan_begin_arg=%u\n",
570 dev
->minor
, cmd
->convert_arg
, cmd
->scan_begin_arg
);
572 /* step 1: make sure trigger sources are trivially valid */
574 tmp
= cmd
->start_src
;
575 cmd
->start_src
&= TRIG_NOW
| TRIG_EXT
| TRIG_INT
;
576 if (!cmd
->start_src
|| tmp
!= cmd
->start_src
)
579 tmp
= cmd
->scan_begin_src
;
580 cmd
->scan_begin_src
&= TRIG_TIMER
| TRIG_FOLLOW
| TRIG_EXT
;
581 if (!cmd
->scan_begin_src
|| tmp
!= cmd
->scan_begin_src
)
584 tmp
= cmd
->convert_src
;
585 cmd
->convert_src
&= TRIG_TIMER
| TRIG_EXT
;
586 if (!cmd
->convert_src
|| tmp
!= cmd
->convert_src
)
589 tmp
= cmd
->scan_end_src
;
590 cmd
->scan_end_src
&= TRIG_COUNT
;
591 if (!cmd
->scan_end_src
|| tmp
!= cmd
->scan_end_src
)
595 stop_mask
= TRIG_COUNT
| TRIG_NONE
;
596 cmd
->stop_src
&= stop_mask
;
597 if (!cmd
->stop_src
|| tmp
!= cmd
->stop_src
)
604 * step 2: make sure trigger sources are unique and mutually compatible
607 if (cmd
->start_src
!= TRIG_NOW
&&
608 cmd
->start_src
!= TRIG_EXT
&& cmd
->start_src
!= TRIG_INT
)
610 if (cmd
->scan_begin_src
!= TRIG_TIMER
&&
611 cmd
->scan_begin_src
!= TRIG_FOLLOW
&&
612 cmd
->scan_begin_src
!= TRIG_EXT
)
614 if (cmd
->convert_src
!= TRIG_TIMER
&& cmd
->convert_src
!= TRIG_EXT
)
616 if (cmd
->stop_src
!= TRIG_COUNT
&&
617 cmd
->stop_src
!= TRIG_EXT
&& cmd
->stop_src
!= TRIG_NONE
)
620 /* can't have external stop and start triggers at once */
621 if (cmd
->start_src
== TRIG_EXT
&& cmd
->stop_src
== TRIG_EXT
)
627 /* step 3: make sure arguments are trivially compatible */
629 if (cmd
->start_src
== TRIG_NOW
&& cmd
->start_arg
!= 0) {
634 if (!cmd
->chanlist_len
)
637 if (cmd
->scan_end_arg
!= cmd
->chanlist_len
) {
638 cmd
->scan_end_arg
= cmd
->chanlist_len
;
642 if (cmd
->chanlist_len
== 1)
645 minSamplPer
= MIN_SAMPLING_PERIOD
;
647 if (cmd
->convert_src
== TRIG_TIMER
) {
648 steps
= cmd
->convert_arg
* 30;
649 if (steps
< (minSamplPer
* 1000))
650 steps
= minSamplPer
* 1000;
652 if (steps
> (MAX_SAMPLING_PERIOD
* 1000))
653 steps
= MAX_SAMPLING_PERIOD
* 1000;
657 if (cmd
->convert_arg
!= tmp
) {
658 cmd
->convert_arg
= tmp
;
663 if (cmd
->scan_begin_src
== TRIG_TIMER
)
667 switch (cmd
->stop_src
) {
669 if (!cmd
->stop_arg
) {
675 if (cmd
->stop_arg
!= 0) {
681 * TRIG_EXT doesn't care since it doesn't trigger
682 * off a numbered channel
691 /* step 4: fix up any arguments */
697 static int usbduxfast_ai_inttrig(struct comedi_device
*dev
,
698 struct comedi_subdevice
*s
,
699 unsigned int trignum
)
702 struct usbduxfastsub_s
*udfs
= dev
->private;
712 #ifdef CONFIG_COMEDI_DEBUG
713 printk(KERN_DEBUG
"comedi%d: usbduxfast_ai_inttrig\n", dev
->minor
);
717 printk(KERN_ERR
"comedi%d: usbduxfast_ai_inttrig: invalid"
718 " trignum\n", dev
->minor
);
722 if (!udfs
->ai_cmd_running
) {
723 udfs
->ai_cmd_running
= 1;
724 ret
= usbduxfastsub_submit_InURBs(udfs
);
726 printk(KERN_ERR
"comedi%d: usbduxfast_ai_inttrig: "
727 "urbSubmit: err=%d\n", dev
->minor
, ret
);
728 udfs
->ai_cmd_running
= 0;
732 s
->async
->inttrig
= NULL
;
734 printk(KERN_ERR
"comedi%d: ai_inttrig but acqu is already"
735 " running\n", dev
->minor
);
742 * offsets for the GPIF bytes
743 * the first byte is the command byte
745 #define LENBASE (1+0x00)
746 #define OPBASE (1+0x08)
747 #define OUTBASE (1+0x10)
748 #define LOGBASE (1+0x18)
750 static int usbduxfast_ai_cmd(struct comedi_device
*dev
,
751 struct comedi_subdevice
*s
)
753 struct comedi_cmd
*cmd
= &s
->async
->cmd
;
754 unsigned int chan
, gain
, rngmask
= 0xff;
756 struct usbduxfastsub_s
*udfs
;
758 long steps
, steps_tmp
;
760 #ifdef CONFIG_COMEDI_DEBUG
761 printk(KERN_DEBUG
"comedi%d: usbduxfast_ai_cmd\n", dev
->minor
);
772 if (udfs
->ai_cmd_running
) {
773 printk(KERN_ERR
"comedi%d: ai_cmd not possible. Another ai_cmd"
774 " is running.\n", dev
->minor
);
778 /* set current channel of the running aquisition to zero */
779 s
->async
->cur_chan
= 0;
782 * ignore the first buffers from the device if there
783 * is an error condition
785 udfs
->ignore
= PACKETS_TO_IGNORE
;
787 if (cmd
->chanlist_len
> 0) {
788 gain
= CR_RANGE(cmd
->chanlist
[0]);
789 for (i
= 0; i
< cmd
->chanlist_len
; ++i
) {
790 chan
= CR_CHAN(cmd
->chanlist
[i
]);
792 printk(KERN_ERR
"comedi%d: cmd is accepting "
793 "only consecutive channels.\n",
798 if ((gain
!= CR_RANGE(cmd
->chanlist
[i
]))
799 && (cmd
->chanlist_len
> 3)) {
800 printk(KERN_ERR
"comedi%d: the gain must be"
801 " the same for all channels.\n",
806 if (i
>= NUMCHANNELS
) {
807 printk(KERN_ERR
"comedi%d: channel list too"
808 " long\n", dev
->minor
);
814 if (cmd
->scan_begin_src
== TRIG_TIMER
) {
815 printk(KERN_ERR
"comedi%d: usbduxfast: "
816 "scan_begin_src==TRIG_TIMER not valid.\n", dev
->minor
);
820 if (cmd
->convert_src
== TRIG_TIMER
)
821 steps
= (cmd
->convert_arg
* 30) / 1000;
823 if ((steps
< MIN_SAMPLING_PERIOD
) && (cmd
->chanlist_len
!= 1)) {
824 printk(KERN_ERR
"comedi%d: usbduxfast: ai_cmd: steps=%ld, "
825 "scan_begin_arg=%d. Not properly tested by cmdtest?\n",
826 dev
->minor
, steps
, cmd
->scan_begin_arg
);
830 if (steps
> MAX_SAMPLING_PERIOD
) {
831 printk(KERN_ERR
"comedi%d: usbduxfast: ai_cmd: sampling rate "
832 "too low.\n", dev
->minor
);
836 if ((cmd
->start_src
== TRIG_EXT
) && (cmd
->chanlist_len
!= 1)
837 && (cmd
->chanlist_len
!= 16)) {
838 printk(KERN_ERR
"comedi%d: usbduxfast: ai_cmd: TRIG_EXT only"
839 " with 1 or 16 channels possible.\n", dev
->minor
);
843 #ifdef CONFIG_COMEDI_DEBUG
844 printk(KERN_DEBUG
"comedi%d: usbduxfast: steps=%ld, convert_arg=%u\n",
845 dev
->minor
, steps
, cmd
->convert_arg
);
848 switch (cmd
->chanlist_len
) {
854 if (CR_RANGE(cmd
->chanlist
[0]) > 0)
855 rngmask
= 0xff - 0x04;
860 * for external trigger: looping in this state until
861 * the RDY0 pin becomes zero
864 /* we loop here until ready has been set */
865 if (cmd
->start_src
== TRIG_EXT
) {
866 /* branch back to state 0 */
867 udfs
->dux_commands
[LENBASE
+ 0] = 0x01;
868 /* deceision state w/o data */
869 udfs
->dux_commands
[OPBASE
+ 0] = 0x01;
870 udfs
->dux_commands
[OUTBASE
+ 0] = 0xFF & rngmask
;
872 udfs
->dux_commands
[LOGBASE
+ 0] = 0x00;
873 } else { /* we just proceed to state 1 */
874 udfs
->dux_commands
[LENBASE
+ 0] = 1;
875 udfs
->dux_commands
[OPBASE
+ 0] = 0;
876 udfs
->dux_commands
[OUTBASE
+ 0] = 0xFF & rngmask
;
877 udfs
->dux_commands
[LOGBASE
+ 0] = 0;
880 if (steps
< MIN_SAMPLING_PERIOD
) {
881 /* for fast single channel aqu without mux */
884 * we just stay here at state 1 and rexecute
885 * the same state this gives us 30MHz sampling
889 /* branch back to state 1 */
890 udfs
->dux_commands
[LENBASE
+ 1] = 0x89;
891 /* deceision state with data */
892 udfs
->dux_commands
[OPBASE
+ 1] = 0x03;
893 udfs
->dux_commands
[OUTBASE
+ 1] =
896 udfs
->dux_commands
[LOGBASE
+ 1] = 0xFF;
899 * we loop through two states: data and delay
902 udfs
->dux_commands
[LENBASE
+ 1] = steps
- 1;
904 udfs
->dux_commands
[OPBASE
+ 1] = 0x02;
905 udfs
->dux_commands
[OUTBASE
+ 1] =
908 udfs
->dux_commands
[LOGBASE
+ 1] = 0;
909 /* branch back to state 1 */
910 udfs
->dux_commands
[LENBASE
+ 2] = 0x09;
911 /* deceision state w/o data */
912 udfs
->dux_commands
[OPBASE
+ 2] = 0x01;
913 udfs
->dux_commands
[OUTBASE
+ 2] =
916 udfs
->dux_commands
[LOGBASE
+ 2] = 0xFF;
920 * we loop through 3 states: 2x delay and 1x data
921 * this gives a min sampling rate of 60kHz
924 /* we have 1 state with duration 1 */
927 /* do the first part of the delay */
928 udfs
->dux_commands
[LENBASE
+ 1] = steps
/ 2;
929 udfs
->dux_commands
[OPBASE
+ 1] = 0;
930 udfs
->dux_commands
[OUTBASE
+ 1] = 0xFF & rngmask
;
931 udfs
->dux_commands
[LOGBASE
+ 1] = 0;
933 /* and the second part */
934 udfs
->dux_commands
[LENBASE
+ 2] = steps
- steps
/ 2;
935 udfs
->dux_commands
[OPBASE
+ 2] = 0;
936 udfs
->dux_commands
[OUTBASE
+ 2] = 0xFF & rngmask
;
937 udfs
->dux_commands
[LOGBASE
+ 2] = 0;
939 /* get the data and branch back */
941 /* branch back to state 1 */
942 udfs
->dux_commands
[LENBASE
+ 3] = 0x09;
943 /* deceision state w data */
944 udfs
->dux_commands
[OPBASE
+ 3] = 0x03;
945 udfs
->dux_commands
[OUTBASE
+ 3] = 0xFF & rngmask
;
947 udfs
->dux_commands
[LOGBASE
+ 3] = 0xFF;
954 * commit data to the FIFO
957 if (CR_RANGE(cmd
->chanlist
[0]) > 0)
958 rngmask
= 0xff - 0x04;
962 udfs
->dux_commands
[LENBASE
+ 0] = 1;
964 udfs
->dux_commands
[OPBASE
+ 0] = 0x02;
965 udfs
->dux_commands
[OUTBASE
+ 0] = 0xFF & rngmask
;
966 udfs
->dux_commands
[LOGBASE
+ 0] = 0;
968 /* we have 1 state with duration 1: state 0 */
969 steps_tmp
= steps
- 1;
971 if (CR_RANGE(cmd
->chanlist
[1]) > 0)
972 rngmask
= 0xff - 0x04;
976 /* do the first part of the delay */
977 udfs
->dux_commands
[LENBASE
+ 1] = steps_tmp
/ 2;
978 udfs
->dux_commands
[OPBASE
+ 1] = 0;
980 udfs
->dux_commands
[OUTBASE
+ 1] = 0xFE & rngmask
;
981 udfs
->dux_commands
[LOGBASE
+ 1] = 0;
983 /* and the second part */
984 udfs
->dux_commands
[LENBASE
+ 2] = steps_tmp
- steps_tmp
/ 2;
985 udfs
->dux_commands
[OPBASE
+ 2] = 0;
986 udfs
->dux_commands
[OUTBASE
+ 2] = 0xFF & rngmask
;
987 udfs
->dux_commands
[LOGBASE
+ 2] = 0;
989 udfs
->dux_commands
[LENBASE
+ 3] = 1;
991 udfs
->dux_commands
[OPBASE
+ 3] = 0x02;
992 udfs
->dux_commands
[OUTBASE
+ 3] = 0xFF & rngmask
;
993 udfs
->dux_commands
[LOGBASE
+ 3] = 0;
996 * we have 2 states with duration 1: step 6 and
999 steps_tmp
= steps
- 2;
1001 if (CR_RANGE(cmd
->chanlist
[0]) > 0)
1002 rngmask
= 0xff - 0x04;
1006 /* do the first part of the delay */
1007 udfs
->dux_commands
[LENBASE
+ 4] = steps_tmp
/ 2;
1008 udfs
->dux_commands
[OPBASE
+ 4] = 0;
1010 udfs
->dux_commands
[OUTBASE
+ 4] = (0xFF - 0x02) & rngmask
;
1011 udfs
->dux_commands
[LOGBASE
+ 4] = 0;
1013 /* and the second part */
1014 udfs
->dux_commands
[LENBASE
+ 5] = steps_tmp
- steps_tmp
/ 2;
1015 udfs
->dux_commands
[OPBASE
+ 5] = 0;
1016 udfs
->dux_commands
[OUTBASE
+ 5] = 0xFF & rngmask
;
1017 udfs
->dux_commands
[LOGBASE
+ 5] = 0;
1019 udfs
->dux_commands
[LENBASE
+ 6] = 1;
1020 udfs
->dux_commands
[OPBASE
+ 6] = 0;
1021 udfs
->dux_commands
[OUTBASE
+ 6] = 0xFF & rngmask
;
1022 udfs
->dux_commands
[LOGBASE
+ 6] = 0;
1029 for (j
= 0; j
< 1; j
++) {
1030 if (CR_RANGE(cmd
->chanlist
[j
]) > 0)
1031 rngmask
= 0xff - 0x04;
1035 * commit data to the FIFO and do the first part
1038 udfs
->dux_commands
[LENBASE
+ j
* 2] = steps
/ 2;
1040 udfs
->dux_commands
[OPBASE
+ j
* 2] = 0x02;
1042 udfs
->dux_commands
[OUTBASE
+ j
* 2] = 0xFF & rngmask
;
1043 udfs
->dux_commands
[LOGBASE
+ j
* 2] = 0;
1045 if (CR_RANGE(cmd
->chanlist
[j
+ 1]) > 0)
1046 rngmask
= 0xff - 0x04;
1050 /* do the second part of the delay */
1051 udfs
->dux_commands
[LENBASE
+ j
* 2 + 1] =
1054 udfs
->dux_commands
[OPBASE
+ j
* 2 + 1] = 0;
1056 udfs
->dux_commands
[OUTBASE
+ j
* 2 + 1] =
1058 udfs
->dux_commands
[LOGBASE
+ j
* 2 + 1] = 0;
1061 /* 2 steps with duration 1: the idele step and step 6: */
1062 steps_tmp
= steps
- 2;
1064 /* commit data to the FIFO and do the first part of the delay */
1065 udfs
->dux_commands
[LENBASE
+ 4] = steps_tmp
/ 2;
1067 udfs
->dux_commands
[OPBASE
+ 4] = 0x02;
1068 udfs
->dux_commands
[OUTBASE
+ 4] = 0xFF & rngmask
;
1069 udfs
->dux_commands
[LOGBASE
+ 4] = 0;
1071 if (CR_RANGE(cmd
->chanlist
[0]) > 0)
1072 rngmask
= 0xff - 0x04;
1076 /* do the second part of the delay */
1077 udfs
->dux_commands
[LENBASE
+ 5] = steps_tmp
- steps_tmp
/ 2;
1079 udfs
->dux_commands
[OPBASE
+ 5] = 0;
1081 udfs
->dux_commands
[OUTBASE
+ 5] = (0xFF - 0x02) & rngmask
;
1082 udfs
->dux_commands
[LOGBASE
+ 5] = 0;
1084 udfs
->dux_commands
[LENBASE
+ 6] = 1;
1085 udfs
->dux_commands
[OPBASE
+ 6] = 0;
1086 udfs
->dux_commands
[OUTBASE
+ 6] = 0xFF & rngmask
;
1087 udfs
->dux_commands
[LOGBASE
+ 6] = 0;
1090 if (CR_RANGE(cmd
->chanlist
[0]) > 0)
1091 rngmask
= 0xff - 0x04;
1095 if (cmd
->start_src
== TRIG_EXT
) {
1097 * we loop here until ready has been set
1100 /* branch back to state 0 */
1101 udfs
->dux_commands
[LENBASE
+ 0] = 0x01;
1102 /* deceision state w/o data */
1103 udfs
->dux_commands
[OPBASE
+ 0] = 0x01;
1105 udfs
->dux_commands
[OUTBASE
+ 0] =
1106 (0xFF - 0x02) & rngmask
;
1108 udfs
->dux_commands
[LOGBASE
+ 0] = 0x00;
1111 * we just proceed to state 1
1114 /* 30us reset pulse */
1115 udfs
->dux_commands
[LENBASE
+ 0] = 255;
1116 udfs
->dux_commands
[OPBASE
+ 0] = 0;
1118 udfs
->dux_commands
[OUTBASE
+ 0] =
1119 (0xFF - 0x02) & rngmask
;
1120 udfs
->dux_commands
[LOGBASE
+ 0] = 0;
1123 /* commit data to the FIFO */
1124 udfs
->dux_commands
[LENBASE
+ 1] = 1;
1126 udfs
->dux_commands
[OPBASE
+ 1] = 0x02;
1127 udfs
->dux_commands
[OUTBASE
+ 1] = 0xFF & rngmask
;
1128 udfs
->dux_commands
[LOGBASE
+ 1] = 0;
1130 /* we have 2 states with duration 1 */
1133 /* do the first part of the delay */
1134 udfs
->dux_commands
[LENBASE
+ 2] = steps
/ 2;
1135 udfs
->dux_commands
[OPBASE
+ 2] = 0;
1136 udfs
->dux_commands
[OUTBASE
+ 2] = 0xFE & rngmask
;
1137 udfs
->dux_commands
[LOGBASE
+ 2] = 0;
1139 /* and the second part */
1140 udfs
->dux_commands
[LENBASE
+ 3] = steps
- steps
/ 2;
1141 udfs
->dux_commands
[OPBASE
+ 3] = 0;
1142 udfs
->dux_commands
[OUTBASE
+ 3] = 0xFF & rngmask
;
1143 udfs
->dux_commands
[LOGBASE
+ 3] = 0;
1145 /* branch back to state 1 */
1146 udfs
->dux_commands
[LENBASE
+ 4] = 0x09;
1147 /* deceision state w/o data */
1148 udfs
->dux_commands
[OPBASE
+ 4] = 0x01;
1149 udfs
->dux_commands
[OUTBASE
+ 4] = 0xFF & rngmask
;
1150 /* doesn't matter */
1151 udfs
->dux_commands
[LOGBASE
+ 4] = 0xFF;
1156 printk(KERN_ERR
"comedi %d: unsupported combination of "
1157 "channels\n", dev
->minor
);
1162 #ifdef CONFIG_COMEDI_DEBUG
1163 printk(KERN_DEBUG
"comedi %d: sending commands to the usb device\n",
1166 /* 0 means that the AD commands are sent */
1167 result
= send_dux_commands(udfs
, SENDADCOMMANDS
);
1169 printk(KERN_ERR
"comedi%d: adc command could not be submitted."
1170 "Aborting...\n", dev
->minor
);
1174 if (cmd
->stop_src
== TRIG_COUNT
) {
1175 udfs
->ai_sample_count
= cmd
->stop_arg
* cmd
->scan_end_arg
;
1176 if (udfs
->ai_sample_count
< 1) {
1177 printk(KERN_ERR
"comedi%d: "
1178 "(cmd->stop_arg)*(cmd->scan_end_arg)<1, "
1179 "aborting.\n", dev
->minor
);
1183 udfs
->ai_continous
= 0;
1185 /* continous aquisition */
1186 udfs
->ai_continous
= 1;
1187 udfs
->ai_sample_count
= 0;
1190 if ((cmd
->start_src
== TRIG_NOW
) || (cmd
->start_src
== TRIG_EXT
)) {
1191 /* enable this acquisition operation */
1192 udfs
->ai_cmd_running
= 1;
1193 ret
= usbduxfastsub_submit_InURBs(udfs
);
1195 udfs
->ai_cmd_running
= 0;
1196 /* fixme: unlink here?? */
1200 s
->async
->inttrig
= NULL
;
1204 * don't enable the acquision operation
1205 * wait for an internal signal
1207 s
->async
->inttrig
= usbduxfast_ai_inttrig
;
1215 * Mode 0 is used to get a single conversion on demand.
1217 static int usbduxfast_ai_insn_read(struct comedi_device
*dev
,
1218 struct comedi_subdevice
*s
,
1219 struct comedi_insn
*insn
, unsigned int *data
)
1221 int i
, j
, n
, actual_length
;
1222 int chan
, range
, rngmask
;
1224 struct usbduxfastsub_s
*udfs
;
1226 udfs
= dev
->private;
1228 printk(KERN_ERR
"comedi%d: ai_insn_read: no usb dev.\n",
1232 #ifdef CONFIG_COMEDI_DEBUG
1233 printk(KERN_DEBUG
"comedi%d: ai_insn_read, insn->n=%d, "
1234 "insn->subdev=%d\n", dev
->minor
, insn
->n
, insn
->subdev
);
1237 if (!udfs
->probed
) {
1241 if (udfs
->ai_cmd_running
) {
1242 printk(KERN_ERR
"comedi%d: ai_insn_read not possible. Async "
1243 "Command is running.\n", dev
->minor
);
1247 /* sample one channel */
1248 chan
= CR_CHAN(insn
->chanspec
);
1249 range
= CR_RANGE(insn
->chanspec
);
1250 /* set command for the first channel */
1253 rngmask
= 0xff - 0x04;
1257 /* commit data to the FIFO */
1258 udfs
->dux_commands
[LENBASE
+ 0] = 1;
1260 udfs
->dux_commands
[OPBASE
+ 0] = 0x02;
1261 udfs
->dux_commands
[OUTBASE
+ 0] = 0xFF & rngmask
;
1262 udfs
->dux_commands
[LOGBASE
+ 0] = 0;
1264 /* do the first part of the delay */
1265 udfs
->dux_commands
[LENBASE
+ 1] = 12;
1266 udfs
->dux_commands
[OPBASE
+ 1] = 0;
1267 udfs
->dux_commands
[OUTBASE
+ 1] = 0xFE & rngmask
;
1268 udfs
->dux_commands
[LOGBASE
+ 1] = 0;
1270 udfs
->dux_commands
[LENBASE
+ 2] = 1;
1271 udfs
->dux_commands
[OPBASE
+ 2] = 0;
1272 udfs
->dux_commands
[OUTBASE
+ 2] = 0xFE & rngmask
;
1273 udfs
->dux_commands
[LOGBASE
+ 2] = 0;
1275 udfs
->dux_commands
[LENBASE
+ 3] = 1;
1276 udfs
->dux_commands
[OPBASE
+ 3] = 0;
1277 udfs
->dux_commands
[OUTBASE
+ 3] = 0xFE & rngmask
;
1278 udfs
->dux_commands
[LOGBASE
+ 3] = 0;
1280 udfs
->dux_commands
[LENBASE
+ 4] = 1;
1281 udfs
->dux_commands
[OPBASE
+ 4] = 0;
1282 udfs
->dux_commands
[OUTBASE
+ 4] = 0xFE & rngmask
;
1283 udfs
->dux_commands
[LOGBASE
+ 4] = 0;
1286 udfs
->dux_commands
[LENBASE
+ 5] = 12;
1287 udfs
->dux_commands
[OPBASE
+ 5] = 0;
1288 udfs
->dux_commands
[OUTBASE
+ 5] = 0xFF & rngmask
;
1289 udfs
->dux_commands
[LOGBASE
+ 5] = 0;
1291 udfs
->dux_commands
[LENBASE
+ 6] = 1;
1292 udfs
->dux_commands
[OPBASE
+ 6] = 0;
1293 udfs
->dux_commands
[OUTBASE
+ 6] = 0xFF & rngmask
;
1294 udfs
->dux_commands
[LOGBASE
+ 0] = 0;
1296 #ifdef CONFIG_COMEDI_DEBUG
1297 printk(KERN_DEBUG
"comedi %d: sending commands to the usb device\n",
1300 /* 0 means that the AD commands are sent */
1301 err
= send_dux_commands(udfs
, SENDADCOMMANDS
);
1303 printk(KERN_ERR
"comedi%d: adc command could not be submitted."
1304 "Aborting...\n", dev
->minor
);
1308 #ifdef CONFIG_COMEDI_DEBUG
1309 printk(KERN_DEBUG
"comedi%d: usbduxfast: submitting in-urb: "
1310 "0x%p,0x%p\n", udfs
->comedidev
->minor
, udfs
->urbIn
->context
,
1313 for (i
= 0; i
< PACKETS_TO_IGNORE
; i
++) {
1314 err
= usb_bulk_msg(udfs
->usbdev
,
1315 usb_rcvbulkpipe(udfs
->usbdev
, BULKINEP
),
1316 udfs
->transfer_buffer
, SIZEINBUF
,
1317 &actual_length
, 10000);
1319 printk(KERN_ERR
"comedi%d: insn timeout. No data.\n",
1326 for (i
= 0; i
< insn
->n
;) {
1327 err
= usb_bulk_msg(udfs
->usbdev
,
1328 usb_rcvbulkpipe(udfs
->usbdev
, BULKINEP
),
1329 udfs
->transfer_buffer
, SIZEINBUF
,
1330 &actual_length
, 10000);
1332 printk(KERN_ERR
"comedi%d: insn data error: %d\n",
1337 n
= actual_length
/ sizeof(uint16_t);
1338 if ((n
% 16) != 0) {
1339 printk(KERN_ERR
"comedi%d: insn data packet "
1340 "corrupted.\n", dev
->minor
);
1344 for (j
= chan
; (j
< n
) && (i
< insn
->n
); j
= j
+ 16) {
1345 data
[i
] = ((uint16_t *) (udfs
->transfer_buffer
))[j
];
1353 #define FIRMWARE_MAX_LEN 0x2000
1355 static int firmwareUpload(struct usbduxfastsub_s
*usbduxfastsub
,
1356 const u8
*firmwareBinary
, int sizeFirmware
)
1361 if (!firmwareBinary
)
1364 if (sizeFirmware
> FIRMWARE_MAX_LEN
) {
1365 dev_err(&usbduxfastsub
->interface
->dev
,
1366 "comedi_: usbduxfast firmware binary it too large for FX2.\n");
1370 /* we generate a local buffer for the firmware */
1371 fwBuf
= kmemdup(firmwareBinary
, sizeFirmware
, GFP_KERNEL
);
1373 dev_err(&usbduxfastsub
->interface
->dev
,
1374 "comedi_: mem alloc for firmware failed\n");
1378 ret
= usbduxfastsub_stop(usbduxfastsub
);
1380 dev_err(&usbduxfastsub
->interface
->dev
,
1381 "comedi_: can not stop firmware\n");
1386 ret
= usbduxfastsub_upload(usbduxfastsub
, fwBuf
, 0, sizeFirmware
);
1388 dev_err(&usbduxfastsub
->interface
->dev
,
1389 "comedi_: firmware upload failed\n");
1393 ret
= usbduxfastsub_start(usbduxfastsub
);
1395 dev_err(&usbduxfastsub
->interface
->dev
,
1396 "comedi_: can not start firmware\n");
1404 static void tidy_up(struct usbduxfastsub_s
*udfs
)
1406 #ifdef CONFIG_COMEDI_DEBUG
1407 printk(KERN_DEBUG
"comedi_: usbduxfast: tiding up\n");
1413 /* shows the usb subsystem that the driver is down */
1414 if (udfs
->interface
)
1415 usb_set_intfdata(udfs
->interface
, NULL
);
1420 /* waits until a running transfer is over */
1421 usb_kill_urb(udfs
->urbIn
);
1423 kfree(udfs
->transfer_buffer
);
1424 udfs
->transfer_buffer
= NULL
;
1426 usb_free_urb(udfs
->urbIn
);
1430 kfree(udfs
->insnBuffer
);
1431 udfs
->insnBuffer
= NULL
;
1433 kfree(udfs
->dux_commands
);
1434 udfs
->dux_commands
= NULL
;
1436 udfs
->ai_cmd_running
= 0;
1439 static void usbduxfast_firmware_request_complete_handler(const struct firmware
1442 struct usbduxfastsub_s
*usbduxfastsub_tmp
= context
;
1443 struct usb_device
*usbdev
= usbduxfastsub_tmp
->usbdev
;
1450 * we need to upload the firmware here because fw will be
1451 * freed once we've left this function
1453 ret
= firmwareUpload(usbduxfastsub_tmp
, fw
->data
, fw
->size
);
1456 dev_err(&usbdev
->dev
,
1457 "Could not upload firmware (err=%d)\n", ret
);
1461 comedi_usb_auto_config(usbdev
, BOARDNAME
);
1463 release_firmware(fw
);
1467 * allocate memory for the urbs and initialise them
1469 static int usbduxfastsub_probe(struct usb_interface
*uinterf
,
1470 const struct usb_device_id
*id
)
1472 struct usb_device
*udev
= interface_to_usbdev(uinterf
);
1477 if (udev
->speed
!= USB_SPEED_HIGH
) {
1478 printk(KERN_ERR
"comedi_: usbduxfast_: This driver needs"
1479 "USB 2.0 to operate. Aborting...\n");
1482 #ifdef CONFIG_COMEDI_DEBUG
1483 printk(KERN_DEBUG
"comedi_: usbduxfast_: finding a free structure for "
1484 "the usb-device\n");
1486 down(&start_stop_sem
);
1487 /* look for a free place in the usbduxfast array */
1489 for (i
= 0; i
< NUMUSBDUXFAST
; i
++) {
1490 if (!usbduxfastsub
[i
].probed
) {
1498 printk(KERN_ERR
"Too many usbduxfast-devices connected.\n");
1499 up(&start_stop_sem
);
1502 #ifdef CONFIG_COMEDI_DEBUG
1503 printk(KERN_DEBUG
"comedi_: usbduxfast: usbduxfastsub[%d] is ready to "
1504 "connect to comedi.\n", index
);
1507 init_MUTEX(&(usbduxfastsub
[index
].sem
));
1508 /* save a pointer to the usb device */
1509 usbduxfastsub
[index
].usbdev
= udev
;
1511 /* save the interface itself */
1512 usbduxfastsub
[index
].interface
= uinterf
;
1513 /* get the interface number from the interface */
1514 usbduxfastsub
[index
].ifnum
= uinterf
->altsetting
->desc
.bInterfaceNumber
;
1516 * hand the private data over to the usb subsystem
1517 * will be needed for disconnect
1519 usb_set_intfdata(uinterf
, &(usbduxfastsub
[index
]));
1521 #ifdef CONFIG_COMEDI_DEBUG
1522 printk(KERN_DEBUG
"comedi_: usbduxfast: ifnum=%d\n",
1523 usbduxfastsub
[index
].ifnum
);
1525 /* create space for the commands going to the usb device */
1526 usbduxfastsub
[index
].dux_commands
= kmalloc(SIZEOFDUXBUFFER
,
1528 if (!usbduxfastsub
[index
].dux_commands
) {
1529 printk(KERN_ERR
"comedi_: usbduxfast: error alloc space for "
1531 tidy_up(&(usbduxfastsub
[index
]));
1532 up(&start_stop_sem
);
1535 /* create space of the instruction buffer */
1536 usbduxfastsub
[index
].insnBuffer
= kmalloc(SIZEINSNBUF
, GFP_KERNEL
);
1537 if (!usbduxfastsub
[index
].insnBuffer
) {
1538 printk(KERN_ERR
"comedi_: usbduxfast: could not alloc space "
1539 "for insnBuffer\n");
1540 tidy_up(&(usbduxfastsub
[index
]));
1541 up(&start_stop_sem
);
1544 /* setting to alternate setting 1: enabling bulk ep */
1545 i
= usb_set_interface(usbduxfastsub
[index
].usbdev
,
1546 usbduxfastsub
[index
].ifnum
, 1);
1548 printk(KERN_ERR
"comedi_: usbduxfast%d: could not switch to "
1549 "alternate setting 1.\n", index
);
1550 tidy_up(&(usbduxfastsub
[index
]));
1551 up(&start_stop_sem
);
1554 usbduxfastsub
[index
].urbIn
= usb_alloc_urb(0, GFP_KERNEL
);
1555 if (!usbduxfastsub
[index
].urbIn
) {
1556 printk(KERN_ERR
"comedi_: usbduxfast%d: Could not alloc."
1558 tidy_up(&(usbduxfastsub
[index
]));
1559 up(&start_stop_sem
);
1562 usbduxfastsub
[index
].transfer_buffer
= kmalloc(SIZEINBUF
, GFP_KERNEL
);
1563 if (!usbduxfastsub
[index
].transfer_buffer
) {
1564 printk(KERN_ERR
"comedi_: usbduxfast%d: could not alloc. "
1565 "transb.\n", index
);
1566 tidy_up(&(usbduxfastsub
[index
]));
1567 up(&start_stop_sem
);
1570 /* we've reached the bottom of the function */
1571 usbduxfastsub
[index
].probed
= 1;
1572 up(&start_stop_sem
);
1574 ret
= request_firmware_nowait(THIS_MODULE
,
1576 "usbduxfast_firmware.bin",
1579 usbduxfastsub
+ index
,
1580 usbduxfast_firmware_request_complete_handler
);
1583 dev_err(&udev
->dev
, "could not load firmware (err=%d)\n", ret
);
1587 printk(KERN_INFO
"comedi_: usbduxfast%d has been successfully "
1588 "initialized.\n", index
);
1593 static void usbduxfastsub_disconnect(struct usb_interface
*intf
)
1595 struct usbduxfastsub_s
*udfs
= usb_get_intfdata(intf
);
1596 struct usb_device
*udev
= interface_to_usbdev(intf
);
1599 printk(KERN_ERR
"comedi_: usbduxfast: disconnect called with "
1603 if (udfs
->usbdev
!= udev
) {
1604 printk(KERN_ERR
"comedi_: usbduxfast: BUG! called with wrong "
1609 comedi_usb_auto_unconfig(udev
);
1611 down(&start_stop_sem
);
1615 up(&start_stop_sem
);
1617 #ifdef CONFIG_COMEDI_DEBUG
1618 printk(KERN_DEBUG
"comedi_: usbduxfast: disconnected from the usb\n");
1623 * is called when comedi-config is called
1625 static int usbduxfast_attach(struct comedi_device
*dev
,
1626 struct comedi_devconfig
*it
)
1631 struct comedi_subdevice
*s
= NULL
;
1632 dev
->private = NULL
;
1634 down(&start_stop_sem
);
1636 * find a valid device which has been detected by the
1637 * probe function of the usb
1640 for (i
= 0; i
< NUMUSBDUXFAST
; i
++) {
1641 if (usbduxfastsub
[i
].probed
&& !usbduxfastsub
[i
].attached
) {
1648 printk(KERN_ERR
"comedi%d: usbduxfast: error: attach failed, "
1649 "no usbduxfast devs connected to the usb bus.\n",
1651 up(&start_stop_sem
);
1655 down(&(usbduxfastsub
[index
].sem
));
1656 /* pointer back to the corresponding comedi device */
1657 usbduxfastsub
[index
].comedidev
= dev
;
1659 /* trying to upload the firmware into the chip */
1660 if (comedi_aux_data(it
->options
, 0) &&
1661 it
->options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]) {
1662 firmwareUpload(&usbduxfastsub
[index
],
1663 comedi_aux_data(it
->options
, 0),
1664 it
->options
[COMEDI_DEVCONF_AUX_DATA_LENGTH
]);
1667 dev
->board_name
= BOARDNAME
;
1669 /* set number of subdevices */
1670 dev
->n_subdevices
= N_SUBDEVICES
;
1672 /* allocate space for the subdevices */
1673 ret
= alloc_subdevices(dev
, N_SUBDEVICES
);
1675 printk(KERN_ERR
"comedi%d: usbduxfast: error alloc space for "
1676 "subdev\n", dev
->minor
);
1677 up(&(usbduxfastsub
[index
].sem
));
1678 up(&start_stop_sem
);
1682 printk(KERN_INFO
"comedi%d: usbduxfast: usb-device %d is attached to "
1683 "comedi.\n", dev
->minor
, index
);
1684 /* private structure is also simply the usb-structure */
1685 dev
->private = usbduxfastsub
+ index
;
1686 /* the first subdevice is the A/D converter */
1687 s
= dev
->subdevices
+ SUBDEV_AD
;
1689 * the URBs get the comedi subdevice which is responsible for reading
1690 * this is the subdevice which reads data
1692 dev
->read_subdev
= s
;
1693 /* the subdevice receives as private structure the usb-structure */
1696 s
->type
= COMEDI_SUBD_AI
;
1697 /* readable and ref is to ground */
1698 s
->subdev_flags
= SDF_READABLE
| SDF_GROUND
| SDF_CMD_READ
;
1701 /* length of the channellist */
1702 s
->len_chanlist
= 16;
1703 /* callback functions */
1704 s
->insn_read
= usbduxfast_ai_insn_read
;
1705 s
->do_cmdtest
= usbduxfast_ai_cmdtest
;
1706 s
->do_cmd
= usbduxfast_ai_cmd
;
1707 s
->cancel
= usbduxfast_ai_cancel
;
1708 /* max value from the A/D converter (12bit+1 bit for overflow) */
1709 s
->maxdata
= 0x1000;
1710 /* range table to convert to physical units */
1711 s
->range_table
= &range_usbduxfast_ai_range
;
1713 /* finally decide that it's attached */
1714 usbduxfastsub
[index
].attached
= 1;
1716 up(&(usbduxfastsub
[index
].sem
));
1717 up(&start_stop_sem
);
1718 printk(KERN_INFO
"comedi%d: successfully attached to usbduxfast.\n",
1724 static int usbduxfast_detach(struct comedi_device
*dev
)
1726 struct usbduxfastsub_s
*udfs
;
1729 printk(KERN_ERR
"comedi?: usbduxfast: detach without dev "
1733 #ifdef CONFIG_COMEDI_DEBUG
1734 printk(KERN_DEBUG
"comedi%d: usbduxfast: detach usb device\n",
1738 udfs
= dev
->private;
1740 printk(KERN_ERR
"comedi?: usbduxfast: detach without ptr to "
1741 "usbduxfastsub[]\n");
1746 down(&start_stop_sem
);
1748 * Don't allow detach to free the private structure
1749 * It's one entry of of usbduxfastsub[]
1751 dev
->private = NULL
;
1753 udfs
->comedidev
= NULL
;
1754 #ifdef CONFIG_COMEDI_DEBUG
1755 printk(KERN_DEBUG
"comedi%d: usbduxfast: detach: successfully "
1756 "removed\n", dev
->minor
);
1758 up(&start_stop_sem
);
1764 * main driver struct
1766 static struct comedi_driver driver_usbduxfast
= {
1767 .driver_name
= "usbduxfast",
1768 .module
= THIS_MODULE
,
1769 .attach
= usbduxfast_attach
,
1770 .detach
= usbduxfast_detach
1774 * Table with the USB-devices: just now only testing IDs
1776 static const struct usb_device_id usbduxfastsub_table
[] = {
1777 /* { USB_DEVICE(0x4b4, 0x8613) }, testing */
1778 {USB_DEVICE(0x13d8, 0x0010)}, /* real ID */
1779 {USB_DEVICE(0x13d8, 0x0011)}, /* real ID */
1780 {} /* Terminating entry */
1783 MODULE_DEVICE_TABLE(usb
, usbduxfastsub_table
);
1786 * The usbduxfastsub-driver
1788 static struct usb_driver usbduxfastsub_driver
= {
1789 #ifdef COMEDI_HAVE_USB_DRIVER_OWNER
1790 .owner
= THIS_MODULE
,
1793 .probe
= usbduxfastsub_probe
,
1794 .disconnect
= usbduxfastsub_disconnect
,
1795 .id_table
= usbduxfastsub_table
1799 * Can't use the nice macro as I have also to initialise the USB subsystem:
1800 * registering the usb-system _and_ the comedi-driver
1802 static int __init
init_usbduxfast(void)
1805 KBUILD_MODNAME
": " DRIVER_VERSION
":" DRIVER_DESC
"\n");
1806 usb_register(&usbduxfastsub_driver
);
1807 comedi_driver_register(&driver_usbduxfast
);
1812 * deregistering the comedi driver and the usb-subsystem
1814 static void __exit
exit_usbduxfast(void)
1816 comedi_driver_unregister(&driver_usbduxfast
);
1817 usb_deregister(&usbduxfastsub_driver
);
1820 module_init(init_usbduxfast
);
1821 module_exit(exit_usbduxfast
);
1823 MODULE_AUTHOR(DRIVER_AUTHOR
);
1824 MODULE_DESCRIPTION(DRIVER_DESC
);
1825 MODULE_LICENSE("GPL");