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