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