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