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 | |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 75 | static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 183 | #ifdef CONFIG_PM |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 184 | |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 185 | static const char power_group[] = "power"; |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 186 | |
| 187 | static ssize_t |
| 188 | show_persist(struct device *dev, struct device_attribute *attr, char *buf) |
| 189 | { |
| 190 | struct usb_device *udev = to_usb_device(dev); |
| 191 | |
| 192 | return sprintf(buf, "%d\n", udev->persist_enabled); |
| 193 | } |
| 194 | |
| 195 | static ssize_t |
| 196 | set_persist(struct device *dev, struct device_attribute *attr, |
| 197 | const char *buf, size_t count) |
| 198 | { |
| 199 | struct usb_device *udev = to_usb_device(dev); |
| 200 | int value; |
| 201 | |
| 202 | /* Hubs are always enabled for USB_PERSIST */ |
| 203 | if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) |
| 204 | return -EPERM; |
| 205 | |
| 206 | if (sscanf(buf, "%d", &value) != 1) |
| 207 | return -EINVAL; |
| 208 | usb_pm_lock(udev); |
| 209 | udev->persist_enabled = !!value; |
| 210 | usb_pm_unlock(udev); |
| 211 | return count; |
| 212 | } |
| 213 | |
| 214 | static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist); |
| 215 | |
| 216 | static int add_persist_attributes(struct device *dev) |
| 217 | { |
| 218 | int rc = 0; |
| 219 | |
| 220 | if (is_usb_device(dev)) { |
| 221 | struct usb_device *udev = to_usb_device(dev); |
| 222 | |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 223 | /* Hubs are automatically enabled for USB_PERSIST, |
| 224 | * no point in creating the attribute file. |
| 225 | */ |
| 226 | if (udev->descriptor.bDeviceClass != USB_CLASS_HUB) |
| 227 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 228 | &dev_attr_persist.attr, |
| 229 | power_group); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 230 | } |
| 231 | return rc; |
| 232 | } |
| 233 | |
| 234 | static void remove_persist_attributes(struct device *dev) |
| 235 | { |
| 236 | sysfs_remove_file_from_group(&dev->kobj, |
| 237 | &dev_attr_persist.attr, |
| 238 | power_group); |
| 239 | } |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 240 | #else |
| 241 | |
| 242 | #define add_persist_attributes(dev) 0 |
| 243 | #define remove_persist_attributes(dev) do {} while (0) |
| 244 | |
Alan Stern | feccc30 | 2008-03-03 15:15:59 -0500 | [diff] [blame] | 245 | #endif /* CONFIG_PM */ |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 246 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 247 | #ifdef CONFIG_USB_SUSPEND |
| 248 | |
| 249 | static ssize_t |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 250 | show_connected_duration(struct device *dev, struct device_attribute *attr, |
| 251 | char *buf) |
| 252 | { |
| 253 | struct usb_device *udev = to_usb_device(dev); |
| 254 | |
| 255 | return sprintf(buf, "%u\n", |
| 256 | jiffies_to_msecs(jiffies - udev->connect_time)); |
| 257 | } |
| 258 | |
| 259 | static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL); |
| 260 | |
| 261 | /* |
| 262 | * If the device is resumed, the last time the device was suspended has |
| 263 | * been pre-subtracted from active_duration. We add the current time to |
| 264 | * get the duration that the device was actually active. |
| 265 | * |
| 266 | * If the device is suspended, the active_duration is up-to-date. |
| 267 | */ |
| 268 | static ssize_t |
| 269 | show_active_duration(struct device *dev, struct device_attribute *attr, |
| 270 | char *buf) |
| 271 | { |
| 272 | struct usb_device *udev = to_usb_device(dev); |
| 273 | int duration; |
| 274 | |
| 275 | if (udev->state != USB_STATE_SUSPENDED) |
| 276 | duration = jiffies_to_msecs(jiffies + udev->active_duration); |
| 277 | else |
| 278 | duration = jiffies_to_msecs(udev->active_duration); |
| 279 | return sprintf(buf, "%u\n", duration); |
| 280 | } |
| 281 | |
| 282 | static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL); |
| 283 | |
| 284 | static ssize_t |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 285 | show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf) |
| 286 | { |
| 287 | struct usb_device *udev = to_usb_device(dev); |
| 288 | |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 289 | return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static ssize_t |
| 293 | set_autosuspend(struct device *dev, struct device_attribute *attr, |
| 294 | const char *buf, size_t count) |
| 295 | { |
| 296 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 297 | int value; |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 298 | |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 299 | if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ || |
| 300 | value <= - INT_MAX/HZ) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 301 | return -EINVAL; |
| 302 | value *= HZ; |
| 303 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 304 | udev->autosuspend_delay = value; |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 305 | if (value >= 0) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 306 | usb_try_autosuspend_device(udev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 307 | else { |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 308 | if (usb_autoresume_device(udev) == 0) |
| 309 | usb_autosuspend_device(udev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 310 | } |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 311 | return count; |
| 312 | } |
| 313 | |
| 314 | static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR, |
| 315 | show_autosuspend, set_autosuspend); |
| 316 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 317 | static const char on_string[] = "on"; |
| 318 | static const char auto_string[] = "auto"; |
| 319 | static const char suspend_string[] = "suspend"; |
| 320 | |
| 321 | static ssize_t |
| 322 | show_level(struct device *dev, struct device_attribute *attr, char *buf) |
| 323 | { |
| 324 | struct usb_device *udev = to_usb_device(dev); |
| 325 | const char *p = auto_string; |
| 326 | |
| 327 | if (udev->state == USB_STATE_SUSPENDED) { |
| 328 | if (udev->autoresume_disabled) |
| 329 | p = suspend_string; |
| 330 | } else { |
| 331 | if (udev->autosuspend_disabled) |
| 332 | p = on_string; |
| 333 | } |
| 334 | return sprintf(buf, "%s\n", p); |
| 335 | } |
| 336 | |
| 337 | static ssize_t |
| 338 | set_level(struct device *dev, struct device_attribute *attr, |
| 339 | const char *buf, size_t count) |
| 340 | { |
| 341 | struct usb_device *udev = to_usb_device(dev); |
| 342 | int len = count; |
| 343 | char *cp; |
| 344 | int rc = 0; |
Alan Stern | dd86557 | 2007-05-22 11:38:19 -0400 | [diff] [blame] | 345 | int old_autosuspend_disabled, old_autoresume_disabled; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 346 | |
| 347 | cp = memchr(buf, '\n', count); |
| 348 | if (cp) |
| 349 | len = cp - buf; |
| 350 | |
| 351 | usb_lock_device(udev); |
Alan Stern | dd86557 | 2007-05-22 11:38:19 -0400 | [diff] [blame] | 352 | old_autosuspend_disabled = udev->autosuspend_disabled; |
| 353 | old_autoresume_disabled = udev->autoresume_disabled; |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 354 | |
| 355 | /* Setting the flags without calling usb_pm_lock is a subject to |
| 356 | * races, but who cares... |
| 357 | */ |
| 358 | if (len == sizeof on_string - 1 && |
| 359 | strncmp(buf, on_string, len) == 0) { |
| 360 | udev->autosuspend_disabled = 1; |
| 361 | udev->autoresume_disabled = 0; |
| 362 | rc = usb_external_resume_device(udev); |
| 363 | |
| 364 | } else if (len == sizeof auto_string - 1 && |
| 365 | strncmp(buf, auto_string, len) == 0) { |
| 366 | udev->autosuspend_disabled = 0; |
| 367 | udev->autoresume_disabled = 0; |
| 368 | rc = usb_external_resume_device(udev); |
| 369 | |
| 370 | } else if (len == sizeof suspend_string - 1 && |
| 371 | strncmp(buf, suspend_string, len) == 0) { |
| 372 | udev->autosuspend_disabled = 0; |
| 373 | udev->autoresume_disabled = 1; |
| 374 | rc = usb_external_suspend_device(udev, PMSG_SUSPEND); |
| 375 | |
| 376 | } else |
| 377 | rc = -EINVAL; |
| 378 | |
Alan Stern | dd86557 | 2007-05-22 11:38:19 -0400 | [diff] [blame] | 379 | if (rc) { |
| 380 | udev->autosuspend_disabled = old_autosuspend_disabled; |
| 381 | udev->autoresume_disabled = old_autoresume_disabled; |
| 382 | } |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 383 | usb_unlock_device(udev); |
| 384 | return (rc < 0 ? rc : count); |
| 385 | } |
| 386 | |
| 387 | static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level); |
| 388 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 389 | static int add_power_attributes(struct device *dev) |
| 390 | { |
| 391 | int rc = 0; |
| 392 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 393 | if (is_usb_device(dev)) { |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 394 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 395 | &dev_attr_autosuspend.attr, |
| 396 | power_group); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 397 | if (rc == 0) |
| 398 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 399 | &dev_attr_level.attr, |
| 400 | power_group); |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 401 | if (rc == 0) |
| 402 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 403 | &dev_attr_connected_duration.attr, |
| 404 | power_group); |
| 405 | if (rc == 0) |
| 406 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 407 | &dev_attr_active_duration.attr, |
| 408 | power_group); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 409 | } |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 410 | return rc; |
| 411 | } |
| 412 | |
| 413 | static void remove_power_attributes(struct device *dev) |
| 414 | { |
| 415 | sysfs_remove_file_from_group(&dev->kobj, |
Sarah Sharp | 1512300 | 2007-12-21 16:54:15 -0800 | [diff] [blame] | 416 | &dev_attr_active_duration.attr, |
| 417 | power_group); |
| 418 | sysfs_remove_file_from_group(&dev->kobj, |
| 419 | &dev_attr_connected_duration.attr, |
| 420 | power_group); |
| 421 | sysfs_remove_file_from_group(&dev->kobj, |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 422 | &dev_attr_level.attr, |
| 423 | power_group); |
| 424 | sysfs_remove_file_from_group(&dev->kobj, |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 425 | &dev_attr_autosuspend.attr, |
| 426 | power_group); |
| 427 | } |
| 428 | |
| 429 | #else |
| 430 | |
| 431 | #define add_power_attributes(dev) 0 |
| 432 | #define remove_power_attributes(dev) do {} while (0) |
| 433 | |
| 434 | #endif /* CONFIG_USB_SUSPEND */ |
| 435 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | /* Descriptor fields */ |
| 438 | #define usb_descriptor_attr_le16(field, format_string) \ |
| 439 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 440 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 441 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { \ |
| 443 | struct usb_device *udev; \ |
| 444 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 445 | udev = to_usb_device(dev); \ |
| 446 | return sprintf(buf, format_string, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | le16_to_cpu(udev->descriptor.field)); \ |
| 448 | } \ |
| 449 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 450 | |
| 451 | usb_descriptor_attr_le16(idVendor, "%04x\n") |
| 452 | usb_descriptor_attr_le16(idProduct, "%04x\n") |
| 453 | usb_descriptor_attr_le16(bcdDevice, "%04x\n") |
| 454 | |
| 455 | #define usb_descriptor_attr(field, format_string) \ |
| 456 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 457 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 458 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | { \ |
| 460 | struct usb_device *udev; \ |
| 461 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 462 | udev = to_usb_device(dev); \ |
| 463 | return sprintf(buf, format_string, udev->descriptor.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } \ |
| 465 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 466 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 467 | usb_descriptor_attr(bDeviceClass, "%02x\n") |
| 468 | usb_descriptor_attr(bDeviceSubClass, "%02x\n") |
| 469 | usb_descriptor_attr(bDeviceProtocol, "%02x\n") |
| 470 | usb_descriptor_attr(bNumConfigurations, "%d\n") |
| 471 | usb_descriptor_attr(bMaxPacketSize0, "%d\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 473 | |
| 474 | |
| 475 | /* show if the device is authorized (1) or not (0) */ |
| 476 | static ssize_t usb_dev_authorized_show(struct device *dev, |
| 477 | struct device_attribute *attr, |
| 478 | char *buf) |
| 479 | { |
| 480 | struct usb_device *usb_dev = to_usb_device(dev); |
| 481 | return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized); |
| 482 | } |
| 483 | |
| 484 | |
| 485 | /* |
| 486 | * Authorize a device to be used in the system |
| 487 | * |
| 488 | * Writing a 0 deauthorizes the device, writing a 1 authorizes it. |
| 489 | */ |
| 490 | static ssize_t usb_dev_authorized_store(struct device *dev, |
| 491 | struct device_attribute *attr, |
| 492 | const char *buf, size_t size) |
| 493 | { |
| 494 | ssize_t result; |
| 495 | struct usb_device *usb_dev = to_usb_device(dev); |
| 496 | unsigned val; |
| 497 | result = sscanf(buf, "%u\n", &val); |
| 498 | if (result != 1) |
| 499 | result = -EINVAL; |
| 500 | else if (val == 0) |
| 501 | result = usb_deauthorize_device(usb_dev); |
| 502 | else |
| 503 | result = usb_authorize_device(usb_dev); |
| 504 | return result < 0? result : size; |
| 505 | } |
| 506 | |
| 507 | static DEVICE_ATTR(authorized, 0644, |
| 508 | usb_dev_authorized_show, usb_dev_authorized_store); |
| 509 | |
| 510 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | static struct attribute *dev_attrs[] = { |
| 512 | /* current configuration's attributes */ |
Alan Stern | b6eb2d8 | 2006-07-06 15:37:42 -0400 | [diff] [blame] | 513 | &dev_attr_configuration.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | &dev_attr_bNumInterfaces.attr, |
| 515 | &dev_attr_bConfigurationValue.attr, |
| 516 | &dev_attr_bmAttributes.attr, |
| 517 | &dev_attr_bMaxPower.attr, |
Sarah Sharp | 4d59d8a | 2007-10-03 14:56:03 -0700 | [diff] [blame] | 518 | &dev_attr_urbnum.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | /* device attributes */ |
| 520 | &dev_attr_idVendor.attr, |
| 521 | &dev_attr_idProduct.attr, |
| 522 | &dev_attr_bcdDevice.attr, |
| 523 | &dev_attr_bDeviceClass.attr, |
| 524 | &dev_attr_bDeviceSubClass.attr, |
| 525 | &dev_attr_bDeviceProtocol.attr, |
| 526 | &dev_attr_bNumConfigurations.attr, |
Greg Kroah-Hartman | cf5910b | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 527 | &dev_attr_bMaxPacketSize0.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | &dev_attr_speed.attr, |
Alan Stern | 83f7d95 | 2007-04-25 15:15:43 -0400 | [diff] [blame] | 529 | &dev_attr_busnum.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | &dev_attr_devnum.attr, |
| 531 | &dev_attr_version.attr, |
| 532 | &dev_attr_maxchild.attr, |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 533 | &dev_attr_quirks.attr, |
Inaky Perez-Gonzalez | e03f2e8 | 2007-07-31 20:34:07 -0700 | [diff] [blame] | 534 | &dev_attr_authorized.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | NULL, |
| 536 | }; |
| 537 | static struct attribute_group dev_attr_grp = { |
| 538 | .attrs = dev_attrs, |
| 539 | }; |
| 540 | |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 541 | /* When modifying this list, be sure to modify dev_string_attrs_are_visible() |
| 542 | * accordingly. |
| 543 | */ |
| 544 | static struct attribute *dev_string_attrs[] = { |
| 545 | &dev_attr_manufacturer.attr, |
| 546 | &dev_attr_product.attr, |
| 547 | &dev_attr_serial.attr, |
| 548 | NULL |
| 549 | }; |
| 550 | |
| 551 | static mode_t dev_string_attrs_are_visible(struct kobject *kobj, |
| 552 | struct attribute *a, int n) |
| 553 | { |
| 554 | struct usb_device *udev = to_usb_device( |
| 555 | container_of(kobj, struct device, kobj)); |
| 556 | |
| 557 | if (a == &dev_attr_manufacturer.attr) { |
| 558 | if (udev->manufacturer == NULL) |
| 559 | return 0; |
| 560 | } else if (a == &dev_attr_product.attr) { |
| 561 | if (udev->product == NULL) |
| 562 | return 0; |
| 563 | } else if (a == &dev_attr_serial.attr) { |
| 564 | if (udev->serial == NULL) |
| 565 | return 0; |
| 566 | } |
| 567 | return a->mode; |
| 568 | } |
| 569 | |
| 570 | static struct attribute_group dev_string_attr_grp = { |
| 571 | .attrs = dev_string_attrs, |
| 572 | .is_visible = dev_string_attrs_are_visible, |
| 573 | }; |
| 574 | |
| 575 | struct attribute_group *usb_device_groups[] = { |
| 576 | &dev_attr_grp, |
| 577 | &dev_string_attr_grp, |
| 578 | NULL |
| 579 | }; |
| 580 | |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 581 | /* Binary descriptors */ |
| 582 | |
| 583 | static ssize_t |
| 584 | read_descriptors(struct kobject *kobj, struct bin_attribute *attr, |
| 585 | char *buf, loff_t off, size_t count) |
| 586 | { |
| 587 | struct usb_device *udev = to_usb_device( |
| 588 | container_of(kobj, struct device, kobj)); |
| 589 | size_t nleft = count; |
| 590 | size_t srclen, n; |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 591 | int cfgno; |
| 592 | void *src; |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 593 | |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 594 | /* The binary attribute begins with the device descriptor. |
| 595 | * Following that are the raw descriptor entries for all the |
| 596 | * configurations (config plus subsidiary descriptors). |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 597 | */ |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 598 | for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations && |
| 599 | nleft > 0; ++cfgno) { |
| 600 | if (cfgno < 0) { |
| 601 | src = &udev->descriptor; |
| 602 | srclen = sizeof(struct usb_device_descriptor); |
| 603 | } else { |
| 604 | src = udev->rawdescriptors[cfgno]; |
| 605 | srclen = __le16_to_cpu(udev->config[cfgno].desc. |
| 606 | wTotalLength); |
| 607 | } |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 608 | if (off < srclen) { |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 609 | n = min(nleft, srclen - (size_t) off); |
| 610 | memcpy(buf, src + off, n); |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 611 | nleft -= n; |
Alan Stern | 217a908 | 2008-05-20 16:40:42 -0400 | [diff] [blame] | 612 | buf += n; |
| 613 | off = 0; |
| 614 | } else { |
| 615 | off -= srclen; |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 616 | } |
| 617 | } |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 618 | return count - nleft; |
| 619 | } |
| 620 | |
| 621 | static struct bin_attribute dev_bin_attr_descriptors = { |
| 622 | .attr = {.name = "descriptors", .mode = 0444}, |
| 623 | .read = read_descriptors, |
| 624 | .size = 18 + 65535, /* dev descr + max-size raw descriptor */ |
| 625 | }; |
| 626 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 627 | int usb_create_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | { |
| 629 | struct device *dev = &udev->dev; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 630 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 632 | /* Unforunately these attributes cannot be created before |
| 633 | * the uevent is broadcast. |
| 634 | */ |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 635 | retval = device_create_bin_file(dev, &dev_bin_attr_descriptors); |
| 636 | if (retval) |
| 637 | goto error; |
| 638 | |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 639 | retval = add_persist_attributes(dev); |
| 640 | if (retval) |
| 641 | goto error; |
| 642 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 643 | retval = add_power_attributes(dev); |
| 644 | if (retval) |
| 645 | goto error; |
| 646 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 647 | retval = usb_create_ep_files(dev, &udev->ep0, udev); |
| 648 | if (retval) |
| 649 | goto error; |
| 650 | return 0; |
| 651 | error: |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 652 | usb_remove_sysfs_dev_files(udev); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 653 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 656 | void usb_remove_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | { |
| 658 | struct device *dev = &udev->dev; |
| 659 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 660 | usb_remove_ep_files(&udev->ep0); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 661 | remove_power_attributes(dev); |
Alan Stern | b41a60e | 2007-05-30 15:39:33 -0400 | [diff] [blame] | 662 | remove_persist_attributes(dev); |
Alan Stern | 69d42a7 | 2007-07-12 17:06:23 -0400 | [diff] [blame] | 663 | device_remove_bin_file(dev, &dev_bin_attr_descriptors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 666 | /* Interface Accociation Descriptor fields */ |
| 667 | #define usb_intf_assoc_attr(field, format_string) \ |
| 668 | static ssize_t \ |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 669 | show_iad_##field(struct device *dev, struct device_attribute *attr, \ |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 670 | char *buf) \ |
| 671 | { \ |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 672 | struct usb_interface *intf = to_usb_interface(dev); \ |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 673 | \ |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 674 | return sprintf(buf, format_string, \ |
| 675 | intf->intf_assoc->field); \ |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 676 | } \ |
| 677 | static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL); |
| 678 | |
Greg Kroah-Hartman | 2c044a4 | 2008-01-30 15:21:33 -0800 | [diff] [blame] | 679 | usb_intf_assoc_attr(bFirstInterface, "%02x\n") |
| 680 | usb_intf_assoc_attr(bInterfaceCount, "%02d\n") |
| 681 | usb_intf_assoc_attr(bFunctionClass, "%02x\n") |
| 682 | usb_intf_assoc_attr(bFunctionSubClass, "%02x\n") |
| 683 | usb_intf_assoc_attr(bFunctionProtocol, "%02x\n") |
Craig W. Nadler | 165fe97 | 2007-06-15 23:14:35 -0400 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | /* Interface fields */ |
| 686 | #define usb_intf_attr(field, format_string) \ |
| 687 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 688 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 689 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | { \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 691 | struct usb_interface *intf = to_usb_interface(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 693 | return sprintf(buf, format_string, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 694 | intf->cur_altsetting->desc.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | } \ |
| 696 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 697 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 698 | usb_intf_attr(bInterfaceNumber, "%02x\n") |
| 699 | usb_intf_attr(bAlternateSetting, "%2d\n") |
| 700 | usb_intf_attr(bNumEndpoints, "%02x\n") |
| 701 | usb_intf_attr(bInterfaceClass, "%02x\n") |
| 702 | usb_intf_attr(bInterfaceSubClass, "%02x\n") |
| 703 | usb_intf_attr(bInterfaceProtocol, "%02x\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 705 | static ssize_t show_interface_string(struct device *dev, |
| 706 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | { |
| 708 | struct usb_interface *intf; |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 709 | char *string; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 711 | intf = to_usb_interface(dev); |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 712 | string = intf->cur_altsetting->string; |
| 713 | barrier(); /* The altsetting might change! */ |
| 714 | |
| 715 | if (!string) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | return 0; |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 717 | return sprintf(buf, "%s\n", string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | } |
| 719 | static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL); |
| 720 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 721 | static ssize_t show_modalias(struct device *dev, |
| 722 | struct device_attribute *attr, char *buf) |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 723 | { |
| 724 | struct usb_interface *intf; |
| 725 | struct usb_device *udev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 726 | struct usb_host_interface *alt; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 727 | |
| 728 | intf = to_usb_interface(dev); |
| 729 | udev = interface_to_usbdev(intf); |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 730 | alt = intf->cur_altsetting; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 731 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 732 | return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X" |
| 733 | "ic%02Xisc%02Xip%02X\n", |
| 734 | le16_to_cpu(udev->descriptor.idVendor), |
| 735 | le16_to_cpu(udev->descriptor.idProduct), |
| 736 | le16_to_cpu(udev->descriptor.bcdDevice), |
| 737 | udev->descriptor.bDeviceClass, |
| 738 | udev->descriptor.bDeviceSubClass, |
| 739 | udev->descriptor.bDeviceProtocol, |
| 740 | alt->desc.bInterfaceClass, |
| 741 | alt->desc.bInterfaceSubClass, |
| 742 | alt->desc.bInterfaceProtocol); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 743 | } |
| 744 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
| 745 | |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame^] | 746 | static ssize_t show_supports_autosuspend(struct device *dev, |
| 747 | struct device_attribute *attr, char *buf) |
| 748 | { |
| 749 | struct usb_interface *intf; |
| 750 | struct usb_device *udev; |
| 751 | int ret; |
| 752 | |
| 753 | intf = to_usb_interface(dev); |
| 754 | udev = interface_to_usbdev(intf); |
| 755 | |
| 756 | usb_lock_device(udev); |
| 757 | /* Devices will be autosuspended even when an interface isn't claimed */ |
| 758 | if (!intf->dev.driver || |
| 759 | to_usb_driver(intf->dev.driver)->supports_autosuspend) |
| 760 | ret = sprintf(buf, "%u\n", 1); |
| 761 | else |
| 762 | ret = sprintf(buf, "%u\n", 0); |
| 763 | usb_unlock_device(udev); |
| 764 | |
| 765 | return ret; |
| 766 | } |
| 767 | static DEVICE_ATTR(supports_autosuspend, S_IRUGO, show_supports_autosuspend, NULL); |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | static struct attribute *intf_attrs[] = { |
| 770 | &dev_attr_bInterfaceNumber.attr, |
| 771 | &dev_attr_bAlternateSetting.attr, |
| 772 | &dev_attr_bNumEndpoints.attr, |
| 773 | &dev_attr_bInterfaceClass.attr, |
| 774 | &dev_attr_bInterfaceSubClass.attr, |
| 775 | &dev_attr_bInterfaceProtocol.attr, |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 776 | &dev_attr_modalias.attr, |
Sarah Sharp | 49e7cc8 | 2008-10-06 14:45:46 -0700 | [diff] [blame^] | 777 | &dev_attr_supports_autosuspend.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | NULL, |
| 779 | }; |
| 780 | static struct attribute_group intf_attr_grp = { |
| 781 | .attrs = intf_attrs, |
| 782 | }; |
| 783 | |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 784 | static struct attribute *intf_assoc_attrs[] = { |
| 785 | &dev_attr_iad_bFirstInterface.attr, |
| 786 | &dev_attr_iad_bInterfaceCount.attr, |
| 787 | &dev_attr_iad_bFunctionClass.attr, |
| 788 | &dev_attr_iad_bFunctionSubClass.attr, |
| 789 | &dev_attr_iad_bFunctionProtocol.attr, |
| 790 | NULL, |
| 791 | }; |
| 792 | |
| 793 | static mode_t intf_assoc_attrs_are_visible(struct kobject *kobj, |
| 794 | struct attribute *a, int n) |
| 795 | { |
| 796 | struct usb_interface *intf = to_usb_interface( |
| 797 | container_of(kobj, struct device, kobj)); |
| 798 | |
| 799 | if (intf->intf_assoc == NULL) |
| 800 | return 0; |
| 801 | return a->mode; |
| 802 | } |
| 803 | |
| 804 | static struct attribute_group intf_assoc_attr_grp = { |
| 805 | .attrs = intf_assoc_attrs, |
| 806 | .is_visible = intf_assoc_attrs_are_visible, |
| 807 | }; |
| 808 | |
| 809 | struct attribute_group *usb_interface_groups[] = { |
| 810 | &intf_attr_grp, |
| 811 | &intf_assoc_attr_grp, |
| 812 | NULL |
| 813 | }; |
| 814 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 815 | static inline void usb_create_intf_ep_files(struct usb_interface *intf, |
| 816 | struct usb_device *udev) |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 817 | { |
| 818 | struct usb_host_interface *iface_desc; |
| 819 | int i; |
| 820 | |
| 821 | iface_desc = intf->cur_altsetting; |
| 822 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Greg Kroah-Hartman | 36679ea | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 823 | usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i], |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 824 | udev); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 827 | 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] | 828 | { |
| 829 | struct usb_host_interface *iface_desc; |
| 830 | int i; |
| 831 | |
| 832 | iface_desc = intf->cur_altsetting; |
| 833 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 834 | usb_remove_ep_files(&iface_desc->endpoint[i]); |
Greg Kroah-Hartman | 094f1649 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 837 | int usb_create_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | { |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 839 | struct usb_device *udev = interface_to_usbdev(intf); |
| 840 | struct usb_host_interface *alt = intf->cur_altsetting; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 841 | int retval; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 842 | |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 843 | if (intf->sysfs_files_created) |
| 844 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 846 | /* The interface string may be present in some altsettings |
| 847 | * and missing in others. Hence its attribute cannot be created |
| 848 | * before the uevent is broadcast. |
| 849 | */ |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 850 | if (alt->string == NULL) |
| 851 | alt->string = usb_cache_string(udev, alt->desc.iInterface); |
| 852 | if (alt->string) |
Alan Stern | 2e5f10e | 2008-04-30 15:37:19 -0400 | [diff] [blame] | 853 | retval = device_create_file(&intf->dev, &dev_attr_interface); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 854 | usb_create_intf_ep_files(intf, udev); |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 855 | intf->sysfs_files_created = 1; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 856 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | } |
| 858 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 859 | void usb_remove_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 861 | struct device *dev = &intf->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 863 | if (!intf->sysfs_files_created) |
| 864 | return; |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 865 | usb_remove_intf_ep_files(intf); |
| 866 | device_remove_file(dev, &dev_attr_interface); |
Alan Stern | 7e61559 | 2007-11-06 11:43:42 -0500 | [diff] [blame] | 867 | intf->sysfs_files_created = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | } |