xref: /openbmc/linux/drivers/usb/core/sysfs.c (revision b830f94f)
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 
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 
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 
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
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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  */
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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 
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) */
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  */
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" */
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 
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
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 
892 	/* The binary attribute begins with the device descriptor.
893 	 * Following that are the raw descriptor entries for all the
894 	 * configurations (config plus subsidiary descriptors).
895 	 */
896 	for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
897 			nleft > 0; ++cfgno) {
898 		if (cfgno < 0) {
899 			src = &udev->descriptor;
900 			srclen = sizeof(struct usb_device_descriptor);
901 		} else {
902 			src = udev->rawdescriptors[cfgno];
903 			srclen = __le16_to_cpu(udev->config[cfgno].desc.
904 					wTotalLength);
905 		}
906 		if (off < srclen) {
907 			n = min(nleft, srclen - (size_t) off);
908 			memcpy(buf, src + off, n);
909 			nleft -= n;
910 			buf += n;
911 			off = 0;
912 		} else {
913 			off -= srclen;
914 		}
915 	}
916 	return count - nleft;
917 }
918 
919 static struct bin_attribute dev_bin_attr_descriptors = {
920 	.attr = {.name = "descriptors", .mode = 0444},
921 	.read = read_descriptors,
922 	.size = 18 + 65535,	/* dev descr + max-size raw descriptor */
923 };
924 
925 int usb_create_sysfs_dev_files(struct usb_device *udev)
926 {
927 	struct device *dev = &udev->dev;
928 	int retval;
929 
930 	retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
931 	if (retval)
932 		goto error;
933 
934 	retval = add_persist_attributes(dev);
935 	if (retval)
936 		goto error;
937 
938 	retval = add_power_attributes(dev);
939 	if (retval)
940 		goto error;
941 	return retval;
942 error:
943 	usb_remove_sysfs_dev_files(udev);
944 	return retval;
945 }
946 
947 void usb_remove_sysfs_dev_files(struct usb_device *udev)
948 {
949 	struct device *dev = &udev->dev;
950 
951 	remove_power_attributes(dev);
952 	remove_persist_attributes(dev);
953 	device_remove_bin_file(dev, &dev_bin_attr_descriptors);
954 }
955 
956 /* Interface Association Descriptor fields */
957 #define usb_intf_assoc_attr(field, format_string)			\
958 static ssize_t								\
959 iad_##field##_show(struct device *dev, struct device_attribute *attr,	\
960 		char *buf)						\
961 {									\
962 	struct usb_interface *intf = to_usb_interface(dev);		\
963 									\
964 	return sprintf(buf, format_string,				\
965 			intf->intf_assoc->field); 			\
966 }									\
967 static DEVICE_ATTR_RO(iad_##field)
968 
969 usb_intf_assoc_attr(bFirstInterface, "%02x\n");
970 usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
971 usb_intf_assoc_attr(bFunctionClass, "%02x\n");
972 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
973 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
974 
975 /* Interface fields */
976 #define usb_intf_attr(field, format_string)				\
977 static ssize_t								\
978 field##_show(struct device *dev, struct device_attribute *attr,		\
979 		char *buf)						\
980 {									\
981 	struct usb_interface *intf = to_usb_interface(dev);		\
982 									\
983 	return sprintf(buf, format_string,				\
984 			intf->cur_altsetting->desc.field); 		\
985 }									\
986 static DEVICE_ATTR_RO(field)
987 
988 usb_intf_attr(bInterfaceNumber, "%02x\n");
989 usb_intf_attr(bAlternateSetting, "%2d\n");
990 usb_intf_attr(bNumEndpoints, "%02x\n");
991 usb_intf_attr(bInterfaceClass, "%02x\n");
992 usb_intf_attr(bInterfaceSubClass, "%02x\n");
993 usb_intf_attr(bInterfaceProtocol, "%02x\n");
994 
995 static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
996 			      char *buf)
997 {
998 	struct usb_interface *intf;
999 	char *string;
1000 
1001 	intf = to_usb_interface(dev);
1002 	string = READ_ONCE(intf->cur_altsetting->string);
1003 	if (!string)
1004 		return 0;
1005 	return sprintf(buf, "%s\n", string);
1006 }
1007 static DEVICE_ATTR_RO(interface);
1008 
1009 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
1010 			     char *buf)
1011 {
1012 	struct usb_interface *intf;
1013 	struct usb_device *udev;
1014 	struct usb_host_interface *alt;
1015 
1016 	intf = to_usb_interface(dev);
1017 	udev = interface_to_usbdev(intf);
1018 	alt = READ_ONCE(intf->cur_altsetting);
1019 
1020 	return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
1021 			"ic%02Xisc%02Xip%02Xin%02X\n",
1022 			le16_to_cpu(udev->descriptor.idVendor),
1023 			le16_to_cpu(udev->descriptor.idProduct),
1024 			le16_to_cpu(udev->descriptor.bcdDevice),
1025 			udev->descriptor.bDeviceClass,
1026 			udev->descriptor.bDeviceSubClass,
1027 			udev->descriptor.bDeviceProtocol,
1028 			alt->desc.bInterfaceClass,
1029 			alt->desc.bInterfaceSubClass,
1030 			alt->desc.bInterfaceProtocol,
1031 			alt->desc.bInterfaceNumber);
1032 }
1033 static DEVICE_ATTR_RO(modalias);
1034 
1035 static ssize_t supports_autosuspend_show(struct device *dev,
1036 					 struct device_attribute *attr,
1037 					 char *buf)
1038 {
1039 	int s;
1040 
1041 	s = device_lock_interruptible(dev);
1042 	if (s < 0)
1043 		return -EINTR;
1044 	/* Devices will be autosuspended even when an interface isn't claimed */
1045 	s = (!dev->driver || to_usb_driver(dev->driver)->supports_autosuspend);
1046 	device_unlock(dev);
1047 
1048 	return sprintf(buf, "%u\n", s);
1049 }
1050 static DEVICE_ATTR_RO(supports_autosuspend);
1051 
1052 /*
1053  * interface_authorized_show - show authorization status of an USB interface
1054  * 1 is authorized, 0 is deauthorized
1055  */
1056 static ssize_t interface_authorized_show(struct device *dev,
1057 		struct device_attribute *attr, char *buf)
1058 {
1059 	struct usb_interface *intf = to_usb_interface(dev);
1060 
1061 	return sprintf(buf, "%u\n", intf->authorized);
1062 }
1063 
1064 /*
1065  * interface_authorized_store - authorize or deauthorize an USB interface
1066  */
1067 static ssize_t interface_authorized_store(struct device *dev,
1068 		struct device_attribute *attr, const char *buf, size_t count)
1069 {
1070 	struct usb_interface *intf = to_usb_interface(dev);
1071 	bool val;
1072 
1073 	if (strtobool(buf, &val) != 0)
1074 		return -EINVAL;
1075 
1076 	if (val)
1077 		usb_authorize_interface(intf);
1078 	else
1079 		usb_deauthorize_interface(intf);
1080 
1081 	return count;
1082 }
1083 static struct device_attribute dev_attr_interface_authorized =
1084 		__ATTR(authorized, S_IRUGO | S_IWUSR,
1085 		interface_authorized_show, interface_authorized_store);
1086 
1087 static struct attribute *intf_attrs[] = {
1088 	&dev_attr_bInterfaceNumber.attr,
1089 	&dev_attr_bAlternateSetting.attr,
1090 	&dev_attr_bNumEndpoints.attr,
1091 	&dev_attr_bInterfaceClass.attr,
1092 	&dev_attr_bInterfaceSubClass.attr,
1093 	&dev_attr_bInterfaceProtocol.attr,
1094 	&dev_attr_modalias.attr,
1095 	&dev_attr_supports_autosuspend.attr,
1096 	&dev_attr_interface_authorized.attr,
1097 	NULL,
1098 };
1099 static struct attribute_group intf_attr_grp = {
1100 	.attrs = intf_attrs,
1101 };
1102 
1103 static struct attribute *intf_assoc_attrs[] = {
1104 	&dev_attr_iad_bFirstInterface.attr,
1105 	&dev_attr_iad_bInterfaceCount.attr,
1106 	&dev_attr_iad_bFunctionClass.attr,
1107 	&dev_attr_iad_bFunctionSubClass.attr,
1108 	&dev_attr_iad_bFunctionProtocol.attr,
1109 	NULL,
1110 };
1111 
1112 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
1113 		struct attribute *a, int n)
1114 {
1115 	struct device *dev = container_of(kobj, struct device, kobj);
1116 	struct usb_interface *intf = to_usb_interface(dev);
1117 
1118 	if (intf->intf_assoc == NULL)
1119 		return 0;
1120 	return a->mode;
1121 }
1122 
1123 static struct attribute_group intf_assoc_attr_grp = {
1124 	.attrs =	intf_assoc_attrs,
1125 	.is_visible =	intf_assoc_attrs_are_visible,
1126 };
1127 
1128 const struct attribute_group *usb_interface_groups[] = {
1129 	&intf_attr_grp,
1130 	&intf_assoc_attr_grp,
1131 	NULL
1132 };
1133 
1134 void usb_create_sysfs_intf_files(struct usb_interface *intf)
1135 {
1136 	struct usb_device *udev = interface_to_usbdev(intf);
1137 	struct usb_host_interface *alt = intf->cur_altsetting;
1138 
1139 	if (intf->sysfs_files_created || intf->unregistering)
1140 		return;
1141 
1142 	if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
1143 		alt->string = usb_cache_string(udev, alt->desc.iInterface);
1144 	if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
1145 		;	/* We don't actually care if the function fails. */
1146 	intf->sysfs_files_created = 1;
1147 }
1148 
1149 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
1150 {
1151 	if (!intf->sysfs_files_created)
1152 		return;
1153 
1154 	device_remove_file(&intf->dev, &dev_attr_interface);
1155 	intf->sysfs_files_created = 0;
1156 }
1157