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