Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/usb/core/sysfs.c |
| 3 | * |
| 4 | * (C) Copyright 2002 David Brownell |
| 5 | * (C) Copyright 2002,2004 Greg Kroah-Hartman |
| 6 | * (C) Copyright 2002,2004 IBM Corp. |
| 7 | * |
| 8 | * All of the sysfs file attributes for usb devices and interfaces. |
| 9 | * |
| 10 | */ |
| 11 | |
| 12 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 14 | #include <linux/string.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/usb.h> |
Alan Stern | 1662e3a | 2009-03-18 14:28:53 -0400 | [diff] [blame] | 16 | #include <linux/usb/quirks.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include "usb.h" |
| 18 | |
| 19 | /* Active configuration fields */ |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 20 | #define usb_actconfig_show(field, format_string) \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 21 | static ssize_t field##_show(struct device *dev, \ |
| 22 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { \ |
| 24 | struct usb_device *udev; \ |
| 25 | struct usb_host_config *actconfig; \ |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 26 | ssize_t rc; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 28 | udev = to_usb_device(dev); \ |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 29 | rc = usb_lock_device_interruptible(udev); \ |
| 30 | if (rc < 0) \ |
| 31 | return -EINTR; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | actconfig = udev->actconfig; \ |
| 33 | if (actconfig) \ |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 34 | rc = sprintf(buf, format_string, \ |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 35 | actconfig->desc.field); \ |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 36 | usb_unlock_device(udev); \ |
| 37 | return rc; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } \ |
| 39 | |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 40 | #define usb_actconfig_attr(field, format_string) \ |
| 41 | usb_actconfig_show(field, format_string) \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 42 | static DEVICE_ATTR_RO(field) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 44 | usb_actconfig_attr(bNumInterfaces, "%2d\n"); |
| 45 | usb_actconfig_attr(bmAttributes, "%2x\n"); |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 46 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 47 | static ssize_t bMaxPower_show(struct device *dev, |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 48 | struct device_attribute *attr, char *buf) |
| 49 | { |
| 50 | struct usb_device *udev; |
| 51 | struct usb_host_config *actconfig; |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 52 | ssize_t rc; |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 53 | |
| 54 | udev = to_usb_device(dev); |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 55 | rc = usb_lock_device_interruptible(udev); |
| 56 | if (rc < 0) |
| 57 | return -EINTR; |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 58 | actconfig = udev->actconfig; |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 59 | if (actconfig) |
| 60 | rc = sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig)); |
| 61 | usb_unlock_device(udev); |
| 62 | return rc; |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 63 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 64 | static DEVICE_ATTR_RO(bMaxPower); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 66 | static ssize_t configuration_show(struct device *dev, |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 67 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
| 69 | struct usb_device *udev; |
| 70 | struct usb_host_config *actconfig; |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 71 | ssize_t rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 73 | udev = to_usb_device(dev); |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 74 | rc = usb_lock_device_interruptible(udev); |
| 75 | if (rc < 0) |
| 76 | return -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | actconfig = udev->actconfig; |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 78 | if (actconfig && actconfig->string) |
| 79 | rc = sprintf(buf, "%s\n", actconfig->string); |
| 80 | usb_unlock_device(udev); |
| 81 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 83 | static DEVICE_ATTR_RO(configuration); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
| 85 | /* configuration value is always present, and r/w */ |
Sebastian Andrzej Siewior | 8d8479d | 2012-12-18 15:25:46 +0100 | [diff] [blame] | 86 | usb_actconfig_show(bConfigurationValue, "%u\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 88 | static ssize_t bConfigurationValue_store(struct device *dev, |
| 89 | struct device_attribute *attr, |
| 90 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 92 | struct usb_device *udev = to_usb_device(dev); |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 93 | int config, value, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 95 | if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return -EINVAL; |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 97 | rc = usb_lock_device_interruptible(udev); |
| 98 | if (rc < 0) |
| 99 | return -EINTR; |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 100 | value = usb_set_configuration(udev, config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | usb_unlock_device(udev); |
| 102 | return (value < 0) ? value : count; |
| 103 | } |
Alan Stern | 356c05d | 2012-05-14 13:30:03 -0400 | [diff] [blame] | 104 | static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR, |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 105 | bConfigurationValue_show, bConfigurationValue_store); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | /* String fields */ |
| 108 | #define usb_string_attr(name) \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 109 | static ssize_t name##_show(struct device *dev, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 110 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { \ |
| 112 | struct usb_device *udev; \ |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 113 | int retval; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 115 | udev = to_usb_device(dev); \ |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 116 | retval = usb_lock_device_interruptible(udev); \ |
| 117 | if (retval < 0) \ |
| 118 | return -EINTR; \ |
Alan Stern | da30712 | 2009-12-08 15:54:44 -0500 | [diff] [blame] | 119 | retval = sprintf(buf, "%s\n", udev->name); \ |
| 120 | usb_unlock_device(udev); \ |
| 121 | return retval; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 123 | static DEVICE_ATTR_RO(name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 125 | usb_string_attr(product); |
| 126 | usb_string_attr(manufacturer); |
| 127 | usb_string_attr(serial); |
| 128 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 129 | static ssize_t speed_show(struct device *dev, struct device_attribute *attr, |
| 130 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | { |
| 132 | struct usb_device *udev; |
| 133 | char *speed; |
| 134 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 135 | udev = to_usb_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | |
| 137 | switch (udev->speed) { |
| 138 | case USB_SPEED_LOW: |
| 139 | speed = "1.5"; |
| 140 | break; |
| 141 | case USB_SPEED_UNKNOWN: |
| 142 | case USB_SPEED_FULL: |
| 143 | speed = "12"; |
| 144 | break; |
| 145 | case USB_SPEED_HIGH: |
| 146 | speed = "480"; |
| 147 | break; |
Greg Kroah-Hartman | 551cdbb | 2010-01-14 11:08:04 -0800 | [diff] [blame] | 148 | case USB_SPEED_WIRELESS: |
Greg Kroah-Hartman | b132b04 | 2010-01-14 10:33:19 -0800 | [diff] [blame] | 149 | speed = "480"; |
| 150 | break; |
| 151 | case USB_SPEED_SUPER: |
| 152 | speed = "5000"; |
| 153 | break; |
Mathias Nyman | b231664 | 2015-12-10 09:59:27 +0200 | [diff] [blame] | 154 | case USB_SPEED_SUPER_PLUS: |
| 155 | speed = "10000"; |
| 156 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | default: |
| 158 | speed = "unknown"; |
| 159 | } |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 160 | return sprintf(buf, "%s\n", speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 162 | static DEVICE_ATTR_RO(speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 164 | static ssize_t busnum_show(struct device *dev, struct device_attribute *attr, |
| 165 | char *buf) |
Alan Stern | 83f7d95 | 2007-04-25 15:15:43 -0400 | [diff] [blame] | 166 | { |
| 167 | struct usb_device *udev; |
| 168 | |
| 169 | udev = to_usb_device(dev); |
| 170 | return sprintf(buf, "%d\n", udev->bus->busnum); |
| 171 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 172 | static DEVICE_ATTR_RO(busnum); |
Alan Stern | 83f7d95 | 2007-04-25 15:15:43 -0400 | [diff] [blame] | 173 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 174 | static ssize_t devnum_show(struct device *dev, struct device_attribute *attr, |
| 175 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { |
| 177 | struct usb_device *udev; |
| 178 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 179 | udev = to_usb_device(dev); |
| 180 | return sprintf(buf, "%d\n", udev->devnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 182 | static DEVICE_ATTR_RO(devnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 184 | static ssize_t devpath_show(struct device *dev, struct device_attribute *attr, |
| 185 | char *buf) |
Greg Kroah-Hartman | 9af2362 | 2009-11-30 11:15:02 -0800 | [diff] [blame] | 186 | { |
| 187 | struct usb_device *udev; |
| 188 | |
| 189 | udev = to_usb_device(dev); |
| 190 | return sprintf(buf, "%s\n", udev->devpath); |
| 191 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 192 | static DEVICE_ATTR_RO(devpath); |
Greg Kroah-Hartman | 9af2362 | 2009-11-30 11:15:02 -0800 | [diff] [blame] | 193 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 194 | static ssize_t version_show(struct device *dev, struct device_attribute *attr, |
| 195 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
| 197 | struct usb_device *udev; |
| 198 | u16 bcdUSB; |
| 199 | |
| 200 | udev = to_usb_device(dev); |
| 201 | bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB); |
| 202 | return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff); |
| 203 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 204 | static DEVICE_ATTR_RO(version); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 206 | static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr, |
| 207 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | struct usb_device *udev; |
| 210 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 211 | udev = to_usb_device(dev); |
| 212 | return sprintf(buf, "%d\n", udev->maxchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 214 | static DEVICE_ATTR_RO(maxchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 216 | static ssize_t quirks_show(struct device *dev, struct device_attribute *attr, |
| 217 | char *buf) |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 218 | { |
| 219 | struct usb_device *udev; |
| 220 | |
| 221 | udev = to_usb_device(dev); |
| 222 | return sprintf(buf, "0x%x\n", udev->quirks); |
| 223 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 224 | static DEVICE_ATTR_RO(quirks); |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 225 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 226 | static ssize_t avoid_reset_quirk_show(struct device *dev, |
| 227 | struct device_attribute *attr, char *buf) |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 228 | { |
| 229 | struct usb_device *udev; |
| 230 | |
| 231 | udev = to_usb_device(dev); |
Lan Tianyu | 7fda953 | 2012-08-17 16:44:56 +0800 | [diff] [blame] | 232 | return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET)); |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 233 | } |
| 234 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 235 | static ssize_t avoid_reset_quirk_store(struct device *dev, |
| 236 | struct device_attribute *attr, |
| 237 | const char *buf, size_t count) |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 238 | { |
| 239 | struct usb_device *udev = to_usb_device(dev); |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 240 | int val, rc; |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 241 | |
Lan Tianyu | 7fc2cc3 | 2012-08-03 16:30:35 +0800 | [diff] [blame] | 242 | if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1) |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 243 | return -EINVAL; |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 244 | rc = usb_lock_device_interruptible(udev); |
| 245 | if (rc < 0) |
| 246 | return -EINTR; |
Lan Tianyu | 7fc2cc3 | 2012-08-03 16:30:35 +0800 | [diff] [blame] | 247 | if (val) |
Lan Tianyu | 7fda953 | 2012-08-17 16:44:56 +0800 | [diff] [blame] | 248 | udev->quirks |= USB_QUIRK_RESET; |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 249 | else |
Lan Tianyu | 7fda953 | 2012-08-17 16:44:56 +0800 | [diff] [blame] | 250 | udev->quirks &= ~USB_QUIRK_RESET; |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 251 | usb_unlock_device(udev); |
| 252 | return count; |
| 253 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 254 | static DEVICE_ATTR_RW(avoid_reset_quirk); |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 255 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 256 | static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr, |
| 257 | char *buf) |
Sarah Sharp | 4d59d8a | 2007-10-03 14:56:03 -0700 | [diff] [blame] | 258 | { |
| 259 | struct usb_device *udev; |
| 260 | |
| 261 | udev = to_usb_device(dev); |
| 262 | return sprintf(buf, "%d\n", atomic_read(&udev->urbnum)); |
| 263 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 264 | static DEVICE_ATTR_RO(urbnum); |
Sarah Sharp | 4d59d8a | 2007-10-03 14:56:03 -0700 | [diff] [blame] | 265 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 266 | static ssize_t removable_show(struct device *dev, struct device_attribute *attr, |
| 267 | char *buf) |
Matthew Garrett | 0846e7e | 2012-02-03 17:11:54 -0500 | [diff] [blame] | 268 | { |
| 269 | struct usb_device *udev; |
| 270 | char *state; |
| 271 | |
| 272 | udev = to_usb_device(dev); |
| 273 | |
| 274 | switch (udev->removable) { |
| 275 | case USB_DEVICE_REMOVABLE: |
| 276 | state = "removable"; |
| 277 | break; |
| 278 | case USB_DEVICE_FIXED: |
| 279 | state = "fixed"; |
| 280 | break; |
| 281 | default: |
| 282 | state = "unknown"; |
| 283 | } |
| 284 | |
| 285 | return sprintf(buf, "%s\n", state); |
| 286 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 287 | static DEVICE_ATTR_RO(removable); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 288 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 289 | static ssize_t ltm_capable_show(struct device *dev, |
| 290 | struct device_attribute *attr, char *buf) |
Sarah Sharp | 024f117 | 2012-07-05 17:17:24 -0700 | [diff] [blame] | 291 | { |
| 292 | if (usb_device_supports_ltm(to_usb_device(dev))) |
| 293 | return sprintf(buf, "%s\n", "yes"); |
| 294 | return sprintf(buf, "%s\n", "no"); |
| 295 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 296 | static DEVICE_ATTR_RO(ltm_capable); |
Sarah Sharp | 024f117 | 2012-07-05 17:17:24 -0700 | [diff] [blame] | 297 | |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 298 | #ifdef CONFIG_PM |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 299 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 300 | static ssize_t persist_show(struct device *dev, struct device_attribute *attr, |
| 301 | char *buf) |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 302 | { |
| 303 | struct usb_device *udev = to_usb_device(dev); |
| 304 | |
| 305 | return sprintf(buf, "%d\n", udev->persist_enabled); |
| 306 | } |
| 307 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 308 | static ssize_t persist_store(struct device *dev, struct device_attribute *attr, |
| 309 | const char *buf, size_t count) |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 310 | { |
| 311 | struct usb_device *udev = to_usb_device(dev); |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 312 | int value, rc; |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 313 | |
| 314 | /* Hubs are always enabled for USB_PERSIST */ |
| 315 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) |
| 316 | return -EPERM; |
| 317 | |
| 318 | if (sscanf(buf, "%d", &value) != 1) |
| 319 | return -EINVAL; |
Alan Stern | 0c4db6d | 2010-01-08 12:56:42 -0500 | [diff] [blame] | 320 | |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 321 | rc = usb_lock_device_interruptible(udev); |
| 322 | if (rc < 0) |
| 323 | return -EINTR; |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 324 | udev->persist_enabled = !!value; |
Alan Stern | 0c4db6d | 2010-01-08 12:56:42 -0500 | [diff] [blame] | 325 | usb_unlock_device(udev); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 326 | return count; |
| 327 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 328 | static DEVICE_ATTR_RW(persist); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 329 | |
| 330 | static int add_persist_attributes(struct device *dev) |
| 331 | { |
| 332 | int rc = 0; |
| 333 | |
| 334 | if (is_usb_device(dev)) { |
| 335 | struct usb_device *udev = to_usb_device(dev); |
| 336 | |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 337 | /* Hubs are automatically enabled for USB_PERSIST, |
| 338 | * no point in creating the attribute file. |
| 339 | */ |
| 340 | if (udev->descriptor.bDeviceClass != USB_CLASS_HUB) |
| 341 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 342 | &dev_attr_persist.attr, |
Alan Stern | 045cac6 | 2010-11-15 15:57:07 -0500 | [diff] [blame] | 343 | power_group_name); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 344 | } |
| 345 | return rc; |
| 346 | } |
| 347 | |
| 348 | static void remove_persist_attributes(struct device *dev) |
| 349 | { |
| 350 | sysfs_remove_file_from_group(&dev->kobj, |
| 351 | &dev_attr_persist.attr, |
Alan Stern | 045cac6 | 2010-11-15 15:57:07 -0500 | [diff] [blame] | 352 | power_group_name); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 353 | } |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 354 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 355 | static ssize_t connected_duration_show(struct device *dev, |
| 356 | struct device_attribute *attr, char *buf) |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 357 | { |
| 358 | struct usb_device *udev = to_usb_device(dev); |
| 359 | |
| 360 | return sprintf(buf, "%u\n", |
| 361 | jiffies_to_msecs(jiffies - udev->connect_time)); |
| 362 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 363 | static DEVICE_ATTR_RO(connected_duration); |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 364 | |
| 365 | /* |
| 366 | * If the device is resumed, the last time the device was suspended has |
| 367 | * been pre-subtracted from active_duration. We add the current time to |
| 368 | * get the duration that the device was actually active. |
| 369 | * |
| 370 | * If the device is suspended, the active_duration is up-to-date. |
| 371 | */ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 372 | static ssize_t active_duration_show(struct device *dev, |
| 373 | struct device_attribute *attr, char *buf) |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 374 | { |
| 375 | struct usb_device *udev = to_usb_device(dev); |
| 376 | int duration; |
| 377 | |
| 378 | if (udev->state != USB_STATE_SUSPENDED) |
| 379 | duration = jiffies_to_msecs(jiffies + udev->active_duration); |
| 380 | else |
| 381 | duration = jiffies_to_msecs(udev->active_duration); |
| 382 | return sprintf(buf, "%u\n", duration); |
| 383 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 384 | static DEVICE_ATTR_RO(active_duration); |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 385 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 386 | static ssize_t autosuspend_show(struct device *dev, |
| 387 | struct device_attribute *attr, char *buf) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 388 | { |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 389 | return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 390 | } |
| 391 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 392 | static ssize_t autosuspend_store(struct device *dev, |
| 393 | struct device_attribute *attr, const char *buf, |
| 394 | size_t count) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 395 | { |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 396 | int value; |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 397 | |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 398 | if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 || |
| 399 | value <= -INT_MAX/1000) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 400 | return -EINVAL; |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 401 | |
Alan Stern | fcc4a01 | 2010-11-15 15:57:51 -0500 | [diff] [blame] | 402 | pm_runtime_set_autosuspend_delay(dev, value * 1000); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 403 | return count; |
| 404 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 405 | static DEVICE_ATTR_RW(autosuspend); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 406 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 407 | static const char on_string[] = "on"; |
| 408 | static const char auto_string[] = "auto"; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 409 | |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 410 | static void warn_level(void) |
| 411 | { |
Alan Stern | a903098 | 2010-04-02 13:22:16 -0400 | [diff] [blame] | 412 | static int level_warned; |
| 413 | |
| 414 | if (!level_warned) { |
| 415 | level_warned = 1; |
| 416 | printk(KERN_WARNING "WARNING! power/level is deprecated; " |
| 417 | "use power/control instead\n"); |
| 418 | } |
| 419 | } |
| 420 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 421 | static ssize_t level_show(struct device *dev, struct device_attribute *attr, |
| 422 | char *buf) |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 423 | { |
| 424 | struct usb_device *udev = to_usb_device(dev); |
| 425 | const char *p = auto_string; |
| 426 | |
Alan Stern | a903098 | 2010-04-02 13:22:16 -0400 | [diff] [blame] | 427 | warn_level(); |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 428 | if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto) |
Alan Stern | 8e4ceb3 | 2009-12-07 13:01:37 -0500 | [diff] [blame] | 429 | p = on_string; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 430 | return sprintf(buf, "%s\n", p); |
| 431 | } |
| 432 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 433 | static ssize_t level_store(struct device *dev, struct device_attribute *attr, |
| 434 | const char *buf, size_t count) |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 435 | { |
| 436 | struct usb_device *udev = to_usb_device(dev); |
| 437 | int len = count; |
| 438 | char *cp; |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 439 | int rc = count; |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 440 | int rv; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 441 | |
Alan Stern | a903098 | 2010-04-02 13:22:16 -0400 | [diff] [blame] | 442 | warn_level(); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 443 | cp = memchr(buf, '\n', count); |
| 444 | if (cp) |
| 445 | len = cp - buf; |
| 446 | |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 447 | rv = usb_lock_device_interruptible(udev); |
| 448 | if (rv < 0) |
| 449 | return -EINTR; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 450 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 451 | if (len == sizeof on_string - 1 && |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 452 | strncmp(buf, on_string, len) == 0) |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 453 | usb_disable_autosuspend(udev); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 454 | |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 455 | else if (len == sizeof auto_string - 1 && |
| 456 | strncmp(buf, auto_string, len) == 0) |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 457 | usb_enable_autosuspend(udev); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 458 | |
Alan Stern | 088f7fe | 2010-01-08 12:56:54 -0500 | [diff] [blame] | 459 | else |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 460 | rc = -EINVAL; |
| 461 | |
| 462 | usb_unlock_device(udev); |
Alan Stern | 9e18c82 | 2010-04-02 13:22:09 -0400 | [diff] [blame] | 463 | return rc; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 464 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 465 | static DEVICE_ATTR_RW(level); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 466 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 467 | static ssize_t usb2_hardware_lpm_show(struct device *dev, |
| 468 | struct device_attribute *attr, char *buf) |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 469 | { |
| 470 | struct usb_device *udev = to_usb_device(dev); |
| 471 | const char *p; |
| 472 | |
Sarah Sharp | de68bab | 2013-09-30 17:26:28 +0300 | [diff] [blame] | 473 | if (udev->usb2_hw_lpm_allowed == 1) |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 474 | p = "enabled"; |
| 475 | else |
| 476 | p = "disabled"; |
| 477 | |
| 478 | return sprintf(buf, "%s\n", p); |
| 479 | } |
| 480 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 481 | static ssize_t usb2_hardware_lpm_store(struct device *dev, |
| 482 | struct device_attribute *attr, |
| 483 | const char *buf, size_t count) |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 484 | { |
| 485 | struct usb_device *udev = to_usb_device(dev); |
| 486 | bool value; |
| 487 | int ret; |
| 488 | |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 489 | ret = usb_lock_device_interruptible(udev); |
| 490 | if (ret < 0) |
| 491 | return -EINTR; |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 492 | |
| 493 | ret = strtobool(buf, &value); |
| 494 | |
Sarah Sharp | de68bab | 2013-09-30 17:26:28 +0300 | [diff] [blame] | 495 | if (!ret) { |
| 496 | udev->usb2_hw_lpm_allowed = value; |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 497 | ret = usb_set_usb2_hardware_lpm(udev, value); |
Sarah Sharp | de68bab | 2013-09-30 17:26:28 +0300 | [diff] [blame] | 498 | } |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 499 | |
| 500 | usb_unlock_device(udev); |
| 501 | |
| 502 | if (!ret) |
| 503 | return count; |
| 504 | |
| 505 | return ret; |
| 506 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 507 | static DEVICE_ATTR_RW(usb2_hardware_lpm); |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 508 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 509 | static ssize_t usb2_lpm_l1_timeout_show(struct device *dev, |
| 510 | struct device_attribute *attr, |
| 511 | char *buf) |
Mathias Nyman | 17f3486 | 2013-05-23 17:14:31 +0300 | [diff] [blame] | 512 | { |
| 513 | struct usb_device *udev = to_usb_device(dev); |
| 514 | return sprintf(buf, "%d\n", udev->l1_params.timeout); |
| 515 | } |
| 516 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 517 | static ssize_t usb2_lpm_l1_timeout_store(struct device *dev, |
| 518 | struct device_attribute *attr, |
| 519 | const char *buf, size_t count) |
Mathias Nyman | 17f3486 | 2013-05-23 17:14:31 +0300 | [diff] [blame] | 520 | { |
| 521 | struct usb_device *udev = to_usb_device(dev); |
| 522 | u16 timeout; |
| 523 | |
| 524 | if (kstrtou16(buf, 0, &timeout)) |
| 525 | return -EINVAL; |
| 526 | |
| 527 | udev->l1_params.timeout = timeout; |
| 528 | |
| 529 | return count; |
| 530 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 531 | static DEVICE_ATTR_RW(usb2_lpm_l1_timeout); |
Mathias Nyman | 17f3486 | 2013-05-23 17:14:31 +0300 | [diff] [blame] | 532 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 533 | static ssize_t usb2_lpm_besl_show(struct device *dev, |
| 534 | struct device_attribute *attr, char *buf) |
Mathias Nyman | 17f3486 | 2013-05-23 17:14:31 +0300 | [diff] [blame] | 535 | { |
| 536 | struct usb_device *udev = to_usb_device(dev); |
| 537 | return sprintf(buf, "%d\n", udev->l1_params.besl); |
| 538 | } |
| 539 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 540 | static ssize_t usb2_lpm_besl_store(struct device *dev, |
| 541 | struct device_attribute *attr, |
| 542 | const char *buf, size_t count) |
Mathias Nyman | 17f3486 | 2013-05-23 17:14:31 +0300 | [diff] [blame] | 543 | { |
| 544 | struct usb_device *udev = to_usb_device(dev); |
| 545 | u8 besl; |
| 546 | |
| 547 | if (kstrtou8(buf, 0, &besl) || besl > 15) |
| 548 | return -EINVAL; |
| 549 | |
| 550 | udev->l1_params.besl = besl; |
| 551 | |
| 552 | return count; |
| 553 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 554 | static DEVICE_ATTR_RW(usb2_lpm_besl); |
Mathias Nyman | 17f3486 | 2013-05-23 17:14:31 +0300 | [diff] [blame] | 555 | |
Lu Baolu | bf5ce5b | 2015-11-14 16:26:32 +0800 | [diff] [blame] | 556 | static ssize_t usb3_hardware_lpm_u1_show(struct device *dev, |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 557 | struct device_attribute *attr, char *buf) |
| 558 | { |
| 559 | struct usb_device *udev = to_usb_device(dev); |
| 560 | const char *p; |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 561 | int rc; |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 562 | |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 563 | rc = usb_lock_device_interruptible(udev); |
| 564 | if (rc < 0) |
| 565 | return -EINTR; |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 566 | |
Lu Baolu | bf5ce5b | 2015-11-14 16:26:32 +0800 | [diff] [blame] | 567 | if (udev->usb3_lpm_u1_enabled) |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 568 | p = "enabled"; |
| 569 | else |
| 570 | p = "disabled"; |
| 571 | |
| 572 | usb_unlock_device(udev); |
| 573 | |
| 574 | return sprintf(buf, "%s\n", p); |
| 575 | } |
Lu Baolu | bf5ce5b | 2015-11-14 16:26:32 +0800 | [diff] [blame] | 576 | static DEVICE_ATTR_RO(usb3_hardware_lpm_u1); |
| 577 | |
| 578 | static ssize_t usb3_hardware_lpm_u2_show(struct device *dev, |
| 579 | struct device_attribute *attr, char *buf) |
| 580 | { |
| 581 | struct usb_device *udev = to_usb_device(dev); |
| 582 | const char *p; |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 583 | int rc; |
Lu Baolu | bf5ce5b | 2015-11-14 16:26:32 +0800 | [diff] [blame] | 584 | |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 585 | rc = usb_lock_device_interruptible(udev); |
| 586 | if (rc < 0) |
| 587 | return -EINTR; |
Lu Baolu | bf5ce5b | 2015-11-14 16:26:32 +0800 | [diff] [blame] | 588 | |
| 589 | if (udev->usb3_lpm_u2_enabled) |
| 590 | p = "enabled"; |
| 591 | else |
| 592 | p = "disabled"; |
| 593 | |
| 594 | usb_unlock_device(udev); |
| 595 | |
| 596 | return sprintf(buf, "%s\n", p); |
| 597 | } |
| 598 | static DEVICE_ATTR_RO(usb3_hardware_lpm_u2); |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 599 | |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 600 | static struct attribute *usb2_hardware_lpm_attr[] = { |
| 601 | &dev_attr_usb2_hardware_lpm.attr, |
Mathias Nyman | 17f3486 | 2013-05-23 17:14:31 +0300 | [diff] [blame] | 602 | &dev_attr_usb2_lpm_l1_timeout.attr, |
| 603 | &dev_attr_usb2_lpm_besl.attr, |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 604 | NULL, |
| 605 | }; |
| 606 | static struct attribute_group usb2_hardware_lpm_attr_group = { |
| 607 | .name = power_group_name, |
| 608 | .attrs = usb2_hardware_lpm_attr, |
| 609 | }; |
| 610 | |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 611 | static struct attribute *usb3_hardware_lpm_attr[] = { |
Lu Baolu | bf5ce5b | 2015-11-14 16:26:32 +0800 | [diff] [blame] | 612 | &dev_attr_usb3_hardware_lpm_u1.attr, |
| 613 | &dev_attr_usb3_hardware_lpm_u2.attr, |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 614 | NULL, |
| 615 | }; |
| 616 | static struct attribute_group usb3_hardware_lpm_attr_group = { |
| 617 | .name = power_group_name, |
| 618 | .attrs = usb3_hardware_lpm_attr, |
| 619 | }; |
| 620 | |
Alan Stern | 045cac6 | 2010-11-15 15:57:07 -0500 | [diff] [blame] | 621 | static struct attribute *power_attrs[] = { |
| 622 | &dev_attr_autosuspend.attr, |
| 623 | &dev_attr_level.attr, |
| 624 | &dev_attr_connected_duration.attr, |
| 625 | &dev_attr_active_duration.attr, |
| 626 | NULL, |
| 627 | }; |
| 628 | static struct attribute_group power_attr_group = { |
| 629 | .name = power_group_name, |
| 630 | .attrs = power_attrs, |
| 631 | }; |
| 632 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 633 | static int add_power_attributes(struct device *dev) |
| 634 | { |
| 635 | int rc = 0; |
| 636 | |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 637 | if (is_usb_device(dev)) { |
| 638 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | 045cac6 | 2010-11-15 15:57:07 -0500 | [diff] [blame] | 639 | rc = sysfs_merge_group(&dev->kobj, &power_attr_group); |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 640 | if (udev->usb2_hw_lpm_capable == 1) |
| 641 | rc = sysfs_merge_group(&dev->kobj, |
| 642 | &usb2_hardware_lpm_attr_group); |
Lu Baolu | bf5ce5b | 2015-11-14 16:26:32 +0800 | [diff] [blame] | 643 | if (udev->speed == USB_SPEED_SUPER && |
| 644 | udev->lpm_capable == 1) |
Kevin Strasser | 655fe4e | 2015-06-16 10:35:30 -0700 | [diff] [blame] | 645 | rc = sysfs_merge_group(&dev->kobj, |
| 646 | &usb3_hardware_lpm_attr_group); |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 647 | } |
| 648 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 649 | return rc; |
| 650 | } |
| 651 | |
| 652 | static void remove_power_attributes(struct device *dev) |
| 653 | { |
Andiry Xu | c1045e8 | 2011-09-23 14:19:53 -0700 | [diff] [blame] | 654 | sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group); |
Alan Stern | 045cac6 | 2010-11-15 15:57:07 -0500 | [diff] [blame] | 655 | sysfs_unmerge_group(&dev->kobj, &power_attr_group); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | #else |
| 659 | |
Rafael J. Wysocki | ceb6c9c | 2014-11-29 23:47:05 +0100 | [diff] [blame] | 660 | #define add_persist_attributes(dev) 0 |
| 661 | #define remove_persist_attributes(dev) do {} while (0) |
| 662 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 663 | #define add_power_attributes(dev) 0 |
| 664 | #define remove_power_attributes(dev) do {} while (0) |
| 665 | |
Rafael J. Wysocki | ceb6c9c | 2014-11-29 23:47:05 +0100 | [diff] [blame] | 666 | #endif /* CONFIG_PM */ |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 667 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | /* Descriptor fields */ |
| 670 | #define usb_descriptor_attr_le16(field, format_string) \ |
| 671 | static ssize_t \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 672 | field##_show(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 673 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | { \ |
| 675 | struct usb_device *udev; \ |
| 676 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 677 | udev = to_usb_device(dev); \ |
| 678 | return sprintf(buf, format_string, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | le16_to_cpu(udev->descriptor.field)); \ |
| 680 | } \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 681 | static DEVICE_ATTR_RO(field) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 683 | usb_descriptor_attr_le16(idVendor, "%04x\n"); |
| 684 | usb_descriptor_attr_le16(idProduct, "%04x\n"); |
| 685 | usb_descriptor_attr_le16(bcdDevice, "%04x\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | |
| 687 | #define usb_descriptor_attr(field, format_string) \ |
| 688 | static ssize_t \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 689 | field##_show(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 690 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { \ |
| 692 | struct usb_device *udev; \ |
| 693 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 694 | udev = to_usb_device(dev); \ |
| 695 | return sprintf(buf, format_string, udev->descriptor.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | } \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 697 | static DEVICE_ATTR_RO(field) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 699 | usb_descriptor_attr(bDeviceClass, "%02x\n"); |
| 700 | usb_descriptor_attr(bDeviceSubClass, "%02x\n"); |
| 701 | usb_descriptor_attr(bDeviceProtocol, "%02x\n"); |
| 702 | usb_descriptor_attr(bNumConfigurations, "%d\n"); |
| 703 | usb_descriptor_attr(bMaxPacketSize0, "%d\n"); |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 704 | |
| 705 | |
| 706 | /* show if the device is authorized (1) or not (0) */ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 707 | static ssize_t authorized_show(struct device *dev, |
| 708 | struct device_attribute *attr, char *buf) |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 709 | { |
| 710 | struct usb_device *usb_dev = to_usb_device(dev); |
| 711 | return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized); |
| 712 | } |
| 713 | |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 714 | /* |
| 715 | * Authorize a device to be used in the system |
| 716 | * |
| 717 | * Writing a 0 deauthorizes the device, writing a 1 authorizes it. |
| 718 | */ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 719 | static ssize_t authorized_store(struct device *dev, |
| 720 | struct device_attribute *attr, const char *buf, |
| 721 | size_t size) |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 722 | { |
| 723 | ssize_t result; |
| 724 | struct usb_device *usb_dev = to_usb_device(dev); |
| 725 | unsigned val; |
| 726 | result = sscanf(buf, "%u\n", &val); |
| 727 | if (result != 1) |
| 728 | result = -EINVAL; |
| 729 | else if (val == 0) |
| 730 | result = usb_deauthorize_device(usb_dev); |
| 731 | else |
| 732 | result = usb_authorize_device(usb_dev); |
Matthias Beyer | 469271f | 2013-10-10 23:41:27 +0200 | [diff] [blame] | 733 | return result < 0 ? result : size; |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 734 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 735 | static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR, |
| 736 | authorized_show, authorized_store); |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 737 | |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 738 | /* "Safely remove a device" */ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 739 | static ssize_t remove_store(struct device *dev, struct device_attribute *attr, |
| 740 | const char *buf, size_t count) |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 741 | { |
| 742 | struct usb_device *udev = to_usb_device(dev); |
| 743 | int rc = 0; |
| 744 | |
| 745 | usb_lock_device(udev); |
| 746 | if (udev->state != USB_STATE_NOTATTACHED) { |
| 747 | |
| 748 | /* To avoid races, first unconfigure and then remove */ |
| 749 | usb_set_configuration(udev, -1); |
| 750 | rc = usb_remove_device(udev); |
| 751 | } |
| 752 | if (rc == 0) |
| 753 | rc = count; |
| 754 | usb_unlock_device(udev); |
| 755 | return rc; |
| 756 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 757 | static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store); |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 758 | |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | static struct attribute *dev_attrs[] = { |
| 761 | /* current configuration's attributes */ |
Alan Stern | b6eb2d8 | 2006-07-06 15:37:42 -0400 | [diff] [blame] | 762 | &dev_attr_configuration.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | &dev_attr_bNumInterfaces.attr, |
| 764 | &dev_attr_bConfigurationValue.attr, |
| 765 | &dev_attr_bmAttributes.attr, |
| 766 | &dev_attr_bMaxPower.attr, |
| 767 | /* device attributes */ |
Greg Kroah-Hartman | 9af2362 | 2009-11-30 11:15:02 -0800 | [diff] [blame] | 768 | &dev_attr_urbnum.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | &dev_attr_idVendor.attr, |
| 770 | &dev_attr_idProduct.attr, |
| 771 | &dev_attr_bcdDevice.attr, |
| 772 | &dev_attr_bDeviceClass.attr, |
| 773 | &dev_attr_bDeviceSubClass.attr, |
| 774 | &dev_attr_bDeviceProtocol.attr, |
| 775 | &dev_attr_bNumConfigurations.attr, |
Greg Kroah-Hartman | cf5910b | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 776 | &dev_attr_bMaxPacketSize0.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | &dev_attr_speed.attr, |
Alan Stern | 83f7d95 | 2007-04-25 15:15:43 -0400 | [diff] [blame] | 778 | &dev_attr_busnum.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | &dev_attr_devnum.attr, |
Greg Kroah-Hartman | 9af2362 | 2009-11-30 11:15:02 -0800 | [diff] [blame] | 780 | &dev_attr_devpath.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | &dev_attr_version.attr, |
| 782 | &dev_attr_maxchild.attr, |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 783 | &dev_attr_quirks.attr, |
Oliver Neukum | ef95534 | 2010-01-16 01:33:03 +0100 | [diff] [blame] | 784 | &dev_attr_avoid_reset_quirk.attr, |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 785 | &dev_attr_authorized.attr, |
Alan Stern | 253e057 | 2009-10-27 15:20:13 -0400 | [diff] [blame] | 786 | &dev_attr_remove.attr, |
Matthew Garrett | 0846e7e | 2012-02-03 17:11:54 -0500 | [diff] [blame] | 787 | &dev_attr_removable.attr, |
Sarah Sharp | 024f117 | 2012-07-05 17:17:24 -0700 | [diff] [blame] | 788 | &dev_attr_ltm_capable.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 789 | NULL, |
| 790 | }; |
| 791 | static struct attribute_group dev_attr_grp = { |
| 792 | .attrs = dev_attrs, |
| 793 | }; |
| 794 | |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 795 | /* When modifying this list, be sure to modify dev_string_attrs_are_visible() |
| 796 | * accordingly. |
| 797 | */ |
| 798 | static struct attribute *dev_string_attrs[] = { |
| 799 | &dev_attr_manufacturer.attr, |
| 800 | &dev_attr_product.attr, |
| 801 | &dev_attr_serial.attr, |
| 802 | NULL |
| 803 | }; |
| 804 | |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 805 | static umode_t dev_string_attrs_are_visible(struct kobject *kobj, |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 806 | struct attribute *a, int n) |
| 807 | { |
H Hartley Sweeten | a864e3aa5 | 2009-04-15 21:34:40 -0400 | [diff] [blame] | 808 | struct device *dev = container_of(kobj, struct device, kobj); |
| 809 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 810 | |
| 811 | if (a == &dev_attr_manufacturer.attr) { |
| 812 | if (udev->manufacturer == NULL) |
| 813 | return 0; |
| 814 | } else if (a == &dev_attr_product.attr) { |
| 815 | if (udev->product == NULL) |
| 816 | return 0; |
| 817 | } else if (a == &dev_attr_serial.attr) { |
| 818 | if (udev->serial == NULL) |
| 819 | return 0; |
| 820 | } |
| 821 | return a->mode; |
| 822 | } |
| 823 | |
| 824 | static struct attribute_group dev_string_attr_grp = { |
| 825 | .attrs = dev_string_attrs, |
| 826 | .is_visible = dev_string_attrs_are_visible, |
| 827 | }; |
| 828 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 829 | const struct attribute_group *usb_device_groups[] = { |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 830 | &dev_attr_grp, |
| 831 | &dev_string_attr_grp, |
| 832 | NULL |
| 833 | }; |
| 834 | |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 835 | /* Binary descriptors */ |
| 836 | |
| 837 | static ssize_t |
Chris Wright | 2c3c8be | 2010-05-12 18:28:57 -0700 | [diff] [blame] | 838 | read_descriptors(struct file *filp, struct kobject *kobj, |
| 839 | struct bin_attribute *attr, |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 840 | char *buf, loff_t off, size_t count) |
| 841 | { |
H Hartley Sweeten | a864e3aa5 | 2009-04-15 21:34:40 -0400 | [diff] [blame] | 842 | struct device *dev = container_of(kobj, struct device, kobj); |
| 843 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 844 | size_t nleft = count; |
| 845 | size_t srclen, n; |
Oliver Neukum | b4a90d0 | 2016-01-21 15:18:48 +0100 | [diff] [blame] | 846 | int cfgno; |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 847 | void *src; |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 848 | |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 849 | /* The binary attribute begins with the device descriptor. |
| 850 | * Following that are the raw descriptor entries for all the |
| 851 | * configurations (config plus subsidiary descriptors). |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 852 | */ |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 853 | for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations && |
| 854 | nleft > 0; ++cfgno) { |
| 855 | if (cfgno < 0) { |
| 856 | src = &udev->descriptor; |
| 857 | srclen = sizeof(struct usb_device_descriptor); |
| 858 | } else { |
| 859 | src = udev->rawdescriptors[cfgno]; |
| 860 | srclen = __le16_to_cpu(udev->config[cfgno].desc. |
| 861 | wTotalLength); |
| 862 | } |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 863 | if (off < srclen) { |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 864 | n = min(nleft, srclen - (size_t) off); |
| 865 | memcpy(buf, src + off, n); |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 866 | nleft -= n; |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 867 | buf += n; |
| 868 | off = 0; |
| 869 | } else { |
| 870 | off -= srclen; |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 871 | } |
| 872 | } |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 873 | return count - nleft; |
| 874 | } |
| 875 | |
| 876 | static struct bin_attribute dev_bin_attr_descriptors = { |
| 877 | .attr = {.name = "descriptors", .mode = 0444}, |
| 878 | .read = read_descriptors, |
| 879 | .size = 18 + 65535, /* dev descr + max-size raw descriptor */ |
| 880 | }; |
| 881 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 882 | int usb_create_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | { |
| 884 | struct device *dev = &udev->dev; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 885 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 887 | retval = device_create_bin_file(dev, &dev_bin_attr_descriptors); |
| 888 | if (retval) |
| 889 | goto error; |
| 890 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 891 | retval = add_persist_attributes(dev); |
| 892 | if (retval) |
| 893 | goto error; |
| 894 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 895 | retval = add_power_attributes(dev); |
| 896 | if (retval) |
| 897 | goto error; |
Alan Stern | 3b23dd6 | 2008-12-05 14:10:34 -0500 | [diff] [blame] | 898 | return retval; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 899 | error: |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 900 | usb_remove_sysfs_dev_files(udev); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 901 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 904 | void usb_remove_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | { |
| 906 | struct device *dev = &udev->dev; |
| 907 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 908 | remove_power_attributes(dev); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 909 | remove_persist_attributes(dev); |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 910 | device_remove_bin_file(dev, &dev_bin_attr_descriptors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Rahul Bedarkar | 025d443 | 2014-01-04 11:24:41 +0530 | [diff] [blame] | 913 | /* Interface Association Descriptor fields */ |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 914 | #define usb_intf_assoc_attr(field, format_string) \ |
| 915 | static ssize_t \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 916 | iad_##field##_show(struct device *dev, struct device_attribute *attr, \ |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 917 | char *buf) \ |
| 918 | { \ |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 919 | struct usb_interface *intf = to_usb_interface(dev); \ |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 920 | \ |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 921 | return sprintf(buf, format_string, \ |
| 922 | intf->intf_assoc->field); \ |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 923 | } \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 924 | static DEVICE_ATTR_RO(iad_##field) |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 925 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 926 | usb_intf_assoc_attr(bFirstInterface, "%02x\n"); |
| 927 | usb_intf_assoc_attr(bInterfaceCount, "%02d\n"); |
| 928 | usb_intf_assoc_attr(bFunctionClass, "%02x\n"); |
| 929 | usb_intf_assoc_attr(bFunctionSubClass, "%02x\n"); |
| 930 | usb_intf_assoc_attr(bFunctionProtocol, "%02x\n"); |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | /* Interface fields */ |
| 933 | #define usb_intf_attr(field, format_string) \ |
| 934 | static ssize_t \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 935 | field##_show(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 936 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | { \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 938 | struct usb_interface *intf = to_usb_interface(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 940 | return sprintf(buf, format_string, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 941 | intf->cur_altsetting->desc.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | } \ |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 943 | static DEVICE_ATTR_RO(field) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 945 | usb_intf_attr(bInterfaceNumber, "%02x\n"); |
| 946 | usb_intf_attr(bAlternateSetting, "%2d\n"); |
| 947 | usb_intf_attr(bNumEndpoints, "%02x\n"); |
| 948 | usb_intf_attr(bInterfaceClass, "%02x\n"); |
| 949 | usb_intf_attr(bInterfaceSubClass, "%02x\n"); |
| 950 | usb_intf_attr(bInterfaceProtocol, "%02x\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 952 | static ssize_t interface_show(struct device *dev, struct device_attribute *attr, |
| 953 | char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | { |
| 955 | struct usb_interface *intf; |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 956 | char *string; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 958 | intf = to_usb_interface(dev); |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 959 | string = ACCESS_ONCE(intf->cur_altsetting->string); |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 960 | if (!string) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | return 0; |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 962 | return sprintf(buf, "%s\n", string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 964 | static DEVICE_ATTR_RO(interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 966 | static ssize_t modalias_show(struct device *dev, struct device_attribute *attr, |
| 967 | char *buf) |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 968 | { |
| 969 | struct usb_interface *intf; |
| 970 | struct usb_device *udev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 971 | struct usb_host_interface *alt; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 972 | |
| 973 | intf = to_usb_interface(dev); |
| 974 | udev = interface_to_usbdev(intf); |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 975 | alt = ACCESS_ONCE(intf->cur_altsetting); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 976 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 977 | return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X" |
Bjørn Mork | 81df2d5 | 2012-05-18 21:27:43 +0200 | [diff] [blame] | 978 | "ic%02Xisc%02Xip%02Xin%02X\n", |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 979 | le16_to_cpu(udev->descriptor.idVendor), |
| 980 | le16_to_cpu(udev->descriptor.idProduct), |
| 981 | le16_to_cpu(udev->descriptor.bcdDevice), |
| 982 | udev->descriptor.bDeviceClass, |
| 983 | udev->descriptor.bDeviceSubClass, |
| 984 | udev->descriptor.bDeviceProtocol, |
| 985 | alt->desc.bInterfaceClass, |
| 986 | alt->desc.bInterfaceSubClass, |
Bjørn Mork | 81df2d5 | 2012-05-18 21:27:43 +0200 | [diff] [blame] | 987 | alt->desc.bInterfaceProtocol, |
| 988 | alt->desc.bInterfaceNumber); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 989 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 990 | static DEVICE_ATTR_RO(modalias); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 991 | |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 992 | static ssize_t supports_autosuspend_show(struct device *dev, |
| 993 | struct device_attribute *attr, |
| 994 | char *buf) |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame] | 995 | { |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 996 | int s; |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame] | 997 | |
Oliver Neukum | 7dd9cba | 2016-01-21 15:18:47 +0100 | [diff] [blame] | 998 | s = device_lock_interruptible(dev); |
| 999 | if (s < 0) |
| 1000 | return -EINTR; |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame] | 1001 | /* Devices will be autosuspended even when an interface isn't claimed */ |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 1002 | s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend); |
| 1003 | device_unlock(dev); |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame] | 1004 | |
Alan Stern | 232275a | 2013-09-24 15:43:39 -0400 | [diff] [blame] | 1005 | return sprintf(buf, "%u\n", s); |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame] | 1006 | } |
Greg Kroah-Hartman | d03f254 | 2013-08-23 16:05:26 -0700 | [diff] [blame] | 1007 | static DEVICE_ATTR_RO(supports_autosuspend); |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame] | 1008 | |
Stefan Koch | 310d2b4 | 2015-08-25 21:10:09 +0200 | [diff] [blame] | 1009 | /* |
| 1010 | * interface_authorized_show - show authorization status of an USB interface |
| 1011 | * 1 is authorized, 0 is deauthorized |
| 1012 | */ |
| 1013 | static ssize_t interface_authorized_show(struct device *dev, |
| 1014 | struct device_attribute *attr, char *buf) |
| 1015 | { |
| 1016 | struct usb_interface *intf = to_usb_interface(dev); |
| 1017 | |
| 1018 | return sprintf(buf, "%u\n", intf->authorized); |
| 1019 | } |
| 1020 | |
| 1021 | /* |
| 1022 | * interface_authorized_store - authorize or deauthorize an USB interface |
| 1023 | */ |
| 1024 | static ssize_t interface_authorized_store(struct device *dev, |
| 1025 | struct device_attribute *attr, const char *buf, size_t count) |
| 1026 | { |
| 1027 | struct usb_interface *intf = to_usb_interface(dev); |
| 1028 | bool val; |
| 1029 | |
| 1030 | if (strtobool(buf, &val) != 0) |
| 1031 | return -EINVAL; |
| 1032 | |
| 1033 | if (val) |
| 1034 | usb_authorize_interface(intf); |
| 1035 | else |
| 1036 | usb_deauthorize_interface(intf); |
| 1037 | |
| 1038 | return count; |
| 1039 | } |
| 1040 | static struct device_attribute dev_attr_interface_authorized = |
| 1041 | __ATTR(authorized, S_IRUGO | S_IWUSR, |
| 1042 | interface_authorized_show, interface_authorized_store); |
| 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | static struct attribute *intf_attrs[] = { |
| 1045 | &dev_attr_bInterfaceNumber.attr, |
| 1046 | &dev_attr_bAlternateSetting.attr, |
| 1047 | &dev_attr_bNumEndpoints.attr, |
| 1048 | &dev_attr_bInterfaceClass.attr, |
| 1049 | &dev_attr_bInterfaceSubClass.attr, |
| 1050 | &dev_attr_bInterfaceProtocol.attr, |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 1051 | &dev_attr_modalias.attr, |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame] | 1052 | &dev_attr_supports_autosuspend.attr, |
Stefan Koch | 310d2b4 | 2015-08-25 21:10:09 +0200 | [diff] [blame] | 1053 | &dev_attr_interface_authorized.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | NULL, |
| 1055 | }; |
| 1056 | static struct attribute_group intf_attr_grp = { |
| 1057 | .attrs = intf_attrs, |
| 1058 | }; |
| 1059 | |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 1060 | static struct attribute *intf_assoc_attrs[] = { |
| 1061 | &dev_attr_iad_bFirstInterface.attr, |
| 1062 | &dev_attr_iad_bInterfaceCount.attr, |
| 1063 | &dev_attr_iad_bFunctionClass.attr, |
| 1064 | &dev_attr_iad_bFunctionSubClass.attr, |
| 1065 | &dev_attr_iad_bFunctionProtocol.attr, |
| 1066 | NULL, |
| 1067 | }; |
| 1068 | |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 1069 | static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj, |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 1070 | struct attribute *a, int n) |
| 1071 | { |
H Hartley Sweeten | a864e3aa5 | 2009-04-15 21:34:40 -0400 | [diff] [blame] | 1072 | struct device *dev = container_of(kobj, struct device, kobj); |
| 1073 | struct usb_interface *intf = to_usb_interface(dev); |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 1074 | |
| 1075 | if (intf->intf_assoc == NULL) |
| 1076 | return 0; |
| 1077 | return a->mode; |
| 1078 | } |
| 1079 | |
| 1080 | static struct attribute_group intf_assoc_attr_grp = { |
| 1081 | .attrs = intf_assoc_attrs, |
| 1082 | .is_visible = intf_assoc_attrs_are_visible, |
| 1083 | }; |
| 1084 | |
David Brownell | a4dbd67 | 2009-06-24 10:06:31 -0700 | [diff] [blame] | 1085 | const struct attribute_group *usb_interface_groups[] = { |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 1086 | &intf_attr_grp, |
| 1087 | &intf_assoc_attr_grp, |
| 1088 | NULL |
| 1089 | }; |
| 1090 | |
Michal Nazarewicz | 643de62 | 2011-04-14 17:47:09 +0200 | [diff] [blame] | 1091 | void usb_create_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | { |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 1093 | struct usb_device *udev = interface_to_usbdev(intf); |
| 1094 | struct usb_host_interface *alt = intf->cur_altsetting; |
| 1095 | |
Alan Stern | 352d026 | 2008-10-29 15:16:58 -0400 | [diff] [blame] | 1096 | if (intf->sysfs_files_created || intf->unregistering) |
Michal Nazarewicz | 643de62 | 2011-04-14 17:47:09 +0200 | [diff] [blame] | 1097 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | |
Michal Nazarewicz | 643de62 | 2011-04-14 17:47:09 +0200 | [diff] [blame] | 1099 | if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS)) |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 1100 | alt->string = usb_cache_string(udev, alt->desc.iInterface); |
Michal Nazarewicz | 643de62 | 2011-04-14 17:47:09 +0200 | [diff] [blame] | 1101 | if (alt->string && device_create_file(&intf->dev, &dev_attr_interface)) |
| 1102 | ; /* We don't actually care if the function fails. */ |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 1103 | intf->sysfs_files_created = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 1106 | void usb_remove_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | { |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 1108 | if (!intf->sysfs_files_created) |
| 1109 | return; |
Alan Stern | 92b0da1 | 2008-10-29 15:18:50 -0400 | [diff] [blame] | 1110 | |
Alan Stern | 92b0da1 | 2008-10-29 15:18:50 -0400 | [diff] [blame] | 1111 | device_remove_file(&intf->dev, &dev_attr_interface); |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 1112 | intf->sysfs_files_created = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |