]> git.karo-electronics.de Git - linux-beck.git/blob - drivers/staging/comedi/drivers/usbduxfast.c
Merge branch 'for-john' of git://git.kernel.org/pub/scm/linux/kernel/git/jberg/mac80211
[linux-beck.git] / drivers / staging / comedi / drivers / usbduxfast.c
1 /*
2  *  Copyright (C) 2004 Bernd Porr, Bernd.Porr@f2s.com
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 /*
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.
24  *
25  * Bernd Porr
26  *
27  *
28  * Revision history:
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
33  *       1MHz/16ch=62.5kHz
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
38  */
39
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"
51
52 /*
53  * timeout for the USB-transfer
54  */
55 #define EZTIMEOUT       30
56
57 /*
58  * constants for "firmware" upload and download
59  */
60 #define USBDUXFASTSUB_FIRMWARE  0xA0
61 #define VENDOR_DIR_IN           0xC0
62 #define VENDOR_DIR_OUT          0x40
63
64 /*
65  * internal addresses of the 8051 processor
66  */
67 #define USBDUXFASTSUB_CPUCS     0xE600
68
69 /*
70  * max lenghth of the transfer-buffer for software upload
71  */
72 #define TB_LEN  0x2000
73
74 /*
75  * input endpoint number
76  */
77 #define BULKINEP        6
78
79 /*
80  * endpoint for the A/D channellist: bulk OUT
81  */
82 #define CHANNELLISTEP   4
83
84 /*
85  * number of channels
86  */
87 #define NUMCHANNELS     32
88
89 /*
90  * size of the waveform descriptor
91  */
92 #define WAVESIZE        0x20
93
94 /*
95  * size of one A/D value
96  */
97 #define SIZEADIN        (sizeof(int16_t))
98
99 /*
100  * size of the input-buffer IN BYTES
101  */
102 #define SIZEINBUF       512
103
104 /*
105  * 16 bytes
106  */
107 #define SIZEINSNBUF     512
108
109 /*
110  * size of the buffer for the dux commands in bytes
111  */
112 #define SIZEOFDUXBUFFER 256
113
114 /*
115  * number of in-URBs which receive the data: min=5
116  */
117 #define NUMOFINBUFFERSHIGH      10
118
119 /*
120  * total number of usbduxfast devices
121  */
122 #define NUMUSBDUXFAST   16
123
124 /*
125  * analogue in subdevice
126  */
127 #define SUBDEV_AD       0
128
129 /*
130  * min delay steps for more than one channel
131  * basically when the mux gives up ;-)
132  *
133  * steps at 30MHz in the FX2
134  */
135 #define MIN_SAMPLING_PERIOD     9
136
137 /*
138  * max number of 1/30MHz delay steps
139  */
140 #define MAX_SAMPLING_PERIOD     500
141
142 /*
143  * number of received packets to ignore before we start handing data
144  * over to comedi, it's quad buffering and we have to ignore 4 packets
145  */
146 #define PACKETS_TO_IGNORE       4
147
148 /*
149  * comedi constants
150  */
151 static const struct comedi_lrange range_usbduxfast_ai_range = {
152         2, {BIP_RANGE(0.75), BIP_RANGE(0.5)}
153 };
154
155 /*
156  * private structure of one subdevice
157  *
158  * this is the structure which holds all the data of this driver
159  * one sub device just now: A/D
160  */
161 struct usbduxfastsub_s {
162         int attached;           /* is attached? */
163         int probed;             /* is it associated with a subdevice? */
164         struct usb_device *usbdev;      /* pointer to the usb-device */
165         struct urb *urbIn;      /* BULK-transfer handling: urb */
166         int8_t *transfer_buffer;
167         int16_t *insnBuffer;    /* input buffer for single insn */
168         int ifnum;              /* interface number */
169         struct usb_interface *interface;        /* interface structure */
170         /* comedi device for the interrupt context */
171         struct comedi_device *comedidev;
172         short int ai_cmd_running;       /* asynchronous command is running */
173         short int ai_continous; /* continous acquisition */
174         long int ai_sample_count;       /* number of samples to acquire */
175         uint8_t *dux_commands;  /* commands */
176         int ignore;             /* counter which ignores the first
177                                    buffers */
178         struct semaphore sem;
179 };
180
181 /*
182  * The pointer to the private usb-data of the driver
183  * is also the private data for the comedi-device.
184  * This has to be global as the usb subsystem needs
185  * global variables. The other reason is that this
186  * structure must be there _before_ any comedi
187  * command is issued. The usb subsystem must be
188  * initialised before comedi can access it.
189  */
190 static struct usbduxfastsub_s usbduxfastsub[NUMUSBDUXFAST];
191
192 static DEFINE_SEMAPHORE(start_stop_sem);
193
194 /*
195  * bulk transfers to usbduxfast
196  */
197 #define SENDADCOMMANDS            0
198 #define SENDINITEP6               1
199
200 static int send_dux_commands(struct usbduxfastsub_s *udfs, int cmd_type)
201 {
202         int tmp, nsent;
203
204         udfs->dux_commands[0] = cmd_type;
205
206 #ifdef CONFIG_COMEDI_DEBUG
207         printk(KERN_DEBUG "comedi%d: usbduxfast: dux_commands: ",
208                udfs->comedidev->minor);
209         for (tmp = 0; tmp < SIZEOFDUXBUFFER; tmp++)
210                 printk(" %02x", udfs->dux_commands[tmp]);
211         printk("\n");
212 #endif
213
214         tmp = usb_bulk_msg(udfs->usbdev,
215                            usb_sndbulkpipe(udfs->usbdev, CHANNELLISTEP),
216                            udfs->dux_commands, SIZEOFDUXBUFFER, &nsent, 10000);
217         if (tmp < 0)
218                 printk(KERN_ERR "comedi%d: could not transmit dux_commands to"
219                        "the usb-device, err=%d\n", udfs->comedidev->minor, tmp);
220         return tmp;
221 }
222
223 /*
224  * Stops the data acquision.
225  * It should be safe to call this function from any context.
226  */
227 static int usbduxfastsub_unlink_InURBs(struct usbduxfastsub_s *udfs)
228 {
229         int j = 0;
230         int err = 0;
231
232         if (udfs && udfs->urbIn) {
233                 udfs->ai_cmd_running = 0;
234                 /* waits until a running transfer is over */
235                 usb_kill_urb(udfs->urbIn);
236                 j = 0;
237         }
238 #ifdef CONFIG_COMEDI_DEBUG
239         printk(KERN_DEBUG "comedi: usbduxfast: unlinked InURB: res=%d\n", j);
240 #endif
241         return err;
242 }
243
244 /*
245  * This will stop a running acquisition operation.
246  * Is called from within this driver from both the
247  * interrupt context and from comedi.
248  */
249 static int usbduxfast_ai_stop(struct usbduxfastsub_s *udfs, int do_unlink)
250 {
251         int ret = 0;
252
253         if (!udfs) {
254                 printk(KERN_ERR "comedi?: usbduxfast_ai_stop: udfs=NULL!\n");
255                 return -EFAULT;
256         }
257 #ifdef CONFIG_COMEDI_DEBUG
258         printk(KERN_DEBUG "comedi: usbduxfast_ai_stop\n");
259 #endif
260
261         udfs->ai_cmd_running = 0;
262
263         if (do_unlink)
264                 /* stop aquistion */
265                 ret = usbduxfastsub_unlink_InURBs(udfs);
266
267         return ret;
268 }
269
270 /*
271  * This will cancel a running acquisition operation.
272  * This is called by comedi but never from inside the driver.
273  */
274 static int usbduxfast_ai_cancel(struct comedi_device *dev,
275                                 struct comedi_subdevice *s)
276 {
277         struct usbduxfastsub_s *udfs;
278         int ret;
279
280         /* force unlink of all urbs */
281 #ifdef CONFIG_COMEDI_DEBUG
282         printk(KERN_DEBUG "comedi: usbduxfast_ai_cancel\n");
283 #endif
284         udfs = dev->private;
285         if (!udfs) {
286                 printk(KERN_ERR "comedi: usbduxfast_ai_cancel: udfs=NULL\n");
287                 return -EFAULT;
288         }
289         down(&udfs->sem);
290         if (!udfs->probed) {
291                 up(&udfs->sem);
292                 return -ENODEV;
293         }
294         /* unlink */
295         ret = usbduxfast_ai_stop(udfs, 1);
296         up(&udfs->sem);
297
298         return ret;
299 }
300
301 /*
302  * analogue IN
303  * interrupt service routine
304  */
305 static void usbduxfastsub_ai_Irq(struct urb *urb)
306 {
307         int n, err;
308         struct usbduxfastsub_s *udfs;
309         struct comedi_device *this_comedidev;
310         struct comedi_subdevice *s;
311         uint16_t *p;
312
313         /* sanity checks - is the urb there? */
314         if (!urb) {
315                 printk(KERN_ERR "comedi_: usbduxfast_: ao int-handler called "
316                        "with urb=NULL!\n");
317                 return;
318         }
319         /* the context variable points to the subdevice */
320         this_comedidev = urb->context;
321         if (!this_comedidev) {
322                 printk(KERN_ERR "comedi_: usbduxfast_: urb context is a NULL "
323                        "pointer!\n");
324                 return;
325         }
326         /* the private structure of the subdevice is usbduxfastsub_s */
327         udfs = this_comedidev->private;
328         if (!udfs) {
329                 printk(KERN_ERR "comedi_: usbduxfast_: private of comedi "
330                        "subdev is a NULL pointer!\n");
331                 return;
332         }
333         /* are we running a command? */
334         if (unlikely(!udfs->ai_cmd_running)) {
335                 /*
336                  * not running a command
337                  * do not continue execution if no asynchronous command
338                  * is running in particular not resubmit
339                  */
340                 return;
341         }
342
343         if (unlikely(!udfs->attached)) {
344                 /* no comedi device there */
345                 return;
346         }
347         /* subdevice which is the AD converter */
348         s = this_comedidev->subdevices + SUBDEV_AD;
349
350         /* first we test if something unusual has just happened */
351         switch (urb->status) {
352         case 0:
353                 break;
354
355                 /*
356                  * happens after an unlink command or when the device
357                  * is plugged out
358                  */
359         case -ECONNRESET:
360         case -ENOENT:
361         case -ESHUTDOWN:
362         case -ECONNABORTED:
363                 /* tell this comedi */
364                 s->async->events |= COMEDI_CB_EOA;
365                 s->async->events |= COMEDI_CB_ERROR;
366                 comedi_event(udfs->comedidev, s);
367                 /* stop the transfer w/o unlink */
368                 usbduxfast_ai_stop(udfs, 0);
369                 return;
370
371         default:
372                 printk("comedi%d: usbduxfast: non-zero urb status received in "
373                        "ai intr context: %d\n",
374                        udfs->comedidev->minor, urb->status);
375                 s->async->events |= COMEDI_CB_EOA;
376                 s->async->events |= COMEDI_CB_ERROR;
377                 comedi_event(udfs->comedidev, s);
378                 usbduxfast_ai_stop(udfs, 0);
379                 return;
380         }
381
382         p = urb->transfer_buffer;
383         if (!udfs->ignore) {
384                 if (!udfs->ai_continous) {
385                         /* not continuous, fixed number of samples */
386                         n = urb->actual_length / sizeof(uint16_t);
387                         if (unlikely(udfs->ai_sample_count < n)) {
388                                 /*
389                                  * we have send only a fraction of the bytes
390                                  * received
391                                  */
392                                 cfc_write_array_to_buffer(s,
393                                                           urb->transfer_buffer,
394                                                           udfs->ai_sample_count
395                                                           * sizeof(uint16_t));
396                                 usbduxfast_ai_stop(udfs, 0);
397                                 /* tell comedi that the acquistion is over */
398                                 s->async->events |= COMEDI_CB_EOA;
399                                 comedi_event(udfs->comedidev, s);
400                                 return;
401                         }
402                         udfs->ai_sample_count -= n;
403                 }
404                 /* write the full buffer to comedi */
405                 err = cfc_write_array_to_buffer(s, urb->transfer_buffer,
406                                                 urb->actual_length);
407                 if (unlikely(err == 0)) {
408                         /* buffer overflow */
409                         usbduxfast_ai_stop(udfs, 0);
410                         return;
411                 }
412
413                 /* tell comedi that data is there */
414                 comedi_event(udfs->comedidev, s);
415
416         } else {
417                 /* ignore this packet */
418                 udfs->ignore--;
419         }
420
421         /*
422          * command is still running
423          * resubmit urb for BULK transfer
424          */
425         urb->dev = udfs->usbdev;
426         urb->status = 0;
427         err = usb_submit_urb(urb, GFP_ATOMIC);
428         if (err < 0) {
429                 printk(KERN_ERR "comedi%d: usbduxfast: urb resubm failed: %d",
430                        udfs->comedidev->minor, err);
431                 s->async->events |= COMEDI_CB_EOA;
432                 s->async->events |= COMEDI_CB_ERROR;
433                 comedi_event(udfs->comedidev, s);
434                 usbduxfast_ai_stop(udfs, 0);
435         }
436 }
437
438 static int usbduxfastsub_start(struct usbduxfastsub_s *udfs)
439 {
440         int ret;
441         unsigned char local_transfer_buffer[16];
442
443         /* 7f92 to zero */
444         local_transfer_buffer[0] = 0;
445         /* bRequest, "Firmware" */
446         ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0),
447                               USBDUXFASTSUB_FIRMWARE,
448                               VENDOR_DIR_OUT,     /* bmRequestType */
449                               USBDUXFASTSUB_CPUCS,    /* Value */
450                               0x0000,   /* Index */
451                               /* address of the transfer buffer */
452                               local_transfer_buffer,
453                               1,      /* Length */
454                               EZTIMEOUT);    /* Timeout */
455         if (ret < 0) {
456                 printk("comedi_: usbduxfast_: control msg failed (start)\n");
457                 return ret;
458         }
459
460         return 0;
461 }
462
463 static int usbduxfastsub_stop(struct usbduxfastsub_s *udfs)
464 {
465         int ret;
466         unsigned char local_transfer_buffer[16];
467
468         /* 7f92 to one */
469         local_transfer_buffer[0] = 1;
470         /* bRequest, "Firmware" */
471         ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0),
472                               USBDUXFASTSUB_FIRMWARE,
473                               VENDOR_DIR_OUT,   /* bmRequestType */
474                               USBDUXFASTSUB_CPUCS,      /* Value */
475                               0x0000,   /* Index */
476                               local_transfer_buffer, 1, /* Length */
477                               EZTIMEOUT);       /* Timeout */
478         if (ret < 0) {
479                 printk(KERN_ERR "comedi_: usbduxfast: control msg failed "
480                        "(stop)\n");
481                 return ret;
482         }
483
484         return 0;
485 }
486
487 static int usbduxfastsub_upload(struct usbduxfastsub_s *udfs,
488                                 unsigned char *local_transfer_buffer,
489                                 unsigned int startAddr, unsigned int len)
490 {
491         int ret;
492
493 #ifdef CONFIG_COMEDI_DEBUG
494         printk(KERN_DEBUG "comedi: usbduxfast: uploading %d bytes", len);
495         printk(KERN_DEBUG " to addr %d, first byte=%d.\n",
496                startAddr, local_transfer_buffer[0]);
497 #endif
498         /* brequest, firmware */
499         ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0),
500                               USBDUXFASTSUB_FIRMWARE,
501                               VENDOR_DIR_OUT,   /* bmRequestType */
502                               startAddr,        /* value */
503                               0x0000,    /* index */
504                               /* our local safe buffer */
505                               local_transfer_buffer,
506                               len,      /* length */
507                               EZTIMEOUT);      /* timeout */
508
509 #ifdef CONFIG_COMEDI_DEBUG
510         printk(KERN_DEBUG "comedi_: usbduxfast: result=%d\n", ret);
511 #endif
512
513         if (ret < 0) {
514                 printk(KERN_ERR "comedi_: usbduxfast: uppload failed\n");
515                 return ret;
516         }
517
518         return 0;
519 }
520
521 static int usbduxfastsub_submit_InURBs(struct usbduxfastsub_s *udfs)
522 {
523         int ret;
524
525         if (!udfs)
526                 return -EFAULT;
527
528         usb_fill_bulk_urb(udfs->urbIn, udfs->usbdev,
529                           usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
530                           udfs->transfer_buffer,
531                           SIZEINBUF, usbduxfastsub_ai_Irq, udfs->comedidev);
532
533 #ifdef CONFIG_COMEDI_DEBUG
534         printk(KERN_DEBUG "comedi%d: usbduxfast: submitting in-urb: "
535                "0x%p,0x%p\n", udfs->comedidev->minor, udfs->urbIn->context,
536                udfs->urbIn->dev);
537 #endif
538         ret = usb_submit_urb(udfs->urbIn, GFP_ATOMIC);
539         if (ret) {
540                 printk(KERN_ERR "comedi_: usbduxfast: ai: usb_submit_urb error"
541                        " %d\n", ret);
542                 return ret;
543         }
544         return 0;
545 }
546
547 static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
548                                  struct comedi_subdevice *s,
549                                  struct comedi_cmd *cmd)
550 {
551         int err = 0, stop_mask = 0;
552         long int steps, tmp;
553         int minSamplPer;
554         struct usbduxfastsub_s *udfs = dev->private;
555
556         if (!udfs->probed)
557                 return -ENODEV;
558
559 #ifdef CONFIG_COMEDI_DEBUG
560         printk(KERN_DEBUG "comedi%d: usbduxfast_ai_cmdtest\n", dev->minor);
561         printk(KERN_DEBUG "comedi%d: usbduxfast: convert_arg=%u "
562                "scan_begin_arg=%u\n",
563                dev->minor, cmd->convert_arg, cmd->scan_begin_arg);
564 #endif
565         /* step 1: make sure trigger sources are trivially valid */
566
567         tmp = cmd->start_src;
568         cmd->start_src &= TRIG_NOW | TRIG_EXT | TRIG_INT;
569         if (!cmd->start_src || tmp != cmd->start_src)
570                 err++;
571
572         tmp = cmd->scan_begin_src;
573         cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT;
574         if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
575                 err++;
576
577         tmp = cmd->convert_src;
578         cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
579         if (!cmd->convert_src || tmp != cmd->convert_src)
580                 err++;
581
582         tmp = cmd->scan_end_src;
583         cmd->scan_end_src &= TRIG_COUNT;
584         if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
585                 err++;
586
587         tmp = cmd->stop_src;
588         stop_mask = TRIG_COUNT | TRIG_NONE;
589         cmd->stop_src &= stop_mask;
590         if (!cmd->stop_src || tmp != cmd->stop_src)
591                 err++;
592
593         if (err)
594                 return 1;
595
596         /*
597          * step 2: make sure trigger sources are unique and mutually compatible
598          */
599
600         if (cmd->start_src != TRIG_NOW &&
601             cmd->start_src != TRIG_EXT && cmd->start_src != TRIG_INT)
602                 err++;
603         if (cmd->scan_begin_src != TRIG_TIMER &&
604             cmd->scan_begin_src != TRIG_FOLLOW &&
605             cmd->scan_begin_src != TRIG_EXT)
606                 err++;
607         if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
608                 err++;
609         if (cmd->stop_src != TRIG_COUNT &&
610             cmd->stop_src != TRIG_EXT && cmd->stop_src != TRIG_NONE)
611                 err++;
612
613         /* can't have external stop and start triggers at once */
614         if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
615                 err++;
616
617         if (err)
618                 return 2;
619
620         /* step 3: make sure arguments are trivially compatible */
621
622         if (cmd->start_src == TRIG_NOW && cmd->start_arg != 0) {
623                 cmd->start_arg = 0;
624                 err++;
625         }
626
627         if (!cmd->chanlist_len)
628                 err++;
629
630         if (cmd->scan_end_arg != cmd->chanlist_len) {
631                 cmd->scan_end_arg = cmd->chanlist_len;
632                 err++;
633         }
634
635         if (cmd->chanlist_len == 1)
636                 minSamplPer = 1;
637         else
638                 minSamplPer = MIN_SAMPLING_PERIOD;
639
640         if (cmd->convert_src == TRIG_TIMER) {
641                 steps = cmd->convert_arg * 30;
642                 if (steps < (minSamplPer * 1000))
643                         steps = minSamplPer * 1000;
644
645                 if (steps > (MAX_SAMPLING_PERIOD * 1000))
646                         steps = MAX_SAMPLING_PERIOD * 1000;
647
648                 /* calc arg again */
649                 tmp = steps / 30;
650                 if (cmd->convert_arg != tmp) {
651                         cmd->convert_arg = tmp;
652                         err++;
653                 }
654         }
655
656         if (cmd->scan_begin_src == TRIG_TIMER)
657                 err++;
658
659         /* stop source */
660         switch (cmd->stop_src) {
661         case TRIG_COUNT:
662                 if (!cmd->stop_arg) {
663                         cmd->stop_arg = 1;
664                         err++;
665                 }
666                 break;
667         case TRIG_NONE:
668                 if (cmd->stop_arg != 0) {
669                         cmd->stop_arg = 0;
670                         err++;
671                 }
672                 break;
673                 /*
674                  * TRIG_EXT doesn't care since it doesn't trigger
675                  * off a numbered channel
676                  */
677         default:
678                 break;
679         }
680
681         if (err)
682                 return 3;
683
684         /* step 4: fix up any arguments */
685
686         return 0;
687
688 }
689
690 static int usbduxfast_ai_inttrig(struct comedi_device *dev,
691                                  struct comedi_subdevice *s,
692                                  unsigned int trignum)
693 {
694         int ret;
695         struct usbduxfastsub_s *udfs = dev->private;
696
697         if (!udfs)
698                 return -EFAULT;
699
700         down(&udfs->sem);
701         if (!udfs->probed) {
702                 up(&udfs->sem);
703                 return -ENODEV;
704         }
705 #ifdef CONFIG_COMEDI_DEBUG
706         printk(KERN_DEBUG "comedi%d: usbduxfast_ai_inttrig\n", dev->minor);
707 #endif
708
709         if (trignum != 0) {
710                 printk(KERN_ERR "comedi%d: usbduxfast_ai_inttrig: invalid"
711                        " trignum\n", dev->minor);
712                 up(&udfs->sem);
713                 return -EINVAL;
714         }
715         if (!udfs->ai_cmd_running) {
716                 udfs->ai_cmd_running = 1;
717                 ret = usbduxfastsub_submit_InURBs(udfs);
718                 if (ret < 0) {
719                         printk(KERN_ERR "comedi%d: usbduxfast_ai_inttrig: "
720                                "urbSubmit: err=%d\n", dev->minor, ret);
721                         udfs->ai_cmd_running = 0;
722                         up(&udfs->sem);
723                         return ret;
724                 }
725                 s->async->inttrig = NULL;
726         } else {
727                 printk(KERN_ERR "comedi%d: ai_inttrig but acqu is already"
728                        " running\n", dev->minor);
729         }
730         up(&udfs->sem);
731         return 1;
732 }
733
734 /*
735  * offsets for the GPIF bytes
736  * the first byte is the command byte
737  */
738 #define LENBASE (1+0x00)
739 #define OPBASE  (1+0x08)
740 #define OUTBASE (1+0x10)
741 #define LOGBASE (1+0x18)
742
743 static int usbduxfast_ai_cmd(struct comedi_device *dev,
744                              struct comedi_subdevice *s)
745 {
746         struct comedi_cmd *cmd = &s->async->cmd;
747         unsigned int chan, gain, rngmask = 0xff;
748         int i, j, ret;
749         struct usbduxfastsub_s *udfs;
750         int result;
751         long steps, steps_tmp;
752
753 #ifdef CONFIG_COMEDI_DEBUG
754         printk(KERN_DEBUG "comedi%d: usbduxfast_ai_cmd\n", dev->minor);
755 #endif
756         udfs = dev->private;
757         if (!udfs)
758                 return -EFAULT;
759
760         down(&udfs->sem);
761         if (!udfs->probed) {
762                 up(&udfs->sem);
763                 return -ENODEV;
764         }
765         if (udfs->ai_cmd_running) {
766                 printk(KERN_ERR "comedi%d: ai_cmd not possible. Another ai_cmd"
767                        " is running.\n", dev->minor);
768                 up(&udfs->sem);
769                 return -EBUSY;
770         }
771         /* set current channel of the running acquisition to zero */
772         s->async->cur_chan = 0;
773
774         /*
775          * ignore the first buffers from the device if there
776          * is an error condition
777          */
778         udfs->ignore = PACKETS_TO_IGNORE;
779
780         if (cmd->chanlist_len > 0) {
781                 gain = CR_RANGE(cmd->chanlist[0]);
782                 for (i = 0; i < cmd->chanlist_len; ++i) {
783                         chan = CR_CHAN(cmd->chanlist[i]);
784                         if (chan != i) {
785                                 printk(KERN_ERR "comedi%d: cmd is accepting "
786                                        "only consecutive channels.\n",
787                                        dev->minor);
788                                 up(&udfs->sem);
789                                 return -EINVAL;
790                         }
791                         if ((gain != CR_RANGE(cmd->chanlist[i]))
792                             && (cmd->chanlist_len > 3)) {
793                                 printk(KERN_ERR "comedi%d: the gain must be"
794                                        " the same for all channels.\n",
795                                        dev->minor);
796                                 up(&udfs->sem);
797                                 return -EINVAL;
798                         }
799                         if (i >= NUMCHANNELS) {
800                                 printk(KERN_ERR "comedi%d: channel list too"
801                                        " long\n", dev->minor);
802                                 break;
803                         }
804                 }
805         }
806         steps = 0;
807         if (cmd->scan_begin_src == TRIG_TIMER) {
808                 printk(KERN_ERR "comedi%d: usbduxfast: "
809                        "scan_begin_src==TRIG_TIMER not valid.\n", dev->minor);
810                 up(&udfs->sem);
811                 return -EINVAL;
812         }
813         if (cmd->convert_src == TRIG_TIMER)
814                 steps = (cmd->convert_arg * 30) / 1000;
815
816         if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
817                 printk(KERN_ERR "comedi%d: usbduxfast: ai_cmd: steps=%ld, "
818                        "scan_begin_arg=%d. Not properly tested by cmdtest?\n",
819                        dev->minor, steps, cmd->scan_begin_arg);
820                 up(&udfs->sem);
821                 return -EINVAL;
822         }
823         if (steps > MAX_SAMPLING_PERIOD) {
824                 printk(KERN_ERR "comedi%d: usbduxfast: ai_cmd: sampling rate "
825                        "too low.\n", dev->minor);
826                 up(&udfs->sem);
827                 return -EINVAL;
828         }
829         if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
830             && (cmd->chanlist_len != 16)) {
831                 printk(KERN_ERR "comedi%d: usbduxfast: ai_cmd: TRIG_EXT only"
832                        " with 1 or 16 channels possible.\n", dev->minor);
833                 up(&udfs->sem);
834                 return -EINVAL;
835         }
836 #ifdef CONFIG_COMEDI_DEBUG
837         printk(KERN_DEBUG "comedi%d: usbduxfast: steps=%ld, convert_arg=%u\n",
838                dev->minor, steps, cmd->convert_arg);
839 #endif
840
841         switch (cmd->chanlist_len) {
842         case 1:
843                 /*
844                  * one channel
845                  */
846
847                 if (CR_RANGE(cmd->chanlist[0]) > 0)
848                         rngmask = 0xff - 0x04;
849                 else
850                         rngmask = 0xff;
851
852                 /*
853                  * for external trigger: looping in this state until
854                  * the RDY0 pin becomes zero
855                  */
856
857                 /* we loop here until ready has been set */
858                 if (cmd->start_src == TRIG_EXT) {
859                         /* branch back to state 0 */
860                         udfs->dux_commands[LENBASE + 0] = 0x01;
861                         /* deceision state w/o data */
862                         udfs->dux_commands[OPBASE + 0] = 0x01;
863                         udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
864                         /* RDY0 = 0 */
865                         udfs->dux_commands[LOGBASE + 0] = 0x00;
866                 } else {        /* we just proceed to state 1 */
867                         udfs->dux_commands[LENBASE + 0] = 1;
868                         udfs->dux_commands[OPBASE + 0] = 0;
869                         udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
870                         udfs->dux_commands[LOGBASE + 0] = 0;
871                 }
872
873                 if (steps < MIN_SAMPLING_PERIOD) {
874                         /* for fast single channel aqu without mux */
875                         if (steps <= 1) {
876                                 /*
877                                  * we just stay here at state 1 and rexecute
878                                  * the same state this gives us 30MHz sampling
879                                  * rate
880                                  */
881
882                                 /* branch back to state 1 */
883                                 udfs->dux_commands[LENBASE + 1] = 0x89;
884                                 /* deceision state with data */
885                                 udfs->dux_commands[OPBASE + 1] = 0x03;
886                                 udfs->dux_commands[OUTBASE + 1] =
887                                     0xFF & rngmask;
888                                 /* doesn't matter */
889                                 udfs->dux_commands[LOGBASE + 1] = 0xFF;
890                         } else {
891                                 /*
892                                  * we loop through two states: data and delay
893                                  * max rate is 15MHz
894                                  */
895                                 udfs->dux_commands[LENBASE + 1] = steps - 1;
896                                 /* data */
897                                 udfs->dux_commands[OPBASE + 1] = 0x02;
898                                 udfs->dux_commands[OUTBASE + 1] =
899                                     0xFF & rngmask;
900                                 /* doesn't matter */
901                                 udfs->dux_commands[LOGBASE + 1] = 0;
902                                 /* branch back to state 1 */
903                                 udfs->dux_commands[LENBASE + 2] = 0x09;
904                                 /* deceision state w/o data */
905                                 udfs->dux_commands[OPBASE + 2] = 0x01;
906                                 udfs->dux_commands[OUTBASE + 2] =
907                                     0xFF & rngmask;
908                                 /* doesn't matter */
909                                 udfs->dux_commands[LOGBASE + 2] = 0xFF;
910                         }
911                 } else {
912                         /*
913                          * we loop through 3 states: 2x delay and 1x data
914                          * this gives a min sampling rate of 60kHz
915                          */
916
917                         /* we have 1 state with duration 1 */
918                         steps = steps - 1;
919
920                         /* do the first part of the delay */
921                         udfs->dux_commands[LENBASE + 1] = steps / 2;
922                         udfs->dux_commands[OPBASE + 1] = 0;
923                         udfs->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
924                         udfs->dux_commands[LOGBASE + 1] = 0;
925
926                         /* and the second part */
927                         udfs->dux_commands[LENBASE + 2] = steps - steps / 2;
928                         udfs->dux_commands[OPBASE + 2] = 0;
929                         udfs->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
930                         udfs->dux_commands[LOGBASE + 2] = 0;
931
932                         /* get the data and branch back */
933
934                         /* branch back to state 1 */
935                         udfs->dux_commands[LENBASE + 3] = 0x09;
936                         /* deceision state w data */
937                         udfs->dux_commands[OPBASE + 3] = 0x03;
938                         udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
939                         /* doesn't matter */
940                         udfs->dux_commands[LOGBASE + 3] = 0xFF;
941                 }
942                 break;
943
944         case 2:
945                 /*
946                  * two channels
947                  * commit data to the FIFO
948                  */
949
950                 if (CR_RANGE(cmd->chanlist[0]) > 0)
951                         rngmask = 0xff - 0x04;
952                 else
953                         rngmask = 0xff;
954
955                 udfs->dux_commands[LENBASE + 0] = 1;
956                 /* data */
957                 udfs->dux_commands[OPBASE + 0] = 0x02;
958                 udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
959                 udfs->dux_commands[LOGBASE + 0] = 0;
960
961                 /* we have 1 state with duration 1: state 0 */
962                 steps_tmp = steps - 1;
963
964                 if (CR_RANGE(cmd->chanlist[1]) > 0)
965                         rngmask = 0xff - 0x04;
966                 else
967                         rngmask = 0xff;
968
969                 /* do the first part of the delay */
970                 udfs->dux_commands[LENBASE + 1] = steps_tmp / 2;
971                 udfs->dux_commands[OPBASE + 1] = 0;
972                 /* count */
973                 udfs->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
974                 udfs->dux_commands[LOGBASE + 1] = 0;
975
976                 /* and the second part */
977                 udfs->dux_commands[LENBASE + 2] = steps_tmp - steps_tmp / 2;
978                 udfs->dux_commands[OPBASE + 2] = 0;
979                 udfs->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
980                 udfs->dux_commands[LOGBASE + 2] = 0;
981
982                 udfs->dux_commands[LENBASE + 3] = 1;
983                 /* data */
984                 udfs->dux_commands[OPBASE + 3] = 0x02;
985                 udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
986                 udfs->dux_commands[LOGBASE + 3] = 0;
987
988                 /*
989                  * we have 2 states with duration 1: step 6 and
990                  * the IDLE state
991                  */
992                 steps_tmp = steps - 2;
993
994                 if (CR_RANGE(cmd->chanlist[0]) > 0)
995                         rngmask = 0xff - 0x04;
996                 else
997                         rngmask = 0xff;
998
999                 /* do the first part of the delay */
1000                 udfs->dux_commands[LENBASE + 4] = steps_tmp / 2;
1001                 udfs->dux_commands[OPBASE + 4] = 0;
1002                 /* reset */
1003                 udfs->dux_commands[OUTBASE + 4] = (0xFF - 0x02) & rngmask;
1004                 udfs->dux_commands[LOGBASE + 4] = 0;
1005
1006                 /* and the second part */
1007                 udfs->dux_commands[LENBASE + 5] = steps_tmp - steps_tmp / 2;
1008                 udfs->dux_commands[OPBASE + 5] = 0;
1009                 udfs->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1010                 udfs->dux_commands[LOGBASE + 5] = 0;
1011
1012                 udfs->dux_commands[LENBASE + 6] = 1;
1013                 udfs->dux_commands[OPBASE + 6] = 0;
1014                 udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1015                 udfs->dux_commands[LOGBASE + 6] = 0;
1016                 break;
1017
1018         case 3:
1019                 /*
1020                  * three channels
1021                  */
1022                 for (j = 0; j < 1; j++) {
1023                         if (CR_RANGE(cmd->chanlist[j]) > 0)
1024                                 rngmask = 0xff - 0x04;
1025                         else
1026                                 rngmask = 0xff;
1027                         /*
1028                          * commit data to the FIFO and do the first part
1029                          * of the delay
1030                          */
1031                         udfs->dux_commands[LENBASE + j * 2] = steps / 2;
1032                         /* data */
1033                         udfs->dux_commands[OPBASE + j * 2] = 0x02;
1034                         /* no change */
1035                         udfs->dux_commands[OUTBASE + j * 2] = 0xFF & rngmask;
1036                         udfs->dux_commands[LOGBASE + j * 2] = 0;
1037
1038                         if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
1039                                 rngmask = 0xff - 0x04;
1040                         else
1041                                 rngmask = 0xff;
1042
1043                         /* do the second part of the delay */
1044                         udfs->dux_commands[LENBASE + j * 2 + 1] =
1045                             steps - steps / 2;
1046                         /* no data */
1047                         udfs->dux_commands[OPBASE + j * 2 + 1] = 0;
1048                         /* count */
1049                         udfs->dux_commands[OUTBASE + j * 2 + 1] =
1050                             0xFE & rngmask;
1051                         udfs->dux_commands[LOGBASE + j * 2 + 1] = 0;
1052                 }
1053
1054                 /* 2 steps with duration 1: the idele step and step 6: */
1055                 steps_tmp = steps - 2;
1056
1057                 /* commit data to the FIFO and do the first part of the delay */
1058                 udfs->dux_commands[LENBASE + 4] = steps_tmp / 2;
1059                 /* data */
1060                 udfs->dux_commands[OPBASE + 4] = 0x02;
1061                 udfs->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1062                 udfs->dux_commands[LOGBASE + 4] = 0;
1063
1064                 if (CR_RANGE(cmd->chanlist[0]) > 0)
1065                         rngmask = 0xff - 0x04;
1066                 else
1067                         rngmask = 0xff;
1068
1069                 /* do the second part of the delay */
1070                 udfs->dux_commands[LENBASE + 5] = steps_tmp - steps_tmp / 2;
1071                 /* no data */
1072                 udfs->dux_commands[OPBASE + 5] = 0;
1073                 /* reset */
1074                 udfs->dux_commands[OUTBASE + 5] = (0xFF - 0x02) & rngmask;
1075                 udfs->dux_commands[LOGBASE + 5] = 0;
1076
1077                 udfs->dux_commands[LENBASE + 6] = 1;
1078                 udfs->dux_commands[OPBASE + 6] = 0;
1079                 udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1080                 udfs->dux_commands[LOGBASE + 6] = 0;
1081
1082         case 16:
1083                 if (CR_RANGE(cmd->chanlist[0]) > 0)
1084                         rngmask = 0xff - 0x04;
1085                 else
1086                         rngmask = 0xff;
1087
1088                 if (cmd->start_src == TRIG_EXT) {
1089                         /*
1090                          * we loop here until ready has been set
1091                          */
1092
1093                         /* branch back to state 0 */
1094                         udfs->dux_commands[LENBASE + 0] = 0x01;
1095                         /* deceision state w/o data */
1096                         udfs->dux_commands[OPBASE + 0] = 0x01;
1097                         /* reset */
1098                         udfs->dux_commands[OUTBASE + 0] =
1099                             (0xFF - 0x02) & rngmask;
1100                         /* RDY0 = 0 */
1101                         udfs->dux_commands[LOGBASE + 0] = 0x00;
1102                 } else {
1103                         /*
1104                          * we just proceed to state 1
1105                          */
1106
1107                         /* 30us reset pulse */
1108                         udfs->dux_commands[LENBASE + 0] = 255;
1109                         udfs->dux_commands[OPBASE + 0] = 0;
1110                         /* reset */
1111                         udfs->dux_commands[OUTBASE + 0] =
1112                             (0xFF - 0x02) & rngmask;
1113                         udfs->dux_commands[LOGBASE + 0] = 0;
1114                 }
1115
1116                 /* commit data to the FIFO */
1117                 udfs->dux_commands[LENBASE + 1] = 1;
1118                 /* data */
1119                 udfs->dux_commands[OPBASE + 1] = 0x02;
1120                 udfs->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
1121                 udfs->dux_commands[LOGBASE + 1] = 0;
1122
1123                 /* we have 2 states with duration 1 */
1124                 steps = steps - 2;
1125
1126                 /* do the first part of the delay */
1127                 udfs->dux_commands[LENBASE + 2] = steps / 2;
1128                 udfs->dux_commands[OPBASE + 2] = 0;
1129                 udfs->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1130                 udfs->dux_commands[LOGBASE + 2] = 0;
1131
1132                 /* and the second part */
1133                 udfs->dux_commands[LENBASE + 3] = steps - steps / 2;
1134                 udfs->dux_commands[OPBASE + 3] = 0;
1135                 udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
1136                 udfs->dux_commands[LOGBASE + 3] = 0;
1137
1138                 /* branch back to state 1 */
1139                 udfs->dux_commands[LENBASE + 4] = 0x09;
1140                 /* deceision state w/o data */
1141                 udfs->dux_commands[OPBASE + 4] = 0x01;
1142                 udfs->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1143                 /* doesn't matter */
1144                 udfs->dux_commands[LOGBASE + 4] = 0xFF;
1145
1146                 break;
1147
1148         default:
1149                 printk(KERN_ERR "comedi %d: unsupported combination of "
1150                        "channels\n", dev->minor);
1151                 up(&udfs->sem);
1152                 return -EFAULT;
1153         }
1154
1155 #ifdef CONFIG_COMEDI_DEBUG
1156         printk(KERN_DEBUG "comedi %d: sending commands to the usb device\n",
1157                dev->minor);
1158 #endif
1159         /* 0 means that the AD commands are sent */
1160         result = send_dux_commands(udfs, SENDADCOMMANDS);
1161         if (result < 0) {
1162                 printk(KERN_ERR "comedi%d: adc command could not be submitted."
1163                        "Aborting...\n", dev->minor);
1164                 up(&udfs->sem);
1165                 return result;
1166         }
1167         if (cmd->stop_src == TRIG_COUNT) {
1168                 udfs->ai_sample_count = cmd->stop_arg * cmd->scan_end_arg;
1169                 if (udfs->ai_sample_count < 1) {
1170                         printk(KERN_ERR "comedi%d: "
1171                                "(cmd->stop_arg)*(cmd->scan_end_arg)<1, "
1172                                "aborting.\n", dev->minor);
1173                         up(&udfs->sem);
1174                         return -EFAULT;
1175                 }
1176                 udfs->ai_continous = 0;
1177         } else {
1178                 /* continous acquisition */
1179                 udfs->ai_continous = 1;
1180                 udfs->ai_sample_count = 0;
1181         }
1182
1183         if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
1184                 /* enable this acquisition operation */
1185                 udfs->ai_cmd_running = 1;
1186                 ret = usbduxfastsub_submit_InURBs(udfs);
1187                 if (ret < 0) {
1188                         udfs->ai_cmd_running = 0;
1189                         /* fixme: unlink here?? */
1190                         up(&udfs->sem);
1191                         return ret;
1192                 }
1193                 s->async->inttrig = NULL;
1194         } else {
1195                 /*
1196                  * TRIG_INT
1197                  * don't enable the acquision operation
1198                  * wait for an internal signal
1199                  */
1200                 s->async->inttrig = usbduxfast_ai_inttrig;
1201         }
1202         up(&udfs->sem);
1203
1204         return 0;
1205 }
1206
1207 /*
1208  * Mode 0 is used to get a single conversion on demand.
1209  */
1210 static int usbduxfast_ai_insn_read(struct comedi_device *dev,
1211                                    struct comedi_subdevice *s,
1212                                    struct comedi_insn *insn, unsigned int *data)
1213 {
1214         int i, j, n, actual_length;
1215         int chan, range, rngmask;
1216         int err;
1217         struct usbduxfastsub_s *udfs;
1218
1219         udfs = dev->private;
1220         if (!udfs) {
1221                 printk(KERN_ERR "comedi%d: ai_insn_read: no usb dev.\n",
1222                        dev->minor);
1223                 return -ENODEV;
1224         }
1225 #ifdef CONFIG_COMEDI_DEBUG
1226         printk(KERN_DEBUG "comedi%d: ai_insn_read, insn->n=%d, "
1227                "insn->subdev=%d\n", dev->minor, insn->n, insn->subdev);
1228 #endif
1229         down(&udfs->sem);
1230         if (!udfs->probed) {
1231                 up(&udfs->sem);
1232                 return -ENODEV;
1233         }
1234         if (udfs->ai_cmd_running) {
1235                 printk(KERN_ERR "comedi%d: ai_insn_read not possible. Async "
1236                        "Command is running.\n", dev->minor);
1237                 up(&udfs->sem);
1238                 return -EBUSY;
1239         }
1240         /* sample one channel */
1241         chan = CR_CHAN(insn->chanspec);
1242         range = CR_RANGE(insn->chanspec);
1243         /* set command for the first channel */
1244
1245         if (range > 0)
1246                 rngmask = 0xff - 0x04;
1247         else
1248                 rngmask = 0xff;
1249
1250         /* commit data to the FIFO */
1251         udfs->dux_commands[LENBASE + 0] = 1;
1252         /* data */
1253         udfs->dux_commands[OPBASE + 0] = 0x02;
1254         udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
1255         udfs->dux_commands[LOGBASE + 0] = 0;
1256
1257         /* do the first part of the delay */
1258         udfs->dux_commands[LENBASE + 1] = 12;
1259         udfs->dux_commands[OPBASE + 1] = 0;
1260         udfs->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
1261         udfs->dux_commands[LOGBASE + 1] = 0;
1262
1263         udfs->dux_commands[LENBASE + 2] = 1;
1264         udfs->dux_commands[OPBASE + 2] = 0;
1265         udfs->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1266         udfs->dux_commands[LOGBASE + 2] = 0;
1267
1268         udfs->dux_commands[LENBASE + 3] = 1;
1269         udfs->dux_commands[OPBASE + 3] = 0;
1270         udfs->dux_commands[OUTBASE + 3] = 0xFE & rngmask;
1271         udfs->dux_commands[LOGBASE + 3] = 0;
1272
1273         udfs->dux_commands[LENBASE + 4] = 1;
1274         udfs->dux_commands[OPBASE + 4] = 0;
1275         udfs->dux_commands[OUTBASE + 4] = 0xFE & rngmask;
1276         udfs->dux_commands[LOGBASE + 4] = 0;
1277
1278         /* second part */
1279         udfs->dux_commands[LENBASE + 5] = 12;
1280         udfs->dux_commands[OPBASE + 5] = 0;
1281         udfs->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1282         udfs->dux_commands[LOGBASE + 5] = 0;
1283
1284         udfs->dux_commands[LENBASE + 6] = 1;
1285         udfs->dux_commands[OPBASE + 6] = 0;
1286         udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1287         udfs->dux_commands[LOGBASE + 0] = 0;
1288
1289 #ifdef CONFIG_COMEDI_DEBUG
1290         printk(KERN_DEBUG "comedi %d: sending commands to the usb device\n",
1291                dev->minor);
1292 #endif
1293         /* 0 means that the AD commands are sent */
1294         err = send_dux_commands(udfs, SENDADCOMMANDS);
1295         if (err < 0) {
1296                 printk(KERN_ERR "comedi%d: adc command could not be submitted."
1297                        "Aborting...\n", dev->minor);
1298                 up(&udfs->sem);
1299                 return err;
1300         }
1301 #ifdef CONFIG_COMEDI_DEBUG
1302         printk(KERN_DEBUG "comedi%d: usbduxfast: submitting in-urb: "
1303                "0x%p,0x%p\n", udfs->comedidev->minor, udfs->urbIn->context,
1304                udfs->urbIn->dev);
1305 #endif
1306         for (i = 0; i < PACKETS_TO_IGNORE; i++) {
1307                 err = usb_bulk_msg(udfs->usbdev,
1308                                    usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
1309                                    udfs->transfer_buffer, SIZEINBUF,
1310                                    &actual_length, 10000);
1311                 if (err < 0) {
1312                         printk(KERN_ERR "comedi%d: insn timeout. No data.\n",
1313                                dev->minor);
1314                         up(&udfs->sem);
1315                         return err;
1316                 }
1317         }
1318         /* data points */
1319         for (i = 0; i < insn->n;) {
1320                 err = usb_bulk_msg(udfs->usbdev,
1321                                    usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
1322                                    udfs->transfer_buffer, SIZEINBUF,
1323                                    &actual_length, 10000);
1324                 if (err < 0) {
1325                         printk(KERN_ERR "comedi%d: insn data error: %d\n",
1326                                dev->minor, err);
1327                         up(&udfs->sem);
1328                         return err;
1329                 }
1330                 n = actual_length / sizeof(uint16_t);
1331                 if ((n % 16) != 0) {
1332                         printk(KERN_ERR "comedi%d: insn data packet "
1333                                "corrupted.\n", dev->minor);
1334                         up(&udfs->sem);
1335                         return -EINVAL;
1336                 }
1337                 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
1338                         data[i] = ((uint16_t *) (udfs->transfer_buffer))[j];
1339                         i++;
1340                 }
1341         }
1342         up(&udfs->sem);
1343         return i;
1344 }
1345
1346 #define FIRMWARE_MAX_LEN 0x2000
1347
1348 static int firmwareUpload(struct usbduxfastsub_s *usbduxfastsub,
1349                           const u8 *firmwareBinary, int sizeFirmware)
1350 {
1351         int ret;
1352         uint8_t *fwBuf;
1353
1354         if (!firmwareBinary)
1355                 return 0;
1356
1357         if (sizeFirmware > FIRMWARE_MAX_LEN) {
1358                 dev_err(&usbduxfastsub->interface->dev,
1359                         "comedi_: usbduxfast firmware binary it too large for FX2.\n");
1360                 return -ENOMEM;
1361         }
1362
1363         /* we generate a local buffer for the firmware */
1364         fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
1365         if (!fwBuf) {
1366                 dev_err(&usbduxfastsub->interface->dev,
1367                         "comedi_: mem alloc for firmware failed\n");
1368                 return -ENOMEM;
1369         }
1370
1371         ret = usbduxfastsub_stop(usbduxfastsub);
1372         if (ret < 0) {
1373                 dev_err(&usbduxfastsub->interface->dev,
1374                         "comedi_: can not stop firmware\n");
1375                 kfree(fwBuf);
1376                 return ret;
1377         }
1378
1379         ret = usbduxfastsub_upload(usbduxfastsub, fwBuf, 0, sizeFirmware);
1380         if (ret < 0) {
1381                 dev_err(&usbduxfastsub->interface->dev,
1382                         "comedi_: firmware upload failed\n");
1383                 kfree(fwBuf);
1384                 return ret;
1385         }
1386         ret = usbduxfastsub_start(usbduxfastsub);
1387         if (ret < 0) {
1388                 dev_err(&usbduxfastsub->interface->dev,
1389                         "comedi_: can not start firmware\n");
1390                 kfree(fwBuf);
1391                 return ret;
1392         }
1393         kfree(fwBuf);
1394         return 0;
1395 }
1396
1397 static void tidy_up(struct usbduxfastsub_s *udfs)
1398 {
1399 #ifdef CONFIG_COMEDI_DEBUG
1400         printk(KERN_DEBUG "comedi_: usbduxfast: tiding up\n");
1401 #endif
1402
1403         if (!udfs)
1404                 return;
1405
1406         /* shows the usb subsystem that the driver is down */
1407         if (udfs->interface)
1408                 usb_set_intfdata(udfs->interface, NULL);
1409
1410         udfs->probed = 0;
1411
1412         if (udfs->urbIn) {
1413                 /* waits until a running transfer is over */
1414                 usb_kill_urb(udfs->urbIn);
1415
1416                 kfree(udfs->transfer_buffer);
1417                 udfs->transfer_buffer = NULL;
1418
1419                 usb_free_urb(udfs->urbIn);
1420                 udfs->urbIn = NULL;
1421         }
1422
1423         kfree(udfs->insnBuffer);
1424         udfs->insnBuffer = NULL;
1425
1426         kfree(udfs->dux_commands);
1427         udfs->dux_commands = NULL;
1428
1429         udfs->ai_cmd_running = 0;
1430 }
1431
1432 /* common part of attach and attach_usb */
1433 static int usbduxfast_attach_common(struct comedi_device *dev,
1434                                     struct usbduxfastsub_s *udfs,
1435                                     void *aux_data, int aux_len)
1436 {
1437         int ret;
1438         struct comedi_subdevice *s;
1439
1440         down(&udfs->sem);
1441         /* pointer back to the corresponding comedi device */
1442         udfs->comedidev = dev;
1443         /* trying to upload the firmware into the chip */
1444         if (aux_data)
1445                 firmwareUpload(udfs, aux_data, aux_len);
1446         dev->board_name = "usbduxfast";
1447         ret = comedi_alloc_subdevices(dev, 1);
1448         if (ret) {
1449                 up(&udfs->sem);
1450                 return ret;
1451         }
1452         /* private structure is also simply the usb-structure */
1453         dev->private = udfs;
1454         /* the first subdevice is the A/D converter */
1455         s = dev->subdevices + SUBDEV_AD;
1456         /*
1457          * the URBs get the comedi subdevice which is responsible for reading
1458          * this is the subdevice which reads data
1459          */
1460         dev->read_subdev = s;
1461         /* the subdevice receives as private structure the usb-structure */
1462         s->private = NULL;
1463         /* analog input */
1464         s->type = COMEDI_SUBD_AI;
1465         /* readable and ref is to ground */
1466         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1467         /* 16 channels */
1468         s->n_chan = 16;
1469         /* length of the channellist */
1470         s->len_chanlist = 16;
1471         /* callback functions */
1472         s->insn_read = usbduxfast_ai_insn_read;
1473         s->do_cmdtest = usbduxfast_ai_cmdtest;
1474         s->do_cmd = usbduxfast_ai_cmd;
1475         s->cancel = usbduxfast_ai_cancel;
1476         /* max value from the A/D converter (12bit+1 bit for overflow) */
1477         s->maxdata = 0x1000;
1478         /* range table to convert to physical units */
1479         s->range_table = &range_usbduxfast_ai_range;
1480         /* finally decide that it's attached */
1481         udfs->attached = 1;
1482         up(&udfs->sem);
1483         dev_info(dev->class_dev, "successfully attached to usbduxfast.\n");
1484         return 0;
1485 }
1486
1487 /* is called for COMEDI_DEVCONFIG ioctl (when comedi_config is run) */
1488 static int usbduxfast_attach(struct comedi_device *dev,
1489                              struct comedi_devconfig *it)
1490 {
1491         int ret;
1492         int index;
1493         int i;
1494         void *aux_data;
1495         int aux_len;
1496
1497         dev->private = NULL;
1498
1499         aux_data = comedi_aux_data(it->options, 0);
1500         aux_len = it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH];
1501         if (aux_data == NULL)
1502                 aux_len = 0;
1503         else if (aux_len == 0)
1504                 aux_data = NULL;
1505         down(&start_stop_sem);
1506         /*
1507          * find a valid device which has been detected by the
1508          * probe function of the usb
1509          */
1510         index = -1;
1511         for (i = 0; i < NUMUSBDUXFAST; i++) {
1512                 if (usbduxfastsub[i].probed && !usbduxfastsub[i].attached) {
1513                         index = i;
1514                         break;
1515                 }
1516         }
1517         if (index < 0) {
1518                 dev_err(dev->class_dev,
1519                         "usbduxfast: error: attach failed, no usbduxfast devs connected to the usb bus.\n");
1520                 ret = -ENODEV;
1521         } else
1522                 ret = usbduxfast_attach_common(dev, &usbduxfastsub[index],
1523                                                aux_data, aux_len);
1524         up(&start_stop_sem);
1525         return ret;
1526 }
1527
1528 /* is called from comedi_usb_auto_config() */
1529 static int usbduxfast_attach_usb(struct comedi_device *dev,
1530                                  struct usb_interface *uinterf)
1531 {
1532         int ret;
1533         struct usbduxfastsub_s *udfs;
1534
1535         dev->private = NULL;
1536         down(&start_stop_sem);
1537         udfs = usb_get_intfdata(uinterf);
1538         if (!udfs || !udfs->probed) {
1539                 dev_err(dev->class_dev,
1540                         "usbduxfast: error: attach_usb failed, not connected\n");
1541                 ret = -ENODEV;
1542         } else if (udfs->attached) {
1543                 dev_err(dev->class_dev,
1544                        "usbduxfast: error: attach_usb failed, already attached\n");
1545                 ret = -ENODEV;
1546         } else
1547                 ret = usbduxfast_attach_common(dev, udfs, NULL, 0);
1548         up(&start_stop_sem);
1549         return ret;
1550 }
1551
1552 static void usbduxfast_detach(struct comedi_device *dev)
1553 {
1554         struct usbduxfastsub_s *usb = dev->private;
1555
1556         if (usb) {
1557                 down(&usb->sem);
1558                 down(&start_stop_sem);
1559                 dev->private = NULL;
1560                 usb->attached = 0;
1561                 usb->comedidev = NULL;
1562                 up(&start_stop_sem);
1563                 up(&usb->sem);
1564         }
1565 }
1566
1567 static struct comedi_driver usbduxfast_driver = {
1568         .driver_name    = "usbduxfast",
1569         .module         = THIS_MODULE,
1570         .attach         = usbduxfast_attach,
1571         .detach         = usbduxfast_detach,
1572         .attach_usb     = usbduxfast_attach_usb,
1573 };
1574
1575 static void usbduxfast_firmware_request_complete_handler(const struct firmware
1576                                                          *fw, void *context)
1577 {
1578         struct usbduxfastsub_s *usbduxfastsub_tmp = context;
1579         struct usb_interface *uinterf = usbduxfastsub_tmp->interface;
1580         int ret;
1581
1582         if (fw == NULL)
1583                 return;
1584
1585         /*
1586          * we need to upload the firmware here because fw will be
1587          * freed once we've left this function
1588          */
1589         ret = firmwareUpload(usbduxfastsub_tmp, fw->data, fw->size);
1590
1591         if (ret) {
1592                 dev_err(&uinterf->dev,
1593                         "Could not upload firmware (err=%d)\n", ret);
1594                 goto out;
1595         }
1596
1597         comedi_usb_auto_config(uinterf, &usbduxfast_driver);
1598  out:
1599         release_firmware(fw);
1600 }
1601
1602 static int usbduxfast_usb_probe(struct usb_interface *uinterf,
1603                                 const struct usb_device_id *id)
1604 {
1605         struct usb_device *udev = interface_to_usbdev(uinterf);
1606         int i;
1607         int index;
1608         int ret;
1609
1610         if (udev->speed != USB_SPEED_HIGH) {
1611                 printk(KERN_ERR "comedi_: usbduxfast_: This driver needs"
1612                        "USB 2.0 to operate. Aborting...\n");
1613                 return -ENODEV;
1614         }
1615 #ifdef CONFIG_COMEDI_DEBUG
1616         printk(KERN_DEBUG "comedi_: usbduxfast_: finding a free structure for "
1617                "the usb-device\n");
1618 #endif
1619         down(&start_stop_sem);
1620         /* look for a free place in the usbduxfast array */
1621         index = -1;
1622         for (i = 0; i < NUMUSBDUXFAST; i++) {
1623                 if (!usbduxfastsub[i].probed) {
1624                         index = i;
1625                         break;
1626                 }
1627         }
1628
1629         /* no more space */
1630         if (index == -1) {
1631                 printk(KERN_ERR "Too many usbduxfast-devices connected.\n");
1632                 up(&start_stop_sem);
1633                 return -EMFILE;
1634         }
1635 #ifdef CONFIG_COMEDI_DEBUG
1636         printk(KERN_DEBUG "comedi_: usbduxfast: usbduxfastsub[%d] is ready to "
1637                "connect to comedi.\n", index);
1638 #endif
1639
1640         sema_init(&(usbduxfastsub[index].sem), 1);
1641         /* save a pointer to the usb device */
1642         usbduxfastsub[index].usbdev = udev;
1643
1644         /* save the interface itself */
1645         usbduxfastsub[index].interface = uinterf;
1646         /* get the interface number from the interface */
1647         usbduxfastsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
1648         /*
1649          * hand the private data over to the usb subsystem
1650          * will be needed for disconnect
1651          */
1652         usb_set_intfdata(uinterf, &(usbduxfastsub[index]));
1653
1654 #ifdef CONFIG_COMEDI_DEBUG
1655         printk(KERN_DEBUG "comedi_: usbduxfast: ifnum=%d\n",
1656                usbduxfastsub[index].ifnum);
1657 #endif
1658         /* create space for the commands going to the usb device */
1659         usbduxfastsub[index].dux_commands = kmalloc(SIZEOFDUXBUFFER,
1660                                                     GFP_KERNEL);
1661         if (!usbduxfastsub[index].dux_commands) {
1662                 printk(KERN_ERR "comedi_: usbduxfast: error alloc space for "
1663                        "dac commands\n");
1664                 tidy_up(&(usbduxfastsub[index]));
1665                 up(&start_stop_sem);
1666                 return -ENOMEM;
1667         }
1668         /* create space of the instruction buffer */
1669         usbduxfastsub[index].insnBuffer = kmalloc(SIZEINSNBUF, GFP_KERNEL);
1670         if (!usbduxfastsub[index].insnBuffer) {
1671                 printk(KERN_ERR "comedi_: usbduxfast: could not alloc space "
1672                        "for insnBuffer\n");
1673                 tidy_up(&(usbduxfastsub[index]));
1674                 up(&start_stop_sem);
1675                 return -ENOMEM;
1676         }
1677         /* setting to alternate setting 1: enabling bulk ep */
1678         i = usb_set_interface(usbduxfastsub[index].usbdev,
1679                               usbduxfastsub[index].ifnum, 1);
1680         if (i < 0) {
1681                 printk(KERN_ERR "comedi_: usbduxfast%d: could not switch to "
1682                        "alternate setting 1.\n", index);
1683                 tidy_up(&(usbduxfastsub[index]));
1684                 up(&start_stop_sem);
1685                 return -ENODEV;
1686         }
1687         usbduxfastsub[index].urbIn = usb_alloc_urb(0, GFP_KERNEL);
1688         if (!usbduxfastsub[index].urbIn) {
1689                 printk(KERN_ERR "comedi_: usbduxfast%d: Could not alloc."
1690                        "urb\n", index);
1691                 tidy_up(&(usbduxfastsub[index]));
1692                 up(&start_stop_sem);
1693                 return -ENOMEM;
1694         }
1695         usbduxfastsub[index].transfer_buffer = kmalloc(SIZEINBUF, GFP_KERNEL);
1696         if (!usbduxfastsub[index].transfer_buffer) {
1697                 printk(KERN_ERR "comedi_: usbduxfast%d: could not alloc. "
1698                        "transb.\n", index);
1699                 tidy_up(&(usbduxfastsub[index]));
1700                 up(&start_stop_sem);
1701                 return -ENOMEM;
1702         }
1703         /* we've reached the bottom of the function */
1704         usbduxfastsub[index].probed = 1;
1705         up(&start_stop_sem);
1706
1707         ret = request_firmware_nowait(THIS_MODULE,
1708                                       FW_ACTION_HOTPLUG,
1709                                       "usbduxfast_firmware.bin",
1710                                       &udev->dev,
1711                                       GFP_KERNEL,
1712                                       usbduxfastsub + index,
1713                                       usbduxfast_firmware_request_complete_handler);
1714
1715         if (ret) {
1716                 dev_err(&udev->dev, "could not load firmware (err=%d)\n", ret);
1717                 return ret;
1718         }
1719
1720         printk(KERN_INFO "comedi_: usbduxfast%d has been successfully "
1721                "initialized.\n", index);
1722         /* success */
1723         return 0;
1724 }
1725
1726 static void usbduxfast_usb_disconnect(struct usb_interface *intf)
1727 {
1728         struct usbduxfastsub_s *udfs = usb_get_intfdata(intf);
1729         struct usb_device *udev = interface_to_usbdev(intf);
1730
1731         if (!udfs) {
1732                 printk(KERN_ERR "comedi_: usbduxfast: disconnect called with "
1733                        "null pointer.\n");
1734                 return;
1735         }
1736         if (udfs->usbdev != udev) {
1737                 printk(KERN_ERR "comedi_: usbduxfast: BUG! called with wrong "
1738                        "ptr!!!\n");
1739                 return;
1740         }
1741
1742         comedi_usb_auto_unconfig(intf);
1743
1744         down(&start_stop_sem);
1745         down(&udfs->sem);
1746         tidy_up(udfs);
1747         up(&udfs->sem);
1748         up(&start_stop_sem);
1749
1750 #ifdef CONFIG_COMEDI_DEBUG
1751         printk(KERN_DEBUG "comedi_: usbduxfast: disconnected from the usb\n");
1752 #endif
1753 }
1754
1755 static const struct usb_device_id usbduxfast_usb_table[] = {
1756         /* { USB_DEVICE(0x4b4, 0x8613) }, testing */
1757         { USB_DEVICE(0x13d8, 0x0010) }, /* real ID */
1758         { USB_DEVICE(0x13d8, 0x0011) }, /* real ID */
1759         { }
1760 };
1761 MODULE_DEVICE_TABLE(usb, usbduxfast_usb_table);
1762
1763 static struct usb_driver usbduxfast_usb_driver = {
1764 #ifdef COMEDI_HAVE_USB_DRIVER_OWNER
1765         .owner          = THIS_MODULE,
1766 #endif
1767         .name           = "usbduxfast",
1768         .probe          = usbduxfast_usb_probe,
1769         .disconnect     = usbduxfast_usb_disconnect,
1770         .id_table       = usbduxfast_usb_table,
1771 };
1772 module_comedi_usb_driver(usbduxfast_driver, usbduxfast_usb_driver);
1773
1774 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1775 MODULE_DESCRIPTION("USB-DUXfast, BerndPorr@f2s.com");
1776 MODULE_LICENSE("GPL");