]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/staging/comedi/drivers/usbduxsigma.c
Merge branch 'pl022' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux...
[mirror_ubuntu-artful-kernel.git] / drivers / staging / comedi / drivers / usbduxsigma.c
1 #define DRIVER_VERSION "v0.6"
2 #define DRIVER_AUTHOR "Bernd Porr, BerndPorr@f2s.com"
3 #define DRIVER_DESC "Stirling/ITL USB-DUX SIGMA -- Bernd.Porr@f2s.com"
4 /*
5 comedi/drivers/usbdux.c
6 Copyright (C) 2011 Bernd Porr, Bernd.Porr@f2s.com
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 */
23 /*
24 Driver: usbduxsigma
25 Description: University of Stirling USB DAQ & INCITE Technology Limited
26 Devices: [ITL] USB-DUX (usbduxsigma.o)
27 Author: Bernd Porr <BerndPorr@f2s.com>
28 Updated: 8 Nov 2011
29 Status: testing
30 */
31 /*
32 * I must give credit here to Chris Baugher who
33 * wrote the driver for AT-MIO-16d. I used some parts of this
34 * driver. I also must give credits to David Brownell
35 * who supported me with the USB development.
36 *
37 * Note: the raw data from the A/D converter is 24 bit big endian
38 * anything else is little endian to/from the dux board
39 *
40 *
41 * Revision history:
42 * 0.1: initial version
43 * 0.2: all basic functions implemented, digital I/O only for one port
44 * 0.3: proper vendor ID and driver name
45 * 0.4: fixed D/A voltage range
46 * 0.5: various bug fixes, health check at startup
47 * 0.6: corrected wrong input range
48 */
49
50 /* generates loads of debug info */
51 /* #define NOISY_DUX_DEBUGBUG */
52
53 #include <linux/kernel.h>
54 #include <linux/module.h>
55 #include <linux/init.h>
56 #include <linux/slab.h>
57 #include <linux/input.h>
58 #include <linux/usb.h>
59 #include <linux/fcntl.h>
60 #include <linux/compiler.h>
61 #include <linux/firmware.h>
62 #include "comedi_fc.h"
63 #include "../comedidev.h"
64
65 #define BOARDNAME "usbduxsigma"
66
67 /* timeout for the USB-transfer in ms*/
68 #define BULK_TIMEOUT 1000
69
70 /* constants for "firmware" upload and download */
71 #define USBDUXSUB_FIRMWARE 0xA0
72 #define VENDOR_DIR_IN 0xC0
73 #define VENDOR_DIR_OUT 0x40
74
75 /* internal addresses of the 8051 processor */
76 #define USBDUXSUB_CPUCS 0xE600
77
78 /*
79 * the minor device number, major is 180 only for debugging purposes and to
80 * upload special firmware (programming the eeprom etc) which is not
81 * compatible with the comedi framwork
82 */
83 #define USBDUXSUB_MINOR 32
84
85 /* max lenghth of the transfer-buffer for software upload */
86 #define TB_LEN 0x2000
87
88 /* Input endpoint number: ISO/IRQ */
89 #define ISOINEP 6
90
91 /* Output endpoint number: ISO/IRQ */
92 #define ISOOUTEP 2
93
94 /* This EP sends DUX commands to USBDUX */
95 #define COMMAND_OUT_EP 1
96
97 /* This EP receives the DUX commands from USBDUX */
98 #define COMMAND_IN_EP 8
99
100 /* Output endpoint for PWM */
101 #define PWM_EP 4
102
103 /* 300Hz max frequ under PWM */
104 #define MIN_PWM_PERIOD ((long)(1E9/300))
105
106 /* Default PWM frequency */
107 #define PWM_DEFAULT_PERIOD ((long)(1E9/100))
108
109 /* Number of channels (16 AD and offset)*/
110 #define NUMCHANNELS 16
111
112 /* Size of one A/D value */
113 #define SIZEADIN ((sizeof(int32_t)))
114
115 /*
116 * Size of the async input-buffer IN BYTES, the DIO state is transmitted
117 * as the first byte.
118 */
119 #define SIZEINBUF (((NUMCHANNELS+1)*SIZEADIN))
120
121 /* 16 bytes. */
122 #define SIZEINSNBUF 16
123
124 /* Number of DA channels */
125 #define NUMOUTCHANNELS 8
126
127 /* size of one value for the D/A converter: channel and value */
128 #define SIZEDAOUT ((sizeof(uint8_t)+sizeof(int16_t)))
129
130 /*
131 * Size of the output-buffer in bytes
132 * Actually only the first 4 triplets are used but for the
133 * high speed mode we need to pad it to 8 (microframes).
134 */
135 #define SIZEOUTBUF ((8*SIZEDAOUT))
136
137 /*
138 * Size of the buffer for the dux commands: just now max size is determined
139 * by the analogue out + command byte + panic bytes...
140 */
141 #define SIZEOFDUXBUFFER ((8*SIZEDAOUT+2))
142
143 /* Number of in-URBs which receive the data: min=2 */
144 #define NUMOFINBUFFERSFULL 5
145
146 /* Number of out-URBs which send the data: min=2 */
147 #define NUMOFOUTBUFFERSFULL 5
148
149 /* Number of in-URBs which receive the data: min=5 */
150 /* must have more buffers due to buggy USB ctr */
151 #define NUMOFINBUFFERSHIGH 10
152
153 /* Number of out-URBs which send the data: min=5 */
154 /* must have more buffers due to buggy USB ctr */
155 #define NUMOFOUTBUFFERSHIGH 10
156
157 /* Total number of usbdux devices */
158 #define NUMUSBDUX 16
159
160 /* Analogue in subdevice */
161 #define SUBDEV_AD 0
162
163 /* Analogue out subdevice */
164 #define SUBDEV_DA 1
165
166 /* Digital I/O */
167 #define SUBDEV_DIO 2
168
169 /* timer aka pwm output */
170 #define SUBDEV_PWM 3
171
172 /* number of retries to get the right dux command */
173 #define RETRIES 10
174
175 /**************************************************/
176 /* comedi constants */
177 static const struct comedi_lrange range_usbdux_ai_range = { 1, {
178 BIP_RANGE
179 (2.65/2.0)
180 }
181 };
182
183 static const struct comedi_lrange range_usbdux_ao_range = { 1, {
184 UNI_RANGE
185 (2.5),
186 }
187 };
188
189 /*
190 * private structure of one subdevice
191 */
192
193 /*
194 * This is the structure which holds all the data of
195 * this driver one sub device just now: A/D
196 */
197 struct usbduxsub {
198 /* attached? */
199 int attached;
200 /* is it associated with a subdevice? */
201 int probed;
202 /* pointer to the usb-device */
203 struct usb_device *usbdev;
204 /* actual number of in-buffers */
205 int numOfInBuffers;
206 /* actual number of out-buffers */
207 int numOfOutBuffers;
208 /* ISO-transfer handling: buffers */
209 struct urb **urbIn;
210 struct urb **urbOut;
211 /* pwm-transfer handling */
212 struct urb *urbPwm;
213 /* PWM period */
214 unsigned int pwmPeriod;
215 /* PWM internal delay for the GPIF in the FX2 */
216 uint8_t pwmDelay;
217 /* size of the PWM buffer which holds the bit pattern */
218 int sizePwmBuf;
219 /* input buffer for the ISO-transfer */
220 int32_t *inBuffer;
221 /* input buffer for single insn */
222 int8_t *insnBuffer;
223 /* output buffer for single DA outputs */
224 int16_t *outBuffer;
225 /* interface number */
226 int ifnum;
227 /* interface structure in 2.6 */
228 struct usb_interface *interface;
229 /* comedi device for the interrupt context */
230 struct comedi_device *comedidev;
231 /* is it USB_SPEED_HIGH or not? */
232 short int high_speed;
233 /* asynchronous command is running */
234 short int ai_cmd_running;
235 short int ao_cmd_running;
236 /* pwm is running */
237 short int pwm_cmd_running;
238 /* continuous acquisition */
239 short int ai_continuous;
240 short int ao_continuous;
241 /* number of samples to acquire */
242 int ai_sample_count;
243 int ao_sample_count;
244 /* time between samples in units of the timer */
245 unsigned int ai_timer;
246 unsigned int ao_timer;
247 /* counter between acquisitions */
248 unsigned int ai_counter;
249 unsigned int ao_counter;
250 /* interval in frames/uframes */
251 unsigned int ai_interval;
252 /* D/A commands */
253 uint8_t *dac_commands;
254 /* commands */
255 uint8_t *dux_commands;
256 struct semaphore sem;
257 };
258
259 /*
260 * The pointer to the private usb-data of the driver is also the private data
261 * for the comedi-device. This has to be global as the usb subsystem needs
262 * global variables. The other reason is that this structure must be there
263 * _before_ any comedi command is issued. The usb subsystem must be initialised
264 * before comedi can access it.
265 */
266 static struct usbduxsub usbduxsub[NUMUSBDUX];
267
268 static DEFINE_SEMAPHORE(start_stop_sem);
269
270 static struct comedi_driver driver_usbduxsigma; /* see below for initializer */
271
272 /*
273 * Stops the data acquision
274 * It should be safe to call this function from any context
275 */
276 static int usbduxsub_unlink_InURBs(struct usbduxsub *usbduxsub_tmp)
277 {
278 int i = 0;
279 int err = 0;
280
281 if (usbduxsub_tmp && usbduxsub_tmp->urbIn) {
282 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
283 if (usbduxsub_tmp->urbIn[i]) {
284 /* We wait here until all transfers have been
285 * cancelled. */
286 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
287 }
288 dev_dbg(&usbduxsub_tmp->interface->dev,
289 "comedi: usbdux: unlinked InURB %d, err=%d\n",
290 i, err);
291 }
292 }
293 return err;
294 }
295
296 /*
297 * This will stop a running acquisition operation
298 * Is called from within this driver from both the
299 * interrupt context and from comedi
300 */
301 static int usbdux_ai_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
302 {
303 int ret = 0;
304
305 if (!this_usbduxsub) {
306 pr_err("comedi?: usbdux_ai_stop: this_usbduxsub=NULL!\n");
307 return -EFAULT;
308 }
309 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_stop\n");
310
311 if (do_unlink) {
312 /* stop aquistion */
313 ret = usbduxsub_unlink_InURBs(this_usbduxsub);
314 }
315
316 this_usbduxsub->ai_cmd_running = 0;
317
318 return ret;
319 }
320
321 /*
322 * This will cancel a running acquisition operation.
323 * This is called by comedi but never from inside the driver.
324 */
325 static int usbdux_ai_cancel(struct comedi_device *dev,
326 struct comedi_subdevice *s)
327 {
328 struct usbduxsub *this_usbduxsub;
329 int res = 0;
330
331 /* force unlink of all urbs */
332 this_usbduxsub = dev->private;
333 if (!this_usbduxsub)
334 return -EFAULT;
335
336 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ai_cancel\n");
337
338 /* prevent other CPUs from submitting new commands just now */
339 down(&this_usbduxsub->sem);
340 if (!(this_usbduxsub->probed)) {
341 up(&this_usbduxsub->sem);
342 return -ENODEV;
343 }
344 /* unlink only if the urb really has been submitted */
345 res = usbdux_ai_stop(this_usbduxsub, this_usbduxsub->ai_cmd_running);
346 up(&this_usbduxsub->sem);
347 return res;
348 }
349
350 /* analogue IN - interrupt service routine */
351 static void usbduxsub_ai_IsocIrq(struct urb *urb)
352 {
353 int i, err, n;
354 struct usbduxsub *this_usbduxsub;
355 struct comedi_device *this_comedidev;
356 struct comedi_subdevice *s;
357 int32_t v;
358 unsigned int dio_state;
359
360 /* the context variable points to the comedi device */
361 this_comedidev = urb->context;
362 /* the private structure of the subdevice is struct usbduxsub */
363 this_usbduxsub = this_comedidev->private;
364 /* subdevice which is the AD converter */
365 s = this_comedidev->subdevices + SUBDEV_AD;
366
367 /* first we test if something unusual has just happened */
368 switch (urb->status) {
369 case 0:
370 /* copy the result in the transfer buffer */
371 memcpy(this_usbduxsub->inBuffer,
372 urb->transfer_buffer, SIZEINBUF);
373 break;
374 case -EILSEQ:
375 /* error in the ISOchronous data */
376 /* we don't copy the data into the transfer buffer */
377 /* and recycle the last data byte */
378 dev_dbg(&urb->dev->dev,
379 "comedi%d: usbdux: CRC error in ISO IN stream.\n",
380 this_usbduxsub->comedidev->minor);
381
382 break;
383
384 case -ECONNRESET:
385 case -ENOENT:
386 case -ESHUTDOWN:
387 case -ECONNABORTED:
388 /* happens after an unlink command */
389 if (this_usbduxsub->ai_cmd_running) {
390 /* we are still running a command */
391 /* tell this comedi */
392 s->async->events |= COMEDI_CB_EOA;
393 s->async->events |= COMEDI_CB_ERROR;
394 comedi_event(this_usbduxsub->comedidev, s);
395 /* stop the transfer w/o unlink */
396 usbdux_ai_stop(this_usbduxsub, 0);
397 }
398 return;
399
400 default:
401 /* a real error on the bus */
402 /* pass error to comedi if we are really running a command */
403 if (this_usbduxsub->ai_cmd_running) {
404 dev_err(&urb->dev->dev,
405 "Non-zero urb status received in ai intr "
406 "context: %d\n", urb->status);
407 s->async->events |= COMEDI_CB_EOA;
408 s->async->events |= COMEDI_CB_ERROR;
409 comedi_event(this_usbduxsub->comedidev, s);
410 /* don't do an unlink here */
411 usbdux_ai_stop(this_usbduxsub, 0);
412 }
413 return;
414 }
415
416 /*
417 * at this point we are reasonably sure that nothing dodgy has happened
418 * are we running a command?
419 */
420 if (unlikely((!(this_usbduxsub->ai_cmd_running)))) {
421 /*
422 * not running a command, do not continue execution if no
423 * asynchronous command is running in particular not resubmit
424 */
425 return;
426 }
427
428 urb->dev = this_usbduxsub->usbdev;
429
430 /* resubmit the urb */
431 err = usb_submit_urb(urb, GFP_ATOMIC);
432 if (unlikely(err < 0)) {
433 dev_err(&urb->dev->dev,
434 "comedi_: urb resubmit failed in int-context!"
435 "err=%d\n",
436 err);
437 if (err == -EL2NSYNC)
438 dev_err(&urb->dev->dev,
439 "buggy USB host controller or bug in IRQ "
440 "handler!\n");
441 s->async->events |= COMEDI_CB_EOA;
442 s->async->events |= COMEDI_CB_ERROR;
443 comedi_event(this_usbduxsub->comedidev, s);
444 /* don't do an unlink here */
445 usbdux_ai_stop(this_usbduxsub, 0);
446 return;
447 }
448
449 /* get the state of the dio pins to allow external trigger */
450 dio_state = be32_to_cpu(this_usbduxsub->inBuffer[0]);
451
452 this_usbduxsub->ai_counter--;
453 if (likely(this_usbduxsub->ai_counter > 0))
454 return;
455
456 /* timer zero, transfer measurements to comedi */
457 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
458
459 /* test, if we transmit only a fixed number of samples */
460 if (!(this_usbduxsub->ai_continuous)) {
461 /* not continuous, fixed number of samples */
462 this_usbduxsub->ai_sample_count--;
463 /* all samples received? */
464 if (this_usbduxsub->ai_sample_count < 0) {
465 /* prevent a resubmit next time */
466 usbdux_ai_stop(this_usbduxsub, 0);
467 /* say comedi that the acquistion is over */
468 s->async->events |= COMEDI_CB_EOA;
469 comedi_event(this_usbduxsub->comedidev, s);
470 return;
471 }
472 }
473 /* get the data from the USB bus and hand it over to comedi */
474 n = s->async->cmd.chanlist_len;
475 for (i = 0; i < n; i++) {
476 /* transfer data, note first byte is the DIO state */
477 v = be32_to_cpu(this_usbduxsub->inBuffer[i+1]);
478 /* strip status byte */
479 v = v & 0x00ffffff;
480 /* convert to unsigned */
481 v = v ^ 0x00800000;
482 /* write the byte to the buffer */
483 err = cfc_write_array_to_buffer(s, &v, sizeof(uint32_t));
484 if (unlikely(err == 0)) {
485 /* buffer overflow */
486 usbdux_ai_stop(this_usbduxsub, 0);
487 return;
488 }
489 }
490 /* tell comedi that data is there */
491 s->async->events |= COMEDI_CB_BLOCK | COMEDI_CB_EOS;
492 comedi_event(this_usbduxsub->comedidev, s);
493 }
494
495 static int usbduxsub_unlink_OutURBs(struct usbduxsub *usbduxsub_tmp)
496 {
497 int i = 0;
498 int err = 0;
499
500 if (usbduxsub_tmp && usbduxsub_tmp->urbOut) {
501 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
502 if (usbduxsub_tmp->urbOut[i])
503 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
504
505 dev_dbg(&usbduxsub_tmp->interface->dev,
506 "comedi: usbdux: unlinked OutURB %d: res=%d\n",
507 i, err);
508 }
509 }
510 return err;
511 }
512
513 /* This will cancel a running acquisition operation
514 * in any context.
515 */
516 static int usbdux_ao_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
517 {
518 int ret = 0;
519
520 if (!this_usbduxsub)
521 return -EFAULT;
522 dev_dbg(&this_usbduxsub->interface->dev, "comedi: usbdux_ao_cancel\n");
523
524 if (do_unlink)
525 ret = usbduxsub_unlink_OutURBs(this_usbduxsub);
526
527 this_usbduxsub->ao_cmd_running = 0;
528
529 return ret;
530 }
531
532 /* force unlink, is called by comedi */
533 static int usbdux_ao_cancel(struct comedi_device *dev,
534 struct comedi_subdevice *s)
535 {
536 struct usbduxsub *this_usbduxsub = dev->private;
537 int res = 0;
538
539 if (!this_usbduxsub)
540 return -EFAULT;
541
542 /* prevent other CPUs from submitting a command just now */
543 down(&this_usbduxsub->sem);
544 if (!(this_usbduxsub->probed)) {
545 up(&this_usbduxsub->sem);
546 return -ENODEV;
547 }
548 /* unlink only if it is really running */
549 res = usbdux_ao_stop(this_usbduxsub, this_usbduxsub->ao_cmd_running);
550 up(&this_usbduxsub->sem);
551 return res;
552 }
553
554 static void usbduxsub_ao_IsocIrq(struct urb *urb)
555 {
556 int i, ret;
557 uint8_t *datap;
558 struct usbduxsub *this_usbduxsub;
559 struct comedi_device *this_comedidev;
560 struct comedi_subdevice *s;
561
562 /* the context variable points to the subdevice */
563 this_comedidev = urb->context;
564 /* the private structure of the subdevice is struct usbduxsub */
565 this_usbduxsub = this_comedidev->private;
566
567 s = this_comedidev->subdevices + SUBDEV_DA;
568
569 switch (urb->status) {
570 case 0:
571 /* success */
572 break;
573
574 case -ECONNRESET:
575 case -ENOENT:
576 case -ESHUTDOWN:
577 case -ECONNABORTED:
578 /* after an unlink command, unplug, ... etc */
579 /* no unlink needed here. Already shutting down. */
580 if (this_usbduxsub->ao_cmd_running) {
581 s->async->events |= COMEDI_CB_EOA;
582 comedi_event(this_usbduxsub->comedidev, s);
583 usbdux_ao_stop(this_usbduxsub, 0);
584 }
585 return;
586
587 default:
588 /* a real error */
589 if (this_usbduxsub->ao_cmd_running) {
590 dev_err(&urb->dev->dev,
591 "comedi_: Non-zero urb status received in ao "
592 "intr context: %d\n", urb->status);
593 s->async->events |= COMEDI_CB_ERROR;
594 s->async->events |= COMEDI_CB_EOA;
595 comedi_event(this_usbduxsub->comedidev, s);
596 /* we do an unlink if we are in the high speed mode */
597 usbdux_ao_stop(this_usbduxsub, 0);
598 }
599 return;
600 }
601
602 /* are we actually running? */
603 if (!(this_usbduxsub->ao_cmd_running))
604 return;
605
606 /* normal operation: executing a command in this subdevice */
607 this_usbduxsub->ao_counter--;
608 if ((int)this_usbduxsub->ao_counter <= 0) {
609 /* timer zero */
610 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
611
612 /* handle non continuous acquisition */
613 if (!(this_usbduxsub->ao_continuous)) {
614 /* fixed number of samples */
615 this_usbduxsub->ao_sample_count--;
616 if (this_usbduxsub->ao_sample_count < 0) {
617 /* all samples transmitted */
618 usbdux_ao_stop(this_usbduxsub, 0);
619 s->async->events |= COMEDI_CB_EOA;
620 comedi_event(this_usbduxsub->comedidev, s);
621 /* no resubmit of the urb */
622 return;
623 }
624 }
625 /* transmit data to the USB bus */
626 ((uint8_t *) (urb->transfer_buffer))[0] =
627 s->async->cmd.chanlist_len;
628 for (i = 0; i < s->async->cmd.chanlist_len; i++) {
629 short temp;
630 if (i >= NUMOUTCHANNELS)
631 break;
632
633 /* pointer to the DA */
634 datap =
635 (&(((uint8_t *) urb->transfer_buffer)[i * 2 + 1]));
636 /* get the data from comedi */
637 ret = comedi_buf_get(s->async, &temp);
638 datap[0] = temp;
639 datap[1] = this_usbduxsub->dac_commands[i];
640 /* printk("data[0]=%x, data[1]=%x, data[2]=%x\n", */
641 /* datap[0],datap[1],datap[2]); */
642 if (ret < 0) {
643 dev_err(&urb->dev->dev,
644 "comedi: buffer underflow\n");
645 s->async->events |= COMEDI_CB_EOA;
646 s->async->events |= COMEDI_CB_OVERFLOW;
647 }
648 /* transmit data to comedi */
649 s->async->events |= COMEDI_CB_BLOCK;
650 comedi_event(this_usbduxsub->comedidev, s);
651 }
652 }
653 urb->transfer_buffer_length = SIZEOUTBUF;
654 urb->dev = this_usbduxsub->usbdev;
655 urb->status = 0;
656 if (this_usbduxsub->ao_cmd_running) {
657 if (this_usbduxsub->high_speed) {
658 /* uframes */
659 urb->interval = 8;
660 } else {
661 /* frames */
662 urb->interval = 1;
663 }
664 urb->number_of_packets = 1;
665 urb->iso_frame_desc[0].offset = 0;
666 urb->iso_frame_desc[0].length = SIZEOUTBUF;
667 urb->iso_frame_desc[0].status = 0;
668 ret = usb_submit_urb(urb, GFP_ATOMIC);
669 if (ret < 0) {
670 dev_err(&urb->dev->dev,
671 "comedi_: ao urb resubm failed in int-cont. "
672 "ret=%d", ret);
673 if (ret == EL2NSYNC)
674 dev_err(&urb->dev->dev,
675 "buggy USB host controller or bug in "
676 "IRQ handling!\n");
677
678 s->async->events |= COMEDI_CB_EOA;
679 s->async->events |= COMEDI_CB_ERROR;
680 comedi_event(this_usbduxsub->comedidev, s);
681 /* don't do an unlink here */
682 usbdux_ao_stop(this_usbduxsub, 0);
683 }
684 }
685 }
686
687 static int usbduxsub_start(struct usbduxsub *usbduxsub)
688 {
689 int errcode = 0;
690 uint8_t local_transfer_buffer[16];
691
692 /* 7f92 to zero */
693 local_transfer_buffer[0] = 0;
694 errcode = usb_control_msg(usbduxsub->usbdev,
695 /* create a pipe for a control transfer */
696 usb_sndctrlpipe(usbduxsub->usbdev, 0),
697 /* bRequest, "Firmware" */
698 USBDUXSUB_FIRMWARE,
699 /* bmRequestType */
700 VENDOR_DIR_OUT,
701 /* Value */
702 USBDUXSUB_CPUCS,
703 /* Index */
704 0x0000,
705 /* address of the transfer buffer */
706 local_transfer_buffer,
707 /* Length */
708 1,
709 /* Timeout */
710 BULK_TIMEOUT);
711 if (errcode < 0) {
712 dev_err(&usbduxsub->interface->dev,
713 "comedi_: control msg failed (start)\n");
714 return errcode;
715 }
716 return 0;
717 }
718
719 static int usbduxsub_stop(struct usbduxsub *usbduxsub)
720 {
721 int errcode = 0;
722
723 uint8_t local_transfer_buffer[16];
724
725 /* 7f92 to one */
726 local_transfer_buffer[0] = 1;
727 errcode = usb_control_msg(usbduxsub->usbdev,
728 usb_sndctrlpipe(usbduxsub->usbdev, 0),
729 /* bRequest, "Firmware" */
730 USBDUXSUB_FIRMWARE,
731 /* bmRequestType */
732 VENDOR_DIR_OUT,
733 /* Value */
734 USBDUXSUB_CPUCS,
735 /* Index */
736 0x0000, local_transfer_buffer,
737 /* Length */
738 1,
739 /* Timeout */
740 BULK_TIMEOUT);
741 if (errcode < 0) {
742 dev_err(&usbduxsub->interface->dev,
743 "comedi_: control msg failed (stop)\n");
744 return errcode;
745 }
746 return 0;
747 }
748
749 static int usbduxsub_upload(struct usbduxsub *usbduxsub,
750 uint8_t *local_transfer_buffer,
751 unsigned int startAddr, unsigned int len)
752 {
753 int errcode;
754
755 errcode = usb_control_msg(usbduxsub->usbdev,
756 usb_sndctrlpipe(usbduxsub->usbdev, 0),
757 /* brequest, firmware */
758 USBDUXSUB_FIRMWARE,
759 /* bmRequestType */
760 VENDOR_DIR_OUT,
761 /* value */
762 startAddr,
763 /* index */
764 0x0000,
765 /* our local safe buffer */
766 local_transfer_buffer,
767 /* length */
768 len,
769 /* timeout */
770 BULK_TIMEOUT);
771 dev_dbg(&usbduxsub->interface->dev, "comedi_: result=%d\n", errcode);
772 if (errcode < 0) {
773 dev_err(&usbduxsub->interface->dev,
774 "comedi_: upload failed\n");
775 return errcode;
776 }
777 return 0;
778 }
779
780 /* the FX2LP has twice as much as the standard FX2 */
781 #define FIRMWARE_MAX_LEN 0x4000
782
783 static int firmwareUpload(struct usbduxsub *usbduxsub,
784 const u8 *firmwareBinary, int sizeFirmware)
785 {
786 int ret;
787 uint8_t *fwBuf;
788
789 if (!firmwareBinary)
790 return 0;
791
792 if (sizeFirmware > FIRMWARE_MAX_LEN) {
793 dev_err(&usbduxsub->interface->dev,
794 "usbduxsigma firmware binary it too large for FX2.\n");
795 return -ENOMEM;
796 }
797
798 /* we generate a local buffer for the firmware */
799 fwBuf = kmemdup(firmwareBinary, sizeFirmware, GFP_KERNEL);
800 if (!fwBuf) {
801 dev_err(&usbduxsub->interface->dev,
802 "comedi_: mem alloc for firmware failed\n");
803 return -ENOMEM;
804 }
805
806 ret = usbduxsub_stop(usbduxsub);
807 if (ret < 0) {
808 dev_err(&usbduxsub->interface->dev,
809 "comedi_: can not stop firmware\n");
810 kfree(fwBuf);
811 return ret;
812 }
813
814 ret = usbduxsub_upload(usbduxsub, fwBuf, 0, sizeFirmware);
815 if (ret < 0) {
816 dev_err(&usbduxsub->interface->dev,
817 "comedi_: firmware upload failed\n");
818 kfree(fwBuf);
819 return ret;
820 }
821 ret = usbduxsub_start(usbduxsub);
822 if (ret < 0) {
823 dev_err(&usbduxsub->interface->dev,
824 "comedi_: can not start firmware\n");
825 kfree(fwBuf);
826 return ret;
827 }
828 kfree(fwBuf);
829 return 0;
830 }
831
832 static int usbduxsub_submit_InURBs(struct usbduxsub *usbduxsub)
833 {
834 int i, errFlag;
835
836 if (!usbduxsub)
837 return -EFAULT;
838
839 /* Submit all URBs and start the transfer on the bus */
840 for (i = 0; i < usbduxsub->numOfInBuffers; i++) {
841 /* in case of a resubmission after an unlink... */
842 usbduxsub->urbIn[i]->interval = usbduxsub->ai_interval;
843 usbduxsub->urbIn[i]->context = usbduxsub->comedidev;
844 usbduxsub->urbIn[i]->dev = usbduxsub->usbdev;
845 usbduxsub->urbIn[i]->status = 0;
846 usbduxsub->urbIn[i]->transfer_flags = URB_ISO_ASAP;
847 dev_dbg(&usbduxsub->interface->dev,
848 "comedi%d: submitting in-urb[%d]: %p,%p intv=%d\n",
849 usbduxsub->comedidev->minor, i,
850 (usbduxsub->urbIn[i]->context),
851 (usbduxsub->urbIn[i]->dev),
852 (usbduxsub->urbIn[i]->interval));
853 errFlag = usb_submit_urb(usbduxsub->urbIn[i], GFP_ATOMIC);
854 if (errFlag) {
855 dev_err(&usbduxsub->interface->dev,
856 "comedi_: ai: usb_submit_urb(%d) error %d\n",
857 i, errFlag);
858 return errFlag;
859 }
860 }
861 return 0;
862 }
863
864 static int usbduxsub_submit_OutURBs(struct usbduxsub *usbduxsub)
865 {
866 int i, errFlag;
867
868 if (!usbduxsub)
869 return -EFAULT;
870
871 for (i = 0; i < usbduxsub->numOfOutBuffers; i++) {
872 dev_dbg(&usbduxsub->interface->dev,
873 "comedi_: submitting out-urb[%d]\n", i);
874 /* in case of a resubmission after an unlink... */
875 usbduxsub->urbOut[i]->context = usbduxsub->comedidev;
876 usbduxsub->urbOut[i]->dev = usbduxsub->usbdev;
877 usbduxsub->urbOut[i]->status = 0;
878 usbduxsub->urbOut[i]->transfer_flags = URB_ISO_ASAP;
879 errFlag = usb_submit_urb(usbduxsub->urbOut[i], GFP_ATOMIC);
880 if (errFlag) {
881 dev_err(&usbduxsub->interface->dev,
882 "comedi_: ao: usb_submit_urb(%d) error %d\n",
883 i, errFlag);
884 return errFlag;
885 }
886 }
887 return 0;
888 }
889
890 static int chanToInterval(int nChannels)
891 {
892 if (nChannels <= 2)
893 /* 4kHz */
894 return 2;
895 if (nChannels <= 8)
896 /* 2kHz */
897 return 4;
898 /* 1kHz */
899 return 8;
900 }
901
902 static int usbdux_ai_cmdtest(struct comedi_device *dev,
903 struct comedi_subdevice *s,
904 struct comedi_cmd *cmd)
905 {
906 int err = 0, tmp, i;
907 unsigned int tmpTimer;
908 struct usbduxsub *this_usbduxsub = dev->private;
909
910 if (!(this_usbduxsub->probed))
911 return -ENODEV;
912
913 dev_dbg(&this_usbduxsub->interface->dev,
914 "comedi%d: usbdux_ai_cmdtest\n", dev->minor);
915
916 /* make sure triggers are valid */
917 /* Only immediate triggers are allowed */
918 tmp = cmd->start_src;
919 cmd->start_src &= TRIG_NOW | TRIG_INT;
920 if (!cmd->start_src || tmp != cmd->start_src)
921 err++;
922
923 /* trigger should happen timed */
924 tmp = cmd->scan_begin_src;
925 /* start a new _scan_ with a timer */
926 cmd->scan_begin_src &= TRIG_TIMER;
927 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
928 err++;
929
930 /* scanning is continuous */
931 tmp = cmd->convert_src;
932 cmd->convert_src &= TRIG_NOW;
933 if (!cmd->convert_src || tmp != cmd->convert_src)
934 err++;
935
936 /* issue a trigger when scan is finished and start a new scan */
937 tmp = cmd->scan_end_src;
938 cmd->scan_end_src &= TRIG_COUNT;
939 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
940 err++;
941
942 /* trigger at the end of count events or not, stop condition or not */
943 tmp = cmd->stop_src;
944 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
945 if (!cmd->stop_src || tmp != cmd->stop_src)
946 err++;
947
948 if (err)
949 return 1;
950
951 /*
952 * step 2: make sure trigger sources are unique and mutually compatible
953 * note that mutual compatibility is not an issue here
954 */
955 if (cmd->scan_begin_src != TRIG_FOLLOW &&
956 cmd->scan_begin_src != TRIG_EXT &&
957 cmd->scan_begin_src != TRIG_TIMER)
958 err++;
959 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
960 err++;
961
962 if (err)
963 return 2;
964
965 /* step 3: make sure arguments are trivially compatible */
966 if (cmd->start_arg != 0) {
967 cmd->start_arg = 0;
968 err++;
969 }
970
971 if (cmd->scan_begin_src == TRIG_FOLLOW) {
972 /* internal trigger */
973 if (cmd->scan_begin_arg != 0) {
974 cmd->scan_begin_arg = 0;
975 err++;
976 }
977 }
978
979 if (cmd->scan_begin_src == TRIG_TIMER) {
980 if (this_usbduxsub->high_speed) {
981 /*
982 * In high speed mode microframes are possible.
983 * However, during one microframe we can roughly
984 * sample two channels. Thus, the more channels
985 * are in the channel list the more time we need.
986 */
987 i = chanToInterval(cmd->chanlist_len);
988 if (cmd->scan_begin_arg < (1000000 / 8 * i)) {
989 cmd->scan_begin_arg = 1000000 / 8 * i;
990 err++;
991 }
992 /* now calc the real sampling rate with all the
993 * rounding errors */
994 tmpTimer =
995 ((unsigned int)(cmd->scan_begin_arg / 125000)) *
996 125000;
997 if (cmd->scan_begin_arg != tmpTimer) {
998 cmd->scan_begin_arg = tmpTimer;
999 err++;
1000 }
1001 } else {
1002 /* full speed */
1003 /* 1kHz scans every USB frame */
1004 if (cmd->scan_begin_arg < 1000000) {
1005 cmd->scan_begin_arg = 1000000;
1006 err++;
1007 }
1008 /*
1009 * calc the real sampling rate with the rounding errors
1010 */
1011 tmpTimer = ((unsigned int)(cmd->scan_begin_arg /
1012 1000000)) * 1000000;
1013 if (cmd->scan_begin_arg != tmpTimer) {
1014 cmd->scan_begin_arg = tmpTimer;
1015 err++;
1016 }
1017 }
1018 }
1019 /* the same argument */
1020 if (cmd->scan_end_arg != cmd->chanlist_len) {
1021 cmd->scan_end_arg = cmd->chanlist_len;
1022 err++;
1023 }
1024
1025 if (cmd->stop_src == TRIG_COUNT) {
1026 /* any count is allowed */
1027 } else {
1028 /* TRIG_NONE */
1029 if (cmd->stop_arg != 0) {
1030 cmd->stop_arg = 0;
1031 err++;
1032 }
1033 }
1034
1035 if (err)
1036 return 3;
1037
1038 return 0;
1039 }
1040
1041 /*
1042 * creates the ADC command for the MAX1271
1043 * range is the range value from comedi
1044 */
1045 static void create_adc_command(unsigned int chan,
1046 uint8_t *muxsg0,
1047 uint8_t *muxsg1)
1048 {
1049 if (chan < 8)
1050 (*muxsg0) = (*muxsg0) | (1 << chan);
1051 else if (chan < 16)
1052 (*muxsg1) = (*muxsg1) | (1 << (chan-8));
1053 }
1054
1055
1056 /* bulk transfers to usbdux */
1057
1058 #define SENDADCOMMANDS 0
1059 #define SENDDACOMMANDS 1
1060 #define SENDDIOCONFIGCOMMAND 2
1061 #define SENDDIOBITSCOMMAND 3
1062 #define SENDSINGLEAD 4
1063 #define SENDPWMON 7
1064 #define SENDPWMOFF 8
1065
1066 static int send_dux_commands(struct usbduxsub *this_usbduxsub, int cmd_type)
1067 {
1068 int result, nsent;
1069
1070 this_usbduxsub->dux_commands[0] = cmd_type;
1071 #ifdef NOISY_DUX_DEBUGBUG
1072 printk(KERN_DEBUG "comedi%d: usbdux: dux_commands: ",
1073 this_usbduxsub->comedidev->minor);
1074 for (result = 0; result < SIZEOFDUXBUFFER; result++)
1075 printk(" %02x", this_usbduxsub->dux_commands[result]);
1076 printk("\n");
1077 #endif
1078 result = usb_bulk_msg(this_usbduxsub->usbdev,
1079 usb_sndbulkpipe(this_usbduxsub->usbdev,
1080 COMMAND_OUT_EP),
1081 this_usbduxsub->dux_commands, SIZEOFDUXBUFFER,
1082 &nsent, BULK_TIMEOUT);
1083 if (result < 0)
1084 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1085 "could not transmit dux_command to the usb-device, "
1086 "err=%d\n", this_usbduxsub->comedidev->minor, result);
1087
1088 return result;
1089 }
1090
1091 static int receive_dux_commands(struct usbduxsub *this_usbduxsub, int command)
1092 {
1093 int result = (-EFAULT);
1094 int nrec;
1095 int i;
1096
1097 for (i = 0; i < RETRIES; i++) {
1098 result = usb_bulk_msg(this_usbduxsub->usbdev,
1099 usb_rcvbulkpipe(this_usbduxsub->usbdev,
1100 COMMAND_IN_EP),
1101 this_usbduxsub->insnBuffer, SIZEINSNBUF,
1102 &nrec, BULK_TIMEOUT);
1103 if (result < 0) {
1104 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1105 "insn: USB error %d "
1106 "while receiving DUX command"
1107 "\n", this_usbduxsub->comedidev->minor,
1108 result);
1109 return result;
1110 }
1111 if (this_usbduxsub->insnBuffer[0] == command)
1112 return result;
1113 }
1114 /* this is only reached if the data has been requested a couple of
1115 * times */
1116 dev_err(&this_usbduxsub->interface->dev, "comedi%d: insn: "
1117 "wrong data returned from firmware: want %d, got %d.\n",
1118 this_usbduxsub->comedidev->minor, command,
1119 this_usbduxsub->insnBuffer[0]);
1120 return -EFAULT;
1121 }
1122
1123 static int usbdux_ai_inttrig(struct comedi_device *dev,
1124 struct comedi_subdevice *s, unsigned int trignum)
1125 {
1126 int ret;
1127 struct usbduxsub *this_usbduxsub = dev->private;
1128 if (!this_usbduxsub)
1129 return -EFAULT;
1130
1131 down(&this_usbduxsub->sem);
1132 if (!(this_usbduxsub->probed)) {
1133 up(&this_usbduxsub->sem);
1134 return -ENODEV;
1135 }
1136 dev_dbg(&this_usbduxsub->interface->dev,
1137 "comedi%d: usbdux_ai_inttrig\n", dev->minor);
1138
1139 if (trignum != 0) {
1140 dev_err(&this_usbduxsub->interface->dev,
1141 "comedi%d: usbdux_ai_inttrig: invalid trignum\n",
1142 dev->minor);
1143 up(&this_usbduxsub->sem);
1144 return -EINVAL;
1145 }
1146 if (!(this_usbduxsub->ai_cmd_running)) {
1147 this_usbduxsub->ai_cmd_running = 1;
1148 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1149 if (ret < 0) {
1150 dev_err(&this_usbduxsub->interface->dev,
1151 "comedi%d: usbdux_ai_inttrig: "
1152 "urbSubmit: err=%d\n", dev->minor, ret);
1153 this_usbduxsub->ai_cmd_running = 0;
1154 up(&this_usbduxsub->sem);
1155 return ret;
1156 }
1157 s->async->inttrig = NULL;
1158 } else {
1159 dev_err(&this_usbduxsub->interface->dev,
1160 "comedi%d: ai_inttrig but acqu is already running\n",
1161 dev->minor);
1162 }
1163 up(&this_usbduxsub->sem);
1164 return 1;
1165 }
1166
1167 static int usbdux_ai_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1168 {
1169 struct comedi_cmd *cmd = &s->async->cmd;
1170 unsigned int chan;
1171 int i, ret;
1172 struct usbduxsub *this_usbduxsub = dev->private;
1173 int result;
1174 uint8_t muxsg0 = 0;
1175 uint8_t muxsg1 = 0;
1176 uint8_t sysred = 0;
1177
1178 if (!this_usbduxsub)
1179 return -EFAULT;
1180
1181 dev_dbg(&this_usbduxsub->interface->dev,
1182 "comedi%d: usbdux_ai_cmd\n", dev->minor);
1183
1184 /* block other CPUs from starting an ai_cmd */
1185 down(&this_usbduxsub->sem);
1186
1187 if (!(this_usbduxsub->probed)) {
1188 up(&this_usbduxsub->sem);
1189 return -ENODEV;
1190 }
1191 if (this_usbduxsub->ai_cmd_running) {
1192 dev_err(&this_usbduxsub->interface->dev, "comedi%d: "
1193 "ai_cmd not possible. Another ai_cmd is running.\n",
1194 dev->minor);
1195 up(&this_usbduxsub->sem);
1196 return -EBUSY;
1197 }
1198 /* set current channel of the running acquisition to zero */
1199 s->async->cur_chan = 0;
1200
1201 /* first the number of channels per time step */
1202 this_usbduxsub->dux_commands[1] = cmd->chanlist_len;
1203
1204 /* CONFIG0 */
1205 this_usbduxsub->dux_commands[2] = 0x12;
1206
1207 /* CONFIG1: 23kHz sampling rate, delay = 0us, */
1208 this_usbduxsub->dux_commands[3] = 0x03;
1209
1210 /* CONFIG3: differential channels off */
1211 this_usbduxsub->dux_commands[4] = 0x00;
1212
1213 for (i = 0; i < cmd->chanlist_len; i++) {
1214 chan = CR_CHAN(cmd->chanlist[i]);
1215 create_adc_command(chan, &muxsg0, &muxsg1);
1216 if (i >= NUMCHANNELS) {
1217 dev_err(&this_usbduxsub->interface->dev,
1218 "comedi%d: channel list too long\n",
1219 dev->minor);
1220 break;
1221 }
1222 }
1223 this_usbduxsub->dux_commands[5] = muxsg0;
1224 this_usbduxsub->dux_commands[6] = muxsg1;
1225 this_usbduxsub->dux_commands[7] = sysred;
1226
1227 dev_dbg(&this_usbduxsub->interface->dev,
1228 "comedi %d: sending commands to the usb device: size=%u\n",
1229 dev->minor, NUMCHANNELS);
1230
1231 result = send_dux_commands(this_usbduxsub, SENDADCOMMANDS);
1232 if (result < 0) {
1233 up(&this_usbduxsub->sem);
1234 return result;
1235 }
1236
1237 if (this_usbduxsub->high_speed) {
1238 /*
1239 * every 2 channels get a time window of 125us. Thus, if we
1240 * sample all 16 channels we need 1ms. If we sample only one
1241 * channel we need only 125us
1242 */
1243 this_usbduxsub->ai_interval =
1244 chanToInterval(cmd->chanlist_len);
1245 this_usbduxsub->ai_timer = cmd->scan_begin_arg / (125000 *
1246 (this_usbduxsub->
1247 ai_interval));
1248 } else {
1249 /* interval always 1ms */
1250 this_usbduxsub->ai_interval = 1;
1251 this_usbduxsub->ai_timer = cmd->scan_begin_arg / 1000000;
1252 }
1253 if (this_usbduxsub->ai_timer < 1) {
1254 dev_err(&this_usbduxsub->interface->dev, "comedi%d: ai_cmd: "
1255 "timer=%d, scan_begin_arg=%d. "
1256 "Not properly tested by cmdtest?\n", dev->minor,
1257 this_usbduxsub->ai_timer, cmd->scan_begin_arg);
1258 up(&this_usbduxsub->sem);
1259 return -EINVAL;
1260 }
1261 this_usbduxsub->ai_counter = this_usbduxsub->ai_timer;
1262
1263 if (cmd->stop_src == TRIG_COUNT) {
1264 /* data arrives as one packet */
1265 this_usbduxsub->ai_sample_count = cmd->stop_arg;
1266 this_usbduxsub->ai_continuous = 0;
1267 } else {
1268 /* continuous acquisition */
1269 this_usbduxsub->ai_continuous = 1;
1270 this_usbduxsub->ai_sample_count = 0;
1271 }
1272
1273 if (cmd->start_src == TRIG_NOW) {
1274 /* enable this acquisition operation */
1275 this_usbduxsub->ai_cmd_running = 1;
1276 ret = usbduxsub_submit_InURBs(this_usbduxsub);
1277 if (ret < 0) {
1278 this_usbduxsub->ai_cmd_running = 0;
1279 /* fixme: unlink here?? */
1280 up(&this_usbduxsub->sem);
1281 return ret;
1282 }
1283 s->async->inttrig = NULL;
1284 } else {
1285 /* TRIG_INT */
1286 /* don't enable the acquision operation */
1287 /* wait for an internal signal */
1288 s->async->inttrig = usbdux_ai_inttrig;
1289 }
1290 up(&this_usbduxsub->sem);
1291 return 0;
1292 }
1293
1294 /* Mode 0 is used to get a single conversion on demand */
1295 static int usbdux_ai_insn_read(struct comedi_device *dev,
1296 struct comedi_subdevice *s,
1297 struct comedi_insn *insn, unsigned int *data)
1298 {
1299 int i;
1300 int32_t one = 0;
1301 int chan;
1302 int err;
1303 struct usbduxsub *this_usbduxsub = dev->private;
1304 uint8_t muxsg0 = 0;
1305 uint8_t muxsg1 = 0;
1306 uint8_t sysred = 0;
1307
1308 if (!this_usbduxsub)
1309 return 0;
1310
1311 dev_dbg(&this_usbduxsub->interface->dev,
1312 "comedi%d: ai_insn_read, insn->n=%d, insn->subdev=%d\n",
1313 dev->minor, insn->n, insn->subdev);
1314
1315 down(&this_usbduxsub->sem);
1316 if (!(this_usbduxsub->probed)) {
1317 up(&this_usbduxsub->sem);
1318 return -ENODEV;
1319 }
1320 if (this_usbduxsub->ai_cmd_running) {
1321 dev_err(&this_usbduxsub->interface->dev,
1322 "comedi%d: ai_insn_read not possible. "
1323 "Async Command is running.\n", dev->minor);
1324 up(&this_usbduxsub->sem);
1325 return 0;
1326 }
1327
1328 /* sample one channel */
1329 /* CONFIG0: chopper on */
1330 this_usbduxsub->dux_commands[1] = 0x16;
1331
1332 /* CONFIG1: 2kHz sampling rate */
1333 this_usbduxsub->dux_commands[2] = 0x80;
1334
1335 /* CONFIG3: differential channels off */
1336 this_usbduxsub->dux_commands[3] = 0x00;
1337
1338 chan = CR_CHAN(insn->chanspec);
1339 create_adc_command(chan, &muxsg0, &muxsg1);
1340
1341 this_usbduxsub->dux_commands[4] = muxsg0;
1342 this_usbduxsub->dux_commands[5] = muxsg1;
1343 this_usbduxsub->dux_commands[6] = sysred;
1344
1345 /* adc commands */
1346 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1347 if (err < 0) {
1348 up(&this_usbduxsub->sem);
1349 return err;
1350 }
1351
1352 for (i = 0; i < insn->n; i++) {
1353 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1354 if (err < 0) {
1355 up(&this_usbduxsub->sem);
1356 return 0;
1357 }
1358 /* 32 bits big endian from the A/D converter */
1359 one = be32_to_cpu(*((int32_t *)
1360 ((this_usbduxsub->insnBuffer)+1)));
1361 /* mask out the staus byte */
1362 one = one & 0x00ffffff;
1363 /* turn it into an unsigned integer */
1364 one = one ^ 0x00800000;
1365 data[i] = one;
1366 }
1367 up(&this_usbduxsub->sem);
1368 return i;
1369 }
1370
1371
1372
1373
1374 static int usbdux_getstatusinfo(struct comedi_device *dev, int chan)
1375 {
1376 struct usbduxsub *this_usbduxsub = dev->private;
1377 uint8_t sysred = 0;
1378 uint32_t one;
1379 int err;
1380
1381 if (!this_usbduxsub)
1382 return 0;
1383
1384 if (this_usbduxsub->ai_cmd_running) {
1385 dev_err(&this_usbduxsub->interface->dev,
1386 "comedi%d: status read not possible. "
1387 "Async Command is running.\n", dev->minor);
1388 return 0;
1389 }
1390
1391 /* CONFIG0 */
1392 this_usbduxsub->dux_commands[1] = 0x12;
1393
1394 /* CONFIG1: 2kHz sampling rate */
1395 this_usbduxsub->dux_commands[2] = 0x80;
1396
1397 /* CONFIG3: differential channels off */
1398 this_usbduxsub->dux_commands[3] = 0x00;
1399
1400 if (chan == 1) {
1401 /* ADC offset */
1402 sysred = sysred | 1;
1403 } else if (chan == 2) {
1404 /* VCC */
1405 sysred = sysred | 4;
1406 } else if (chan == 3) {
1407 /* temperature */
1408 sysred = sysred | 8;
1409 } else if (chan == 4) {
1410 /* gain */
1411 sysred = sysred | 16;
1412 } else if (chan == 5) {
1413 /* ref */
1414 sysred = sysred | 32;
1415 }
1416
1417 this_usbduxsub->dux_commands[4] = 0;
1418 this_usbduxsub->dux_commands[5] = 0;
1419 this_usbduxsub->dux_commands[6] = sysred;
1420
1421 /* adc commands */
1422 err = send_dux_commands(this_usbduxsub, SENDSINGLEAD);
1423 if (err < 0)
1424 return err;
1425
1426 err = receive_dux_commands(this_usbduxsub, SENDSINGLEAD);
1427 if (err < 0)
1428 return err;
1429
1430 /* 32 bits big endian from the A/D converter */
1431 one = be32_to_cpu(*((int32_t *)((this_usbduxsub->insnBuffer)+1)));
1432 /* mask out the staus byte */
1433 one = one & 0x00ffffff;
1434 one = one ^ 0x00800000;
1435
1436 return (int)one;
1437 }
1438
1439
1440
1441
1442
1443
1444 /************************************/
1445 /* analog out */
1446
1447 static int usbdux_ao_insn_read(struct comedi_device *dev,
1448 struct comedi_subdevice *s,
1449 struct comedi_insn *insn, unsigned int *data)
1450 {
1451 int i;
1452 int chan = CR_CHAN(insn->chanspec);
1453 struct usbduxsub *this_usbduxsub = dev->private;
1454
1455 if (!this_usbduxsub)
1456 return -EFAULT;
1457
1458 down(&this_usbduxsub->sem);
1459 if (!(this_usbduxsub->probed)) {
1460 up(&this_usbduxsub->sem);
1461 return -ENODEV;
1462 }
1463 for (i = 0; i < insn->n; i++)
1464 data[i] = this_usbduxsub->outBuffer[chan];
1465
1466 up(&this_usbduxsub->sem);
1467 return i;
1468 }
1469
1470 static int usbdux_ao_insn_write(struct comedi_device *dev,
1471 struct comedi_subdevice *s,
1472 struct comedi_insn *insn, unsigned int *data)
1473 {
1474 int i, err;
1475 int chan = CR_CHAN(insn->chanspec);
1476 struct usbduxsub *this_usbduxsub = dev->private;
1477
1478 if (!this_usbduxsub)
1479 return -EFAULT;
1480
1481 dev_dbg(&this_usbduxsub->interface->dev,
1482 "comedi%d: ao_insn_write\n", dev->minor);
1483
1484 down(&this_usbduxsub->sem);
1485 if (!(this_usbduxsub->probed)) {
1486 up(&this_usbduxsub->sem);
1487 return -ENODEV;
1488 }
1489 if (this_usbduxsub->ao_cmd_running) {
1490 dev_err(&this_usbduxsub->interface->dev,
1491 "comedi%d: ao_insn_write: "
1492 "ERROR: asynchronous ao_cmd is running\n", dev->minor);
1493 up(&this_usbduxsub->sem);
1494 return 0;
1495 }
1496
1497 for (i = 0; i < insn->n; i++) {
1498 dev_dbg(&this_usbduxsub->interface->dev,
1499 "comedi%d: ao_insn_write: data[chan=%d,i=%d]=%d\n",
1500 dev->minor, chan, i, data[i]);
1501
1502 /* number of channels: 1 */
1503 this_usbduxsub->dux_commands[1] = 1;
1504 /* channel number */
1505 this_usbduxsub->dux_commands[2] = data[i];
1506 this_usbduxsub->outBuffer[chan] = data[i];
1507 this_usbduxsub->dux_commands[3] = chan;
1508 err = send_dux_commands(this_usbduxsub, SENDDACOMMANDS);
1509 if (err < 0) {
1510 up(&this_usbduxsub->sem);
1511 return err;
1512 }
1513 }
1514 up(&this_usbduxsub->sem);
1515
1516 return i;
1517 }
1518
1519 static int usbdux_ao_inttrig(struct comedi_device *dev,
1520 struct comedi_subdevice *s, unsigned int trignum)
1521 {
1522 int ret;
1523 struct usbduxsub *this_usbduxsub = dev->private;
1524
1525 if (!this_usbduxsub)
1526 return -EFAULT;
1527
1528 down(&this_usbduxsub->sem);
1529
1530 if (!(this_usbduxsub->probed)) {
1531 ret = -ENODEV;
1532 goto out;
1533 }
1534 if (trignum != 0) {
1535 dev_err(&this_usbduxsub->interface->dev,
1536 "comedi%d: usbdux_ao_inttrig: invalid trignum\n",
1537 dev->minor);
1538 ret = -EINVAL;
1539 goto out;
1540 }
1541 if (!(this_usbduxsub->ao_cmd_running)) {
1542 this_usbduxsub->ao_cmd_running = 1;
1543 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1544 if (ret < 0) {
1545 dev_err(&this_usbduxsub->interface->dev,
1546 "comedi%d: usbdux_ao_inttrig: submitURB: "
1547 "err=%d\n", dev->minor, ret);
1548 this_usbduxsub->ao_cmd_running = 0;
1549 goto out;
1550 }
1551 s->async->inttrig = NULL;
1552 } else {
1553 dev_err(&this_usbduxsub->interface->dev,
1554 "comedi%d: ao_inttrig but acqu is already running.\n",
1555 dev->minor);
1556 }
1557 ret = 1;
1558 out:
1559 up(&this_usbduxsub->sem);
1560 return ret;
1561 }
1562
1563 static int usbdux_ao_cmdtest(struct comedi_device *dev,
1564 struct comedi_subdevice *s,
1565 struct comedi_cmd *cmd)
1566 {
1567 int err = 0, tmp;
1568 struct usbduxsub *this_usbduxsub = dev->private;
1569
1570 if (!this_usbduxsub)
1571 return -EFAULT;
1572
1573 if (!(this_usbduxsub->probed))
1574 return -ENODEV;
1575
1576 dev_dbg(&this_usbduxsub->interface->dev,
1577 "comedi%d: usbdux_ao_cmdtest\n", dev->minor);
1578
1579 /* make sure triggers are valid */
1580 /* Only immediate triggers are allowed */
1581 tmp = cmd->start_src;
1582 cmd->start_src &= TRIG_NOW | TRIG_INT;
1583 if (!cmd->start_src || tmp != cmd->start_src)
1584 err++;
1585
1586 /* trigger should happen timed */
1587 tmp = cmd->scan_begin_src;
1588 /* just now we scan also in the high speed mode every frame */
1589 /* this is due to ehci driver limitations */
1590 if (0) { /* (this_usbduxsub->high_speed) */
1591 /* start immediately a new scan */
1592 /* the sampling rate is set by the coversion rate */
1593 cmd->scan_begin_src &= TRIG_FOLLOW;
1594 } else {
1595 /* start a new scan (output at once) with a timer */
1596 cmd->scan_begin_src &= TRIG_TIMER;
1597 }
1598 if (!cmd->scan_begin_src || tmp != cmd->scan_begin_src)
1599 err++;
1600
1601 /* scanning is continuous */
1602 tmp = cmd->convert_src;
1603
1604 /* all conversion events happen simultaneously */
1605 cmd->convert_src &= TRIG_NOW;
1606
1607 if (!cmd->convert_src || tmp != cmd->convert_src)
1608 err++;
1609
1610 /* issue a trigger when scan is finished and start a new scan */
1611 tmp = cmd->scan_end_src;
1612 cmd->scan_end_src &= TRIG_COUNT;
1613 if (!cmd->scan_end_src || tmp != cmd->scan_end_src)
1614 err++;
1615
1616 /* trigger at the end of count events or not, stop condition or not */
1617 tmp = cmd->stop_src;
1618 cmd->stop_src &= TRIG_COUNT | TRIG_NONE;
1619 if (!cmd->stop_src || tmp != cmd->stop_src)
1620 err++;
1621
1622 if (err)
1623 return 1;
1624
1625 /*
1626 * step 2: make sure trigger sources
1627 * are unique and mutually compatible
1628 * note that mutual compatibility is not an issue here
1629 */
1630 if (cmd->scan_begin_src != TRIG_FOLLOW &&
1631 cmd->scan_begin_src != TRIG_EXT &&
1632 cmd->scan_begin_src != TRIG_TIMER)
1633 err++;
1634 if (cmd->stop_src != TRIG_COUNT && cmd->stop_src != TRIG_NONE)
1635 err++;
1636
1637 if (err)
1638 return 2;
1639
1640 /* step 3: make sure arguments are trivially compatible */
1641
1642 if (cmd->start_arg != 0) {
1643 cmd->start_arg = 0;
1644 err++;
1645 }
1646
1647 if (cmd->scan_begin_src == TRIG_FOLLOW) {
1648 /* internal trigger */
1649 if (cmd->scan_begin_arg != 0) {
1650 cmd->scan_begin_arg = 0;
1651 err++;
1652 }
1653 }
1654
1655 if (cmd->scan_begin_src == TRIG_TIMER) {
1656 /* timer */
1657 if (cmd->scan_begin_arg < 1000000) {
1658 cmd->scan_begin_arg = 1000000;
1659 err++;
1660 }
1661 }
1662 /* not used now, is for later use */
1663 if (cmd->convert_src == TRIG_TIMER) {
1664 if (cmd->convert_arg < 125000) {
1665 cmd->convert_arg = 125000;
1666 err++;
1667 }
1668 }
1669
1670 /* the same argument */
1671 if (cmd->scan_end_arg != cmd->chanlist_len) {
1672 cmd->scan_end_arg = cmd->chanlist_len;
1673 err++;
1674 }
1675
1676 if (cmd->stop_src == TRIG_COUNT) {
1677 /* any count is allowed */
1678 } else {
1679 /* TRIG_NONE */
1680 if (cmd->stop_arg != 0) {
1681 cmd->stop_arg = 0;
1682 err++;
1683 }
1684 }
1685
1686 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: err=%d, "
1687 "scan_begin_src=%d, scan_begin_arg=%d, convert_src=%d, "
1688 "convert_arg=%d\n", dev->minor, err, cmd->scan_begin_src,
1689 cmd->scan_begin_arg, cmd->convert_src, cmd->convert_arg);
1690
1691 if (err)
1692 return 3;
1693
1694 return 0;
1695 }
1696
1697 static int usbdux_ao_cmd(struct comedi_device *dev, struct comedi_subdevice *s)
1698 {
1699 struct comedi_cmd *cmd = &s->async->cmd;
1700 unsigned int chan, gain;
1701 int i, ret;
1702 struct usbduxsub *this_usbduxsub = dev->private;
1703
1704 if (!this_usbduxsub)
1705 return -EFAULT;
1706
1707 down(&this_usbduxsub->sem);
1708 if (!(this_usbduxsub->probed)) {
1709 up(&this_usbduxsub->sem);
1710 return -ENODEV;
1711 }
1712 dev_dbg(&this_usbduxsub->interface->dev,
1713 "comedi%d: %s\n", dev->minor, __func__);
1714
1715 /* set current channel of the running acquisition to zero */
1716 s->async->cur_chan = 0;
1717 for (i = 0; i < cmd->chanlist_len; ++i) {
1718 chan = CR_CHAN(cmd->chanlist[i]);
1719 gain = CR_RANGE(cmd->chanlist[i]);
1720 if (i >= NUMOUTCHANNELS) {
1721 dev_err(&this_usbduxsub->interface->dev,
1722 "comedi%d: %s: channel list too long\n",
1723 dev->minor, __func__);
1724 break;
1725 }
1726 this_usbduxsub->dac_commands[i] = chan;
1727 dev_dbg(&this_usbduxsub->interface->dev,
1728 "comedi%d: dac command for ch %d is %x\n",
1729 dev->minor, i, this_usbduxsub->dac_commands[i]);
1730 }
1731
1732 /* we count in steps of 1ms (125us) */
1733 /* 125us mode not used yet */
1734 if (0) { /* (this_usbduxsub->high_speed) */
1735 /* 125us */
1736 /* timing of the conversion itself: every 125 us */
1737 this_usbduxsub->ao_timer = cmd->convert_arg / 125000;
1738 } else {
1739 /* 1ms */
1740 /* timing of the scan: we get all channels at once */
1741 this_usbduxsub->ao_timer = cmd->scan_begin_arg / 1000000;
1742 dev_dbg(&this_usbduxsub->interface->dev,
1743 "comedi%d: scan_begin_src=%d, scan_begin_arg=%d, "
1744 "convert_src=%d, convert_arg=%d\n", dev->minor,
1745 cmd->scan_begin_src, cmd->scan_begin_arg,
1746 cmd->convert_src, cmd->convert_arg);
1747 dev_dbg(&this_usbduxsub->interface->dev,
1748 "comedi%d: ao_timer=%d (ms)\n",
1749 dev->minor, this_usbduxsub->ao_timer);
1750 if (this_usbduxsub->ao_timer < 1) {
1751 dev_err(&this_usbduxsub->interface->dev,
1752 "comedi%d: usbdux: ao_timer=%d, "
1753 "scan_begin_arg=%d. "
1754 "Not properly tested by cmdtest?\n",
1755 dev->minor, this_usbduxsub->ao_timer,
1756 cmd->scan_begin_arg);
1757 up(&this_usbduxsub->sem);
1758 return -EINVAL;
1759 }
1760 }
1761 this_usbduxsub->ao_counter = this_usbduxsub->ao_timer;
1762
1763 if (cmd->stop_src == TRIG_COUNT) {
1764 /* not continuous */
1765 /* counter */
1766 /* high speed also scans everything at once */
1767 if (0) { /* (this_usbduxsub->high_speed) */
1768 this_usbduxsub->ao_sample_count =
1769 (cmd->stop_arg) * (cmd->scan_end_arg);
1770 } else {
1771 /* there's no scan as the scan has been */
1772 /* perf inside the FX2 */
1773 /* data arrives as one packet */
1774 this_usbduxsub->ao_sample_count = cmd->stop_arg;
1775 }
1776 this_usbduxsub->ao_continuous = 0;
1777 } else {
1778 /* continuous acquisition */
1779 this_usbduxsub->ao_continuous = 1;
1780 this_usbduxsub->ao_sample_count = 0;
1781 }
1782
1783 if (cmd->start_src == TRIG_NOW) {
1784 /* enable this acquisition operation */
1785 this_usbduxsub->ao_cmd_running = 1;
1786 ret = usbduxsub_submit_OutURBs(this_usbduxsub);
1787 if (ret < 0) {
1788 this_usbduxsub->ao_cmd_running = 0;
1789 /* fixme: unlink here?? */
1790 up(&this_usbduxsub->sem);
1791 return ret;
1792 }
1793 s->async->inttrig = NULL;
1794 } else {
1795 /* TRIG_INT */
1796 /* submit the urbs later */
1797 /* wait for an internal signal */
1798 s->async->inttrig = usbdux_ao_inttrig;
1799 }
1800
1801 up(&this_usbduxsub->sem);
1802 return 0;
1803 }
1804
1805 static int usbdux_dio_insn_config(struct comedi_device *dev,
1806 struct comedi_subdevice *s,
1807 struct comedi_insn *insn, unsigned int *data)
1808 {
1809 int chan = CR_CHAN(insn->chanspec);
1810
1811 /* The input or output configuration of each digital line is
1812 * configured by a special insn_config instruction. chanspec
1813 * contains the channel to be changed, and data[0] contains the
1814 * value COMEDI_INPUT or COMEDI_OUTPUT. */
1815
1816 switch (data[0]) {
1817 case INSN_CONFIG_DIO_OUTPUT:
1818 s->io_bits |= 1 << chan; /* 1 means Out */
1819 break;
1820 case INSN_CONFIG_DIO_INPUT:
1821 s->io_bits &= ~(1 << chan);
1822 break;
1823 case INSN_CONFIG_DIO_QUERY:
1824 data[1] =
1825 (s->io_bits & (1 << chan)) ? COMEDI_OUTPUT : COMEDI_INPUT;
1826 break;
1827 default:
1828 return -EINVAL;
1829 break;
1830 }
1831 /* we don't tell the firmware here as it would take 8 frames */
1832 /* to submit the information. We do it in the insn_bits. */
1833 return insn->n;
1834 }
1835
1836 static int usbdux_dio_insn_bits(struct comedi_device *dev,
1837 struct comedi_subdevice *s,
1838 struct comedi_insn *insn,
1839 unsigned int *data)
1840 {
1841
1842 struct usbduxsub *this_usbduxsub = dev->private;
1843 int err;
1844
1845 if (!this_usbduxsub)
1846 return -EFAULT;
1847
1848 if (insn->n != 2)
1849 return -EINVAL;
1850
1851 down(&this_usbduxsub->sem);
1852
1853 if (!(this_usbduxsub->probed)) {
1854 up(&this_usbduxsub->sem);
1855 return -ENODEV;
1856 }
1857
1858 /* The insn data is a mask in data[0] and the new data
1859 * in data[1], each channel cooresponding to a bit. */
1860 s->state &= ~data[0];
1861 s->state |= data[0] & data[1];
1862 /* The commands are 8 bits wide */
1863 this_usbduxsub->dux_commands[1] = (s->io_bits) & 0x000000FF;
1864 this_usbduxsub->dux_commands[4] = (s->state) & 0x000000FF;
1865 this_usbduxsub->dux_commands[2] = ((s->io_bits) & 0x0000FF00) >> 8;
1866 this_usbduxsub->dux_commands[5] = ((s->state) & 0x0000FF00) >> 8;
1867 this_usbduxsub->dux_commands[3] = ((s->io_bits) & 0x00FF0000) >> 16;
1868 this_usbduxsub->dux_commands[6] = ((s->state) & 0x00FF0000) >> 16;
1869
1870 /* This command also tells the firmware to return */
1871 /* the digital input lines */
1872 err = send_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1873 if (err < 0) {
1874 up(&this_usbduxsub->sem);
1875 return err;
1876 }
1877 err = receive_dux_commands(this_usbduxsub, SENDDIOBITSCOMMAND);
1878 if (err < 0) {
1879 up(&this_usbduxsub->sem);
1880 return err;
1881 }
1882
1883 data[1] = (((unsigned int)(this_usbduxsub->insnBuffer[1]))&0xff) |
1884 ((((unsigned int)(this_usbduxsub->insnBuffer[2]))&0xff) << 8) |
1885 ((((unsigned int)(this_usbduxsub->insnBuffer[3]))&0xff) << 16);
1886
1887 s->state = data[1];
1888
1889 up(&this_usbduxsub->sem);
1890 return 2;
1891 }
1892
1893 /***********************************/
1894 /* PWM */
1895
1896 static int usbduxsub_unlink_PwmURBs(struct usbduxsub *usbduxsub_tmp)
1897 {
1898 int err = 0;
1899
1900 if (usbduxsub_tmp && usbduxsub_tmp->urbPwm) {
1901 if (usbduxsub_tmp->urbPwm)
1902 usb_kill_urb(usbduxsub_tmp->urbPwm);
1903 dev_dbg(&usbduxsub_tmp->interface->dev,
1904 "comedi: unlinked PwmURB: res=%d\n", err);
1905 }
1906 return err;
1907 }
1908
1909 /* This cancels a running acquisition operation
1910 * in any context.
1911 */
1912 static int usbdux_pwm_stop(struct usbduxsub *this_usbduxsub, int do_unlink)
1913 {
1914 int ret = 0;
1915
1916 if (!this_usbduxsub)
1917 return -EFAULT;
1918
1919 dev_dbg(&this_usbduxsub->interface->dev, "comedi: %s\n", __func__);
1920 if (do_unlink)
1921 ret = usbduxsub_unlink_PwmURBs(this_usbduxsub);
1922
1923 this_usbduxsub->pwm_cmd_running = 0;
1924
1925 return ret;
1926 }
1927
1928 /* force unlink - is called by comedi */
1929 static int usbdux_pwm_cancel(struct comedi_device *dev,
1930 struct comedi_subdevice *s)
1931 {
1932 struct usbduxsub *this_usbduxsub = dev->private;
1933 int res = 0;
1934
1935 /* unlink only if it is really running */
1936 res = usbdux_pwm_stop(this_usbduxsub, this_usbduxsub->pwm_cmd_running);
1937
1938 dev_dbg(&this_usbduxsub->interface->dev,
1939 "comedi %d: sending pwm off command to the usb device.\n",
1940 dev->minor);
1941 res = send_dux_commands(this_usbduxsub, SENDPWMOFF);
1942 if (res < 0)
1943 return res;
1944
1945 return res;
1946 }
1947
1948 static void usbduxsub_pwm_irq(struct urb *urb)
1949 {
1950 int ret;
1951 struct usbduxsub *this_usbduxsub;
1952 struct comedi_device *this_comedidev;
1953 struct comedi_subdevice *s;
1954
1955 /* printk(KERN_DEBUG "PWM: IRQ\n"); */
1956
1957 /* the context variable points to the subdevice */
1958 this_comedidev = urb->context;
1959 /* the private structure of the subdevice is struct usbduxsub */
1960 this_usbduxsub = this_comedidev->private;
1961
1962 s = this_comedidev->subdevices + SUBDEV_DA;
1963
1964 switch (urb->status) {
1965 case 0:
1966 /* success */
1967 break;
1968
1969 case -ECONNRESET:
1970 case -ENOENT:
1971 case -ESHUTDOWN:
1972 case -ECONNABORTED:
1973 /*
1974 * after an unlink command, unplug, ... etc
1975 * no unlink needed here. Already shutting down.
1976 */
1977 if (this_usbduxsub->pwm_cmd_running)
1978 usbdux_pwm_stop(this_usbduxsub, 0);
1979
1980 return;
1981
1982 default:
1983 /* a real error */
1984 if (this_usbduxsub->pwm_cmd_running) {
1985 dev_err(&this_usbduxsub->interface->dev,
1986 "comedi_: Non-zero urb status received in "
1987 "pwm intr context: %d\n", urb->status);
1988 usbdux_pwm_stop(this_usbduxsub, 0);
1989 }
1990 return;
1991 }
1992
1993 /* are we actually running? */
1994 if (!(this_usbduxsub->pwm_cmd_running))
1995 return;
1996
1997 urb->transfer_buffer_length = this_usbduxsub->sizePwmBuf;
1998 urb->dev = this_usbduxsub->usbdev;
1999 urb->status = 0;
2000 if (this_usbduxsub->pwm_cmd_running) {
2001 ret = usb_submit_urb(urb, GFP_ATOMIC);
2002 if (ret < 0) {
2003 dev_err(&this_usbduxsub->interface->dev,
2004 "comedi_: pwm urb resubm failed in int-cont. "
2005 "ret=%d", ret);
2006 if (ret == EL2NSYNC)
2007 dev_err(&this_usbduxsub->interface->dev,
2008 "buggy USB host controller or bug in "
2009 "IRQ handling!\n");
2010
2011 /* don't do an unlink here */
2012 usbdux_pwm_stop(this_usbduxsub, 0);
2013 }
2014 }
2015 }
2016
2017 static int usbduxsub_submit_PwmURBs(struct usbduxsub *usbduxsub)
2018 {
2019 int errFlag;
2020
2021 if (!usbduxsub)
2022 return -EFAULT;
2023
2024 dev_dbg(&usbduxsub->interface->dev, "comedi_: submitting pwm-urb\n");
2025
2026 /* in case of a resubmission after an unlink... */
2027 usb_fill_bulk_urb(usbduxsub->urbPwm,
2028 usbduxsub->usbdev,
2029 usb_sndbulkpipe(usbduxsub->usbdev, PWM_EP),
2030 usbduxsub->urbPwm->transfer_buffer,
2031 usbduxsub->sizePwmBuf, usbduxsub_pwm_irq,
2032 usbduxsub->comedidev);
2033
2034 errFlag = usb_submit_urb(usbduxsub->urbPwm, GFP_ATOMIC);
2035 if (errFlag) {
2036 dev_err(&usbduxsub->interface->dev,
2037 "comedi_: usbduxsigma: pwm: usb_submit_urb error %d\n",
2038 errFlag);
2039 return errFlag;
2040 }
2041 return 0;
2042 }
2043
2044 static int usbdux_pwm_period(struct comedi_device *dev,
2045 struct comedi_subdevice *s, unsigned int period)
2046 {
2047 struct usbduxsub *this_usbduxsub = dev->private;
2048 int fx2delay = 255;
2049
2050 if (period < MIN_PWM_PERIOD) {
2051 dev_err(&this_usbduxsub->interface->dev,
2052 "comedi%d: illegal period setting for pwm.\n",
2053 dev->minor);
2054 return -EAGAIN;
2055 } else {
2056 fx2delay = period / ((int)(6 * 512 * (1.0 / 0.033))) - 6;
2057 if (fx2delay > 255) {
2058 dev_err(&this_usbduxsub->interface->dev,
2059 "comedi%d: period %d for pwm is too low.\n",
2060 dev->minor, period);
2061 return -EAGAIN;
2062 }
2063 }
2064 this_usbduxsub->pwmDelay = fx2delay;
2065 this_usbduxsub->pwmPeriod = period;
2066 dev_dbg(&this_usbduxsub->interface->dev, "%s: frequ=%d, period=%d\n",
2067 __func__, period, fx2delay);
2068 return 0;
2069 }
2070
2071 /* is called from insn so there's no need to do all the sanity checks */
2072 static int usbdux_pwm_start(struct comedi_device *dev,
2073 struct comedi_subdevice *s)
2074 {
2075 int ret, i;
2076 struct usbduxsub *this_usbduxsub = dev->private;
2077
2078 dev_dbg(&this_usbduxsub->interface->dev, "comedi%d: %s\n",
2079 dev->minor, __func__);
2080
2081 if (this_usbduxsub->pwm_cmd_running) {
2082 /* already running */
2083 return 0;
2084 }
2085
2086 this_usbduxsub->dux_commands[1] = ((uint8_t) this_usbduxsub->pwmDelay);
2087 ret = send_dux_commands(this_usbduxsub, SENDPWMON);
2088 if (ret < 0)
2089 return ret;
2090
2091 /* initialise the buffer */
2092 for (i = 0; i < this_usbduxsub->sizePwmBuf; i++)
2093 ((char *)(this_usbduxsub->urbPwm->transfer_buffer))[i] = 0;
2094
2095 this_usbduxsub->pwm_cmd_running = 1;
2096 ret = usbduxsub_submit_PwmURBs(this_usbduxsub);
2097 if (ret < 0) {
2098 this_usbduxsub->pwm_cmd_running = 0;
2099 return ret;
2100 }
2101 return 0;
2102 }
2103
2104 /* generates the bit pattern for PWM with the optional sign bit */
2105 static int usbdux_pwm_pattern(struct comedi_device *dev,
2106 struct comedi_subdevice *s, int channel,
2107 unsigned int value, unsigned int sign)
2108 {
2109 struct usbduxsub *this_usbduxsub = dev->private;
2110 int i, szbuf;
2111 char *pBuf;
2112 char pwm_mask;
2113 char sgn_mask;
2114 char c;
2115
2116 if (!this_usbduxsub)
2117 return -EFAULT;
2118
2119 /* this is the DIO bit which carries the PWM data */
2120 pwm_mask = (1 << channel);
2121 /* this is the DIO bit which carries the optional direction bit */
2122 sgn_mask = (16 << channel);
2123 /* this is the buffer which will be filled with the with bit */
2124 /* pattern for one period */
2125 szbuf = this_usbduxsub->sizePwmBuf;
2126 pBuf = (char *)(this_usbduxsub->urbPwm->transfer_buffer);
2127 for (i = 0; i < szbuf; i++) {
2128 c = *pBuf;
2129 /* reset bits */
2130 c = c & (~pwm_mask);
2131 /* set the bit as long as the index is lower than the value */
2132 if (i < value)
2133 c = c | pwm_mask;
2134 /* set the optional sign bit for a relay */
2135 if (!sign) {
2136 /* positive value */
2137 c = c & (~sgn_mask);
2138 } else {
2139 /* negative value */
2140 c = c | sgn_mask;
2141 }
2142 *(pBuf++) = c;
2143 }
2144 return 1;
2145 }
2146
2147 static int usbdux_pwm_write(struct comedi_device *dev,
2148 struct comedi_subdevice *s,
2149 struct comedi_insn *insn, unsigned int *data)
2150 {
2151 struct usbduxsub *this_usbduxsub = dev->private;
2152
2153 if (!this_usbduxsub)
2154 return -EFAULT;
2155
2156 if ((insn->n) != 1) {
2157 /*
2158 * doesn't make sense to have more than one value here because
2159 * it would just overwrite the PWM buffer a couple of times
2160 */
2161 return -EINVAL;
2162 }
2163
2164 /*
2165 * the sign is set via a special INSN only, this gives us 8 bits for
2166 * normal operation
2167 * relay sign 0 by default
2168 */
2169 return usbdux_pwm_pattern(dev, s, CR_CHAN(insn->chanspec), data[0], 0);
2170 }
2171
2172 static int usbdux_pwm_read(struct comedi_device *x1,
2173 struct comedi_subdevice *x2, struct comedi_insn *x3,
2174 unsigned int *x4)
2175 {
2176 /* not needed */
2177 return -EINVAL;
2178 };
2179
2180 /* switches on/off PWM */
2181 static int usbdux_pwm_config(struct comedi_device *dev,
2182 struct comedi_subdevice *s,
2183 struct comedi_insn *insn, unsigned int *data)
2184 {
2185 struct usbduxsub *this_usbduxsub = dev->private;
2186 switch (data[0]) {
2187 case INSN_CONFIG_ARM:
2188 /* switch it on */
2189 dev_dbg(&this_usbduxsub->interface->dev,
2190 "comedi%d: %s: pwm on\n", dev->minor, __func__);
2191 /*
2192 * if not zero the PWM is limited to a certain time which is
2193 * not supported here
2194 */
2195 if (data[1] != 0)
2196 return -EINVAL;
2197 return usbdux_pwm_start(dev, s);
2198 case INSN_CONFIG_DISARM:
2199 dev_dbg(&this_usbduxsub->interface->dev,
2200 "comedi%d: %s: pwm off\n", dev->minor, __func__);
2201 return usbdux_pwm_cancel(dev, s);
2202 case INSN_CONFIG_GET_PWM_STATUS:
2203 /*
2204 * to check if the USB transmission has failed or in case PWM
2205 * was limited to n cycles to check if it has terminated
2206 */
2207 data[1] = this_usbduxsub->pwm_cmd_running;
2208 return 0;
2209 case INSN_CONFIG_PWM_SET_PERIOD:
2210 dev_dbg(&this_usbduxsub->interface->dev,
2211 "comedi%d: %s: setting period\n", dev->minor,
2212 __func__);
2213 return usbdux_pwm_period(dev, s, data[1]);
2214 case INSN_CONFIG_PWM_GET_PERIOD:
2215 data[1] = this_usbduxsub->pwmPeriod;
2216 return 0;
2217 case INSN_CONFIG_PWM_SET_H_BRIDGE:
2218 /* value in the first byte and the sign in the second for a
2219 relay */
2220 return usbdux_pwm_pattern(dev, s,
2221 /* the channel number */
2222 CR_CHAN(insn->chanspec),
2223 /* actual PWM data */
2224 data[1],
2225 /* just a sign */
2226 (data[2] != 0));
2227 case INSN_CONFIG_PWM_GET_H_BRIDGE:
2228 /* values are not kept in this driver, nothing to return */
2229 return -EINVAL;
2230 }
2231 return -EINVAL;
2232 }
2233
2234 /* end of PWM */
2235 /*****************************************************************/
2236
2237 static void tidy_up(struct usbduxsub *usbduxsub_tmp)
2238 {
2239 int i;
2240
2241 if (!usbduxsub_tmp)
2242 return;
2243 dev_dbg(&usbduxsub_tmp->interface->dev, "comedi_: tiding up\n");
2244
2245 /* shows the usb subsystem that the driver is down */
2246 if (usbduxsub_tmp->interface)
2247 usb_set_intfdata(usbduxsub_tmp->interface, NULL);
2248
2249 usbduxsub_tmp->probed = 0;
2250
2251 if (usbduxsub_tmp->urbIn) {
2252 if (usbduxsub_tmp->ai_cmd_running) {
2253 usbduxsub_tmp->ai_cmd_running = 0;
2254 usbduxsub_unlink_InURBs(usbduxsub_tmp);
2255 }
2256 for (i = 0; i < usbduxsub_tmp->numOfInBuffers; i++) {
2257 kfree(usbduxsub_tmp->urbIn[i]->transfer_buffer);
2258 usbduxsub_tmp->urbIn[i]->transfer_buffer = NULL;
2259 usb_kill_urb(usbduxsub_tmp->urbIn[i]);
2260 usb_free_urb(usbduxsub_tmp->urbIn[i]);
2261 usbduxsub_tmp->urbIn[i] = NULL;
2262 }
2263 kfree(usbduxsub_tmp->urbIn);
2264 usbduxsub_tmp->urbIn = NULL;
2265 }
2266 if (usbduxsub_tmp->urbOut) {
2267 if (usbduxsub_tmp->ao_cmd_running) {
2268 usbduxsub_tmp->ao_cmd_running = 0;
2269 usbduxsub_unlink_OutURBs(usbduxsub_tmp);
2270 }
2271 for (i = 0; i < usbduxsub_tmp->numOfOutBuffers; i++) {
2272 if (usbduxsub_tmp->urbOut[i]->transfer_buffer) {
2273 kfree(usbduxsub_tmp->
2274 urbOut[i]->transfer_buffer);
2275 usbduxsub_tmp->urbOut[i]->transfer_buffer =
2276 NULL;
2277 }
2278 if (usbduxsub_tmp->urbOut[i]) {
2279 usb_kill_urb(usbduxsub_tmp->urbOut[i]);
2280 usb_free_urb(usbduxsub_tmp->urbOut[i]);
2281 usbduxsub_tmp->urbOut[i] = NULL;
2282 }
2283 }
2284 kfree(usbduxsub_tmp->urbOut);
2285 usbduxsub_tmp->urbOut = NULL;
2286 }
2287 if (usbduxsub_tmp->urbPwm) {
2288 if (usbduxsub_tmp->pwm_cmd_running) {
2289 usbduxsub_tmp->pwm_cmd_running = 0;
2290 usbduxsub_unlink_PwmURBs(usbduxsub_tmp);
2291 }
2292 kfree(usbduxsub_tmp->urbPwm->transfer_buffer);
2293 usbduxsub_tmp->urbPwm->transfer_buffer = NULL;
2294 usb_kill_urb(usbduxsub_tmp->urbPwm);
2295 usb_free_urb(usbduxsub_tmp->urbPwm);
2296 usbduxsub_tmp->urbPwm = NULL;
2297 }
2298 kfree(usbduxsub_tmp->inBuffer);
2299 usbduxsub_tmp->inBuffer = NULL;
2300 kfree(usbduxsub_tmp->insnBuffer);
2301 usbduxsub_tmp->insnBuffer = NULL;
2302 kfree(usbduxsub_tmp->outBuffer);
2303 usbduxsub_tmp->outBuffer = NULL;
2304 kfree(usbduxsub_tmp->dac_commands);
2305 usbduxsub_tmp->dac_commands = NULL;
2306 kfree(usbduxsub_tmp->dux_commands);
2307 usbduxsub_tmp->dux_commands = NULL;
2308 usbduxsub_tmp->ai_cmd_running = 0;
2309 usbduxsub_tmp->ao_cmd_running = 0;
2310 usbduxsub_tmp->pwm_cmd_running = 0;
2311 }
2312
2313 static void usbdux_firmware_request_complete_handler(const struct firmware *fw,
2314 void *context)
2315 {
2316 struct usbduxsub *usbduxsub_tmp = context;
2317 struct usb_interface *uinterf = usbduxsub_tmp->interface;
2318 int ret;
2319
2320 if (fw == NULL) {
2321 dev_err(&uinterf->dev,
2322 "Firmware complete handler without firmware!\n");
2323 return;
2324 }
2325
2326 /*
2327 * we need to upload the firmware here because fw will be
2328 * freed once we've left this function
2329 */
2330 ret = firmwareUpload(usbduxsub_tmp, fw->data, fw->size);
2331
2332 if (ret) {
2333 dev_err(&uinterf->dev,
2334 "Could not upload firmware (err=%d)\n", ret);
2335 goto out;
2336 }
2337 comedi_usb_auto_config(uinterf, &driver_usbduxsigma);
2338 out:
2339 release_firmware(fw);
2340 }
2341
2342 /* allocate memory for the urbs and initialise them */
2343 static int usbduxsigma_probe(struct usb_interface *uinterf,
2344 const struct usb_device_id *id)
2345 {
2346 struct usb_device *udev = interface_to_usbdev(uinterf);
2347 struct device *dev = &uinterf->dev;
2348 int i;
2349 int index;
2350 int ret;
2351
2352 dev_dbg(dev, "comedi_: usbdux_: "
2353 "finding a free structure for the usb-device\n");
2354
2355 down(&start_stop_sem);
2356 /* look for a free place in the usbdux array */
2357 index = -1;
2358 for (i = 0; i < NUMUSBDUX; i++) {
2359 if (!(usbduxsub[i].probed)) {
2360 index = i;
2361 break;
2362 }
2363 }
2364
2365 /* no more space */
2366 if (index == -1) {
2367 dev_err(dev, "Too many usbduxsigma-devices connected.\n");
2368 up(&start_stop_sem);
2369 return -EMFILE;
2370 }
2371 dev_dbg(dev, "comedi_: usbdux: "
2372 "usbduxsub[%d] is ready to connect to comedi.\n", index);
2373
2374 sema_init(&(usbduxsub[index].sem), 1);
2375 /* save a pointer to the usb device */
2376 usbduxsub[index].usbdev = udev;
2377
2378 /* save the interface itself */
2379 usbduxsub[index].interface = uinterf;
2380 /* get the interface number from the interface */
2381 usbduxsub[index].ifnum = uinterf->altsetting->desc.bInterfaceNumber;
2382 /* hand the private data over to the usb subsystem */
2383 /* will be needed for disconnect */
2384 usb_set_intfdata(uinterf, &(usbduxsub[index]));
2385
2386 dev_dbg(dev, "comedi_: usbdux: ifnum=%d\n", usbduxsub[index].ifnum);
2387
2388 /* test if it is high speed (USB 2.0) */
2389 usbduxsub[index].high_speed =
2390 (usbduxsub[index].usbdev->speed == USB_SPEED_HIGH);
2391
2392 /* create space for the commands of the DA converter */
2393 usbduxsub[index].dac_commands = kzalloc(NUMOUTCHANNELS, GFP_KERNEL);
2394 if (!usbduxsub[index].dac_commands) {
2395 dev_err(dev, "comedi_: usbduxsigma: "
2396 "error alloc space for dac commands\n");
2397 tidy_up(&(usbduxsub[index]));
2398 up(&start_stop_sem);
2399 return -ENOMEM;
2400 }
2401 /* create space for the commands going to the usb device */
2402 usbduxsub[index].dux_commands = kzalloc(SIZEOFDUXBUFFER, GFP_KERNEL);
2403 if (!usbduxsub[index].dux_commands) {
2404 dev_err(dev, "comedi_: usbduxsigma: "
2405 "error alloc space for dux commands\n");
2406 tidy_up(&(usbduxsub[index]));
2407 up(&start_stop_sem);
2408 return -ENOMEM;
2409 }
2410 /* create space for the in buffer and set it to zero */
2411 usbduxsub[index].inBuffer = kzalloc(SIZEINBUF, GFP_KERNEL);
2412 if (!(usbduxsub[index].inBuffer)) {
2413 dev_err(dev, "comedi_: usbduxsigma: "
2414 "could not alloc space for inBuffer\n");
2415 tidy_up(&(usbduxsub[index]));
2416 up(&start_stop_sem);
2417 return -ENOMEM;
2418 }
2419 /* create space of the instruction buffer */
2420 usbduxsub[index].insnBuffer = kzalloc(SIZEINSNBUF, GFP_KERNEL);
2421 if (!(usbduxsub[index].insnBuffer)) {
2422 dev_err(dev, "comedi_: usbduxsigma: "
2423 "could not alloc space for insnBuffer\n");
2424 tidy_up(&(usbduxsub[index]));
2425 up(&start_stop_sem);
2426 return -ENOMEM;
2427 }
2428 /* create space for the outbuffer */
2429 usbduxsub[index].outBuffer = kzalloc(SIZEOUTBUF, GFP_KERNEL);
2430 if (!(usbduxsub[index].outBuffer)) {
2431 dev_err(dev, "comedi_: usbduxsigma: "
2432 "could not alloc space for outBuffer\n");
2433 tidy_up(&(usbduxsub[index]));
2434 up(&start_stop_sem);
2435 return -ENOMEM;
2436 }
2437 /* setting to alternate setting 3: enabling iso ep and bulk ep. */
2438 i = usb_set_interface(usbduxsub[index].usbdev,
2439 usbduxsub[index].ifnum, 3);
2440 if (i < 0) {
2441 dev_err(dev, "comedi_: usbduxsigma%d: "
2442 "could not set alternate setting 3 in high speed.\n",
2443 index);
2444 tidy_up(&(usbduxsub[index]));
2445 up(&start_stop_sem);
2446 return -ENODEV;
2447 }
2448 if (usbduxsub[index].high_speed)
2449 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSHIGH;
2450 else
2451 usbduxsub[index].numOfInBuffers = NUMOFINBUFFERSFULL;
2452
2453 usbduxsub[index].urbIn =
2454 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfInBuffers,
2455 GFP_KERNEL);
2456 if (!(usbduxsub[index].urbIn)) {
2457 dev_err(dev, "comedi_: usbduxsigma: "
2458 "Could not alloc. urbIn array\n");
2459 tidy_up(&(usbduxsub[index]));
2460 up(&start_stop_sem);
2461 return -ENOMEM;
2462 }
2463 for (i = 0; i < usbduxsub[index].numOfInBuffers; i++) {
2464 /* one frame: 1ms */
2465 usbduxsub[index].urbIn[i] = usb_alloc_urb(1, GFP_KERNEL);
2466 if (usbduxsub[index].urbIn[i] == NULL) {
2467 dev_err(dev, "comedi_: usbduxsigma%d: "
2468 "Could not alloc. urb(%d)\n", index, i);
2469 tidy_up(&(usbduxsub[index]));
2470 up(&start_stop_sem);
2471 return -ENOMEM;
2472 }
2473 usbduxsub[index].urbIn[i]->dev = usbduxsub[index].usbdev;
2474 /* will be filled later with a pointer to the comedi-device */
2475 /* and ONLY then the urb should be submitted */
2476 usbduxsub[index].urbIn[i]->context = NULL;
2477 usbduxsub[index].urbIn[i]->pipe =
2478 usb_rcvisocpipe(usbduxsub[index].usbdev, ISOINEP);
2479 usbduxsub[index].urbIn[i]->transfer_flags = URB_ISO_ASAP;
2480 usbduxsub[index].urbIn[i]->transfer_buffer =
2481 kzalloc(SIZEINBUF, GFP_KERNEL);
2482 if (!(usbduxsub[index].urbIn[i]->transfer_buffer)) {
2483 dev_err(dev, "comedi_: usbduxsigma%d: "
2484 "could not alloc. transb.\n", index);
2485 tidy_up(&(usbduxsub[index]));
2486 up(&start_stop_sem);
2487 return -ENOMEM;
2488 }
2489 usbduxsub[index].urbIn[i]->complete = usbduxsub_ai_IsocIrq;
2490 usbduxsub[index].urbIn[i]->number_of_packets = 1;
2491 usbduxsub[index].urbIn[i]->transfer_buffer_length = SIZEINBUF;
2492 usbduxsub[index].urbIn[i]->iso_frame_desc[0].offset = 0;
2493 usbduxsub[index].urbIn[i]->iso_frame_desc[0].length =
2494 SIZEINBUF;
2495 }
2496
2497 /* out */
2498 if (usbduxsub[index].high_speed)
2499 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSHIGH;
2500 else
2501 usbduxsub[index].numOfOutBuffers = NUMOFOUTBUFFERSFULL;
2502
2503 usbduxsub[index].urbOut =
2504 kzalloc(sizeof(struct urb *) * usbduxsub[index].numOfOutBuffers,
2505 GFP_KERNEL);
2506 if (!(usbduxsub[index].urbOut)) {
2507 dev_err(dev, "comedi_: usbduxsigma: "
2508 "Could not alloc. urbOut array\n");
2509 tidy_up(&(usbduxsub[index]));
2510 up(&start_stop_sem);
2511 return -ENOMEM;
2512 }
2513 for (i = 0; i < usbduxsub[index].numOfOutBuffers; i++) {
2514 /* one frame: 1ms */
2515 usbduxsub[index].urbOut[i] = usb_alloc_urb(1, GFP_KERNEL);
2516 if (usbduxsub[index].urbOut[i] == NULL) {
2517 dev_err(dev, "comedi_: usbduxsigma%d: "
2518 "Could not alloc. urb(%d)\n", index, i);
2519 tidy_up(&(usbduxsub[index]));
2520 up(&start_stop_sem);
2521 return -ENOMEM;
2522 }
2523 usbduxsub[index].urbOut[i]->dev = usbduxsub[index].usbdev;
2524 /* will be filled later with a pointer to the comedi-device */
2525 /* and ONLY then the urb should be submitted */
2526 usbduxsub[index].urbOut[i]->context = NULL;
2527 usbduxsub[index].urbOut[i]->pipe =
2528 usb_sndisocpipe(usbduxsub[index].usbdev, ISOOUTEP);
2529 usbduxsub[index].urbOut[i]->transfer_flags = URB_ISO_ASAP;
2530 usbduxsub[index].urbOut[i]->transfer_buffer =
2531 kzalloc(SIZEOUTBUF, GFP_KERNEL);
2532 if (!(usbduxsub[index].urbOut[i]->transfer_buffer)) {
2533 dev_err(dev, "comedi_: usbduxsigma%d: "
2534 "could not alloc. transb.\n", index);
2535 tidy_up(&(usbduxsub[index]));
2536 up(&start_stop_sem);
2537 return -ENOMEM;
2538 }
2539 usbduxsub[index].urbOut[i]->complete = usbduxsub_ao_IsocIrq;
2540 usbduxsub[index].urbOut[i]->number_of_packets = 1;
2541 usbduxsub[index].urbOut[i]->transfer_buffer_length =
2542 SIZEOUTBUF;
2543 usbduxsub[index].urbOut[i]->iso_frame_desc[0].offset = 0;
2544 usbduxsub[index].urbOut[i]->iso_frame_desc[0].length =
2545 SIZEOUTBUF;
2546 if (usbduxsub[index].high_speed) {
2547 /* uframes */
2548 usbduxsub[index].urbOut[i]->interval = 8;
2549 } else {
2550 /* frames */
2551 usbduxsub[index].urbOut[i]->interval = 1;
2552 }
2553 }
2554
2555 /* pwm */
2556 if (usbduxsub[index].high_speed) {
2557 /* max bulk ep size in high speed */
2558 usbduxsub[index].sizePwmBuf = 512;
2559 usbduxsub[index].urbPwm = usb_alloc_urb(0, GFP_KERNEL);
2560 if (usbduxsub[index].urbPwm == NULL) {
2561 dev_err(dev, "comedi_: usbduxsigma%d: "
2562 "Could not alloc. pwm urb\n", index);
2563 tidy_up(&(usbduxsub[index]));
2564 up(&start_stop_sem);
2565 return -ENOMEM;
2566 }
2567 usbduxsub[index].urbPwm->transfer_buffer =
2568 kzalloc(usbduxsub[index].sizePwmBuf, GFP_KERNEL);
2569 if (!(usbduxsub[index].urbPwm->transfer_buffer)) {
2570 dev_err(dev, "comedi_: usbduxsigma%d: "
2571 "could not alloc. transb. for pwm\n", index);
2572 tidy_up(&(usbduxsub[index]));
2573 up(&start_stop_sem);
2574 return -ENOMEM;
2575 }
2576 } else {
2577 usbduxsub[index].urbPwm = NULL;
2578 usbduxsub[index].sizePwmBuf = 0;
2579 }
2580
2581 usbduxsub[index].ai_cmd_running = 0;
2582 usbduxsub[index].ao_cmd_running = 0;
2583 usbduxsub[index].pwm_cmd_running = 0;
2584
2585 /* we've reached the bottom of the function */
2586 usbduxsub[index].probed = 1;
2587 up(&start_stop_sem);
2588
2589 ret = request_firmware_nowait(THIS_MODULE,
2590 FW_ACTION_HOTPLUG,
2591 "usbduxsigma_firmware.bin",
2592 &udev->dev,
2593 GFP_KERNEL,
2594 usbduxsub + index,
2595 usbdux_firmware_request_complete_handler
2596 );
2597
2598 if (ret) {
2599 dev_err(dev, "Could not load firmware (err=%d)\n", ret);
2600 return ret;
2601 }
2602
2603 dev_info(dev, "comedi_: successfully initialised.\n");
2604 /* success */
2605 return 0;
2606 }
2607
2608 static void usbduxsigma_disconnect(struct usb_interface *intf)
2609 {
2610 struct usbduxsub *usbduxsub_tmp = usb_get_intfdata(intf);
2611 struct usb_device *udev = interface_to_usbdev(intf);
2612
2613 if (!usbduxsub_tmp) {
2614 dev_err(&intf->dev,
2615 "comedi_: disconnect called with null pointer.\n");
2616 return;
2617 }
2618 if (usbduxsub_tmp->usbdev != udev) {
2619 dev_err(&intf->dev, "comedi_: BUG! wrong ptr!\n");
2620 return;
2621 }
2622 if (usbduxsub_tmp->ai_cmd_running)
2623 /* we are still running a command */
2624 usbdux_ai_stop(usbduxsub_tmp, 1);
2625 if (usbduxsub_tmp->ao_cmd_running)
2626 /* we are still running a command */
2627 usbdux_ao_stop(usbduxsub_tmp, 1);
2628 comedi_usb_auto_unconfig(intf);
2629 down(&start_stop_sem);
2630 down(&usbduxsub_tmp->sem);
2631 tidy_up(usbduxsub_tmp);
2632 up(&usbduxsub_tmp->sem);
2633 up(&start_stop_sem);
2634 dev_info(&intf->dev, "comedi_: disconnected from the usb\n");
2635 }
2636
2637 /* is called when comedi-config is called */
2638 static int usbduxsigma_attach(struct comedi_device *dev,
2639 struct comedi_devconfig *it)
2640 {
2641 int ret;
2642 int index;
2643 int i;
2644 struct usbduxsub *udev;
2645
2646 int offset;
2647
2648 struct comedi_subdevice *s = NULL;
2649 dev->private = NULL;
2650
2651 down(&start_stop_sem);
2652 /* find a valid device which has been detected by the probe function of
2653 * the usb */
2654 index = -1;
2655 for (i = 0; i < NUMUSBDUX; i++) {
2656 if ((usbduxsub[i].probed) && (!usbduxsub[i].attached)) {
2657 index = i;
2658 break;
2659 }
2660 }
2661
2662 if (index < 0) {
2663 printk(KERN_ERR "comedi%d: usbduxsigma: error: attach failed,"
2664 "dev not connected to the usb bus.\n", dev->minor);
2665 up(&start_stop_sem);
2666 return -ENODEV;
2667 }
2668
2669 udev = &usbduxsub[index];
2670 down(&udev->sem);
2671 /* pointer back to the corresponding comedi device */
2672 udev->comedidev = dev;
2673
2674 /* trying to upload the firmware into the FX2 */
2675 if (comedi_aux_data(it->options, 0) &&
2676 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]) {
2677 firmwareUpload(udev, comedi_aux_data(it->options, 0),
2678 it->options[COMEDI_DEVCONF_AUX_DATA_LENGTH]);
2679 }
2680
2681 dev->board_name = BOARDNAME;
2682
2683 /* set number of subdevices */
2684 if (udev->high_speed) {
2685 /* with pwm */
2686 dev->n_subdevices = 4;
2687 } else {
2688 /* without pwm */
2689 dev->n_subdevices = 3;
2690 }
2691
2692 /* allocate space for the subdevices */
2693 ret = alloc_subdevices(dev, dev->n_subdevices);
2694 if (ret < 0) {
2695 dev_err(&udev->interface->dev,
2696 "comedi%d: no space for subdev\n", dev->minor);
2697 up(&udev->sem);
2698 up(&start_stop_sem);
2699 return ret;
2700 }
2701
2702 /* private structure is also simply the usb-structure */
2703 dev->private = udev;
2704
2705 /* the first subdevice is the A/D converter */
2706 s = dev->subdevices + SUBDEV_AD;
2707 /* the URBs get the comedi subdevice */
2708 /* which is responsible for reading */
2709 /* this is the subdevice which reads data */
2710 dev->read_subdev = s;
2711 /* the subdevice receives as private structure the */
2712 /* usb-structure */
2713 s->private = NULL;
2714 /* analog input */
2715 s->type = COMEDI_SUBD_AI;
2716 /* readable and ref is to ground, 32 bit wide data! */
2717 s->subdev_flags = SDF_READABLE | SDF_GROUND |
2718 SDF_CMD_READ | SDF_LSAMPL;
2719 /* 16 A/D channels */
2720 s->n_chan = NUMCHANNELS;
2721 /* length of the channellist */
2722 s->len_chanlist = NUMCHANNELS;
2723 /* callback functions */
2724 s->insn_read = usbdux_ai_insn_read;
2725 s->do_cmdtest = usbdux_ai_cmdtest;
2726 s->do_cmd = usbdux_ai_cmd;
2727 s->cancel = usbdux_ai_cancel;
2728 /* max value from the A/D converter (24bit) */
2729 s->maxdata = 0x00FFFFFF;
2730 /* range table to convert to physical units */
2731 s->range_table = (&range_usbdux_ai_range);
2732
2733 /* analog out */
2734 s = dev->subdevices + SUBDEV_DA;
2735 /* analog out */
2736 s->type = COMEDI_SUBD_AO;
2737 /* backward pointer */
2738 dev->write_subdev = s;
2739 /* the subdevice receives as private structure the */
2740 /* usb-structure */
2741 s->private = NULL;
2742 /* are writable */
2743 s->subdev_flags = SDF_WRITABLE | SDF_GROUND | SDF_CMD_WRITE;
2744 /* 4 channels */
2745 s->n_chan = 4;
2746 /* length of the channellist */
2747 s->len_chanlist = 4;
2748 /* 8 bit resolution */
2749 s->maxdata = 0x00ff;
2750 /* unipolar range */
2751 s->range_table = (&range_usbdux_ao_range);
2752 /* callback */
2753 s->do_cmdtest = usbdux_ao_cmdtest;
2754 s->do_cmd = usbdux_ao_cmd;
2755 s->cancel = usbdux_ao_cancel;
2756 s->insn_read = usbdux_ao_insn_read;
2757 s->insn_write = usbdux_ao_insn_write;
2758
2759 /* digital I/O */
2760 s = dev->subdevices + SUBDEV_DIO;
2761 s->type = COMEDI_SUBD_DIO;
2762 s->subdev_flags = SDF_READABLE | SDF_WRITABLE;
2763 /* 8 external and 16 internal channels */
2764 s->n_chan = 24;
2765 s->maxdata = 1;
2766 s->range_table = (&range_digital);
2767 s->insn_bits = usbdux_dio_insn_bits;
2768 s->insn_config = usbdux_dio_insn_config;
2769 /* we don't use it */
2770 s->private = NULL;
2771
2772 if (udev->high_speed) {
2773 /* timer / pwm */
2774 s = dev->subdevices + SUBDEV_PWM;
2775 s->type = COMEDI_SUBD_PWM;
2776 s->subdev_flags = SDF_WRITABLE | SDF_PWM_HBRIDGE;
2777 s->n_chan = 8;
2778 /* this defines the max duty cycle resolution */
2779 s->maxdata = udev->sizePwmBuf;
2780 s->insn_write = usbdux_pwm_write;
2781 s->insn_read = usbdux_pwm_read;
2782 s->insn_config = usbdux_pwm_config;
2783 usbdux_pwm_period(dev, s, PWM_DEFAULT_PERIOD);
2784 }
2785 /* finally decide that it's attached */
2786 udev->attached = 1;
2787
2788 up(&udev->sem);
2789
2790 up(&start_stop_sem);
2791
2792 offset = usbdux_getstatusinfo(dev, 0);
2793 if (offset < 0)
2794 dev_err(&udev->interface->dev,
2795 "Communication to USBDUXSIGMA failed!"
2796 "Check firmware and cabling.");
2797
2798 dev_info(&udev->interface->dev,
2799 "comedi%d: attached, ADC_zero = %x", dev->minor, offset);
2800
2801 return 0;
2802 }
2803
2804 static void usbduxsigma_detach(struct comedi_device *dev)
2805 {
2806 struct usbduxsub *usb = dev->private;
2807
2808 if (usb) {
2809 down(&usb->sem);
2810 dev->private = NULL;
2811 usb->attached = 0;
2812 usb->comedidev = NULL;
2813 up(&usb->sem);
2814 }
2815 }
2816
2817 /* main driver struct */
2818 static struct comedi_driver driver_usbduxsigma = {
2819 .driver_name = "usbduxsigma",
2820 .module = THIS_MODULE,
2821 .attach = usbduxsigma_attach,
2822 .detach = usbduxsigma_detach,
2823 };
2824
2825 /* Table with the USB-devices */
2826 static const struct usb_device_id usbduxsigma_table[] = {
2827 {USB_DEVICE(0x13d8, 0x0020)},
2828 {USB_DEVICE(0x13d8, 0x0021)},
2829 {USB_DEVICE(0x13d8, 0x0022)},
2830 {} /* Terminating entry */
2831 };
2832
2833 MODULE_DEVICE_TABLE(usb, usbduxsigma_table);
2834
2835 /* The usbduxsub-driver */
2836 static struct usb_driver usbduxsigma_driver = {
2837 .name = BOARDNAME,
2838 .probe = usbduxsigma_probe,
2839 .disconnect = usbduxsigma_disconnect,
2840 .id_table = usbduxsigma_table,
2841 };
2842
2843 /* Can't use the nice macro as I have also to initialise the USB */
2844 /* subsystem: */
2845 /* registering the usb-system _and_ the comedi-driver */
2846 static int __init init_usbduxsigma(void)
2847 {
2848 printk(KERN_INFO KBUILD_MODNAME ": "
2849 DRIVER_VERSION ":" DRIVER_DESC "\n");
2850 usb_register(&usbduxsigma_driver);
2851 comedi_driver_register(&driver_usbduxsigma);
2852 return 0;
2853 }
2854
2855 /* deregistering the comedi driver and the usb-subsystem */
2856 static void __exit exit_usbduxsigma(void)
2857 {
2858 comedi_driver_unregister(&driver_usbduxsigma);
2859 usb_deregister(&usbduxsigma_driver);
2860 }
2861
2862 module_init(init_usbduxsigma);
2863 module_exit(exit_usbduxsigma);
2864
2865 MODULE_AUTHOR(DRIVER_AUTHOR);
2866 MODULE_DESCRIPTION(DRIVER_DESC);
2867 MODULE_LICENSE("GPL");