init from v2.6.32.60
[mach-moxart.git] / drivers / staging / comedi / drivers / usbdux.c
blob5c9c1bc3eb61260be5a24ea969be3a041de802fc
1 #define DRIVER_VERSION "v2.4"
2 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
3 #define DRIVER_DESC "Stirling/ITL USB-DUX -- Bernd.Porr@f2s.com"
4 /*
5 comedi/drivers/usbdux.c
6 Copyright (C) 2003-2007 Bernd Porr, Bernd.Porr@f2s.com
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.
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.
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.
24 Driver: usbdux
25 Description: University of Stirling USB DAQ & INCITE Technology Limited
26 Devices: [ITL] USB-DUX (usbdux.o)
27 Author: Bernd Porr <BerndPorr@f2s.com>
28 Updated: 8 Dec 2008
29 Status: Stable
30 Configuration options:
31 You have to upload firmware with the -i option. The
32 firmware is usually installed under /usr/share/usb or
33 /usr/local/share/usb or /lib/firmware.
35 Connection scheme for the counter at the digital port:
36 0=/CLK0, 1=UP/DOWN0, 2=RESET0, 4=/CLK1, 5=UP/DOWN1, 6=RESET1.
37 The sampling rate of the counter is approximately 500Hz.
39 Please note that under USB2.0 the length of the channel list determines
40 the max sampling rate. If you sample only one channel you get 8kHz
41 sampling rate. If you sample two channels you get 4kHz and so on.
44 * I must give credit here to Chris Baugher who
45 * wrote the driver for AT-MIO-16d. I used some parts of this
46 * driver. I also must give credits to David Brownell
47 * who supported me with the USB development.
49 * Bernd Porr
52 * Revision history:
53 * 0.94: D/A output should work now with any channel list combinations
54 * 0.95: .owner commented out for kernel vers below 2.4.19
55 * sanity checks in ai/ao_cmd
56 * 0.96: trying to get it working with 2.6, moved all memory alloc to comedi's
57 * attach final USB IDs
58 * moved memory allocation completely to the corresponding comedi
59 * functions firmware upload is by fxload and no longer by comedi (due to
60 * enumeration)
61 * 0.97: USB IDs received, adjusted table
62 * 0.98: SMP, locking, memroy alloc: moved all usb memory alloc
63 * to the usb subsystem and moved all comedi related memory
64 * alloc to comedi.
65 * | kernel | registration | usbdux-usb | usbdux-comedi | comedi |
66 * 0.99: USB 2.0: changed protocol to isochronous transfer
67 * IRQ transfer is too buggy and too risky in 2.0
68 * for the high speed ISO transfer is now a working version
69 * available
70 * 0.99b: Increased the iso transfer buffer for high sp.to 10 buffers. Some VIA
71 * chipsets miss out IRQs. Deeper buffering is needed.
72 * 1.00: full USB 2.0 support for the A/D converter. Now: max 8kHz sampling
73 * rate.
74 * Firmware vers 1.00 is needed for this.
75 * Two 16 bit up/down/reset counter with a sampling rate of 1kHz
76 * And loads of cleaning up, in particular streamlining the
77 * bulk transfers.
78 * 1.1: moved EP4 transfers to EP1 to make space for a PWM output on EP4
79 * 1.2: added PWM suport via EP4
80 * 2.0: PWM seems to be stable and is not interfering with the other functions
81 * 2.1: changed PWM API
82 * 2.2: added firmware kernel request to fix an udev problem
83 * 2.3: corrected a bug in bulk timeouts which were far too short
84 * 2.4: fixed a bug which causes the driver to hang when it ran out of data.
85 * Thanks to Jan-Matthias Braun and Ian to spot the bug and fix it.
89 /* generates loads of debug info */
90 /* #define NOISY_DUX_DEBUGBUG */
92 #include <linux/kernel.h>
93 #include <linux/module.h>
94 #include <linux/init.h>
95 #include <linux/slab.h>
96 #include <linux/input.h>
97 #include <linux/usb.h>
98 #include <linux/smp_lock.h>
99 #include <linux/fcntl.h>
100 #include <linux/compiler.h>
101 #include <linux/firmware.h>
103 #include "../comedidev.h"
105 #define BOARDNAME "usbdux"
107 /* timeout for the USB-transfer in ms*/
108 #define BULK_TIMEOUT 1000
110 /* constants for "firmware" upload and download */
111 #define USBDUXSUB_FIRMWARE 0xA0
112 #define VENDOR_DIR_IN 0xC0
113 #define VENDOR_DIR_OUT 0x40
115 /* internal adresses of the 8051 processor */
116 #define USBDUXSUB_CPUCS 0xE600
119 * the minor device number, major is 180 only for debugging purposes and to
120 * upload special firmware (programming the eeprom etc) which is not compatible
121 * with the comedi framwork
123 #define USBDUXSUB_MINOR 32
125 /* max lenghth of the transfer-buffer for software upload */
126 #define TB_LEN 0x2000
128 /* Input endpoint number: ISO/IRQ */
129 #define ISOINEP 6
131 /* Output endpoint number: ISO/IRQ */
132 #define ISOOUTEP 2
134 /* This EP sends DUX commands to USBDUX */
135 #define COMMAND_OUT_EP 1
137 /* This EP receives the DUX commands from USBDUX */
138 #define COMMAND_IN_EP 8
140 /* Output endpoint for PWM */
141 #define PWM_EP 4
143 /* 300Hz max frequ under PWM */
144 #define MIN_PWM_PERIOD ((long)(1E9/300))
146 /* Default PWM frequency */
147 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
149 /* Number of channels */
150 #define NUMCHANNELS 8
152 /* Size of one A/D value */
153 #define SIZEADIN ((sizeof(int16_t)))
156 * Size of the input-buffer IN BYTES
157 * Always multiple of 8 for 8 microframes which is needed in the highspeed mode
159 #define SIZEINBUF ((8*SIZEADIN))
161 /* 16 bytes. */
162 #define SIZEINSNBUF 16
164 /* Number of DA channels */
165 #define NUMOUTCHANNELS 8
167 /* size of one value for the D/A converter: channel and value */
168 #define SIZEDAOUT ((sizeof(int8_t)+sizeof(int16_t)))
171 * Size of the output-buffer in bytes
172 * Actually only the first 4 triplets are used but for the
173 * high speed mode we need to pad it to 8 (microframes).
175 #define SIZEOUTBUF ((8*SIZEDAOUT))
178 * Size of the buffer for the dux commands: just now max size is determined
179 * by the analogue out + command byte + panic bytes...
181 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
183 /* Number of in-URBs which receive the data: min=2 */
184 #define NUMOFINBUFFERSFULL 5
186 /* Number of out-URBs which send the data: min=2 */
187 #define NUMOFOUTBUFFERSFULL 5
189 /* Number of in-URBs which receive the data: min=5 */
190 /* must have more buffers due to buggy USB ctr */
191 #define NUMOFINBUFFERSHIGH 10
193 /* Number of out-URBs which send the data: min=5 */
194 /* must have more buffers due to buggy USB ctr */
195 #define NUMOFOUTBUFFERSHIGH 10
197 /* Total number of usbdux devices */
198 #define NUMUSBDUX 16
200 /* Analogue in subdevice */
201 #define SUBDEV_AD 0
203 /* Analogue out subdevice */
204 #define SUBDEV_DA 1
206 /* Digital I/O */
207 #define SUBDEV_DIO 2
209 /* counter */
210 #define SUBDEV_COUNTER 3
212 /* timer aka pwm output */
213 #define SUBDEV_PWM 4
215 /* number of retries to get the right dux command */
216 #define RETRIES 10
218 /**************************************************/
219 /* comedi constants */
220 static const struct comedi_lrange range_usbdux_ai_range = { 4, {
221 BIP_RANGE
222 (4.096),
223 BIP_RANGE(4.096
224 / 2),
225 UNI_RANGE
226 (4.096),
227 UNI_RANGE(4.096
228 / 2)
232 static const struct comedi_lrange range_usbdux_ao_range = { 2, {
233 BIP_RANGE
234 (4.096),
235 UNI_RANGE
236 (4.096),
241 * private structure of one subdevice
245 * This is the structure which holds all the data of
246 * this driver one sub device just now: A/D
248 struct usbduxsub {
249 /* attached? */
250 int attached;
251 /* is it associated with a subdevice? */
252 int probed;
253 /* pointer to the usb-device */
254 struct usb_device *usbdev;
255 /* actual number of in-buffers */
256 int numOfInBuffers;
257 /* actual number of out-buffers */
258 int numOfOutBuffers;
259 /* ISO-transfer handling: buffers */
260 struct urb **urbIn;
261 struct urb **urbOut;
262 /* pwm-transfer handling */
263 struct urb *urbPwm;
264 /* PWM period */
265 unsigned int pwmPeriod;
266 /* PWM internal delay for the GPIF in the FX2 */
267 int8_t pwmDelay;
268 /* size of the PWM buffer which holds the bit pattern */
269 int sizePwmBuf;
270 /* input buffer for the ISO-transfer */
271 int16_t *inBuffer;
272 /* input buffer for single insn */
273 int16_t *insnBuffer;
274 /* output buffer for single DA outputs */
275 int16_t *outBuffer;
276 /* interface number */
277 int ifnum;
278 /* interface structure in 2.6 */
279 struct usb_interface *interface;
280 /* comedi device for the interrupt context */
281 struct comedi_device *comedidev;
282 /* is it USB_SPEED_HIGH or not? */
283 short int high_speed;
284 /* asynchronous command is running */
285 short int ai_cmd_running;
286 short int ao_cmd_running;
287 /* pwm is running */
288 short int pwm_cmd_running;
289 /* continous aquisition */
290 short int ai_continous;
291 short int ao_continous;
292 /* number of samples to aquire */
293 int ai_sample_count;
294 int ao_sample_count;
295 /* time between samples in units of the timer */
296 unsigned int ai_timer;
297 unsigned int ao_timer;
298 /* counter between aquisitions */
299 unsigned int ai_counter;
300 unsigned int ao_counter;
301 /* interval in frames/uframes */
302 unsigned int ai_interval;
303 /* D/A commands */
304 int8_t *dac_commands;
305 /* commands */
306 int8_t *dux_commands;
307 struct semaphore sem;
311 * The pointer to the private usb-data of the driver is also the private data
312 * for the comedi-device. This has to be global as the usb subsystem needs
313 * global variables. The other reason is that this structure must be there
314 * _before_ any comedi command is issued. The usb subsystem must be initialised
315 * before comedi can access it.
317 static struct usbduxsub usbduxsub[NUMUSBDUX];
319 static DECLARE_MUTEX(start_stop_sem);
322 * Stops the data acquision
323 * It should be safe to call this function from any context
325 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
327 int i = 0;
328 int err = 0;
330 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
331 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
332 if (usbduxsub_tmp->urbIn[i]) {
333 /* We wait here until all transfers have been
334 * cancelled. */
335 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
337 dev_dbg(&usbduxsub_tmp->interface->dev,
338 "comedi: usbdux: unlinked InURB %d, err=%d\n",
339 i, err);
342 return err;
346 * This will stop a running acquisition operation
347 * Is called from within this driver from both the
348 * interrupt context and from comedi
350 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
352 int ret = 0;
354 if (!this_usbduxsub) {
355 dev_err(&this_usbduxsub->interface->dev,
356 "comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
357 return -EFAULT;
359 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
361 if (do_unlink) {
362 /* stop aquistion */
363 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
366 this_usbduxsub->ai_cmd_running = 0;
368 return ret;
372 * This will cancel a running acquisition operation.
373 * This is called by comedi but never from inside the driver.
375 static int usbdux_ai_cancel(struct comedi_device *dev,
376 struct comedi_subdevice *s)
378 struct usbduxsub *this_usbduxsub;
379 int res = 0;
381 /* force unlink of all urbs */
382 this_usbduxsub = dev->private;
383 if (!this_usbduxsub)
384 return -EFAULT;
386 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
388 /* prevent other CPUs from submitting new commands just now */
389 down(&this_usbduxsub->sem);
390 if (!(this_usbduxsub->probed)) {
391 up(&this_usbduxsub->sem);
392 return -ENODEV;
394 /* unlink only if the urb really has been submitted */
395 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
396 up(&this_usbduxsub->sem);
397 return res;
400 /* analogue IN - interrupt service routine */
401 static void usbduxsub_ai_IsocIrq(struct urb *urb)
403 int i, err, n;
404 struct usbduxsub *this_usbduxsub;
405 struct comedi_device *this_comedidev;
406 struct comedi_subdevice *s;
408 /* the context variable points to the subdevice */
409 this_comedidev = urb->context;
410 /* the private structure of the subdevice is struct usbduxsub */
411 this_usbduxsub = this_comedidev->private;
412 /* subdevice which is the AD converter */
413 s = this_comedidev->subdevices + SUBDEV_AD;
415 /* first we test if something unusual has just happened */
416 switch (urb->status) {
417 case 0:
418 /* copy the result in the transfer buffer */
419 memcpy(this_usbduxsub->inBuffer,
420 urb->transfer_buffer, SIZEINBUF);
421 break;
422 case -EILSEQ:
423 /* error in the ISOchronous data */
424 /* we don't copy the data into the transfer buffer */
425 /* and recycle the last data byte */
426 dev_dbg(&urb->dev->dev,
427 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
428 this_usbduxsub->comedidev->minor);
430 break;
432 case -ECONNRESET:
433 case -ENOENT:
434 case -ESHUTDOWN:
435 case -ECONNABORTED:
436 /* happens after an unlink command */
437 if (this_usbduxsub->ai_cmd_running) {
438 /* we are still running a command */
439 /* tell this comedi */
440 s->async->events |= COMEDI_CB_EOA;
441 s->async->events |= COMEDI_CB_ERROR;
442 comedi_event(this_usbduxsub->comedidev, s);
443 /* stop the transfer w/o unlink */
444 usbdux_ai_stop(this_usbduxsub, 0);
446 return;
448 default:
449 /* a real error on the bus */
450 /* pass error to comedi if we are really running a command */
451 if (this_usbduxsub->ai_cmd_running) {
452 dev_err(&urb->dev->dev,
453 "Non-zero urb status received in ai intr "
454 "context: %d\n", urb->status);
455 s->async->events |= COMEDI_CB_EOA;
456 s->async->events |= COMEDI_CB_ERROR;
457 comedi_event(this_usbduxsub->comedidev, s);
458 /* don't do an unlink here */
459 usbdux_ai_stop(this_usbduxsub, 0);
461 return;
465 * at this point we are reasonably sure that nothing dodgy has happened
466 * are we running a command?
468 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
470 * not running a command, do not continue execution if no
471 * asynchronous command is running in particular not resubmit
473 return;
476 urb->dev = this_usbduxsub->usbdev;
478 /* resubmit the urb */
479 err = usb_submit_urb(urb, GFP_ATOMIC);
480 if (unlikely(err < 0)) {
481 dev_err(&urb->dev->dev,
482 "comedi_: urb resubmit failed in int-context! err=%d\n",
483 err);
484 if (err == -EL2NSYNC)
485 dev_err(&urb->dev->dev,
486 "buggy USB host controller or bug in IRQ "
487 "handler!\n");
488 s->async->events |= COMEDI_CB_EOA;
489 s->async->events |= COMEDI_CB_ERROR;
490 comedi_event(this_usbduxsub->comedidev, s);
491 /* don't do an unlink here */
492 usbdux_ai_stop(this_usbduxsub, 0);
493 return;
496 this_usbduxsub->ai_counter--;
497 if (likely(this_usbduxsub->ai_counter > 0))
498 return;
500 /* timer zero, transfer measurements to comedi */
501 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
503 /* test, if we transmit only a fixed number of samples */
504 if (!(this_usbduxsub->ai_continous)) {
505 /* not continous, fixed number of samples */
506 this_usbduxsub->ai_sample_count--;
507 /* all samples received? */
508 if (this_usbduxsub->ai_sample_count < 0) {
509 /* prevent a resubmit next time */
510 usbdux_ai_stop(this_usbduxsub, 0);
511 /* say comedi that the acquistion is over */
512 s->async->events |= COMEDI_CB_EOA;
513 comedi_event(this_usbduxsub->comedidev, s);
514 return;
517 /* get the data from the USB bus and hand it over to comedi */
518 n = s->async->cmd.chanlist_len;
519 for (i = 0; i < n; i++) {
520 /* transfer data */
521 if (CR_RANGE(s->async->cmd.chanlist[i]) <= 1) {
522 err = comedi_buf_put
523 (s->async,
524 le16_to_cpu(this_usbduxsub->inBuffer[i]) ^ 0x800);
525 } else {
526 err = comedi_buf_put
527 (s->async,
528 le16_to_cpu(this_usbduxsub->inBuffer[i]));
530 if (unlikely(err == 0)) {
531 /* buffer overflow */
532 usbdux_ai_stop(this_usbduxsub, 0);
533 return;
536 /* tell comedi that data is there */
537 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
538 comedi_event(this_usbduxsub->comedidev, s);
541 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
543 int i = 0;
544 int err = 0;
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]);
551 dev_dbg(&usbduxsub_tmp->interface->dev,
552 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
553 i, err);
556 return err;
559 /* This will cancel a running acquisition operation
560 * in any context.
562 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
564 int ret = 0;
566 if (!this_usbduxsub)
567 return -EFAULT;
568 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
570 if (do_unlink)
571 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
573 this_usbduxsub->ao_cmd_running = 0;
575 return ret;
578 /* force unlink, is called by comedi */
579 static int usbdux_ao_cancel(struct comedi_device *dev,
580 struct comedi_subdevice *s)
582 struct usbduxsub *this_usbduxsub = dev->private;
583 int res = 0;
585 if (!this_usbduxsub)
586 return -EFAULT;
588 /* prevent other CPUs from submitting a command just now */
589 down(&this_usbduxsub->sem);
590 if (!(this_usbduxsub->probed)) {
591 up(&this_usbduxsub->sem);
592 return -ENODEV;
594 /* unlink only if it is really running */
595 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
596 up(&this_usbduxsub->sem);
597 return res;
600 static void usbduxsub_ao_IsocIrq(struct urb *urb)
602 int i, ret;
603 int8_t *datap;
604 struct usbduxsub *this_usbduxsub;
605 struct comedi_device *this_comedidev;
606 struct comedi_subdevice *s;
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;
613 s = this_comedidev->subdevices + SUBDEV_DA;
615 switch (urb->status) {
616 case 0:
617 /* success */
618 break;
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);
631 return;
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);
645 return;
648 /* are we actually running? */
649 if (!(this_usbduxsub->ao_cmd_running))
650 return;
652 /* normal operation: executing a command in this subdevice */
653 this_usbduxsub->ao_counter--;
654 if (this_usbduxsub->ao_counter <= 0) {
655 /* timer zero */
656 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
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;
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;
679 /* pointer to the DA */
680 datap =
681 (&(((int8_t *) urb->transfer_buffer)[i * 3 + 1]));
682 /* get the data from comedi */
683 ret = comedi_buf_get(s->async, &temp);
684 datap[0] = temp;
685 datap[1] = temp >> 8;
686 datap[2] = this_usbduxsub->dac_commands[i];
687 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
688 /* datap[0],datap[1],datap[2]); */
689 if (ret < 0) {
690 dev_err(&urb->dev->dev,
691 "comedi: buffer underflow\n");
692 s->async->events |= COMEDI_CB_EOA;
693 s->async->events |= COMEDI_CB_OVERFLOW;
695 /* transmit data to comedi */
696 s->async->events |= COMEDI_CB_BLOCK;
697 comedi_event(this_usbduxsub->comedidev, s);
700 urb->transfer_buffer_length = SIZEOUTBUF;
701 urb->dev = this_usbduxsub->usbdev;
702 urb->status = 0;
703 if (this_usbduxsub->ao_cmd_running) {
704 if (this_usbduxsub->high_speed) {
705 /* uframes */
706 urb->interval = 8;
707 } else {
708 /* frames */
709 urb->interval = 1;
711 urb->number_of_packets = 1;
712 urb->iso_frame_desc[0].offset = 0;
713 urb->iso_frame_desc[0].length = SIZEOUTBUF;
714 urb->iso_frame_desc[0].status = 0;
715 ret = usb_submit_urb(urb, GFP_ATOMIC);
716 if (ret < 0) {
717 dev_err(&urb->dev->dev,
718 "comedi_: ao urb resubm failed in int-cont. "
719 "ret=%d", ret);
720 if (ret == EL2NSYNC)
721 dev_err(&urb->dev->dev,
722 "buggy USB host controller or bug in "
723 "IRQ handling!\n");
725 s->async->events |= COMEDI_CB_EOA;
726 s->async->events |= COMEDI_CB_ERROR;
727 comedi_event(this_usbduxsub->comedidev, s);
728 /* don't do an unlink here */
729 usbdux_ao_stop(this_usbduxsub, 0);
734 static int usbduxsub_start(struct usbduxsub *usbduxsub)
736 int errcode = 0;
737 uint8_t local_transfer_buffer[16];
739 /* 7f92 to zero */
740 local_transfer_buffer[0] = 0;
741 errcode = usb_control_msg(usbduxsub->usbdev,
742 /* create a pipe for a control transfer */
743 usb_sndctrlpipe(usbduxsub->usbdev, 0),
744 /* bRequest, "Firmware" */
745 USBDUXSUB_FIRMWARE,
746 /* bmRequestType */
747 VENDOR_DIR_OUT,
748 /* Value */
749 USBDUXSUB_CPUCS,
750 /* Index */
751 0x0000,
752 /* address of the transfer buffer */
753 local_transfer_buffer,
754 /* Length */
756 /* Timeout */
757 BULK_TIMEOUT);
758 if (errcode < 0) {
759 dev_err(&usbduxsub->interface->dev,
760 "comedi_: control msg failed (start)\n");
761 return errcode;
763 return 0;
766 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
768 int errcode = 0;
770 uint8_t local_transfer_buffer[16];
772 /* 7f92 to one */
773 local_transfer_buffer[0] = 1;
774 errcode = usb_control_msg(usbduxsub->usbdev,
775 usb_sndctrlpipe(usbduxsub->usbdev, 0),
776 /* bRequest, "Firmware" */
777 USBDUXSUB_FIRMWARE,
778 /* bmRequestType */
779 VENDOR_DIR_OUT,
780 /* Value */
781 USBDUXSUB_CPUCS,
782 /* Index */
783 0x0000, local_transfer_buffer,
784 /* Length */
786 /* Timeout */
787 BULK_TIMEOUT);
788 if (errcode < 0) {
789 dev_err(&usbduxsub->interface->dev,
790 "comedi_: control msg failed (stop)\n");
791 return errcode;
793 return 0;
796 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
797 uint8_t * local_transfer_buffer,
798 unsigned int startAddr, unsigned int len)
800 int errcode;
802 errcode = usb_control_msg(usbduxsub->usbdev,
803 usb_sndctrlpipe(usbduxsub->usbdev, 0),
804 /* brequest, firmware */
805 USBDUXSUB_FIRMWARE,
806 /* bmRequestType */
807 VENDOR_DIR_OUT,
808 /* value */
809 startAddr,
810 /* index */
811 0x0000,
812 /* our local safe buffer */
813 local_transfer_buffer,
814 /* length */
815 len,
816 /* timeout */
817 BULK_TIMEOUT);
818 dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
819 if (errcode < 0) {
820 dev_err(&usbduxsub->interface->dev, "comedi_: upload failed\n");
821 return errcode;
823 return 0;
826 #define FIRMWARE_MAX_LEN 0x2000
828 static int firmwareUpload(struct usbduxsub *usbduxsub,
829 const u8 * firmwareBinary, int sizeFirmware)
831 int ret;
832 uint8_t *fwBuf;
834 if (!firmwareBinary)
835 return 0;
837 if (sizeFirmware > FIRMWARE_MAX_LEN) {
838 dev_err(&usbduxsub->interface->dev,
839 "comedi_: usbdux firmware binary it too large for FX2.\n");
840 return -ENOMEM;
843 /* we generate a local buffer for the firmware */
844 fwBuf = kzalloc(sizeFirmware, GFP_KERNEL);
845 if (!fwBuf) {
846 dev_err(&usbduxsub->interface->dev,
847 "comedi_: mem alloc for firmware failed\n");
848 return -ENOMEM;
850 memcpy(fwBuf, firmwareBinary, sizeFirmware);
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;
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;
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;
874 kfree(fwBuf);
875 return 0;
878 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
880 int i, errFlag;
882 if (!usbduxsub)
883 return -EFAULT;
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;
907 return 0;
910 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
912 int i, errFlag;
914 if (!usbduxsub)
915 return -EFAULT;
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;
933 return 0;
936 static int usbdux_ai_cmdtest(struct comedi_device *dev,
937 struct comedi_subdevice *s, struct comedi_cmd *cmd)
939 int err = 0, tmp, i;
940 unsigned int tmpTimer;
941 struct usbduxsub *this_usbduxsub = dev->private;
943 if (!(this_usbduxsub->probed))
944 return -ENODEV;
946 dev_dbg(&this_usbduxsub->interface->dev,
947 "comedi%d: usbdux_ai_cmdtest\n", dev->minor);
949 /* make sure triggers are valid */
950 /* Only immediate triggers are allowed */
951 tmp = cmd->start_src;
952 cmd->start_src &= TRIG_NOW | TRIG_INT;
953 if (!cmd->start_src || tmp != cmd->start_src)
954 err++;
956 /* trigger should happen timed */
957 tmp = cmd->scan_begin_src;
958 /* start a new _scan_ with a timer */
959 cmd->scan_begin_src &= TRIG_TIMER;
960 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
961 err++;
963 /* scanning is continous */
964 tmp = cmd->convert_src;
965 cmd->convert_src &= TRIG_NOW;
966 if (!cmd->convert_src || tmp != cmd->convert_src)
967 err++;
969 /* issue a trigger when scan is finished and start a new scan */
970 tmp = cmd->scan_end_src;
971 cmd->scan_end_src &= TRIG_COUNT;
972 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
973 err++;
975 /* trigger at the end of count events or not, stop condition or not */
976 tmp = cmd->stop_src;
977 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
978 if (!cmd->stop_src || tmp != cmd->stop_src)
979 err++;
981 if (err)
982 return 1;
985 * step 2: make sure trigger sources are unique and mutually compatible
986 * note that mutual compatiblity is not an issue here
988 if (cmd->scan_begin_src != TRIG_FOLLOW &&
989 cmd->scan_begin_src != TRIG_EXT &&
990 cmd->scan_begin_src != TRIG_TIMER)
991 err++;
992 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
993 err++;
995 if (err)
996 return 2;
998 /* step 3: make sure arguments are trivially compatible */
999 if (cmd->start_arg != 0) {
1000 cmd->start_arg = 0;
1001 err++;
1004 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1005 /* internal trigger */
1006 if (cmd->scan_begin_arg != 0) {
1007 cmd->scan_begin_arg = 0;
1008 err++;
1012 if (cmd->scan_begin_src == TRIG_TIMER) {
1013 if (this_usbduxsub->high_speed) {
1015 * In high speed mode microframes are possible.
1016 * However, during one microframe we can roughly
1017 * sample one channel. Thus, the more channels
1018 * are in the channel list the more time we need.
1020 i = 1;
1021 /* find a power of 2 for the number of channels */
1022 while (i < (cmd->chanlist_len))
1023 i = i * 2;
1025 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
1026 cmd->scan_begin_arg = 1000000 / 8 * i;
1027 err++;
1029 /* now calc the real sampling rate with all the
1030 * rounding errors */
1031 tmpTimer =
1032 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
1033 125000;
1034 if (cmd->scan_begin_arg != tmpTimer) {
1035 cmd->scan_begin_arg = tmpTimer;
1036 err++;
1038 } else {
1039 /* full speed */
1040 /* 1kHz scans every USB frame */
1041 if (cmd->scan_begin_arg < 1000000) {
1042 cmd->scan_begin_arg = 1000000;
1043 err++;
1046 * calc the real sampling rate with the rounding errors
1048 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
1049 1000000)) * 1000000;
1050 if (cmd->scan_begin_arg != tmpTimer) {
1051 cmd->scan_begin_arg = tmpTimer;
1052 err++;
1056 /* the same argument */
1057 if (cmd->scan_end_arg != cmd->chanlist_len) {
1058 cmd->scan_end_arg = cmd->chanlist_len;
1059 err++;
1062 if (cmd->stop_src == TRIG_COUNT) {
1063 /* any count is allowed */
1064 } else {
1065 /* TRIG_NONE */
1066 if (cmd->stop_arg != 0) {
1067 cmd->stop_arg = 0;
1068 err++;
1072 if (err)
1073 return 3;
1075 return 0;
1079 * creates the ADC command for the MAX1271
1080 * range is the range value from comedi
1082 static int8_t create_adc_command(unsigned int chan, int range)
1084 int8_t p = (range <= 1);
1085 int8_t r = ((range % 2) == 0);
1086 return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
1089 /* bulk transfers to usbdux */
1091 #define SENDADCOMMANDS 0
1092 #define SENDDACOMMANDS 1
1093 #define SENDDIOCONFIGCOMMAND 2
1094 #define SENDDIOBITSCOMMAND 3
1095 #define SENDSINGLEAD 4
1096 #define READCOUNTERCOMMAND 5
1097 #define WRITECOUNTERCOMMAND 6
1098 #define SENDPWMON 7
1099 #define SENDPWMOFF 8
1101 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1103 int result, nsent;
1105 this_usbduxsub->dux_commands[0] = cmd_type;
1106 #ifdef NOISY_DUX_DEBUGBUG
1107 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1108 this_usbduxsub->comedidev->minor);
1109 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1110 printk(" %02x", this_usbduxsub->dux_commands[result]);
1111 printk("\n");
1112 #endif
1113 result = usb_bulk_msg(this_usbduxsub->usbdev,
1114 usb_sndbulkpipe(this_usbduxsub->usbdev,
1115 COMMAND_OUT_EP),
1116 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1117 &nsent, BULK_TIMEOUT);
1118 if (result < 0)
1119 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1120 "could not transmit dux_command to the usb-device, "
1121 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1123 return result;
1126 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1128 int result = (-EFAULT);
1129 int nrec;
1130 int i;
1132 for (i = 0; i < RETRIES; i++) {
1133 result = usb_bulk_msg(this_usbduxsub->usbdev,
1134 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1135 COMMAND_IN_EP),
1136 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1137 &nrec, BULK_TIMEOUT);
1138 if (result < 0) {
1139 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1140 "insn: USB error %d while receiving DUX command"
1141 "\n", this_usbduxsub->comedidev->minor, result);
1142 return result;
1144 if (le16_to_cpu(this_usbduxsub->insnBuffer[0]) == command)
1145 return result;
1147 /* this is only reached if the data has been requested a couple of
1148 * times */
1149 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1150 "wrong data returned from firmware: want cmd %d, got cmd %d.\n",
1151 this_usbduxsub->comedidev->minor, command,
1152 le16_to_cpu(this_usbduxsub->insnBuffer[0]));
1153 return -EFAULT;
1156 static int usbdux_ai_inttrig(struct comedi_device *dev,
1157 struct comedi_subdevice *s, unsigned int trignum)
1159 int ret;
1160 struct usbduxsub *this_usbduxsub = dev->private;
1161 if (!this_usbduxsub)
1162 return -EFAULT;
1164 down(&this_usbduxsub->sem);
1165 if (!(this_usbduxsub->probed)) {
1166 up(&this_usbduxsub->sem);
1167 return -ENODEV;
1169 dev_dbg(&this_usbduxsub->interface->dev,
1170 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1172 if (trignum != 0) {
1173 dev_err(&this_usbduxsub->interface->dev,
1174 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1175 dev->minor);
1176 up(&this_usbduxsub->sem);
1177 return -EINVAL;
1179 if (!(this_usbduxsub->ai_cmd_running)) {
1180 this_usbduxsub->ai_cmd_running = 1;
1181 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1182 if (ret < 0) {
1183 dev_err(&this_usbduxsub->interface->dev,
1184 "comedi%d: usbdux_ai_inttrig: "
1185 "urbSubmit: err=%d\n", dev->minor, ret);
1186 this_usbduxsub->ai_cmd_running = 0;
1187 up(&this_usbduxsub->sem);
1188 return ret;
1190 s->async->inttrig = NULL;
1191 } else {
1192 dev_err(&this_usbduxsub->interface->dev,
1193 "comedi%d: ai_inttrig but acqu is already running\n",
1194 dev->minor);
1196 up(&this_usbduxsub->sem);
1197 return 1;
1200 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1202 struct comedi_cmd *cmd = &s->async->cmd;
1203 unsigned int chan, range;
1204 int i, ret;
1205 struct usbduxsub *this_usbduxsub = dev->private;
1206 int result;
1208 if (!this_usbduxsub)
1209 return -EFAULT;
1211 dev_dbg(&this_usbduxsub->interface->dev,
1212 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1214 /* block other CPUs from starting an ai_cmd */
1215 down(&this_usbduxsub->sem);
1217 if (!(this_usbduxsub->probed)) {
1218 up(&this_usbduxsub->sem);
1219 return -ENODEV;
1221 if (this_usbduxsub->ai_cmd_running) {
1222 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1223 "ai_cmd not possible. Another ai_cmd is running.\n",
1224 dev->minor);
1225 up(&this_usbduxsub->sem);
1226 return -EBUSY;
1228 /* set current channel of the running aquisition to zero */
1229 s->async->cur_chan = 0;
1231 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1232 for (i = 0; i < cmd->chanlist_len; ++i) {
1233 chan = CR_CHAN(cmd->chanlist[i]);
1234 range = CR_RANGE(cmd->chanlist[i]);
1235 if (i >= NUMCHANNELS) {
1236 dev_err(&this_usbduxsub->interface->dev,
1237 "comedi%d: channel list too long\n",
1238 dev->minor);
1239 break;
1241 this_usbduxsub->dux_commands[i + 2] =
1242 create_adc_command(chan, range);
1245 dev_dbg(&this_usbduxsub->interface->dev,
1246 "comedi %d: sending commands to the usb device: size=%u\n",
1247 dev->minor, NUMCHANNELS);
1249 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1250 if (result < 0) {
1251 up(&this_usbduxsub->sem);
1252 return result;
1255 if (this_usbduxsub->high_speed) {
1257 * every channel gets a time window of 125us. Thus, if we
1258 * sample all 8 channels we need 1ms. If we sample only one
1259 * channel we need only 125us
1261 this_usbduxsub->ai_interval = 1;
1262 /* find a power of 2 for the interval */
1263 while ((this_usbduxsub->ai_interval) < (cmd->chanlist_len)) {
1264 this_usbduxsub->ai_interval =
1265 (this_usbduxsub->ai_interval) * 2;
1267 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1268 (this_usbduxsub->
1269 ai_interval));
1270 } else {
1271 /* interval always 1ms */
1272 this_usbduxsub->ai_interval = 1;
1273 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1275 if (this_usbduxsub->ai_timer < 1) {
1276 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1277 "timer=%d, scan_begin_arg=%d. "
1278 "Not properly tested by cmdtest?\n", dev->minor,
1279 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1280 up(&this_usbduxsub->sem);
1281 return -EINVAL;
1283 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1285 if (cmd->stop_src == TRIG_COUNT) {
1286 /* data arrives as one packet */
1287 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1288 this_usbduxsub->ai_continous = 0;
1289 } else {
1290 /* continous aquisition */
1291 this_usbduxsub->ai_continous = 1;
1292 this_usbduxsub->ai_sample_count = 0;
1295 if (cmd->start_src == TRIG_NOW) {
1296 /* enable this acquisition operation */
1297 this_usbduxsub->ai_cmd_running = 1;
1298 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1299 if (ret < 0) {
1300 this_usbduxsub->ai_cmd_running = 0;
1301 /* fixme: unlink here?? */
1302 up(&this_usbduxsub->sem);
1303 return ret;
1305 s->async->inttrig = NULL;
1306 } else {
1307 /* TRIG_INT */
1308 /* don't enable the acquision operation */
1309 /* wait for an internal signal */
1310 s->async->inttrig = usbdux_ai_inttrig;
1312 up(&this_usbduxsub->sem);
1313 return 0;
1316 /* Mode 0 is used to get a single conversion on demand */
1317 static int usbdux_ai_insn_read(struct comedi_device *dev,
1318 struct comedi_subdevice *s,
1319 struct comedi_insn *insn, unsigned int *data)
1321 int i;
1322 unsigned int one = 0;
1323 int chan, range;
1324 int err;
1325 struct usbduxsub *this_usbduxsub = dev->private;
1327 if (!this_usbduxsub)
1328 return 0;
1330 dev_dbg(&this_usbduxsub->interface->dev,
1331 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1332 dev->minor, insn->n, insn->subdev);
1334 down(&this_usbduxsub->sem);
1335 if (!(this_usbduxsub->probed)) {
1336 up(&this_usbduxsub->sem);
1337 return -ENODEV;
1339 if (this_usbduxsub->ai_cmd_running) {
1340 dev_err(&this_usbduxsub->interface->dev,
1341 "comedi%d: ai_insn_read not possible. "
1342 "Async Command is running.\n", dev->minor);
1343 up(&this_usbduxsub->sem);
1344 return 0;
1347 /* sample one channel */
1348 chan = CR_CHAN(insn->chanspec);
1349 range = CR_RANGE(insn->chanspec);
1350 /* set command for the first channel */
1351 this_usbduxsub->dux_commands[1] = create_adc_command(chan, range);
1353 /* adc commands */
1354 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1355 if (err < 0) {
1356 up(&this_usbduxsub->sem);
1357 return err;
1360 for (i = 0; i < insn->n; i++) {
1361 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1362 if (err < 0) {
1363 up(&this_usbduxsub->sem);
1364 return 0;
1366 one = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1367 if (CR_RANGE(insn->chanspec) <= 1)
1368 one = one ^ 0x800;
1370 data[i] = one;
1372 up(&this_usbduxsub->sem);
1373 return i;
1376 /************************************/
1377 /* analog out */
1379 static int usbdux_ao_insn_read(struct comedi_device *dev,
1380 struct comedi_subdevice *s,
1381 struct comedi_insn *insn, unsigned int *data)
1383 int i;
1384 int chan = CR_CHAN(insn->chanspec);
1385 struct usbduxsub *this_usbduxsub = dev->private;
1387 if (!this_usbduxsub)
1388 return -EFAULT;
1390 down(&this_usbduxsub->sem);
1391 if (!(this_usbduxsub->probed)) {
1392 up(&this_usbduxsub->sem);
1393 return -ENODEV;
1395 for (i = 0; i < insn->n; i++)
1396 data[i] = this_usbduxsub->outBuffer[chan];
1398 up(&this_usbduxsub->sem);
1399 return i;
1402 static int usbdux_ao_insn_write(struct comedi_device *dev,
1403 struct comedi_subdevice *s,
1404 struct comedi_insn *insn, unsigned int *data)
1406 int i, err;
1407 int chan = CR_CHAN(insn->chanspec);
1408 struct usbduxsub *this_usbduxsub = dev->private;
1410 if (!this_usbduxsub)
1411 return -EFAULT;
1413 dev_dbg(&this_usbduxsub->interface->dev,
1414 "comedi%d: ao_insn_write\n", dev->minor);
1416 down(&this_usbduxsub->sem);
1417 if (!(this_usbduxsub->probed)) {
1418 up(&this_usbduxsub->sem);
1419 return -ENODEV;
1421 if (this_usbduxsub->ao_cmd_running) {
1422 dev_err(&this_usbduxsub->interface->dev,
1423 "comedi%d: ao_insn_write: "
1424 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1425 up(&this_usbduxsub->sem);
1426 return 0;
1429 for (i = 0; i < insn->n; i++) {
1430 dev_dbg(&this_usbduxsub->interface->dev,
1431 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1432 dev->minor, chan, i, data[i]);
1434 /* number of channels: 1 */
1435 this_usbduxsub->dux_commands[1] = 1;
1436 /* one 16 bit value */
1437 *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
1438 cpu_to_le16(data[i]);
1439 this_usbduxsub->outBuffer[chan] = data[i];
1440 /* channel number */
1441 this_usbduxsub->dux_commands[4] = (chan << 6);
1442 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1443 if (err < 0) {
1444 up(&this_usbduxsub->sem);
1445 return err;
1448 up(&this_usbduxsub->sem);
1450 return i;
1453 static int usbdux_ao_inttrig(struct comedi_device *dev,
1454 struct comedi_subdevice *s, unsigned int trignum)
1456 int ret;
1457 struct usbduxsub *this_usbduxsub = dev->private;
1459 if (!this_usbduxsub)
1460 return -EFAULT;
1462 down(&this_usbduxsub->sem);
1463 if (!(this_usbduxsub->probed)) {
1464 up(&this_usbduxsub->sem);
1465 return -ENODEV;
1467 if (trignum != 0) {
1468 dev_err(&this_usbduxsub->interface->dev,
1469 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1470 dev->minor);
1471 return -EINVAL;
1473 if (!(this_usbduxsub->ao_cmd_running)) {
1474 this_usbduxsub->ao_cmd_running = 1;
1475 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1476 if (ret < 0) {
1477 dev_err(&this_usbduxsub->interface->dev,
1478 "comedi%d: usbdux_ao_inttrig: submitURB: "
1479 "err=%d\n", dev->minor, ret);
1480 this_usbduxsub->ao_cmd_running = 0;
1481 up(&this_usbduxsub->sem);
1482 return ret;
1484 s->async->inttrig = NULL;
1485 } else {
1486 dev_err(&this_usbduxsub->interface->dev,
1487 "comedi%d: ao_inttrig but acqu is already running.\n",
1488 dev->minor);
1490 up(&this_usbduxsub->sem);
1491 return 1;
1494 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1495 struct comedi_subdevice *s, struct comedi_cmd *cmd)
1497 int err = 0, tmp;
1498 struct usbduxsub *this_usbduxsub = dev->private;
1500 if (!this_usbduxsub)
1501 return -EFAULT;
1503 if (!(this_usbduxsub->probed))
1504 return -ENODEV;
1506 dev_dbg(&this_usbduxsub->interface->dev,
1507 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1509 /* make sure triggers are valid */
1510 /* Only immediate triggers are allowed */
1511 tmp = cmd->start_src;
1512 cmd->start_src &= TRIG_NOW | TRIG_INT;
1513 if (!cmd->start_src || tmp != cmd->start_src)
1514 err++;
1516 /* trigger should happen timed */
1517 tmp = cmd->scan_begin_src;
1518 /* just now we scan also in the high speed mode every frame */
1519 /* this is due to ehci driver limitations */
1520 if (0) { /* (this_usbduxsub->high_speed) */
1521 /* start immidiately a new scan */
1522 /* the sampling rate is set by the coversion rate */
1523 cmd->scan_begin_src &= TRIG_FOLLOW;
1524 } else {
1525 /* start a new scan (output at once) with a timer */
1526 cmd->scan_begin_src &= TRIG_TIMER;
1528 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1529 err++;
1531 /* scanning is continous */
1532 tmp = cmd->convert_src;
1533 /* we always output at 1kHz just now all channels at once */
1534 if (0) { /* (this_usbduxsub->high_speed) */
1536 * in usb-2.0 only one conversion it tranmitted but with 8kHz/n
1538 cmd->convert_src &= TRIG_TIMER;
1539 } else {
1540 /* all conversion events happen simultaneously with a rate of
1541 * 1kHz/n */
1542 cmd->convert_src &= TRIG_NOW;
1544 if (!cmd->convert_src || tmp != cmd->convert_src)
1545 err++;
1547 /* issue a trigger when scan is finished and start a new scan */
1548 tmp = cmd->scan_end_src;
1549 cmd->scan_end_src &= TRIG_COUNT;
1550 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1551 err++;
1553 /* trigger at the end of count events or not, stop condition or not */
1554 tmp = cmd->stop_src;
1555 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1556 if (!cmd->stop_src || tmp != cmd->stop_src)
1557 err++;
1559 if (err)
1560 return 1;
1563 * step 2: make sure trigger sources are unique and mutually compatible
1564 * note that mutual compatiblity is not an issue here
1566 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1567 cmd->scan_begin_src != TRIG_EXT &&
1568 cmd->scan_begin_src != TRIG_TIMER)
1569 err++;
1570 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1571 err++;
1573 if (err)
1574 return 2;
1576 /* step 3: make sure arguments are trivially compatible */
1578 if (cmd->start_arg != 0) {
1579 cmd->start_arg = 0;
1580 err++;
1583 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1584 /* internal trigger */
1585 if (cmd->scan_begin_arg != 0) {
1586 cmd->scan_begin_arg = 0;
1587 err++;
1591 if (cmd->scan_begin_src == TRIG_TIMER) {
1592 /* timer */
1593 if (cmd->scan_begin_arg < 1000000) {
1594 cmd->scan_begin_arg = 1000000;
1595 err++;
1598 /* not used now, is for later use */
1599 if (cmd->convert_src == TRIG_TIMER) {
1600 if (cmd->convert_arg < 125000) {
1601 cmd->convert_arg = 125000;
1602 err++;
1606 /* the same argument */
1607 if (cmd->scan_end_arg != cmd->chanlist_len) {
1608 cmd->scan_end_arg = cmd->chanlist_len;
1609 err++;
1612 if (cmd->stop_src == TRIG_COUNT) {
1613 /* any count is allowed */
1614 } else {
1615 /* TRIG_NONE */
1616 if (cmd->stop_arg != 0) {
1617 cmd->stop_arg = 0;
1618 err++;
1622 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: err=%d, "
1623 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1624 "convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src,
1625 cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1627 if (err)
1628 return 3;
1630 return 0;
1633 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1635 struct comedi_cmd *cmd = &s->async->cmd;
1636 unsigned int chan, gain;
1637 int i, ret;
1638 struct usbduxsub *this_usbduxsub = dev->private;
1640 if (!this_usbduxsub)
1641 return -EFAULT;
1643 down(&this_usbduxsub->sem);
1644 if (!(this_usbduxsub->probed)) {
1645 up(&this_usbduxsub->sem);
1646 return -ENODEV;
1648 dev_dbg(&this_usbduxsub->interface->dev,
1649 "comedi%d: %s\n", dev->minor, __func__);
1651 /* set current channel of the running aquisition to zero */
1652 s->async->cur_chan = 0;
1653 for (i = 0; i < cmd->chanlist_len; ++i) {
1654 chan = CR_CHAN(cmd->chanlist[i]);
1655 gain = CR_RANGE(cmd->chanlist[i]);
1656 if (i >= NUMOUTCHANNELS) {
1657 dev_err(&this_usbduxsub->interface->dev,
1658 "comedi%d: %s: channel list too long\n",
1659 dev->minor, __func__);
1660 break;
1662 this_usbduxsub->dac_commands[i] = (chan << 6);
1663 dev_dbg(&this_usbduxsub->interface->dev,
1664 "comedi%d: dac command for ch %d is %x\n",
1665 dev->minor, i, this_usbduxsub->dac_commands[i]);
1668 /* we count in steps of 1ms (125us) */
1669 /* 125us mode not used yet */
1670 if (0) { /* (this_usbduxsub->high_speed) */
1671 /* 125us */
1672 /* timing of the conversion itself: every 125 us */
1673 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1674 } else {
1675 /* 1ms */
1676 /* timing of the scan: we get all channels at once */
1677 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1678 dev_dbg(&this_usbduxsub->interface->dev,
1679 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1680 "convert_src=%d, convert_arg=%d\n", dev->minor,
1681 cmd->scan_begin_src, cmd->scan_begin_arg,
1682 cmd->convert_src, cmd->convert_arg);
1683 dev_dbg(&this_usbduxsub->interface->dev,
1684 "comedi%d: ao_timer=%d (ms)\n",
1685 dev->minor, this_usbduxsub->ao_timer);
1686 if (this_usbduxsub->ao_timer < 1) {
1687 dev_err(&this_usbduxsub->interface->dev,
1688 "comedi%d: usbdux: ao_timer=%d, "
1689 "scan_begin_arg=%d. "
1690 "Not properly tested by cmdtest?\n",
1691 dev->minor, this_usbduxsub->ao_timer,
1692 cmd->scan_begin_arg);
1693 up(&this_usbduxsub->sem);
1694 return -EINVAL;
1697 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1699 if (cmd->stop_src == TRIG_COUNT) {
1700 /* not continous */
1701 /* counter */
1702 /* high speed also scans everything at once */
1703 if (0) { /* (this_usbduxsub->high_speed) */
1704 this_usbduxsub->ao_sample_count =
1705 (cmd->stop_arg) * (cmd->scan_end_arg);
1706 } else {
1707 /* there's no scan as the scan has been */
1708 /* perf inside the FX2 */
1709 /* data arrives as one packet */
1710 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1712 this_usbduxsub->ao_continous = 0;
1713 } else {
1714 /* continous aquisition */
1715 this_usbduxsub->ao_continous = 1;
1716 this_usbduxsub->ao_sample_count = 0;
1719 if (cmd->start_src == TRIG_NOW) {
1720 /* enable this acquisition operation */
1721 this_usbduxsub->ao_cmd_running = 1;
1722 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1723 if (ret < 0) {
1724 this_usbduxsub->ao_cmd_running = 0;
1725 /* fixme: unlink here?? */
1726 up(&this_usbduxsub->sem);
1727 return ret;
1729 s->async->inttrig = NULL;
1730 } else {
1731 /* TRIG_INT */
1732 /* submit the urbs later */
1733 /* wait for an internal signal */
1734 s->async->inttrig = usbdux_ao_inttrig;
1737 up(&this_usbduxsub->sem);
1738 return 0;
1741 static int usbdux_dio_insn_config(struct comedi_device *dev,
1742 struct comedi_subdevice *s,
1743 struct comedi_insn *insn, unsigned int *data)
1745 int chan = CR_CHAN(insn->chanspec);
1747 /* The input or output configuration of each digital line is
1748 * configured by a special insn_config instruction. chanspec
1749 * contains the channel to be changed, and data[0] contains the
1750 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1752 switch (data[0]) {
1753 case INSN_CONFIG_DIO_OUTPUT:
1754 s->io_bits |= 1 << chan; /* 1 means Out */
1755 break;
1756 case INSN_CONFIG_DIO_INPUT:
1757 s->io_bits &= ~(1 << chan);
1758 break;
1759 case INSN_CONFIG_DIO_QUERY:
1760 data[1] =
1761 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1762 break;
1763 default:
1764 return -EINVAL;
1765 break;
1767 /* we don't tell the firmware here as it would take 8 frames */
1768 /* to submit the information. We do it in the insn_bits. */
1769 return insn->n;
1772 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1773 struct comedi_subdevice *s,
1774 struct comedi_insn *insn, unsigned int *data)
1777 struct usbduxsub *this_usbduxsub = dev->private;
1778 int err;
1780 if (!this_usbduxsub)
1781 return -EFAULT;
1783 if (insn->n != 2)
1784 return -EINVAL;
1786 down(&this_usbduxsub->sem);
1788 if (!(this_usbduxsub->probed)) {
1789 up(&this_usbduxsub->sem);
1790 return -ENODEV;
1793 /* The insn data is a mask in data[0] and the new data
1794 * in data[1], each channel cooresponding to a bit. */
1795 s->state &= ~data[0];
1796 s->state |= data[0] & data[1];
1797 this_usbduxsub->dux_commands[1] = s->io_bits;
1798 this_usbduxsub->dux_commands[2] = s->state;
1800 /* This command also tells the firmware to return */
1801 /* the digital input lines */
1802 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1803 if (err < 0) {
1804 up(&this_usbduxsub->sem);
1805 return err;
1807 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1808 if (err < 0) {
1809 up(&this_usbduxsub->sem);
1810 return err;
1813 data[1] = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1814 up(&this_usbduxsub->sem);
1815 return 2;
1818 /* reads the 4 counters, only two are used just now */
1819 static int usbdux_counter_read(struct comedi_device *dev,
1820 struct comedi_subdevice *s,
1821 struct comedi_insn *insn, unsigned int *data)
1823 struct usbduxsub *this_usbduxsub = dev->private;
1824 int chan = insn->chanspec;
1825 int err;
1827 if (!this_usbduxsub)
1828 return -EFAULT;
1830 down(&this_usbduxsub->sem);
1832 if (!(this_usbduxsub->probed)) {
1833 up(&this_usbduxsub->sem);
1834 return -ENODEV;
1837 err = send_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1838 if (err < 0) {
1839 up(&this_usbduxsub->sem);
1840 return err;
1843 err = receive_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1844 if (err < 0) {
1845 up(&this_usbduxsub->sem);
1846 return err;
1849 data[0] = le16_to_cpu(this_usbduxsub->insnBuffer[chan + 1]);
1850 up(&this_usbduxsub->sem);
1851 return 1;
1854 static int usbdux_counter_write(struct comedi_device *dev,
1855 struct comedi_subdevice *s,
1856 struct comedi_insn *insn, unsigned int *data)
1858 struct usbduxsub *this_usbduxsub = dev->private;
1859 int err;
1861 if (!this_usbduxsub)
1862 return -EFAULT;
1864 down(&this_usbduxsub->sem);
1866 if (!(this_usbduxsub->probed)) {
1867 up(&this_usbduxsub->sem);
1868 return -ENODEV;
1871 this_usbduxsub->dux_commands[1] = insn->chanspec;
1872 *((int16_t *) (this_usbduxsub->dux_commands + 2)) = cpu_to_le16(*data);
1874 err = send_dux_commands(this_usbduxsub, WRITECOUNTERCOMMAND);
1875 if (err < 0) {
1876 up(&this_usbduxsub->sem);
1877 return err;
1880 up(&this_usbduxsub->sem);
1882 return 1;
1885 static int usbdux_counter_config(struct comedi_device *dev,
1886 struct comedi_subdevice *s,
1887 struct comedi_insn *insn, unsigned int *data)
1889 /* nothing to do so far */
1890 return 2;
1893 /***********************************/
1894 /* PWM */
1896 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1898 int err = 0;
1900 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1901 if (usbduxsub_tmp->urbPwm)
1902 usb_kill_urb(usbduxsub_tmp->urbPwm);
1903 dev_dbg(&usbduxsub_tmp->interface->dev,
1904 "comedi: unlinked PwmURB: res=%d\n", err);
1906 return err;
1909 /* This cancels a running acquisition operation
1910 * in any context.
1912 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1914 int ret = 0;
1916 if (!this_usbduxsub)
1917 return -EFAULT;
1919 dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1920 if (do_unlink)
1921 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1923 this_usbduxsub->pwm_cmd_running = 0;
1925 return ret;
1928 /* force unlink - is called by comedi */
1929 static int usbdux_pwm_cancel(struct comedi_device *dev,
1930 struct comedi_subdevice *s)
1932 struct usbduxsub *this_usbduxsub = dev->private;
1933 int res = 0;
1935 /* unlink only if it is really running */
1936 res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1938 dev_dbg(&this_usbduxsub->interface->dev,
1939 "comedi %d: sending pwm off command to the usb device.\n",
1940 dev->minor);
1941 res = send_dux_commands(this_usbduxsub, SENDPWMOFF);
1942 if (res < 0)
1943 return res;
1945 return res;
1948 static void usbduxsub_pwm_irq(struct urb *urb)
1950 int ret;
1951 struct usbduxsub *this_usbduxsub;
1952 struct comedi_device *this_comedidev;
1953 struct comedi_subdevice *s;
1955 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1957 /* the context variable points to the subdevice */
1958 this_comedidev = urb->context;
1959 /* the private structure of the subdevice is struct usbduxsub */
1960 this_usbduxsub = this_comedidev->private;
1962 s = this_comedidev->subdevices + SUBDEV_DA;
1964 switch (urb->status) {
1965 case 0:
1966 /* success */
1967 break;
1969 case -ECONNRESET:
1970 case -ENOENT:
1971 case -ESHUTDOWN:
1972 case -ECONNABORTED:
1974 * after an unlink command, unplug, ... etc
1975 * no unlink needed here. Already shutting down.
1977 if (this_usbduxsub->pwm_cmd_running)
1978 usbdux_pwm_stop(this_usbduxsub, 0);
1980 return;
1982 default:
1983 /* a real error */
1984 if (this_usbduxsub->pwm_cmd_running) {
1985 dev_err(&this_usbduxsub->interface->dev,
1986 "comedi_: Non-zero urb status received in "
1987 "pwm intr context: %d\n", urb->status);
1988 usbdux_pwm_stop(this_usbduxsub, 0);
1990 return;
1993 /* are we actually running? */
1994 if (!(this_usbduxsub->pwm_cmd_running))
1995 return;
1997 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1998 urb->dev = this_usbduxsub->usbdev;
1999 urb->status = 0;
2000 if (this_usbduxsub->pwm_cmd_running) {
2001 ret = usb_submit_urb(urb, GFP_ATOMIC);
2002 if (ret < 0) {
2003 dev_err(&this_usbduxsub->interface->dev,
2004 "comedi_: pwm urb resubm failed in int-cont. "
2005 "ret=%d", ret);
2006 if (ret == EL2NSYNC)
2007 dev_err(&this_usbduxsub->interface->dev,
2008 "buggy USB host controller or bug in "
2009 "IRQ handling!\n");
2011 /* don't do an unlink here */
2012 usbdux_pwm_stop(this_usbduxsub, 0);
2017 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
2019 int errFlag;
2021 if (!usbduxsub)
2022 return -EFAULT;
2024 dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
2026 /* in case of a resubmission after an unlink... */
2027 usb_fill_bulk_urb(usbduxsub->urbPwm,
2028 usbduxsub->usbdev,
2029 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
2030 usbduxsub->urbPwm->transfer_buffer,
2031 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq,
2032 usbduxsub->comedidev);
2034 errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
2035 if (errFlag) {
2036 dev_err(&usbduxsub->interface->dev,
2037 "comedi_: usbdux: pwm: usb_submit_urb error %d\n",
2038 errFlag);
2039 return errFlag;
2041 return 0;
2044 static int usbdux_pwm_period(struct comedi_device *dev,
2045 struct comedi_subdevice *s, unsigned int period)
2047 struct usbduxsub *this_usbduxsub = dev->private;
2048 int fx2delay = 255;
2050 if (period < MIN_PWM_PERIOD) {
2051 dev_err(&this_usbduxsub->interface->dev,
2052 "comedi%d: illegal period setting for pwm.\n",
2053 dev->minor);
2054 return -EAGAIN;
2055 } else {
2056 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
2057 if (fx2delay > 255) {
2058 dev_err(&this_usbduxsub->interface->dev,
2059 "comedi%d: period %d for pwm is too low.\n",
2060 dev->minor, period);
2061 return -EAGAIN;
2064 this_usbduxsub->pwmDelay = fx2delay;
2065 this_usbduxsub->pwmPeriod = period;
2066 dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
2067 __func__, period, fx2delay);
2068 return 0;
2071 /* is called from insn so there's no need to do all the sanity checks */
2072 static int usbdux_pwm_start(struct comedi_device *dev,
2073 struct comedi_subdevice *s)
2075 int ret, i;
2076 struct usbduxsub *this_usbduxsub = dev->private;
2078 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
2079 dev->minor, __func__);
2081 if (this_usbduxsub->pwm_cmd_running) {
2082 /* already running */
2083 return 0;
2086 this_usbduxsub->dux_commands[1] = ((int8_t) this_usbduxsub->pwmDelay);
2087 ret = send_dux_commands(this_usbduxsub, SENDPWMON);
2088 if (ret < 0)
2089 return ret;
2091 /* initalise the buffer */
2092 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
2093 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
2095 this_usbduxsub->pwm_cmd_running = 1;
2096 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
2097 if (ret < 0) {
2098 this_usbduxsub->pwm_cmd_running = 0;
2099 return ret;
2101 return 0;
2104 /* generates the bit pattern for PWM with the optional sign bit */
2105 static int usbdux_pwm_pattern(struct comedi_device *dev,
2106 struct comedi_subdevice *s, int channel,
2107 unsigned int value, unsigned int sign)
2109 struct usbduxsub *this_usbduxsub = dev->private;
2110 int i, szbuf;
2111 char *pBuf;
2112 char pwm_mask;
2113 char sgn_mask;
2114 char c;
2116 if (!this_usbduxsub)
2117 return -EFAULT;
2119 /* this is the DIO bit which carries the PWM data */
2120 pwm_mask = (1 << channel);
2121 /* this is the DIO bit which carries the optional direction bit */
2122 sgn_mask = (16 << channel);
2123 /* this is the buffer which will be filled with the with bit */
2124 /* pattern for one period */
2125 szbuf = this_usbduxsub->sizePwmBuf;
2126 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2127 for (i = 0; i < szbuf; i++) {
2128 c = *pBuf;
2129 /* reset bits */
2130 c = c & (~pwm_mask);
2131 /* set the bit as long as the index is lower than the value */
2132 if (i < value)
2133 c = c | pwm_mask;
2134 /* set the optional sign bit for a relay */
2135 if (!sign) {
2136 /* positive value */
2137 c = c & (~sgn_mask);
2138 } else {
2139 /* negative value */
2140 c = c | sgn_mask;
2142 *(pBuf++) = c;
2144 return 1;
2147 static int usbdux_pwm_write(struct comedi_device *dev,
2148 struct comedi_subdevice *s,
2149 struct comedi_insn *insn, unsigned int *data)
2151 struct usbduxsub *this_usbduxsub = dev->private;
2153 if (!this_usbduxsub)
2154 return -EFAULT;
2156 if ((insn->n) != 1) {
2158 * doesn't make sense to have more than one value here because
2159 * it would just overwrite the PWM buffer a couple of times
2161 return -EINVAL;
2165 * the sign is set via a special INSN only, this gives us 8 bits for
2166 * normal operation
2167 * relay sign 0 by default
2169 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
2172 static int usbdux_pwm_read(struct comedi_device *x1,
2173 struct comedi_subdevice *x2, struct comedi_insn *x3,
2174 unsigned int *x4)
2176 /* not needed */
2177 return -EINVAL;
2180 /* switches on/off PWM */
2181 static int usbdux_pwm_config(struct comedi_device *dev,
2182 struct comedi_subdevice *s,
2183 struct comedi_insn *insn, unsigned int *data)
2185 struct usbduxsub *this_usbduxsub = dev->private;
2186 switch (data[0]) {
2187 case INSN_CONFIG_ARM:
2188 /* switch it on */
2189 dev_dbg(&this_usbduxsub->interface->dev,
2190 "comedi%d: %s: pwm on\n", dev->minor, __func__);
2192 * if not zero the PWM is limited to a certain time which is
2193 * not supported here
2195 if (data[1] != 0)
2196 return -EINVAL;
2197 return usbdux_pwm_start(dev, s);
2198 case INSN_CONFIG_DISARM:
2199 dev_dbg(&this_usbduxsub->interface->dev,
2200 "comedi%d: %s: pwm off\n", dev->minor, __func__);
2201 return usbdux_pwm_cancel(dev, s);
2202 case INSN_CONFIG_GET_PWM_STATUS:
2204 * to check if the USB transmission has failed or in case PWM
2205 * was limited to n cycles to check if it has terminated
2207 data[1] = this_usbduxsub->pwm_cmd_running;
2208 return 0;
2209 case INSN_CONFIG_PWM_SET_PERIOD:
2210 dev_dbg(&this_usbduxsub->interface->dev,
2211 "comedi%d: %s: setting period\n", dev->minor, __func__);
2212 return usbdux_pwm_period(dev, s, data[1]);
2213 case INSN_CONFIG_PWM_GET_PERIOD:
2214 data[1] = this_usbduxsub->pwmPeriod;
2215 return 0;
2216 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2217 /* value in the first byte and the sign in the second for a
2218 relay */
2219 return usbdux_pwm_pattern(dev, s,
2220 /* the channel number */
2221 CR_CHAN(insn->chanspec),
2222 /* actual PWM data */
2223 data[1],
2224 /* just a sign */
2225 (data[2] != 0));
2226 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2227 /* values are not kept in this driver, nothing to return here */
2228 return -EINVAL;
2230 return -EINVAL;
2233 /* end of PWM */
2234 /*****************************************************************/
2236 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2238 int i;
2240 if (!usbduxsub_tmp)
2241 return;
2242 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2244 /* shows the usb subsystem that the driver is down */
2245 if (usbduxsub_tmp->interface)
2246 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2248 usbduxsub_tmp->probed = 0;
2250 if (usbduxsub_tmp->urbIn) {
2251 if (usbduxsub_tmp->ai_cmd_running) {
2252 usbduxsub_tmp->ai_cmd_running = 0;
2253 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2255 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2256 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2257 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2258 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2259 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2260 usbduxsub_tmp->urbIn[i] = NULL;
2262 kfree(usbduxsub_tmp->urbIn);
2263 usbduxsub_tmp->urbIn = NULL;
2265 if (usbduxsub_tmp->urbOut) {
2266 if (usbduxsub_tmp->ao_cmd_running) {
2267 usbduxsub_tmp->ao_cmd_running = 0;
2268 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2270 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2271 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2272 kfree(usbduxsub_tmp->
2273 urbOut[i]->transfer_buffer);
2274 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2275 NULL;
2277 if (usbduxsub_tmp->urbOut[i]) {
2278 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2279 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2280 usbduxsub_tmp->urbOut[i] = NULL;
2283 kfree(usbduxsub_tmp->urbOut);
2284 usbduxsub_tmp->urbOut = NULL;
2286 if (usbduxsub_tmp->urbPwm) {
2287 if (usbduxsub_tmp->pwm_cmd_running) {
2288 usbduxsub_tmp->pwm_cmd_running = 0;
2289 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2291 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2292 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2293 usb_kill_urb(usbduxsub_tmp->urbPwm);
2294 usb_free_urb(usbduxsub_tmp->urbPwm);
2295 usbduxsub_tmp->urbPwm = NULL;
2297 kfree(usbduxsub_tmp->inBuffer);
2298 usbduxsub_tmp->inBuffer = NULL;
2299 kfree(usbduxsub_tmp->insnBuffer);
2300 usbduxsub_tmp->insnBuffer = NULL;
2301 kfree(usbduxsub_tmp->inBuffer);
2302 usbduxsub_tmp->inBuffer = NULL;
2303 kfree(usbduxsub_tmp->dac_commands);
2304 usbduxsub_tmp->dac_commands = NULL;
2305 kfree(usbduxsub_tmp->dux_commands);
2306 usbduxsub_tmp->dux_commands = NULL;
2307 usbduxsub_tmp->ai_cmd_running = 0;
2308 usbduxsub_tmp->ao_cmd_running = 0;
2309 usbduxsub_tmp->pwm_cmd_running = 0;
2312 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2313 void *context)
2315 struct usbduxsub *usbduxsub_tmp = context;
2316 struct usb_device *usbdev = usbduxsub_tmp->usbdev;
2317 int ret;
2319 if (fw == NULL) {
2320 dev_err(&usbdev->dev,
2321 "Firmware complete handler without firmware!\n");
2322 return;
2326 * we need to upload the firmware here because fw will be
2327 * freed once we've left this function
2329 ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2331 if (ret) {
2332 dev_err(&usbdev->dev,
2333 "Could not upload firmware (err=%d)\n", ret);
2334 return;
2336 comedi_usb_auto_config(usbdev, BOARDNAME);
2339 /* allocate memory for the urbs and initialise them */
2340 static int usbduxsub_probe(struct usb_interface *uinterf,
2341 const struct usb_device_id *id)
2343 struct usb_device *udev = interface_to_usbdev(uinterf);
2344 struct device *dev = &uinterf->dev;
2345 int i;
2346 int index;
2347 int ret;
2349 dev_dbg(dev, "comedi_: usbdux_: "
2350 "finding a free structure for the usb-device\n");
2352 down(&start_stop_sem);
2353 /* look for a free place in the usbdux array */
2354 index = -1;
2355 for (i = 0; i < NUMUSBDUX; i++) {
2356 if (!(usbduxsub[i].probed)) {
2357 index = i;
2358 break;
2362 /* no more space */
2363 if (index == -1) {
2364 dev_err(dev, "Too many usbdux-devices connected.\n");
2365 up(&start_stop_sem);
2366 return -EMFILE;
2368 dev_dbg(dev, "comedi_: usbdux: "
2369 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2371 init_MUTEX(&(usbduxsub[index].sem));
2372 /* save a pointer to the usb device */
2373 usbduxsub[index].usbdev = udev;
2375 /* 2.6: save the interface itself */
2376 usbduxsub[index].interface = uinterf;
2377 /* get the interface number from the interface */
2378 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2379 /* hand the private data over to the usb subsystem */
2380 /* will be needed for disconnect */
2381 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2383 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2385 /* test if it is high speed (USB 2.0) */
2386 usbduxsub[index].high_speed =
2387 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2389 /* create space for the commands of the DA converter */
2390 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2391 if (!usbduxsub[index].dac_commands) {
2392 dev_err(dev, "comedi_: usbdux: "
2393 "error alloc space for dac commands\n");
2394 tidy_up(&(usbduxsub[index]));
2395 up(&start_stop_sem);
2396 return -ENOMEM;
2398 /* create space for the commands going to the usb device */
2399 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2400 if (!usbduxsub[index].dux_commands) {
2401 dev_err(dev, "comedi_: usbdux: "
2402 "error alloc space for dac commands\n");
2403 tidy_up(&(usbduxsub[index]));
2404 up(&start_stop_sem);
2405 return -ENOMEM;
2407 /* create space for the in buffer and set it to zero */
2408 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2409 if (!(usbduxsub[index].inBuffer)) {
2410 dev_err(dev, "comedi_: usbdux: "
2411 "could not alloc space for inBuffer\n");
2412 tidy_up(&(usbduxsub[index]));
2413 up(&start_stop_sem);
2414 return -ENOMEM;
2416 /* create space of the instruction buffer */
2417 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2418 if (!(usbduxsub[index].insnBuffer)) {
2419 dev_err(dev, "comedi_: usbdux: "
2420 "could not alloc space for insnBuffer\n");
2421 tidy_up(&(usbduxsub[index]));
2422 up(&start_stop_sem);
2423 return -ENOMEM;
2425 /* create space for the outbuffer */
2426 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2427 if (!(usbduxsub[index].outBuffer)) {
2428 dev_err(dev, "comedi_: usbdux: "
2429 "could not alloc space for outBuffer\n");
2430 tidy_up(&(usbduxsub[index]));
2431 up(&start_stop_sem);
2432 return -ENOMEM;
2434 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2435 i = usb_set_interface(usbduxsub[index].usbdev,
2436 usbduxsub[index].ifnum, 3);
2437 if (i < 0) {
2438 dev_err(dev, "comedi_: usbdux%d: "
2439 "could not set alternate setting 3 in high speed.\n",
2440 index);
2441 tidy_up(&(usbduxsub[index]));
2442 up(&start_stop_sem);
2443 return -ENODEV;
2445 if (usbduxsub[index].high_speed)
2446 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2447 else
2448 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2450 usbduxsub[index].urbIn =
2451 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2452 GFP_KERNEL);
2453 if (!(usbduxsub[index].urbIn)) {
2454 dev_err(dev, "comedi_: usbdux: Could not alloc. urbIn array\n");
2455 tidy_up(&(usbduxsub[index]));
2456 up(&start_stop_sem);
2457 return -ENOMEM;
2459 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2460 /* one frame: 1ms */
2461 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2462 if (usbduxsub[index].urbIn[i] == NULL) {
2463 dev_err(dev, "comedi_: usbdux%d: "
2464 "Could not alloc. urb(%d)\n", index, i);
2465 tidy_up(&(usbduxsub[index]));
2466 up(&start_stop_sem);
2467 return -ENOMEM;
2469 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2470 /* will be filled later with a pointer to the comedi-device */
2471 /* and ONLY then the urb should be submitted */
2472 usbduxsub[index].urbIn[i]->context = NULL;
2473 usbduxsub[index].urbIn[i]->pipe =
2474 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2475 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2476 usbduxsub[index].urbIn[i]->transfer_buffer =
2477 kzalloc(SIZEINBUF, GFP_KERNEL);
2478 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2479 dev_err(dev, "comedi_: usbdux%d: "
2480 "could not alloc. transb.\n", index);
2481 tidy_up(&(usbduxsub[index]));
2482 up(&start_stop_sem);
2483 return -ENOMEM;
2485 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2486 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2487 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2488 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2489 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length = SIZEINBUF;
2492 /* out */
2493 if (usbduxsub[index].high_speed)
2494 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2495 else
2496 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2498 usbduxsub[index].urbOut =
2499 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2500 GFP_KERNEL);
2501 if (!(usbduxsub[index].urbOut)) {
2502 dev_err(dev, "comedi_: usbdux: "
2503 "Could not alloc. urbOut array\n");
2504 tidy_up(&(usbduxsub[index]));
2505 up(&start_stop_sem);
2506 return -ENOMEM;
2508 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2509 /* one frame: 1ms */
2510 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2511 if (usbduxsub[index].urbOut[i] == NULL) {
2512 dev_err(dev, "comedi_: usbdux%d: "
2513 "Could not alloc. urb(%d)\n", index, i);
2514 tidy_up(&(usbduxsub[index]));
2515 up(&start_stop_sem);
2516 return -ENOMEM;
2518 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2519 /* will be filled later with a pointer to the comedi-device */
2520 /* and ONLY then the urb should be submitted */
2521 usbduxsub[index].urbOut[i]->context = NULL;
2522 usbduxsub[index].urbOut[i]->pipe =
2523 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2524 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2525 usbduxsub[index].urbOut[i]->transfer_buffer =
2526 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2527 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2528 dev_err(dev, "comedi_: usbdux%d: "
2529 "could not alloc. transb.\n", index);
2530 tidy_up(&(usbduxsub[index]));
2531 up(&start_stop_sem);
2532 return -ENOMEM;
2534 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2535 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2536 usbduxsub[index].urbOut[i]->transfer_buffer_length = SIZEOUTBUF;
2537 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2538 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2539 SIZEOUTBUF;
2540 if (usbduxsub[index].high_speed) {
2541 /* uframes */
2542 usbduxsub[index].urbOut[i]->interval = 8;
2543 } else {
2544 /* frames */
2545 usbduxsub[index].urbOut[i]->interval = 1;
2549 /* pwm */
2550 if (usbduxsub[index].high_speed) {
2551 /* max bulk ep size in high speed */
2552 usbduxsub[index].sizePwmBuf = 512;
2553 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2554 if (usbduxsub[index].urbPwm == NULL) {
2555 dev_err(dev, "comedi_: usbdux%d: "
2556 "Could not alloc. pwm urb\n", index);
2557 tidy_up(&(usbduxsub[index]));
2558 up(&start_stop_sem);
2559 return -ENOMEM;
2561 usbduxsub[index].urbPwm->transfer_buffer =
2562 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2563 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2564 dev_err(dev, "comedi_: usbdux%d: "
2565 "could not alloc. transb. for pwm\n", index);
2566 tidy_up(&(usbduxsub[index]));
2567 up(&start_stop_sem);
2568 return -ENOMEM;
2570 } else {
2571 usbduxsub[index].urbPwm = NULL;
2572 usbduxsub[index].sizePwmBuf = 0;
2575 usbduxsub[index].ai_cmd_running = 0;
2576 usbduxsub[index].ao_cmd_running = 0;
2577 usbduxsub[index].pwm_cmd_running = 0;
2579 /* we've reached the bottom of the function */
2580 usbduxsub[index].probed = 1;
2581 up(&start_stop_sem);
2583 ret = request_firmware_nowait(THIS_MODULE,
2584 FW_ACTION_HOTPLUG,
2585 "usbdux_firmware.bin",
2586 &udev->dev,
2587 usbduxsub + index,
2588 usbdux_firmware_request_complete_handler);
2590 if (ret) {
2591 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2592 return ret;
2595 dev_info(dev, "comedi_: usbdux%d "
2596 "has been successfully initialised.\n", index);
2597 /* success */
2598 return 0;
2601 static void usbduxsub_disconnect(struct usb_interface *intf)
2603 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2604 struct usb_device *udev = interface_to_usbdev(intf);
2606 if (!usbduxsub_tmp) {
2607 dev_err(&intf->dev,
2608 "comedi_: disconnect called with null pointer.\n");
2609 return;
2611 if (usbduxsub_tmp->usbdev != udev) {
2612 dev_err(&intf->dev, "comedi_: BUG! called with wrong ptr!!!\n");
2613 return;
2615 comedi_usb_auto_unconfig(udev);
2616 down(&start_stop_sem);
2617 down(&usbduxsub_tmp->sem);
2618 tidy_up(usbduxsub_tmp);
2619 up(&usbduxsub_tmp->sem);
2620 up(&start_stop_sem);
2621 dev_dbg(&intf->dev, "comedi_: disconnected from the usb\n");
2624 /* is called when comedi-config is called */
2625 static int usbdux_attach(struct comedi_device *dev, struct comedi_devconfig *it)
2627 int ret;
2628 int index;
2629 int i;
2630 struct usbduxsub *udev;
2632 struct comedi_subdevice *s = NULL;
2633 dev->private = NULL;
2635 down(&start_stop_sem);
2636 /* find a valid device which has been detected by the probe function of
2637 * the usb */
2638 index = -1;
2639 for (i = 0; i < NUMUSBDUX; i++) {
2640 if ((usbduxsub[i].probed) && (!usbduxsub[i].attached)) {
2641 index = i;
2642 break;
2646 if (index < 0) {
2647 printk(KERN_ERR "comedi%d: usbdux: error: attach failed, no "
2648 "usbdux devs connected to the usb bus.\n", dev->minor);
2649 up(&start_stop_sem);
2650 return -ENODEV;
2653 udev = &usbduxsub[index];
2654 down(&udev->sem);
2655 /* pointer back to the corresponding comedi device */
2656 udev->comedidev = dev;
2658 /* trying to upload the firmware into the chip */
2659 if (comedi_aux_data(it->options, 0) &&
2660 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
2661 firmwareUpload(udev, comedi_aux_data(it->options, 0),
2662 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
2665 dev->board_name = BOARDNAME;
2667 /* set number of subdevices */
2668 if (udev->high_speed) {
2669 /* with pwm */
2670 dev->n_subdevices = 5;
2671 } else {
2672 /* without pwm */
2673 dev->n_subdevices = 4;
2676 /* allocate space for the subdevices */
2677 ret = alloc_subdevices(dev, dev->n_subdevices);
2678 if (ret < 0) {
2679 dev_err(&udev->interface->dev,
2680 "comedi%d: error alloc space for subdev\n", dev->minor);
2681 up(&start_stop_sem);
2682 return ret;
2685 dev_info(&udev->interface->dev,
2686 "comedi%d: usb-device %d is attached to comedi.\n",
2687 dev->minor, index);
2688 /* private structure is also simply the usb-structure */
2689 dev->private = udev;
2691 /* the first subdevice is the A/D converter */
2692 s = dev->subdevices + SUBDEV_AD;
2693 /* the URBs get the comedi subdevice */
2694 /* which is responsible for reading */
2695 /* this is the subdevice which reads data */
2696 dev->read_subdev = s;
2697 /* the subdevice receives as private structure the */
2698 /* usb-structure */
2699 s->private = NULL;
2700 /* analog input */
2701 s->type = COMEDI_SUBD_AI;
2702 /* readable and ref is to ground */
2703 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
2704 /* 8 channels */
2705 s->n_chan = 8;
2706 /* length of the channellist */
2707 s->len_chanlist = 8;
2708 /* callback functions */
2709 s->insn_read = usbdux_ai_insn_read;
2710 s->do_cmdtest = usbdux_ai_cmdtest;
2711 s->do_cmd = usbdux_ai_cmd;
2712 s->cancel = usbdux_ai_cancel;
2713 /* max value from the A/D converter (12bit) */
2714 s->maxdata = 0xfff;
2715 /* range table to convert to physical units */
2716 s->range_table = (&range_usbdux_ai_range);
2718 /* analog out */
2719 s = dev->subdevices + SUBDEV_DA;
2720 /* analog out */
2721 s->type = COMEDI_SUBD_AO;
2722 /* backward pointer */
2723 dev->write_subdev = s;
2724 /* the subdevice receives as private structure the */
2725 /* usb-structure */
2726 s->private = NULL;
2727 /* are writable */
2728 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2729 /* 4 channels */
2730 s->n_chan = 4;
2731 /* length of the channellist */
2732 s->len_chanlist = 4;
2733 /* 12 bit resolution */
2734 s->maxdata = 0x0fff;
2735 /* bipolar range */
2736 s->range_table = (&range_usbdux_ao_range);
2737 /* callback */
2738 s->do_cmdtest = usbdux_ao_cmdtest;
2739 s->do_cmd = usbdux_ao_cmd;
2740 s->cancel = usbdux_ao_cancel;
2741 s->insn_read = usbdux_ao_insn_read;
2742 s->insn_write = usbdux_ao_insn_write;
2744 /* digital I/O */
2745 s = dev->subdevices + SUBDEV_DIO;
2746 s->type = COMEDI_SUBD_DIO;
2747 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2748 s->n_chan = 8;
2749 s->maxdata = 1;
2750 s->range_table = (&range_digital);
2751 s->insn_bits = usbdux_dio_insn_bits;
2752 s->insn_config = usbdux_dio_insn_config;
2753 /* we don't use it */
2754 s->private = NULL;
2756 /* counter */
2757 s = dev->subdevices + SUBDEV_COUNTER;
2758 s->type = COMEDI_SUBD_COUNTER;
2759 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2760 s->n_chan = 4;
2761 s->maxdata = 0xFFFF;
2762 s->insn_read = usbdux_counter_read;
2763 s->insn_write = usbdux_counter_write;
2764 s->insn_config = usbdux_counter_config;
2766 if (udev->high_speed) {
2767 /* timer / pwm */
2768 s = dev->subdevices + SUBDEV_PWM;
2769 s->type = COMEDI_SUBD_PWM;
2770 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2771 s->n_chan = 8;
2772 /* this defines the max duty cycle resolution */
2773 s->maxdata = udev->sizePwmBuf;
2774 s->insn_write = usbdux_pwm_write;
2775 s->insn_read = usbdux_pwm_read;
2776 s->insn_config = usbdux_pwm_config;
2777 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2779 /* finally decide that it's attached */
2780 udev->attached = 1;
2782 up(&udev->sem);
2784 up(&start_stop_sem);
2786 dev_info(&udev->interface->dev, "comedi%d: attached to usbdux.\n",
2787 dev->minor);
2789 return 0;
2792 static int usbdux_detach(struct comedi_device *dev)
2794 struct usbduxsub *usbduxsub_tmp;
2796 if (!dev) {
2797 printk(KERN_ERR
2798 "comedi?: usbdux: detach without dev variable...\n");
2799 return -EFAULT;
2802 usbduxsub_tmp = dev->private;
2803 if (!usbduxsub_tmp) {
2804 printk(KERN_ERR
2805 "comedi?: usbdux: detach without ptr to usbduxsub[]\n");
2806 return -EFAULT;
2809 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi%d: detach usb device\n",
2810 dev->minor);
2812 down(&usbduxsub_tmp->sem);
2813 /* Don't allow detach to free the private structure */
2814 /* It's one entry of of usbduxsub[] */
2815 dev->private = NULL;
2816 usbduxsub_tmp->attached = 0;
2817 usbduxsub_tmp->comedidev = NULL;
2818 dev_dbg(&usbduxsub_tmp->interface->dev,
2819 "comedi%d: detach: successfully removed\n", dev->minor);
2820 up(&usbduxsub_tmp->sem);
2821 return 0;
2824 /* main driver struct */
2825 static struct comedi_driver driver_usbdux = {
2826 .driver_name = "usbdux",
2827 .module = THIS_MODULE,
2828 .attach = usbdux_attach,
2829 .detach = usbdux_detach,
2832 /* Table with the USB-devices: just now only testing IDs */
2833 static struct usb_device_id usbduxsub_table[] = {
2834 {USB_DEVICE(0x13d8, 0x0001)},
2835 {USB_DEVICE(0x13d8, 0x0002)},
2836 {} /* Terminating entry */
2839 MODULE_DEVICE_TABLE(usb, usbduxsub_table);
2841 /* The usbduxsub-driver */
2842 static struct usb_driver usbduxsub_driver = {
2843 .name = BOARDNAME,
2844 .probe = usbduxsub_probe,
2845 .disconnect = usbduxsub_disconnect,
2846 .id_table = usbduxsub_table,
2849 /* Can't use the nice macro as I have also to initialise the USB */
2850 /* subsystem: */
2851 /* registering the usb-system _and_ the comedi-driver */
2852 static int __init init_usbdux(void)
2854 printk(KERN_INFO KBUILD_MODNAME ": "
2855 DRIVER_VERSION ":" DRIVER_DESC "\n");
2856 usb_register(&usbduxsub_driver);
2857 comedi_driver_register(&driver_usbdux);
2858 return 0;
2861 /* deregistering the comedi driver and the usb-subsystem */
2862 static void __exit exit_usbdux(void)
2864 comedi_driver_unregister(&driver_usbdux);
2865 usb_deregister(&usbduxsub_driver);
2868 module_init(init_usbdux);
2869 module_exit(exit_usbdux);
2871 MODULE_AUTHOR(DRIVER_AUTHOR);
2872 MODULE_DESCRIPTION(DRIVER_DESC);
2873 MODULE_LICENSE("GPL");