Version:  2.0.40 2.2.26 2.4.37 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1

Linux/drivers/staging/comedi/drivers/usbdux.c

  1 /*
  2  * usbdux.c
  3  * Copyright (C) 2003-2014 Bernd Porr, mail@berndporr.me.uk
  4  *
  5  * This program is free software; you can redistribute it and/or modify
  6  * it under the terms of the GNU General Public License as published by
  7  * the Free Software Foundation; either version 2 of the License, or
  8  * (at your option) any later version.
  9 
 10  * This program is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU General Public License for more details.
 14  */
 15 
 16 /*
 17  * Driver: usbdux
 18  * Description: University of Stirling USB DAQ & INCITE Technology Limited
 19  * Devices: [ITL] USB-DUX (usbdux)
 20  * Author: Bernd Porr <mail@berndporr.me.uk>
 21  * Updated: 10 Oct 2014
 22  * Status: Stable
 23  *
 24  * Connection scheme for the counter at the digital port:
 25  * 0=/CLK0, 1=UP/DOWN0, 2=RESET0, 4=/CLK1, 5=UP/DOWN1, 6=RESET1.
 26  * The sampling rate of the counter is approximately 500Hz.
 27  *
 28  * Note that under USB2.0 the length of the channel list determines
 29  * the max sampling rate. If you sample only one channel you get 8kHz
 30  * sampling rate. If you sample two channels you get 4kHz and so on.
 31  */
 32 
 33 /*
 34  * I must give credit here to Chris Baugher who
 35  * wrote the driver for AT-MIO-16d. I used some parts of this
 36  * driver. I also must give credits to David Brownell
 37  * who supported me with the USB development.
 38  *
 39  * Bernd Porr
 40  *
 41  *
 42  * Revision history:
 43  * 0.94: D/A output should work now with any channel list combinations
 44  * 0.95: .owner commented out for kernel vers below 2.4.19
 45  *       sanity checks in ai/ao_cmd
 46  * 0.96: trying to get it working with 2.6, moved all memory alloc to comedi's
 47  *       attach final USB IDs
 48  *       moved memory allocation completely to the corresponding comedi
 49  *       functions firmware upload is by fxload and no longer by comedi (due to
 50  *       enumeration)
 51  * 0.97: USB IDs received, adjusted table
 52  * 0.98: SMP, locking, memory alloc: moved all usb memory alloc
 53  *       to the usb subsystem and moved all comedi related memory
 54  *       alloc to comedi.
 55  *       | kernel | registration | usbdux-usb | usbdux-comedi | comedi |
 56  * 0.99: USB 2.0: changed protocol to isochronous transfer
 57  *                IRQ transfer is too buggy and too risky in 2.0
 58  *                for the high speed ISO transfer is now a working version
 59  *                available
 60  * 0.99b: Increased the iso transfer buffer for high sp.to 10 buffers. Some VIA
 61  *        chipsets miss out IRQs. Deeper buffering is needed.
 62  * 1.00: full USB 2.0 support for the A/D converter. Now: max 8kHz sampling
 63  *       rate.
 64  *       Firmware vers 1.00 is needed for this.
 65  *       Two 16 bit up/down/reset counter with a sampling rate of 1kHz
 66  *       And loads of cleaning up, in particular streamlining the
 67  *       bulk transfers.
 68  * 1.1:  moved EP4 transfers to EP1 to make space for a PWM output on EP4
 69  * 1.2:  added PWM support via EP4
 70  * 2.0:  PWM seems to be stable and is not interfering with the other functions
 71  * 2.1:  changed PWM API
 72  * 2.2:  added firmware kernel request to fix an udev problem
 73  * 2.3:  corrected a bug in bulk timeouts which were far too short
 74  * 2.4:  fixed a bug which causes the driver to hang when it ran out of data.
 75  *       Thanks to Jan-Matthias Braun and Ian to spot the bug and fix it.
 76  *
 77  */
 78 
 79 #include <linux/kernel.h>
 80 #include <linux/module.h>
 81 #include <linux/slab.h>
 82 #include <linux/input.h>
 83 #include <linux/fcntl.h>
 84 #include <linux/compiler.h>
 85 
 86 #include "../comedi_usb.h"
 87 
 88 /* constants for firmware upload and download */
 89 #define USBDUX_FIRMWARE         "usbdux_firmware.bin"
 90 #define USBDUX_FIRMWARE_MAX_LEN 0x2000
 91 #define USBDUX_FIRMWARE_CMD     0xa0
 92 #define VENDOR_DIR_IN           0xc0
 93 #define VENDOR_DIR_OUT          0x40
 94 #define USBDUX_CPU_CS           0xe600
 95 
 96 /* usbdux bulk transfer commands */
 97 #define USBDUX_CMD_MULT_AI      0
 98 #define USBDUX_CMD_AO           1
 99 #define USBDUX_CMD_DIO_CFG      2
