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