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/mutex.h>
31 #include <linux/usb.h>
32 #include <linux/usb/tmc.h>
36 #define USBTMC_HEADER_SIZE 12
37 #define USBTMC_MINOR_BASE 176
40 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
41 * large as wMaxPacketSize (which is usually 512 bytes).
43 #define USBTMC_SIZE_IOBUFFER 2048
45 /* Default USB timeout (in milliseconds) */
46 #define USBTMC_TIMEOUT 5000
49 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
50 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
51 * packet is never read.
53 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
55 static const struct usb_device_id usbtmc_devices[] = {
56 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
57 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 1), },
58 { 0, } /* terminating entry */
60 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
63 * This structure is the capabilities for the device
64 * See section 4.2.1.8 of the USBTMC specification,
65 * and section 4.2.2 of the USBTMC usb488 subclass
66 * specification for details.
68 struct usbtmc_dev_capabilities {
69 __u8 interface_capabilities;
70 __u8 device_capabilities;
71 __u8 usb488_interface_capabilities;
72 __u8 usb488_device_capabilities;
75 /* This structure holds private data for each USBTMC device. One copy is
76 * allocated for each USBTMC device in the driver's probe function.
78 struct usbtmc_device_data {
79 const struct usb_device_id *id;
80 struct usb_device *usb_dev;
81 struct usb_interface *intf;
84 unsigned int bulk_out;
87 u8 bTag_last_write; /* needed for abort */
88 u8 bTag_last_read; /* needed for abort */
92 /* attributes from the USB TMC spec for this device */
97 bool zombie; /* fd of disconnected device */
99 struct usbtmc_dev_capabilities capabilities;
101 struct mutex io_mutex; /* only one i/o function running at a time */
103 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
105 struct usbtmc_ID_rigol_quirk {
110 static const struct usbtmc_ID_rigol_quirk usbtmc_id_quirk[] = {
116 /* Forward declarations */
117 static struct usb_driver usbtmc_driver;
119 static void usbtmc_delete(struct kref *kref)
121 struct usbtmc_device_data *data = to_usbtmc_data(kref);
123 usb_put_dev(data->usb_dev);
126 static int usbtmc_open(struct inode *inode, struct file *filp)
128 struct usb_interface *intf;
129 struct usbtmc_device_data *data;
132 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
134 pr_err("can not find device for minor %d", iminor(inode));
138 data = usb_get_intfdata(intf);
139 kref_get(&data->kref);
141 /* Store pointer in file structure's private data field */
142 filp->private_data = data;
147 static int usbtmc_release(struct inode *inode, struct file *file)
149 struct usbtmc_device_data *data = file->private_data;
151 kref_put(&data->kref, usbtmc_delete);
155 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
162 struct usb_host_interface *current_setting;
165 dev = &data->intf->dev;
166 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
170 rv = usb_control_msg(data->usb_dev,
171 usb_rcvctrlpipe(data->usb_dev, 0),
172 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
173 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
174 data->bTag_last_read, data->bulk_in,
175 buffer, 2, USBTMC_TIMEOUT);
178 dev_err(dev, "usb_control_msg returned %d\n", rv);
182 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
184 if (buffer[0] == USBTMC_STATUS_FAILED) {
189 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
190 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
197 current_setting = data->intf->cur_altsetting;
198 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
199 if (current_setting->endpoint[n].desc.bEndpointAddress ==
201 max_size = usb_endpoint_maxp(¤t_setting->endpoint[n].desc);
204 dev_err(dev, "Couldn't get wMaxPacketSize\n");
209 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
214 dev_dbg(dev, "Reading from bulk in EP\n");
216 rv = usb_bulk_msg(data->usb_dev,
217 usb_rcvbulkpipe(data->usb_dev,
219 buffer, USBTMC_SIZE_IOBUFFER,
220 &actual, USBTMC_TIMEOUT);
225 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
228 } while ((actual == max_size) &&
229 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
231 if (actual == max_size) {
232 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
233 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
240 usbtmc_abort_bulk_in_status:
241 rv = usb_control_msg(data->usb_dev,
242 usb_rcvctrlpipe(data->usb_dev, 0),
243 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
244 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
245 0, data->bulk_in, buffer, 0x08,
249 dev_err(dev, "usb_control_msg returned %d\n", rv);
253 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
255 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
260 if (buffer[0] != USBTMC_STATUS_PENDING) {
261 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
268 dev_dbg(dev, "Reading from bulk in EP\n");
270 rv = usb_bulk_msg(data->usb_dev,
271 usb_rcvbulkpipe(data->usb_dev,
273 buffer, USBTMC_SIZE_IOBUFFER,
274 &actual, USBTMC_TIMEOUT);
279 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
282 } while ((actual == max_size) &&
283 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
285 if (actual == max_size) {
286 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
287 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
292 goto usbtmc_abort_bulk_in_status;
300 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
307 dev = &data->intf->dev;
309 buffer = kmalloc(8, GFP_KERNEL);
313 rv = usb_control_msg(data->usb_dev,
314 usb_rcvctrlpipe(data->usb_dev, 0),
315 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
316 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
317 data->bTag_last_write, data->bulk_out,
318 buffer, 2, USBTMC_TIMEOUT);
321 dev_err(dev, "usb_control_msg returned %d\n", rv);
325 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
327 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
328 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
336 usbtmc_abort_bulk_out_check_status:
337 rv = usb_control_msg(data->usb_dev,
338 usb_rcvctrlpipe(data->usb_dev, 0),
339 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
340 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
341 0, data->bulk_out, buffer, 0x08,
345 dev_err(dev, "usb_control_msg returned %d\n", rv);
349 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
351 if (buffer[0] == USBTMC_STATUS_SUCCESS)
352 goto usbtmc_abort_bulk_out_clear_halt;
354 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
355 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
356 goto usbtmc_abort_bulk_out_check_status;
361 usbtmc_abort_bulk_out_clear_halt:
362 rv = usb_clear_halt(data->usb_dev,
363 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
366 dev_err(dev, "usb_control_msg returned %d\n", rv);
377 * Sends a REQUEST_DEV_DEP_MSG_IN message on the Bulk-IN endpoint.
378 * @transfer_size: number of bytes to request from the device.
380 * See the USBTMC specification, Table 4.
382 * Also updates bTag_last_write.
384 static int send_request_dev_dep_msg_in(struct usbtmc_device_data *data, size_t transfer_size)
390 buffer = kmalloc(USBTMC_HEADER_SIZE, GFP_KERNEL);
393 /* Setup IO buffer for REQUEST_DEV_DEP_MSG_IN message
394 * Refer to class specs for details
397 buffer[1] = data->bTag;
398 buffer[2] = ~data->bTag;
399 buffer[3] = 0; /* Reserved */
400 buffer[4] = transfer_size >> 0;
401 buffer[5] = transfer_size >> 8;
402 buffer[6] = transfer_size >> 16;
403 buffer[7] = transfer_size >> 24;
404 buffer[8] = data->TermCharEnabled * 2;
405 /* Use term character? */
406 buffer[9] = data->TermChar;
407 buffer[10] = 0; /* Reserved */
408 buffer[11] = 0; /* Reserved */
411 retval = usb_bulk_msg(data->usb_dev,
412 usb_sndbulkpipe(data->usb_dev,
414 buffer, USBTMC_HEADER_SIZE, &actual, USBTMC_TIMEOUT);
416 /* Store bTag (in case we need to abort) */
417 data->bTag_last_write = data->bTag;
419 /* Increment bTag -- and increment again if zero */
426 dev_err(&data->intf->dev, "usb_bulk_msg in send_request_dev_dep_msg_in() returned %d\n", retval);
433 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
434 size_t count, loff_t *f_pos)
436 struct usbtmc_device_data *data;
446 /* Get pointer to private data structure */
447 data = filp->private_data;
448 dev = &data->intf->dev;
450 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
454 mutex_lock(&data->io_mutex);
460 if (data->rigol_quirk) {
461 dev_dbg(dev, "usb_bulk_msg_in: count(%zu)\n", count);
463 retval = send_request_dev_dep_msg_in(data, count);
466 if (data->auto_abort)
467 usbtmc_ioctl_abort_bulk_out(data);
472 /* Loop until we have fetched everything we requested */
474 this_part = remaining;
477 while (remaining > 0) {
478 if (!data->rigol_quirk) {
479 dev_dbg(dev, "usb_bulk_msg_in: remaining(%zu), count(%zu)\n", remaining, count);
481 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3)
482 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE - 3;
484 this_part = remaining;
486 retval = send_request_dev_dep_msg_in(data, this_part);
488 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
489 if (data->auto_abort)
490 usbtmc_ioctl_abort_bulk_out(data);
496 retval = usb_bulk_msg(data->usb_dev,
497 usb_rcvbulkpipe(data->usb_dev,
499 buffer, USBTMC_SIZE_IOBUFFER, &actual,
502 dev_dbg(dev, "usb_bulk_msg: retval(%u), done(%zu), remaining(%zu), actual(%d)\n", retval, done, remaining, actual);
504 /* Store bTag (in case we need to abort) */
505 data->bTag_last_read = data->bTag;
508 dev_dbg(dev, "Unable to read data, error %d\n", retval);
509 if (data->auto_abort)
510 usbtmc_ioctl_abort_bulk_in(data);
514 /* Parse header in first packet */
515 if ((done == 0) || !data->rigol_quirk) {
516 /* Sanity checks for the header */
517 if (actual < USBTMC_HEADER_SIZE) {
518 dev_err(dev, "Device sent too small first packet: %u < %u\n", actual, USBTMC_HEADER_SIZE);
519 if (data->auto_abort)
520 usbtmc_ioctl_abort_bulk_in(data);
524 if (buffer[0] != 2) {
525 dev_err(dev, "Device sent reply with wrong MsgID: %u != 2\n", buffer[0]);
526 if (data->auto_abort)
527 usbtmc_ioctl_abort_bulk_in(data);
531 if (buffer[1] != data->bTag_last_write) {
532 dev_err(dev, "Device sent reply with wrong bTag: %u != %u\n", buffer[1], data->bTag_last_write);
533 if (data->auto_abort)
534 usbtmc_ioctl_abort_bulk_in(data);
538 /* How many characters did the instrument send? */
539 n_characters = buffer[4] +
544 if (n_characters > this_part) {
545 dev_err(dev, "Device wants to return more data than requested: %u > %zu\n", n_characters, count);
546 if (data->auto_abort)
547 usbtmc_ioctl_abort_bulk_in(data);
551 /* Remove the USBTMC header */
552 actual -= USBTMC_HEADER_SIZE;
554 /* Check if the message is smaller than requested */
555 if (data->rigol_quirk) {
556 if (remaining > n_characters)
557 remaining = n_characters;
558 /* Remove padding if it exists */
559 if (actual > remaining)
563 if (this_part > n_characters)
564 this_part = n_characters;
565 /* Remove padding if it exists */
566 if (actual > this_part)
570 dev_dbg(dev, "Bulk-IN header: N_characters(%u), bTransAttr(%u)\n", n_characters, buffer[8]);
574 /* Terminate if end-of-message bit received from device */
575 if ((buffer[8] & 0x01) && (actual >= n_characters))
578 dev_dbg(dev, "Bulk-IN header: remaining(%zu), buf(%p), buffer(%p) done(%zu)\n", remaining,buf,buffer,done);
581 /* Copy buffer to user space */
582 if (copy_to_user(buf + done, &buffer[USBTMC_HEADER_SIZE], actual)) {
583 /* There must have been an addressing problem */
590 if (actual > remaining)
595 dev_dbg(dev, "Bulk-IN header cont: actual(%u), done(%zu), remaining(%zu), buf(%p), buffer(%p)\n", actual, done, remaining,buf,buffer);
597 /* Copy buffer to user space */
598 if (copy_to_user(buf + done, buffer, actual)) {
599 /* There must have been an addressing problem */
607 /* Update file position value */
608 *f_pos = *f_pos + done;
612 mutex_unlock(&data->io_mutex);
617 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
618 size_t count, loff_t *f_pos)
620 struct usbtmc_device_data *data;
624 unsigned long int n_bytes;
629 data = filp->private_data;
631 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
635 mutex_lock(&data->io_mutex);
644 while (remaining > 0) {
645 if (remaining > USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE) {
646 this_part = USBTMC_SIZE_IOBUFFER - USBTMC_HEADER_SIZE;
649 this_part = remaining;
653 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
655 buffer[1] = data->bTag;
656 buffer[2] = ~data->bTag;
657 buffer[3] = 0; /* Reserved */
658 buffer[4] = this_part >> 0;
659 buffer[5] = this_part >> 8;
660 buffer[6] = this_part >> 16;
661 buffer[7] = this_part >> 24;
662 /* buffer[8] is set above... */
663 buffer[9] = 0; /* Reserved */
664 buffer[10] = 0; /* Reserved */
665 buffer[11] = 0; /* Reserved */
667 if (copy_from_user(&buffer[USBTMC_HEADER_SIZE], buf + done, this_part)) {
672 n_bytes = roundup(USBTMC_HEADER_SIZE + this_part, 4);
673 memset(buffer + USBTMC_HEADER_SIZE + this_part, 0, n_bytes - (USBTMC_HEADER_SIZE + this_part));
676 retval = usb_bulk_msg(data->usb_dev,
677 usb_sndbulkpipe(data->usb_dev,
680 &actual, USBTMC_TIMEOUT);
686 data->bTag_last_write = data->bTag;
693 dev_err(&data->intf->dev,
694 "Unable to send data, error %d\n", retval);
695 if (data->auto_abort)
696 usbtmc_ioctl_abort_bulk_out(data);
700 remaining -= this_part;
706 mutex_unlock(&data->io_mutex);
711 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
713 struct usb_host_interface *current_setting;
714 struct usb_endpoint_descriptor *desc;
722 dev = &data->intf->dev;
724 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
726 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
730 rv = usb_control_msg(data->usb_dev,
731 usb_rcvctrlpipe(data->usb_dev, 0),
732 USBTMC_REQUEST_INITIATE_CLEAR,
733 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
734 0, 0, buffer, 1, USBTMC_TIMEOUT);
736 dev_err(dev, "usb_control_msg returned %d\n", rv);
740 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
742 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
743 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
749 current_setting = data->intf->cur_altsetting;
750 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
751 desc = ¤t_setting->endpoint[n].desc;
752 if (desc->bEndpointAddress == data->bulk_in)
753 max_size = usb_endpoint_maxp(desc);
757 dev_err(dev, "Couldn't get wMaxPacketSize\n");
762 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
766 usbtmc_clear_check_status:
768 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
770 rv = usb_control_msg(data->usb_dev,
771 usb_rcvctrlpipe(data->usb_dev, 0),
772 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
773 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
774 0, 0, buffer, 2, USBTMC_TIMEOUT);
776 dev_err(dev, "usb_control_msg returned %d\n", rv);
780 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
782 if (buffer[0] == USBTMC_STATUS_SUCCESS)
783 goto usbtmc_clear_bulk_out_halt;
785 if (buffer[0] != USBTMC_STATUS_PENDING) {
786 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
793 dev_dbg(dev, "Reading from bulk in EP\n");
795 rv = usb_bulk_msg(data->usb_dev,
796 usb_rcvbulkpipe(data->usb_dev,
798 buffer, USBTMC_SIZE_IOBUFFER,
799 &actual, USBTMC_TIMEOUT);
803 dev_err(dev, "usb_control_msg returned %d\n",
807 } while ((actual == max_size) &&
808 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
810 if (actual == max_size) {
811 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
812 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
817 goto usbtmc_clear_check_status;
819 usbtmc_clear_bulk_out_halt:
821 rv = usb_clear_halt(data->usb_dev,
822 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
824 dev_err(dev, "usb_control_msg returned %d\n", rv);
834 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
838 rv = usb_clear_halt(data->usb_dev,
839 usb_sndbulkpipe(data->usb_dev, data->bulk_out));
842 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
849 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
853 rv = usb_clear_halt(data->usb_dev,
854 usb_rcvbulkpipe(data->usb_dev, data->bulk_in));
857 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
864 static int get_capabilities(struct usbtmc_device_data *data)
866 struct device *dev = &data->usb_dev->dev;
870 buffer = kmalloc(0x18, GFP_KERNEL);
874 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
875 USBTMC_REQUEST_GET_CAPABILITIES,
876 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
877 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
879 dev_err(dev, "usb_control_msg returned %d\n", rv);
883 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
884 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
885 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
889 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
890 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
891 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
892 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
894 data->capabilities.interface_capabilities = buffer[4];
895 data->capabilities.device_capabilities = buffer[5];
896 data->capabilities.usb488_interface_capabilities = buffer[14];
897 data->capabilities.usb488_device_capabilities = buffer[15];
905 #define capability_attribute(name) \
906 static ssize_t name##_show(struct device *dev, \
907 struct device_attribute *attr, char *buf) \
909 struct usb_interface *intf = to_usb_interface(dev); \
910 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
912 return sprintf(buf, "%d\n", data->capabilities.name); \
914 static DEVICE_ATTR_RO(name)
916 capability_attribute(interface_capabilities);
917 capability_attribute(device_capabilities);
918 capability_attribute(usb488_interface_capabilities);
919 capability_attribute(usb488_device_capabilities);
921 static struct attribute *capability_attrs[] = {
922 &dev_attr_interface_capabilities.attr,
923 &dev_attr_device_capabilities.attr,
924 &dev_attr_usb488_interface_capabilities.attr,
925 &dev_attr_usb488_device_capabilities.attr,
929 static struct attribute_group capability_attr_grp = {
930 .attrs = capability_attrs,
933 static ssize_t TermChar_show(struct device *dev,
934 struct device_attribute *attr, char *buf)
936 struct usb_interface *intf = to_usb_interface(dev);
937 struct usbtmc_device_data *data = usb_get_intfdata(intf);
939 return sprintf(buf, "%c\n", data->TermChar);
942 static ssize_t TermChar_store(struct device *dev,
943 struct device_attribute *attr,
944 const char *buf, size_t count)
946 struct usb_interface *intf = to_usb_interface(dev);
947 struct usbtmc_device_data *data = usb_get_intfdata(intf);
951 data->TermChar = buf[0];
954 static DEVICE_ATTR_RW(TermChar);
956 #define data_attribute(name) \
957 static ssize_t name##_show(struct device *dev, \
958 struct device_attribute *attr, char *buf) \
960 struct usb_interface *intf = to_usb_interface(dev); \
961 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
963 return sprintf(buf, "%d\n", data->name); \
965 static ssize_t name##_store(struct device *dev, \
966 struct device_attribute *attr, \
967 const char *buf, size_t count) \
969 struct usb_interface *intf = to_usb_interface(dev); \
970 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
974 result = sscanf(buf, "%u\n", &val); \
983 static DEVICE_ATTR_RW(name)
985 data_attribute(TermCharEnabled);
986 data_attribute(auto_abort);
988 static struct attribute *data_attrs[] = {
989 &dev_attr_TermChar.attr,
990 &dev_attr_TermCharEnabled.attr,
991 &dev_attr_auto_abort.attr,
995 static struct attribute_group data_attr_grp = {
999 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
1005 dev = &data->intf->dev;
1007 buffer = kmalloc(2, GFP_KERNEL);
1011 rv = usb_control_msg(data->usb_dev,
1012 usb_rcvctrlpipe(data->usb_dev, 0),
1013 USBTMC_REQUEST_INDICATOR_PULSE,
1014 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
1015 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
1018 dev_err(dev, "usb_control_msg returned %d\n", rv);
1022 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1024 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
1025 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
1036 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1038 struct usbtmc_device_data *data;
1039 int retval = -EBADRQC;
1041 data = file->private_data;
1042 mutex_lock(&data->io_mutex);
1045 goto skip_io_on_zombie;
1049 case USBTMC_IOCTL_CLEAR_OUT_HALT:
1050 retval = usbtmc_ioctl_clear_out_halt(data);
1053 case USBTMC_IOCTL_CLEAR_IN_HALT:
1054 retval = usbtmc_ioctl_clear_in_halt(data);
1057 case USBTMC_IOCTL_INDICATOR_PULSE:
1058 retval = usbtmc_ioctl_indicator_pulse(data);
1061 case USBTMC_IOCTL_CLEAR:
1062 retval = usbtmc_ioctl_clear(data);
1065 case USBTMC_IOCTL_ABORT_BULK_OUT:
1066 retval = usbtmc_ioctl_abort_bulk_out(data);
1069 case USBTMC_IOCTL_ABORT_BULK_IN:
1070 retval = usbtmc_ioctl_abort_bulk_in(data);
1075 mutex_unlock(&data->io_mutex);
1079 static const struct file_operations fops = {
1080 .owner = THIS_MODULE,
1081 .read = usbtmc_read,
1082 .write = usbtmc_write,
1083 .open = usbtmc_open,
1084 .release = usbtmc_release,
1085 .unlocked_ioctl = usbtmc_ioctl,
1086 .llseek = default_llseek,
1089 static struct usb_class_driver usbtmc_class = {
1092 .minor_base = USBTMC_MINOR_BASE,
1096 static int usbtmc_probe(struct usb_interface *intf,
1097 const struct usb_device_id *id)
1099 struct usbtmc_device_data *data;
1100 struct usb_host_interface *iface_desc;
1101 struct usb_endpoint_descriptor *endpoint;
1105 dev_dbg(&intf->dev, "%s called\n", __func__);
1107 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
1113 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
1114 usb_set_intfdata(intf, data);
1115 kref_init(&data->kref);
1116 mutex_init(&data->io_mutex);
1119 /* Determine if it is a Rigol or not */
1120 data->rigol_quirk = 0;
1121 dev_dbg(&intf->dev, "Trying to find if device Vendor 0x%04X Product 0x%04X has the RIGOL quirk\n",
1122 le16_to_cpu(data->usb_dev->descriptor.idVendor),
1123 le16_to_cpu(data->usb_dev->descriptor.idProduct));
1124 for(n = 0; usbtmc_id_quirk[n].idVendor > 0; n++) {
1125 if ((usbtmc_id_quirk[n].idVendor == le16_to_cpu(data->usb_dev->descriptor.idVendor)) &&
1126 (usbtmc_id_quirk[n].idProduct == le16_to_cpu(data->usb_dev->descriptor.idProduct))) {
1127 dev_dbg(&intf->dev, "Setting this device as having the RIGOL quirk\n");
1128 data->rigol_quirk = 1;
1133 /* Initialize USBTMC bTag and other fields */
1135 data->TermCharEnabled = 0;
1136 data->TermChar = '\n';
1138 /* USBTMC devices have only one setting, so use that */
1139 iface_desc = data->intf->cur_altsetting;
1141 /* Find bulk in endpoint */
1142 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1143 endpoint = &iface_desc->endpoint[n].desc;
1145 if (usb_endpoint_is_bulk_in(endpoint)) {
1146 data->bulk_in = endpoint->bEndpointAddress;
1147 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1153 /* Find bulk out endpoint */
1154 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1155 endpoint = &iface_desc->endpoint[n].desc;
1157 if (usb_endpoint_is_bulk_out(endpoint)) {
1158 data->bulk_out = endpoint->bEndpointAddress;
1159 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1165 retcode = get_capabilities(data);
1167 dev_err(&intf->dev, "can't read capabilities\n");
1169 retcode = sysfs_create_group(&intf->dev.kobj,
1170 &capability_attr_grp);
1172 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1174 retcode = usb_register_dev(intf, &usbtmc_class);
1176 dev_err(&intf->dev, "Not able to get a minor"
1177 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1179 goto error_register;
1181 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1186 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1187 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1188 kref_put(&data->kref, usbtmc_delete);
1192 static void usbtmc_disconnect(struct usb_interface *intf)
1194 struct usbtmc_device_data *data;
1196 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1198 data = usb_get_intfdata(intf);
1199 usb_deregister_dev(intf, &usbtmc_class);
1200 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1201 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1202 mutex_lock(&data->io_mutex);
1204 mutex_unlock(&data->io_mutex);
1205 kref_put(&data->kref, usbtmc_delete);
1208 static int usbtmc_suspend(struct usb_interface *intf, pm_message_t message)
1210 /* this driver does not have pending URBs */
1214 static int usbtmc_resume(struct usb_interface *intf)
1219 static struct usb_driver usbtmc_driver = {
1221 .id_table = usbtmc_devices,
1222 .probe = usbtmc_probe,
1223 .disconnect = usbtmc_disconnect,
1224 .suspend = usbtmc_suspend,
1225 .resume = usbtmc_resume,
1228 module_usb_driver(usbtmc_driver);
1230 MODULE_LICENSE("GPL");