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/module.h>
25 #include <linux/kernel.h>
27 #include <linux/uaccess.h>
28 #include <linux/kref.h>
29 #include <linux/slab.h>
30 #include <linux/poll.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 */
91 /* data for interrupt in endpoint handling */
97 atomic_t iin_data_valid
;
102 u16 iin_wMaxPacketSize
;
103 atomic_t srq_asserted
;
105 /* coalesced usb488_caps from usbtmc_dev_capabilities */
110 /* attributes from the USB TMC spec for this device */
112 bool TermCharEnabled
;
115 bool zombie
; /* fd of disconnected device */
117 struct usbtmc_dev_capabilities capabilities
;
119 struct mutex io_mutex
; /* only one i/o function running at a time */
120 wait_queue_head_t waitq
;
121 struct fasync_struct
*fasync
;
123 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
125 struct usbtmc_ID_rigol_quirk
{
130 static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk
[] = {
136 /* Forward declarations */
137 static struct usb_driver usbtmc_driver
;
139 static void usbtmc_delete(struct kref
*kref
)
141 struct usbtmc_device_data
*data
= to_usbtmc_data(kref
);
143 usb_put_dev(data
->usb_dev
);
146 static int usbtmc_open(struct inode
*inode
, struct file
*filp
)
148 struct usb_interface
*intf
;
149 struct usbtmc_device_data
*data
;
152 intf
= usb_find_interface(&usbtmc_driver
, iminor(inode
));
154 pr_err("can not find device for minor %d", iminor(inode
));
158 data
= usb_get_intfdata(intf
);
159 kref_get(&data
->kref
);
161 /* Store pointer in file structure's private data field */
162 filp
->private_data
= data
;
167 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
169 struct usbtmc_device_data
*data
= file
->private_data
;
171 kref_put(&data
->kref
, usbtmc_delete
);
175 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
182 struct usb_host_interface
*current_setting
;
185 dev
= &data
->intf
->dev
;
186 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
190 rv
= usb_control_msg(data
->usb_dev
,
191 usb_rcvctrlpipe(data
->usb_dev
, 0),
192 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
193 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
194 data
->bTag_last_read
, data
->bulk_in
,
195 buffer
, 2, USBTMC_TIMEOUT
);
198 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
202 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
204 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
209 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
210 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
217 current_setting
= data
->intf
->cur_altsetting
;
218 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++)
219 if (current_setting
->endpoint
[n
].desc
.bEndpointAddress
==
221 max_size
= usb_endpoint_maxp(¤t_setting
->endpoint
[n
].desc
);
224 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
229 dev_dbg(&data
->intf
->dev
, "wMaxPacketSize is %d\n", max_size
);
234 dev_dbg(dev
, "Reading from bulk in EP\n");
236 rv
= usb_bulk_msg(data
->usb_dev
,
237 usb_rcvbulkpipe(data
->usb_dev
,
239 buffer
, USBTMC_SIZE_IOBUFFER
,
240 &actual
, USBTMC_TIMEOUT
);
245 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
248 } while ((actual
== max_size
) &&
249 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
251 if (actual
== max_size
) {
252 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
253 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
260 usbtmc_abort_bulk_in_status
:
261 rv
= usb_control_msg(data
->usb_dev
,
262 usb_rcvctrlpipe(data
->usb_dev
, 0),
263 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
264 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
265 0, data
->bulk_in
, buffer
, 0x08,
269 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
273 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
275 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
280 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
281 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
288 dev_dbg(dev
, "Reading from bulk in EP\n");
290 rv
= usb_bulk_msg(data
->usb_dev
,
291 usb_rcvbulkpipe(data
->usb_dev
,
293 buffer
, USBTMC_SIZE_IOBUFFER
,
294 &actual
, USBTMC_TIMEOUT
);
299 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
302 } while ((actual
== max_size
) &&
303 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
305 if (actual
== max_size
) {
306 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
307 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
312 goto usbtmc_abort_bulk_in_status
;
320 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
327 dev
= &data
->intf
->dev
;
329 buffer
= kmalloc(8, GFP_KERNEL
);
333 rv
= usb_control_msg(data
->usb_dev
,
334 usb_rcvctrlpipe(data
->usb_dev
, 0),
335 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
336 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
337 data
->bTag_last_write
, data
->bulk_out
,
338 buffer
, 2, USBTMC_TIMEOUT
);
341 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
345 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
347 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
348 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
356 usbtmc_abort_bulk_out_check_status
:
357 rv
= usb_control_msg(data
->usb_dev
,
358 usb_rcvctrlpipe(data
->usb_dev
, 0),
359 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
360 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
361 0, data
->bulk_out
, buffer
, 0x08,
365 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
369 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
371 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
372 goto usbtmc_abort_bulk_out_clear_halt
;
374 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
375 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
376 goto usbtmc_abort_bulk_out_check_status
;
381 usbtmc_abort_bulk_out_clear_halt
:
382 rv
= usb_clear_halt(data
->usb_dev
,
383 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
386 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
396 static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data
*data
,
399 struct device
*dev
= &data
->intf
->dev
;
405 dev_dbg(dev
, "Enter ioctl_read_stb iin_ep_present: %d\n",
406 data
->iin_ep_present
);
408 buffer
= kmalloc(8, GFP_KERNEL
);
412 atomic_set(&data
->iin_data_valid
, 0);
414 /* must issue read_stb before using poll or select */
415 atomic_set(&data
->srq_asserted
, 0);
417 rv
= usb_control_msg(data
->usb_dev
,
418 usb_rcvctrlpipe(data
->usb_dev
, 0),
419 USBTMC488_REQUEST_READ_STATUS_BYTE
,
420 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
423 buffer
, 0x03, USBTMC_TIMEOUT
);
425 dev_err(dev
, "stb usb_control_msg returned %d\n", rv
);
429 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
430 dev_err(dev
, "control status returned %x\n", buffer
[0]);
435 if (data
->iin_ep_present
) {
436 rv
= wait_event_interruptible_timeout(
438 atomic_read(&data
->iin_data_valid
) != 0,
441 dev_dbg(dev
, "wait interrupted %d\n", rv
);
446 dev_dbg(dev
, "wait timed out\n");
451 tag
= data
->bNotify1
& 0x7f;
452 if (tag
!= data
->iin_bTag
) {
453 dev_err(dev
, "expected bTag %x got %x\n",
454 data
->iin_bTag
, tag
);
457 stb
= data
->bNotify2
;
462 rv
= copy_to_user(arg
, &stb
, sizeof(stb
));
467 /* bump interrupt bTag */
469 if (data
->iin_bTag
> 127)
470 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
477 static int usbtmc488_ioctl_simple(struct usbtmc_device_data
*data
,
478 void __user
*arg
, unsigned int cmd
)
480 struct device
*dev
= &data
->intf
->dev
;
486 if (!(data
->usb488_caps
& USBTMC488_CAPABILITY_SIMPLE
))
489 buffer
= kmalloc(8, GFP_KERNEL
);
493 if (cmd
== USBTMC488_REQUEST_REN_CONTROL
) {
494 rv
= copy_from_user(&val
, arg
, sizeof(val
));
499 wValue
= val
? 1 : 0;
504 rv
= usb_control_msg(data
->usb_dev
,
505 usb_rcvctrlpipe(data
->usb_dev
, 0),
507 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
510 buffer
, 0x01, USBTMC_TIMEOUT
);
512 dev_err(dev
, "simple usb_control_msg failed %d\n", rv
);
514 } else if (rv
!= 1) {
515 dev_warn(dev
, "simple usb_control_msg returned %d\n", rv
);
520 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
521 dev_err(dev
, "simple control status returned %x\n", buffer
[0]);
533 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
534 * @transfer_size: number of bytes to request from the device.
536 * See the USBTMC specification, Table 4.
538 * Also updates bTag_last_write.
540 static int send_request_dev_dep_msg_in(struct usbtmc_device_data
*data
, size_t transfer_size
)
546 buffer
= kmalloc(USBTMC_HEADER_SIZE
, GFP_KERNEL
);
549 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
550 * Refer to class specs for details
553 buffer
[1] = data
->bTag
;
554 buffer
[2] = ~data
->bTag
;
555 buffer
[3] = 0; /* Reserved */
556 buffer
[4] = transfer_size
>> 0;
557 buffer
[5] = transfer_size
>> 8;
558 buffer
[6] = transfer_size
>> 16;
559 buffer
[7] = transfer_size
>> 24;
560 buffer
[8] = data
->TermCharEnabled
* 2;
561 /* Use term character? */
562 buffer
[9] = data
->TermChar
;
563 buffer
[10] = 0; /* Reserved */
564 buffer
[11] = 0; /* Reserved */
567 retval
= usb_bulk_msg(data
->usb_dev
,
568 usb_sndbulkpipe(data
->usb_dev
,
570 buffer
, USBTMC_HEADER_SIZE
, &actual
, USBTMC_TIMEOUT
);
572 /* Store bTag (in case we need to abort) */
573 data
->bTag_last_write
= data
->bTag
;
575 /* Increment bTag -- and increment again if zero */
582 dev_err(&data
->intf
->dev
, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval
);
589 static ssize_t
usbtmc_read(struct file
*filp
, char __user
*buf
,
590 size_t count
, loff_t
*f_pos
)
592 struct usbtmc_device_data
*data
;
602 /* Get pointer to private data structure */
603 data
= filp
->private_data
;
604 dev
= &data
->intf
->dev
;
606 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
610 mutex_lock(&data
->io_mutex
);
616 if (data
->rigol_quirk
) {
617 dev_dbg(dev
, "usb_bulk_msg_in: count(%zu)\n", count
);
619 retval
= send_request_dev_dep_msg_in(data
, count
);
622 if (data
->auto_abort
)
623 usbtmc_ioctl_abort_bulk_out(data
);
628 /* Loop until we have fetched everything we requested */
630 this_part
= remaining
;
633 while (remaining
> 0) {
634 if (!data
->rigol_quirk
) {
635 dev_dbg(dev
, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining
, count
);
637 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3)
638 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3;
640 this_part
= remaining
;
642 retval
= send_request_dev_dep_msg_in(data
, this_part
);
644 dev_err(dev
, "usb_bulk_msg returned %d\n", retval
);
645 if (data
->auto_abort
)
646 usbtmc_ioctl_abort_bulk_out(data
);
652 retval
= usb_bulk_msg(data
->usb_dev
,
653 usb_rcvbulkpipe(data
->usb_dev
,
655 buffer
, USBTMC_SIZE_IOBUFFER
, &actual
,
658 dev_dbg(dev
, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval
, done
, remaining
, actual
);
660 /* Store bTag (in case we need to abort) */
661 data
->bTag_last_read
= data
->bTag
;
664 dev_dbg(dev
, "Unable to read data, error %d\n", retval
);
665 if (data
->auto_abort
)
666 usbtmc_ioctl_abort_bulk_in(data
);
670 /* Parse header in first packet */
671 if ((done
== 0) || !data
->rigol_quirk
) {
672 /* Sanity checks for the header */
673 if (actual
< USBTMC_HEADER_SIZE
) {
674 dev_err(dev
, "Device sent too small first packet: %u < %u\n", actual
, USBTMC_HEADER_SIZE
);
675 if (data
->auto_abort
)
676 usbtmc_ioctl_abort_bulk_in(data
);
680 if (buffer
[0] != 2) {
681 dev_err(dev
, "Device sent reply with wrong MsgID: %u != 2\n", buffer
[0]);
682 if (data
->auto_abort
)
683 usbtmc_ioctl_abort_bulk_in(data
);
687 if (buffer
[1] != data
->bTag_last_write
) {
688 dev_err(dev
, "Device sent reply with wrong bTag: %u != %u\n", buffer
[1], data
->bTag_last_write
);
689 if (data
->auto_abort
)
690 usbtmc_ioctl_abort_bulk_in(data
);
694 /* How many characters did the instrument send? */
695 n_characters
= buffer
[4] +
700 if (n_characters
> this_part
) {
701 dev_err(dev
, "Device wants to return more data than requested: %u > %zu\n", n_characters
, count
);
702 if (data
->auto_abort
)
703 usbtmc_ioctl_abort_bulk_in(data
);
707 /* Remove the USBTMC header */
708 actual
-= USBTMC_HEADER_SIZE
;
710 /* Check if the message is smaller than requested */
711 if (data
->rigol_quirk
) {
712 if (remaining
> n_characters
)
713 remaining
= n_characters
;
714 /* Remove padding if it exists */
715 if (actual
> remaining
)
719 if (this_part
> n_characters
)
720 this_part
= n_characters
;
721 /* Remove padding if it exists */
722 if (actual
> this_part
)
726 dev_dbg(dev
, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters
, buffer
[8]);
730 /* Terminate if end-of-message bit received from device */
731 if ((buffer
[8] & 0x01) && (actual
>= n_characters
))
734 dev_dbg(dev
, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining
,buf
,buffer
,done
);
737 /* Copy buffer to user space */
738 if (copy_to_user(buf
+ done
, &buffer
[USBTMC_HEADER_SIZE
], actual
)) {
739 /* There must have been an addressing problem */
746 if (actual
> remaining
)
751 dev_dbg(dev
, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual
, done
, remaining
,buf
,buffer
);
753 /* Copy buffer to user space */
754 if (copy_to_user(buf
+ done
, buffer
, actual
)) {
755 /* There must have been an addressing problem */
763 /* Update file position value */
764 *f_pos
= *f_pos
+ done
;
768 mutex_unlock(&data
->io_mutex
);
773 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
774 size_t count
, loff_t
*f_pos
)
776 struct usbtmc_device_data
*data
;
780 unsigned long int n_bytes
;
785 data
= filp
->private_data
;
787 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
791 mutex_lock(&data
->io_mutex
);
800 while (remaining
> 0) {
801 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
) {
802 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
;
805 this_part
= remaining
;
809 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
811 buffer
[1] = data
->bTag
;
812 buffer
[2] = ~data
->bTag
;
813 buffer
[3] = 0; /* Reserved */
814 buffer
[4] = this_part
>> 0;
815 buffer
[5] = this_part
>> 8;
816 buffer
[6] = this_part
>> 16;
817 buffer
[7] = this_part
>> 24;
818 /* buffer[8] is set above... */
819 buffer
[9] = 0; /* Reserved */
820 buffer
[10] = 0; /* Reserved */
821 buffer
[11] = 0; /* Reserved */
823 if (copy_from_user(&buffer
[USBTMC_HEADER_SIZE
], buf
+ done
, this_part
)) {
828 n_bytes
= roundup(USBTMC_HEADER_SIZE
+ this_part
, 4);
829 memset(buffer
+ USBTMC_HEADER_SIZE
+ this_part
, 0, n_bytes
- (USBTMC_HEADER_SIZE
+ this_part
));
832 retval
= usb_bulk_msg(data
->usb_dev
,
833 usb_sndbulkpipe(data
->usb_dev
,
836 &actual
, USBTMC_TIMEOUT
);
842 data
->bTag_last_write
= data
->bTag
;
849 dev_err(&data
->intf
->dev
,
850 "Unable to send data, error %d\n", retval
);
851 if (data
->auto_abort
)
852 usbtmc_ioctl_abort_bulk_out(data
);
856 remaining
-= this_part
;
862 mutex_unlock(&data
->io_mutex
);
867 static int usbtmc_ioctl_clear(struct usbtmc_device_data
*data
)
869 struct usb_host_interface
*current_setting
;
870 struct usb_endpoint_descriptor
*desc
;
878 dev
= &data
->intf
->dev
;
880 dev_dbg(dev
, "Sending INITIATE_CLEAR request\n");
882 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
886 rv
= usb_control_msg(data
->usb_dev
,
887 usb_rcvctrlpipe(data
->usb_dev
, 0),
888 USBTMC_REQUEST_INITIATE_CLEAR
,
889 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
890 0, 0, buffer
, 1, USBTMC_TIMEOUT
);
892 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
896 dev_dbg(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
898 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
899 dev_err(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
905 current_setting
= data
->intf
->cur_altsetting
;
906 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++) {
907 desc
= ¤t_setting
->endpoint
[n
].desc
;
908 if (desc
->bEndpointAddress
== data
->bulk_in
)
909 max_size
= usb_endpoint_maxp(desc
);
913 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
918 dev_dbg(dev
, "wMaxPacketSize is %d\n", max_size
);
922 usbtmc_clear_check_status
:
924 dev_dbg(dev
, "Sending CHECK_CLEAR_STATUS request\n");
926 rv
= usb_control_msg(data
->usb_dev
,
927 usb_rcvctrlpipe(data
->usb_dev
, 0),
928 USBTMC_REQUEST_CHECK_CLEAR_STATUS
,
929 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
930 0, 0, buffer
, 2, USBTMC_TIMEOUT
);
932 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
936 dev_dbg(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
938 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
939 goto usbtmc_clear_bulk_out_halt
;
941 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
942 dev_err(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
949 dev_dbg(dev
, "Reading from bulk in EP\n");
951 rv
= usb_bulk_msg(data
->usb_dev
,
952 usb_rcvbulkpipe(data
->usb_dev
,
954 buffer
, USBTMC_SIZE_IOBUFFER
,
955 &actual
, USBTMC_TIMEOUT
);
959 dev_err(dev
, "usb_control_msg returned %d\n",
963 } while ((actual
== max_size
) &&
964 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
966 if (actual
== max_size
) {
967 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
968 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
973 goto usbtmc_clear_check_status
;
975 usbtmc_clear_bulk_out_halt
:
977 rv
= usb_clear_halt(data
->usb_dev
,
978 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
980 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
990 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data
*data
)
994 rv
= usb_clear_halt(data
->usb_dev
,
995 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
998 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
1005 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data
*data
)
1009 rv
= usb_clear_halt(data
->usb_dev
,
1010 usb_rcvbulkpipe(data
->usb_dev
, data
->bulk_in
));
1013 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
1020 static int get_capabilities(struct usbtmc_device_data
*data
)
1022 struct device
*dev
= &data
->usb_dev
->dev
;
1026 buffer
= kmalloc(0x18, GFP_KERNEL
);
1030 rv
= usb_control_msg(data
->usb_dev
, usb_rcvctrlpipe(data
->usb_dev
, 0),
1031 USBTMC_REQUEST_GET_CAPABILITIES
,
1032 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1033 0, 0, buffer
, 0x18, USBTMC_TIMEOUT
);
1035 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1039 dev_dbg(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
1040 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1041 dev_err(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
1045 dev_dbg(dev
, "Interface capabilities are %x\n", buffer
[4]);
1046 dev_dbg(dev
, "Device capabilities are %x\n", buffer
[5]);
1047 dev_dbg(dev
, "USB488 interface capabilities are %x\n", buffer
[14]);
1048 dev_dbg(dev
, "USB488 device capabilities are %x\n", buffer
[15]);
1050 data
->capabilities
.interface_capabilities
= buffer
[4];
1051 data
->capabilities
.device_capabilities
= buffer
[5];
1052 data
->capabilities
.usb488_interface_capabilities
= buffer
[14];
1053 data
->capabilities
.usb488_device_capabilities
= buffer
[15];
1054 data
->usb488_caps
= (buffer
[14] & 0x07) | ((buffer
[15] & 0x0f) << 4);
1062 #define capability_attribute(name) \
1063 static ssize_t name##_show(struct device *dev, \
1064 struct device_attribute *attr, char *buf) \
1066 struct usb_interface *intf = to_usb_interface(dev); \
1067 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1069 return sprintf(buf, "%d\n", data->capabilities.name); \
1071 static DEVICE_ATTR_RO(name)
1073 capability_attribute(interface_capabilities
);
1074 capability_attribute(device_capabilities
);
1075 capability_attribute(usb488_interface_capabilities
);
1076 capability_attribute(usb488_device_capabilities
);
1078 static struct attribute
*capability_attrs
[] = {
1079 &dev_attr_interface_capabilities
.attr
,
1080 &dev_attr_device_capabilities
.attr
,
1081 &dev_attr_usb488_interface_capabilities
.attr
,
1082 &dev_attr_usb488_device_capabilities
.attr
,
1086 static struct attribute_group capability_attr_grp
= {
1087 .attrs
= capability_attrs
,
1090 static ssize_t
TermChar_show(struct device
*dev
,
1091 struct device_attribute
*attr
, char *buf
)
1093 struct usb_interface
*intf
= to_usb_interface(dev
);
1094 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
1096 return sprintf(buf
, "%c\n", data
->TermChar
);
1099 static ssize_t
TermChar_store(struct device
*dev
,
1100 struct device_attribute
*attr
,
1101 const char *buf
, size_t count
)
1103 struct usb_interface
*intf
= to_usb_interface(dev
);
1104 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
1108 data
->TermChar
= buf
[0];
1111 static DEVICE_ATTR_RW(TermChar
);
1113 #define data_attribute(name) \
1114 static ssize_t name##_show(struct device *dev, \
1115 struct device_attribute *attr, char *buf) \
1117 struct usb_interface *intf = to_usb_interface(dev); \
1118 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1120 return sprintf(buf, "%d\n", data->name); \
1122 static ssize_t name##_store(struct device *dev, \
1123 struct device_attribute *attr, \
1124 const char *buf, size_t count) \
1126 struct usb_interface *intf = to_usb_interface(dev); \
1127 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1131 result = sscanf(buf, "%u\n", &val); \
1140 static DEVICE_ATTR_RW(name)
1142 data_attribute(TermCharEnabled
);
1143 data_attribute(auto_abort
);
1145 static struct attribute
*data_attrs
[] = {
1146 &dev_attr_TermChar
.attr
,
1147 &dev_attr_TermCharEnabled
.attr
,
1148 &dev_attr_auto_abort
.attr
,
1152 static struct attribute_group data_attr_grp
= {
1153 .attrs
= data_attrs
,
1156 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
1162 dev
= &data
->intf
->dev
;
1164 buffer
= kmalloc(2, GFP_KERNEL
);
1168 rv
= usb_control_msg(data
->usb_dev
,
1169 usb_rcvctrlpipe(data
->usb_dev
, 0),
1170 USBTMC_REQUEST_INDICATOR_PULSE
,
1171 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1172 0, 0, buffer
, 0x01, USBTMC_TIMEOUT
);
1175 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1179 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1181 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1182 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1193 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1195 struct usbtmc_device_data
*data
;
1196 int retval
= -EBADRQC
;
1198 data
= file
->private_data
;
1199 mutex_lock(&data
->io_mutex
);
1202 goto skip_io_on_zombie
;
1206 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
1207 retval
= usbtmc_ioctl_clear_out_halt(data
);
1210 case USBTMC_IOCTL_CLEAR_IN_HALT
:
1211 retval
= usbtmc_ioctl_clear_in_halt(data
);
1214 case USBTMC_IOCTL_INDICATOR_PULSE
:
1215 retval
= usbtmc_ioctl_indicator_pulse(data
);
1218 case USBTMC_IOCTL_CLEAR
:
1219 retval
= usbtmc_ioctl_clear(data
);
1222 case USBTMC_IOCTL_ABORT_BULK_OUT
:
1223 retval
= usbtmc_ioctl_abort_bulk_out(data
);
1226 case USBTMC_IOCTL_ABORT_BULK_IN
:
1227 retval
= usbtmc_ioctl_abort_bulk_in(data
);
1230 case USBTMC488_IOCTL_GET_CAPS
:
1231 retval
= copy_to_user((void __user
*)arg
,
1233 sizeof(data
->usb488_caps
));
1238 case USBTMC488_IOCTL_READ_STB
:
1239 retval
= usbtmc488_ioctl_read_stb(data
, (void __user
*)arg
);
1242 case USBTMC488_IOCTL_REN_CONTROL
:
1243 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1244 USBTMC488_REQUEST_REN_CONTROL
);
1247 case USBTMC488_IOCTL_GOTO_LOCAL
:
1248 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1249 USBTMC488_REQUEST_GOTO_LOCAL
);
1252 case USBTMC488_IOCTL_LOCAL_LOCKOUT
:
1253 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1254 USBTMC488_REQUEST_LOCAL_LOCKOUT
);
1259 mutex_unlock(&data
->io_mutex
);
1263 static int usbtmc_fasync(int fd
, struct file
*file
, int on
)
1265 struct usbtmc_device_data
*data
= file
->private_data
;
1267 return fasync_helper(fd
, file
, on
, &data
->fasync
);
1270 static unsigned int usbtmc_poll(struct file
*file
, poll_table
*wait
)
1272 struct usbtmc_device_data
*data
= file
->private_data
;
1275 mutex_lock(&data
->io_mutex
);
1278 mask
= POLLHUP
| POLLERR
;
1282 poll_wait(file
, &data
->waitq
, wait
);
1284 mask
= (atomic_read(&data
->srq_asserted
)) ? POLLIN
| POLLRDNORM
: 0;
1287 mutex_unlock(&data
->io_mutex
);
1291 static const struct file_operations fops
= {
1292 .owner
= THIS_MODULE
,
1293 .read
= usbtmc_read
,
1294 .write
= usbtmc_write
,
1295 .open
= usbtmc_open
,
1296 .release
= usbtmc_release
,
1297 .unlocked_ioctl
= usbtmc_ioctl
,
1298 .fasync
= usbtmc_fasync
,
1299 .poll
= usbtmc_poll
,
1300 .llseek
= default_llseek
,
1303 static struct usb_class_driver usbtmc_class
= {
1306 .minor_base
= USBTMC_MINOR_BASE
,
1309 static void usbtmc_interrupt(struct urb
*urb
)
1311 struct usbtmc_device_data
*data
= urb
->context
;
1312 struct device
*dev
= &data
->intf
->dev
;
1313 int status
= urb
->status
;
1316 dev_dbg(&data
->intf
->dev
, "int status: %d len %d\n",
1317 status
, urb
->actual_length
);
1320 case 0: /* SUCCESS */
1321 /* check for valid STB notification */
1322 if (data
->iin_buffer
[0] > 0x81) {
1323 data
->bNotify1
= data
->iin_buffer
[0];
1324 data
->bNotify2
= data
->iin_buffer
[1];
1325 atomic_set(&data
->iin_data_valid
, 1);
1326 wake_up_interruptible(&data
->waitq
);
1329 /* check for SRQ notification */
1330 if (data
->iin_buffer
[0] == 0x81) {
1332 kill_fasync(&data
->fasync
,
1335 atomic_set(&data
->srq_asserted
, 1);
1336 wake_up_interruptible(&data
->waitq
);
1339 dev_warn(dev
, "invalid notification: %x\n", data
->iin_buffer
[0]);
1342 dev_err(dev
, "overflow with length %d, actual length is %d\n",
1343 data
->iin_wMaxPacketSize
, urb
->actual_length
);
1349 /* urb terminated, clean up */
1350 dev_dbg(dev
, "urb terminated, status: %d\n", status
);
1353 dev_err(dev
, "unknown status received: %d\n", status
);
1356 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
1358 dev_err(dev
, "usb_submit_urb failed: %d\n", rv
);
1361 static void usbtmc_free_int(struct usbtmc_device_data
*data
)
1363 if (!data
->iin_ep_present
|| !data
->iin_urb
)
1365 usb_kill_urb(data
->iin_urb
);
1366 kfree(data
->iin_buffer
);
1367 usb_free_urb(data
->iin_urb
);
1368 kref_put(&data
->kref
, usbtmc_delete
);
1371 static int usbtmc_probe(struct usb_interface
*intf
,
1372 const struct usb_device_id
*id
)
1374 struct usbtmc_device_data
*data
;
1375 struct usb_host_interface
*iface_desc
;
1376 struct usb_endpoint_descriptor
*endpoint
;
1380 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
1382 data
= devm_kzalloc(&intf
->dev
, sizeof(*data
), GFP_KERNEL
);
1388 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
1389 usb_set_intfdata(intf
, data
);
1390 kref_init(&data
->kref
);
1391 mutex_init(&data
->io_mutex
);
1392 init_waitqueue_head(&data
->waitq
);
1393 atomic_set(&data
->iin_data_valid
, 0);
1394 atomic_set(&data
->srq_asserted
, 0);
1397 /* Determine if it is a Rigol or not */
1398 data
->rigol_quirk
= 0;
1399 dev_dbg(&intf
->dev
, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1400 le16_to_cpu(data
->usb_dev
->descriptor
.idVendor
),
1401 le16_to_cpu(data
->usb_dev
->descriptor
.idProduct
));
1402 for(n
= 0; usbtmc_id_quirk
[n
].idVendor
> 0; n
++) {
1403 if ((usbtmc_id_quirk
[n
].idVendor
== le16_to_cpu(data
->usb_dev
->descriptor
.idVendor
)) &&
1404 (usbtmc_id_quirk
[n
].idProduct
== le16_to_cpu(data
->usb_dev
->descriptor
.idProduct
))) {
1405 dev_dbg(&intf
->dev
, "Setting this device as having the RIGOL quirk\n");
1406 data
->rigol_quirk
= 1;
1411 /* Initialize USBTMC bTag and other fields */
1413 data
->TermCharEnabled
= 0;
1414 data
->TermChar
= '\n';
1415 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
1418 /* USBTMC devices have only one setting, so use that */
1419 iface_desc
= data
->intf
->cur_altsetting
;
1420 data
->ifnum
= iface_desc
->desc
.bInterfaceNumber
;
1422 /* Find bulk in endpoint */
1423 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1424 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1426 if (usb_endpoint_is_bulk_in(endpoint
)) {
1427 data
->bulk_in
= endpoint
->bEndpointAddress
;
1428 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n",
1434 /* Find bulk out endpoint */
1435 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1436 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1438 if (usb_endpoint_is_bulk_out(endpoint
)) {
1439 data
->bulk_out
= endpoint
->bEndpointAddress
;
1440 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n",
1445 /* Find int endpoint */
1446 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1447 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1449 if (usb_endpoint_is_int_in(endpoint
)) {
1450 data
->iin_ep_present
= 1;
1451 data
->iin_ep
= endpoint
->bEndpointAddress
;
1452 data
->iin_wMaxPacketSize
= usb_endpoint_maxp(endpoint
);
1453 data
->iin_interval
= endpoint
->bInterval
;
1454 dev_dbg(&intf
->dev
, "Found Int in endpoint at %u\n",
1460 retcode
= get_capabilities(data
);
1462 dev_err(&intf
->dev
, "can't read capabilities\n");
1464 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
1465 &capability_attr_grp
);
1467 if (data
->iin_ep_present
) {
1468 /* allocate int urb */
1469 data
->iin_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1470 if (!data
->iin_urb
) {
1471 dev_err(&intf
->dev
, "Failed to allocate int urb\n");
1472 goto error_register
;
1475 /* will reference data in int urb */
1476 kref_get(&data
->kref
);
1478 /* allocate buffer for interrupt in */
1479 data
->iin_buffer
= kmalloc(data
->iin_wMaxPacketSize
,
1481 if (!data
->iin_buffer
) {
1482 dev_err(&intf
->dev
, "Failed to allocate int buf\n");
1483 goto error_register
;
1486 /* fill interrupt urb */
1487 usb_fill_int_urb(data
->iin_urb
, data
->usb_dev
,
1488 usb_rcvintpipe(data
->usb_dev
, data
->iin_ep
),
1489 data
->iin_buffer
, data
->iin_wMaxPacketSize
,
1491 data
, data
->iin_interval
);
1493 retcode
= usb_submit_urb(data
->iin_urb
, GFP_KERNEL
);
1495 dev_err(&intf
->dev
, "Failed to submit iin_urb\n");
1496 goto error_register
;
1500 retcode
= sysfs_create_group(&intf
->dev
.kobj
, &data_attr_grp
);
1502 retcode
= usb_register_dev(intf
, &usbtmc_class
);
1504 dev_err(&intf
->dev
, "Not able to get a minor"
1505 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE
,
1507 goto error_register
;
1509 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
1514 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1515 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1516 usbtmc_free_int(data
);
1517 kref_put(&data
->kref
, usbtmc_delete
);
1521 static void usbtmc_disconnect(struct usb_interface
*intf
)
1523 struct usbtmc_device_data
*data
;
1525 dev_dbg(&intf
->dev
, "usbtmc_disconnect called\n");
1527 data
= usb_get_intfdata(intf
);
1528 usb_deregister_dev(intf
, &usbtmc_class
);
1529 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1530 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1531 mutex_lock(&data
->io_mutex
);
1533 wake_up_all(&data
->waitq
);
1534 mutex_unlock(&data
->io_mutex
);
1535 usbtmc_free_int(data
);
1536 kref_put(&data
->kref
, usbtmc_delete
);
1539 static int usbtmc_suspend(struct usb_interface
*intf
, pm_message_t message
)
1541 /* this driver does not have pending URBs */
1545 static int usbtmc_resume(struct usb_interface
*intf
)
1550 static struct usb_driver usbtmc_driver
= {
1552 .id_table
= usbtmc_devices
,
1553 .probe
= usbtmc_probe
,
1554 .disconnect
= usbtmc_disconnect
,
1555 .suspend
= usbtmc_suspend
,
1556 .resume
= usbtmc_resume
,
1559 module_usb_driver(usbtmc_driver
);
1561 MODULE_LICENSE("GPL");