staging: comedi: usbdux: pass comedi_device pointer to usbdux_alloc_usb_buffers()
[linux-2.6-block.git] / drivers / staging / comedi / drivers / usbdux.c
1 /*
2    comedi/drivers/usbdux.c
3    Copyright (C) 2003-2007 Bernd Porr, Bernd.Porr@f2s.com
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14  */
15 /*
16 Driver: usbdux
17 Description: University of Stirling USB DAQ & INCITE Technology Limited
18 Devices: [ITL] USB-DUX (usbdux.o)
19 Author: Bernd Porr <BerndPorr@f2s.com>
20 Updated: 8 Dec 2008
21 Status: Stable
22 Configuration options:
23   You have to upload firmware with the -i option. The
24   firmware is usually installed under /usr/share/usb or
25   /usr/local/share/usb or /lib/firmware.
26
27 Connection scheme for the counter at the digital port:
28   0=/CLK0, 1=UP/DOWN0, 2=RESET0, 4=/CLK1, 5=UP/DOWN1, 6=RESET1.
29   The sampling rate of the counter is approximately 500Hz.
30
31 Please note that under USB2.0 the length of the channel list determines
32 the max sampling rate. If you sample only one channel you get 8kHz
33 sampling rate. If you sample two channels you get 4kHz and so on.
34 */
35 /*
36  * I must give credit here to Chris Baugher who
37  * wrote the driver for AT-MIO-16d. I used some parts of this
38  * driver. I also must give credits to David Brownell
39  * who supported me with the USB development.
40  *
41  * Bernd Porr
42  *
43  *
44  * Revision history:
45  * 0.94: D/A output should work now with any channel list combinations
46  * 0.95: .owner commented out for kernel vers below 2.4.19
47  *       sanity checks in ai/ao_cmd
48  * 0.96: trying to get it working with 2.6, moved all memory alloc to comedi's
49  *       attach final USB IDs
50  *       moved memory allocation completely to the corresponding comedi
51  *       functions firmware upload is by fxload and no longer by comedi (due to
52  *       enumeration)
53  * 0.97: USB IDs received, adjusted table
54  * 0.98: SMP, locking, memory alloc: moved all usb memory alloc
55  *       to the usb subsystem and moved all comedi related memory
56  *       alloc to comedi.
57  *       | kernel | registration | usbdux-usb | usbdux-comedi | comedi |
58  * 0.99: USB 2.0: changed protocol to isochronous transfer
59  *                IRQ transfer is too buggy and too risky in 2.0
60  *                for the high speed ISO transfer is now a working version
61  *                available
62  * 0.99b: Increased the iso transfer buffer for high sp.to 10 buffers. Some VIA
63  *        chipsets miss out IRQs. Deeper buffering is needed.
64  * 1.00: full USB 2.0 support for the A/D converter. Now: max 8kHz sampling
65  *       rate.
66  *       Firmware vers 1.00 is needed for this.
67  *       Two 16 bit up/down/reset counter with a sampling rate of 1kHz
68  *       And loads of cleaning up, in particular streamlining the
69  *       bulk transfers.
70  * 1.1:  moved EP4 transfers to EP1 to make space for a PWM output on EP4
71  * 1.2:  added PWM support via EP4
72  * 2.0:  PWM seems to be stable and is not interfering with the other functions
73  * 2.1:  changed PWM API
74  * 2.2:  added firmware kernel request to fix an udev problem
75  * 2.3:  corrected a bug in bulk timeouts which were far too short
76  * 2.4:  fixed a bug which causes the driver to hang when it ran out of data.
77  *       Thanks to Jan-Matthias Braun and Ian to spot the bug and fix it.
78  *
79  */
80
81 #include <linux/kernel.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
90 #include "../comedidev.h"
91
92 #include "comedi_fc.h"
93
94 /* timeout for the USB-transfer in ms*/
95 #define BULK_TIMEOUT 1000
96
97 /* constants for "firmware" upload and download */
98 #define FIRMWARE "usbdux_firmware.bin"
99 #define USBDUXSUB_FIRMWARE 0xA0
100 #define VENDOR_DIR_IN  0xC0
101 #define VENDOR_DIR_OUT 0x40
102
103 /* internal addresses of the 8051 processor */
104 #define USBDUXSUB_CPUCS 0xE600
105
106 /*
107  * the minor device number, major is 180 only for debugging purposes and to
108  * upload special firmware (programming the eeprom etc) which is not compatible
109  * with the comedi framwork
110  */
111 #define USBDUXSUB_MINOR 32
112
113 /* max lenghth of the transfer-buffer for software upload */
114 #define TB_LEN 0x2000
115
116 /* Input endpoint number: ISO/IRQ */
117 #define ISOINEP           6
118
119 /* Output endpoint number: ISO/IRQ */
120 #define ISOOUTEP          2
121
122 /* This EP sends DUX commands to USBDUX */
123 #define COMMAND_OUT_EP     1
124
125 /* This EP receives the DUX commands from USBDUX */
126 #define COMMAND_IN_EP        8
127
128 /* Output endpoint for PWM */
129 #define PWM_EP         4
130
131 /* 300Hz max frequ under PWM */
132 #define MIN_PWM_PERIOD  ((long)(1E9/300))
133
134 /* Default PWM frequency */
135 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
136
137 /* Number of channels */
138 #define NUMCHANNELS       8
139
140 /* Size of one A/D value */
141 #define SIZEADIN          ((sizeof(int16_t)))
142
143 /*
144  * Size of the input-buffer IN BYTES
145  * Always multiple of 8 for 8 microframes which is needed in the highspeed mode
146  */
147 #define SIZEINBUF         ((8*SIZEADIN))
148
149 /* 16 bytes. */
150 #define SIZEINSNBUF       16
151
152 /* Number of DA channels */
153 #define NUMOUTCHANNELS    8
154
155 /* size of one value for the D/A converter: channel and value */
156 #define SIZEDAOUT          ((sizeof(int8_t)+sizeof(int16_t)))
157
158 /*
159  * Size of the output-buffer in bytes
160  * Actually only the first 4 triplets are used but for the
161  * high speed mode we need to pad it to 8 (microframes).
162  */
163 #define SIZEOUTBUF         ((8*SIZEDAOUT))
164
165 /*
166  * Size of the buffer for the dux commands: just now max size is determined
167  * by the analogue out + command byte + panic bytes...
168  */
169 #define SIZEOFDUXBUFFER    ((8*SIZEDAOUT+2))
170
171 /* Number of in-URBs which receive the data: min=2 */
172 #define NUMOFINBUFFERSFULL     5
173
174 /* Number of out-URBs which send the data: min=2 */
175 #define NUMOFOUTBUFFERSFULL    5
176
177 /* Number of in-URBs which receive the data: min=5 */
178 /* must have more buffers due to buggy USB ctr */
179 #define NUMOFINBUFFERSHIGH     10
180
181 /* Number of out-URBs which send the data: min=5 */
182 /* must have more buffers due to buggy USB ctr */
183 #define NUMOFOUTBUFFERSHIGH    10
184
185 /* number of retries to get the right dux command */
186 #define RETRIES 10
187
188 static const struct comedi_lrange range_usbdux_ai_range = {
189         4, {
190                 BIP_RANGE(4.096),
191                 BIP_RANGE(4.096 / 2),
192                 UNI_RANGE(4.096),
193                 UNI_RANGE(4.096 / 2)
194         }
195 };
196
197 static const struct comedi_lrange range_usbdux_ao_range = {
198         2, {
199                 BIP_RANGE(4.096),
200                 UNI_RANGE(4.096)
201         }
202 };
203
204 struct usbdux_private {
205         /* pointer to the usb-device */
206         struct usb_device *usbdev;
207         /* actual number of in-buffers */
208         int num_in_buffers;
209         /* actual number of out-buffers */
210         int num_out_buffers;
211         /* ISO-transfer handling: buffers */
212         struct urb **urb_in;
213         struct urb **urb_out;
214         /* pwm-transfer handling */
215         struct urb *urb_pwm;
216         /* PWM period */
217         unsigned int pwm_period;
218         /* PWM internal delay for the GPIF in the FX2 */
219         int8_t pwn_delay;
220         /* size of the PWM buffer which holds the bit pattern */
221         int size_pwm_buf;
222         /* input buffer for the ISO-transfer */
223         int16_t *in_buffer;
224         /* input buffer for single insn */
225         int16_t *insn_buffer;
226         /* output buffer for single DA outputs */
227         int16_t *out_buffer;
228         /* interface number */
229         int ifnum;
230         /* is it USB_SPEED_HIGH or not? */
231         short int high_speed;
232         /* asynchronous command is running */
233         short int ai_cmd_running;
234         short int ao_cmd_running;
235         /* pwm is running */
236         short int pwm_cmd_running;
237         /* continous acquisition */
238         short int ai_continous;
239         short int ao_continous;
240         /* number of samples to acquire */
241         int ai_sample_count;
242         int ao_sample_count;
243         /* time between samples in units of the timer */
244         unsigned int ai_timer;
245         unsigned int ao_timer;
246         /* counter between aquisitions */
247         unsigned int ai_counter;
248         unsigned int ao_counter;
249         /* interval in frames/uframes */
250         unsigned int ai_interval;
251         /* D/A commands */
252         int8_t *dac_commands;
253         /* commands */
254         int8_t *dux_commands;
255         struct semaphore sem;
256 };
257
258 /*
259  * Stops the data acquision
260  * It should be safe to call this function from any context
261  */
262 static int usbduxsub_unlink_inurbs(struct usbdux_private *usbduxsub_tmp)
263 {
264         int i = 0;
265         int err = 0;
266
267         if (usbduxsub_tmp && usbduxsub_tmp->urb_in) {
268                 for (i = 0; i < usbduxsub_tmp->num_in_buffers; i++) {
269                         if (usbduxsub_tmp->urb_in[i]) {
270                                 /* We wait here until all transfers have been
271                                  * cancelled. */
272                                 usb_kill_urb(usbduxsub_tmp->urb_in[i]);
273                         }
274                 }
275         }
276         return err;
277 }
278
279 /*
280  * This will stop a running acquisition operation
281  * Is called from within this driver from both the
282  * interrupt context and from comedi
283  */
284 static int usbdux_ai_stop(struct usbdux_private *this_usbduxsub, int do_unlink)
285 {
286         int ret = 0;
287
288         if (!this_usbduxsub)
289                 return -EFAULT;
290
291         if (do_unlink) {
292                 /* stop aquistion */
293                 ret = usbduxsub_unlink_inurbs(this_usbduxsub);
294         }
295
296         this_usbduxsub->ai_cmd_running = 0;
297
298         return ret;
299 }
300
301 /*
302  * This will cancel a running acquisition operation.
303  * This is called by comedi but never from inside the driver.
304  */
305 static int usbdux_ai_cancel(struct comedi_device *dev,
306                             struct comedi_subdevice *s)
307 {
308         struct usbdux_private *this_usbduxsub;
309         int res = 0;
310
311         /* force unlink of all urbs */
312         this_usbduxsub = dev->private;
313         if (!this_usbduxsub)
314                 return -EFAULT;
315
316         /* prevent other CPUs from submitting new commands just now */
317         down(&this_usbduxsub->sem);
318         /* unlink only if the urb really has been submitted */
319         res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
320         up(&this_usbduxsub->sem);
321         return res;
322 }
323
324 /* analogue IN - interrupt service routine */
325 static void usbduxsub_ai_isoc_irq(struct urb *urb)
326 {
327         struct comedi_device *dev = urb->context;
328         struct comedi_subdevice *s = dev->read_subdev;
329         struct usbdux_private *devpriv = dev->private;
330         int i, err, n;
331
332         /* first we test if something unusual has just happened */
333         switch (urb->status) {
334         case 0:
335                 /* copy the result in the transfer buffer */
336                 memcpy(devpriv->in_buffer, urb->transfer_buffer, SIZEINBUF);
337                 break;
338         case -EILSEQ:
339                 /* error in the ISOchronous data */
340                 /* we don't copy the data into the transfer buffer */
341                 /* and recycle the last data byte */
342                 dev_dbg(dev->class_dev, "CRC error in ISO IN stream\n");
343                 break;
344
345         case -ECONNRESET:
346         case -ENOENT:
347         case -ESHUTDOWN:
348         case -ECONNABORTED:
349                 /* happens after an unlink command */
350                 if (devpriv->ai_cmd_running) {
351                         s->async->events |= COMEDI_CB_EOA;
352                         s->async->events |= COMEDI_CB_ERROR;
353                         comedi_event(dev, s);
354                         /* stop the transfer w/o unlink */
355                         usbdux_ai_stop(devpriv, 0);
356                 }
357                 return;
358
359         default:
360                 /* a real error on the bus */
361                 /* pass error to comedi if we are really running a command */
362                 if (devpriv->ai_cmd_running) {
363                         dev_err(dev->class_dev,
364                                 "Non-zero urb status received in ai intr context: %d\n",
365                                 urb->status);
366                         s->async->events |= COMEDI_CB_EOA;
367                         s->async->events |= COMEDI_CB_ERROR;
368                         comedi_event(dev, s);
369                         /* don't do an unlink here */
370                         usbdux_ai_stop(devpriv, 0);
371                 }
372                 return;
373         }
374
375         /*
376          * at this point we are reasonably sure that nothing dodgy has happened
377          * are we running a command?
378          */
379         if (unlikely(!devpriv->ai_cmd_running)) {
380                 /*
381                  * not running a command, do not continue execution if no
382                  * asynchronous command is running in particular not resubmit
383                  */
384                 return;
385         }
386
387         urb->dev = devpriv->usbdev;
388
389         /* resubmit the urb */
390         err = usb_submit_urb(urb, GFP_ATOMIC);
391         if (unlikely(err < 0)) {
392                 dev_err(dev->class_dev,
393                         "urb resubmit failed in int-context! err=%d\n", err);
394                 if (err == -EL2NSYNC)
395                         dev_err(dev->class_dev,
396                                 "buggy USB host controller or bug in IRQ handler!\n");
397                 s->async->events |= COMEDI_CB_EOA;
398                 s->async->events |= COMEDI_CB_ERROR;
399                 comedi_event(dev, s);
400                 /* don't do an unlink here */
401                 usbdux_ai_stop(devpriv, 0);
402                 return;
403         }
404
405         devpriv->ai_counter--;
406         if (likely(devpriv->ai_counter > 0))
407                 return;
408
409         /* timer zero, transfer measurements to comedi */
410         devpriv->ai_counter = devpriv->ai_timer;
411
412         /* test, if we transmit only a fixed number of samples */
413         if (!devpriv->ai_continous) {
414                 /* not continuous, fixed number of samples */
415                 devpriv->ai_sample_count--;
416                 /* all samples received? */
417                 if (devpriv->ai_sample_count < 0) {
418                         /* prevent a resubmit next time */
419                         usbdux_ai_stop(devpriv, 0);
420                         /* say comedi that the acquistion is over */
421                         s->async->events |= COMEDI_CB_EOA;
422                         comedi_event(dev, s);
423                         return;
424                 }
425         }
426         /* get the data from the USB bus and hand it over to comedi */
427         n = s->async->cmd.chanlist_len;
428         for (i = 0; i < n; i++) {
429                 /* transfer data */
430                 if (CR_RANGE(s->async->cmd.chanlist[i]) <= 1) {
431                         err = comedi_buf_put(s->async,
432                              le16_to_cpu(devpriv->in_buffer[i]) ^ 0x800);
433                 } else {
434                         err = comedi_buf_put(s->async,
435                              le16_to_cpu(devpriv->in_buffer[i]));
436                 }
437                 if (unlikely(err == 0)) {
438                         /* buffer overflow */
439                         usbdux_ai_stop(devpriv, 0);
440                         return;
441                 }
442         }
443         /* tell comedi that data is there */
444         s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
445         comedi_event(dev, s);
446 }
447
448 static int usbduxsub_unlink_outurbs(struct usbdux_private *usbduxsub_tmp)
449 {
450         int i = 0;
451         int err = 0;
452
453         if (usbduxsub_tmp && usbduxsub_tmp->urb_out) {
454                 for (i = 0; i < usbduxsub_tmp->num_out_buffers; i++) {
455                         if (usbduxsub_tmp->urb_out[i])
456                                 usb_kill_urb(usbduxsub_tmp->urb_out[i]);
457                 }
458         }
459         return err;
460 }
461
462 /* This will cancel a running acquisition operation
463  * in any context.
464  */
465 static int usbdux_ao_stop(struct usbdux_private *this_usbduxsub, int do_unlink)
466 {
467         int ret = 0;
468
469         if (!this_usbduxsub)
470                 return -EFAULT;
471
472         if (do_unlink)
473                 ret = usbduxsub_unlink_outurbs(this_usbduxsub);
474
475         this_usbduxsub->ao_cmd_running = 0;
476
477         return ret;
478 }
479
480 /* force unlink, is called by comedi */
481 static int usbdux_ao_cancel(struct comedi_device *dev,
482                             struct comedi_subdevice *s)
483 {
484         struct usbdux_private *this_usbduxsub = dev->private;
485         int res = 0;
486
487         if (!this_usbduxsub)
488                 return -EFAULT;
489
490         /* prevent other CPUs from submitting a command just now */
491         down(&this_usbduxsub->sem);
492         /* unlink only if it is really running */
493         res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
494         up(&this_usbduxsub->sem);
495         return res;
496 }
497
498 static void usbduxsub_ao_isoc_irq(struct urb *urb)
499 {
500         struct comedi_device *dev = urb->context;
501         struct comedi_subdevice *s = dev->write_subdev;
502         struct usbdux_private *devpriv = dev->private;
503         int i, ret;
504         int8_t *datap;
505
506         switch (urb->status) {
507         case 0:
508                 /* success */
509                 break;
510
511         case -ECONNRESET:
512         case -ENOENT:
513         case -ESHUTDOWN:
514         case -ECONNABORTED:
515                 /* after an unlink command, unplug, ... etc */
516                 /* no unlink needed here. Already shutting down. */
517                 if (devpriv->ao_cmd_running) {
518                         s->async->events |= COMEDI_CB_EOA;
519                         comedi_event(dev, s);
520                         usbdux_ao_stop(devpriv, 0);
521                 }
522                 return;
523
524         default:
525                 /* a real error */
526                 if (devpriv->ao_cmd_running) {
527                         dev_err(dev->class_dev,
528                                 "Non-zero urb status received in ao intr context: %d\n",
529                                 urb->status);
530                         s->async->events |= COMEDI_CB_ERROR;
531                         s->async->events |= COMEDI_CB_EOA;
532                         comedi_event(dev, s);
533                         /* we do an unlink if we are in the high speed mode */
534                         usbdux_ao_stop(devpriv, 0);
535                 }
536                 return;
537         }
538
539         /* are we actually running? */
540         if (!devpriv->ao_cmd_running)
541                 return;
542
543         /* normal operation: executing a command in this subdevice */
544         devpriv->ao_counter--;
545         if ((int)devpriv->ao_counter <= 0) {
546                 /* timer zero */
547                 devpriv->ao_counter = devpriv->ao_timer;
548
549                 /* handle non continous acquisition */
550                 if (!devpriv->ao_continous) {
551                         /* fixed number of samples */
552                         devpriv->ao_sample_count--;
553                         if (devpriv->ao_sample_count < 0) {
554                                 /* all samples transmitted */
555                                 usbdux_ao_stop(devpriv, 0);
556                                 s->async->events |= COMEDI_CB_EOA;
557                                 comedi_event(dev, s);
558                                 /* no resubmit of the urb */
559                                 return;
560                         }
561                 }
562                 /* transmit data to the USB bus */
563                 ((uint8_t *) (urb->transfer_buffer))[0] =
564                     s->async->cmd.chanlist_len;
565                 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
566                         short temp;
567                         if (i >= NUMOUTCHANNELS)
568                                 break;
569
570                         /* pointer to the DA */
571                         datap =
572                             (&(((int8_t *) urb->transfer_buffer)[i * 3 + 1]));
573                         /* get the data from comedi */
574                         ret = comedi_buf_get(s->async, &temp);
575                         datap[0] = temp;
576                         datap[1] = temp >> 8;
577                         datap[2] = devpriv->dac_commands[i];
578                         if (ret < 0) {
579                                 dev_err(&urb->dev->dev,
580                                         "comedi: buffer underflow\n");
581                                 s->async->events |= COMEDI_CB_EOA;
582                                 s->async->events |= COMEDI_CB_OVERFLOW;
583                         }
584                         /* transmit data to comedi */
585                         s->async->events |= COMEDI_CB_BLOCK;
586                         comedi_event(dev, s);
587                 }
588         }
589         urb->transfer_buffer_length = SIZEOUTBUF;
590         urb->dev = devpriv->usbdev;
591         urb->status = 0;
592         if (devpriv->ao_cmd_running) {
593                 if (devpriv->high_speed)
594                         urb->interval = 8;      /* uframes */
595                 else
596                         urb->interval = 1;      /* frames */
597                 urb->number_of_packets = 1;
598                 urb->iso_frame_desc[0].offset = 0;
599                 urb->iso_frame_desc[0].length = SIZEOUTBUF;
600                 urb->iso_frame_desc[0].status = 0;
601                 ret = usb_submit_urb(urb, GFP_ATOMIC);
602                 if (ret < 0) {
603                         dev_err(dev->class_dev,
604                                 "ao urb resubm failed in int-cont. ret=%d",
605                                 ret);
606                         if (ret == EL2NSYNC)
607                                 dev_err(dev->class_dev,
608                                         "buggy USB host controller or bug in IRQ handling!\n");
609
610                         s->async->events |= COMEDI_CB_EOA;
611                         s->async->events |= COMEDI_CB_ERROR;
612                         comedi_event(dev, s);
613                         /* don't do an unlink here */
614                         usbdux_ao_stop(devpriv, 0);
615                 }
616         }
617 }
618
619 #define FIRMWARE_MAX_LEN 0x2000
620
621 static int usbdux_firmware_upload(struct comedi_device *dev,
622                                   const u8 *data, size_t size,
623                                   unsigned long context)
624 {
625         struct usbdux_private *usbduxsub = dev->private;
626         struct usb_device *usb = usbduxsub->usbdev;
627         uint8_t *buf;
628         uint8_t *tmp;
629         int ret;
630
631         if (!data)
632                 return 0;
633
634         if (size > FIRMWARE_MAX_LEN) {
635                 dev_err(dev->class_dev,
636                         "usbdux firmware binary it too large for FX2.\n");
637                 return -ENOMEM;
638         }
639
640         /* we generate a local buffer for the firmware */
641         buf = kmemdup(data, size, GFP_KERNEL);
642         if (!buf)
643                 return -ENOMEM;
644
645         /* we need a malloc'ed buffer for usb_control_msg() */
646         tmp = kmalloc(1, GFP_KERNEL);
647         if (!tmp) {
648                 kfree(buf);
649                 return -ENOMEM;
650         }
651
652         /* stop the current firmware on the device */
653         *tmp = 1;       /* 7f92 to one */
654         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
655                               USBDUXSUB_FIRMWARE,
656                               VENDOR_DIR_OUT,
657                               USBDUXSUB_CPUCS, 0x0000,
658                               tmp, 1,
659                               BULK_TIMEOUT);
660         if (ret < 0) {
661                 dev_err(dev->class_dev, "can not stop firmware\n");
662                 goto done;
663         }
664
665         /* upload the new firmware to the device */
666         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
667                               USBDUXSUB_FIRMWARE,
668                               VENDOR_DIR_OUT,
669                               0, 0x0000,
670                               buf, size,
671                               BULK_TIMEOUT);
672         if (ret < 0) {
673                 dev_err(dev->class_dev, "firmware upload failed\n");
674                 goto done;
675         }
676
677         /* start the new firmware on the device */
678         *tmp = 0;       /* 7f92 to zero */
679         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
680                               USBDUXSUB_FIRMWARE,
681                               VENDOR_DIR_OUT,
682                               USBDUXSUB_CPUCS, 0x0000,
683                               tmp, 1,
684                               BULK_TIMEOUT);
685         if (ret < 0)
686                 dev_err(dev->class_dev, "can not start firmware\n");
687
688 done:
689         kfree(tmp);
690         kfree(buf);
691         return ret;
692 }
693
694 static int usbduxsub_submit_inurbs(struct comedi_device *dev)
695 {
696         struct usbdux_private *devpriv = dev->private;
697         struct urb *urb;
698         int ret;
699         int i;
700
701         /* Submit all URBs and start the transfer on the bus */
702         for (i = 0; i < devpriv->num_in_buffers; i++) {
703                 urb = devpriv->urb_in[i];
704
705                 /* in case of a resubmission after an unlink... */
706                 urb->interval = devpriv->ai_interval;
707                 urb->context = dev;
708                 urb->dev = devpriv->usbdev;
709                 urb->status = 0;
710                 urb->transfer_flags = URB_ISO_ASAP;
711
712                 ret = usb_submit_urb(urb, GFP_ATOMIC);
713                 if (ret)
714                         return ret;
715         }
716         return 0;
717 }
718
719 static int usbduxsub_submit_outurbs(struct comedi_device *dev)
720 {
721         struct usbdux_private *devpriv = dev->private;
722         struct urb *urb;
723         int ret;
724         int i;
725
726         for (i = 0; i < devpriv->num_out_buffers; i++) {
727                 urb = devpriv->urb_out[i];
728
729                 /* in case of a resubmission after an unlink... */
730                 urb->context = dev;
731                 urb->dev = devpriv->usbdev;
732                 urb->status = 0;
733                 urb->transfer_flags = URB_ISO_ASAP;
734
735                 ret = usb_submit_urb(urb, GFP_ATOMIC);
736                 if (ret)
737                         return ret;
738         }
739         return 0;
740 }
741
742 static int usbdux_ai_cmdtest(struct comedi_device *dev,
743                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
744 {
745         struct usbdux_private *this_usbduxsub = dev->private;
746         int err = 0, i;
747         unsigned int tmp_timer;
748
749         /* Step 1 : check if triggers are trivially valid */
750
751         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
752         err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
753         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
754         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
755         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
756
757         if (err)
758                 return 1;
759
760         /* Step 2a : make sure trigger sources are unique */
761
762         err |= cfc_check_trigger_is_unique(cmd->start_src);
763         err |= cfc_check_trigger_is_unique(cmd->stop_src);
764
765         /* Step 2b : and mutually compatible */
766
767         if (err)
768                 return 2;
769
770         /* Step 3: check if arguments are trivially valid */
771
772         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
773
774         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
775                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
776
777         if (cmd->scan_begin_src == TRIG_TIMER) {
778                 if (this_usbduxsub->high_speed) {
779                         /*
780                          * In high speed mode microframes are possible.
781                          * However, during one microframe we can roughly
782                          * sample one channel. Thus, the more channels
783                          * are in the channel list the more time we need.
784                          */
785                         i = 1;
786                         /* find a power of 2 for the number of channels */
787                         while (i < (cmd->chanlist_len))
788                                 i = i * 2;
789
790                         err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
791                                                          1000000 / 8 * i);
792                         /* now calc the real sampling rate with all the
793                          * rounding errors */
794                         tmp_timer =
795                             ((unsigned int)(cmd->scan_begin_arg / 125000)) *
796                             125000;
797                 } else {
798                         /* full speed */
799                         /* 1kHz scans every USB frame */
800                         err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
801                                                          1000000);
802                         /*
803                          * calc the real sampling rate with the rounding errors
804                          */
805                         tmp_timer = ((unsigned int)(cmd->scan_begin_arg /
806                                                    1000000)) * 1000000;
807                 }
808                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg,
809                                                 tmp_timer);
810         }
811
812         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
813
814         if (cmd->stop_src == TRIG_COUNT) {
815                 /* any count is allowed */
816         } else {
817                 /* TRIG_NONE */
818                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
819         }
820
821         if (err)
822                 return 3;
823
824         return 0;
825 }
826
827 /*
828  * creates the ADC command for the MAX1271
829  * range is the range value from comedi
830  */
831 static int8_t create_adc_command(unsigned int chan, int range)
832 {
833         int8_t p = (range <= 1);
834         int8_t r = ((range % 2) == 0);
835         return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
836 }
837
838 /* bulk transfers to usbdux */
839
840 #define SENDADCOMMANDS            0
841 #define SENDDACOMMANDS            1
842 #define SENDDIOCONFIGCOMMAND      2
843 #define SENDDIOBITSCOMMAND        3
844 #define SENDSINGLEAD              4
845 #define READCOUNTERCOMMAND        5
846 #define WRITECOUNTERCOMMAND       6
847 #define SENDPWMON                 7
848 #define SENDPWMOFF                8
849
850 static int send_dux_commands(struct comedi_device *dev, int cmd_type)
851 {
852         struct usbdux_private *devpriv = dev->private;
853         struct usb_device *usb = devpriv->usbdev;
854         int nsent;
855
856         devpriv->dux_commands[0] = cmd_type;
857
858         return usb_bulk_msg(usb, usb_sndbulkpipe(usb, COMMAND_OUT_EP),
859                             devpriv->dux_commands, SIZEOFDUXBUFFER,
860                             &nsent, BULK_TIMEOUT);
861 }
862
863 static int receive_dux_commands(struct comedi_device *dev, int command)
864 {
865         struct usbdux_private *devpriv = dev->private;
866         struct usb_device *usb = devpriv->usbdev;
867         int ret;
868         int nrec;
869         int i;
870
871         for (i = 0; i < RETRIES; i++) {
872                 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, COMMAND_IN_EP),
873                                       devpriv->insn_buffer, SIZEINSNBUF,
874                                       &nrec, BULK_TIMEOUT);
875                 if (ret < 0)
876                         return ret;
877                 if (le16_to_cpu(devpriv->insn_buffer[0]) == command)
878                         return ret;
879         }
880         /* command not received */
881         return -EFAULT;
882 }
883
884 static int usbdux_ai_inttrig(struct comedi_device *dev,
885                              struct comedi_subdevice *s, unsigned int trignum)
886 {
887         int ret;
888         struct usbdux_private *this_usbduxsub = dev->private;
889         if (!this_usbduxsub)
890                 return -EFAULT;
891
892         down(&this_usbduxsub->sem);
893
894         if (trignum != 0) {
895                 up(&this_usbduxsub->sem);
896                 return -EINVAL;
897         }
898         if (!(this_usbduxsub->ai_cmd_running)) {
899                 this_usbduxsub->ai_cmd_running = 1;
900                 ret = usbduxsub_submit_inurbs(dev);
901                 if (ret < 0) {
902                         this_usbduxsub->ai_cmd_running = 0;
903                         up(&this_usbduxsub->sem);
904                         return ret;
905                 }
906                 s->async->inttrig = NULL;
907         }
908         up(&this_usbduxsub->sem);
909         return 1;
910 }
911
912 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
913 {
914         struct comedi_cmd *cmd = &s->async->cmd;
915         unsigned int chan, range;
916         int i, ret;
917         struct usbdux_private *this_usbduxsub = dev->private;
918         int result;
919
920         if (!this_usbduxsub)
921                 return -EFAULT;
922
923         /* block other CPUs from starting an ai_cmd */
924         down(&this_usbduxsub->sem);
925         if (this_usbduxsub->ai_cmd_running) {
926                 up(&this_usbduxsub->sem);
927                 return -EBUSY;
928         }
929         /* set current channel of the running acquisition to zero */
930         s->async->cur_chan = 0;
931
932         this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
933         for (i = 0; i < cmd->chanlist_len; ++i) {
934                 chan = CR_CHAN(cmd->chanlist[i]);
935                 range = CR_RANGE(cmd->chanlist[i]);
936                 if (i >= NUMCHANNELS)
937                         break;
938                 this_usbduxsub->dux_commands[i + 2] =
939                     create_adc_command(chan, range);
940         }
941
942         result = send_dux_commands(dev, SENDADCOMMANDS);
943         if (result < 0) {
944                 up(&this_usbduxsub->sem);
945                 return result;
946         }
947
948         if (this_usbduxsub->high_speed) {
949                 /*
950                  * every channel gets a time window of 125us. Thus, if we
951                  * sample all 8 channels we need 1ms. If we sample only one
952                  * channel we need only 125us
953                  */
954                 this_usbduxsub->ai_interval = 1;
955                 /* find a power of 2 for the interval */
956                 while ((this_usbduxsub->ai_interval) < (cmd->chanlist_len)) {
957                         this_usbduxsub->ai_interval =
958                             (this_usbduxsub->ai_interval) * 2;
959                 }
960                 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
961                                                           (this_usbduxsub->
962                                                            ai_interval));
963         } else {
964                 /* interval always 1ms */
965                 this_usbduxsub->ai_interval = 1;
966                 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
967         }
968         if (this_usbduxsub->ai_timer < 1) {
969                 up(&this_usbduxsub->sem);
970                 return -EINVAL;
971         }
972         this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
973
974         if (cmd->stop_src == TRIG_COUNT) {
975                 /* data arrives as one packet */
976                 this_usbduxsub->ai_sample_count = cmd->stop_arg;
977                 this_usbduxsub->ai_continous = 0;
978         } else {
979                 /* continous acquisition */
980                 this_usbduxsub->ai_continous = 1;
981                 this_usbduxsub->ai_sample_count = 0;
982         }
983
984         if (cmd->start_src == TRIG_NOW) {
985                 /* enable this acquisition operation */
986                 this_usbduxsub->ai_cmd_running = 1;
987                 ret = usbduxsub_submit_inurbs(dev);
988                 if (ret < 0) {
989                         this_usbduxsub->ai_cmd_running = 0;
990                         /* fixme: unlink here?? */
991                         up(&this_usbduxsub->sem);
992                         return ret;
993                 }
994                 s->async->inttrig = NULL;
995         } else {
996                 /* TRIG_INT */
997                 /* don't enable the acquision operation */
998                 /* wait for an internal signal */
999                 s->async->inttrig = usbdux_ai_inttrig;
1000         }
1001         up(&this_usbduxsub->sem);
1002         return 0;
1003 }
1004
1005 /* Mode 0 is used to get a single conversion on demand */
1006 static int usbdux_ai_insn_read(struct comedi_device *dev,
1007                                struct comedi_subdevice *s,
1008                                struct comedi_insn *insn, unsigned int *data)
1009 {
1010         int i;
1011         unsigned int one = 0;
1012         int chan, range;
1013         int err;
1014         struct usbdux_private *this_usbduxsub = dev->private;
1015
1016         if (!this_usbduxsub)
1017                 return 0;
1018
1019         down(&this_usbduxsub->sem);
1020         if (this_usbduxsub->ai_cmd_running) {
1021                 up(&this_usbduxsub->sem);
1022                 return 0;
1023         }
1024
1025         /* sample one channel */
1026         chan = CR_CHAN(insn->chanspec);
1027         range = CR_RANGE(insn->chanspec);
1028         /* set command for the first channel */
1029         this_usbduxsub->dux_commands[1] = create_adc_command(chan, range);
1030
1031         /* adc commands */
1032         err = send_dux_commands(dev, SENDSINGLEAD);
1033         if (err < 0) {
1034                 up(&this_usbduxsub->sem);
1035                 return err;
1036         }
1037
1038         for (i = 0; i < insn->n; i++) {
1039                 err = receive_dux_commands(dev, SENDSINGLEAD);
1040                 if (err < 0) {
1041                         up(&this_usbduxsub->sem);
1042                         return 0;
1043                 }
1044                 one = le16_to_cpu(this_usbduxsub->insn_buffer[1]);
1045                 if (CR_RANGE(insn->chanspec) <= 1)
1046                         one = one ^ 0x800;
1047
1048                 data[i] = one;
1049         }
1050         up(&this_usbduxsub->sem);
1051         return i;
1052 }
1053
1054 /************************************/
1055 /* analog out */
1056
1057 static int usbdux_ao_insn_read(struct comedi_device *dev,
1058                                struct comedi_subdevice *s,
1059                                struct comedi_insn *insn, unsigned int *data)
1060 {
1061         int i;
1062         int chan = CR_CHAN(insn->chanspec);
1063         struct usbdux_private *this_usbduxsub = dev->private;
1064
1065         if (!this_usbduxsub)
1066                 return -EFAULT;
1067
1068         down(&this_usbduxsub->sem);
1069         for (i = 0; i < insn->n; i++)
1070                 data[i] = this_usbduxsub->out_buffer[chan];
1071
1072         up(&this_usbduxsub->sem);
1073         return i;
1074 }
1075
1076 static int usbdux_ao_insn_write(struct comedi_device *dev,
1077                                 struct comedi_subdevice *s,
1078                                 struct comedi_insn *insn, unsigned int *data)
1079 {
1080         int i, err;
1081         int chan = CR_CHAN(insn->chanspec);
1082         struct usbdux_private *this_usbduxsub = dev->private;
1083
1084         if (!this_usbduxsub)
1085                 return -EFAULT;
1086
1087         down(&this_usbduxsub->sem);
1088         if (this_usbduxsub->ao_cmd_running) {
1089                 up(&this_usbduxsub->sem);
1090                 return 0;
1091         }
1092
1093         for (i = 0; i < insn->n; i++) {
1094                 /* number of channels: 1 */
1095                 this_usbduxsub->dux_commands[1] = 1;
1096                 /* one 16 bit value */
1097                 *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
1098                     cpu_to_le16(data[i]);
1099                 this_usbduxsub->out_buffer[chan] = data[i];
1100                 /* channel number */
1101                 this_usbduxsub->dux_commands[4] = (chan << 6);
1102                 err = send_dux_commands(dev, SENDDACOMMANDS);
1103                 if (err < 0) {
1104                         up(&this_usbduxsub->sem);
1105                         return err;
1106                 }
1107         }
1108         up(&this_usbduxsub->sem);
1109
1110         return i;
1111 }
1112
1113 static int usbdux_ao_inttrig(struct comedi_device *dev,
1114                              struct comedi_subdevice *s, unsigned int trignum)
1115 {
1116         int ret;
1117         struct usbdux_private *this_usbduxsub = dev->private;
1118
1119         if (!this_usbduxsub)
1120                 return -EFAULT;
1121
1122         down(&this_usbduxsub->sem);
1123         if (trignum != 0) {
1124                 up(&this_usbduxsub->sem);
1125                 return -EINVAL;
1126         }
1127         if (!(this_usbduxsub->ao_cmd_running)) {
1128                 this_usbduxsub->ao_cmd_running = 1;
1129                 ret = usbduxsub_submit_outurbs(dev);
1130                 if (ret < 0) {
1131                         this_usbduxsub->ao_cmd_running = 0;
1132                         up(&this_usbduxsub->sem);
1133                         return ret;
1134                 }
1135                 s->async->inttrig = NULL;
1136         }
1137         up(&this_usbduxsub->sem);
1138         return 1;
1139 }
1140
1141 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1142                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
1143 {
1144         struct usbdux_private *this_usbduxsub = dev->private;
1145         int err = 0;
1146         unsigned int flags;
1147
1148         if (!this_usbduxsub)
1149                 return -EFAULT;
1150
1151         /* Step 1 : check if triggers are trivially valid */
1152
1153         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
1154
1155         if (0) {                /* (this_usbduxsub->high_speed) */
1156                 /* the sampling rate is set by the coversion rate */
1157                 flags = TRIG_FOLLOW;
1158         } else {
1159                 /* start a new scan (output at once) with a timer */
1160                 flags = TRIG_TIMER;
1161         }
1162         err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
1163
1164         if (0) {                /* (this_usbduxsub->high_speed) */
1165                 /*
1166                  * in usb-2.0 only one conversion it transmitted
1167                  * but with 8kHz/n
1168                  */
1169                 flags = TRIG_TIMER;
1170         } else {
1171                 /*
1172                  * all conversion events happen simultaneously with
1173                  * a rate of 1kHz/n
1174                  */
1175                 flags = TRIG_NOW;
1176         }
1177         err |= cfc_check_trigger_src(&cmd->convert_src, flags);
1178
1179         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
1180         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
1181
1182         if (err)
1183                 return 1;
1184
1185         /* Step 2a : make sure trigger sources are unique */
1186
1187         err |= cfc_check_trigger_is_unique(cmd->start_src);
1188         err |= cfc_check_trigger_is_unique(cmd->stop_src);
1189
1190         /* Step 2b : and mutually compatible */
1191
1192         if (err)
1193                 return 2;
1194
1195         /* Step 3: check if arguments are trivially valid */
1196
1197         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
1198
1199         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
1200                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
1201
1202         if (cmd->scan_begin_src == TRIG_TIMER)
1203                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
1204                                                  1000000);
1205
1206         /* not used now, is for later use */
1207         if (cmd->convert_src == TRIG_TIMER)
1208                 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
1209
1210         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
1211
1212         if (cmd->stop_src == TRIG_COUNT) {
1213                 /* any count is allowed */
1214         } else {
1215                 /* TRIG_NONE */
1216                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1217         }
1218
1219         if (err)
1220                 return 3;
1221
1222         return 0;
1223 }
1224
1225 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1226 {
1227         struct comedi_cmd *cmd = &s->async->cmd;
1228         unsigned int chan, gain;
1229         int i, ret;
1230         struct usbdux_private *this_usbduxsub = dev->private;
1231
1232         if (!this_usbduxsub)
1233                 return -EFAULT;
1234
1235         down(&this_usbduxsub->sem);
1236
1237         /* set current channel of the running acquisition to zero */
1238         s->async->cur_chan = 0;
1239         for (i = 0; i < cmd->chanlist_len; ++i) {
1240                 chan = CR_CHAN(cmd->chanlist[i]);
1241                 gain = CR_RANGE(cmd->chanlist[i]);
1242                 if (i >= NUMOUTCHANNELS)
1243                         break;
1244                 this_usbduxsub->dac_commands[i] = (chan << 6);
1245         }
1246
1247         /* we count in steps of 1ms (125us) */
1248         /* 125us mode not used yet */
1249         if (0) {                /* (this_usbduxsub->high_speed) */
1250                 /* 125us */
1251                 /* timing of the conversion itself: every 125 us */
1252                 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1253         } else {
1254                 /* 1ms */
1255                 /* timing of the scan: we get all channels at once */
1256                 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1257                 if (this_usbduxsub->ao_timer < 1) {
1258                         up(&this_usbduxsub->sem);
1259                         return -EINVAL;
1260                 }
1261         }
1262         this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1263
1264         if (cmd->stop_src == TRIG_COUNT) {
1265                 /* not continuous */
1266                 /* counter */
1267                 /* high speed also scans everything at once */
1268                 if (0) {        /* (this_usbduxsub->high_speed) */
1269                         this_usbduxsub->ao_sample_count =
1270                             (cmd->stop_arg) * (cmd->scan_end_arg);
1271                 } else {
1272                         /* there's no scan as the scan has been */
1273                         /* perf inside the FX2 */
1274                         /* data arrives as one packet */
1275                         this_usbduxsub->ao_sample_count = cmd->stop_arg;
1276                 }
1277                 this_usbduxsub->ao_continous = 0;
1278         } else {
1279                 /* continous acquisition */
1280                 this_usbduxsub->ao_continous = 1;
1281                 this_usbduxsub->ao_sample_count = 0;
1282         }
1283
1284         if (cmd->start_src == TRIG_NOW) {
1285                 /* enable this acquisition operation */
1286                 this_usbduxsub->ao_cmd_running = 1;
1287                 ret = usbduxsub_submit_outurbs(dev);
1288                 if (ret < 0) {
1289                         this_usbduxsub->ao_cmd_running = 0;
1290                         /* fixme: unlink here?? */
1291                         up(&this_usbduxsub->sem);
1292                         return ret;
1293                 }
1294                 s->async->inttrig = NULL;
1295         } else {
1296                 /* TRIG_INT */
1297                 /* submit the urbs later */
1298                 /* wait for an internal signal */
1299                 s->async->inttrig = usbdux_ao_inttrig;
1300         }
1301
1302         up(&this_usbduxsub->sem);
1303         return 0;
1304 }
1305
1306 static int usbdux_dio_insn_config(struct comedi_device *dev,
1307                                   struct comedi_subdevice *s,
1308                                   struct comedi_insn *insn, unsigned int *data)
1309 {
1310         int chan = CR_CHAN(insn->chanspec);
1311
1312         /* The input or output configuration of each digital line is
1313          * configured by a special insn_config instruction.  chanspec
1314          * contains the channel to be changed, and data[0] contains the
1315          * value COMEDI_INPUT or COMEDI_OUTPUT. */
1316
1317         switch (data[0]) {
1318         case INSN_CONFIG_DIO_OUTPUT:
1319                 s->io_bits |= 1 << chan;        /* 1 means Out */
1320                 break;
1321         case INSN_CONFIG_DIO_INPUT:
1322                 s->io_bits &= ~(1 << chan);
1323                 break;
1324         case INSN_CONFIG_DIO_QUERY:
1325                 data[1] =
1326                     (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1327                 break;
1328         default:
1329                 return -EINVAL;
1330                 break;
1331         }
1332         /* we don't tell the firmware here as it would take 8 frames */
1333         /* to submit the information. We do it in the insn_bits. */
1334         return insn->n;
1335 }
1336
1337 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1338                                 struct comedi_subdevice *s,
1339                                 struct comedi_insn *insn, unsigned int *data)
1340 {
1341
1342         struct usbdux_private *this_usbduxsub = dev->private;
1343         int err;
1344
1345         if (!this_usbduxsub)
1346                 return -EFAULT;
1347
1348         down(&this_usbduxsub->sem);
1349
1350         /* The insn data is a mask in data[0] and the new data
1351          * in data[1], each channel cooresponding to a bit. */
1352         s->state &= ~data[0];
1353         s->state |= data[0] & data[1];
1354         this_usbduxsub->dux_commands[1] = s->io_bits;
1355         this_usbduxsub->dux_commands[2] = s->state;
1356
1357         /* This command also tells the firmware to return */
1358         /* the digital input lines */
1359         err = send_dux_commands(dev, SENDDIOBITSCOMMAND);
1360         if (err < 0) {
1361                 up(&this_usbduxsub->sem);
1362                 return err;
1363         }
1364         err = receive_dux_commands(dev, SENDDIOBITSCOMMAND);
1365         if (err < 0) {
1366                 up(&this_usbduxsub->sem);
1367                 return err;
1368         }
1369
1370         data[1] = le16_to_cpu(this_usbduxsub->insn_buffer[1]);
1371         up(&this_usbduxsub->sem);
1372         return insn->n;
1373 }
1374
1375 /* reads the 4 counters, only two are used just now */
1376 static int usbdux_counter_read(struct comedi_device *dev,
1377                                struct comedi_subdevice *s,
1378                                struct comedi_insn *insn, unsigned int *data)
1379 {
1380         struct usbdux_private *this_usbduxsub = dev->private;
1381         int chan = insn->chanspec;
1382         int err;
1383
1384         if (!this_usbduxsub)
1385                 return -EFAULT;
1386
1387         down(&this_usbduxsub->sem);
1388         err = send_dux_commands(dev, READCOUNTERCOMMAND);
1389         if (err < 0) {
1390                 up(&this_usbduxsub->sem);
1391                 return err;
1392         }
1393
1394         err = receive_dux_commands(dev, READCOUNTERCOMMAND);
1395         if (err < 0) {
1396                 up(&this_usbduxsub->sem);
1397                 return err;
1398         }
1399
1400         data[0] = le16_to_cpu(this_usbduxsub->insn_buffer[chan + 1]);
1401         up(&this_usbduxsub->sem);
1402         return 1;
1403 }
1404
1405 static int usbdux_counter_write(struct comedi_device *dev,
1406                                 struct comedi_subdevice *s,
1407                                 struct comedi_insn *insn, unsigned int *data)
1408 {
1409         struct usbdux_private *this_usbduxsub = dev->private;
1410         int err;
1411
1412         if (!this_usbduxsub)
1413                 return -EFAULT;
1414
1415         down(&this_usbduxsub->sem);
1416         this_usbduxsub->dux_commands[1] = insn->chanspec;
1417         *((int16_t *) (this_usbduxsub->dux_commands + 2)) = cpu_to_le16(*data);
1418
1419         err = send_dux_commands(dev, WRITECOUNTERCOMMAND);
1420         if (err < 0) {
1421                 up(&this_usbduxsub->sem);
1422                 return err;
1423         }
1424
1425         up(&this_usbduxsub->sem);
1426
1427         return 1;
1428 }
1429
1430 static int usbdux_counter_config(struct comedi_device *dev,
1431                                  struct comedi_subdevice *s,
1432                                  struct comedi_insn *insn, unsigned int *data)
1433 {
1434         /* nothing to do so far */
1435         return 2;
1436 }
1437
1438 /***********************************/
1439 /* PWM */
1440
1441 static int usbduxsub_unlink_pwm_urbs(struct usbdux_private *usbduxsub_tmp)
1442 {
1443         int err = 0;
1444
1445         if (usbduxsub_tmp && usbduxsub_tmp->urb_pwm) {
1446                 if (usbduxsub_tmp->urb_pwm)
1447                         usb_kill_urb(usbduxsub_tmp->urb_pwm);
1448         }
1449         return err;
1450 }
1451
1452 /* This cancels a running acquisition operation
1453  * in any context.
1454  */
1455 static int usbdux_pwm_stop(struct usbdux_private *this_usbduxsub, int do_unlink)
1456 {
1457         int ret = 0;
1458
1459         if (!this_usbduxsub)
1460                 return -EFAULT;
1461
1462         if (do_unlink)
1463                 ret = usbduxsub_unlink_pwm_urbs(this_usbduxsub);
1464
1465         this_usbduxsub->pwm_cmd_running = 0;
1466
1467         return ret;
1468 }
1469
1470 /* force unlink - is called by comedi */
1471 static int usbdux_pwm_cancel(struct comedi_device *dev,
1472                              struct comedi_subdevice *s)
1473 {
1474         struct usbdux_private *this_usbduxsub = dev->private;
1475         int res = 0;
1476
1477         /* unlink only if it is really running */
1478         res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1479
1480         return send_dux_commands(dev, SENDPWMOFF);
1481 }
1482
1483 static void usbduxsub_pwm_irq(struct urb *urb)
1484 {
1485         struct comedi_device *dev = urb->context;
1486         struct usbdux_private *devpriv = dev->private;
1487         int ret;
1488
1489         switch (urb->status) {
1490         case 0:
1491                 /* success */
1492                 break;
1493
1494         case -ECONNRESET:
1495         case -ENOENT:
1496         case -ESHUTDOWN:
1497         case -ECONNABORTED:
1498                 /*
1499                  * after an unlink command, unplug, ... etc
1500                  * no unlink needed here. Already shutting down.
1501                  */
1502                 if (devpriv->pwm_cmd_running)
1503                         usbdux_pwm_stop(devpriv, 0);
1504
1505                 return;
1506
1507         default:
1508                 /* a real error */
1509                 if (devpriv->pwm_cmd_running) {
1510                         dev_err(dev->class_dev,
1511                                 "Non-zero urb status received in pwm intr context: %d\n",
1512                                 urb->status);
1513                         usbdux_pwm_stop(devpriv, 0);
1514                 }
1515                 return;
1516         }
1517
1518         /* are we actually running? */
1519         if (!devpriv->pwm_cmd_running)
1520                 return;
1521
1522         urb->transfer_buffer_length = devpriv->size_pwm_buf;
1523         urb->dev = devpriv->usbdev;
1524         urb->status = 0;
1525         if (devpriv->pwm_cmd_running) {
1526                 ret = usb_submit_urb(urb, GFP_ATOMIC);
1527                 if (ret < 0) {
1528                         dev_err(dev->class_dev,
1529                                 "pwm urb resubm failed in int-cont. ret=%d",
1530                                 ret);
1531                         if (ret == EL2NSYNC)
1532                                 dev_err(dev->class_dev,
1533                                         "buggy USB host controller or bug in IRQ handling!\n");
1534
1535                         /* don't do an unlink here */
1536                         usbdux_pwm_stop(devpriv, 0);
1537                 }
1538         }
1539 }
1540
1541 static int usbduxsub_submit_pwm_urbs(struct comedi_device *dev)
1542 {
1543         struct usbdux_private *devpriv = dev->private;
1544         struct urb *urb = devpriv->urb_pwm;
1545
1546         /* in case of a resubmission after an unlink... */
1547         usb_fill_bulk_urb(urb, devpriv->usbdev,
1548                           usb_sndbulkpipe(devpriv->usbdev, PWM_EP),
1549                           urb->transfer_buffer,
1550                           devpriv->size_pwm_buf,
1551                           usbduxsub_pwm_irq,
1552                           dev);
1553
1554         return usb_submit_urb(urb, GFP_ATOMIC);
1555 }
1556
1557 static int usbdux_pwm_period(struct comedi_device *dev,
1558                              struct comedi_subdevice *s, unsigned int period)
1559 {
1560         struct usbdux_private *this_usbduxsub = dev->private;
1561         int fx2delay = 255;
1562
1563         if (period < MIN_PWM_PERIOD) {
1564                 return -EAGAIN;
1565         } else {
1566                 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1567                 if (fx2delay > 255)
1568                         return -EAGAIN;
1569         }
1570         this_usbduxsub->pwn_delay = fx2delay;
1571         this_usbduxsub->pwm_period = period;
1572
1573         return 0;
1574 }
1575
1576 /* is called from insn so there's no need to do all the sanity checks */
1577 static int usbdux_pwm_start(struct comedi_device *dev,
1578                             struct comedi_subdevice *s)
1579 {
1580         int ret, i;
1581         struct usbdux_private *this_usbduxsub = dev->private;
1582
1583         if (this_usbduxsub->pwm_cmd_running) {
1584                 /* already running */
1585                 return 0;
1586         }
1587
1588         this_usbduxsub->dux_commands[1] = ((int8_t) this_usbduxsub->pwn_delay);
1589         ret = send_dux_commands(dev, SENDPWMON);
1590         if (ret < 0)
1591                 return ret;
1592
1593         /* initialise the buffer */
1594         for (i = 0; i < this_usbduxsub->size_pwm_buf; i++)
1595                 ((char *)(this_usbduxsub->urb_pwm->transfer_buffer))[i] = 0;
1596
1597         this_usbduxsub->pwm_cmd_running = 1;
1598         ret = usbduxsub_submit_pwm_urbs(dev);
1599         if (ret < 0) {
1600                 this_usbduxsub->pwm_cmd_running = 0;
1601                 return ret;
1602         }
1603         return 0;
1604 }
1605
1606 /* generates the bit pattern for PWM with the optional sign bit */
1607 static int usbdux_pwm_pattern(struct comedi_device *dev,
1608                               struct comedi_subdevice *s, int channel,
1609                               unsigned int value, unsigned int sign)
1610 {
1611         struct usbdux_private *this_usbduxsub = dev->private;
1612         int i, szbuf;
1613         char *p_buf;
1614         char pwm_mask;
1615         char sgn_mask;
1616         char c;
1617
1618         if (!this_usbduxsub)
1619                 return -EFAULT;
1620
1621         /* this is the DIO bit which carries the PWM data */
1622         pwm_mask = (1 << channel);
1623         /* this is the DIO bit which carries the optional direction bit */
1624         sgn_mask = (16 << channel);
1625         /* this is the buffer which will be filled with the with bit */
1626         /* pattern for one period */
1627         szbuf = this_usbduxsub->size_pwm_buf;
1628         p_buf = (char *)(this_usbduxsub->urb_pwm->transfer_buffer);
1629         for (i = 0; i < szbuf; i++) {
1630                 c = *p_buf;
1631                 /* reset bits */
1632                 c = c & (~pwm_mask);
1633                 /* set the bit as long as the index is lower than the value */
1634                 if (i < value)
1635                         c = c | pwm_mask;
1636                 /* set the optional sign bit for a relay */
1637                 if (!sign) {
1638                         /* positive value */
1639                         c = c & (~sgn_mask);
1640                 } else {
1641                         /* negative value */
1642                         c = c | sgn_mask;
1643                 }
1644                 *(p_buf++) = c;
1645         }
1646         return 1;
1647 }
1648
1649 static int usbdux_pwm_write(struct comedi_device *dev,
1650                             struct comedi_subdevice *s,
1651                             struct comedi_insn *insn, unsigned int *data)
1652 {
1653         struct usbdux_private *this_usbduxsub = dev->private;
1654
1655         if (!this_usbduxsub)
1656                 return -EFAULT;
1657
1658         if ((insn->n) != 1) {
1659                 /*
1660                  * doesn't make sense to have more than one value here because
1661                  * it would just overwrite the PWM buffer a couple of times
1662                  */
1663                 return -EINVAL;
1664         }
1665
1666         /*
1667          * the sign is set via a special INSN only, this gives us 8 bits for
1668          * normal operation
1669          * relay sign 0 by default
1670          */
1671         return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
1672 }
1673
1674 static int usbdux_pwm_read(struct comedi_device *x1,
1675                            struct comedi_subdevice *x2, struct comedi_insn *x3,
1676                            unsigned int *x4)
1677 {
1678         /* not needed */
1679         return -EINVAL;
1680 };
1681
1682 /* switches on/off PWM */
1683 static int usbdux_pwm_config(struct comedi_device *dev,
1684                              struct comedi_subdevice *s,
1685                              struct comedi_insn *insn, unsigned int *data)
1686 {
1687         struct usbdux_private *this_usbduxsub = dev->private;
1688         switch (data[0]) {
1689         case INSN_CONFIG_ARM:
1690                 /* switch it on */
1691                 /*
1692                  * if not zero the PWM is limited to a certain time which is
1693                  * not supported here
1694                  */
1695                 if (data[1] != 0)
1696                         return -EINVAL;
1697                 return usbdux_pwm_start(dev, s);
1698         case INSN_CONFIG_DISARM:
1699                 return usbdux_pwm_cancel(dev, s);
1700         case INSN_CONFIG_GET_PWM_STATUS:
1701                 /*
1702                  * to check if the USB transmission has failed or in case PWM
1703                  * was limited to n cycles to check if it has terminated
1704                  */
1705                 data[1] = this_usbduxsub->pwm_cmd_running;
1706                 return 0;
1707         case INSN_CONFIG_PWM_SET_PERIOD:
1708                 return usbdux_pwm_period(dev, s, data[1]);
1709         case INSN_CONFIG_PWM_GET_PERIOD:
1710                 data[1] = this_usbduxsub->pwm_period;
1711                 return 0;
1712         case INSN_CONFIG_PWM_SET_H_BRIDGE:
1713                 /* value in the first byte and the sign in the second for a
1714                    relay */
1715                 return usbdux_pwm_pattern(dev, s,
1716                                           /* the channel number */
1717                                           CR_CHAN(insn->chanspec),
1718                                           /* actual PWM data */
1719                                           data[1],
1720                                           /* just a sign */
1721                                           (data[2] != 0));
1722         case INSN_CONFIG_PWM_GET_H_BRIDGE:
1723                 /* values are not kept in this driver, nothing to return here */
1724                 return -EINVAL;
1725         }
1726         return -EINVAL;
1727 }
1728
1729 /* end of PWM */
1730 /*****************************************************************/
1731
1732 static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
1733 {
1734         struct usbdux_private *devpriv = dev->private;
1735         struct urb *urb;
1736         int i;
1737
1738         /* create space for the commands of the DA converter */
1739         devpriv->dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
1740         if (!devpriv->dac_commands)
1741                 return -ENOMEM;
1742
1743         /* create space for the commands going to the usb device */
1744         devpriv->dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
1745         if (!devpriv->dux_commands)
1746                 return -ENOMEM;
1747
1748         /* create space for the in buffer and set it to zero */
1749         devpriv->in_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1750         if (!devpriv->in_buffer)
1751                 return -ENOMEM;
1752
1753         /* create space of the instruction buffer */
1754         devpriv->insn_buffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
1755         if (!devpriv->insn_buffer)
1756                 return -ENOMEM;
1757
1758         /* create space for the outbuffer */
1759         devpriv->out_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1760         if (!devpriv->out_buffer)
1761                 return -ENOMEM;
1762
1763         /* in urbs */
1764         devpriv->urb_in = kcalloc(devpriv->num_in_buffers, sizeof(*urb),
1765                                   GFP_KERNEL);
1766         if (!devpriv->urb_in)
1767                 return -ENOMEM;
1768
1769         for (i = 0; i < devpriv->num_in_buffers; i++) {
1770                 /* one frame: 1ms */
1771                 urb = usb_alloc_urb(1, GFP_KERNEL);
1772                 if (!urb)
1773                         return -ENOMEM;
1774                 devpriv->urb_in[i] = urb;
1775
1776                 urb->dev = devpriv->usbdev;
1777                 /* will be filled later with a pointer to the comedi-device */
1778                 /* and ONLY then the urb should be submitted */
1779                 urb->context = NULL;
1780                 urb->pipe = usb_rcvisocpipe(devpriv->usbdev, ISOINEP);
1781                 urb->transfer_flags = URB_ISO_ASAP;
1782                 urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1783                 if (!urb->transfer_buffer)
1784                         return -ENOMEM;
1785
1786                 urb->complete = usbduxsub_ai_isoc_irq;
1787                 urb->number_of_packets = 1;
1788                 urb->transfer_buffer_length = SIZEINBUF;
1789                 urb->iso_frame_desc[0].offset = 0;
1790                 urb->iso_frame_desc[0].length = SIZEINBUF;
1791         }
1792
1793         /* out urbs */
1794         devpriv->urb_out = kcalloc(devpriv->num_out_buffers, sizeof(*urb),
1795                                    GFP_KERNEL);
1796         if (!devpriv->urb_out)
1797                 return -ENOMEM;
1798
1799         for (i = 0; i < devpriv->num_out_buffers; i++) {
1800                 /* one frame: 1ms */
1801                 urb = usb_alloc_urb(1, GFP_KERNEL);
1802                 if (!urb)
1803                         return -ENOMEM;
1804                 devpriv->urb_out[i] = urb;
1805
1806                 urb->dev = devpriv->usbdev;
1807                 /* will be filled later with a pointer to the comedi-device */
1808                 /* and ONLY then the urb should be submitted */
1809                 urb->context = NULL;
1810                 urb->pipe = usb_sndisocpipe(devpriv->usbdev, ISOOUTEP);
1811                 urb->transfer_flags = URB_ISO_ASAP;
1812                 urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1813                 if (!urb->transfer_buffer)
1814                         return -ENOMEM;
1815
1816                 urb->complete = usbduxsub_ao_isoc_irq;
1817                 urb->number_of_packets = 1;
1818                 urb->transfer_buffer_length = SIZEOUTBUF;
1819                 urb->iso_frame_desc[0].offset = 0;
1820                 urb->iso_frame_desc[0].length = SIZEOUTBUF;
1821                 if (devpriv->high_speed)
1822                         urb->interval = 8;      /* uframes */
1823                 else
1824                         urb->interval = 1;      /* frames */
1825         }
1826
1827         /* pwm */
1828         if (devpriv->size_pwm_buf) {
1829                 urb = usb_alloc_urb(0, GFP_KERNEL);
1830                 if (!urb)
1831                         return -ENOMEM;
1832                 devpriv->urb_pwm = urb;
1833
1834                 /* max bulk ep size in high speed */
1835                 urb->transfer_buffer = kzalloc(devpriv->size_pwm_buf,
1836                                                GFP_KERNEL);
1837                 if (!urb->transfer_buffer)
1838                         return -ENOMEM;
1839         }
1840
1841         return 0;
1842 }
1843
1844 static void usbdux_free_usb_buffers(struct usbdux_private *devpriv)
1845 {
1846         struct urb *urb;
1847         int i;
1848
1849         urb = devpriv->urb_pwm;
1850         if (urb) {
1851                 kfree(urb->transfer_buffer);
1852                 usb_kill_urb(urb);
1853                 usb_free_urb(urb);
1854         }
1855         if (devpriv->urb_out) {
1856                 for (i = 0; i < devpriv->num_out_buffers; i++) {
1857                         urb = devpriv->urb_out[i];
1858                         if (urb) {
1859                                 kfree(urb->transfer_buffer);
1860                                 usb_kill_urb(urb);
1861                                 usb_free_urb(urb);
1862                         }
1863                 }
1864                 kfree(devpriv->urb_out);
1865         }
1866         if (devpriv->urb_in) {
1867                 for (i = 0; i < devpriv->num_in_buffers; i++) {
1868                         urb = devpriv->urb_in[i];
1869                         if (urb) {
1870                                 kfree(urb->transfer_buffer);
1871                                 usb_kill_urb(urb);
1872                                 usb_free_urb(urb);
1873                         }
1874                 }
1875                 kfree(devpriv->urb_in);
1876         }
1877         kfree(devpriv->out_buffer);
1878         kfree(devpriv->insn_buffer);
1879         kfree(devpriv->in_buffer);
1880         kfree(devpriv->dux_commands);
1881         kfree(devpriv->dac_commands);
1882 }
1883
1884 static int usbdux_auto_attach(struct comedi_device *dev,
1885                               unsigned long context_unused)
1886 {
1887         struct usb_interface *intf = comedi_to_usb_interface(dev);
1888         struct usb_device *usb = comedi_to_usb_dev(dev);
1889         struct usbdux_private *devpriv;
1890         struct comedi_subdevice *s;
1891         int ret;
1892
1893         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1894         if (!devpriv)
1895                 return -ENOMEM;
1896
1897         sema_init(&devpriv->sem, 1);
1898
1899         devpriv->usbdev = usb;
1900         devpriv->ifnum = intf->altsetting->desc.bInterfaceNumber;
1901         usb_set_intfdata(intf, devpriv);
1902
1903         devpriv->high_speed = (devpriv->usbdev->speed == USB_SPEED_HIGH);
1904         if (devpriv->high_speed) {
1905                 devpriv->num_in_buffers = NUMOFINBUFFERSHIGH;
1906                 devpriv->num_out_buffers = NUMOFOUTBUFFERSHIGH;
1907                 devpriv->size_pwm_buf = 512;
1908         } else {
1909                 devpriv->num_in_buffers = NUMOFINBUFFERSFULL;
1910                 devpriv->num_out_buffers = NUMOFOUTBUFFERSFULL;
1911         }
1912
1913         ret = usbdux_alloc_usb_buffers(dev);
1914         if (ret)
1915                 return ret;
1916
1917         /* setting to alternate setting 3: enabling iso ep and bulk ep. */
1918         ret = usb_set_interface(devpriv->usbdev, devpriv->ifnum, 3);
1919         if (ret < 0) {
1920                 dev_err(dev->class_dev,
1921                         "could not set alternate setting 3 in high speed\n");
1922                 return ret;
1923         }
1924
1925         ret = comedi_load_firmware(dev, &usb->dev, FIRMWARE,
1926                                    usbdux_firmware_upload, 0);
1927         if (ret < 0)
1928                 return ret;
1929
1930         ret = comedi_alloc_subdevices(dev, (devpriv->high_speed) ? 5 : 4);
1931         if (ret)
1932                 return ret;
1933
1934         /* Analog Input subdevice */
1935         s = &dev->subdevices[0];
1936         dev->read_subdev = s;
1937         s->type         = COMEDI_SUBD_AI;
1938         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1939         s->n_chan       = 8;
1940         s->maxdata      = 0x0fff;
1941         s->len_chanlist = 8;
1942         s->range_table  = &range_usbdux_ai_range;
1943         s->insn_read    = usbdux_ai_insn_read;
1944         s->do_cmdtest   = usbdux_ai_cmdtest;
1945         s->do_cmd       = usbdux_ai_cmd;
1946         s->cancel       = usbdux_ai_cancel;
1947
1948         /* Analog Output subdevice */
1949         s = &dev->subdevices[1];
1950         dev->write_subdev = s;
1951         s->type         = COMEDI_SUBD_AO;
1952         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1953         s->n_chan       = 4;
1954         s->maxdata      = 0x0fff;
1955         s->len_chanlist = 4;
1956         s->range_table  = &range_usbdux_ao_range;
1957         s->do_cmdtest   = usbdux_ao_cmdtest;
1958         s->do_cmd       = usbdux_ao_cmd;
1959         s->cancel       = usbdux_ao_cancel;
1960         s->insn_read    = usbdux_ao_insn_read;
1961         s->insn_write   = usbdux_ao_insn_write;
1962
1963         /* Digital I/O subdevice */
1964         s = &dev->subdevices[2];
1965         s->type         = COMEDI_SUBD_DIO;
1966         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1967         s->n_chan       = 8;
1968         s->maxdata      = 1;
1969         s->range_table  = &range_digital;
1970         s->insn_bits    = usbdux_dio_insn_bits;
1971         s->insn_config  = usbdux_dio_insn_config;
1972
1973         /* Counter subdevice */
1974         s = &dev->subdevices[3];
1975         s->type         = COMEDI_SUBD_COUNTER;
1976         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
1977         s->n_chan       = 4;
1978         s->maxdata      = 0xffff;
1979         s->insn_read    = usbdux_counter_read;
1980         s->insn_write   = usbdux_counter_write;
1981         s->insn_config  = usbdux_counter_config;
1982
1983         if (devpriv->high_speed) {
1984                 /* PWM subdevice */
1985                 s = &dev->subdevices[4];
1986                 s->type         = COMEDI_SUBD_PWM;
1987                 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
1988                 s->n_chan       = 8;
1989                 s->maxdata      = devpriv->size_pwm_buf;
1990                 s->insn_write   = usbdux_pwm_write;
1991                 s->insn_read    = usbdux_pwm_read;
1992                 s->insn_config  = usbdux_pwm_config;
1993
1994                 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
1995         }
1996
1997         return 0;
1998 }
1999
2000 static void usbdux_detach(struct comedi_device *dev)
2001 {
2002         struct usb_interface *intf = comedi_to_usb_interface(dev);
2003         struct usbdux_private *devpriv = dev->private;
2004
2005         if (devpriv) {
2006                 down(&devpriv->sem);
2007
2008                 usb_set_intfdata(intf, NULL);
2009
2010                 if (devpriv->pwm_cmd_running)
2011                         usbduxsub_unlink_pwm_urbs(devpriv);
2012                 if (devpriv->ao_cmd_running)
2013                         usbduxsub_unlink_outurbs(devpriv);
2014                 if (devpriv->ai_cmd_running)
2015                         usbduxsub_unlink_inurbs(devpriv);
2016
2017                 usbdux_free_usb_buffers(devpriv);
2018
2019                 up(&devpriv->sem);
2020         }
2021 }
2022
2023 static struct comedi_driver usbdux_driver = {
2024         .driver_name    = "usbdux",
2025         .module         = THIS_MODULE,
2026         .auto_attach    = usbdux_auto_attach,
2027         .detach         = usbdux_detach,
2028 };
2029
2030 static int usbdux_usb_probe(struct usb_interface *intf,
2031                             const struct usb_device_id *id)
2032 {
2033         return comedi_usb_auto_config(intf, &usbdux_driver, 0);
2034 }
2035
2036 static const struct usb_device_id usbdux_usb_table[] = {
2037         { USB_DEVICE(0x13d8, 0x0001) },
2038         { USB_DEVICE(0x13d8, 0x0002) },
2039         { }
2040 };
2041 MODULE_DEVICE_TABLE(usb, usbdux_usb_table);
2042
2043 static struct usb_driver usbdux_usb_driver = {
2044         .name           = "usbdux",
2045         .probe          = usbdux_usb_probe,
2046         .disconnect     = comedi_usb_auto_unconfig,
2047         .id_table       = usbdux_usb_table,
2048 };
2049 module_comedi_usb_driver(usbdux_driver, usbdux_usb_driver);
2050
2051 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
2052 MODULE_DESCRIPTION("Stirling/ITL USB-DUX -- Bernd.Porr@f2s.com");
2053 MODULE_LICENSE("GPL");
2054 MODULE_FIRMWARE(FIRMWARE);