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>
24 #include <linux/kernel.h>
26 #include <linux/uaccess.h>
27 #include <linux/kref.h>
28 #include <linux/mutex.h>
29 #include <linux/usb.h>
30 #include <linux/usb/tmc.h>
33 #define USBTMC_MINOR_BASE 176
36 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
37 * large as wMaxPacketSize (which is usually 512 bytes).
39 #define USBTMC_SIZE_IOBUFFER 2048
41 /* Default USB timeout (in milliseconds) */
42 #define USBTMC_TIMEOUT 10
45 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
46 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
47 * packet is never read.
49 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
51 static struct usb_device_id usbtmc_devices
[] = {
52 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 0), },
53 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 1), },
54 { 0, } /* terminating entry */
56 MODULE_DEVICE_TABLE(usb
, usbtmc_devices
);
59 * This structure is the capabilities for the device
60 * See section 4.2.1.8 of the USBTMC specification for details.
62 struct usbtmc_dev_capabilities
{
63 __u8 interface_capabilities
;
64 __u8 device_capabilities
;
65 __u8 usb488_interface_capabilities
;
66 __u8 usb488_device_capabilities
;
69 /* This structure holds private data for each USBTMC device. One copy is
70 * allocated for each USBTMC device in the driver's probe function.
72 struct usbtmc_device_data
{
73 const struct usb_device_id
*id
;
74 struct usb_device
*usb_dev
;
75 struct usb_interface
*intf
;
78 unsigned int bulk_out
;
81 u8 bTag_last_write
; /* needed for abort */
82 u8 bTag_last_read
; /* needed for abort */
84 /* attributes from the USB TMC spec for this device */
89 struct usbtmc_dev_capabilities capabilities
;
91 struct mutex io_mutex
; /* only one i/o function running at a time */
93 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
95 /* Forward declarations */
96 static struct usb_driver usbtmc_driver
;
98 static void usbtmc_delete(struct kref
*kref
)
100 struct usbtmc_device_data
*data
= to_usbtmc_data(kref
);
102 usb_put_dev(data
->usb_dev
);
106 static int usbtmc_open(struct inode
*inode
, struct file
*filp
)
108 struct usb_interface
*intf
;
109 struct usbtmc_device_data
*data
;
112 intf
= usb_find_interface(&usbtmc_driver
, iminor(inode
));
114 printk(KERN_ERR KBUILD_MODNAME
115 ": can not find device for minor %d", iminor(inode
));
120 data
= usb_get_intfdata(intf
);
121 kref_get(&data
->kref
);
123 /* Store pointer in file structure's private data field */
124 filp
->private_data
= data
;
130 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
132 struct usbtmc_device_data
*data
= file
->private_data
;
134 kref_put(&data
->kref
, usbtmc_delete
);
138 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
145 struct usb_host_interface
*current_setting
;
148 dev
= &data
->intf
->dev
;
149 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
153 rv
= usb_control_msg(data
->usb_dev
,
154 usb_rcvctrlpipe(data
->usb_dev
, 0),
155 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
156 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
157 data
->bTag_last_read
, data
->bulk_in
,
158 buffer
, 2, USBTMC_TIMEOUT
);
161 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
165 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
167 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
172 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
173 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
180 current_setting
= data
->intf
->cur_altsetting
;
181 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++)
182 if (current_setting
->endpoint
[n
].desc
.bEndpointAddress
==
184 max_size
= le16_to_cpu(current_setting
->endpoint
[n
].
185 desc
.wMaxPacketSize
);
188 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
193 dev_dbg(&data
->intf
->dev
, "wMaxPacketSize is %d\n", max_size
);
198 dev_dbg(dev
, "Reading from bulk in EP\n");
200 rv
= usb_bulk_msg(data
->usb_dev
,
201 usb_rcvbulkpipe(data
->usb_dev
,
203 buffer
, USBTMC_SIZE_IOBUFFER
,
204 &actual
, USBTMC_TIMEOUT
);
209 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
212 } while ((actual
== max_size
) &&
213 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
215 if (actual
== max_size
) {
216 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
217 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
224 usbtmc_abort_bulk_in_status
:
225 rv
= usb_control_msg(data
->usb_dev
,
226 usb_rcvctrlpipe(data
->usb_dev
, 0),
227 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
228 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
229 0, data
->bulk_in
, buffer
, 0x08,
233 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
237 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
239 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
244 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
245 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
252 dev_dbg(dev
, "Reading from bulk in EP\n");
254 rv
= usb_bulk_msg(data
->usb_dev
,
255 usb_rcvbulkpipe(data
->usb_dev
,
257 buffer
, USBTMC_SIZE_IOBUFFER
,
258 &actual
, USBTMC_TIMEOUT
);
263 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
266 } while ((actual
= max_size
) &&
267 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
269 if (actual
== max_size
) {
270 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
271 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
276 goto usbtmc_abort_bulk_in_status
;
284 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
291 dev
= &data
->intf
->dev
;
293 buffer
= kmalloc(8, GFP_KERNEL
);
297 rv
= usb_control_msg(data
->usb_dev
,
298 usb_rcvctrlpipe(data
->usb_dev
, 0),
299 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
300 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
301 data
->bTag_last_write
, data
->bulk_out
,
302 buffer
, 2, USBTMC_TIMEOUT
);
305 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
309 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
311 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
312 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
320 usbtmc_abort_bulk_out_check_status
:
321 rv
= usb_control_msg(data
->usb_dev
,
322 usb_rcvctrlpipe(data
->usb_dev
, 0),
323 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
324 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
325 0, data
->bulk_out
, buffer
, 0x08,
329 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
333 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
335 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
336 goto usbtmc_abort_bulk_out_clear_halt
;
338 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
339 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
340 goto usbtmc_abort_bulk_out_check_status
;
345 usbtmc_abort_bulk_out_clear_halt
:
346 rv
= usb_control_msg(data
->usb_dev
,
347 usb_sndctrlpipe(data
->usb_dev
, 0),
348 USB_REQ_CLEAR_FEATURE
,
349 USB_DIR_OUT
| USB_TYPE_STANDARD
|
351 USB_ENDPOINT_HALT
, data
->bulk_out
, buffer
,
355 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
365 static ssize_t
usbtmc_read(struct file
*filp
, char __user
*buf
,
366 size_t count
, loff_t
*f_pos
)
368 struct usbtmc_device_data
*data
;
370 unsigned long int n_characters
;
378 /* Get pointer to private data structure */
379 data
= filp
->private_data
;
380 dev
= &data
->intf
->dev
;
382 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
386 mutex_lock(&data
->io_mutex
);
391 while (remaining
> 0) {
392 if (remaining
> USBTMC_SIZE_IOBUFFER
- 12 - 3)
393 this_part
= USBTMC_SIZE_IOBUFFER
- 12 - 3;
395 this_part
= remaining
;
397 /* Setup IO buffer for DEV_DEP_MSG_IN message
398 * Refer to class specs for details
401 buffer
[1] = data
->bTag
;
402 buffer
[2] = ~(data
->bTag
);
403 buffer
[3] = 0; /* Reserved */
404 buffer
[4] = (this_part
- 12 - 3) & 255;
405 buffer
[5] = ((this_part
- 12 - 3) >> 8) & 255;
406 buffer
[6] = ((this_part
- 12 - 3) >> 16) & 255;
407 buffer
[7] = ((this_part
- 12 - 3) >> 24) & 255;
408 buffer
[8] = data
->TermCharEnabled
* 2;
409 /* Use term character? */
410 buffer
[9] = data
->TermChar
;
411 buffer
[10] = 0; /* Reserved */
412 buffer
[11] = 0; /* Reserved */
415 retval
= usb_bulk_msg(data
->usb_dev
,
416 usb_sndbulkpipe(data
->usb_dev
,
418 buffer
, 12, &actual
, USBTMC_TIMEOUT
);
420 /* Store bTag (in case we need to abort) */
421 data
->bTag_last_write
= data
->bTag
;
423 /* Increment bTag -- and increment again if zero */
429 dev_err(dev
, "usb_bulk_msg returned %d\n", retval
);
430 if (data
->auto_abort
)
431 usbtmc_ioctl_abort_bulk_out(data
);
436 retval
= usb_bulk_msg(data
->usb_dev
,
437 usb_rcvbulkpipe(data
->usb_dev
,
439 buffer
, USBTMC_SIZE_IOBUFFER
, &actual
,
442 /* Store bTag (in case we need to abort) */
443 data
->bTag_last_read
= data
->bTag
;
446 dev_err(dev
, "Unable to read data, error %d\n", retval
);
447 if (data
->auto_abort
)
448 usbtmc_ioctl_abort_bulk_in(data
);
452 /* How many characters did the instrument send? */
453 n_characters
= buffer
[4] +
458 /* Copy buffer to user space */
459 if (copy_to_user(buf
+ done
, &buffer
[12], n_characters
)) {
460 /* There must have been an addressing problem */
465 done
+= n_characters
;
466 if (n_characters
< USBTMC_SIZE_IOBUFFER
)
470 /* Update file position value */
471 *f_pos
= *f_pos
+ done
;
475 mutex_unlock(&data
->io_mutex
);
480 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
481 size_t count
, loff_t
*f_pos
)
483 struct usbtmc_device_data
*data
;
487 unsigned long int n_bytes
;
492 data
= filp
->private_data
;
494 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
498 mutex_lock(&data
->io_mutex
);
503 while (remaining
> 0) {
504 if (remaining
> USBTMC_SIZE_IOBUFFER
- 12) {
505 this_part
= USBTMC_SIZE_IOBUFFER
- 12;
508 this_part
= remaining
;
512 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
514 buffer
[1] = data
->bTag
;
515 buffer
[2] = ~(data
->bTag
);
516 buffer
[3] = 0; /* Reserved */
517 buffer
[4] = this_part
& 255;
518 buffer
[5] = (this_part
>> 8) & 255;
519 buffer
[6] = (this_part
>> 16) & 255;
520 buffer
[7] = (this_part
>> 24) & 255;
521 /* buffer[8] is set above... */
522 buffer
[9] = 0; /* Reserved */
523 buffer
[10] = 0; /* Reserved */
524 buffer
[11] = 0; /* Reserved */
526 if (copy_from_user(&buffer
[12], buf
+ done
, this_part
)) {
531 n_bytes
= roundup(12 + this_part
, 4);
532 memset(buffer
+ 12 + this_part
, 0, n_bytes
- (12 + this_part
));
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]);
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];
790 #define capability_attribute(name) \
791 static ssize_t show_##name(struct device *dev, \
792 struct device_attribute *attr, char *buf) \
794 struct usb_interface *intf = to_usb_interface(dev); \
795 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
797 return sprintf(buf, "%d\n", data->capabilities.name); \
799 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
801 capability_attribute(interface_capabilities
);
802 capability_attribute(device_capabilities
);
803 capability_attribute(usb488_interface_capabilities
);
804 capability_attribute(usb488_device_capabilities
);
806 static struct attribute
*capability_attrs
[] = {
807 &dev_attr_interface_capabilities
.attr
,
808 &dev_attr_device_capabilities
.attr
,
809 &dev_attr_usb488_interface_capabilities
.attr
,
810 &dev_attr_usb488_device_capabilities
.attr
,
814 static struct attribute_group capability_attr_grp
= {
815 .attrs
= capability_attrs
,
818 static ssize_t
show_TermChar(struct device
*dev
,
819 struct device_attribute
*attr
, char *buf
)
821 struct usb_interface
*intf
= to_usb_interface(dev
);
822 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
824 return sprintf(buf
, "%c\n", data
->TermChar
);
827 static ssize_t
store_TermChar(struct device
*dev
,
828 struct device_attribute
*attr
,
829 const char *buf
, size_t count
)
831 struct usb_interface
*intf
= to_usb_interface(dev
);
832 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
836 data
->TermChar
= buf
[0];
839 static DEVICE_ATTR(TermChar
, S_IRUGO
, show_TermChar
, store_TermChar
);
841 #define data_attribute(name) \
842 static ssize_t show_##name(struct device *dev, \
843 struct device_attribute *attr, char *buf) \
845 struct usb_interface *intf = to_usb_interface(dev); \
846 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
848 return sprintf(buf, "%d\n", data->name); \
850 static ssize_t store_##name(struct device *dev, \
851 struct device_attribute *attr, \
852 const char *buf, size_t count) \
854 struct usb_interface *intf = to_usb_interface(dev); \
855 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
859 result = sscanf(buf, "%u\n", &val); \
868 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
870 data_attribute(TermCharEnabled
);
871 data_attribute(auto_abort
);
873 static struct attribute
*data_attrs
[] = {
874 &dev_attr_TermChar
.attr
,
875 &dev_attr_TermCharEnabled
.attr
,
876 &dev_attr_auto_abort
.attr
,
880 static struct attribute_group data_attr_grp
= {
884 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
890 dev
= &data
->intf
->dev
;
892 buffer
= kmalloc(2, GFP_KERNEL
);
896 rv
= usb_control_msg(data
->usb_dev
,
897 usb_rcvctrlpipe(data
->usb_dev
, 0),
898 USBTMC_REQUEST_INDICATOR_PULSE
,
899 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
900 0, 0, buffer
, 0x01, USBTMC_TIMEOUT
);
903 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
907 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
909 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
910 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
921 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
923 struct usbtmc_device_data
*data
;
924 int retval
= -EBADRQC
;
926 data
= file
->private_data
;
927 mutex_lock(&data
->io_mutex
);
930 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
931 retval
= usbtmc_ioctl_clear_out_halt(data
);
934 case USBTMC_IOCTL_CLEAR_IN_HALT
:
935 retval
= usbtmc_ioctl_clear_in_halt(data
);
938 case USBTMC_IOCTL_INDICATOR_PULSE
:
939 retval
= usbtmc_ioctl_indicator_pulse(data
);
942 case USBTMC_IOCTL_CLEAR
:
943 retval
= usbtmc_ioctl_clear(data
);
946 case USBTMC_IOCTL_ABORT_BULK_OUT
:
947 retval
= usbtmc_ioctl_abort_bulk_out(data
);
950 case USBTMC_IOCTL_ABORT_BULK_IN
:
951 retval
= usbtmc_ioctl_abort_bulk_in(data
);
955 mutex_unlock(&data
->io_mutex
);
959 static struct file_operations fops
= {
960 .owner
= THIS_MODULE
,
962 .write
= usbtmc_write
,
964 .release
= usbtmc_release
,
965 .unlocked_ioctl
= usbtmc_ioctl
,
968 static struct usb_class_driver usbtmc_class
= {
971 .minor_base
= USBTMC_MINOR_BASE
,
975 static int usbtmc_probe(struct usb_interface
*intf
,
976 const struct usb_device_id
*id
)
978 struct usbtmc_device_data
*data
;
979 struct usb_host_interface
*iface_desc
;
980 struct usb_endpoint_descriptor
*endpoint
;
984 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
986 data
= kmalloc(sizeof(struct usbtmc_device_data
), GFP_KERNEL
);
988 dev_err(&intf
->dev
, "Unable to allocate kernel memory\n");
994 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
995 usb_set_intfdata(intf
, data
);
996 kref_init(&data
->kref
);
997 mutex_init(&data
->io_mutex
);
999 /* Initialize USBTMC bTag and other fields */
1001 data
->TermCharEnabled
= 0;
1002 data
->TermChar
= '\n';
1004 /* USBTMC devices have only one setting, so use that */
1005 iface_desc
= data
->intf
->cur_altsetting
;
1007 /* Find bulk in endpoint */
1008 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1009 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1011 if (usb_endpoint_is_bulk_in(endpoint
)) {
1012 data
->bulk_in
= endpoint
->bEndpointAddress
;
1013 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n",
1019 /* Find bulk out endpoint */
1020 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1021 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1023 if (usb_endpoint_is_bulk_out(endpoint
)) {
1024 data
->bulk_out
= endpoint
->bEndpointAddress
;
1025 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n",
1031 retcode
= get_capabilities(data
);
1033 dev_err(&intf
->dev
, "can't read capabilities\n");
1035 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
1036 &capability_attr_grp
);
1038 retcode
= sysfs_create_group(&intf
->dev
.kobj
, &data_attr_grp
);
1040 retcode
= usb_register_dev(intf
, &usbtmc_class
);
1042 dev_err(&intf
->dev
, "Not able to get a minor"
1043 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE
,
1045 goto error_register
;
1047 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
1052 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1053 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1054 kref_put(&data
->kref
, usbtmc_delete
);
1058 static void usbtmc_disconnect(struct usb_interface
*intf
)
1060 struct usbtmc_device_data
*data
;
1062 dev_dbg(&intf
->dev
, "usbtmc_disconnect called\n");
1064 data
= usb_get_intfdata(intf
);
1065 usb_deregister_dev(intf
, &usbtmc_class
);
1066 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1067 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1068 kref_put(&data
->kref
, usbtmc_delete
);
1071 static struct usb_driver usbtmc_driver
= {
1073 .id_table
= usbtmc_devices
,
1074 .probe
= usbtmc_probe
,
1075 .disconnect
= usbtmc_disconnect
1078 static int __init
usbtmc_init(void)
1082 retcode
= usb_register(&usbtmc_driver
);
1084 printk(KERN_ERR KBUILD_MODNAME
": Unable to register driver\n");
1087 module_init(usbtmc_init
);
1089 static void __exit
usbtmc_exit(void)
1091 usb_deregister(&usbtmc_driver
);
1093 module_exit(usbtmc_exit
);
1095 MODULE_LICENSE("GPL");