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 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 172 | #ifdef CONFIG_USB_SUSPEND |
| 173 | |
| 174 | static ssize_t |
| 175 | show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf) |
| 176 | { |
| 177 | struct usb_device *udev = to_usb_device(dev); |
| 178 | |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 179 | return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static ssize_t |
| 183 | set_autosuspend(struct device *dev, struct device_attribute *attr, |
| 184 | const char *buf, size_t count) |
| 185 | { |
| 186 | struct usb_device *udev = to_usb_device(dev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 187 | int value; |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 188 | |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 189 | if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ || |
| 190 | value <= - INT_MAX/HZ) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 191 | return -EINVAL; |
| 192 | value *= HZ; |
| 193 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 194 | udev->autosuspend_delay = value; |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 195 | if (value >= 0) |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 196 | usb_try_autosuspend_device(udev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 197 | else { |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 198 | if (usb_autoresume_device(udev) == 0) |
| 199 | usb_autosuspend_device(udev); |
Alan Stern | eaafbc3 | 2007-03-13 16:39:15 -0400 | [diff] [blame] | 200 | } |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 201 | return count; |
| 202 | } |
| 203 | |
| 204 | static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR, |
| 205 | show_autosuspend, set_autosuspend); |
| 206 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 207 | static const char on_string[] = "on"; |
| 208 | static const char auto_string[] = "auto"; |
| 209 | static const char suspend_string[] = "suspend"; |
| 210 | |
| 211 | static ssize_t |
| 212 | show_level(struct device *dev, struct device_attribute *attr, char *buf) |
| 213 | { |
| 214 | struct usb_device *udev = to_usb_device(dev); |
| 215 | const char *p = auto_string; |
| 216 | |
| 217 | if (udev->state == USB_STATE_SUSPENDED) { |
| 218 | if (udev->autoresume_disabled) |
| 219 | p = suspend_string; |
| 220 | } else { |
| 221 | if (udev->autosuspend_disabled) |
| 222 | p = on_string; |
| 223 | } |
| 224 | return sprintf(buf, "%s\n", p); |
| 225 | } |
| 226 | |
| 227 | static ssize_t |
| 228 | set_level(struct device *dev, struct device_attribute *attr, |
| 229 | const char *buf, size_t count) |
| 230 | { |
| 231 | struct usb_device *udev = to_usb_device(dev); |
| 232 | int len = count; |
| 233 | char *cp; |
| 234 | int rc = 0; |
| 235 | |
| 236 | cp = memchr(buf, '\n', count); |
| 237 | if (cp) |
| 238 | len = cp - buf; |
| 239 | |
| 240 | usb_lock_device(udev); |
| 241 | |
| 242 | /* Setting the flags without calling usb_pm_lock is a subject to |
| 243 | * races, but who cares... |
| 244 | */ |
| 245 | if (len == sizeof on_string - 1 && |
| 246 | strncmp(buf, on_string, len) == 0) { |
| 247 | udev->autosuspend_disabled = 1; |
| 248 | udev->autoresume_disabled = 0; |
| 249 | rc = usb_external_resume_device(udev); |
| 250 | |
| 251 | } else if (len == sizeof auto_string - 1 && |
| 252 | strncmp(buf, auto_string, len) == 0) { |
| 253 | udev->autosuspend_disabled = 0; |
| 254 | udev->autoresume_disabled = 0; |
| 255 | rc = usb_external_resume_device(udev); |
| 256 | |
| 257 | } else if (len == sizeof suspend_string - 1 && |
| 258 | strncmp(buf, suspend_string, len) == 0) { |
| 259 | udev->autosuspend_disabled = 0; |
| 260 | udev->autoresume_disabled = 1; |
| 261 | rc = usb_external_suspend_device(udev, PMSG_SUSPEND); |
| 262 | |
| 263 | } else |
| 264 | rc = -EINVAL; |
| 265 | |
| 266 | usb_unlock_device(udev); |
| 267 | return (rc < 0 ? rc : count); |
| 268 | } |
| 269 | |
| 270 | static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level); |
| 271 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 272 | static char power_group[] = "power"; |
| 273 | |
| 274 | static int add_power_attributes(struct device *dev) |
| 275 | { |
| 276 | int rc = 0; |
| 277 | |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 278 | if (is_usb_device(dev)) { |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 279 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 280 | &dev_attr_autosuspend.attr, |
| 281 | power_group); |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 282 | if (rc == 0) |
| 283 | rc = sysfs_add_file_to_group(&dev->kobj, |
| 284 | &dev_attr_level.attr, |
| 285 | power_group); |
| 286 | } |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 287 | return rc; |
| 288 | } |
| 289 | |
| 290 | static void remove_power_attributes(struct device *dev) |
| 291 | { |
| 292 | sysfs_remove_file_from_group(&dev->kobj, |
Alan Stern | 2add522 | 2007-03-20 14:59:39 -0400 | [diff] [blame] | 293 | &dev_attr_level.attr, |
| 294 | power_group); |
| 295 | sysfs_remove_file_from_group(&dev->kobj, |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 296 | &dev_attr_autosuspend.attr, |
| 297 | power_group); |
| 298 | } |
| 299 | |
| 300 | #else |
| 301 | |
| 302 | #define add_power_attributes(dev) 0 |
| 303 | #define remove_power_attributes(dev) do {} while (0) |
| 304 | |
| 305 | #endif /* CONFIG_USB_SUSPEND */ |
| 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | /* Descriptor fields */ |
| 308 | #define usb_descriptor_attr_le16(field, format_string) \ |
| 309 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 310 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 311 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | { \ |
| 313 | struct usb_device *udev; \ |
| 314 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 315 | udev = to_usb_device(dev); \ |
| 316 | return sprintf(buf, format_string, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | le16_to_cpu(udev->descriptor.field)); \ |
| 318 | } \ |
| 319 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 320 | |
| 321 | usb_descriptor_attr_le16(idVendor, "%04x\n") |
| 322 | usb_descriptor_attr_le16(idProduct, "%04x\n") |
| 323 | usb_descriptor_attr_le16(bcdDevice, "%04x\n") |
| 324 | |
| 325 | #define usb_descriptor_attr(field, format_string) \ |
| 326 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 327 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 328 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | { \ |
| 330 | struct usb_device *udev; \ |
| 331 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 332 | udev = to_usb_device(dev); \ |
| 333 | return sprintf(buf, format_string, udev->descriptor.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | } \ |
| 335 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 336 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 337 | usb_descriptor_attr(bDeviceClass, "%02x\n") |
| 338 | usb_descriptor_attr(bDeviceSubClass, "%02x\n") |
| 339 | usb_descriptor_attr(bDeviceProtocol, "%02x\n") |
| 340 | usb_descriptor_attr(bNumConfigurations, "%d\n") |
| 341 | usb_descriptor_attr(bMaxPacketSize0, "%d\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | |
| 343 | static struct attribute *dev_attrs[] = { |
| 344 | /* current configuration's attributes */ |
Alan Stern | b6eb2d8 | 2006-07-06 15:37:42 -0400 | [diff] [blame] | 345 | &dev_attr_configuration.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | &dev_attr_bNumInterfaces.attr, |
| 347 | &dev_attr_bConfigurationValue.attr, |
| 348 | &dev_attr_bmAttributes.attr, |
| 349 | &dev_attr_bMaxPower.attr, |
| 350 | /* device attributes */ |
| 351 | &dev_attr_idVendor.attr, |
| 352 | &dev_attr_idProduct.attr, |
| 353 | &dev_attr_bcdDevice.attr, |
| 354 | &dev_attr_bDeviceClass.attr, |
| 355 | &dev_attr_bDeviceSubClass.attr, |
| 356 | &dev_attr_bDeviceProtocol.attr, |
| 357 | &dev_attr_bNumConfigurations.attr, |
Greg Kroah-Hartman | cf5910b | 2005-06-29 16:53:29 -0700 | [diff] [blame] | 358 | &dev_attr_bMaxPacketSize0.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | &dev_attr_speed.attr, |
Alan Stern | 83f7d95 | 2007-04-25 15:15:43 -0400 | [diff] [blame] | 360 | &dev_attr_busnum.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | &dev_attr_devnum.attr, |
| 362 | &dev_attr_version.attr, |
| 363 | &dev_attr_maxchild.attr, |
Oliver Neukum | 7ceec1f | 2007-01-26 14:26:21 +0100 | [diff] [blame] | 364 | &dev_attr_quirks.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | NULL, |
| 366 | }; |
| 367 | static struct attribute_group dev_attr_grp = { |
| 368 | .attrs = dev_attrs, |
| 369 | }; |
| 370 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 371 | int usb_create_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 373 | struct device *dev = &udev->dev; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 374 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 376 | retval = sysfs_create_group(&dev->kobj, &dev_attr_grp); |
| 377 | if (retval) |
| 378 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 380 | retval = add_power_attributes(dev); |
| 381 | if (retval) |
| 382 | goto error; |
| 383 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 384 | if (udev->manufacturer) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 385 | retval = device_create_file(dev, &dev_attr_manufacturer); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 386 | if (retval) |
| 387 | goto error; |
| 388 | } |
| 389 | if (udev->product) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 390 | retval = device_create_file(dev, &dev_attr_product); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 391 | if (retval) |
| 392 | goto error; |
| 393 | } |
| 394 | if (udev->serial) { |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 395 | retval = device_create_file(dev, &dev_attr_serial); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 396 | if (retval) |
| 397 | goto error; |
| 398 | } |
| 399 | retval = usb_create_ep_files(dev, &udev->ep0, udev); |
| 400 | if (retval) |
| 401 | goto error; |
| 402 | return 0; |
| 403 | error: |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 404 | usb_remove_sysfs_dev_files(udev); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 405 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 408 | void usb_remove_sysfs_dev_files(struct usb_device *udev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | { |
| 410 | struct device *dev = &udev->dev; |
| 411 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 412 | usb_remove_ep_files(&udev->ep0); |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 413 | device_remove_file(dev, &dev_attr_manufacturer); |
| 414 | device_remove_file(dev, &dev_attr_product); |
| 415 | device_remove_file(dev, &dev_attr_serial); |
Alan Stern | 19c2623 | 2007-02-20 15:03:32 -0500 | [diff] [blame] | 416 | remove_power_attributes(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | sysfs_remove_group(&dev->kobj, &dev_attr_grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Interface fields */ |
| 421 | #define usb_intf_attr(field, format_string) \ |
| 422 | static ssize_t \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 423 | show_##field(struct device *dev, struct device_attribute *attr, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 424 | char *buf) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | { \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 426 | struct usb_interface *intf = to_usb_interface(dev); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | \ |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 428 | return sprintf(buf, format_string, \ |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 429 | intf->cur_altsetting->desc.field); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } \ |
| 431 | static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL); |
| 432 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 433 | usb_intf_attr(bInterfaceNumber, "%02x\n") |
| 434 | usb_intf_attr(bAlternateSetting, "%2d\n") |
| 435 | usb_intf_attr(bNumEndpoints, "%02x\n") |
| 436 | usb_intf_attr(bInterfaceClass, "%02x\n") |
| 437 | usb_intf_attr(bInterfaceSubClass, "%02x\n") |
| 438 | usb_intf_attr(bInterfaceProtocol, "%02x\n") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 440 | static ssize_t show_interface_string(struct device *dev, |
| 441 | struct device_attribute *attr, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
| 443 | struct usb_interface *intf; |
| 444 | struct usb_device *udev; |
| 445 | int len; |
| 446 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 447 | intf = to_usb_interface(dev); |
| 448 | udev = interface_to_usbdev(intf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | len = snprintf(buf, 256, "%s", intf->cur_altsetting->string); |
| 450 | if (len < 0) |
| 451 | return 0; |
| 452 | buf[len] = '\n'; |
| 453 | buf[len+1] = 0; |
| 454 | return len+1; |
| 455 | } |
| 456 | static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL); |
| 457 | |
Alan Stern | b724ae7 | 2005-10-24 15:36:00 -0400 | [diff] [blame] | 458 | static ssize_t show_modalias(struct device *dev, |
| 459 | struct device_attribute *attr, char *buf) |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 460 | { |
| 461 | struct usb_interface *intf; |
| 462 | struct usb_device *udev; |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 463 | struct usb_host_interface *alt; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 464 | |
| 465 | intf = to_usb_interface(dev); |
| 466 | udev = interface_to_usbdev(intf); |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 467 | alt = intf->cur_altsetting; |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 468 | |
Greg Kroah-Hartman | 7521803 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 469 | return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X" |
| 470 | "ic%02Xisc%02Xip%02X\n", |
| 471 | le16_to_cpu(udev->descriptor.idVendor), |
| 472 | le16_to_cpu(udev->descriptor.idProduct), |
| 473 | le16_to_cpu(udev->descriptor.bcdDevice), |
| 474 | udev->descriptor.bDeviceClass, |
| 475 | udev->descriptor.bDeviceSubClass, |
| 476 | udev->descriptor.bDeviceProtocol, |
| 477 | alt->desc.bInterfaceClass, |
| 478 | alt->desc.bInterfaceSubClass, |
| 479 | alt->desc.bInterfaceProtocol); |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 480 | } |
| 481 | static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL); |
| 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | static struct attribute *intf_attrs[] = { |
| 484 | &dev_attr_bInterfaceNumber.attr, |
| 485 | &dev_attr_bAlternateSetting.attr, |
| 486 | &dev_attr_bNumEndpoints.attr, |
| 487 | &dev_attr_bInterfaceClass.attr, |
| 488 | &dev_attr_bInterfaceSubClass.attr, |
| 489 | &dev_attr_bInterfaceProtocol.attr, |
Greg KH | 360b52b | 2005-05-10 06:45:10 -0700 | [diff] [blame] | 490 | &dev_attr_modalias.attr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | NULL, |
| 492 | }; |
| 493 | static struct attribute_group intf_attr_grp = { |
| 494 | .attrs = intf_attrs, |
| 495 | }; |
| 496 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 497 | static inline void usb_create_intf_ep_files(struct usb_interface *intf, |
| 498 | struct usb_device *udev) |
Greg Kroah-Hartman | 094f164 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 499 | { |
| 500 | struct usb_host_interface *iface_desc; |
| 501 | int i; |
| 502 | |
| 503 | iface_desc = intf->cur_altsetting; |
| 504 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Greg Kroah-Hartman | 36679ea | 2006-06-14 12:14:34 -0700 | [diff] [blame] | 505 | usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i], |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 506 | udev); |
Greg Kroah-Hartman | 094f164 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 509 | static inline void usb_remove_intf_ep_files(struct usb_interface *intf) |
Greg Kroah-Hartman | 094f164 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 510 | { |
| 511 | struct usb_host_interface *iface_desc; |
| 512 | int i; |
| 513 | |
| 514 | iface_desc = intf->cur_altsetting; |
| 515 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) |
Alan Stern | be69e5b | 2005-10-25 15:56:06 -0400 | [diff] [blame] | 516 | usb_remove_ep_files(&iface_desc->endpoint[i]); |
Greg Kroah-Hartman | 094f164 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 517 | } |
| 518 | |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 519 | int usb_create_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 521 | struct device *dev = &intf->dev; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 522 | struct usb_device *udev = interface_to_usbdev(intf); |
| 523 | struct usb_host_interface *alt = intf->cur_altsetting; |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 524 | int retval; |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 525 | |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 526 | retval = sysfs_create_group(&dev->kobj, &intf_attr_grp); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 527 | if (retval) |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 528 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 530 | if (alt->string == NULL) |
| 531 | alt->string = usb_cache_string(udev, alt->desc.iInterface); |
| 532 | if (alt->string) |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 533 | retval = device_create_file(dev, &dev_attr_interface); |
Alan Stern | 4f62efe | 2005-10-24 16:24:14 -0400 | [diff] [blame] | 534 | usb_create_intf_ep_files(intf, udev); |
Greg Kroah-Hartman | 1b21d5e | 2006-08-28 11:43:25 -0700 | [diff] [blame] | 535 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Oliver Neukum | 9251644 | 2007-01-23 15:55:28 -0500 | [diff] [blame] | 538 | void usb_remove_sysfs_intf_files(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | { |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 540 | struct device *dev = &intf->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
Alan Stern | aa084f3 | 2007-02-20 14:59:59 -0500 | [diff] [blame] | 542 | usb_remove_intf_ep_files(intf); |
| 543 | device_remove_file(dev, &dev_attr_interface); |
| 544 | sysfs_remove_group(&dev->kobj, &intf_attr_grp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | } |