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 #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/slab.h>
29 #include <linux/mutex.h>
30 #include <linux/usb.h>
31 #include <linux/usb/tmc.h>
34 #define USBTMC_MINOR_BASE 176
37 * Size of driver internal IO buffer. Must be multiple of 4 and at least as
38 * large as wMaxPacketSize (which is usually 512 bytes).
40 #define USBTMC_SIZE_IOBUFFER 2048
42 /* Default USB timeout (in milliseconds) */
43 #define USBTMC_TIMEOUT 5000
46 * Maximum number of read cycles to empty bulk in endpoint during CLEAR and
47 * ABORT_BULK_IN requests. Ends the loop if (for whatever reason) a short
48 * packet is never read.
50 #define USBTMC_MAX_READS_TO_CLEAR_BULK_IN 100
52 static const struct usb_device_id usbtmc_devices
[] = {
53 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 0), },
54 { USB_INTERFACE_INFO(USB_CLASS_APP_SPEC
, 3, 1), },
55 { 0, } /* terminating entry */
57 MODULE_DEVICE_TABLE(usb
, usbtmc_devices
);
60 * This structure is the capabilities for the device
61 * See section 4.2.1.8 of the USBTMC specification,
62 * and section 4.2.2 of the USBTMC usb488 subclass
63 * specification for details.
65 struct usbtmc_dev_capabilities
{
66 __u8 interface_capabilities
;
67 __u8 device_capabilities
;
68 __u8 usb488_interface_capabilities
;
69 __u8 usb488_device_capabilities
;
72 /* This structure holds private data for each USBTMC device. One copy is
73 * allocated for each USBTMC device in the driver's probe function.
75 struct usbtmc_device_data
{
76 const struct usb_device_id
*id
;
77 struct usb_device
*usb_dev
;
78 struct usb_interface
*intf
;
81 unsigned int bulk_out
;
84 u8 bTag_last_write
; /* needed for abort */
85 u8 bTag_last_read
; /* needed for abort */
87 /* attributes from the USB TMC spec for this device */
92 bool zombie
; /* fd of disconnected device */
94 struct usbtmc_dev_capabilities capabilities
;
96 struct mutex io_mutex
; /* only one i/o function running at a time */
98 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
100 /* Forward declarations */
101 static struct usb_driver usbtmc_driver
;
103 static void usbtmc_delete(struct kref
*kref
)
105 struct usbtmc_device_data
*data
= to_usbtmc_data(kref
);
107 usb_put_dev(data
->usb_dev
);
111 static int usbtmc_open(struct inode
*inode
, struct file
*filp
)
113 struct usb_interface
*intf
;
114 struct usbtmc_device_data
*data
;
117 intf
= usb_find_interface(&usbtmc_driver
, iminor(inode
));
119 printk(KERN_ERR KBUILD_MODNAME
120 ": can not find device for minor %d", iminor(inode
));
125 data
= usb_get_intfdata(intf
);
126 kref_get(&data
->kref
);
128 /* Store pointer in file structure's private data field */
129 filp
->private_data
= data
;
135 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
137 struct usbtmc_device_data
*data
= file
->private_data
;
139 kref_put(&data
->kref
, usbtmc_delete
);
143 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
150 struct usb_host_interface
*current_setting
;
153 dev
= &data
->intf
->dev
;
154 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
158 rv
= usb_control_msg(data
->usb_dev
,
159 usb_rcvctrlpipe(data
->usb_dev
, 0),
160 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
161 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
162 data
->bTag_last_read
, data
->bulk_in
,
163 buffer
, 2, USBTMC_TIMEOUT
);
166 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
170 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
172 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
177 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
178 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
185 current_setting
= data
->intf
->cur_altsetting
;
186 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++)
187 if (current_setting
->endpoint
[n
].desc
.bEndpointAddress
==
189 max_size
= le16_to_cpu(current_setting
->endpoint
[n
].
190 desc
.wMaxPacketSize
);
193 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
198 dev_dbg(&data
->intf
->dev
, "wMaxPacketSize is %d\n", max_size
);
203 dev_dbg(dev
, "Reading from bulk in EP\n");
205 rv
= usb_bulk_msg(data
->usb_dev
,
206 usb_rcvbulkpipe(data
->usb_dev
,
208 buffer
, USBTMC_SIZE_IOBUFFER
,
209 &actual
, USBTMC_TIMEOUT
);
214 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
217 } while ((actual
== max_size
) &&
218 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
220 if (actual
== max_size
) {
221 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
222 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
229 usbtmc_abort_bulk_in_status
:
230 rv
= usb_control_msg(data
->usb_dev
,
231 usb_rcvctrlpipe(data
->usb_dev
, 0),
232 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
233 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
234 0, data
->bulk_in
, buffer
, 0x08,
238 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
242 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
244 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
249 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
250 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
257 dev_dbg(dev
, "Reading from bulk in EP\n");
259 rv
= usb_bulk_msg(data
->usb_dev
,
260 usb_rcvbulkpipe(data
->usb_dev
,
262 buffer
, USBTMC_SIZE_IOBUFFER
,
263 &actual
, USBTMC_TIMEOUT
);
268 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
271 } while ((actual
= max_size
) &&
272 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
274 if (actual
== max_size
) {
275 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
276 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
281 goto usbtmc_abort_bulk_in_status
;
289 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
296 dev
= &data
->intf
->dev
;
298 buffer
= kmalloc(8, GFP_KERNEL
);
302 rv
= usb_control_msg(data
->usb_dev
,
303 usb_rcvctrlpipe(data
->usb_dev
, 0),
304 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
305 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
306 data
->bTag_last_write
, data
->bulk_out
,
307 buffer
, 2, USBTMC_TIMEOUT
);
310 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
314 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
316 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
317 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
325 usbtmc_abort_bulk_out_check_status
:
326 rv
= usb_control_msg(data
->usb_dev
,
327 usb_rcvctrlpipe(data
->usb_dev
, 0),
328 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
329 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
330 0, data
->bulk_out
, buffer
, 0x08,
334 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
338 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
340 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
341 goto usbtmc_abort_bulk_out_clear_halt
;
343 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
344 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
345 goto usbtmc_abort_bulk_out_check_status
;
350 usbtmc_abort_bulk_out_clear_halt
:
351 rv
= usb_clear_halt(data
->usb_dev
,
352 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
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
;
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
);
395 while (remaining
> 0) {
396 if (remaining
> USBTMC_SIZE_IOBUFFER
- 12 - 3)
397 this_part
= USBTMC_SIZE_IOBUFFER
- 12 - 3;
399 this_part
= remaining
;
401 /* Setup IO buffer for DEV_DEP_MSG_IN message
402 * Refer to class specs for details
405 buffer
[1] = data
->bTag
;
406 buffer
[2] = ~(data
->bTag
);
407 buffer
[3] = 0; /* Reserved */
408 buffer
[4] = (this_part
) & 255;
409 buffer
[5] = ((this_part
) >> 8) & 255;
410 buffer
[6] = ((this_part
) >> 16) & 255;
411 buffer
[7] = ((this_part
) >> 24) & 255;
412 buffer
[8] = data
->TermCharEnabled
* 2;
413 /* Use term character? */
414 buffer
[9] = data
->TermChar
;
415 buffer
[10] = 0; /* Reserved */
416 buffer
[11] = 0; /* Reserved */
419 retval
= usb_bulk_msg(data
->usb_dev
,
420 usb_sndbulkpipe(data
->usb_dev
,
422 buffer
, 12, &actual
, USBTMC_TIMEOUT
);
424 /* Store bTag (in case we need to abort) */
425 data
->bTag_last_write
= data
->bTag
;
427 /* Increment bTag -- and increment again if zero */
433 dev_err(dev
, "usb_bulk_msg returned %d\n", retval
);
434 if (data
->auto_abort
)
435 usbtmc_ioctl_abort_bulk_out(data
);
440 retval
= usb_bulk_msg(data
->usb_dev
,
441 usb_rcvbulkpipe(data
->usb_dev
,
443 buffer
, USBTMC_SIZE_IOBUFFER
, &actual
,
446 /* Store bTag (in case we need to abort) */
447 data
->bTag_last_read
= data
->bTag
;
450 dev_err(dev
, "Unable to read data, error %d\n", retval
);
451 if (data
->auto_abort
)
452 usbtmc_ioctl_abort_bulk_in(data
);
456 /* How many characters did the instrument send? */
457 n_characters
= buffer
[4] +
462 /* Ensure the instrument doesn't lie about it */
463 if(n_characters
> actual
- 12) {
464 dev_err(dev
, "Device lies about message size: %u > %d\n", n_characters
, actual
- 12);
465 n_characters
= actual
- 12;
468 /* Ensure the instrument doesn't send more back than requested */
469 if(n_characters
> this_part
) {
470 dev_err(dev
, "Device returns more than requested: %zu > %zu\n", done
+ n_characters
, done
+ this_part
);
471 n_characters
= this_part
;
474 /* Bound amount of data received by amount of data requested */
475 if (n_characters
> this_part
)
476 n_characters
= this_part
;
478 /* Copy buffer to user space */
479 if (copy_to_user(buf
+ done
, &buffer
[12], n_characters
)) {
480 /* There must have been an addressing problem */
485 done
+= n_characters
;
486 /* Terminate if end-of-message bit recieved from device */
487 if ((buffer
[8] & 0x01) && (actual
>= n_characters
+ 12))
490 remaining
-= n_characters
;
493 /* Update file position value */
494 *f_pos
= *f_pos
+ done
;
498 mutex_unlock(&data
->io_mutex
);
503 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
504 size_t count
, loff_t
*f_pos
)
506 struct usbtmc_device_data
*data
;
510 unsigned long int n_bytes
;
515 data
= filp
->private_data
;
517 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
521 mutex_lock(&data
->io_mutex
);
530 while (remaining
> 0) {
531 if (remaining
> USBTMC_SIZE_IOBUFFER
- 12) {
532 this_part
= USBTMC_SIZE_IOBUFFER
- 12;
535 this_part
= remaining
;
539 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
541 buffer
[1] = data
->bTag
;
542 buffer
[2] = ~(data
->bTag
);
543 buffer
[3] = 0; /* Reserved */
544 buffer
[4] = this_part
& 255;
545 buffer
[5] = (this_part
>> 8) & 255;
546 buffer
[6] = (this_part
>> 16) & 255;
547 buffer
[7] = (this_part
>> 24) & 255;
548 /* buffer[8] is set above... */
549 buffer
[9] = 0; /* Reserved */
550 buffer
[10] = 0; /* Reserved */
551 buffer
[11] = 0; /* Reserved */
553 if (copy_from_user(&buffer
[12], buf
+ done
, this_part
)) {
558 n_bytes
= roundup(12 + this_part
, 4);
559 memset(buffer
+ 12 + this_part
, 0, n_bytes
- (12 + this_part
));
562 retval
= usb_bulk_msg(data
->usb_dev
,
563 usb_sndbulkpipe(data
->usb_dev
,
566 &actual
, USBTMC_TIMEOUT
);
572 data
->bTag_last_write
= data
->bTag
;
579 dev_err(&data
->intf
->dev
,
580 "Unable to send data, error %d\n", retval
);
581 if (data
->auto_abort
)
582 usbtmc_ioctl_abort_bulk_out(data
);
586 remaining
-= this_part
;
592 mutex_unlock(&data
->io_mutex
);
597 static int usbtmc_ioctl_clear(struct usbtmc_device_data
*data
)
599 struct usb_host_interface
*current_setting
;
600 struct usb_endpoint_descriptor
*desc
;
608 dev
= &data
->intf
->dev
;
610 dev_dbg(dev
, "Sending INITIATE_CLEAR request\n");
612 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
616 rv
= usb_control_msg(data
->usb_dev
,
617 usb_rcvctrlpipe(data
->usb_dev
, 0),
618 USBTMC_REQUEST_INITIATE_CLEAR
,
619 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
620 0, 0, buffer
, 1, USBTMC_TIMEOUT
);
622 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
626 dev_dbg(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
628 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
629 dev_err(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
635 current_setting
= data
->intf
->cur_altsetting
;
636 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++) {
637 desc
= ¤t_setting
->endpoint
[n
].desc
;
638 if (desc
->bEndpointAddress
== data
->bulk_in
)
639 max_size
= le16_to_cpu(desc
->wMaxPacketSize
);
643 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
648 dev_dbg(dev
, "wMaxPacketSize is %d\n", max_size
);
652 usbtmc_clear_check_status
:
654 dev_dbg(dev
, "Sending CHECK_CLEAR_STATUS request\n");
656 rv
= usb_control_msg(data
->usb_dev
,
657 usb_rcvctrlpipe(data
->usb_dev
, 0),
658 USBTMC_REQUEST_CHECK_CLEAR_STATUS
,
659 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
660 0, 0, buffer
, 2, USBTMC_TIMEOUT
);
662 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
666 dev_dbg(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
668 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
669 goto usbtmc_clear_bulk_out_halt
;
671 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
672 dev_err(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
679 dev_dbg(dev
, "Reading from bulk in EP\n");
681 rv
= usb_bulk_msg(data
->usb_dev
,
682 usb_rcvbulkpipe(data
->usb_dev
,
684 buffer
, USBTMC_SIZE_IOBUFFER
,
685 &actual
, USBTMC_TIMEOUT
);
689 dev_err(dev
, "usb_control_msg returned %d\n",
693 } while ((actual
== max_size
) &&
694 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
696 if (actual
== max_size
) {
697 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
698 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
703 goto usbtmc_clear_check_status
;
705 usbtmc_clear_bulk_out_halt
:
707 rv
= usb_clear_halt(data
->usb_dev
,
708 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
710 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
720 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data
*data
)
725 buffer
= kmalloc(2, GFP_KERNEL
);
729 rv
= usb_clear_halt(data
->usb_dev
,
730 usb_sndbulkpipe(data
->usb_dev
, data
->bulk_out
));
733 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
744 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data
*data
)
749 buffer
= kmalloc(2, GFP_KERNEL
);
753 rv
= usb_clear_halt(data
->usb_dev
,
754 usb_rcvbulkpipe(data
->usb_dev
, data
->bulk_in
));
757 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
768 static int get_capabilities(struct usbtmc_device_data
*data
)
770 struct device
*dev
= &data
->usb_dev
->dev
;
774 buffer
= kmalloc(0x18, GFP_KERNEL
);
778 rv
= usb_control_msg(data
->usb_dev
, usb_rcvctrlpipe(data
->usb_dev
, 0),
779 USBTMC_REQUEST_GET_CAPABILITIES
,
780 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
781 0, 0, buffer
, 0x18, USBTMC_TIMEOUT
);
783 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
787 dev_dbg(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
788 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
789 dev_err(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
793 dev_dbg(dev
, "Interface capabilities are %x\n", buffer
[4]);
794 dev_dbg(dev
, "Device capabilities are %x\n", buffer
[5]);
795 dev_dbg(dev
, "USB488 interface capabilities are %x\n", buffer
[14]);
796 dev_dbg(dev
, "USB488 device capabilities are %x\n", buffer
[15]);
798 data
->capabilities
.interface_capabilities
= buffer
[4];
799 data
->capabilities
.device_capabilities
= buffer
[5];
800 data
->capabilities
.usb488_interface_capabilities
= buffer
[14];
801 data
->capabilities
.usb488_device_capabilities
= buffer
[15];
809 #define capability_attribute(name) \
810 static ssize_t show_##name(struct device *dev, \
811 struct device_attribute *attr, char *buf) \
813 struct usb_interface *intf = to_usb_interface(dev); \
814 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
816 return sprintf(buf, "%d\n", data->capabilities.name); \
818 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
820 capability_attribute(interface_capabilities
);
821 capability_attribute(device_capabilities
);
822 capability_attribute(usb488_interface_capabilities
);
823 capability_attribute(usb488_device_capabilities
);
825 static struct attribute
*capability_attrs
[] = {
826 &dev_attr_interface_capabilities
.attr
,
827 &dev_attr_device_capabilities
.attr
,
828 &dev_attr_usb488_interface_capabilities
.attr
,
829 &dev_attr_usb488_device_capabilities
.attr
,
833 static struct attribute_group capability_attr_grp
= {
834 .attrs
= capability_attrs
,
837 static ssize_t
show_TermChar(struct device
*dev
,
838 struct device_attribute
*attr
, char *buf
)
840 struct usb_interface
*intf
= to_usb_interface(dev
);
841 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
843 return sprintf(buf
, "%c\n", data
->TermChar
);
846 static ssize_t
store_TermChar(struct device
*dev
,
847 struct device_attribute
*attr
,
848 const char *buf
, size_t count
)
850 struct usb_interface
*intf
= to_usb_interface(dev
);
851 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
855 data
->TermChar
= buf
[0];
858 static DEVICE_ATTR(TermChar
, S_IRUGO
, show_TermChar
, store_TermChar
);
860 #define data_attribute(name) \
861 static ssize_t show_##name(struct device *dev, \
862 struct device_attribute *attr, char *buf) \
864 struct usb_interface *intf = to_usb_interface(dev); \
865 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
867 return sprintf(buf, "%d\n", data->name); \
869 static ssize_t store_##name(struct device *dev, \
870 struct device_attribute *attr, \
871 const char *buf, size_t count) \
873 struct usb_interface *intf = to_usb_interface(dev); \
874 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
878 result = sscanf(buf, "%u\n", &val); \
887 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
889 data_attribute(TermCharEnabled
);
890 data_attribute(auto_abort
);
892 static struct attribute
*data_attrs
[] = {
893 &dev_attr_TermChar
.attr
,
894 &dev_attr_TermCharEnabled
.attr
,
895 &dev_attr_auto_abort
.attr
,
899 static struct attribute_group data_attr_grp
= {
903 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
909 dev
= &data
->intf
->dev
;
911 buffer
= kmalloc(2, GFP_KERNEL
);
915 rv
= usb_control_msg(data
->usb_dev
,
916 usb_rcvctrlpipe(data
->usb_dev
, 0),
917 USBTMC_REQUEST_INDICATOR_PULSE
,
918 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
919 0, 0, buffer
, 0x01, USBTMC_TIMEOUT
);
922 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
926 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
928 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
929 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
940 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
942 struct usbtmc_device_data
*data
;
943 int retval
= -EBADRQC
;
945 data
= file
->private_data
;
946 mutex_lock(&data
->io_mutex
);
949 goto skip_io_on_zombie
;
953 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
954 retval
= usbtmc_ioctl_clear_out_halt(data
);
957 case USBTMC_IOCTL_CLEAR_IN_HALT
:
958 retval
= usbtmc_ioctl_clear_in_halt(data
);
961 case USBTMC_IOCTL_INDICATOR_PULSE
:
962 retval
= usbtmc_ioctl_indicator_pulse(data
);
965 case USBTMC_IOCTL_CLEAR
:
966 retval
= usbtmc_ioctl_clear(data
);
969 case USBTMC_IOCTL_ABORT_BULK_OUT
:
970 retval
= usbtmc_ioctl_abort_bulk_out(data
);
973 case USBTMC_IOCTL_ABORT_BULK_IN
:
974 retval
= usbtmc_ioctl_abort_bulk_in(data
);
979 mutex_unlock(&data
->io_mutex
);
983 static const struct file_operations fops
= {
984 .owner
= THIS_MODULE
,
986 .write
= usbtmc_write
,
988 .release
= usbtmc_release
,
989 .unlocked_ioctl
= usbtmc_ioctl
,
992 static struct usb_class_driver usbtmc_class
= {
995 .minor_base
= USBTMC_MINOR_BASE
,
999 static int usbtmc_probe(struct usb_interface
*intf
,
1000 const struct usb_device_id
*id
)
1002 struct usbtmc_device_data
*data
;
1003 struct usb_host_interface
*iface_desc
;
1004 struct usb_endpoint_descriptor
*endpoint
;
1008 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
1010 data
= kmalloc(sizeof(struct usbtmc_device_data
), GFP_KERNEL
);
1012 dev_err(&intf
->dev
, "Unable to allocate kernel memory\n");
1018 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
1019 usb_set_intfdata(intf
, data
);
1020 kref_init(&data
->kref
);
1021 mutex_init(&data
->io_mutex
);
1024 /* Initialize USBTMC bTag and other fields */
1026 data
->TermCharEnabled
= 0;
1027 data
->TermChar
= '\n';
1029 /* USBTMC devices have only one setting, so use that */
1030 iface_desc
= data
->intf
->cur_altsetting
;
1032 /* Find bulk in endpoint */
1033 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1034 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1036 if (usb_endpoint_is_bulk_in(endpoint
)) {
1037 data
->bulk_in
= endpoint
->bEndpointAddress
;
1038 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n",
1044 /* Find bulk out endpoint */
1045 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1046 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1048 if (usb_endpoint_is_bulk_out(endpoint
)) {
1049 data
->bulk_out
= endpoint
->bEndpointAddress
;
1050 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n",
1056 retcode
= get_capabilities(data
);
1058 dev_err(&intf
->dev
, "can't read capabilities\n");
1060 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
1061 &capability_attr_grp
);
1063 retcode
= sysfs_create_group(&intf
->dev
.kobj
, &data_attr_grp
);
1065 retcode
= usb_register_dev(intf
, &usbtmc_class
);
1067 dev_err(&intf
->dev
, "Not able to get a minor"
1068 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE
,
1070 goto error_register
;
1072 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
1077 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1078 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1079 kref_put(&data
->kref
, usbtmc_delete
);
1083 static void usbtmc_disconnect(struct usb_interface
*intf
)
1085 struct usbtmc_device_data
*data
;
1087 dev_dbg(&intf
->dev
, "usbtmc_disconnect called\n");
1089 data
= usb_get_intfdata(intf
);
1090 usb_deregister_dev(intf
, &usbtmc_class
);
1091 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1092 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1093 mutex_lock(&data
->io_mutex
);
1095 mutex_unlock(&data
->io_mutex
);
1096 kref_put(&data
->kref
, usbtmc_delete
);
1099 static int usbtmc_suspend(struct usb_interface
*intf
, pm_message_t message
)
1101 /* this driver does not have pending URBs */
1105 static int usbtmc_resume(struct usb_interface
*intf
)
1110 static struct usb_driver usbtmc_driver
= {
1112 .id_table
= usbtmc_devices
,
1113 .probe
= usbtmc_probe
,
1114 .disconnect
= usbtmc_disconnect
,
1115 .suspend
= usbtmc_suspend
,
1116 .resume
= usbtmc_resume
,
1119 static int __init
usbtmc_init(void)
1123 retcode
= usb_register(&usbtmc_driver
);
1125 printk(KERN_ERR KBUILD_MODNAME
": Unable to register driver\n");
1128 module_init(usbtmc_init
);
1130 static void __exit
usbtmc_exit(void)
1132 usb_deregister(&usbtmc_driver
);
1134 module_exit(usbtmc_exit
);
1136 MODULE_LICENSE("GPL");