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 */
54 MODULE_DEVICE_TABLE(usb, usbtmc_devices);
57 * This structure is the capabilities for the device
58 * See section 4.2.1.8 of the USBTMC specification for details.
60 struct usbtmc_dev_capabilities {
61 __u8 interface_capabilities;
62 __u8 device_capabilities;
63 __u8 usb488_interface_capabilities;
64 __u8 usb488_device_capabilities;
67 /* This structure holds private data for each USBTMC device. One copy is
68 * allocated for each USBTMC device in the driver's probe function.
70 struct usbtmc_device_data {
71 const struct usb_device_id *id;
72 struct usb_device *usb_dev;
73 struct usb_interface *intf;
76 unsigned int bulk_out;
79 u8 bTag_last_write; /* needed for abort */
80 u8 bTag_last_read; /* needed for abort */
82 /* attributes from the USB TMC spec for this device */
87 struct usbtmc_dev_capabilities capabilities;
89 struct mutex io_mutex; /* only one i/o function running at a time */
91 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
93 /* Forward declarations */
94 static struct usb_driver usbtmc_driver;
96 static void usbtmc_delete(struct kref *kref)
98 struct usbtmc_device_data *data = to_usbtmc_data(kref);
100 usb_put_dev(data->usb_dev);
104 static int usbtmc_open(struct inode *inode, struct file *filp)
106 struct usb_interface *intf;
107 struct usbtmc_device_data *data;
108 int retval = -ENODEV;
110 intf = usb_find_interface(&usbtmc_driver, iminor(inode));
112 printk(KERN_ERR KBUILD_MODNAME
113 ": can not find device for minor %d", iminor(inode));
117 data = usb_get_intfdata(intf);
118 kref_get(&data->kref);
120 /* Store pointer in file structure's private data field */
121 filp->private_data = data;
127 static int usbtmc_release(struct inode *inode, struct file *file)
129 struct usbtmc_device_data *data = file->private_data;
131 kref_put(&data->kref, usbtmc_delete);
135 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data *data)
142 struct usb_host_interface *current_setting;
145 dev = &data->intf->dev;
146 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
150 rv = usb_control_msg(data->usb_dev,
151 usb_rcvctrlpipe(data->usb_dev, 0),
152 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN,
153 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
154 data->bTag_last_read, data->bulk_in,
155 buffer, 2, USBTMC_TIMEOUT);
158 dev_err(dev, "usb_control_msg returned %d\n", rv);
162 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
164 if (buffer[0] == USBTMC_STATUS_FAILED) {
169 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
170 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n",
177 current_setting = data->intf->cur_altsetting;
178 for (n = 0; n < current_setting->desc.bNumEndpoints; n++)
179 if (current_setting->endpoint[n].desc.bEndpointAddress ==
181 max_size = le16_to_cpu(current_setting->endpoint[n].
182 desc.wMaxPacketSize);
185 dev_err(dev, "Couldn't get wMaxPacketSize\n");
190 dev_dbg(&data->intf->dev, "wMaxPacketSize is %d\n", max_size);
195 dev_dbg(dev, "Reading from bulk in EP\n");
197 rv = usb_bulk_msg(data->usb_dev,
198 usb_rcvbulkpipe(data->usb_dev,
200 buffer, USBTMC_SIZE_IOBUFFER,
201 &actual, USBTMC_TIMEOUT);
206 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
209 } while ((actual == max_size) &&
210 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
212 if (actual == max_size) {
213 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
214 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
221 usbtmc_abort_bulk_in_status:
222 rv = usb_control_msg(data->usb_dev,
223 usb_rcvctrlpipe(data->usb_dev, 0),
224 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS,
225 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
226 0, data->bulk_in, buffer, 0x08,
230 dev_err(dev, "usb_control_msg returned %d\n", rv);
234 dev_dbg(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
236 if (buffer[0] == USBTMC_STATUS_SUCCESS) {
241 if (buffer[0] != USBTMC_STATUS_PENDING) {
242 dev_err(dev, "INITIATE_ABORT_BULK_IN returned %x\n", buffer[0]);
249 dev_dbg(dev, "Reading from bulk in EP\n");
251 rv = usb_bulk_msg(data->usb_dev,
252 usb_rcvbulkpipe(data->usb_dev,
254 buffer, USBTMC_SIZE_IOBUFFER,
255 &actual, USBTMC_TIMEOUT);
260 dev_err(dev, "usb_bulk_msg returned %d\n", rv);
263 } while ((actual = max_size) &&
264 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
266 if (actual == max_size) {
267 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
268 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
273 goto usbtmc_abort_bulk_in_status;
281 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data *data)
288 dev = &data->intf->dev;
290 buffer = kmalloc(8, GFP_KERNEL);
294 rv = usb_control_msg(data->usb_dev,
295 usb_rcvctrlpipe(data->usb_dev, 0),
296 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT,
297 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
298 data->bTag_last_write, data->bulk_out,
299 buffer, 2, USBTMC_TIMEOUT);
302 dev_err(dev, "usb_control_msg returned %d\n", rv);
306 dev_dbg(dev, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer[0]);
308 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
309 dev_err(dev, "INITIATE_ABORT_BULK_OUT returned %x\n",
317 usbtmc_abort_bulk_out_check_status:
318 rv = usb_control_msg(data->usb_dev,
319 usb_rcvctrlpipe(data->usb_dev, 0),
320 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS,
321 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_ENDPOINT,
322 0, data->bulk_out, buffer, 0x08,
326 dev_err(dev, "usb_control_msg returned %d\n", rv);
330 dev_dbg(dev, "CHECK_ABORT_BULK_OUT returned %x\n", buffer[0]);
332 if (buffer[0] == USBTMC_STATUS_SUCCESS)
333 goto usbtmc_abort_bulk_out_clear_halt;
335 if ((buffer[0] == USBTMC_STATUS_PENDING) &&
336 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN))
337 goto usbtmc_abort_bulk_out_check_status;
342 usbtmc_abort_bulk_out_clear_halt:
343 rv = usb_control_msg(data->usb_dev,
344 usb_sndctrlpipe(data->usb_dev, 0),
345 USB_REQ_CLEAR_FEATURE,
346 USB_DIR_OUT | USB_TYPE_STANDARD |
348 USB_ENDPOINT_HALT, data->bulk_out, buffer,
352 dev_err(dev, "usb_control_msg returned %d\n", rv);
362 static ssize_t usbtmc_read(struct file *filp, char __user *buf,
363 size_t count, loff_t *f_pos)
365 struct usbtmc_device_data *data;
367 unsigned long int n_characters;
375 /* Get pointer to private data structure */
376 data = filp->private_data;
377 dev = &data->intf->dev;
379 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
383 mutex_lock(&data->io_mutex);
388 while (remaining > 0) {
389 if (remaining > USBTMC_SIZE_IOBUFFER - 12 - 3)
390 this_part = USBTMC_SIZE_IOBUFFER - 12 - 3;
392 this_part = remaining;
394 /* Setup IO buffer for DEV_DEP_MSG_IN message
395 * Refer to class specs for details
398 buffer[1] = data->bTag;
399 buffer[2] = ~(data->bTag);
400 buffer[3] = 0; /* Reserved */
401 buffer[4] = (this_part - 12 - 3) & 255;
402 buffer[5] = ((this_part - 12 - 3) >> 8) & 255;
403 buffer[6] = ((this_part - 12 - 3) >> 16) & 255;
404 buffer[7] = ((this_part - 12 - 3) >> 24) & 255;
405 buffer[8] = data->TermCharEnabled * 2;
406 /* Use term character? */
407 buffer[9] = data->TermChar;
408 buffer[10] = 0; /* Reserved */
409 buffer[11] = 0; /* Reserved */
412 retval = usb_bulk_msg(data->usb_dev,
413 usb_sndbulkpipe(data->usb_dev,
415 buffer, 12, &actual, USBTMC_TIMEOUT);
417 /* Store bTag (in case we need to abort) */
418 data->bTag_last_write = data->bTag;
420 /* Increment bTag -- and increment again if zero */
426 dev_err(dev, "usb_bulk_msg returned %d\n", retval);
427 if (data->auto_abort)
428 usbtmc_ioctl_abort_bulk_out(data);
433 retval = usb_bulk_msg(data->usb_dev,
434 usb_rcvbulkpipe(data->usb_dev,
436 buffer, USBTMC_SIZE_IOBUFFER, &actual,
439 /* Store bTag (in case we need to abort) */
440 data->bTag_last_read = data->bTag;
443 dev_err(dev, "Unable to read data, error %d\n", retval);
444 if (data->auto_abort)
445 usbtmc_ioctl_abort_bulk_in(data);
449 /* How many characters did the instrument send? */
450 n_characters = buffer[4] +
455 /* Copy buffer to user space */
456 if (copy_to_user(buf + done, &buffer[12], n_characters)) {
457 /* There must have been an addressing problem */
462 done += n_characters;
463 if (n_characters < USBTMC_SIZE_IOBUFFER)
467 /* Update file position value */
468 *f_pos = *f_pos + done;
472 mutex_unlock(&data->io_mutex);
477 static ssize_t usbtmc_write(struct file *filp, const char __user *buf,
478 size_t count, loff_t *f_pos)
480 struct usbtmc_device_data *data;
484 unsigned long int n_bytes;
490 data = filp->private_data;
492 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
496 mutex_lock(&data->io_mutex);
501 while (remaining > 0) {
502 if (remaining > USBTMC_SIZE_IOBUFFER - 12) {
503 this_part = USBTMC_SIZE_IOBUFFER - 12;
506 this_part = remaining;
510 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
512 buffer[1] = data->bTag;
513 buffer[2] = ~(data->bTag);
514 buffer[3] = 0; /* Reserved */
515 buffer[4] = this_part & 255;
516 buffer[5] = (this_part >> 8) & 255;
517 buffer[6] = (this_part >> 16) & 255;
518 buffer[7] = (this_part >> 24) & 255;
519 /* buffer[8] is set above... */
520 buffer[9] = 0; /* Reserved */
521 buffer[10] = 0; /* Reserved */
522 buffer[11] = 0; /* Reserved */
524 if (copy_from_user(&buffer[12], buf + done, this_part)) {
529 n_bytes = 12 + this_part;
531 n_bytes += 4 - this_part % 4;
532 for (n = 12 + this_part; n < n_bytes; n++)
535 retval = usb_bulk_msg(data->usb_dev,
536 usb_sndbulkpipe(data->usb_dev,
538 buffer, n_bytes, &actual, USBTMC_TIMEOUT);
540 data->bTag_last_write = data->bTag;
547 dev_err(&data->intf->dev,
548 "Unable to send data, error %d\n", retval);
549 if (data->auto_abort)
550 usbtmc_ioctl_abort_bulk_out(data);
554 remaining -= this_part;
560 mutex_unlock(&data->io_mutex);
565 static int usbtmc_ioctl_clear(struct usbtmc_device_data *data)
567 struct usb_host_interface *current_setting;
568 struct usb_endpoint_descriptor *desc;
576 dev = &data->intf->dev;
578 dev_dbg(dev, "Sending INITIATE_CLEAR request\n");
580 buffer = kmalloc(USBTMC_SIZE_IOBUFFER, GFP_KERNEL);
584 rv = usb_control_msg(data->usb_dev,
585 usb_rcvctrlpipe(data->usb_dev, 0),
586 USBTMC_REQUEST_INITIATE_CLEAR,
587 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
588 0, 0, buffer, 1, USBTMC_TIMEOUT);
590 dev_err(dev, "usb_control_msg returned %d\n", rv);
594 dev_dbg(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
596 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
597 dev_err(dev, "INITIATE_CLEAR returned %x\n", buffer[0]);
603 current_setting = data->intf->cur_altsetting;
604 for (n = 0; n < current_setting->desc.bNumEndpoints; n++) {
605 desc = ¤t_setting->endpoint[n].desc;
606 if (desc->bEndpointAddress == data->bulk_in)
607 max_size = le16_to_cpu(desc->wMaxPacketSize);
611 dev_err(dev, "Couldn't get wMaxPacketSize\n");
616 dev_dbg(dev, "wMaxPacketSize is %d\n", max_size);
620 usbtmc_clear_check_status:
622 dev_dbg(dev, "Sending CHECK_CLEAR_STATUS request\n");
624 rv = usb_control_msg(data->usb_dev,
625 usb_rcvctrlpipe(data->usb_dev, 0),
626 USBTMC_REQUEST_CHECK_CLEAR_STATUS,
627 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
628 0, 0, buffer, 2, USBTMC_TIMEOUT);
630 dev_err(dev, "usb_control_msg returned %d\n", rv);
634 dev_dbg(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
636 if (buffer[0] == USBTMC_STATUS_SUCCESS)
637 goto usbtmc_clear_bulk_out_halt;
639 if (buffer[0] != USBTMC_STATUS_PENDING) {
640 dev_err(dev, "CHECK_CLEAR_STATUS returned %x\n", buffer[0]);
647 dev_dbg(dev, "Reading from bulk in EP\n");
649 rv = usb_bulk_msg(data->usb_dev,
650 usb_rcvbulkpipe(data->usb_dev,
652 buffer, USBTMC_SIZE_IOBUFFER,
653 &actual, USBTMC_TIMEOUT);
657 dev_err(dev, "usb_control_msg returned %d\n",
661 } while ((actual == max_size) &&
662 (n < USBTMC_MAX_READS_TO_CLEAR_BULK_IN));
664 if (actual == max_size) {
665 dev_err(dev, "Couldn't clear device buffer within %d cycles\n",
666 USBTMC_MAX_READS_TO_CLEAR_BULK_IN);
671 goto usbtmc_clear_check_status;
673 usbtmc_clear_bulk_out_halt:
675 rv = usb_control_msg(data->usb_dev,
676 usb_sndctrlpipe(data->usb_dev, 0),
677 USB_REQ_CLEAR_FEATURE,
678 USB_DIR_OUT | USB_TYPE_STANDARD |
681 data->bulk_out, buffer, 0,
684 dev_err(dev, "usb_control_msg returned %d\n", rv);
694 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data *data)
699 buffer = kmalloc(2, GFP_KERNEL);
703 rv = usb_control_msg(data->usb_dev,
704 usb_sndctrlpipe(data->usb_dev, 0),
705 USB_REQ_CLEAR_FEATURE,
706 USB_DIR_OUT | USB_TYPE_STANDARD |
708 USB_ENDPOINT_HALT, data->bulk_out,
709 buffer, 0, USBTMC_TIMEOUT);
712 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
723 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data *data)
728 buffer = kmalloc(2, GFP_KERNEL);
732 rv = usb_control_msg(data->usb_dev, usb_sndctrlpipe(data->usb_dev, 0),
733 USB_REQ_CLEAR_FEATURE,
734 USB_DIR_OUT | USB_TYPE_STANDARD |
736 USB_ENDPOINT_HALT, data->bulk_in, buffer, 0,
740 dev_err(&data->usb_dev->dev, "usb_control_msg returned %d\n",
751 static int get_capabilities(struct usbtmc_device_data *data)
753 struct device *dev = &data->usb_dev->dev;
757 buffer = kmalloc(0x18, GFP_KERNEL);
761 rv = usb_control_msg(data->usb_dev, usb_rcvctrlpipe(data->usb_dev, 0),
762 USBTMC_REQUEST_GET_CAPABILITIES,
763 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
764 0, 0, buffer, 0x18, USBTMC_TIMEOUT);
766 dev_err(dev, "usb_control_msg returned %d\n", rv);
770 dev_dbg(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
771 dev_dbg(dev, "Interface capabilities are %x\n", buffer[4]);
772 dev_dbg(dev, "Device capabilities are %x\n", buffer[5]);
773 dev_dbg(dev, "USB488 interface capabilities are %x\n", buffer[14]);
774 dev_dbg(dev, "USB488 device capabilities are %x\n", buffer[15]);
775 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
776 dev_err(dev, "GET_CAPABILITIES returned %x\n", buffer[0]);
780 data->capabilities.interface_capabilities = buffer[4];
781 data->capabilities.device_capabilities = buffer[5];
782 data->capabilities.usb488_interface_capabilities = buffer[14];
783 data->capabilities.usb488_device_capabilities = buffer[15];
789 #define capability_attribute(name) \
790 static ssize_t show_##name(struct device *dev, \
791 struct device_attribute *attr, char *buf) \
793 struct usb_interface *intf = to_usb_interface(dev); \
794 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
796 return sprintf(buf, "%d\n", data->capabilities.name); \
798 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
800 capability_attribute(interface_capabilities);
801 capability_attribute(device_capabilities);
802 capability_attribute(usb488_interface_capabilities);
803 capability_attribute(usb488_device_capabilities);
805 static struct attribute *capability_attrs[] = {
806 &dev_attr_interface_capabilities.attr,
807 &dev_attr_device_capabilities.attr,
808 &dev_attr_usb488_interface_capabilities.attr,
809 &dev_attr_usb488_device_capabilities.attr,
813 static struct attribute_group capability_attr_grp = {
814 .attrs = capability_attrs,
817 static ssize_t show_TermChar(struct device *dev,
818 struct device_attribute *attr, char *buf)
820 struct usb_interface *intf = to_usb_interface(dev);
821 struct usbtmc_device_data *data = usb_get_intfdata(intf);
823 return sprintf(buf, "%c\n", data->TermChar);
826 static ssize_t store_TermChar(struct device *dev,
827 struct device_attribute *attr,
828 const char *buf, size_t count)
830 struct usb_interface *intf = to_usb_interface(dev);
831 struct usbtmc_device_data *data = usb_get_intfdata(intf);
835 data->TermChar = buf[0];
838 static DEVICE_ATTR(TermChar, S_IRUGO, show_TermChar, store_TermChar);
840 #define data_attribute(name) \
841 static ssize_t show_##name(struct device *dev, \
842 struct device_attribute *attr, char *buf) \
844 struct usb_interface *intf = to_usb_interface(dev); \
845 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
847 return sprintf(buf, "%d\n", data->name); \
849 static ssize_t store_##name(struct device *dev, \
850 struct device_attribute *attr, \
851 const char *buf, size_t count) \
853 struct usb_interface *intf = to_usb_interface(dev); \
854 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
858 result = sscanf(buf, "%u\n", &val); \
867 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
869 data_attribute(TermCharEnabled);
870 data_attribute(auto_abort);
872 static struct attribute *data_attrs[] = {
873 &dev_attr_TermChar.attr,
874 &dev_attr_TermCharEnabled.attr,
875 &dev_attr_auto_abort.attr,
879 static struct attribute_group data_attr_grp = {
883 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data *data)
889 dev = &data->intf->dev;
891 buffer = kmalloc(2, GFP_KERNEL);
895 rv = usb_control_msg(data->usb_dev,
896 usb_rcvctrlpipe(data->usb_dev, 0),
897 USBTMC_REQUEST_INDICATOR_PULSE,
898 USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
899 0, 0, buffer, 0x01, USBTMC_TIMEOUT);
902 dev_err(dev, "usb_control_msg returned %d\n", rv);
906 dev_dbg(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
908 if (buffer[0] != USBTMC_STATUS_SUCCESS) {
909 dev_err(dev, "INDICATOR_PULSE returned %x\n", buffer[0]);
920 static long usbtmc_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
922 struct usbtmc_device_data *data;
923 int retval = -EBADRQC;
925 data = file->private_data;
926 mutex_lock(&data->io_mutex);
929 case USBTMC_IOCTL_CLEAR_OUT_HALT:
930 retval = usbtmc_ioctl_clear_out_halt(data);
932 case USBTMC_IOCTL_CLEAR_IN_HALT:
933 retval = usbtmc_ioctl_clear_in_halt(data);
935 case USBTMC_IOCTL_INDICATOR_PULSE:
936 retval = usbtmc_ioctl_indicator_pulse(data);
938 case USBTMC_IOCTL_CLEAR:
939 retval = usbtmc_ioctl_clear(data);
941 case USBTMC_IOCTL_ABORT_BULK_OUT:
942 retval = usbtmc_ioctl_abort_bulk_out(data);
944 case USBTMC_IOCTL_ABORT_BULK_IN:
945 retval = usbtmc_ioctl_abort_bulk_in(data);
948 mutex_unlock(&data->io_mutex);
952 static struct file_operations fops = {
953 .owner = THIS_MODULE,
955 .write = usbtmc_write,
957 .release = usbtmc_release,
958 .unlocked_ioctl = usbtmc_ioctl,
961 static struct usb_class_driver usbtmc_class = {
964 .minor_base = USBTMC_MINOR_BASE,
968 static int usbtmc_probe(struct usb_interface *intf,
969 const struct usb_device_id *id)
971 struct usbtmc_device_data *data;
972 struct usb_host_interface *iface_desc;
973 struct usb_endpoint_descriptor *endpoint;
977 dev_dbg(&intf->dev, "%s called\n", __func__);
979 data = kmalloc(sizeof(struct usbtmc_device_data), GFP_KERNEL);
981 dev_err(&intf->dev, "Unable to allocate kernel memory\n");
987 data->usb_dev = usb_get_dev(interface_to_usbdev(intf));
988 usb_set_intfdata(intf, data);
989 kref_init(&data->kref);
990 mutex_init(&data->io_mutex);
992 /* Initialize USBTMC bTag and other fields */
994 data->TermCharEnabled = 0;
995 data->TermChar = '\n';
997 /* USBTMC devices have only one setting, so use that */
998 iface_desc = data->intf->cur_altsetting;
1000 /* Find bulk in endpoint */
1001 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1002 endpoint = &iface_desc->endpoint[n].desc;
1004 if (usb_endpoint_is_bulk_in(endpoint)) {
1005 data->bulk_in = endpoint->bEndpointAddress;
1006 dev_dbg(&intf->dev, "Found bulk in endpoint at %u\n",
1012 /* Find bulk out endpoint */
1013 for (n = 0; n < iface_desc->desc.bNumEndpoints; n++) {
1014 endpoint = &iface_desc->endpoint[n].desc;
1016 if (usb_endpoint_is_bulk_out(endpoint)) {
1017 data->bulk_out = endpoint->bEndpointAddress;
1018 dev_dbg(&intf->dev, "Found Bulk out endpoint at %u\n",
1024 retcode = get_capabilities(data);
1026 dev_err(&intf->dev, "can't read capabilities\n");
1028 retcode = sysfs_create_group(&intf->dev.kobj,
1029 &capability_attr_grp);
1031 retcode = sysfs_create_group(&intf->dev.kobj, &data_attr_grp);
1033 retcode = usb_register_dev(intf, &usbtmc_class);
1035 dev_err(&intf->dev, "Not able to get a minor"
1036 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE,
1038 goto error_register;
1040 dev_dbg(&intf->dev, "Using minor number %d\n", intf->minor);
1045 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1046 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1047 kref_put(&data->kref, usbtmc_delete);
1051 static void usbtmc_disconnect(struct usb_interface *intf)
1053 struct usbtmc_device_data *data;
1055 dev_dbg(&intf->dev, "usbtmc_disconnect called\n");
1057 data = usb_get_intfdata(intf);
1058 usb_deregister_dev(intf, &usbtmc_class);
1059 sysfs_remove_group(&intf->dev.kobj, &capability_attr_grp);
1060 sysfs_remove_group(&intf->dev.kobj, &data_attr_grp);
1061 kref_put(&data->kref, usbtmc_delete);
1064 static struct usb_driver usbtmc_driver = {
1066 .id_table = usbtmc_devices,
1067 .probe = usbtmc_probe,
1068 .disconnect = usbtmc_disconnect
1071 static int __init usbtmc_init(void)
1075 retcode = usb_register(&usbtmc_driver);
1077 printk(KERN_ERR KBUILD_MODNAME": Unable to register driver\n");
1080 module_init(usbtmc_init);
1082 static void __exit usbtmc_exit(void)
1084 usb_deregister(&usbtmc_driver);
1086 module_exit(usbtmc_exit);
1088 MODULE_LICENSE("GPL");