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