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