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 kref_get(&data
->kref
);
162 /* Store pointer in file structure's private data field */
163 filp
->private_data
= data
;
168 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
170 struct usbtmc_device_data
*data
= file
->private_data
;
172 kref_put(&data
->kref
, usbtmc_delete
);
176 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
183 struct usb_host_interface
*current_setting
;
186 dev
= &data
->intf
->dev
;
187 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
191 rv
= usb_control_msg(data
->usb_dev
,
192 usb_rcvctrlpipe(data
->usb_dev
, 0),
193 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
194 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
195 data
->bTag_last_read
, data
->bulk_in
,
196 buffer
, 2, USBTMC_TIMEOUT
);
199 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
203 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
205 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
210 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
211 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
218 current_setting
= data
->intf
->cur_altsetting
;
219 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++)
220 if (current_setting
->endpoint
[n
].desc
.bEndpointAddress
==
222 max_size
= usb_endpoint_maxp(¤t_setting
->endpoint
[n
].desc
);
225 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
230 dev_dbg(&data
->intf
->dev
, "wMaxPacketSize is %d\n", max_size
);
235 dev_dbg(dev
, "Reading from bulk in EP\n");
237 rv
= usb_bulk_msg(data
->usb_dev
,
238 usb_rcvbulkpipe(data
->usb_dev
,
240 buffer
, USBTMC_SIZE_IOBUFFER
,
241 &actual
, USBTMC_TIMEOUT
);
246 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
249 } while ((actual
== max_size
) &&
250 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
252 if (actual
== max_size
) {
253 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
254 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
261 usbtmc_abort_bulk_in_status
:
262 rv
= usb_control_msg(data
->usb_dev
,
263 usb_rcvctrlpipe(data
->usb_dev
, 0),
264 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
265 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
266 0, data
->bulk_in
, buffer
, 0x08,
270 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
274 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
276 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
281 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
282 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
289 dev_dbg(dev
, "Reading from bulk in EP\n");
291 rv
= usb_bulk_msg(data
->usb_dev
,
292 usb_rcvbulkpipe(data
->usb_dev
,
294 buffer
, USBTMC_SIZE_IOBUFFER
,
295 &actual
, USBTMC_TIMEOUT
);
300 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
303 } while ((actual
== max_size
) &&
304 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
306 if (actual
== max_size
) {
307 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
308 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
313 goto usbtmc_abort_bulk_in_status
;
321 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
328 dev
= &data
->intf
->dev
;
330 buffer
= kmalloc(8, GFP_KERNEL
);
334 rv
= usb_control_msg(data
->usb_dev
,
335 usb_rcvctrlpipe(data
->usb_dev
, 0),
336 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
337 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
338 data
->bTag_last_write
, data
->bulk_out
,
339 buffer
, 2, USBTMC_TIMEOUT
);
342 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
346 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
348 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
349 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
357 usbtmc_abort_bulk_out_check_status
:
358 rv
= usb_control_msg(data
->usb_dev
,
359 usb_rcvctrlpipe(data
->usb_dev
, 0),
360 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
361 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
362 0, data
->bulk_out
, buffer
, 0x08,
366 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
370 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
372 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
373 goto usbtmc_abort_bulk_out_clear_halt
;
375 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
376 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
377 goto usbtmc_abort_bulk_out_check_status
;
382 usbtmc_abort_bulk_out_clear_halt
:
383 rv
= usb_clear_halt(data
->usb_dev
,
384 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
387 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
397 static int usbtmc488_ioctl_read_stb(struct usbtmc_device_data
*data
,
400 struct device
*dev
= &data
->intf
->dev
;
406 dev_dbg(dev
, "Enter ioctl_read_stb iin_ep_present: %d\n",
407 data
->iin_ep_present
);
409 buffer
= kmalloc(8, GFP_KERNEL
);
413 atomic_set(&data
->iin_data_valid
, 0);
415 /* must issue read_stb before using poll or select */
416 atomic_set(&data
->srq_asserted
, 0);
418 rv
= usb_control_msg(data
->usb_dev
,
419 usb_rcvctrlpipe(data
->usb_dev
, 0),
420 USBTMC488_REQUEST_READ_STATUS_BYTE
,
421 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
424 buffer
, 0x03, USBTMC_TIMEOUT
);
426 dev_err(dev
, "stb usb_control_msg returned %d\n", rv
);
430 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
431 dev_err(dev
, "control status returned %x\n", buffer
[0]);
436 if (data
->iin_ep_present
) {
437 rv
= wait_event_interruptible_timeout(
439 atomic_read(&data
->iin_data_valid
) != 0,
442 dev_dbg(dev
, "wait interrupted %d\n", rv
);
447 dev_dbg(dev
, "wait timed out\n");
452 tag
= data
->bNotify1
& 0x7f;
453 if (tag
!= data
->iin_bTag
) {
454 dev_err(dev
, "expected bTag %x got %x\n",
455 data
->iin_bTag
, tag
);
458 stb
= data
->bNotify2
;
463 rv
= copy_to_user(arg
, &stb
, sizeof(stb
));
468 /* bump interrupt bTag */
470 if (data
->iin_bTag
> 127)
471 /* 1 is for SRQ see USBTMC-USB488 subclass spec section 4.3.1 */
478 static int usbtmc488_ioctl_simple(struct usbtmc_device_data
*data
,
479 void __user
*arg
, unsigned int cmd
)
481 struct device
*dev
= &data
->intf
->dev
;
487 if (!(data
->usb488_caps
& USBTMC488_CAPABILITY_SIMPLE
))
490 buffer
= kmalloc(8, GFP_KERNEL
);
494 if (cmd
== USBTMC488_REQUEST_REN_CONTROL
) {
495 rv
= copy_from_user(&val
, arg
, sizeof(val
));
500 wValue
= val
? 1 : 0;
505 rv
= usb_control_msg(data
->usb_dev
,
506 usb_rcvctrlpipe(data
->usb_dev
, 0),
508 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
511 buffer
, 0x01, USBTMC_TIMEOUT
);
513 dev_err(dev
, "simple usb_control_msg failed %d\n", rv
);
515 } else if (rv
!= 1) {
516 dev_warn(dev
, "simple usb_control_msg returned %d\n", rv
);
521 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
522 dev_err(dev
, "simple control status returned %x\n", buffer
[0]);
534 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
535 * @transfer_size: number of bytes to request from the device.
537 * See the USBTMC specification, Table 4.
539 * Also updates bTag_last_write.
541 static int send_request_dev_dep_msg_in(struct usbtmc_device_data
*data
, size_t transfer_size
)
547 buffer
= kmalloc(USBTMC_HEADER_SIZE
, GFP_KERNEL
);
550 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
551 * Refer to class specs for details
554 buffer
[1] = data
->bTag
;
555 buffer
[2] = ~data
->bTag
;
556 buffer
[3] = 0; /* Reserved */
557 buffer
[4] = transfer_size
>> 0;
558 buffer
[5] = transfer_size
>> 8;
559 buffer
[6] = transfer_size
>> 16;
560 buffer
[7] = transfer_size
>> 24;
561 buffer
[8] = data
->TermCharEnabled
* 2;
562 /* Use term character? */
563 buffer
[9] = data
->TermChar
;
564 buffer
[10] = 0; /* Reserved */
565 buffer
[11] = 0; /* Reserved */
568 retval
= usb_bulk_msg(data
->usb_dev
,
569 usb_sndbulkpipe(data
->usb_dev
,
571 buffer
, USBTMC_HEADER_SIZE
, &actual
, USBTMC_TIMEOUT
);
573 /* Store bTag (in case we need to abort) */
574 data
->bTag_last_write
= data
->bTag
;
576 /* Increment bTag -- and increment again if zero */
583 dev_err(&data
->intf
->dev
, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval
);
590 static ssize_t
usbtmc_read(struct file
*filp
, char __user
*buf
,
591 size_t count
, loff_t
*f_pos
)
593 struct usbtmc_device_data
*data
;
603 /* Get pointer to private data structure */
604 data
= filp
->private_data
;
605 dev
= &data
->intf
->dev
;
607 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
611 mutex_lock(&data
->io_mutex
);
617 if (data
->rigol_quirk
) {
618 dev_dbg(dev
, "usb_bulk_msg_in: count(%zu)\n", count
);
620 retval
= send_request_dev_dep_msg_in(data
, count
);
623 if (data
->auto_abort
)
624 usbtmc_ioctl_abort_bulk_out(data
);
629 /* Loop until we have fetched everything we requested */
631 this_part
= remaining
;
634 while (remaining
> 0) {
635 if (!data
->rigol_quirk
) {
636 dev_dbg(dev
, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining
, count
);
638 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3)
639 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
- 3;
641 this_part
= remaining
;
643 retval
= send_request_dev_dep_msg_in(data
, this_part
);
645 dev_err(dev
, "usb_bulk_msg returned %d\n", retval
);
646 if (data
->auto_abort
)
647 usbtmc_ioctl_abort_bulk_out(data
);
653 retval
= usb_bulk_msg(data
->usb_dev
,
654 usb_rcvbulkpipe(data
->usb_dev
,
656 buffer
, USBTMC_SIZE_IOBUFFER
, &actual
,
659 dev_dbg(dev
, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval
, done
, remaining
, actual
);
661 /* Store bTag (in case we need to abort) */
662 data
->bTag_last_read
= data
->bTag
;
665 dev_dbg(dev
, "Unable to read data, error %d\n", retval
);
666 if (data
->auto_abort
)
667 usbtmc_ioctl_abort_bulk_in(data
);
671 /* Parse header in first packet */
672 if ((done
== 0) || !data
->rigol_quirk
) {
673 /* Sanity checks for the header */
674 if (actual
< USBTMC_HEADER_SIZE
) {
675 dev_err(dev
, "Device sent too small first packet: %u < %u\n", actual
, USBTMC_HEADER_SIZE
);
676 if (data
->auto_abort
)
677 usbtmc_ioctl_abort_bulk_in(data
);
681 if (buffer
[0] != 2) {
682 dev_err(dev
, "Device sent reply with wrong MsgID: %u != 2\n", buffer
[0]);
683 if (data
->auto_abort
)
684 usbtmc_ioctl_abort_bulk_in(data
);
688 if (buffer
[1] != data
->bTag_last_write
) {
689 dev_err(dev
, "Device sent reply with wrong bTag: %u != %u\n", buffer
[1], data
->bTag_last_write
);
690 if (data
->auto_abort
)
691 usbtmc_ioctl_abort_bulk_in(data
);
695 /* How many characters did the instrument send? */
696 n_characters
= buffer
[4] +
701 if (n_characters
> this_part
) {
702 dev_err(dev
, "Device wants to return more data than requested: %u > %zu\n", n_characters
, count
);
703 if (data
->auto_abort
)
704 usbtmc_ioctl_abort_bulk_in(data
);
708 /* Remove the USBTMC header */
709 actual
-= USBTMC_HEADER_SIZE
;
711 /* Check if the message is smaller than requested */
712 if (data
->rigol_quirk
) {
713 if (remaining
> n_characters
)
714 remaining
= n_characters
;
715 /* Remove padding if it exists */
716 if (actual
> remaining
)
720 if (this_part
> n_characters
)
721 this_part
= n_characters
;
722 /* Remove padding if it exists */
723 if (actual
> this_part
)
727 dev_dbg(dev
, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters
, buffer
[8]);
731 /* Terminate if end-of-message bit received from device */
732 if ((buffer
[8] & 0x01) && (actual
>= n_characters
))
735 dev_dbg(dev
, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining
,buf
,buffer
,done
);
738 /* Copy buffer to user space */
739 if (copy_to_user(buf
+ done
, &buffer
[USBTMC_HEADER_SIZE
], actual
)) {
740 /* There must have been an addressing problem */
747 if (actual
> remaining
)
752 dev_dbg(dev
, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual
, done
, remaining
,buf
,buffer
);
754 /* Copy buffer to user space */
755 if (copy_to_user(buf
+ done
, buffer
, actual
)) {
756 /* There must have been an addressing problem */
764 /* Update file position value */
765 *f_pos
= *f_pos
+ done
;
769 mutex_unlock(&data
->io_mutex
);
774 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
775 size_t count
, loff_t
*f_pos
)
777 struct usbtmc_device_data
*data
;
781 unsigned long int n_bytes
;
786 data
= filp
->private_data
;
788 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
792 mutex_lock(&data
->io_mutex
);
801 while (remaining
> 0) {
802 if (remaining
> USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
) {
803 this_part
= USBTMC_SIZE_IOBUFFER
- USBTMC_HEADER_SIZE
;
806 this_part
= remaining
;
810 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
812 buffer
[1] = data
->bTag
;
813 buffer
[2] = ~data
->bTag
;
814 buffer
[3] = 0; /* Reserved */
815 buffer
[4] = this_part
>> 0;
816 buffer
[5] = this_part
>> 8;
817 buffer
[6] = this_part
>> 16;
818 buffer
[7] = this_part
>> 24;
819 /* buffer[8] is set above... */
820 buffer
[9] = 0; /* Reserved */
821 buffer
[10] = 0; /* Reserved */
822 buffer
[11] = 0; /* Reserved */
824 if (copy_from_user(&buffer
[USBTMC_HEADER_SIZE
], buf
+ done
, this_part
)) {
829 n_bytes
= roundup(USBTMC_HEADER_SIZE
+ this_part
, 4);
830 memset(buffer
+ USBTMC_HEADER_SIZE
+ this_part
, 0, n_bytes
- (USBTMC_HEADER_SIZE
+ this_part
));
833 retval
= usb_bulk_msg(data
->usb_dev
,
834 usb_sndbulkpipe(data
->usb_dev
,
837 &actual
, USBTMC_TIMEOUT
);
843 data
->bTag_last_write
= data
->bTag
;
850 dev_err(&data
->intf
->dev
,
851 "Unable to send data, error %d\n", retval
);
852 if (data
->auto_abort
)
853 usbtmc_ioctl_abort_bulk_out(data
);
857 remaining
-= this_part
;
863 mutex_unlock(&data
->io_mutex
);
868 static int usbtmc_ioctl_clear(struct usbtmc_device_data
*data
)
870 struct usb_host_interface
*current_setting
;
871 struct usb_endpoint_descriptor
*desc
;
879 dev
= &data
->intf
->dev
;
881 dev_dbg(dev
, "Sending INITIATE_CLEAR request\n");
883 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
887 rv
= usb_control_msg(data
->usb_dev
,
888 usb_rcvctrlpipe(data
->usb_dev
, 0),
889 USBTMC_REQUEST_INITIATE_CLEAR
,
890 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
891 0, 0, buffer
, 1, USBTMC_TIMEOUT
);
893 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
897 dev_dbg(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
899 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
900 dev_err(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
906 current_setting
= data
->intf
->cur_altsetting
;
907 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++) {
908 desc
= ¤t_setting
->endpoint
[n
].desc
;
909 if (desc
->bEndpointAddress
== data
->bulk_in
)
910 max_size
= usb_endpoint_maxp(desc
);
914 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
919 dev_dbg(dev
, "wMaxPacketSize is %d\n", max_size
);
923 usbtmc_clear_check_status
:
925 dev_dbg(dev
, "Sending CHECK_CLEAR_STATUS request\n");
927 rv
= usb_control_msg(data
->usb_dev
,
928 usb_rcvctrlpipe(data
->usb_dev
, 0),
929 USBTMC_REQUEST_CHECK_CLEAR_STATUS
,
930 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
931 0, 0, buffer
, 2, USBTMC_TIMEOUT
);
933 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
937 dev_dbg(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
939 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
940 goto usbtmc_clear_bulk_out_halt
;
942 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
943 dev_err(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
950 dev_dbg(dev
, "Reading from bulk in EP\n");
952 rv
= usb_bulk_msg(data
->usb_dev
,
953 usb_rcvbulkpipe(data
->usb_dev
,
955 buffer
, USBTMC_SIZE_IOBUFFER
,
956 &actual
, USBTMC_TIMEOUT
);
960 dev_err(dev
, "usb_control_msg returned %d\n",
964 } while ((actual
== max_size
) &&
965 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
967 if (actual
== max_size
) {
968 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
969 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
974 goto usbtmc_clear_check_status
;
976 usbtmc_clear_bulk_out_halt
:
978 rv
= usb_clear_halt(data
->usb_dev
,
979 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
981 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
991 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data
*data
)
995 rv
= usb_clear_halt(data
->usb_dev
,
996 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
999 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
1006 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data
*data
)
1010 rv
= usb_clear_halt(data
->usb_dev
,
1011 usb_rcvbulkpipe(data
->usb_dev
, data
->bulk_in
));
1014 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
1021 static int get_capabilities(struct usbtmc_device_data
*data
)
1023 struct device
*dev
= &data
->usb_dev
->dev
;
1027 buffer
= kmalloc(0x18, GFP_KERNEL
);
1031 rv
= usb_control_msg(data
->usb_dev
, usb_rcvctrlpipe(data
->usb_dev
, 0),
1032 USBTMC_REQUEST_GET_CAPABILITIES
,
1033 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1034 0, 0, buffer
, 0x18, USBTMC_TIMEOUT
);
1036 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1040 dev_dbg(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
1041 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1042 dev_err(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
1046 dev_dbg(dev
, "Interface capabilities are %x\n", buffer
[4]);
1047 dev_dbg(dev
, "Device capabilities are %x\n", buffer
[5]);
1048 dev_dbg(dev
, "USB488 interface capabilities are %x\n", buffer
[14]);
1049 dev_dbg(dev
, "USB488 device capabilities are %x\n", buffer
[15]);
1051 data
->capabilities
.interface_capabilities
= buffer
[4];
1052 data
->capabilities
.device_capabilities
= buffer
[5];
1053 data
->capabilities
.usb488_interface_capabilities
= buffer
[14];
1054 data
->capabilities
.usb488_device_capabilities
= buffer
[15];
1055 data
->usb488_caps
= (buffer
[14] & 0x07) | ((buffer
[15] & 0x0f) << 4);
1063 #define capability_attribute(name) \
1064 static ssize_t name##_show(struct device *dev, \
1065 struct device_attribute *attr, char *buf) \
1067 struct usb_interface *intf = to_usb_interface(dev); \
1068 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1070 return sprintf(buf, "%d\n", data->capabilities.name); \
1072 static DEVICE_ATTR_RO(name)
1074 capability_attribute(interface_capabilities
);
1075 capability_attribute(device_capabilities
);
1076 capability_attribute(usb488_interface_capabilities
);
1077 capability_attribute(usb488_device_capabilities
);
1079 static struct attribute
*capability_attrs
[] = {
1080 &dev_attr_interface_capabilities
.attr
,
1081 &dev_attr_device_capabilities
.attr
,
1082 &dev_attr_usb488_interface_capabilities
.attr
,
1083 &dev_attr_usb488_device_capabilities
.attr
,
1087 static struct attribute_group capability_attr_grp
= {
1088 .attrs
= capability_attrs
,
1091 static ssize_t
TermChar_show(struct device
*dev
,
1092 struct device_attribute
*attr
, char *buf
)
1094 struct usb_interface
*intf
= to_usb_interface(dev
);
1095 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
1097 return sprintf(buf
, "%c\n", data
->TermChar
);
1100 static ssize_t
TermChar_store(struct device
*dev
,
1101 struct device_attribute
*attr
,
1102 const char *buf
, size_t count
)
1104 struct usb_interface
*intf
= to_usb_interface(dev
);
1105 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
1109 data
->TermChar
= buf
[0];
1112 static DEVICE_ATTR_RW(TermChar
);
1114 #define data_attribute(name) \
1115 static ssize_t name##_show(struct device *dev, \
1116 struct device_attribute *attr, char *buf) \
1118 struct usb_interface *intf = to_usb_interface(dev); \
1119 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1121 return sprintf(buf, "%d\n", data->name); \
1123 static ssize_t name##_store(struct device *dev, \
1124 struct device_attribute *attr, \
1125 const char *buf, size_t count) \
1127 struct usb_interface *intf = to_usb_interface(dev); \
1128 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
1132 result = sscanf(buf, "%u\n", &val); \
1141 static DEVICE_ATTR_RW(name)
1143 data_attribute(TermCharEnabled
);
1144 data_attribute(auto_abort
);
1146 static struct attribute
*data_attrs
[] = {
1147 &dev_attr_TermChar
.attr
,
1148 &dev_attr_TermCharEnabled
.attr
,
1149 &dev_attr_auto_abort
.attr
,
1153 static struct attribute_group data_attr_grp
= {
1154 .attrs
= data_attrs
,
1157 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
1163 dev
= &data
->intf
->dev
;
1165 buffer
= kmalloc(2, GFP_KERNEL
);
1169 rv
= usb_control_msg(data
->usb_dev
,
1170 usb_rcvctrlpipe(data
->usb_dev
, 0),
1171 USBTMC_REQUEST_INDICATOR_PULSE
,
1172 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
1173 0, 0, buffer
, 0x01, USBTMC_TIMEOUT
);
1176 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
1180 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1182 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
1183 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
1194 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
1196 struct usbtmc_device_data
*data
;
1197 int retval
= -EBADRQC
;
1199 data
= file
->private_data
;
1200 mutex_lock(&data
->io_mutex
);
1203 goto skip_io_on_zombie
;
1207 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
1208 retval
= usbtmc_ioctl_clear_out_halt(data
);
1211 case USBTMC_IOCTL_CLEAR_IN_HALT
:
1212 retval
= usbtmc_ioctl_clear_in_halt(data
);
1215 case USBTMC_IOCTL_INDICATOR_PULSE
:
1216 retval
= usbtmc_ioctl_indicator_pulse(data
);
1219 case USBTMC_IOCTL_CLEAR
:
1220 retval
= usbtmc_ioctl_clear(data
);
1223 case USBTMC_IOCTL_ABORT_BULK_OUT
:
1224 retval
= usbtmc_ioctl_abort_bulk_out(data
);
1227 case USBTMC_IOCTL_ABORT_BULK_IN
:
1228 retval
= usbtmc_ioctl_abort_bulk_in(data
);
1231 case USBTMC488_IOCTL_GET_CAPS
:
1232 retval
= copy_to_user((void __user
*)arg
,
1234 sizeof(data
->usb488_caps
));
1239 case USBTMC488_IOCTL_READ_STB
:
1240 retval
= usbtmc488_ioctl_read_stb(data
, (void __user
*)arg
);
1243 case USBTMC488_IOCTL_REN_CONTROL
:
1244 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1245 USBTMC488_REQUEST_REN_CONTROL
);
1248 case USBTMC488_IOCTL_GOTO_LOCAL
:
1249 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1250 USBTMC488_REQUEST_GOTO_LOCAL
);
1253 case USBTMC488_IOCTL_LOCAL_LOCKOUT
:
1254 retval
= usbtmc488_ioctl_simple(data
, (void __user
*)arg
,
1255 USBTMC488_REQUEST_LOCAL_LOCKOUT
);
1260 mutex_unlock(&data
->io_mutex
);
1264 static int usbtmc_fasync(int fd
, struct file
*file
, int on
)
1266 struct usbtmc_device_data
*data
= file
->private_data
;
1268 return fasync_helper(fd
, file
, on
, &data
->fasync
);
1271 static unsigned int usbtmc_poll(struct file
*file
, poll_table
*wait
)
1273 struct usbtmc_device_data
*data
= file
->private_data
;
1276 mutex_lock(&data
->io_mutex
);
1279 mask
= POLLHUP
| POLLERR
;
1283 poll_wait(file
, &data
->waitq
, wait
);
1285 mask
= (atomic_read(&data
->srq_asserted
)) ? POLLIN
| POLLRDNORM
: 0;
1288 mutex_unlock(&data
->io_mutex
);
1292 static const struct file_operations fops
= {
1293 .owner
= THIS_MODULE
,
1294 .read
= usbtmc_read
,
1295 .write
= usbtmc_write
,
1296 .open
= usbtmc_open
,
1297 .release
= usbtmc_release
,
1298 .unlocked_ioctl
= usbtmc_ioctl
,
1299 .fasync
= usbtmc_fasync
,
1300 .poll
= usbtmc_poll
,
1301 .llseek
= default_llseek
,
1304 static struct usb_class_driver usbtmc_class
= {
1307 .minor_base
= USBTMC_MINOR_BASE
,
1310 static void usbtmc_interrupt(struct urb
*urb
)
1312 struct usbtmc_device_data
*data
= urb
->context
;
1313 struct device
*dev
= &data
->intf
->dev
;
1314 int status
= urb
->status
;
1317 dev_dbg(&data
->intf
->dev
, "int status: %d len %d\n",
1318 status
, urb
->actual_length
);
1321 case 0: /* SUCCESS */
1322 /* check for valid STB notification */
1323 if (data
->iin_buffer
[0] > 0x81) {
1324 data
->bNotify1
= data
->iin_buffer
[0];
1325 data
->bNotify2
= data
->iin_buffer
[1];
1326 atomic_set(&data
->iin_data_valid
, 1);
1327 wake_up_interruptible(&data
->waitq
);
1330 /* check for SRQ notification */
1331 if (data
->iin_buffer
[0] == 0x81) {
1333 kill_fasync(&data
->fasync
,
1336 atomic_set(&data
->srq_asserted
, 1);
1337 wake_up_interruptible(&data
->waitq
);
1340 dev_warn(dev
, "invalid notification: %x\n", data
->iin_buffer
[0]);
1343 dev_err(dev
, "overflow with length %d, actual length is %d\n",
1344 data
->iin_wMaxPacketSize
, urb
->actual_length
);
1350 /* urb terminated, clean up */
1351 dev_dbg(dev
, "urb terminated, status: %d\n", status
);
1354 dev_err(dev
, "unknown status received: %d\n", status
);
1357 rv
= usb_submit_urb(urb
, GFP_ATOMIC
);
1359 dev_err(dev
, "usb_submit_urb failed: %d\n", rv
);
1362 static void usbtmc_free_int(struct usbtmc_device_data
*data
)
1364 if (!data
->iin_ep_present
|| !data
->iin_urb
)
1366 usb_kill_urb(data
->iin_urb
);
1367 kfree(data
->iin_buffer
);
1368 usb_free_urb(data
->iin_urb
);
1369 kref_put(&data
->kref
, usbtmc_delete
);
1372 static int usbtmc_probe(struct usb_interface
*intf
,
1373 const struct usb_device_id
*id
)
1375 struct usbtmc_device_data
*data
;
1376 struct usb_host_interface
*iface_desc
;
1377 struct usb_endpoint_descriptor
*endpoint
;
1381 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
1383 data
= kzalloc(sizeof(*data
), GFP_KERNEL
);
1389 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
1390 usb_set_intfdata(intf
, data
);
1391 kref_init(&data
->kref
);
1392 mutex_init(&data
->io_mutex
);
1393 init_waitqueue_head(&data
->waitq
);
1394 atomic_set(&data
->iin_data_valid
, 0);
1395 atomic_set(&data
->srq_asserted
, 0);
1398 /* Determine if it is a Rigol or not */
1399 data
->rigol_quirk
= 0;
1400 dev_dbg(&intf
->dev
, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1401 le16_to_cpu(data
->usb_dev
->descriptor
.idVendor
),
1402 le16_to_cpu(data
->usb_dev
->descriptor
.idProduct
));
1403 for(n
= 0; usbtmc_id_quirk
[n
].idVendor
> 0; n
++) {
1404 if ((usbtmc_id_quirk
[n
].idVendor
== le16_to_cpu(data
->usb_dev
->descriptor
.idVendor
)) &&
1405 (usbtmc_id_quirk
[n
].idProduct
== le16_to_cpu(data
->usb_dev
->descriptor
.idProduct
))) {
1406 dev_dbg(&intf
->dev
, "Setting this device as having the RIGOL quirk\n");
1407 data
->rigol_quirk
= 1;
1412 /* Initialize USBTMC bTag and other fields */
1414 data
->TermCharEnabled
= 0;
1415 data
->TermChar
= '\n';
1416 /* 2 <= bTag <= 127 USBTMC-USB488 subclass specification 4.3.1 */
1419 /* USBTMC devices have only one setting, so use that */
1420 iface_desc
= data
->intf
->cur_altsetting
;
1421 data
->ifnum
= iface_desc
->desc
.bInterfaceNumber
;
1423 /* Find bulk in endpoint */
1424 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1425 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1427 if (usb_endpoint_is_bulk_in(endpoint
)) {
1428 data
->bulk_in
= endpoint
->bEndpointAddress
;
1429 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n",
1435 /* Find bulk out endpoint */
1436 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1437 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1439 if (usb_endpoint_is_bulk_out(endpoint
)) {
1440 data
->bulk_out
= endpoint
->bEndpointAddress
;
1441 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n",
1447 if (!data
->bulk_out
|| !data
->bulk_in
) {
1448 dev_err(&intf
->dev
, "bulk endpoints not found\n");
1453 /* Find int endpoint */
1454 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1455 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1457 if (usb_endpoint_is_int_in(endpoint
)) {
1458 data
->iin_ep_present
= 1;
1459 data
->iin_ep
= endpoint
->bEndpointAddress
;
1460 data
->iin_wMaxPacketSize
= usb_endpoint_maxp(endpoint
);
1461 data
->iin_interval
= endpoint
->bInterval
;
1462 dev_dbg(&intf
->dev
, "Found Int in endpoint at %u\n",
1468 retcode
= get_capabilities(data
);
1470 dev_err(&intf
->dev
, "can't read capabilities\n");
1472 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
1473 &capability_attr_grp
);
1475 if (data
->iin_ep_present
) {
1476 /* allocate int urb */
1477 data
->iin_urb
= usb_alloc_urb(0, GFP_KERNEL
);
1478 if (!data
->iin_urb
) {
1480 goto error_register
;
1483 /* will reference data in int urb */
1484 kref_get(&data
->kref
);
1486 /* allocate buffer for interrupt in */
1487 data
->iin_buffer
= kmalloc(data
->iin_wMaxPacketSize
,
1489 if (!data
->iin_buffer
) {
1491 goto error_register
;
1494 /* fill interrupt urb */
1495 usb_fill_int_urb(data
->iin_urb
, data
->usb_dev
,
1496 usb_rcvintpipe(data
->usb_dev
, data
->iin_ep
),
1497 data
->iin_buffer
, data
->iin_wMaxPacketSize
,
1499 data
, data
->iin_interval
);
1501 retcode
= usb_submit_urb(data
->iin_urb
, GFP_KERNEL
);
1503 dev_err(&intf
->dev
, "Failed to submit iin_urb\n");
1504 goto error_register
;
1508 retcode
= sysfs_create_group(&intf
->dev
.kobj
, &data_attr_grp
);
1510 retcode
= usb_register_dev(intf
, &usbtmc_class
);
1512 dev_err(&intf
->dev
, "Not able to get a minor"
1513 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE
,
1515 goto error_register
;
1517 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
1522 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1523 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1524 usbtmc_free_int(data
);
1526 kref_put(&data
->kref
, usbtmc_delete
);
1530 static void usbtmc_disconnect(struct usb_interface
*intf
)
1532 struct usbtmc_device_data
*data
;
1534 dev_dbg(&intf
->dev
, "usbtmc_disconnect called\n");
1536 data
= usb_get_intfdata(intf
);
1537 usb_deregister_dev(intf
, &usbtmc_class
);
1538 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1539 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1540 mutex_lock(&data
->io_mutex
);
1542 wake_up_all(&data
->waitq
);
1543 mutex_unlock(&data
->io_mutex
);
1544 usbtmc_free_int(data
);
1545 kref_put(&data
->kref
, usbtmc_delete
);
1548 static int usbtmc_suspend(struct usb_interface
*intf
, pm_message_t message
)
1550 /* this driver does not have pending URBs */
1554 static int usbtmc_resume(struct usb_interface
*intf
)
1559 static struct usb_driver usbtmc_driver
= {
1561 .id_table
= usbtmc_devices
,
1562 .probe
= usbtmc_probe
,
1563 .disconnect
= usbtmc_disconnect
,
1564 .suspend
= usbtmc_suspend
,
1565 .resume
= usbtmc_resume
,
1568 module_usb_driver(usbtmc_driver
);
1570 MODULE_LICENSE("GPL");