clocksource: make CLOCKSOURCE_OF_DECLARE type safe
[cascardo/linux.git] / 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[16];
685
686         /* 7f92 to zero */
687         local_transfer_buffer[0] = 0;
688         errcode = usb_control_msg(usbduxsub->usbdev,
689                                   /* create a pipe for a control transfer */
690                                   usb_sndctrlpipe(usbduxsub->usbdev, 0),
691                                   /* bRequest, "Firmware" */
692                                   USBDUXSUB_FIRMWARE,
693                                   /* bmRequestType */
694                                   VENDOR_DIR_OUT,
695                                   /* Value */
696                                   USBDUXSUB_CPUCS,
697                                   /* Index */
698                                   0x0000,
699                                   /* address of the transfer buffer */
700                                   local_transfer_buffer,
701                                   /* Length */
702                                   1,
703                                   /* Timeout */
704                                   BULK_TIMEOUT);
705         if (errcode < 0) {
706                 dev_err(&usbduxsub->interface->dev,
707                         "comedi_: control msg failed (start)\n");
708                 return errcode;
709         }
710         return 0;
711 }
712
713 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
714 {
715         int errcode = 0;
716
717         uint8_t local_transfer_buffer[16];
718
719         /* 7f92 to one */
720         local_transfer_buffer[0] = 1;
721         errcode = usb_control_msg(usbduxsub->usbdev,
722                                   usb_sndctrlpipe(usbduxsub->usbdev, 0),
723                                   /* bRequest, "Firmware" */
724                                   USBDUXSUB_FIRMWARE,
725                                   /* bmRequestType */
726                                   VENDOR_DIR_OUT,
727                                   /* Value */
728                                   USBDUXSUB_CPUCS,
729                                   /* Index */
730                                   0x0000, local_transfer_buffer,
731                                   /* Length */
732                                   1,
733                                   /* Timeout */
734                                   BULK_TIMEOUT);
735         if (errcode < 0) {
736                 dev_err(&usbduxsub->interface->dev,
737                         "comedi_: control msg failed (stop)\n");
738                 return errcode;
739         }
740         return 0;
741 }
742
743 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
744                             uint8_t *local_transfer_buffer,
745                             unsigned int startAddr, unsigned int len)
746 {
747         int errcode;
748
749         errcode = usb_control_msg(usbduxsub->usbdev,
750                                   usb_sndctrlpipe(usbduxsub->usbdev, 0),
751                                   /* brequest, firmware */
752                                   USBDUXSUB_FIRMWARE,
753                                   /* bmRequestType */
754                                   VENDOR_DIR_OUT,
755                                   /* value */
756                                   startAddr,
757                                   /* index */
758                                   0x0000,
759                                   /* our local safe buffer */
760                                   local_transfer_buffer,
761                                   /* length */
762                                   len,
763                                   /* timeout */
764                                   BULK_TIMEOUT);
765         dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
766         if (errcode < 0) {
767                 dev_err(&usbduxsub->interface->dev,
768                         "comedi_: upload failed\n");
769                 return errcode;
770         }
771         return 0;
772 }
773
774 /* the FX2LP has twice as much as the standard FX2 */
775 #define FIRMWARE_MAX_LEN 0x4000
776
777 static int firmwareUpload(struct usbduxsub *usbduxsub,
778                           const u8 *firmwareBinary, int sizeFirmware)
779 {
780         int ret;
781         uint8_t *fwBuf;
782
783         if (!firmwareBinary)
784                 return 0;
785
786         if (sizeFirmware > FIRMWARE_MAX_LEN) {
787                 dev_err(&usbduxsub->interface->dev,
788                         "usbduxsigma firmware binary it too large for FX2.\n");
789                 return -ENOMEM;
790         }
791
792         /* we generate a local buffer for the firmware */
793         fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
794         if (!fwBuf) {
795                 dev_err(&usbduxsub->interface->dev,
796                         "comedi_: mem alloc for firmware failed\n");
797                 return -ENOMEM;
798         }
799
800         ret = usbduxsub_stop(usbduxsub);
801         if (ret < 0) {
802                 dev_err(&usbduxsub->interface->dev,
803                         "comedi_: can not stop firmware\n");
804                 kfree(fwBuf);
805                 return ret;
806         }
807
808         ret = usbduxsub_upload(usbduxsub, fwBuf, 0, sizeFirmware);
809         if (ret < 0) {
810                 dev_err(&usbduxsub->interface->dev,
811                         "comedi_: firmware upload failed\n");
812                 kfree(fwBuf);
813                 return ret;
814         }
815         ret = usbduxsub_start(usbduxsub);
816         if (ret < 0) {
817                 dev_err(&usbduxsub->interface->dev,
818                         "comedi_: can not start firmware\n");
819                 kfree(fwBuf);
820                 return ret;
821         }
822         kfree(fwBuf);
823         return 0;
824 }
825
826 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
827 {
828         int i, errFlag;
829
830         if (!usbduxsub)
831                 return -EFAULT;
832
833         /* Submit all URBs and start the transfer on the bus */
834         for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
835                 /* in case of a resubmission after an unlink... */
836                 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
837                 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
838                 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
839                 usbduxsub->urbIn[i]->status = 0;
840                 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
841                 dev_dbg(&usbduxsub->interface->dev,
842                         "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
843                         usbduxsub->comedidev->minor, i,
844                         (usbduxsub->urbIn[i]->context),
845                         (usbduxsub->urbIn[i]->dev),
846                         (usbduxsub->urbIn[i]->interval));
847                 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
848                 if (errFlag) {
849                         dev_err(&usbduxsub->interface->dev,
850                                 "comedi_: ai: usb_submit_urb(%d) error %d\n",
851                                 i, errFlag);
852                         return errFlag;
853                 }
854         }
855         return 0;
856 }
857
858 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
859 {
860         int i, errFlag;
861
862         if (!usbduxsub)
863                 return -EFAULT;
864
865         for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
866                 dev_dbg(&usbduxsub->interface->dev,
867                         "comedi_: submitting out-urb[%d]\n", i);
868                 /* in case of a resubmission after an unlink... */
869                 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
870                 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
871                 usbduxsub->urbOut[i]->status = 0;
872                 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
873                 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
874                 if (errFlag) {
875                         dev_err(&usbduxsub->interface->dev,
876                                 "comedi_: ao: usb_submit_urb(%d) error %d\n",
877                                 i, errFlag);
878                         return errFlag;
879                 }
880         }
881         return 0;
882 }
883
884 static int chanToInterval(int nChannels)
885 {
886         if (nChannels <= 2)
887                 /* 4kHz */
888                 return 2;
889         if (nChannels <= 8)
890                 /* 2kHz */
891                 return 4;
892         /* 1kHz */
893         return 8;
894 }
895
896 static int usbdux_ai_cmdtest(struct comedi_device *dev,
897                              struct comedi_subdevice *s,
898                              struct comedi_cmd *cmd)
899 {
900         struct usbduxsub *this_usbduxsub = dev->private;
901         int err = 0, i;
902         unsigned int tmpTimer;
903
904         if (!(this_usbduxsub->probed))
905                 return -ENODEV;
906
907         /* Step 1 : check if triggers are trivially valid */
908
909         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
910         err |= cfc_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
911         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
912         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
913         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
914
915         if (err)
916                 return 1;
917
918         /* Step 2a : make sure trigger sources are unique */
919
920         err |= cfc_check_trigger_is_unique(cmd->start_src);
921         err |= cfc_check_trigger_is_unique(cmd->stop_src);
922
923         /* Step 2b : and mutually compatible */
924
925         if (err)
926                 return 2;
927
928         /* Step 3: check if arguments are trivially valid */
929
930         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
931
932         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
933                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
934
935         if (cmd->scan_begin_src == TRIG_TIMER) {
936                 if (this_usbduxsub->high_speed) {
937                         /*
938                          * In high speed mode microframes are possible.
939                          * However, during one microframe we can roughly
940                          * sample two channels. Thus, the more channels
941                          * are in the channel list the more time we need.
942                          */
943                         i = chanToInterval(cmd->chanlist_len);
944                         err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
945                                                          (1000000 / 8 * i));
946                         /* now calc the real sampling rate with all the
947                          * rounding errors */
948                         tmpTimer =
949                             ((unsigned int)(cmd->scan_begin_arg / 125000)) *
950                             125000;
951                 } else {
952                         /* full speed */
953                         /* 1kHz scans every USB frame */
954                         err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
955                                                          1000000);
956                         /*
957                          * calc the real sampling rate with the rounding errors
958                          */
959                         tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
960                                                    1000000)) * 1000000;
961                 }
962                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg,
963                                                 tmpTimer);
964         }
965
966         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
967
968         if (cmd->stop_src == TRIG_COUNT) {
969                 /* any count is allowed */
970         } else {
971                 /* TRIG_NONE */
972                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
973         }
974
975         if (err)
976                 return 3;
977
978         return 0;
979 }
980
981 /*
982  * creates the ADC command for the MAX1271
983  * range is the range value from comedi
984  */
985 static void create_adc_command(unsigned int chan,
986                                uint8_t *muxsg0,
987                                uint8_t *muxsg1)
988 {
989         if (chan < 8)
990                 (*muxsg0) = (*muxsg0) | (1 << chan);
991         else if (chan < 16)
992                 (*muxsg1) = (*muxsg1) | (1 << (chan-8));
993 }
994
995
996 /* bulk transfers to usbdux */
997
998 #define SENDADCOMMANDS            0
999 #define SENDDACOMMANDS            1
1000 #define SENDDIOCONFIGCOMMAND      2
1001 #define SENDDIOBITSCOMMAND        3
1002 #define SENDSINGLEAD              4
1003 #define SENDPWMON                 7
1004 #define SENDPWMOFF                8
1005
1006 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1007 {
1008         int result, nsent;
1009
1010         this_usbduxsub->dux_commands[0] = cmd_type;
1011 #ifdef NOISY_DUX_DEBUGBUG
1012         printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1013                this_usbduxsub->comedidev->minor);
1014         for (result = 0; result < SIZEOFDUXBUFFER; result++)
1015                 printk(" %02x", this_usbduxsub->dux_commands[result]);
1016         printk("\n");
1017 #endif
1018         result = usb_bulk_msg(this_usbduxsub->usbdev,
1019                               usb_sndbulkpipe(this_usbduxsub->usbdev,
1020                                               COMMAND_OUT_EP),
1021                               this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1022                               &nsent, BULK_TIMEOUT);
1023         if (result < 0)
1024                 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1025                         "could not transmit dux_command to the usb-device, "
1026                         "err=%d\n", this_usbduxsub->comedidev->minor, result);
1027
1028         return result;
1029 }
1030
1031 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1032 {
1033         int result = (-EFAULT);
1034         int nrec;
1035         int i;
1036
1037         for (i = 0; i < RETRIES; i++) {
1038                 result = usb_bulk_msg(this_usbduxsub->usbdev,
1039                                       usb_rcvbulkpipe(this_usbduxsub->usbdev,
1040                                                       COMMAND_IN_EP),
1041                                       this_usbduxsub->insnBuffer, SIZEINSNBUF,
1042                                       &nrec, BULK_TIMEOUT);
1043                 if (result < 0) {
1044                         dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1045                                 "insn: USB error %d "
1046                                 "while receiving DUX command"
1047                                 "\n", this_usbduxsub->comedidev->minor,
1048                                 result);
1049                         return result;
1050                 }
1051                 if (this_usbduxsub->insnBuffer[0] == command)
1052                         return result;
1053         }
1054         /* this is only reached if the data has been requested a couple of
1055          * times */
1056         dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1057                 "wrong data returned from firmware: want %d, got %d.\n",
1058                 this_usbduxsub->comedidev->minor, command,
1059                 this_usbduxsub->insnBuffer[0]);
1060         return -EFAULT;
1061 }
1062
1063 static int usbdux_ai_inttrig(struct comedi_device *dev,
1064                              struct comedi_subdevice *s, unsigned int trignum)
1065 {
1066         int ret;
1067         struct usbduxsub *this_usbduxsub = dev->private;
1068         if (!this_usbduxsub)
1069                 return -EFAULT;
1070
1071         down(&this_usbduxsub->sem);
1072         if (!(this_usbduxsub->probed)) {
1073                 up(&this_usbduxsub->sem);
1074                 return -ENODEV;
1075         }
1076         dev_dbg(&this_usbduxsub->interface->dev,
1077                 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1078
1079         if (trignum != 0) {
1080                 dev_err(&this_usbduxsub->interface->dev,
1081                         "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1082                         dev->minor);
1083                 up(&this_usbduxsub->sem);
1084                 return -EINVAL;
1085         }
1086         if (!(this_usbduxsub->ai_cmd_running)) {
1087                 this_usbduxsub->ai_cmd_running = 1;
1088                 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1089                 if (ret < 0) {
1090                         dev_err(&this_usbduxsub->interface->dev,
1091                                 "comedi%d: usbdux_ai_inttrig: "
1092                                 "urbSubmit: err=%d\n", dev->minor, ret);
1093                         this_usbduxsub->ai_cmd_running = 0;
1094                         up(&this_usbduxsub->sem);
1095                         return ret;
1096                 }
1097                 s->async->inttrig = NULL;
1098         } else {
1099                 dev_err(&this_usbduxsub->interface->dev,
1100                         "comedi%d: ai_inttrig but acqu is already running\n",
1101                         dev->minor);
1102         }
1103         up(&this_usbduxsub->sem);
1104         return 1;
1105 }
1106
1107 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1108 {
1109         struct comedi_cmd *cmd = &s->async->cmd;
1110         unsigned int chan;
1111         int i, ret;
1112         struct usbduxsub *this_usbduxsub = dev->private;
1113         int result;
1114         uint8_t muxsg0 = 0;
1115         uint8_t muxsg1 = 0;
1116         uint8_t sysred = 0;
1117
1118         if (!this_usbduxsub)
1119                 return -EFAULT;
1120
1121         dev_dbg(&this_usbduxsub->interface->dev,
1122                 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1123
1124         /* block other CPUs from starting an ai_cmd */
1125         down(&this_usbduxsub->sem);
1126
1127         if (!(this_usbduxsub->probed)) {
1128                 up(&this_usbduxsub->sem);
1129                 return -ENODEV;
1130         }
1131         if (this_usbduxsub->ai_cmd_running) {
1132                 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1133                         "ai_cmd not possible. Another ai_cmd is running.\n",
1134                         dev->minor);
1135                 up(&this_usbduxsub->sem);
1136                 return -EBUSY;
1137         }
1138         /* set current channel of the running acquisition to zero */
1139         s->async->cur_chan = 0;
1140
1141         /* first the number of channels per time step */
1142         this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1143
1144         /* CONFIG0 */
1145         this_usbduxsub->dux_commands[2] = 0x12;
1146
1147         /* CONFIG1: 23kHz sampling rate, delay = 0us,  */
1148         this_usbduxsub->dux_commands[3] = 0x03;
1149
1150         /* CONFIG3: differential channels off */
1151         this_usbduxsub->dux_commands[4] = 0x00;
1152
1153         for (i = 0; i < cmd->chanlist_len; i++) {
1154                 chan = CR_CHAN(cmd->chanlist[i]);
1155                 create_adc_command(chan, &muxsg0, &muxsg1);
1156                 if (i >= NUMCHANNELS) {
1157                         dev_err(&this_usbduxsub->interface->dev,
1158                                 "comedi%d: channel list too long\n",
1159                                 dev->minor);
1160                         break;
1161                 }
1162         }
1163         this_usbduxsub->dux_commands[5] = muxsg0;
1164         this_usbduxsub->dux_commands[6] = muxsg1;
1165         this_usbduxsub->dux_commands[7] = sysred;
1166
1167         dev_dbg(&this_usbduxsub->interface->dev,
1168                 "comedi %d: sending commands to the usb device: size=%u\n",
1169                 dev->minor, NUMCHANNELS);
1170
1171         result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1172         if (result < 0) {
1173                 up(&this_usbduxsub->sem);
1174                 return result;
1175         }
1176
1177         if (this_usbduxsub->high_speed) {
1178                 /*
1179                  * every 2 channels get a time window of 125us. Thus, if we
1180                  * sample all 16 channels we need 1ms. If we sample only one
1181                  * channel we need only 125us
1182                  */
1183                 this_usbduxsub->ai_interval =
1184                         chanToInterval(cmd->chanlist_len);
1185                 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1186                                                           (this_usbduxsub->
1187                                                            ai_interval));
1188         } else {
1189                 /* interval always 1ms */
1190                 this_usbduxsub->ai_interval = 1;
1191                 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1192         }
1193         if (this_usbduxsub->ai_timer < 1) {
1194                 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1195                         "timer=%d, scan_begin_arg=%d. "
1196                         "Not properly tested by cmdtest?\n", dev->minor,
1197                         this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1198                 up(&this_usbduxsub->sem);
1199                 return -EINVAL;
1200         }
1201         this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1202
1203         if (cmd->stop_src == TRIG_COUNT) {
1204                 /* data arrives as one packet */
1205                 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1206                 this_usbduxsub->ai_continuous = 0;
1207         } else {
1208                 /* continuous acquisition */
1209                 this_usbduxsub->ai_continuous = 1;
1210                 this_usbduxsub->ai_sample_count = 0;
1211         }
1212
1213         if (cmd->start_src == TRIG_NOW) {
1214                 /* enable this acquisition operation */
1215                 this_usbduxsub->ai_cmd_running = 1;
1216                 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1217                 if (ret < 0) {
1218                         this_usbduxsub->ai_cmd_running = 0;
1219                         /* fixme: unlink here?? */
1220                         up(&this_usbduxsub->sem);
1221                         return ret;
1222                 }
1223                 s->async->inttrig = NULL;
1224         } else {
1225                 /* TRIG_INT */
1226                 /* don't enable the acquision operation */
1227                 /* wait for an internal signal */
1228                 s->async->inttrig = usbdux_ai_inttrig;
1229         }
1230         up(&this_usbduxsub->sem);
1231         return 0;
1232 }
1233
1234 /* Mode 0 is used to get a single conversion on demand */
1235 static int usbdux_ai_insn_read(struct comedi_device *dev,
1236                                struct comedi_subdevice *s,
1237                                struct comedi_insn *insn, unsigned int *data)
1238 {
1239         int i;
1240         int32_t one = 0;
1241         int chan;
1242         int err;
1243         struct usbduxsub *this_usbduxsub = dev->private;
1244         uint8_t muxsg0 = 0;
1245         uint8_t muxsg1 = 0;
1246         uint8_t sysred = 0;
1247
1248         if (!this_usbduxsub)
1249                 return 0;
1250
1251         dev_dbg(&this_usbduxsub->interface->dev,
1252                 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1253                 dev->minor, insn->n, insn->subdev);
1254
1255         down(&this_usbduxsub->sem);
1256         if (!(this_usbduxsub->probed)) {
1257                 up(&this_usbduxsub->sem);
1258                 return -ENODEV;
1259         }
1260         if (this_usbduxsub->ai_cmd_running) {
1261                 dev_err(&this_usbduxsub->interface->dev,
1262                         "comedi%d: ai_insn_read not possible. "
1263                         "Async Command is running.\n", dev->minor);
1264                 up(&this_usbduxsub->sem);
1265                 return 0;
1266         }
1267
1268         /* sample one channel */
1269         /* CONFIG0: chopper on */
1270         this_usbduxsub->dux_commands[1] = 0x16;
1271
1272         /* CONFIG1: 2kHz sampling rate */
1273         this_usbduxsub->dux_commands[2] = 0x80;
1274
1275         /* CONFIG3: differential channels off */
1276         this_usbduxsub->dux_commands[3] = 0x00;
1277
1278         chan = CR_CHAN(insn->chanspec);
1279         create_adc_command(chan, &muxsg0, &muxsg1);
1280
1281         this_usbduxsub->dux_commands[4] = muxsg0;
1282         this_usbduxsub->dux_commands[5] = muxsg1;
1283         this_usbduxsub->dux_commands[6] = sysred;
1284
1285         /* adc commands */
1286         err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1287         if (err < 0) {
1288                 up(&this_usbduxsub->sem);
1289                 return err;
1290         }
1291
1292         for (i = 0; i < insn->n; i++) {
1293                 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1294                 if (err < 0) {
1295                         up(&this_usbduxsub->sem);
1296                         return 0;
1297                 }
1298                 /* 32 bits big endian from the A/D converter */
1299                 one = be32_to_cpu(*((int32_t *)
1300                                     ((this_usbduxsub->insnBuffer)+1)));
1301                 /* mask out the status byte */
1302                 one = one & 0x00ffffff;
1303                 /* turn it into an unsigned integer */
1304                 one = one ^ 0x00800000;
1305                 data[i] = one;
1306         }
1307         up(&this_usbduxsub->sem);
1308         return i;
1309 }
1310
1311
1312
1313
1314 static int usbdux_getstatusinfo(struct comedi_device *dev, int chan)
1315 {
1316         struct usbduxsub *this_usbduxsub = dev->private;
1317         uint8_t sysred = 0;
1318         uint32_t one;
1319         int err;
1320
1321         if (!this_usbduxsub)
1322                 return 0;
1323
1324         if (this_usbduxsub->ai_cmd_running) {
1325                 dev_err(&this_usbduxsub->interface->dev,
1326                         "comedi%d: status read not possible. "
1327                         "Async Command is running.\n", dev->minor);
1328                 return 0;
1329         }
1330
1331         /* CONFIG0 */
1332         this_usbduxsub->dux_commands[1] = 0x12;
1333
1334         /* CONFIG1: 2kHz sampling rate */
1335         this_usbduxsub->dux_commands[2] = 0x80;
1336
1337         /* CONFIG3: differential channels off */
1338         this_usbduxsub->dux_commands[3] = 0x00;
1339
1340         if (chan == 1) {
1341                 /* ADC offset */
1342                 sysred = sysred | 1;
1343         } else if (chan == 2) {
1344                 /* VCC */
1345                 sysred = sysred | 4;
1346         } else if (chan == 3) {
1347                 /* temperature */
1348                 sysred = sysred | 8;
1349         } else if (chan == 4) {
1350                 /* gain */
1351                 sysred = sysred | 16;
1352         } else if (chan == 5) {
1353                 /* ref */
1354                 sysred = sysred | 32;
1355         }
1356
1357         this_usbduxsub->dux_commands[4] = 0;
1358         this_usbduxsub->dux_commands[5] = 0;
1359         this_usbduxsub->dux_commands[6] = sysred;
1360
1361         /* adc commands */
1362         err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1363         if (err < 0)
1364                 return err;
1365
1366         err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1367         if (err < 0)
1368                 return err;
1369
1370         /* 32 bits big endian from the A/D converter */
1371         one = be32_to_cpu(*((int32_t *)((this_usbduxsub->insnBuffer)+1)));
1372         /* mask out the staus byte */
1373         one = one & 0x00ffffff;
1374         one = one ^ 0x00800000;
1375
1376         return (int)one;
1377 }
1378
1379
1380
1381
1382
1383
1384 /************************************/
1385 /* analog out */
1386
1387 static int usbdux_ao_insn_read(struct comedi_device *dev,
1388                                struct comedi_subdevice *s,
1389                                struct comedi_insn *insn, unsigned int *data)
1390 {
1391         int i;
1392         int chan = CR_CHAN(insn->chanspec);
1393         struct usbduxsub *this_usbduxsub = dev->private;
1394
1395         if (!this_usbduxsub)
1396                 return -EFAULT;
1397
1398         down(&this_usbduxsub->sem);
1399         if (!(this_usbduxsub->probed)) {
1400                 up(&this_usbduxsub->sem);
1401                 return -ENODEV;
1402         }
1403         for (i = 0; i < insn->n; i++)
1404                 data[i] = this_usbduxsub->outBuffer[chan];
1405
1406         up(&this_usbduxsub->sem);
1407         return i;
1408 }
1409
1410 static int usbdux_ao_insn_write(struct comedi_device *dev,
1411                                 struct comedi_subdevice *s,
1412                                 struct comedi_insn *insn, unsigned int *data)
1413 {
1414         int i, err;
1415         int chan = CR_CHAN(insn->chanspec);
1416         struct usbduxsub *this_usbduxsub = dev->private;
1417
1418         if (!this_usbduxsub)
1419                 return -EFAULT;
1420
1421         dev_dbg(&this_usbduxsub->interface->dev,
1422                 "comedi%d: ao_insn_write\n", dev->minor);
1423
1424         down(&this_usbduxsub->sem);
1425         if (!(this_usbduxsub->probed)) {
1426                 up(&this_usbduxsub->sem);
1427                 return -ENODEV;
1428         }
1429         if (this_usbduxsub->ao_cmd_running) {
1430                 dev_err(&this_usbduxsub->interface->dev,
1431                         "comedi%d: ao_insn_write: "
1432                         "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1433                 up(&this_usbduxsub->sem);
1434                 return 0;
1435         }
1436
1437         for (i = 0; i < insn->n; i++) {
1438                 dev_dbg(&this_usbduxsub->interface->dev,
1439                         "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1440                         dev->minor, chan, i, data[i]);
1441
1442                 /* number of channels: 1 */
1443                 this_usbduxsub->dux_commands[1] = 1;
1444                 /* channel number */
1445                 this_usbduxsub->dux_commands[2] = data[i];
1446                 this_usbduxsub->outBuffer[chan] = data[i];
1447                 this_usbduxsub->dux_commands[3] = chan;
1448                 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1449                 if (err < 0) {
1450                         up(&this_usbduxsub->sem);
1451                         return err;
1452                 }
1453         }
1454         up(&this_usbduxsub->sem);
1455
1456         return i;
1457 }
1458
1459 static int usbdux_ao_inttrig(struct comedi_device *dev,
1460                              struct comedi_subdevice *s, unsigned int trignum)
1461 {
1462         int ret;
1463         struct usbduxsub *this_usbduxsub = dev->private;
1464
1465         if (!this_usbduxsub)
1466                 return -EFAULT;
1467
1468         down(&this_usbduxsub->sem);
1469
1470         if (!(this_usbduxsub->probed)) {
1471                 ret = -ENODEV;
1472                 goto out;
1473         }
1474         if (trignum != 0) {
1475                 dev_err(&this_usbduxsub->interface->dev,
1476                         "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1477                         dev->minor);
1478                 ret = -EINVAL;
1479                 goto out;
1480         }
1481         if (!(this_usbduxsub->ao_cmd_running)) {
1482                 this_usbduxsub->ao_cmd_running = 1;
1483                 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1484                 if (ret < 0) {
1485                         dev_err(&this_usbduxsub->interface->dev,
1486                                 "comedi%d: usbdux_ao_inttrig: submitURB: "
1487                                 "err=%d\n", dev->minor, ret);
1488                         this_usbduxsub->ao_cmd_running = 0;
1489                         goto out;
1490                 }
1491                 s->async->inttrig = NULL;
1492         } else {
1493                 dev_err(&this_usbduxsub->interface->dev,
1494                         "comedi%d: ao_inttrig but acqu is already running.\n",
1495                         dev->minor);
1496         }
1497         ret = 1;
1498 out:
1499         up(&this_usbduxsub->sem);
1500         return ret;
1501 }
1502
1503 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1504                              struct comedi_subdevice *s,
1505                              struct comedi_cmd *cmd)
1506 {
1507         struct usbduxsub *this_usbduxsub = dev->private;
1508         int err = 0;
1509         unsigned int flags;
1510
1511         if (!this_usbduxsub)
1512                 return -EFAULT;
1513
1514         if (!(this_usbduxsub->probed))
1515                 return -ENODEV;
1516
1517         dev_dbg(&this_usbduxsub->interface->dev,
1518                 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1519
1520         /* Step 1 : check if triggers are trivially valid */
1521
1522         err |= cfc_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
1523
1524         if (0) {                /* (this_usbduxsub->high_speed) */
1525                 /*
1526                  * start immediately a new scan
1527                  * the sampling rate is set by the coversion rate
1528                  */
1529                 flags = TRIG_FOLLOW;
1530         } else {
1531                 /* start a new scan (output at once) with a timer */
1532                 flags = TRIG_TIMER;
1533         }
1534         err |= cfc_check_trigger_src(&cmd->scan_begin_src, flags);
1535
1536         err |= cfc_check_trigger_src(&cmd->convert_src, TRIG_NOW);
1537         err |= cfc_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
1538         err |= cfc_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
1539
1540         if (err)
1541                 return 1;
1542
1543         /* Step 2a : make sure trigger sources are unique */
1544
1545         err |= cfc_check_trigger_is_unique(cmd->start_src);
1546         err |= cfc_check_trigger_is_unique(cmd->stop_src);
1547
1548         /* Step 2b : and mutually compatible */
1549
1550         if (err)
1551                 return 2;
1552
1553         /* Step 3: check if arguments are trivially valid */
1554
1555         err |= cfc_check_trigger_arg_is(&cmd->start_arg, 0);
1556
1557         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
1558                 err |= cfc_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
1559
1560         if (cmd->scan_begin_src == TRIG_TIMER)
1561                 err |= cfc_check_trigger_arg_min(&cmd->scan_begin_arg,
1562                                                  1000000);
1563
1564         /* not used now, is for later use */
1565         if (cmd->convert_src == TRIG_TIMER)
1566                 err |= cfc_check_trigger_arg_min(&cmd->convert_arg, 125000);
1567
1568         err |= cfc_check_trigger_arg_is(&cmd->scan_end_arg, cmd->chanlist_len);
1569
1570         if (cmd->stop_src == TRIG_COUNT) {
1571                 /* any count is allowed */
1572         } else {
1573                 /* TRIG_NONE */
1574                 err |= cfc_check_trigger_arg_is(&cmd->stop_arg, 0);
1575         }
1576
1577         if (err)
1578                 return 3;
1579
1580         return 0;
1581 }
1582
1583 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1584 {
1585         struct comedi_cmd *cmd = &s->async->cmd;
1586         unsigned int chan, gain;
1587         int i, ret;
1588         struct usbduxsub *this_usbduxsub = dev->private;
1589
1590         if (!this_usbduxsub)
1591                 return -EFAULT;
1592
1593         down(&this_usbduxsub->sem);
1594         if (!(this_usbduxsub->probed)) {
1595                 up(&this_usbduxsub->sem);
1596                 return -ENODEV;
1597         }
1598         dev_dbg(&this_usbduxsub->interface->dev,
1599                 "comedi%d: %s\n", dev->minor, __func__);
1600
1601         /* set current channel of the running acquisition to zero */
1602         s->async->cur_chan = 0;
1603         for (i = 0; i < cmd->chanlist_len; ++i) {
1604                 chan = CR_CHAN(cmd->chanlist[i]);
1605                 gain = CR_RANGE(cmd->chanlist[i]);
1606                 if (i >= NUMOUTCHANNELS) {
1607                         dev_err(&this_usbduxsub->interface->dev,
1608                                 "comedi%d: %s: channel list too long\n",
1609                                 dev->minor, __func__);
1610                         break;
1611                 }
1612                 this_usbduxsub->dac_commands[i] = chan;
1613                 dev_dbg(&this_usbduxsub->interface->dev,
1614                         "comedi%d: dac command for ch %d is %x\n",
1615                         dev->minor, i, this_usbduxsub->dac_commands[i]);
1616         }
1617
1618         /* we count in steps of 1ms (125us) */
1619         /* 125us mode not used yet */
1620         if (0) {                /* (this_usbduxsub->high_speed) */
1621                 /* 125us */
1622                 /* timing of the conversion itself: every 125 us */
1623                 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1624         } else {
1625                 /* 1ms */
1626                 /* timing of the scan: we get all channels at once */
1627                 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1628                 dev_dbg(&this_usbduxsub->interface->dev,
1629                         "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1630                         "convert_src=%d, convert_arg=%d\n", dev->minor,
1631                         cmd->scan_begin_src, cmd->scan_begin_arg,
1632                         cmd->convert_src, cmd->convert_arg);
1633                 dev_dbg(&this_usbduxsub->interface->dev,
1634                         "comedi%d: ao_timer=%d (ms)\n",
1635                         dev->minor, this_usbduxsub->ao_timer);
1636                 if (this_usbduxsub->ao_timer < 1) {
1637                         dev_err(&this_usbduxsub->interface->dev,
1638                                 "comedi%d: usbdux: ao_timer=%d, "
1639                                 "scan_begin_arg=%d. "
1640                                 "Not properly tested by cmdtest?\n",
1641                                 dev->minor, this_usbduxsub->ao_timer,
1642                                 cmd->scan_begin_arg);
1643                         up(&this_usbduxsub->sem);
1644                         return -EINVAL;
1645                 }
1646         }
1647         this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1648
1649         if (cmd->stop_src == TRIG_COUNT) {
1650                 /* not continuous */
1651                 /* counter */
1652                 /* high speed also scans everything at once */
1653                 if (0) {        /* (this_usbduxsub->high_speed) */
1654                         this_usbduxsub->ao_sample_count =
1655                             (cmd->stop_arg) * (cmd->scan_end_arg);
1656                 } else {
1657                         /* there's no scan as the scan has been */
1658                         /* perf inside the FX2 */
1659                         /* data arrives as one packet */
1660                         this_usbduxsub->ao_sample_count = cmd->stop_arg;
1661                 }
1662                 this_usbduxsub->ao_continuous = 0;
1663         } else {
1664                 /* continuous acquisition */
1665                 this_usbduxsub->ao_continuous = 1;
1666                 this_usbduxsub->ao_sample_count = 0;
1667         }
1668
1669         if (cmd->start_src == TRIG_NOW) {
1670                 /* enable this acquisition operation */
1671                 this_usbduxsub->ao_cmd_running = 1;
1672                 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1673                 if (ret < 0) {
1674                         this_usbduxsub->ao_cmd_running = 0;
1675                         /* fixme: unlink here?? */
1676                         up(&this_usbduxsub->sem);
1677                         return ret;
1678                 }
1679                 s->async->inttrig = NULL;
1680         } else {
1681                 /* TRIG_INT */
1682                 /* submit the urbs later */
1683                 /* wait for an internal signal */
1684                 s->async->inttrig = usbdux_ao_inttrig;
1685         }
1686
1687         up(&this_usbduxsub->sem);
1688         return 0;
1689 }
1690
1691 static int usbdux_dio_insn_config(struct comedi_device *dev,
1692                                   struct comedi_subdevice *s,
1693                                   struct comedi_insn *insn, unsigned int *data)
1694 {
1695         int chan = CR_CHAN(insn->chanspec);
1696
1697         /* The input or output configuration of each digital line is
1698          * configured by a special insn_config instruction.  chanspec
1699          * contains the channel to be changed, and data[0] contains the
1700          * value COMEDI_INPUT or COMEDI_OUTPUT. */
1701
1702         switch (data[0]) {
1703         case INSN_CONFIG_DIO_OUTPUT:
1704                 s->io_bits |= 1 << chan;        /* 1 means Out */
1705                 break;
1706         case INSN_CONFIG_DIO_INPUT:
1707                 s->io_bits &= ~(1 << chan);
1708                 break;
1709         case INSN_CONFIG_DIO_QUERY:
1710                 data[1] =
1711                     (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1712                 break;
1713         default:
1714                 return -EINVAL;
1715                 break;
1716         }
1717         /* we don't tell the firmware here as it would take 8 frames */
1718         /* to submit the information. We do it in the insn_bits. */
1719         return insn->n;
1720 }
1721
1722 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1723                                 struct comedi_subdevice *s,
1724                                 struct comedi_insn *insn,
1725                                 unsigned int *data)
1726 {
1727
1728         struct usbduxsub *this_usbduxsub = dev->private;
1729         int err;
1730
1731         if (!this_usbduxsub)
1732                 return -EFAULT;
1733
1734         down(&this_usbduxsub->sem);
1735
1736         if (!(this_usbduxsub->probed)) {
1737                 up(&this_usbduxsub->sem);
1738                 return -ENODEV;
1739         }
1740
1741         /* The insn data is a mask in data[0] and the new data
1742          * in data[1], each channel cooresponding to a bit. */
1743         s->state &= ~data[0];
1744         s->state |= data[0] & data[1];
1745         /* The commands are 8 bits wide */
1746         this_usbduxsub->dux_commands[1] = (s->io_bits) & 0x000000FF;
1747         this_usbduxsub->dux_commands[4] = (s->state) & 0x000000FF;
1748         this_usbduxsub->dux_commands[2] = ((s->io_bits) & 0x0000FF00) >> 8;
1749         this_usbduxsub->dux_commands[5] = ((s->state) & 0x0000FF00) >> 8;
1750         this_usbduxsub->dux_commands[3] = ((s->io_bits) & 0x00FF0000) >> 16;
1751         this_usbduxsub->dux_commands[6] = ((s->state) & 0x00FF0000) >> 16;
1752
1753         /* This command also tells the firmware to return */
1754         /* the digital input lines */
1755         err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1756         if (err < 0) {
1757                 up(&this_usbduxsub->sem);
1758                 return err;
1759         }
1760         err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1761         if (err < 0) {
1762                 up(&this_usbduxsub->sem);
1763                 return err;
1764         }
1765
1766         data[1] = (((unsigned int)(this_usbduxsub->insnBuffer[1]))&0xff) |
1767                 ((((unsigned int)(this_usbduxsub->insnBuffer[2]))&0xff) << 8) |
1768                 ((((unsigned int)(this_usbduxsub->insnBuffer[3]))&0xff) << 16);
1769
1770         s->state = data[1];
1771
1772         up(&this_usbduxsub->sem);
1773         return insn->n;
1774 }
1775
1776 /***********************************/
1777 /* PWM */
1778
1779 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1780 {
1781         int err = 0;
1782
1783         if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1784                 if (usbduxsub_tmp->urbPwm)
1785                         usb_kill_urb(usbduxsub_tmp->urbPwm);
1786                 dev_dbg(&usbduxsub_tmp->interface->dev,
1787                         "comedi: unlinked PwmURB: res=%d\n", err);
1788         }
1789         return err;
1790 }
1791
1792 /* This cancels a running acquisition operation
1793  * in any context.
1794  */
1795 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1796 {
1797         int ret = 0;
1798
1799         if (!this_usbduxsub)
1800                 return -EFAULT;
1801
1802         dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1803         if (do_unlink)
1804                 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1805
1806         this_usbduxsub->pwm_cmd_running = 0;
1807
1808         return ret;
1809 }
1810
1811 /* force unlink - is called by comedi */
1812 static int usbdux_pwm_cancel(struct comedi_device *dev,
1813                              struct comedi_subdevice *s)
1814 {
1815         struct usbduxsub *this_usbduxsub = dev->private;
1816         int res = 0;
1817
1818         /* unlink only if it is really running */
1819         res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1820
1821         dev_dbg(&this_usbduxsub->interface->dev,
1822                 "comedi %d: sending pwm off command to the usb device.\n",
1823                 dev->minor);
1824         res = send_dux_commands(this_usbduxsub, SENDPWMOFF);
1825         if (res < 0)
1826                 return res;
1827
1828         return res;
1829 }
1830
1831 static void usbduxsub_pwm_irq(struct urb *urb)
1832 {
1833         int ret;
1834         struct usbduxsub *this_usbduxsub;
1835         struct comedi_device *this_comedidev;
1836         struct comedi_subdevice *s;
1837
1838         /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1839
1840         /* the context variable points to the subdevice */
1841         this_comedidev = urb->context;
1842         /* the private structure of the subdevice is struct usbduxsub */
1843         this_usbduxsub = this_comedidev->private;
1844
1845         s = &this_comedidev->subdevices[SUBDEV_DA];
1846
1847         switch (urb->status) {
1848         case 0:
1849                 /* success */
1850                 break;
1851
1852         case -ECONNRESET:
1853         case -ENOENT:
1854         case -ESHUTDOWN:
1855         case -ECONNABORTED:
1856                 /*
1857                  * after an unlink command, unplug, ... etc
1858                  * no unlink needed here. Already shutting down.
1859                  */
1860                 if (this_usbduxsub->pwm_cmd_running)
1861                         usbdux_pwm_stop(this_usbduxsub, 0);
1862
1863                 return;
1864
1865         default:
1866                 /* a real error */
1867                 if (this_usbduxsub->pwm_cmd_running) {
1868                         dev_err(&this_usbduxsub->interface->dev,
1869                                 "comedi_: Non-zero urb status received in "
1870                                 "pwm intr context: %d\n", urb->status);
1871                         usbdux_pwm_stop(this_usbduxsub, 0);
1872                 }
1873                 return;
1874         }
1875
1876         /* are we actually running? */
1877         if (!(this_usbduxsub->pwm_cmd_running))
1878                 return;
1879
1880         urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1881         urb->dev = this_usbduxsub->usbdev;
1882         urb->status = 0;
1883         if (this_usbduxsub->pwm_cmd_running) {
1884                 ret = usb_submit_urb(urb, GFP_ATOMIC);
1885                 if (ret < 0) {
1886                         dev_err(&this_usbduxsub->interface->dev,
1887                                 "comedi_: pwm urb resubm failed in int-cont. "
1888                                 "ret=%d", ret);
1889                         if (ret == EL2NSYNC)
1890                                 dev_err(&this_usbduxsub->interface->dev,
1891                                         "buggy USB host controller or bug in "
1892                                         "IRQ handling!\n");
1893
1894                         /* don't do an unlink here */
1895                         usbdux_pwm_stop(this_usbduxsub, 0);
1896                 }
1897         }
1898 }
1899
1900 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
1901 {
1902         int errFlag;
1903
1904         if (!usbduxsub)
1905                 return -EFAULT;
1906
1907         dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
1908
1909         /* in case of a resubmission after an unlink... */
1910         usb_fill_bulk_urb(usbduxsub->urbPwm,
1911                           usbduxsub->usbdev,
1912                           usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
1913                           usbduxsub->urbPwm->transfer_buffer,
1914                           usbduxsub->sizePwmBuf, usbduxsub_pwm_irq,
1915                           usbduxsub->comedidev);
1916
1917         errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
1918         if (errFlag) {
1919                 dev_err(&usbduxsub->interface->dev,
1920                         "comedi_: usbduxsigma: pwm: usb_submit_urb error %d\n",
1921                         errFlag);
1922                 return errFlag;
1923         }
1924         return 0;
1925 }
1926
1927 static int usbdux_pwm_period(struct comedi_device *dev,
1928                              struct comedi_subdevice *s, unsigned int period)
1929 {
1930         struct usbduxsub *this_usbduxsub = dev->private;
1931         int fx2delay = 255;
1932
1933         if (period < MIN_PWM_PERIOD) {
1934                 dev_err(&this_usbduxsub->interface->dev,
1935                         "comedi%d: illegal period setting for pwm.\n",
1936                         dev->minor);
1937                 return -EAGAIN;
1938         } else {
1939                 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
1940                 if (fx2delay > 255) {
1941                         dev_err(&this_usbduxsub->interface->dev,
1942                                 "comedi%d: period %d for pwm is too low.\n",
1943                                 dev->minor, period);
1944                         return -EAGAIN;
1945                 }
1946         }
1947         this_usbduxsub->pwmDelay = fx2delay;
1948         this_usbduxsub->pwmPeriod = period;
1949         dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
1950                 __func__, period, fx2delay);
1951         return 0;
1952 }
1953
1954 /* is called from insn so there's no need to do all the sanity checks */
1955 static int usbdux_pwm_start(struct comedi_device *dev,
1956                             struct comedi_subdevice *s)
1957 {
1958         int ret, i;
1959         struct usbduxsub *this_usbduxsub = dev->private;
1960
1961         dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
1962                 dev->minor, __func__);
1963
1964         if (this_usbduxsub->pwm_cmd_running) {
1965                 /* already running */
1966                 return 0;
1967         }
1968
1969         this_usbduxsub->dux_commands[1] = ((uint8_t) this_usbduxsub->pwmDelay);
1970         ret = send_dux_commands(this_usbduxsub, SENDPWMON);
1971         if (ret < 0)
1972                 return ret;
1973
1974         /* initialise the buffer */
1975         for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
1976                 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
1977
1978         this_usbduxsub->pwm_cmd_running = 1;
1979         ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
1980         if (ret < 0) {
1981                 this_usbduxsub->pwm_cmd_running = 0;
1982                 return ret;
1983         }
1984         return 0;
1985 }
1986
1987 /* generates the bit pattern for PWM with the optional sign bit */
1988 static int usbdux_pwm_pattern(struct comedi_device *dev,
1989                               struct comedi_subdevice *s, int channel,
1990                               unsigned int value, unsigned int sign)
1991 {
1992         struct usbduxsub *this_usbduxsub = dev->private;
1993         int i, szbuf;
1994         char *pBuf;
1995         char pwm_mask;
1996         char sgn_mask;
1997         char c;
1998
1999         if (!this_usbduxsub)
2000                 return -EFAULT;
2001
2002         /* this is the DIO bit which carries the PWM data */
2003         pwm_mask = (1 << channel);
2004         /* this is the DIO bit which carries the optional direction bit */
2005         sgn_mask = (16 << channel);
2006         /* this is the buffer which will be filled with the with bit */
2007         /* pattern for one period */
2008         szbuf = this_usbduxsub->sizePwmBuf;
2009         pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2010         for (i = 0; i < szbuf; i++) {
2011                 c = *pBuf;
2012                 /* reset bits */
2013                 c = c & (~pwm_mask);
2014                 /* set the bit as long as the index is lower than the value */
2015                 if (i < value)
2016                         c = c | pwm_mask;
2017                 /* set the optional sign bit for a relay */
2018                 if (!sign) {
2019                         /* positive value */
2020                         c = c & (~sgn_mask);
2021                 } else {
2022                         /* negative value */
2023                         c = c | sgn_mask;
2024                 }
2025                 *(pBuf++) = c;
2026         }
2027         return 1;
2028 }
2029
2030 static int usbdux_pwm_write(struct comedi_device *dev,
2031                             struct comedi_subdevice *s,
2032                             struct comedi_insn *insn, unsigned int *data)
2033 {
2034         struct usbduxsub *this_usbduxsub = dev->private;
2035
2036         if (!this_usbduxsub)
2037                 return -EFAULT;
2038
2039         if ((insn->n) != 1) {
2040                 /*
2041                  * doesn't make sense to have more than one value here because
2042                  * it would just overwrite the PWM buffer a couple of times
2043                  */
2044                 return -EINVAL;
2045         }
2046
2047         /*
2048          * the sign is set via a special INSN only, this gives us 8 bits for
2049          * normal operation
2050          * relay sign 0 by default
2051          */
2052         return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
2053 }
2054
2055 static int usbdux_pwm_read(struct comedi_device *x1,
2056                            struct comedi_subdevice *x2, struct comedi_insn *x3,
2057                            unsigned int *x4)
2058 {
2059         /* not needed */
2060         return -EINVAL;
2061 };
2062
2063 /* switches on/off PWM */
2064 static int usbdux_pwm_config(struct comedi_device *dev,
2065                              struct comedi_subdevice *s,
2066                              struct comedi_insn *insn, unsigned int *data)
2067 {
2068         struct usbduxsub *this_usbduxsub = dev->private;
2069         switch (data[0]) {
2070         case INSN_CONFIG_ARM:
2071                 /* switch it on */
2072                 dev_dbg(&this_usbduxsub->interface->dev,
2073                         "comedi%d: %s: pwm on\n", dev->minor, __func__);
2074                 /*
2075                  * if not zero the PWM is limited to a certain time which is
2076                  * not supported here
2077                  */
2078                 if (data[1] != 0)
2079                         return -EINVAL;
2080                 return usbdux_pwm_start(dev, s);
2081         case INSN_CONFIG_DISARM:
2082                 dev_dbg(&this_usbduxsub->interface->dev,
2083                         "comedi%d: %s: pwm off\n", dev->minor, __func__);
2084                 return usbdux_pwm_cancel(dev, s);
2085         case INSN_CONFIG_GET_PWM_STATUS:
2086                 /*
2087                  * to check if the USB transmission has failed or in case PWM
2088                  * was limited to n cycles to check if it has terminated
2089                  */
2090                 data[1] = this_usbduxsub->pwm_cmd_running;
2091                 return 0;
2092         case INSN_CONFIG_PWM_SET_PERIOD:
2093                 dev_dbg(&this_usbduxsub->interface->dev,
2094                         "comedi%d: %s: setting period\n", dev->minor,
2095                         __func__);
2096                 return usbdux_pwm_period(dev, s, data[1]);
2097         case INSN_CONFIG_PWM_GET_PERIOD:
2098                 data[1] = this_usbduxsub->pwmPeriod;
2099                 return 0;
2100         case INSN_CONFIG_PWM_SET_H_BRIDGE:
2101                 /* value in the first byte and the sign in the second for a
2102                    relay */
2103                 return usbdux_pwm_pattern(dev, s,
2104                                           /* the channel number */
2105                                           CR_CHAN(insn->chanspec),
2106                                           /* actual PWM data */
2107                                           data[1],
2108                                           /* just a sign */
2109                                           (data[2] != 0));
2110         case INSN_CONFIG_PWM_GET_H_BRIDGE:
2111                 /* values are not kept in this driver, nothing to return */
2112                 return -EINVAL;
2113         }
2114         return -EINVAL;
2115 }
2116
2117 /* end of PWM */
2118 /*****************************************************************/
2119
2120 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2121 {
2122         int i;
2123
2124         if (!usbduxsub_tmp)
2125                 return;
2126         dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2127
2128         /* shows the usb subsystem that the driver is down */
2129         if (usbduxsub_tmp->interface)
2130                 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2131
2132         usbduxsub_tmp->probed = 0;
2133
2134         if (usbduxsub_tmp->urbIn) {
2135                 if (usbduxsub_tmp->ai_cmd_running) {
2136                         usbduxsub_tmp->ai_cmd_running = 0;
2137                         usbduxsub_unlink_InURBs(usbduxsub_tmp);
2138                 }
2139                 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2140                         kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2141                         usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2142                         usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2143                         usb_free_urb(usbduxsub_tmp->urbIn[i]);
2144                         usbduxsub_tmp->urbIn[i] = NULL;
2145                 }
2146                 kfree(usbduxsub_tmp->urbIn);
2147                 usbduxsub_tmp->urbIn = NULL;
2148         }
2149         if (usbduxsub_tmp->urbOut) {
2150                 if (usbduxsub_tmp->ao_cmd_running) {
2151                         usbduxsub_tmp->ao_cmd_running = 0;
2152                         usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2153                 }
2154                 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2155                         if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2156                                 kfree(usbduxsub_tmp->
2157                                       urbOut[i]->transfer_buffer);
2158                                 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2159                                     NULL;
2160                         }
2161                         if (usbduxsub_tmp->urbOut[i]) {
2162                                 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2163                                 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2164                                 usbduxsub_tmp->urbOut[i] = NULL;
2165                         }
2166                 }
2167                 kfree(usbduxsub_tmp->urbOut);
2168                 usbduxsub_tmp->urbOut = NULL;
2169         }
2170         if (usbduxsub_tmp->urbPwm) {
2171                 if (usbduxsub_tmp->pwm_cmd_running) {
2172                         usbduxsub_tmp->pwm_cmd_running = 0;
2173                         usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2174                 }
2175                 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2176                 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2177                 usb_kill_urb(usbduxsub_tmp->urbPwm);
2178                 usb_free_urb(usbduxsub_tmp->urbPwm);
2179                 usbduxsub_tmp->urbPwm = NULL;
2180         }
2181         kfree(usbduxsub_tmp->inBuffer);
2182         usbduxsub_tmp->inBuffer = NULL;
2183         kfree(usbduxsub_tmp->insnBuffer);
2184         usbduxsub_tmp->insnBuffer = NULL;
2185         kfree(usbduxsub_tmp->outBuffer);
2186         usbduxsub_tmp->outBuffer = NULL;
2187         kfree(usbduxsub_tmp->dac_commands);
2188         usbduxsub_tmp->dac_commands = NULL;
2189         kfree(usbduxsub_tmp->dux_commands);
2190         usbduxsub_tmp->dux_commands = NULL;
2191         usbduxsub_tmp->ai_cmd_running = 0;
2192         usbduxsub_tmp->ao_cmd_running = 0;
2193         usbduxsub_tmp->pwm_cmd_running = 0;
2194 }
2195
2196 static int usbduxsigma_attach_common(struct comedi_device *dev,
2197                                      struct usbduxsub *uds)
2198 {
2199         int ret;
2200         struct comedi_subdevice *s;
2201         int n_subdevs;
2202         int offset;
2203
2204         down(&uds->sem);
2205         /* pointer back to the corresponding comedi device */
2206         uds->comedidev = dev;
2207         dev->board_name = "usbduxsigma";
2208         /* set number of subdevices */
2209         if (uds->high_speed)
2210                 n_subdevs = 4;  /* with pwm */
2211         else
2212                 n_subdevs = 3;  /* without pwm */
2213         ret = comedi_alloc_subdevices(dev, n_subdevs);
2214         if (ret) {
2215                 up(&uds->sem);
2216                 return ret;
2217         }
2218         /* private structure is also simply the usb-structure */
2219         dev->private = uds;
2220         /* the first subdevice is the A/D converter */
2221         s = &dev->subdevices[SUBDEV_AD];
2222         /* the URBs get the comedi subdevice */
2223         /* which is responsible for reading */
2224         /* this is the subdevice which reads data */
2225         dev->read_subdev = s;
2226         /* the subdevice receives as private structure the */
2227         /* usb-structure */
2228         s->private = NULL;
2229         /* analog input */
2230         s->type = COMEDI_SUBD_AI;
2231         /* readable and ref is to ground, 32 bit wide data! */
2232         s->subdev_flags = SDF_READABLE | SDF_GROUND |
2233                 SDF_CMD_READ | SDF_LSAMPL;
2234         /* 16 A/D channels */
2235         s->n_chan = NUMCHANNELS;
2236         /* length of the channellist */
2237         s->len_chanlist = NUMCHANNELS;
2238         /* callback functions */
2239         s->insn_read = usbdux_ai_insn_read;
2240         s->do_cmdtest = usbdux_ai_cmdtest;
2241         s->do_cmd = usbdux_ai_cmd;
2242         s->cancel = usbdux_ai_cancel;
2243         /* max value from the A/D converter (24bit) */
2244         s->maxdata = 0x00FFFFFF;
2245         /* range table to convert to physical units */
2246         s->range_table = (&range_usbdux_ai_range);
2247         /* analog output subdevice */
2248         s = &dev->subdevices[SUBDEV_DA];
2249         /* analog out */
2250         s->type = COMEDI_SUBD_AO;
2251         /* backward pointer */
2252         dev->write_subdev = s;
2253         /* the subdevice receives as private structure the */
2254         /* usb-structure */
2255         s->private = NULL;
2256         /* are writable */
2257         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2258         /* 4 channels */
2259         s->n_chan = 4;
2260         /* length of the channellist */
2261         s->len_chanlist = 4;
2262         /* 8 bit resolution */
2263         s->maxdata = 0x00ff;
2264         /* unipolar range */
2265         s->range_table = (&range_usbdux_ao_range);
2266         /* callback */
2267         s->do_cmdtest = usbdux_ao_cmdtest;
2268         s->do_cmd = usbdux_ao_cmd;
2269         s->cancel = usbdux_ao_cancel;
2270         s->insn_read = usbdux_ao_insn_read;
2271         s->insn_write = usbdux_ao_insn_write;
2272         /* digital I/O subdevice */
2273         s = &dev->subdevices[SUBDEV_DIO];
2274         s->type = COMEDI_SUBD_DIO;
2275         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2276         /* 8 external and 16 internal channels */
2277         s->n_chan = 24;
2278         s->maxdata = 1;
2279         s->range_table = (&range_digital);
2280         s->insn_bits = usbdux_dio_insn_bits;
2281         s->insn_config = usbdux_dio_insn_config;
2282         /* we don't use it */
2283         s->private = NULL;
2284         if (uds->high_speed) {
2285                 /* timer / pwm subdevice */
2286                 s = &dev->subdevices[SUBDEV_PWM];
2287                 s->type = COMEDI_SUBD_PWM;
2288                 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2289                 s->n_chan = 8;
2290                 /* this defines the max duty cycle resolution */
2291                 s->maxdata = uds->sizePwmBuf;
2292                 s->insn_write = usbdux_pwm_write;
2293                 s->insn_read = usbdux_pwm_read;
2294                 s->insn_config = usbdux_pwm_config;
2295                 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2296         }
2297         /* finally decide that it's attached */
2298         uds->attached = 1;
2299         up(&uds->sem);
2300         offset = usbdux_getstatusinfo(dev, 0);
2301         if (offset < 0)
2302                 dev_err(&uds->interface->dev,
2303                         "Communication to USBDUXSIGMA failed! Check firmware and cabling.");
2304         dev_info(&uds->interface->dev,
2305                  "comedi%d: attached, ADC_zero = %x\n", dev->minor, offset);
2306         return 0;
2307 }
2308
2309 static int usbduxsigma_auto_attach(struct comedi_device *dev,
2310                                    unsigned long context_unused)
2311 {
2312         struct usb_interface *uinterf = comedi_to_usb_interface(dev);
2313         int ret;
2314         struct usbduxsub *uds;
2315
2316         dev->private = NULL;
2317         down(&start_stop_sem);
2318         uds = usb_get_intfdata(uinterf);
2319         if (!uds || !uds->probed) {
2320                 dev_err(dev->class_dev,
2321                         "usbduxsigma: error: auto_attach failed, not connected\n");
2322                 ret = -ENODEV;
2323         } else if (uds->attached) {
2324                 dev_err(dev->class_dev,
2325                        "usbduxsigma: error: auto_attach failed, already attached\n");
2326                 ret = -ENODEV;
2327         } else
2328                 ret = usbduxsigma_attach_common(dev, uds);
2329         up(&start_stop_sem);
2330         return ret;
2331 }
2332
2333 static void usbduxsigma_detach(struct comedi_device *dev)
2334 {
2335         struct usbduxsub *usb = dev->private;
2336
2337         if (usb) {
2338                 down(&usb->sem);
2339                 dev->private = NULL;
2340                 usb->attached = 0;
2341                 usb->comedidev = NULL;
2342                 up(&usb->sem);
2343         }
2344 }
2345
2346 static struct comedi_driver usbduxsigma_driver = {
2347         .driver_name    = "usbduxsigma",
2348         .module         = THIS_MODULE,
2349         .auto_attach    = usbduxsigma_auto_attach,
2350         .detach         = usbduxsigma_detach,
2351 };
2352
2353 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2354                                                      void *context)
2355 {
2356         struct usbduxsub *usbduxsub_tmp = context;
2357         struct usb_interface *uinterf = usbduxsub_tmp->interface;
2358         int ret;
2359
2360         if (fw == NULL) {
2361                 dev_err(&uinterf->dev,
2362                         "Firmware complete handler without firmware!\n");
2363                 return;
2364         }
2365
2366         /*
2367          * we need to upload the firmware here because fw will be
2368          * freed once we've left this function
2369          */
2370         ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2371
2372         if (ret) {
2373                 dev_err(&uinterf->dev,
2374                         "Could not upload firmware (err=%d)\n", ret);
2375                 goto out;
2376         }
2377         comedi_usb_auto_config(uinterf, &usbduxsigma_driver, 0);
2378 out:
2379         release_firmware(fw);
2380 }
2381
2382 static int usbduxsigma_usb_probe(struct usb_interface *uinterf,
2383                                  const struct usb_device_id *id)
2384 {
2385         struct usb_device *udev = interface_to_usbdev(uinterf);
2386         struct device *dev = &uinterf->dev;
2387         int i;
2388         int index;
2389         int ret;
2390
2391         dev_dbg(dev, "comedi_: usbdux_: "
2392                 "finding a free structure for the usb-device\n");
2393
2394         down(&start_stop_sem);
2395         /* look for a free place in the usbdux array */
2396         index = -1;
2397         for (i = 0; i < NUMUSBDUX; i++) {
2398                 if (!(usbduxsub[i].probed)) {
2399                         index = i;
2400                         break;
2401                 }
2402         }
2403
2404         /* no more space */
2405         if (index == -1) {
2406                 dev_err(dev, "Too many usbduxsigma-devices connected.\n");
2407                 up(&start_stop_sem);
2408                 return -EMFILE;
2409         }
2410         dev_dbg(dev, "comedi_: usbdux: "
2411                 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2412
2413         sema_init(&(usbduxsub[index].sem), 1);
2414         /* save a pointer to the usb device */
2415         usbduxsub[index].usbdev = udev;
2416
2417         /* save the interface itself */
2418         usbduxsub[index].interface = uinterf;
2419         /* get the interface number from the interface */
2420         usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2421         /* hand the private data over to the usb subsystem */
2422         /* will be needed for disconnect */
2423         usb_set_intfdata(uinterf, &(usbduxsub[index]));
2424
2425         dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2426
2427         /* test if it is high speed (USB 2.0) */
2428         usbduxsub[index].high_speed =
2429             (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2430
2431         /* create space for the commands of the DA converter */
2432         usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2433         if (!usbduxsub[index].dac_commands) {
2434                 tidy_up(&(usbduxsub[index]));
2435                 up(&start_stop_sem);
2436                 return -ENOMEM;
2437         }
2438         /* create space for the commands going to the usb device */
2439         usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2440         if (!usbduxsub[index].dux_commands) {
2441                 tidy_up(&(usbduxsub[index]));
2442                 up(&start_stop_sem);
2443                 return -ENOMEM;
2444         }
2445         /* create space for the in buffer and set it to zero */
2446         usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2447         if (!(usbduxsub[index].inBuffer)) {
2448                 tidy_up(&(usbduxsub[index]));
2449                 up(&start_stop_sem);
2450                 return -ENOMEM;
2451         }
2452         /* create space of the instruction buffer */
2453         usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2454         if (!(usbduxsub[index].insnBuffer)) {
2455                 tidy_up(&(usbduxsub[index]));
2456                 up(&start_stop_sem);
2457                 return -ENOMEM;
2458         }
2459         /* create space for the outbuffer */
2460         usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2461         if (!(usbduxsub[index].outBuffer)) {
2462                 tidy_up(&(usbduxsub[index]));
2463                 up(&start_stop_sem);
2464                 return -ENOMEM;
2465         }
2466         /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2467         i = usb_set_interface(usbduxsub[index].usbdev,
2468                               usbduxsub[index].ifnum, 3);
2469         if (i < 0) {
2470                 dev_err(dev, "comedi_: usbduxsigma%d: "
2471                         "could not set alternate setting 3 in high speed.\n",
2472                         index);
2473                 tidy_up(&(usbduxsub[index]));
2474                 up(&start_stop_sem);
2475                 return -ENODEV;
2476         }
2477         if (usbduxsub[index].high_speed)
2478                 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2479         else
2480                 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2481
2482         usbduxsub[index].urbIn = kcalloc(usbduxsub[index].numOfInBuffers,
2483                                          sizeof(struct urb *),
2484                                          GFP_KERNEL);
2485         if (!(usbduxsub[index].urbIn)) {
2486                 tidy_up(&(usbduxsub[index]));
2487                 up(&start_stop_sem);
2488                 return -ENOMEM;
2489         }
2490         for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2491                 /* one frame: 1ms */
2492                 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2493                 if (usbduxsub[index].urbIn[i] == NULL) {
2494                         dev_err(dev, "comedi_: usbduxsigma%d: "
2495                                 "Could not alloc. urb(%d)\n", index, i);
2496                         tidy_up(&(usbduxsub[index]));
2497                         up(&start_stop_sem);
2498                         return -ENOMEM;
2499                 }
2500                 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2501                 /* will be filled later with a pointer to the comedi-device */
2502                 /* and ONLY then the urb should be submitted */
2503                 usbduxsub[index].urbIn[i]->context = NULL;
2504                 usbduxsub[index].urbIn[i]->pipe =
2505                     usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2506                 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2507                 usbduxsub[index].urbIn[i]->transfer_buffer =
2508                     kzalloc(SIZEINBUF, GFP_KERNEL);
2509                 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2510                         tidy_up(&(usbduxsub[index]));
2511                         up(&start_stop_sem);
2512                         return -ENOMEM;
2513                 }
2514                 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2515                 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2516                 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2517                 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2518                 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length =
2519                         SIZEINBUF;
2520         }
2521
2522         /* out */
2523         if (usbduxsub[index].high_speed)
2524                 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2525         else
2526                 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2527
2528         usbduxsub[index].urbOut = kcalloc(usbduxsub[index].numOfOutBuffers,
2529                                           sizeof(struct urb *), GFP_KERNEL);
2530         if (!(usbduxsub[index].urbOut)) {
2531                 tidy_up(&(usbduxsub[index]));
2532                 up(&start_stop_sem);
2533                 return -ENOMEM;
2534         }
2535         for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2536                 /* one frame: 1ms */
2537                 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2538                 if (usbduxsub[index].urbOut[i] == NULL) {
2539                         dev_err(dev, "comedi_: usbduxsigma%d: "
2540                                 "Could not alloc. urb(%d)\n", index, i);
2541                         tidy_up(&(usbduxsub[index]));
2542                         up(&start_stop_sem);
2543                         return -ENOMEM;
2544                 }
2545                 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2546                 /* will be filled later with a pointer to the comedi-device */
2547                 /* and ONLY then the urb should be submitted */
2548                 usbduxsub[index].urbOut[i]->context = NULL;
2549                 usbduxsub[index].urbOut[i]->pipe =
2550                     usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2551                 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2552                 usbduxsub[index].urbOut[i]->transfer_buffer =
2553                     kzalloc(SIZEOUTBUF, GFP_KERNEL);
2554                 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2555                         tidy_up(&(usbduxsub[index]));
2556                         up(&start_stop_sem);
2557                         return -ENOMEM;
2558                 }
2559                 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2560                 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2561                 usbduxsub[index].urbOut[i]->transfer_buffer_length =
2562                         SIZEOUTBUF;
2563                 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2564                 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2565                     SIZEOUTBUF;
2566                 if (usbduxsub[index].high_speed) {
2567                         /* uframes */
2568                         usbduxsub[index].urbOut[i]->interval = 8;
2569                 } else {
2570                         /* frames */
2571                         usbduxsub[index].urbOut[i]->interval = 1;
2572                 }
2573         }
2574
2575         /* pwm */
2576         if (usbduxsub[index].high_speed) {
2577                 /* max bulk ep size in high speed */
2578                 usbduxsub[index].sizePwmBuf = 512;
2579                 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2580                 if (usbduxsub[index].urbPwm == NULL) {
2581                         dev_err(dev, "comedi_: usbduxsigma%d: "
2582                                 "Could not alloc. pwm urb\n", index);
2583                         tidy_up(&(usbduxsub[index]));
2584                         up(&start_stop_sem);
2585                         return -ENOMEM;
2586                 }
2587                 usbduxsub[index].urbPwm->transfer_buffer =
2588                     kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2589                 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2590                         tidy_up(&(usbduxsub[index]));
2591                         up(&start_stop_sem);
2592                         return -ENOMEM;
2593                 }
2594         } else {
2595                 usbduxsub[index].urbPwm = NULL;
2596                 usbduxsub[index].sizePwmBuf = 0;
2597         }
2598
2599         usbduxsub[index].ai_cmd_running = 0;
2600         usbduxsub[index].ao_cmd_running = 0;
2601         usbduxsub[index].pwm_cmd_running = 0;
2602
2603         /* we've reached the bottom of the function */
2604         usbduxsub[index].probed = 1;
2605         up(&start_stop_sem);
2606
2607         ret = request_firmware_nowait(THIS_MODULE,
2608                                       FW_ACTION_HOTPLUG,
2609                                       FIRMWARE,
2610                                       &udev->dev,
2611                                       GFP_KERNEL,
2612                                       usbduxsub + index,
2613                                       usbdux_firmware_request_complete_handler
2614                                       );
2615
2616         if (ret) {
2617                 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2618                 return ret;
2619         }
2620
2621         dev_info(dev, "comedi_: successfully initialised.\n");
2622         /* success */
2623         return 0;
2624 }
2625
2626 static void usbduxsigma_usb_disconnect(struct usb_interface *intf)
2627 {
2628         struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2629         struct usb_device *udev = interface_to_usbdev(intf);
2630
2631         if (!usbduxsub_tmp) {
2632                 dev_err(&intf->dev,
2633                         "comedi_: disconnect called with null pointer.\n");
2634                 return;
2635         }
2636         if (usbduxsub_tmp->usbdev != udev) {
2637                 dev_err(&intf->dev, "comedi_: BUG! wrong ptr!\n");
2638                 return;
2639         }
2640         if (usbduxsub_tmp->ai_cmd_running)
2641                 /* we are still running a command */
2642                 usbdux_ai_stop(usbduxsub_tmp, 1);
2643         if (usbduxsub_tmp->ao_cmd_running)
2644                 /* we are still running a command */
2645                 usbdux_ao_stop(usbduxsub_tmp, 1);
2646         comedi_usb_auto_unconfig(intf);
2647         down(&start_stop_sem);
2648         down(&usbduxsub_tmp->sem);
2649         tidy_up(usbduxsub_tmp);
2650         up(&usbduxsub_tmp->sem);
2651         up(&start_stop_sem);
2652         dev_info(&intf->dev, "comedi_: disconnected from the usb\n");
2653 }
2654
2655 static const struct usb_device_id usbduxsigma_usb_table[] = {
2656         { USB_DEVICE(0x13d8, 0x0020) },
2657         { USB_DEVICE(0x13d8, 0x0021) },
2658         { USB_DEVICE(0x13d8, 0x0022) },
2659         { }
2660 };
2661 MODULE_DEVICE_TABLE(usb, usbduxsigma_usb_table);
2662
2663 static struct usb_driver usbduxsigma_usb_driver = {
2664         .name           = "usbduxsigma",
2665         .probe          = usbduxsigma_usb_probe,
2666         .disconnect     = usbduxsigma_usb_disconnect,
2667         .id_table       = usbduxsigma_usb_table,
2668 };
2669 module_comedi_usb_driver(usbduxsigma_driver, usbduxsigma_usb_driver);
2670
2671 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
2672 MODULE_DESCRIPTION("Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com");
2673 MODULE_LICENSE("GPL");
2674 MODULE_FIRMWARE(FIRMWARE);