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