]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/comedi/drivers/usbduxfast.c
Merge branch 'next' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[karo-tx-linux.git] / drivers / staging / comedi / drivers / usbduxfast.c
1 /*
2  *  Copyright (C) 2004 Bernd Porr, Bernd.Porr@f2s.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 /*
16  * I must give credit here to Chris Baugher who
17  * wrote the driver for AT-MIO-16d. I used some parts of this
18  * driver. I also must give credits to David Brownell
19  * who supported me with the USB development.
20  *
21  * Bernd Porr
22  *
23  *
24  * Revision history:
25  * 0.9: Dropping the first data packet which seems to be from the last transfer.
26  *      Buffer overflows in the FX2 are handed over to comedi.
27  * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
28  *       Added insn command basically for testing. Sample rate is
29  *       1MHz/16ch=62.5kHz
30  * 0.99: Ian Abbott pointed out a bug which has been corrected. Thanks!
31  * 0.99a: added external trigger.
32  * 1.00: added firmware kernel request to the driver which fixed
33  *       udev coldplug problem
34  */
35
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
37
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/input.h>
43 #include <linux/usb.h>
44 #include <linux/fcntl.h>
45 #include <linux/compiler.h>
46 #include "comedi_fc.h"
47 #include "../comedidev.h"
48
49 /*
50  * timeout for the USB-transfer
51  */
52 #define EZTIMEOUT       30
53
54 /*
55  * constants for "firmware" upload and download
56  */
57 #define FIRMWARE                "usbduxfast_firmware.bin"
58 #define FIRMWARE_MAX_LEN        0x2000
59 #define USBDUXFASTSUB_FIRMWARE  0xA0
60 #define VENDOR_DIR_IN           0xC0
61 #define VENDOR_DIR_OUT          0x40
62
63 /*
64  * internal addresses of the 8051 processor
65  */
66 #define USBDUXFASTSUB_CPUCS     0xE600
67
68 /*
69  * max lenghth of the transfer-buffer for software upload
70  */
71 #define TB_LEN  0x2000
72
73 /*
74  * input endpoint number
75  */
76 #define BULKINEP        6
77
78 /*
79  * endpoint for the A/D channellist: bulk OUT
80  */
81 #define CHANNELLISTEP   4
82
83 /*
84  * number of channels
85  */
86 #define NUMCHANNELS     32
87
88 /*
89  * size of the waveform descriptor
90  */
91 #define WAVESIZE        0x20
92
93 /*
94  * size of one A/D value
95  */
96 #define SIZEADIN        (sizeof(int16_t))
97
98 /*
99  * size of the input-buffer IN BYTES
100  */
101 #define SIZEINBUF       512
102
103 /*
104  * 16 bytes
105  */
106 #define SIZEINSNBUF     512
107
108 /*
109  * size of the buffer for the dux commands in bytes
110  */
111 #define SIZEOFDUXBUF    256
112
113 /*
114  * number of in-URBs which receive the data: min=5
115  */
116 #define NUMOFINBUFFERSHIGH      10
117
118 /*
119  * min delay steps for more than one channel
120  * basically when the mux gives up ;-)
121  *
122  * steps at 30MHz in the FX2
123  */
124 #define MIN_SAMPLING_PERIOD     9
125
126 /*
127  * max number of 1/30MHz delay steps
128  */
129 #define MAX_SAMPLING_PERIOD     500
130
131 /*
132  * number of received packets to ignore before we start handing data
133  * over to comedi, it's quad buffering and we have to ignore 4 packets
134  */
135 #define PACKETS_TO_IGNORE       4
136
137 /*
138  * comedi constants
139  */
140 static const struct comedi_lrange range_usbduxfast_ai_range = {
141         2, {BIP_RANGE(0.75), BIP_RANGE(0.5)}
142 };
143
144 /*
145  * private structure of one subdevice
146  *
147  * this is the structure which holds all the data of this driver
148  * one sub device just now: A/D
149  */
150 struct usbduxfast_private {
151         struct urb *urb;        /* BULK-transfer handling: urb */
152         uint8_t *duxbuf;
153         int8_t *inbuf;
154         short int ai_cmd_running;       /* asynchronous command is running */
155         short int ai_continous; /* continous acquisition */
156         long int ai_sample_count;       /* number of samples to acquire */
157         int ignore;             /* counter which ignores the first
158                                    buffers */
159         struct semaphore sem;
160 };
161
162 /*
163  * bulk transfers to usbduxfast
164  */
165 #define SENDADCOMMANDS            0
166 #define SENDINITEP6               1
167
168 static int usbduxfast_send_cmd(struct comedi_device *dev, int cmd_type)
169 {
170         struct usb_device *usb = comedi_to_usb_dev(dev);
171         struct usbduxfast_private *devpriv = dev->private;
172         int nsent;
173         int ret;
174
175         devpriv->duxbuf[0] = cmd_type;
176
177         ret = usb_bulk_msg(usb, usb_sndbulkpipe(usb, CHANNELLISTEP),
178                            devpriv->duxbuf, SIZEOFDUXBUF,
179                            &nsent, 10000);
180         if (ret < 0)
181                 dev_err(dev->class_dev,
182                         "could not transmit command to the usb-device, err=%d\n",
183                         ret);
184         return ret;
185 }
186
187 static void usbduxfast_cmd_data(struct comedi_device *dev, int index,
188                                 uint8_t len, uint8_t op, uint8_t out,
189                                 uint8_t log)
190 {
191         struct usbduxfast_private *devpriv = dev->private;
192
193         /* Set the GPIF bytes, the first byte is the command byte */
194         devpriv->duxbuf[1 + 0x00 + index] = len;
195         devpriv->duxbuf[1 + 0x08 + index] = op;
196         devpriv->duxbuf[1 + 0x10 + index] = out;
197         devpriv->duxbuf[1 + 0x18 + index] = log;
198 }
199
200 static int usbduxfast_ai_stop(struct comedi_device *dev, int do_unlink)
201 {
202         struct usbduxfast_private *devpriv = dev->private;
203
204         /* stop aquistion */
205         devpriv->ai_cmd_running = 0;
206
207         if (do_unlink && devpriv->urb) {
208                 /* kill the running transfer */
209                 usb_kill_urb(devpriv->urb);
210         }
211
212         return 0;
213 }
214
215 static int usbduxfast_ai_cancel(struct comedi_device *dev,
216                                 struct comedi_subdevice *s)
217 {
218         struct usbduxfast_private *devpriv = dev->private;
219         int ret;
220
221         if (!devpriv)
222                 return -EFAULT;
223
224         down(&devpriv->sem);
225         ret = usbduxfast_ai_stop(dev, 1);
226         up(&devpriv->sem);
227
228         return ret;
229 }
230
231 /*
232  * analogue IN
233  * interrupt service routine
234  */
235 static void usbduxfast_ai_interrupt(struct urb *urb)
236 {
237         struct comedi_device *dev = urb->context;
238         struct comedi_subdevice *s = dev->read_subdev;
239         struct comedi_async *async = s->async;
240         struct usb_device *usb = comedi_to_usb_dev(dev);
241         struct usbduxfast_private *devpriv = dev->private;
242         int n, err;
243
244         /* are we running a command? */
245         if (unlikely(!devpriv->ai_cmd_running)) {
246                 /*
247                  * not running a command
248                  * do not continue execution if no asynchronous command
249                  * is running in particular not resubmit
250                  */
251                 return;
252         }
253
254         /* first we test if something unusual has just happened */
255         switch (urb->status) {
256         case 0:
257                 break;
258
259                 /*
260                  * happens after an unlink command or when the device
261                  * is plugged out
262                  */
263         case -ECONNRESET:
264         case -ENOENT:
265         case -ESHUTDOWN:
266         case -ECONNABORTED:
267                 /* tell this comedi */
268                 async->events |= COMEDI_CB_EOA;
269                 async->events |= COMEDI_CB_ERROR;
270                 comedi_event(dev, s);
271                 /* stop the transfer w/o unlink */
272                 usbduxfast_ai_stop(dev, 0);
273                 return;
274
275         default:
276                 pr_err("non-zero urb status received in ai intr context: %d\n",
277                        urb->status);
278                 async->events |= COMEDI_CB_EOA;
279                 async->events |= COMEDI_CB_ERROR;
280                 comedi_event(dev, s);
281                 usbduxfast_ai_stop(dev, 0);
282                 return;
283         }
284
285         if (!devpriv->ignore) {
286                 if (!devpriv->ai_continous) {
287                         /* not continuous, fixed number of samples */
288                         n = urb->actual_length / sizeof(uint16_t);
289                         if (unlikely(devpriv->ai_sample_count < n)) {
290                                 unsigned int num_bytes;
291
292                                 /* partial sample received */
293                                 num_bytes = devpriv->ai_sample_count *
294                                             sizeof(uint16_t);
295                                 cfc_write_array_to_buffer(s,
296                                                           urb->transfer_buffer,
297                                                           num_bytes);
298                                 usbduxfast_ai_stop(dev, 0);
299                                 /* tell comedi that the acquistion is over */
300                                 async->events |= COMEDI_CB_EOA;
301                                 comedi_event(dev, s);
302                                 return;
303                         }
304                         devpriv->ai_sample_count -= n;
305                 }
306                 /* write the full buffer to comedi */
307                 err = cfc_write_array_to_buffer(s, urb->transfer_buffer,
308                                                 urb->actual_length);
309                 if (unlikely(err == 0)) {
310                         /* buffer overflow */
311                         usbduxfast_ai_stop(dev, 0);
312                         return;
313                 }
314
315                 /* tell comedi that data is there */
316                 comedi_event(dev, s);
317         } else {
318                 /* ignore this packet */
319                 devpriv->ignore--;
320         }
321
322         /*
323          * command is still running
324          * resubmit urb for BULK transfer
325          */
326         urb->dev = usb;
327         urb->status = 0;
328         err = usb_submit_urb(urb, GFP_ATOMIC);
329         if (err < 0) {
330                 dev_err(dev->class_dev,
331                         "urb resubm failed: %d", err);
332                 async->events |= COMEDI_CB_EOA;
333                 async->events |= COMEDI_CB_ERROR;
334                 comedi_event(dev, s);
335                 usbduxfast_ai_stop(dev, 0);
336         }
337 }
338
339 static int usbduxfast_submit_urb(struct comedi_device *dev)
340 {
341         struct usb_device *usb = comedi_to_usb_dev(dev);
342         struct usbduxfast_private *devpriv = dev->private;
343         int ret;
344
345         if (!devpriv)
346                 return -EFAULT;
347
348         usb_fill_bulk_urb(devpriv->urb, usb, usb_rcvbulkpipe(usb, BULKINEP),
349                           devpriv->inbuf, SIZEINBUF,
350                           usbduxfast_ai_interrupt, dev);
351
352         ret = usb_submit_urb(devpriv->urb, GFP_ATOMIC);
353         if (ret) {
354                 dev_err(dev->class_dev, "usb_submit_urb error %d\n", ret);
355                 return ret;
356         }
357         return 0;
358 }
359
360 static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
361                                  struct comedi_subdevice *s,
362                                  struct comedi_cmd *cmd)
363 {
364         int err = 0;
365         long int steps, tmp;
366         int min_sample_period;
367
368         /* Step 1 : check if triggers are trivially valid */
369
370         err |= cfc_check_trigger_src(&cmd->start_src,
371                                         TRIG_NOW | TRIG_EXT | TRIG_INT);
372         err |= cfc_check_trigger_src(&cmd->scan_begin_src,
373                                         TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT);
374         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_TIMER | TRIG_EXT);
375         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
376         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
377
378         if (err)
379                 return 1;
380
381         /* Step 2a : make sure trigger sources are unique */
382
383         err |= cfc_check_trigger_is_unique(cmd->start_src);
384         err |= cfc_check_trigger_is_unique(cmd->scan_begin_src);
385         err |= cfc_check_trigger_is_unique(cmd->convert_src);
386         err |= cfc_check_trigger_is_unique(cmd->stop_src);
387
388         /* Step 2b : and mutually compatible */
389
390         /* can't have external stop and start triggers at once */
391         if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
392                 err |= -EINVAL;
393
394         if (err)
395                 return 2;
396
397         /* Step 3: check if arguments are trivially valid */
398
399         if (cmd->start_src == TRIG_NOW)
400                 err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
401
402         if (!cmd->chanlist_len)
403                 err |= -EINVAL;
404
405         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
406
407         if (cmd->chanlist_len == 1)
408                 min_sample_period = 1;
409         else
410                 min_sample_period = MIN_SAMPLING_PERIOD;
411
412         if (cmd->convert_src == TRIG_TIMER) {
413                 steps = cmd->convert_arg * 30;
414                 if (steps < (min_sample_period * 1000))
415                         steps = min_sample_period * 1000;
416
417                 if (steps > (MAX_SAMPLING_PERIOD * 1000))
418                         steps = MAX_SAMPLING_PERIOD * 1000;
419
420                 /* calc arg again */
421                 tmp = steps / 30;
422                 err |= cfc_check_trigger_arg_is(&cmd->convert_arg, tmp);
423         }
424
425         if (cmd->scan_begin_src == TRIG_TIMER)
426                 err |= -EINVAL;
427
428         /* stop source */
429         switch (cmd->stop_src) {
430         case TRIG_COUNT:
431                 err |= cfc_check_trigger_arg_min(&cmd->stop_arg, 1);
432                 break;
433         case TRIG_NONE:
434                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
435                 break;
436                 /*
437                  * TRIG_EXT doesn't care since it doesn't trigger
438                  * off a numbered channel
439                  */
440         default:
441                 break;
442         }
443
444         if (err)
445                 return 3;
446
447         /* step 4: fix up any arguments */
448
449         return 0;
450
451 }
452
453 static int usbduxfast_ai_inttrig(struct comedi_device *dev,
454                                  struct comedi_subdevice *s,
455                                  unsigned int trignum)
456 {
457         struct usbduxfast_private *devpriv = dev->private;
458         int ret;
459
460         if (!devpriv)
461                 return -EFAULT;
462
463         down(&devpriv->sem);
464
465         if (trignum != 0) {
466                 dev_err(dev->class_dev, "invalid trignum\n");
467                 up(&devpriv->sem);
468                 return -EINVAL;
469         }
470         if (!devpriv->ai_cmd_running) {
471                 devpriv->ai_cmd_running = 1;
472                 ret = usbduxfast_submit_urb(dev);
473                 if (ret < 0) {
474                         dev_err(dev->class_dev, "urbSubmit: err=%d\n", ret);
475                         devpriv->ai_cmd_running = 0;
476                         up(&devpriv->sem);
477                         return ret;
478                 }
479                 s->async->inttrig = NULL;
480         } else {
481                 dev_err(dev->class_dev, "ai is already running\n");
482         }
483         up(&devpriv->sem);
484         return 1;
485 }
486
487 static int usbduxfast_ai_cmd(struct comedi_device *dev,
488                              struct comedi_subdevice *s)
489 {
490         struct usbduxfast_private *devpriv = dev->private;
491         struct comedi_cmd *cmd = &s->async->cmd;
492         unsigned int chan, gain, rngmask = 0xff;
493         int i, j, ret;
494         int result;
495         long steps, steps_tmp;
496
497         if (!devpriv)
498                 return -EFAULT;
499
500         down(&devpriv->sem);
501         if (devpriv->ai_cmd_running) {
502                 dev_err(dev->class_dev, "ai_cmd not possible\n");
503                 up(&devpriv->sem);
504                 return -EBUSY;
505         }
506         /* set current channel of the running acquisition to zero */
507         s->async->cur_chan = 0;
508
509         /*
510          * ignore the first buffers from the device if there
511          * is an error condition
512          */
513         devpriv->ignore = PACKETS_TO_IGNORE;
514
515         if (cmd->chanlist_len > 0) {
516                 gain = CR_RANGE(cmd->chanlist[0]);
517                 for (i = 0; i < cmd->chanlist_len; ++i) {
518                         chan = CR_CHAN(cmd->chanlist[i]);
519                         if (chan != i) {
520                                 dev_err(dev->class_dev,
521                                         "channels are not consecutive\n");
522                                 up(&devpriv->sem);
523                                 return -EINVAL;
524                         }
525                         if ((gain != CR_RANGE(cmd->chanlist[i]))
526                             && (cmd->chanlist_len > 3)) {
527                                 dev_err(dev->class_dev,
528                                         "gain must be the same for all channels\n");
529                                 up(&devpriv->sem);
530                                 return -EINVAL;
531                         }
532                         if (i >= NUMCHANNELS) {
533                                 dev_err(dev->class_dev, "chanlist too long\n");
534                                 break;
535                         }
536                 }
537         }
538         steps = 0;
539         if (cmd->scan_begin_src == TRIG_TIMER) {
540                 dev_err(dev->class_dev,
541                         "scan_begin_src==TRIG_TIMER not valid\n");
542                 up(&devpriv->sem);
543                 return -EINVAL;
544         }
545         if (cmd->convert_src == TRIG_TIMER)
546                 steps = (cmd->convert_arg * 30) / 1000;
547
548         if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
549                 dev_err(dev->class_dev,
550                         "steps=%ld, scan_begin_arg=%d. Not properly tested by cmdtest?\n",
551                         steps, cmd->scan_begin_arg);
552                 up(&devpriv->sem);
553                 return -EINVAL;
554         }
555         if (steps > MAX_SAMPLING_PERIOD) {
556                 dev_err(dev->class_dev, "sampling rate too low\n");
557                 up(&devpriv->sem);
558                 return -EINVAL;
559         }
560         if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
561             && (cmd->chanlist_len != 16)) {
562                 dev_err(dev->class_dev,
563                         "TRIG_EXT only with 1 or 16 channels possible\n");
564                 up(&devpriv->sem);
565                 return -EINVAL;
566         }
567
568         switch (cmd->chanlist_len) {
569         case 1:
570                 /*
571                  * one channel
572                  */
573
574                 if (CR_RANGE(cmd->chanlist[0]) > 0)
575                         rngmask = 0xff - 0x04;
576                 else
577                         rngmask = 0xff;
578
579                 /*
580                  * for external trigger: looping in this state until
581                  * the RDY0 pin becomes zero
582                  */
583
584                 /* we loop here until ready has been set */
585                 if (cmd->start_src == TRIG_EXT) {
586                         /* branch back to state 0 */
587                         /* deceision state w/o data */
588                         /* RDY0 = 0 */
589                         usbduxfast_cmd_data(dev, 0, 0x01, 0x01, rngmask, 0x00);
590                 } else {        /* we just proceed to state 1 */
591                         usbduxfast_cmd_data(dev, 0, 0x01, 0x00, rngmask, 0x00);
592                 }
593
594                 if (steps < MIN_SAMPLING_PERIOD) {
595                         /* for fast single channel aqu without mux */
596                         if (steps <= 1) {
597                                 /*
598                                  * we just stay here at state 1 and rexecute
599                                  * the same state this gives us 30MHz sampling
600                                  * rate
601                                  */
602
603                                 /* branch back to state 1 */
604                                 /* deceision state with data */
605                                 /* doesn't matter */
606                                 usbduxfast_cmd_data(dev, 1,
607                                                     0x89, 0x03, rngmask, 0xff);
608                         } else {
609                                 /*
610                                  * we loop through two states: data and delay
611                                  * max rate is 15MHz
612                                  */
613                                 /* data */
614                                 /* doesn't matter */
615                                 usbduxfast_cmd_data(dev, 1, steps - 1,
616                                                     0x02, rngmask, 0x00);
617
618                                 /* branch back to state 1 */
619                                 /* deceision state w/o data */
620                                 /* doesn't matter */
621                                 usbduxfast_cmd_data(dev, 2,
622                                                     0x09, 0x01, rngmask, 0xff);
623                         }
624                 } else {
625                         /*
626                          * we loop through 3 states: 2x delay and 1x data
627                          * this gives a min sampling rate of 60kHz
628                          */
629
630                         /* we have 1 state with duration 1 */
631                         steps = steps - 1;
632
633                         /* do the first part of the delay */
634                         usbduxfast_cmd_data(dev, 1,
635                                             steps / 2, 0x00, rngmask, 0x00);
636
637                         /* and the second part */
638                         usbduxfast_cmd_data(dev, 2, steps - steps / 2,
639                                             0x00, rngmask, 0x00);
640
641                         /* get the data and branch back */
642
643                         /* branch back to state 1 */
644                         /* deceision state w data */
645                         /* doesn't matter */
646                         usbduxfast_cmd_data(dev, 3,
647                                             0x09, 0x03, rngmask, 0xff);
648                 }
649                 break;
650
651         case 2:
652                 /*
653                  * two channels
654                  * commit data to the FIFO
655                  */
656
657                 if (CR_RANGE(cmd->chanlist[0]) > 0)
658                         rngmask = 0xff - 0x04;
659                 else
660                         rngmask = 0xff;
661
662                 /* data */
663                 usbduxfast_cmd_data(dev, 0, 0x01, 0x02, rngmask, 0x00);
664
665                 /* we have 1 state with duration 1: state 0 */
666                 steps_tmp = steps - 1;
667
668                 if (CR_RANGE(cmd->chanlist[1]) > 0)
669                         rngmask = 0xff - 0x04;
670                 else
671                         rngmask = 0xff;
672
673                 /* do the first part of the delay */
674                 /* count */
675                 usbduxfast_cmd_data(dev, 1, steps_tmp / 2,
676                                     0x00, 0xfe & rngmask, 0x00);
677
678                 /* and the second part */
679                 usbduxfast_cmd_data(dev, 2, steps_tmp  - steps_tmp / 2,
680                                     0x00, rngmask, 0x00);
681
682                 /* data */
683                 usbduxfast_cmd_data(dev, 3, 0x01, 0x02, rngmask, 0x00);
684
685                 /*
686                  * we have 2 states with duration 1: step 6 and
687                  * the IDLE state
688                  */
689                 steps_tmp = steps - 2;
690
691                 if (CR_RANGE(cmd->chanlist[0]) > 0)
692                         rngmask = 0xff - 0x04;
693                 else
694                         rngmask = 0xff;
695
696                 /* do the first part of the delay */
697                 /* reset */
698                 usbduxfast_cmd_data(dev, 4, steps_tmp / 2,
699                                     0x00, (0xff - 0x02) & rngmask, 0x00);
700
701                 /* and the second part */
702                 usbduxfast_cmd_data(dev, 5, steps_tmp - steps_tmp / 2,
703                                     0x00, rngmask, 0x00);
704
705                 usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
706                 break;
707
708         case 3:
709                 /*
710                  * three channels
711                  */
712                 for (j = 0; j < 1; j++) {
713                         int index = j * 2;
714
715                         if (CR_RANGE(cmd->chanlist[j]) > 0)
716                                 rngmask = 0xff - 0x04;
717                         else
718                                 rngmask = 0xff;
719                         /*
720                          * commit data to the FIFO and do the first part
721                          * of the delay
722                          */
723                         /* data */
724                         /* no change */
725                         usbduxfast_cmd_data(dev, index, steps / 2,
726                                             0x02, rngmask, 0x00);
727
728                         if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
729                                 rngmask = 0xff - 0x04;
730                         else
731                                 rngmask = 0xff;
732
733                         /* do the second part of the delay */
734                         /* no data */
735                         /* count */
736                         usbduxfast_cmd_data(dev, index + 1, steps - steps / 2,
737                                             0x00, 0xfe & rngmask, 0x00);
738                 }
739
740                 /* 2 steps with duration 1: the idele step and step 6: */
741                 steps_tmp = steps - 2;
742
743                 /* commit data to the FIFO and do the first part of the delay */
744                 /* data */
745                 usbduxfast_cmd_data(dev, 4, steps_tmp / 2,
746                                     0x02, rngmask, 0x00);
747
748                 if (CR_RANGE(cmd->chanlist[0]) > 0)
749                         rngmask = 0xff - 0x04;
750                 else
751                         rngmask = 0xff;
752
753                 /* do the second part of the delay */
754                 /* no data */
755                 /* reset */
756                 usbduxfast_cmd_data(dev, 5, steps_tmp - steps_tmp / 2,
757                                     0x00, (0xff - 0x02) & rngmask, 0x00);
758
759                 usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
760
761         case 16:
762                 if (CR_RANGE(cmd->chanlist[0]) > 0)
763                         rngmask = 0xff - 0x04;
764                 else
765                         rngmask = 0xff;
766
767                 if (cmd->start_src == TRIG_EXT) {
768                         /*
769                          * we loop here until ready has been set
770                          */
771
772                         /* branch back to state 0 */
773                         /* deceision state w/o data */
774                         /* reset */
775                         /* RDY0 = 0 */
776                         usbduxfast_cmd_data(dev, 0, 0x01, 0x01,
777                                             (0xff - 0x02) & rngmask, 0x00);
778                 } else {
779                         /*
780                          * we just proceed to state 1
781                          */
782
783                         /* 30us reset pulse */
784                         /* reset */
785                         usbduxfast_cmd_data(dev, 0, 0xff, 0x00,
786                                             (0xff - 0x02) & rngmask, 0x00);
787                 }
788
789                 /* commit data to the FIFO */
790                 /* data */
791                 usbduxfast_cmd_data(dev, 1, 0x01, 0x02, rngmask, 0x00);
792
793                 /* we have 2 states with duration 1 */
794                 steps = steps - 2;
795
796                 /* do the first part of the delay */
797                 usbduxfast_cmd_data(dev, 2, steps / 2,
798                                     0x00, 0xfe & rngmask, 0x00);
799
800                 /* and the second part */
801                 usbduxfast_cmd_data(dev, 3, steps - steps / 2,
802                                     0x00, rngmask, 0x00);
803
804                 /* branch back to state 1 */
805                 /* deceision state w/o data */
806                 /* doesn't matter */
807                 usbduxfast_cmd_data(dev, 4, 0x09, 0x01, rngmask, 0xff);
808
809                 break;
810
811         default:
812                 dev_err(dev->class_dev, "unsupported combination of channels\n");
813                 up(&devpriv->sem);
814                 return -EFAULT;
815         }
816
817         /* 0 means that the AD commands are sent */
818         result = usbduxfast_send_cmd(dev, SENDADCOMMANDS);
819         if (result < 0) {
820                 up(&devpriv->sem);
821                 return result;
822         }
823         if (cmd->stop_src == TRIG_COUNT) {
824                 devpriv->ai_sample_count = cmd->stop_arg * cmd->scan_end_arg;
825                 if (devpriv->ai_sample_count < 1) {
826                         dev_err(dev->class_dev,
827                                 "(cmd->stop_arg)*(cmd->scan_end_arg)<1, aborting\n");
828                         up(&devpriv->sem);
829                         return -EFAULT;
830                 }
831                 devpriv->ai_continous = 0;
832         } else {
833                 /* continous acquisition */
834                 devpriv->ai_continous = 1;
835                 devpriv->ai_sample_count = 0;
836         }
837
838         if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
839                 /* enable this acquisition operation */
840                 devpriv->ai_cmd_running = 1;
841                 ret = usbduxfast_submit_urb(dev);
842                 if (ret < 0) {
843                         devpriv->ai_cmd_running = 0;
844                         /* fixme: unlink here?? */
845                         up(&devpriv->sem);
846                         return ret;
847                 }
848                 s->async->inttrig = NULL;
849         } else {
850                 /*
851                  * TRIG_INT
852                  * don't enable the acquision operation
853                  * wait for an internal signal
854                  */
855                 s->async->inttrig = usbduxfast_ai_inttrig;
856         }
857         up(&devpriv->sem);
858
859         return 0;
860 }
861
862 /*
863  * Mode 0 is used to get a single conversion on demand.
864  */
865 static int usbduxfast_ai_insn_read(struct comedi_device *dev,
866                                    struct comedi_subdevice *s,
867                                    struct comedi_insn *insn,
868                                    unsigned int *data)
869 {
870         struct usb_device *usb = comedi_to_usb_dev(dev);
871         struct usbduxfast_private *devpriv = dev->private;
872         unsigned int chan = CR_CHAN(insn->chanspec);
873         unsigned int range = CR_RANGE(insn->chanspec);
874         uint8_t rngmask = range ? (0xff - 0x04) : 0xff;
875         int i, j, n, actual_length;
876         int ret;
877
878         down(&devpriv->sem);
879
880         if (devpriv->ai_cmd_running) {
881                 dev_err(dev->class_dev,
882                         "ai_insn_read not possible, async cmd is running\n");
883                 up(&devpriv->sem);
884                 return -EBUSY;
885         }
886
887         /* set command for the first channel */
888
889         /* commit data to the FIFO */
890         /* data */
891         usbduxfast_cmd_data(dev, 0, 0x01, 0x02, rngmask, 0x00);
892
893         /* do the first part of the delay */
894         usbduxfast_cmd_data(dev, 1, 0x0c, 0x00, 0xfe & rngmask, 0x00);
895         usbduxfast_cmd_data(dev, 2, 0x01, 0x00, 0xfe & rngmask, 0x00);
896         usbduxfast_cmd_data(dev, 3, 0x01, 0x00, 0xfe & rngmask, 0x00);
897         usbduxfast_cmd_data(dev, 4, 0x01, 0x00, 0xfe & rngmask, 0x00);
898
899         /* second part */
900         usbduxfast_cmd_data(dev, 5, 0x0c, 0x00, rngmask, 0x00);
901         usbduxfast_cmd_data(dev, 6, 0x01, 0x00, rngmask, 0x00);
902
903         ret = usbduxfast_send_cmd(dev, SENDADCOMMANDS);
904         if (ret < 0) {
905                 up(&devpriv->sem);
906                 return ret;
907         }
908
909         for (i = 0; i < PACKETS_TO_IGNORE; i++) {
910                 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, BULKINEP),
911                                    devpriv->inbuf, SIZEINBUF,
912                                    &actual_length, 10000);
913                 if (ret < 0) {
914                         dev_err(dev->class_dev, "insn timeout, no data\n");
915                         up(&devpriv->sem);
916                         return ret;
917                 }
918         }
919
920         for (i = 0; i < insn->n;) {
921                 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, BULKINEP),
922                                    devpriv->inbuf, SIZEINBUF,
923                                    &actual_length, 10000);
924                 if (ret < 0) {
925                         dev_err(dev->class_dev, "insn data error: %d\n", ret);
926                         up(&devpriv->sem);
927                         return ret;
928                 }
929                 n = actual_length / sizeof(uint16_t);
930                 if ((n % 16) != 0) {
931                         dev_err(dev->class_dev, "insn data packet corrupted\n");
932                         up(&devpriv->sem);
933                         return -EINVAL;
934                 }
935                 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
936                         data[i] = ((uint16_t *) (devpriv->inbuf))[j];
937                         i++;
938                 }
939         }
940
941         up(&devpriv->sem);
942
943         return insn->n;
944 }
945
946 static int usbduxfast_attach_common(struct comedi_device *dev)
947 {
948         struct usbduxfast_private *devpriv = dev->private;
949         struct comedi_subdevice *s;
950         int ret;
951
952         down(&devpriv->sem);
953
954         ret = comedi_alloc_subdevices(dev, 1);
955         if (ret) {
956                 up(&devpriv->sem);
957                 return ret;
958         }
959
960         /* Analog Input subdevice */
961         s = &dev->subdevices[0];
962         dev->read_subdev = s;
963         s->type         = COMEDI_SUBD_AI;
964         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
965         s->n_chan       = 16;
966         s->len_chanlist = 16;
967         s->insn_read    = usbduxfast_ai_insn_read;
968         s->do_cmdtest   = usbduxfast_ai_cmdtest;
969         s->do_cmd       = usbduxfast_ai_cmd;
970         s->cancel       = usbduxfast_ai_cancel;
971         s->maxdata      = 0x1000;
972         s->range_table  = &range_usbduxfast_ai_range;
973
974         up(&devpriv->sem);
975
976         return 0;
977 }
978
979 static int usbduxfast_upload_firmware(struct comedi_device *dev,
980                                       const u8 *data, size_t size,
981                                       unsigned long context)
982 {
983         struct usb_device *usb = comedi_to_usb_dev(dev);
984         uint8_t *buf;
985         unsigned char *tmp;
986         int ret;
987
988         if (!data)
989                 return 0;
990
991         if (size > FIRMWARE_MAX_LEN) {
992                 dev_err(dev->class_dev, "firmware binary too large for FX2\n");
993                 return -ENOMEM;
994         }
995
996         /* we generate a local buffer for the firmware */
997         buf = kmemdup(data, size, GFP_KERNEL);
998         if (!buf)
999                 return -ENOMEM;
1000
1001         /* we need a malloc'ed buffer for usb_control_msg() */
1002         tmp = kmalloc(1, GFP_KERNEL);
1003         if (!tmp) {
1004                 kfree(buf);
1005                 return -ENOMEM;
1006         }
1007
1008         /* stop the current firmware on the device */
1009         *tmp = 1;       /* 7f92 to one */
1010         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1011                               USBDUXFASTSUB_FIRMWARE,
1012                               VENDOR_DIR_OUT,
1013                               USBDUXFASTSUB_CPUCS, 0x0000,
1014                               tmp, 1,
1015                               EZTIMEOUT);
1016         if (ret < 0) {
1017                 dev_err(dev->class_dev, "can not stop firmware\n");
1018                 goto done;
1019         }
1020
1021         /* upload the new firmware to the device */
1022         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1023                               USBDUXFASTSUB_FIRMWARE,
1024                               VENDOR_DIR_OUT,
1025                               0, 0x0000,
1026                               buf, size,
1027                               EZTIMEOUT);
1028         if (ret < 0) {
1029                 dev_err(dev->class_dev, "firmware upload failed\n");
1030                 goto done;
1031         }
1032
1033         /* start the new firmware on the device */
1034         *tmp = 0;       /* 7f92 to zero */
1035         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1036                               USBDUXFASTSUB_FIRMWARE,
1037                               VENDOR_DIR_OUT,
1038                               USBDUXFASTSUB_CPUCS, 0x0000,
1039                               tmp, 1,
1040                               EZTIMEOUT);
1041         if (ret < 0)
1042                 dev_err(dev->class_dev, "can not start firmware\n");
1043
1044 done:
1045         kfree(tmp);
1046         kfree(buf);
1047         return ret;
1048 }
1049
1050 static int usbduxfast_auto_attach(struct comedi_device *dev,
1051                                   unsigned long context_unused)
1052 {
1053         struct usb_interface *intf = comedi_to_usb_interface(dev);
1054         struct usb_device *usb = comedi_to_usb_dev(dev);
1055         struct usbduxfast_private *devpriv;
1056         int ret;
1057
1058         if (usb->speed != USB_SPEED_HIGH) {
1059                 dev_err(dev->class_dev,
1060                         "This driver needs USB 2.0 to operate. Aborting...\n");
1061                 return -ENODEV;
1062         }
1063
1064         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1065         if (!devpriv)
1066                 return -ENOMEM;
1067
1068         sema_init(&devpriv->sem, 1);
1069         usb_set_intfdata(intf, devpriv);
1070
1071         devpriv->duxbuf = kmalloc(SIZEOFDUXBUF, GFP_KERNEL);
1072         if (!devpriv->duxbuf)
1073                 return -ENOMEM;
1074
1075         ret = usb_set_interface(usb,
1076                                 intf->altsetting->desc.bInterfaceNumber, 1);
1077         if (ret < 0) {
1078                 dev_err(dev->class_dev,
1079                         "could not switch to alternate setting 1\n");
1080                 return -ENODEV;
1081         }
1082
1083         devpriv->urb = usb_alloc_urb(0, GFP_KERNEL);
1084         if (!devpriv->urb) {
1085                 dev_err(dev->class_dev, "Could not alloc. urb\n");
1086                 return -ENOMEM;
1087         }
1088
1089         devpriv->inbuf = kmalloc(SIZEINBUF, GFP_KERNEL);
1090         if (!devpriv->inbuf)
1091                 return -ENOMEM;
1092
1093         ret = comedi_load_firmware(dev, &usb->dev, FIRMWARE,
1094                                    usbduxfast_upload_firmware, 0);
1095         if (ret)
1096                 return ret;
1097
1098         return usbduxfast_attach_common(dev);
1099 }
1100
1101 static void usbduxfast_detach(struct comedi_device *dev)
1102 {
1103         struct usb_interface *intf = comedi_to_usb_interface(dev);
1104         struct usbduxfast_private *devpriv = dev->private;
1105
1106         if (!devpriv)
1107                 return;
1108
1109         down(&devpriv->sem);
1110
1111         usb_set_intfdata(intf, NULL);
1112
1113         if (devpriv->urb) {
1114                 /* waits until a running transfer is over */
1115                 usb_kill_urb(devpriv->urb);
1116
1117                 kfree(devpriv->inbuf);
1118                 devpriv->inbuf = NULL;
1119
1120                 usb_free_urb(devpriv->urb);
1121                 devpriv->urb = NULL;
1122         }
1123
1124         kfree(devpriv->duxbuf);
1125         devpriv->duxbuf = NULL;
1126
1127         devpriv->ai_cmd_running = 0;
1128
1129         up(&devpriv->sem);
1130 }
1131
1132 static struct comedi_driver usbduxfast_driver = {
1133         .driver_name    = "usbduxfast",
1134         .module         = THIS_MODULE,
1135         .auto_attach    = usbduxfast_auto_attach,
1136         .detach         = usbduxfast_detach,
1137 };
1138
1139 static int usbduxfast_usb_probe(struct usb_interface *intf,
1140                                 const struct usb_device_id *id)
1141 {
1142         return comedi_usb_auto_config(intf, &usbduxfast_driver, 0);
1143 }
1144
1145 static const struct usb_device_id usbduxfast_usb_table[] = {
1146         /* { USB_DEVICE(0x4b4, 0x8613) }, testing */
1147         { USB_DEVICE(0x13d8, 0x0010) }, /* real ID */
1148         { USB_DEVICE(0x13d8, 0x0011) }, /* real ID */
1149         { }
1150 };
1151 MODULE_DEVICE_TABLE(usb, usbduxfast_usb_table);
1152
1153 static struct usb_driver usbduxfast_usb_driver = {
1154         .name           = "usbduxfast",
1155         .probe          = usbduxfast_usb_probe,
1156         .disconnect     = comedi_usb_auto_unconfig,
1157         .id_table       = usbduxfast_usb_table,
1158 };
1159 module_comedi_usb_driver(usbduxfast_driver, usbduxfast_usb_driver);
1160
1161 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1162 MODULE_DESCRIPTION("USB-DUXfast, BerndPorr@f2s.com");
1163 MODULE_LICENSE("GPL");
1164 MODULE_FIRMWARE(FIRMWARE);