2 * drivers/usb/class/usbtmc.c - USB Test & Measurement class driver
4 * Copyright (C) 2007 Stefan Kopp, Gechingen, Germany
5 * Copyright (C) 2008 Novell, Inc.
6 * Copyright (C) 2008 Greg Kroah-Hartman <gregkh@suse.de>
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.
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.
18 * The GNU General Public License is available at
19 * http://www.gnu.org/copyleft/gpl.html.
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/kernel.h>
26 #include <linux/uaccess.h>
27 #include <linux/kref.h>
28 #include <linux/slab.h>
29 #include <linux/mutex.h>
30 #include <linux/usb.h>
31 #include <linux/usb/tmc.h>
35 #define USBTMC_HEADER_SIZE 12
36 #define USBTMC_MINOR_BASE 176
39 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
40 * large as wMaxPacketSize (which is usually 512 bytes).
42 #define USBTMC_SIZE_IOBUFFER 2048
44 /* Default USB timeout (in milliseconds) */
45 #define USBTMC_TIMEOUT 5000
48 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
49 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
50 * packet is never read.
52 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
54 static const struct usb_device_id usbtmc_devices
[] = {
55 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 0), },
56 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 1), },
57 { 0, } /* terminating entry */
59 MODULE_DEVICE_TABLE(usb
, usbtmc_devices
);
62 * This structure is the capabilities for the device
63 * See section 4.2.1.8 of the USBTMC specification,
64 * and section 4.2.2 of the USBTMC usb488 subclass
65 * specification for details.
67 struct usbtmc_dev_capabilities
{
68 __u8 interface_capabilities
;
69 __u8 device_capabilities
;
70 __u8 usb488_interface_capabilities
;
71 __u8 usb488_device_capabilities
;
74 /* This structure holds private data for each USBTMC device. One copy is
75 * allocated for each USBTMC device in the driver's probe function.
77 struct usbtmc_device_data
{
78 const struct usb_device_id
*id
;
79 struct usb_device
*usb_dev
;
80 struct usb_interface
*intf
;
83 unsigned int bulk_out
;
86 u8 bTag_last_write
; /* needed for abort */
87 u8 bTag_last_read
; /* needed for abort */
91 /* attributes from the USB TMC spec for this device */
96 bool zombie
; /* fd of disconnected device */
98 struct usbtmc_dev_capabilities capabilities
;
100 struct mutex io_mutex
; /* only one i/o function running at a time */
102 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
104 struct usbtmc_ID_rigol_quirk
{
109 static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk
[] = {
114 /* Forward declarations */
115 static struct usb_driver usbtmc_driver
;
117 static void usbtmc_delete(struct kref
*kref
)
119 struct usbtmc_device_data
*data
= to_usbtmc_data(kref
);
121 usb_put_dev(data
->usb_dev
);
125 static int usbtmc_open(struct inode
*inode
, struct file
*filp
)
127 struct usb_interface
*intf
;
128 struct usbtmc_device_data
*data
;
131 intf
= usb_find_interface(&usbtmc_driver
, iminor(inode
));
133 printk(KERN_ERR KBUILD_MODNAME
134 ": can not find device for minor %d", iminor(inode
));
139 data
= usb_get_intfdata(intf
);
140 kref_get(&data
->kref
);
142 /* Store pointer in file structure's private data field */
143 filp
->private_data
= data
;
149 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
151 struct usbtmc_device_data
*data
= file
->private_data
;
153 kref_put(&data
->kref
, usbtmc_delete
);
157 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
164 struct usb_host_interface
*current_setting
;
167 dev
= &data
->intf
->dev
;
168 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
172 rv
= usb_control_msg(data
->usb_dev
,
173 usb_rcvctrlpipe(data
->usb_dev
, 0),
174 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
175 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
176 data
->bTag_last_read
, data
->bulk_in
,
177 buffer
, 2, USBTMC_TIMEOUT
);
180 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
184 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
186 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
191 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
192 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
199 current_setting
= data
->intf
->cur_altsetting
;
200 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++)
201 if (current_setting
->endpoint
[n
].desc
.bEndpointAddress
==
203 max_size
= usb_endpoint_maxp(¤t_setting
->endpoint
[n
].desc
);
206 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
211 dev_dbg(&data
->intf
->dev
, "wMaxPacketSize is %d\n", max_size
);
216 dev_dbg(dev
, "Reading from bulk in EP\n");
218 rv
= usb_bulk_msg(data
->usb_dev
,
219 usb_rcvbulkpipe(data
->usb_dev
,
221 buffer
, USBTMC_SIZE_IOBUFFER
,
222 &actual
, USBTMC_TIMEOUT
);
227 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
230 } while ((actual
== max_size
) &&
231 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
233 if (actual
== max_size
) {
234 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
235 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
242 usbtmc_abort_bulk_in_status
:
243 rv
= usb_control_msg(data
->usb_dev
,
244 usb_rcvctrlpipe(data
->usb_dev
, 0),
245 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
246 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
247 0, data
->bulk_in
, buffer
, 0x08,
251 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
255 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
257 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
262 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
263 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
270 dev_dbg(dev
, "Reading from bulk in EP\n");
272 rv
= usb_bulk_msg(data
->usb_dev
,
273 usb_rcvbulkpipe(data
->usb_dev
,
275 buffer
, USBTMC_SIZE_IOBUFFER
,
276 &actual
, USBTMC_TIMEOUT
);
281 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
284 } while ((actual
== max_size
) &&
285 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
287 if (actual
== max_size
) {
288 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
289 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
294 goto usbtmc_abort_bulk_in_status
;
302 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
309 dev
= &data
->intf
->dev
;
311 buffer
= kmalloc(8, GFP_KERNEL
);
315 rv
= usb_control_msg(data
->usb_dev
,
316 usb_rcvctrlpipe(data
->usb_dev
, 0),
317 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
318 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
319 data
->bTag_last_write
, data
->bulk_out
,
320 buffer
, 2, USBTMC_TIMEOUT
);
323 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
327 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
329 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
330 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
338 usbtmc_abort_bulk_out_check_status
:
339 rv
= usb_control_msg(data
->usb_dev
,
340 usb_rcvctrlpipe(data
->usb_dev
, 0),
341 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
342 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
343 0, data
->bulk_out
, buffer
, 0x08,
347 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
351 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
353 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
354 goto usbtmc_abort_bulk_out_clear_halt
;
356 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
357 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
358 goto usbtmc_abort_bulk_out_check_status
;
363 usbtmc_abort_bulk_out_clear_halt
:
364 rv
= usb_clear_halt(data
->usb_dev
,
365 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
368 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
379 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
380 * @transfer_size: number of bytes to request from the device.
382 * See the USBTMC specification, Table 4.
384 * Also updates bTag_last_write.
386 static int send_request_dev_dep_msg_in(struct usbtmc_device_data
*data
, size_t transfer_size
)
389 u8 buffer
[USBTMC_HEADER_SIZE
];
392 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
393 * Refer to class specs for details
396 buffer
[1] = data
->bTag
;
397 buffer
[2] = ~(data
->bTag
);
398 buffer
[3] = 0; /* Reserved */
399 buffer
[4] = (transfer_size
) & 255;
400 buffer
[5] = ((transfer_size
) >> 8) & 255;
401 buffer
[6] = ((transfer_size
) >> 16) & 255;
402 buffer
[7] = ((transfer_size
) >> 24) & 255;
403 buffer
[8] = data
->TermCharEnabled
* 2;
404 /* Use term character? */
405 buffer
[9] = data
->TermChar
;
406 buffer
[10] = 0; /* Reserved */
407 buffer
[11] = 0; /* Reserved */
410 retval
= usb_bulk_msg(data
->usb_dev
,
411 usb_sndbulkpipe(data
->usb_dev
,
413 buffer
, USBTMC_HEADER_SIZE
, &actual
, USBTMC_TIMEOUT
);
415 /* Store bTag (in case we need to abort) */
416 data
->bTag_last_write
= data
->bTag
;
418 /* Increment bTag -- and increment again if zero */
424 dev_err(&data
->intf
->dev
, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval
);
431 static ssize_t
usbtmc_read(struct file
*filp
, char __user
*buf
,
432 size_t count
, loff_t
*f_pos
)
434 struct usbtmc_device_data
*data
;
444 /* Get pointer to private data structure */
445 data
= filp
->private_data
;
446 dev
= &data
->intf
->dev
;
448 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
452 mutex_lock(&data
->io_mutex
);
458 if (data
->rigol_quirk
) {
459 dev_dbg(dev
, "usb_bulk_msg_in: count(%zu)\n", count
);
461 retval
= send_request_dev_dep_msg_in(data
, count
);
464 if (data
->auto_abort
)
465 usbtmc_ioctl_abort_bulk_out(data
);
470 /* Loop until we have fetched everything we requested */
472 this_part
= remaining
;
475 while (remaining
> 0) {
476 if (!(data
->rigol_quirk
)) {
477 dev_dbg(dev
, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining
, count
);
479 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3)
480 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3;
482 this_part
= remaining
;
484 retval
= send_request_dev_dep_msg_in(data
, this_part
);
486 dev_err(dev
, "usb_bulk_msg returned %d\n", retval
);
487 if (data
->auto_abort
)
488 usbtmc_ioctl_abort_bulk_out(data
);
494 retval
= usb_bulk_msg(data
->usb_dev
,
495 usb_rcvbulkpipe(data
->usb_dev
,
497 buffer
, USBTMC_SIZE_IOBUFFER
, &actual
,
500 dev_dbg(dev
, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval
, done
, remaining
, actual
);
502 /* Store bTag (in case we need to abort) */
503 data
->bTag_last_read
= data
->bTag
;
506 dev_dbg(dev
, "Unable to read data, error %d\n", retval
);
507 if (data
->auto_abort
)
508 usbtmc_ioctl_abort_bulk_in(data
);
512 /* Parse header in first packet */
513 if ((done
== 0) || (!(data
->rigol_quirk
))) {
514 /* Sanity checks for the header */
515 if (actual
< USBTMC_HEADER_SIZE
) {
516 dev_err(dev
, "Device sent too small first packet: %u < %u\n", actual
, USBTMC_HEADER_SIZE
);
517 if (data
->auto_abort
)
518 usbtmc_ioctl_abort_bulk_in(data
);
522 if (buffer
[0] != 2) {
523 dev_err(dev
, "Device sent reply with wrong MsgID: %u != 2\n", buffer
[0]);
524 if (data
->auto_abort
)
525 usbtmc_ioctl_abort_bulk_in(data
);
529 if (buffer
[1] != data
->bTag_last_write
) {
530 dev_err(dev
, "Device sent reply with wrong bTag: %u != %u\n", buffer
[1], data
->bTag_last_write
);
531 if (data
->auto_abort
)
532 usbtmc_ioctl_abort_bulk_in(data
);
536 /* How many characters did the instrument send? */
537 n_characters
= buffer
[4] +
542 if (n_characters
> this_part
) {
543 dev_err(dev
, "Device wants to return more data than requested: %u > %zu\n", n_characters
, count
);
544 if (data
->auto_abort
)
545 usbtmc_ioctl_abort_bulk_in(data
);
549 /* Remove the USBTMC header */
550 actual
-= USBTMC_HEADER_SIZE
;
552 /* Check if the message is smaller than requested */
553 if (data
->rigol_quirk
) {
554 if (remaining
> n_characters
)
555 remaining
= n_characters
;
556 /* Remove padding if it exists */
557 if (actual
> remaining
)
561 if (this_part
> n_characters
)
562 this_part
= n_characters
;
563 /* Remove padding if it exists */
564 if (actual
> this_part
)
568 dev_dbg(dev
, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters
, buffer
[8]);
572 /* Terminate if end-of-message bit received from device */
573 if ((buffer
[8] & 0x01) && (actual
>= n_characters
))
576 dev_dbg(dev
, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining
,buf
,buffer
,done
);
579 /* Copy buffer to user space */
580 if (copy_to_user(buf
+ done
, &buffer
[USBTMC_HEADER_SIZE
], actual
)) {
581 /* There must have been an addressing problem */
588 if (actual
> remaining
)
593 dev_dbg(dev
, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual
, done
, remaining
,buf
,buffer
);
595 /* Copy buffer to user space */
596 if (copy_to_user(buf
+ done
, buffer
, actual
)) {
597 /* There must have been an addressing problem */
605 /* Update file position value */
606 *f_pos
= *f_pos
+ done
;
610 mutex_unlock(&data
->io_mutex
);
615 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
616 size_t count
, loff_t
*f_pos
)
618 struct usbtmc_device_data
*data
;
622 unsigned long int n_bytes
;
627 data
= filp
->private_data
;
629 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
633 mutex_lock(&data
->io_mutex
);
642 while (remaining
> 0) {
643 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
) {
644 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
;
647 this_part
= remaining
;
651 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
653 buffer
[1] = data
->bTag
;
654 buffer
[2] = ~(data
->bTag
);
655 buffer
[3] = 0; /* Reserved */
656 buffer
[4] = this_part
& 255;
657 buffer
[5] = (this_part
>> 8) & 255;
658 buffer
[6] = (this_part
>> 16) & 255;
659 buffer
[7] = (this_part
>> 24) & 255;
660 /* buffer[8] is set above... */
661 buffer
[9] = 0; /* Reserved */
662 buffer
[10] = 0; /* Reserved */
663 buffer
[11] = 0; /* Reserved */
665 if (copy_from_user(&buffer
[USBTMC_HEADER_SIZE
], buf
+ done
, this_part
)) {
670 n_bytes
= roundup(USBTMC_HEADER_SIZE
+ this_part
, 4);
671 memset(buffer
+ USBTMC_HEADER_SIZE
+ this_part
, 0, n_bytes
- (USBTMC_HEADER_SIZE
+ this_part
));
674 retval
= usb_bulk_msg(data
->usb_dev
,
675 usb_sndbulkpipe(data
->usb_dev
,
678 &actual
, USBTMC_TIMEOUT
);
684 data
->bTag_last_write
= data
->bTag
;
691 dev_err(&data
->intf
->dev
,
692 "Unable to send data, error %d\n", retval
);
693 if (data
->auto_abort
)
694 usbtmc_ioctl_abort_bulk_out(data
);
698 remaining
-= this_part
;
704 mutex_unlock(&data
->io_mutex
);
709 static int usbtmc_ioctl_clear(struct usbtmc_device_data
*data
)
711 struct usb_host_interface
*current_setting
;
712 struct usb_endpoint_descriptor
*desc
;
720 dev
= &data
->intf
->dev
;
722 dev_dbg(dev
, "Sending INITIATE_CLEAR request\n");
724 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
728 rv
= usb_control_msg(data
->usb_dev
,
729 usb_rcvctrlpipe(data
->usb_dev
, 0),
730 USBTMC_REQUEST_INITIATE_CLEAR
,
731 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
732 0, 0, buffer
, 1, USBTMC_TIMEOUT
);
734 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
738 dev_dbg(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
740 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
741 dev_err(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
747 current_setting
= data
->intf
->cur_altsetting
;
748 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++) {
749 desc
= ¤t_setting
->endpoint
[n
].desc
;
750 if (desc
->bEndpointAddress
== data
->bulk_in
)
751 max_size
= usb_endpoint_maxp(desc
);
755 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
760 dev_dbg(dev
, "wMaxPacketSize is %d\n", max_size
);
764 usbtmc_clear_check_status
:
766 dev_dbg(dev
, "Sending CHECK_CLEAR_STATUS request\n");
768 rv
= usb_control_msg(data
->usb_dev
,
769 usb_rcvctrlpipe(data
->usb_dev
, 0),
770 USBTMC_REQUEST_CHECK_CLEAR_STATUS
,
771 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
772 0, 0, buffer
, 2, USBTMC_TIMEOUT
);
774 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
778 dev_dbg(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
780 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
781 goto usbtmc_clear_bulk_out_halt
;
783 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
784 dev_err(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
791 dev_dbg(dev
, "Reading from bulk in EP\n");
793 rv
= usb_bulk_msg(data
->usb_dev
,
794 usb_rcvbulkpipe(data
->usb_dev
,
796 buffer
, USBTMC_SIZE_IOBUFFER
,
797 &actual
, USBTMC_TIMEOUT
);
801 dev_err(dev
, "usb_control_msg returned %d\n",
805 } while ((actual
== max_size
) &&
806 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
808 if (actual
== max_size
) {
809 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
810 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
815 goto usbtmc_clear_check_status
;
817 usbtmc_clear_bulk_out_halt
:
819 rv
= usb_clear_halt(data
->usb_dev
,
820 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
822 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
832 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data
*data
)
836 rv
= usb_clear_halt(data
->usb_dev
,
837 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
840 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
847 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data
*data
)
851 rv
= usb_clear_halt(data
->usb_dev
,
852 usb_rcvbulkpipe(data
->usb_dev
, data
->bulk_in
));
855 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
862 static int get_capabilities(struct usbtmc_device_data
*data
)
864 struct device
*dev
= &data
->usb_dev
->dev
;
868 buffer
= kmalloc(0x18, GFP_KERNEL
);
872 rv
= usb_control_msg(data
->usb_dev
, usb_rcvctrlpipe(data
->usb_dev
, 0),
873 USBTMC_REQUEST_GET_CAPABILITIES
,
874 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
875 0, 0, buffer
, 0x18, USBTMC_TIMEOUT
);
877 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
881 dev_dbg(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
882 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
883 dev_err(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
887 dev_dbg(dev
, "Interface capabilities are %x\n", buffer
[4]);
888 dev_dbg(dev
, "Device capabilities are %x\n", buffer
[5]);
889 dev_dbg(dev
, "USB488 interface capabilities are %x\n", buffer
[14]);
890 dev_dbg(dev
, "USB488 device capabilities are %x\n", buffer
[15]);
892 data
->capabilities
.interface_capabilities
= buffer
[4];
893 data
->capabilities
.device_capabilities
= buffer
[5];
894 data
->capabilities
.usb488_interface_capabilities
= buffer
[14];
895 data
->capabilities
.usb488_device_capabilities
= buffer
[15];
903 #define capability_attribute(name) \
904 static ssize_t show_##name(struct device *dev, \
905 struct device_attribute *attr, char *buf) \
907 struct usb_interface *intf = to_usb_interface(dev); \
908 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
910 return sprintf(buf, "%d\n", data->capabilities.name); \
912 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
914 capability_attribute(interface_capabilities
);
915 capability_attribute(device_capabilities
);
916 capability_attribute(usb488_interface_capabilities
);
917 capability_attribute(usb488_device_capabilities
);
919 static struct attribute
*capability_attrs
[] = {
920 &dev_attr_interface_capabilities
.attr
,
921 &dev_attr_device_capabilities
.attr
,
922 &dev_attr_usb488_interface_capabilities
.attr
,
923 &dev_attr_usb488_device_capabilities
.attr
,
927 static struct attribute_group capability_attr_grp
= {
928 .attrs
= capability_attrs
,
931 static ssize_t
show_TermChar(struct device
*dev
,
932 struct device_attribute
*attr
, char *buf
)
934 struct usb_interface
*intf
= to_usb_interface(dev
);
935 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
937 return sprintf(buf
, "%c\n", data
->TermChar
);
940 static ssize_t
store_TermChar(struct device
*dev
,
941 struct device_attribute
*attr
,
942 const char *buf
, size_t count
)
944 struct usb_interface
*intf
= to_usb_interface(dev
);
945 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
949 data
->TermChar
= buf
[0];
952 static DEVICE_ATTR(TermChar
, S_IRUGO
, show_TermChar
, store_TermChar
);
954 #define data_attribute(name) \
955 static ssize_t show_##name(struct device *dev, \
956 struct device_attribute *attr, char *buf) \
958 struct usb_interface *intf = to_usb_interface(dev); \
959 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
961 return sprintf(buf, "%d\n", data->name); \
963 static ssize_t store_##name(struct device *dev, \
964 struct device_attribute *attr, \
965 const char *buf, size_t count) \
967 struct usb_interface *intf = to_usb_interface(dev); \
968 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
972 result = sscanf(buf, "%u\n", &val); \
981 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
983 data_attribute(TermCharEnabled
);
984 data_attribute(auto_abort
);
986 static struct attribute
*data_attrs
[] = {
987 &dev_attr_TermChar
.attr
,
988 &dev_attr_TermCharEnabled
.attr
,
989 &dev_attr_auto_abort
.attr
,
993 static struct attribute_group data_attr_grp
= {
997 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
1003 dev
= &data
->intf
->dev
;
1005 buffer
= kmalloc(2, GFP_KERNEL
);
1009 rv
= usb_control_msg(data
->usb_dev
,
1010 usb_rcvctrlpipe(data
->usb_dev
, 0),
1011 USBTMC_REQUEST_INDICATOR_PULSE
,
1012 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1013 0, 0, buffer
, 0x01, USBTMC_TIMEOUT
);
1016 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1020 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1022 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1023 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1034 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1036 struct usbtmc_device_data
*data
;
1037 int retval
= -EBADRQC
;
1039 data
= file
->private_data
;
1040 mutex_lock(&data
->io_mutex
);
1043 goto skip_io_on_zombie
;
1047 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
1048 retval
= usbtmc_ioctl_clear_out_halt(data
);
1051 case USBTMC_IOCTL_CLEAR_IN_HALT
:
1052 retval
= usbtmc_ioctl_clear_in_halt(data
);
1055 case USBTMC_IOCTL_INDICATOR_PULSE
:
1056 retval
= usbtmc_ioctl_indicator_pulse(data
);
1059 case USBTMC_IOCTL_CLEAR
:
1060 retval
= usbtmc_ioctl_clear(data
);
1063 case USBTMC_IOCTL_ABORT_BULK_OUT
:
1064 retval
= usbtmc_ioctl_abort_bulk_out(data
);
1067 case USBTMC_IOCTL_ABORT_BULK_IN
:
1068 retval
= usbtmc_ioctl_abort_bulk_in(data
);
1073 mutex_unlock(&data
->io_mutex
);
1077 static const struct file_operations fops
= {
1078 .owner
= THIS_MODULE
,
1079 .read
= usbtmc_read
,
1080 .write
= usbtmc_write
,
1081 .open
= usbtmc_open
,
1082 .release
= usbtmc_release
,
1083 .unlocked_ioctl
= usbtmc_ioctl
,
1084 .llseek
= default_llseek
,
1087 static struct usb_class_driver usbtmc_class
= {
1090 .minor_base
= USBTMC_MINOR_BASE
,
1094 static int usbtmc_probe(struct usb_interface
*intf
,
1095 const struct usb_device_id
*id
)
1097 struct usbtmc_device_data
*data
;
1098 struct usb_host_interface
*iface_desc
;
1099 struct usb_endpoint_descriptor
*endpoint
;
1103 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
1105 data
= kmalloc(sizeof(struct usbtmc_device_data
), GFP_KERNEL
);
1107 dev_err(&intf
->dev
, "Unable to allocate kernel memory\n");
1113 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
1114 usb_set_intfdata(intf
, data
);
1115 kref_init(&data
->kref
);
1116 mutex_init(&data
->io_mutex
);
1119 /* Determine if it is a Rigol or not */
1120 data
->rigol_quirk
= 0;
1121 dev_dbg(&intf
->dev
, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1122 data
->usb_dev
->descriptor
.idVendor
,
1123 data
->usb_dev
->descriptor
.idProduct
);
1124 for(n
= 0; usbtmc_id_quirk
[n
].idVendor
> 0; n
++) {
1125 if ((usbtmc_id_quirk
[n
].idVendor
== data
->usb_dev
->descriptor
.idVendor
) &&
1126 (usbtmc_id_quirk
[n
].idProduct
== data
->usb_dev
->descriptor
.idProduct
)) {
1127 dev_dbg(&intf
->dev
, "Setting this device as having the RIGOL quirk\n");
1128 data
->rigol_quirk
= 1;
1133 /* Initialize USBTMC bTag and other fields */
1135 data
->TermCharEnabled
= 0;
1136 data
->TermChar
= '\n';
1138 /* USBTMC devices have only one setting, so use that */
1139 iface_desc
= data
->intf
->cur_altsetting
;
1141 /* Find bulk in endpoint */
1142 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1143 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1145 if (usb_endpoint_is_bulk_in(endpoint
)) {
1146 data
->bulk_in
= endpoint
->bEndpointAddress
;
1147 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n",
1153 /* Find bulk out endpoint */
1154 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1155 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1157 if (usb_endpoint_is_bulk_out(endpoint
)) {
1158 data
->bulk_out
= endpoint
->bEndpointAddress
;
1159 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n",
1165 retcode
= get_capabilities(data
);
1167 dev_err(&intf
->dev
, "can't read capabilities\n");
1169 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
1170 &capability_attr_grp
);
1172 retcode
= sysfs_create_group(&intf
->dev
.kobj
, &data_attr_grp
);
1174 retcode
= usb_register_dev(intf
, &usbtmc_class
);
1176 dev_err(&intf
->dev
, "Not able to get a minor"
1177 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE
,
1179 goto error_register
;
1181 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
1186 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1187 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1188 kref_put(&data
->kref
, usbtmc_delete
);
1192 static void usbtmc_disconnect(struct usb_interface
*intf
)
1194 struct usbtmc_device_data
*data
;
1196 dev_dbg(&intf
->dev
, "usbtmc_disconnect called\n");
1198 data
= usb_get_intfdata(intf
);
1199 usb_deregister_dev(intf
, &usbtmc_class
);
1200 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1201 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1202 mutex_lock(&data
->io_mutex
);
1204 mutex_unlock(&data
->io_mutex
);
1205 kref_put(&data
->kref
, usbtmc_delete
);
1208 static int usbtmc_suspend(struct usb_interface
*intf
, pm_message_t message
)
1210 /* this driver does not have pending URBs */
1214 static int usbtmc_resume(struct usb_interface
*intf
)
1219 static struct usb_driver usbtmc_driver
= {
1221 .id_table
= usbtmc_devices
,
1222 .probe
= usbtmc_probe
,
1223 .disconnect
= usbtmc_disconnect
,
1224 .suspend
= usbtmc_suspend
,
1225 .resume
= usbtmc_resume
,
1228 module_usb_driver(usbtmc_driver
);
1230 MODULE_LICENSE("GPL");