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
);
147 static int usbtmc_open(struct inode
*inode
, struct file
*filp
)
149 struct usb_interface
*intf
;
150 struct usbtmc_device_data
*data
;
153 intf
= usb_find_interface(&usbtmc_driver
, iminor(inode
));
155 pr_err("can not find device for minor %d", iminor(inode
));
159 data
= usb_get_intfdata(intf
);
160 /* Protect reference to data from file structure until release */
161 kref_get(&data
->kref
);
163 /* Store pointer in file structure's private data field */
164 filp
->private_data
= data
;
169 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
171 struct usbtmc_device_data
*data
= file
->private_data
;
173 kref_put(&data
->kref
, usbtmc_delete
);
177 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
184 struct usb_host_interface
*current_setting
;
187 dev
= &data
->intf
->dev
;
188 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
192 rv
= usb_control_msg(data
->usb_dev
,
193 usb_rcvctrlpipe(data
->usb_dev
, 0),
194 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
195 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
196 data
->bTag_last_read
, data
->bulk_in
,
197 buffer
, 2, USBTMC_TIMEOUT
);
200 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
204 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
206 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
211 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
212 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
219 current_setting
= data
->intf
->cur_altsetting
;
220 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++)
221 if (current_setting
->endpoint
[n
].desc
.bEndpointAddress
==
223 max_size
= usb_endpoint_maxp(¤t_setting
->endpoint
[n
].desc
);
226 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
231 dev_dbg(&data
->intf
->dev
, "wMaxPacketSize is %d\n", max_size
);
236 dev_dbg(dev
, "Reading from bulk in EP\n");
238 rv
= usb_bulk_msg(data
->usb_dev
,
239 usb_rcvbulkpipe(data
->usb_dev
,
241 buffer
, USBTMC_SIZE_IOBUFFER
,
242 &actual
, USBTMC_TIMEOUT
);
247 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
250 } while ((actual
== max_size
) &&
251 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
253 if (actual
== max_size
) {
254 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
255 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
262 usbtmc_abort_bulk_in_status
:
263 rv
= usb_control_msg(data
->usb_dev
,
264 usb_rcvctrlpipe(data
->usb_dev
, 0),
265 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
266 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
267 0, data
->bulk_in
, buffer
, 0x08,
271 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
275 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
277 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
282 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
283 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
290 dev_dbg(dev
, "Reading from bulk in EP\n");
292 rv
= usb_bulk_msg(data
->usb_dev
,
293 usb_rcvbulkpipe(data
->usb_dev
,
295 buffer
, USBTMC_SIZE_IOBUFFER
,
296 &actual
, USBTMC_TIMEOUT
);
301 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
304 } while ((actual
== max_size
) &&
305 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
307 if (actual
== max_size
) {
308 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
309 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
314 goto usbtmc_abort_bulk_in_status
;
322 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
329 dev
= &data
->intf
->dev
;
331 buffer
= kmalloc(8, GFP_KERNEL
);
335 rv
= usb_control_msg(data
->usb_dev
,
336 usb_rcvctrlpipe(data
->usb_dev
, 0),
337 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
338 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
339 data
->bTag_last_write
, data
->bulk_out
,
340 buffer
, 2, USBTMC_TIMEOUT
);
343 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
347 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
349 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
350 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
358 usbtmc_abort_bulk_out_check_status
:
359 rv
= usb_control_msg(data
->usb_dev
,
360 usb_rcvctrlpipe(data
->usb_dev
, 0),
361 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
362 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
363 0, data
->bulk_out
, buffer
, 0x08,
367 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
371 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
373 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
374 goto usbtmc_abort_bulk_out_clear_halt
;
376 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
377 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
378 goto usbtmc_abort_bulk_out_check_status
;
383 usbtmc_abort_bulk_out_clear_halt
:
384 rv
= usb_clear_halt(data
->usb_dev
,
385 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
388 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
398 static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data
*data
,
401 struct device
*dev
= &data
->intf
->dev
;
407 dev_dbg(dev
, "Enter ioctl_read_stb iin_ep_present: %d\n",
408 data
->iin_ep_present
);
410 buffer
= kmalloc(8, GFP_KERNEL
);
414 atomic_set(&data
->iin_data_valid
, 0);
416 /* must issue read_stb before using poll or select */
417 atomic_set(&data
->srq_asserted
, 0);
419 rv
= usb_control_msg(data
->usb_dev
,
420 usb_rcvctrlpipe(data
->usb_dev
, 0),
421 USBTMC488_REQUEST_READ_STATUS_BYTE
,
422 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
425 buffer
, 0x03, USBTMC_TIMEOUT
);
427 dev_err(dev
, "stb usb_control_msg returned %d\n", rv
);
431 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
432 dev_err(dev
, "control status returned %x\n", buffer
[0]);
437 if (data
->iin_ep_present
) {
438 rv
= wait_event_interruptible_timeout(
440 atomic_read(&data
->iin_data_valid
) != 0,
443 dev_dbg(dev
, "wait interrupted %d\n", rv
);
448 dev_dbg(dev
, "wait timed out\n");
453 tag
= data
->bNotify1
& 0x7f;
454 if (tag
!= data
->iin_bTag
) {
455 dev_err(dev
, "expected bTag %x got %x\n",
456 data
->iin_bTag
, tag
);
459 stb
= data
->bNotify2
;
464 rv
= copy_to_user(arg
, &stb
, sizeof(stb
));
469 /* bump interrupt bTag */
471 if (data
->iin_bTag
> 127)
472 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
479 static int usbtmc488_ioctl_simple(struct usbtmc_device_data
*data
,
480 void __user
*arg
, unsigned int cmd
)
482 struct device
*dev
= &data
->intf
->dev
;
488 if (!(data
->usb488_caps
& USBTMC488_CAPABILITY_SIMPLE
))
491 buffer
= kmalloc(8, GFP_KERNEL
);
495 if (cmd
== USBTMC488_REQUEST_REN_CONTROL
) {
496 rv
= copy_from_user(&val
, arg
, sizeof(val
));
501 wValue
= val
? 1 : 0;
506 rv
= usb_control_msg(data
->usb_dev
,
507 usb_rcvctrlpipe(data
->usb_dev
, 0),
509 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
512 buffer
, 0x01, USBTMC_TIMEOUT
);
514 dev_err(dev
, "simple usb_control_msg failed %d\n", rv
);
516 } else if (rv
!= 1) {
517 dev_warn(dev
, "simple usb_control_msg returned %d\n", rv
);
522 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
523 dev_err(dev
, "simple control status returned %x\n", buffer
[0]);
535 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-OUT endpoint.
536 * @transfer_size: number of bytes to request from the device.
538 * See the USBTMC specification, Table 4.
540 * Also updates bTag_last_write.
542 static int send_request_dev_dep_msg_in(struct usbtmc_device_data
*data
, size_t transfer_size
)
548 buffer
= kmalloc(USBTMC_HEADER_SIZE
, GFP_KERNEL
);
551 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
552 * Refer to class specs for details
555 buffer
[1] = data
->bTag
;
556 buffer
[2] = ~data
->bTag
;
557 buffer
[3] = 0; /* Reserved */
558 buffer
[4] = transfer_size
>> 0;
559 buffer
[5] = transfer_size
>> 8;
560 buffer
[6] = transfer_size
>> 16;
561 buffer
[7] = transfer_size
>> 24;
562 buffer
[8] = data
->TermCharEnabled
* 2;
563 /* Use term character? */
564 buffer
[9] = data
->TermChar
;
565 buffer
[10] = 0; /* Reserved */
566 buffer
[11] = 0; /* Reserved */
569 retval
= usb_bulk_msg(data
->usb_dev
,
570 usb_sndbulkpipe(data
->usb_dev
,
572 buffer
, USBTMC_HEADER_SIZE
, &actual
, USBTMC_TIMEOUT
);
574 /* Store bTag (in case we need to abort) */
575 data
->bTag_last_write
= data
->bTag
;
577 /* Increment bTag -- and increment again if zero */
584 dev_err(&data
->intf
->dev
, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval
);
591 static ssize_t
usbtmc_read(struct file
*filp
, char __user
*buf
,
592 size_t count
, loff_t
*f_pos
)
594 struct usbtmc_device_data
*data
;
604 /* Get pointer to private data structure */
605 data
= filp
->private_data
;
606 dev
= &data
->intf
->dev
;
608 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
612 mutex_lock(&data
->io_mutex
);
618 if (data
->rigol_quirk
) {
619 dev_dbg(dev
, "usb_bulk_msg_in: count(%zu)\n", count
);
621 retval
= send_request_dev_dep_msg_in(data
, count
);
624 if (data
->auto_abort
)
625 usbtmc_ioctl_abort_bulk_out(data
);
630 /* Loop until we have fetched everything we requested */
632 this_part
= remaining
;
635 while (remaining
> 0) {
636 if (!data
->rigol_quirk
) {
637 dev_dbg(dev
, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining
, count
);
639 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3)
640 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3;
642 this_part
= remaining
;
644 retval
= send_request_dev_dep_msg_in(data
, this_part
);
646 dev_err(dev
, "usb_bulk_msg returned %d\n", retval
);
647 if (data
->auto_abort
)
648 usbtmc_ioctl_abort_bulk_out(data
);
654 retval
= usb_bulk_msg(data
->usb_dev
,
655 usb_rcvbulkpipe(data
->usb_dev
,
657 buffer
, USBTMC_SIZE_IOBUFFER
, &actual
,
660 dev_dbg(dev
, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval
, done
, remaining
, actual
);
662 /* Store bTag (in case we need to abort) */
663 data
->bTag_last_read
= data
->bTag
;
666 dev_dbg(dev
, "Unable to read data, error %d\n", retval
);
667 if (data
->auto_abort
)
668 usbtmc_ioctl_abort_bulk_in(data
);
672 /* Parse header in first packet */
673 if ((done
== 0) || !data
->rigol_quirk
) {
674 /* Sanity checks for the header */
675 if (actual
< USBTMC_HEADER_SIZE
) {
676 dev_err(dev
, "Device sent too small first packet: %u < %u\n", actual
, USBTMC_HEADER_SIZE
);
677 if (data
->auto_abort
)
678 usbtmc_ioctl_abort_bulk_in(data
);
682 if (buffer
[0] != 2) {
683 dev_err(dev
, "Device sent reply with wrong MsgID: %u != 2\n", buffer
[0]);
684 if (data
->auto_abort
)
685 usbtmc_ioctl_abort_bulk_in(data
);
689 if (buffer
[1] != data
->bTag_last_write
) {
690 dev_err(dev
, "Device sent reply with wrong bTag: %u != %u\n", buffer
[1], data
->bTag_last_write
);
691 if (data
->auto_abort
)
692 usbtmc_ioctl_abort_bulk_in(data
);
696 /* How many characters did the instrument send? */
697 n_characters
= buffer
[4] +
702 if (n_characters
> this_part
) {
703 dev_err(dev
, "Device wants to return more data than requested: %u > %zu\n", n_characters
, count
);
704 if (data
->auto_abort
)
705 usbtmc_ioctl_abort_bulk_in(data
);
709 /* Remove the USBTMC header */
710 actual
-= USBTMC_HEADER_SIZE
;
712 /* Check if the message is smaller than requested */
713 if (data
->rigol_quirk
) {
714 if (remaining
> n_characters
)
715 remaining
= n_characters
;
716 /* Remove padding if it exists */
717 if (actual
> remaining
)
721 if (this_part
> n_characters
)
722 this_part
= n_characters
;
723 /* Remove padding if it exists */
724 if (actual
> this_part
)
728 dev_dbg(dev
, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters
, buffer
[8]);
732 /* Terminate if end-of-message bit received from device */
733 if ((buffer
[8] & 0x01) && (actual
>= n_characters
))
736 dev_dbg(dev
, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining
,buf
,buffer
,done
);
739 /* Copy buffer to user space */
740 if (copy_to_user(buf
+ done
, &buffer
[USBTMC_HEADER_SIZE
], actual
)) {
741 /* There must have been an addressing problem */
748 if (actual
> remaining
)
753 dev_dbg(dev
, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual
, done
, remaining
,buf
,buffer
);
755 /* Copy buffer to user space */
756 if (copy_to_user(buf
+ done
, buffer
, actual
)) {
757 /* There must have been an addressing problem */
765 /* Update file position value */
766 *f_pos
= *f_pos
+ done
;
770 mutex_unlock(&data
->io_mutex
);
775 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
776 size_t count
, loff_t
*f_pos
)
778 struct usbtmc_device_data
*data
;
782 unsigned long int n_bytes
;
787 data
= filp
->private_data
;
789 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
793 mutex_lock(&data
->io_mutex
);
802 while (remaining
> 0) {
803 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
) {
804 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
;
807 this_part
= remaining
;
811 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
813 buffer
[1] = data
->bTag
;
814 buffer
[2] = ~data
->bTag
;
815 buffer
[3] = 0; /* Reserved */
816 buffer
[4] = this_part
>> 0;
817 buffer
[5] = this_part
>> 8;
818 buffer
[6] = this_part
>> 16;
819 buffer
[7] = this_part
>> 24;
820 /* buffer[8] is set above... */
821 buffer
[9] = 0; /* Reserved */
822 buffer
[10] = 0; /* Reserved */
823 buffer
[11] = 0; /* Reserved */
825 if (copy_from_user(&buffer
[USBTMC_HEADER_SIZE
], buf
+ done
, this_part
)) {
830 n_bytes
= roundup(USBTMC_HEADER_SIZE
+ this_part
, 4);
831 memset(buffer
+ USBTMC_HEADER_SIZE
+ this_part
, 0, n_bytes
- (USBTMC_HEADER_SIZE
+ this_part
));
834 retval
= usb_bulk_msg(data
->usb_dev
,
835 usb_sndbulkpipe(data
->usb_dev
,
838 &actual
, USBTMC_TIMEOUT
);
844 data
->bTag_last_write
= data
->bTag
;
851 dev_err(&data
->intf
->dev
,
852 "Unable to send data, error %d\n", retval
);
853 if (data
->auto_abort
)
854 usbtmc_ioctl_abort_bulk_out(data
);
858 remaining
-= this_part
;
864 mutex_unlock(&data
->io_mutex
);
869 static int usbtmc_ioctl_clear(struct usbtmc_device_data
*data
)
871 struct usb_host_interface
*current_setting
;
872 struct usb_endpoint_descriptor
*desc
;
880 dev
= &data
->intf
->dev
;
882 dev_dbg(dev
, "Sending INITIATE_CLEAR request\n");
884 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
888 rv
= usb_control_msg(data
->usb_dev
,
889 usb_rcvctrlpipe(data
->usb_dev
, 0),
890 USBTMC_REQUEST_INITIATE_CLEAR
,
891 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
892 0, 0, buffer
, 1, USBTMC_TIMEOUT
);
894 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
898 dev_dbg(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
900 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
901 dev_err(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
907 current_setting
= data
->intf
->cur_altsetting
;
908 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++) {
909 desc
= ¤t_setting
->endpoint
[n
].desc
;
910 if (desc
->bEndpointAddress
== data
->bulk_in
)
911 max_size
= usb_endpoint_maxp(desc
);
915 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
920 dev_dbg(dev
, "wMaxPacketSize is %d\n", max_size
);
924 usbtmc_clear_check_status
:
926 dev_dbg(dev
, "Sending CHECK_CLEAR_STATUS request\n");
928 rv
= usb_control_msg(data
->usb_dev
,
929 usb_rcvctrlpipe(data
->usb_dev
, 0),
930 USBTMC_REQUEST_CHECK_CLEAR_STATUS
,
931 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
932 0, 0, buffer
, 2, USBTMC_TIMEOUT
);
934 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
938 dev_dbg(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
940 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
941 goto usbtmc_clear_bulk_out_halt
;
943 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
944 dev_err(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
951 dev_dbg(dev
, "Reading from bulk in EP\n");
953 rv
= usb_bulk_msg(data
->usb_dev
,
954 usb_rcvbulkpipe(data
->usb_dev
,
956 buffer
, USBTMC_SIZE_IOBUFFER
,
957 &actual
, USBTMC_TIMEOUT
);
961 dev_err(dev
, "usb_control_msg returned %d\n",
965 } while ((actual
== max_size
) &&
966 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
968 if (actual
== max_size
) {
969 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
970 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
975 goto usbtmc_clear_check_status
;
977 usbtmc_clear_bulk_out_halt
:
979 rv
= usb_clear_halt(data
->usb_dev
,
980 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
982 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
992 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data
*data
)
996 rv
= usb_clear_halt(data
->usb_dev
,
997 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
1000 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
1007 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data
*data
)
1011 rv
= usb_clear_halt(data
->usb_dev
,
1012 usb_rcvbulkpipe(data
->usb_dev
, data
->bulk_in
));
1015 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
1022 static int get_capabilities(struct usbtmc_device_data
*data
)
1024 struct device
*dev
= &data
->usb_dev
->dev
;
1028 buffer
= kmalloc(0x18, GFP_KERNEL
);
1032 rv
= usb_control_msg(data
->usb_dev
, usb_rcvctrlpipe(data
->usb_dev
, 0),
1033 USBTMC_REQUEST_GET_CAPABILITIES
,
1034 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1035 0, 0, buffer
, 0x18, USBTMC_TIMEOUT
);
1037 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1041 dev_dbg(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
1042 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1043 dev_err(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
1047 dev_dbg(dev
, "Interface capabilities are %x\n", buffer
[4]);
1048 dev_dbg(dev
, "Device capabilities are %x\n", buffer
[5]);
1049 dev_dbg(dev
, "USB488 interface capabilities are %x\n", buffer
[14]);
1050 dev_dbg(dev
, "USB488 device capabilities are %x\n", buffer
[15]);
1052 data
->capabilities
.interface_capabilities
= buffer
[4];
1053 data
->capabilities
.device_capabilities
= buffer
[5];
1054 data
->capabilities
.usb488_interface_capabilities
= buffer
[14];
1055 data
->capabilities
.usb488_device_capabilities
= buffer
[15];
1056 data
->usb488_caps
= (buffer
[14] & 0x07) | ((buffer
[15] & 0x0f) << 4);
1064 #define capability_attribute(name) \
1065 static ssize_t name##_show(struct device *dev, \
1066 struct device_attribute *attr, char *buf) \
1068 struct usb_interface *intf = to_usb_interface(dev); \
1069 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1071 return sprintf(buf, "%d\n", data->capabilities.name); \
1073 static DEVICE_ATTR_RO(name)
1075 capability_attribute(interface_capabilities
);
1076 capability_attribute(device_capabilities
);
1077 capability_attribute(usb488_interface_capabilities
);
1078 capability_attribute(usb488_device_capabilities
);
1080 static struct attribute
*capability_attrs
[] = {
1081 &dev_attr_interface_capabilities
.attr
,
1082 &dev_attr_device_capabilities
.attr
,
1083 &dev_attr_usb488_interface_capabilities
.attr
,
1084 &dev_attr_usb488_device_capabilities
.attr
,
1088 static struct attribute_group capability_attr_grp
= {
1089 .attrs
= capability_attrs
,
1092 static ssize_t
TermChar_show(struct device
*dev
,
1093 struct device_attribute
*attr
, char *buf
)
1095 struct usb_interface
*intf
= to_usb_interface(dev
);
1096 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
1098 return sprintf(buf
, "%c\n", data
->TermChar
);
1101 static ssize_t
TermChar_store(struct device
*dev
,
1102 struct device_attribute
*attr
,
1103 const char *buf
, size_t count
)
1105 struct usb_interface
*intf
= to_usb_interface(dev
);
1106 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
1110 data
->TermChar
= buf
[0];
1113 static DEVICE_ATTR_RW(TermChar
);
1115 #define data_attribute(name) \
1116 static ssize_t name##_show(struct device *dev, \
1117 struct device_attribute *attr, char *buf) \
1119 struct usb_interface *intf = to_usb_interface(dev); \
1120 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1122 return sprintf(buf, "%d\n", data->name); \
1124 static ssize_t name##_store(struct device *dev, \
1125 struct device_attribute *attr, \
1126 const char *buf, size_t count) \
1128 struct usb_interface *intf = to_usb_interface(dev); \
1129 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1133 result = sscanf(buf, "%u\n", &val); \
1142 static DEVICE_ATTR_RW(name)
1144 data_attribute(TermCharEnabled
);
1145 data_attribute(auto_abort
);
1147 static struct attribute
*data_attrs
[] = {
1148 &dev_attr_TermChar
.attr
,
1149 &dev_attr_TermCharEnabled
.attr
,
1150 &dev_attr_auto_abort
.attr
,
1154 static struct attribute_group data_attr_grp
= {
1155 .attrs
= data_attrs
,
1158 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
1164 dev
= &data
->intf
->dev
;
1166 buffer
= kmalloc(2, GFP_KERNEL
);
1170 rv
= usb_control_msg(data
->usb_dev
,
1171 usb_rcvctrlpipe(data
->usb_dev
, 0),
1172 USBTMC_REQUEST_INDICATOR_PULSE
,
1173 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1174 0, 0, buffer
, 0x01, USBTMC_TIMEOUT
);
1177 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1181 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1183 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1184 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1195 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1197 struct usbtmc_device_data
*data
;
1198 int retval
= -EBADRQC
;
1200 data
= file
->private_data
;
1201 mutex_lock(&data
->io_mutex
);
1204 goto skip_io_on_zombie
;
1208 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
1209 retval
= usbtmc_ioctl_clear_out_halt(data
);
1212 case USBTMC_IOCTL_CLEAR_IN_HALT
:
1213 retval
= usbtmc_ioctl_clear_in_halt(data
);
1216 case USBTMC_IOCTL_INDICATOR_PULSE
:
1217 retval
= usbtmc_ioctl_indicator_pulse(data
);
1220 case USBTMC_IOCTL_CLEAR
:
1221 retval
= usbtmc_ioctl_clear(data
);
1224 case USBTMC_IOCTL_ABORT_BULK_OUT
:
1225 retval
= usbtmc_ioctl_abort_bulk_out(data
);
1228 case USBTMC_IOCTL_ABORT_BULK_IN
:
1229 retval
= usbtmc_ioctl_abort_bulk_in(data
);
1232 case USBTMC488_IOCTL_GET_CAPS
:
1233 retval
= copy_to_user((void __user
*)arg
,
1235 sizeof(data
->usb488_caps
));
1240 case USBTMC488_IOCTL_READ_STB
:
1241 retval
= usbtmc488_ioctl_read_stb(data
, (void __user
*)arg
);
1244 case USBTMC488_IOCTL_REN_CONTROL
:
1245 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1246 USBTMC488_REQUEST_REN_CONTROL
);
1249 case USBTMC488_IOCTL_GOTO_LOCAL
:
1250 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1251 USBTMC488_REQUEST_GOTO_LOCAL
);
1254 case USBTMC488_IOCTL_LOCAL_LOCKOUT
:
1255 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1256 USBTMC488_REQUEST_LOCAL_LOCKOUT
);
1261 mutex_unlock(&data
->io_mutex
);
1265 static int usbtmc_fasync(int fd
, struct file
*file
, int on
)
1267 struct usbtmc_device_data
*data
= file
->private_data
;
1269 return fasync_helper(fd
, file
, on
, &data
->fasync
);
1272 static unsigned int usbtmc_poll(struct file
*file
, poll_table
*wait
)
1274 struct usbtmc_device_data
*data
= file
->private_data
;
1277 mutex_lock(&data
->io_mutex
);
1280 mask
= POLLHUP
| POLLERR
;
1284 poll_wait(file
, &data
->waitq
, wait
);
1286 mask
= (atomic_read(&data
->srq_asserted
)) ? POLLIN
| POLLRDNORM
: 0;
1289 mutex_unlock(&data
->io_mutex
);
1293 static const struct file_operations fops
= {
1294 .owner
= THIS_MODULE
,
1295 .read
= usbtmc_read
,
1296 .write
= usbtmc_write
,
1297 .open
= usbtmc_open
,
1298 .release
= usbtmc_release
,
1299 .unlocked_ioctl
= usbtmc_ioctl
,
1300 .fasync
= usbtmc_fasync
,
1301 .poll
= usbtmc_poll
,
1302 .llseek
= default_llseek
,
1305 static struct usb_class_driver usbtmc_class
= {
1308 .minor_base
= USBTMC_MINOR_BASE
,
1311 static void usbtmc_interrupt(struct urb
*urb
)
1313 struct usbtmc_device_data
*data
= urb
->context
;
1314 struct device
*dev
= &data
->intf
->dev
;
1315 int status
= urb
->status
;
1318 dev_dbg(&data
->intf
->dev
, "int status: %d len %d\n",
1319 status
, urb
->actual_length
);
1322 case 0: /* SUCCESS */
1323 /* check for valid STB notification */
1324 if (data
->iin_buffer
[0] > 0x81) {
1325 data
->bNotify1
= data
->iin_buffer
[0];
1326 data
->bNotify2
= data
->iin_buffer
[1];
1327 atomic_set(&data
->iin_data_valid
, 1);
1328 wake_up_interruptible(&data
->waitq
);
1331 /* check for SRQ notification */
1332 if (data
->iin_buffer
[0] == 0x81) {
1334 kill_fasync(&data
->fasync
,
1337 atomic_set(&data
->srq_asserted
, 1);
1338 wake_up_interruptible(&data
->waitq
);
1341 dev_warn(dev
, "invalid notification: %x\n", data
->iin_buffer
[0]);
1344 dev_err(dev
, "overflow with length %d, actual length is %d\n",
1345 data
->iin_wMaxPacketSize
, urb
->actual_length
);
1351 /* urb terminated, clean up */
1352 dev_dbg(dev
, "urb terminated, status: %d\n", status
);
1355 dev_err(dev
, "unknown status received: %d\n", status
);
1358 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
1360 dev_err(dev
, "usb_submit_urb failed: %d\n", rv
);
1363 static void usbtmc_free_int(struct usbtmc_device_data
*data
)
1365 if (!data
->iin_ep_present
|| !data
->iin_urb
)
1367 usb_kill_urb(data
->iin_urb
);
1368 kfree(data
->iin_buffer
);
1369 usb_free_urb(data
->iin_urb
);
1370 kref_put(&data
->kref
, usbtmc_delete
);
1373 static int usbtmc_probe(struct usb_interface
*intf
,
1374 const struct usb_device_id
*id
)
1376 struct usbtmc_device_data
*data
;
1377 struct usb_host_interface
*iface_desc
;
1378 struct usb_endpoint_descriptor
*bulk_in
, *bulk_out
, *int_in
;
1382 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
1384 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1390 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
1391 usb_set_intfdata(intf
, data
);
1392 kref_init(&data
->kref
);
1393 mutex_init(&data
->io_mutex
);
1394 init_waitqueue_head(&data
->waitq
);
1395 atomic_set(&data
->iin_data_valid
, 0);
1396 atomic_set(&data
->srq_asserted
, 0);
1399 /* Determine if it is a Rigol or not */
1400 data
->rigol_quirk
= 0;
1401 dev_dbg(&intf
->dev
, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1402 le16_to_cpu(data
->usb_dev
->descriptor
.idVendor
),
1403 le16_to_cpu(data
->usb_dev
->descriptor
.idProduct
));
1404 for(n
= 0; usbtmc_id_quirk
[n
].idVendor
> 0; n
++) {
1405 if ((usbtmc_id_quirk
[n
].idVendor
== le16_to_cpu(data
->usb_dev
->descriptor
.idVendor
)) &&
1406 (usbtmc_id_quirk
[n
].idProduct
== le16_to_cpu(data
->usb_dev
->descriptor
.idProduct
))) {
1407 dev_dbg(&intf
->dev
, "Setting this device as having the RIGOL quirk\n");
1408 data
->rigol_quirk
= 1;
1413 /* Initialize USBTMC bTag and other fields */
1415 data
->TermCharEnabled
= 0;
1416 data
->TermChar
= '\n';
1417 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
1420 /* USBTMC devices have only one setting, so use that */
1421 iface_desc
= data
->intf
->cur_altsetting
;
1422 data
->ifnum
= iface_desc
->desc
.bInterfaceNumber
;
1424 /* Find bulk endpoints */
1425 retcode
= usb_find_common_endpoints(iface_desc
,
1426 &bulk_in
, &bulk_out
, NULL
, NULL
);
1428 dev_err(&intf
->dev
, "bulk endpoints not found\n");
1432 data
->bulk_in
= bulk_in
->bEndpointAddress
;
1433 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n", data
->bulk_in
);
1435 data
->bulk_out
= bulk_out
->bEndpointAddress
;
1436 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n", data
->bulk_out
);
1438 /* Find int endpoint */
1439 retcode
= usb_find_int_in_endpoint(iface_desc
, &int_in
);
1441 data
->iin_ep_present
= 1;
1442 data
->iin_ep
= int_in
->bEndpointAddress
;
1443 data
->iin_wMaxPacketSize
= usb_endpoint_maxp(int_in
);
1444 data
->iin_interval
= int_in
->bInterval
;
1445 dev_dbg(&intf
->dev
, "Found Int in endpoint at %u\n",
1449 retcode
= get_capabilities(data
);
1451 dev_err(&intf
->dev
, "can't read capabilities\n");
1453 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
1454 &capability_attr_grp
);
1456 if (data
->iin_ep_present
) {
1457 /* allocate int urb */
1458 data
->iin_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1459 if (!data
->iin_urb
) {
1461 goto error_register
;
1464 /* Protect interrupt in endpoint data until iin_urb is freed */
1465 kref_get(&data
->kref
);
1467 /* allocate buffer for interrupt in */
1468 data
->iin_buffer
= kmalloc(data
->iin_wMaxPacketSize
,
1470 if (!data
->iin_buffer
) {
1472 goto error_register
;
1475 /* fill interrupt urb */
1476 usb_fill_int_urb(data
->iin_urb
, data
->usb_dev
,
1477 usb_rcvintpipe(data
->usb_dev
, data
->iin_ep
),
1478 data
->iin_buffer
, data
->iin_wMaxPacketSize
,
1480 data
, data
->iin_interval
);
1482 retcode
= usb_submit_urb(data
->iin_urb
, GFP_KERNEL
);
1484 dev_err(&intf
->dev
, "Failed to submit iin_urb\n");
1485 goto error_register
;
1489 retcode
= sysfs_create_group(&intf
->dev
.kobj
, &data_attr_grp
);
1491 retcode
= usb_register_dev(intf
, &usbtmc_class
);
1493 dev_err(&intf
->dev
, "Not able to get a minor"
1494 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE
,
1496 goto error_register
;
1498 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
1503 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1504 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1505 usbtmc_free_int(data
);
1507 kref_put(&data
->kref
, usbtmc_delete
);
1511 static void usbtmc_disconnect(struct usb_interface
*intf
)
1513 struct usbtmc_device_data
*data
;
1515 dev_dbg(&intf
->dev
, "usbtmc_disconnect called\n");
1517 data
= usb_get_intfdata(intf
);
1518 usb_deregister_dev(intf
, &usbtmc_class
);
1519 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1520 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1521 mutex_lock(&data
->io_mutex
);
1523 wake_up_all(&data
->waitq
);
1524 mutex_unlock(&data
->io_mutex
);
1525 usbtmc_free_int(data
);
1526 kref_put(&data
->kref
, usbtmc_delete
);
1529 static int usbtmc_suspend(struct usb_interface
*intf
, pm_message_t message
)
1531 /* this driver does not have pending URBs */
1535 static int usbtmc_resume(struct usb_interface
*intf
)
1540 static struct usb_driver usbtmc_driver
= {
1542 .id_table
= usbtmc_devices
,
1543 .probe
= usbtmc_probe
,
1544 .disconnect
= usbtmc_disconnect
,
1545 .suspend
= usbtmc_suspend
,
1546 .resume
= usbtmc_resume
,
1549 module_usb_driver(usbtmc_driver
);
1551 MODULE_LICENSE("GPL");