blob: 895a0983900121ccd19542670da41ae490aaa9b9 [file] [log] [blame]
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -07001/**
2 * drivers/usb/class/usbtmc.c - USB Test & Measurment class driver
3 *
4 * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
5 * Copyright (C) 2008 Novell, Inc.
6 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (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 * The GNU General Public License is available at
19 * http://www.gnu.org/copyleft/gpl.html.
20 */
21
22#include <linux/init.h>
23#include <linux/module.h>
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +020024#include <linux/kernel.h>
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070025#include <linux/fs.h>
26#include <linux/uaccess.h>
27#include <linux/kref.h>
28#include <linux/mutex.h>
29#include <linux/usb.h>
30#include <linux/usb/tmc.h>
31
32
33#define USBTMC_MINOR_BASE 176
34
35/*
36 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
37 * large as wMaxPacketSize (which is usually 512 bytes).
38 */
39#define USBTMC_SIZE_IOBUFFER 2048
40
41/* Default USB timeout (in milliseconds) */
42#define USBTMC_TIMEOUT 10
43
44/*
45 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
46 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
47 * packet is never read.
48 */
49#define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
50
51static struct usb_device_id usbtmc_devices[] = {
52 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
53 { 0, } /* terminating entry */
54};
Greg Kroah-Hartman5413aa42008-12-03 16:33:09 -080055MODULE_DEVICE_TABLE(usb, usbtmc_devices);
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -070056
57/*
58 * This structure is the capabilities for the device
59 * See section 4.2.1.8 of the USBTMC specification for details.
60 */
61struct usbtmc_dev_capabilities {
62 __u8 interface_capabilities;
63 __u8 device_capabilities;
64 __u8 usb488_interface_capabilities;
65 __u8 usb488_device_capabilities;
66};
67
68/* This structure holds private data for each USBTMC device. One copy is
69 * allocated for each USBTMC device in the driver's probe function.
70 */
71struct usbtmc_device_data {
72 const struct usb_device_id *id;
73 struct usb_device *usb_dev;
74 struct usb_interface *intf;
75
76 unsigned int bulk_in;
77 unsigned int bulk_out;
78
79 u8 bTag;
80 u8 bTag_last_write; /* needed for abort */
81 u8 bTag_last_read; /* needed for abort */
82
83 /* attributes from the USB TMC spec for this device */
84 u8 TermChar;
85 bool TermCharEnabled;
86 bool auto_abort;
87
88 struct usbtmc_dev_capabilities capabilities;
89 struct kref kref;
90 struct mutex io_mutex; /* only one i/o function running at a time */
91};
92#define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
93
94/* Forward declarations */
95static struct usb_driver usbtmc_driver;
96
97static void usbtmc_delete(struct kref *kref)
98{
99 struct usbtmc_device_data *data = to_usbtmc_data(kref);
100
101 usb_put_dev(data->usb_dev);
102 kfree(data);
103}
104
105static int usbtmc_open(struct inode *inode, struct file *filp)
106{
107 struct usb_interface *intf;
108 struct usbtmc_device_data *data;
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700109 int retval = 0;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700110
111 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
112 if (!intf) {
113 printk(KERN_ERR KBUILD_MODNAME
114 ": can not find device for minor %d", iminor(inode));
Greg Kroah-Hartman5b109162009-03-10 20:42:55 -0700115 retval = -ENODEV;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700116 goto exit;
117 }
118
119 data = usb_get_intfdata(intf);
120 kref_get(&data->kref);
121
122 /* Store pointer in file structure's private data field */
123 filp->private_data = data;
124
125exit:
126 return retval;
127}
128
129static int usbtmc_release(struct inode *inode, struct file *file)
130{
131 struct usbtmc_device_data *data = file->private_data;
132
133 kref_put(&data->kref, usbtmc_delete);
134 return 0;
135}
136
137static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
138{
Chris Malleyb361a6e2008-10-25 22:07:32 +0100139 u8 *buffer;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700140 struct device *dev;
141 int rv;
142 int n;
143 int actual;
144 struct usb_host_interface *current_setting;
145 int max_size;
146
147 dev = &data->intf->dev;
148 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
149 if (!buffer)
150 return -ENOMEM;
151
152 rv = usb_control_msg(data->usb_dev,
153 usb_rcvctrlpipe(data->usb_dev, 0),
154 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
155 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
156 data->bTag_last_read, data->bulk_in,
157 buffer, 2, USBTMC_TIMEOUT);
158
159 if (rv < 0) {
160 dev_err(dev, "usb_control_msg returned %d\n", rv);
161 goto exit;
162 }
163
164 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
165
166 if (buffer[0] == USBTMC_STATUS_FAILED) {
167 rv = 0;
168 goto exit;
169 }
170
171 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
172 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
173 buffer[0]);
174 rv = -EPERM;
175 goto exit;
176 }
177
178 max_size = 0;
179 current_setting = data->intf->cur_altsetting;
180 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
181 if (current_setting->endpoint[n].desc.bEndpointAddress ==
182 data->bulk_in)
183 max_size = le16_to_cpu(current_setting->endpoint[n].
184 desc.wMaxPacketSize);
185
186 if (max_size == 0) {
187 dev_err(dev, "Couldn't get wMaxPacketSize\n");
188 rv = -EPERM;
189 goto exit;
190 }
191
192 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
193
194 n = 0;
195
196 do {
197 dev_dbg(dev, "Reading from bulk in EP\n");
198
199 rv = usb_bulk_msg(data->usb_dev,
200 usb_rcvbulkpipe(data->usb_dev,
201 data->bulk_in),
202 buffer, USBTMC_SIZE_IOBUFFER,
203 &actual, USBTMC_TIMEOUT);
204
205 n++;
206
207 if (rv < 0) {
208 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
209 goto exit;
210 }
211 } while ((actual == max_size) &&
212 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
213
214 if (actual == max_size) {
215 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
216 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
217 rv = -EPERM;
218 goto exit;
219 }
220
221 n = 0;
222
223usbtmc_abort_bulk_in_status:
224 rv = usb_control_msg(data->usb_dev,
225 usb_rcvctrlpipe(data->usb_dev, 0),
226 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
227 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
228 0, data->bulk_in, buffer, 0x08,
229 USBTMC_TIMEOUT);
230
231 if (rv < 0) {
232 dev_err(dev, "usb_control_msg returned %d\n", rv);
233 goto exit;
234 }
235
236 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
237
238 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
239 rv = 0;
240 goto exit;
241 }
242
243 if (buffer[0] != USBTMC_STATUS_PENDING) {
244 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
245 rv = -EPERM;
246 goto exit;
247 }
248
249 if (buffer[1] == 1)
250 do {
251 dev_dbg(dev, "Reading from bulk in EP\n");
252
253 rv = usb_bulk_msg(data->usb_dev,
254 usb_rcvbulkpipe(data->usb_dev,
255 data->bulk_in),
256 buffer, USBTMC_SIZE_IOBUFFER,
257 &actual, USBTMC_TIMEOUT);
258
259 n++;
260
261 if (rv < 0) {
262 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
263 goto exit;
264 }
265 } while ((actual = max_size) &&
266 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
267
268 if (actual == max_size) {
269 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
270 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
271 rv = -EPERM;
272 goto exit;
273 }
274
275 goto usbtmc_abort_bulk_in_status;
276
277exit:
278 kfree(buffer);
279 return rv;
280
281}
282
283static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
284{
285 struct device *dev;
286 u8 *buffer;
287 int rv;
288 int n;
289
290 dev = &data->intf->dev;
291
292 buffer = kmalloc(8, GFP_KERNEL);
293 if (!buffer)
294 return -ENOMEM;
295
296 rv = usb_control_msg(data->usb_dev,
297 usb_rcvctrlpipe(data->usb_dev, 0),
298 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
299 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
300 data->bTag_last_write, data->bulk_out,
301 buffer, 2, USBTMC_TIMEOUT);
302
303 if (rv < 0) {
304 dev_err(dev, "usb_control_msg returned %d\n", rv);
305 goto exit;
306 }
307
308 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
309
310 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
311 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
312 buffer[0]);
313 rv = -EPERM;
314 goto exit;
315 }
316
317 n = 0;
318
319usbtmc_abort_bulk_out_check_status:
320 rv = usb_control_msg(data->usb_dev,
321 usb_rcvctrlpipe(data->usb_dev, 0),
322 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
323 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
324 0, data->bulk_out, buffer, 0x08,
325 USBTMC_TIMEOUT);
326 n++;
327 if (rv < 0) {
328 dev_err(dev, "usb_control_msg returned %d\n", rv);
329 goto exit;
330 }
331
332 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
333
334 if (buffer[0] == USBTMC_STATUS_SUCCESS)
335 goto usbtmc_abort_bulk_out_clear_halt;
336
337 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
338 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
339 goto usbtmc_abort_bulk_out_check_status;
340
341 rv = -EPERM;
342 goto exit;
343
344usbtmc_abort_bulk_out_clear_halt:
345 rv = usb_control_msg(data->usb_dev,
346 usb_sndctrlpipe(data->usb_dev, 0),
347 USB_REQ_CLEAR_FEATURE,
348 USB_DIR_OUT | USB_TYPE_STANDARD |
349 USB_RECIP_ENDPOINT,
350 USB_ENDPOINT_HALT, data->bulk_out, buffer,
351 0, USBTMC_TIMEOUT);
352
353 if (rv < 0) {
354 dev_err(dev, "usb_control_msg returned %d\n", rv);
355 goto exit;
356 }
357 rv = 0;
358
359exit:
360 kfree(buffer);
361 return rv;
362}
363
364static ssize_t usbtmc_read(struct file *filp, char __user *buf,
365 size_t count, loff_t *f_pos)
366{
367 struct usbtmc_device_data *data;
368 struct device *dev;
369 unsigned long int n_characters;
370 u8 *buffer;
371 int actual;
372 int done;
373 int remaining;
374 int retval;
375 int this_part;
376
377 /* Get pointer to private data structure */
378 data = filp->private_data;
379 dev = &data->intf->dev;
380
381 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
382 if (!buffer)
383 return -ENOMEM;
384
385 mutex_lock(&data->io_mutex);
386
387 remaining = count;
388 done = 0;
389
390 while (remaining > 0) {
391 if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
392 this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
393 else
394 this_part = remaining;
395
396 /* Setup IO buffer for DEV_DEP_MSG_IN message
397 * Refer to class specs for details
398 */
399 buffer[0] = 2;
400 buffer[1] = data->bTag;
401 buffer[2] = ~(data->bTag);
402 buffer[3] = 0; /* Reserved */
403 buffer[4] = (this_part - 12 - 3) & 255;
404 buffer[5] = ((this_part - 12 - 3) >> 8) & 255;
405 buffer[6] = ((this_part - 12 - 3) >> 16) & 255;
406 buffer[7] = ((this_part - 12 - 3) >> 24) & 255;
407 buffer[8] = data->TermCharEnabled * 2;
408 /* Use term character? */
409 buffer[9] = data->TermChar;
410 buffer[10] = 0; /* Reserved */
411 buffer[11] = 0; /* Reserved */
412
413 /* Send bulk URB */
414 retval = usb_bulk_msg(data->usb_dev,
415 usb_sndbulkpipe(data->usb_dev,
416 data->bulk_out),
417 buffer, 12, &actual, USBTMC_TIMEOUT);
418
419 /* Store bTag (in case we need to abort) */
420 data->bTag_last_write = data->bTag;
421
422 /* Increment bTag -- and increment again if zero */
423 data->bTag++;
424 if (!data->bTag)
425 (data->bTag)++;
426
427 if (retval < 0) {
428 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
429 if (data->auto_abort)
430 usbtmc_ioctl_abort_bulk_out(data);
431 goto exit;
432 }
433
434 /* Send bulk URB */
435 retval = usb_bulk_msg(data->usb_dev,
436 usb_rcvbulkpipe(data->usb_dev,
437 data->bulk_in),
438 buffer, USBTMC_SIZE_IOBUFFER, &actual,
439 USBTMC_TIMEOUT);
440
441 /* Store bTag (in case we need to abort) */
442 data->bTag_last_read = data->bTag;
443
444 if (retval < 0) {
445 dev_err(dev, "Unable to read data, error %d\n", retval);
446 if (data->auto_abort)
447 usbtmc_ioctl_abort_bulk_in(data);
448 goto exit;
449 }
450
451 /* How many characters did the instrument send? */
452 n_characters = buffer[4] +
453 (buffer[5] << 8) +
454 (buffer[6] << 16) +
455 (buffer[7] << 24);
456
457 /* Copy buffer to user space */
458 if (copy_to_user(buf + done, &buffer[12], n_characters)) {
459 /* There must have been an addressing problem */
460 retval = -EFAULT;
461 goto exit;
462 }
463
464 done += n_characters;
465 if (n_characters < USBTMC_SIZE_IOBUFFER)
466 remaining = 0;
467 }
468
469 /* Update file position value */
470 *f_pos = *f_pos + done;
471 retval = done;
472
473exit:
474 mutex_unlock(&data->io_mutex);
475 kfree(buffer);
476 return retval;
477}
478
479static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
480 size_t count, loff_t *f_pos)
481{
482 struct usbtmc_device_data *data;
483 u8 *buffer;
484 int retval;
485 int actual;
486 unsigned long int n_bytes;
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700487 int remaining;
488 int done;
489 int this_part;
490
491 data = filp->private_data;
492
493 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
494 if (!buffer)
495 return -ENOMEM;
496
497 mutex_lock(&data->io_mutex);
498
499 remaining = count;
500 done = 0;
501
502 while (remaining > 0) {
503 if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
504 this_part = USBTMC_SIZE_IOBUFFER - 12;
505 buffer[8] = 0;
506 } else {
507 this_part = remaining;
508 buffer[8] = 1;
509 }
510
511 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
512 buffer[0] = 1;
513 buffer[1] = data->bTag;
514 buffer[2] = ~(data->bTag);
515 buffer[3] = 0; /* Reserved */
516 buffer[4] = this_part & 255;
517 buffer[5] = (this_part >> 8) & 255;
518 buffer[6] = (this_part >> 16) & 255;
519 buffer[7] = (this_part >> 24) & 255;
520 /* buffer[8] is set above... */
521 buffer[9] = 0; /* Reserved */
522 buffer[10] = 0; /* Reserved */
523 buffer[11] = 0; /* Reserved */
524
525 if (copy_from_user(&buffer[12], buf + done, this_part)) {
526 retval = -EFAULT;
527 goto exit;
528 }
529
Ilpo Järvinen857cc4d2008-10-30 13:56:47 +0200530 n_bytes = roundup(12 + this_part, 4);
531 memset(buffer + 12 + this_part, 0, n_bytes - (12 + this_part));
Greg Kroah-Hartman5b775f62008-08-26 16:22:06 -0700532
533 retval = usb_bulk_msg(data->usb_dev,
534 usb_sndbulkpipe(data->usb_dev,
535 data->bulk_out),
536 buffer, n_bytes, &actual, USBTMC_TIMEOUT);
537
538 data->bTag_last_write = data->bTag;
539 data->bTag++;
540
541 if (!data->bTag)
542 data->bTag++;
543
544 if (retval < 0) {
545 dev_err(&data->intf->dev,
546 "Unable to send data, error %d\n", retval);
547 if (data->auto_abort)
548 usbtmc_ioctl_abort_bulk_out(data);
549 goto exit;
550 }
551
552 remaining -= this_part;
553 done += this_part;
554 }
555
556 retval = count;
557exit:
558 mutex_unlock(&data->io_mutex);
559 kfree(buffer);
560 return retval;
561}
562
563static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
564{
565 struct usb_host_interface *current_setting;
566 struct usb_endpoint_descriptor *desc;
567 struct device *dev;
568 u8 *buffer;
569 int rv;
570 int n;
571 int actual;
572 int max_size;
573
574 dev = &data->intf->dev;
575
576 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
577
578 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
579 if (!buffer)
580 return -ENOMEM;
581
582 rv = usb_control_msg(data->usb_dev,
583 usb_rcvctrlpipe(data->usb_dev, 0),
584 USBTMC_REQUEST_INITIATE_CLEAR,
585 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
586 0, 0, buffer, 1, USBTMC_TIMEOUT);
587 if (rv < 0) {
588 dev_err(dev, "usb_control_msg returned %d\n", rv);
589 goto exit;
590 }
591
592 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
593
594 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
595 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
596 rv = -EPERM;
597 goto exit;
598 }
599
600 max_size = 0;
601 current_setting = data->intf->cur_altsetting;
602 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
603 desc = &current_setting->endpoint[n].desc;
604 if (desc->bEndpointAddress == data->bulk_in)
605 max_size = le16_to_cpu(desc->wMaxPacketSize);
606 }
607
608 if (max_size == 0) {
609 dev_err(dev, "Couldn't get wMaxPacketSize\n");
610 rv = -EPERM;
611 goto exit;
612 }
613
614 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
615
616 n = 0;
617
618usbtmc_clear_check_status:
619
620 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
621
622 rv = usb_control_msg(data->usb_dev,
623 usb_rcvctrlpipe(data->usb_dev, 0),
624 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
625 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
626 0, 0, buffer, 2, USBTMC_TIMEOUT);
627 if (rv < 0) {
628 dev_err(dev, "usb_control_msg returned %d\n", rv);
629 goto exit;
630 }
631
632 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
633
634 if (buffer[0] == USBTMC_STATUS_SUCCESS)
635 goto usbtmc_clear_bulk_out_halt;
636
637 if (buffer[0] != USBTMC_STATUS_PENDING) {
638 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
639 rv = -EPERM;
640 goto exit;
641 }
642
643 if (buffer[1] == 1)
644 do {
645 dev_dbg(dev, "Reading from bulk in EP\n");
646
647 rv = usb_bulk_msg(data->usb_dev,
648 usb_rcvbulkpipe(data->usb_dev,
649 data->bulk_in),
650 buffer, USBTMC_SIZE_IOBUFFER,
651 &actual, USBTMC_TIMEOUT);
652 n++;
653
654 if (rv < 0) {
655 dev_err(dev, "usb_control_msg returned %d\n",
656 rv);
657 goto exit;
658 }
659 } while ((actual == max_size) &&
660 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
661
662 if (actual == max_size) {
663 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
664 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
665 rv = -EPERM;
666 goto exit;
667 }
668
669 goto usbtmc_clear_check_status;
670
671usbtmc_clear_bulk_out_halt:
672
673 rv = usb_control_msg(data->usb_dev,
674 usb_sndctrlpipe(data->usb_dev, 0),
675 USB_REQ_CLEAR_FEATURE,
676 USB_DIR_OUT | USB_TYPE_STANDARD |
677 USB_RECIP_ENDPOINT,
678 USB_ENDPOINT_HALT,
679 data->bulk_out, buffer, 0,
680 USBTMC_TIMEOUT);
681 if (rv < 0) {
682 dev_err(dev, "usb_control_msg returned %d\n", rv);
683 goto exit;
684 }
685 rv = 0;
686
687exit:
688 kfree(buffer);
689 return rv;
690}
691
692static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
693{
694 u8 *buffer;
695 int rv;
696
697 buffer = kmalloc(2, GFP_KERNEL);
698 if (!buffer)
699 return -ENOMEM;
700
701 rv = usb_control_msg(data->usb_dev,
702 usb_sndctrlpipe(data->usb_dev, 0),
703 USB_REQ_CLEAR_FEATURE,
704 USB_DIR_OUT | USB_TYPE_STANDARD |
705 USB_RECIP_ENDPOINT,
706 USB_ENDPOINT_HALT, data->bulk_out,
707 buffer, 0, USBTMC_TIMEOUT);
708
709 if (rv < 0) {
710 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
711 rv);
712 goto exit;
713 }
714 rv = 0;
715
716exit:
717 kfree(buffer);
718 return rv;
719}
720
721static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
722{
723 u8 *buffer;
724 int rv;
725
726 buffer = kmalloc(2, GFP_KERNEL);
727 if (!buffer)
728 return -ENOMEM;
729
730 rv = usb_control_msg(data->usb_dev, usb_sndctrlpipe(data->usb_dev, 0),
731 USB_REQ_CLEAR_FEATURE,
732 USB_DIR_OUT | USB_TYPE_STANDARD |
733 USB_RECIP_ENDPOINT,
734 USB_ENDPOINT_HALT, data->bulk_in, buffer, 0,
735 USBTMC_TIMEOUT);
736
737 if (rv < 0) {
738 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
739 rv);
740 goto exit;
741 }
742 rv = 0;
743
744exit:
745 kfree(buffer);
746 return rv;
747}
748
749static int get_capabilities(struct usbtmc_device_data *data)
750{
751 struct device *dev = &data->usb_dev->dev;
752 char *buffer;
753 int rv;
754
755 buffer = kmalloc(0x18, GFP_KERNEL);
756 if (!buffer)
757 return -ENOMEM;
758
759 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
760 USBTMC_REQUEST_GET_CAPABILITIES,
761 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
762 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
763 if (rv < 0) {
764 dev_err(dev, "usb_control_msg returned %d\n", rv);
765 return rv;
766 }
767
768 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
769 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
770 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
771 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
772 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
773 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
774 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
775 return -EPERM;
776 }
777
778 data->capabilities.interface_capabilities = buffer[4];
779 data->capabilities.device_capabilities = buffer[5];
780 data->capabilities.usb488_interface_capabilities = buffer[14];
781 data->capabilities.usb488_device_capabilities = buffer[15];
782
783 kfree(buffer);
784 return 0;
785}
786
787#define capability_attribute(name) \
788static ssize_t show_##name(struct device *dev, \
789 struct device_attribute *attr, char *buf) \
790{ \
791 struct usb_interface *intf = to_usb_interface(dev); \
792 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
793 \
794 return sprintf(buf, "%d\n", data->capabilities.name); \
795} \
796static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
797
798capability_attribute(interface_capabilities);
799capability_attribute(device_capabilities);
800capability_attribute(usb488_interface_capabilities);
801capability_attribute(usb488_device_capabilities);
802
803static struct attribute *capability_attrs[] = {
804 &dev_attr_interface_capabilities.attr,
805 &dev_attr_device_capabilities.attr,
806 &dev_attr_usb488_interface_capabilities.attr,
807 &dev_attr_usb488_device_capabilities.attr,
808 NULL,
809};
810
811static struct attribute_group capability_attr_grp = {
812 .attrs = capability_attrs,
813};
814
815static ssize_t show_TermChar(struct device *dev,
816 struct device_attribute *attr, char *buf)
817{
818 struct usb_interface *intf = to_usb_interface(dev);
819 struct usbtmc_device_data *data = usb_get_intfdata(intf);
820
821 return sprintf(buf, "%c\n", data->TermChar);
822}
823
824static ssize_t store_TermChar(struct device *dev,
825 struct device_attribute *attr,
826 const char *buf, size_t count)
827{
828 struct usb_interface *intf = to_usb_interface(dev);
829 struct usbtmc_device_data *data = usb_get_intfdata(intf);
830
831 if (count < 1)
832 return -EINVAL;
833 data->TermChar = buf[0];
834 return count;
835}
836static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
837
838#define data_attribute(name) \
839static ssize_t show_##name(struct device *dev, \
840 struct device_attribute *attr, char *buf) \
841{ \
842 struct usb_interface *intf = to_usb_interface(dev); \
843 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
844 \
845 return sprintf(buf, "%d\n", data->name); \
846} \
847static ssize_t store_##name(struct device *dev, \
848 struct device_attribute *attr, \
849 const char *buf, size_t count) \
850{ \
851 struct usb_interface *intf = to_usb_interface(dev); \
852 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
853 ssize_t result; \
854 unsigned val; \
855 \
856 result = sscanf(buf, "%u\n", &val); \
857 if (result != 1) \
858 result = -EINVAL; \
859 data->name = val; \
860 if (result < 0) \
861 return result; \
862 else \
863 return count; \
864} \
865static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
866
867data_attribute(TermCharEnabled);
868data_attribute(auto_abort);
869
870static struct attribute *data_attrs[] = {
871 &dev_attr_TermChar.attr,
872 &dev_attr_TermCharEnabled.attr,
873 &dev_attr_auto_abort.attr,
874 NULL,
875};
876
877static struct attribute_group data_attr_grp = {
878 .attrs = data_attrs,
879};
880
881static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
882{
883 struct device *dev;
884 u8 *buffer;
885 int rv;
886
887 dev = &data->intf->dev;
888
889 buffer = kmalloc(2, GFP_KERNEL);
890 if (!buffer)
891 return -ENOMEM;
892
893 rv = usb_control_msg(data->usb_dev,
894 usb_rcvctrlpipe(data->usb_dev, 0),
895 USBTMC_REQUEST_INDICATOR_PULSE,
896 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
897 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
898
899 if (rv < 0) {
900 dev_err(dev, "usb_control_msg returned %d\n", rv);
901 goto exit;
902 }
903
904 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
905
906 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
907 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
908 rv = -EPERM;
909 goto exit;
910 }
911 rv = 0;
912
913exit:
914 kfree(buffer);
915 return rv;
916}
917
918static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
919{
920 struct usbtmc_device_data *data;
921 int retval = -EBADRQC;
922
923 data = file->private_data;
924 mutex_lock(&data->io_mutex);
925
926 switch (cmd) {
927 case USBTMC_IOCTL_CLEAR_OUT_HALT:
928 retval = usbtmc_ioctl_clear_out_halt(data);
929
930 case USBTMC_IOCTL_CLEAR_IN_HALT:
931 retval = usbtmc_ioctl_clear_in_halt(data);
932
933 case USBTMC_IOCTL_INDICATOR_PULSE:
934 retval = usbtmc_ioctl_indicator_pulse(data);
935
936 case USBTMC_IOCTL_CLEAR:
937 retval = usbtmc_ioctl_clear(data);
938
939 case USBTMC_IOCTL_ABORT_BULK_OUT:
940 retval = usbtmc_ioctl_abort_bulk_out(data);
941
942 case USBTMC_IOCTL_ABORT_BULK_IN:
943 retval = usbtmc_ioctl_abort_bulk_in(data);
944 }
945
946 mutex_unlock(&data->io_mutex);
947 return retval;
948}
949
950static struct file_operations fops = {
951 .owner = THIS_MODULE,
952 .read = usbtmc_read,
953 .write = usbtmc_write,
954 .open = usbtmc_open,
955 .release = usbtmc_release,
956 .unlocked_ioctl = usbtmc_ioctl,
957};
958
959static struct usb_class_driver usbtmc_class = {
960 .name = "usbtmc%d",
961 .fops = &fops,
962 .minor_base = USBTMC_MINOR_BASE,
963};
964
965
966static int usbtmc_probe(struct usb_interface *intf,
967 const struct usb_device_id *id)
968{
969 struct usbtmc_device_data *data;
970 struct usb_host_interface *iface_desc;
971 struct usb_endpoint_descriptor *endpoint;
972 int n;
973 int retcode;
974
975 dev_dbg(&intf->dev, "%s called\n", __func__);
976
977 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
978 if (!data) {
979 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
980 return -ENOMEM;
981 }
982
983 data->intf = intf;
984 data->id = id;
985 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
986 usb_set_intfdata(intf, data);
987 kref_init(&data->kref);
988 mutex_init(&data->io_mutex);
989
990 /* Initialize USBTMC bTag and other fields */
991 data->bTag = 1;
992 data->TermCharEnabled = 0;
993 data->TermChar = '\n';
994
995 /* USBTMC devices have only one setting, so use that */
996 iface_desc = data->intf->cur_altsetting;
997
998 /* Find bulk in endpoint */
999 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1000 endpoint = &iface_desc->endpoint[n].desc;
1001
1002 if (usb_endpoint_is_bulk_in(endpoint)) {
1003 data->bulk_in = endpoint->bEndpointAddress;
1004 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1005 data->bulk_in);
1006 break;
1007 }
1008 }
1009
1010 /* Find bulk out endpoint */
1011 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1012 endpoint = &iface_desc->endpoint[n].desc;
1013
1014 if (usb_endpoint_is_bulk_out(endpoint)) {
1015 data->bulk_out = endpoint->bEndpointAddress;
1016 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1017 data->bulk_out);
1018 break;
1019 }
1020 }
1021
1022 retcode = get_capabilities(data);
1023 if (retcode)
1024 dev_err(&intf->dev, "can't read capabilities\n");
1025 else
1026 retcode = sysfs_create_group(&intf->dev.kobj,
1027 &capability_attr_grp);
1028
1029 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1030
1031 retcode = usb_register_dev(intf, &usbtmc_class);
1032 if (retcode) {
1033 dev_err(&intf->dev, "Not able to get a minor"
1034 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1035 retcode);
1036 goto error_register;
1037 }
1038 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1039
1040 return 0;
1041
1042error_register:
1043 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1044 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1045 kref_put(&data->kref, usbtmc_delete);
1046 return retcode;
1047}
1048
1049static void usbtmc_disconnect(struct usb_interface *intf)
1050{
1051 struct usbtmc_device_data *data;
1052
1053 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1054
1055 data = usb_get_intfdata(intf);
1056 usb_deregister_dev(intf, &usbtmc_class);
1057 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1058 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1059 kref_put(&data->kref, usbtmc_delete);
1060}
1061
1062static struct usb_driver usbtmc_driver = {
1063 .name = "usbtmc",
1064 .id_table = usbtmc_devices,
1065 .probe = usbtmc_probe,
1066 .disconnect = usbtmc_disconnect
1067};
1068
1069static int __init usbtmc_init(void)
1070{
1071 int retcode;
1072
1073 retcode = usb_register(&usbtmc_driver);
1074 if (retcode)
1075 printk(KERN_ERR KBUILD_MODNAME": Unable to register driver\n");
1076 return retcode;
1077}
1078module_init(usbtmc_init);
1079
1080static void __exit usbtmc_exit(void)
1081{
1082 usb_deregister(&usbtmc_driver);
1083}
1084module_exit(usbtmc_exit);
1085
1086MODULE_LICENSE("GPL");