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