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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
28 #include <linux/uaccess.h>
29 #include <linux/kref.h>
30 #include <linux/slab.h>
31 #include <linux/mutex.h>
32 #include <linux/usb.h>
33 #include <linux/usb/tmc.h>
37 #define USBTMC_HEADER_SIZE 12
38 #define USBTMC_MINOR_BASE 176
41 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
42 * large as wMaxPacketSize (which is usually 512 bytes).
44 #define USBTMC_SIZE_IOBUFFER 2048
46 /* Default USB timeout (in milliseconds) */
47 #define USBTMC_TIMEOUT 5000
50 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
51 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
52 * packet is never read.
54 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
56 static const struct usb_device_id usbtmc_devices
[] = {
57 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 0), },
58 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 1), },
59 { 0, } /* terminating entry */
61 MODULE_DEVICE_TABLE(usb
, usbtmc_devices
);
64 * This structure is the capabilities for the device
65 * See section 4.2.1.8 of the USBTMC specification,
66 * and section 4.2.2 of the USBTMC usb488 subclass
67 * specification for details.
69 struct usbtmc_dev_capabilities
{
70 __u8 interface_capabilities
;
71 __u8 device_capabilities
;
72 __u8 usb488_interface_capabilities
;
73 __u8 usb488_device_capabilities
;
76 /* This structure holds private data for each USBTMC device. One copy is
77 * allocated for each USBTMC device in the driver's probe function.
79 struct usbtmc_device_data
{
80 const struct usb_device_id
*id
;
81 struct usb_device
*usb_dev
;
82 struct usb_interface
*intf
;
85 unsigned int bulk_out
;
88 u8 bTag_last_write
; /* needed for abort */
89 u8 bTag_last_read
; /* needed for abort */
93 /* attributes from the USB TMC spec for this device */
98 bool zombie
; /* fd of disconnected device */
100 struct usbtmc_dev_capabilities capabilities
;
102 struct mutex io_mutex
; /* only one i/o function running at a time */
104 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
106 struct usbtmc_ID_rigol_quirk
{
111 static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk
[] = {
116 /* Forward declarations */
117 static struct usb_driver usbtmc_driver
;
119 static void usbtmc_delete(struct kref
*kref
)
121 struct usbtmc_device_data
*data
= to_usbtmc_data(kref
);
123 usb_put_dev(data
->usb_dev
);
126 static int usbtmc_open(struct inode
*inode
, struct file
*filp
)
128 struct usb_interface
*intf
;
129 struct usbtmc_device_data
*data
;
132 intf
= usb_find_interface(&usbtmc_driver
, iminor(inode
));
134 pr_err("can not find device for minor %d", iminor(inode
));
138 data
= usb_get_intfdata(intf
);
139 kref_get(&data
->kref
);
141 /* Store pointer in file structure's private data field */
142 filp
->private_data
= data
;
147 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
149 struct usbtmc_device_data
*data
= file
->private_data
;
151 kref_put(&data
->kref
, usbtmc_delete
);
155 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
162 struct usb_host_interface
*current_setting
;
165 dev
= &data
->intf
->dev
;
166 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
170 rv
= usb_control_msg(data
->usb_dev
,
171 usb_rcvctrlpipe(data
->usb_dev
, 0),
172 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
173 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
174 data
->bTag_last_read
, data
->bulk_in
,
175 buffer
, 2, USBTMC_TIMEOUT
);
178 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
182 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
184 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
189 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
190 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
197 current_setting
= data
->intf
->cur_altsetting
;
198 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++)
199 if (current_setting
->endpoint
[n
].desc
.bEndpointAddress
==
201 max_size
= usb_endpoint_maxp(¤t_setting
->endpoint
[n
].desc
);
204 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
209 dev_dbg(&data
->intf
->dev
, "wMaxPacketSize is %d\n", max_size
);
214 dev_dbg(dev
, "Reading from bulk in EP\n");
216 rv
= usb_bulk_msg(data
->usb_dev
,
217 usb_rcvbulkpipe(data
->usb_dev
,
219 buffer
, USBTMC_SIZE_IOBUFFER
,
220 &actual
, USBTMC_TIMEOUT
);
225 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
228 } while ((actual
== max_size
) &&
229 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
231 if (actual
== max_size
) {
232 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
233 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
240 usbtmc_abort_bulk_in_status
:
241 rv
= usb_control_msg(data
->usb_dev
,
242 usb_rcvctrlpipe(data
->usb_dev
, 0),
243 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
244 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
245 0, data
->bulk_in
, buffer
, 0x08,
249 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
253 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
255 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
260 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
261 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
268 dev_dbg(dev
, "Reading from bulk in EP\n");
270 rv
= usb_bulk_msg(data
->usb_dev
,
271 usb_rcvbulkpipe(data
->usb_dev
,
273 buffer
, USBTMC_SIZE_IOBUFFER
,
274 &actual
, USBTMC_TIMEOUT
);
279 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
282 } while ((actual
== max_size
) &&
283 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
285 if (actual
== max_size
) {
286 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
287 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
292 goto usbtmc_abort_bulk_in_status
;
300 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
307 dev
= &data
->intf
->dev
;
309 buffer
= kmalloc(8, GFP_KERNEL
);
313 rv
= usb_control_msg(data
->usb_dev
,
314 usb_rcvctrlpipe(data
->usb_dev
, 0),
315 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
316 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
317 data
->bTag_last_write
, data
->bulk_out
,
318 buffer
, 2, USBTMC_TIMEOUT
);
321 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
325 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
327 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
328 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
336 usbtmc_abort_bulk_out_check_status
:
337 rv
= usb_control_msg(data
->usb_dev
,
338 usb_rcvctrlpipe(data
->usb_dev
, 0),
339 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
340 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
341 0, data
->bulk_out
, buffer
, 0x08,
345 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
349 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
351 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
352 goto usbtmc_abort_bulk_out_clear_halt
;
354 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
355 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
356 goto usbtmc_abort_bulk_out_check_status
;
361 usbtmc_abort_bulk_out_clear_halt
:
362 rv
= usb_clear_halt(data
->usb_dev
,
363 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
366 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
377 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
378 * @transfer_size: number of bytes to request from the device.
380 * See the USBTMC specification, Table 4.
382 * Also updates bTag_last_write.
384 static int send_request_dev_dep_msg_in(struct usbtmc_device_data
*data
, size_t transfer_size
)
387 u8 buffer
[USBTMC_HEADER_SIZE
];
390 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
391 * Refer to class specs for details
394 buffer
[1] = data
->bTag
;
395 buffer
[2] = ~data
->bTag
;
396 buffer
[3] = 0; /* Reserved */
397 buffer
[4] = transfer_size
>> 0;
398 buffer
[5] = transfer_size
>> 8;
399 buffer
[6] = transfer_size
>> 16;
400 buffer
[7] = transfer_size
>> 24;
401 buffer
[8] = data
->TermCharEnabled
* 2;
402 /* Use term character? */
403 buffer
[9] = data
->TermChar
;
404 buffer
[10] = 0; /* Reserved */
405 buffer
[11] = 0; /* Reserved */
408 retval
= usb_bulk_msg(data
->usb_dev
,
409 usb_sndbulkpipe(data
->usb_dev
,
411 buffer
, USBTMC_HEADER_SIZE
, &actual
, USBTMC_TIMEOUT
);
413 /* Store bTag (in case we need to abort) */
414 data
->bTag_last_write
= data
->bTag
;
416 /* Increment bTag -- and increment again if zero */
422 dev_err(&data
->intf
->dev
, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval
);
429 static ssize_t
usbtmc_read(struct file
*filp
, char __user
*buf
,
430 size_t count
, loff_t
*f_pos
)
432 struct usbtmc_device_data
*data
;
442 /* Get pointer to private data structure */
443 data
= filp
->private_data
;
444 dev
= &data
->intf
->dev
;
446 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
450 mutex_lock(&data
->io_mutex
);
456 if (data
->rigol_quirk
) {
457 dev_dbg(dev
, "usb_bulk_msg_in: count(%zu)\n", count
);
459 retval
= send_request_dev_dep_msg_in(data
, count
);
462 if (data
->auto_abort
)
463 usbtmc_ioctl_abort_bulk_out(data
);
468 /* Loop until we have fetched everything we requested */
470 this_part
= remaining
;
473 while (remaining
> 0) {
474 if (!data
->rigol_quirk
) {
475 dev_dbg(dev
, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining
, count
);
477 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3)
478 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3;
480 this_part
= remaining
;
482 retval
= send_request_dev_dep_msg_in(data
, this_part
);
484 dev_err(dev
, "usb_bulk_msg returned %d\n", retval
);
485 if (data
->auto_abort
)
486 usbtmc_ioctl_abort_bulk_out(data
);
492 retval
= usb_bulk_msg(data
->usb_dev
,
493 usb_rcvbulkpipe(data
->usb_dev
,
495 buffer
, USBTMC_SIZE_IOBUFFER
, &actual
,
498 dev_dbg(dev
, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval
, done
, remaining
, actual
);
500 /* Store bTag (in case we need to abort) */
501 data
->bTag_last_read
= data
->bTag
;
504 dev_dbg(dev
, "Unable to read data, error %d\n", retval
);
505 if (data
->auto_abort
)
506 usbtmc_ioctl_abort_bulk_in(data
);
510 /* Parse header in first packet */
511 if ((done
== 0) || !data
->rigol_quirk
) {
512 /* Sanity checks for the header */
513 if (actual
< USBTMC_HEADER_SIZE
) {
514 dev_err(dev
, "Device sent too small first packet: %u < %u\n", actual
, USBTMC_HEADER_SIZE
);
515 if (data
->auto_abort
)
516 usbtmc_ioctl_abort_bulk_in(data
);
520 if (buffer
[0] != 2) {
521 dev_err(dev
, "Device sent reply with wrong MsgID: %u != 2\n", buffer
[0]);
522 if (data
->auto_abort
)
523 usbtmc_ioctl_abort_bulk_in(data
);
527 if (buffer
[1] != data
->bTag_last_write
) {
528 dev_err(dev
, "Device sent reply with wrong bTag: %u != %u\n", buffer
[1], data
->bTag_last_write
);
529 if (data
->auto_abort
)
530 usbtmc_ioctl_abort_bulk_in(data
);
534 /* How many characters did the instrument send? */
535 n_characters
= buffer
[4] +
540 if (n_characters
> this_part
) {
541 dev_err(dev
, "Device wants to return more data than requested: %u > %zu\n", n_characters
, count
);
542 if (data
->auto_abort
)
543 usbtmc_ioctl_abort_bulk_in(data
);
547 /* Remove the USBTMC header */
548 actual
-= USBTMC_HEADER_SIZE
;
550 /* Check if the message is smaller than requested */
551 if (data
->rigol_quirk
) {
552 if (remaining
> n_characters
)
553 remaining
= n_characters
;
554 /* Remove padding if it exists */
555 if (actual
> remaining
)
559 if (this_part
> n_characters
)
560 this_part
= n_characters
;
561 /* Remove padding if it exists */
562 if (actual
> this_part
)
566 dev_dbg(dev
, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters
, buffer
[8]);
570 /* Terminate if end-of-message bit received from device */
571 if ((buffer
[8] & 0x01) && (actual
>= n_characters
))
574 dev_dbg(dev
, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining
,buf
,buffer
,done
);
577 /* Copy buffer to user space */
578 if (copy_to_user(buf
+ done
, &buffer
[USBTMC_HEADER_SIZE
], actual
)) {
579 /* There must have been an addressing problem */
586 if (actual
> remaining
)
591 dev_dbg(dev
, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual
, done
, remaining
,buf
,buffer
);
593 /* Copy buffer to user space */
594 if (copy_to_user(buf
+ done
, buffer
, actual
)) {
595 /* There must have been an addressing problem */
603 /* Update file position value */
604 *f_pos
= *f_pos
+ done
;
608 mutex_unlock(&data
->io_mutex
);
613 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
614 size_t count
, loff_t
*f_pos
)
616 struct usbtmc_device_data
*data
;
620 unsigned long int n_bytes
;
625 data
= filp
->private_data
;
627 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
631 mutex_lock(&data
->io_mutex
);
640 while (remaining
> 0) {
641 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
) {
642 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
;
645 this_part
= remaining
;
649 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
651 buffer
[1] = data
->bTag
;
652 buffer
[2] = ~data
->bTag
;
653 buffer
[3] = 0; /* Reserved */
654 buffer
[4] = this_part
>> 0;
655 buffer
[5] = this_part
>> 8;
656 buffer
[6] = this_part
>> 16;
657 buffer
[7] = this_part
>> 24;
658 /* buffer[8] is set above... */
659 buffer
[9] = 0; /* Reserved */
660 buffer
[10] = 0; /* Reserved */
661 buffer
[11] = 0; /* Reserved */
663 if (copy_from_user(&buffer
[USBTMC_HEADER_SIZE
], buf
+ done
, this_part
)) {
668 n_bytes
= roundup(USBTMC_HEADER_SIZE
+ this_part
, 4);
669 memset(buffer
+ USBTMC_HEADER_SIZE
+ this_part
, 0, n_bytes
- (USBTMC_HEADER_SIZE
+ this_part
));
672 retval
= usb_bulk_msg(data
->usb_dev
,
673 usb_sndbulkpipe(data
->usb_dev
,
676 &actual
, USBTMC_TIMEOUT
);
682 data
->bTag_last_write
= data
->bTag
;
689 dev_err(&data
->intf
->dev
,
690 "Unable to send data, error %d\n", retval
);
691 if (data
->auto_abort
)
692 usbtmc_ioctl_abort_bulk_out(data
);
696 remaining
-= this_part
;
702 mutex_unlock(&data
->io_mutex
);
707 static int usbtmc_ioctl_clear(struct usbtmc_device_data
*data
)
709 struct usb_host_interface
*current_setting
;
710 struct usb_endpoint_descriptor
*desc
;
718 dev
= &data
->intf
->dev
;
720 dev_dbg(dev
, "Sending INITIATE_CLEAR request\n");
722 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
726 rv
= usb_control_msg(data
->usb_dev
,
727 usb_rcvctrlpipe(data
->usb_dev
, 0),
728 USBTMC_REQUEST_INITIATE_CLEAR
,
729 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
730 0, 0, buffer
, 1, USBTMC_TIMEOUT
);
732 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
736 dev_dbg(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
738 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
739 dev_err(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
745 current_setting
= data
->intf
->cur_altsetting
;
746 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++) {
747 desc
= ¤t_setting
->endpoint
[n
].desc
;
748 if (desc
->bEndpointAddress
== data
->bulk_in
)
749 max_size
= usb_endpoint_maxp(desc
);
753 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
758 dev_dbg(dev
, "wMaxPacketSize is %d\n", max_size
);
762 usbtmc_clear_check_status
:
764 dev_dbg(dev
, "Sending CHECK_CLEAR_STATUS request\n");
766 rv
= usb_control_msg(data
->usb_dev
,
767 usb_rcvctrlpipe(data
->usb_dev
, 0),
768 USBTMC_REQUEST_CHECK_CLEAR_STATUS
,
769 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
770 0, 0, buffer
, 2, USBTMC_TIMEOUT
);
772 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
776 dev_dbg(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
778 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
779 goto usbtmc_clear_bulk_out_halt
;
781 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
782 dev_err(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
789 dev_dbg(dev
, "Reading from bulk in EP\n");
791 rv
= usb_bulk_msg(data
->usb_dev
,
792 usb_rcvbulkpipe(data
->usb_dev
,
794 buffer
, USBTMC_SIZE_IOBUFFER
,
795 &actual
, USBTMC_TIMEOUT
);
799 dev_err(dev
, "usb_control_msg returned %d\n",
803 } while ((actual
== max_size
) &&
804 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
806 if (actual
== max_size
) {
807 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
808 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
813 goto usbtmc_clear_check_status
;
815 usbtmc_clear_bulk_out_halt
:
817 rv
= usb_clear_halt(data
->usb_dev
,
818 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
820 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
830 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data
*data
)
834 rv
= usb_clear_halt(data
->usb_dev
,
835 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
838 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
845 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data
*data
)
849 rv
= usb_clear_halt(data
->usb_dev
,
850 usb_rcvbulkpipe(data
->usb_dev
, data
->bulk_in
));
853 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
860 static int get_capabilities(struct usbtmc_device_data
*data
)
862 struct device
*dev
= &data
->usb_dev
->dev
;
866 buffer
= kmalloc(0x18, GFP_KERNEL
);
870 rv
= usb_control_msg(data
->usb_dev
, usb_rcvctrlpipe(data
->usb_dev
, 0),
871 USBTMC_REQUEST_GET_CAPABILITIES
,
872 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
873 0, 0, buffer
, 0x18, USBTMC_TIMEOUT
);
875 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
879 dev_dbg(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
880 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
881 dev_err(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
885 dev_dbg(dev
, "Interface capabilities are %x\n", buffer
[4]);
886 dev_dbg(dev
, "Device capabilities are %x\n", buffer
[5]);
887 dev_dbg(dev
, "USB488 interface capabilities are %x\n", buffer
[14]);
888 dev_dbg(dev
, "USB488 device capabilities are %x\n", buffer
[15]);
890 data
->capabilities
.interface_capabilities
= buffer
[4];
891 data
->capabilities
.device_capabilities
= buffer
[5];
892 data
->capabilities
.usb488_interface_capabilities
= buffer
[14];
893 data
->capabilities
.usb488_device_capabilities
= buffer
[15];
901 #define capability_attribute(name) \
902 static ssize_t name##_show(struct device *dev, \
903 struct device_attribute *attr, char *buf) \
905 struct usb_interface *intf = to_usb_interface(dev); \
906 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
908 return sprintf(buf, "%d\n", data->capabilities.name); \
910 static DEVICE_ATTR_RO(name)
912 capability_attribute(interface_capabilities
);
913 capability_attribute(device_capabilities
);
914 capability_attribute(usb488_interface_capabilities
);
915 capability_attribute(usb488_device_capabilities
);
917 static struct attribute
*capability_attrs
[] = {
918 &dev_attr_interface_capabilities
.attr
,
919 &dev_attr_device_capabilities
.attr
,
920 &dev_attr_usb488_interface_capabilities
.attr
,
921 &dev_attr_usb488_device_capabilities
.attr
,
925 static struct attribute_group capability_attr_grp
= {
926 .attrs
= capability_attrs
,
929 static ssize_t
TermChar_show(struct device
*dev
,
930 struct device_attribute
*attr
, char *buf
)
932 struct usb_interface
*intf
= to_usb_interface(dev
);
933 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
935 return sprintf(buf
, "%c\n", data
->TermChar
);
938 static ssize_t
TermChar_store(struct device
*dev
,
939 struct device_attribute
*attr
,
940 const char *buf
, size_t count
)
942 struct usb_interface
*intf
= to_usb_interface(dev
);
943 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
947 data
->TermChar
= buf
[0];
950 static DEVICE_ATTR_RW(TermChar
);
952 #define data_attribute(name) \
953 static ssize_t name##_show(struct device *dev, \
954 struct device_attribute *attr, char *buf) \
956 struct usb_interface *intf = to_usb_interface(dev); \
957 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
959 return sprintf(buf, "%d\n", data->name); \
961 static ssize_t name##_store(struct device *dev, \
962 struct device_attribute *attr, \
963 const char *buf, size_t count) \
965 struct usb_interface *intf = to_usb_interface(dev); \
966 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
970 result = sscanf(buf, "%u\n", &val); \
979 static DEVICE_ATTR_RW(name)
981 data_attribute(TermCharEnabled
);
982 data_attribute(auto_abort
);
984 static struct attribute
*data_attrs
[] = {
985 &dev_attr_TermChar
.attr
,
986 &dev_attr_TermCharEnabled
.attr
,
987 &dev_attr_auto_abort
.attr
,
991 static struct attribute_group data_attr_grp
= {
995 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
1001 dev
= &data
->intf
->dev
;
1003 buffer
= kmalloc(2, GFP_KERNEL
);
1007 rv
= usb_control_msg(data
->usb_dev
,
1008 usb_rcvctrlpipe(data
->usb_dev
, 0),
1009 USBTMC_REQUEST_INDICATOR_PULSE
,
1010 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1011 0, 0, buffer
, 0x01, USBTMC_TIMEOUT
);
1014 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1018 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1020 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1021 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1032 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1034 struct usbtmc_device_data
*data
;
1035 int retval
= -EBADRQC
;
1037 data
= file
->private_data
;
1038 mutex_lock(&data
->io_mutex
);
1041 goto skip_io_on_zombie
;
1045 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
1046 retval
= usbtmc_ioctl_clear_out_halt(data
);
1049 case USBTMC_IOCTL_CLEAR_IN_HALT
:
1050 retval
= usbtmc_ioctl_clear_in_halt(data
);
1053 case USBTMC_IOCTL_INDICATOR_PULSE
:
1054 retval
= usbtmc_ioctl_indicator_pulse(data
);
1057 case USBTMC_IOCTL_CLEAR
:
1058 retval
= usbtmc_ioctl_clear(data
);
1061 case USBTMC_IOCTL_ABORT_BULK_OUT
:
1062 retval
= usbtmc_ioctl_abort_bulk_out(data
);
1065 case USBTMC_IOCTL_ABORT_BULK_IN
:
1066 retval
= usbtmc_ioctl_abort_bulk_in(data
);
1071 mutex_unlock(&data
->io_mutex
);
1075 static const struct file_operations fops
= {
1076 .owner
= THIS_MODULE
,
1077 .read
= usbtmc_read
,
1078 .write
= usbtmc_write
,
1079 .open
= usbtmc_open
,
1080 .release
= usbtmc_release
,
1081 .unlocked_ioctl
= usbtmc_ioctl
,
1082 .llseek
= default_llseek
,
1085 static struct usb_class_driver usbtmc_class
= {
1088 .minor_base
= USBTMC_MINOR_BASE
,
1092 static int usbtmc_probe(struct usb_interface
*intf
,
1093 const struct usb_device_id
*id
)
1095 struct usbtmc_device_data
*data
;
1096 struct usb_host_interface
*iface_desc
;
1097 struct usb_endpoint_descriptor
*endpoint
;
1101 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
1103 data
= devm_kzalloc(&intf
->dev
, sizeof(*data
), GFP_KERNEL
);
1105 dev_err(&intf
->dev
, "Unable to allocate kernel memory\n");
1111 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
1112 usb_set_intfdata(intf
, data
);
1113 kref_init(&data
->kref
);
1114 mutex_init(&data
->io_mutex
);
1117 /* Determine if it is a Rigol or not */
1118 data
->rigol_quirk
= 0;
1119 dev_dbg(&intf
->dev
, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1120 le16_to_cpu(data
->usb_dev
->descriptor
.idVendor
),
1121 le16_to_cpu(data
->usb_dev
->descriptor
.idProduct
));
1122 for(n
= 0; usbtmc_id_quirk
[n
].idVendor
> 0; n
++) {
1123 if ((usbtmc_id_quirk
[n
].idVendor
== le16_to_cpu(data
->usb_dev
->descriptor
.idVendor
)) &&
1124 (usbtmc_id_quirk
[n
].idProduct
== le16_to_cpu(data
->usb_dev
->descriptor
.idProduct
))) {
1125 dev_dbg(&intf
->dev
, "Setting this device as having the RIGOL quirk\n");
1126 data
->rigol_quirk
= 1;
1131 /* Initialize USBTMC bTag and other fields */
1133 data
->TermCharEnabled
= 0;
1134 data
->TermChar
= '\n';
1136 /* USBTMC devices have only one setting, so use that */
1137 iface_desc
= data
->intf
->cur_altsetting
;
1139 /* Find bulk in endpoint */
1140 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1141 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1143 if (usb_endpoint_is_bulk_in(endpoint
)) {
1144 data
->bulk_in
= endpoint
->bEndpointAddress
;
1145 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n",
1151 /* Find bulk out endpoint */
1152 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1153 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1155 if (usb_endpoint_is_bulk_out(endpoint
)) {
1156 data
->bulk_out
= endpoint
->bEndpointAddress
;
1157 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n",
1163 retcode
= get_capabilities(data
);
1165 dev_err(&intf
->dev
, "can't read capabilities\n");
1167 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
1168 &capability_attr_grp
);
1170 retcode
= sysfs_create_group(&intf
->dev
.kobj
, &data_attr_grp
);
1172 retcode
= usb_register_dev(intf
, &usbtmc_class
);
1174 dev_err(&intf
->dev
, "Not able to get a minor"
1175 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE
,
1177 goto error_register
;
1179 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
1184 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1185 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1186 kref_put(&data
->kref
, usbtmc_delete
);
1190 static void usbtmc_disconnect(struct usb_interface
*intf
)
1192 struct usbtmc_device_data
*data
;
1194 dev_dbg(&intf
->dev
, "usbtmc_disconnect called\n");
1196 data
= usb_get_intfdata(intf
);
1197 usb_deregister_dev(intf
, &usbtmc_class
);
1198 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1199 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1200 mutex_lock(&data
->io_mutex
);
1202 mutex_unlock(&data
->io_mutex
);
1203 kref_put(&data
->kref
, usbtmc_delete
);
1206 static int usbtmc_suspend(struct usb_interface
*intf
, pm_message_t message
)
1208 /* this driver does not have pending URBs */
1212 static int usbtmc_resume(struct usb_interface
*intf
)
1217 static struct usb_driver usbtmc_driver
= {
1219 .id_table
= usbtmc_devices
,
1220 .probe
= usbtmc_probe
,
1221 .disconnect
= usbtmc_disconnect
,
1222 .suspend
= usbtmc_suspend
,
1223 .resume
= usbtmc_resume
,
1226 module_usb_driver(usbtmc_driver
);
1228 MODULE_LICENSE("GPL");