]> rtime.felk.cvut.cz Git - can-eth-gw-linux.git/blob - drivers/staging/comedi/drivers/usbdux.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[can-eth-gw-linux.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    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19  */
20 /*
21 Driver: usbdux
22 Description: University of Stirling USB DAQ & INCITE Technology Limited
23 Devices: [ITL] USB-DUX (usbdux.o)
24 Author: Bernd Porr <BerndPorr@f2s.com>
25 Updated: 8 Dec 2008
26 Status: Stable
27 Configuration options:
28   You have to upload firmware with the -i option. The
29   firmware is usually installed under /usr/share/usb or
30   /usr/local/share/usb or /lib/firmware.
31
32 Connection scheme for the counter at the digital port:
33   0=/CLK0, 1=UP/DOWN0, 2=RESET0, 4=/CLK1, 5=UP/DOWN1, 6=RESET1.
34   The sampling rate of the counter is approximately 500Hz.
35
36 Please note that under USB2.0 the length of the channel list determines
37 the max sampling rate. If you sample only one channel you get 8kHz
38 sampling rate. If you sample two channels you get 4kHz and so on.
39 */
40 /*
41  * I must give credit here to Chris Baugher who
42  * wrote the driver for AT-MIO-16d. I used some parts of this
43  * driver. I also must give credits to David Brownell
44  * who supported me with the USB development.
45  *
46  * Bernd Porr
47  *
48  *
49  * Revision history:
50  * 0.94: D/A output should work now with any channel list combinations
51  * 0.95: .owner commented out for kernel vers below 2.4.19
52  *       sanity checks in ai/ao_cmd
53  * 0.96: trying to get it working with 2.6, moved all memory alloc to comedi's
54  *       attach final USB IDs
55  *       moved memory allocation completely to the corresponding comedi
56  *       functions firmware upload is by fxload and no longer by comedi (due to
57  *       enumeration)
58  * 0.97: USB IDs received, adjusted table
59  * 0.98: SMP, locking, memroy alloc: moved all usb memory alloc
60  *       to the usb subsystem and moved all comedi related memory
61  *       alloc to comedi.
62  *       | kernel | registration | usbdux-usb | usbdux-comedi | comedi |
63  * 0.99: USB 2.0: changed protocol to isochronous transfer
64  *                IRQ transfer is too buggy and too risky in 2.0
65  *                for the high speed ISO transfer is now a working version
66  *                available
67  * 0.99b: Increased the iso transfer buffer for high sp.to 10 buffers. Some VIA
68  *        chipsets miss out IRQs. Deeper buffering is needed.
69  * 1.00: full USB 2.0 support for the A/D converter. Now: max 8kHz sampling
70  *       rate.
71  *       Firmware vers 1.00 is needed for this.
72  *       Two 16 bit up/down/reset counter with a sampling rate of 1kHz
73  *       And loads of cleaning up, in particular streamlining the
74  *       bulk transfers.
75  * 1.1:  moved EP4 transfers to EP1 to make space for a PWM output on EP4
76  * 1.2:  added PWM support via EP4
77  * 2.0:  PWM seems to be stable and is not interfering with the other functions
78  * 2.1:  changed PWM API
79  * 2.2:  added firmware kernel request to fix an udev problem
80  * 2.3:  corrected a bug in bulk timeouts which were far too short
81  * 2.4:  fixed a bug which causes the driver to hang when it ran out of data.
82  *       Thanks to Jan-Matthias Braun and Ian to spot the bug and fix it.
83  *
84  */
85
86 /* generates loads of debug info */
87 /* #define NOISY_DUX_DEBUGBUG */
88
89 #include <linux/kernel.h>
90 #include <linux/module.h>
91 #include <linux/init.h>
92 #include <linux/slab.h>
93 #include <linux/input.h>
94 #include <linux/usb.h>
95 #include <linux/fcntl.h>
96 #include <linux/compiler.h>
97 #include <linux/firmware.h>
98
99 #include "../comedidev.h"
100
101 #include "comedi_fc.h"
102
103 /* timeout for the USB-transfer in ms*/
104 #define BULK_TIMEOUT 1000
105
106 /* constants for "firmware" upload and download */
107 #define FIRMWARE "usbdux_firmware.bin"
108 #define USBDUXSUB_FIRMWARE 0xA0
109 #define VENDOR_DIR_IN  0xC0
110 #define VENDOR_DIR_OUT 0x40
111
112 /* internal addresses of the 8051 processor */
113 #define USBDUXSUB_CPUCS 0xE600
114
115 /*
116  * the minor device number, major is 180 only for debugging purposes and to
117  * upload special firmware (programming the eeprom etc) which is not compatible
118  * with the comedi framwork
119  */
120 #define USBDUXSUB_MINOR 32
121
122 /* max lenghth of the transfer-buffer for software upload */
123 #define TB_LEN 0x2000
124
125 /* Input endpoint number: ISO/IRQ */
126 #define ISOINEP           6
127
128 /* Output endpoint number: ISO/IRQ */
129 #define ISOOUTEP          2
130
131 /* This EP sends DUX commands to USBDUX */
132 #define COMMAND_OUT_EP     1
133
134 /* This EP receives the DUX commands from USBDUX */
135 #define COMMAND_IN_EP        8
136
137 /* Output endpoint for PWM */
138 #define PWM_EP         4
139
140 /* 300Hz max frequ under PWM */
141 #define MIN_PWM_PERIOD  ((long)(1E9/300))
142
143 /* Default PWM frequency */
144 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
145
146 /* Number of channels */
147 #define NUMCHANNELS       8
148
149 /* Size of one A/D value */
150 #define SIZEADIN          ((sizeof(int16_t)))
151
152 /*
153  * Size of the input-buffer IN BYTES
154  * Always multiple of 8 for 8 microframes which is needed in the highspeed mode
155  */
156 #define SIZEINBUF         ((8*SIZEADIN))
157
158 /* 16 bytes. */
159 #define SIZEINSNBUF       16
160
161 /* Number of DA channels */
162 #define NUMOUTCHANNELS    8
163
164 /* size of one value for the D/A converter: channel and value */
165 #define SIZEDAOUT          ((sizeof(int8_t)+sizeof(int16_t)))
166
167 /*
168  * Size of the output-buffer in bytes
169  * Actually only the first 4 triplets are used but for the
170  * high speed mode we need to pad it to 8 (microframes).
171  */
172 #define SIZEOUTBUF         ((8*SIZEDAOUT))
173
174 /*
175  * Size of the buffer for the dux commands: just now max size is determined
176  * by the analogue out + command byte + panic bytes...
177  */
178 #define SIZEOFDUXBUFFER    ((8*SIZEDAOUT+2))
179
180 /* Number of in-URBs which receive the data: min=2 */
181 #define NUMOFINBUFFERSFULL     5
182
183 /* Number of out-URBs which send the data: min=2 */
184 #define NUMOFOUTBUFFERSFULL    5
185
186 /* Number of in-URBs which receive the data: min=5 */
187 /* must have more buffers due to buggy USB ctr */
188 #define NUMOFINBUFFERSHIGH     10
189
190 /* Number of out-URBs which send the data: min=5 */
191 /* must have more buffers due to buggy USB ctr */
192 #define NUMOFOUTBUFFERSHIGH    10
193
194 /* Total number of usbdux devices */
195 #define NUMUSBDUX             16
196
197 /* Analogue in subdevice */
198 #define SUBDEV_AD             0
199
200 /* Analogue out subdevice */
201 #define SUBDEV_DA             1
202
203 /* Digital I/O */
204 #define SUBDEV_DIO            2
205
206 /* counter */
207 #define SUBDEV_COUNTER        3
208
209 /* timer aka pwm output */
210 #define SUBDEV_PWM            4
211
212 /* number of retries to get the right dux command */
213 #define RETRIES 10
214
215 /**************************************************/
216 /* comedi constants */
217 static const struct comedi_lrange range_usbdux_ai_range = { 4, {
218                                                                 BIP_RANGE
219                                                                 (4.096),
220                                                                 BIP_RANGE(4.096
221                                                                           / 2),
222                                                                 UNI_RANGE
223                                                                 (4.096),
224                                                                 UNI_RANGE(4.096
225                                                                           / 2)
226                                                                 }
227 };
228
229 static const struct comedi_lrange range_usbdux_ao_range = { 2, {
230                                                                 BIP_RANGE
231                                                                 (4.096),
232                                                                 UNI_RANGE
233                                                                 (4.096),
234                                                                 }
235 };
236
237 /*
238  * private structure of one subdevice
239  */
240
241 /*
242  * This is the structure which holds all the data of
243  * this driver one sub device just now: A/D
244  */
245 struct usbduxsub {
246         /* attached? */
247         int attached;
248         /* is it associated with a subdevice? */
249         int probed;
250         /* pointer to the usb-device */
251         struct usb_device *usbdev;
252         /* actual number of in-buffers */
253         int numOfInBuffers;
254         /* actual number of out-buffers */
255         int numOfOutBuffers;
256         /* ISO-transfer handling: buffers */
257         struct urb **urbIn;
258         struct urb **urbOut;
259         /* pwm-transfer handling */
260         struct urb *urbPwm;
261         /* PWM period */
262         unsigned int pwmPeriod;
263         /* PWM internal delay for the GPIF in the FX2 */
264         int8_t pwmDelay;
265         /* size of the PWM buffer which holds the bit pattern */
266         int sizePwmBuf;
267         /* input buffer for the ISO-transfer */
268         int16_t *inBuffer;
269         /* input buffer for single insn */
270         int16_t *insnBuffer;
271         /* output buffer for single DA outputs */
272         int16_t *outBuffer;
273         /* interface number */
274         int ifnum;
275         /* interface structure in 2.6 */
276         struct usb_interface *interface;
277         /* comedi device for the interrupt context */
278         struct comedi_device *comedidev;
279         /* is it USB_SPEED_HIGH or not? */
280         short int high_speed;
281         /* asynchronous command is running */
282         short int ai_cmd_running;
283         short int ao_cmd_running;
284         /* pwm is running */
285         short int pwm_cmd_running;
286         /* continous acquisition */
287         short int ai_continous;
288         short int ao_continous;
289         /* number of samples to acquire */
290         int ai_sample_count;
291         int ao_sample_count;
292         /* time between samples in units of the timer */
293         unsigned int ai_timer;
294         unsigned int ao_timer;
295         /* counter between aquisitions */
296         unsigned int ai_counter;
297         unsigned int ao_counter;
298         /* interval in frames/uframes */
299         unsigned int ai_interval;
300         /* D/A commands */
301         int8_t *dac_commands;
302         /* commands */
303         int8_t *dux_commands;
304         struct semaphore sem;
305 };
306
307 /*
308  * The pointer to the private usb-data of the driver is also the private data
309  * for the comedi-device.  This has to be global as the usb subsystem needs
310  * global variables. The other reason is that this structure must be there
311  * _before_ any comedi command is issued. The usb subsystem must be initialised
312  * before comedi can access it.
313  */
314 static struct usbduxsub usbduxsub[NUMUSBDUX];
315
316 static DEFINE_SEMAPHORE(start_stop_sem);
317
318 /*
319  * Stops the data acquision
320  * It should be safe to call this function from any context
321  */
322 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
323 {
324         int i = 0;
325         int err = 0;
326
327         if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
328                 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
329                         if (usbduxsub_tmp->urbIn[i]) {
330                                 /* We wait here until all transfers have been
331                                  * cancelled. */
332                                 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
333                         }
334                         dev_dbg(&usbduxsub_tmp->interface->dev,
335                                 "comedi: usbdux: unlinked InURB %d, err=%d\n",
336                                 i, err);
337                 }
338         }
339         return err;
340 }
341
342 /*
343  * This will stop a running acquisition operation
344  * Is called from within this driver from both the
345  * interrupt context and from comedi
346  */
347 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
348 {
349         int ret = 0;
350
351         if (!this_usbduxsub) {
352                 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
353                 return -EFAULT;
354         }
355         dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
356
357         if (do_unlink) {
358                 /* stop aquistion */
359                 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
360         }
361
362         this_usbduxsub->ai_cmd_running = 0;
363
364         return ret;
365 }
366
367 /*
368  * This will cancel a running acquisition operation.
369  * This is called by comedi but never from inside the driver.
370  */
371 static int usbdux_ai_cancel(struct comedi_device *dev,
372                             struct comedi_subdevice *s)
373 {
374         struct usbduxsub *this_usbduxsub;
375         int res = 0;
376
377         /* force unlink of all urbs */
378         this_usbduxsub = dev->private;
379         if (!this_usbduxsub)
380                 return -EFAULT;
381
382         dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
383
384         /* prevent other CPUs from submitting new commands just now */
385         down(&this_usbduxsub->sem);
386         if (!(this_usbduxsub->probed)) {
387                 up(&this_usbduxsub->sem);
388                 return -ENODEV;
389         }
390         /* unlink only if the urb really has been submitted */
391         res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
392         up(&this_usbduxsub->sem);
393         return res;
394 }
395
396 /* analogue IN - interrupt service routine */
397 static void usbduxsub_ai_IsocIrq(struct urb *urb)
398 {
399         int i, err, n;
400         struct usbduxsub *this_usbduxsub;
401         struct comedi_device *this_comedidev;
402         struct comedi_subdevice *s;
403
404         /* the context variable points to the subdevice */
405         this_comedidev = urb->context;
406         /* the private structure of the subdevice is struct usbduxsub */
407         this_usbduxsub = this_comedidev->private;
408         /* subdevice which is the AD converter */
409         s = &this_comedidev->subdevices[SUBDEV_AD];
410
411         /* first we test if something unusual has just happened */
412         switch (urb->status) {
413         case 0:
414                 /* copy the result in the transfer buffer */
415                 memcpy(this_usbduxsub->inBuffer,
416                        urb->transfer_buffer, SIZEINBUF);
417                 break;
418         case -EILSEQ:
419                 /* error in the ISOchronous data */
420                 /* we don't copy the data into the transfer buffer */
421                 /* and recycle the last data byte */
422                 dev_dbg(&urb->dev->dev,
423                         "comedi%d: usbdux: CRC error in ISO IN stream.\n",
424                         this_usbduxsub->comedidev->minor);
425
426                 break;
427
428         case -ECONNRESET:
429         case -ENOENT:
430         case -ESHUTDOWN:
431         case -ECONNABORTED:
432                 /* happens after an unlink command */
433                 if (this_usbduxsub->ai_cmd_running) {
434                         /* we are still running a command */
435                         /* tell this comedi */
436                         s->async->events |= COMEDI_CB_EOA;
437                         s->async->events |= COMEDI_CB_ERROR;
438                         comedi_event(this_usbduxsub->comedidev, s);
439                         /* stop the transfer w/o unlink */
440                         usbdux_ai_stop(this_usbduxsub, 0);
441                 }
442                 return;
443
444         default:
445                 /* a real error on the bus */
446                 /* pass error to comedi if we are really running a command */
447                 if (this_usbduxsub->ai_cmd_running) {
448                         dev_err(&urb->dev->dev,
449                                 "Non-zero urb status received in ai intr "
450                                 "context: %d\n", urb->status);
451                         s->async->events |= COMEDI_CB_EOA;
452                         s->async->events |= COMEDI_CB_ERROR;
453                         comedi_event(this_usbduxsub->comedidev, s);
454                         /* don't do an unlink here */
455                         usbdux_ai_stop(this_usbduxsub, 0);
456                 }
457                 return;
458         }
459
460         /*
461          * at this point we are reasonably sure that nothing dodgy has happened
462          * are we running a command?
463          */
464         if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
465                 /*
466                  * not running a command, do not continue execution if no
467                  * asynchronous command is running in particular not resubmit
468                  */
469                 return;
470         }
471
472         urb->dev = this_usbduxsub->usbdev;
473
474         /* resubmit the urb */
475         err = usb_submit_urb(urb, GFP_ATOMIC);
476         if (unlikely(err < 0)) {
477                 dev_err(&urb->dev->dev,
478                         "comedi_: urb resubmit failed in int-context! err=%d\n",
479                         err);
480                 if (err == -EL2NSYNC)
481                         dev_err(&urb->dev->dev,
482                                 "buggy USB host controller or bug in IRQ "
483                                 "handler!\n");
484                 s->async->events |= COMEDI_CB_EOA;
485                 s->async->events |= COMEDI_CB_ERROR;
486                 comedi_event(this_usbduxsub->comedidev, s);
487                 /* don't do an unlink here */
488                 usbdux_ai_stop(this_usbduxsub, 0);
489                 return;
490         }
491
492         this_usbduxsub->ai_counter--;
493         if (likely(this_usbduxsub->ai_counter > 0))
494                 return;
495
496         /* timer zero, transfer measurements to comedi */
497         this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
498
499         /* test, if we transmit only a fixed number of samples */
500         if (!(this_usbduxsub->ai_continous)) {
501                 /* not continuous, fixed number of samples */
502                 this_usbduxsub->ai_sample_count--;
503                 /* all samples received? */
504                 if (this_usbduxsub->ai_sample_count < 0) {
505                         /* prevent a resubmit next time */
506                         usbdux_ai_stop(this_usbduxsub, 0);
507                         /* say comedi that the acquistion is over */
508                         s->async->events |= COMEDI_CB_EOA;
509                         comedi_event(this_usbduxsub->comedidev, s);
510                         return;
511                 }
512         }
513         /* get the data from the USB bus and hand it over to comedi */
514         n = s->async->cmd.chanlist_len;
515         for (i = 0; i < n; i++) {
516                 /* transfer data */
517                 if (CR_RANGE(s->async->cmd.chanlist[i]) <= 1) {
518                         err = comedi_buf_put
519                             (s->async,
520                              le16_to_cpu(this_usbduxsub->inBuffer[i]) ^ 0x800);
521                 } else {
522                         err = comedi_buf_put
523                             (s->async,
524                              le16_to_cpu(this_usbduxsub->inBuffer[i]));
525                 }
526                 if (unlikely(err == 0)) {
527                         /* buffer overflow */
528                         usbdux_ai_stop(this_usbduxsub, 0);
529                         return;
530                 }
531         }
532         /* tell comedi that data is there */
533         s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
534         comedi_event(this_usbduxsub->comedidev, s);
535 }
536
537 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
538 {
539         int i = 0;
540         int err = 0;
541
542         if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
543                 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
544                         if (usbduxsub_tmp->urbOut[i])
545                                 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
546
547                         dev_dbg(&usbduxsub_tmp->interface->dev,
548                                 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
549                                 i, err);
550                 }
551         }
552         return err;
553 }
554
555 /* This will cancel a running acquisition operation
556  * in any context.
557  */
558 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
559 {
560         int ret = 0;
561
562         if (!this_usbduxsub)
563                 return -EFAULT;
564         dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
565
566         if (do_unlink)
567                 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
568
569         this_usbduxsub->ao_cmd_running = 0;
570
571         return ret;
572 }
573
574 /* force unlink, is called by comedi */
575 static int usbdux_ao_cancel(struct comedi_device *dev,
576                             struct comedi_subdevice *s)
577 {
578         struct usbduxsub *this_usbduxsub = dev->private;
579         int res = 0;
580
581         if (!this_usbduxsub)
582                 return -EFAULT;
583
584         /* prevent other CPUs from submitting a command just now */
585         down(&this_usbduxsub->sem);
586         if (!(this_usbduxsub->probed)) {
587                 up(&this_usbduxsub->sem);
588                 return -ENODEV;
589         }
590         /* unlink only if it is really running */
591         res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
592         up(&this_usbduxsub->sem);
593         return res;
594 }
595
596 static void usbduxsub_ao_IsocIrq(struct urb *urb)
597 {
598         int i, ret;
599         int8_t *datap;
600         struct usbduxsub *this_usbduxsub;
601         struct comedi_device *this_comedidev;
602         struct comedi_subdevice *s;
603
604         /* the context variable points to the subdevice */
605         this_comedidev = urb->context;
606         /* the private structure of the subdevice is struct usbduxsub */
607         this_usbduxsub = this_comedidev->private;
608
609         s = &this_comedidev->subdevices[SUBDEV_DA];
610
611         switch (urb->status) {
612         case 0:
613                 /* success */
614                 break;
615
616         case -ECONNRESET:
617         case -ENOENT:
618         case -ESHUTDOWN:
619         case -ECONNABORTED:
620                 /* after an unlink command, unplug, ... etc */
621                 /* no unlink needed here. Already shutting down. */
622                 if (this_usbduxsub->ao_cmd_running) {
623                         s->async->events |= COMEDI_CB_EOA;
624                         comedi_event(this_usbduxsub->comedidev, s);
625                         usbdux_ao_stop(this_usbduxsub, 0);
626                 }
627                 return;
628
629         default:
630                 /* a real error */
631                 if (this_usbduxsub->ao_cmd_running) {
632                         dev_err(&urb->dev->dev,
633                                 "comedi_: Non-zero urb status received in ao "
634                                 "intr context: %d\n", urb->status);
635                         s->async->events |= COMEDI_CB_ERROR;
636                         s->async->events |= COMEDI_CB_EOA;
637                         comedi_event(this_usbduxsub->comedidev, s);
638                         /* we do an unlink if we are in the high speed mode */
639                         usbdux_ao_stop(this_usbduxsub, 0);
640                 }
641                 return;
642         }
643
644         /* are we actually running? */
645         if (!(this_usbduxsub->ao_cmd_running))
646                 return;
647
648         /* normal operation: executing a command in this subdevice */
649         this_usbduxsub->ao_counter--;
650         if ((int)this_usbduxsub->ao_counter <= 0) {
651                 /* timer zero */
652                 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
653
654                 /* handle non continous acquisition */
655                 if (!(this_usbduxsub->ao_continous)) {
656                         /* fixed number of samples */
657                         this_usbduxsub->ao_sample_count--;
658                         if (this_usbduxsub->ao_sample_count < 0) {
659                                 /* all samples transmitted */
660                                 usbdux_ao_stop(this_usbduxsub, 0);
661                                 s->async->events |= COMEDI_CB_EOA;
662                                 comedi_event(this_usbduxsub->comedidev, s);
663                                 /* no resubmit of the urb */
664                                 return;
665                         }
666                 }
667                 /* transmit data to the USB bus */
668                 ((uint8_t *) (urb->transfer_buffer))[0] =
669                     s->async->cmd.chanlist_len;
670                 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
671                         short temp;
672                         if (i >= NUMOUTCHANNELS)
673                                 break;
674
675                         /* pointer to the DA */
676                         datap =
677                             (&(((int8_t *) urb->transfer_buffer)[i * 3 + 1]));
678                         /* get the data from comedi */
679                         ret = comedi_buf_get(s->async, &temp);
680                         datap[0] = temp;
681                         datap[1] = temp >> 8;
682                         datap[2] = this_usbduxsub->dac_commands[i];
683                         /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
684                         /* datap[0],datap[1],datap[2]); */
685                         if (ret < 0) {
686                                 dev_err(&urb->dev->dev,
687                                         "comedi: buffer underflow\n");
688                                 s->async->events |= COMEDI_CB_EOA;
689                                 s->async->events |= COMEDI_CB_OVERFLOW;
690                         }
691                         /* transmit data to comedi */
692                         s->async->events |= COMEDI_CB_BLOCK;
693                         comedi_event(this_usbduxsub->comedidev, s);
694                 }
695         }
696         urb->transfer_buffer_length = SIZEOUTBUF;
697         urb->dev = this_usbduxsub->usbdev;
698         urb->status = 0;
699         if (this_usbduxsub->ao_cmd_running) {
700                 if (this_usbduxsub->high_speed) {
701                         /* uframes */
702                         urb->interval = 8;
703                 } else {
704                         /* frames */
705                         urb->interval = 1;
706                 }
707                 urb->number_of_packets = 1;
708                 urb->iso_frame_desc[0].offset = 0;
709                 urb->iso_frame_desc[0].length = SIZEOUTBUF;
710                 urb->iso_frame_desc[0].status = 0;
711                 ret = usb_submit_urb(urb, GFP_ATOMIC);
712                 if (ret < 0) {
713                         dev_err(&urb->dev->dev,
714                                 "comedi_: ao urb resubm failed in int-cont. "
715                                 "ret=%d", ret);
716                         if (ret == EL2NSYNC)
717                                 dev_err(&urb->dev->dev,
718                                         "buggy USB host controller or bug in "
719                                         "IRQ handling!\n");
720
721                         s->async->events |= COMEDI_CB_EOA;
722                         s->async->events |= COMEDI_CB_ERROR;
723                         comedi_event(this_usbduxsub->comedidev, s);
724                         /* don't do an unlink here */
725                         usbdux_ao_stop(this_usbduxsub, 0);
726                 }
727         }
728 }
729
730 static int usbduxsub_start(struct usbduxsub *usbduxsub)
731 {
732         int errcode = 0;
733         uint8_t local_transfer_buffer[16];
734
735         /* 7f92 to zero */
736         local_transfer_buffer[0] = 0;
737         errcode = usb_control_msg(usbduxsub->usbdev,
738                                   /* create a pipe for a control transfer */
739                                   usb_sndctrlpipe(usbduxsub->usbdev, 0),
740                                   /* bRequest, "Firmware" */
741                                   USBDUXSUB_FIRMWARE,
742                                   /* bmRequestType */
743                                   VENDOR_DIR_OUT,
744                                   /* Value */
745                                   USBDUXSUB_CPUCS,
746                                   /* Index */
747                                   0x0000,
748                                   /* address of the transfer buffer */
749                                   local_transfer_buffer,
750                                   /* Length */
751                                   1,
752                                   /* Timeout */
753                                   BULK_TIMEOUT);
754         if (errcode < 0) {
755                 dev_err(&usbduxsub->interface->dev,
756                         "comedi_: control msg failed (start)\n");
757                 return errcode;
758         }
759         return 0;
760 }
761
762 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
763 {
764         int errcode = 0;
765
766         uint8_t local_transfer_buffer[16];
767
768         /* 7f92 to one */
769         local_transfer_buffer[0] = 1;
770         errcode = usb_control_msg(usbduxsub->usbdev,
771                                   usb_sndctrlpipe(usbduxsub->usbdev, 0),
772                                   /* bRequest, "Firmware" */
773                                   USBDUXSUB_FIRMWARE,
774                                   /* bmRequestType */
775                                   VENDOR_DIR_OUT,
776                                   /* Value */
777                                   USBDUXSUB_CPUCS,
778                                   /* Index */
779                                   0x0000, local_transfer_buffer,
780                                   /* Length */
781                                   1,
782                                   /* Timeout */
783                                   BULK_TIMEOUT);
784         if (errcode < 0) {
785                 dev_err(&usbduxsub->interface->dev,
786                         "comedi_: control msg failed (stop)\n");
787                 return errcode;
788         }
789         return 0;
790 }
791
792 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
793                             uint8_t *local_transfer_buffer,
794                             unsigned int startAddr, unsigned int len)
795 {
796         int errcode;
797
798         errcode = usb_control_msg(usbduxsub->usbdev,
799                                   usb_sndctrlpipe(usbduxsub->usbdev, 0),
800                                   /* brequest, firmware */
801                                   USBDUXSUB_FIRMWARE,
802                                   /* bmRequestType */
803                                   VENDOR_DIR_OUT,
804                                   /* value */
805                                   startAddr,
806                                   /* index */
807                                   0x0000,
808                                   /* our local safe buffer */
809                                   local_transfer_buffer,
810                                   /* length */
811                                   len,
812                                   /* timeout */
813                                   BULK_TIMEOUT);
814         dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
815         if (errcode < 0) {
816                 dev_err(&usbduxsub->interface->dev, "comedi_: upload failed\n");
817                 return errcode;
818         }
819         return 0;
820 }
821
822 #define FIRMWARE_MAX_LEN 0x2000
823
824 static int firmwareUpload(struct usbduxsub *usbduxsub,
825                           const u8 *firmwareBinary, int sizeFirmware)
826 {
827         int ret;
828         uint8_t *fwBuf;
829
830         if (!firmwareBinary)
831                 return 0;
832
833         if (sizeFirmware > FIRMWARE_MAX_LEN) {
834                 dev_err(&usbduxsub->interface->dev,
835                         "usbdux firmware binary it too large for FX2.\n");
836                 return -ENOMEM;
837         }
838
839         /* we generate a local buffer for the firmware */
840         fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
841         if (!fwBuf) {
842                 dev_err(&usbduxsub->interface->dev,
843                         "comedi_: mem alloc for firmware failed\n");
844                 return -ENOMEM;
845         }
846
847         ret = usbduxsub_stop(usbduxsub);
848         if (ret < 0) {
849                 dev_err(&usbduxsub->interface->dev,
850                         "comedi_: can not stop firmware\n");
851                 kfree(fwBuf);
852                 return ret;
853         }
854
855         ret = usbduxsub_upload(usbduxsub, fwBuf, 0, sizeFirmware);
856         if (ret < 0) {
857                 dev_err(&usbduxsub->interface->dev,
858                         "comedi_: firmware upload failed\n");
859                 kfree(fwBuf);
860                 return ret;
861         }
862         ret = usbduxsub_start(usbduxsub);
863         if (ret < 0) {
864                 dev_err(&usbduxsub->interface->dev,
865                         "comedi_: can not start firmware\n");
866                 kfree(fwBuf);
867                 return ret;
868         }
869         kfree(fwBuf);
870         return 0;
871 }
872
873 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
874 {
875         int i, errFlag;
876
877         if (!usbduxsub)
878                 return -EFAULT;
879
880         /* Submit all URBs and start the transfer on the bus */
881         for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
882                 /* in case of a resubmission after an unlink... */
883                 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
884                 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
885                 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
886                 usbduxsub->urbIn[i]->status = 0;
887                 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
888                 dev_dbg(&usbduxsub->interface->dev,
889                         "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
890                         usbduxsub->comedidev->minor, i,
891                         (usbduxsub->urbIn[i]->context),
892                         (usbduxsub->urbIn[i]->dev),
893                         (usbduxsub->urbIn[i]->interval));
894                 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
895                 if (errFlag) {
896                         dev_err(&usbduxsub->interface->dev,
897                                 "comedi_: ai: usb_submit_urb(%d) error %d\n",
898                                 i, errFlag);
899                         return errFlag;
900                 }
901         }
902         return 0;
903 }
904
905 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
906 {
907         int i, errFlag;
908
909         if (!usbduxsub)
910                 return -EFAULT;
911
912         for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
913                 dev_dbg(&usbduxsub->interface->dev,
914                         "comedi_: submitting out-urb[%d]\n", i);
915                 /* in case of a resubmission after an unlink... */
916                 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
917                 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
918                 usbduxsub->urbOut[i]->status = 0;
919                 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
920                 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
921                 if (errFlag) {
922                         dev_err(&usbduxsub->interface->dev,
923                                 "comedi_: ao: usb_submit_urb(%d) error %d\n",
924                                 i, errFlag);
925                         return errFlag;
926                 }
927         }
928         return 0;
929 }
930
931 static int usbdux_ai_cmdtest(struct comedi_device *dev,
932                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
933 {
934         struct usbduxsub *this_usbduxsub = dev->private;
935         int err = 0, i;
936         unsigned int tmpTimer;
937
938         if (!(this_usbduxsub->probed))
939                 return -ENODEV;
940
941         /* Step 1 : check if triggers are trivially valid */
942
943         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
944         err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
945         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
946         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
947         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
948
949         if (err)
950                 return 1;
951
952         /* Step 2a : make sure trigger sources are unique */
953
954         err |= cfc_check_trigger_is_unique(cmd->start_src);
955         err |= cfc_check_trigger_is_unique(cmd->stop_src);
956
957         /* Step 2b : and mutually compatible */
958
959         if (err)
960                 return 2;
961
962         /* Step 3: check if arguments are trivially valid */
963
964         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
965
966         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
967                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
968
969         if (cmd->scan_begin_src == TRIG_TIMER) {
970                 if (this_usbduxsub->high_speed) {
971                         /*
972                          * In high speed mode microframes are possible.
973                          * However, during one microframe we can roughly
974                          * sample one channel. Thus, the more channels
975                          * are in the channel list the more time we need.
976                          */
977                         i = 1;
978                         /* find a power of 2 for the number of channels */
979                         while (i < (cmd->chanlist_len))
980                                 i = i * 2;
981
982                         err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
983                                                          1000000 / 8 * i);
984                         /* now calc the real sampling rate with all the
985                          * rounding errors */
986                         tmpTimer =
987                             ((unsigned int)(cmd->scan_begin_arg / 125000)) *
988                             125000;
989                 } else {
990                         /* full speed */
991                         /* 1kHz scans every USB frame */
992                         err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
993                                                          1000000);
994                         /*
995                          * calc the real sampling rate with the rounding errors
996                          */
997                         tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
998                                                    1000000)) * 1000000;
999                 }
1000                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg,
1001                                                 tmpTimer);
1002         }
1003
1004         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
1005
1006         if (cmd->stop_src == TRIG_COUNT) {
1007                 /* any count is allowed */
1008         } else {
1009                 /* TRIG_NONE */
1010                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1011         }
1012
1013         if (err)
1014                 return 3;
1015
1016         return 0;
1017 }
1018
1019 /*
1020  * creates the ADC command for the MAX1271
1021  * range is the range value from comedi
1022  */
1023 static int8_t create_adc_command(unsigned int chan, int range)
1024 {
1025         int8_t p = (range <= 1);
1026         int8_t r = ((range % 2) == 0);
1027         return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
1028 }
1029
1030 /* bulk transfers to usbdux */
1031
1032 #define SENDADCOMMANDS            0
1033 #define SENDDACOMMANDS            1
1034 #define SENDDIOCONFIGCOMMAND      2
1035 #define SENDDIOBITSCOMMAND        3
1036 #define SENDSINGLEAD              4
1037 #define READCOUNTERCOMMAND        5
1038 #define WRITECOUNTERCOMMAND       6
1039 #define SENDPWMON                 7
1040 #define SENDPWMOFF                8
1041
1042 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1043 {
1044         int result, nsent;
1045
1046         this_usbduxsub->dux_commands[0] = cmd_type;
1047 #ifdef NOISY_DUX_DEBUGBUG
1048         printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1049                this_usbduxsub->comedidev->minor);
1050         for (result = 0; result < SIZEOFDUXBUFFER; result++)
1051                 printk(" %02x", this_usbduxsub->dux_commands[result]);
1052         printk("\n");
1053 #endif
1054         result = usb_bulk_msg(this_usbduxsub->usbdev,
1055                               usb_sndbulkpipe(this_usbduxsub->usbdev,
1056                                               COMMAND_OUT_EP),
1057                               this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1058                               &nsent, BULK_TIMEOUT);
1059         if (result < 0)
1060                 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1061                         "could not transmit dux_command to the usb-device, "
1062                         "err=%d\n", this_usbduxsub->comedidev->minor, result);
1063
1064         return result;
1065 }
1066
1067 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1068 {
1069         int result = (-EFAULT);
1070         int nrec;
1071         int i;
1072
1073         for (i = 0; i < RETRIES; i++) {
1074                 result = usb_bulk_msg(this_usbduxsub->usbdev,
1075                                       usb_rcvbulkpipe(this_usbduxsub->usbdev,
1076                                                       COMMAND_IN_EP),
1077                                       this_usbduxsub->insnBuffer, SIZEINSNBUF,
1078                                       &nrec, BULK_TIMEOUT);
1079                 if (result < 0) {
1080                         dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1081                                 "insn: USB error %d while receiving DUX command"
1082                                 "\n", this_usbduxsub->comedidev->minor, result);
1083                         return result;
1084                 }
1085                 if (le16_to_cpu(this_usbduxsub->insnBuffer[0]) == command)
1086                         return result;
1087         }
1088         /* this is only reached if the data has been requested a couple of
1089          * times */
1090         dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1091                 "wrong data returned from firmware: want cmd %d, got cmd %d.\n",
1092                 this_usbduxsub->comedidev->minor, command,
1093                 le16_to_cpu(this_usbduxsub->insnBuffer[0]));
1094         return -EFAULT;
1095 }
1096
1097 static int usbdux_ai_inttrig(struct comedi_device *dev,
1098                              struct comedi_subdevice *s, unsigned int trignum)
1099 {
1100         int ret;
1101         struct usbduxsub *this_usbduxsub = dev->private;
1102         if (!this_usbduxsub)
1103                 return -EFAULT;
1104
1105         down(&this_usbduxsub->sem);
1106         if (!(this_usbduxsub->probed)) {
1107                 up(&this_usbduxsub->sem);
1108                 return -ENODEV;
1109         }
1110         dev_dbg(&this_usbduxsub->interface->dev,
1111                 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1112
1113         if (trignum != 0) {
1114                 dev_err(&this_usbduxsub->interface->dev,
1115                         "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1116                         dev->minor);
1117                 up(&this_usbduxsub->sem);
1118                 return -EINVAL;
1119         }
1120         if (!(this_usbduxsub->ai_cmd_running)) {
1121                 this_usbduxsub->ai_cmd_running = 1;
1122                 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1123                 if (ret < 0) {
1124                         dev_err(&this_usbduxsub->interface->dev,
1125                                 "comedi%d: usbdux_ai_inttrig: "
1126                                 "urbSubmit: err=%d\n", dev->minor, ret);
1127                         this_usbduxsub->ai_cmd_running = 0;
1128                         up(&this_usbduxsub->sem);
1129                         return ret;
1130                 }
1131                 s->async->inttrig = NULL;
1132         } else {
1133                 dev_err(&this_usbduxsub->interface->dev,
1134                         "comedi%d: ai_inttrig but acqu is already running\n",
1135                         dev->minor);
1136         }
1137         up(&this_usbduxsub->sem);
1138         return 1;
1139 }
1140
1141 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1142 {
1143         struct comedi_cmd *cmd = &s->async->cmd;
1144         unsigned int chan, range;
1145         int i, ret;
1146         struct usbduxsub *this_usbduxsub = dev->private;
1147         int result;
1148
1149         if (!this_usbduxsub)
1150                 return -EFAULT;
1151
1152         dev_dbg(&this_usbduxsub->interface->dev,
1153                 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1154
1155         /* block other CPUs from starting an ai_cmd */
1156         down(&this_usbduxsub->sem);
1157
1158         if (!(this_usbduxsub->probed)) {
1159                 up(&this_usbduxsub->sem);
1160                 return -ENODEV;
1161         }
1162         if (this_usbduxsub->ai_cmd_running) {
1163                 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1164                         "ai_cmd not possible. Another ai_cmd is running.\n",
1165                         dev->minor);
1166                 up(&this_usbduxsub->sem);
1167                 return -EBUSY;
1168         }
1169         /* set current channel of the running acquisition to zero */
1170         s->async->cur_chan = 0;
1171
1172         this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1173         for (i = 0; i < cmd->chanlist_len; ++i) {
1174                 chan = CR_CHAN(cmd->chanlist[i]);
1175                 range = CR_RANGE(cmd->chanlist[i]);
1176                 if (i >= NUMCHANNELS) {
1177                         dev_err(&this_usbduxsub->interface->dev,
1178                                 "comedi%d: channel list too long\n",
1179                                 dev->minor);
1180                         break;
1181                 }
1182                 this_usbduxsub->dux_commands[i + 2] =
1183                     create_adc_command(chan, range);
1184         }
1185
1186         dev_dbg(&this_usbduxsub->interface->dev,
1187                 "comedi %d: sending commands to the usb device: size=%u\n",
1188                 dev->minor, NUMCHANNELS);
1189
1190         result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1191         if (result < 0) {
1192                 up(&this_usbduxsub->sem);
1193                 return result;
1194         }
1195
1196         if (this_usbduxsub->high_speed) {
1197                 /*
1198                  * every channel gets a time window of 125us. Thus, if we
1199                  * sample all 8 channels we need 1ms. If we sample only one
1200                  * channel we need only 125us
1201                  */
1202                 this_usbduxsub->ai_interval = 1;
1203                 /* find a power of 2 for the interval */
1204                 while ((this_usbduxsub->ai_interval) < (cmd->chanlist_len)) {
1205                         this_usbduxsub->ai_interval =
1206                             (this_usbduxsub->ai_interval) * 2;
1207                 }
1208                 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1209                                                           (this_usbduxsub->
1210                                                            ai_interval));
1211         } else {
1212                 /* interval always 1ms */
1213                 this_usbduxsub->ai_interval = 1;
1214                 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1215         }
1216         if (this_usbduxsub->ai_timer < 1) {
1217                 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1218                         "timer=%d, scan_begin_arg=%d. "
1219                         "Not properly tested by cmdtest?\n", dev->minor,
1220                         this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1221                 up(&this_usbduxsub->sem);
1222                 return -EINVAL;
1223         }
1224         this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1225
1226         if (cmd->stop_src == TRIG_COUNT) {
1227                 /* data arrives as one packet */
1228                 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1229                 this_usbduxsub->ai_continous = 0;
1230         } else {
1231                 /* continous acquisition */
1232                 this_usbduxsub->ai_continous = 1;
1233                 this_usbduxsub->ai_sample_count = 0;
1234         }
1235
1236         if (cmd->start_src == TRIG_NOW) {
1237                 /* enable this acquisition operation */
1238                 this_usbduxsub->ai_cmd_running = 1;
1239                 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1240                 if (ret < 0) {
1241                         this_usbduxsub->ai_cmd_running = 0;
1242                         /* fixme: unlink here?? */
1243                         up(&this_usbduxsub->sem);
1244                         return ret;
1245                 }
1246                 s->async->inttrig = NULL;
1247         } else {
1248                 /* TRIG_INT */
1249                 /* don't enable the acquision operation */
1250                 /* wait for an internal signal */
1251                 s->async->inttrig = usbdux_ai_inttrig;
1252         }
1253         up(&this_usbduxsub->sem);
1254         return 0;
1255 }
1256
1257 /* Mode 0 is used to get a single conversion on demand */
1258 static int usbdux_ai_insn_read(struct comedi_device *dev,
1259                                struct comedi_subdevice *s,
1260                                struct comedi_insn *insn, unsigned int *data)
1261 {
1262         int i;
1263         unsigned int one = 0;
1264         int chan, range;
1265         int err;
1266         struct usbduxsub *this_usbduxsub = dev->private;
1267
1268         if (!this_usbduxsub)
1269                 return 0;
1270
1271         dev_dbg(&this_usbduxsub->interface->dev,
1272                 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1273                 dev->minor, insn->n, insn->subdev);
1274
1275         down(&this_usbduxsub->sem);
1276         if (!(this_usbduxsub->probed)) {
1277                 up(&this_usbduxsub->sem);
1278                 return -ENODEV;
1279         }
1280         if (this_usbduxsub->ai_cmd_running) {
1281                 dev_err(&this_usbduxsub->interface->dev,
1282                         "comedi%d: ai_insn_read not possible. "
1283                         "Async Command is running.\n", dev->minor);
1284                 up(&this_usbduxsub->sem);
1285                 return 0;
1286         }
1287
1288         /* sample one channel */
1289         chan = CR_CHAN(insn->chanspec);
1290         range = CR_RANGE(insn->chanspec);
1291         /* set command for the first channel */
1292         this_usbduxsub->dux_commands[1] = create_adc_command(chan, range);
1293
1294         /* adc commands */
1295         err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1296         if (err < 0) {
1297                 up(&this_usbduxsub->sem);
1298                 return err;
1299         }
1300
1301         for (i = 0; i < insn->n; i++) {
1302                 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1303                 if (err < 0) {
1304                         up(&this_usbduxsub->sem);
1305                         return 0;
1306                 }
1307                 one = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1308                 if (CR_RANGE(insn->chanspec) <= 1)
1309                         one = one ^ 0x800;
1310
1311                 data[i] = one;
1312         }
1313         up(&this_usbduxsub->sem);
1314         return i;
1315 }
1316
1317 /************************************/
1318 /* analog out */
1319
1320 static int usbdux_ao_insn_read(struct comedi_device *dev,
1321                                struct comedi_subdevice *s,
1322                                struct comedi_insn *insn, unsigned int *data)
1323 {
1324         int i;
1325         int chan = CR_CHAN(insn->chanspec);
1326         struct usbduxsub *this_usbduxsub = dev->private;
1327
1328         if (!this_usbduxsub)
1329                 return -EFAULT;
1330
1331         down(&this_usbduxsub->sem);
1332         if (!(this_usbduxsub->probed)) {
1333                 up(&this_usbduxsub->sem);
1334                 return -ENODEV;
1335         }
1336         for (i = 0; i < insn->n; i++)
1337                 data[i] = this_usbduxsub->outBuffer[chan];
1338
1339         up(&this_usbduxsub->sem);
1340         return i;
1341 }
1342
1343 static int usbdux_ao_insn_write(struct comedi_device *dev,
1344                                 struct comedi_subdevice *s,
1345                                 struct comedi_insn *insn, unsigned int *data)
1346 {
1347         int i, err;
1348         int chan = CR_CHAN(insn->chanspec);
1349         struct usbduxsub *this_usbduxsub = dev->private;
1350
1351         if (!this_usbduxsub)
1352                 return -EFAULT;
1353
1354         dev_dbg(&this_usbduxsub->interface->dev,
1355                 "comedi%d: ao_insn_write\n", dev->minor);
1356
1357         down(&this_usbduxsub->sem);
1358         if (!(this_usbduxsub->probed)) {
1359                 up(&this_usbduxsub->sem);
1360                 return -ENODEV;
1361         }
1362         if (this_usbduxsub->ao_cmd_running) {
1363                 dev_err(&this_usbduxsub->interface->dev,
1364                         "comedi%d: ao_insn_write: "
1365                         "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1366                 up(&this_usbduxsub->sem);
1367                 return 0;
1368         }
1369
1370         for (i = 0; i < insn->n; i++) {
1371                 dev_dbg(&this_usbduxsub->interface->dev,
1372                         "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1373                         dev->minor, chan, i, data[i]);
1374
1375                 /* number of channels: 1 */
1376                 this_usbduxsub->dux_commands[1] = 1;
1377                 /* one 16 bit value */
1378                 *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
1379                     cpu_to_le16(data[i]);
1380                 this_usbduxsub->outBuffer[chan] = data[i];
1381                 /* channel number */
1382                 this_usbduxsub->dux_commands[4] = (chan << 6);
1383                 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1384                 if (err < 0) {
1385                         up(&this_usbduxsub->sem);
1386                         return err;
1387                 }
1388         }
1389         up(&this_usbduxsub->sem);
1390
1391         return i;
1392 }
1393
1394 static int usbdux_ao_inttrig(struct comedi_device *dev,
1395                              struct comedi_subdevice *s, unsigned int trignum)
1396 {
1397         int ret;
1398         struct usbduxsub *this_usbduxsub = dev->private;
1399
1400         if (!this_usbduxsub)
1401                 return -EFAULT;
1402
1403         down(&this_usbduxsub->sem);
1404         if (!(this_usbduxsub->probed)) {
1405                 up(&this_usbduxsub->sem);
1406                 return -ENODEV;
1407         }
1408         if (trignum != 0) {
1409                 dev_err(&this_usbduxsub->interface->dev,
1410                         "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1411                         dev->minor);
1412                 up(&this_usbduxsub->sem);
1413                 return -EINVAL;
1414         }
1415         if (!(this_usbduxsub->ao_cmd_running)) {
1416                 this_usbduxsub->ao_cmd_running = 1;
1417                 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1418                 if (ret < 0) {
1419                         dev_err(&this_usbduxsub->interface->dev,
1420                                 "comedi%d: usbdux_ao_inttrig: submitURB: "
1421                                 "err=%d\n", dev->minor, ret);
1422                         this_usbduxsub->ao_cmd_running = 0;
1423                         up(&this_usbduxsub->sem);
1424                         return ret;
1425                 }
1426                 s->async->inttrig = NULL;
1427         } else {
1428                 dev_err(&this_usbduxsub->interface->dev,
1429                         "comedi%d: ao_inttrig but acqu is already running.\n",
1430                         dev->minor);
1431         }
1432         up(&this_usbduxsub->sem);
1433         return 1;
1434 }
1435
1436 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1437                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
1438 {
1439         struct usbduxsub *this_usbduxsub = dev->private;
1440         int err = 0;
1441         unsigned int flags;
1442
1443         if (!this_usbduxsub)
1444                 return -EFAULT;
1445
1446         if (!(this_usbduxsub->probed))
1447                 return -ENODEV;
1448
1449         /* Step 1 : check if triggers are trivially valid */
1450
1451         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
1452
1453         if (0) {                /* (this_usbduxsub->high_speed) */
1454                 /* the sampling rate is set by the coversion rate */
1455                 flags = TRIG_FOLLOW;
1456         } else {
1457                 /* start a new scan (output at once) with a timer */
1458                 flags = TRIG_TIMER;
1459         }
1460         err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
1461
1462         if (0) {                /* (this_usbduxsub->high_speed) */
1463                 /*
1464                  * in usb-2.0 only one conversion it transmitted
1465                  * but with 8kHz/n
1466                  */
1467                 flags = TRIG_TIMER;
1468         } else {
1469                 /*
1470                  * all conversion events happen simultaneously with
1471                  * a rate of 1kHz/n
1472                  */
1473                 flags = TRIG_NOW;
1474         }
1475         err |= cfc_check_trigger_src(&cmd->convert_src, flags);
1476
1477         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
1478         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
1479
1480         if (err)
1481                 return 1;
1482
1483         /* Step 2a : make sure trigger sources are unique */
1484
1485         err |= cfc_check_trigger_is_unique(cmd->start_src);
1486         err |= cfc_check_trigger_is_unique(cmd->stop_src);
1487
1488         /* Step 2b : and mutually compatible */
1489
1490         if (err)
1491                 return 2;
1492
1493         /* Step 3: check if arguments are trivially valid */
1494
1495         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
1496
1497         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
1498                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
1499
1500         if (cmd->scan_begin_src == TRIG_TIMER)
1501                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
1502                                                  1000000);
1503
1504         /* not used now, is for later use */
1505         if (cmd->convert_src == TRIG_TIMER)
1506                 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
1507
1508         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
1509
1510         if (cmd->stop_src == TRIG_COUNT) {
1511                 /* any count is allowed */
1512         } else {
1513                 /* TRIG_NONE */
1514                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1515         }
1516
1517         if (err)
1518                 return 3;
1519
1520         return 0;
1521 }
1522
1523 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1524 {
1525         struct comedi_cmd *cmd = &s->async->cmd;
1526         unsigned int chan, gain;
1527         int i, ret;
1528         struct usbduxsub *this_usbduxsub = dev->private;
1529
1530         if (!this_usbduxsub)
1531                 return -EFAULT;
1532
1533         down(&this_usbduxsub->sem);
1534         if (!(this_usbduxsub->probed)) {
1535                 up(&this_usbduxsub->sem);
1536                 return -ENODEV;
1537         }
1538         dev_dbg(&this_usbduxsub->interface->dev,
1539                 "comedi%d: %s\n", dev->minor, __func__);
1540
1541         /* set current channel of the running acquisition to zero */
1542         s->async->cur_chan = 0;
1543         for (i = 0; i < cmd->chanlist_len; ++i) {
1544                 chan = CR_CHAN(cmd->chanlist[i]);
1545                 gain = CR_RANGE(cmd->chanlist[i]);
1546                 if (i >= NUMOUTCHANNELS) {
1547                         dev_err(&this_usbduxsub->interface->dev,
1548                                 "comedi%d: %s: channel list too long\n",
1549                                 dev->minor, __func__);
1550                         break;
1551                 }
1552                 this_usbduxsub->dac_commands[i] = (chan << 6);
1553                 dev_dbg(&this_usbduxsub->interface->dev,
1554                         "comedi%d: dac command for ch %d is %x\n",
1555                         dev->minor, i, this_usbduxsub->dac_commands[i]);
1556         }
1557
1558         /* we count in steps of 1ms (125us) */
1559         /* 125us mode not used yet */
1560         if (0) {                /* (this_usbduxsub->high_speed) */
1561                 /* 125us */
1562                 /* timing of the conversion itself: every 125 us */
1563                 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1564         } else {
1565                 /* 1ms */
1566                 /* timing of the scan: we get all channels at once */
1567                 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1568                 dev_dbg(&this_usbduxsub->interface->dev,
1569                         "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1570                         "convert_src=%d, convert_arg=%d\n", dev->minor,
1571                         cmd->scan_begin_src, cmd->scan_begin_arg,
1572                         cmd->convert_src, cmd->convert_arg);
1573                 dev_dbg(&this_usbduxsub->interface->dev,
1574                         "comedi%d: ao_timer=%d (ms)\n",
1575                         dev->minor, this_usbduxsub->ao_timer);
1576                 if (this_usbduxsub->ao_timer < 1) {
1577                         dev_err(&this_usbduxsub->interface->dev,
1578                                 "comedi%d: usbdux: ao_timer=%d, "
1579                                 "scan_begin_arg=%d. "
1580                                 "Not properly tested by cmdtest?\n",
1581                                 dev->minor, this_usbduxsub->ao_timer,
1582                                 cmd->scan_begin_arg);
1583                         up(&this_usbduxsub->sem);
1584                         return -EINVAL;
1585                 }
1586         }
1587         this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1588
1589         if (cmd->stop_src == TRIG_COUNT) {
1590                 /* not continuous */
1591                 /* counter */
1592                 /* high speed also scans everything at once */
1593                 if (0) {        /* (this_usbduxsub->high_speed) */
1594                         this_usbduxsub->ao_sample_count =
1595                             (cmd->stop_arg) * (cmd->scan_end_arg);
1596                 } else {
1597                         /* there's no scan as the scan has been */
1598                         /* perf inside the FX2 */
1599                         /* data arrives as one packet */
1600                         this_usbduxsub->ao_sample_count = cmd->stop_arg;
1601                 }
1602                 this_usbduxsub->ao_continous = 0;
1603         } else {
1604                 /* continous acquisition */
1605                 this_usbduxsub->ao_continous = 1;
1606                 this_usbduxsub->ao_sample_count = 0;
1607         }
1608
1609         if (cmd->start_src == TRIG_NOW) {
1610                 /* enable this acquisition operation */
1611                 this_usbduxsub->ao_cmd_running = 1;
1612                 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1613                 if (ret < 0) {
1614                         this_usbduxsub->ao_cmd_running = 0;
1615                         /* fixme: unlink here?? */
1616                         up(&this_usbduxsub->sem);
1617                         return ret;
1618                 }
1619                 s->async->inttrig = NULL;
1620         } else {
1621                 /* TRIG_INT */
1622                 /* submit the urbs later */
1623                 /* wait for an internal signal */
1624                 s->async->inttrig = usbdux_ao_inttrig;
1625         }
1626
1627         up(&this_usbduxsub->sem);
1628         return 0;
1629 }
1630
1631 static int usbdux_dio_insn_config(struct comedi_device *dev,
1632                                   struct comedi_subdevice *s,
1633                                   struct comedi_insn *insn, unsigned int *data)
1634 {
1635         int chan = CR_CHAN(insn->chanspec);
1636
1637         /* The input or output configuration of each digital line is
1638          * configured by a special insn_config instruction.  chanspec
1639          * contains the channel to be changed, and data[0] contains the
1640          * value COMEDI_INPUT or COMEDI_OUTPUT. */
1641
1642         switch (data[0]) {
1643         case INSN_CONFIG_DIO_OUTPUT:
1644                 s->io_bits |= 1 << chan;        /* 1 means Out */
1645                 break;
1646         case INSN_CONFIG_DIO_INPUT:
1647                 s->io_bits &= ~(1 << chan);
1648                 break;
1649         case INSN_CONFIG_DIO_QUERY:
1650                 data[1] =
1651                     (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1652                 break;
1653         default:
1654                 return -EINVAL;
1655                 break;
1656         }
1657         /* we don't tell the firmware here as it would take 8 frames */
1658         /* to submit the information. We do it in the insn_bits. */
1659         return insn->n;
1660 }
1661
1662 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1663                                 struct comedi_subdevice *s,
1664                                 struct comedi_insn *insn, unsigned int *data)
1665 {
1666
1667         struct usbduxsub *this_usbduxsub = dev->private;
1668         int err;
1669
1670         if (!this_usbduxsub)
1671                 return -EFAULT;
1672
1673         down(&this_usbduxsub->sem);
1674
1675         if (!(this_usbduxsub->probed)) {
1676                 up(&this_usbduxsub->sem);
1677                 return -ENODEV;
1678         }
1679
1680         /* The insn data is a mask in data[0] and the new data
1681          * in data[1], each channel cooresponding to a bit. */
1682         s->state &= ~data[0];
1683         s->state |= data[0] & data[1];
1684         this_usbduxsub->dux_commands[1] = s->io_bits;
1685         this_usbduxsub->dux_commands[2] = s->state;
1686
1687         /* This command also tells the firmware to return */
1688         /* the digital input lines */
1689         err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1690         if (err < 0) {
1691                 up(&this_usbduxsub->sem);
1692                 return err;
1693         }
1694         err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1695         if (err < 0) {
1696                 up(&this_usbduxsub->sem);
1697                 return err;
1698         }
1699
1700         data[1] = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1701         up(&this_usbduxsub->sem);
1702         return insn->n;
1703 }
1704
1705 /* reads the 4 counters, only two are used just now */
1706 static int usbdux_counter_read(struct comedi_device *dev,
1707                                struct comedi_subdevice *s,
1708                                struct comedi_insn *insn, unsigned int *data)
1709 {
1710         struct usbduxsub *this_usbduxsub = dev->private;
1711         int chan = insn->chanspec;
1712         int err;
1713
1714         if (!this_usbduxsub)
1715                 return -EFAULT;
1716
1717         down(&this_usbduxsub->sem);
1718
1719         if (!(this_usbduxsub->probed)) {
1720                 up(&this_usbduxsub->sem);
1721                 return -ENODEV;
1722         }
1723
1724         err = send_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1725         if (err < 0) {
1726                 up(&this_usbduxsub->sem);
1727                 return err;
1728         }
1729
1730         err = receive_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1731         if (err < 0) {
1732                 up(&this_usbduxsub->sem);
1733                 return err;
1734         }
1735
1736         data[0] = le16_to_cpu(this_usbduxsub->insnBuffer[chan + 1]);
1737         up(&this_usbduxsub->sem);
1738         return 1;
1739 }
1740
1741 static int usbdux_counter_write(struct comedi_device *dev,
1742                                 struct comedi_subdevice *s,
1743                                 struct comedi_insn *insn, unsigned int *data)
1744 {
1745         struct usbduxsub *this_usbduxsub = dev->private;
1746         int err;
1747
1748         if (!this_usbduxsub)
1749                 return -EFAULT;
1750
1751         down(&this_usbduxsub->sem);
1752
1753         if (!(this_usbduxsub->probed)) {
1754                 up(&this_usbduxsub->sem);
1755                 return -ENODEV;
1756         }
1757
1758         this_usbduxsub->dux_commands[1] = insn->chanspec;
1759         *((int16_t *) (this_usbduxsub->dux_commands + 2)) = cpu_to_le16(*data);
1760
1761         err = send_dux_commands(this_usbduxsub, WRITECOUNTERCOMMAND);
1762         if (err < 0) {
1763                 up(&this_usbduxsub->sem);
1764                 return err;
1765         }
1766
1767         up(&this_usbduxsub->sem);
1768
1769         return 1;
1770 }
1771
1772 static int usbdux_counter_config(struct comedi_device *dev,
1773                                  struct comedi_subdevice *s,
1774                                  struct comedi_insn *insn, unsigned int *data)
1775 {
1776         /* nothing to do so far */
1777         return 2;
1778 }
1779
1780 /***********************************/
1781 /* PWM */
1782
1783 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1784 {
1785         int err = 0;
1786
1787         if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1788                 if (usbduxsub_tmp->urbPwm)
1789                         usb_kill_urb(usbduxsub_tmp->urbPwm);
1790                 dev_dbg(&usbduxsub_tmp->interface->dev,
1791                         "comedi: unlinked PwmURB: res=%d\n", err);
1792         }
1793         return err;
1794 }
1795
1796 /* This cancels a running acquisition operation
1797  * in any context.
1798  */
1799 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1800 {
1801         int ret = 0;
1802
1803         if (!this_usbduxsub)
1804                 return -EFAULT;
1805
1806         dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1807         if (do_unlink)
1808                 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1809
1810         this_usbduxsub->pwm_cmd_running = 0;
1811
1812         return ret;
1813 }
1814
1815 /* force unlink - is called by comedi */
1816 static int usbdux_pwm_cancel(struct comedi_device *dev,
1817                              struct comedi_subdevice *s)
1818 {
1819         struct usbduxsub *this_usbduxsub = dev->private;
1820         int res = 0;
1821
1822         /* unlink only if it is really running */
1823         res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1824
1825         dev_dbg(&this_usbduxsub->interface->dev,
1826                 "comedi %d: sending pwm off command to the usb device.\n",
1827                 dev->minor);
1828
1829         return send_dux_commands(this_usbduxsub, SENDPWMOFF);
1830 }
1831
1832 static void usbduxsub_pwm_irq(struct urb *urb)
1833 {
1834         int ret;
1835         struct usbduxsub *this_usbduxsub;
1836         struct comedi_device *this_comedidev;
1837         struct comedi_subdevice *s;
1838
1839         /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1840
1841         /* the context variable points to the subdevice */
1842         this_comedidev = urb->context;
1843         /* the private structure of the subdevice is struct usbduxsub */
1844         this_usbduxsub = this_comedidev->private;
1845
1846         s = &this_comedidev->subdevices[SUBDEV_DA];
1847
1848         switch (urb->status) {
1849         case 0:
1850                 /* success */
1851                 break;
1852
1853         case -ECONNRESET:
1854         case -ENOENT:
1855         case -ESHUTDOWN:
1856         case -ECONNABORTED:
1857                 /*
1858                  * after an unlink command, unplug, ... etc
1859                  * no unlink needed here. Already shutting down.
1860                  */
1861                 if (this_usbduxsub->pwm_cmd_running)
1862                         usbdux_pwm_stop(this_usbduxsub, 0);
1863
1864                 return;
1865
1866         default:
1867                 /* a real error */
1868                 if (this_usbduxsub->pwm_cmd_running) {
1869                         dev_err(&this_usbduxsub->interface->dev,
1870                                 "comedi_: Non-zero urb status received in "
1871                                 "pwm intr context: %d\n", urb->status);
1872                         usbdux_pwm_stop(this_usbduxsub, 0);
1873                 }
1874                 return;
1875         }
1876
1877         /* are we actually running? */
1878         if (!(this_usbduxsub->pwm_cmd_running))
1879                 return;
1880
1881         urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1882         urb->dev = this_usbduxsub->usbdev;
1883         urb->status = 0;
1884         if (this_usbduxsub->pwm_cmd_running) {
1885                 ret = usb_submit_urb(urb, GFP_ATOMIC);
1886                 if (ret < 0) {
1887                         dev_err(&this_usbduxsub->interface->dev,
1888                                 "comedi_: pwm urb resubm failed in int-cont. "
1889                                 "ret=%d", ret);
1890                         if (ret == EL2NSYNC)
1891                                 dev_err(&this_usbduxsub->interface->dev,
1892                                         "buggy USB host controller or bug in "
1893                                         "IRQ handling!\n");
1894
1895                         /* don't do an unlink here */
1896                         usbdux_pwm_stop(this_usbduxsub, 0);
1897                 }
1898         }
1899 }
1900
1901 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
1902 {
1903         int errFlag;
1904
1905         if (!usbduxsub)
1906                 return -EFAULT;
1907
1908         dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
1909
1910         /* in case of a resubmission after an unlink... */
1911         usb_fill_bulk_urb(usbduxsub->urbPwm,
1912                           usbduxsub->usbdev,
1913                           usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
1914                           usbduxsub->urbPwm->transfer_buffer,
1915                           usbduxsub->sizePwmBuf, usbduxsub_pwm_irq,
1916                           usbduxsub->comedidev);
1917
1918         errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
1919         if (errFlag) {
1920                 dev_err(&usbduxsub->interface->dev,
1921                         "comedi_: usbdux: pwm: usb_submit_urb error %d\n",
1922                         errFlag);
1923                 return errFlag;
1924         }
1925         return 0;
1926 }
1927
1928 static int usbdux_pwm_period(struct comedi_device *dev,
1929                              struct comedi_subdevice *s, unsigned int period)
1930 {
1931         struct usbduxsub *this_usbduxsub = dev->private;
1932         int fx2delay = 255;
1933
1934         if (period < MIN_PWM_PERIOD) {
1935                 dev_err(&this_usbduxsub->interface->dev,
1936                         "comedi%d: illegal period setting for pwm.\n",
1937                         dev->minor);
1938                 return -EAGAIN;
1939         } else {
1940                 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1941                 if (fx2delay > 255) {
1942                         dev_err(&this_usbduxsub->interface->dev,
1943                                 "comedi%d: period %d for pwm is too low.\n",
1944                                 dev->minor, period);
1945                         return -EAGAIN;
1946                 }
1947         }
1948         this_usbduxsub->pwmDelay = fx2delay;
1949         this_usbduxsub->pwmPeriod = period;
1950         dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
1951                 __func__, period, fx2delay);
1952         return 0;
1953 }
1954
1955 /* is called from insn so there's no need to do all the sanity checks */
1956 static int usbdux_pwm_start(struct comedi_device *dev,
1957                             struct comedi_subdevice *s)
1958 {
1959         int ret, i;
1960         struct usbduxsub *this_usbduxsub = dev->private;
1961
1962         dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
1963                 dev->minor, __func__);
1964
1965         if (this_usbduxsub->pwm_cmd_running) {
1966                 /* already running */
1967                 return 0;
1968         }
1969
1970         this_usbduxsub->dux_commands[1] = ((int8_t) this_usbduxsub->pwmDelay);
1971         ret = send_dux_commands(this_usbduxsub, SENDPWMON);
1972         if (ret < 0)
1973                 return ret;
1974
1975         /* initialise the buffer */
1976         for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
1977                 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
1978
1979         this_usbduxsub->pwm_cmd_running = 1;
1980         ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
1981         if (ret < 0) {
1982                 this_usbduxsub->pwm_cmd_running = 0;
1983                 return ret;
1984         }
1985         return 0;
1986 }
1987
1988 /* generates the bit pattern for PWM with the optional sign bit */
1989 static int usbdux_pwm_pattern(struct comedi_device *dev,
1990                               struct comedi_subdevice *s, int channel,
1991                               unsigned int value, unsigned int sign)
1992 {
1993         struct usbduxsub *this_usbduxsub = dev->private;
1994         int i, szbuf;
1995         char *pBuf;
1996         char pwm_mask;
1997         char sgn_mask;
1998         char c;
1999
2000         if (!this_usbduxsub)
2001                 return -EFAULT;
2002
2003         /* this is the DIO bit which carries the PWM data */
2004         pwm_mask = (1 << channel);
2005         /* this is the DIO bit which carries the optional direction bit */
2006         sgn_mask = (16 << channel);
2007         /* this is the buffer which will be filled with the with bit */
2008         /* pattern for one period */
2009         szbuf = this_usbduxsub->sizePwmBuf;
2010         pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2011         for (i = 0; i < szbuf; i++) {
2012                 c = *pBuf;
2013                 /* reset bits */
2014                 c = c & (~pwm_mask);
2015                 /* set the bit as long as the index is lower than the value */
2016                 if (i < value)
2017                         c = c | pwm_mask;
2018                 /* set the optional sign bit for a relay */
2019                 if (!sign) {
2020                         /* positive value */
2021                         c = c & (~sgn_mask);
2022                 } else {
2023                         /* negative value */
2024                         c = c | sgn_mask;
2025                 }
2026                 *(pBuf++) = c;
2027         }
2028         return 1;
2029 }
2030
2031 static int usbdux_pwm_write(struct comedi_device *dev,
2032                             struct comedi_subdevice *s,
2033                             struct comedi_insn *insn, unsigned int *data)
2034 {
2035         struct usbduxsub *this_usbduxsub = dev->private;
2036
2037         if (!this_usbduxsub)
2038                 return -EFAULT;
2039
2040         if ((insn->n) != 1) {
2041                 /*
2042                  * doesn't make sense to have more than one value here because
2043                  * it would just overwrite the PWM buffer a couple of times
2044                  */
2045                 return -EINVAL;
2046         }
2047
2048         /*
2049          * the sign is set via a special INSN only, this gives us 8 bits for
2050          * normal operation
2051          * relay sign 0 by default
2052          */
2053         return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
2054 }
2055
2056 static int usbdux_pwm_read(struct comedi_device *x1,
2057                            struct comedi_subdevice *x2, struct comedi_insn *x3,
2058                            unsigned int *x4)
2059 {
2060         /* not needed */
2061         return -EINVAL;
2062 };
2063
2064 /* switches on/off PWM */
2065 static int usbdux_pwm_config(struct comedi_device *dev,
2066                              struct comedi_subdevice *s,
2067                              struct comedi_insn *insn, unsigned int *data)
2068 {
2069         struct usbduxsub *this_usbduxsub = dev->private;
2070         switch (data[0]) {
2071         case INSN_CONFIG_ARM:
2072                 /* switch it on */
2073                 dev_dbg(&this_usbduxsub->interface->dev,
2074                         "comedi%d: %s: pwm on\n", dev->minor, __func__);
2075                 /*
2076                  * if not zero the PWM is limited to a certain time which is
2077                  * not supported here
2078                  */
2079                 if (data[1] != 0)
2080                         return -EINVAL;
2081                 return usbdux_pwm_start(dev, s);
2082         case INSN_CONFIG_DISARM:
2083                 dev_dbg(&this_usbduxsub->interface->dev,
2084                         "comedi%d: %s: pwm off\n", dev->minor, __func__);
2085                 return usbdux_pwm_cancel(dev, s);
2086         case INSN_CONFIG_GET_PWM_STATUS:
2087                 /*
2088                  * to check if the USB transmission has failed or in case PWM
2089                  * was limited to n cycles to check if it has terminated
2090                  */
2091                 data[1] = this_usbduxsub->pwm_cmd_running;
2092                 return 0;
2093         case INSN_CONFIG_PWM_SET_PERIOD:
2094                 dev_dbg(&this_usbduxsub->interface->dev,
2095                         "comedi%d: %s: setting period\n", dev->minor, __func__);
2096                 return usbdux_pwm_period(dev, s, data[1]);
2097         case INSN_CONFIG_PWM_GET_PERIOD:
2098                 data[1] = this_usbduxsub->pwmPeriod;
2099                 return 0;
2100         case INSN_CONFIG_PWM_SET_H_BRIDGE:
2101                 /* value in the first byte and the sign in the second for a
2102                    relay */
2103                 return usbdux_pwm_pattern(dev, s,
2104                                           /* the channel number */
2105                                           CR_CHAN(insn->chanspec),
2106                                           /* actual PWM data */
2107                                           data[1],
2108                                           /* just a sign */
2109                                           (data[2] != 0));
2110         case INSN_CONFIG_PWM_GET_H_BRIDGE:
2111                 /* values are not kept in this driver, nothing to return here */
2112                 return -EINVAL;
2113         }
2114         return -EINVAL;
2115 }
2116
2117 /* end of PWM */
2118 /*****************************************************************/
2119
2120 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2121 {
2122         int i;
2123
2124         if (!usbduxsub_tmp)
2125                 return;
2126         dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2127
2128         /* shows the usb subsystem that the driver is down */
2129         if (usbduxsub_tmp->interface)
2130                 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2131
2132         usbduxsub_tmp->probed = 0;
2133
2134         if (usbduxsub_tmp->urbIn) {
2135                 if (usbduxsub_tmp->ai_cmd_running) {
2136                         usbduxsub_tmp->ai_cmd_running = 0;
2137                         usbduxsub_unlink_InURBs(usbduxsub_tmp);
2138                 }
2139                 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2140                         kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2141                         usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2142                         usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2143                         usb_free_urb(usbduxsub_tmp->urbIn[i]);
2144                         usbduxsub_tmp->urbIn[i] = NULL;
2145                 }
2146                 kfree(usbduxsub_tmp->urbIn);
2147                 usbduxsub_tmp->urbIn = NULL;
2148         }
2149         if (usbduxsub_tmp->urbOut) {
2150                 if (usbduxsub_tmp->ao_cmd_running) {
2151                         usbduxsub_tmp->ao_cmd_running = 0;
2152                         usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2153                 }
2154                 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2155                         kfree(usbduxsub_tmp->urbOut[i]->transfer_buffer);
2156                         usbduxsub_tmp->urbOut[i]->transfer_buffer = NULL;
2157                         if (usbduxsub_tmp->urbOut[i]) {
2158                                 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2159                                 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2160                                 usbduxsub_tmp->urbOut[i] = NULL;
2161                         }
2162                 }
2163                 kfree(usbduxsub_tmp->urbOut);
2164                 usbduxsub_tmp->urbOut = NULL;
2165         }
2166         if (usbduxsub_tmp->urbPwm) {
2167                 if (usbduxsub_tmp->pwm_cmd_running) {
2168                         usbduxsub_tmp->pwm_cmd_running = 0;
2169                         usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2170                 }
2171                 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2172                 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2173                 usb_kill_urb(usbduxsub_tmp->urbPwm);
2174                 usb_free_urb(usbduxsub_tmp->urbPwm);
2175                 usbduxsub_tmp->urbPwm = NULL;
2176         }
2177         kfree(usbduxsub_tmp->inBuffer);
2178         usbduxsub_tmp->inBuffer = NULL;
2179         kfree(usbduxsub_tmp->insnBuffer);
2180         usbduxsub_tmp->insnBuffer = NULL;
2181         kfree(usbduxsub_tmp->outBuffer);
2182         usbduxsub_tmp->outBuffer = NULL;
2183         kfree(usbduxsub_tmp->dac_commands);
2184         usbduxsub_tmp->dac_commands = NULL;
2185         kfree(usbduxsub_tmp->dux_commands);
2186         usbduxsub_tmp->dux_commands = NULL;
2187         usbduxsub_tmp->ai_cmd_running = 0;
2188         usbduxsub_tmp->ao_cmd_running = 0;
2189         usbduxsub_tmp->pwm_cmd_running = 0;
2190 }
2191
2192 static int usbdux_attach_common(struct comedi_device *dev,
2193                                 struct usbduxsub *udev)
2194 {
2195         int ret;
2196         struct comedi_subdevice *s = NULL;
2197         int n_subdevs;
2198
2199         down(&udev->sem);
2200         /* pointer back to the corresponding comedi device */
2201         udev->comedidev = dev;
2202
2203         dev->board_name = "usbdux";
2204
2205         /* set number of subdevices */
2206         if (udev->high_speed) {
2207                 /* with pwm */
2208                 n_subdevs = 5;
2209         } else {
2210                 /* without pwm */
2211                 n_subdevs = 4;
2212         }
2213
2214         ret = comedi_alloc_subdevices(dev, n_subdevs);
2215         if (ret) {
2216                 up(&udev->sem);
2217                 return ret;
2218         }
2219
2220         /* private structure is also simply the usb-structure */
2221         dev->private = udev;
2222
2223         /* the first subdevice is the A/D converter */
2224         s = &dev->subdevices[SUBDEV_AD];
2225         /* the URBs get the comedi subdevice */
2226         /* which is responsible for reading */
2227         /* this is the subdevice which reads data */
2228         dev->read_subdev = s;
2229         /* the subdevice receives as private structure the */
2230         /* usb-structure */
2231         s->private = NULL;
2232         /* analog input */
2233         s->type = COMEDI_SUBD_AI;
2234         /* readable and ref is to ground */
2235         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
2236         /* 8 channels */
2237         s->n_chan = 8;
2238         /* length of the channellist */
2239         s->len_chanlist = 8;
2240         /* callback functions */
2241         s->insn_read = usbdux_ai_insn_read;
2242         s->do_cmdtest = usbdux_ai_cmdtest;
2243         s->do_cmd = usbdux_ai_cmd;
2244         s->cancel = usbdux_ai_cancel;
2245         /* max value from the A/D converter (12bit) */
2246         s->maxdata = 0xfff;
2247         /* range table to convert to physical units */
2248         s->range_table = (&range_usbdux_ai_range);
2249
2250         /* analog out */
2251         s = &dev->subdevices[SUBDEV_DA];
2252         /* analog out */
2253         s->type = COMEDI_SUBD_AO;
2254         /* backward pointer */
2255         dev->write_subdev = s;
2256         /* the subdevice receives as private structure the */
2257         /* usb-structure */
2258         s->private = NULL;
2259         /* are writable */
2260         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2261         /* 4 channels */
2262         s->n_chan = 4;
2263         /* length of the channellist */
2264         s->len_chanlist = 4;
2265         /* 12 bit resolution */
2266         s->maxdata = 0x0fff;
2267         /* bipolar range */
2268         s->range_table = (&range_usbdux_ao_range);
2269         /* callback */
2270         s->do_cmdtest = usbdux_ao_cmdtest;
2271         s->do_cmd = usbdux_ao_cmd;
2272         s->cancel = usbdux_ao_cancel;
2273         s->insn_read = usbdux_ao_insn_read;
2274         s->insn_write = usbdux_ao_insn_write;
2275
2276         /* digital I/O */
2277         s = &dev->subdevices[SUBDEV_DIO];
2278         s->type = COMEDI_SUBD_DIO;
2279         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2280         s->n_chan = 8;
2281         s->maxdata = 1;
2282         s->range_table = (&range_digital);
2283         s->insn_bits = usbdux_dio_insn_bits;
2284         s->insn_config = usbdux_dio_insn_config;
2285         /* we don't use it */
2286         s->private = NULL;
2287
2288         /* counter */
2289         s = &dev->subdevices[SUBDEV_COUNTER];
2290         s->type = COMEDI_SUBD_COUNTER;
2291         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2292         s->n_chan = 4;
2293         s->maxdata = 0xFFFF;
2294         s->insn_read = usbdux_counter_read;
2295         s->insn_write = usbdux_counter_write;
2296         s->insn_config = usbdux_counter_config;
2297
2298         if (udev->high_speed) {
2299                 /* timer / pwm */
2300                 s = &dev->subdevices[SUBDEV_PWM];
2301                 s->type = COMEDI_SUBD_PWM;
2302                 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2303                 s->n_chan = 8;
2304                 /* this defines the max duty cycle resolution */
2305                 s->maxdata = udev->sizePwmBuf;
2306                 s->insn_write = usbdux_pwm_write;
2307                 s->insn_read = usbdux_pwm_read;
2308                 s->insn_config = usbdux_pwm_config;
2309                 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2310         }
2311         /* finally decide that it's attached */
2312         udev->attached = 1;
2313
2314         up(&udev->sem);
2315
2316         dev_info(&udev->interface->dev, "comedi%d: attached to usbdux.\n",
2317                  dev->minor);
2318
2319         return 0;
2320 }
2321
2322 static int usbdux_auto_attach(struct comedi_device *dev,
2323                               unsigned long context_unused)
2324 {
2325         struct usb_interface *uinterf = comedi_to_usb_interface(dev);
2326         int ret;
2327         struct usbduxsub *this_usbduxsub;
2328
2329         dev->private = NULL;
2330
2331         down(&start_stop_sem);
2332         this_usbduxsub = usb_get_intfdata(uinterf);
2333         if (!this_usbduxsub || !this_usbduxsub->probed) {
2334                 dev_err(dev->class_dev,
2335                         "usbdux: error: auto_attach failed, not connected\n");
2336                 ret = -ENODEV;
2337         } else if (this_usbduxsub->attached) {
2338                 dev_err(dev->class_dev,
2339                         "error: auto_attach failed, already attached\n");
2340                 ret = -ENODEV;
2341         } else
2342                 ret = usbdux_attach_common(dev, this_usbduxsub);
2343         up(&start_stop_sem);
2344         return ret;
2345 }
2346
2347 static void usbdux_detach(struct comedi_device *dev)
2348 {
2349         struct usbduxsub *usb = dev->private;
2350
2351         if (usb) {
2352                 down(&usb->sem);
2353                 dev->private = NULL;
2354                 usb->attached = 0;
2355                 usb->comedidev = NULL;
2356                 up(&usb->sem);
2357         }
2358 }
2359
2360 static struct comedi_driver usbdux_driver = {
2361         .driver_name    = "usbdux",
2362         .module         = THIS_MODULE,
2363         .auto_attach    = usbdux_auto_attach,
2364         .detach         = usbdux_detach,
2365 };
2366
2367 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2368                                                      void *context)
2369 {
2370         struct usbduxsub *usbduxsub_tmp = context;
2371         struct usb_interface *uinterf = usbduxsub_tmp->interface;
2372         int ret;
2373
2374         if (fw == NULL) {
2375                 dev_err(&uinterf->dev,
2376                         "Firmware complete handler without firmware!\n");
2377                 return;
2378         }
2379
2380         /*
2381          * we need to upload the firmware here because fw will be
2382          * freed once we've left this function
2383          */
2384         ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2385
2386         if (ret) {
2387                 dev_err(&uinterf->dev,
2388                         "Could not upload firmware (err=%d)\n", ret);
2389                 goto out;
2390         }
2391         comedi_usb_auto_config(uinterf, &usbdux_driver);
2392  out:
2393         release_firmware(fw);
2394 }
2395
2396 static int usbdux_usb_probe(struct usb_interface *uinterf,
2397                             const struct usb_device_id *id)
2398 {
2399         struct usb_device *udev = interface_to_usbdev(uinterf);
2400         struct device *dev = &uinterf->dev;
2401         int i;
2402         int index;
2403         int ret;
2404
2405         dev_dbg(dev, "comedi_: usbdux_: "
2406                 "finding a free structure for the usb-device\n");
2407
2408         down(&start_stop_sem);
2409         /* look for a free place in the usbdux array */
2410         index = -1;
2411         for (i = 0; i < NUMUSBDUX; i++) {
2412                 if (!(usbduxsub[i].probed)) {
2413                         index = i;
2414                         break;
2415                 }
2416         }
2417
2418         /* no more space */
2419         if (index == -1) {
2420                 dev_err(dev, "Too many usbdux-devices connected.\n");
2421                 up(&start_stop_sem);
2422                 return -EMFILE;
2423         }
2424         dev_dbg(dev, "comedi_: usbdux: "
2425                 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2426
2427         sema_init(&(usbduxsub[index].sem), 1);
2428         /* save a pointer to the usb device */
2429         usbduxsub[index].usbdev = udev;
2430
2431         /* 2.6: save the interface itself */
2432         usbduxsub[index].interface = uinterf;
2433         /* get the interface number from the interface */
2434         usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2435         /* hand the private data over to the usb subsystem */
2436         /* will be needed for disconnect */
2437         usb_set_intfdata(uinterf, &(usbduxsub[index]));
2438
2439         dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2440
2441         /* test if it is high speed (USB 2.0) */
2442         usbduxsub[index].high_speed =
2443             (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2444
2445         /* create space for the commands of the DA converter */
2446         usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2447         if (!usbduxsub[index].dac_commands) {
2448                 dev_err(dev, "comedi_: usbdux: "
2449                         "error alloc space for dac commands\n");
2450                 tidy_up(&(usbduxsub[index]));
2451                 up(&start_stop_sem);
2452                 return -ENOMEM;
2453         }
2454         /* create space for the commands going to the usb device */
2455         usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2456         if (!usbduxsub[index].dux_commands) {
2457                 dev_err(dev, "comedi_: usbdux: "
2458                         "error alloc space for dux commands\n");
2459                 tidy_up(&(usbduxsub[index]));
2460                 up(&start_stop_sem);
2461                 return -ENOMEM;
2462         }
2463         /* create space for the in buffer and set it to zero */
2464         usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2465         if (!(usbduxsub[index].inBuffer)) {
2466                 dev_err(dev, "comedi_: usbdux: "
2467                         "could not alloc space for inBuffer\n");
2468                 tidy_up(&(usbduxsub[index]));
2469                 up(&start_stop_sem);
2470                 return -ENOMEM;
2471         }
2472         /* create space of the instruction buffer */
2473         usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2474         if (!(usbduxsub[index].insnBuffer)) {
2475                 dev_err(dev, "comedi_: usbdux: "
2476                         "could not alloc space for insnBuffer\n");
2477                 tidy_up(&(usbduxsub[index]));
2478                 up(&start_stop_sem);
2479                 return -ENOMEM;
2480         }
2481         /* create space for the outbuffer */
2482         usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2483         if (!(usbduxsub[index].outBuffer)) {
2484                 dev_err(dev, "comedi_: usbdux: "
2485                         "could not alloc space for outBuffer\n");
2486                 tidy_up(&(usbduxsub[index]));
2487                 up(&start_stop_sem);
2488                 return -ENOMEM;
2489         }
2490         /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2491         i = usb_set_interface(usbduxsub[index].usbdev,
2492                               usbduxsub[index].ifnum, 3);
2493         if (i < 0) {
2494                 dev_err(dev, "comedi_: usbdux%d: "
2495                         "could not set alternate setting 3 in high speed.\n",
2496                         index);
2497                 tidy_up(&(usbduxsub[index]));
2498                 up(&start_stop_sem);
2499                 return -ENODEV;
2500         }
2501         if (usbduxsub[index].high_speed)
2502                 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2503         else
2504                 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2505
2506         usbduxsub[index].urbIn =
2507             kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2508                     GFP_KERNEL);
2509         if (!(usbduxsub[index].urbIn)) {
2510                 dev_err(dev, "comedi_: usbdux: Could not alloc. urbIn array\n");
2511                 tidy_up(&(usbduxsub[index]));
2512                 up(&start_stop_sem);
2513                 return -ENOMEM;
2514         }
2515         for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2516                 /* one frame: 1ms */
2517                 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2518                 if (usbduxsub[index].urbIn[i] == NULL) {
2519                         dev_err(dev, "comedi_: usbdux%d: "
2520                                 "Could not alloc. urb(%d)\n", index, i);
2521                         tidy_up(&(usbduxsub[index]));
2522                         up(&start_stop_sem);
2523                         return -ENOMEM;
2524                 }
2525                 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2526                 /* will be filled later with a pointer to the comedi-device */
2527                 /* and ONLY then the urb should be submitted */
2528                 usbduxsub[index].urbIn[i]->context = NULL;
2529                 usbduxsub[index].urbIn[i]->pipe =
2530                     usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2531                 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2532                 usbduxsub[index].urbIn[i]->transfer_buffer =
2533                     kzalloc(SIZEINBUF, GFP_KERNEL);
2534                 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2535                         dev_err(dev, "comedi_: usbdux%d: "
2536                                 "could not alloc. transb.\n", index);
2537                         tidy_up(&(usbduxsub[index]));
2538                         up(&start_stop_sem);
2539                         return -ENOMEM;
2540                 }
2541                 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2542                 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2543                 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2544                 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2545                 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length = SIZEINBUF;
2546         }
2547
2548         /* out */
2549         if (usbduxsub[index].high_speed)
2550                 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2551         else
2552                 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2553
2554         usbduxsub[index].urbOut =
2555             kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2556                     GFP_KERNEL);
2557         if (!(usbduxsub[index].urbOut)) {
2558                 dev_err(dev, "comedi_: usbdux: "
2559                         "Could not alloc. urbOut array\n");
2560                 tidy_up(&(usbduxsub[index]));
2561                 up(&start_stop_sem);
2562                 return -ENOMEM;
2563         }
2564         for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2565                 /* one frame: 1ms */
2566                 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2567                 if (usbduxsub[index].urbOut[i] == NULL) {
2568                         dev_err(dev, "comedi_: usbdux%d: "
2569                                 "Could not alloc. urb(%d)\n", index, i);
2570                         tidy_up(&(usbduxsub[index]));
2571                         up(&start_stop_sem);
2572                         return -ENOMEM;
2573                 }
2574                 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2575                 /* will be filled later with a pointer to the comedi-device */
2576                 /* and ONLY then the urb should be submitted */
2577                 usbduxsub[index].urbOut[i]->context = NULL;
2578                 usbduxsub[index].urbOut[i]->pipe =
2579                     usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2580                 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2581                 usbduxsub[index].urbOut[i]->transfer_buffer =
2582                     kzalloc(SIZEOUTBUF, GFP_KERNEL);
2583                 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2584                         dev_err(dev, "comedi_: usbdux%d: "
2585                                 "could not alloc. transb.\n", index);
2586                         tidy_up(&(usbduxsub[index]));
2587                         up(&start_stop_sem);
2588                         return -ENOMEM;
2589                 }
2590                 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2591                 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2592                 usbduxsub[index].urbOut[i]->transfer_buffer_length = SIZEOUTBUF;
2593                 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2594                 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2595                     SIZEOUTBUF;
2596                 if (usbduxsub[index].high_speed) {
2597                         /* uframes */
2598                         usbduxsub[index].urbOut[i]->interval = 8;
2599                 } else {
2600                         /* frames */
2601                         usbduxsub[index].urbOut[i]->interval = 1;
2602                 }
2603         }
2604
2605         /* pwm */
2606         if (usbduxsub[index].high_speed) {
2607                 /* max bulk ep size in high speed */
2608                 usbduxsub[index].sizePwmBuf = 512;
2609                 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2610                 if (usbduxsub[index].urbPwm == NULL) {
2611                         dev_err(dev, "comedi_: usbdux%d: "
2612                                 "Could not alloc. pwm urb\n", index);
2613                         tidy_up(&(usbduxsub[index]));
2614                         up(&start_stop_sem);
2615                         return -ENOMEM;
2616                 }
2617                 usbduxsub[index].urbPwm->transfer_buffer =
2618                     kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2619                 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2620                         dev_err(dev, "comedi_: usbdux%d: "
2621                                 "could not alloc. transb. for pwm\n", index);
2622                         tidy_up(&(usbduxsub[index]));
2623                         up(&start_stop_sem);
2624                         return -ENOMEM;
2625                 }
2626         } else {
2627                 usbduxsub[index].urbPwm = NULL;
2628                 usbduxsub[index].sizePwmBuf = 0;
2629         }
2630
2631         usbduxsub[index].ai_cmd_running = 0;
2632         usbduxsub[index].ao_cmd_running = 0;
2633         usbduxsub[index].pwm_cmd_running = 0;
2634
2635         /* we've reached the bottom of the function */
2636         usbduxsub[index].probed = 1;
2637         up(&start_stop_sem);
2638
2639         ret = request_firmware_nowait(THIS_MODULE,
2640                                       FW_ACTION_HOTPLUG,
2641                                       FIRMWARE,
2642                                       &udev->dev,
2643                                       GFP_KERNEL,
2644                                       usbduxsub + index,
2645                                       usbdux_firmware_request_complete_handler);
2646
2647         if (ret) {
2648                 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2649                 return ret;
2650         }
2651
2652         dev_info(dev, "comedi_: usbdux%d "
2653                  "has been successfully initialised.\n", index);
2654         /* success */
2655         return 0;
2656 }
2657
2658 static void usbdux_usb_disconnect(struct usb_interface *intf)
2659 {
2660         struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2661         struct usb_device *udev = interface_to_usbdev(intf);
2662
2663         if (!usbduxsub_tmp) {
2664                 dev_err(&intf->dev,
2665                         "comedi_: disconnect called with null pointer.\n");
2666                 return;
2667         }
2668         if (usbduxsub_tmp->usbdev != udev) {
2669                 dev_err(&intf->dev, "comedi_: BUG! called with wrong ptr!!!\n");
2670                 return;
2671         }
2672         comedi_usb_auto_unconfig(intf);
2673         down(&start_stop_sem);
2674         down(&usbduxsub_tmp->sem);
2675         tidy_up(usbduxsub_tmp);
2676         up(&usbduxsub_tmp->sem);
2677         up(&start_stop_sem);
2678         dev_dbg(&intf->dev, "comedi_: disconnected from the usb\n");
2679 }
2680
2681 static const struct usb_device_id usbdux_usb_table[] = {
2682         { USB_DEVICE(0x13d8, 0x0001) },
2683         { USB_DEVICE(0x13d8, 0x0002) },
2684         { }
2685 };
2686
2687 MODULE_DEVICE_TABLE(usb, usbdux_usb_table);
2688
2689 static struct usb_driver usbdux_usb_driver = {
2690         .name           = "usbdux",
2691         .probe          = usbdux_usb_probe,
2692         .disconnect     = usbdux_usb_disconnect,
2693         .id_table       = usbdux_usb_table,
2694 };
2695 module_comedi_usb_driver(usbdux_driver, usbdux_usb_driver);
2696
2697 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
2698 MODULE_DESCRIPTION("Stirling/ITL USB-DUX -- Bernd.Porr@f2s.com");
2699 MODULE_LICENSE("GPL");
2700 MODULE_FIRMWARE(FIRMWARE);