2 * drivers/usb/class/usbtmc.c - USB Test & Measurment 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 #include <linux/init.h>
23 #include <linux/module.h>
25 #include <linux/uaccess.h>
26 #include <linux/kref.h>
27 #include <linux/mutex.h>
28 #include <linux/usb.h>
29 #include <linux/usb/tmc.h>
32 #define USBTMC_MINOR_BASE 176
35 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
36 * large as wMaxPacketSize (which is usually 512 bytes).
38 #define USBTMC_SIZE_IOBUFFER 2048
40 /* Default USB timeout (in milliseconds) */
41 #define USBTMC_TIMEOUT 10
44 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
45 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
46 * packet is never read.
48 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
50 static struct usb_device_id usbtmc_devices[] = {
51 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC, 3, 0), },
52 { 0, } /* terminating entry */
56 * This structure is the capabilities for the device
57 * See section 4.2.1.8 of the USBTMC specification for details.
59 struct usbtmc_dev_capabilities {
60 __u8 interface_capabilities;
61 __u8 device_capabilities;
62 __u8 usb488_interface_capabilities;
63 __u8 usb488_device_capabilities;
66 /* This structure holds private data for each USBTMC device. One copy is
67 * allocated for each USBTMC device in the driver's probe function.
69 struct usbtmc_device_data {
70 const struct usb_device_id *id;
71 struct usb_device *usb_dev;
72 struct usb_interface *intf;
75 unsigned int bulk_out;
78 u8 bTag_last_write; /* needed for abort */
79 u8 bTag_last_read; /* needed for abort */
81 /* attributes from the USB TMC spec for this device */
86 struct usbtmc_dev_capabilities capabilities;
88 struct mutex io_mutex; /* only one i/o function running at a time */
90 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
92 /* Forward declarations */
93 static struct usb_driver usbtmc_driver;
95 static void usbtmc_delete(struct kref *kref)
97 struct usbtmc_device_data *data = to_usbtmc_data(kref);
99 usb_put_dev(data->usb_dev);
103 static int usbtmc_open(struct inode *inode, struct file *filp)
105 struct usb_interface *intf;
106 struct usbtmc_device_data *data;
107 int retval = -ENODEV;
109 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
111 printk(KERN_ERR KBUILD_MODNAME
112 ": can not find device for minor %d", iminor(inode));
116 data = usb_get_intfdata(intf);
117 kref_get(&data->kref);
119 /* Store pointer in file structure's private data field */
120 filp->private_data = data;
126 static int usbtmc_release(struct inode *inode, struct file *file)
128 struct usbtmc_device_data *data = file->private_data;
130 kref_put(&data->kref, usbtmc_delete);
134 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
141 struct usb_host_interface *current_setting;
144 dev = &data->intf->dev;
145 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
149 rv = usb_control_msg(data->usb_dev,
150 usb_rcvctrlpipe(data->usb_dev, 0),
151 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
152 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
153 data->bTag_last_read, data->bulk_in,
154 buffer, 2, USBTMC_TIMEOUT);
157 dev_err(dev, "usb_control_msg returned %d\n", rv);
161 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
163 if (buffer[0] == USBTMC_STATUS_FAILED) {
168 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
169 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
176 current_setting = data->intf->cur_altsetting;
177 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
178 if (current_setting->endpoint[n].desc.bEndpointAddress ==
180 max_size = le16_to_cpu(current_setting->endpoint[n].
181 desc.wMaxPacketSize);
184 dev_err(dev, "Couldn't get wMaxPacketSize\n");
189 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
194 dev_dbg(dev, "Reading from bulk in EP\n");
196 rv = usb_bulk_msg(data->usb_dev,
197 usb_rcvbulkpipe(data->usb_dev,
199 buffer, USBTMC_SIZE_IOBUFFER,
200 &actual, USBTMC_TIMEOUT);
205 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
208 } while ((actual == max_size) &&
209 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
211 if (actual == max_size) {
212 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
213 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
220 usbtmc_abort_bulk_in_status:
221 rv = usb_control_msg(data->usb_dev,
222 usb_rcvctrlpipe(data->usb_dev, 0),
223 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
224 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
225 0, data->bulk_in, buffer, 0x08,
229 dev_err(dev, "usb_control_msg returned %d\n", rv);
233 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
235 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
240 if (buffer[0] != USBTMC_STATUS_PENDING) {
241 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
248 dev_dbg(dev, "Reading from bulk in EP\n");
250 rv = usb_bulk_msg(data->usb_dev,
251 usb_rcvbulkpipe(data->usb_dev,
253 buffer, USBTMC_SIZE_IOBUFFER,
254 &actual, USBTMC_TIMEOUT);
259 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
262 } while ((actual = max_size) &&
263 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
265 if (actual == max_size) {
266 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
267 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
272 goto usbtmc_abort_bulk_in_status;
280 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
287 dev = &data->intf->dev;
289 buffer = kmalloc(8, GFP_KERNEL);
293 rv = usb_control_msg(data->usb_dev,
294 usb_rcvctrlpipe(data->usb_dev, 0),
295 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
296 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
297 data->bTag_last_write, data->bulk_out,
298 buffer, 2, USBTMC_TIMEOUT);
301 dev_err(dev, "usb_control_msg returned %d\n", rv);
305 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
307 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
308 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
316 usbtmc_abort_bulk_out_check_status:
317 rv = usb_control_msg(data->usb_dev,
318 usb_rcvctrlpipe(data->usb_dev, 0),
319 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
320 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
321 0, data->bulk_out, buffer, 0x08,
325 dev_err(dev, "usb_control_msg returned %d\n", rv);
329 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
331 if (buffer[0] == USBTMC_STATUS_SUCCESS)
332 goto usbtmc_abort_bulk_out_clear_halt;
334 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
335 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
336 goto usbtmc_abort_bulk_out_check_status;
341 usbtmc_abort_bulk_out_clear_halt:
342 rv = usb_control_msg(data->usb_dev,
343 usb_sndctrlpipe(data->usb_dev, 0),
344 USB_REQ_CLEAR_FEATURE,
345 USB_DIR_OUT | USB_TYPE_STANDARD |
347 USB_ENDPOINT_HALT, data->bulk_out, buffer,
351 dev_err(dev, "usb_control_msg returned %d\n", rv);
361 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
362 size_t count, loff_t *f_pos)
364 struct usbtmc_device_data *data;
366 unsigned long int n_characters;
374 /* Get pointer to private data structure */
375 data = filp->private_data;
376 dev = &data->intf->dev;
378 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
382 mutex_lock(&data->io_mutex);
387 while (remaining > 0) {
388 if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
389 this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
391 this_part = remaining;
393 /* Setup IO buffer for 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] = (this_part - 12 - 3) & 255;
401 buffer[5] = ((this_part - 12 - 3) >> 8) & 255;
402 buffer[6] = ((this_part - 12 - 3) >> 16) & 255;
403 buffer[7] = ((this_part - 12 - 3) >> 24) & 255;
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, 12, &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 */
425 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
426 if (data->auto_abort)
427 usbtmc_ioctl_abort_bulk_out(data);
432 retval = usb_bulk_msg(data->usb_dev,
433 usb_rcvbulkpipe(data->usb_dev,
435 buffer, USBTMC_SIZE_IOBUFFER, &actual,
438 /* Store bTag (in case we need to abort) */
439 data->bTag_last_read = data->bTag;
442 dev_err(dev, "Unable to read data, error %d\n", retval);
443 if (data->auto_abort)
444 usbtmc_ioctl_abort_bulk_in(data);
448 /* How many characters did the instrument send? */
449 n_characters = buffer[4] +
454 /* Copy buffer to user space */
455 if (copy_to_user(buf + done, &buffer[12], n_characters)) {
456 /* There must have been an addressing problem */
461 done += n_characters;
462 if (n_characters < USBTMC_SIZE_IOBUFFER)
466 /* Update file position value */
467 *f_pos = *f_pos + done;
471 mutex_unlock(&data->io_mutex);
476 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
477 size_t count, loff_t *f_pos)
479 struct usbtmc_device_data *data;
483 unsigned long int n_bytes;
489 data = filp->private_data;
491 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
495 mutex_lock(&data->io_mutex);
500 while (remaining > 0) {
501 if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
502 this_part = USBTMC_SIZE_IOBUFFER - 12;
505 this_part = remaining;
509 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
511 buffer[1] = data->bTag;
512 buffer[2] = ~(data->bTag);
513 buffer[3] = 0; /* Reserved */
514 buffer[4] = this_part & 255;
515 buffer[5] = (this_part >> 8) & 255;
516 buffer[6] = (this_part >> 16) & 255;
517 buffer[7] = (this_part >> 24) & 255;
518 /* buffer[8] is set above... */
519 buffer[9] = 0; /* Reserved */
520 buffer[10] = 0; /* Reserved */
521 buffer[11] = 0; /* Reserved */
523 if (copy_from_user(&buffer[12], buf + done, this_part)) {
528 n_bytes = 12 + this_part;
530 n_bytes += 4 - this_part % 4;
531 for (n = 12 + this_part; n < n_bytes; n++)
534 retval = usb_bulk_msg(data->usb_dev,
535 usb_sndbulkpipe(data->usb_dev,
537 buffer, n_bytes, &actual, USBTMC_TIMEOUT);
539 data->bTag_last_write = data->bTag;
546 dev_err(&data->intf->dev,
547 "Unable to send data, error %d\n", retval);
548 if (data->auto_abort)
549 usbtmc_ioctl_abort_bulk_out(data);
553 remaining -= this_part;
559 mutex_unlock(&data->io_mutex);
564 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
566 struct usb_host_interface *current_setting;
567 struct usb_endpoint_descriptor *desc;
575 dev = &data->intf->dev;
577 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
579 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
583 rv = usb_control_msg(data->usb_dev,
584 usb_rcvctrlpipe(data->usb_dev, 0),
585 USBTMC_REQUEST_INITIATE_CLEAR,
586 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
587 0, 0, buffer, 1, USBTMC_TIMEOUT);
589 dev_err(dev, "usb_control_msg returned %d\n", rv);
593 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
595 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
596 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
602 current_setting = data->intf->cur_altsetting;
603 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
604 desc = ¤t_setting->endpoint[n].desc;
605 if (desc->bEndpointAddress == data->bulk_in)
606 max_size = le16_to_cpu(desc->wMaxPacketSize);
610 dev_err(dev, "Couldn't get wMaxPacketSize\n");
615 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
619 usbtmc_clear_check_status:
621 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
623 rv = usb_control_msg(data->usb_dev,
624 usb_rcvctrlpipe(data->usb_dev, 0),
625 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
626 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
627 0, 0, buffer, 2, USBTMC_TIMEOUT);
629 dev_err(dev, "usb_control_msg returned %d\n", rv);
633 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
635 if (buffer[0] == USBTMC_STATUS_SUCCESS)
636 goto usbtmc_clear_bulk_out_halt;
638 if (buffer[0] != USBTMC_STATUS_PENDING) {
639 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
646 dev_dbg(dev, "Reading from bulk in EP\n");
648 rv = usb_bulk_msg(data->usb_dev,
649 usb_rcvbulkpipe(data->usb_dev,
651 buffer, USBTMC_SIZE_IOBUFFER,
652 &actual, USBTMC_TIMEOUT);
656 dev_err(dev, "usb_control_msg returned %d\n",
660 } while ((actual == max_size) &&
661 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
663 if (actual == max_size) {
664 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
665 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
670 goto usbtmc_clear_check_status;
672 usbtmc_clear_bulk_out_halt:
674 rv = usb_control_msg(data->usb_dev,
675 usb_sndctrlpipe(data->usb_dev, 0),
676 USB_REQ_CLEAR_FEATURE,
677 USB_DIR_OUT | USB_TYPE_STANDARD |
680 data->bulk_out, buffer, 0,
683 dev_err(dev, "usb_control_msg returned %d\n", rv);
693 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
698 buffer = kmalloc(2, GFP_KERNEL);
702 rv = usb_control_msg(data->usb_dev,
703 usb_sndctrlpipe(data->usb_dev, 0),
704 USB_REQ_CLEAR_FEATURE,
705 USB_DIR_OUT | USB_TYPE_STANDARD |
707 USB_ENDPOINT_HALT, data->bulk_out,
708 buffer, 0, USBTMC_TIMEOUT);
711 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
722 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
727 buffer = kmalloc(2, GFP_KERNEL);
731 rv = usb_control_msg(data->usb_dev, usb_sndctrlpipe(data->usb_dev, 0),
732 USB_REQ_CLEAR_FEATURE,
733 USB_DIR_OUT | USB_TYPE_STANDARD |
735 USB_ENDPOINT_HALT, data->bulk_in, buffer, 0,
739 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
750 static int get_capabilities(struct usbtmc_device_data *data)
752 struct device *dev = &data->usb_dev->dev;
756 buffer = kmalloc(0x18, GFP_KERNEL);
760 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
761 USBTMC_REQUEST_GET_CAPABILITIES,
762 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
763 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
765 dev_err(dev, "usb_control_msg returned %d\n", rv);
769 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
770 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
771 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
772 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
773 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
774 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
775 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
779 data->capabilities.interface_capabilities = buffer[4];
780 data->capabilities.device_capabilities = buffer[5];
781 data->capabilities.usb488_interface_capabilities = buffer[14];
782 data->capabilities.usb488_device_capabilities = buffer[15];
788 #define capability_attribute(name) \
789 static ssize_t show_##name(struct device *dev, \
790 struct device_attribute *attr, char *buf) \
792 struct usb_interface *intf = to_usb_interface(dev); \
793 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
795 return sprintf(buf, "%d\n", data->capabilities.name); \
797 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
799 capability_attribute(interface_capabilities);
800 capability_attribute(device_capabilities);
801 capability_attribute(usb488_interface_capabilities);
802 capability_attribute(usb488_device_capabilities);
804 static struct attribute *capability_attrs[] = {
805 &dev_attr_interface_capabilities.attr,
806 &dev_attr_device_capabilities.attr,
807 &dev_attr_usb488_interface_capabilities.attr,
808 &dev_attr_usb488_device_capabilities.attr,
812 static struct attribute_group capability_attr_grp = {
813 .attrs = capability_attrs,
816 static ssize_t show_TermChar(struct device *dev,
817 struct device_attribute *attr, char *buf)
819 struct usb_interface *intf = to_usb_interface(dev);
820 struct usbtmc_device_data *data = usb_get_intfdata(intf);
822 return sprintf(buf, "%c\n", data->TermChar);
825 static ssize_t store_TermChar(struct device *dev,
826 struct device_attribute *attr,
827 const char *buf, size_t count)
829 struct usb_interface *intf = to_usb_interface(dev);
830 struct usbtmc_device_data *data = usb_get_intfdata(intf);
834 data->TermChar = buf[0];
837 static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
839 #define data_attribute(name) \
840 static ssize_t show_##name(struct device *dev, \
841 struct device_attribute *attr, char *buf) \
843 struct usb_interface *intf = to_usb_interface(dev); \
844 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
846 return sprintf(buf, "%d\n", data->name); \
848 static ssize_t store_##name(struct device *dev, \
849 struct device_attribute *attr, \
850 const char *buf, size_t count) \
852 struct usb_interface *intf = to_usb_interface(dev); \
853 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
857 result = sscanf(buf, "%u\n", &val); \
866 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
868 data_attribute(TermCharEnabled);
869 data_attribute(auto_abort);
871 static struct attribute *data_attrs[] = {
872 &dev_attr_TermChar.attr,
873 &dev_attr_TermCharEnabled.attr,
874 &dev_attr_auto_abort.attr,
878 static struct attribute_group data_attr_grp = {
882 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
888 dev = &data->intf->dev;
890 buffer = kmalloc(2, GFP_KERNEL);
894 rv = usb_control_msg(data->usb_dev,
895 usb_rcvctrlpipe(data->usb_dev, 0),
896 USBTMC_REQUEST_INDICATOR_PULSE,
897 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
898 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
901 dev_err(dev, "usb_control_msg returned %d\n", rv);
905 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
907 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
908 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
919 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
921 struct usbtmc_device_data *data;
922 int retval = -EBADRQC;
924 data = file->private_data;
925 mutex_lock(&data->io_mutex);
928 case USBTMC_IOCTL_CLEAR_OUT_HALT:
929 retval = usbtmc_ioctl_clear_out_halt(data);
931 case USBTMC_IOCTL_CLEAR_IN_HALT:
932 retval = usbtmc_ioctl_clear_in_halt(data);
934 case USBTMC_IOCTL_INDICATOR_PULSE:
935 retval = usbtmc_ioctl_indicator_pulse(data);
937 case USBTMC_IOCTL_CLEAR:
938 retval = usbtmc_ioctl_clear(data);
940 case USBTMC_IOCTL_ABORT_BULK_OUT:
941 retval = usbtmc_ioctl_abort_bulk_out(data);
943 case USBTMC_IOCTL_ABORT_BULK_IN:
944 retval = usbtmc_ioctl_abort_bulk_in(data);
947 mutex_unlock(&data->io_mutex);
951 static struct file_operations fops = {
952 .owner = THIS_MODULE,
954 .write = usbtmc_write,
956 .release = usbtmc_release,
957 .unlocked_ioctl = usbtmc_ioctl,
960 static struct usb_class_driver usbtmc_class = {
963 .minor_base = USBTMC_MINOR_BASE,
967 static int usbtmc_probe(struct usb_interface *intf,
968 const struct usb_device_id *id)
970 struct usbtmc_device_data *data;
971 struct usb_host_interface *iface_desc;
972 struct usb_endpoint_descriptor *endpoint;
976 dev_dbg(&intf->dev, "%s called\n", __func__);
978 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
980 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
986 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
987 usb_set_intfdata(intf, data);
988 kref_init(&data->kref);
989 mutex_init(&data->io_mutex);
991 /* Initialize USBTMC bTag and other fields */
993 data->TermCharEnabled = 0;
994 data->TermChar = '\n';
996 /* USBTMC devices have only one setting, so use that */
997 iface_desc = data->intf->cur_altsetting;
999 /* Find bulk in endpoint */
1000 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1001 endpoint = &iface_desc->endpoint[n].desc;
1003 if (usb_endpoint_is_bulk_in(endpoint)) {
1004 data->bulk_in = endpoint->bEndpointAddress;
1005 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1011 /* Find bulk out endpoint */
1012 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1013 endpoint = &iface_desc->endpoint[n].desc;
1015 if (usb_endpoint_is_bulk_out(endpoint)) {
1016 data->bulk_out = endpoint->bEndpointAddress;
1017 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1023 retcode = get_capabilities(data);
1025 dev_err(&intf->dev, "can't read capabilities\n");
1027 retcode = sysfs_create_group(&intf->dev.kobj,
1028 &capability_attr_grp);
1030 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1032 retcode = usb_register_dev(intf, &usbtmc_class);
1034 dev_err(&intf->dev, "Not able to get a minor"
1035 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1037 goto error_register;
1039 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1044 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1045 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1046 kref_put(&data->kref, usbtmc_delete);
1050 static void usbtmc_disconnect(struct usb_interface *intf)
1052 struct usbtmc_device_data *data;
1054 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1056 data = usb_get_intfdata(intf);
1057 usb_deregister_dev(intf, &usbtmc_class);
1058 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1059 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1060 kref_put(&data->kref, usbtmc_delete);
1063 static struct usb_driver usbtmc_driver = {
1065 .id_table = usbtmc_devices,
1066 .probe = usbtmc_probe,
1067 .disconnect = usbtmc_disconnect
1070 static int __init usbtmc_init(void)
1074 retcode = usb_register(&usbtmc_driver);
1076 printk(KERN_ERR KBUILD_MODNAME": Unable to register driver\n");
1079 module_init(usbtmc_init);
1081 static void __exit usbtmc_exit(void)
1083 usb_deregister(&usbtmc_driver);
1085 module_exit(usbtmc_exit);
1087 MODULE_LICENSE("GPL");