1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * drivers/usb/core/sysfs.c
4 *
5 * (C) Copyright 2002 David Brownell
6 * (C) Copyright 2002,2004 Greg Kroah-Hartman
7 * (C) Copyright 2002,2004 IBM Corp.
8 *
9 * All of the sysfs file attributes for usb devices and interfaces.
10 *
11 * Released under the GPLv2 only.
12 */
13
14
15 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/usb.h>
18 #include <linux/usb/hcd.h>
19 #include <linux/usb/quirks.h>
20 #include <linux/of.h>
21 #include "usb.h"
22
23 /* Active configuration fields */
24 #define usb_actconfig_show(field, format_string) \
25 static ssize_t field##_show(struct device *dev, \
26 struct device_attribute *attr, char *buf) \
27 { \
28 struct usb_device *udev; \
29 struct usb_host_config *actconfig; \
30 ssize_t rc; \
31 \
32 udev = to_usb_device(dev); \
33 rc = usb_lock_device_interruptible(udev); \
34 if (rc < 0) \
35 return -EINTR; \
36 actconfig = udev->actconfig; \
37 if (actconfig) \
38 rc = sprintf(buf, format_string, \
39 actconfig->desc.field); \
40 usb_unlock_device(udev); \
41 return rc; \
42 } \
43
44 #define usb_actconfig_attr(field, format_string) \
45 usb_actconfig_show(field, format_string) \
46 static DEVICE_ATTR_RO(field)
47
48 usb_actconfig_attr(bNumInterfaces, "%2d\n");
49 usb_actconfig_attr(bmAttributes, "%2x\n");
50
bMaxPower_show(struct device * dev,struct device_attribute * attr,char * buf)51 static ssize_t bMaxPower_show(struct device *dev,
52 struct device_attribute *attr, char *buf)
53 {
54 struct usb_device *udev;
55 struct usb_host_config *actconfig;
56 ssize_t rc;
57
58 udev = to_usb_device(dev);
59 rc = usb_lock_device_interruptible(udev);
60 if (rc < 0)
61 return -EINTR;
62 actconfig = udev->actconfig;
63 if (actconfig)
64 rc = sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
65 usb_unlock_device(udev);
66 return rc;
67 }
68 static DEVICE_ATTR_RO(bMaxPower);
69
configuration_show(struct device * dev,struct device_attribute * attr,char * buf)70 static ssize_t configuration_show(struct device *dev,
71 struct device_attribute *attr, char *buf)
72 {
73 struct usb_device *udev;
74 struct usb_host_config *actconfig;
75 ssize_t rc;
76
77 udev = to_usb_device(dev);
78 rc = usb_lock_device_interruptible(udev);
79 if (rc < 0)
80 return -EINTR;
81 actconfig = udev->actconfig;
82 if (actconfig && actconfig->string)
83 rc = sprintf(buf, "%s\n", actconfig->string);
84 usb_unlock_device(udev);
85 return rc;
86 }
87 static DEVICE_ATTR_RO(configuration);
88
89 /* configuration value is always present, and r/w */
90 usb_actconfig_show(bConfigurationValue, "%u\n");
91
bConfigurationValue_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)92 static ssize_t bConfigurationValue_store(struct device *dev,
93 struct device_attribute *attr,
94 const char *buf, size_t count)
95 {
96 struct usb_device *udev = to_usb_device(dev);
97 int config, value, rc;
98
99 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
100 return -EINVAL;
101 rc = usb_lock_device_interruptible(udev);
102 if (rc < 0)
103 return -EINTR;
104 value = usb_set_configuration(udev, config);
105 usb_unlock_device(udev);
106 return (value < 0) ? value : count;
107 }
108 static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
109 bConfigurationValue_show, bConfigurationValue_store);
110
111 #ifdef CONFIG_OF
devspec_show(struct device * dev,struct device_attribute * attr,char * buf)112 static ssize_t devspec_show(struct device *dev, struct device_attribute *attr,
113 char *buf)
114 {
115 struct device_node *of_node = dev->of_node;
116
117 return sprintf(buf, "%pOF\n", of_node);
118 }
119 static DEVICE_ATTR_RO(devspec);
120 #endif
121
122 /* String fields */
123 #define usb_string_attr(name) \
124 static ssize_t name##_show(struct device *dev, \
125 struct device_attribute *attr, char *buf) \
126 { \
127 struct usb_device *udev; \
128 int retval; \
129 \
130 udev = to_usb_device(dev); \
131 retval = usb_lock_device_interruptible(udev); \
132 if (retval < 0) \
133 return -EINTR; \
134 retval = sprintf(buf, "%s\n", udev->name); \
135 usb_unlock_device(udev); \
136 return retval; \
137 } \
138 static DEVICE_ATTR_RO(name)
139
140 usb_string_attr(product);
141 usb_string_attr(manufacturer);
142 usb_string_attr(serial);
143
speed_show(struct device * dev,struct device_attribute * attr,char * buf)144 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
145 char *buf)
146 {
147 struct usb_device *udev;
148 char *speed;
149
150 udev = to_usb_device(dev);
151
152 switch (udev->speed) {
153 case USB_SPEED_LOW:
154 speed = "1.5";
155 break;
156 case USB_SPEED_UNKNOWN:
157 case USB_SPEED_FULL:
158 speed = "12";
159 break;
160 case USB_SPEED_HIGH:
161 speed = "480";
162 break;
163 case USB_SPEED_WIRELESS:
164 speed = "480";
165 break;
166 case USB_SPEED_SUPER:
167 speed = "5000";
168 break;
169 case USB_SPEED_SUPER_PLUS:
170 if (udev->ssp_rate == USB_SSP_GEN_2x2)
171 speed = "20000";
172 else
173 speed = "10000";
174 break;
175 default:
176 speed = "unknown";
177 }
178 return sprintf(buf, "%s\n", speed);
179 }
180 static DEVICE_ATTR_RO(speed);
181
rx_lanes_show(struct device * dev,struct device_attribute * attr,char * buf)182 static ssize_t rx_lanes_show(struct device *dev, struct device_attribute *attr,
183 char *buf)
184 {
185 struct usb_device *udev;
186
187 udev = to_usb_device(dev);
188 return sprintf(buf, "%d\n", udev->rx_lanes);
189 }
190 static DEVICE_ATTR_RO(rx_lanes);
191
tx_lanes_show(struct device * dev,struct device_attribute * attr,char * buf)192 static ssize_t tx_lanes_show(struct device *dev, struct device_attribute *attr,
193 char *buf)
194 {
195 struct usb_device *udev;
196
197 udev = to_usb_device(dev);
198 return sprintf(buf, "%d\n", udev->tx_lanes);
199 }
200 static DEVICE_ATTR_RO(tx_lanes);
201
busnum_show(struct device * dev,struct device_attribute * attr,char * buf)202 static ssize_t busnum_show(struct device *dev, struct device_attribute *attr,
203 char *buf)
204 {
205 struct usb_device *udev;
206
207 udev = to_usb_device(dev);
208 return sprintf(buf, "%d\n", udev->bus->busnum);
209 }
210 static DEVICE_ATTR_RO(busnum);
211
devnum_show(struct device * dev,struct device_attribute * attr,char * buf)212 static ssize_t devnum_show(struct device *dev, struct device_attribute *attr,
213 char *buf)
214 {
215 struct usb_device *udev;
216
217 udev = to_usb_device(dev);
218 return sprintf(buf, "%d\n", udev->devnum);
219 }
220 static DEVICE_ATTR_RO(devnum);
221
devpath_show(struct device * dev,struct device_attribute * attr,char * buf)222 static ssize_t devpath_show(struct device *dev, struct device_attribute *attr,
223 char *buf)
224 {
225 struct usb_device *udev;
226
227 udev = to_usb_device(dev);
228 return sprintf(buf, "%s\n", udev->devpath);
229 }
230 static DEVICE_ATTR_RO(devpath);
231
version_show(struct device * dev,struct device_attribute * attr,char * buf)232 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
233 char *buf)
234 {
235 struct usb_device *udev;
236 u16 bcdUSB;
237
238 udev = to_usb_device(dev);
239 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
240 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
241 }
242 static DEVICE_ATTR_RO(version);
243
maxchild_show(struct device * dev,struct device_attribute * attr,char * buf)244 static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr,
245 char *buf)
246 {
247 struct usb_device *udev;
248
249 udev = to_usb_device(dev);
250 return sprintf(buf, "%d\n", udev->maxchild);
251 }
252 static DEVICE_ATTR_RO(maxchild);
253
quirks_show(struct device * dev,struct device_attribute * attr,char * buf)254 static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
255 char *buf)
256 {
257 struct usb_device *udev;
258
259 udev = to_usb_device(dev);
260 return sprintf(buf, "0x%x\n", udev->quirks);
261 }
262 static DEVICE_ATTR_RO(quirks);
263
avoid_reset_quirk_show(struct device * dev,struct device_attribute * attr,char * buf)264 static ssize_t avoid_reset_quirk_show(struct device *dev,
265 struct device_attribute *attr, char *buf)
266 {
267 struct usb_device *udev;
268
269 udev = to_usb_device(dev);
270 return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
271 }
272
avoid_reset_quirk_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)273 static ssize_t avoid_reset_quirk_store(struct device *dev,
274 struct device_attribute *attr,
275 const char *buf, size_t count)
276 {
277 struct usb_device *udev = to_usb_device(dev);
278 int val, rc;
279
280 if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
281 return -EINVAL;
282 rc = usb_lock_device_interruptible(udev);
283 if (rc < 0)
284 return -EINTR;
285 if (val)
286 udev->quirks |= USB_QUIRK_RESET;
287 else
288 udev->quirks &= ~USB_QUIRK_RESET;
289 usb_unlock_device(udev);
290 return count;
291 }
292 static DEVICE_ATTR_RW(avoid_reset_quirk);
293
urbnum_show(struct device * dev,struct device_attribute * attr,char * buf)294 static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr,
295 char *buf)
296 {
297 struct usb_device *udev;
298
299 udev = to_usb_device(dev);
300 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
301 }
302 static DEVICE_ATTR_RO(urbnum);
303
removable_show(struct device * dev,struct device_attribute * attr,char * buf)304 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
305 char *buf)
306 {
307 struct usb_device *udev;
308 char *state;
309
310 udev = to_usb_device(dev);
311
312 switch (udev->removable) {
313 case USB_DEVICE_REMOVABLE:
314 state = "removable";
315 break;
316 case USB_DEVICE_FIXED:
317 state = "fixed";
318 break;
319 default:
320 state = "unknown";
321 }
322
323 return sprintf(buf, "%s\n", state);
324 }
325 static DEVICE_ATTR_RO(removable);
326
ltm_capable_show(struct device * dev,struct device_attribute * attr,char * buf)327 static ssize_t ltm_capable_show(struct device *dev,
328 struct device_attribute *attr, char *buf)
329 {
330 if (usb_device_supports_ltm(to_usb_device(dev)))
331 return sprintf(buf, "%s\n", "yes");
332 return sprintf(buf, "%s\n", "no");
333 }
334 static DEVICE_ATTR_RO(ltm_capable);
335
336 #ifdef CONFIG_PM
337
persist_show(struct device * dev,struct device_attribute * attr,char * buf)338 static ssize_t persist_show(struct device *dev, struct device_attribute *attr,
339 char *buf)
340 {
341 struct usb_device *udev = to_usb_device(dev);
342
343 return sprintf(buf, "%d\n", udev->persist_enabled);
344 }
345
persist_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)346 static ssize_t persist_store(struct device *dev, struct device_attribute *attr,
347 const char *buf, size_t count)
348 {
349 struct usb_device *udev = to_usb_device(dev);
350 int value, rc;
351
352 /* Hubs are always enabled for USB_PERSIST */
353 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
354 return -EPERM;
355
356 if (sscanf(buf, "%d", &value) != 1)
357 return -EINVAL;
358
359 rc = usb_lock_device_interruptible(udev);
360 if (rc < 0)
361 return -EINTR;
362 udev->persist_enabled = !!value;
363 usb_unlock_device(udev);
364 return count;
365 }
366 static DEVICE_ATTR_RW(persist);
367
add_persist_attributes(struct device * dev)368 static int add_persist_attributes(struct device *dev)
369 {
370 int rc = 0;
371
372 if (is_usb_device(dev)) {
373 struct usb_device *udev = to_usb_device(dev);
374
375 /* Hubs are automatically enabled for USB_PERSIST,
376 * no point in creating the attribute file.
377 */
378 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
379 rc = sysfs_add_file_to_group(&dev->kobj,
380 &dev_attr_persist.attr,
381 power_group_name);
382 }
383 return rc;
384 }
385
remove_persist_attributes(struct device * dev)386 static void remove_persist_attributes(struct device *dev)
387 {
388 sysfs_remove_file_from_group(&dev->kobj,
389 &dev_attr_persist.attr,
390 power_group_name);
391 }
392
connected_duration_show(struct device * dev,struct device_attribute * attr,char * buf)393 static ssize_t connected_duration_show(struct device *dev,
394 struct device_attribute *attr, char *buf)
395 {
396 struct usb_device *udev = to_usb_device(dev);
397
398 return sprintf(buf, "%u\n",
399 jiffies_to_msecs(jiffies - udev->connect_time));
400 }
401 static DEVICE_ATTR_RO(connected_duration);
402
403 /*
404 * If the device is resumed, the last time the device was suspended has
405 * been pre-subtracted from active_duration. We add the current time to
406 * get the duration that the device was actually active.
407 *
408 * If the device is suspended, the active_duration is up-to-date.
409 */
active_duration_show(struct device * dev,struct device_attribute * attr,char * buf)410 static ssize_t active_duration_show(struct device *dev,
411 struct device_attribute *attr, char *buf)
412 {
413 struct usb_device *udev = to_usb_device(dev);
414 int duration;
415
416 if (udev->state != USB_STATE_SUSPENDED)
417 duration = jiffies_to_msecs(jiffies + udev->active_duration);
418 else
419 duration = jiffies_to_msecs(udev->active_duration);
420 return sprintf(buf, "%u\n", duration);
421 }
422 static DEVICE_ATTR_RO(active_duration);
423
autosuspend_show(struct device * dev,struct device_attribute * attr,char * buf)424 static ssize_t autosuspend_show(struct device *dev,
425 struct device_attribute *attr, char *buf)
426 {
427 return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
428 }
429
autosuspend_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)430 static ssize_t autosuspend_store(struct device *dev,
431 struct device_attribute *attr, const char *buf,
432 size_t count)
433 {
434 int value;
435
436 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
437 value <= -INT_MAX/1000)
438 return -EINVAL;
439
440 pm_runtime_set_autosuspend_delay(dev, value * 1000);
441 return count;
442 }
443 static DEVICE_ATTR_RW(autosuspend);
444
445 static const char on_string[] = "on";
446 static const char auto_string[] = "auto";
447
warn_level(void)448 static void warn_level(void)
449 {
450 static int level_warned;
451
452 if (!level_warned) {
453 level_warned = 1;
454 printk(KERN_WARNING "WARNING! power/level is deprecated; "
455 "use power/control instead\n");
456 }
457 }
458
level_show(struct device * dev,struct device_attribute * attr,char * buf)459 static ssize_t level_show(struct device *dev, struct device_attribute *attr,
460 char *buf)
461 {
462 struct usb_device *udev = to_usb_device(dev);
463 const char *p = auto_string;
464
465 warn_level();
466 if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
467 p = on_string;
468 return sprintf(buf, "%s\n", p);
469 }
470
level_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)471 static ssize_t level_store(struct device *dev, struct device_attribute *attr,
472 const char *buf, size_t count)
473 {
474 struct usb_device *udev = to_usb_device(dev);
475 int len = count;
476 char *cp;
477 int rc = count;
478 int rv;
479
480 warn_level();
481 cp = memchr(buf, '\n', count);
482 if (cp)
483 len = cp - buf;
484
485 rv = usb_lock_device_interruptible(udev);
486 if (rv < 0)
487 return -EINTR;
488
489 if (len == sizeof on_string - 1 &&
490 strncmp(buf, on_string, len) == 0)
491 usb_disable_autosuspend(udev);
492
493 else if (len == sizeof auto_string - 1 &&
494 strncmp(buf, auto_string, len) == 0)
495 usb_enable_autosuspend(udev);
496
497 else
498 rc = -EINVAL;
499
500 usb_unlock_device(udev);
501 return rc;
502 }
503 static DEVICE_ATTR_RW(level);
504
usb2_hardware_lpm_show(struct device * dev,struct device_attribute * attr,char * buf)505 static ssize_t usb2_hardware_lpm_show(struct device *dev,
506 struct device_attribute *attr, char *buf)
507 {
508 struct usb_device *udev = to_usb_device(dev);
509 const char *p;
510
511 if (udev->usb2_hw_lpm_allowed == 1)
512 p = "enabled";
513 else
514 p = "disabled";
515
516 return sprintf(buf, "%s\n", p);
517 }
518
usb2_hardware_lpm_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)519 static ssize_t usb2_hardware_lpm_store(struct device *dev,
520 struct device_attribute *attr,
521 const char *buf, size_t count)
522 {
523 struct usb_device *udev = to_usb_device(dev);
524 bool value;
525 int ret;
526
527 ret = usb_lock_device_interruptible(udev);
528 if (ret < 0)
529 return -EINTR;
530
531 ret = strtobool(buf, &value);
532
533 if (!ret) {
534 udev->usb2_hw_lpm_allowed = value;
535 if (value)
536 ret = usb_enable_usb2_hardware_lpm(udev);
537 else
538 ret = usb_disable_usb2_hardware_lpm(udev);
539 }
540
541 usb_unlock_device(udev);
542
543 if (!ret)
544 return count;
545
546 return ret;
547 }
548 static DEVICE_ATTR_RW(usb2_hardware_lpm);
549
usb2_lpm_l1_timeout_show(struct device * dev,struct device_attribute * attr,char * buf)550 static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
551 struct device_attribute *attr,
552 char *buf)
553 {
554 struct usb_device *udev = to_usb_device(dev);
555 return sprintf(buf, "%d\n", udev->l1_params.timeout);
556 }
557
usb2_lpm_l1_timeout_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)558 static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
559 struct device_attribute *attr,
560 const char *buf, size_t count)
561 {
562 struct usb_device *udev = to_usb_device(dev);
563 u16 timeout;
564
565 if (kstrtou16(buf, 0, &timeout))
566 return -EINVAL;
567
568 udev->l1_params.timeout = timeout;
569
570 return count;
571 }
572 static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
573
usb2_lpm_besl_show(struct device * dev,struct device_attribute * attr,char * buf)574 static ssize_t usb2_lpm_besl_show(struct device *dev,
575 struct device_attribute *attr, char *buf)
576 {
577 struct usb_device *udev = to_usb_device(dev);
578 return sprintf(buf, "%d\n", udev->l1_params.besl);
579 }
580
usb2_lpm_besl_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)581 static ssize_t usb2_lpm_besl_store(struct device *dev,
582 struct device_attribute *attr,
583 const char *buf, size_t count)
584 {
585 struct usb_device *udev = to_usb_device(dev);
586 u8 besl;
587
588 if (kstrtou8(buf, 0, &besl) || besl > 15)
589 return -EINVAL;
590
591 udev->l1_params.besl = besl;
592
593 return count;
594 }
595 static DEVICE_ATTR_RW(usb2_lpm_besl);
596
usb3_hardware_lpm_u1_show(struct device * dev,struct device_attribute * attr,char * buf)597 static ssize_t usb3_hardware_lpm_u1_show(struct device *dev,
598 struct device_attribute *attr, char *buf)
599 {
600 struct usb_device *udev = to_usb_device(dev);
601 const char *p;
602 int rc;
603
604 rc = usb_lock_device_interruptible(udev);
605 if (rc < 0)
606 return -EINTR;
607
608 if (udev->usb3_lpm_u1_enabled)
609 p = "enabled";
610 else
611 p = "disabled";
612
613 usb_unlock_device(udev);
614
615 return sprintf(buf, "%s\n", p);
616 }
617 static DEVICE_ATTR_RO(usb3_hardware_lpm_u1);
618
usb3_hardware_lpm_u2_show(struct device * dev,struct device_attribute * attr,char * buf)619 static ssize_t usb3_hardware_lpm_u2_show(struct device *dev,
620 struct device_attribute *attr, char *buf)
621 {
622 struct usb_device *udev = to_usb_device(dev);
623 const char *p;
624 int rc;
625
626 rc = usb_lock_device_interruptible(udev);
627 if (rc < 0)
628 return -EINTR;
629
630 if (udev->usb3_lpm_u2_enabled)
631 p = "enabled";
632 else
633 p = "disabled";
634
635 usb_unlock_device(udev);
636
637 return sprintf(buf, "%s\n", p);
638 }
639 static DEVICE_ATTR_RO(usb3_hardware_lpm_u2);
640
641 static struct attribute *usb2_hardware_lpm_attr[] = {
642 &dev_attr_usb2_hardware_lpm.attr,
643 &dev_attr_usb2_lpm_l1_timeout.attr,
644 &dev_attr_usb2_lpm_besl.attr,
645 NULL,
646 };
647 static const struct attribute_group usb2_hardware_lpm_attr_group = {
648 .name = power_group_name,
649 .attrs = usb2_hardware_lpm_attr,
650 };
651
652 static struct attribute *usb3_hardware_lpm_attr[] = {
653 &dev_attr_usb3_hardware_lpm_u1.attr,
654 &dev_attr_usb3_hardware_lpm_u2.attr,
655 NULL,
656 };
657 static const struct attribute_group usb3_hardware_lpm_attr_group = {
658 .name = power_group_name,
659 .attrs = usb3_hardware_lpm_attr,
660 };
661
662 static struct attribute *power_attrs[] = {
663 &dev_attr_autosuspend.attr,
664 &dev_attr_level.attr,
665 &dev_attr_connected_duration.attr,
666 &dev_attr_active_duration.attr,
667 NULL,
668 };
669 static const struct attribute_group power_attr_group = {
670 .name = power_group_name,
671 .attrs = power_attrs,
672 };
673
add_power_attributes(struct device * dev)674 static int add_power_attributes(struct device *dev)
675 {
676 int rc = 0;
677
678 if (is_usb_device(dev)) {
679 struct usb_device *udev = to_usb_device(dev);
680 rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
681 if (udev->usb2_hw_lpm_capable == 1)
682 rc = sysfs_merge_group(&dev->kobj,
683 &usb2_hardware_lpm_attr_group);
684 if ((udev->speed == USB_SPEED_SUPER ||
685 udev->speed == USB_SPEED_SUPER_PLUS) &&
686 udev->lpm_capable == 1)
687 rc = sysfs_merge_group(&dev->kobj,
688 &usb3_hardware_lpm_attr_group);
689 }
690
691 return rc;
692 }
693
remove_power_attributes(struct device * dev)694 static void remove_power_attributes(struct device *dev)
695 {
696 sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
697 sysfs_unmerge_group(&dev->kobj, &power_attr_group);
698 }
699
700 #else
701
702 #define add_persist_attributes(dev) 0
703 #define remove_persist_attributes(dev) do {} while (0)
704
705 #define add_power_attributes(dev) 0
706 #define remove_power_attributes(dev) do {} while (0)
707
708 #endif /* CONFIG_PM */
709
710
711 /* Descriptor fields */
712 #define usb_descriptor_attr_le16(field, format_string) \
713 static ssize_t \
714 field##_show(struct device *dev, struct device_attribute *attr, \
715 char *buf) \
716 { \
717 struct usb_device *udev; \
718 \
719 udev = to_usb_device(dev); \
720 return sprintf(buf, format_string, \
721 le16_to_cpu(udev->descriptor.field)); \
722 } \
723 static DEVICE_ATTR_RO(field)
724
725 usb_descriptor_attr_le16(idVendor, "%04x\n");
726 usb_descriptor_attr_le16(idProduct, "%04x\n");
727 usb_descriptor_attr_le16(bcdDevice, "%04x\n");
728
729 #define usb_descriptor_attr(field, format_string) \
730 static ssize_t \
731 field##_show(struct device *dev, struct device_attribute *attr, \
732 char *buf) \
733 { \
734 struct usb_device *udev; \
735 \
736 udev = to_usb_device(dev); \
737 return sprintf(buf, format_string, udev->descriptor.field); \
738 } \
739 static DEVICE_ATTR_RO(field)
740
741 usb_descriptor_attr(bDeviceClass, "%02x\n");
742 usb_descriptor_attr(bDeviceSubClass, "%02x\n");
743 usb_descriptor_attr(bDeviceProtocol, "%02x\n");
744 usb_descriptor_attr(bNumConfigurations, "%d\n");
745 usb_descriptor_attr(bMaxPacketSize0, "%d\n");
746
747
748 /* show if the device is authorized (1) or not (0) */
authorized_show(struct device * dev,struct device_attribute * attr,char * buf)749 static ssize_t authorized_show(struct device *dev,
750 struct device_attribute *attr, char *buf)
751 {
752 struct usb_device *usb_dev = to_usb_device(dev);
753 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
754 }
755
756 /*
757 * Authorize a device to be used in the system
758 *
759 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
760 */
authorized_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)761 static ssize_t authorized_store(struct device *dev,
762 struct device_attribute *attr, const char *buf,
763 size_t size)
764 {
765 ssize_t result;
766 struct usb_device *usb_dev = to_usb_device(dev);
767 unsigned val;
768 result = sscanf(buf, "%u\n", &val);
769 if (result != 1)
770 result = -EINVAL;
771 else if (val == 0)
772 result = usb_deauthorize_device(usb_dev);
773 else
774 result = usb_authorize_device(usb_dev);
775 return result < 0 ? result : size;
776 }
777 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
778 authorized_show, authorized_store);
779
780 /* "Safely remove a device" */
remove_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)781 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
782 const char *buf, size_t count)
783 {
784 struct usb_device *udev = to_usb_device(dev);
785 int rc = 0;
786
787 usb_lock_device(udev);
788 if (udev->state != USB_STATE_NOTATTACHED) {
789
790 /* To avoid races, first unconfigure and then remove */
791 usb_set_configuration(udev, -1);
792 rc = usb_remove_device(udev);
793 }
794 if (rc == 0)
795 rc = count;
796 usb_unlock_device(udev);
797 return rc;
798 }
799 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
800
801
802 static struct attribute *dev_attrs[] = {
803 /* current configuration's attributes */
804 &dev_attr_configuration.attr,
805 &dev_attr_bNumInterfaces.attr,
806 &dev_attr_bConfigurationValue.attr,
807 &dev_attr_bmAttributes.attr,
808 &dev_attr_bMaxPower.attr,
809 /* device attributes */
810 &dev_attr_urbnum.attr,
811 &dev_attr_idVendor.attr,
812 &dev_attr_idProduct.attr,
813 &dev_attr_bcdDevice.attr,
814 &dev_attr_bDeviceClass.attr,
815 &dev_attr_bDeviceSubClass.attr,
816 &dev_attr_bDeviceProtocol.attr,
817 &dev_attr_bNumConfigurations.attr,
818 &dev_attr_bMaxPacketSize0.attr,
819 &dev_attr_speed.attr,
820 &dev_attr_rx_lanes.attr,
821 &dev_attr_tx_lanes.attr,
822 &dev_attr_busnum.attr,
823 &dev_attr_devnum.attr,
824 &dev_attr_devpath.attr,
825 &dev_attr_version.attr,
826 &dev_attr_maxchild.attr,
827 &dev_attr_quirks.attr,
828 &dev_attr_avoid_reset_quirk.attr,
829 &dev_attr_authorized.attr,
830 &dev_attr_remove.attr,
831 &dev_attr_removable.attr,
832 &dev_attr_ltm_capable.attr,
833 #ifdef CONFIG_OF
834 &dev_attr_devspec.attr,
835 #endif
836 NULL,
837 };
838 static const struct attribute_group dev_attr_grp = {
839 .attrs = dev_attrs,
840 };
841
842 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
843 * accordingly.
844 */
845 static struct attribute *dev_string_attrs[] = {
846 &dev_attr_manufacturer.attr,
847 &dev_attr_product.attr,
848 &dev_attr_serial.attr,
849 NULL
850 };
851
dev_string_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)852 static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
853 struct attribute *a, int n)
854 {
855 struct device *dev = kobj_to_dev(kobj);
856 struct usb_device *udev = to_usb_device(dev);
857
858 if (a == &dev_attr_manufacturer.attr) {
859 if (udev->manufacturer == NULL)
860 return 0;
861 } else if (a == &dev_attr_product.attr) {
862 if (udev->product == NULL)
863 return 0;
864 } else if (a == &dev_attr_serial.attr) {
865 if (udev->serial == NULL)
866 return 0;
867 }
868 return a->mode;
869 }
870
871 static const struct attribute_group dev_string_attr_grp = {
872 .attrs = dev_string_attrs,
873 .is_visible = dev_string_attrs_are_visible,
874 };
875
876 const struct attribute_group *usb_device_groups[] = {
877 &dev_attr_grp,
878 &dev_string_attr_grp,
879 NULL
880 };
881
882 /* Binary descriptors */
883
884 static ssize_t
read_descriptors(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)885 read_descriptors(struct file *filp, struct kobject *kobj,
886 struct bin_attribute *attr,
887 char *buf, loff_t off, size_t count)
888 {
889 struct device *dev = kobj_to_dev(kobj);
890 struct usb_device *udev = to_usb_device(dev);
891 size_t nleft = count;
892 size_t srclen, n;
893 int cfgno;
894 void *src;
895 int retval;
896
897 retval = usb_lock_device_interruptible(udev);
898 if (retval < 0)
899 return -EINTR;
900 /* The binary attribute begins with the device descriptor.
901 * Following that are the raw descriptor entries for all the
902 * configurations (config plus subsidiary descriptors).
903 */
904 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
905 nleft > 0; ++cfgno) {
906 if (cfgno < 0) {
907 src = &udev->descriptor;
908 srclen = sizeof(struct usb_device_descriptor);
909 } else {
910 src = udev->rawdescriptors[cfgno];
911 srclen = __le16_to_cpu(udev->config[cfgno].desc.
912 wTotalLength);
913 }
914 if (off < srclen) {
915 n = min(nleft, srclen - (size_t) off);
916 memcpy(buf, src + off, n);
917 nleft -= n;
918 buf += n;
919 off = 0;
920 } else {
921 off -= srclen;
922 }
923 }
924 usb_unlock_device(udev);
925 return count - nleft;
926 }
927
928 static struct bin_attribute dev_bin_attr_descriptors = {
929 .attr = {.name = "descriptors", .mode = 0444},
930 .read = read_descriptors,
931 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
932 };
933
934 /*
935 * Show & store the current value of authorized_default
936 */
authorized_default_show(struct device * dev,struct device_attribute * attr,char * buf)937 static ssize_t authorized_default_show(struct device *dev,
938 struct device_attribute *attr, char *buf)
939 {
940 struct usb_device *rh_usb_dev = to_usb_device(dev);
941 struct usb_bus *usb_bus = rh_usb_dev->bus;
942 struct usb_hcd *hcd;
943
944 hcd = bus_to_hcd(usb_bus);
945 return snprintf(buf, PAGE_SIZE, "%u\n", hcd->dev_policy);
946 }
947
authorized_default_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t size)948 static ssize_t authorized_default_store(struct device *dev,
949 struct device_attribute *attr,
950 const char *buf, size_t size)
951 {
952 ssize_t result;
953 unsigned int val;
954 struct usb_device *rh_usb_dev = to_usb_device(dev);
955 struct usb_bus *usb_bus = rh_usb_dev->bus;
956 struct usb_hcd *hcd;
957
958 hcd = bus_to_hcd(usb_bus);
959 result = sscanf(buf, "%u\n", &val);
960 if (result == 1) {
961 hcd->dev_policy = val <= USB_DEVICE_AUTHORIZE_INTERNAL ?
962 val : USB_DEVICE_AUTHORIZE_ALL;
963 result = size;
964 } else {
965 result = -EINVAL;
966 }
967 return result;
968 }
969 static DEVICE_ATTR_RW(authorized_default);
970
971 /*
972 * interface_authorized_default_show - show default authorization status
973 * for USB interfaces
974 *
975 * note: interface_authorized_default is the default value
976 * for initializing the authorized attribute of interfaces
977 */
interface_authorized_default_show(struct device * dev,struct device_attribute * attr,char * buf)978 static ssize_t interface_authorized_default_show(struct device *dev,
979 struct device_attribute *attr, char *buf)
980 {
981 struct usb_device *usb_dev = to_usb_device(dev);
982 struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
983
984 return sprintf(buf, "%u\n", !!HCD_INTF_AUTHORIZED(hcd));
985 }
986
987 /*
988 * interface_authorized_default_store - store default authorization status
989 * for USB interfaces
990 *
991 * note: interface_authorized_default is the default value
992 * for initializing the authorized attribute of interfaces
993 */
interface_authorized_default_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)994 static ssize_t interface_authorized_default_store(struct device *dev,
995 struct device_attribute *attr, const char *buf, size_t count)
996 {
997 struct usb_device *usb_dev = to_usb_device(dev);
998 struct usb_hcd *hcd = bus_to_hcd(usb_dev->bus);
999 int rc = count;
1000 bool val;
1001
1002 if (strtobool(buf, &val) != 0)
1003 return -EINVAL;
1004
1005 if (val)
1006 set_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
1007 else
1008 clear_bit(HCD_FLAG_INTF_AUTHORIZED, &hcd->flags);
1009
1010 return rc;
1011 }
1012 static DEVICE_ATTR_RW(interface_authorized_default);
1013
1014 /* Group all the USB bus attributes */
1015 static struct attribute *usb_bus_attrs[] = {
1016 &dev_attr_authorized_default.attr,
1017 &dev_attr_interface_authorized_default.attr,
1018 NULL,
1019 };
1020
1021 static const struct attribute_group usb_bus_attr_group = {
1022 .name = NULL, /* we want them in the same directory */
1023 .attrs = usb_bus_attrs,
1024 };
1025
1026
add_default_authorized_attributes(struct device * dev)1027 static int add_default_authorized_attributes(struct device *dev)
1028 {
1029 int rc = 0;
1030
1031 if (is_usb_device(dev))
1032 rc = sysfs_create_group(&dev->kobj, &usb_bus_attr_group);
1033
1034 return rc;
1035 }
1036
remove_default_authorized_attributes(struct device * dev)1037 static void remove_default_authorized_attributes(struct device *dev)
1038 {
1039 if (is_usb_device(dev)) {
1040 sysfs_remove_group(&dev->kobj, &usb_bus_attr_group);
1041 }
1042 }
1043
usb_create_sysfs_dev_files(struct usb_device * udev)1044 int usb_create_sysfs_dev_files(struct usb_device *udev)
1045 {
1046 struct device *dev = &udev->dev;
1047 int retval;
1048
1049 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
1050 if (retval)
1051 goto error;
1052
1053 retval = add_persist_attributes(dev);
1054 if (retval)
1055 goto error;
1056
1057 retval = add_power_attributes(dev);
1058 if (retval)
1059 goto error;
1060
1061 if (is_root_hub(udev)) {
1062 retval = add_default_authorized_attributes(dev);
1063 if (retval)
1064 goto error;
1065 }
1066 return retval;
1067
1068 error:
1069 usb_remove_sysfs_dev_files(udev);
1070 return retval;
1071 }
1072
usb_remove_sysfs_dev_files(struct usb_device * udev)1073 void usb_remove_sysfs_dev_files(struct usb_device *udev)
1074 {
1075 struct device *dev = &udev->dev;
1076
1077 if (is_root_hub(udev))
1078 remove_default_authorized_attributes(dev);
1079
1080 remove_power_attributes(dev);
1081 remove_persist_attributes(dev);
1082 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
1083 }
1084
1085 /* Interface Association Descriptor fields */
1086 #define usb_intf_assoc_attr(field, format_string) \
1087 static ssize_t \
1088 iad_##field##_show(struct device *dev, struct device_attribute *attr, \
1089 char *buf) \
1090 { \
1091 struct usb_interface *intf = to_usb_interface(dev); \
1092 \
1093 return sprintf(buf, format_string, \
1094 intf->intf_assoc->field); \
1095 } \
1096 static DEVICE_ATTR_RO(iad_##field)
1097
1098 usb_intf_assoc_attr(bFirstInterface, "%02x\n");
1099 usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
1100 usb_intf_assoc_attr(bFunctionClass, "%02x\n");
1101 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
1102 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
1103
1104 /* Interface fields */
1105 #define usb_intf_attr(field, format_string) \
1106 static ssize_t \
1107 field##_show(struct device *dev, struct device_attribute *attr, \
1108 char *buf) \
1109 { \
1110 struct usb_interface *intf = to_usb_interface(dev); \
1111 \
1112 return sprintf(buf, format_string, \
1113 intf->cur_altsetting->desc.field); \
1114 } \
1115 static DEVICE_ATTR_RO(field)
1116
1117 usb_intf_attr(bInterfaceNumber, "%02x\n");
1118 usb_intf_attr(bAlternateSetting, "%2d\n");
1119 usb_intf_attr(bNumEndpoints, "%02x\n");
1120 usb_intf_attr(bInterfaceClass, "%02x\n");
1121 usb_intf_attr(bInterfaceSubClass, "%02x\n");
1122 usb_intf_attr(bInterfaceProtocol, "%02x\n");
1123
interface_show(struct device * dev,struct device_attribute * attr,char * buf)1124 static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
1125 char *buf)
1126 {
1127 struct usb_interface *intf;
1128 char *string;
1129
1130 intf = to_usb_interface(dev);
1131 string = READ_ONCE(intf->cur_altsetting->string);
1132 if (!string)
1133 return 0;
1134 return sprintf(buf, "%s\n", string);
1135 }
1136 static DEVICE_ATTR_RO(interface);
1137
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)1138 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1139 char *buf)
1140 {
1141 struct usb_interface *intf;
1142 struct usb_device *udev;
1143 struct usb_host_interface *alt;
1144
1145 intf = to_usb_interface(dev);
1146 udev = interface_to_usbdev(intf);
1147 alt = READ_ONCE(intf->cur_altsetting);
1148
1149 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
1150 "ic%02Xisc%02Xip%02Xin%02X\n",
1151 le16_to_cpu(udev->descriptor.idVendor),
1152 le16_to_cpu(udev->descriptor.idProduct),
1153 le16_to_cpu(udev->descriptor.bcdDevice),
1154 udev->descriptor.bDeviceClass,
1155 udev->descriptor.bDeviceSubClass,
1156 udev->descriptor.bDeviceProtocol,
1157 alt->desc.bInterfaceClass,
1158 alt->desc.bInterfaceSubClass,
1159 alt->desc.bInterfaceProtocol,
1160 alt->desc.bInterfaceNumber);
1161 }
1162 static DEVICE_ATTR_RO(modalias);
1163
supports_autosuspend_show(struct device * dev,struct device_attribute * attr,char * buf)1164 static ssize_t supports_autosuspend_show(struct device *dev,
1165 struct device_attribute *attr,
1166 char *buf)
1167 {
1168 int s;
1169
1170 s = device_lock_interruptible(dev);
1171 if (s < 0)
1172 return -EINTR;
1173 /* Devices will be autosuspended even when an interface isn't claimed */
1174 s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
1175 device_unlock(dev);
1176
1177 return sprintf(buf, "%u\n", s);
1178 }
1179 static DEVICE_ATTR_RO(supports_autosuspend);
1180
1181 /*
1182 * interface_authorized_show - show authorization status of an USB interface
1183 * 1 is authorized, 0 is deauthorized
1184 */
interface_authorized_show(struct device * dev,struct device_attribute * attr,char * buf)1185 static ssize_t interface_authorized_show(struct device *dev,
1186 struct device_attribute *attr, char *buf)
1187 {
1188 struct usb_interface *intf = to_usb_interface(dev);
1189
1190 return sprintf(buf, "%u\n", intf->authorized);
1191 }
1192
1193 /*
1194 * interface_authorized_store - authorize or deauthorize an USB interface
1195 */
interface_authorized_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1196 static ssize_t interface_authorized_store(struct device *dev,
1197 struct device_attribute *attr, const char *buf, size_t count)
1198 {
1199 struct usb_interface *intf = to_usb_interface(dev);
1200 bool val;
1201
1202 if (strtobool(buf, &val) != 0)
1203 return -EINVAL;
1204
1205 if (val)
1206 usb_authorize_interface(intf);
1207 else
1208 usb_deauthorize_interface(intf);
1209
1210 return count;
1211 }
1212 static struct device_attribute dev_attr_interface_authorized =
1213 __ATTR(authorized, S_IRUGO | S_IWUSR,
1214 interface_authorized_show, interface_authorized_store);
1215
1216 static struct attribute *intf_attrs[] = {
1217 &dev_attr_bInterfaceNumber.attr,
1218 &dev_attr_bAlternateSetting.attr,
1219 &dev_attr_bNumEndpoints.attr,
1220 &dev_attr_bInterfaceClass.attr,
1221 &dev_attr_bInterfaceSubClass.attr,
1222 &dev_attr_bInterfaceProtocol.attr,
1223 &dev_attr_modalias.attr,
1224 &dev_attr_supports_autosuspend.attr,
1225 &dev_attr_interface_authorized.attr,
1226 NULL,
1227 };
1228 static const struct attribute_group intf_attr_grp = {
1229 .attrs = intf_attrs,
1230 };
1231
1232 static struct attribute *intf_assoc_attrs[] = {
1233 &dev_attr_iad_bFirstInterface.attr,
1234 &dev_attr_iad_bInterfaceCount.attr,
1235 &dev_attr_iad_bFunctionClass.attr,
1236 &dev_attr_iad_bFunctionSubClass.attr,
1237 &dev_attr_iad_bFunctionProtocol.attr,
1238 NULL,
1239 };
1240
intf_assoc_attrs_are_visible(struct kobject * kobj,struct attribute * a,int n)1241 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
1242 struct attribute *a, int n)
1243 {
1244 struct device *dev = kobj_to_dev(kobj);
1245 struct usb_interface *intf = to_usb_interface(dev);
1246
1247 if (intf->intf_assoc == NULL)
1248 return 0;
1249 return a->mode;
1250 }
1251
1252 static const struct attribute_group intf_assoc_attr_grp = {
1253 .attrs = intf_assoc_attrs,
1254 .is_visible = intf_assoc_attrs_are_visible,
1255 };
1256
1257 const struct attribute_group *usb_interface_groups[] = {
1258 &intf_attr_grp,
1259 &intf_assoc_attr_grp,
1260 NULL
1261 };
1262
usb_create_sysfs_intf_files(struct usb_interface * intf)1263 void usb_create_sysfs_intf_files(struct usb_interface *intf)
1264 {
1265 struct usb_device *udev = interface_to_usbdev(intf);
1266 struct usb_host_interface *alt = intf->cur_altsetting;
1267
1268 if (intf->sysfs_files_created || intf->unregistering)
1269 return;
1270
1271 if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
1272 alt->string = usb_cache_string(udev, alt->desc.iInterface);
1273 if (alt->string && device_create_file(&intf->dev, &dev_attr_interface)) {
1274 /* This is not a serious error */
1275 dev_dbg(&intf->dev, "interface string descriptor file not created\n");
1276 }
1277 intf->sysfs_files_created = 1;
1278 }
1279
usb_remove_sysfs_intf_files(struct usb_interface * intf)1280 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
1281 {
1282 if (!intf->sysfs_files_created)
1283 return;
1284
1285 device_remove_file(&intf->dev, &dev_attr_interface);
1286 intf->sysfs_files_created = 0;
1287 }
1288