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