Version:  2.0.40 2.2.26 2.4.37 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 4.2

Linux/drivers/staging/comedi/drivers/usbduxsigma.c

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

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