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