GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / staging / comedi / drivers / usbdux.c
blobe41f62b9473260993e181ba56baa9ef63e48c70f
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/fcntl.h>
99 #include <linux/compiler.h>
100 #include <linux/firmware.h>
102 #include "../comedidev.h"
104 #define BOARDNAME "usbdux"
106 /* timeout for the USB-transfer in ms*/
107 #define BULK_TIMEOUT 1000
109 /* constants for "firmware" upload and download */
110 #define USBDUXSUB_FIRMWARE 0xA0
111 #define VENDOR_DIR_IN 0xC0
112 #define VENDOR_DIR_OUT 0x40
114 /* internal addresses of the 8051 processor */
115 #define USBDUXSUB_CPUCS 0xE600
118 * the minor device number, major is 180 only for debugging purposes and to
119 * upload special firmware (programming the eeprom etc) which is not compatible
120 * with the comedi framwork
122 #define USBDUXSUB_MINOR 32
124 /* max lenghth of the transfer-buffer for software upload */
125 #define TB_LEN 0x2000
127 /* Input endpoint number: ISO/IRQ */
128 #define ISOINEP 6
130 /* Output endpoint number: ISO/IRQ */
131 #define ISOOUTEP 2
133 /* This EP sends DUX commands to USBDUX */
134 #define COMMAND_OUT_EP 1
136 /* This EP receives the DUX commands from USBDUX */
137 #define COMMAND_IN_EP 8
139 /* Output endpoint for PWM */
140 #define PWM_EP 4
142 /* 300Hz max frequ under PWM */
143 #define MIN_PWM_PERIOD ((long)(1E9/300))
145 /* Default PWM frequency */
146 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
148 /* Number of channels */
149 #define NUMCHANNELS 8
151 /* Size of one A/D value */
152 #define SIZEADIN ((sizeof(int16_t)))
155 * Size of the input-buffer IN BYTES
156 * Always multiple of 8 for 8 microframes which is needed in the highspeed mode
158 #define SIZEINBUF ((8*SIZEADIN))
160 /* 16 bytes. */
161 #define SIZEINSNBUF 16
163 /* Number of DA channels */
164 #define NUMOUTCHANNELS 8
166 /* size of one value for the D/A converter: channel and value */
167 #define SIZEDAOUT ((sizeof(int8_t)+sizeof(int16_t)))
170 * Size of the output-buffer in bytes
171 * Actually only the first 4 triplets are used but for the
172 * high speed mode we need to pad it to 8 (microframes).
174 #define SIZEOUTBUF ((8*SIZEDAOUT))
177 * Size of the buffer for the dux commands: just now max size is determined
178 * by the analogue out + command byte + panic bytes...
180 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
182 /* Number of in-URBs which receive the data: min=2 */
183 #define NUMOFINBUFFERSFULL 5
185 /* Number of out-URBs which send the data: min=2 */
186 #define NUMOFOUTBUFFERSFULL 5
188 /* Number of in-URBs which receive the data: min=5 */
189 /* must have more buffers due to buggy USB ctr */
190 #define NUMOFINBUFFERSHIGH 10
192 /* Number of out-URBs which send the data: min=5 */
193 /* must have more buffers due to buggy USB ctr */
194 #define NUMOFOUTBUFFERSHIGH 10
196 /* Total number of usbdux devices */
197 #define NUMUSBDUX 16
199 /* Analogue in subdevice */
200 #define SUBDEV_AD 0
202 /* Analogue out subdevice */
203 #define SUBDEV_DA 1
205 /* Digital I/O */
206 #define SUBDEV_DIO 2
208 /* counter */
209 #define SUBDEV_COUNTER 3
211 /* timer aka pwm output */
212 #define SUBDEV_PWM 4
214 /* number of retries to get the right dux command */
215 #define RETRIES 10
217 /**************************************************/
218 /* comedi constants */
219 static const struct comedi_lrange range_usbdux_ai_range = { 4, {
220 BIP_RANGE
221 (4.096),
222 BIP_RANGE(4.096
223 / 2),
224 UNI_RANGE
225 (4.096),
226 UNI_RANGE(4.096
227 / 2)
231 static const struct comedi_lrange range_usbdux_ao_range = { 2, {
232 BIP_RANGE
233 (4.096),
234 UNI_RANGE
235 (4.096),
240 * private structure of one subdevice
244 * This is the structure which holds all the data of
245 * this driver one sub device just now: A/D
247 struct usbduxsub {
248 /* attached? */
249 int attached;
250 /* is it associated with a subdevice? */
251 int probed;
252 /* pointer to the usb-device */
253 struct usb_device *usbdev;
254 /* actual number of in-buffers */
255 int numOfInBuffers;
256 /* actual number of out-buffers */
257 int numOfOutBuffers;
258 /* ISO-transfer handling: buffers */
259 struct urb **urbIn;
260 struct urb **urbOut;
261 /* pwm-transfer handling */
262 struct urb *urbPwm;
263 /* PWM period */
264 unsigned int pwmPeriod;
265 /* PWM internal delay for the GPIF in the FX2 */
266 int8_t pwmDelay;
267 /* size of the PWM buffer which holds the bit pattern */
268 int sizePwmBuf;
269 /* input buffer for the ISO-transfer */
270 int16_t *inBuffer;
271 /* input buffer for single insn */
272 int16_t *insnBuffer;
273 /* output buffer for single DA outputs */
274 int16_t *outBuffer;
275 /* interface number */
276 int ifnum;
277 /* interface structure in 2.6 */
278 struct usb_interface *interface;
279 /* comedi device for the interrupt context */
280 struct comedi_device *comedidev;
281 /* is it USB_SPEED_HIGH or not? */
282 short int high_speed;
283 /* asynchronous command is running */
284 short int ai_cmd_running;
285 short int ao_cmd_running;
286 /* pwm is running */
287 short int pwm_cmd_running;
288 /* continous aquisition */
289 short int ai_continous;
290 short int ao_continous;
291 /* number of samples to acquire */
292 int ai_sample_count;
293 int ao_sample_count;
294 /* time between samples in units of the timer */
295 unsigned int ai_timer;
296 unsigned int ao_timer;
297 /* counter between aquisitions */
298 unsigned int ai_counter;
299 unsigned int ao_counter;
300 /* interval in frames/uframes */
301 unsigned int ai_interval;
302 /* D/A commands */
303 int8_t *dac_commands;
304 /* commands */
305 int8_t *dux_commands;
306 struct semaphore sem;
310 * The pointer to the private usb-data of the driver is also the private data
311 * for the comedi-device. This has to be global as the usb subsystem needs
312 * global variables. The other reason is that this structure must be there
313 * _before_ any comedi command is issued. The usb subsystem must be initialised
314 * before comedi can access it.
316 static struct usbduxsub usbduxsub[NUMUSBDUX];
318 static DECLARE_MUTEX(start_stop_sem);
321 * Stops the data acquision
322 * It should be safe to call this function from any context
324 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
326 int i = 0;
327 int err = 0;
329 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
330 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
331 if (usbduxsub_tmp->urbIn[i]) {
332 /* We wait here until all transfers have been
333 * cancelled. */
334 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
336 dev_dbg(&usbduxsub_tmp->interface->dev,
337 "comedi: usbdux: unlinked InURB %d, err=%d\n",
338 i, err);
341 return err;
345 * This will stop a running acquisition operation
346 * Is called from within this driver from both the
347 * interrupt context and from comedi
349 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
351 int ret = 0;
353 if (!this_usbduxsub) {
354 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
355 return -EFAULT;
357 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
359 if (do_unlink) {
360 /* stop aquistion */
361 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
364 this_usbduxsub->ai_cmd_running = 0;
366 return ret;
370 * This will cancel a running acquisition operation.
371 * This is called by comedi but never from inside the driver.
373 static int usbdux_ai_cancel(struct comedi_device *dev,
374 struct comedi_subdevice *s)
376 struct usbduxsub *this_usbduxsub;
377 int res = 0;
379 /* force unlink of all urbs */
380 this_usbduxsub = dev->private;
381 if (!this_usbduxsub)
382 return -EFAULT;
384 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
386 /* prevent other CPUs from submitting new commands just now */
387 down(&this_usbduxsub->sem);
388 if (!(this_usbduxsub->probed)) {
389 up(&this_usbduxsub->sem);
390 return -ENODEV;
392 /* unlink only if the urb really has been submitted */
393 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
394 up(&this_usbduxsub->sem);
395 return res;
398 /* analogue IN - interrupt service routine */
399 static void usbduxsub_ai_IsocIrq(struct urb *urb)
401 int i, err, n;
402 struct usbduxsub *this_usbduxsub;
403 struct comedi_device *this_comedidev;
404 struct comedi_subdevice *s;
406 /* the context variable points to the subdevice */
407 this_comedidev = urb->context;
408 /* the private structure of the subdevice is struct usbduxsub */
409 this_usbduxsub = this_comedidev->private;
410 /* subdevice which is the AD converter */
411 s = this_comedidev->subdevices + SUBDEV_AD;
413 /* first we test if something unusual has just happened */
414 switch (urb->status) {
415 case 0:
416 /* copy the result in the transfer buffer */
417 memcpy(this_usbduxsub->inBuffer,
418 urb->transfer_buffer, SIZEINBUF);
419 break;
420 case -EILSEQ:
421 /* error in the ISOchronous data */
422 /* we don't copy the data into the transfer buffer */
423 /* and recycle the last data byte */
424 dev_dbg(&urb->dev->dev,
425 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
426 this_usbduxsub->comedidev->minor);
428 break;
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);
444 return;
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);
459 return;
463 * at this point we are reasonably sure that nothing dodgy has happened
464 * are we running a command?
466 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
468 * not running a command, do not continue execution if no
469 * asynchronous command is running in particular not resubmit
471 return;
474 urb->dev = this_usbduxsub->usbdev;
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! err=%d\n",
481 err);
482 if (err == -EL2NSYNC)
483 dev_err(&urb->dev->dev,
484 "buggy USB host controller or bug in IRQ "
485 "handler!\n");
486 s->async->events |= COMEDI_CB_EOA;
487 s->async->events |= COMEDI_CB_ERROR;
488 comedi_event(this_usbduxsub->comedidev, s);
489 /* don't do an unlink here */
490 usbdux_ai_stop(this_usbduxsub, 0);
491 return;
494 this_usbduxsub->ai_counter--;
495 if (likely(this_usbduxsub->ai_counter > 0))
496 return;
498 /* timer zero, transfer measurements to comedi */
499 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
501 /* test, if we transmit only a fixed number of samples */
502 if (!(this_usbduxsub->ai_continous)) {
503 /* not continous, fixed number of samples */
504 this_usbduxsub->ai_sample_count--;
505 /* all samples received? */
506 if (this_usbduxsub->ai_sample_count < 0) {
507 /* prevent a resubmit next time */
508 usbdux_ai_stop(this_usbduxsub, 0);
509 /* say comedi that the acquistion is over */
510 s->async->events |= COMEDI_CB_EOA;
511 comedi_event(this_usbduxsub->comedidev, s);
512 return;
515 /* get the data from the USB bus and hand it over to comedi */
516 n = s->async->cmd.chanlist_len;
517 for (i = 0; i < n; i++) {
518 /* transfer data */
519 if (CR_RANGE(s->async->cmd.chanlist[i]) <= 1) {
520 err = comedi_buf_put
521 (s->async,
522 le16_to_cpu(this_usbduxsub->inBuffer[i]) ^ 0x800);
523 } else {
524 err = comedi_buf_put
525 (s->async,
526 le16_to_cpu(this_usbduxsub->inBuffer[i]));
528 if (unlikely(err == 0)) {
529 /* buffer overflow */
530 usbdux_ai_stop(this_usbduxsub, 0);
531 return;
534 /* tell comedi that data is there */
535 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
536 comedi_event(this_usbduxsub->comedidev, s);
539 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
541 int i = 0;
542 int err = 0;
544 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
545 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
546 if (usbduxsub_tmp->urbOut[i])
547 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
549 dev_dbg(&usbduxsub_tmp->interface->dev,
550 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
551 i, err);
554 return err;
557 /* This will cancel a running acquisition operation
558 * in any context.
560 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
562 int ret = 0;
564 if (!this_usbduxsub)
565 return -EFAULT;
566 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
568 if (do_unlink)
569 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
571 this_usbduxsub->ao_cmd_running = 0;
573 return ret;
576 /* force unlink, is called by comedi */
577 static int usbdux_ao_cancel(struct comedi_device *dev,
578 struct comedi_subdevice *s)
580 struct usbduxsub *this_usbduxsub = dev->private;
581 int res = 0;
583 if (!this_usbduxsub)
584 return -EFAULT;
586 /* prevent other CPUs from submitting a command just now */
587 down(&this_usbduxsub->sem);
588 if (!(this_usbduxsub->probed)) {
589 up(&this_usbduxsub->sem);
590 return -ENODEV;
592 /* unlink only if it is really running */
593 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
594 up(&this_usbduxsub->sem);
595 return res;
598 static void usbduxsub_ao_IsocIrq(struct urb *urb)
600 int i, ret;
601 int8_t *datap;
602 struct usbduxsub *this_usbduxsub;
603 struct comedi_device *this_comedidev;
604 struct comedi_subdevice *s;
606 /* the context variable points to the subdevice */
607 this_comedidev = urb->context;
608 /* the private structure of the subdevice is struct usbduxsub */
609 this_usbduxsub = this_comedidev->private;
611 s = this_comedidev->subdevices + SUBDEV_DA;
613 switch (urb->status) {
614 case 0:
615 /* success */
616 break;
618 case -ECONNRESET:
619 case -ENOENT:
620 case -ESHUTDOWN:
621 case -ECONNABORTED:
622 /* after an unlink command, unplug, ... etc */
623 /* no unlink needed here. Already shutting down. */
624 if (this_usbduxsub->ao_cmd_running) {
625 s->async->events |= COMEDI_CB_EOA;
626 comedi_event(this_usbduxsub->comedidev, s);
627 usbdux_ao_stop(this_usbduxsub, 0);
629 return;
631 default:
632 /* a real error */
633 if (this_usbduxsub->ao_cmd_running) {
634 dev_err(&urb->dev->dev,
635 "comedi_: Non-zero urb status received in ao "
636 "intr context: %d\n", urb->status);
637 s->async->events |= COMEDI_CB_ERROR;
638 s->async->events |= COMEDI_CB_EOA;
639 comedi_event(this_usbduxsub->comedidev, s);
640 /* we do an unlink if we are in the high speed mode */
641 usbdux_ao_stop(this_usbduxsub, 0);
643 return;
646 /* are we actually running? */
647 if (!(this_usbduxsub->ao_cmd_running))
648 return;
650 /* normal operation: executing a command in this subdevice */
651 this_usbduxsub->ao_counter--;
652 if (this_usbduxsub->ao_counter <= 0) {
653 /* timer zero */
654 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
656 /* handle non continous aquisition */
657 if (!(this_usbduxsub->ao_continous)) {
658 /* fixed number of samples */
659 this_usbduxsub->ao_sample_count--;
660 if (this_usbduxsub->ao_sample_count < 0) {
661 /* all samples transmitted */
662 usbdux_ao_stop(this_usbduxsub, 0);
663 s->async->events |= COMEDI_CB_EOA;
664 comedi_event(this_usbduxsub->comedidev, s);
665 /* no resubmit of the urb */
666 return;
669 /* transmit data to the USB bus */
670 ((uint8_t *) (urb->transfer_buffer))[0] =
671 s->async->cmd.chanlist_len;
672 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
673 short temp;
674 if (i >= NUMOUTCHANNELS)
675 break;
677 /* pointer to the DA */
678 datap =
679 (&(((int8_t *) urb->transfer_buffer)[i * 3 + 1]));
680 /* get the data from comedi */
681 ret = comedi_buf_get(s->async, &temp);
682 datap[0] = temp;
683 datap[1] = temp >> 8;
684 datap[2] = this_usbduxsub->dac_commands[i];
685 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
686 /* datap[0],datap[1],datap[2]); */
687 if (ret < 0) {
688 dev_err(&urb->dev->dev,
689 "comedi: buffer underflow\n");
690 s->async->events |= COMEDI_CB_EOA;
691 s->async->events |= COMEDI_CB_OVERFLOW;
693 /* transmit data to comedi */
694 s->async->events |= COMEDI_CB_BLOCK;
695 comedi_event(this_usbduxsub->comedidev, s);
698 urb->transfer_buffer_length = SIZEOUTBUF;
699 urb->dev = this_usbduxsub->usbdev;
700 urb->status = 0;
701 if (this_usbduxsub->ao_cmd_running) {
702 if (this_usbduxsub->high_speed) {
703 /* uframes */
704 urb->interval = 8;
705 } else {
706 /* frames */
707 urb->interval = 1;
709 urb->number_of_packets = 1;
710 urb->iso_frame_desc[0].offset = 0;
711 urb->iso_frame_desc[0].length = SIZEOUTBUF;
712 urb->iso_frame_desc[0].status = 0;
713 ret = usb_submit_urb(urb, GFP_ATOMIC);
714 if (ret < 0) {
715 dev_err(&urb->dev->dev,
716 "comedi_: ao urb resubm failed in int-cont. "
717 "ret=%d", ret);
718 if (ret == EL2NSYNC)
719 dev_err(&urb->dev->dev,
720 "buggy USB host controller or bug in "
721 "IRQ handling!\n");
723 s->async->events |= COMEDI_CB_EOA;
724 s->async->events |= COMEDI_CB_ERROR;
725 comedi_event(this_usbduxsub->comedidev, s);
726 /* don't do an unlink here */
727 usbdux_ao_stop(this_usbduxsub, 0);
732 static int usbduxsub_start(struct usbduxsub *usbduxsub)
734 int errcode = 0;
735 uint8_t local_transfer_buffer[16];
737 /* 7f92 to zero */
738 local_transfer_buffer[0] = 0;
739 errcode = usb_control_msg(usbduxsub->usbdev,
740 /* create a pipe for a control transfer */
741 usb_sndctrlpipe(usbduxsub->usbdev, 0),
742 /* bRequest, "Firmware" */
743 USBDUXSUB_FIRMWARE,
744 /* bmRequestType */
745 VENDOR_DIR_OUT,
746 /* Value */
747 USBDUXSUB_CPUCS,
748 /* Index */
749 0x0000,
750 /* address of the transfer buffer */
751 local_transfer_buffer,
752 /* Length */
754 /* Timeout */
755 BULK_TIMEOUT);
756 if (errcode < 0) {
757 dev_err(&usbduxsub->interface->dev,
758 "comedi_: control msg failed (start)\n");
759 return errcode;
761 return 0;
764 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
766 int errcode = 0;
768 uint8_t local_transfer_buffer[16];
770 /* 7f92 to one */
771 local_transfer_buffer[0] = 1;
772 errcode = usb_control_msg(usbduxsub->usbdev,
773 usb_sndctrlpipe(usbduxsub->usbdev, 0),
774 /* bRequest, "Firmware" */
775 USBDUXSUB_FIRMWARE,
776 /* bmRequestType */
777 VENDOR_DIR_OUT,
778 /* Value */
779 USBDUXSUB_CPUCS,
780 /* Index */
781 0x0000, local_transfer_buffer,
782 /* Length */
784 /* Timeout */
785 BULK_TIMEOUT);
786 if (errcode < 0) {
787 dev_err(&usbduxsub->interface->dev,
788 "comedi_: control msg failed (stop)\n");
789 return errcode;
791 return 0;
794 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
795 uint8_t *local_transfer_buffer,
796 unsigned int startAddr, unsigned int len)
798 int errcode;
800 errcode = usb_control_msg(usbduxsub->usbdev,
801 usb_sndctrlpipe(usbduxsub->usbdev, 0),
802 /* brequest, firmware */
803 USBDUXSUB_FIRMWARE,
804 /* bmRequestType */
805 VENDOR_DIR_OUT,
806 /* value */
807 startAddr,
808 /* index */
809 0x0000,
810 /* our local safe buffer */
811 local_transfer_buffer,
812 /* length */
813 len,
814 /* timeout */
815 BULK_TIMEOUT);
816 dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
817 if (errcode < 0) {
818 dev_err(&usbduxsub->interface->dev, "comedi_: upload failed\n");
819 return errcode;
821 return 0;
824 #define FIRMWARE_MAX_LEN 0x2000
826 static int firmwareUpload(struct usbduxsub *usbduxsub,
827 const u8 *firmwareBinary, int sizeFirmware)
829 int ret;
830 uint8_t *fwBuf;
832 if (!firmwareBinary)
833 return 0;
835 if (sizeFirmware > FIRMWARE_MAX_LEN) {
836 dev_err(&usbduxsub->interface->dev,
837 "usbdux firmware binary it too large for FX2.\n");
838 return -ENOMEM;
841 /* we generate a local buffer for the firmware */
842 fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
843 if (!fwBuf) {
844 dev_err(&usbduxsub->interface->dev,
845 "comedi_: mem alloc for firmware failed\n");
846 return -ENOMEM;
849 ret = usbduxsub_stop(usbduxsub);
850 if (ret < 0) {
851 dev_err(&usbduxsub->interface->dev,
852 "comedi_: can not stop firmware\n");
853 kfree(fwBuf);
854 return ret;
857 ret = usbduxsub_upload(usbduxsub, fwBuf, 0, sizeFirmware);
858 if (ret < 0) {
859 dev_err(&usbduxsub->interface->dev,
860 "comedi_: firmware upload failed\n");
861 kfree(fwBuf);
862 return ret;
864 ret = usbduxsub_start(usbduxsub);
865 if (ret < 0) {
866 dev_err(&usbduxsub->interface->dev,
867 "comedi_: can not start firmware\n");
868 kfree(fwBuf);
869 return ret;
871 kfree(fwBuf);
872 return 0;
875 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
877 int i, errFlag;
879 if (!usbduxsub)
880 return -EFAULT;
882 /* Submit all URBs and start the transfer on the bus */
883 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
884 /* in case of a resubmission after an unlink... */
885 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
886 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
887 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
888 usbduxsub->urbIn[i]->status = 0;
889 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
890 dev_dbg(&usbduxsub->interface->dev,
891 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
892 usbduxsub->comedidev->minor, i,
893 (usbduxsub->urbIn[i]->context),
894 (usbduxsub->urbIn[i]->dev),
895 (usbduxsub->urbIn[i]->interval));
896 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
897 if (errFlag) {
898 dev_err(&usbduxsub->interface->dev,
899 "comedi_: ai: usb_submit_urb(%d) error %d\n",
900 i, errFlag);
901 return errFlag;
904 return 0;
907 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
909 int i, errFlag;
911 if (!usbduxsub)
912 return -EFAULT;
914 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
915 dev_dbg(&usbduxsub->interface->dev,
916 "comedi_: submitting out-urb[%d]\n", i);
917 /* in case of a resubmission after an unlink... */
918 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
919 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
920 usbduxsub->urbOut[i]->status = 0;
921 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
922 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
923 if (errFlag) {
924 dev_err(&usbduxsub->interface->dev,
925 "comedi_: ao: usb_submit_urb(%d) error %d\n",
926 i, errFlag);
927 return errFlag;
930 return 0;
933 static int usbdux_ai_cmdtest(struct comedi_device *dev,
934 struct comedi_subdevice *s, struct comedi_cmd *cmd)
936 int err = 0, tmp, i;
937 unsigned int tmpTimer;
938 struct usbduxsub *this_usbduxsub = dev->private;
940 if (!(this_usbduxsub->probed))
941 return -ENODEV;
943 dev_dbg(&this_usbduxsub->interface->dev,
944 "comedi%d: usbdux_ai_cmdtest\n", dev->minor);
946 /* make sure triggers are valid */
947 /* Only immediate triggers are allowed */
948 tmp = cmd->start_src;
949 cmd->start_src &= TRIG_NOW | TRIG_INT;
950 if (!cmd->start_src || tmp != cmd->start_src)
951 err++;
953 /* trigger should happen timed */
954 tmp = cmd->scan_begin_src;
955 /* start a new _scan_ with a timer */
956 cmd->scan_begin_src &= TRIG_TIMER;
957 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
958 err++;
960 /* scanning is continous */
961 tmp = cmd->convert_src;
962 cmd->convert_src &= TRIG_NOW;
963 if (!cmd->convert_src || tmp != cmd->convert_src)
964 err++;
966 /* issue a trigger when scan is finished and start a new scan */
967 tmp = cmd->scan_end_src;
968 cmd->scan_end_src &= TRIG_COUNT;
969 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
970 err++;
972 /* trigger at the end of count events or not, stop condition or not */
973 tmp = cmd->stop_src;
974 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
975 if (!cmd->stop_src || tmp != cmd->stop_src)
976 err++;
978 if (err)
979 return 1;
982 * step 2: make sure trigger sources are unique and mutually compatible
983 * note that mutual compatibility is not an issue here
985 if (cmd->scan_begin_src != TRIG_FOLLOW &&
986 cmd->scan_begin_src != TRIG_EXT &&
987 cmd->scan_begin_src != TRIG_TIMER)
988 err++;
989 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
990 err++;
992 if (err)
993 return 2;
995 /* step 3: make sure arguments are trivially compatible */
996 if (cmd->start_arg != 0) {
997 cmd->start_arg = 0;
998 err++;
1001 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1002 /* internal trigger */
1003 if (cmd->scan_begin_arg != 0) {
1004 cmd->scan_begin_arg = 0;
1005 err++;
1009 if (cmd->scan_begin_src == TRIG_TIMER) {
1010 if (this_usbduxsub->high_speed) {
1012 * In high speed mode microframes are possible.
1013 * However, during one microframe we can roughly
1014 * sample one channel. Thus, the more channels
1015 * are in the channel list the more time we need.
1017 i = 1;
1018 /* find a power of 2 for the number of channels */
1019 while (i < (cmd->chanlist_len))
1020 i = i * 2;
1022 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
1023 cmd->scan_begin_arg = 1000000 / 8 * i;
1024 err++;
1026 /* now calc the real sampling rate with all the
1027 * rounding errors */
1028 tmpTimer =
1029 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
1030 125000;
1031 if (cmd->scan_begin_arg != tmpTimer) {
1032 cmd->scan_begin_arg = tmpTimer;
1033 err++;
1035 } else {
1036 /* full speed */
1037 /* 1kHz scans every USB frame */
1038 if (cmd->scan_begin_arg < 1000000) {
1039 cmd->scan_begin_arg = 1000000;
1040 err++;
1043 * calc the real sampling rate with the rounding errors
1045 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
1046 1000000)) * 1000000;
1047 if (cmd->scan_begin_arg != tmpTimer) {
1048 cmd->scan_begin_arg = tmpTimer;
1049 err++;
1053 /* the same argument */
1054 if (cmd->scan_end_arg != cmd->chanlist_len) {
1055 cmd->scan_end_arg = cmd->chanlist_len;
1056 err++;
1059 if (cmd->stop_src == TRIG_COUNT) {
1060 /* any count is allowed */
1061 } else {
1062 /* TRIG_NONE */
1063 if (cmd->stop_arg != 0) {
1064 cmd->stop_arg = 0;
1065 err++;
1069 if (err)
1070 return 3;
1072 return 0;
1076 * creates the ADC command for the MAX1271
1077 * range is the range value from comedi
1079 static int8_t create_adc_command(unsigned int chan, int range)
1081 int8_t p = (range <= 1);
1082 int8_t r = ((range % 2) == 0);
1083 return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
1086 /* bulk transfers to usbdux */
1088 #define SENDADCOMMANDS 0
1089 #define SENDDACOMMANDS 1
1090 #define SENDDIOCONFIGCOMMAND 2
1091 #define SENDDIOBITSCOMMAND 3
1092 #define SENDSINGLEAD 4
1093 #define READCOUNTERCOMMAND 5
1094 #define WRITECOUNTERCOMMAND 6
1095 #define SENDPWMON 7
1096 #define SENDPWMOFF 8
1098 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1100 int result, nsent;
1102 this_usbduxsub->dux_commands[0] = cmd_type;
1103 #ifdef NOISY_DUX_DEBUGBUG
1104 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1105 this_usbduxsub->comedidev->minor);
1106 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1107 printk(" %02x", this_usbduxsub->dux_commands[result]);
1108 printk("\n");
1109 #endif
1110 result = usb_bulk_msg(this_usbduxsub->usbdev,
1111 usb_sndbulkpipe(this_usbduxsub->usbdev,
1112 COMMAND_OUT_EP),
1113 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1114 &nsent, BULK_TIMEOUT);
1115 if (result < 0)
1116 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1117 "could not transmit dux_command to the usb-device, "
1118 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1120 return result;
1123 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1125 int result = (-EFAULT);
1126 int nrec;
1127 int i;
1129 for (i = 0; i < RETRIES; i++) {
1130 result = usb_bulk_msg(this_usbduxsub->usbdev,
1131 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1132 COMMAND_IN_EP),
1133 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1134 &nrec, BULK_TIMEOUT);
1135 if (result < 0) {
1136 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1137 "insn: USB error %d while receiving DUX command"
1138 "\n", this_usbduxsub->comedidev->minor, result);
1139 return result;
1141 if (le16_to_cpu(this_usbduxsub->insnBuffer[0]) == command)
1142 return result;
1144 /* this is only reached if the data has been requested a couple of
1145 * times */
1146 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1147 "wrong data returned from firmware: want cmd %d, got cmd %d.\n",
1148 this_usbduxsub->comedidev->minor, command,
1149 le16_to_cpu(this_usbduxsub->insnBuffer[0]));
1150 return -EFAULT;
1153 static int usbdux_ai_inttrig(struct comedi_device *dev,
1154 struct comedi_subdevice *s, unsigned int trignum)
1156 int ret;
1157 struct usbduxsub *this_usbduxsub = dev->private;
1158 if (!this_usbduxsub)
1159 return -EFAULT;
1161 down(&this_usbduxsub->sem);
1162 if (!(this_usbduxsub->probed)) {
1163 up(&this_usbduxsub->sem);
1164 return -ENODEV;
1166 dev_dbg(&this_usbduxsub->interface->dev,
1167 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1169 if (trignum != 0) {
1170 dev_err(&this_usbduxsub->interface->dev,
1171 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1172 dev->minor);
1173 up(&this_usbduxsub->sem);
1174 return -EINVAL;
1176 if (!(this_usbduxsub->ai_cmd_running)) {
1177 this_usbduxsub->ai_cmd_running = 1;
1178 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1179 if (ret < 0) {
1180 dev_err(&this_usbduxsub->interface->dev,
1181 "comedi%d: usbdux_ai_inttrig: "
1182 "urbSubmit: err=%d\n", dev->minor, ret);
1183 this_usbduxsub->ai_cmd_running = 0;
1184 up(&this_usbduxsub->sem);
1185 return ret;
1187 s->async->inttrig = NULL;
1188 } else {
1189 dev_err(&this_usbduxsub->interface->dev,
1190 "comedi%d: ai_inttrig but acqu is already running\n",
1191 dev->minor);
1193 up(&this_usbduxsub->sem);
1194 return 1;
1197 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1199 struct comedi_cmd *cmd = &s->async->cmd;
1200 unsigned int chan, range;
1201 int i, ret;
1202 struct usbduxsub *this_usbduxsub = dev->private;
1203 int result;
1205 if (!this_usbduxsub)
1206 return -EFAULT;
1208 dev_dbg(&this_usbduxsub->interface->dev,
1209 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1211 /* block other CPUs from starting an ai_cmd */
1212 down(&this_usbduxsub->sem);
1214 if (!(this_usbduxsub->probed)) {
1215 up(&this_usbduxsub->sem);
1216 return -ENODEV;
1218 if (this_usbduxsub->ai_cmd_running) {
1219 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1220 "ai_cmd not possible. Another ai_cmd is running.\n",
1221 dev->minor);
1222 up(&this_usbduxsub->sem);
1223 return -EBUSY;
1225 /* set current channel of the running aquisition to zero */
1226 s->async->cur_chan = 0;
1228 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1229 for (i = 0; i < cmd->chanlist_len; ++i) {
1230 chan = CR_CHAN(cmd->chanlist[i]);
1231 range = CR_RANGE(cmd->chanlist[i]);
1232 if (i >= NUMCHANNELS) {
1233 dev_err(&this_usbduxsub->interface->dev,
1234 "comedi%d: channel list too long\n",
1235 dev->minor);
1236 break;
1238 this_usbduxsub->dux_commands[i + 2] =
1239 create_adc_command(chan, range);
1242 dev_dbg(&this_usbduxsub->interface->dev,
1243 "comedi %d: sending commands to the usb device: size=%u\n",
1244 dev->minor, NUMCHANNELS);
1246 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1247 if (result < 0) {
1248 up(&this_usbduxsub->sem);
1249 return result;
1252 if (this_usbduxsub->high_speed) {
1254 * every channel gets a time window of 125us. Thus, if we
1255 * sample all 8 channels we need 1ms. If we sample only one
1256 * channel we need only 125us
1258 this_usbduxsub->ai_interval = 1;
1259 /* find a power of 2 for the interval */
1260 while ((this_usbduxsub->ai_interval) < (cmd->chanlist_len)) {
1261 this_usbduxsub->ai_interval =
1262 (this_usbduxsub->ai_interval) * 2;
1264 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1265 (this_usbduxsub->
1266 ai_interval));
1267 } else {
1268 /* interval always 1ms */
1269 this_usbduxsub->ai_interval = 1;
1270 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1272 if (this_usbduxsub->ai_timer < 1) {
1273 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1274 "timer=%d, scan_begin_arg=%d. "
1275 "Not properly tested by cmdtest?\n", dev->minor,
1276 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1277 up(&this_usbduxsub->sem);
1278 return -EINVAL;
1280 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1282 if (cmd->stop_src == TRIG_COUNT) {
1283 /* data arrives as one packet */
1284 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1285 this_usbduxsub->ai_continous = 0;
1286 } else {
1287 /* continous aquisition */
1288 this_usbduxsub->ai_continous = 1;
1289 this_usbduxsub->ai_sample_count = 0;
1292 if (cmd->start_src == TRIG_NOW) {
1293 /* enable this acquisition operation */
1294 this_usbduxsub->ai_cmd_running = 1;
1295 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1296 if (ret < 0) {
1297 this_usbduxsub->ai_cmd_running = 0;
1298 up(&this_usbduxsub->sem);
1299 return ret;
1301 s->async->inttrig = NULL;
1302 } else {
1303 /* TRIG_INT */
1304 /* don't enable the acquision operation */
1305 /* wait for an internal signal */
1306 s->async->inttrig = usbdux_ai_inttrig;
1308 up(&this_usbduxsub->sem);
1309 return 0;
1312 /* Mode 0 is used to get a single conversion on demand */
1313 static int usbdux_ai_insn_read(struct comedi_device *dev,
1314 struct comedi_subdevice *s,
1315 struct comedi_insn *insn, unsigned int *data)
1317 int i;
1318 unsigned int one = 0;
1319 int chan, range;
1320 int err;
1321 struct usbduxsub *this_usbduxsub = dev->private;
1323 if (!this_usbduxsub)
1324 return 0;
1326 dev_dbg(&this_usbduxsub->interface->dev,
1327 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1328 dev->minor, insn->n, insn->subdev);
1330 down(&this_usbduxsub->sem);
1331 if (!(this_usbduxsub->probed)) {
1332 up(&this_usbduxsub->sem);
1333 return -ENODEV;
1335 if (this_usbduxsub->ai_cmd_running) {
1336 dev_err(&this_usbduxsub->interface->dev,
1337 "comedi%d: ai_insn_read not possible. "
1338 "Async Command is running.\n", dev->minor);
1339 up(&this_usbduxsub->sem);
1340 return 0;
1343 /* sample one channel */
1344 chan = CR_CHAN(insn->chanspec);
1345 range = CR_RANGE(insn->chanspec);
1346 /* set command for the first channel */
1347 this_usbduxsub->dux_commands[1] = create_adc_command(chan, range);
1349 /* adc commands */
1350 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1351 if (err < 0) {
1352 up(&this_usbduxsub->sem);
1353 return err;
1356 for (i = 0; i < insn->n; i++) {
1357 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1358 if (err < 0) {
1359 up(&this_usbduxsub->sem);
1360 return 0;
1362 one = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1363 if (CR_RANGE(insn->chanspec) <= 1)
1364 one = one ^ 0x800;
1366 data[i] = one;
1368 up(&this_usbduxsub->sem);
1369 return i;
1372 /************************************/
1373 /* analog out */
1375 static int usbdux_ao_insn_read(struct comedi_device *dev,
1376 struct comedi_subdevice *s,
1377 struct comedi_insn *insn, unsigned int *data)
1379 int i;
1380 int chan = CR_CHAN(insn->chanspec);
1381 struct usbduxsub *this_usbduxsub = dev->private;
1383 if (!this_usbduxsub)
1384 return -EFAULT;
1386 down(&this_usbduxsub->sem);
1387 if (!(this_usbduxsub->probed)) {
1388 up(&this_usbduxsub->sem);
1389 return -ENODEV;
1391 for (i = 0; i < insn->n; i++)
1392 data[i] = this_usbduxsub->outBuffer[chan];
1394 up(&this_usbduxsub->sem);
1395 return i;
1398 static int usbdux_ao_insn_write(struct comedi_device *dev,
1399 struct comedi_subdevice *s,
1400 struct comedi_insn *insn, unsigned int *data)
1402 int i, err;
1403 int chan = CR_CHAN(insn->chanspec);
1404 struct usbduxsub *this_usbduxsub = dev->private;
1406 if (!this_usbduxsub)
1407 return -EFAULT;
1409 dev_dbg(&this_usbduxsub->interface->dev,
1410 "comedi%d: ao_insn_write\n", dev->minor);
1412 down(&this_usbduxsub->sem);
1413 if (!(this_usbduxsub->probed)) {
1414 up(&this_usbduxsub->sem);
1415 return -ENODEV;
1417 if (this_usbduxsub->ao_cmd_running) {
1418 dev_err(&this_usbduxsub->interface->dev,
1419 "comedi%d: ao_insn_write: "
1420 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1421 up(&this_usbduxsub->sem);
1422 return 0;
1425 for (i = 0; i < insn->n; i++) {
1426 dev_dbg(&this_usbduxsub->interface->dev,
1427 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1428 dev->minor, chan, i, data[i]);
1430 /* number of channels: 1 */
1431 this_usbduxsub->dux_commands[1] = 1;
1432 /* one 16 bit value */
1433 *((int16_t *) (this_usbduxsub->dux_commands + 2)) =
1434 cpu_to_le16(data[i]);
1435 this_usbduxsub->outBuffer[chan] = data[i];
1436 /* channel number */
1437 this_usbduxsub->dux_commands[4] = (chan << 6);
1438 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1439 if (err < 0) {
1440 up(&this_usbduxsub->sem);
1441 return err;
1444 up(&this_usbduxsub->sem);
1446 return i;
1449 static int usbdux_ao_inttrig(struct comedi_device *dev,
1450 struct comedi_subdevice *s, unsigned int trignum)
1452 int ret;
1453 struct usbduxsub *this_usbduxsub = dev->private;
1455 if (!this_usbduxsub)
1456 return -EFAULT;
1458 down(&this_usbduxsub->sem);
1459 if (!(this_usbduxsub->probed)) {
1460 up(&this_usbduxsub->sem);
1461 return -ENODEV;
1463 if (trignum != 0) {
1464 dev_err(&this_usbduxsub->interface->dev,
1465 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1466 dev->minor);
1467 return -EINVAL;
1469 if (!(this_usbduxsub->ao_cmd_running)) {
1470 this_usbduxsub->ao_cmd_running = 1;
1471 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1472 if (ret < 0) {
1473 dev_err(&this_usbduxsub->interface->dev,
1474 "comedi%d: usbdux_ao_inttrig: submitURB: "
1475 "err=%d\n", dev->minor, ret);
1476 this_usbduxsub->ao_cmd_running = 0;
1477 up(&this_usbduxsub->sem);
1478 return ret;
1480 s->async->inttrig = NULL;
1481 } else {
1482 dev_err(&this_usbduxsub->interface->dev,
1483 "comedi%d: ao_inttrig but acqu is already running.\n",
1484 dev->minor);
1486 up(&this_usbduxsub->sem);
1487 return 1;
1490 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1491 struct comedi_subdevice *s, struct comedi_cmd *cmd)
1493 int err = 0, tmp;
1494 struct usbduxsub *this_usbduxsub = dev->private;
1496 if (!this_usbduxsub)
1497 return -EFAULT;
1499 if (!(this_usbduxsub->probed))
1500 return -ENODEV;
1502 dev_dbg(&this_usbduxsub->interface->dev,
1503 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1505 /* make sure triggers are valid */
1506 /* Only immediate triggers are allowed */
1507 tmp = cmd->start_src;
1508 cmd->start_src &= TRIG_NOW | TRIG_INT;
1509 if (!cmd->start_src || tmp != cmd->start_src)
1510 err++;
1512 /* trigger should happen timed */
1513 tmp = cmd->scan_begin_src;
1514 /* just now we scan also in the high speed mode every frame */
1515 /* this is due to ehci driver limitations */
1516 if (0) { /* (this_usbduxsub->high_speed) */
1517 /* start immidiately a new scan */
1518 /* the sampling rate is set by the coversion rate */
1519 cmd->scan_begin_src &= TRIG_FOLLOW;
1520 } else {
1521 /* start a new scan (output at once) with a timer */
1522 cmd->scan_begin_src &= TRIG_TIMER;
1524 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1525 err++;
1527 /* scanning is continous */
1528 tmp = cmd->convert_src;
1529 /* we always output at 1kHz just now all channels at once */
1530 if (0) { /* (this_usbduxsub->high_speed) */
1532 * in usb-2.0 only one conversion it tranmitted but with 8kHz/n
1534 cmd->convert_src &= TRIG_TIMER;
1535 } else {
1536 /* all conversion events happen simultaneously with a rate of
1537 * 1kHz/n */
1538 cmd->convert_src &= TRIG_NOW;
1540 if (!cmd->convert_src || tmp != cmd->convert_src)
1541 err++;
1543 /* issue a trigger when scan is finished and start a new scan */
1544 tmp = cmd->scan_end_src;
1545 cmd->scan_end_src &= TRIG_COUNT;
1546 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1547 err++;
1549 /* trigger at the end of count events or not, stop condition or not */
1550 tmp = cmd->stop_src;
1551 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1552 if (!cmd->stop_src || tmp != cmd->stop_src)
1553 err++;
1555 if (err)
1556 return 1;
1559 * step 2: make sure trigger sources are unique and mutually compatible
1560 * note that mutual compatibility is not an issue here
1562 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1563 cmd->scan_begin_src != TRIG_EXT &&
1564 cmd->scan_begin_src != TRIG_TIMER)
1565 err++;
1566 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1567 err++;
1569 if (err)
1570 return 2;
1572 /* step 3: make sure arguments are trivially compatible */
1574 if (cmd->start_arg != 0) {
1575 cmd->start_arg = 0;
1576 err++;
1579 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1580 /* internal trigger */
1581 if (cmd->scan_begin_arg != 0) {
1582 cmd->scan_begin_arg = 0;
1583 err++;
1587 if (cmd->scan_begin_src == TRIG_TIMER) {
1588 /* timer */
1589 if (cmd->scan_begin_arg < 1000000) {
1590 cmd->scan_begin_arg = 1000000;
1591 err++;
1594 /* not used now, is for later use */
1595 if (cmd->convert_src == TRIG_TIMER) {
1596 if (cmd->convert_arg < 125000) {
1597 cmd->convert_arg = 125000;
1598 err++;
1602 /* the same argument */
1603 if (cmd->scan_end_arg != cmd->chanlist_len) {
1604 cmd->scan_end_arg = cmd->chanlist_len;
1605 err++;
1608 if (cmd->stop_src == TRIG_COUNT) {
1609 /* any count is allowed */
1610 } else {
1611 /* TRIG_NONE */
1612 if (cmd->stop_arg != 0) {
1613 cmd->stop_arg = 0;
1614 err++;
1618 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: err=%d, "
1619 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1620 "convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src,
1621 cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1623 if (err)
1624 return 3;
1626 return 0;
1629 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1631 struct comedi_cmd *cmd = &s->async->cmd;
1632 unsigned int chan, gain;
1633 int i, ret;
1634 struct usbduxsub *this_usbduxsub = dev->private;
1636 if (!this_usbduxsub)
1637 return -EFAULT;
1639 down(&this_usbduxsub->sem);
1640 if (!(this_usbduxsub->probed)) {
1641 up(&this_usbduxsub->sem);
1642 return -ENODEV;
1644 dev_dbg(&this_usbduxsub->interface->dev,
1645 "comedi%d: %s\n", dev->minor, __func__);
1647 /* set current channel of the running aquisition to zero */
1648 s->async->cur_chan = 0;
1649 for (i = 0; i < cmd->chanlist_len; ++i) {
1650 chan = CR_CHAN(cmd->chanlist[i]);
1651 gain = CR_RANGE(cmd->chanlist[i]);
1652 if (i >= NUMOUTCHANNELS) {
1653 dev_err(&this_usbduxsub->interface->dev,
1654 "comedi%d: %s: channel list too long\n",
1655 dev->minor, __func__);
1656 break;
1658 this_usbduxsub->dac_commands[i] = (chan << 6);
1659 dev_dbg(&this_usbduxsub->interface->dev,
1660 "comedi%d: dac command for ch %d is %x\n",
1661 dev->minor, i, this_usbduxsub->dac_commands[i]);
1664 /* we count in steps of 1ms (125us) */
1665 /* 125us mode not used yet */
1666 if (0) { /* (this_usbduxsub->high_speed) */
1667 /* 125us */
1668 /* timing of the conversion itself: every 125 us */
1669 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1670 } else {
1671 /* 1ms */
1672 /* timing of the scan: we get all channels at once */
1673 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1674 dev_dbg(&this_usbduxsub->interface->dev,
1675 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1676 "convert_src=%d, convert_arg=%d\n", dev->minor,
1677 cmd->scan_begin_src, cmd->scan_begin_arg,
1678 cmd->convert_src, cmd->convert_arg);
1679 dev_dbg(&this_usbduxsub->interface->dev,
1680 "comedi%d: ao_timer=%d (ms)\n",
1681 dev->minor, this_usbduxsub->ao_timer);
1682 if (this_usbduxsub->ao_timer < 1) {
1683 dev_err(&this_usbduxsub->interface->dev,
1684 "comedi%d: usbdux: ao_timer=%d, "
1685 "scan_begin_arg=%d. "
1686 "Not properly tested by cmdtest?\n",
1687 dev->minor, this_usbduxsub->ao_timer,
1688 cmd->scan_begin_arg);
1689 up(&this_usbduxsub->sem);
1690 return -EINVAL;
1693 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1695 if (cmd->stop_src == TRIG_COUNT) {
1696 /* not continous */
1697 /* counter */
1698 /* high speed also scans everything at once */
1699 if (0) { /* (this_usbduxsub->high_speed) */
1700 this_usbduxsub->ao_sample_count =
1701 (cmd->stop_arg) * (cmd->scan_end_arg);
1702 } else {
1703 /* there's no scan as the scan has been */
1704 /* perf inside the FX2 */
1705 /* data arrives as one packet */
1706 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1708 this_usbduxsub->ao_continous = 0;
1709 } else {
1710 /* continous aquisition */
1711 this_usbduxsub->ao_continous = 1;
1712 this_usbduxsub->ao_sample_count = 0;
1715 if (cmd->start_src == TRIG_NOW) {
1716 /* enable this acquisition operation */
1717 this_usbduxsub->ao_cmd_running = 1;
1718 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1719 if (ret < 0) {
1720 this_usbduxsub->ao_cmd_running = 0;
1721 up(&this_usbduxsub->sem);
1722 return ret;
1724 s->async->inttrig = NULL;
1725 } else {
1726 /* TRIG_INT */
1727 /* submit the urbs later */
1728 /* wait for an internal signal */
1729 s->async->inttrig = usbdux_ao_inttrig;
1732 up(&this_usbduxsub->sem);
1733 return 0;
1736 static int usbdux_dio_insn_config(struct comedi_device *dev,
1737 struct comedi_subdevice *s,
1738 struct comedi_insn *insn, unsigned int *data)
1740 int chan = CR_CHAN(insn->chanspec);
1742 /* The input or output configuration of each digital line is
1743 * configured by a special insn_config instruction. chanspec
1744 * contains the channel to be changed, and data[0] contains the
1745 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1747 switch (data[0]) {
1748 case INSN_CONFIG_DIO_OUTPUT:
1749 s->io_bits |= 1 << chan; /* 1 means Out */
1750 break;
1751 case INSN_CONFIG_DIO_INPUT:
1752 s->io_bits &= ~(1 << chan);
1753 break;
1754 case INSN_CONFIG_DIO_QUERY:
1755 data[1] =
1756 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1757 break;
1758 default:
1759 return -EINVAL;
1760 break;
1762 /* we don't tell the firmware here as it would take 8 frames */
1763 /* to submit the information. We do it in the insn_bits. */
1764 return insn->n;
1767 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1768 struct comedi_subdevice *s,
1769 struct comedi_insn *insn, unsigned int *data)
1772 struct usbduxsub *this_usbduxsub = dev->private;
1773 int err;
1775 if (!this_usbduxsub)
1776 return -EFAULT;
1778 if (insn->n != 2)
1779 return -EINVAL;
1781 down(&this_usbduxsub->sem);
1783 if (!(this_usbduxsub->probed)) {
1784 up(&this_usbduxsub->sem);
1785 return -ENODEV;
1788 /* The insn data is a mask in data[0] and the new data
1789 * in data[1], each channel cooresponding to a bit. */
1790 s->state &= ~data[0];
1791 s->state |= data[0] & data[1];
1792 this_usbduxsub->dux_commands[1] = s->io_bits;
1793 this_usbduxsub->dux_commands[2] = s->state;
1795 /* This command also tells the firmware to return */
1796 /* the digital input lines */
1797 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1798 if (err < 0) {
1799 up(&this_usbduxsub->sem);
1800 return err;
1802 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1803 if (err < 0) {
1804 up(&this_usbduxsub->sem);
1805 return err;
1808 data[1] = le16_to_cpu(this_usbduxsub->insnBuffer[1]);
1809 up(&this_usbduxsub->sem);
1810 return 2;
1813 /* reads the 4 counters, only two are used just now */
1814 static int usbdux_counter_read(struct comedi_device *dev,
1815 struct comedi_subdevice *s,
1816 struct comedi_insn *insn, unsigned int *data)
1818 struct usbduxsub *this_usbduxsub = dev->private;
1819 int chan = insn->chanspec;
1820 int err;
1822 if (!this_usbduxsub)
1823 return -EFAULT;
1825 down(&this_usbduxsub->sem);
1827 if (!(this_usbduxsub->probed)) {
1828 up(&this_usbduxsub->sem);
1829 return -ENODEV;
1832 err = send_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1833 if (err < 0) {
1834 up(&this_usbduxsub->sem);
1835 return err;
1838 err = receive_dux_commands(this_usbduxsub, READCOUNTERCOMMAND);
1839 if (err < 0) {
1840 up(&this_usbduxsub->sem);
1841 return err;
1844 data[0] = le16_to_cpu(this_usbduxsub->insnBuffer[chan + 1]);
1845 up(&this_usbduxsub->sem);
1846 return 1;
1849 static int usbdux_counter_write(struct comedi_device *dev,
1850 struct comedi_subdevice *s,
1851 struct comedi_insn *insn, unsigned int *data)
1853 struct usbduxsub *this_usbduxsub = dev->private;
1854 int err;
1856 if (!this_usbduxsub)
1857 return -EFAULT;
1859 down(&this_usbduxsub->sem);
1861 if (!(this_usbduxsub->probed)) {
1862 up(&this_usbduxsub->sem);
1863 return -ENODEV;
1866 this_usbduxsub->dux_commands[1] = insn->chanspec;
1867 *((int16_t *) (this_usbduxsub->dux_commands + 2)) = cpu_to_le16(*data);
1869 err = send_dux_commands(this_usbduxsub, WRITECOUNTERCOMMAND);
1870 if (err < 0) {
1871 up(&this_usbduxsub->sem);
1872 return err;
1875 up(&this_usbduxsub->sem);
1877 return 1;
1880 static int usbdux_counter_config(struct comedi_device *dev,
1881 struct comedi_subdevice *s,
1882 struct comedi_insn *insn, unsigned int *data)
1884 /* nothing to do so far */
1885 return 2;
1888 /***********************************/
1889 /* PWM */
1891 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1893 int err = 0;
1895 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1896 if (usbduxsub_tmp->urbPwm)
1897 usb_kill_urb(usbduxsub_tmp->urbPwm);
1898 dev_dbg(&usbduxsub_tmp->interface->dev,
1899 "comedi: unlinked PwmURB: res=%d\n", err);
1901 return err;
1904 /* This cancels a running acquisition operation
1905 * in any context.
1907 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1909 int ret = 0;
1911 if (!this_usbduxsub)
1912 return -EFAULT;
1914 dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1915 if (do_unlink)
1916 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1918 this_usbduxsub->pwm_cmd_running = 0;
1920 return ret;
1923 /* force unlink - is called by comedi */
1924 static int usbdux_pwm_cancel(struct comedi_device *dev,
1925 struct comedi_subdevice *s)
1927 struct usbduxsub *this_usbduxsub = dev->private;
1928 int res = 0;
1930 /* unlink only if it is really running */
1931 res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1933 dev_dbg(&this_usbduxsub->interface->dev,
1934 "comedi %d: sending pwm off command to the usb device.\n",
1935 dev->minor);
1936 res = send_dux_commands(this_usbduxsub, SENDPWMOFF);
1937 if (res < 0)
1938 return res;
1940 return res;
1943 static void usbduxsub_pwm_irq(struct urb *urb)
1945 int ret;
1946 struct usbduxsub *this_usbduxsub;
1947 struct comedi_device *this_comedidev;
1948 struct comedi_subdevice *s;
1950 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1952 /* the context variable points to the subdevice */
1953 this_comedidev = urb->context;
1954 /* the private structure of the subdevice is struct usbduxsub */
1955 this_usbduxsub = this_comedidev->private;
1957 s = this_comedidev->subdevices + SUBDEV_DA;
1959 switch (urb->status) {
1960 case 0:
1961 /* success */
1962 break;
1964 case -ECONNRESET:
1965 case -ENOENT:
1966 case -ESHUTDOWN:
1967 case -ECONNABORTED:
1969 * after an unlink command, unplug, ... etc
1970 * no unlink needed here. Already shutting down.
1972 if (this_usbduxsub->pwm_cmd_running)
1973 usbdux_pwm_stop(this_usbduxsub, 0);
1975 return;
1977 default:
1978 /* a real error */
1979 if (this_usbduxsub->pwm_cmd_running) {
1980 dev_err(&this_usbduxsub->interface->dev,
1981 "comedi_: Non-zero urb status received in "
1982 "pwm intr context: %d\n", urb->status);
1983 usbdux_pwm_stop(this_usbduxsub, 0);
1985 return;
1988 /* are we actually running? */
1989 if (!(this_usbduxsub->pwm_cmd_running))
1990 return;
1992 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1993 urb->dev = this_usbduxsub->usbdev;
1994 urb->status = 0;
1995 if (this_usbduxsub->pwm_cmd_running) {
1996 ret = usb_submit_urb(urb, GFP_ATOMIC);
1997 if (ret < 0) {
1998 dev_err(&this_usbduxsub->interface->dev,
1999 "comedi_: pwm urb resubm failed in int-cont. "
2000 "ret=%d", ret);
2001 if (ret == EL2NSYNC)
2002 dev_err(&this_usbduxsub->interface->dev,
2003 "buggy USB host controller or bug in "
2004 "IRQ handling!\n");
2006 /* don't do an unlink here */
2007 usbdux_pwm_stop(this_usbduxsub, 0);
2012 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
2014 int errFlag;
2016 if (!usbduxsub)
2017 return -EFAULT;
2019 dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
2021 /* in case of a resubmission after an unlink... */
2022 usb_fill_bulk_urb(usbduxsub->urbPwm,
2023 usbduxsub->usbdev,
2024 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
2025 usbduxsub->urbPwm->transfer_buffer,
2026 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq,
2027 usbduxsub->comedidev);
2029 errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
2030 if (errFlag) {
2031 dev_err(&usbduxsub->interface->dev,
2032 "comedi_: usbdux: pwm: usb_submit_urb error %d\n",
2033 errFlag);
2034 return errFlag;
2036 return 0;
2039 static int usbdux_pwm_period(struct comedi_device *dev,
2040 struct comedi_subdevice *s, unsigned int period)
2042 struct usbduxsub *this_usbduxsub = dev->private;
2043 int fx2delay = 255;
2045 if (period < MIN_PWM_PERIOD) {
2046 dev_err(&this_usbduxsub->interface->dev,
2047 "comedi%d: illegal period setting for pwm.\n",
2048 dev->minor);
2049 return -EAGAIN;
2050 } else {
2051 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
2052 if (fx2delay > 255) {
2053 dev_err(&this_usbduxsub->interface->dev,
2054 "comedi%d: period %d for pwm is too low.\n",
2055 dev->minor, period);
2056 return -EAGAIN;
2059 this_usbduxsub->pwmDelay = fx2delay;
2060 this_usbduxsub->pwmPeriod = period;
2061 dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
2062 __func__, period, fx2delay);
2063 return 0;
2066 /* is called from insn so there's no need to do all the sanity checks */
2067 static int usbdux_pwm_start(struct comedi_device *dev,
2068 struct comedi_subdevice *s)
2070 int ret, i;
2071 struct usbduxsub *this_usbduxsub = dev->private;
2073 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
2074 dev->minor, __func__);
2076 if (this_usbduxsub->pwm_cmd_running) {
2077 /* already running */
2078 return 0;
2081 this_usbduxsub->dux_commands[1] = ((int8_t) this_usbduxsub->pwmDelay);
2082 ret = send_dux_commands(this_usbduxsub, SENDPWMON);
2083 if (ret < 0)
2084 return ret;
2086 /* initialise the buffer */
2087 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
2088 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
2090 this_usbduxsub->pwm_cmd_running = 1;
2091 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
2092 if (ret < 0) {
2093 this_usbduxsub->pwm_cmd_running = 0;
2094 return ret;
2096 return 0;
2099 /* generates the bit pattern for PWM with the optional sign bit */
2100 static int usbdux_pwm_pattern(struct comedi_device *dev,
2101 struct comedi_subdevice *s, int channel,
2102 unsigned int value, unsigned int sign)
2104 struct usbduxsub *this_usbduxsub = dev->private;
2105 int i, szbuf;
2106 char *pBuf;
2107 char pwm_mask;
2108 char sgn_mask;
2109 char c;
2111 if (!this_usbduxsub)
2112 return -EFAULT;
2114 /* this is the DIO bit which carries the PWM data */
2115 pwm_mask = (1 << channel);
2116 /* this is the DIO bit which carries the optional direction bit */
2117 sgn_mask = (16 << channel);
2118 /* this is the buffer which will be filled with the with bit */
2119 /* pattern for one period */
2120 szbuf = this_usbduxsub->sizePwmBuf;
2121 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2122 for (i = 0; i < szbuf; i++) {
2123 c = *pBuf;
2124 /* reset bits */
2125 c = c & (~pwm_mask);
2126 /* set the bit as long as the index is lower than the value */
2127 if (i < value)
2128 c = c | pwm_mask;
2129 /* set the optional sign bit for a relay */
2130 if (!sign) {
2131 /* positive value */
2132 c = c & (~sgn_mask);
2133 } else {
2134 /* negative value */
2135 c = c | sgn_mask;
2137 *(pBuf++) = c;
2139 return 1;
2142 static int usbdux_pwm_write(struct comedi_device *dev,
2143 struct comedi_subdevice *s,
2144 struct comedi_insn *insn, unsigned int *data)
2146 struct usbduxsub *this_usbduxsub = dev->private;
2148 if (!this_usbduxsub)
2149 return -EFAULT;
2151 if ((insn->n) != 1) {
2153 * doesn't make sense to have more than one value here because
2154 * it would just overwrite the PWM buffer a couple of times
2156 return -EINVAL;
2160 * the sign is set via a special INSN only, this gives us 8 bits for
2161 * normal operation
2162 * relay sign 0 by default
2164 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
2167 static int usbdux_pwm_read(struct comedi_device *x1,
2168 struct comedi_subdevice *x2, struct comedi_insn *x3,
2169 unsigned int *x4)
2171 /* not needed */
2172 return -EINVAL;
2175 /* switches on/off PWM */
2176 static int usbdux_pwm_config(struct comedi_device *dev,
2177 struct comedi_subdevice *s,
2178 struct comedi_insn *insn, unsigned int *data)
2180 struct usbduxsub *this_usbduxsub = dev->private;
2181 switch (data[0]) {
2182 case INSN_CONFIG_ARM:
2183 /* switch it on */
2184 dev_dbg(&this_usbduxsub->interface->dev,
2185 "comedi%d: %s: pwm on\n", dev->minor, __func__);
2187 * if not zero the PWM is limited to a certain time which is
2188 * not supported here
2190 if (data[1] != 0)
2191 return -EINVAL;
2192 return usbdux_pwm_start(dev, s);
2193 case INSN_CONFIG_DISARM:
2194 dev_dbg(&this_usbduxsub->interface->dev,
2195 "comedi%d: %s: pwm off\n", dev->minor, __func__);
2196 return usbdux_pwm_cancel(dev, s);
2197 case INSN_CONFIG_GET_PWM_STATUS:
2199 * to check if the USB transmission has failed or in case PWM
2200 * was limited to n cycles to check if it has terminated
2202 data[1] = this_usbduxsub->pwm_cmd_running;
2203 return 0;
2204 case INSN_CONFIG_PWM_SET_PERIOD:
2205 dev_dbg(&this_usbduxsub->interface->dev,
2206 "comedi%d: %s: setting period\n", dev->minor, __func__);
2207 return usbdux_pwm_period(dev, s, data[1]);
2208 case INSN_CONFIG_PWM_GET_PERIOD:
2209 data[1] = this_usbduxsub->pwmPeriod;
2210 return 0;
2211 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2212 /* value in the first byte and the sign in the second for a
2213 relay */
2214 return usbdux_pwm_pattern(dev, s,
2215 /* the channel number */
2216 CR_CHAN(insn->chanspec),
2217 /* actual PWM data */
2218 data[1],
2219 /* just a sign */
2220 (data[2] != 0));
2221 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2222 /* values are not kept in this driver, nothing to return here */
2223 return -EINVAL;
2225 return -EINVAL;
2228 /* end of PWM */
2229 /*****************************************************************/
2231 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2233 int i;
2235 if (!usbduxsub_tmp)
2236 return;
2237 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2239 /* shows the usb subsystem that the driver is down */
2240 if (usbduxsub_tmp->interface)
2241 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2243 usbduxsub_tmp->probed = 0;
2245 if (usbduxsub_tmp->urbIn) {
2246 if (usbduxsub_tmp->ai_cmd_running) {
2247 usbduxsub_tmp->ai_cmd_running = 0;
2248 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2250 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2251 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2252 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2253 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2254 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2255 usbduxsub_tmp->urbIn[i] = NULL;
2257 kfree(usbduxsub_tmp->urbIn);
2258 usbduxsub_tmp->urbIn = NULL;
2260 if (usbduxsub_tmp->urbOut) {
2261 if (usbduxsub_tmp->ao_cmd_running) {
2262 usbduxsub_tmp->ao_cmd_running = 0;
2263 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2265 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2266 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2267 kfree(usbduxsub_tmp->
2268 urbOut[i]->transfer_buffer);
2269 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2270 NULL;
2272 if (usbduxsub_tmp->urbOut[i]) {
2273 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2274 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2275 usbduxsub_tmp->urbOut[i] = NULL;
2278 kfree(usbduxsub_tmp->urbOut);
2279 usbduxsub_tmp->urbOut = NULL;
2281 if (usbduxsub_tmp->urbPwm) {
2282 if (usbduxsub_tmp->pwm_cmd_running) {
2283 usbduxsub_tmp->pwm_cmd_running = 0;
2284 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2286 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2287 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2288 usb_kill_urb(usbduxsub_tmp->urbPwm);
2289 usb_free_urb(usbduxsub_tmp->urbPwm);
2290 usbduxsub_tmp->urbPwm = NULL;
2292 kfree(usbduxsub_tmp->inBuffer);
2293 usbduxsub_tmp->inBuffer = NULL;
2294 kfree(usbduxsub_tmp->insnBuffer);
2295 usbduxsub_tmp->insnBuffer = NULL;
2296 kfree(usbduxsub_tmp->inBuffer);
2297 usbduxsub_tmp->inBuffer = NULL;
2298 kfree(usbduxsub_tmp->dac_commands);
2299 usbduxsub_tmp->dac_commands = NULL;
2300 kfree(usbduxsub_tmp->dux_commands);
2301 usbduxsub_tmp->dux_commands = NULL;
2302 usbduxsub_tmp->ai_cmd_running = 0;
2303 usbduxsub_tmp->ao_cmd_running = 0;
2304 usbduxsub_tmp->pwm_cmd_running = 0;
2307 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2308 void *context)
2310 struct usbduxsub *usbduxsub_tmp = context;
2311 struct usb_device *usbdev = usbduxsub_tmp->usbdev;
2312 int ret;
2314 if (fw == NULL) {
2315 dev_err(&usbdev->dev,
2316 "Firmware complete handler without firmware!\n");
2317 return;
2321 * we need to upload the firmware here because fw will be
2322 * freed once we've left this function
2324 ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2326 if (ret) {
2327 dev_err(&usbdev->dev,
2328 "Could not upload firmware (err=%d)\n", ret);
2329 goto out;
2331 comedi_usb_auto_config(usbdev, BOARDNAME);
2332 out:
2333 release_firmware(fw);
2336 /* allocate memory for the urbs and initialise them */
2337 static int usbduxsub_probe(struct usb_interface *uinterf,
2338 const struct usb_device_id *id)
2340 struct usb_device *udev = interface_to_usbdev(uinterf);
2341 struct device *dev = &uinterf->dev;
2342 int i;
2343 int index;
2344 int ret;
2346 dev_dbg(dev, "comedi_: usbdux_: "
2347 "finding a free structure for the usb-device\n");
2349 down(&start_stop_sem);
2350 /* look for a free place in the usbdux array */
2351 index = -1;
2352 for (i = 0; i < NUMUSBDUX; i++) {
2353 if (!(usbduxsub[i].probed)) {
2354 index = i;
2355 break;
2359 /* no more space */
2360 if (index == -1) {
2361 dev_err(dev, "Too many usbdux-devices connected.\n");
2362 up(&start_stop_sem);
2363 return -EMFILE;
2365 dev_dbg(dev, "comedi_: usbdux: "
2366 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2368 init_MUTEX(&(usbduxsub[index].sem));
2369 /* save a pointer to the usb device */
2370 usbduxsub[index].usbdev = udev;
2372 /* 2.6: save the interface itself */
2373 usbduxsub[index].interface = uinterf;
2374 /* get the interface number from the interface */
2375 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2376 /* hand the private data over to the usb subsystem */
2377 /* will be needed for disconnect */
2378 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2380 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2382 /* test if it is high speed (USB 2.0) */
2383 usbduxsub[index].high_speed =
2384 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2386 /* create space for the commands of the DA converter */
2387 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2388 if (!usbduxsub[index].dac_commands) {
2389 dev_err(dev, "comedi_: usbdux: "
2390 "error alloc space for dac commands\n");
2391 tidy_up(&(usbduxsub[index]));
2392 up(&start_stop_sem);
2393 return -ENOMEM;
2395 /* create space for the commands going to the usb device */
2396 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2397 if (!usbduxsub[index].dux_commands) {
2398 dev_err(dev, "comedi_: usbdux: "
2399 "error alloc space for dac commands\n");
2400 tidy_up(&(usbduxsub[index]));
2401 up(&start_stop_sem);
2402 return -ENOMEM;
2404 /* create space for the in buffer and set it to zero */
2405 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2406 if (!(usbduxsub[index].inBuffer)) {
2407 dev_err(dev, "comedi_: usbdux: "
2408 "could not alloc space for inBuffer\n");
2409 tidy_up(&(usbduxsub[index]));
2410 up(&start_stop_sem);
2411 return -ENOMEM;
2413 /* create space of the instruction buffer */
2414 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2415 if (!(usbduxsub[index].insnBuffer)) {
2416 dev_err(dev, "comedi_: usbdux: "
2417 "could not alloc space for insnBuffer\n");
2418 tidy_up(&(usbduxsub[index]));
2419 up(&start_stop_sem);
2420 return -ENOMEM;
2422 /* create space for the outbuffer */
2423 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2424 if (!(usbduxsub[index].outBuffer)) {
2425 dev_err(dev, "comedi_: usbdux: "
2426 "could not alloc space for outBuffer\n");
2427 tidy_up(&(usbduxsub[index]));
2428 up(&start_stop_sem);
2429 return -ENOMEM;
2431 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2432 i = usb_set_interface(usbduxsub[index].usbdev,
2433 usbduxsub[index].ifnum, 3);
2434 if (i < 0) {
2435 dev_err(dev, "comedi_: usbdux%d: "
2436 "could not set alternate setting 3 in high speed.\n",
2437 index);
2438 tidy_up(&(usbduxsub[index]));
2439 up(&start_stop_sem);
2440 return -ENODEV;
2442 if (usbduxsub[index].high_speed)
2443 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2444 else
2445 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2447 usbduxsub[index].urbIn =
2448 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2449 GFP_KERNEL);
2450 if (!(usbduxsub[index].urbIn)) {
2451 dev_err(dev, "comedi_: usbdux: Could not alloc. urbIn array\n");
2452 tidy_up(&(usbduxsub[index]));
2453 up(&start_stop_sem);
2454 return -ENOMEM;
2456 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2457 /* one frame: 1ms */
2458 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2459 if (usbduxsub[index].urbIn[i] == NULL) {
2460 dev_err(dev, "comedi_: usbdux%d: "
2461 "Could not alloc. urb(%d)\n", index, i);
2462 tidy_up(&(usbduxsub[index]));
2463 up(&start_stop_sem);
2464 return -ENOMEM;
2466 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2467 /* will be filled later with a pointer to the comedi-device */
2468 /* and ONLY then the urb should be submitted */
2469 usbduxsub[index].urbIn[i]->context = NULL;
2470 usbduxsub[index].urbIn[i]->pipe =
2471 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2472 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2473 usbduxsub[index].urbIn[i]->transfer_buffer =
2474 kzalloc(SIZEINBUF, GFP_KERNEL);
2475 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2476 dev_err(dev, "comedi_: usbdux%d: "
2477 "could not alloc. transb.\n", index);
2478 tidy_up(&(usbduxsub[index]));
2479 up(&start_stop_sem);
2480 return -ENOMEM;
2482 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2483 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2484 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2485 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2486 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length = SIZEINBUF;
2489 /* out */
2490 if (usbduxsub[index].high_speed)
2491 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2492 else
2493 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2495 usbduxsub[index].urbOut =
2496 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2497 GFP_KERNEL);
2498 if (!(usbduxsub[index].urbOut)) {
2499 dev_err(dev, "comedi_: usbdux: "
2500 "Could not alloc. urbOut array\n");
2501 tidy_up(&(usbduxsub[index]));
2502 up(&start_stop_sem);
2503 return -ENOMEM;
2505 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2506 /* one frame: 1ms */
2507 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2508 if (usbduxsub[index].urbOut[i] == NULL) {
2509 dev_err(dev, "comedi_: usbdux%d: "
2510 "Could not alloc. urb(%d)\n", index, i);
2511 tidy_up(&(usbduxsub[index]));
2512 up(&start_stop_sem);
2513 return -ENOMEM;
2515 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2516 /* will be filled later with a pointer to the comedi-device */
2517 /* and ONLY then the urb should be submitted */
2518 usbduxsub[index].urbOut[i]->context = NULL;
2519 usbduxsub[index].urbOut[i]->pipe =
2520 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2521 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2522 usbduxsub[index].urbOut[i]->transfer_buffer =
2523 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2524 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2525 dev_err(dev, "comedi_: usbdux%d: "
2526 "could not alloc. transb.\n", index);
2527 tidy_up(&(usbduxsub[index]));
2528 up(&start_stop_sem);
2529 return -ENOMEM;
2531 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2532 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2533 usbduxsub[index].urbOut[i]->transfer_buffer_length = SIZEOUTBUF;
2534 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2535 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2536 SIZEOUTBUF;
2537 if (usbduxsub[index].high_speed) {
2538 /* uframes */
2539 usbduxsub[index].urbOut[i]->interval = 8;
2540 } else {
2541 /* frames */
2542 usbduxsub[index].urbOut[i]->interval = 1;
2546 /* pwm */
2547 if (usbduxsub[index].high_speed) {
2548 /* max bulk ep size in high speed */
2549 usbduxsub[index].sizePwmBuf = 512;
2550 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2551 if (usbduxsub[index].urbPwm == NULL) {
2552 dev_err(dev, "comedi_: usbdux%d: "
2553 "Could not alloc. pwm urb\n", index);
2554 tidy_up(&(usbduxsub[index]));
2555 up(&start_stop_sem);
2556 return -ENOMEM;
2558 usbduxsub[index].urbPwm->transfer_buffer =
2559 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2560 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2561 dev_err(dev, "comedi_: usbdux%d: "
2562 "could not alloc. transb. for pwm\n", index);
2563 tidy_up(&(usbduxsub[index]));
2564 up(&start_stop_sem);
2565 return -ENOMEM;
2567 } else {
2568 usbduxsub[index].urbPwm = NULL;
2569 usbduxsub[index].sizePwmBuf = 0;
2572 usbduxsub[index].ai_cmd_running = 0;
2573 usbduxsub[index].ao_cmd_running = 0;
2574 usbduxsub[index].pwm_cmd_running = 0;
2576 /* we've reached the bottom of the function */
2577 usbduxsub[index].probed = 1;
2578 up(&start_stop_sem);
2580 ret = request_firmware_nowait(THIS_MODULE,
2581 FW_ACTION_HOTPLUG,
2582 "usbdux_firmware.bin",
2583 &udev->dev,
2584 GFP_KERNEL,
2585 usbduxsub + index,
2586 usbdux_firmware_request_complete_handler);
2588 if (ret) {
2589 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2590 return ret;
2593 dev_info(dev, "comedi_: usbdux%d "
2594 "has been successfully initialised.\n", index);
2595 /* success */
2596 return 0;
2599 static void usbduxsub_disconnect(struct usb_interface *intf)
2601 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2602 struct usb_device *udev = interface_to_usbdev(intf);
2604 if (!usbduxsub_tmp) {
2605 dev_err(&intf->dev,
2606 "comedi_: disconnect called with null pointer.\n");
2607 return;
2609 if (usbduxsub_tmp->usbdev != udev) {
2610 dev_err(&intf->dev, "comedi_: BUG! called with wrong ptr!!!\n");
2611 return;
2613 comedi_usb_auto_unconfig(udev);
2614 down(&start_stop_sem);
2615 down(&usbduxsub_tmp->sem);
2616 tidy_up(usbduxsub_tmp);
2617 up(&usbduxsub_tmp->sem);
2618 up(&start_stop_sem);
2619 dev_dbg(&intf->dev, "comedi_: disconnected from the usb\n");
2622 /* is called when comedi-config is called */
2623 static int usbdux_attach(struct comedi_device *dev, struct comedi_devconfig *it)
2625 int ret;
2626 int index;
2627 int i;
2628 struct usbduxsub *udev;
2630 struct comedi_subdevice *s = NULL;
2631 dev->private = NULL;
2633 down(&start_stop_sem);
2634 /* find a valid device which has been detected by the probe function of
2635 * the usb */
2636 index = -1;
2637 for (i = 0; i < NUMUSBDUX; i++) {
2638 if ((usbduxsub[i].probed) && (!usbduxsub[i].attached)) {
2639 index = i;
2640 break;
2644 if (index < 0) {
2645 printk(KERN_ERR "comedi%d: usbdux: error: attach failed, no "
2646 "usbdux devs connected to the usb bus.\n", dev->minor);
2647 up(&start_stop_sem);
2648 return -ENODEV;
2651 udev = &usbduxsub[index];
2652 down(&udev->sem);
2653 /* pointer back to the corresponding comedi device */
2654 udev->comedidev = dev;
2656 /* trying to upload the firmware into the chip */
2657 if (comedi_aux_data(it->options, 0) &&
2658 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
2659 firmwareUpload(udev, comedi_aux_data(it->options, 0),
2660 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
2663 dev->board_name = BOARDNAME;
2665 /* set number of subdevices */
2666 if (udev->high_speed) {
2667 /* with pwm */
2668 dev->n_subdevices = 5;
2669 } else {
2670 /* without pwm */
2671 dev->n_subdevices = 4;
2674 /* allocate space for the subdevices */
2675 ret = alloc_subdevices(dev, dev->n_subdevices);
2676 if (ret < 0) {
2677 dev_err(&udev->interface->dev,
2678 "comedi%d: error alloc space for subdev\n", dev->minor);
2679 up(&start_stop_sem);
2680 return ret;
2683 dev_info(&udev->interface->dev,
2684 "comedi%d: usb-device %d is attached to comedi.\n",
2685 dev->minor, index);
2686 /* private structure is also simply the usb-structure */
2687 dev->private = udev;
2689 /* the first subdevice is the A/D converter */
2690 s = dev->subdevices + SUBDEV_AD;
2691 /* the URBs get the comedi subdevice */
2692 /* which is responsible for reading */
2693 /* this is the subdevice which reads data */
2694 dev->read_subdev = s;
2695 /* the subdevice receives as private structure the */
2696 /* usb-structure */
2697 s->private = NULL;
2698 /* analog input */
2699 s->type = COMEDI_SUBD_AI;
2700 /* readable and ref is to ground */
2701 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
2702 /* 8 channels */
2703 s->n_chan = 8;
2704 /* length of the channellist */
2705 s->len_chanlist = 8;
2706 /* callback functions */
2707 s->insn_read = usbdux_ai_insn_read;
2708 s->do_cmdtest = usbdux_ai_cmdtest;
2709 s->do_cmd = usbdux_ai_cmd;
2710 s->cancel = usbdux_ai_cancel;
2711 /* max value from the A/D converter (12bit) */
2712 s->maxdata = 0xfff;
2713 /* range table to convert to physical units */
2714 s->range_table = (&range_usbdux_ai_range);
2716 /* analog out */
2717 s = dev->subdevices + SUBDEV_DA;
2718 /* analog out */
2719 s->type = COMEDI_SUBD_AO;
2720 /* backward pointer */
2721 dev->write_subdev = s;
2722 /* the subdevice receives as private structure the */
2723 /* usb-structure */
2724 s->private = NULL;
2725 /* are writable */
2726 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2727 /* 4 channels */
2728 s->n_chan = 4;
2729 /* length of the channellist */
2730 s->len_chanlist = 4;
2731 /* 12 bit resolution */
2732 s->maxdata = 0x0fff;
2733 /* bipolar range */
2734 s->range_table = (&range_usbdux_ao_range);
2735 /* callback */
2736 s->do_cmdtest = usbdux_ao_cmdtest;
2737 s->do_cmd = usbdux_ao_cmd;
2738 s->cancel = usbdux_ao_cancel;
2739 s->insn_read = usbdux_ao_insn_read;
2740 s->insn_write = usbdux_ao_insn_write;
2742 /* digital I/O */
2743 s = dev->subdevices + SUBDEV_DIO;
2744 s->type = COMEDI_SUBD_DIO;
2745 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2746 s->n_chan = 8;
2747 s->maxdata = 1;
2748 s->range_table = (&range_digital);
2749 s->insn_bits = usbdux_dio_insn_bits;
2750 s->insn_config = usbdux_dio_insn_config;
2751 /* we don't use it */
2752 s->private = NULL;
2754 /* counter */
2755 s = dev->subdevices + SUBDEV_COUNTER;
2756 s->type = COMEDI_SUBD_COUNTER;
2757 s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
2758 s->n_chan = 4;
2759 s->maxdata = 0xFFFF;
2760 s->insn_read = usbdux_counter_read;
2761 s->insn_write = usbdux_counter_write;
2762 s->insn_config = usbdux_counter_config;
2764 if (udev->high_speed) {
2765 /* timer / pwm */
2766 s = dev->subdevices + SUBDEV_PWM;
2767 s->type = COMEDI_SUBD_PWM;
2768 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2769 s->n_chan = 8;
2770 /* this defines the max duty cycle resolution */
2771 s->maxdata = udev->sizePwmBuf;
2772 s->insn_write = usbdux_pwm_write;
2773 s->insn_read = usbdux_pwm_read;
2774 s->insn_config = usbdux_pwm_config;
2775 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2777 /* finally decide that it's attached */
2778 udev->attached = 1;
2780 up(&udev->sem);
2782 up(&start_stop_sem);
2784 dev_info(&udev->interface->dev, "comedi%d: attached to usbdux.\n",
2785 dev->minor);
2787 return 0;
2790 static int usbdux_detach(struct comedi_device *dev)
2792 struct usbduxsub *usbduxsub_tmp;
2794 if (!dev) {
2795 printk(KERN_ERR
2796 "comedi?: usbdux: detach without dev variable...\n");
2797 return -EFAULT;
2800 usbduxsub_tmp = dev->private;
2801 if (!usbduxsub_tmp) {
2802 printk(KERN_ERR
2803 "comedi?: usbdux: detach without ptr to usbduxsub[]\n");
2804 return -EFAULT;
2807 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi%d: detach usb device\n",
2808 dev->minor);
2810 down(&usbduxsub_tmp->sem);
2811 /* Don't allow detach to free the private structure */
2812 /* It's one entry of of usbduxsub[] */
2813 dev->private = NULL;
2814 usbduxsub_tmp->attached = 0;
2815 usbduxsub_tmp->comedidev = NULL;
2816 dev_dbg(&usbduxsub_tmp->interface->dev,
2817 "comedi%d: detach: successfully removed\n", dev->minor);
2818 up(&usbduxsub_tmp->sem);
2819 return 0;
2822 /* main driver struct */
2823 static struct comedi_driver driver_usbdux = {
2824 .driver_name = "usbdux",
2825 .module = THIS_MODULE,
2826 .attach = usbdux_attach,
2827 .detach = usbdux_detach,
2830 /* Table with the USB-devices: just now only testing IDs */
2831 static const struct usb_device_id usbduxsub_table[] = {
2832 {USB_DEVICE(0x13d8, 0x0001)},
2833 {USB_DEVICE(0x13d8, 0x0002)},
2834 {} /* Terminating entry */
2837 MODULE_DEVICE_TABLE(usb, usbduxsub_table);
2839 /* The usbduxsub-driver */
2840 static struct usb_driver usbduxsub_driver = {
2841 .name = BOARDNAME,
2842 .probe = usbduxsub_probe,
2843 .disconnect = usbduxsub_disconnect,
2844 .id_table = usbduxsub_table,
2847 /* Can't use the nice macro as I have also to initialise the USB */
2848 /* subsystem: */
2849 /* registering the usb-system _and_ the comedi-driver */
2850 static int __init init_usbdux(void)
2852 printk(KERN_INFO KBUILD_MODNAME ": "
2853 DRIVER_VERSION ":" DRIVER_DESC "\n");
2854 usb_register(&usbduxsub_driver);
2855 comedi_driver_register(&driver_usbdux);
2856 return 0;
2859 /* deregistering the comedi driver and the usb-subsystem */
2860 static void __exit exit_usbdux(void)
2862 comedi_driver_unregister(&driver_usbdux);
2863 usb_deregister(&usbduxsub_driver);
2866 module_init(init_usbdux);
2867 module_exit(exit_usbdux);
2869 MODULE_AUTHOR(DRIVER_AUTHOR);
2870 MODULE_DESCRIPTION(DRIVER_DESC);
2871 MODULE_LICENSE("GPL");