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