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