]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/usbduxfast.c
Merge tag 'late-omap' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[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
316         /* sanity checks - is the urb there? */
317         if (!urb) {
318                 pr_err("ao int-handler called with urb=NULL!\n");
319                 return;
320         }
321         /* the context variable points to the subdevice */
322         this_comedidev = urb->context;
323         if (!this_comedidev) {
324                 pr_err("urb context is a NULL pointer!\n");
325                 return;
326         }
327         /* the private structure of the subdevice is usbduxfastsub_s */
328         udfs = this_comedidev->private;
329         if (!udfs) {
330                 pr_err("private of comedi 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                 pr_err("non-zero urb status received in ai intr context: %d\n",
373                        urb->status);
374                 s->async->events |= COMEDI_CB_EOA;
375                 s->async->events |= COMEDI_CB_ERROR;
376                 comedi_event(udfs->comedidev, s);
377                 usbduxfast_ai_stop(udfs, 0);
378                 return;
379         }
380
381         if (!udfs->ignore) {
382                 if (!udfs->ai_continous) {
383                         /* not continuous, fixed number of samples */
384                         n = urb->actual_length / sizeof(uint16_t);
385                         if (unlikely(udfs->ai_sample_count < n)) {
386                                 /*
387                                  * we have send only a fraction of the bytes
388                                  * received
389                                  */
390                                 cfc_write_array_to_buffer(s,
391                                                           urb->transfer_buffer,
392                                                           udfs->ai_sample_count
393                                                           * sizeof(uint16_t));
394                                 usbduxfast_ai_stop(udfs, 0);
395                                 /* tell comedi that the acquistion is over */
396                                 s->async->events |= COMEDI_CB_EOA;
397                                 comedi_event(udfs->comedidev, s);
398                                 return;
399                         }
400                         udfs->ai_sample_count -= n;
401                 }
402                 /* write the full buffer to comedi */
403                 err = cfc_write_array_to_buffer(s, urb->transfer_buffer,
404                                                 urb->actual_length);
405                 if (unlikely(err == 0)) {
406                         /* buffer overflow */
407                         usbduxfast_ai_stop(udfs, 0);
408                         return;
409                 }
410
411                 /* tell comedi that data is there */
412                 comedi_event(udfs->comedidev, s);
413
414         } else {
415                 /* ignore this packet */
416                 udfs->ignore--;
417         }
418
419         /*
420          * command is still running
421          * resubmit urb for BULK transfer
422          */
423         urb->dev = udfs->usbdev;
424         urb->status = 0;
425         err = usb_submit_urb(urb, GFP_ATOMIC);
426         if (err < 0) {
427                 dev_err(&urb->dev->dev,
428                         "urb resubm failed: %d", err);
429                 s->async->events |= COMEDI_CB_EOA;
430                 s->async->events |= COMEDI_CB_ERROR;
431                 comedi_event(udfs->comedidev, s);
432                 usbduxfast_ai_stop(udfs, 0);
433         }
434 }
435
436 static int usbduxfastsub_start(struct usbduxfastsub_s *udfs)
437 {
438         int ret;
439         unsigned char local_transfer_buffer[16];
440
441         /* 7f92 to zero */
442         local_transfer_buffer[0] = 0;
443         /* bRequest, "Firmware" */
444         ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0),
445                               USBDUXFASTSUB_FIRMWARE,
446                               VENDOR_DIR_OUT,     /* bmRequestType */
447                               USBDUXFASTSUB_CPUCS,    /* Value */
448                               0x0000,   /* Index */
449                               /* address of the transfer buffer */
450                               local_transfer_buffer,
451                               1,      /* Length */
452                               EZTIMEOUT);    /* Timeout */
453         if (ret < 0) {
454                 dev_err(&udfs->interface->dev,
455                         "control msg failed (start)\n");
456                 return ret;
457         }
458
459         return 0;
460 }
461
462 static int usbduxfastsub_stop(struct usbduxfastsub_s *udfs)
463 {
464         int ret;
465         unsigned char local_transfer_buffer[16];
466
467         /* 7f92 to one */
468         local_transfer_buffer[0] = 1;
469         /* bRequest, "Firmware" */
470         ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0),
471                               USBDUXFASTSUB_FIRMWARE,
472                               VENDOR_DIR_OUT,   /* bmRequestType */
473                               USBDUXFASTSUB_CPUCS,      /* Value */
474                               0x0000,   /* Index */
475                               local_transfer_buffer, 1, /* Length */
476                               EZTIMEOUT);       /* Timeout */
477         if (ret < 0) {
478                 dev_err(&udfs->interface->dev,
479                         "control msg failed (stop)\n");
480                 return ret;
481         }
482
483         return 0;
484 }
485
486 static int usbduxfastsub_upload(struct usbduxfastsub_s *udfs,
487                                 unsigned char *local_transfer_buffer,
488                                 unsigned int startAddr, unsigned int len)
489 {
490         int ret;
491
492 #ifdef CONFIG_COMEDI_DEBUG
493         printk(KERN_DEBUG "comedi: usbduxfast: uploading %d bytes", len);
494         printk(KERN_DEBUG " to addr %d, first byte=%d.\n",
495                startAddr, local_transfer_buffer[0]);
496 #endif
497         /* brequest, firmware */
498         ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0),
499                               USBDUXFASTSUB_FIRMWARE,
500                               VENDOR_DIR_OUT,   /* bmRequestType */
501                               startAddr,        /* value */
502                               0x0000,    /* index */
503                               /* our local safe buffer */
504                               local_transfer_buffer,
505                               len,      /* length */
506                               EZTIMEOUT);      /* timeout */
507
508 #ifdef CONFIG_COMEDI_DEBUG
509         printk(KERN_DEBUG "comedi_: usbduxfast: result=%d\n", ret);
510 #endif
511
512         if (ret < 0) {
513                 dev_err(&udfs->interface->dev, "uppload failed\n");
514                 return ret;
515         }
516
517         return 0;
518 }
519
520 static int usbduxfastsub_submit_InURBs(struct usbduxfastsub_s *udfs)
521 {
522         int ret;
523
524         if (!udfs)
525                 return -EFAULT;
526
527         usb_fill_bulk_urb(udfs->urbIn, udfs->usbdev,
528                           usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
529                           udfs->transfer_buffer,
530                           SIZEINBUF, usbduxfastsub_ai_Irq, udfs->comedidev);
531
532 #ifdef CONFIG_COMEDI_DEBUG
533         printk(KERN_DEBUG "comedi%d: usbduxfast: submitting in-urb: "
534                "0x%p,0x%p\n", udfs->comedidev->minor, udfs->urbIn->context,
535                udfs->urbIn->dev);
536 #endif
537         ret = usb_submit_urb(udfs->urbIn, GFP_ATOMIC);
538         if (ret) {
539                 dev_err(&udfs->interface->dev,
540                         "ai: usb_submit_urb error %d\n", ret);
541                 return ret;
542         }
543         return 0;
544 }
545
546 static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
547                                  struct comedi_subdevice *s,
548                                  struct comedi_cmd *cmd)
549 {
550         struct usbduxfastsub_s *udfs = dev->private;
551         int err = 0;
552         long int steps, tmp;
553         int minSamplPer;
554
555         if (!udfs->probed)
556                 return -ENODEV;
557
558         /* Step 1 : check if triggers are trivially valid */
559
560         err |= cfc_check_trigger_src(&cmd->start_src,
561                                         TRIG_NOW | TRIG_EXT | TRIG_INT);
562         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
563                                         TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT);
564         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
565         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
566         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
567
568         if (err)
569                 return 1;
570
571         /* Step 2a : make sure trigger sources are unique */
572
573         err |= cfc_check_trigger_is_unique(cmd->start_src);
574         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
575         err |= cfc_check_trigger_is_unique(cmd->convert_src);
576         err |= cfc_check_trigger_is_unique(cmd->stop_src);
577
578         /* Step 2b : and mutually compatible */
579
580         /* can't have external stop and start triggers at once */
581         if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
582                 err |= -EINVAL;
583
584         if (err)
585                 return 2;
586
587         /* Step 3: check if arguments are trivially valid */
588
589         if (cmd->start_src == TRIG_NOW)
590                 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
591
592         if (!cmd->chanlist_len)
593                 err |= -EINVAL;
594
595         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
596
597         if (cmd->chanlist_len == 1)
598                 minSamplPer = 1;
599         else
600                 minSamplPer = MIN_SAMPLING_PERIOD;
601
602         if (cmd->convert_src == TRIG_TIMER) {
603                 steps = cmd->convert_arg * 30;
604                 if (steps < (minSamplPer * 1000))
605                         steps = minSamplPer * 1000;
606
607                 if (steps > (MAX_SAMPLING_PERIOD * 1000))
608                         steps = MAX_SAMPLING_PERIOD * 1000;
609
610                 /* calc arg again */
611                 tmp = steps / 30;
612                 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, tmp);
613         }
614
615         if (cmd->scan_begin_src == TRIG_TIMER)
616                 err |= -EINVAL;
617
618         /* stop source */
619         switch (cmd->stop_src) {
620         case TRIG_COUNT:
621                 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
622                 break;
623         case TRIG_NONE:
624                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
625                 break;
626                 /*
627                  * TRIG_EXT doesn't care since it doesn't trigger
628                  * off a numbered channel
629                  */
630         default:
631                 break;
632         }
633
634         if (err)
635                 return 3;
636
637         /* step 4: fix up any arguments */
638
639         return 0;
640
641 }
642
643 static int usbduxfast_ai_inttrig(struct comedi_device *dev,
644                                  struct comedi_subdevice *s,
645                                  unsigned int trignum)
646 {
647         int ret;
648         struct usbduxfastsub_s *udfs = dev->private;
649
650         if (!udfs)
651                 return -EFAULT;
652
653         down(&udfs->sem);
654         if (!udfs->probed) {
655                 up(&udfs->sem);
656                 return -ENODEV;
657         }
658 #ifdef CONFIG_COMEDI_DEBUG
659         printk(KERN_DEBUG "comedi%d: usbduxfast_ai_inttrig\n", dev->minor);
660 #endif
661
662         if (trignum != 0) {
663                 dev_err(dev->class_dev, "%s: invalid trignum\n", __func__);
664                 up(&udfs->sem);
665                 return -EINVAL;
666         }
667         if (!udfs->ai_cmd_running) {
668                 udfs->ai_cmd_running = 1;
669                 ret = usbduxfastsub_submit_InURBs(udfs);
670                 if (ret < 0) {
671                         dev_err(dev->class_dev,
672                                 "%s: urbSubmit: err=%d\n", __func__, ret);
673                         udfs->ai_cmd_running = 0;
674                         up(&udfs->sem);
675                         return ret;
676                 }
677                 s->async->inttrig = NULL;
678         } else {
679                 dev_err(dev->class_dev,
680                         "ai_inttrig but acqu is already running\n");
681         }
682         up(&udfs->sem);
683         return 1;
684 }
685
686 /*
687  * offsets for the GPIF bytes
688  * the first byte is the command byte
689  */
690 #define LENBASE (1+0x00)
691 #define OPBASE  (1+0x08)
692 #define OUTBASE (1+0x10)
693 #define LOGBASE (1+0x18)
694
695 static int usbduxfast_ai_cmd(struct comedi_device *dev,
696                              struct comedi_subdevice *s)
697 {
698         struct comedi_cmd *cmd = &s->async->cmd;
699         unsigned int chan, gain, rngmask = 0xff;
700         int i, j, ret;
701         struct usbduxfastsub_s *udfs;
702         int result;
703         long steps, steps_tmp;
704
705 #ifdef CONFIG_COMEDI_DEBUG
706         printk(KERN_DEBUG "comedi%d: usbduxfast_ai_cmd\n", dev->minor);
707 #endif
708         udfs = dev->private;
709         if (!udfs)
710                 return -EFAULT;
711
712         down(&udfs->sem);
713         if (!udfs->probed) {
714                 up(&udfs->sem);
715                 return -ENODEV;
716         }
717         if (udfs->ai_cmd_running) {
718                 dev_err(dev->class_dev,
719                         "ai_cmd not possible. Another ai_cmd is running.\n");
720                 up(&udfs->sem);
721                 return -EBUSY;
722         }
723         /* set current channel of the running acquisition to zero */
724         s->async->cur_chan = 0;
725
726         /*
727          * ignore the first buffers from the device if there
728          * is an error condition
729          */
730         udfs->ignore = PACKETS_TO_IGNORE;
731
732         if (cmd->chanlist_len > 0) {
733                 gain = CR_RANGE(cmd->chanlist[0]);
734                 for (i = 0; i < cmd->chanlist_len; ++i) {
735                         chan = CR_CHAN(cmd->chanlist[i]);
736                         if (chan != i) {
737                                 dev_err(dev->class_dev,
738                                         "cmd is accepting only consecutive channels.\n");
739                                 up(&udfs->sem);
740                                 return -EINVAL;
741                         }
742                         if ((gain != CR_RANGE(cmd->chanlist[i]))
743                             && (cmd->chanlist_len > 3)) {
744                                 dev_err(dev->class_dev,
745                                         "the gain must be the same for all channels.\n");
746                                 up(&udfs->sem);
747                                 return -EINVAL;
748                         }
749                         if (i >= NUMCHANNELS) {
750                                 dev_err(dev->class_dev,
751                                         "channel list too long\n");
752                                 break;
753                         }
754                 }
755         }
756         steps = 0;
757         if (cmd->scan_begin_src == TRIG_TIMER) {
758                 dev_err(dev->class_dev,
759                         "scan_begin_src==TRIG_TIMER not valid.\n");
760                 up(&udfs->sem);
761                 return -EINVAL;
762         }
763         if (cmd->convert_src == TRIG_TIMER)
764                 steps = (cmd->convert_arg * 30) / 1000;
765
766         if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
767                 dev_err(dev->class_dev,
768                         "ai_cmd: steps=%ld, scan_begin_arg=%d. Not properly tested by cmdtest?\n",
769                         steps, cmd->scan_begin_arg);
770                 up(&udfs->sem);
771                 return -EINVAL;
772         }
773         if (steps > MAX_SAMPLING_PERIOD) {
774                 dev_err(dev->class_dev, "ai_cmd: sampling rate too low.\n");
775                 up(&udfs->sem);
776                 return -EINVAL;
777         }
778         if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
779             && (cmd->chanlist_len != 16)) {
780                 dev_err(dev->class_dev,
781                         "ai_cmd: TRIG_EXT only with 1 or 16 channels possible.\n");
782                 up(&udfs->sem);
783                 return -EINVAL;
784         }
785 #ifdef CONFIG_COMEDI_DEBUG
786         printk(KERN_DEBUG "comedi%d: usbduxfast: steps=%ld, convert_arg=%u\n",
787                dev->minor, steps, cmd->convert_arg);
788 #endif
789
790         switch (cmd->chanlist_len) {
791         case 1:
792                 /*
793                  * one channel
794                  */
795
796                 if (CR_RANGE(cmd->chanlist[0]) > 0)
797                         rngmask = 0xff - 0x04;
798                 else
799                         rngmask = 0xff;
800
801                 /*
802                  * for external trigger: looping in this state until
803                  * the RDY0 pin becomes zero
804                  */
805
806                 /* we loop here until ready has been set */
807                 if (cmd->start_src == TRIG_EXT) {
808                         /* branch back to state 0 */
809                         udfs->dux_commands[LENBASE + 0] = 0x01;
810                         /* deceision state w/o data */
811                         udfs->dux_commands[OPBASE + 0] = 0x01;
812                         udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
813                         /* RDY0 = 0 */
814                         udfs->dux_commands[LOGBASE + 0] = 0x00;
815                 } else {        /* we just proceed to state 1 */
816                         udfs->dux_commands[LENBASE + 0] = 1;
817                         udfs->dux_commands[OPBASE + 0] = 0;
818                         udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
819                         udfs->dux_commands[LOGBASE + 0] = 0;
820                 }
821
822                 if (steps < MIN_SAMPLING_PERIOD) {
823                         /* for fast single channel aqu without mux */
824                         if (steps <= 1) {
825                                 /*
826                                  * we just stay here at state 1 and rexecute
827                                  * the same state this gives us 30MHz sampling
828                                  * rate
829                                  */
830
831                                 /* branch back to state 1 */
832                                 udfs->dux_commands[LENBASE + 1] = 0x89;
833                                 /* deceision state with data */
834                                 udfs->dux_commands[OPBASE + 1] = 0x03;
835                                 udfs->dux_commands[OUTBASE + 1] =
836                                     0xFF & rngmask;
837                                 /* doesn't matter */
838                                 udfs->dux_commands[LOGBASE + 1] = 0xFF;
839                         } else {
840                                 /*
841                                  * we loop through two states: data and delay
842                                  * max rate is 15MHz
843                                  */
844                                 udfs->dux_commands[LENBASE + 1] = steps - 1;
845                                 /* data */
846                                 udfs->dux_commands[OPBASE + 1] = 0x02;
847                                 udfs->dux_commands[OUTBASE + 1] =
848                                     0xFF & rngmask;
849                                 /* doesn't matter */
850                                 udfs->dux_commands[LOGBASE + 1] = 0;
851                                 /* branch back to state 1 */
852                                 udfs->dux_commands[LENBASE + 2] = 0x09;
853                                 /* deceision state w/o data */
854                                 udfs->dux_commands[OPBASE + 2] = 0x01;
855                                 udfs->dux_commands[OUTBASE + 2] =
856                                     0xFF & rngmask;
857                                 /* doesn't matter */
858                                 udfs->dux_commands[LOGBASE + 2] = 0xFF;
859                         }
860                 } else {
861                         /*
862                          * we loop through 3 states: 2x delay and 1x data
863                          * this gives a min sampling rate of 60kHz
864                          */
865
866                         /* we have 1 state with duration 1 */
867                         steps = steps - 1;
868
869                         /* do the first part of the delay */
870                         udfs->dux_commands[LENBASE + 1] = steps / 2;
871                         udfs->dux_commands[OPBASE + 1] = 0;
872                         udfs->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
873                         udfs->dux_commands[LOGBASE + 1] = 0;
874
875                         /* and the second part */
876                         udfs->dux_commands[LENBASE + 2] = steps - steps / 2;
877                         udfs->dux_commands[OPBASE + 2] = 0;
878                         udfs->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
879                         udfs->dux_commands[LOGBASE + 2] = 0;
880
881                         /* get the data and branch back */
882
883                         /* branch back to state 1 */
884                         udfs->dux_commands[LENBASE + 3] = 0x09;
885                         /* deceision state w data */
886                         udfs->dux_commands[OPBASE + 3] = 0x03;
887                         udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
888                         /* doesn't matter */
889                         udfs->dux_commands[LOGBASE + 3] = 0xFF;
890                 }
891                 break;
892
893         case 2:
894                 /*
895                  * two channels
896                  * commit data to the FIFO
897                  */
898
899                 if (CR_RANGE(cmd->chanlist[0]) > 0)
900                         rngmask = 0xff - 0x04;
901                 else
902                         rngmask = 0xff;
903
904                 udfs->dux_commands[LENBASE + 0] = 1;
905                 /* data */
906                 udfs->dux_commands[OPBASE + 0] = 0x02;
907                 udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
908                 udfs->dux_commands[LOGBASE + 0] = 0;
909
910                 /* we have 1 state with duration 1: state 0 */
911                 steps_tmp = steps - 1;
912
913                 if (CR_RANGE(cmd->chanlist[1]) > 0)
914                         rngmask = 0xff - 0x04;
915                 else
916                         rngmask = 0xff;
917
918                 /* do the first part of the delay */
919                 udfs->dux_commands[LENBASE + 1] = steps_tmp / 2;
920                 udfs->dux_commands[OPBASE + 1] = 0;
921                 /* count */
922                 udfs->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
923                 udfs->dux_commands[LOGBASE + 1] = 0;
924
925                 /* and the second part */
926                 udfs->dux_commands[LENBASE + 2] = steps_tmp - steps_tmp / 2;
927                 udfs->dux_commands[OPBASE + 2] = 0;
928                 udfs->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
929                 udfs->dux_commands[LOGBASE + 2] = 0;
930
931                 udfs->dux_commands[LENBASE + 3] = 1;
932                 /* data */
933                 udfs->dux_commands[OPBASE + 3] = 0x02;
934                 udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
935                 udfs->dux_commands[LOGBASE + 3] = 0;
936
937                 /*
938                  * we have 2 states with duration 1: step 6 and
939                  * the IDLE state
940                  */
941                 steps_tmp = steps - 2;
942
943                 if (CR_RANGE(cmd->chanlist[0]) > 0)
944                         rngmask = 0xff - 0x04;
945                 else
946                         rngmask = 0xff;
947
948                 /* do the first part of the delay */
949                 udfs->dux_commands[LENBASE + 4] = steps_tmp / 2;
950                 udfs->dux_commands[OPBASE + 4] = 0;
951                 /* reset */
952                 udfs->dux_commands[OUTBASE + 4] = (0xFF - 0x02) & rngmask;
953                 udfs->dux_commands[LOGBASE + 4] = 0;
954
955                 /* and the second part */
956                 udfs->dux_commands[LENBASE + 5] = steps_tmp - steps_tmp / 2;
957                 udfs->dux_commands[OPBASE + 5] = 0;
958                 udfs->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
959                 udfs->dux_commands[LOGBASE + 5] = 0;
960
961                 udfs->dux_commands[LENBASE + 6] = 1;
962                 udfs->dux_commands[OPBASE + 6] = 0;
963                 udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
964                 udfs->dux_commands[LOGBASE + 6] = 0;
965                 break;
966
967         case 3:
968                 /*
969                  * three channels
970                  */
971                 for (j = 0; j < 1; j++) {
972                         if (CR_RANGE(cmd->chanlist[j]) > 0)
973                                 rngmask = 0xff - 0x04;
974                         else
975                                 rngmask = 0xff;
976                         /*
977                          * commit data to the FIFO and do the first part
978                          * of the delay
979                          */
980                         udfs->dux_commands[LENBASE + j * 2] = steps / 2;
981                         /* data */
982                         udfs->dux_commands[OPBASE + j * 2] = 0x02;
983                         /* no change */
984                         udfs->dux_commands[OUTBASE + j * 2] = 0xFF & rngmask;
985                         udfs->dux_commands[LOGBASE + j * 2] = 0;
986
987                         if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
988                                 rngmask = 0xff - 0x04;
989                         else
990                                 rngmask = 0xff;
991
992                         /* do the second part of the delay */
993                         udfs->dux_commands[LENBASE + j * 2 + 1] =
994                             steps - steps / 2;
995                         /* no data */
996                         udfs->dux_commands[OPBASE + j * 2 + 1] = 0;
997                         /* count */
998                         udfs->dux_commands[OUTBASE + j * 2 + 1] =
999                             0xFE & rngmask;
1000                         udfs->dux_commands[LOGBASE + j * 2 + 1] = 0;
1001                 }
1002
1003                 /* 2 steps with duration 1: the idele step and step 6: */
1004                 steps_tmp = steps - 2;
1005
1006                 /* commit data to the FIFO and do the first part of the delay */
1007                 udfs->dux_commands[LENBASE + 4] = steps_tmp / 2;
1008                 /* data */
1009                 udfs->dux_commands[OPBASE + 4] = 0x02;
1010                 udfs->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1011                 udfs->dux_commands[LOGBASE + 4] = 0;
1012
1013                 if (CR_RANGE(cmd->chanlist[0]) > 0)
1014                         rngmask = 0xff - 0x04;
1015                 else
1016                         rngmask = 0xff;
1017
1018                 /* do the second part of the delay */
1019                 udfs->dux_commands[LENBASE + 5] = steps_tmp - steps_tmp / 2;
1020                 /* no data */
1021                 udfs->dux_commands[OPBASE + 5] = 0;
1022                 /* reset */
1023                 udfs->dux_commands[OUTBASE + 5] = (0xFF - 0x02) & rngmask;
1024                 udfs->dux_commands[LOGBASE + 5] = 0;
1025
1026                 udfs->dux_commands[LENBASE + 6] = 1;
1027                 udfs->dux_commands[OPBASE + 6] = 0;
1028                 udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1029                 udfs->dux_commands[LOGBASE + 6] = 0;
1030
1031         case 16:
1032                 if (CR_RANGE(cmd->chanlist[0]) > 0)
1033                         rngmask = 0xff - 0x04;
1034                 else
1035                         rngmask = 0xff;
1036
1037                 if (cmd->start_src == TRIG_EXT) {
1038                         /*
1039                          * we loop here until ready has been set
1040                          */
1041
1042                         /* branch back to state 0 */
1043                         udfs->dux_commands[LENBASE + 0] = 0x01;
1044                         /* deceision state w/o data */
1045                         udfs->dux_commands[OPBASE + 0] = 0x01;
1046                         /* reset */
1047                         udfs->dux_commands[OUTBASE + 0] =
1048                             (0xFF - 0x02) & rngmask;
1049                         /* RDY0 = 0 */
1050                         udfs->dux_commands[LOGBASE + 0] = 0x00;
1051                 } else {
1052                         /*
1053                          * we just proceed to state 1
1054                          */
1055
1056                         /* 30us reset pulse */
1057                         udfs->dux_commands[LENBASE + 0] = 255;
1058                         udfs->dux_commands[OPBASE + 0] = 0;
1059                         /* reset */
1060                         udfs->dux_commands[OUTBASE + 0] =
1061                             (0xFF - 0x02) & rngmask;
1062                         udfs->dux_commands[LOGBASE + 0] = 0;
1063                 }
1064
1065                 /* commit data to the FIFO */
1066                 udfs->dux_commands[LENBASE + 1] = 1;
1067                 /* data */
1068                 udfs->dux_commands[OPBASE + 1] = 0x02;
1069                 udfs->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
1070                 udfs->dux_commands[LOGBASE + 1] = 0;
1071
1072                 /* we have 2 states with duration 1 */
1073                 steps = steps - 2;
1074
1075                 /* do the first part of the delay */
1076                 udfs->dux_commands[LENBASE + 2] = steps / 2;
1077                 udfs->dux_commands[OPBASE + 2] = 0;
1078                 udfs->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1079                 udfs->dux_commands[LOGBASE + 2] = 0;
1080
1081                 /* and the second part */
1082                 udfs->dux_commands[LENBASE + 3] = steps - steps / 2;
1083                 udfs->dux_commands[OPBASE + 3] = 0;
1084                 udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
1085                 udfs->dux_commands[LOGBASE + 3] = 0;
1086
1087                 /* branch back to state 1 */
1088                 udfs->dux_commands[LENBASE + 4] = 0x09;
1089                 /* deceision state w/o data */
1090                 udfs->dux_commands[OPBASE + 4] = 0x01;
1091                 udfs->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1092                 /* doesn't matter */
1093                 udfs->dux_commands[LOGBASE + 4] = 0xFF;
1094
1095                 break;
1096
1097         default:
1098                 dev_err(dev->class_dev, "unsupported combination of channels\n");
1099                 up(&udfs->sem);
1100                 return -EFAULT;
1101         }
1102
1103 #ifdef CONFIG_COMEDI_DEBUG
1104         printk(KERN_DEBUG "comedi %d: sending commands to the usb device\n",
1105                dev->minor);
1106 #endif
1107         /* 0 means that the AD commands are sent */
1108         result = send_dux_commands(udfs, SENDADCOMMANDS);
1109         if (result < 0) {
1110                 dev_err(dev->class_dev,
1111                         "adc command could not be submitted. Aborting...\n");
1112                 up(&udfs->sem);
1113                 return result;
1114         }
1115         if (cmd->stop_src == TRIG_COUNT) {
1116                 udfs->ai_sample_count = cmd->stop_arg * cmd->scan_end_arg;
1117                 if (udfs->ai_sample_count < 1) {
1118                         dev_err(dev->class_dev,
1119                                 "(cmd->stop_arg)*(cmd->scan_end_arg)<1, aborting.\n");
1120                         up(&udfs->sem);
1121                         return -EFAULT;
1122                 }
1123                 udfs->ai_continous = 0;
1124         } else {
1125                 /* continous acquisition */
1126                 udfs->ai_continous = 1;
1127                 udfs->ai_sample_count = 0;
1128         }
1129
1130         if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
1131                 /* enable this acquisition operation */
1132                 udfs->ai_cmd_running = 1;
1133                 ret = usbduxfastsub_submit_InURBs(udfs);
1134                 if (ret < 0) {
1135                         udfs->ai_cmd_running = 0;
1136                         /* fixme: unlink here?? */
1137                         up(&udfs->sem);
1138                         return ret;
1139                 }
1140                 s->async->inttrig = NULL;
1141         } else {
1142                 /*
1143                  * TRIG_INT
1144                  * don't enable the acquision operation
1145                  * wait for an internal signal
1146                  */
1147                 s->async->inttrig = usbduxfast_ai_inttrig;
1148         }
1149         up(&udfs->sem);
1150
1151         return 0;
1152 }
1153
1154 /*
1155  * Mode 0 is used to get a single conversion on demand.
1156  */
1157 static int usbduxfast_ai_insn_read(struct comedi_device *dev,
1158                                    struct comedi_subdevice *s,
1159                                    struct comedi_insn *insn, unsigned int *data)
1160 {
1161         int i, j, n, actual_length;
1162         int chan, range, rngmask;
1163         int err;
1164         struct usbduxfastsub_s *udfs;
1165
1166         udfs = dev->private;
1167         if (!udfs) {
1168                 dev_err(dev->class_dev, "%s: no usb dev.\n", __func__);
1169                 return -ENODEV;
1170         }
1171 #ifdef CONFIG_COMEDI_DEBUG
1172         printk(KERN_DEBUG "comedi%d: ai_insn_read, insn->n=%d, "
1173                "insn->subdev=%d\n", dev->minor, insn->n, insn->subdev);
1174 #endif
1175         down(&udfs->sem);
1176         if (!udfs->probed) {
1177                 up(&udfs->sem);
1178                 return -ENODEV;
1179         }
1180         if (udfs->ai_cmd_running) {
1181                 dev_err(dev->class_dev,
1182                         "ai_insn_read not possible. Async Command is running.\n");
1183                 up(&udfs->sem);
1184                 return -EBUSY;
1185         }
1186         /* sample one channel */
1187         chan = CR_CHAN(insn->chanspec);
1188         range = CR_RANGE(insn->chanspec);
1189         /* set command for the first channel */
1190
1191         if (range > 0)
1192                 rngmask = 0xff - 0x04;
1193         else
1194                 rngmask = 0xff;
1195
1196         /* commit data to the FIFO */
1197         udfs->dux_commands[LENBASE + 0] = 1;
1198         /* data */
1199         udfs->dux_commands[OPBASE + 0] = 0x02;
1200         udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
1201         udfs->dux_commands[LOGBASE + 0] = 0;
1202
1203         /* do the first part of the delay */
1204         udfs->dux_commands[LENBASE + 1] = 12;
1205         udfs->dux_commands[OPBASE + 1] = 0;
1206         udfs->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
1207         udfs->dux_commands[LOGBASE + 1] = 0;
1208
1209         udfs->dux_commands[LENBASE + 2] = 1;
1210         udfs->dux_commands[OPBASE + 2] = 0;
1211         udfs->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1212         udfs->dux_commands[LOGBASE + 2] = 0;
1213
1214         udfs->dux_commands[LENBASE + 3] = 1;
1215         udfs->dux_commands[OPBASE + 3] = 0;
1216         udfs->dux_commands[OUTBASE + 3] = 0xFE & rngmask;
1217         udfs->dux_commands[LOGBASE + 3] = 0;
1218
1219         udfs->dux_commands[LENBASE + 4] = 1;
1220         udfs->dux_commands[OPBASE + 4] = 0;
1221         udfs->dux_commands[OUTBASE + 4] = 0xFE & rngmask;
1222         udfs->dux_commands[LOGBASE + 4] = 0;
1223
1224         /* second part */
1225         udfs->dux_commands[LENBASE + 5] = 12;
1226         udfs->dux_commands[OPBASE + 5] = 0;
1227         udfs->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1228         udfs->dux_commands[LOGBASE + 5] = 0;
1229
1230         udfs->dux_commands[LENBASE + 6] = 1;
1231         udfs->dux_commands[OPBASE + 6] = 0;
1232         udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1233         udfs->dux_commands[LOGBASE + 0] = 0;
1234
1235 #ifdef CONFIG_COMEDI_DEBUG
1236         printk(KERN_DEBUG "comedi %d: sending commands to the usb device\n",
1237                dev->minor);
1238 #endif
1239         /* 0 means that the AD commands are sent */
1240         err = send_dux_commands(udfs, SENDADCOMMANDS);
1241         if (err < 0) {
1242                 dev_err(dev->class_dev,
1243                         "adc command could not be submitted. Aborting...\n");
1244                 up(&udfs->sem);
1245                 return err;
1246         }
1247 #ifdef CONFIG_COMEDI_DEBUG
1248         printk(KERN_DEBUG "comedi%d: usbduxfast: submitting in-urb: "
1249                "0x%p,0x%p\n", udfs->comedidev->minor, udfs->urbIn->context,
1250                udfs->urbIn->dev);
1251 #endif
1252         for (i = 0; i < PACKETS_TO_IGNORE; i++) {
1253                 err = usb_bulk_msg(udfs->usbdev,
1254                                    usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
1255                                    udfs->transfer_buffer, SIZEINBUF,
1256                                    &actual_length, 10000);
1257                 if (err < 0) {
1258                         dev_err(dev->class_dev, "insn timeout. No data.\n");
1259                         up(&udfs->sem);
1260                         return err;
1261                 }
1262         }
1263         /* data points */
1264         for (i = 0; i < insn->n;) {
1265                 err = usb_bulk_msg(udfs->usbdev,
1266                                    usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
1267                                    udfs->transfer_buffer, SIZEINBUF,
1268                                    &actual_length, 10000);
1269                 if (err < 0) {
1270                         dev_err(dev->class_dev, "insn data error: %d\n", err);
1271                         up(&udfs->sem);
1272                         return err;
1273                 }
1274                 n = actual_length / sizeof(uint16_t);
1275                 if ((n % 16) != 0) {
1276                         dev_err(dev->class_dev, "insn data packet corrupted.\n");
1277                         up(&udfs->sem);
1278                         return -EINVAL;
1279                 }
1280                 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
1281                         data[i] = ((uint16_t *) (udfs->transfer_buffer))[j];
1282                         i++;
1283                 }
1284         }
1285         up(&udfs->sem);
1286         return i;
1287 }
1288
1289 #define FIRMWARE_MAX_LEN 0x2000
1290
1291 static int firmwareUpload(struct usbduxfastsub_s *usbduxfastsub,
1292                           const u8 *firmwareBinary, int sizeFirmware)
1293 {
1294         int ret;
1295         uint8_t *fwBuf;
1296
1297         if (!firmwareBinary)
1298                 return 0;
1299
1300         if (sizeFirmware > FIRMWARE_MAX_LEN) {
1301                 dev_err(&usbduxfastsub->interface->dev,
1302                         "comedi_: usbduxfast firmware binary it too large for FX2.\n");
1303                 return -ENOMEM;
1304         }
1305
1306         /* we generate a local buffer for the firmware */
1307         fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
1308         if (!fwBuf) {
1309                 dev_err(&usbduxfastsub->interface->dev,
1310                         "comedi_: mem alloc for firmware failed\n");
1311                 return -ENOMEM;
1312         }
1313
1314         ret = usbduxfastsub_stop(usbduxfastsub);
1315         if (ret < 0) {
1316                 dev_err(&usbduxfastsub->interface->dev,
1317                         "comedi_: can not stop firmware\n");
1318                 kfree(fwBuf);
1319                 return ret;
1320         }
1321
1322         ret = usbduxfastsub_upload(usbduxfastsub, fwBuf, 0, sizeFirmware);
1323         if (ret < 0) {
1324                 dev_err(&usbduxfastsub->interface->dev,
1325                         "comedi_: firmware upload failed\n");
1326                 kfree(fwBuf);
1327                 return ret;
1328         }
1329         ret = usbduxfastsub_start(usbduxfastsub);
1330         if (ret < 0) {
1331                 dev_err(&usbduxfastsub->interface->dev,
1332                         "comedi_: can not start firmware\n");
1333                 kfree(fwBuf);
1334                 return ret;
1335         }
1336         kfree(fwBuf);
1337         return 0;
1338 }
1339
1340 static void tidy_up(struct usbduxfastsub_s *udfs)
1341 {
1342 #ifdef CONFIG_COMEDI_DEBUG
1343         printk(KERN_DEBUG "comedi_: usbduxfast: tiding up\n");
1344 #endif
1345
1346         if (!udfs)
1347                 return;
1348
1349         /* shows the usb subsystem that the driver is down */
1350         if (udfs->interface)
1351                 usb_set_intfdata(udfs->interface, NULL);
1352
1353         udfs->probed = 0;
1354
1355         if (udfs->urbIn) {
1356                 /* waits until a running transfer is over */
1357                 usb_kill_urb(udfs->urbIn);
1358
1359                 kfree(udfs->transfer_buffer);
1360                 udfs->transfer_buffer = NULL;
1361
1362                 usb_free_urb(udfs->urbIn);
1363                 udfs->urbIn = NULL;
1364         }
1365
1366         kfree(udfs->insnBuffer);
1367         udfs->insnBuffer = NULL;
1368
1369         kfree(udfs->dux_commands);
1370         udfs->dux_commands = NULL;
1371
1372         udfs->ai_cmd_running = 0;
1373 }
1374
1375 static int usbduxfast_attach_common(struct comedi_device *dev,
1376                                     struct usbduxfastsub_s *udfs)
1377 {
1378         int ret;
1379         struct comedi_subdevice *s;
1380
1381         down(&udfs->sem);
1382         /* pointer back to the corresponding comedi device */
1383         udfs->comedidev = dev;
1384         dev->board_name = "usbduxfast";
1385         ret = comedi_alloc_subdevices(dev, 1);
1386         if (ret) {
1387                 up(&udfs->sem);
1388                 return ret;
1389         }
1390         /* private structure is also simply the usb-structure */
1391         dev->private = udfs;
1392         /* the first subdevice is the A/D converter */
1393         s = &dev->subdevices[SUBDEV_AD];
1394         /*
1395          * the URBs get the comedi subdevice which is responsible for reading
1396          * this is the subdevice which reads data
1397          */
1398         dev->read_subdev = s;
1399         /* the subdevice receives as private structure the usb-structure */
1400         s->private = NULL;
1401         /* analog input */
1402         s->type = COMEDI_SUBD_AI;
1403         /* readable and ref is to ground */
1404         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1405         /* 16 channels */
1406         s->n_chan = 16;
1407         /* length of the channellist */
1408         s->len_chanlist = 16;
1409         /* callback functions */
1410         s->insn_read = usbduxfast_ai_insn_read;
1411         s->do_cmdtest = usbduxfast_ai_cmdtest;
1412         s->do_cmd = usbduxfast_ai_cmd;
1413         s->cancel = usbduxfast_ai_cancel;
1414         /* max value from the A/D converter (12bit+1 bit for overflow) */
1415         s->maxdata = 0x1000;
1416         /* range table to convert to physical units */
1417         s->range_table = &range_usbduxfast_ai_range;
1418         /* finally decide that it's attached */
1419         udfs->attached = 1;
1420         up(&udfs->sem);
1421         dev_info(dev->class_dev, "successfully attached to usbduxfast.\n");
1422         return 0;
1423 }
1424
1425 static int usbduxfast_auto_attach(struct comedi_device *dev,
1426                                   unsigned long context_unused)
1427 {
1428         struct usb_interface *uinterf = comedi_to_usb_interface(dev);
1429         int ret;
1430         struct usbduxfastsub_s *udfs;
1431
1432         dev->private = NULL;
1433         down(&start_stop_sem);
1434         udfs = usb_get_intfdata(uinterf);
1435         if (!udfs || !udfs->probed) {
1436                 dev_err(dev->class_dev,
1437                         "usbduxfast: error: auto_attach failed, not connected\n");
1438                 ret = -ENODEV;
1439         } else if (udfs->attached) {
1440                 dev_err(dev->class_dev,
1441                        "usbduxfast: error: auto_attach failed, already attached\n");
1442                 ret = -ENODEV;
1443         } else
1444                 ret = usbduxfast_attach_common(dev, udfs);
1445         up(&start_stop_sem);
1446         return ret;
1447 }
1448
1449 static void usbduxfast_detach(struct comedi_device *dev)
1450 {
1451         struct usbduxfastsub_s *usb = dev->private;
1452
1453         if (usb) {
1454                 down(&usb->sem);
1455                 down(&start_stop_sem);
1456                 dev->private = NULL;
1457                 usb->attached = 0;
1458                 usb->comedidev = NULL;
1459                 up(&start_stop_sem);
1460                 up(&usb->sem);
1461         }
1462 }
1463
1464 static struct comedi_driver usbduxfast_driver = {
1465         .driver_name    = "usbduxfast",
1466         .module         = THIS_MODULE,
1467         .auto_attach    = usbduxfast_auto_attach,
1468         .detach         = usbduxfast_detach,
1469 };
1470
1471 static void usbduxfast_firmware_request_complete_handler(const struct firmware
1472                                                          *fw, void *context)
1473 {
1474         struct usbduxfastsub_s *usbduxfastsub_tmp = context;
1475         struct usb_interface *uinterf = usbduxfastsub_tmp->interface;
1476         int ret;
1477
1478         if (fw == NULL)
1479                 return;
1480
1481         /*
1482          * we need to upload the firmware here because fw will be
1483          * freed once we've left this function
1484          */
1485         ret = firmwareUpload(usbduxfastsub_tmp, fw->data, fw->size);
1486
1487         if (ret) {
1488                 dev_err(&uinterf->dev,
1489                         "Could not upload firmware (err=%d)\n", ret);
1490                 goto out;
1491         }
1492
1493         comedi_usb_auto_config(uinterf, &usbduxfast_driver, 0);
1494  out:
1495         release_firmware(fw);
1496 }
1497
1498 static int usbduxfast_usb_probe(struct usb_interface *uinterf,
1499                                 const struct usb_device_id *id)
1500 {
1501         struct usb_device *udev = interface_to_usbdev(uinterf);
1502         int i;
1503         int index;
1504         int ret;
1505
1506         if (udev->speed != USB_SPEED_HIGH) {
1507                 dev_err(&uinterf->dev,
1508                         "This driver needs USB 2.0 to operate. Aborting...\n");
1509                 return -ENODEV;
1510         }
1511 #ifdef CONFIG_COMEDI_DEBUG
1512         printk(KERN_DEBUG "comedi_: usbduxfast_: finding a free structure for "
1513                "the usb-device\n");
1514 #endif
1515         down(&start_stop_sem);
1516         /* look for a free place in the usbduxfast array */
1517         index = -1;
1518         for (i = 0; i < NUMUSBDUXFAST; i++) {
1519                 if (!usbduxfastsub[i].probed) {
1520                         index = i;
1521                         break;
1522                 }
1523         }
1524
1525         /* no more space */
1526         if (index == -1) {
1527                 dev_err(&uinterf->dev,
1528                         "Too many usbduxfast-devices connected.\n");
1529                 up(&start_stop_sem);
1530                 return -EMFILE;
1531         }
1532 #ifdef CONFIG_COMEDI_DEBUG
1533         printk(KERN_DEBUG "comedi_: usbduxfast: usbduxfastsub[%d] is ready to "
1534                "connect to comedi.\n", index);
1535 #endif
1536
1537         sema_init(&(usbduxfastsub[index].sem), 1);
1538         /* save a pointer to the usb device */
1539         usbduxfastsub[index].usbdev = udev;
1540
1541         /* save the interface itself */
1542         usbduxfastsub[index].interface = uinterf;
1543         /* get the interface number from the interface */
1544         usbduxfastsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
1545         /*
1546          * hand the private data over to the usb subsystem
1547          * will be needed for disconnect
1548          */
1549         usb_set_intfdata(uinterf, &(usbduxfastsub[index]));
1550
1551 #ifdef CONFIG_COMEDI_DEBUG
1552         printk(KERN_DEBUG "comedi_: usbduxfast: ifnum=%d\n",
1553                usbduxfastsub[index].ifnum);
1554 #endif
1555         /* create space for the commands going to the usb device */
1556         usbduxfastsub[index].dux_commands = kmalloc(SIZEOFDUXBUFFER,
1557                                                     GFP_KERNEL);
1558         if (!usbduxfastsub[index].dux_commands) {
1559                 tidy_up(&(usbduxfastsub[index]));
1560                 up(&start_stop_sem);
1561                 return -ENOMEM;
1562         }
1563         /* create space of the instruction buffer */
1564         usbduxfastsub[index].insnBuffer = kmalloc(SIZEINSNBUF, GFP_KERNEL);
1565         if (!usbduxfastsub[index].insnBuffer) {
1566                 tidy_up(&(usbduxfastsub[index]));
1567                 up(&start_stop_sem);
1568                 return -ENOMEM;
1569         }
1570         /* setting to alternate setting 1: enabling bulk ep */
1571         i = usb_set_interface(usbduxfastsub[index].usbdev,
1572                               usbduxfastsub[index].ifnum, 1);
1573         if (i < 0) {
1574                 dev_err(&uinterf->dev,
1575                         "usbduxfast%d: could not switch to alternate setting 1.\n",
1576                         index);
1577                 tidy_up(&(usbduxfastsub[index]));
1578                 up(&start_stop_sem);
1579                 return -ENODEV;
1580         }
1581         usbduxfastsub[index].urbIn = usb_alloc_urb(0, GFP_KERNEL);
1582         if (!usbduxfastsub[index].urbIn) {
1583                 dev_err(&uinterf->dev,
1584                         "usbduxfast%d: Could not alloc. urb\n", index);
1585                 tidy_up(&(usbduxfastsub[index]));
1586                 up(&start_stop_sem);
1587                 return -ENOMEM;
1588         }
1589         usbduxfastsub[index].transfer_buffer = kmalloc(SIZEINBUF, GFP_KERNEL);
1590         if (!usbduxfastsub[index].transfer_buffer) {
1591                 tidy_up(&(usbduxfastsub[index]));
1592                 up(&start_stop_sem);
1593                 return -ENOMEM;
1594         }
1595         /* we've reached the bottom of the function */
1596         usbduxfastsub[index].probed = 1;
1597         up(&start_stop_sem);
1598
1599         ret = request_firmware_nowait(THIS_MODULE,
1600                                       FW_ACTION_HOTPLUG,
1601                                       FIRMWARE,
1602                                       &udev->dev,
1603                                       GFP_KERNEL,
1604                                       usbduxfastsub + index,
1605                                       usbduxfast_firmware_request_complete_handler);
1606
1607         if (ret) {
1608                 dev_err(&uinterf->dev, "could not load firmware (err=%d)\n", ret);
1609                 return ret;
1610         }
1611
1612         dev_info(&uinterf->dev,
1613                  "usbduxfast%d has been successfully initialized.\n", index);
1614         /* success */
1615         return 0;
1616 }
1617
1618 static void usbduxfast_usb_disconnect(struct usb_interface *intf)
1619 {
1620         struct usbduxfastsub_s *udfs = usb_get_intfdata(intf);
1621         struct usb_device *udev = interface_to_usbdev(intf);
1622
1623         if (!udfs) {
1624                 dev_err(&intf->dev, "disconnect called with null pointer.\n");
1625                 return;
1626         }
1627         if (udfs->usbdev != udev) {
1628                 dev_err(&intf->dev, "BUG! called with wrong ptr!!!\n");
1629                 return;
1630         }
1631
1632         comedi_usb_auto_unconfig(intf);
1633
1634         down(&start_stop_sem);
1635         down(&udfs->sem);
1636         tidy_up(udfs);
1637         up(&udfs->sem);
1638         up(&start_stop_sem);
1639
1640 #ifdef CONFIG_COMEDI_DEBUG
1641         printk(KERN_DEBUG "comedi_: usbduxfast: disconnected from the usb\n");
1642 #endif
1643 }
1644
1645 static const struct usb_device_id usbduxfast_usb_table[] = {
1646         /* { USB_DEVICE(0x4b4, 0x8613) }, testing */
1647         { USB_DEVICE(0x13d8, 0x0010) }, /* real ID */
1648         { USB_DEVICE(0x13d8, 0x0011) }, /* real ID */
1649         { }
1650 };
1651 MODULE_DEVICE_TABLE(usb, usbduxfast_usb_table);
1652
1653 static struct usb_driver usbduxfast_usb_driver = {
1654 #ifdef COMEDI_HAVE_USB_DRIVER_OWNER
1655         .owner          = THIS_MODULE,
1656 #endif
1657         .name           = "usbduxfast",
1658         .probe          = usbduxfast_usb_probe,
1659         .disconnect     = usbduxfast_usb_disconnect,
1660         .id_table       = usbduxfast_usb_table,
1661 };
1662 module_comedi_usb_driver(usbduxfast_driver, usbduxfast_usb_driver);
1663
1664 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1665 MODULE_DESCRIPTION("USB-DUXfast, BerndPorr@f2s.com");
1666 MODULE_LICENSE("GPL");
1667 MODULE_FIRMWARE(FIRMWARE);