100 #define USBDUX_CMD_DIO_BITS     3
101 #define USBDUX_CMD_SINGLE_AI    4
102 #define USBDUX_CMD_TIMER_RD     5
103 #define USBDUX_CMD_TIMER_WR     6
104 #define USBDUX_CMD_PWM_ON       7
105 #define USBDUX_CMD_PWM_OFF      8
106 
107 /* timeout for the USB-transfer in ms */
108 #define BULK_TIMEOUT            1000
109 
110 /* 300Hz max frequ under PWM */
111 #define MIN_PWM_PERIOD  ((long)(1E9/300))
112 
113 /* Default PWM frequency */
114 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
115 
116 /* Size of one A/D value */
117 #define SIZEADIN          ((sizeof(uint16_t)))
118 
119 /*
120  * Size of the input-buffer IN BYTES
121  * Always multiple of 8 for 8 microframes which is needed in the highspeed mode
122  */
123 #define SIZEINBUF         ((8*SIZEADIN))
124 
125 /* 16 bytes. */
126 #define SIZEINSNBUF       16
127 
128 /* size of one value for the D/A converter: channel and value */
129 #define SIZEDAOUT          ((sizeof(uint8_t)+sizeof(uint16_t)))
130 
131 /*
132  * Size of the output-buffer in bytes
133  * Actually only the first 4 triplets are used but for the
134  * high speed mode we need to pad it to 8 (microframes).
135  */
136 #define SIZEOUTBUF         ((8*SIZEDAOUT))
137 
138 /*
139  * Size of the buffer for the dux commands: just now max size is determined
140  * by the analogue out + command byte + panic bytes...
141  */
142 #define SIZEOFDUXBUFFER    ((8*SIZEDAOUT+2))
143 
144 /* Number of in-URBs which receive the data: min=2 */
145 #define NUMOFINBUFFERSFULL     5
146 
147 /* Number of out-URBs which send the data: min=2 */
148 #define NUMOFOUTBUFFERSFULL    5
149 
150 /* Number of in-URBs which receive the data: min=5 */
151 /* must have more buffers due to buggy USB ctr */
152 #define NUMOFINBUFFERSHIGH     10
153 
154 /* Number of out-URBs which send the data: min=5 */
155 /* must have more buffers due to buggy USB ctr */
156 #define NUMOFOUTBUFFERSHIGH    10
157 
158 /* number of retries to get the right dux command */
159 #define RETRIES 10
160 
161 static const struct comedi_lrange range_usbdux_ai_range = {
162         4, {
163                 BIP_RANGE(4.096),
164                 BIP_RANGE(4.096 / 2),
165                 UNI_RANGE(4.096),
166                 UNI_RANGE(4.096 / 2)
167         }
168 };
169 
170 static const struct comedi_lrange range_usbdux_ao_range = {
171         2, {
172                 BIP_RANGE(4.096),
173                 UNI_RANGE(4.096)
174         }
175 };
176 
177 struct usbdux_private {
178         /* actual number of in-buffers */
179         int n_ai_urbs;
180         /* actual number of out-buffers */
181         int n_ao_urbs;
182         /* ISO-transfer handling: buffers */
183         struct urb **ai_urbs;
184         struct urb **ao_urbs;
185         /* pwm-transfer handling */
186         struct urb *pwm_urb;
187         /* PWM period */
188         unsigned int pwm_period;
189         /* PWM internal delay for the GPIF in the FX2 */
190         uint8_t pwm_delay;
191         /* size of the PWM buffer which holds the bit pattern */
192         int pwm_buf_sz;
193         /* input buffer for the ISO-transfer */
194         __le16 *in_buf;
195         /* input buffer for single insn */
196         __le16 *insn_buf;
197 
198         unsigned int high_speed:1;
199         unsigned int ai_cmd_running:1;
200         unsigned int ao_cmd_running:1;
201         unsigned int pwm_cmd_running:1;
202 
203         /* time between samples in units of the timer */
204         unsigned int ai_timer;
205         unsigned int ao_timer;
206         /* counter between aquisitions */
207         unsigned int ai_counter;
208         unsigned int ao_counter;
209         /* interval in frames/uframes */
210         unsigned int ai_interval;
211         /* commands */
212         uint8_t *dux_commands;
213         struct semaphore sem;
214 };
215 
216 static void usbdux_unlink_urbs(struct urb **urbs, int num_urbs)
217 {
218         int i;
219 
220         for (i = 0; i < num_urbs; i++)
221                 usb_kill_urb(urbs[i]);
222 }
223 
224 static void usbdux_ai_stop(struct comedi_device *dev, int do_unlink)
225 {
226         struct usbdux_private *devpriv = dev->private;
227 
228         if (do_unlink && devpriv->ai_urbs)
229                 usbdux_unlink_urbs(devpriv->ai_urbs, devpriv->n_ai_urbs);
230 
231         devpriv->ai_cmd_running = 0;
232 }
233 
234 static int usbdux_ai_cancel(struct comedi_device *dev,
235                             struct comedi_subdevice *s)
236 {
237         struct usbdux_private *devpriv = dev->private;
238 
239         /* prevent other CPUs from submitting new commands just now */
240         down(&devpriv->sem);
241         /* unlink only if the urb really has been submitted */
242         usbdux_ai_stop(dev, devpriv->ai_cmd_running);
243         up(&devpriv->sem);
244 
245         return 0;
246 }
247 
248 static void usbduxsub_ai_handle_urb(struct comedi_device *dev,
249                                     struct comedi_subdevice *s,
250                                     struct urb *urb)
251 {
252         struct usbdux_private *devpriv = dev->private;
253         struct comedi_async *async = s->async;
254         struct comedi_cmd *cmd = &async->cmd;
255         int ret;
256         int i;
257 
258         devpriv->ai_counter--;
259         if (devpriv->ai_counter == 0) {
260                 devpriv->ai_counter = devpriv->ai_timer;
261 
262                 /* get the data from the USB bus and hand it over to comedi */
263                 for (i = 0; i < cmd->chanlist_len; i++) {
264                         unsigned int range = CR_RANGE(cmd->chanlist[i]);
265                         uint16_t val = le16_to_cpu(devpriv->in_buf[i]);
266 
267                         /* bipolar data is two's-complement */
268                         if (comedi_range_is_bipolar(s, range))
269                                 val ^= ((s->maxdata + 1) >> 1);
270 
271                         /* transfer data */
272                         if (!comedi_buf_write_samples(s, &val, 1))
273                                 return;
274                 }
275 
276                 if (cmd->stop_src == TRIG_COUNT &&
277                     async->scans_done >= cmd->stop_arg)
278                         async->events |= COMEDI_CB_EOA;
279         }
280 
281         /* if command is still running, resubmit urb */
282         if (!(async->events & COMEDI_CB_CANCEL_MASK)) {
283                 urb->dev = comedi_to_usb_dev(dev);
284                 ret = usb_submit_urb(urb, GFP_ATOMIC);
285                 if (ret < 0) {
286                         dev_err(dev->class_dev,
287                                 "urb resubmit failed in int-context! err=%d\n",
288                                 ret);
289                         if (ret == -EL2NSYNC)
290                                 dev_err(dev->class_dev,
291                                         "buggy USB host controller or bug in IRQ handler!\n");
292                         async->events |= COMEDI_CB_ERROR;
293                 }
294         }
295 }
296 
297 static void usbduxsub_ai_isoc_irq(struct urb *urb)
298 {
299         struct comedi_device *dev = urb->context;
300         struct comedi_subdevice *s = dev->read_subdev;
301         struct comedi_async *async = s->async;
302         struct usbdux_private *devpriv = dev->private;
303 
304         /* exit if not running a command, do not resubmit urb */
305         if (!devpriv->ai_cmd_running)
306                 return;
307 
308         switch (urb->status) {
309         case 0:
310                 /* copy the result in the transfer buffer */
311                 memcpy(devpriv->in_buf, urb->transfer_buffer, SIZEINBUF);
312                 usbduxsub_ai_handle_urb(dev, s, urb);
313                 break;
314 
315         case -EILSEQ:
316                 /*
317                  * error in the ISOchronous data
318                  * we don't copy the data into the transfer buffer
319                  * and recycle the last data byte
320                  */
321                 dev_dbg(dev->class_dev, "CRC error in ISO IN stream\n");
322                 usbduxsub_ai_handle_urb(dev, s, urb);
323                 break;
324 
325         case -ECONNRESET:
326         case -ENOENT:
327         case -ESHUTDOWN:
328         case -ECONNABORTED:
329                 /* after an unlink command, unplug, ... etc */
330                 async->events |= COMEDI_CB_ERROR;
331                 break;
332 
333         default:
334                 /* a real error */
335                 dev_err(dev->class_dev,
336                         "Non-zero urb status received in ai intr context: %d\n",
337                         urb->status);
338                 async->events |= COMEDI_CB_ERROR;
339                 break;
340         }
341 
342         /*
343          * comedi_handle_events() cannot be used in this driver. The (*cancel)
344          * operation would unlink the urb.
345          */
346         if (async->events & COMEDI_CB_CANCEL_MASK)
347                 usbdux_ai_stop(dev, 0);
348 
349         comedi_event(dev, s);
350 }
351 
352 static void usbdux_ao_stop(struct comedi_device *dev, int do_unlink)
353 {
354         struct usbdux_private *devpriv = dev->private;
355 
356         if (do_unlink && devpriv->ao_urbs)
357                 usbdux_unlink_urbs(devpriv->ao_urbs, devpriv->n_ao_urbs);
358 
359         devpriv->ao_cmd_running = 0;
360 }
361 
362 static int usbdux_ao_cancel(struct comedi_device *dev,
363                             struct comedi_subdevice *s)
364 {
365         struct usbdux_private *devpriv = dev->private;
366 
367         /* prevent other CPUs from submitting a command just now */
368         down(&devpriv->sem);
369         /* unlink only if it is really running */
370         usbdux_ao_stop(dev, devpriv->ao_cmd_running);
371         up(&devpriv->sem);
372 
373         return 0;
374 }
375 
376 static void usbduxsub_ao_handle_urb(struct comedi_device *dev,
377                                     struct comedi_subdevice *s,
378                                     struct urb *urb)
379 {
380         struct usbdux_private *devpriv = dev->private;
381         struct comedi_async *async = s->async;
382         struct comedi_cmd *cmd = &async->cmd;
383         uint8_t *datap;
384         int ret;
385         int i;
386 
387         devpriv->ao_counter--;
388         if (devpriv->ao_counter == 0) {
389                 devpriv->ao_counter = devpriv->ao_timer;
390 
391                 if (cmd->stop_src == TRIG_COUNT &&
392                     async->scans_done >= cmd->stop_arg) {
393                         async->events |= COMEDI_CB_EOA;
394                         return;
395                 }
396 
397                 /* transmit data to the USB bus */
398                 datap = urb->transfer_buffer;
399                 *datap++ = cmd->chanlist_len;
400                 for (i = 0; i < cmd->chanlist_len; i++) {
401                         unsigned int chan = CR_CHAN(cmd->chanlist[i]);
402                         unsigned short val;
403 
404                         if (!comedi_buf_read_samples(s, &val, 1)) {
405                                 dev_err(dev->class_dev, "buffer underflow\n");
406                                 async->events |= COMEDI_CB_OVERFLOW;
407                                 return;
408                         }
409 
410                         /* pointer to the DA */
411                         *datap++ = val & 0xff;
412                         *datap++ = (val >> 8) & 0xff;
413                         *datap++ = chan << 6;
414                         s->readback[chan] = val;
415                 }
416         }
417 
418         /* if command is still running, resubmit urb for BULK transfer */
419         if (!(async->events & COMEDI_CB_CANCEL_MASK)) {
420                 urb->transfer_buffer_length = SIZEOUTBUF;
421                 urb->dev = comedi_to_usb_dev(dev);
422                 urb->status = 0;
423                 if (devpriv->high_speed)
424                         urb->interval = 8;      /* uframes */
425                 else
426                         urb->interval = 1;      /* frames */
427                 urb->number_of_packets = 1;
428                 urb->iso_frame_desc[0].offset = 0;
429                 urb->iso_frame_desc[0].length = SIZEOUTBUF;
430                 urb->iso_frame_desc[0].status = 0;
431                 ret = usb_submit_urb(urb, GFP_ATOMIC);
432                 if (ret < 0) {
433                         dev_err(dev->class_dev,
434                                 "ao urb resubm failed in int-cont. ret=%d",
435                                 ret);
436                         if (ret == -EL2NSYNC)
437                                 dev_err(dev->class_dev,
438                                         "buggy USB host controller or bug in IRQ handling!\n");
439                         async->events |= COMEDI_CB_ERROR;
440                 }
441         }
442 }
443 
444 static void usbduxsub_ao_isoc_irq(struct urb *urb)
445 {
446         struct comedi_device *dev = urb->context;
447         struct comedi_subdevice *s = dev->write_subdev;
448         struct comedi_async *async = s->async;
449         struct usbdux_private *devpriv = dev->private;
450 
451         /* exit if not running a command, do not resubmit urb */
452         if (!devpriv->ao_cmd_running)
453                 return;
454 
455         switch (urb->status) {
456         case 0:
457                 usbduxsub_ao_handle_urb(dev, s, urb);
458                 break;
459 
460         case -ECONNRESET:
461         case -ENOENT:
462         case -ESHUTDOWN:
463         case -ECONNABORTED:
464                 /* after an unlink command, unplug, ... etc */
465                 async->events |= COMEDI_CB_ERROR;
466                 break;
467 
468         default:
469                 /* a real error */
470                 dev_err(dev->class_dev,
471                         "Non-zero urb status received in ao intr context: %d\n",
472                         urb->status);
473                 async->events |= COMEDI_CB_ERROR;
474                 break;
475         }
476 
477         /*
478          * comedi_handle_events() cannot be used in this driver. The (*cancel)
479          * operation would unlink the urb.
480          */
481         if (async->events & COMEDI_CB_CANCEL_MASK)
482                 usbdux_ao_stop(dev, 0);
483 
484         comedi_event(dev, s);
485 }
486 
487 static int usbdux_submit_urbs(struct comedi_device *dev,
488                               struct urb **urbs, int num_urbs,
489                               int input_urb)
490 {
491         struct usb_device *usb = comedi_to_usb_dev(dev);
492         struct usbdux_private *devpriv = dev->private;
493         struct urb *urb;
494         int ret;
495         int i;
496 
497         /* Submit all URBs and start the transfer on the bus */
498         for (i = 0; i < num_urbs; i++) {
499                 urb = urbs[i];
500 
501                 /* in case of a resubmission after an unlink... */
502                 if (input_urb)
503                         urb->interval = devpriv->ai_interval;
504                 urb->context = dev;
505                 urb->dev = usb;
506                 urb->status = 0;
507                 urb->transfer_flags = URB_ISO_ASAP;
508 
509                 ret = usb_submit_urb(urb, GFP_ATOMIC);
510                 if (ret)
511                         return ret;
512         }
513         return 0;
514 }
515 
516 static int usbdux_ai_cmdtest(struct comedi_device *dev,
517                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
518 {
519         struct usbdux_private *this_usbduxsub = dev->private;
520         int err = 0, i;
521         unsigned int tmp_timer;
522 
523         /* Step 1 : check if triggers are trivially valid */
524 
525         err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
526         err |= comedi_check_trigger_src(&cmd->scan_begin_src, TRIG_TIMER);
527         err |= comedi_check_trigger_src(&cmd->convert_src, TRIG_NOW);
528         err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
529         err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
530 
531         if (err)
532                 return 1;
533 
534         /* Step 2a : make sure trigger sources are unique */
535 
536         err |= comedi_check_trigger_is_unique(cmd->start_src);
537         err |= comedi_check_trigger_is_unique(cmd->stop_src);
538 
539         /* Step 2b : and mutually compatible */
540 
541         if (err)
542                 return 2;
543 
544         /* Step 3: check if arguments are trivially valid */
545 
546         err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
547 
548         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
549                 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
550 
551         if (cmd->scan_begin_src == TRIG_TIMER) {
552                 if (this_usbduxsub->high_speed) {
553                         /*
554                          * In high speed mode microframes are possible.
555                          * However, during one microframe we can roughly
556                          * sample one channel. Thus, the more channels
557                          * are in the channel list the more time we need.
558                          */
559                         i = 1;
560                         /* find a power of 2 for the number of channels */
561                         while (i < (cmd->chanlist_len))
562                                 i = i * 2;
563 
564                         err |= comedi_check_trigger_arg_min(&cmd->
565                                                             scan_begin_arg,
566                                                             1000000 / 8 * i);
567                         /* now calc the real sampling rate with all the
568                          * rounding errors */
569                         tmp_timer =
570                             ((unsigned int)(cmd->scan_begin_arg / 125000)) *
571                             125000;
572                 } else {
573                         /* full speed */
574                         /* 1kHz scans every USB frame */
575                         err |= comedi_check_trigger_arg_min(&cmd->
576                                                             scan_begin_arg,
577                                                             1000000);
578                         /*
579                          * calc the real sampling rate with the rounding errors
580                          */
581                         tmp_timer = ((unsigned int)(cmd->scan_begin_arg /
582                                                    1000000)) * 1000000;
583                 }
584                 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg,
585                                                    tmp_timer);
586         }
587 
588         err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
589                                            cmd->chanlist_len);
590 
591         if (cmd->stop_src == TRIG_COUNT)
592                 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
593         else    /* TRIG_NONE */
594                 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
595 
596         if (err)
597                 return 3;
598 
599         return 0;
600 }
601 
602 /*
603  * creates the ADC command for the MAX1271
604  * range is the range value from comedi
605  */
606 static uint8_t create_adc_command(unsigned int chan, unsigned int range)
607 {
608         uint8_t p = (range <= 1);
609         uint8_t r = ((range % 2) == 0);
610 
611         return (chan << 4) | ((p == 1) << 2) | ((r == 1) << 3);
612 }
613 
614 static int send_dux_commands(struct comedi_device *dev, unsigned int cmd_type)
615 {
616         struct usb_device *usb = comedi_to_usb_dev(dev);
617         struct usbdux_private *devpriv = dev->private;
618         int nsent;
619 
620         devpriv->dux_commands[0] = cmd_type;
621 
622         return usb_bulk_msg(usb, usb_sndbulkpipe(usb, 1),
623                             devpriv->dux_commands, SIZEOFDUXBUFFER,
624                             &nsent, BULK_TIMEOUT);
625 }
626 
627 static int receive_dux_commands(struct comedi_device *dev, unsigned int command)
628 {
629         struct usb_device *usb = comedi_to_usb_dev(dev);
630         struct usbdux_private *devpriv = dev->private;
631         int ret;
632         int nrec;
633         int i;
634 
635         for (i = 0; i < RETRIES; i++) {
636                 ret = usb_bulk_msg(usb, usb_rcvbulkpipe(usb, 8),
637                                    devpriv->insn_buf, SIZEINSNBUF,
638                                    &nrec, BULK_TIMEOUT);
639                 if (ret < 0)
640                         return ret;
641                 if (le16_to_cpu(devpriv->insn_buf[0]) == command)
642                         return ret;
643         }
644         /* command not received */
645         return -EFAULT;
646 }
647 
648 static int usbdux_ai_inttrig(struct comedi_device *dev,
649                              struct comedi_subdevice *s,
650                              unsigned int trig_num)
651 {
652         struct usbdux_private *devpriv = dev->private;
653         struct comedi_cmd *cmd = &s->async->cmd;
654         int ret;
655 
656         if (trig_num != cmd->start_arg)
657                 return -EINVAL;
658 
659         down(&devpriv->sem);
660 
661         if (!devpriv->ai_cmd_running) {
662                 devpriv->ai_cmd_running = 1;
663                 ret = usbdux_submit_urbs(dev, devpriv->ai_urbs,
664                                          devpriv->n_ai_urbs, 1);
665                 if (ret < 0) {
666                         devpriv->ai_cmd_running = 0;
667                         goto ai_trig_exit;
668                 }
669                 s->async->inttrig = NULL;
670         } else {
671                 ret = -EBUSY;
672         }
673 
674 ai_trig_exit:
675         up(&devpriv->sem);
676         return ret;
677 }
678 
679 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
680 {
681         struct usbdux_private *devpriv = dev->private;
682         struct comedi_cmd *cmd = &s->async->cmd;
683         int len = cmd->chanlist_len;
684         int ret = -EBUSY;
685         int i;
686 
687         /* block other CPUs from starting an ai_cmd */
688         down(&devpriv->sem);
689 
690         if (devpriv->ai_cmd_running)
691                 goto ai_cmd_exit;
692 
693         devpriv->dux_commands[1] = len;
694         for (i = 0; i < len; ++i) {
695                 unsigned int chan = CR_CHAN(cmd->chanlist[i]);
696                 unsigned int range = CR_RANGE(cmd->chanlist[i]);
697 
698                 devpriv->dux_commands[i + 2] = create_adc_command(chan, range);
699         }
700 
701         ret = send_dux_commands(dev, USBDUX_CMD_MULT_AI);
702         if (ret < 0)
703                 goto ai_cmd_exit;
704 
705         if (devpriv->high_speed) {
706                 /*
707                  * every channel gets a time window of 125us. Thus, if we
708                  * sample all 8 channels we need 1ms. If we sample only one
709                  * channel we need only 125us
710                  */
711                 devpriv->ai_interval = 1;
712                 /* find a power of 2 for the interval */
713                 while (devpriv->ai_interval < len)
714                         devpriv->ai_interval *= 2;
715 
716                 devpriv->ai_timer = cmd->scan_begin_arg /
717                                     (125000 * devpriv->ai_interval);
718         } else {
719                 /* interval always 1ms */
720                 devpriv->ai_interval = 1;
721                 devpriv->ai_timer = cmd->scan_begin_arg / 1000000;
722         }
723         if (devpriv->ai_timer < 1) {
724                 ret = -EINVAL;
725                 goto ai_cmd_exit;
726         }
727 
728         devpriv->ai_counter = devpriv->ai_timer;
729 
730         if (cmd->start_src == TRIG_NOW) {
731                 /* enable this acquisition operation */
732                 devpriv->ai_cmd_running = 1;
733                 ret = usbdux_submit_urbs(dev, devpriv->ai_urbs,
734                                          devpriv->n_ai_urbs, 1);
735                 if (ret < 0) {
736                         devpriv->ai_cmd_running = 0;
737                         /* fixme: unlink here?? */
738                         goto ai_cmd_exit;
739                 }
740                 s->async->inttrig = NULL;
741         } else {
742                 /* TRIG_INT */
743                 /* don't enable the acquision operation */
744                 /* wait for an internal signal */
745                 s->async->inttrig = usbdux_ai_inttrig;
746         }
747 
748 ai_cmd_exit:
749         up(&devpriv->sem);
750 
751         return ret;
752 }
753 
754 /* Mode 0 is used to get a single conversion on demand */
755 static int usbdux_ai_insn_read(struct comedi_device *dev,
756                                struct comedi_subdevice *s,
757                                struct comedi_insn *insn,
758                                unsigned int *data)
759 {
760         struct usbdux_private *devpriv = dev->private;
761         unsigned int chan = CR_CHAN(insn->chanspec);
762         unsigned int range = CR_RANGE(insn->chanspec);
763         unsigned int val;
764         int ret = -EBUSY;
765         int i;
766 
767         down(&devpriv->sem);
768 
769         if (devpriv->ai_cmd_running)
770                 goto ai_read_exit;
771 
772         /* set command for the first channel */
773         devpriv->dux_commands[1] = create_adc_command(chan, range);
774 
775         /* adc commands */
776         ret = send_dux_commands(dev, USBDUX_CMD_SINGLE_AI);
777         if (ret < 0)
778                 goto ai_read_exit;
779 
780         for (i = 0; i < insn->n; i++) {
781                 ret = receive_dux_commands(dev, USBDUX_CMD_SINGLE_AI);
782                 if (ret < 0)
783                         goto ai_read_exit;
784 
785                 val = le16_to_cpu(devpriv->insn_buf[1]);
786 
787                 /* bipolar data is two's-complement */
788                 if (comedi_range_is_bipolar(s, range))
789                         val ^= ((s->maxdata + 1) >> 1);
790 
791                 data[i] = val;
792         }
793 
794 ai_read_exit:
795         up(&devpriv->sem);
796 
797         return ret ? ret : insn->n;
798 }
799 
800 static int usbdux_ao_insn_read(struct comedi_device *dev,
801                                struct comedi_subdevice *s,
802                                struct comedi_insn *insn,
803                                unsigned int *data)
804 {
805         struct usbdux_private *devpriv = dev->private;
806         int ret;
807 
808         down(&devpriv->sem);
809         ret = comedi_readback_insn_read(dev, s, insn, data);
810         up(&devpriv->sem);
811 
812         return ret;
813 }
814 
815 static int usbdux_ao_insn_write(struct comedi_device *dev,
816                                 struct comedi_subdevice *s,
817                                 struct comedi_insn *insn,
818                                 unsigned int *data)
819 {
820         struct usbdux_private *devpriv = dev->private;
821         unsigned int chan = CR_CHAN(insn->chanspec);
822         unsigned int val = s->readback[chan];
823         __le16 *p = (__le16 *)&devpriv->dux_commands[2];
824         int ret = -EBUSY;
825         int i;
826 
827         down(&devpriv->sem);
828 
829         if (devpriv->ao_cmd_running)
830                 goto ao_write_exit;
831 
832         /* number of channels: 1 */
833         devpriv->dux_commands[1] = 1;
834         /* channel number */
835         devpriv->dux_commands[4] = chan << 6;
836 
837         for (i = 0; i < insn->n; i++) {
838                 val = data[i];
839 
840                 /* one 16 bit value */
841                 *p = cpu_to_le16(val);
842 
843                 ret = send_dux_commands(dev, USBDUX_CMD_AO);
844                 if (ret < 0)
845                         goto ao_write_exit;
846 
847                 s->readback[chan] = val;
848         }
849 
850 ao_write_exit:
851         up(&devpriv->sem);
852 
853         return ret ? ret : insn->n;
854 }
855 
856 static int usbdux_ao_inttrig(struct comedi_device *dev,
857                              struct comedi_subdevice *s,
858                              unsigned int trig_num)
859 {
860         struct usbdux_private *devpriv = dev->private;
861         struct comedi_cmd *cmd = &s->async->cmd;
862         int ret;
863 
864         if (trig_num != cmd->start_arg)
865                 return -EINVAL;
866 
867         down(&devpriv->sem);
868 
869         if (!devpriv->ao_cmd_running) {
870                 devpriv->ao_cmd_running = 1;
871                 ret = usbdux_submit_urbs(dev, devpriv->ao_urbs,
872                                          devpriv->n_ao_urbs, 0);
873                 if (ret < 0) {
874                         devpriv->ao_cmd_running = 0;
875                         goto ao_trig_exit;
876                 }
877                 s->async->inttrig = NULL;
878         } else {
879                 ret = -EBUSY;
880         }
881 
882 ao_trig_exit:
883         up(&devpriv->sem);
884         return ret;
885 }
886 
887 static int usbdux_ao_cmdtest(struct comedi_device *dev,
888                              struct comedi_subdevice *s, struct comedi_cmd *cmd)
889 {
890         struct usbdux_private *this_usbduxsub = dev->private;
891         int err = 0;
892         unsigned int flags;
893 
894         if (!this_usbduxsub)
895                 return -EFAULT;
896 
897         /* Step 1 : check if triggers are trivially valid */
898 
899         err |= comedi_check_trigger_src(&cmd->start_src, TRIG_NOW | TRIG_INT);
900 
901         if (0) {                /* (this_usbduxsub->high_speed) */
902                 /* the sampling rate is set by the coversion rate */
903                 flags = TRIG_FOLLOW;
904         } else {
905                 /* start a new scan (output at once) with a timer */
906                 flags = TRIG_TIMER;
907         }
908         err |= comedi_check_trigger_src(&cmd->scan_begin_src, flags);
909 
910         if (0) {                /* (this_usbduxsub->high_speed) */
911                 /*
912                  * in usb-2.0 only one conversion it transmitted
913                  * but with 8kHz/n
914                  */
915                 flags = TRIG_TIMER;
916         } else {
917                 /*
918                  * all conversion events happen simultaneously with
919                  * a rate of 1kHz/n
920                  */
921                 flags = TRIG_NOW;
922         }
923         err |= comedi_check_trigger_src(&cmd->convert_src, flags);
924 
925         err |= comedi_check_trigger_src(&cmd->scan_end_src, TRIG_COUNT);
926         err |= comedi_check_trigger_src(&cmd->stop_src, TRIG_COUNT | TRIG_NONE);
927 
928         if (err)
929                 return 1;
930 
931         /* Step 2a : make sure trigger sources are unique */
932 
933         err |= comedi_check_trigger_is_unique(cmd->start_src);
934         err |= comedi_check_trigger_is_unique(cmd->stop_src);
935 
936         /* Step 2b : and mutually compatible */
937 
938         if (err)
939                 return 2;
940 
941         /* Step 3: check if arguments are trivially valid */
942 
943         err |= comedi_check_trigger_arg_is(&cmd->start_arg, 0);
944 
945         if (cmd->scan_begin_src == TRIG_FOLLOW) /* internal trigger */
946                 err |= comedi_check_trigger_arg_is(&cmd->scan_begin_arg, 0);
947 
948         if (cmd->scan_begin_src == TRIG_TIMER) {
949                 err |= comedi_check_trigger_arg_min(&cmd->scan_begin_arg,
950                                                     1000000);
951         }
952 
953         /* not used now, is for later use */
954         if (cmd->convert_src == TRIG_TIMER)
955                 err |= comedi_check_trigger_arg_min(&cmd->convert_arg, 125000);
956 
957         err |= comedi_check_trigger_arg_is(&cmd->scan_end_arg,
958                                            cmd->chanlist_len);
959 
960         if (cmd->stop_src == TRIG_COUNT)
961                 err |= comedi_check_trigger_arg_min(&cmd->stop_arg, 1);
962         else    /* TRIG_NONE */
963                 err |= comedi_check_trigger_arg_is(&cmd->stop_arg, 0);
964 
965         if (err)
966                 return 3;
967 
968         return 0;
969 }
970 
971 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
972 {
973         struct usbdux_private *devpriv = dev->private;
974         struct comedi_cmd *cmd = &s->async->cmd;
975         int ret = -EBUSY;
976 
977         down(&devpriv->sem);
978 
979         if (devpriv->ao_cmd_running)
980                 goto ao_cmd_exit;
981 
982         /* we count in steps of 1ms (125us) */
983         /* 125us mode not used yet */
984         if (0) {                /* (devpriv->high_speed) */
985                 /* 125us */
986                 /* timing of the conversion itself: every 125 us */
987                 devpriv->ao_timer = cmd->convert_arg / 125000;
988         } else {
989                 /* 1ms */
990                 /* timing of the scan: we get all channels at once */
991                 devpriv->ao_timer = cmd->scan_begin_arg / 1000000;
992                 if (devpriv->ao_timer < 1) {
993                         ret = -EINVAL;
994                         goto ao_cmd_exit;
995                 }
996         }
997 
998         devpriv->ao_counter = devpriv->ao_timer;
999 
1000         if (cmd->start_src == TRIG_NOW) {
1001                 /* enable this acquisition operation */
1002                 devpriv->ao_cmd_running = 1;
1003                 ret = usbdux_submit_urbs(dev, devpriv->ao_urbs,
1004                                          devpriv->n_ao_urbs, 0);
1005                 if (ret < 0) {
1006                         devpriv->ao_cmd_running = 0;
1007                         /* fixme: unlink here?? */
1008                         goto ao_cmd_exit;
1009                 }
1010                 s->async->inttrig = NULL;
1011         } else {
1012                 /* TRIG_INT */
1013                 /* submit the urbs later */
1014                 /* wait for an internal signal */
1015                 s->async->inttrig = usbdux_ao_inttrig;
1016         }
1017 
1018 ao_cmd_exit:
1019         up(&devpriv->sem);
1020 
1021         return ret;
1022 }
1023 
1024 static int usbdux_dio_insn_config(struct comedi_device *dev,
1025                                   struct comedi_subdevice *s,
1026                                   struct comedi_insn *insn,
1027                                   unsigned int *data)
1028 {
1029         int ret;
1030 
1031         ret = comedi_dio_insn_config(dev, s, insn, data, 0);
1032         if (ret)
1033                 return ret;
1034 
1035         /*
1036          * We don't tell the firmware here as it would take 8 frames
1037          * to submit the information. We do it in the insn_bits.
1038          */
1039         return insn->n;
1040 }
1041 
1042 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1043                                 struct comedi_subdevice *s,
1044                                 struct comedi_insn *insn,
1045                                 unsigned int *data)
1046 {
1047         struct usbdux_private *devpriv = dev->private;
1048         int ret;
1049 
1050         down(&devpriv->sem);
1051 
1052         comedi_dio_update_state(s, data);
1053 
1054         /* Always update the hardware. See the (*insn_config). */
1055         devpriv->dux_commands[1] = s->io_bits;
1056         devpriv->dux_commands[2] = s->state;
1057 
1058         /*
1059          * This command also tells the firmware to return
1060          * the digital input lines.
1061          */
1062         ret = send_dux_commands(dev, USBDUX_CMD_DIO_BITS);
1063         if (ret < 0)
1064                 goto dio_exit;
1065         ret = receive_dux_commands(dev, USBDUX_CMD_DIO_BITS);
1066         if (ret < 0)
1067                 goto dio_exit;
1068 
1069         data[1] = le16_to_cpu(devpriv->insn_buf[1]);
1070 
1071 dio_exit:
1072         up(&devpriv->sem);
1073 
1074         return ret ? ret : insn->n;
1075 }
1076 
1077 static int usbdux_counter_read(struct comedi_device *dev,
1078                                struct comedi_subdevice *s,
1079                                struct comedi_insn *insn,
1080                                unsigned int *data)
1081 {
1082         struct usbdux_private *devpriv = dev->private;
1083         unsigned int chan = CR_CHAN(insn->chanspec);
1084         int ret = 0;
1085         int i;
1086 
1087         down(&devpriv->sem);
1088 
1089         for (i = 0; i < insn->n; i++) {
1090                 ret = send_dux_commands(dev, USBDUX_CMD_TIMER_RD);
1091                 if (ret < 0)
1092                         goto counter_read_exit;
1093                 ret = receive_dux_commands(dev, USBDUX_CMD_TIMER_RD);
1094                 if (ret < 0)
1095                         goto counter_read_exit;
1096 
1097                 data[i] = le16_to_cpu(devpriv->insn_buf[chan + 1]);
1098         }
1099 
1100 counter_read_exit:
1101         up(&devpriv->sem);
1102 
1103         return ret ? ret : insn->n;
1104 }
1105 
1106 static int usbdux_counter_write(struct comedi_device *dev,
1107                                 struct comedi_subdevice *s,
1108                                 struct comedi_insn *insn,
1109                                 unsigned int *data)
1110 {
1111         struct usbdux_private *devpriv = dev->private;
1112         unsigned int chan = CR_CHAN(insn->chanspec);
1113         __le16 *p = (__le16 *)&devpriv->dux_commands[2];
1114         int ret = 0;
1115         int i;
1116 
1117         down(&devpriv->sem);
1118 
1119         devpriv->dux_commands[1] = chan;
1120 
1121         for (i = 0; i < insn->n; i++) {
1122                 *p = cpu_to_le16(data[i]);
1123 
1124                 ret = send_dux_commands(dev, USBDUX_CMD_TIMER_WR);
1125                 if (ret < 0)
1126                         break;
1127         }
1128 
1129         up(&devpriv->sem);
1130 
1131         return ret ? ret : insn->n;
1132 }
1133 
1134 static int usbdux_counter_config(struct comedi_device *dev,
1135                                  struct comedi_subdevice *s,
1136                                  struct comedi_insn *insn, unsigned int *data)
1137 {
1138         /* nothing to do so far */
1139         return 2;
1140 }
1141 
1142 static void usbduxsub_unlink_pwm_urbs(struct comedi_device *dev)
1143 {
1144         struct usbdux_private *devpriv = dev->private;
1145 
1146         usb_kill_urb(devpriv->pwm_urb);
1147 }
1148 
1149 static void usbdux_pwm_stop(struct comedi_device *dev, int do_unlink)
1150 {
1151         struct usbdux_private *devpriv = dev->private;
1152 
1153         if (do_unlink)
1154                 usbduxsub_unlink_pwm_urbs(dev);
1155 
1156         devpriv->pwm_cmd_running = 0;
1157 }
1158 
1159 static int usbdux_pwm_cancel(struct comedi_device *dev,
1160                              struct comedi_subdevice *s)
1161 {
1162         struct usbdux_private *devpriv = dev->private;
1163         int ret;
1164 
1165         down(&devpriv->sem);
1166         /* unlink only if it is really running */
1167         usbdux_pwm_stop(dev, devpriv->pwm_cmd_running);
1168         ret = send_dux_commands(dev, USBDUX_CMD_PWM_OFF);
1169         up(&devpriv->sem);
1170 
1171         return ret;
1172 }
1173 
1174 static void usbduxsub_pwm_irq(struct urb *urb)
1175 {
1176         struct comedi_device *dev = urb->context;
1177         struct usbdux_private *devpriv = dev->private;
1178         int ret;
1179 
1180         switch (urb->status) {
1181         case 0:
1182                 /* success */
1183                 break;
1184 
1185         case -ECONNRESET:
1186         case -ENOENT:
1187         case -ESHUTDOWN:
1188         case -ECONNABORTED:
1189                 /*
1190                  * after an unlink command, unplug, ... etc
1191                  * no unlink needed here. Already shutting down.
1192                  */
1193                 if (devpriv->pwm_cmd_running)
1194                         usbdux_pwm_stop(dev, 0);
1195 
1196                 return;
1197 
1198         default:
1199                 /* a real error */
1200                 if (devpriv->pwm_cmd_running) {
1201                         dev_err(dev->class_dev,
1202                                 "Non-zero urb status received in pwm intr context: %d\n",
1203                                 urb->status);
1204                         usbdux_pwm_stop(dev, 0);
1205                 }
1206                 return;
1207         }
1208 
1209         /* are we actually running? */
1210         if (!devpriv->pwm_cmd_running)
1211                 return;
1212 
1213         urb->transfer_buffer_length = devpriv->pwm_buf_sz;
1214         urb->dev = comedi_to_usb_dev(dev);
1215         urb->status = 0;
1216         if (devpriv->pwm_cmd_running) {
1217                 ret = usb_submit_urb(urb, GFP_ATOMIC);
1218                 if (ret < 0) {
1219                         dev_err(dev->class_dev,
1220                                 "pwm urb resubm failed in int-cont. ret=%d",
1221                                 ret);
1222                         if (ret == -EL2NSYNC)
1223                                 dev_err(dev->class_dev,
1224                                         "buggy USB host controller or bug in IRQ handling!\n");
1225 
1226                         /* don't do an unlink here */
1227                         usbdux_pwm_stop(dev, 0);
1228                 }
1229         }
1230 }
1231 
1232 static int usbduxsub_submit_pwm_urbs(struct comedi_device *dev)
1233 {
1234         struct usb_device *usb = comedi_to_usb_dev(dev);
1235         struct usbdux_private *devpriv = dev->private;
1236         struct urb *urb = devpriv->pwm_urb;
1237 
1238         /* in case of a resubmission after an unlink... */
1239         usb_fill_bulk_urb(urb, usb, usb_sndbulkpipe(usb, 4),
1240                           urb->transfer_buffer,
1241                           devpriv->pwm_buf_sz,
1242                           usbduxsub_pwm_irq,
1243                           dev);
1244 
1245         return usb_submit_urb(urb, GFP_ATOMIC);
1246 }
1247 
1248 static int usbdux_pwm_period(struct comedi_device *dev,
1249                              struct comedi_subdevice *s,
1250                              unsigned int period)
1251 {
1252         struct usbdux_private *devpriv = dev->private;
1253         int fx2delay = 255;
1254 
1255         if (period < MIN_PWM_PERIOD)
1256                 return -EAGAIN;
1257 
1258         fx2delay = (period / (6 * 512 * 1000 / 33)) - 6;
1259         if (fx2delay > 255)
1260                 return -EAGAIN;
1261 
1262         devpriv->pwm_delay = fx2delay;
1263         devpriv->pwm_period = period;
1264 
1265         return 0;
1266 }
1267 
1268 static int usbdux_pwm_start(struct comedi_device *dev,
1269                             struct comedi_subdevice *s)
1270 {
1271         struct usbdux_private *devpriv = dev->private;
1272         int ret = 0;
1273 
1274         down(&devpriv->sem);
1275 
1276         if (devpriv->pwm_cmd_running)
1277                 goto pwm_start_exit;
1278 
1279         devpriv->dux_commands[1] = devpriv->pwm_delay;
1280         ret = send_dux_commands(dev, USBDUX_CMD_PWM_ON);
1281         if (ret < 0)
1282                 goto pwm_start_exit;
1283 
1284         /* initialise the buffer */
1285         memset(devpriv->pwm_urb->transfer_buffer, 0, devpriv->pwm_buf_sz);
1286 
1287         devpriv->pwm_cmd_running = 1;
1288         ret = usbduxsub_submit_pwm_urbs(dev);
1289         if (ret < 0)
1290                 devpriv->pwm_cmd_running = 0;
1291 
1292 pwm_start_exit:
1293         up(&devpriv->sem);
1294 
1295         return ret;
1296 }
1297 
1298 static void usbdux_pwm_pattern(struct comedi_device *dev,
1299                                struct comedi_subdevice *s,
1300                                unsigned int chan,
1301                                unsigned int value,
1302                                unsigned int sign)
1303 {
1304         struct usbdux_private *devpriv = dev->private;
1305         char pwm_mask = (1 << chan);    /* DIO bit for the PWM data */
1306         char sgn_mask = (16 << chan);   /* DIO bit for the sign */
1307         char *buf = (char *)(devpriv->pwm_urb->transfer_buffer);
1308         int szbuf = devpriv->pwm_buf_sz;
1309         int i;
1310 
1311         for (i = 0; i < szbuf; i++) {
1312                 char c = *buf;
1313 
1314                 c &= ~pwm_mask;
1315                 if (i < value)
1316                         c |= pwm_mask;
1317                 if (!sign)
1318                         c &= ~sgn_mask;
1319                 else
1320                         c |= sgn_mask;
1321                 *buf++ = c;
1322         }
1323 }
1324 
1325 static int usbdux_pwm_write(struct comedi_device *dev,
1326                             struct comedi_subdevice *s,
1327                             struct comedi_insn *insn,
1328                             unsigned int *data)
1329 {
1330         unsigned int chan = CR_CHAN(insn->chanspec);
1331 
1332         /*
1333          * It doesn't make sense to support more than one value here
1334          * because it would just overwrite the PWM buffer.
1335          */
1336         if (insn->n != 1)
1337                 return -EINVAL;
1338 
1339         /*
1340          * The sign is set via a special INSN only, this gives us 8 bits
1341          * for normal operation, sign is 0 by default.
1342          */
1343         usbdux_pwm_pattern(dev, s, chan, data[0], 0);
1344 
1345         return insn->n;
1346 }
1347 
1348 static int usbdux_pwm_config(struct comedi_device *dev,
1349                              struct comedi_subdevice *s,
1350                              struct comedi_insn *insn,
1351                              unsigned int *data)
1352 {
1353         struct usbdux_private *devpriv = dev->private;
1354         unsigned int chan = CR_CHAN(insn->chanspec);
1355 
1356         switch (data[0]) {
1357         case INSN_CONFIG_ARM:
1358                 /*
1359                  * if not zero the PWM is limited to a certain time which is
1360                  * not supported here
1361                  */
1362                 if (data[1] != 0)
1363                         return -EINVAL;
1364                 return usbdux_pwm_start(dev, s);
1365         case INSN_CONFIG_DISARM:
1366                 return usbdux_pwm_cancel(dev, s);
1367         case INSN_CONFIG_GET_PWM_STATUS:
1368                 data[1] = devpriv->pwm_cmd_running;
1369                 return 0;
1370         case INSN_CONFIG_PWM_SET_PERIOD:
1371                 return usbdux_pwm_period(dev, s, data[1]);
1372         case INSN_CONFIG_PWM_GET_PERIOD:
1373                 data[1] = devpriv->pwm_period;
1374                 return 0;
1375         case INSN_CONFIG_PWM_SET_H_BRIDGE:
1376                 /*
1377                  * data[1] = value
1378                  * data[2] = sign (for a relay)
1379                  */
1380                 usbdux_pwm_pattern(dev, s, chan, data[1], (data[2] != 0));
1381                 return 0;
1382         case INSN_CONFIG_PWM_GET_H_BRIDGE:
1383                 /* values are not kept in this driver, nothing to return here */
1384                 return -EINVAL;
1385         }
1386         return -EINVAL;
1387 }
1388 
1389 static int usbdux_firmware_upload(struct comedi_device *dev,
1390                                   const u8 *data, size_t size,
1391                                   unsigned long context)
1392 {
1393         struct usb_device *usb = comedi_to_usb_dev(dev);
1394         uint8_t *buf;
1395         uint8_t *tmp;
1396         int ret;
1397 
1398         if (!data)
1399                 return 0;
1400 
1401         if (size > USBDUX_FIRMWARE_MAX_LEN) {
1402                 dev_err(dev->class_dev,
1403                         "usbdux firmware binary it too large for FX2.\n");
1404                 return -ENOMEM;
1405         }
1406 
1407         /* we generate a local buffer for the firmware */
1408         buf = kmemdup(data, size, GFP_KERNEL);
1409         if (!buf)
1410                 return -ENOMEM;
1411 
1412         /* we need a malloc'ed buffer for usb_control_msg() */
1413         tmp = kmalloc(1, GFP_KERNEL);
1414         if (!tmp) {
1415                 kfree(buf);
1416                 return -ENOMEM;
1417         }
1418 
1419         /* stop the current firmware on the device */
1420         *tmp = 1;       /* 7f92 to one */
1421         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1422                               USBDUX_FIRMWARE_CMD,
1423                               VENDOR_DIR_OUT,
1424                               USBDUX_CPU_CS, 0x0000,
1425                               tmp, 1,
1426                               BULK_TIMEOUT);
1427         if (ret < 0) {
1428                 dev_err(dev->class_dev, "can not stop firmware\n");
1429                 goto done;
1430         }
1431 
1432         /* upload the new firmware to the device */
1433         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1434                               USBDUX_FIRMWARE_CMD,
1435                               VENDOR_DIR_OUT,
1436                               0, 0x0000,
1437                               buf, size,
1438                               BULK_TIMEOUT);
1439         if (ret < 0) {
1440                 dev_err(dev->class_dev, "firmware upload failed\n");
1441                 goto done;
1442         }
1443 
1444         /* start the new firmware on the device */
1445         *tmp = 0;       /* 7f92 to zero */
1446         ret = usb_control_msg(usb, usb_sndctrlpipe(usb, 0),
1447                               USBDUX_FIRMWARE_CMD,
1448                               VENDOR_DIR_OUT,
1449                               USBDUX_CPU_CS, 0x0000,
1450                               tmp, 1,
1451                               BULK_TIMEOUT);
1452         if (ret < 0)
1453                 dev_err(dev->class_dev, "can not start firmware\n");
1454 
1455 done:
1456         kfree(tmp);
1457         kfree(buf);
1458         return ret;
1459 }
1460 
1461 static int usbdux_alloc_usb_buffers(struct comedi_device *dev)
1462 {
1463         struct usb_device *usb = comedi_to_usb_dev(dev);
1464         struct usbdux_private *devpriv = dev->private;
1465         struct urb *urb;
1466         int i;
1467 
1468         devpriv->dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
1469         devpriv->in_buf = kzalloc(SIZEINBUF, GFP_KERNEL);
1470         devpriv->insn_buf = kzalloc(SIZEINSNBUF, GFP_KERNEL);
1471         devpriv->ai_urbs = kcalloc(devpriv->n_ai_urbs, sizeof(void *),
1472                                    GFP_KERNEL);
1473         devpriv->ao_urbs = kcalloc(devpriv->n_ao_urbs, sizeof(void *),
1474                                    GFP_KERNEL);
1475         if (!devpriv->dux_commands || !devpriv->in_buf || !devpriv->insn_buf ||
1476             !devpriv->ai_urbs || !devpriv->ao_urbs)
1477                 return -ENOMEM;
1478 
1479         for (i = 0; i < devpriv->n_ai_urbs; i++) {
1480                 /* one frame: 1ms */
1481                 urb = usb_alloc_urb(1, GFP_KERNEL);
1482                 if (!urb)
1483                         return -ENOMEM;
1484                 devpriv->ai_urbs[i] = urb;
1485 
1486                 urb->dev = usb;
1487                 urb->context = dev;
1488                 urb->pipe = usb_rcvisocpipe(usb, 6);
1489                 urb->transfer_flags = URB_ISO_ASAP;
1490                 urb->transfer_buffer = kzalloc(SIZEINBUF, GFP_KERNEL);
1491                 if (!urb->transfer_buffer)
1492                         return -ENOMEM;
1493 
1494                 urb->complete = usbduxsub_ai_isoc_irq;
1495                 urb->number_of_packets = 1;
1496                 urb->transfer_buffer_length = SIZEINBUF;
1497                 urb->iso_frame_desc[0].offset = 0;
1498                 urb->iso_frame_desc[0].length = SIZEINBUF;
1499         }
1500 
1501         for (i = 0; i < devpriv->n_ao_urbs; i++) {
1502                 /* one frame: 1ms */
1503                 urb = usb_alloc_urb(1, GFP_KERNEL);
1504                 if (!urb)
1505                         return -ENOMEM;
1506                 devpriv->ao_urbs[i] = urb;
1507 
1508                 urb->dev = usb;
1509                 urb->context = dev;
1510                 urb->pipe = usb_sndisocpipe(usb, 2);
1511                 urb->transfer_flags = URB_ISO_ASAP;
1512                 urb->transfer_buffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
1513                 if (!urb->transfer_buffer)
1514                         return -ENOMEM;
1515 
1516                 urb->complete = usbduxsub_ao_isoc_irq;
1517                 urb->number_of_packets = 1;
1518                 urb->transfer_buffer_length = SIZEOUTBUF;
1519                 urb->iso_frame_desc[0].offset = 0;
1520                 urb->iso_frame_desc[0].length = SIZEOUTBUF;
1521                 if (devpriv->high_speed)
1522                         urb->interval = 8;      /* uframes */
1523                 else
1524                         urb->interval = 1;      /* frames */
1525         }
1526 
1527         /* pwm */
1528         if (devpriv->pwm_buf_sz) {
1529                 urb = usb_alloc_urb(0, GFP_KERNEL);
1530                 if (!urb)
1531                         return -ENOMEM;
1532                 devpriv->pwm_urb = urb;
1533 
1534                 /* max bulk ep size in high speed */
1535                 urb->transfer_buffer = kzalloc(devpriv->pwm_buf_sz,
1536                                                GFP_KERNEL);
1537                 if (!urb->transfer_buffer)
1538                         return -ENOMEM;
1539         }
1540 
1541         return 0;
1542 }
1543 
1544 static void usbdux_free_usb_buffers(struct comedi_device *dev)
1545 {
1546         struct usbdux_private *devpriv = dev->private;
1547         struct urb *urb;
1548         int i;
1549 
1550         urb = devpriv->pwm_urb;
1551         if (urb) {
1552                 kfree(urb->transfer_buffer);
1553                 usb_free_urb(urb);
1554         }
1555         if (devpriv->ao_urbs) {
1556                 for (i = 0; i < devpriv->n_ao_urbs; i++) {
1557                         urb = devpriv->ao_urbs[i];
1558                         if (urb) {
1559                                 kfree(urb->transfer_buffer);
1560                                 usb_free_urb(urb);
1561                         }
1562                 }
1563                 kfree(devpriv->ao_urbs);
1564         }
1565         if (devpriv->ai_urbs) {
1566                 for (i = 0; i < devpriv->n_ai_urbs; i++) {
1567                         urb = devpriv->ai_urbs[i];
1568                         if (urb) {
1569                                 kfree(urb->transfer_buffer);
1570                                 usb_free_urb(urb);
1571                         }
1572                 }
1573                 kfree(devpriv->ai_urbs);
1574         }
1575         kfree(devpriv->insn_buf);
1576         kfree(devpriv->in_buf);
1577         kfree(devpriv->dux_commands);
1578 }
1579 
1580 static int usbdux_auto_attach(struct comedi_device *dev,
1581                               unsigned long context_unused)
1582 {
1583         struct usb_interface *intf = comedi_to_usb_interface(dev);
1584         struct usb_device *usb = comedi_to_usb_dev(dev);
1585         struct usbdux_private *devpriv;
1586         struct comedi_subdevice *s;
1587         int ret;
1588 
1589         devpriv = comedi_alloc_devpriv(dev, sizeof(*devpriv));
1590         if (!devpriv)
1591                 return -ENOMEM;
1592 
1593         sema_init(&devpriv->sem, 1);
1594 
1595         usb_set_intfdata(intf, devpriv);
1596 
1597         devpriv->high_speed = (usb->speed == USB_SPEED_HIGH);
1598         if (devpriv->high_speed) {
1599                 devpriv->n_ai_urbs = NUMOFINBUFFERSHIGH;
1600                 devpriv->n_ao_urbs = NUMOFOUTBUFFERSHIGH;
1601                 devpriv->pwm_buf_sz = 512;
1602         } else {
1603                 devpriv->n_ai_urbs = NUMOFINBUFFERSFULL;
1604                 devpriv->n_ao_urbs = NUMOFOUTBUFFERSFULL;
1605         }
1606 
1607         ret = usbdux_alloc_usb_buffers(dev);
1608         if (ret)
1609                 return ret;
1610 
1611         /* setting to alternate setting 3: enabling iso ep and bulk ep. */
1612         ret = usb_set_interface(usb, intf->altsetting->desc.bInterfaceNumber,
1613                                 3);
1614         if (ret < 0) {
1615                 dev_err(dev->class_dev,
1616                         "could not set alternate setting 3 in high speed\n");
1617                 return ret;
1618         }
1619 
1620         ret = comedi_load_firmware(dev, &usb->dev, USBDUX_FIRMWARE,
1621                                    usbdux_firmware_upload, 0);
1622         if (ret < 0)
1623                 return ret;
1624 
1625         ret = comedi_alloc_subdevices(dev, (devpriv->high_speed) ? 5 : 4);
1626         if (ret)
1627                 return ret;
1628 
1629         /* Analog Input subdevice */
1630         s = &dev->subdevices[0];
1631         dev->read_subdev = s;
1632         s->type         = COMEDI_SUBD_AI;
1633         s->subdev_flags = SDF_READABLE | SDF_GROUND | SDF_CMD_READ;
1634         s->n_chan       = 8;
1635         s->maxdata      = 0x0fff;
1636         s->len_chanlist = 8;
1637         s->range_table  = &range_usbdux_ai_range;
1638         s->insn_read    = usbdux_ai_insn_read;
1639         s->do_cmdtest   = usbdux_ai_cmdtest;
1640         s->do_cmd       = usbdux_ai_cmd;
1641         s->cancel       = usbdux_ai_cancel;
1642 
1643         /* Analog Output subdevice */
1644         s = &dev->subdevices[1];
1645         dev->write_subdev = s;
1646         s->type         = COMEDI_SUBD_AO;
1647         s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
1648         s->n_chan       = 4;
1649         s->maxdata      = 0x0fff;
1650         s->len_chanlist = s->n_chan;
1651         s->range_table  = &range_usbdux_ao_range;
1652         s->do_cmdtest   = usbdux_ao_cmdtest;
1653         s->do_cmd       = usbdux_ao_cmd;
1654         s->cancel       = usbdux_ao_cancel;
1655         s->insn_read    = usbdux_ao_insn_read;
1656         s->insn_write   = usbdux_ao_insn_write;
1657 
1658         ret = comedi_alloc_subdev_readback(s);
1659         if (ret)
1660                 return ret;
1661 
1662         /* Digital I/O subdevice */
1663         s = &dev->subdevices[2];
1664         s->type         = COMEDI_SUBD_DIO;
1665         s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
1666         s->n_chan       = 8;
1667         s->maxdata      = 1;
1668         s->range_table  = &range_digital;
1669         s->insn_bits    = usbdux_dio_insn_bits;
1670         s->insn_config  = usbdux_dio_insn_config;
1671 
1672         /* Counter subdevice */
1673         s = &dev->subdevices[3];
1674         s->type         = COMEDI_SUBD_COUNTER;
1675         s->subdev_flags = SDF_WRITABLE | SDF_READABLE;
1676         s->n_chan       = 4;
1677         s->maxdata      = 0xffff;
1678         s->insn_read    = usbdux_counter_read;
1679         s->insn_write   = usbdux_counter_write;
1680         s->insn_config  = usbdux_counter_config;
1681 
1682         if (devpriv->high_speed) {
1683                 /* PWM subdevice */
1684                 s = &dev->subdevices[4];
1685                 s->type         = COMEDI_SUBD_PWM;
1686                 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
1687                 s->n_chan       = 8;
1688                 s->maxdata      = devpriv->pwm_buf_sz;
1689                 s->insn_write   = usbdux_pwm_write;
1690                 s->insn_config  = usbdux_pwm_config;
1691 
1692                 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
1693         }
1694 
1695         return 0;
1696 }
1697 
1698 static void usbdux_detach(struct comedi_device *dev)
1699 {
1700         struct usb_interface *intf = comedi_to_usb_interface(dev);
1701         struct usbdux_private *devpriv = dev->private;
1702 
1703         usb_set_intfdata(intf, NULL);
1704 
1705         if (!devpriv)
1706                 return;
1707 
1708         down(&devpriv->sem);
1709 
1710         /* force unlink all urbs */
1711         usbdux_pwm_stop(dev, 1);
1712         usbdux_ao_stop(dev, 1);
1713         usbdux_ai_stop(dev, 1);
1714 
1715         usbdux_free_usb_buffers(dev);
1716 
1717         up(&devpriv->sem);
1718 }
1719 
1720 static struct comedi_driver usbdux_driver = {
1721         .driver_name    = "usbdux",
1722         .module         = THIS_MODULE,
1723         .auto_attach    = usbdux_auto_attach,
1724         .detach         = usbdux_detach,
1725 };
1726 
1727 static int usbdux_usb_probe(struct usb_interface *intf,
1728                             const struct usb_device_id *id)
1729 {
1730         return comedi_usb_auto_config(intf, &usbdux_driver, 0);
1731 }
1732 
1733 static const struct usb_device_id usbdux_usb_table[] = {
1734         { USB_DEVICE(0x13d8, 0x0001) },
1735         { USB_DEVICE(0x13d8, 0x0002) },
1736         { }
1737 };
1738 MODULE_DEVICE_TABLE(usb, usbdux_usb_table);
1739 
1740 static struct usb_driver usbdux_usb_driver = {
1741         .name           = "usbdux",
1742         .probe          = usbdux_usb_probe,
1743         .disconnect     = comedi_usb_auto_unconfig,
1744         .id_table       = usbdux_usb_table,
1745 };
1746 module_comedi_usb_driver(usbdux_driver, usbdux_usb_driver);
1747 
1748 MODULE_AUTHOR("Bernd Porr, BerndPorr@f2s.com");
1749 MODULE_DESCRIPTION("Stirling/ITL USB-DUX -- Bernd.Porr@f2s.com");
1750 MODULE_LICENSE("GPL");
1751 MODULE_FIRMWARE(USBDUX_FIRMWARE);
1752 

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us