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 5000
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,
61 * and section 4.2.2 of the USBTMC usb488 subclass
62 * specification for details.
64 struct usbtmc_dev_capabilities
{
65 __u8 interface_capabilities
;
66 __u8 device_capabilities
;
67 __u8 usb488_interface_capabilities
;
68 __u8 usb488_device_capabilities
;
71 /* This structure holds private data for each USBTMC device. One copy is
72 * allocated for each USBTMC device in the driver's probe function.
74 struct usbtmc_device_data
{
75 const struct usb_device_id
*id
;
76 struct usb_device
*usb_dev
;
77 struct usb_interface
*intf
;
80 unsigned int bulk_out
;
83 u8 bTag_last_write
; /* needed for abort */
84 u8 bTag_last_read
; /* needed for abort */
86 /* attributes from the USB TMC spec for this device */
91 bool zombie
; /* fd of disconnected device */
93 struct usbtmc_dev_capabilities capabilities
;
95 struct mutex io_mutex
; /* only one i/o function running at a time */
97 #define to_usbtmc_data(d) container_of(d, struct usbtmc_device_data, kref)
99 /* Forward declarations */
100 static struct usb_driver usbtmc_driver
;
102 static void usbtmc_delete(struct kref
*kref
)
104 struct usbtmc_device_data
*data
= to_usbtmc_data(kref
);
106 usb_put_dev(data
->usb_dev
);
110 static int usbtmc_open(struct inode
*inode
, struct file
*filp
)
112 struct usb_interface
*intf
;
113 struct usbtmc_device_data
*data
;
116 intf
= usb_find_interface(&usbtmc_driver
, iminor(inode
));
118 printk(KERN_ERR KBUILD_MODNAME
119 ": can not find device for minor %d", iminor(inode
));
124 data
= usb_get_intfdata(intf
);
125 kref_get(&data
->kref
);
127 /* Store pointer in file structure's private data field */
128 filp
->private_data
= data
;
134 static int usbtmc_release(struct inode
*inode
, struct file
*file
)
136 struct usbtmc_device_data
*data
= file
->private_data
;
138 kref_put(&data
->kref
, usbtmc_delete
);
142 static int usbtmc_ioctl_abort_bulk_in(struct usbtmc_device_data
*data
)
149 struct usb_host_interface
*current_setting
;
152 dev
= &data
->intf
->dev
;
153 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
157 rv
= usb_control_msg(data
->usb_dev
,
158 usb_rcvctrlpipe(data
->usb_dev
, 0),
159 USBTMC_REQUEST_INITIATE_ABORT_BULK_IN
,
160 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
161 data
->bTag_last_read
, data
->bulk_in
,
162 buffer
, 2, USBTMC_TIMEOUT
);
165 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
169 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
171 if (buffer
[0] == USBTMC_STATUS_FAILED
) {
176 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
177 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n",
184 current_setting
= data
->intf
->cur_altsetting
;
185 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++)
186 if (current_setting
->endpoint
[n
].desc
.bEndpointAddress
==
188 max_size
= le16_to_cpu(current_setting
->endpoint
[n
].
189 desc
.wMaxPacketSize
);
192 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
197 dev_dbg(&data
->intf
->dev
, "wMaxPacketSize is %d\n", max_size
);
202 dev_dbg(dev
, "Reading from bulk in EP\n");
204 rv
= usb_bulk_msg(data
->usb_dev
,
205 usb_rcvbulkpipe(data
->usb_dev
,
207 buffer
, USBTMC_SIZE_IOBUFFER
,
208 &actual
, USBTMC_TIMEOUT
);
213 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
216 } while ((actual
== max_size
) &&
217 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
219 if (actual
== max_size
) {
220 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
221 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
228 usbtmc_abort_bulk_in_status
:
229 rv
= usb_control_msg(data
->usb_dev
,
230 usb_rcvctrlpipe(data
->usb_dev
, 0),
231 USBTMC_REQUEST_CHECK_ABORT_BULK_IN_STATUS
,
232 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
233 0, data
->bulk_in
, buffer
, 0x08,
237 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
241 dev_dbg(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
243 if (buffer
[0] == USBTMC_STATUS_SUCCESS
) {
248 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
249 dev_err(dev
, "INITIATE_ABORT_BULK_IN returned %x\n", buffer
[0]);
256 dev_dbg(dev
, "Reading from bulk in EP\n");
258 rv
= usb_bulk_msg(data
->usb_dev
,
259 usb_rcvbulkpipe(data
->usb_dev
,
261 buffer
, USBTMC_SIZE_IOBUFFER
,
262 &actual
, USBTMC_TIMEOUT
);
267 dev_err(dev
, "usb_bulk_msg returned %d\n", rv
);
270 } while ((actual
= max_size
) &&
271 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
273 if (actual
== max_size
) {
274 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
275 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
280 goto usbtmc_abort_bulk_in_status
;
288 static int usbtmc_ioctl_abort_bulk_out(struct usbtmc_device_data
*data
)
295 dev
= &data
->intf
->dev
;
297 buffer
= kmalloc(8, GFP_KERNEL
);
301 rv
= usb_control_msg(data
->usb_dev
,
302 usb_rcvctrlpipe(data
->usb_dev
, 0),
303 USBTMC_REQUEST_INITIATE_ABORT_BULK_OUT
,
304 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
305 data
->bTag_last_write
, data
->bulk_out
,
306 buffer
, 2, USBTMC_TIMEOUT
);
309 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
313 dev_dbg(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n", buffer
[0]);
315 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
316 dev_err(dev
, "INITIATE_ABORT_BULK_OUT returned %x\n",
324 usbtmc_abort_bulk_out_check_status
:
325 rv
= usb_control_msg(data
->usb_dev
,
326 usb_rcvctrlpipe(data
->usb_dev
, 0),
327 USBTMC_REQUEST_CHECK_ABORT_BULK_OUT_STATUS
,
328 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_ENDPOINT
,
329 0, data
->bulk_out
, buffer
, 0x08,
333 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
337 dev_dbg(dev
, "CHECK_ABORT_BULK_OUT returned %x\n", buffer
[0]);
339 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
340 goto usbtmc_abort_bulk_out_clear_halt
;
342 if ((buffer
[0] == USBTMC_STATUS_PENDING
) &&
343 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
))
344 goto usbtmc_abort_bulk_out_check_status
;
349 usbtmc_abort_bulk_out_clear_halt
:
350 rv
= usb_control_msg(data
->usb_dev
,
351 usb_sndctrlpipe(data
->usb_dev
, 0),
352 USB_REQ_CLEAR_FEATURE
,
353 USB_DIR_OUT
| USB_TYPE_STANDARD
|
355 USB_ENDPOINT_HALT
, data
->bulk_out
, buffer
,
359 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
369 static ssize_t
usbtmc_read(struct file
*filp
, char __user
*buf
,
370 size_t count
, loff_t
*f_pos
)
372 struct usbtmc_device_data
*data
;
382 /* Get pointer to private data structure */
383 data
= filp
->private_data
;
384 dev
= &data
->intf
->dev
;
386 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
390 mutex_lock(&data
->io_mutex
);
399 while (remaining
> 0) {
400 if (remaining
> USBTMC_SIZE_IOBUFFER
- 12 - 3)
401 this_part
= USBTMC_SIZE_IOBUFFER
- 12 - 3;
403 this_part
= remaining
;
405 /* Setup IO buffer for DEV_DEP_MSG_IN message
406 * Refer to class specs for details
409 buffer
[1] = data
->bTag
;
410 buffer
[2] = ~(data
->bTag
);
411 buffer
[3] = 0; /* Reserved */
412 buffer
[4] = (this_part
) & 255;
413 buffer
[5] = ((this_part
) >> 8) & 255;
414 buffer
[6] = ((this_part
) >> 16) & 255;
415 buffer
[7] = ((this_part
) >> 24) & 255;
416 buffer
[8] = data
->TermCharEnabled
* 2;
417 /* Use term character? */
418 buffer
[9] = data
->TermChar
;
419 buffer
[10] = 0; /* Reserved */
420 buffer
[11] = 0; /* Reserved */
423 retval
= usb_bulk_msg(data
->usb_dev
,
424 usb_sndbulkpipe(data
->usb_dev
,
426 buffer
, 12, &actual
, USBTMC_TIMEOUT
);
428 /* Store bTag (in case we need to abort) */
429 data
->bTag_last_write
= data
->bTag
;
431 /* Increment bTag -- and increment again if zero */
437 dev_err(dev
, "usb_bulk_msg returned %d\n", retval
);
438 if (data
->auto_abort
)
439 usbtmc_ioctl_abort_bulk_out(data
);
444 retval
= usb_bulk_msg(data
->usb_dev
,
445 usb_rcvbulkpipe(data
->usb_dev
,
447 buffer
, USBTMC_SIZE_IOBUFFER
, &actual
,
450 /* Store bTag (in case we need to abort) */
451 data
->bTag_last_read
= data
->bTag
;
454 dev_err(dev
, "Unable to read data, error %d\n", retval
);
455 if (data
->auto_abort
)
456 usbtmc_ioctl_abort_bulk_in(data
);
460 /* How many characters did the instrument send? */
461 n_characters
= buffer
[4] +
466 /* Ensure the instrument doesn't lie about it */
467 if(n_characters
> actual
- 12) {
468 dev_err(dev
, "Device lies about message size: %u > %d\n", n_characters
, actual
- 12);
469 n_characters
= actual
- 12;
472 /* Ensure the instrument doesn't send more back than requested */
473 if(n_characters
> this_part
) {
474 dev_err(dev
, "Device returns more than requested: %zu > %zu\n", done
+ n_characters
, done
+ this_part
);
475 n_characters
= this_part
;
478 /* Bound amount of data received by amount of data requested */
479 if (n_characters
> this_part
)
480 n_characters
= this_part
;
482 /* Copy buffer to user space */
483 if (copy_to_user(buf
+ done
, &buffer
[12], n_characters
)) {
484 /* There must have been an addressing problem */
489 done
+= n_characters
;
490 /* Terminate if end-of-message bit recieved from device */
491 if ((buffer
[8] & 0x01) && (actual
>= n_characters
+ 12))
494 remaining
-= n_characters
;
497 /* Update file position value */
498 *f_pos
= *f_pos
+ done
;
502 mutex_unlock(&data
->io_mutex
);
507 static ssize_t
usbtmc_write(struct file
*filp
, const char __user
*buf
,
508 size_t count
, loff_t
*f_pos
)
510 struct usbtmc_device_data
*data
;
514 unsigned long int n_bytes
;
519 data
= filp
->private_data
;
521 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
525 mutex_lock(&data
->io_mutex
);
534 while (remaining
> 0) {
535 if (remaining
> USBTMC_SIZE_IOBUFFER
- 12) {
536 this_part
= USBTMC_SIZE_IOBUFFER
- 12;
539 this_part
= remaining
;
543 /* Setup IO buffer for DEV_DEP_MSG_OUT message */
545 buffer
[1] = data
->bTag
;
546 buffer
[2] = ~(data
->bTag
);
547 buffer
[3] = 0; /* Reserved */
548 buffer
[4] = this_part
& 255;
549 buffer
[5] = (this_part
>> 8) & 255;
550 buffer
[6] = (this_part
>> 16) & 255;
551 buffer
[7] = (this_part
>> 24) & 255;
552 /* buffer[8] is set above... */
553 buffer
[9] = 0; /* Reserved */
554 buffer
[10] = 0; /* Reserved */
555 buffer
[11] = 0; /* Reserved */
557 if (copy_from_user(&buffer
[12], buf
+ done
, this_part
)) {
562 n_bytes
= roundup(12 + this_part
, 4);
563 memset(buffer
+ 12 + this_part
, 0, n_bytes
- (12 + this_part
));
565 retval
= usb_bulk_msg(data
->usb_dev
,
566 usb_sndbulkpipe(data
->usb_dev
,
568 buffer
, n_bytes
, &actual
, USBTMC_TIMEOUT
);
570 data
->bTag_last_write
= data
->bTag
;
577 dev_err(&data
->intf
->dev
,
578 "Unable to send data, error %d\n", retval
);
579 if (data
->auto_abort
)
580 usbtmc_ioctl_abort_bulk_out(data
);
584 remaining
-= this_part
;
590 mutex_unlock(&data
->io_mutex
);
595 static int usbtmc_ioctl_clear(struct usbtmc_device_data
*data
)
597 struct usb_host_interface
*current_setting
;
598 struct usb_endpoint_descriptor
*desc
;
606 dev
= &data
->intf
->dev
;
608 dev_dbg(dev
, "Sending INITIATE_CLEAR request\n");
610 buffer
= kmalloc(USBTMC_SIZE_IOBUFFER
, GFP_KERNEL
);
614 rv
= usb_control_msg(data
->usb_dev
,
615 usb_rcvctrlpipe(data
->usb_dev
, 0),
616 USBTMC_REQUEST_INITIATE_CLEAR
,
617 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
618 0, 0, buffer
, 1, USBTMC_TIMEOUT
);
620 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
624 dev_dbg(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
626 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
627 dev_err(dev
, "INITIATE_CLEAR returned %x\n", buffer
[0]);
633 current_setting
= data
->intf
->cur_altsetting
;
634 for (n
= 0; n
< current_setting
->desc
.bNumEndpoints
; n
++) {
635 desc
= ¤t_setting
->endpoint
[n
].desc
;
636 if (desc
->bEndpointAddress
== data
->bulk_in
)
637 max_size
= le16_to_cpu(desc
->wMaxPacketSize
);
641 dev_err(dev
, "Couldn't get wMaxPacketSize\n");
646 dev_dbg(dev
, "wMaxPacketSize is %d\n", max_size
);
650 usbtmc_clear_check_status
:
652 dev_dbg(dev
, "Sending CHECK_CLEAR_STATUS request\n");
654 rv
= usb_control_msg(data
->usb_dev
,
655 usb_rcvctrlpipe(data
->usb_dev
, 0),
656 USBTMC_REQUEST_CHECK_CLEAR_STATUS
,
657 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
658 0, 0, buffer
, 2, USBTMC_TIMEOUT
);
660 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
664 dev_dbg(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
666 if (buffer
[0] == USBTMC_STATUS_SUCCESS
)
667 goto usbtmc_clear_bulk_out_halt
;
669 if (buffer
[0] != USBTMC_STATUS_PENDING
) {
670 dev_err(dev
, "CHECK_CLEAR_STATUS returned %x\n", buffer
[0]);
677 dev_dbg(dev
, "Reading from bulk in EP\n");
679 rv
= usb_bulk_msg(data
->usb_dev
,
680 usb_rcvbulkpipe(data
->usb_dev
,
682 buffer
, USBTMC_SIZE_IOBUFFER
,
683 &actual
, USBTMC_TIMEOUT
);
687 dev_err(dev
, "usb_control_msg returned %d\n",
691 } while ((actual
== max_size
) &&
692 (n
< USBTMC_MAX_READS_TO_CLEAR_BULK_IN
));
694 if (actual
== max_size
) {
695 dev_err(dev
, "Couldn't clear device buffer within %d cycles\n",
696 USBTMC_MAX_READS_TO_CLEAR_BULK_IN
);
701 goto usbtmc_clear_check_status
;
703 usbtmc_clear_bulk_out_halt
:
705 rv
= usb_control_msg(data
->usb_dev
,
706 usb_sndctrlpipe(data
->usb_dev
, 0),
707 USB_REQ_CLEAR_FEATURE
,
708 USB_DIR_OUT
| USB_TYPE_STANDARD
|
711 data
->bulk_out
, buffer
, 0,
714 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
724 static int usbtmc_ioctl_clear_out_halt(struct usbtmc_device_data
*data
)
729 buffer
= kmalloc(2, GFP_KERNEL
);
733 rv
= usb_control_msg(data
->usb_dev
,
734 usb_sndctrlpipe(data
->usb_dev
, 0),
735 USB_REQ_CLEAR_FEATURE
,
736 USB_DIR_OUT
| USB_TYPE_STANDARD
|
738 USB_ENDPOINT_HALT
, data
->bulk_out
,
739 buffer
, 0, USBTMC_TIMEOUT
);
742 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
753 static int usbtmc_ioctl_clear_in_halt(struct usbtmc_device_data
*data
)
758 buffer
= kmalloc(2, GFP_KERNEL
);
762 rv
= usb_control_msg(data
->usb_dev
, usb_sndctrlpipe(data
->usb_dev
, 0),
763 USB_REQ_CLEAR_FEATURE
,
764 USB_DIR_OUT
| USB_TYPE_STANDARD
|
766 USB_ENDPOINT_HALT
, data
->bulk_in
, buffer
, 0,
770 dev_err(&data
->usb_dev
->dev
, "usb_control_msg returned %d\n",
781 static int get_capabilities(struct usbtmc_device_data
*data
)
783 struct device
*dev
= &data
->usb_dev
->dev
;
787 buffer
= kmalloc(0x18, GFP_KERNEL
);
791 rv
= usb_control_msg(data
->usb_dev
, usb_rcvctrlpipe(data
->usb_dev
, 0),
792 USBTMC_REQUEST_GET_CAPABILITIES
,
793 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
794 0, 0, buffer
, 0x18, USBTMC_TIMEOUT
);
796 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
800 dev_dbg(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
801 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
802 dev_err(dev
, "GET_CAPABILITIES returned %x\n", buffer
[0]);
806 dev_dbg(dev
, "Interface capabilities are %x\n", buffer
[4]);
807 dev_dbg(dev
, "Device capabilities are %x\n", buffer
[5]);
808 dev_dbg(dev
, "USB488 interface capabilities are %x\n", buffer
[14]);
809 dev_dbg(dev
, "USB488 device capabilities are %x\n", buffer
[15]);
811 data
->capabilities
.interface_capabilities
= buffer
[4];
812 data
->capabilities
.device_capabilities
= buffer
[5];
813 data
->capabilities
.usb488_interface_capabilities
= buffer
[14];
814 data
->capabilities
.usb488_device_capabilities
= buffer
[15];
822 #define capability_attribute(name) \
823 static ssize_t show_##name(struct device *dev, \
824 struct device_attribute *attr, char *buf) \
826 struct usb_interface *intf = to_usb_interface(dev); \
827 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
829 return sprintf(buf, "%d\n", data->capabilities.name); \
831 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
833 capability_attribute(interface_capabilities
);
834 capability_attribute(device_capabilities
);
835 capability_attribute(usb488_interface_capabilities
);
836 capability_attribute(usb488_device_capabilities
);
838 static struct attribute
*capability_attrs
[] = {
839 &dev_attr_interface_capabilities
.attr
,
840 &dev_attr_device_capabilities
.attr
,
841 &dev_attr_usb488_interface_capabilities
.attr
,
842 &dev_attr_usb488_device_capabilities
.attr
,
846 static struct attribute_group capability_attr_grp
= {
847 .attrs
= capability_attrs
,
850 static ssize_t
show_TermChar(struct device
*dev
,
851 struct device_attribute
*attr
, char *buf
)
853 struct usb_interface
*intf
= to_usb_interface(dev
);
854 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
856 return sprintf(buf
, "%c\n", data
->TermChar
);
859 static ssize_t
store_TermChar(struct device
*dev
,
860 struct device_attribute
*attr
,
861 const char *buf
, size_t count
)
863 struct usb_interface
*intf
= to_usb_interface(dev
);
864 struct usbtmc_device_data
*data
= usb_get_intfdata(intf
);
868 data
->TermChar
= buf
[0];
871 static DEVICE_ATTR(TermChar
, S_IRUGO
, show_TermChar
, store_TermChar
);
873 #define data_attribute(name) \
874 static ssize_t show_##name(struct device *dev, \
875 struct device_attribute *attr, char *buf) \
877 struct usb_interface *intf = to_usb_interface(dev); \
878 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
880 return sprintf(buf, "%d\n", data->name); \
882 static ssize_t store_##name(struct device *dev, \
883 struct device_attribute *attr, \
884 const char *buf, size_t count) \
886 struct usb_interface *intf = to_usb_interface(dev); \
887 struct usbtmc_device_data *data = usb_get_intfdata(intf); \
891 result = sscanf(buf, "%u\n", &val); \
900 static DEVICE_ATTR(name, S_IRUGO, show_##name, store_##name)
902 data_attribute(TermCharEnabled
);
903 data_attribute(auto_abort
);
905 static struct attribute
*data_attrs
[] = {
906 &dev_attr_TermChar
.attr
,
907 &dev_attr_TermCharEnabled
.attr
,
908 &dev_attr_auto_abort
.attr
,
912 static struct attribute_group data_attr_grp
= {
916 static int usbtmc_ioctl_indicator_pulse(struct usbtmc_device_data
*data
)
922 dev
= &data
->intf
->dev
;
924 buffer
= kmalloc(2, GFP_KERNEL
);
928 rv
= usb_control_msg(data
->usb_dev
,
929 usb_rcvctrlpipe(data
->usb_dev
, 0),
930 USBTMC_REQUEST_INDICATOR_PULSE
,
931 USB_DIR_IN
| USB_TYPE_CLASS
| USB_RECIP_INTERFACE
,
932 0, 0, buffer
, 0x01, USBTMC_TIMEOUT
);
935 dev_err(dev
, "usb_control_msg returned %d\n", rv
);
939 dev_dbg(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
941 if (buffer
[0] != USBTMC_STATUS_SUCCESS
) {
942 dev_err(dev
, "INDICATOR_PULSE returned %x\n", buffer
[0]);
953 static long usbtmc_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
955 struct usbtmc_device_data
*data
;
956 int retval
= -EBADRQC
;
958 data
= file
->private_data
;
959 mutex_lock(&data
->io_mutex
);
962 goto skip_io_on_zombie
;
966 case USBTMC_IOCTL_CLEAR_OUT_HALT
:
967 retval
= usbtmc_ioctl_clear_out_halt(data
);
970 case USBTMC_IOCTL_CLEAR_IN_HALT
:
971 retval
= usbtmc_ioctl_clear_in_halt(data
);
974 case USBTMC_IOCTL_INDICATOR_PULSE
:
975 retval
= usbtmc_ioctl_indicator_pulse(data
);
978 case USBTMC_IOCTL_CLEAR
:
979 retval
= usbtmc_ioctl_clear(data
);
982 case USBTMC_IOCTL_ABORT_BULK_OUT
:
983 retval
= usbtmc_ioctl_abort_bulk_out(data
);
986 case USBTMC_IOCTL_ABORT_BULK_IN
:
987 retval
= usbtmc_ioctl_abort_bulk_in(data
);
992 mutex_unlock(&data
->io_mutex
);
996 static const struct file_operations fops
= {
997 .owner
= THIS_MODULE
,
999 .write
= usbtmc_write
,
1000 .open
= usbtmc_open
,
1001 .release
= usbtmc_release
,
1002 .unlocked_ioctl
= usbtmc_ioctl
,
1005 static struct usb_class_driver usbtmc_class
= {
1008 .minor_base
= USBTMC_MINOR_BASE
,
1012 static int usbtmc_probe(struct usb_interface
*intf
,
1013 const struct usb_device_id
*id
)
1015 struct usbtmc_device_data
*data
;
1016 struct usb_host_interface
*iface_desc
;
1017 struct usb_endpoint_descriptor
*endpoint
;
1021 dev_dbg(&intf
->dev
, "%s called\n", __func__
);
1023 data
= kmalloc(sizeof(struct usbtmc_device_data
), GFP_KERNEL
);
1025 dev_err(&intf
->dev
, "Unable to allocate kernel memory\n");
1031 data
->usb_dev
= usb_get_dev(interface_to_usbdev(intf
));
1032 usb_set_intfdata(intf
, data
);
1033 kref_init(&data
->kref
);
1034 mutex_init(&data
->io_mutex
);
1037 /* Initialize USBTMC bTag and other fields */
1039 data
->TermCharEnabled
= 0;
1040 data
->TermChar
= '\n';
1042 /* USBTMC devices have only one setting, so use that */
1043 iface_desc
= data
->intf
->cur_altsetting
;
1045 /* Find bulk in endpoint */
1046 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1047 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1049 if (usb_endpoint_is_bulk_in(endpoint
)) {
1050 data
->bulk_in
= endpoint
->bEndpointAddress
;
1051 dev_dbg(&intf
->dev
, "Found bulk in endpoint at %u\n",
1057 /* Find bulk out endpoint */
1058 for (n
= 0; n
< iface_desc
->desc
.bNumEndpoints
; n
++) {
1059 endpoint
= &iface_desc
->endpoint
[n
].desc
;
1061 if (usb_endpoint_is_bulk_out(endpoint
)) {
1062 data
->bulk_out
= endpoint
->bEndpointAddress
;
1063 dev_dbg(&intf
->dev
, "Found Bulk out endpoint at %u\n",
1069 retcode
= get_capabilities(data
);
1071 dev_err(&intf
->dev
, "can't read capabilities\n");
1073 retcode
= sysfs_create_group(&intf
->dev
.kobj
,
1074 &capability_attr_grp
);
1076 retcode
= sysfs_create_group(&intf
->dev
.kobj
, &data_attr_grp
);
1078 retcode
= usb_register_dev(intf
, &usbtmc_class
);
1080 dev_err(&intf
->dev
, "Not able to get a minor"
1081 " (base %u, slice default): %d\n", USBTMC_MINOR_BASE
,
1083 goto error_register
;
1085 dev_dbg(&intf
->dev
, "Using minor number %d\n", intf
->minor
);
1090 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1091 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1092 kref_put(&data
->kref
, usbtmc_delete
);
1096 static void usbtmc_disconnect(struct usb_interface
*intf
)
1098 struct usbtmc_device_data
*data
;
1100 dev_dbg(&intf
->dev
, "usbtmc_disconnect called\n");
1102 data
= usb_get_intfdata(intf
);
1103 usb_deregister_dev(intf
, &usbtmc_class
);
1104 sysfs_remove_group(&intf
->dev
.kobj
, &capability_attr_grp
);
1105 sysfs_remove_group(&intf
->dev
.kobj
, &data_attr_grp
);
1106 mutex_lock(&data
->io_mutex
);
1108 mutex_unlock(&data
->io_mutex
);
1109 kref_put(&data
->kref
, usbtmc_delete
);
1112 static int usbtmc_suspend (struct usb_interface
*intf
, pm_message_t message
)
1114 /* this driver does not have pending URBs */
1118 static int usbtmc_resume (struct usb_interface
*intf
)
1123 static struct usb_driver usbtmc_driver
= {
1125 .id_table
= usbtmc_devices
,
1126 .probe
= usbtmc_probe
,
1127 .disconnect
= usbtmc_disconnect
,
1128 .suspend
= usbtmc_suspend
,
1129 .resume
= usbtmc_resume
,
1132 static int __init
usbtmc_init(void)
1136 retcode
= usb_register(&usbtmc_driver
);
1138 printk(KERN_ERR KBUILD_MODNAME
": Unable to register driver\n");
1141 module_init(usbtmc_init
);
1143 static void __exit
usbtmc_exit(void)
1145 usb_deregister(&usbtmc_driver
);
1147 module_exit(usbtmc_exit
);
1149 MODULE_LICENSE("GPL");