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