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