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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include "usb.h" |
| 17 | |
| 18 | /* Active configuration fields */ |
| 19 | #define usb_actconfig_show(field, multiplier, format_string) \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 20 | static ssize_t show_##field(struct device *dev, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 21 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | { \ |
| 23 | struct usb_device *udev; \ |
| 24 | struct usb_host_config *actconfig; \ |
| 25 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 26 | udev = to_usb_device(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | actconfig = udev->actconfig; \ |
| 28 | if (actconfig) \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 29 | return sprintf(buf, format_string, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | actconfig->desc.field * multiplier); \ |
| 31 | else \ |
| 32 | return 0; \ |
| 33 | } \ |
| 34 | |
| 35 | #define usb_actconfig_attr(field, multiplier, format_string) \ |
| 36 | usb_actconfig_show(field, multiplier, format_string) \ |
| 37 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 38 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 39 | usb_actconfig_attr(bNumInterfaces, 1, "%2d\n") |
| 40 | usb_actconfig_attr(bmAttributes, 1, "%2x\n") |
| 41 | usb_actconfig_attr(bMaxPower, 2, "%3dmA\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 43 | static ssize_t show_configuration_string(struct device *dev, |
| 44 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | { |
| 46 | struct usb_device *udev; |
| 47 | struct usb_host_config *actconfig; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 49 | udev = to_usb_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | actconfig = udev->actconfig; |
| 51 | if ((!actconfig) || (!actconfig->string)) |
| 52 | return 0; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 53 | return sprintf(buf, "%s\n", actconfig->string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | } |
| 55 | static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL); |
| 56 | |
| 57 | /* configuration value is always present, and r/w */ |
| 58 | usb_actconfig_show(bConfigurationValue, 1, "%u\n"); |
| 59 | |
| 60 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 61 | set_bConfigurationValue(struct device *dev, struct device_attribute *attr, |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 62 | const char *buf, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 64 | struct usb_device *udev = to_usb_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | int config, value; |
| 66 | |
Alan Stern | 3f141e2 | 2007-02-08 16:40:43 -0500 | [diff] [blame] | 67 | if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | return -EINVAL; |
| 69 | usb_lock_device(udev); |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 70 | value = usb_set_configuration(udev, config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | usb_unlock_device(udev); |
| 72 | return (value < 0) ? value : count; |
| 73 | } |
| 74 | |
| 75 | static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR, |
| 76 | show_bConfigurationValue, set_bConfigurationValue); |
| 77 | |
| 78 | /* String fields */ |
| 79 | #define usb_string_attr(name) \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 80 | static ssize_t show_##name(struct device *dev, \ |
| 81 | struct device_attribute *attr, char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { \ |
| 83 | struct usb_device *udev; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 85 | udev = to_usb_device(dev); \ |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 86 | return sprintf(buf, "%s\n", udev->name); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } \ |
| 88 | static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL); |
| 89 | |
| 90 | usb_string_attr(product); |
| 91 | usb_string_attr(manufacturer); |
| 92 | usb_string_attr(serial); |
| 93 | |
| 94 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 95 | show_speed(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { |
| 97 | struct usb_device *udev; |
| 98 | char *speed; |
| 99 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 100 | udev = to_usb_device(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | switch (udev->speed) { |
| 103 | case USB_SPEED_LOW: |
| 104 | speed = "1.5"; |
| 105 | break; |
| 106 | case USB_SPEED_UNKNOWN: |
| 107 | case USB_SPEED_FULL: |
| 108 | speed = "12"; |
| 109 | break; |
| 110 | case USB_SPEED_HIGH: |
| 111 | speed = "480"; |
| 112 | break; |
| 113 | default: |
| 114 | speed = "unknown"; |
| 115 | } |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 116 | return sprintf(buf, "%s\n", speed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL); |
| 119 | |
| 120 | static ssize_t |
Alan Stern | 83f7d95 | 2007-04-25 15:15:43 -0400 | [diff] [blame] | 121 | show_busnum(struct device *dev, struct device_attribute *attr, char *buf) |
| 122 | { |
| 123 | struct usb_device *udev; |
| 124 | |
| 125 | udev = to_usb_device(dev); |
| 126 | return sprintf(buf, "%d\n", udev->bus->busnum); |
| 127 | } |
| 128 | static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL); |
| 129 | |
| 130 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 131 | show_devnum(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
| 133 | struct usb_device *udev; |
| 134 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 135 | udev = to_usb_device(dev); |
| 136 | return sprintf(buf, "%d\n", udev->devnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | } |
| 138 | static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL); |
| 139 | |
| 140 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 141 | show_version(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
| 143 | struct usb_device *udev; |
| 144 | u16 bcdUSB; |
| 145 | |
| 146 | udev = to_usb_device(dev); |
| 147 | bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB); |
| 148 | return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff); |
| 149 | } |
| 150 | static DEVICE_ATTR(version, S_IRUGO, show_version, NULL); |
| 151 | |
| 152 | static ssize_t |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 153 | show_maxchild(struct device *dev, struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
| 155 | struct usb_device *udev; |
| 156 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 157 | udev = to_usb_device(dev); |
| 158 | return sprintf(buf, "%d\n", udev->maxchild); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
| 160 | static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL); |
| 161 | |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 162 | static ssize_t |
| 163 | show_quirks(struct device *dev, struct device_attribute *attr, char *buf) |
| 164 | { |
| 165 | struct usb_device *udev; |
| 166 | |
| 167 | udev = to_usb_device(dev); |
| 168 | return sprintf(buf, "0x%x\n", udev->quirks); |
| 169 | } |
| 170 | static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL); |
| 171 | |
Sarah Sharp | 4d59d8a | 2007-10-03 14:56:03 -0700 | [diff] [blame] | 172 | static ssize_t |
| 173 | show_urbnum(struct device *dev, struct device_attribute *attr, char *buf) |
| 174 | { |
| 175 | struct usb_device *udev; |
| 176 | |
| 177 | udev = to_usb_device(dev); |
| 178 | return sprintf(buf, "%d\n", atomic_read(&udev->urbnum)); |
| 179 | } |
| 180 | static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL); |
| 181 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 182 | |
| 183 | #if defined(CONFIG_USB_PERSIST) || defined(CONFIG_USB_SUSPEND) |
| 184 | static const char power_group[] = "power"; |
| 185 | #endif |
| 186 | |
| 187 | #ifdef CONFIG_USB_PERSIST |
| 188 | |
| 189 | static ssize_t |
| 190 | show_persist(struct device *dev, struct device_attribute *attr, char *buf) |
| 191 | { |
| 192 | struct usb_device *udev = to_usb_device(dev); |
| 193 | |
| 194 | return sprintf(buf, "%d\n", udev->persist_enabled); |
| 195 | } |
| 196 | |
| 197 | static ssize_t |
| 198 | set_persist(struct device *dev, struct device_attribute *attr, |
| 199 | const char *buf, size_t count) |
| 200 | { |
| 201 | struct usb_device *udev = to_usb_device(dev); |
| 202 | int value; |
| 203 | |
| 204 | /* Hubs are always enabled for USB_PERSIST */ |
| 205 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) |
| 206 | return -EPERM; |
| 207 | |
| 208 | if (sscanf(buf, "%d", &value) != 1) |
| 209 | return -EINVAL; |
| 210 | usb_pm_lock(udev); |
| 211 | udev->persist_enabled = !!value; |
| 212 | usb_pm_unlock(udev); |
| 213 | return count; |
| 214 | } |
| 215 | |
| 216 | static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist); |
| 217 | |
| 218 | static int add_persist_attributes(struct device *dev) |
| 219 | { |
| 220 | int rc = 0; |
| 221 | |
| 222 | if (is_usb_device(dev)) { |
| 223 | struct usb_device *udev = to_usb_device(dev); |
| 224 | |
| 225 | /* Hubs are automatically enabled for USB_PERSIST */ |
| 226 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) |
| 227 | udev->persist_enabled = 1; |
| 228 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 229 | &dev_attr_persist.attr, |
| 230 | power_group); |
| 231 | } |
| 232 | return rc; |
| 233 | } |
| 234 | |
| 235 | static void remove_persist_attributes(struct device *dev) |
| 236 | { |
| 237 | sysfs_remove_file_from_group(&dev->kobj, |
| 238 | &dev_attr_persist.attr, |
| 239 | power_group); |
| 240 | } |
| 241 | |
| 242 | #else |
| 243 | |
| 244 | #define add_persist_attributes(dev) 0 |
| 245 | #define remove_persist_attributes(dev) do {} while (0) |
| 246 | |
| 247 | #endif /* CONFIG_USB_PERSIST */ |
| 248 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 249 | #ifdef CONFIG_USB_SUSPEND |
| 250 | |
| 251 | static ssize_t |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 252 | show_connected_duration(struct device *dev, struct device_attribute *attr, |
| 253 | char *buf) |
| 254 | { |
| 255 | struct usb_device *udev = to_usb_device(dev); |
| 256 | |
| 257 | return sprintf(buf, "%u\n", |
| 258 | jiffies_to_msecs(jiffies - udev->connect_time)); |
| 259 | } |
| 260 | |
| 261 | static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL); |
| 262 | |
| 263 | /* |
| 264 | * If the device is resumed, the last time the device was suspended has |
| 265 | * been pre-subtracted from active_duration. We add the current time to |
| 266 | * get the duration that the device was actually active. |
| 267 | * |
| 268 | * If the device is suspended, the active_duration is up-to-date. |
| 269 | */ |
| 270 | static ssize_t |
| 271 | show_active_duration(struct device *dev, struct device_attribute *attr, |
| 272 | char *buf) |
| 273 | { |
| 274 | struct usb_device *udev = to_usb_device(dev); |
| 275 | int duration; |
| 276 | |
| 277 | if (udev->state != USB_STATE_SUSPENDED) |
| 278 | duration = jiffies_to_msecs(jiffies + udev->active_duration); |
| 279 | else |
| 280 | duration = jiffies_to_msecs(udev->active_duration); |
| 281 | return sprintf(buf, "%u\n", duration); |
| 282 | } |
| 283 | |
| 284 | static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL); |
| 285 | |
| 286 | static ssize_t |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 287 | show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf) |
| 288 | { |
| 289 | struct usb_device *udev = to_usb_device(dev); |
| 290 | |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 291 | return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | static ssize_t |
| 295 | set_autosuspend(struct device *dev, struct device_attribute *attr, |
| 296 | const char *buf, size_t count) |
| 297 | { |
| 298 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 299 | int value; |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 300 | |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 301 | if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ || |
| 302 | value <= - INT_MAX/HZ) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 303 | return -EINVAL; |
| 304 | value *= HZ; |
| 305 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 306 | udev->autosuspend_delay = value; |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 307 | if (value >= 0) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 308 | usb_try_autosuspend_device(udev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 309 | else { |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 310 | if (usb_autoresume_device(udev) == 0) |
| 311 | usb_autosuspend_device(udev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 312 | } |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 313 | return count; |
| 314 | } |
| 315 | |
| 316 | static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR, |
| 317 | show_autosuspend, set_autosuspend); |
| 318 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 319 | static const char on_string[] = "on"; |
| 320 | static const char auto_string[] = "auto"; |
| 321 | static const char suspend_string[] = "suspend"; |
| 322 | |
| 323 | static ssize_t |
| 324 | show_level(struct device *dev, struct device_attribute *attr, char *buf) |
| 325 | { |
| 326 | struct usb_device *udev = to_usb_device(dev); |
| 327 | const char *p = auto_string; |
| 328 | |
| 329 | if (udev->state == USB_STATE_SUSPENDED) { |
| 330 | if (udev->autoresume_disabled) |
| 331 | p = suspend_string; |
| 332 | } else { |
| 333 | if (udev->autosuspend_disabled) |
| 334 | p = on_string; |
| 335 | } |
| 336 | return sprintf(buf, "%s\n", p); |
| 337 | } |
| 338 | |
| 339 | static ssize_t |
| 340 | set_level(struct device *dev, struct device_attribute *attr, |
| 341 | const char *buf, size_t count) |
| 342 | { |
| 343 | struct usb_device *udev = to_usb_device(dev); |
| 344 | int len = count; |
| 345 | char *cp; |
| 346 | int rc = 0; |
Alan Stern | dd86557 | 2007-05-22 11:38:19 -0400 | [diff] [blame] | 347 | int old_autosuspend_disabled, old_autoresume_disabled; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 348 | |
| 349 | cp = memchr(buf, '\n', count); |
| 350 | if (cp) |
| 351 | len = cp - buf; |
| 352 | |
| 353 | usb_lock_device(udev); |
Alan Stern | dd86557 | 2007-05-22 11:38:19 -0400 | [diff] [blame] | 354 | old_autosuspend_disabled = udev->autosuspend_disabled; |
| 355 | old_autoresume_disabled = udev->autoresume_disabled; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 356 | |
| 357 | /* Setting the flags without calling usb_pm_lock is a subject to |
| 358 | * races, but who cares... |
| 359 | */ |
| 360 | if (len == sizeof on_string - 1 && |
| 361 | strncmp(buf, on_string, len) == 0) { |
| 362 | udev->autosuspend_disabled = 1; |
| 363 | udev->autoresume_disabled = 0; |
| 364 | rc = usb_external_resume_device(udev); |
| 365 | |
| 366 | } else if (len == sizeof auto_string - 1 && |
| 367 | strncmp(buf, auto_string, len) == 0) { |
| 368 | udev->autosuspend_disabled = 0; |
| 369 | udev->autoresume_disabled = 0; |
| 370 | rc = usb_external_resume_device(udev); |
| 371 | |
| 372 | } else if (len == sizeof suspend_string - 1 && |
| 373 | strncmp(buf, suspend_string, len) == 0) { |
| 374 | udev->autosuspend_disabled = 0; |
| 375 | udev->autoresume_disabled = 1; |
| 376 | rc = usb_external_suspend_device(udev, PMSG_SUSPEND); |
| 377 | |
| 378 | } else |
| 379 | rc = -EINVAL; |
| 380 | |
Alan Stern | dd86557 | 2007-05-22 11:38:19 -0400 | [diff] [blame] | 381 | if (rc) { |
| 382 | udev->autosuspend_disabled = old_autosuspend_disabled; |
| 383 | udev->autoresume_disabled = old_autoresume_disabled; |
| 384 | } |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 385 | usb_unlock_device(udev); |
| 386 | return (rc < 0 ? rc : count); |
| 387 | } |
| 388 | |
| 389 | static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level); |
| 390 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 391 | static int add_power_attributes(struct device *dev) |
| 392 | { |
| 393 | int rc = 0; |
| 394 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 395 | if (is_usb_device(dev)) { |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 396 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 397 | &dev_attr_autosuspend.attr, |
| 398 | power_group); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 399 | if (rc == 0) |
| 400 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 401 | &dev_attr_level.attr, |
| 402 | power_group); |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 403 | if (rc == 0) |
| 404 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 405 | &dev_attr_connected_duration.attr, |
| 406 | power_group); |
| 407 | if (rc == 0) |
| 408 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 409 | &dev_attr_active_duration.attr, |
| 410 | power_group); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 411 | } |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 412 | return rc; |
| 413 | } |
| 414 | |
| 415 | static void remove_power_attributes(struct device *dev) |
| 416 | { |
| 417 | sysfs_remove_file_from_group(&dev->kobj, |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 418 | &dev_attr_active_duration.attr, |
| 419 | power_group); |
| 420 | sysfs_remove_file_from_group(&dev->kobj, |
| 421 | &dev_attr_connected_duration.attr, |
| 422 | power_group); |
| 423 | sysfs_remove_file_from_group(&dev->kobj, |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 424 | &dev_attr_level.attr, |
| 425 | power_group); |
| 426 | sysfs_remove_file_from_group(&dev->kobj, |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 427 | &dev_attr_autosuspend.attr, |
| 428 | power_group); |
| 429 | } |
| 430 | |
| 431 | #else |
| 432 | |
| 433 | #define add_power_attributes(dev) 0 |
| 434 | #define remove_power_attributes(dev) do {} while (0) |
| 435 | |
| 436 | #endif /* CONFIG_USB_SUSPEND */ |
| 437 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | /* Descriptor fields */ |
| 440 | #define usb_descriptor_attr_le16(field, format_string) \ |
| 441 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 442 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 443 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | { \ |
| 445 | struct usb_device *udev; \ |
| 446 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 447 | udev = to_usb_device(dev); \ |
| 448 | return sprintf(buf, format_string, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | le16_to_cpu(udev->descriptor.field)); \ |
| 450 | } \ |
| 451 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 452 | |
| 453 | usb_descriptor_attr_le16(idVendor, "%04x\n") |
| 454 | usb_descriptor_attr_le16(idProduct, "%04x\n") |
| 455 | usb_descriptor_attr_le16(bcdDevice, "%04x\n") |
| 456 | |
| 457 | #define usb_descriptor_attr(field, format_string) \ |
| 458 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 459 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 460 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { \ |
| 462 | struct usb_device *udev; \ |
| 463 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 464 | udev = to_usb_device(dev); \ |
| 465 | return sprintf(buf, format_string, udev->descriptor.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | } \ |
| 467 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 468 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 469 | usb_descriptor_attr(bDeviceClass, "%02x\n") |
| 470 | usb_descriptor_attr(bDeviceSubClass, "%02x\n") |
| 471 | usb_descriptor_attr(bDeviceProtocol, "%02x\n") |
| 472 | usb_descriptor_attr(bNumConfigurations, "%d\n") |
| 473 | usb_descriptor_attr(bMaxPacketSize0, "%d\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 475 | |
| 476 | |
| 477 | /* show if the device is authorized (1) or not (0) */ |
| 478 | static ssize_t usb_dev_authorized_show(struct device *dev, |
| 479 | struct device_attribute *attr, |
| 480 | char *buf) |
| 481 | { |
| 482 | struct usb_device *usb_dev = to_usb_device(dev); |
| 483 | return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized); |
| 484 | } |
| 485 | |
| 486 | |
| 487 | /* |
| 488 | * Authorize a device to be used in the system |
| 489 | * |
| 490 | * Writing a 0 deauthorizes the device, writing a 1 authorizes it. |
| 491 | */ |
| 492 | static ssize_t usb_dev_authorized_store(struct device *dev, |
| 493 | struct device_attribute *attr, |
| 494 | const char *buf, size_t size) |
| 495 | { |
| 496 | ssize_t result; |
| 497 | struct usb_device *usb_dev = to_usb_device(dev); |
| 498 | unsigned val; |
| 499 | result = sscanf(buf, "%u\n", &val); |
| 500 | if (result != 1) |
| 501 | result = -EINVAL; |
| 502 | else if (val == 0) |
| 503 | result = usb_deauthorize_device(usb_dev); |
| 504 | else |
| 505 | result = usb_authorize_device(usb_dev); |
| 506 | return result < 0? result : size; |
| 507 | } |
| 508 | |
| 509 | static DEVICE_ATTR(authorized, 0644, |
| 510 | usb_dev_authorized_show, usb_dev_authorized_store); |
| 511 | |
| 512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | static struct attribute *dev_attrs[] = { |
| 514 | /* current configuration's attributes */ |
Alan Stern | b6eb2d8 | 2006-07-06 15:37:42 -0400 | [diff] [blame] | 515 | &dev_attr_configuration.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | &dev_attr_bNumInterfaces.attr, |
| 517 | &dev_attr_bConfigurationValue.attr, |
| 518 | &dev_attr_bmAttributes.attr, |
| 519 | &dev_attr_bMaxPower.attr, |
Sarah Sharp | 4d59d8a | 2007-10-03 14:56:03 -0700 | [diff] [blame] | 520 | &dev_attr_urbnum.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | /* device attributes */ |
| 522 | &dev_attr_idVendor.attr, |
| 523 | &dev_attr_idProduct.attr, |
| 524 | &dev_attr_bcdDevice.attr, |
| 525 | &dev_attr_bDeviceClass.attr, |
| 526 | &dev_attr_bDeviceSubClass.attr, |
| 527 | &dev_attr_bDeviceProtocol.attr, |
| 528 | &dev_attr_bNumConfigurations.attr, |
Greg Kroah-Hartman | cf5910b | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 529 | &dev_attr_bMaxPacketSize0.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | &dev_attr_speed.attr, |
Alan Stern | 83f7d95 | 2007-04-25 15:15:43 -0400 | [diff] [blame] | 531 | &dev_attr_busnum.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | &dev_attr_devnum.attr, |
| 533 | &dev_attr_version.attr, |
| 534 | &dev_attr_maxchild.attr, |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 535 | &dev_attr_quirks.attr, |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 536 | &dev_attr_authorized.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | NULL, |
| 538 | }; |
| 539 | static struct attribute_group dev_attr_grp = { |
| 540 | .attrs = dev_attrs, |
| 541 | }; |
| 542 | |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 543 | /* Binary descriptors */ |
| 544 | |
| 545 | static ssize_t |
| 546 | read_descriptors(struct kobject *kobj, struct bin_attribute *attr, |
| 547 | char *buf, loff_t off, size_t count) |
| 548 | { |
| 549 | struct usb_device *udev = to_usb_device( |
| 550 | container_of(kobj, struct device, kobj)); |
| 551 | size_t nleft = count; |
| 552 | size_t srclen, n; |
| 553 | |
| 554 | usb_lock_device(udev); |
| 555 | |
| 556 | /* The binary attribute begins with the device descriptor */ |
| 557 | srclen = sizeof(struct usb_device_descriptor); |
| 558 | if (off < srclen) { |
| 559 | n = min_t(size_t, nleft, srclen - off); |
| 560 | memcpy(buf, off + (char *) &udev->descriptor, n); |
| 561 | nleft -= n; |
| 562 | buf += n; |
| 563 | off = 0; |
| 564 | } else { |
| 565 | off -= srclen; |
| 566 | } |
| 567 | |
| 568 | /* Then follows the raw descriptor entry for the current |
| 569 | * configuration (config plus subsidiary descriptors). |
| 570 | */ |
| 571 | if (udev->actconfig) { |
| 572 | int cfgno = udev->actconfig - udev->config; |
| 573 | |
| 574 | srclen = __le16_to_cpu(udev->actconfig->desc.wTotalLength); |
| 575 | if (off < srclen) { |
| 576 | n = min_t(size_t, nleft, srclen - off); |
| 577 | memcpy(buf, off + udev->rawdescriptors[cfgno], n); |
| 578 | nleft -= n; |
| 579 | } |
| 580 | } |
| 581 | usb_unlock_device(udev); |
| 582 | return count - nleft; |
| 583 | } |
| 584 | |
| 585 | static struct bin_attribute dev_bin_attr_descriptors = { |
| 586 | .attr = {.name = "descriptors", .mode = 0444}, |
| 587 | .read = read_descriptors, |
| 588 | .size = 18 + 65535, /* dev descr + max-size raw descriptor */ |
| 589 | }; |
| 590 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 591 | int usb_create_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
| 593 | struct device *dev = &udev->dev; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 594 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 596 | retval = sysfs_create_group(&dev->kobj, &dev_attr_grp); |
| 597 | if (retval) |
| 598 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 600 | retval = device_create_bin_file(dev, &dev_bin_attr_descriptors); |
| 601 | if (retval) |
| 602 | goto error; |
| 603 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 604 | retval = add_persist_attributes(dev); |
| 605 | if (retval) |
| 606 | goto error; |
| 607 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 608 | retval = add_power_attributes(dev); |
| 609 | if (retval) |
| 610 | goto error; |
| 611 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 612 | if (udev->manufacturer) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 613 | retval = device_create_file(dev, &dev_attr_manufacturer); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 614 | if (retval) |
| 615 | goto error; |
| 616 | } |
| 617 | if (udev->product) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 618 | retval = device_create_file(dev, &dev_attr_product); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 619 | if (retval) |
| 620 | goto error; |
| 621 | } |
| 622 | if (udev->serial) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 623 | retval = device_create_file(dev, &dev_attr_serial); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 624 | if (retval) |
| 625 | goto error; |
| 626 | } |
| 627 | retval = usb_create_ep_files(dev, &udev->ep0, udev); |
| 628 | if (retval) |
| 629 | goto error; |
| 630 | return 0; |
| 631 | error: |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 632 | usb_remove_sysfs_dev_files(udev); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 633 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | } |
| 635 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 636 | void usb_remove_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | { |
| 638 | struct device *dev = &udev->dev; |
| 639 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 640 | usb_remove_ep_files(&udev->ep0); |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 641 | device_remove_file(dev, &dev_attr_manufacturer); |
| 642 | device_remove_file(dev, &dev_attr_product); |
| 643 | device_remove_file(dev, &dev_attr_serial); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 644 | remove_power_attributes(dev); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 645 | remove_persist_attributes(dev); |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 646 | device_remove_bin_file(dev, &dev_bin_attr_descriptors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | sysfs_remove_group(&dev->kobj, &dev_attr_grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 650 | /* Interface Accociation Descriptor fields */ |
| 651 | #define usb_intf_assoc_attr(field, format_string) \ |
| 652 | static ssize_t \ |
| 653 | show_iad_##field (struct device *dev, struct device_attribute *attr, \ |
| 654 | char *buf) \ |
| 655 | { \ |
| 656 | struct usb_interface *intf = to_usb_interface (dev); \ |
| 657 | \ |
| 658 | return sprintf (buf, format_string, \ |
| 659 | intf->intf_assoc->field); \ |
| 660 | } \ |
| 661 | static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL); |
| 662 | |
| 663 | usb_intf_assoc_attr (bFirstInterface, "%02x\n") |
| 664 | usb_intf_assoc_attr (bInterfaceCount, "%02d\n") |
| 665 | usb_intf_assoc_attr (bFunctionClass, "%02x\n") |
| 666 | usb_intf_assoc_attr (bFunctionSubClass, "%02x\n") |
| 667 | usb_intf_assoc_attr (bFunctionProtocol, "%02x\n") |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | /* Interface fields */ |
| 670 | #define usb_intf_attr(field, format_string) \ |
| 671 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 672 | show_##field(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 | { \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 675 | struct usb_interface *intf = to_usb_interface(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 677 | return sprintf(buf, format_string, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 678 | intf->cur_altsetting->desc.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | } \ |
| 680 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 681 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 682 | usb_intf_attr(bInterfaceNumber, "%02x\n") |
| 683 | usb_intf_attr(bAlternateSetting, "%2d\n") |
| 684 | usb_intf_attr(bNumEndpoints, "%02x\n") |
| 685 | usb_intf_attr(bInterfaceClass, "%02x\n") |
| 686 | usb_intf_attr(bInterfaceSubClass, "%02x\n") |
| 687 | usb_intf_attr(bInterfaceProtocol, "%02x\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 689 | static ssize_t show_interface_string(struct device *dev, |
| 690 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | { |
| 692 | struct usb_interface *intf; |
| 693 | struct usb_device *udev; |
| 694 | int len; |
| 695 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 696 | intf = to_usb_interface(dev); |
| 697 | udev = interface_to_usbdev(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | len = snprintf(buf, 256, "%s", intf->cur_altsetting->string); |
| 699 | if (len < 0) |
| 700 | return 0; |
| 701 | buf[len] = '\n'; |
| 702 | buf[len+1] = 0; |
| 703 | return len+1; |
| 704 | } |
| 705 | static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL); |
| 706 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 707 | static ssize_t show_modalias(struct device *dev, |
| 708 | struct device_attribute *attr, char *buf) |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 709 | { |
| 710 | struct usb_interface *intf; |
| 711 | struct usb_device *udev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 712 | struct usb_host_interface *alt; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 713 | |
| 714 | intf = to_usb_interface(dev); |
| 715 | udev = interface_to_usbdev(intf); |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 716 | alt = intf->cur_altsetting; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 717 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 718 | return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X" |
| 719 | "ic%02Xisc%02Xip%02X\n", |
| 720 | le16_to_cpu(udev->descriptor.idVendor), |
| 721 | le16_to_cpu(udev->descriptor.idProduct), |
| 722 | le16_to_cpu(udev->descriptor.bcdDevice), |
| 723 | udev->descriptor.bDeviceClass, |
| 724 | udev->descriptor.bDeviceSubClass, |
| 725 | udev->descriptor.bDeviceProtocol, |
| 726 | alt->desc.bInterfaceClass, |
| 727 | alt->desc.bInterfaceSubClass, |
| 728 | alt->desc.bInterfaceProtocol); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 729 | } |
| 730 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
| 731 | |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 732 | static struct attribute *intf_assoc_attrs[] = { |
| 733 | &dev_attr_iad_bFirstInterface.attr, |
| 734 | &dev_attr_iad_bInterfaceCount.attr, |
| 735 | &dev_attr_iad_bFunctionClass.attr, |
| 736 | &dev_attr_iad_bFunctionSubClass.attr, |
| 737 | &dev_attr_iad_bFunctionProtocol.attr, |
| 738 | NULL, |
| 739 | }; |
| 740 | static struct attribute_group intf_assoc_attr_grp = { |
| 741 | .attrs = intf_assoc_attrs, |
| 742 | }; |
| 743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | static struct attribute *intf_attrs[] = { |
| 745 | &dev_attr_bInterfaceNumber.attr, |
| 746 | &dev_attr_bAlternateSetting.attr, |
| 747 | &dev_attr_bNumEndpoints.attr, |
| 748 | &dev_attr_bInterfaceClass.attr, |
| 749 | &dev_attr_bInterfaceSubClass.attr, |
| 750 | &dev_attr_bInterfaceProtocol.attr, |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 751 | &dev_attr_modalias.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | NULL, |
| 753 | }; |
| 754 | static struct attribute_group intf_attr_grp = { |
| 755 | .attrs = intf_attrs, |
| 756 | }; |
| 757 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 758 | static inline void usb_create_intf_ep_files(struct usb_interface *intf, |
| 759 | struct usb_device *udev) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 760 | { |
| 761 | struct usb_host_interface *iface_desc; |
| 762 | int i; |
| 763 | |
| 764 | iface_desc = intf->cur_altsetting; |
| 765 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Greg Kroah-Hartman | 36679ea | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 766 | usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i], |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 767 | udev); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 770 | static inline void usb_remove_intf_ep_files(struct usb_interface *intf) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 771 | { |
| 772 | struct usb_host_interface *iface_desc; |
| 773 | int i; |
| 774 | |
| 775 | iface_desc = intf->cur_altsetting; |
| 776 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 777 | usb_remove_ep_files(&iface_desc->endpoint[i]); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 780 | int usb_create_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | { |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 782 | struct device *dev = &intf->dev; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 783 | struct usb_device *udev = interface_to_usbdev(intf); |
| 784 | struct usb_host_interface *alt = intf->cur_altsetting; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 785 | int retval; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 786 | |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 787 | if (intf->sysfs_files_created) |
| 788 | return 0; |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 789 | retval = sysfs_create_group(&dev->kobj, &intf_attr_grp); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 790 | if (retval) |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 791 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 793 | if (alt->string == NULL) |
| 794 | alt->string = usb_cache_string(udev, alt->desc.iInterface); |
| 795 | if (alt->string) |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 796 | retval = device_create_file(dev, &dev_attr_interface); |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 797 | if (intf->intf_assoc) |
| 798 | retval = sysfs_create_group(&dev->kobj, &intf_assoc_attr_grp); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 799 | usb_create_intf_ep_files(intf, udev); |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 800 | intf->sysfs_files_created = 1; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 801 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | } |
| 803 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 804 | void usb_remove_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | { |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 806 | struct device *dev = &intf->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 808 | if (!intf->sysfs_files_created) |
| 809 | return; |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 810 | usb_remove_intf_ep_files(intf); |
| 811 | device_remove_file(dev, &dev_attr_interface); |
| 812 | sysfs_remove_group(&dev->kobj, &intf_attr_grp); |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 813 | sysfs_remove_group(&intf->dev.kobj, &intf_assoc_attr_grp); |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 814 | intf->sysfs_files_created = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 815 | } |