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