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 / usbduxfast.c
blob2ea874dda4b4c6ce1ca8994f76e0e27ffd0e5137
1 /*
2 * Copyright (C) 2004 Bernd Porr, Bernd.Porr@f2s.com
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * I must give credit here to Chris Baugher who
21 * wrote the driver for AT-MIO-16d. I used some parts of this
22 * driver. I also must give credits to David Brownell
23 * who supported me with the USB development.
25 * Bernd Porr
28 * Revision history:
29 * 0.9: Dropping the first data packet which seems to be from the last transfer.
30 * Buffer overflows in the FX2 are handed over to comedi.
31 * 0.92: Dropping now 4 packets. The quad buffer has to be emptied.
32 * Added insn command basically for testing. Sample rate is
33 * 1MHz/16ch=62.5kHz
34 * 0.99: Ian Abbott pointed out a bug which has been corrected. Thanks!
35 * 0.99a: added external trigger.
36 * 1.00: added firmware kernel request to the driver which fixed
37 * udev coldplug problem
40 #include <linux/kernel.h>
41 #include <linux/firmware.h>
42 #include <linux/module.h>
43 #include <linux/init.h>
44 #include <linux/slab.h>
45 #include <linux/input.h>
46 #include <linux/usb.h>
47 #include <linux/fcntl.h>
48 #include <linux/compiler.h>
49 #include "comedi_fc.h"
50 #include "../comedidev.h"
52 #define DRIVER_VERSION "v1.0"
53 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
54 #define DRIVER_DESC "USB-DUXfast, BerndPorr@f2s.com"
55 #define BOARDNAME "usbduxfast"
58 * timeout for the USB-transfer
60 #define EZTIMEOUT 30
63 * constants for "firmware" upload and download
65 #define USBDUXFASTSUB_FIRMWARE 0xA0
66 #define VENDOR_DIR_IN 0xC0
67 #define VENDOR_DIR_OUT 0x40
70 * internal addresses of the 8051 processor
72 #define USBDUXFASTSUB_CPUCS 0xE600
75 * max lenghth of the transfer-buffer for software upload
77 #define TB_LEN 0x2000
80 * input endpoint number
82 #define BULKINEP 6
85 * endpoint for the A/D channellist: bulk OUT
87 #define CHANNELLISTEP 4
90 * number of channels
92 #define NUMCHANNELS 32
95 * size of the waveform descriptor
97 #define WAVESIZE 0x20
100 * size of one A/D value
102 #define SIZEADIN (sizeof(int16_t))
105 * size of the input-buffer IN BYTES
107 #define SIZEINBUF 512
110 * 16 bytes
112 #define SIZEINSNBUF 512
115 * size of the buffer for the dux commands in bytes
117 #define SIZEOFDUXBUFFER 256
120 * number of in-URBs which receive the data: min=5
122 #define NUMOFINBUFFERSHIGH 10
125 * total number of usbduxfast devices
127 #define NUMUSBDUXFAST 16
130 * number of subdevices
132 #define N_SUBDEVICES 1
135 * analogue in subdevice
137 #define SUBDEV_AD 0
140 * min delay steps for more than one channel
141 * basically when the mux gives up ;-)
143 * steps at 30MHz in the FX2
145 #define MIN_SAMPLING_PERIOD 9
148 * max number of 1/30MHz delay steps
150 #define MAX_SAMPLING_PERIOD 500
153 * number of received packets to ignore before we start handing data
154 * over to comedi, it's quad buffering and we have to ignore 4 packets
156 #define PACKETS_TO_IGNORE 4
159 * comedi constants
161 static const struct comedi_lrange range_usbduxfast_ai_range = {
162 2, {BIP_RANGE(0.75), BIP_RANGE(0.5)}
166 * private structure of one subdevice
168 * this is the structure which holds all the data of this driver
169 * one sub device just now: A/D
171 struct usbduxfastsub_s {
172 int attached; /* is attached? */
173 int probed; /* is it associated with a subdevice? */
174 struct usb_device *usbdev; /* pointer to the usb-device */
175 struct urb *urbIn; /* BULK-transfer handling: urb */
176 int8_t *transfer_buffer;
177 int16_t *insnBuffer; /* input buffer for single insn */
178 int ifnum; /* interface number */
179 struct usb_interface *interface; /* interface structure */
180 /* comedi device for the interrupt context */
181 struct comedi_device *comedidev;
182 short int ai_cmd_running; /* asynchronous command is running */
183 short int ai_continous; /* continous aquisition */
184 long int ai_sample_count; /* number of samples to acquire */
185 uint8_t *dux_commands; /* commands */
186 int ignore; /* counter which ignores the first
187 buffers */
188 struct semaphore sem;
192 * The pointer to the private usb-data of the driver
193 * is also the private data for the comedi-device.
194 * This has to be global as the usb subsystem needs
195 * global variables. The other reason is that this
196 * structure must be there _before_ any comedi
197 * command is issued. The usb subsystem must be
198 * initialised before comedi can access it.
200 static struct usbduxfastsub_s usbduxfastsub[NUMUSBDUXFAST];
202 static DECLARE_MUTEX(start_stop_sem);
205 * bulk transfers to usbduxfast
207 #define SENDADCOMMANDS 0
208 #define SENDINITEP6 1
210 static int send_dux_commands(struct usbduxfastsub_s *udfs, int cmd_type)
212 int tmp, nsent;
214 udfs->dux_commands[0] = cmd_type;
216 #ifdef CONFIG_COMEDI_DEBUG
217 printk(KERN_DEBUG "comedi%d: usbduxfast: dux_commands: ",
218 udfs->comedidev->minor);
219 for (tmp = 0; tmp < SIZEOFDUXBUFFER; tmp++)
220 printk(" %02x", udfs->dux_commands[tmp]);
221 printk("\n");
222 #endif
224 tmp = usb_bulk_msg(udfs->usbdev,
225 usb_sndbulkpipe(udfs->usbdev, CHANNELLISTEP),
226 udfs->dux_commands, SIZEOFDUXBUFFER, &nsent, 10000);
227 if (tmp < 0)
228 printk(KERN_ERR "comedi%d: could not transmit dux_commands to"
229 "the usb-device, err=%d\n", udfs->comedidev->minor, tmp);
230 return tmp;
234 * Stops the data acquision.
235 * It should be safe to call this function from any context.
237 static int usbduxfastsub_unlink_InURBs(struct usbduxfastsub_s *udfs)
239 int j = 0;
240 int err = 0;
242 if (udfs && udfs->urbIn) {
243 udfs->ai_cmd_running = 0;
244 /* waits until a running transfer is over */
245 usb_kill_urb(udfs->urbIn);
246 j = 0;
248 #ifdef CONFIG_COMEDI_DEBUG
249 printk(KERN_DEBUG "comedi: usbduxfast: unlinked InURB: res=%d\n", j);
250 #endif
251 return err;
255 * This will stop a running acquisition operation.
256 * Is called from within this driver from both the
257 * interrupt context and from comedi.
259 static int usbduxfast_ai_stop(struct usbduxfastsub_s *udfs, int do_unlink)
261 int ret = 0;
263 if (!udfs) {
264 printk(KERN_ERR "comedi?: usbduxfast_ai_stop: udfs=NULL!\n");
265 return -EFAULT;
267 #ifdef CONFIG_COMEDI_DEBUG
268 printk(KERN_DEBUG "comedi: usbduxfast_ai_stop\n");
269 #endif
271 udfs->ai_cmd_running = 0;
273 if (do_unlink)
274 /* stop aquistion */
275 ret = usbduxfastsub_unlink_InURBs(udfs);
277 return ret;
281 * This will cancel a running acquisition operation.
282 * This is called by comedi but never from inside the driver.
284 static int usbduxfast_ai_cancel(struct comedi_device *dev,
285 struct comedi_subdevice *s)
287 struct usbduxfastsub_s *udfs;
288 int ret;
290 /* force unlink of all urbs */
291 #ifdef CONFIG_COMEDI_DEBUG
292 printk(KERN_DEBUG "comedi: usbduxfast_ai_cancel\n");
293 #endif
294 udfs = dev->private;
295 if (!udfs) {
296 printk(KERN_ERR "comedi: usbduxfast_ai_cancel: udfs=NULL\n");
297 return -EFAULT;
299 down(&udfs->sem);
300 if (!udfs->probed) {
301 up(&udfs->sem);
302 return -ENODEV;
304 /* unlink */
305 ret = usbduxfast_ai_stop(udfs, 1);
306 up(&udfs->sem);
308 return ret;
312 * analogue IN
313 * interrupt service routine
315 static void usbduxfastsub_ai_Irq(struct urb *urb)
317 int n, err;
318 struct usbduxfastsub_s *udfs;
319 struct comedi_device *this_comedidev;
320 struct comedi_subdevice *s;
321 uint16_t *p;
323 /* sanity checks - is the urb there? */
324 if (!urb) {
325 printk(KERN_ERR "comedi_: usbduxfast_: ao int-handler called "
326 "with urb=NULL!\n");
327 return;
329 /* the context variable points to the subdevice */
330 this_comedidev = urb->context;
331 if (!this_comedidev) {
332 printk(KERN_ERR "comedi_: usbduxfast_: urb context is a NULL "
333 "pointer!\n");
334 return;
336 /* the private structure of the subdevice is usbduxfastsub_s */
337 udfs = this_comedidev->private;
338 if (!udfs) {
339 printk(KERN_ERR "comedi_: usbduxfast_: private of comedi "
340 "subdev is a NULL pointer!\n");
341 return;
343 /* are we running a command? */
344 if (unlikely(!udfs->ai_cmd_running)) {
346 * not running a command
347 * do not continue execution if no asynchronous command
348 * is running in particular not resubmit
350 return;
353 if (unlikely(!udfs->attached)) {
354 /* no comedi device there */
355 return;
357 /* subdevice which is the AD converter */
358 s = this_comedidev->subdevices + SUBDEV_AD;
360 /* first we test if something unusual has just happened */
361 switch (urb->status) {
362 case 0:
363 break;
366 * happens after an unlink command or when the device
367 * is plugged out
369 case -ECONNRESET:
370 case -ENOENT:
371 case -ESHUTDOWN:
372 case -ECONNABORTED:
373 /* tell this comedi */
374 s->async->events |= COMEDI_CB_EOA;
375 s->async->events |= COMEDI_CB_ERROR;
376 comedi_event(udfs->comedidev, s);
377 /* stop the transfer w/o unlink */
378 usbduxfast_ai_stop(udfs, 0);
379 return;
381 default:
382 printk("comedi%d: usbduxfast: non-zero urb status received in "
383 "ai intr context: %d\n",
384 udfs->comedidev->minor, urb->status);
385 s->async->events |= COMEDI_CB_EOA;
386 s->async->events |= COMEDI_CB_ERROR;
387 comedi_event(udfs->comedidev, s);
388 usbduxfast_ai_stop(udfs, 0);
389 return;
392 p = urb->transfer_buffer;
393 if (!udfs->ignore) {
394 if (!udfs->ai_continous) {
395 /* not continous, fixed number of samples */
396 n = urb->actual_length / sizeof(uint16_t);
397 if (unlikely(udfs->ai_sample_count < n)) {
399 * we have send only a fraction of the bytes
400 * received
402 cfc_write_array_to_buffer(s,
403 urb->transfer_buffer,
404 udfs->ai_sample_count
405 * sizeof(uint16_t));
406 usbduxfast_ai_stop(udfs, 0);
407 /* tell comedi that the acquistion is over */
408 s->async->events |= COMEDI_CB_EOA;
409 comedi_event(udfs->comedidev, s);
410 return;
412 udfs->ai_sample_count -= n;
414 /* write the full buffer to comedi */
415 err = cfc_write_array_to_buffer(s, urb->transfer_buffer,
416 urb->actual_length);
417 if (unlikely(err == 0)) {
418 /* buffer overflow */
419 usbduxfast_ai_stop(udfs, 0);
420 return;
423 /* tell comedi that data is there */
424 comedi_event(udfs->comedidev, s);
426 } else {
427 /* ignore this packet */
428 udfs->ignore--;
432 * command is still running
433 * resubmit urb for BULK transfer
435 urb->dev = udfs->usbdev;
436 urb->status = 0;
437 err = usb_submit_urb(urb, GFP_ATOMIC);
438 if (err < 0) {
439 printk(KERN_ERR "comedi%d: usbduxfast: urb resubm failed: %d",
440 udfs->comedidev->minor, err);
441 s->async->events |= COMEDI_CB_EOA;
442 s->async->events |= COMEDI_CB_ERROR;
443 comedi_event(udfs->comedidev, s);
444 usbduxfast_ai_stop(udfs, 0);
448 static int usbduxfastsub_start(struct usbduxfastsub_s *udfs)
450 int ret;
451 unsigned char local_transfer_buffer[16];
453 /* 7f92 to zero */
454 local_transfer_buffer[0] = 0;
455 /* bRequest, "Firmware" */
456 ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0), USBDUXFASTSUB_FIRMWARE,
457 VENDOR_DIR_OUT, /* bmRequestType */
458 USBDUXFASTSUB_CPUCS, /* Value */
459 0x0000, /* Index */
460 /* address of the transfer buffer */
461 local_transfer_buffer,
462 1, /* Length */
463 EZTIMEOUT); /* Timeout */
464 if (ret < 0) {
465 printk("comedi_: usbduxfast_: control msg failed (start)\n");
466 return ret;
469 return 0;
472 static int usbduxfastsub_stop(struct usbduxfastsub_s *udfs)
474 int ret;
475 unsigned char local_transfer_buffer[16];
477 /* 7f92 to one */
478 local_transfer_buffer[0] = 1;
479 /* bRequest, "Firmware" */
480 ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0), USBDUXFASTSUB_FIRMWARE,
481 VENDOR_DIR_OUT, /* bmRequestType */
482 USBDUXFASTSUB_CPUCS, /* Value */
483 0x0000, /* Index */
484 local_transfer_buffer, 1, /* Length */
485 EZTIMEOUT); /* Timeout */
486 if (ret < 0) {
487 printk(KERN_ERR "comedi_: usbduxfast: control msg failed "
488 "(stop)\n");
489 return ret;
492 return 0;
495 static int usbduxfastsub_upload(struct usbduxfastsub_s *udfs,
496 unsigned char *local_transfer_buffer,
497 unsigned int startAddr, unsigned int len)
499 int ret;
501 #ifdef CONFIG_COMEDI_DEBUG
502 printk(KERN_DEBUG "comedi: usbduxfast: uploading %d bytes", len);
503 printk(KERN_DEBUG " to addr %d, first byte=%d.\n",
504 startAddr, local_transfer_buffer[0]);
505 #endif
506 /* brequest, firmware */
507 ret = usb_control_msg(udfs->usbdev, usb_sndctrlpipe(udfs->usbdev, 0), USBDUXFASTSUB_FIRMWARE,
508 VENDOR_DIR_OUT, /* bmRequestType */
509 startAddr, /* value */
510 0x0000, /* index */
511 /* our local safe buffer */
512 local_transfer_buffer,
513 len, /* length */
514 EZTIMEOUT); /* timeout */
516 #ifdef CONFIG_COMEDI_DEBUG
517 printk(KERN_DEBUG "comedi_: usbduxfast: result=%d\n", ret);
518 #endif
520 if (ret < 0) {
521 printk(KERN_ERR "comedi_: usbduxfast: uppload failed\n");
522 return ret;
525 return 0;
528 static int usbduxfastsub_submit_InURBs(struct usbduxfastsub_s *udfs)
530 int ret;
532 if (!udfs)
533 return -EFAULT;
535 usb_fill_bulk_urb(udfs->urbIn, udfs->usbdev,
536 usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
537 udfs->transfer_buffer,
538 SIZEINBUF, usbduxfastsub_ai_Irq, udfs->comedidev);
540 #ifdef CONFIG_COMEDI_DEBUG
541 printk(KERN_DEBUG "comedi%d: usbduxfast: submitting in-urb: "
542 "0x%p,0x%p\n", udfs->comedidev->minor, udfs->urbIn->context,
543 udfs->urbIn->dev);
544 #endif
545 ret = usb_submit_urb(udfs->urbIn, GFP_ATOMIC);
546 if (ret) {
547 printk(KERN_ERR "comedi_: usbduxfast: ai: usb_submit_urb error"
548 " %d\n", ret);
549 return ret;
551 return 0;
554 static int usbduxfast_ai_cmdtest(struct comedi_device *dev,
555 struct comedi_subdevice *s,
556 struct comedi_cmd *cmd)
558 int err = 0, stop_mask = 0;
559 long int steps, tmp;
560 int minSamplPer;
561 struct usbduxfastsub_s *udfs = dev->private;
563 if (!udfs->probed)
564 return -ENODEV;
566 #ifdef CONFIG_COMEDI_DEBUG
567 printk(KERN_DEBUG "comedi%d: usbduxfast_ai_cmdtest\n", dev->minor);
568 printk(KERN_DEBUG "comedi%d: usbduxfast: convert_arg=%u "
569 "scan_begin_arg=%u\n",
570 dev->minor, cmd->convert_arg, cmd->scan_begin_arg);
571 #endif
572 /* step 1: make sure trigger sources are trivially valid */
574 tmp = cmd->start_src;
575 cmd->start_src &= TRIG_NOW | TRIG_EXT | TRIG_INT;
576 if (!cmd->start_src || tmp != cmd->start_src)
577 err++;
579 tmp = cmd->scan_begin_src;
580 cmd->scan_begin_src &= TRIG_TIMER | TRIG_FOLLOW | TRIG_EXT;
581 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
582 err++;
584 tmp = cmd->convert_src;
585 cmd->convert_src &= TRIG_TIMER | TRIG_EXT;
586 if (!cmd->convert_src || tmp != cmd->convert_src)
587 err++;
589 tmp = cmd->scan_end_src;
590 cmd->scan_end_src &= TRIG_COUNT;
591 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
592 err++;
594 tmp = cmd->stop_src;
595 stop_mask = TRIG_COUNT | TRIG_NONE;
596 cmd->stop_src &= stop_mask;
597 if (!cmd->stop_src || tmp != cmd->stop_src)
598 err++;
600 if (err)
601 return 1;
604 * step 2: make sure trigger sources are unique and mutually compatible
607 if (cmd->start_src != TRIG_NOW &&
608 cmd->start_src != TRIG_EXT && cmd->start_src != TRIG_INT)
609 err++;
610 if (cmd->scan_begin_src != TRIG_TIMER &&
611 cmd->scan_begin_src != TRIG_FOLLOW &&
612 cmd->scan_begin_src != TRIG_EXT)
613 err++;
614 if (cmd->convert_src != TRIG_TIMER && cmd->convert_src != TRIG_EXT)
615 err++;
616 if (cmd->stop_src != TRIG_COUNT &&
617 cmd->stop_src != TRIG_EXT && cmd->stop_src != TRIG_NONE)
618 err++;
620 /* can't have external stop and start triggers at once */
621 if (cmd->start_src == TRIG_EXT && cmd->stop_src == TRIG_EXT)
622 err++;
624 if (err)
625 return 2;
627 /* step 3: make sure arguments are trivially compatible */
629 if (cmd->start_src == TRIG_NOW && cmd->start_arg != 0) {
630 cmd->start_arg = 0;
631 err++;
634 if (!cmd->chanlist_len)
635 err++;
637 if (cmd->scan_end_arg != cmd->chanlist_len) {
638 cmd->scan_end_arg = cmd->chanlist_len;
639 err++;
642 if (cmd->chanlist_len == 1)
643 minSamplPer = 1;
644 else
645 minSamplPer = MIN_SAMPLING_PERIOD;
647 if (cmd->convert_src == TRIG_TIMER) {
648 steps = cmd->convert_arg * 30;
649 if (steps < (minSamplPer * 1000))
650 steps = minSamplPer * 1000;
652 if (steps > (MAX_SAMPLING_PERIOD * 1000))
653 steps = MAX_SAMPLING_PERIOD * 1000;
655 /* calc arg again */
656 tmp = steps / 30;
657 if (cmd->convert_arg != tmp) {
658 cmd->convert_arg = tmp;
659 err++;
663 if (cmd->scan_begin_src == TRIG_TIMER)
664 err++;
666 /* stop source */
667 switch (cmd->stop_src) {
668 case TRIG_COUNT:
669 if (!cmd->stop_arg) {
670 cmd->stop_arg = 1;
671 err++;
673 break;
674 case TRIG_NONE:
675 if (cmd->stop_arg != 0) {
676 cmd->stop_arg = 0;
677 err++;
679 break;
681 * TRIG_EXT doesn't care since it doesn't trigger
682 * off a numbered channel
684 default:
685 break;
688 if (err)
689 return 3;
691 /* step 4: fix up any arguments */
693 return 0;
697 static int usbduxfast_ai_inttrig(struct comedi_device *dev,
698 struct comedi_subdevice *s,
699 unsigned int trignum)
701 int ret;
702 struct usbduxfastsub_s *udfs = dev->private;
704 if (!udfs)
705 return -EFAULT;
707 down(&udfs->sem);
708 if (!udfs->probed) {
709 up(&udfs->sem);
710 return -ENODEV;
712 #ifdef CONFIG_COMEDI_DEBUG
713 printk(KERN_DEBUG "comedi%d: usbduxfast_ai_inttrig\n", dev->minor);
714 #endif
716 if (trignum != 0) {
717 printk(KERN_ERR "comedi%d: usbduxfast_ai_inttrig: invalid"
718 " trignum\n", dev->minor);
719 up(&udfs->sem);
720 return -EINVAL;
722 if (!udfs->ai_cmd_running) {
723 udfs->ai_cmd_running = 1;
724 ret = usbduxfastsub_submit_InURBs(udfs);
725 if (ret < 0) {
726 printk(KERN_ERR "comedi%d: usbduxfast_ai_inttrig: "
727 "urbSubmit: err=%d\n", dev->minor, ret);
728 udfs->ai_cmd_running = 0;
729 up(&udfs->sem);
730 return ret;
732 s->async->inttrig = NULL;
733 } else {
734 printk(KERN_ERR "comedi%d: ai_inttrig but acqu is already"
735 " running\n", dev->minor);
737 up(&udfs->sem);
738 return 1;
742 * offsets for the GPIF bytes
743 * the first byte is the command byte
745 #define LENBASE (1+0x00)
746 #define OPBASE (1+0x08)
747 #define OUTBASE (1+0x10)
748 #define LOGBASE (1+0x18)
750 static int usbduxfast_ai_cmd(struct comedi_device *dev,
751 struct comedi_subdevice *s)
753 struct comedi_cmd *cmd = &s->async->cmd;
754 unsigned int chan, gain, rngmask = 0xff;
755 int i, j, ret;
756 struct usbduxfastsub_s *udfs;
757 int result;
758 long steps, steps_tmp;
760 #ifdef CONFIG_COMEDI_DEBUG
761 printk(KERN_DEBUG "comedi%d: usbduxfast_ai_cmd\n", dev->minor);
762 #endif
763 udfs = dev->private;
764 if (!udfs)
765 return -EFAULT;
767 down(&udfs->sem);
768 if (!udfs->probed) {
769 up(&udfs->sem);
770 return -ENODEV;
772 if (udfs->ai_cmd_running) {
773 printk(KERN_ERR "comedi%d: ai_cmd not possible. Another ai_cmd"
774 " is running.\n", dev->minor);
775 up(&udfs->sem);
776 return -EBUSY;
778 /* set current channel of the running aquisition to zero */
779 s->async->cur_chan = 0;
782 * ignore the first buffers from the device if there
783 * is an error condition
785 udfs->ignore = PACKETS_TO_IGNORE;
787 if (cmd->chanlist_len > 0) {
788 gain = CR_RANGE(cmd->chanlist[0]);
789 for (i = 0; i < cmd->chanlist_len; ++i) {
790 chan = CR_CHAN(cmd->chanlist[i]);
791 if (chan != i) {
792 printk(KERN_ERR "comedi%d: cmd is accepting "
793 "only consecutive channels.\n",
794 dev->minor);
795 up(&udfs->sem);
796 return -EINVAL;
798 if ((gain != CR_RANGE(cmd->chanlist[i]))
799 && (cmd->chanlist_len > 3)) {
800 printk(KERN_ERR "comedi%d: the gain must be"
801 " the same for all channels.\n",
802 dev->minor);
803 up(&udfs->sem);
804 return -EINVAL;
806 if (i >= NUMCHANNELS) {
807 printk(KERN_ERR "comedi%d: channel list too"
808 " long\n", dev->minor);
809 break;
813 steps = 0;
814 if (cmd->scan_begin_src == TRIG_TIMER) {
815 printk(KERN_ERR "comedi%d: usbduxfast: "
816 "scan_begin_src==TRIG_TIMER not valid.\n", dev->minor);
817 up(&udfs->sem);
818 return -EINVAL;
820 if (cmd->convert_src == TRIG_TIMER)
821 steps = (cmd->convert_arg * 30) / 1000;
823 if ((steps < MIN_SAMPLING_PERIOD) && (cmd->chanlist_len != 1)) {
824 printk(KERN_ERR "comedi%d: usbduxfast: ai_cmd: steps=%ld, "
825 "scan_begin_arg=%d. Not properly tested by cmdtest?\n",
826 dev->minor, steps, cmd->scan_begin_arg);
827 up(&udfs->sem);
828 return -EINVAL;
830 if (steps > MAX_SAMPLING_PERIOD) {
831 printk(KERN_ERR "comedi%d: usbduxfast: ai_cmd: sampling rate "
832 "too low.\n", dev->minor);
833 up(&udfs->sem);
834 return -EINVAL;
836 if ((cmd->start_src == TRIG_EXT) && (cmd->chanlist_len != 1)
837 && (cmd->chanlist_len != 16)) {
838 printk(KERN_ERR "comedi%d: usbduxfast: ai_cmd: TRIG_EXT only"
839 " with 1 or 16 channels possible.\n", dev->minor);
840 up(&udfs->sem);
841 return -EINVAL;
843 #ifdef CONFIG_COMEDI_DEBUG
844 printk(KERN_DEBUG "comedi%d: usbduxfast: steps=%ld, convert_arg=%u\n",
845 dev->minor, steps, cmd->convert_arg);
846 #endif
848 switch (cmd->chanlist_len) {
849 case 1:
851 * one channel
854 if (CR_RANGE(cmd->chanlist[0]) > 0)
855 rngmask = 0xff - 0x04;
856 else
857 rngmask = 0xff;
860 * for external trigger: looping in this state until
861 * the RDY0 pin becomes zero
864 /* we loop here until ready has been set */
865 if (cmd->start_src == TRIG_EXT) {
866 /* branch back to state 0 */
867 udfs->dux_commands[LENBASE + 0] = 0x01;
868 /* deceision state w/o data */
869 udfs->dux_commands[OPBASE + 0] = 0x01;
870 udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
871 /* RDY0 = 0 */
872 udfs->dux_commands[LOGBASE + 0] = 0x00;
873 } else { /* we just proceed to state 1 */
874 udfs->dux_commands[LENBASE + 0] = 1;
875 udfs->dux_commands[OPBASE + 0] = 0;
876 udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
877 udfs->dux_commands[LOGBASE + 0] = 0;
880 if (steps < MIN_SAMPLING_PERIOD) {
881 /* for fast single channel aqu without mux */
882 if (steps <= 1) {
884 * we just stay here at state 1 and rexecute
885 * the same state this gives us 30MHz sampling
886 * rate
889 /* branch back to state 1 */
890 udfs->dux_commands[LENBASE + 1] = 0x89;
891 /* deceision state with data */
892 udfs->dux_commands[OPBASE + 1] = 0x03;
893 udfs->dux_commands[OUTBASE + 1] =
894 0xFF & rngmask;
895 /* doesn't matter */
896 udfs->dux_commands[LOGBASE + 1] = 0xFF;
897 } else {
899 * we loop through two states: data and delay
900 * max rate is 15MHz
902 udfs->dux_commands[LENBASE + 1] = steps - 1;
903 /* data */
904 udfs->dux_commands[OPBASE + 1] = 0x02;
905 udfs->dux_commands[OUTBASE + 1] =
906 0xFF & rngmask;
907 /* doesn't matter */
908 udfs->dux_commands[LOGBASE + 1] = 0;
909 /* branch back to state 1 */
910 udfs->dux_commands[LENBASE + 2] = 0x09;
911 /* deceision state w/o data */
912 udfs->dux_commands[OPBASE + 2] = 0x01;
913 udfs->dux_commands[OUTBASE + 2] =
914 0xFF & rngmask;
915 /* doesn't matter */
916 udfs->dux_commands[LOGBASE + 2] = 0xFF;
918 } else {
920 * we loop through 3 states: 2x delay and 1x data
921 * this gives a min sampling rate of 60kHz
924 /* we have 1 state with duration 1 */
925 steps = steps - 1;
927 /* do the first part of the delay */
928 udfs->dux_commands[LENBASE + 1] = steps / 2;
929 udfs->dux_commands[OPBASE + 1] = 0;
930 udfs->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
931 udfs->dux_commands[LOGBASE + 1] = 0;
933 /* and the second part */
934 udfs->dux_commands[LENBASE + 2] = steps - steps / 2;
935 udfs->dux_commands[OPBASE + 2] = 0;
936 udfs->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
937 udfs->dux_commands[LOGBASE + 2] = 0;
939 /* get the data and branch back */
941 /* branch back to state 1 */
942 udfs->dux_commands[LENBASE + 3] = 0x09;
943 /* deceision state w data */
944 udfs->dux_commands[OPBASE + 3] = 0x03;
945 udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
946 /* doesn't matter */
947 udfs->dux_commands[LOGBASE + 3] = 0xFF;
949 break;
951 case 2:
953 * two channels
954 * commit data to the FIFO
957 if (CR_RANGE(cmd->chanlist[0]) > 0)
958 rngmask = 0xff - 0x04;
959 else
960 rngmask = 0xff;
962 udfs->dux_commands[LENBASE + 0] = 1;
963 /* data */
964 udfs->dux_commands[OPBASE + 0] = 0x02;
965 udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
966 udfs->dux_commands[LOGBASE + 0] = 0;
968 /* we have 1 state with duration 1: state 0 */
969 steps_tmp = steps - 1;
971 if (CR_RANGE(cmd->chanlist[1]) > 0)
972 rngmask = 0xff - 0x04;
973 else
974 rngmask = 0xff;
976 /* do the first part of the delay */
977 udfs->dux_commands[LENBASE + 1] = steps_tmp / 2;
978 udfs->dux_commands[OPBASE + 1] = 0;
979 /* count */
980 udfs->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
981 udfs->dux_commands[LOGBASE + 1] = 0;
983 /* and the second part */
984 udfs->dux_commands[LENBASE + 2] = steps_tmp - steps_tmp / 2;
985 udfs->dux_commands[OPBASE + 2] = 0;
986 udfs->dux_commands[OUTBASE + 2] = 0xFF & rngmask;
987 udfs->dux_commands[LOGBASE + 2] = 0;
989 udfs->dux_commands[LENBASE + 3] = 1;
990 /* data */
991 udfs->dux_commands[OPBASE + 3] = 0x02;
992 udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
993 udfs->dux_commands[LOGBASE + 3] = 0;
996 * we have 2 states with duration 1: step 6 and
997 * the IDLE state
999 steps_tmp = steps - 2;
1001 if (CR_RANGE(cmd->chanlist[0]) > 0)
1002 rngmask = 0xff - 0x04;
1003 else
1004 rngmask = 0xff;
1006 /* do the first part of the delay */
1007 udfs->dux_commands[LENBASE + 4] = steps_tmp / 2;
1008 udfs->dux_commands[OPBASE + 4] = 0;
1009 /* reset */
1010 udfs->dux_commands[OUTBASE + 4] = (0xFF - 0x02) & rngmask;
1011 udfs->dux_commands[LOGBASE + 4] = 0;
1013 /* and the second part */
1014 udfs->dux_commands[LENBASE + 5] = steps_tmp - steps_tmp / 2;
1015 udfs->dux_commands[OPBASE + 5] = 0;
1016 udfs->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1017 udfs->dux_commands[LOGBASE + 5] = 0;
1019 udfs->dux_commands[LENBASE + 6] = 1;
1020 udfs->dux_commands[OPBASE + 6] = 0;
1021 udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1022 udfs->dux_commands[LOGBASE + 6] = 0;
1023 break;
1025 case 3:
1027 * three channels
1029 for (j = 0; j < 1; j++) {
1030 if (CR_RANGE(cmd->chanlist[j]) > 0)
1031 rngmask = 0xff - 0x04;
1032 else
1033 rngmask = 0xff;
1035 * commit data to the FIFO and do the first part
1036 * of the delay
1038 udfs->dux_commands[LENBASE + j * 2] = steps / 2;
1039 /* data */
1040 udfs->dux_commands[OPBASE + j * 2] = 0x02;
1041 /* no change */
1042 udfs->dux_commands[OUTBASE + j * 2] = 0xFF & rngmask;
1043 udfs->dux_commands[LOGBASE + j * 2] = 0;
1045 if (CR_RANGE(cmd->chanlist[j + 1]) > 0)
1046 rngmask = 0xff - 0x04;
1047 else
1048 rngmask = 0xff;
1050 /* do the second part of the delay */
1051 udfs->dux_commands[LENBASE + j * 2 + 1] =
1052 steps - steps / 2;
1053 /* no data */
1054 udfs->dux_commands[OPBASE + j * 2 + 1] = 0;
1055 /* count */
1056 udfs->dux_commands[OUTBASE + j * 2 + 1] =
1057 0xFE & rngmask;
1058 udfs->dux_commands[LOGBASE + j * 2 + 1] = 0;
1061 /* 2 steps with duration 1: the idele step and step 6: */
1062 steps_tmp = steps - 2;
1064 /* commit data to the FIFO and do the first part of the delay */
1065 udfs->dux_commands[LENBASE + 4] = steps_tmp / 2;
1066 /* data */
1067 udfs->dux_commands[OPBASE + 4] = 0x02;
1068 udfs->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1069 udfs->dux_commands[LOGBASE + 4] = 0;
1071 if (CR_RANGE(cmd->chanlist[0]) > 0)
1072 rngmask = 0xff - 0x04;
1073 else
1074 rngmask = 0xff;
1076 /* do the second part of the delay */
1077 udfs->dux_commands[LENBASE + 5] = steps_tmp - steps_tmp / 2;
1078 /* no data */
1079 udfs->dux_commands[OPBASE + 5] = 0;
1080 /* reset */
1081 udfs->dux_commands[OUTBASE + 5] = (0xFF - 0x02) & rngmask;
1082 udfs->dux_commands[LOGBASE + 5] = 0;
1084 udfs->dux_commands[LENBASE + 6] = 1;
1085 udfs->dux_commands[OPBASE + 6] = 0;
1086 udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1087 udfs->dux_commands[LOGBASE + 6] = 0;
1089 case 16:
1090 if (CR_RANGE(cmd->chanlist[0]) > 0)
1091 rngmask = 0xff - 0x04;
1092 else
1093 rngmask = 0xff;
1095 if (cmd->start_src == TRIG_EXT) {
1097 * we loop here until ready has been set
1100 /* branch back to state 0 */
1101 udfs->dux_commands[LENBASE + 0] = 0x01;
1102 /* deceision state w/o data */
1103 udfs->dux_commands[OPBASE + 0] = 0x01;
1104 /* reset */
1105 udfs->dux_commands[OUTBASE + 0] =
1106 (0xFF - 0x02) & rngmask;
1107 /* RDY0 = 0 */
1108 udfs->dux_commands[LOGBASE + 0] = 0x00;
1109 } else {
1111 * we just proceed to state 1
1114 /* 30us reset pulse */
1115 udfs->dux_commands[LENBASE + 0] = 255;
1116 udfs->dux_commands[OPBASE + 0] = 0;
1117 /* reset */
1118 udfs->dux_commands[OUTBASE + 0] =
1119 (0xFF - 0x02) & rngmask;
1120 udfs->dux_commands[LOGBASE + 0] = 0;
1123 /* commit data to the FIFO */
1124 udfs->dux_commands[LENBASE + 1] = 1;
1125 /* data */
1126 udfs->dux_commands[OPBASE + 1] = 0x02;
1127 udfs->dux_commands[OUTBASE + 1] = 0xFF & rngmask;
1128 udfs->dux_commands[LOGBASE + 1] = 0;
1130 /* we have 2 states with duration 1 */
1131 steps = steps - 2;
1133 /* do the first part of the delay */
1134 udfs->dux_commands[LENBASE + 2] = steps / 2;
1135 udfs->dux_commands[OPBASE + 2] = 0;
1136 udfs->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1137 udfs->dux_commands[LOGBASE + 2] = 0;
1139 /* and the second part */
1140 udfs->dux_commands[LENBASE + 3] = steps - steps / 2;
1141 udfs->dux_commands[OPBASE + 3] = 0;
1142 udfs->dux_commands[OUTBASE + 3] = 0xFF & rngmask;
1143 udfs->dux_commands[LOGBASE + 3] = 0;
1145 /* branch back to state 1 */
1146 udfs->dux_commands[LENBASE + 4] = 0x09;
1147 /* deceision state w/o data */
1148 udfs->dux_commands[OPBASE + 4] = 0x01;
1149 udfs->dux_commands[OUTBASE + 4] = 0xFF & rngmask;
1150 /* doesn't matter */
1151 udfs->dux_commands[LOGBASE + 4] = 0xFF;
1153 break;
1155 default:
1156 printk(KERN_ERR "comedi %d: unsupported combination of "
1157 "channels\n", dev->minor);
1158 up(&udfs->sem);
1159 return -EFAULT;
1162 #ifdef CONFIG_COMEDI_DEBUG
1163 printk(KERN_DEBUG "comedi %d: sending commands to the usb device\n",
1164 dev->minor);
1165 #endif
1166 /* 0 means that the AD commands are sent */
1167 result = send_dux_commands(udfs, SENDADCOMMANDS);
1168 if (result < 0) {
1169 printk(KERN_ERR "comedi%d: adc command could not be submitted."
1170 "Aborting...\n", dev->minor);
1171 up(&udfs->sem);
1172 return result;
1174 if (cmd->stop_src == TRIG_COUNT) {
1175 udfs->ai_sample_count = cmd->stop_arg * cmd->scan_end_arg;
1176 if (udfs->ai_sample_count < 1) {
1177 printk(KERN_ERR "comedi%d: "
1178 "(cmd->stop_arg)*(cmd->scan_end_arg)<1, "
1179 "aborting.\n", dev->minor);
1180 up(&udfs->sem);
1181 return -EFAULT;
1183 udfs->ai_continous = 0;
1184 } else {
1185 /* continous aquisition */
1186 udfs->ai_continous = 1;
1187 udfs->ai_sample_count = 0;
1190 if ((cmd->start_src == TRIG_NOW) || (cmd->start_src == TRIG_EXT)) {
1191 /* enable this acquisition operation */
1192 udfs->ai_cmd_running = 1;
1193 ret = usbduxfastsub_submit_InURBs(udfs);
1194 if (ret < 0) {
1195 udfs->ai_cmd_running = 0;
1196 up(&udfs->sem);
1197 return ret;
1199 s->async->inttrig = NULL;
1200 } else {
1202 * TRIG_INT
1203 * don't enable the acquision operation
1204 * wait for an internal signal
1206 s->async->inttrig = usbduxfast_ai_inttrig;
1208 up(&udfs->sem);
1210 return 0;
1214 * Mode 0 is used to get a single conversion on demand.
1216 static int usbduxfast_ai_insn_read(struct comedi_device *dev,
1217 struct comedi_subdevice *s,
1218 struct comedi_insn *insn, unsigned int *data)
1220 int i, j, n, actual_length;
1221 int chan, range, rngmask;
1222 int err;
1223 struct usbduxfastsub_s *udfs;
1225 udfs = dev->private;
1226 if (!udfs) {
1227 printk(KERN_ERR "comedi%d: ai_insn_read: no usb dev.\n",
1228 dev->minor);
1229 return -ENODEV;
1231 #ifdef CONFIG_COMEDI_DEBUG
1232 printk(KERN_DEBUG "comedi%d: ai_insn_read, insn->n=%d, "
1233 "insn->subdev=%d\n", dev->minor, insn->n, insn->subdev);
1234 #endif
1235 down(&udfs->sem);
1236 if (!udfs->probed) {
1237 up(&udfs->sem);
1238 return -ENODEV;
1240 if (udfs->ai_cmd_running) {
1241 printk(KERN_ERR "comedi%d: ai_insn_read not possible. Async "
1242 "Command is running.\n", dev->minor);
1243 up(&udfs->sem);
1244 return -EBUSY;
1246 /* sample one channel */
1247 chan = CR_CHAN(insn->chanspec);
1248 range = CR_RANGE(insn->chanspec);
1249 /* set command for the first channel */
1251 if (range > 0)
1252 rngmask = 0xff - 0x04;
1253 else
1254 rngmask = 0xff;
1256 /* commit data to the FIFO */
1257 udfs->dux_commands[LENBASE + 0] = 1;
1258 /* data */
1259 udfs->dux_commands[OPBASE + 0] = 0x02;
1260 udfs->dux_commands[OUTBASE + 0] = 0xFF & rngmask;
1261 udfs->dux_commands[LOGBASE + 0] = 0;
1263 /* do the first part of the delay */
1264 udfs->dux_commands[LENBASE + 1] = 12;
1265 udfs->dux_commands[OPBASE + 1] = 0;
1266 udfs->dux_commands[OUTBASE + 1] = 0xFE & rngmask;
1267 udfs->dux_commands[LOGBASE + 1] = 0;
1269 udfs->dux_commands[LENBASE + 2] = 1;
1270 udfs->dux_commands[OPBASE + 2] = 0;
1271 udfs->dux_commands[OUTBASE + 2] = 0xFE & rngmask;
1272 udfs->dux_commands[LOGBASE + 2] = 0;
1274 udfs->dux_commands[LENBASE + 3] = 1;
1275 udfs->dux_commands[OPBASE + 3] = 0;
1276 udfs->dux_commands[OUTBASE + 3] = 0xFE & rngmask;
1277 udfs->dux_commands[LOGBASE + 3] = 0;
1279 udfs->dux_commands[LENBASE + 4] = 1;
1280 udfs->dux_commands[OPBASE + 4] = 0;
1281 udfs->dux_commands[OUTBASE + 4] = 0xFE & rngmask;
1282 udfs->dux_commands[LOGBASE + 4] = 0;
1284 /* second part */
1285 udfs->dux_commands[LENBASE + 5] = 12;
1286 udfs->dux_commands[OPBASE + 5] = 0;
1287 udfs->dux_commands[OUTBASE + 5] = 0xFF & rngmask;
1288 udfs->dux_commands[LOGBASE + 5] = 0;
1290 udfs->dux_commands[LENBASE + 6] = 1;
1291 udfs->dux_commands[OPBASE + 6] = 0;
1292 udfs->dux_commands[OUTBASE + 6] = 0xFF & rngmask;
1293 udfs->dux_commands[LOGBASE + 0] = 0;
1295 #ifdef CONFIG_COMEDI_DEBUG
1296 printk(KERN_DEBUG "comedi %d: sending commands to the usb device\n",
1297 dev->minor);
1298 #endif
1299 /* 0 means that the AD commands are sent */
1300 err = send_dux_commands(udfs, SENDADCOMMANDS);
1301 if (err < 0) {
1302 printk(KERN_ERR "comedi%d: adc command could not be submitted."
1303 "Aborting...\n", dev->minor);
1304 up(&udfs->sem);
1305 return err;
1307 #ifdef CONFIG_COMEDI_DEBUG
1308 printk(KERN_DEBUG "comedi%d: usbduxfast: submitting in-urb: "
1309 "0x%p,0x%p\n", udfs->comedidev->minor, udfs->urbIn->context,
1310 udfs->urbIn->dev);
1311 #endif
1312 for (i = 0; i < PACKETS_TO_IGNORE; i++) {
1313 err = usb_bulk_msg(udfs->usbdev,
1314 usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
1315 udfs->transfer_buffer, SIZEINBUF,
1316 &actual_length, 10000);
1317 if (err < 0) {
1318 printk(KERN_ERR "comedi%d: insn timeout. No data.\n",
1319 dev->minor);
1320 up(&udfs->sem);
1321 return err;
1324 /* data points */
1325 for (i = 0; i < insn->n;) {
1326 err = usb_bulk_msg(udfs->usbdev,
1327 usb_rcvbulkpipe(udfs->usbdev, BULKINEP),
1328 udfs->transfer_buffer, SIZEINBUF,
1329 &actual_length, 10000);
1330 if (err < 0) {
1331 printk(KERN_ERR "comedi%d: insn data error: %d\n",
1332 dev->minor, err);
1333 up(&udfs->sem);
1334 return err;
1336 n = actual_length / sizeof(uint16_t);
1337 if ((n % 16) != 0) {
1338 printk(KERN_ERR "comedi%d: insn data packet "
1339 "corrupted.\n", dev->minor);
1340 up(&udfs->sem);
1341 return -EINVAL;
1343 for (j = chan; (j < n) && (i < insn->n); j = j + 16) {
1344 data[i] = ((uint16_t *) (udfs->transfer_buffer))[j];
1345 i++;
1348 up(&udfs->sem);
1349 return i;
1352 #define FIRMWARE_MAX_LEN 0x2000
1354 static int firmwareUpload(struct usbduxfastsub_s *usbduxfastsub,
1355 const u8 *firmwareBinary, int sizeFirmware)
1357 int ret;
1358 uint8_t *fwBuf;
1360 if (!firmwareBinary)
1361 return 0;
1363 if (sizeFirmware > FIRMWARE_MAX_LEN) {
1364 dev_err(&usbduxfastsub->interface->dev,
1365 "comedi_: usbduxfast firmware binary it too large for FX2.\n");
1366 return -ENOMEM;
1369 /* we generate a local buffer for the firmware */
1370 fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
1371 if (!fwBuf) {
1372 dev_err(&usbduxfastsub->interface->dev,
1373 "comedi_: mem alloc for firmware failed\n");
1374 return -ENOMEM;
1377 ret = usbduxfastsub_stop(usbduxfastsub);
1378 if (ret < 0) {
1379 dev_err(&usbduxfastsub->interface->dev,
1380 "comedi_: can not stop firmware\n");
1381 kfree(fwBuf);
1382 return ret;
1385 ret = usbduxfastsub_upload(usbduxfastsub, fwBuf, 0, sizeFirmware);
1386 if (ret < 0) {
1387 dev_err(&usbduxfastsub->interface->dev,
1388 "comedi_: firmware upload failed\n");
1389 kfree(fwBuf);
1390 return ret;
1392 ret = usbduxfastsub_start(usbduxfastsub);
1393 if (ret < 0) {
1394 dev_err(&usbduxfastsub->interface->dev,
1395 "comedi_: can not start firmware\n");
1396 kfree(fwBuf);
1397 return ret;
1399 kfree(fwBuf);
1400 return 0;
1403 static void tidy_up(struct usbduxfastsub_s *udfs)
1405 #ifdef CONFIG_COMEDI_DEBUG
1406 printk(KERN_DEBUG "comedi_: usbduxfast: tiding up\n");
1407 #endif
1409 if (!udfs)
1410 return;
1412 /* shows the usb subsystem that the driver is down */
1413 if (udfs->interface)
1414 usb_set_intfdata(udfs->interface, NULL);
1416 udfs->probed = 0;
1418 if (udfs->urbIn) {
1419 /* waits until a running transfer is over */
1420 usb_kill_urb(udfs->urbIn);
1422 kfree(udfs->transfer_buffer);
1423 udfs->transfer_buffer = NULL;
1425 usb_free_urb(udfs->urbIn);
1426 udfs->urbIn = NULL;
1429 kfree(udfs->insnBuffer);
1430 udfs->insnBuffer = NULL;
1432 kfree(udfs->dux_commands);
1433 udfs->dux_commands = NULL;
1435 udfs->ai_cmd_running = 0;
1438 static void usbduxfast_firmware_request_complete_handler(const struct firmware
1439 *fw, void *context)
1441 struct usbduxfastsub_s *usbduxfastsub_tmp = context;
1442 struct usb_device *usbdev = usbduxfastsub_tmp->usbdev;
1443 int ret;
1445 if (fw == NULL)
1446 return;
1449 * we need to upload the firmware here because fw will be
1450 * freed once we've left this function
1452 ret = firmwareUpload(usbduxfastsub_tmp, fw->data, fw->size);
1454 if (ret) {
1455 dev_err(&usbdev->dev,
1456 "Could not upload firmware (err=%d)\n", ret);
1457 goto out;
1460 comedi_usb_auto_config(usbdev, BOARDNAME);
1461 out:
1462 release_firmware(fw);
1466 * allocate memory for the urbs and initialise them
1468 static int usbduxfastsub_probe(struct usb_interface *uinterf,
1469 const struct usb_device_id *id)
1471 struct usb_device *udev = interface_to_usbdev(uinterf);
1472 int i;
1473 int index;
1474 int ret;
1476 if (udev->speed != USB_SPEED_HIGH) {
1477 printk(KERN_ERR "comedi_: usbduxfast_: This driver needs"
1478 "USB 2.0 to operate. Aborting...\n");
1479 return -ENODEV;
1481 #ifdef CONFIG_COMEDI_DEBUG
1482 printk(KERN_DEBUG "comedi_: usbduxfast_: finding a free structure for "
1483 "the usb-device\n");
1484 #endif
1485 down(&start_stop_sem);
1486 /* look for a free place in the usbduxfast array */
1487 index = -1;
1488 for (i = 0; i < NUMUSBDUXFAST; i++) {
1489 if (!usbduxfastsub[i].probed) {
1490 index = i;
1491 break;
1495 /* no more space */
1496 if (index == -1) {
1497 printk(KERN_ERR "Too many usbduxfast-devices connected.\n");
1498 up(&start_stop_sem);
1499 return -EMFILE;
1501 #ifdef CONFIG_COMEDI_DEBUG
1502 printk(KERN_DEBUG "comedi_: usbduxfast: usbduxfastsub[%d] is ready to "
1503 "connect to comedi.\n", index);
1504 #endif
1506 init_MUTEX(&(usbduxfastsub[index].sem));
1507 /* save a pointer to the usb device */
1508 usbduxfastsub[index].usbdev = udev;
1510 /* save the interface itself */
1511 usbduxfastsub[index].interface = uinterf;
1512 /* get the interface number from the interface */
1513 usbduxfastsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
1515 * hand the private data over to the usb subsystem
1516 * will be needed for disconnect
1518 usb_set_intfdata(uinterf, &(usbduxfastsub[index]));
1520 #ifdef CONFIG_COMEDI_DEBUG
1521 printk(KERN_DEBUG "comedi_: usbduxfast: ifnum=%d\n",
1522 usbduxfastsub[index].ifnum);
1523 #endif
1524 /* create space for the commands going to the usb device */
1525 usbduxfastsub[index].dux_commands = kmalloc(SIZEOFDUXBUFFER,
1526 GFP_KERNEL);
1527 if (!usbduxfastsub[index].dux_commands) {
1528 printk(KERN_ERR "comedi_: usbduxfast: error alloc space for "
1529 "dac commands\n");
1530 tidy_up(&(usbduxfastsub[index]));
1531 up(&start_stop_sem);
1532 return -ENOMEM;
1534 /* create space of the instruction buffer */
1535 usbduxfastsub[index].insnBuffer = kmalloc(SIZEINSNBUF, GFP_KERNEL);
1536 if (!usbduxfastsub[index].insnBuffer) {
1537 printk(KERN_ERR "comedi_: usbduxfast: could not alloc space "
1538 "for insnBuffer\n");
1539 tidy_up(&(usbduxfastsub[index]));
1540 up(&start_stop_sem);
1541 return -ENOMEM;
1543 /* setting to alternate setting 1: enabling bulk ep */
1544 i = usb_set_interface(usbduxfastsub[index].usbdev,
1545 usbduxfastsub[index].ifnum, 1);
1546 if (i < 0) {
1547 printk(KERN_ERR "comedi_: usbduxfast%d: could not switch to "
1548 "alternate setting 1.\n", index);
1549 tidy_up(&(usbduxfastsub[index]));
1550 up(&start_stop_sem);
1551 return -ENODEV;
1553 usbduxfastsub[index].urbIn = usb_alloc_urb(0, GFP_KERNEL);
1554 if (!usbduxfastsub[index].urbIn) {
1555 printk(KERN_ERR "comedi_: usbduxfast%d: Could not alloc."
1556 "urb\n", index);
1557 tidy_up(&(usbduxfastsub[index]));
1558 up(&start_stop_sem);
1559 return -ENOMEM;
1561 usbduxfastsub[index].transfer_buffer = kmalloc(SIZEINBUF, GFP_KERNEL);
1562 if (!usbduxfastsub[index].transfer_buffer) {
1563 printk(KERN_ERR "comedi_: usbduxfast%d: could not alloc. "
1564 "transb.\n", index);
1565 tidy_up(&(usbduxfastsub[index]));
1566 up(&start_stop_sem);
1567 return -ENOMEM;
1569 /* we've reached the bottom of the function */
1570 usbduxfastsub[index].probed = 1;
1571 up(&start_stop_sem);
1573 ret = request_firmware_nowait(THIS_MODULE,
1574 FW_ACTION_HOTPLUG,
1575 "usbduxfast_firmware.bin",
1576 &udev->dev,
1577 GFP_KERNEL,
1578 usbduxfastsub + index,
1579 usbduxfast_firmware_request_complete_handler);
1581 if (ret) {
1582 dev_err(&udev->dev, "could not load firmware (err=%d)\n", ret);
1583 return ret;
1586 printk(KERN_INFO "comedi_: usbduxfast%d has been successfully "
1587 "initialized.\n", index);
1588 /* success */
1589 return 0;
1592 static void usbduxfastsub_disconnect(struct usb_interface *intf)
1594 struct usbduxfastsub_s *udfs = usb_get_intfdata(intf);
1595 struct usb_device *udev = interface_to_usbdev(intf);
1597 if (!udfs) {
1598 printk(KERN_ERR "comedi_: usbduxfast: disconnect called with "
1599 "null pointer.\n");
1600 return;
1602 if (udfs->usbdev != udev) {
1603 printk(KERN_ERR "comedi_: usbduxfast: BUG! called with wrong "
1604 "ptr!!!\n");
1605 return;
1608 comedi_usb_auto_unconfig(udev);
1610 down(&start_stop_sem);
1611 down(&udfs->sem);
1612 tidy_up(udfs);
1613 up(&udfs->sem);
1614 up(&start_stop_sem);
1616 #ifdef CONFIG_COMEDI_DEBUG
1617 printk(KERN_DEBUG "comedi_: usbduxfast: disconnected from the usb\n");
1618 #endif
1622 * is called when comedi-config is called
1624 static int usbduxfast_attach(struct comedi_device *dev,
1625 struct comedi_devconfig *it)
1627 int ret;
1628 int index;
1629 int i;
1630 struct comedi_subdevice *s = NULL;
1631 dev->private = NULL;
1633 down(&start_stop_sem);
1635 * find a valid device which has been detected by the
1636 * probe function of the usb
1638 index = -1;
1639 for (i = 0; i < NUMUSBDUXFAST; i++) {
1640 if (usbduxfastsub[i].probed && !usbduxfastsub[i].attached) {
1641 index = i;
1642 break;
1646 if (index < 0) {
1647 printk(KERN_ERR "comedi%d: usbduxfast: error: attach failed, "
1648 "no usbduxfast devs connected to the usb bus.\n",
1649 dev->minor);
1650 up(&start_stop_sem);
1651 return -ENODEV;
1654 down(&(usbduxfastsub[index].sem));
1655 /* pointer back to the corresponding comedi device */
1656 usbduxfastsub[index].comedidev = dev;
1658 /* trying to upload the firmware into the chip */
1659 if (comedi_aux_data(it->options, 0) &&
1660 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
1661 firmwareUpload(&usbduxfastsub[index],
1662 comedi_aux_data(it->options, 0),
1663 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
1666 dev->board_name = BOARDNAME;
1668 /* set number of subdevices */
1669 dev->n_subdevices = N_SUBDEVICES;
1671 /* allocate space for the subdevices */
1672 ret = alloc_subdevices(dev, N_SUBDEVICES);
1673 if (ret < 0) {
1674 printk(KERN_ERR "comedi%d: usbduxfast: error alloc space for "
1675 "subdev\n", dev->minor);
1676 up(&(usbduxfastsub[index].sem));
1677 up(&start_stop_sem);
1678 return ret;
1681 printk(KERN_INFO "comedi%d: usbduxfast: usb-device %d is attached to "
1682 "comedi.\n", dev->minor, index);
1683 /* private structure is also simply the usb-structure */
1684 dev->private = usbduxfastsub + index;
1685 /* the first subdevice is the A/D converter */
1686 s = dev->subdevices + SUBDEV_AD;
1688 * the URBs get the comedi subdevice which is responsible for reading
1689 * this is the subdevice which reads data
1691 dev->read_subdev = s;
1692 /* the subdevice receives as private structure the usb-structure */
1693 s->private = NULL;
1694 /* analog input */
1695 s->type = COMEDI_SUBD_AI;
1696 /* readable and ref is to ground */
1697 s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1698 /* 16 channels */
1699 s->n_chan = 16;
1700 /* length of the channellist */
1701 s->len_chanlist = 16;
1702 /* callback functions */
1703 s->insn_read = usbduxfast_ai_insn_read;
1704 s->do_cmdtest = usbduxfast_ai_cmdtest;
1705 s->do_cmd = usbduxfast_ai_cmd;
1706 s->cancel = usbduxfast_ai_cancel;
1707 /* max value from the A/D converter (12bit+1 bit for overflow) */
1708 s->maxdata = 0x1000;
1709 /* range table to convert to physical units */
1710 s->range_table = &range_usbduxfast_ai_range;
1712 /* finally decide that it's attached */
1713 usbduxfastsub[index].attached = 1;
1715 up(&(usbduxfastsub[index].sem));
1716 up(&start_stop_sem);
1717 printk(KERN_INFO "comedi%d: successfully attached to usbduxfast.\n",
1718 dev->minor);
1720 return 0;
1723 static int usbduxfast_detach(struct comedi_device *dev)
1725 struct usbduxfastsub_s *udfs;
1727 if (!dev) {
1728 printk(KERN_ERR "comedi?: usbduxfast: detach without dev "
1729 "variable...\n");
1730 return -EFAULT;
1732 #ifdef CONFIG_COMEDI_DEBUG
1733 printk(KERN_DEBUG "comedi%d: usbduxfast: detach usb device\n",
1734 dev->minor);
1735 #endif
1737 udfs = dev->private;
1738 if (!udfs) {
1739 printk(KERN_ERR "comedi?: usbduxfast: detach without ptr to "
1740 "usbduxfastsub[]\n");
1741 return -EFAULT;
1744 down(&udfs->sem);
1745 down(&start_stop_sem);
1747 * Don't allow detach to free the private structure
1748 * It's one entry of of usbduxfastsub[]
1750 dev->private = NULL;
1751 udfs->attached = 0;
1752 udfs->comedidev = NULL;
1753 #ifdef CONFIG_COMEDI_DEBUG
1754 printk(KERN_DEBUG "comedi%d: usbduxfast: detach: successfully "
1755 "removed\n", dev->minor);
1756 #endif
1757 up(&start_stop_sem);
1758 up(&udfs->sem);
1759 return 0;
1763 * main driver struct
1765 static struct comedi_driver driver_usbduxfast = {
1766 .driver_name = "usbduxfast",
1767 .module = THIS_MODULE,
1768 .attach = usbduxfast_attach,
1769 .detach = usbduxfast_detach
1773 * Table with the USB-devices: just now only testing IDs
1775 static const struct usb_device_id usbduxfastsub_table[] = {
1776 /* { USB_DEVICE(0x4b4, 0x8613) }, testing */
1777 {USB_DEVICE(0x13d8, 0x0010)}, /* real ID */
1778 {USB_DEVICE(0x13d8, 0x0011)}, /* real ID */
1779 {} /* Terminating entry */
1782 MODULE_DEVICE_TABLE(usb, usbduxfastsub_table);
1785 * The usbduxfastsub-driver
1787 static struct usb_driver usbduxfastsub_driver = {
1788 #ifdef COMEDI_HAVE_USB_DRIVER_OWNER
1789 .owner = THIS_MODULE,
1790 #endif
1791 .name = BOARDNAME,
1792 .probe = usbduxfastsub_probe,
1793 .disconnect = usbduxfastsub_disconnect,
1794 .id_table = usbduxfastsub_table
1798 * Can't use the nice macro as I have also to initialise the USB subsystem:
1799 * registering the usb-system _and_ the comedi-driver
1801 static int __init init_usbduxfast(void)
1803 printk(KERN_INFO
1804 KBUILD_MODNAME ": " DRIVER_VERSION ":" DRIVER_DESC "\n");
1805 usb_register(&usbduxfastsub_driver);
1806 comedi_driver_register(&driver_usbduxfast);
1807 return 0;
1811 * deregistering the comedi driver and the usb-subsystem
1813 static void __exit exit_usbduxfast(void)
1815 comedi_driver_unregister(&driver_usbduxfast);
1816 usb_deregister(&usbduxfastsub_driver);
1819 module_init(init_usbduxfast);
1820 module_exit(exit_usbduxfast);
1822 MODULE_AUTHOR(DRIVER_AUTHOR);
1823 MODULE_DESCRIPTION(DRIVER_DESC);
1824 MODULE_LICENSE("GPL");