staging: comedi: usbduxsigma: Fixed wrong range for the analogue channel.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / comedi / drivers / usbduxsigma.c
blob6144afb8cbaaf92a1ca09dfa5404787d72ee35ed
1 #define DRIVER_VERSION "v0.6"
2 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
3 #define DRIVER_DESC "Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com"
4 /*
5 comedi/drivers/usbdux.c
6 Copyright (C) 2011 Bernd Porr, Bernd.Porr@f2s.com
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: usbduxsigma
25 Description: University of Stirling USB DAQ & INCITE Technology Limited
26 Devices: [ITL] USB-DUX (usbduxsigma.o)
27 Author: Bernd Porr <BerndPorr@f2s.com>
28 Updated: 8 Nov 2011
29 Status: testing
32 * I must give credit here to Chris Baugher who
33 * wrote the driver for AT-MIO-16d. I used some parts of this
34 * driver. I also must give credits to David Brownell
35 * who supported me with the USB development.
37 * Note: the raw data from the A/D converter is 24 bit big endian
38 * anything else is little endian to/from the dux board
41 * Revision history:
42 * 0.1: inital version
43 * 0.2: all basic functions implemented, digital I/O only for one port
44 * 0.3: proper vendor ID and driver name
45 * 0.4: fixed D/A voltage range
46 * 0.5: various bug fixes, health check at startup
47 * 0.6: corrected wrong input range
50 /* generates loads of debug info */
51 /* #define NOISY_DUX_DEBUGBUG */
53 #include <linux/kernel.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/input.h>
58 #include <linux/usb.h>
59 #include <linux/fcntl.h>
60 #include <linux/compiler.h>
61 #include <linux/firmware.h>
62 #include "comedi_fc.h"
63 #include "../comedidev.h"
65 #define BOARDNAME "usbduxsigma"
67 /* timeout for the USB-transfer in ms*/
68 #define BULK_TIMEOUT 1000
70 /* constants for "firmware" upload and download */
71 #define USBDUXSUB_FIRMWARE 0xA0
72 #define VENDOR_DIR_IN 0xC0
73 #define VENDOR_DIR_OUT 0x40
75 /* internal addresses of the 8051 processor */
76 #define USBDUXSUB_CPUCS 0xE600
79 * the minor device number, major is 180 only for debugging purposes and to
80 * upload special firmware (programming the eeprom etc) which is not
81 * compatible with the comedi framwork
83 #define USBDUXSUB_MINOR 32
85 /* max lenghth of the transfer-buffer for software upload */
86 #define TB_LEN 0x2000
88 /* Input endpoint number: ISO/IRQ */
89 #define ISOINEP 6
91 /* Output endpoint number: ISO/IRQ */
92 #define ISOOUTEP 2
94 /* This EP sends DUX commands to USBDUX */
95 #define COMMAND_OUT_EP 1
97 /* This EP receives the DUX commands from USBDUX */
98 #define COMMAND_IN_EP 8
100 /* Output endpoint for PWM */
101 #define PWM_EP 4
103 /* 300Hz max frequ under PWM */
104 #define MIN_PWM_PERIOD ((long)(1E9/300))
106 /* Default PWM frequency */
107 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
109 /* Number of channels (16 AD and offset)*/
110 #define NUMCHANNELS 16
112 /* Size of one A/D value */
113 #define SIZEADIN ((sizeof(int32_t)))
116 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
117 * as the first byte.
119 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
121 /* 16 bytes. */
122 #define SIZEINSNBUF 16
124 /* Number of DA channels */
125 #define NUMOUTCHANNELS 8
127 /* size of one value for the D/A converter: channel and value */
128 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(int16_t)))
131 * Size of the output-buffer in bytes
132 * Actually only the first 4 triplets are used but for the
133 * high speed mode we need to pad it to 8 (microframes).
135 #define SIZEOUTBUF ((8*SIZEDAOUT))
138 * Size of the buffer for the dux commands: just now max size is determined
139 * by the analogue out + command byte + panic bytes...
141 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
143 /* Number of in-URBs which receive the data: min=2 */
144 #define NUMOFINBUFFERSFULL 5
146 /* Number of out-URBs which send the data: min=2 */
147 #define NUMOFOUTBUFFERSFULL 5
149 /* Number of in-URBs which receive the data: min=5 */
150 /* must have more buffers due to buggy USB ctr */
151 #define NUMOFINBUFFERSHIGH 10
153 /* Number of out-URBs which send the data: min=5 */
154 /* must have more buffers due to buggy USB ctr */
155 #define NUMOFOUTBUFFERSHIGH 10
157 /* Total number of usbdux devices */
158 #define NUMUSBDUX 16
160 /* Analogue in subdevice */
161 #define SUBDEV_AD 0
163 /* Analogue out subdevice */
164 #define SUBDEV_DA 1
166 /* Digital I/O */
167 #define SUBDEV_DIO 2
169 /* timer aka pwm output */
170 #define SUBDEV_PWM 3
172 /* number of retries to get the right dux command */
173 #define RETRIES 10
175 /**************************************************/
176 /* comedi constants */
177 static const struct comedi_lrange range_usbdux_ai_range = { 1, {
178 BIP_RANGE
179 (2.65/2.0)
183 static const struct comedi_lrange range_usbdux_ao_range = { 1, {
184 UNI_RANGE
185 (2.5),
190 * private structure of one subdevice
194 * This is the structure which holds all the data of
195 * this driver one sub device just now: A/D
197 struct usbduxsub {
198 /* attached? */
199 int attached;
200 /* is it associated with a subdevice? */
201 int probed;
202 /* pointer to the usb-device */
203 struct usb_device *usbdev;
204 /* actual number of in-buffers */
205 int numOfInBuffers;
206 /* actual number of out-buffers */
207 int numOfOutBuffers;
208 /* ISO-transfer handling: buffers */
209 struct urb **urbIn;
210 struct urb **urbOut;
211 /* pwm-transfer handling */
212 struct urb *urbPwm;
213 /* PWM period */
214 unsigned int pwmPeriod;
215 /* PWM internal delay for the GPIF in the FX2 */
216 uint8_t pwmDelay;
217 /* size of the PWM buffer which holds the bit pattern */
218 int sizePwmBuf;
219 /* input buffer for the ISO-transfer */
220 int32_t *inBuffer;
221 /* input buffer for single insn */
222 int8_t *insnBuffer;
223 /* output buffer for single DA outputs */
224 int16_t *outBuffer;
225 /* interface number */
226 int ifnum;
227 /* interface structure in 2.6 */
228 struct usb_interface *interface;
229 /* comedi device for the interrupt context */
230 struct comedi_device *comedidev;
231 /* is it USB_SPEED_HIGH or not? */
232 short int high_speed;
233 /* asynchronous command is running */
234 short int ai_cmd_running;
235 short int ao_cmd_running;
236 /* pwm is running */
237 short int pwm_cmd_running;
238 /* continous aquisition */
239 short int ai_continous;
240 short int ao_continous;
241 /* number of samples to acquire */
242 int ai_sample_count;
243 int ao_sample_count;
244 /* time between samples in units of the timer */
245 unsigned int ai_timer;
246 unsigned int ao_timer;
247 /* counter between aquisitions */
248 unsigned int ai_counter;
249 unsigned int ao_counter;
250 /* interval in frames/uframes */
251 unsigned int ai_interval;
252 /* D/A commands */
253 uint8_t *dac_commands;
254 /* commands */
255 uint8_t *dux_commands;
256 struct semaphore sem;
260 * The pointer to the private usb-data of the driver is also the private data
261 * for the comedi-device. This has to be global as the usb subsystem needs
262 * global variables. The other reason is that this structure must be there
263 * _before_ any comedi command is issued. The usb subsystem must be initialised
264 * before comedi can access it.
266 static struct usbduxsub usbduxsub[NUMUSBDUX];
268 static DEFINE_SEMAPHORE(start_stop_sem);
271 * Stops the data acquision
272 * It should be safe to call this function from any context
274 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
276 int i = 0;
277 int err = 0;
279 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
280 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
281 if (usbduxsub_tmp->urbIn[i]) {
282 /* We wait here until all transfers have been
283 * cancelled. */
284 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
286 dev_dbg(&usbduxsub_tmp->interface->dev,
287 "comedi: usbdux: unlinked InURB %d, err=%d\n",
288 i, err);
291 return err;
295 * This will stop a running acquisition operation
296 * Is called from within this driver from both the
297 * interrupt context and from comedi
299 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
301 int ret = 0;
303 if (!this_usbduxsub) {
304 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
305 return -EFAULT;
307 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
309 if (do_unlink) {
310 /* stop aquistion */
311 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
314 this_usbduxsub->ai_cmd_running = 0;
316 return ret;
320 * This will cancel a running acquisition operation.
321 * This is called by comedi but never from inside the driver.
323 static int usbdux_ai_cancel(struct comedi_device *dev,
324 struct comedi_subdevice *s)
326 struct usbduxsub *this_usbduxsub;
327 int res = 0;
329 /* force unlink of all urbs */
330 this_usbduxsub = dev->private;
331 if (!this_usbduxsub)
332 return -EFAULT;
334 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
336 /* prevent other CPUs from submitting new commands just now */
337 down(&this_usbduxsub->sem);
338 if (!(this_usbduxsub->probed)) {
339 up(&this_usbduxsub->sem);
340 return -ENODEV;
342 /* unlink only if the urb really has been submitted */
343 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
344 up(&this_usbduxsub->sem);
345 return res;
348 /* analogue IN - interrupt service routine */
349 static void usbduxsub_ai_IsocIrq(struct urb *urb)
351 int i, err, n;
352 struct usbduxsub *this_usbduxsub;
353 struct comedi_device *this_comedidev;
354 struct comedi_subdevice *s;
355 int32_t v;
356 unsigned int dio_state;
358 /* the context variable points to the comedi device */
359 this_comedidev = urb->context;
360 /* the private structure of the subdevice is struct usbduxsub */
361 this_usbduxsub = this_comedidev->private;
362 /* subdevice which is the AD converter */
363 s = this_comedidev->subdevices + SUBDEV_AD;
365 /* first we test if something unusual has just happened */
366 switch (urb->status) {
367 case 0:
368 /* copy the result in the transfer buffer */
369 memcpy(this_usbduxsub->inBuffer,
370 urb->transfer_buffer, SIZEINBUF);
371 break;
372 case -EILSEQ:
373 /* error in the ISOchronous data */
374 /* we don't copy the data into the transfer buffer */
375 /* and recycle the last data byte */
376 dev_dbg(&urb->dev->dev,
377 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
378 this_usbduxsub->comedidev->minor);
380 break;
382 case -ECONNRESET:
383 case -ENOENT:
384 case -ESHUTDOWN:
385 case -ECONNABORTED:
386 /* happens after an unlink command */
387 if (this_usbduxsub->ai_cmd_running) {
388 /* we are still running a command */
389 /* tell this comedi */
390 s->async->events |= COMEDI_CB_EOA;
391 s->async->events |= COMEDI_CB_ERROR;
392 comedi_event(this_usbduxsub->comedidev, s);
393 /* stop the transfer w/o unlink */
394 usbdux_ai_stop(this_usbduxsub, 0);
396 return;
398 default:
399 /* a real error on the bus */
400 /* pass error to comedi if we are really running a command */
401 if (this_usbduxsub->ai_cmd_running) {
402 dev_err(&urb->dev->dev,
403 "Non-zero urb status received in ai intr "
404 "context: %d\n", urb->status);
405 s->async->events |= COMEDI_CB_EOA;
406 s->async->events |= COMEDI_CB_ERROR;
407 comedi_event(this_usbduxsub->comedidev, s);
408 /* don't do an unlink here */
409 usbdux_ai_stop(this_usbduxsub, 0);
411 return;
415 * at this point we are reasonably sure that nothing dodgy has happened
416 * are we running a command?
418 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
420 * not running a command, do not continue execution if no
421 * asynchronous command is running in particular not resubmit
423 return;
426 urb->dev = this_usbduxsub->usbdev;
428 /* resubmit the urb */
429 err = usb_submit_urb(urb, GFP_ATOMIC);
430 if (unlikely(err < 0)) {
431 dev_err(&urb->dev->dev,
432 "comedi_: urb resubmit failed in int-context!"
433 "err=%d\n",
434 err);
435 if (err == -EL2NSYNC)
436 dev_err(&urb->dev->dev,
437 "buggy USB host controller or bug in IRQ "
438 "handler!\n");
439 s->async->events |= COMEDI_CB_EOA;
440 s->async->events |= COMEDI_CB_ERROR;
441 comedi_event(this_usbduxsub->comedidev, s);
442 /* don't do an unlink here */
443 usbdux_ai_stop(this_usbduxsub, 0);
444 return;
447 /* get the state of the dio pins to allow external trigger */
448 dio_state = be32_to_cpu(this_usbduxsub->inBuffer[0]);
450 this_usbduxsub->ai_counter--;
451 if (likely(this_usbduxsub->ai_counter > 0))
452 return;
454 /* timer zero, transfer measurements to comedi */
455 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
457 /* test, if we transmit only a fixed number of samples */
458 if (!(this_usbduxsub->ai_continous)) {
459 /* not continous, fixed number of samples */
460 this_usbduxsub->ai_sample_count--;
461 /* all samples received? */
462 if (this_usbduxsub->ai_sample_count < 0) {
463 /* prevent a resubmit next time */
464 usbdux_ai_stop(this_usbduxsub, 0);
465 /* say comedi that the acquistion is over */
466 s->async->events |= COMEDI_CB_EOA;
467 comedi_event(this_usbduxsub->comedidev, s);
468 return;
471 /* get the data from the USB bus and hand it over to comedi */
472 n = s->async->cmd.chanlist_len;
473 for (i = 0; i < n; i++) {
474 /* transfer data, note first byte is the DIO state */
475 v = be32_to_cpu(this_usbduxsub->inBuffer[i+1]);
476 /* strip status byte */
477 v = v & 0x00ffffff;
478 /* convert to unsigned */
479 v = v ^ 0x00800000;
480 /* write the byte to the buffer */
481 err = cfc_write_array_to_buffer(s, &v, sizeof(uint32_t));
482 if (unlikely(err == 0)) {
483 /* buffer overflow */
484 usbdux_ai_stop(this_usbduxsub, 0);
485 return;
488 /* tell comedi that data is there */
489 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
490 comedi_event(this_usbduxsub->comedidev, s);
493 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
495 int i = 0;
496 int err = 0;
498 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
499 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
500 if (usbduxsub_tmp->urbOut[i])
501 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
503 dev_dbg(&usbduxsub_tmp->interface->dev,
504 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
505 i, err);
508 return err;
511 /* This will cancel a running acquisition operation
512 * in any context.
514 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
516 int ret = 0;
518 if (!this_usbduxsub)
519 return -EFAULT;
520 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
522 if (do_unlink)
523 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
525 this_usbduxsub->ao_cmd_running = 0;
527 return ret;
530 /* force unlink, is called by comedi */
531 static int usbdux_ao_cancel(struct comedi_device *dev,
532 struct comedi_subdevice *s)
534 struct usbduxsub *this_usbduxsub = dev->private;
535 int res = 0;
537 if (!this_usbduxsub)
538 return -EFAULT;
540 /* prevent other CPUs from submitting a command just now */
541 down(&this_usbduxsub->sem);
542 if (!(this_usbduxsub->probed)) {
543 up(&this_usbduxsub->sem);
544 return -ENODEV;
546 /* unlink only if it is really running */
547 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
548 up(&this_usbduxsub->sem);
549 return res;
552 static void usbduxsub_ao_IsocIrq(struct urb *urb)
554 int i, ret;
555 uint8_t *datap;
556 struct usbduxsub *this_usbduxsub;
557 struct comedi_device *this_comedidev;
558 struct comedi_subdevice *s;
560 /* the context variable points to the subdevice */
561 this_comedidev = urb->context;
562 /* the private structure of the subdevice is struct usbduxsub */
563 this_usbduxsub = this_comedidev->private;
565 s = this_comedidev->subdevices + SUBDEV_DA;
567 switch (urb->status) {
568 case 0:
569 /* success */
570 break;
572 case -ECONNRESET:
573 case -ENOENT:
574 case -ESHUTDOWN:
575 case -ECONNABORTED:
576 /* after an unlink command, unplug, ... etc */
577 /* no unlink needed here. Already shutting down. */
578 if (this_usbduxsub->ao_cmd_running) {
579 s->async->events |= COMEDI_CB_EOA;
580 comedi_event(this_usbduxsub->comedidev, s);
581 usbdux_ao_stop(this_usbduxsub, 0);
583 return;
585 default:
586 /* a real error */
587 if (this_usbduxsub->ao_cmd_running) {
588 dev_err(&urb->dev->dev,
589 "comedi_: Non-zero urb status received in ao "
590 "intr context: %d\n", urb->status);
591 s->async->events |= COMEDI_CB_ERROR;
592 s->async->events |= COMEDI_CB_EOA;
593 comedi_event(this_usbduxsub->comedidev, s);
594 /* we do an unlink if we are in the high speed mode */
595 usbdux_ao_stop(this_usbduxsub, 0);
597 return;
600 /* are we actually running? */
601 if (!(this_usbduxsub->ao_cmd_running))
602 return;
604 /* normal operation: executing a command in this subdevice */
605 this_usbduxsub->ao_counter--;
606 if ((int)this_usbduxsub->ao_counter <= 0) {
607 /* timer zero */
608 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
610 /* handle non continous aquisition */
611 if (!(this_usbduxsub->ao_continous)) {
612 /* fixed number of samples */
613 this_usbduxsub->ao_sample_count--;
614 if (this_usbduxsub->ao_sample_count < 0) {
615 /* all samples transmitted */
616 usbdux_ao_stop(this_usbduxsub, 0);
617 s->async->events |= COMEDI_CB_EOA;
618 comedi_event(this_usbduxsub->comedidev, s);
619 /* no resubmit of the urb */
620 return;
623 /* transmit data to the USB bus */
624 ((uint8_t *) (urb->transfer_buffer))[0] =
625 s->async->cmd.chanlist_len;
626 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
627 short temp;
628 if (i >= NUMOUTCHANNELS)
629 break;
631 /* pointer to the DA */
632 datap =
633 (&(((uint8_t *) urb->transfer_buffer)[i * 2 + 1]));
634 /* get the data from comedi */
635 ret = comedi_buf_get(s->async, &temp);
636 datap[0] = temp;
637 datap[1] = this_usbduxsub->dac_commands[i];
638 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
639 /* datap[0],datap[1],datap[2]); */
640 if (ret < 0) {
641 dev_err(&urb->dev->dev,
642 "comedi: buffer underflow\n");
643 s->async->events |= COMEDI_CB_EOA;
644 s->async->events |= COMEDI_CB_OVERFLOW;
646 /* transmit data to comedi */
647 s->async->events |= COMEDI_CB_BLOCK;
648 comedi_event(this_usbduxsub->comedidev, s);
651 urb->transfer_buffer_length = SIZEOUTBUF;
652 urb->dev = this_usbduxsub->usbdev;
653 urb->status = 0;
654 if (this_usbduxsub->ao_cmd_running) {
655 if (this_usbduxsub->high_speed) {
656 /* uframes */
657 urb->interval = 8;
658 } else {
659 /* frames */
660 urb->interval = 1;
662 urb->number_of_packets = 1;
663 urb->iso_frame_desc[0].offset = 0;
664 urb->iso_frame_desc[0].length = SIZEOUTBUF;
665 urb->iso_frame_desc[0].status = 0;
666 ret = usb_submit_urb(urb, GFP_ATOMIC);
667 if (ret < 0) {
668 dev_err(&urb->dev->dev,
669 "comedi_: ao urb resubm failed in int-cont. "
670 "ret=%d", ret);
671 if (ret == EL2NSYNC)
672 dev_err(&urb->dev->dev,
673 "buggy USB host controller or bug in "
674 "IRQ handling!\n");
676 s->async->events |= COMEDI_CB_EOA;
677 s->async->events |= COMEDI_CB_ERROR;
678 comedi_event(this_usbduxsub->comedidev, s);
679 /* don't do an unlink here */
680 usbdux_ao_stop(this_usbduxsub, 0);
685 static int usbduxsub_start(struct usbduxsub *usbduxsub)
687 int errcode = 0;
688 uint8_t local_transfer_buffer[16];
690 /* 7f92 to zero */
691 local_transfer_buffer[0] = 0;
692 errcode = usb_control_msg(usbduxsub->usbdev,
693 /* create a pipe for a control transfer */
694 usb_sndctrlpipe(usbduxsub->usbdev, 0),
695 /* bRequest, "Firmware" */
696 USBDUXSUB_FIRMWARE,
697 /* bmRequestType */
698 VENDOR_DIR_OUT,
699 /* Value */
700 USBDUXSUB_CPUCS,
701 /* Index */
702 0x0000,
703 /* address of the transfer buffer */
704 local_transfer_buffer,
705 /* Length */
707 /* Timeout */
708 BULK_TIMEOUT);
709 if (errcode < 0) {
710 dev_err(&usbduxsub->interface->dev,
711 "comedi_: control msg failed (start)\n");
712 return errcode;
714 return 0;
717 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
719 int errcode = 0;
721 uint8_t local_transfer_buffer[16];
723 /* 7f92 to one */
724 local_transfer_buffer[0] = 1;
725 errcode = usb_control_msg(usbduxsub->usbdev,
726 usb_sndctrlpipe(usbduxsub->usbdev, 0),
727 /* bRequest, "Firmware" */
728 USBDUXSUB_FIRMWARE,
729 /* bmRequestType */
730 VENDOR_DIR_OUT,
731 /* Value */
732 USBDUXSUB_CPUCS,
733 /* Index */
734 0x0000, local_transfer_buffer,
735 /* Length */
737 /* Timeout */
738 BULK_TIMEOUT);
739 if (errcode < 0) {
740 dev_err(&usbduxsub->interface->dev,
741 "comedi_: control msg failed (stop)\n");
742 return errcode;
744 return 0;
747 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
748 uint8_t *local_transfer_buffer,
749 unsigned int startAddr, unsigned int len)
751 int errcode;
753 errcode = usb_control_msg(usbduxsub->usbdev,
754 usb_sndctrlpipe(usbduxsub->usbdev, 0),
755 /* brequest, firmware */
756 USBDUXSUB_FIRMWARE,
757 /* bmRequestType */
758 VENDOR_DIR_OUT,
759 /* value */
760 startAddr,
761 /* index */
762 0x0000,
763 /* our local safe buffer */
764 local_transfer_buffer,
765 /* length */
766 len,
767 /* timeout */
768 BULK_TIMEOUT);
769 dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
770 if (errcode < 0) {
771 dev_err(&usbduxsub->interface->dev,
772 "comedi_: upload failed\n");
773 return errcode;
775 return 0;
778 /* the FX2LP has twice as much as the standard FX2 */
779 #define FIRMWARE_MAX_LEN 0x4000
781 static int firmwareUpload(struct usbduxsub *usbduxsub,
782 const u8 *firmwareBinary, int sizeFirmware)
784 int ret;
785 uint8_t *fwBuf;
787 if (!firmwareBinary)
788 return 0;
790 if (sizeFirmware > FIRMWARE_MAX_LEN) {
791 dev_err(&usbduxsub->interface->dev,
792 "usbduxsigma firmware binary it too large for FX2.\n");
793 return -ENOMEM;
796 /* we generate a local buffer for the firmware */
797 fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
798 if (!fwBuf) {
799 dev_err(&usbduxsub->interface->dev,
800 "comedi_: mem alloc for firmware failed\n");
801 return -ENOMEM;
804 ret = usbduxsub_stop(usbduxsub);
805 if (ret < 0) {
806 dev_err(&usbduxsub->interface->dev,
807 "comedi_: can not stop firmware\n");
808 kfree(fwBuf);
809 return ret;
812 ret = usbduxsub_upload(usbduxsub, fwBuf, 0, sizeFirmware);
813 if (ret < 0) {
814 dev_err(&usbduxsub->interface->dev,
815 "comedi_: firmware upload failed\n");
816 kfree(fwBuf);
817 return ret;
819 ret = usbduxsub_start(usbduxsub);
820 if (ret < 0) {
821 dev_err(&usbduxsub->interface->dev,
822 "comedi_: can not start firmware\n");
823 kfree(fwBuf);
824 return ret;
826 kfree(fwBuf);
827 return 0;
830 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
832 int i, errFlag;
834 if (!usbduxsub)
835 return -EFAULT;
837 /* Submit all URBs and start the transfer on the bus */
838 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
839 /* in case of a resubmission after an unlink... */
840 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
841 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
842 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
843 usbduxsub->urbIn[i]->status = 0;
844 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
845 dev_dbg(&usbduxsub->interface->dev,
846 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
847 usbduxsub->comedidev->minor, i,
848 (usbduxsub->urbIn[i]->context),
849 (usbduxsub->urbIn[i]->dev),
850 (usbduxsub->urbIn[i]->interval));
851 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
852 if (errFlag) {
853 dev_err(&usbduxsub->interface->dev,
854 "comedi_: ai: usb_submit_urb(%d) error %d\n",
855 i, errFlag);
856 return errFlag;
859 return 0;
862 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
864 int i, errFlag;
866 if (!usbduxsub)
867 return -EFAULT;
869 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
870 dev_dbg(&usbduxsub->interface->dev,
871 "comedi_: submitting out-urb[%d]\n", i);
872 /* in case of a resubmission after an unlink... */
873 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
874 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
875 usbduxsub->urbOut[i]->status = 0;
876 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
877 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
878 if (errFlag) {
879 dev_err(&usbduxsub->interface->dev,
880 "comedi_: ao: usb_submit_urb(%d) error %d\n",
881 i, errFlag);
882 return errFlag;
885 return 0;
888 static int chanToInterval(int nChannels)
890 if (nChannels <= 2)
891 /* 4kHz */
892 return 2;
893 if (nChannels <= 8)
894 /* 2kHz */
895 return 4;
896 /* 1kHz */
897 return 8;
900 static int usbdux_ai_cmdtest(struct comedi_device *dev,
901 struct comedi_subdevice *s,
902 struct comedi_cmd *cmd)
904 int err = 0, tmp, i;
905 unsigned int tmpTimer;
906 struct usbduxsub *this_usbduxsub = dev->private;
908 if (!(this_usbduxsub->probed))
909 return -ENODEV;
911 dev_dbg(&this_usbduxsub->interface->dev,
912 "comedi%d: usbdux_ai_cmdtest\n", dev->minor);
914 /* make sure triggers are valid */
915 /* Only immediate triggers are allowed */
916 tmp = cmd->start_src;
917 cmd->start_src &= TRIG_NOW | TRIG_INT;
918 if (!cmd->start_src || tmp != cmd->start_src)
919 err++;
921 /* trigger should happen timed */
922 tmp = cmd->scan_begin_src;
923 /* start a new _scan_ with a timer */
924 cmd->scan_begin_src &= TRIG_TIMER;
925 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
926 err++;
928 /* scanning is continous */
929 tmp = cmd->convert_src;
930 cmd->convert_src &= TRIG_NOW;
931 if (!cmd->convert_src || tmp != cmd->convert_src)
932 err++;
934 /* issue a trigger when scan is finished and start a new scan */
935 tmp = cmd->scan_end_src;
936 cmd->scan_end_src &= TRIG_COUNT;
937 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
938 err++;
940 /* trigger at the end of count events or not, stop condition or not */
941 tmp = cmd->stop_src;
942 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
943 if (!cmd->stop_src || tmp != cmd->stop_src)
944 err++;
946 if (err)
947 return 1;
950 * step 2: make sure trigger sources are unique and mutually compatible
951 * note that mutual compatibility is not an issue here
953 if (cmd->scan_begin_src != TRIG_FOLLOW &&
954 cmd->scan_begin_src != TRIG_EXT &&
955 cmd->scan_begin_src != TRIG_TIMER)
956 err++;
957 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
958 err++;
960 if (err)
961 return 2;
963 /* step 3: make sure arguments are trivially compatible */
964 if (cmd->start_arg != 0) {
965 cmd->start_arg = 0;
966 err++;
969 if (cmd->scan_begin_src == TRIG_FOLLOW) {
970 /* internal trigger */
971 if (cmd->scan_begin_arg != 0) {
972 cmd->scan_begin_arg = 0;
973 err++;
977 if (cmd->scan_begin_src == TRIG_TIMER) {
978 if (this_usbduxsub->high_speed) {
980 * In high speed mode microframes are possible.
981 * However, during one microframe we can roughly
982 * sample two channels. Thus, the more channels
983 * are in the channel list the more time we need.
985 i = chanToInterval(cmd->chanlist_len);
986 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
987 cmd->scan_begin_arg = 1000000 / 8 * i;
988 err++;
990 /* now calc the real sampling rate with all the
991 * rounding errors */
992 tmpTimer =
993 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
994 125000;
995 if (cmd->scan_begin_arg != tmpTimer) {
996 cmd->scan_begin_arg = tmpTimer;
997 err++;
999 } else {
1000 /* full speed */
1001 /* 1kHz scans every USB frame */
1002 if (cmd->scan_begin_arg < 1000000) {
1003 cmd->scan_begin_arg = 1000000;
1004 err++;
1007 * calc the real sampling rate with the rounding errors
1009 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
1010 1000000)) * 1000000;
1011 if (cmd->scan_begin_arg != tmpTimer) {
1012 cmd->scan_begin_arg = tmpTimer;
1013 err++;
1017 /* the same argument */
1018 if (cmd->scan_end_arg != cmd->chanlist_len) {
1019 cmd->scan_end_arg = cmd->chanlist_len;
1020 err++;
1023 if (cmd->stop_src == TRIG_COUNT) {
1024 /* any count is allowed */
1025 } else {
1026 /* TRIG_NONE */
1027 if (cmd->stop_arg != 0) {
1028 cmd->stop_arg = 0;
1029 err++;
1033 if (err)
1034 return 3;
1036 return 0;
1040 * creates the ADC command for the MAX1271
1041 * range is the range value from comedi
1043 static void create_adc_command(unsigned int chan,
1044 uint8_t *muxsg0,
1045 uint8_t *muxsg1)
1047 if (chan < 8)
1048 (*muxsg0) = (*muxsg0) | (1 << chan);
1049 else if (chan < 16)
1050 (*muxsg1) = (*muxsg1) | (1 << (chan-8));
1054 /* bulk transfers to usbdux */
1056 #define SENDADCOMMANDS 0
1057 #define SENDDACOMMANDS 1
1058 #define SENDDIOCONFIGCOMMAND 2
1059 #define SENDDIOBITSCOMMAND 3
1060 #define SENDSINGLEAD 4
1061 #define SENDPWMON 7
1062 #define SENDPWMOFF 8
1064 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1066 int result, nsent;
1068 this_usbduxsub->dux_commands[0] = cmd_type;
1069 #ifdef NOISY_DUX_DEBUGBUG
1070 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1071 this_usbduxsub->comedidev->minor);
1072 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1073 printk(" %02x", this_usbduxsub->dux_commands[result]);
1074 printk("\n");
1075 #endif
1076 result = usb_bulk_msg(this_usbduxsub->usbdev,
1077 usb_sndbulkpipe(this_usbduxsub->usbdev,
1078 COMMAND_OUT_EP),
1079 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1080 &nsent, BULK_TIMEOUT);
1081 if (result < 0)
1082 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1083 "could not transmit dux_command to the usb-device, "
1084 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1086 return result;
1089 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1091 int result = (-EFAULT);
1092 int nrec;
1093 int i;
1095 for (i = 0; i < RETRIES; i++) {
1096 result = usb_bulk_msg(this_usbduxsub->usbdev,
1097 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1098 COMMAND_IN_EP),
1099 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1100 &nrec, BULK_TIMEOUT);
1101 if (result < 0) {
1102 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1103 "insn: USB error %d "
1104 "while receiving DUX command"
1105 "\n", this_usbduxsub->comedidev->minor,
1106 result);
1107 return result;
1109 if (this_usbduxsub->insnBuffer[0] == command)
1110 return result;
1112 /* this is only reached if the data has been requested a couple of
1113 * times */
1114 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1115 "wrong data returned from firmware: want %d, got %d.\n",
1116 this_usbduxsub->comedidev->minor, command,
1117 this_usbduxsub->insnBuffer[0]);
1118 return -EFAULT;
1121 static int usbdux_ai_inttrig(struct comedi_device *dev,
1122 struct comedi_subdevice *s, unsigned int trignum)
1124 int ret;
1125 struct usbduxsub *this_usbduxsub = dev->private;
1126 if (!this_usbduxsub)
1127 return -EFAULT;
1129 down(&this_usbduxsub->sem);
1130 if (!(this_usbduxsub->probed)) {
1131 up(&this_usbduxsub->sem);
1132 return -ENODEV;
1134 dev_dbg(&this_usbduxsub->interface->dev,
1135 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1137 if (trignum != 0) {
1138 dev_err(&this_usbduxsub->interface->dev,
1139 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1140 dev->minor);
1141 up(&this_usbduxsub->sem);
1142 return -EINVAL;
1144 if (!(this_usbduxsub->ai_cmd_running)) {
1145 this_usbduxsub->ai_cmd_running = 1;
1146 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1147 if (ret < 0) {
1148 dev_err(&this_usbduxsub->interface->dev,
1149 "comedi%d: usbdux_ai_inttrig: "
1150 "urbSubmit: err=%d\n", dev->minor, ret);
1151 this_usbduxsub->ai_cmd_running = 0;
1152 up(&this_usbduxsub->sem);
1153 return ret;
1155 s->async->inttrig = NULL;
1156 } else {
1157 dev_err(&this_usbduxsub->interface->dev,
1158 "comedi%d: ai_inttrig but acqu is already running\n",
1159 dev->minor);
1161 up(&this_usbduxsub->sem);
1162 return 1;
1165 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1167 struct comedi_cmd *cmd = &s->async->cmd;
1168 unsigned int chan;
1169 int i, ret;
1170 struct usbduxsub *this_usbduxsub = dev->private;
1171 int result;
1172 uint8_t muxsg0 = 0;
1173 uint8_t muxsg1 = 0;
1174 uint8_t sysred = 0;
1176 if (!this_usbduxsub)
1177 return -EFAULT;
1179 dev_dbg(&this_usbduxsub->interface->dev,
1180 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1182 /* block other CPUs from starting an ai_cmd */
1183 down(&this_usbduxsub->sem);
1185 if (!(this_usbduxsub->probed)) {
1186 up(&this_usbduxsub->sem);
1187 return -ENODEV;
1189 if (this_usbduxsub->ai_cmd_running) {
1190 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1191 "ai_cmd not possible. Another ai_cmd is running.\n",
1192 dev->minor);
1193 up(&this_usbduxsub->sem);
1194 return -EBUSY;
1196 /* set current channel of the running aquisition to zero */
1197 s->async->cur_chan = 0;
1199 /* first the number of channels per time step */
1200 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1202 /* CONFIG0 */
1203 this_usbduxsub->dux_commands[2] = 0x12;
1205 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
1206 this_usbduxsub->dux_commands[3] = 0x03;
1208 /* CONFIG3: differential channels off */
1209 this_usbduxsub->dux_commands[4] = 0x00;
1211 for (i = 0; i < cmd->chanlist_len; i++) {
1212 chan = CR_CHAN(cmd->chanlist[i]);
1213 create_adc_command(chan, &muxsg0, &muxsg1);
1214 if (i >= NUMCHANNELS) {
1215 dev_err(&this_usbduxsub->interface->dev,
1216 "comedi%d: channel list too long\n",
1217 dev->minor);
1218 break;
1221 this_usbduxsub->dux_commands[5] = muxsg0;
1222 this_usbduxsub->dux_commands[6] = muxsg1;
1223 this_usbduxsub->dux_commands[7] = sysred;
1225 dev_dbg(&this_usbduxsub->interface->dev,
1226 "comedi %d: sending commands to the usb device: size=%u\n",
1227 dev->minor, NUMCHANNELS);
1229 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1230 if (result < 0) {
1231 up(&this_usbduxsub->sem);
1232 return result;
1235 if (this_usbduxsub->high_speed) {
1237 * every 2 channels get a time window of 125us. Thus, if we
1238 * sample all 16 channels we need 1ms. If we sample only one
1239 * channel we need only 125us
1241 this_usbduxsub->ai_interval =
1242 chanToInterval(cmd->chanlist_len);
1243 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1244 (this_usbduxsub->
1245 ai_interval));
1246 } else {
1247 /* interval always 1ms */
1248 this_usbduxsub->ai_interval = 1;
1249 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1251 if (this_usbduxsub->ai_timer < 1) {
1252 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1253 "timer=%d, scan_begin_arg=%d. "
1254 "Not properly tested by cmdtest?\n", dev->minor,
1255 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1256 up(&this_usbduxsub->sem);
1257 return -EINVAL;
1259 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1261 if (cmd->stop_src == TRIG_COUNT) {
1262 /* data arrives as one packet */
1263 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1264 this_usbduxsub->ai_continous = 0;
1265 } else {
1266 /* continous aquisition */
1267 this_usbduxsub->ai_continous = 1;
1268 this_usbduxsub->ai_sample_count = 0;
1271 if (cmd->start_src == TRIG_NOW) {
1272 /* enable this acquisition operation */
1273 this_usbduxsub->ai_cmd_running = 1;
1274 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1275 if (ret < 0) {
1276 this_usbduxsub->ai_cmd_running = 0;
1277 /* fixme: unlink here?? */
1278 up(&this_usbduxsub->sem);
1279 return ret;
1281 s->async->inttrig = NULL;
1282 } else {
1283 /* TRIG_INT */
1284 /* don't enable the acquision operation */
1285 /* wait for an internal signal */
1286 s->async->inttrig = usbdux_ai_inttrig;
1288 up(&this_usbduxsub->sem);
1289 return 0;
1292 /* Mode 0 is used to get a single conversion on demand */
1293 static int usbdux_ai_insn_read(struct comedi_device *dev,
1294 struct comedi_subdevice *s,
1295 struct comedi_insn *insn, unsigned int *data)
1297 int i;
1298 int32_t one = 0;
1299 int chan;
1300 int err;
1301 struct usbduxsub *this_usbduxsub = dev->private;
1302 uint8_t muxsg0 = 0;
1303 uint8_t muxsg1 = 0;
1304 uint8_t sysred = 0;
1306 if (!this_usbduxsub)
1307 return 0;
1309 dev_dbg(&this_usbduxsub->interface->dev,
1310 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1311 dev->minor, insn->n, insn->subdev);
1313 down(&this_usbduxsub->sem);
1314 if (!(this_usbduxsub->probed)) {
1315 up(&this_usbduxsub->sem);
1316 return -ENODEV;
1318 if (this_usbduxsub->ai_cmd_running) {
1319 dev_err(&this_usbduxsub->interface->dev,
1320 "comedi%d: ai_insn_read not possible. "
1321 "Async Command is running.\n", dev->minor);
1322 up(&this_usbduxsub->sem);
1323 return 0;
1326 /* sample one channel */
1327 /* CONFIG0: chopper on */
1328 this_usbduxsub->dux_commands[1] = 0x16;
1330 /* CONFIG1: 2kHz sampling rate */
1331 this_usbduxsub->dux_commands[2] = 0x80;
1333 /* CONFIG3: differential channels off */
1334 this_usbduxsub->dux_commands[3] = 0x00;
1336 chan = CR_CHAN(insn->chanspec);
1337 create_adc_command(chan, &muxsg0, &muxsg1);
1339 this_usbduxsub->dux_commands[4] = muxsg0;
1340 this_usbduxsub->dux_commands[5] = muxsg1;
1341 this_usbduxsub->dux_commands[6] = sysred;
1343 /* adc commands */
1344 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1345 if (err < 0) {
1346 up(&this_usbduxsub->sem);
1347 return err;
1350 for (i = 0; i < insn->n; i++) {
1351 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1352 if (err < 0) {
1353 up(&this_usbduxsub->sem);
1354 return 0;
1356 /* 32 bits big endian from the A/D converter */
1357 one = be32_to_cpu(*((int32_t *)
1358 ((this_usbduxsub->insnBuffer)+1)));
1359 /* mask out the staus byte */
1360 one = one & 0x00ffffff;
1361 /* turn it into an unsigned integer */
1362 one = one ^ 0x00800000;
1363 data[i] = one;
1365 up(&this_usbduxsub->sem);
1366 return i;
1372 static int usbdux_getstatusinfo(struct comedi_device *dev, int chan)
1374 struct usbduxsub *this_usbduxsub = dev->private;
1375 uint8_t sysred = 0;
1376 uint32_t one;
1377 int err;
1379 if (!this_usbduxsub)
1380 return 0;
1382 if (this_usbduxsub->ai_cmd_running) {
1383 dev_err(&this_usbduxsub->interface->dev,
1384 "comedi%d: status read not possible. "
1385 "Async Command is running.\n", dev->minor);
1386 return 0;
1389 /* CONFIG0 */
1390 this_usbduxsub->dux_commands[1] = 0x12;
1392 /* CONFIG1: 2kHz sampling rate */
1393 this_usbduxsub->dux_commands[2] = 0x80;
1395 /* CONFIG3: differential channels off */
1396 this_usbduxsub->dux_commands[3] = 0x00;
1398 if (chan == 1) {
1399 /* ADC offset */
1400 sysred = sysred | 1;
1401 } else if (chan == 2) {
1402 /* VCC */
1403 sysred = sysred | 4;
1404 } else if (chan == 3) {
1405 /* temperature */
1406 sysred = sysred | 8;
1407 } else if (chan == 4) {
1408 /* gain */
1409 sysred = sysred | 16;
1410 } else if (chan == 5) {
1411 /* ref */
1412 sysred = sysred | 32;
1415 this_usbduxsub->dux_commands[4] = 0;
1416 this_usbduxsub->dux_commands[5] = 0;
1417 this_usbduxsub->dux_commands[6] = sysred;
1419 /* adc commands */
1420 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1421 if (err < 0)
1422 return err;
1424 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1425 if (err < 0)
1426 return err;
1428 /* 32 bits big endian from the A/D converter */
1429 one = be32_to_cpu(*((int32_t *)((this_usbduxsub->insnBuffer)+1)));
1430 /* mask out the staus byte */
1431 one = one & 0x00ffffff;
1432 one = one ^ 0x00800000;
1434 return (int)one;
1442 /************************************/
1443 /* analog out */
1445 static int usbdux_ao_insn_read(struct comedi_device *dev,
1446 struct comedi_subdevice *s,
1447 struct comedi_insn *insn, unsigned int *data)
1449 int i;
1450 int chan = CR_CHAN(insn->chanspec);
1451 struct usbduxsub *this_usbduxsub = dev->private;
1453 if (!this_usbduxsub)
1454 return -EFAULT;
1456 down(&this_usbduxsub->sem);
1457 if (!(this_usbduxsub->probed)) {
1458 up(&this_usbduxsub->sem);
1459 return -ENODEV;
1461 for (i = 0; i < insn->n; i++)
1462 data[i] = this_usbduxsub->outBuffer[chan];
1464 up(&this_usbduxsub->sem);
1465 return i;
1468 static int usbdux_ao_insn_write(struct comedi_device *dev,
1469 struct comedi_subdevice *s,
1470 struct comedi_insn *insn, unsigned int *data)
1472 int i, err;
1473 int chan = CR_CHAN(insn->chanspec);
1474 struct usbduxsub *this_usbduxsub = dev->private;
1476 if (!this_usbduxsub)
1477 return -EFAULT;
1479 dev_dbg(&this_usbduxsub->interface->dev,
1480 "comedi%d: ao_insn_write\n", dev->minor);
1482 down(&this_usbduxsub->sem);
1483 if (!(this_usbduxsub->probed)) {
1484 up(&this_usbduxsub->sem);
1485 return -ENODEV;
1487 if (this_usbduxsub->ao_cmd_running) {
1488 dev_err(&this_usbduxsub->interface->dev,
1489 "comedi%d: ao_insn_write: "
1490 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1491 up(&this_usbduxsub->sem);
1492 return 0;
1495 for (i = 0; i < insn->n; i++) {
1496 dev_dbg(&this_usbduxsub->interface->dev,
1497 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1498 dev->minor, chan, i, data[i]);
1500 /* number of channels: 1 */
1501 this_usbduxsub->dux_commands[1] = 1;
1502 /* channel number */
1503 this_usbduxsub->dux_commands[2] = data[i];
1504 this_usbduxsub->outBuffer[chan] = data[i];
1505 this_usbduxsub->dux_commands[3] = chan;
1506 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1507 if (err < 0) {
1508 up(&this_usbduxsub->sem);
1509 return err;
1512 up(&this_usbduxsub->sem);
1514 return i;
1517 static int usbdux_ao_inttrig(struct comedi_device *dev,
1518 struct comedi_subdevice *s, unsigned int trignum)
1520 int ret;
1521 struct usbduxsub *this_usbduxsub = dev->private;
1523 if (!this_usbduxsub)
1524 return -EFAULT;
1526 down(&this_usbduxsub->sem);
1527 if (!(this_usbduxsub->probed)) {
1528 up(&this_usbduxsub->sem);
1529 return -ENODEV;
1531 if (trignum != 0) {
1532 dev_err(&this_usbduxsub->interface->dev,
1533 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1534 dev->minor);
1535 return -EINVAL;
1537 if (!(this_usbduxsub->ao_cmd_running)) {
1538 this_usbduxsub->ao_cmd_running = 1;
1539 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1540 if (ret < 0) {
1541 dev_err(&this_usbduxsub->interface->dev,
1542 "comedi%d: usbdux_ao_inttrig: submitURB: "
1543 "err=%d\n", dev->minor, ret);
1544 this_usbduxsub->ao_cmd_running = 0;
1545 up(&this_usbduxsub->sem);
1546 return ret;
1548 s->async->inttrig = NULL;
1549 } else {
1550 dev_err(&this_usbduxsub->interface->dev,
1551 "comedi%d: ao_inttrig but acqu is already running.\n",
1552 dev->minor);
1554 up(&this_usbduxsub->sem);
1555 return 1;
1558 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1559 struct comedi_subdevice *s,
1560 struct comedi_cmd *cmd)
1562 int err = 0, tmp;
1563 struct usbduxsub *this_usbduxsub = dev->private;
1565 if (!this_usbduxsub)
1566 return -EFAULT;
1568 if (!(this_usbduxsub->probed))
1569 return -ENODEV;
1571 dev_dbg(&this_usbduxsub->interface->dev,
1572 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1574 /* make sure triggers are valid */
1575 /* Only immediate triggers are allowed */
1576 tmp = cmd->start_src;
1577 cmd->start_src &= TRIG_NOW | TRIG_INT;
1578 if (!cmd->start_src || tmp != cmd->start_src)
1579 err++;
1581 /* trigger should happen timed */
1582 tmp = cmd->scan_begin_src;
1583 /* just now we scan also in the high speed mode every frame */
1584 /* this is due to ehci driver limitations */
1585 if (0) { /* (this_usbduxsub->high_speed) */
1586 /* start immidiately a new scan */
1587 /* the sampling rate is set by the coversion rate */
1588 cmd->scan_begin_src &= TRIG_FOLLOW;
1589 } else {
1590 /* start a new scan (output at once) with a timer */
1591 cmd->scan_begin_src &= TRIG_TIMER;
1593 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1594 err++;
1596 /* scanning is continous */
1597 tmp = cmd->convert_src;
1599 /* all conversion events happen simultaneously */
1600 cmd->convert_src &= TRIG_NOW;
1602 if (!cmd->convert_src || tmp != cmd->convert_src)
1603 err++;
1605 /* issue a trigger when scan is finished and start a new scan */
1606 tmp = cmd->scan_end_src;
1607 cmd->scan_end_src &= TRIG_COUNT;
1608 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1609 err++;
1611 /* trigger at the end of count events or not, stop condition or not */
1612 tmp = cmd->stop_src;
1613 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1614 if (!cmd->stop_src || tmp != cmd->stop_src)
1615 err++;
1617 if (err)
1618 return 1;
1621 * step 2: make sure trigger sources
1622 * are unique and mutually compatible
1623 * note that mutual compatibility is not an issue here
1625 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1626 cmd->scan_begin_src != TRIG_EXT &&
1627 cmd->scan_begin_src != TRIG_TIMER)
1628 err++;
1629 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1630 err++;
1632 if (err)
1633 return 2;
1635 /* step 3: make sure arguments are trivially compatible */
1637 if (cmd->start_arg != 0) {
1638 cmd->start_arg = 0;
1639 err++;
1642 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1643 /* internal trigger */
1644 if (cmd->scan_begin_arg != 0) {
1645 cmd->scan_begin_arg = 0;
1646 err++;
1650 if (cmd->scan_begin_src == TRIG_TIMER) {
1651 /* timer */
1652 if (cmd->scan_begin_arg < 1000000) {
1653 cmd->scan_begin_arg = 1000000;
1654 err++;
1657 /* not used now, is for later use */
1658 if (cmd->convert_src == TRIG_TIMER) {
1659 if (cmd->convert_arg < 125000) {
1660 cmd->convert_arg = 125000;
1661 err++;
1665 /* the same argument */
1666 if (cmd->scan_end_arg != cmd->chanlist_len) {
1667 cmd->scan_end_arg = cmd->chanlist_len;
1668 err++;
1671 if (cmd->stop_src == TRIG_COUNT) {
1672 /* any count is allowed */
1673 } else {
1674 /* TRIG_NONE */
1675 if (cmd->stop_arg != 0) {
1676 cmd->stop_arg = 0;
1677 err++;
1681 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: err=%d, "
1682 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1683 "convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src,
1684 cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1686 if (err)
1687 return 3;
1689 return 0;
1692 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1694 struct comedi_cmd *cmd = &s->async->cmd;
1695 unsigned int chan, gain;
1696 int i, ret;
1697 struct usbduxsub *this_usbduxsub = dev->private;
1699 if (!this_usbduxsub)
1700 return -EFAULT;
1702 down(&this_usbduxsub->sem);
1703 if (!(this_usbduxsub->probed)) {
1704 up(&this_usbduxsub->sem);
1705 return -ENODEV;
1707 dev_dbg(&this_usbduxsub->interface->dev,
1708 "comedi%d: %s\n", dev->minor, __func__);
1710 /* set current channel of the running aquisition to zero */
1711 s->async->cur_chan = 0;
1712 for (i = 0; i < cmd->chanlist_len; ++i) {
1713 chan = CR_CHAN(cmd->chanlist[i]);
1714 gain = CR_RANGE(cmd->chanlist[i]);
1715 if (i >= NUMOUTCHANNELS) {
1716 dev_err(&this_usbduxsub->interface->dev,
1717 "comedi%d: %s: channel list too long\n",
1718 dev->minor, __func__);
1719 break;
1721 this_usbduxsub->dac_commands[i] = chan;
1722 dev_dbg(&this_usbduxsub->interface->dev,
1723 "comedi%d: dac command for ch %d is %x\n",
1724 dev->minor, i, this_usbduxsub->dac_commands[i]);
1727 /* we count in steps of 1ms (125us) */
1728 /* 125us mode not used yet */
1729 if (0) { /* (this_usbduxsub->high_speed) */
1730 /* 125us */
1731 /* timing of the conversion itself: every 125 us */
1732 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1733 } else {
1734 /* 1ms */
1735 /* timing of the scan: we get all channels at once */
1736 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1737 dev_dbg(&this_usbduxsub->interface->dev,
1738 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1739 "convert_src=%d, convert_arg=%d\n", dev->minor,
1740 cmd->scan_begin_src, cmd->scan_begin_arg,
1741 cmd->convert_src, cmd->convert_arg);
1742 dev_dbg(&this_usbduxsub->interface->dev,
1743 "comedi%d: ao_timer=%d (ms)\n",
1744 dev->minor, this_usbduxsub->ao_timer);
1745 if (this_usbduxsub->ao_timer < 1) {
1746 dev_err(&this_usbduxsub->interface->dev,
1747 "comedi%d: usbdux: ao_timer=%d, "
1748 "scan_begin_arg=%d. "
1749 "Not properly tested by cmdtest?\n",
1750 dev->minor, this_usbduxsub->ao_timer,
1751 cmd->scan_begin_arg);
1752 up(&this_usbduxsub->sem);
1753 return -EINVAL;
1756 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1758 if (cmd->stop_src == TRIG_COUNT) {
1759 /* not continous */
1760 /* counter */
1761 /* high speed also scans everything at once */
1762 if (0) { /* (this_usbduxsub->high_speed) */
1763 this_usbduxsub->ao_sample_count =
1764 (cmd->stop_arg) * (cmd->scan_end_arg);
1765 } else {
1766 /* there's no scan as the scan has been */
1767 /* perf inside the FX2 */
1768 /* data arrives as one packet */
1769 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1771 this_usbduxsub->ao_continous = 0;
1772 } else {
1773 /* continous aquisition */
1774 this_usbduxsub->ao_continous = 1;
1775 this_usbduxsub->ao_sample_count = 0;
1778 if (cmd->start_src == TRIG_NOW) {
1779 /* enable this acquisition operation */
1780 this_usbduxsub->ao_cmd_running = 1;
1781 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1782 if (ret < 0) {
1783 this_usbduxsub->ao_cmd_running = 0;
1784 /* fixme: unlink here?? */
1785 up(&this_usbduxsub->sem);
1786 return ret;
1788 s->async->inttrig = NULL;
1789 } else {
1790 /* TRIG_INT */
1791 /* submit the urbs later */
1792 /* wait for an internal signal */
1793 s->async->inttrig = usbdux_ao_inttrig;
1796 up(&this_usbduxsub->sem);
1797 return 0;
1800 static int usbdux_dio_insn_config(struct comedi_device *dev,
1801 struct comedi_subdevice *s,
1802 struct comedi_insn *insn, unsigned int *data)
1804 int chan = CR_CHAN(insn->chanspec);
1806 /* The input or output configuration of each digital line is
1807 * configured by a special insn_config instruction. chanspec
1808 * contains the channel to be changed, and data[0] contains the
1809 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1811 switch (data[0]) {
1812 case INSN_CONFIG_DIO_OUTPUT:
1813 s->io_bits |= 1 << chan; /* 1 means Out */
1814 break;
1815 case INSN_CONFIG_DIO_INPUT:
1816 s->io_bits &= ~(1 << chan);
1817 break;
1818 case INSN_CONFIG_DIO_QUERY:
1819 data[1] =
1820 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1821 break;
1822 default:
1823 return -EINVAL;
1824 break;
1826 /* we don't tell the firmware here as it would take 8 frames */
1827 /* to submit the information. We do it in the insn_bits. */
1828 return insn->n;
1831 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1832 struct comedi_subdevice *s,
1833 struct comedi_insn *insn,
1834 unsigned int *data)
1837 struct usbduxsub *this_usbduxsub = dev->private;
1838 int err;
1840 if (!this_usbduxsub)
1841 return -EFAULT;
1843 if (insn->n != 2)
1844 return -EINVAL;
1846 down(&this_usbduxsub->sem);
1848 if (!(this_usbduxsub->probed)) {
1849 up(&this_usbduxsub->sem);
1850 return -ENODEV;
1853 /* The insn data is a mask in data[0] and the new data
1854 * in data[1], each channel cooresponding to a bit. */
1855 s->state &= ~data[0];
1856 s->state |= data[0] & data[1];
1857 /* The commands are 8 bits wide */
1858 this_usbduxsub->dux_commands[1] = (s->io_bits) & 0x000000FF;
1859 this_usbduxsub->dux_commands[4] = (s->state) & 0x000000FF;
1860 this_usbduxsub->dux_commands[2] = ((s->io_bits) & 0x0000FF00) >> 8;
1861 this_usbduxsub->dux_commands[5] = ((s->state) & 0x0000FF00) >> 8;
1862 this_usbduxsub->dux_commands[3] = ((s->io_bits) & 0x00FF0000) >> 16;
1863 this_usbduxsub->dux_commands[6] = ((s->state) & 0x00FF0000) >> 16;
1865 /* This command also tells the firmware to return */
1866 /* the digital input lines */
1867 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1868 if (err < 0) {
1869 up(&this_usbduxsub->sem);
1870 return err;
1872 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1873 if (err < 0) {
1874 up(&this_usbduxsub->sem);
1875 return err;
1878 data[1] = (((unsigned int)(this_usbduxsub->insnBuffer[1]))&0xff) |
1879 ((((unsigned int)(this_usbduxsub->insnBuffer[2]))&0xff) << 8) |
1880 ((((unsigned int)(this_usbduxsub->insnBuffer[3]))&0xff) << 16);
1882 s->state = data[1];
1884 up(&this_usbduxsub->sem);
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_: usbduxsigma: 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] = ((uint8_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,
2207 __func__);
2208 return usbdux_pwm_period(dev, s, data[1]);
2209 case INSN_CONFIG_PWM_GET_PERIOD:
2210 data[1] = this_usbduxsub->pwmPeriod;
2211 return 0;
2212 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2213 /* value in the first byte and the sign in the second for a
2214 relay */
2215 return usbdux_pwm_pattern(dev, s,
2216 /* the channel number */
2217 CR_CHAN(insn->chanspec),
2218 /* actual PWM data */
2219 data[1],
2220 /* just a sign */
2221 (data[2] != 0));
2222 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2223 /* values are not kept in this driver, nothing to return */
2224 return -EINVAL;
2226 return -EINVAL;
2229 /* end of PWM */
2230 /*****************************************************************/
2232 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2234 int i;
2236 if (!usbduxsub_tmp)
2237 return;
2238 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2240 /* shows the usb subsystem that the driver is down */
2241 if (usbduxsub_tmp->interface)
2242 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2244 usbduxsub_tmp->probed = 0;
2246 if (usbduxsub_tmp->urbIn) {
2247 if (usbduxsub_tmp->ai_cmd_running) {
2248 usbduxsub_tmp->ai_cmd_running = 0;
2249 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2251 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2252 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2253 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2254 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2255 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2256 usbduxsub_tmp->urbIn[i] = NULL;
2258 kfree(usbduxsub_tmp->urbIn);
2259 usbduxsub_tmp->urbIn = NULL;
2261 if (usbduxsub_tmp->urbOut) {
2262 if (usbduxsub_tmp->ao_cmd_running) {
2263 usbduxsub_tmp->ao_cmd_running = 0;
2264 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2266 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2267 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2268 kfree(usbduxsub_tmp->
2269 urbOut[i]->transfer_buffer);
2270 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2271 NULL;
2273 if (usbduxsub_tmp->urbOut[i]) {
2274 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2275 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2276 usbduxsub_tmp->urbOut[i] = NULL;
2279 kfree(usbduxsub_tmp->urbOut);
2280 usbduxsub_tmp->urbOut = NULL;
2282 if (usbduxsub_tmp->urbPwm) {
2283 if (usbduxsub_tmp->pwm_cmd_running) {
2284 usbduxsub_tmp->pwm_cmd_running = 0;
2285 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2287 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2288 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2289 usb_kill_urb(usbduxsub_tmp->urbPwm);
2290 usb_free_urb(usbduxsub_tmp->urbPwm);
2291 usbduxsub_tmp->urbPwm = NULL;
2293 kfree(usbduxsub_tmp->inBuffer);
2294 usbduxsub_tmp->inBuffer = NULL;
2295 kfree(usbduxsub_tmp->insnBuffer);
2296 usbduxsub_tmp->insnBuffer = NULL;
2297 kfree(usbduxsub_tmp->outBuffer);
2298 usbduxsub_tmp->outBuffer = NULL;
2299 kfree(usbduxsub_tmp->dac_commands);
2300 usbduxsub_tmp->dac_commands = NULL;
2301 kfree(usbduxsub_tmp->dux_commands);
2302 usbduxsub_tmp->dux_commands = NULL;
2303 usbduxsub_tmp->ai_cmd_running = 0;
2304 usbduxsub_tmp->ao_cmd_running = 0;
2305 usbduxsub_tmp->pwm_cmd_running = 0;
2308 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2309 void *context)
2311 struct usbduxsub *usbduxsub_tmp = context;
2312 struct usb_device *usbdev = usbduxsub_tmp->usbdev;
2313 int ret;
2315 if (fw == NULL) {
2316 dev_err(&usbdev->dev,
2317 "Firmware complete handler without firmware!\n");
2318 return;
2322 * we need to upload the firmware here because fw will be
2323 * freed once we've left this function
2325 ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2327 if (ret) {
2328 dev_err(&usbdev->dev,
2329 "Could not upload firmware (err=%d)\n", ret);
2330 goto out;
2332 comedi_usb_auto_config(usbdev, BOARDNAME);
2333 out:
2334 release_firmware(fw);
2337 /* allocate memory for the urbs and initialise them */
2338 static int usbduxsigma_probe(struct usb_interface *uinterf,
2339 const struct usb_device_id *id)
2341 struct usb_device *udev = interface_to_usbdev(uinterf);
2342 struct device *dev = &uinterf->dev;
2343 int i;
2344 int index;
2345 int ret;
2347 dev_dbg(dev, "comedi_: usbdux_: "
2348 "finding a free structure for the usb-device\n");
2350 down(&start_stop_sem);
2351 /* look for a free place in the usbdux array */
2352 index = -1;
2353 for (i = 0; i < NUMUSBDUX; i++) {
2354 if (!(usbduxsub[i].probed)) {
2355 index = i;
2356 break;
2360 /* no more space */
2361 if (index == -1) {
2362 dev_err(dev, "Too many usbduxsigma-devices connected.\n");
2363 up(&start_stop_sem);
2364 return -EMFILE;
2366 dev_dbg(dev, "comedi_: usbdux: "
2367 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2369 sema_init(&(usbduxsub[index].sem), 1);
2370 /* save a pointer to the usb device */
2371 usbduxsub[index].usbdev = udev;
2373 /* save the interface itself */
2374 usbduxsub[index].interface = uinterf;
2375 /* get the interface number from the interface */
2376 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2377 /* hand the private data over to the usb subsystem */
2378 /* will be needed for disconnect */
2379 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2381 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2383 /* test if it is high speed (USB 2.0) */
2384 usbduxsub[index].high_speed =
2385 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2387 /* create space for the commands of the DA converter */
2388 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2389 if (!usbduxsub[index].dac_commands) {
2390 dev_err(dev, "comedi_: usbduxsigma: "
2391 "error alloc space for dac commands\n");
2392 tidy_up(&(usbduxsub[index]));
2393 up(&start_stop_sem);
2394 return -ENOMEM;
2396 /* create space for the commands going to the usb device */
2397 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2398 if (!usbduxsub[index].dux_commands) {
2399 dev_err(dev, "comedi_: usbduxsigma: "
2400 "error alloc space for dux commands\n");
2401 tidy_up(&(usbduxsub[index]));
2402 up(&start_stop_sem);
2403 return -ENOMEM;
2405 /* create space for the in buffer and set it to zero */
2406 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2407 if (!(usbduxsub[index].inBuffer)) {
2408 dev_err(dev, "comedi_: usbduxsigma: "
2409 "could not alloc space for inBuffer\n");
2410 tidy_up(&(usbduxsub[index]));
2411 up(&start_stop_sem);
2412 return -ENOMEM;
2414 /* create space of the instruction buffer */
2415 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2416 if (!(usbduxsub[index].insnBuffer)) {
2417 dev_err(dev, "comedi_: usbduxsigma: "
2418 "could not alloc space for insnBuffer\n");
2419 tidy_up(&(usbduxsub[index]));
2420 up(&start_stop_sem);
2421 return -ENOMEM;
2423 /* create space for the outbuffer */
2424 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2425 if (!(usbduxsub[index].outBuffer)) {
2426 dev_err(dev, "comedi_: usbduxsigma: "
2427 "could not alloc space for outBuffer\n");
2428 tidy_up(&(usbduxsub[index]));
2429 up(&start_stop_sem);
2430 return -ENOMEM;
2432 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2433 i = usb_set_interface(usbduxsub[index].usbdev,
2434 usbduxsub[index].ifnum, 3);
2435 if (i < 0) {
2436 dev_err(dev, "comedi_: usbduxsigma%d: "
2437 "could not set alternate setting 3 in high speed.\n",
2438 index);
2439 tidy_up(&(usbduxsub[index]));
2440 up(&start_stop_sem);
2441 return -ENODEV;
2443 if (usbduxsub[index].high_speed)
2444 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2445 else
2446 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2448 usbduxsub[index].urbIn =
2449 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2450 GFP_KERNEL);
2451 if (!(usbduxsub[index].urbIn)) {
2452 dev_err(dev, "comedi_: usbduxsigma: "
2453 "Could not alloc. urbIn array\n");
2454 tidy_up(&(usbduxsub[index]));
2455 up(&start_stop_sem);
2456 return -ENOMEM;
2458 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2459 /* one frame: 1ms */
2460 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2461 if (usbduxsub[index].urbIn[i] == NULL) {
2462 dev_err(dev, "comedi_: usbduxsigma%d: "
2463 "Could not alloc. urb(%d)\n", index, i);
2464 tidy_up(&(usbduxsub[index]));
2465 up(&start_stop_sem);
2466 return -ENOMEM;
2468 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2469 /* will be filled later with a pointer to the comedi-device */
2470 /* and ONLY then the urb should be submitted */
2471 usbduxsub[index].urbIn[i]->context = NULL;
2472 usbduxsub[index].urbIn[i]->pipe =
2473 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2474 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2475 usbduxsub[index].urbIn[i]->transfer_buffer =
2476 kzalloc(SIZEINBUF, GFP_KERNEL);
2477 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2478 dev_err(dev, "comedi_: usbduxsigma%d: "
2479 "could not alloc. transb.\n", index);
2480 tidy_up(&(usbduxsub[index]));
2481 up(&start_stop_sem);
2482 return -ENOMEM;
2484 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2485 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2486 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2487 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2488 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length =
2489 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_: usbduxsigma: "
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_: usbduxsigma%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_: usbduxsigma%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 =
2537 SIZEOUTBUF;
2538 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2539 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2540 SIZEOUTBUF;
2541 if (usbduxsub[index].high_speed) {
2542 /* uframes */
2543 usbduxsub[index].urbOut[i]->interval = 8;
2544 } else {
2545 /* frames */
2546 usbduxsub[index].urbOut[i]->interval = 1;
2550 /* pwm */
2551 if (usbduxsub[index].high_speed) {
2552 /* max bulk ep size in high speed */
2553 usbduxsub[index].sizePwmBuf = 512;
2554 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2555 if (usbduxsub[index].urbPwm == NULL) {
2556 dev_err(dev, "comedi_: usbduxsigma%d: "
2557 "Could not alloc. pwm urb\n", index);
2558 tidy_up(&(usbduxsub[index]));
2559 up(&start_stop_sem);
2560 return -ENOMEM;
2562 usbduxsub[index].urbPwm->transfer_buffer =
2563 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2564 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2565 dev_err(dev, "comedi_: usbduxsigma%d: "
2566 "could not alloc. transb. for pwm\n", index);
2567 tidy_up(&(usbduxsub[index]));
2568 up(&start_stop_sem);
2569 return -ENOMEM;
2571 } else {
2572 usbduxsub[index].urbPwm = NULL;
2573 usbduxsub[index].sizePwmBuf = 0;
2576 usbduxsub[index].ai_cmd_running = 0;
2577 usbduxsub[index].ao_cmd_running = 0;
2578 usbduxsub[index].pwm_cmd_running = 0;
2580 /* we've reached the bottom of the function */
2581 usbduxsub[index].probed = 1;
2582 up(&start_stop_sem);
2584 ret = request_firmware_nowait(THIS_MODULE,
2585 FW_ACTION_HOTPLUG,
2586 "usbduxsigma_firmware.bin",
2587 &udev->dev,
2588 GFP_KERNEL,
2589 usbduxsub + index,
2590 usbdux_firmware_request_complete_handler
2593 if (ret) {
2594 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2595 return ret;
2598 dev_info(dev, "comedi_: successfully initialised.\n");
2599 /* success */
2600 return 0;
2603 static void usbduxsigma_disconnect(struct usb_interface *intf)
2605 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2606 struct usb_device *udev = interface_to_usbdev(intf);
2608 if (!usbduxsub_tmp) {
2609 dev_err(&intf->dev,
2610 "comedi_: disconnect called with null pointer.\n");
2611 return;
2613 if (usbduxsub_tmp->usbdev != udev) {
2614 dev_err(&intf->dev, "comedi_: BUG! wrong ptr!\n");
2615 return;
2617 if (usbduxsub_tmp->ai_cmd_running)
2618 /* we are still running a command */
2619 usbdux_ai_stop(usbduxsub_tmp, 1);
2620 if (usbduxsub_tmp->ao_cmd_running)
2621 /* we are still running a command */
2622 usbdux_ao_stop(usbduxsub_tmp, 1);
2623 comedi_usb_auto_unconfig(udev);
2624 down(&start_stop_sem);
2625 down(&usbduxsub_tmp->sem);
2626 tidy_up(usbduxsub_tmp);
2627 up(&usbduxsub_tmp->sem);
2628 up(&start_stop_sem);
2629 dev_info(&intf->dev, "comedi_: disconnected from the usb\n");
2632 /* is called when comedi-config is called */
2633 static int usbduxsigma_attach(struct comedi_device *dev,
2634 struct comedi_devconfig *it)
2636 int ret;
2637 int index;
2638 int i;
2639 struct usbduxsub *udev;
2641 int offset;
2643 struct comedi_subdevice *s = NULL;
2644 dev->private = NULL;
2646 down(&start_stop_sem);
2647 /* find a valid device which has been detected by the probe function of
2648 * the usb */
2649 index = -1;
2650 for (i = 0; i < NUMUSBDUX; i++) {
2651 if ((usbduxsub[i].probed) && (!usbduxsub[i].attached)) {
2652 index = i;
2653 break;
2657 if (index < 0) {
2658 printk(KERN_ERR "comedi%d: usbduxsigma: error: attach failed,"
2659 "dev not connected to the usb bus.\n", dev->minor);
2660 up(&start_stop_sem);
2661 return -ENODEV;
2664 udev = &usbduxsub[index];
2665 down(&udev->sem);
2666 /* pointer back to the corresponding comedi device */
2667 udev->comedidev = dev;
2669 /* trying to upload the firmware into the FX2 */
2670 if (comedi_aux_data(it->options, 0) &&
2671 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
2672 firmwareUpload(udev, comedi_aux_data(it->options, 0),
2673 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
2676 dev->board_name = BOARDNAME;
2678 /* set number of subdevices */
2679 if (udev->high_speed) {
2680 /* with pwm */
2681 dev->n_subdevices = 4;
2682 } else {
2683 /* without pwm */
2684 dev->n_subdevices = 3;
2687 /* allocate space for the subdevices */
2688 ret = alloc_subdevices(dev, dev->n_subdevices);
2689 if (ret < 0) {
2690 dev_err(&udev->interface->dev,
2691 "comedi%d: no space for subdev\n", dev->minor);
2692 up(&start_stop_sem);
2693 return ret;
2696 /* private structure is also simply the usb-structure */
2697 dev->private = udev;
2699 /* the first subdevice is the A/D converter */
2700 s = dev->subdevices + SUBDEV_AD;
2701 /* the URBs get the comedi subdevice */
2702 /* which is responsible for reading */
2703 /* this is the subdevice which reads data */
2704 dev->read_subdev = s;
2705 /* the subdevice receives as private structure the */
2706 /* usb-structure */
2707 s->private = NULL;
2708 /* analog input */
2709 s->type = COMEDI_SUBD_AI;
2710 /* readable and ref is to ground, 32 bit wide data! */
2711 s->subdev_flags = SDF_READABLE | SDF_GROUND |
2712 SDF_CMD_READ | SDF_LSAMPL;
2713 /* 16 A/D channels */
2714 s->n_chan = NUMCHANNELS;
2715 /* length of the channellist */
2716 s->len_chanlist = NUMCHANNELS;
2717 /* callback functions */
2718 s->insn_read = usbdux_ai_insn_read;
2719 s->do_cmdtest = usbdux_ai_cmdtest;
2720 s->do_cmd = usbdux_ai_cmd;
2721 s->cancel = usbdux_ai_cancel;
2722 /* max value from the A/D converter (24bit) */
2723 s->maxdata = 0x00FFFFFF;
2724 /* range table to convert to physical units */
2725 s->range_table = (&range_usbdux_ai_range);
2727 /* analog out */
2728 s = dev->subdevices + SUBDEV_DA;
2729 /* analog out */
2730 s->type = COMEDI_SUBD_AO;
2731 /* backward pointer */
2732 dev->write_subdev = s;
2733 /* the subdevice receives as private structure the */
2734 /* usb-structure */
2735 s->private = NULL;
2736 /* are writable */
2737 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2738 /* 4 channels */
2739 s->n_chan = 4;
2740 /* length of the channellist */
2741 s->len_chanlist = 4;
2742 /* 8 bit resolution */
2743 s->maxdata = 0x00ff;
2744 /* unipolar range */
2745 s->range_table = (&range_usbdux_ao_range);
2746 /* callback */
2747 s->do_cmdtest = usbdux_ao_cmdtest;
2748 s->do_cmd = usbdux_ao_cmd;
2749 s->cancel = usbdux_ao_cancel;
2750 s->insn_read = usbdux_ao_insn_read;
2751 s->insn_write = usbdux_ao_insn_write;
2753 /* digital I/O */
2754 s = dev->subdevices + SUBDEV_DIO;
2755 s->type = COMEDI_SUBD_DIO;
2756 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2757 /* 8 external and 16 internal channels */
2758 s->n_chan = 24;
2759 s->maxdata = 1;
2760 s->range_table = (&range_digital);
2761 s->insn_bits = usbdux_dio_insn_bits;
2762 s->insn_config = usbdux_dio_insn_config;
2763 /* we don't use it */
2764 s->private = NULL;
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 offset = usbdux_getstatusinfo(dev, 0);
2787 if (offset < 0)
2788 dev_err(&udev->interface->dev,
2789 "Communication to USBDUXSIGMA failed!"
2790 "Check firmware and cabling.");
2792 dev_info(&udev->interface->dev,
2793 "comedi%d: attached, ADC_zero = %x", dev->minor, offset);
2795 return 0;
2798 static int usbduxsigma_detach(struct comedi_device *dev)
2800 struct usbduxsub *usbduxsub_tmp;
2802 if (!dev) {
2803 printk(KERN_ERR
2804 "comedi? usbduxsigma detach: dev=NULL\n");
2805 return -EFAULT;
2808 usbduxsub_tmp = dev->private;
2809 if (!usbduxsub_tmp) {
2810 printk(KERN_ERR
2811 "comedi?: usbduxsigma detach: private=NULL\n");
2812 return -EFAULT;
2815 dev_dbg(&usbduxsub_tmp->interface->dev,
2816 "comedi%d: detach usb device\n",
2817 dev->minor);
2819 down(&usbduxsub_tmp->sem);
2820 /* Don't allow detach to free the private structure */
2821 /* It's one entry of of usbduxsub[] */
2822 dev->private = NULL;
2823 usbduxsub_tmp->attached = 0;
2824 usbduxsub_tmp->comedidev = NULL;
2825 dev_info(&usbduxsub_tmp->interface->dev,
2826 "comedi%d: successfully detached.\n", dev->minor);
2827 up(&usbduxsub_tmp->sem);
2828 return 0;
2831 /* main driver struct */
2832 static struct comedi_driver driver_usbduxsigma = {
2833 .driver_name = "usbduxsigma",
2834 .module = THIS_MODULE,
2835 .attach = usbduxsigma_attach,
2836 .detach = usbduxsigma_detach,
2839 /* Table with the USB-devices */
2840 static const struct usb_device_id usbduxsigma_table[] = {
2841 {USB_DEVICE(0x13d8, 0x0020)},
2842 {USB_DEVICE(0x13d8, 0x0021)},
2843 {USB_DEVICE(0x13d8, 0x0022)},
2844 {} /* Terminating entry */
2847 MODULE_DEVICE_TABLE(usb, usbduxsigma_table);
2849 /* The usbduxsub-driver */
2850 static struct usb_driver usbduxsigma_driver = {
2851 .name = BOARDNAME,
2852 .probe = usbduxsigma_probe,
2853 .disconnect = usbduxsigma_disconnect,
2854 .id_table = usbduxsigma_table,
2857 /* Can't use the nice macro as I have also to initialise the USB */
2858 /* subsystem: */
2859 /* registering the usb-system _and_ the comedi-driver */
2860 static int __init init_usbduxsigma(void)
2862 printk(KERN_INFO KBUILD_MODNAME ": "
2863 DRIVER_VERSION ":" DRIVER_DESC "\n");
2864 usb_register(&usbduxsigma_driver);
2865 comedi_driver_register(&driver_usbduxsigma);
2866 return 0;
2869 /* deregistering the comedi driver and the usb-subsystem */
2870 static void __exit exit_usbduxsigma(void)
2872 comedi_driver_unregister(&driver_usbduxsigma);
2873 usb_deregister(&usbduxsigma_driver);
2876 module_init(init_usbduxsigma);
2877 module_exit(exit_usbduxsigma);
2879 MODULE_AUTHOR(DRIVER_AUTHOR);
2880 MODULE_DESCRIPTION(DRIVER_DESC);
2881 MODULE_LICENSE("GPL");