xref: /openbmc/linux/drivers/usb/core/sysfs.c (revision f35e839a)
1 /*
2  * drivers/usb/core/sysfs.c
3  *
4  * (C) Copyright 2002 David Brownell
5  * (C) Copyright 2002,2004 Greg Kroah-Hartman
6  * (C) Copyright 2002,2004 IBM Corp.
7  *
8  * All of the sysfs file attributes for usb devices and interfaces.
9  *
10  */
11 
12 
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/usb.h>
16 #include <linux/usb/quirks.h>
17 #include "usb.h"
18 
19 /* Active configuration fields */
20 #define usb_actconfig_show(field, format_string)			\
21 static ssize_t  show_##field(struct device *dev,			\
22 		struct device_attribute *attr, char *buf)		\
23 {									\
24 	struct usb_device *udev;					\
25 	struct usb_host_config *actconfig;				\
26 									\
27 	udev = to_usb_device(dev);					\
28 	actconfig = udev->actconfig;					\
29 	if (actconfig)							\
30 		return sprintf(buf, format_string,			\
31 				actconfig->desc.field);			\
32 	else								\
33 		return 0;						\
34 }									\
35 
36 #define usb_actconfig_attr(field, format_string)		\
37 	usb_actconfig_show(field, format_string)		\
38 	static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
39 
40 usb_actconfig_attr(bNumInterfaces, "%2d\n")
41 usb_actconfig_attr(bmAttributes, "%2x\n")
42 
43 static ssize_t show_bMaxPower(struct device *dev,
44 		struct device_attribute *attr, char *buf)
45 {
46 	struct usb_device *udev;
47 	struct usb_host_config *actconfig;
48 
49 	udev = to_usb_device(dev);
50 	actconfig = udev->actconfig;
51 	if (!actconfig)
52 		return 0;
53 	return sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
54 }
55 static DEVICE_ATTR(bMaxPower, S_IRUGO, show_bMaxPower, NULL);
56 
57 static ssize_t show_configuration_string(struct device *dev,
58 		struct device_attribute *attr, char *buf)
59 {
60 	struct usb_device *udev;
61 	struct usb_host_config *actconfig;
62 
63 	udev = to_usb_device(dev);
64 	actconfig = udev->actconfig;
65 	if ((!actconfig) || (!actconfig->string))
66 		return 0;
67 	return sprintf(buf, "%s\n", actconfig->string);
68 }
69 static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
70 
71 /* configuration value is always present, and r/w */
72 usb_actconfig_show(bConfigurationValue, "%u\n");
73 
74 static ssize_t
75 set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
76 		const char *buf, size_t count)
77 {
78 	struct usb_device	*udev = to_usb_device(dev);
79 	int			config, value;
80 
81 	if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
82 		return -EINVAL;
83 	usb_lock_device(udev);
84 	value = usb_set_configuration(udev, config);
85 	usb_unlock_device(udev);
86 	return (value < 0) ? value : count;
87 }
88 
89 static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
90 		show_bConfigurationValue, set_bConfigurationValue);
91 
92 /* String fields */
93 #define usb_string_attr(name)						\
94 static ssize_t  show_##name(struct device *dev,				\
95 		struct device_attribute *attr, char *buf)		\
96 {									\
97 	struct usb_device *udev;					\
98 	int retval;							\
99 									\
100 	udev = to_usb_device(dev);					\
101 	usb_lock_device(udev);						\
102 	retval = sprintf(buf, "%s\n", udev->name);			\
103 	usb_unlock_device(udev);					\
104 	return retval;							\
105 }									\
106 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
107 
108 usb_string_attr(product);
109 usb_string_attr(manufacturer);
110 usb_string_attr(serial);
111 
112 static ssize_t
113 show_speed(struct device *dev, struct device_attribute *attr, char *buf)
114 {
115 	struct usb_device *udev;
116 	char *speed;
117 
118 	udev = to_usb_device(dev);
119 
120 	switch (udev->speed) {
121 	case USB_SPEED_LOW:
122 		speed = "1.5";
123 		break;
124 	case USB_SPEED_UNKNOWN:
125 	case USB_SPEED_FULL:
126 		speed = "12";
127 		break;
128 	case USB_SPEED_HIGH:
129 		speed = "480";
130 		break;
131 	case USB_SPEED_WIRELESS:
132 		speed = "480";
133 		break;
134 	case USB_SPEED_SUPER:
135 		speed = "5000";
136 		break;
137 	default:
138 		speed = "unknown";
139 	}
140 	return sprintf(buf, "%s\n", speed);
141 }
142 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
143 
144 static ssize_t
145 show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
146 {
147 	struct usb_device *udev;
148 
149 	udev = to_usb_device(dev);
150 	return sprintf(buf, "%d\n", udev->bus->busnum);
151 }
152 static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
153 
154 static ssize_t
155 show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
156 {
157 	struct usb_device *udev;
158 
159 	udev = to_usb_device(dev);
160 	return sprintf(buf, "%d\n", udev->devnum);
161 }
162 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
163 
164 static ssize_t
165 show_devpath(struct device *dev, struct device_attribute *attr, char *buf)
166 {
167 	struct usb_device *udev;
168 
169 	udev = to_usb_device(dev);
170 	return sprintf(buf, "%s\n", udev->devpath);
171 }
172 static DEVICE_ATTR(devpath, S_IRUGO, show_devpath, NULL);
173 
174 static ssize_t
175 show_version(struct device *dev, struct device_attribute *attr, char *buf)
176 {
177 	struct usb_device *udev;
178 	u16 bcdUSB;
179 
180 	udev = to_usb_device(dev);
181 	bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
182 	return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
183 }
184 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
185 
186 static ssize_t
187 show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
188 {
189 	struct usb_device *udev;
190 
191 	udev = to_usb_device(dev);
192 	return sprintf(buf, "%d\n", udev->maxchild);
193 }
194 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
195 
196 static ssize_t
197 show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
198 {
199 	struct usb_device *udev;
200 
201 	udev = to_usb_device(dev);
202 	return sprintf(buf, "0x%x\n", udev->quirks);
203 }
204 static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
205 
206 static ssize_t
207 show_avoid_reset_quirk(struct device *dev, struct device_attribute *attr, char *buf)
208 {
209 	struct usb_device *udev;
210 
211 	udev = to_usb_device(dev);
212 	return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
213 }
214 
215 static ssize_t
216 set_avoid_reset_quirk(struct device *dev, struct device_attribute *attr,
217 		const char *buf, size_t count)
218 {
219 	struct usb_device	*udev = to_usb_device(dev);
220 	int			val;
221 
222 	if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
223 		return -EINVAL;
224 	usb_lock_device(udev);
225 	if (val)
226 		udev->quirks |= USB_QUIRK_RESET;
227 	else
228 		udev->quirks &= ~USB_QUIRK_RESET;
229 	usb_unlock_device(udev);
230 	return count;
231 }
232 
233 static DEVICE_ATTR(avoid_reset_quirk, S_IRUGO | S_IWUSR,
234 		show_avoid_reset_quirk, set_avoid_reset_quirk);
235 
236 static ssize_t
237 show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
238 {
239 	struct usb_device *udev;
240 
241 	udev = to_usb_device(dev);
242 	return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
243 }
244 static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
245 
246 static ssize_t
247 show_removable(struct device *dev, struct device_attribute *attr, char *buf)
248 {
249 	struct usb_device *udev;
250 	char *state;
251 
252 	udev = to_usb_device(dev);
253 
254 	switch (udev->removable) {
255 	case USB_DEVICE_REMOVABLE:
256 		state = "removable";
257 		break;
258 	case USB_DEVICE_FIXED:
259 		state = "fixed";
260 		break;
261 	default:
262 		state = "unknown";
263 	}
264 
265 	return sprintf(buf, "%s\n", state);
266 }
267 static DEVICE_ATTR(removable, S_IRUGO, show_removable, NULL);
268 
269 static ssize_t
270 show_ltm_capable(struct device *dev, struct device_attribute *attr, char *buf)
271 {
272 	if (usb_device_supports_ltm(to_usb_device(dev)))
273 		return sprintf(buf, "%s\n", "yes");
274 	return sprintf(buf, "%s\n", "no");
275 }
276 static DEVICE_ATTR(ltm_capable, S_IRUGO, show_ltm_capable, NULL);
277 
278 #ifdef	CONFIG_PM
279 
280 static ssize_t
281 show_persist(struct device *dev, struct device_attribute *attr, char *buf)
282 {
283 	struct usb_device *udev = to_usb_device(dev);
284 
285 	return sprintf(buf, "%d\n", udev->persist_enabled);
286 }
287 
288 static ssize_t
289 set_persist(struct device *dev, struct device_attribute *attr,
290 		const char *buf, size_t count)
291 {
292 	struct usb_device *udev = to_usb_device(dev);
293 	int value;
294 
295 	/* Hubs are always enabled for USB_PERSIST */
296 	if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
297 		return -EPERM;
298 
299 	if (sscanf(buf, "%d", &value) != 1)
300 		return -EINVAL;
301 
302 	usb_lock_device(udev);
303 	udev->persist_enabled = !!value;
304 	usb_unlock_device(udev);
305 	return count;
306 }
307 
308 static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
309 
310 static int add_persist_attributes(struct device *dev)
311 {
312 	int rc = 0;
313 
314 	if (is_usb_device(dev)) {
315 		struct usb_device *udev = to_usb_device(dev);
316 
317 		/* Hubs are automatically enabled for USB_PERSIST,
318 		 * no point in creating the attribute file.
319 		 */
320 		if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
321 			rc = sysfs_add_file_to_group(&dev->kobj,
322 					&dev_attr_persist.attr,
323 					power_group_name);
324 	}
325 	return rc;
326 }
327 
328 static void remove_persist_attributes(struct device *dev)
329 {
330 	sysfs_remove_file_from_group(&dev->kobj,
331 			&dev_attr_persist.attr,
332 			power_group_name);
333 }
334 #else
335 
336 #define add_persist_attributes(dev)	0
337 #define remove_persist_attributes(dev)	do {} while (0)
338 
339 #endif	/* CONFIG_PM */
340 
341 #ifdef	CONFIG_PM_RUNTIME
342 
343 static ssize_t
344 show_connected_duration(struct device *dev, struct device_attribute *attr,
345 		char *buf)
346 {
347 	struct usb_device *udev = to_usb_device(dev);
348 
349 	return sprintf(buf, "%u\n",
350 			jiffies_to_msecs(jiffies - udev->connect_time));
351 }
352 
353 static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL);
354 
355 /*
356  * If the device is resumed, the last time the device was suspended has
357  * been pre-subtracted from active_duration.  We add the current time to
358  * get the duration that the device was actually active.
359  *
360  * If the device is suspended, the active_duration is up-to-date.
361  */
362 static ssize_t
363 show_active_duration(struct device *dev, struct device_attribute *attr,
364 		char *buf)
365 {
366 	struct usb_device *udev = to_usb_device(dev);
367 	int duration;
368 
369 	if (udev->state != USB_STATE_SUSPENDED)
370 		duration = jiffies_to_msecs(jiffies + udev->active_duration);
371 	else
372 		duration = jiffies_to_msecs(udev->active_duration);
373 	return sprintf(buf, "%u\n", duration);
374 }
375 
376 static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL);
377 
378 static ssize_t
379 show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
380 {
381 	return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
382 }
383 
384 static ssize_t
385 set_autosuspend(struct device *dev, struct device_attribute *attr,
386 		const char *buf, size_t count)
387 {
388 	int value;
389 
390 	if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
391 			value <= -INT_MAX/1000)
392 		return -EINVAL;
393 
394 	pm_runtime_set_autosuspend_delay(dev, value * 1000);
395 	return count;
396 }
397 
398 static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
399 		show_autosuspend, set_autosuspend);
400 
401 static const char on_string[] = "on";
402 static const char auto_string[] = "auto";
403 
404 static void warn_level(void) {
405 	static int level_warned;
406 
407 	if (!level_warned) {
408 		level_warned = 1;
409 		printk(KERN_WARNING "WARNING! power/level is deprecated; "
410 				"use power/control instead\n");
411 	}
412 }
413 
414 static ssize_t
415 show_level(struct device *dev, struct device_attribute *attr, char *buf)
416 {
417 	struct usb_device *udev = to_usb_device(dev);
418 	const char *p = auto_string;
419 
420 	warn_level();
421 	if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
422 		p = on_string;
423 	return sprintf(buf, "%s\n", p);
424 }
425 
426 static ssize_t
427 set_level(struct device *dev, struct device_attribute *attr,
428 		const char *buf, size_t count)
429 {
430 	struct usb_device *udev = to_usb_device(dev);
431 	int len = count;
432 	char *cp;
433 	int rc = count;
434 
435 	warn_level();
436 	cp = memchr(buf, '\n', count);
437 	if (cp)
438 		len = cp - buf;
439 
440 	usb_lock_device(udev);
441 
442 	if (len == sizeof on_string - 1 &&
443 			strncmp(buf, on_string, len) == 0)
444 		usb_disable_autosuspend(udev);
445 
446 	else if (len == sizeof auto_string - 1 &&
447 			strncmp(buf, auto_string, len) == 0)
448 		usb_enable_autosuspend(udev);
449 
450 	else
451 		rc = -EINVAL;
452 
453 	usb_unlock_device(udev);
454 	return rc;
455 }
456 
457 static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
458 
459 static ssize_t
460 show_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
461 				char *buf)
462 {
463 	struct usb_device *udev = to_usb_device(dev);
464 	const char *p;
465 
466 	if (udev->usb2_hw_lpm_enabled == 1)
467 		p = "enabled";
468 	else
469 		p = "disabled";
470 
471 	return sprintf(buf, "%s\n", p);
472 }
473 
474 static ssize_t
475 set_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
476 		const char *buf, size_t count)
477 {
478 	struct usb_device *udev = to_usb_device(dev);
479 	bool value;
480 	int ret;
481 
482 	usb_lock_device(udev);
483 
484 	ret = strtobool(buf, &value);
485 
486 	if (!ret)
487 		ret = usb_set_usb2_hardware_lpm(udev, value);
488 
489 	usb_unlock_device(udev);
490 
491 	if (!ret)
492 		return count;
493 
494 	return ret;
495 }
496 
497 static DEVICE_ATTR(usb2_hardware_lpm, S_IRUGO | S_IWUSR, show_usb2_hardware_lpm,
498 			set_usb2_hardware_lpm);
499 
500 static struct attribute *usb2_hardware_lpm_attr[] = {
501 	&dev_attr_usb2_hardware_lpm.attr,
502 	NULL,
503 };
504 static struct attribute_group usb2_hardware_lpm_attr_group = {
505 	.name	= power_group_name,
506 	.attrs	= usb2_hardware_lpm_attr,
507 };
508 
509 static struct attribute *power_attrs[] = {
510 	&dev_attr_autosuspend.attr,
511 	&dev_attr_level.attr,
512 	&dev_attr_connected_duration.attr,
513 	&dev_attr_active_duration.attr,
514 	NULL,
515 };
516 static struct attribute_group power_attr_group = {
517 	.name	= power_group_name,
518 	.attrs	= power_attrs,
519 };
520 
521 static int add_power_attributes(struct device *dev)
522 {
523 	int rc = 0;
524 
525 	if (is_usb_device(dev)) {
526 		struct usb_device *udev = to_usb_device(dev);
527 		rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
528 		if (udev->usb2_hw_lpm_capable == 1)
529 			rc = sysfs_merge_group(&dev->kobj,
530 					&usb2_hardware_lpm_attr_group);
531 	}
532 
533 	return rc;
534 }
535 
536 static void remove_power_attributes(struct device *dev)
537 {
538 	sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
539 	sysfs_unmerge_group(&dev->kobj, &power_attr_group);
540 }
541 
542 #else
543 
544 #define add_power_attributes(dev)	0
545 #define remove_power_attributes(dev)	do {} while (0)
546 
547 #endif	/* CONFIG_PM_RUNTIME */
548 
549 
550 /* Descriptor fields */
551 #define usb_descriptor_attr_le16(field, format_string)			\
552 static ssize_t								\
553 show_##field(struct device *dev, struct device_attribute *attr,	\
554 		char *buf)						\
555 {									\
556 	struct usb_device *udev;					\
557 									\
558 	udev = to_usb_device(dev);					\
559 	return sprintf(buf, format_string, 				\
560 			le16_to_cpu(udev->descriptor.field));		\
561 }									\
562 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
563 
564 usb_descriptor_attr_le16(idVendor, "%04x\n")
565 usb_descriptor_attr_le16(idProduct, "%04x\n")
566 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
567 
568 #define usb_descriptor_attr(field, format_string)			\
569 static ssize_t								\
570 show_##field(struct device *dev, struct device_attribute *attr,	\
571 		char *buf)						\
572 {									\
573 	struct usb_device *udev;					\
574 									\
575 	udev = to_usb_device(dev);					\
576 	return sprintf(buf, format_string, udev->descriptor.field);	\
577 }									\
578 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
579 
580 usb_descriptor_attr(bDeviceClass, "%02x\n")
581 usb_descriptor_attr(bDeviceSubClass, "%02x\n")
582 usb_descriptor_attr(bDeviceProtocol, "%02x\n")
583 usb_descriptor_attr(bNumConfigurations, "%d\n")
584 usb_descriptor_attr(bMaxPacketSize0, "%d\n")
585 
586 
587 
588 /* show if the device is authorized (1) or not (0) */
589 static ssize_t usb_dev_authorized_show(struct device *dev,
590 				       struct device_attribute *attr,
591 				       char *buf)
592 {
593 	struct usb_device *usb_dev = to_usb_device(dev);
594 	return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
595 }
596 
597 
598 /*
599  * Authorize a device to be used in the system
600  *
601  * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
602  */
603 static ssize_t usb_dev_authorized_store(struct device *dev,
604 					struct device_attribute *attr,
605 					const char *buf, size_t size)
606 {
607 	ssize_t result;
608 	struct usb_device *usb_dev = to_usb_device(dev);
609 	unsigned val;
610 	result = sscanf(buf, "%u\n", &val);
611 	if (result != 1)
612 		result = -EINVAL;
613 	else if (val == 0)
614 		result = usb_deauthorize_device(usb_dev);
615 	else
616 		result = usb_authorize_device(usb_dev);
617 	return result < 0? result : size;
618 }
619 
620 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, 0644,
621 	    usb_dev_authorized_show, usb_dev_authorized_store);
622 
623 /* "Safely remove a device" */
624 static ssize_t usb_remove_store(struct device *dev,
625 		struct device_attribute *attr,
626 		const char *buf, size_t count)
627 {
628 	struct usb_device *udev = to_usb_device(dev);
629 	int rc = 0;
630 
631 	usb_lock_device(udev);
632 	if (udev->state != USB_STATE_NOTATTACHED) {
633 
634 		/* To avoid races, first unconfigure and then remove */
635 		usb_set_configuration(udev, -1);
636 		rc = usb_remove_device(udev);
637 	}
638 	if (rc == 0)
639 		rc = count;
640 	usb_unlock_device(udev);
641 	return rc;
642 }
643 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0200, NULL, usb_remove_store);
644 
645 
646 static struct attribute *dev_attrs[] = {
647 	/* current configuration's attributes */
648 	&dev_attr_configuration.attr,
649 	&dev_attr_bNumInterfaces.attr,
650 	&dev_attr_bConfigurationValue.attr,
651 	&dev_attr_bmAttributes.attr,
652 	&dev_attr_bMaxPower.attr,
653 	/* device attributes */
654 	&dev_attr_urbnum.attr,
655 	&dev_attr_idVendor.attr,
656 	&dev_attr_idProduct.attr,
657 	&dev_attr_bcdDevice.attr,
658 	&dev_attr_bDeviceClass.attr,
659 	&dev_attr_bDeviceSubClass.attr,
660 	&dev_attr_bDeviceProtocol.attr,
661 	&dev_attr_bNumConfigurations.attr,
662 	&dev_attr_bMaxPacketSize0.attr,
663 	&dev_attr_speed.attr,
664 	&dev_attr_busnum.attr,
665 	&dev_attr_devnum.attr,
666 	&dev_attr_devpath.attr,
667 	&dev_attr_version.attr,
668 	&dev_attr_maxchild.attr,
669 	&dev_attr_quirks.attr,
670 	&dev_attr_avoid_reset_quirk.attr,
671 	&dev_attr_authorized.attr,
672 	&dev_attr_remove.attr,
673 	&dev_attr_removable.attr,
674 	&dev_attr_ltm_capable.attr,
675 	NULL,
676 };
677 static struct attribute_group dev_attr_grp = {
678 	.attrs = dev_attrs,
679 };
680 
681 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
682  * accordingly.
683  */
684 static struct attribute *dev_string_attrs[] = {
685 	&dev_attr_manufacturer.attr,
686 	&dev_attr_product.attr,
687 	&dev_attr_serial.attr,
688 	NULL
689 };
690 
691 static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
692 		struct attribute *a, int n)
693 {
694 	struct device *dev = container_of(kobj, struct device, kobj);
695 	struct usb_device *udev = to_usb_device(dev);
696 
697 	if (a == &dev_attr_manufacturer.attr) {
698 		if (udev->manufacturer == NULL)
699 			return 0;
700 	} else if (a == &dev_attr_product.attr) {
701 		if (udev->product == NULL)
702 			return 0;
703 	} else if (a == &dev_attr_serial.attr) {
704 		if (udev->serial == NULL)
705 			return 0;
706 	}
707 	return a->mode;
708 }
709 
710 static struct attribute_group dev_string_attr_grp = {
711 	.attrs =	dev_string_attrs,
712 	.is_visible =	dev_string_attrs_are_visible,
713 };
714 
715 const struct attribute_group *usb_device_groups[] = {
716 	&dev_attr_grp,
717 	&dev_string_attr_grp,
718 	NULL
719 };
720 
721 /* Binary descriptors */
722 
723 static ssize_t
724 read_descriptors(struct file *filp, struct kobject *kobj,
725 		struct bin_attribute *attr,
726 		char *buf, loff_t off, size_t count)
727 {
728 	struct device *dev = container_of(kobj, struct device, kobj);
729 	struct usb_device *udev = to_usb_device(dev);
730 	size_t nleft = count;
731 	size_t srclen, n;
732 	int cfgno;
733 	void *src;
734 
735 	/* The binary attribute begins with the device descriptor.
736 	 * Following that are the raw descriptor entries for all the
737 	 * configurations (config plus subsidiary descriptors).
738 	 */
739 	for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
740 			nleft > 0; ++cfgno) {
741 		if (cfgno < 0) {
742 			src = &udev->descriptor;
743 			srclen = sizeof(struct usb_device_descriptor);
744 		} else {
745 			src = udev->rawdescriptors[cfgno];
746 			srclen = __le16_to_cpu(udev->config[cfgno].desc.
747 					wTotalLength);
748 		}
749 		if (off < srclen) {
750 			n = min(nleft, srclen - (size_t) off);
751 			memcpy(buf, src + off, n);
752 			nleft -= n;
753 			buf += n;
754 			off = 0;
755 		} else {
756 			off -= srclen;
757 		}
758 	}
759 	return count - nleft;
760 }
761 
762 static struct bin_attribute dev_bin_attr_descriptors = {
763 	.attr = {.name = "descriptors", .mode = 0444},
764 	.read = read_descriptors,
765 	.size = 18 + 65535,	/* dev descr + max-size raw descriptor */
766 };
767 
768 int usb_create_sysfs_dev_files(struct usb_device *udev)
769 {
770 	struct device *dev = &udev->dev;
771 	int retval;
772 
773 	retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
774 	if (retval)
775 		goto error;
776 
777 	retval = add_persist_attributes(dev);
778 	if (retval)
779 		goto error;
780 
781 	retval = add_power_attributes(dev);
782 	if (retval)
783 		goto error;
784 	return retval;
785 error:
786 	usb_remove_sysfs_dev_files(udev);
787 	return retval;
788 }
789 
790 void usb_remove_sysfs_dev_files(struct usb_device *udev)
791 {
792 	struct device *dev = &udev->dev;
793 
794 	remove_power_attributes(dev);
795 	remove_persist_attributes(dev);
796 	device_remove_bin_file(dev, &dev_bin_attr_descriptors);
797 }
798 
799 /* Interface Accociation Descriptor fields */
800 #define usb_intf_assoc_attr(field, format_string)			\
801 static ssize_t								\
802 show_iad_##field(struct device *dev, struct device_attribute *attr,	\
803 		char *buf)						\
804 {									\
805 	struct usb_interface *intf = to_usb_interface(dev);		\
806 									\
807 	return sprintf(buf, format_string,				\
808 			intf->intf_assoc->field); 			\
809 }									\
810 static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
811 
812 usb_intf_assoc_attr(bFirstInterface, "%02x\n")
813 usb_intf_assoc_attr(bInterfaceCount, "%02d\n")
814 usb_intf_assoc_attr(bFunctionClass, "%02x\n")
815 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n")
816 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n")
817 
818 /* Interface fields */
819 #define usb_intf_attr(field, format_string)				\
820 static ssize_t								\
821 show_##field(struct device *dev, struct device_attribute *attr,	\
822 		char *buf)						\
823 {									\
824 	struct usb_interface *intf = to_usb_interface(dev);		\
825 									\
826 	return sprintf(buf, format_string,				\
827 			intf->cur_altsetting->desc.field); 		\
828 }									\
829 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
830 
831 usb_intf_attr(bInterfaceNumber, "%02x\n")
832 usb_intf_attr(bAlternateSetting, "%2d\n")
833 usb_intf_attr(bNumEndpoints, "%02x\n")
834 usb_intf_attr(bInterfaceClass, "%02x\n")
835 usb_intf_attr(bInterfaceSubClass, "%02x\n")
836 usb_intf_attr(bInterfaceProtocol, "%02x\n")
837 
838 static ssize_t show_interface_string(struct device *dev,
839 		struct device_attribute *attr, char *buf)
840 {
841 	struct usb_interface *intf;
842 	char *string;
843 
844 	intf = to_usb_interface(dev);
845 	string = intf->cur_altsetting->string;
846 	barrier();		/* The altsetting might change! */
847 
848 	if (!string)
849 		return 0;
850 	return sprintf(buf, "%s\n", string);
851 }
852 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
853 
854 static ssize_t show_modalias(struct device *dev,
855 		struct device_attribute *attr, char *buf)
856 {
857 	struct usb_interface *intf;
858 	struct usb_device *udev;
859 	struct usb_host_interface *alt;
860 
861 	intf = to_usb_interface(dev);
862 	udev = interface_to_usbdev(intf);
863 	alt = intf->cur_altsetting;
864 
865 	return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
866 			"ic%02Xisc%02Xip%02Xin%02X\n",
867 			le16_to_cpu(udev->descriptor.idVendor),
868 			le16_to_cpu(udev->descriptor.idProduct),
869 			le16_to_cpu(udev->descriptor.bcdDevice),
870 			udev->descriptor.bDeviceClass,
871 			udev->descriptor.bDeviceSubClass,
872 			udev->descriptor.bDeviceProtocol,
873 			alt->desc.bInterfaceClass,
874 			alt->desc.bInterfaceSubClass,
875 			alt->desc.bInterfaceProtocol,
876 			alt->desc.bInterfaceNumber);
877 }
878 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
879 
880 static ssize_t show_supports_autosuspend(struct device *dev,
881 		struct device_attribute *attr, char *buf)
882 {
883 	struct usb_interface *intf;
884 	struct usb_device *udev;
885 	int ret;
886 
887 	intf = to_usb_interface(dev);
888 	udev = interface_to_usbdev(intf);
889 
890 	usb_lock_device(udev);
891 	/* Devices will be autosuspended even when an interface isn't claimed */
892 	if (!intf->dev.driver ||
893 			to_usb_driver(intf->dev.driver)->supports_autosuspend)
894 		ret = sprintf(buf, "%u\n", 1);
895 	else
896 		ret = sprintf(buf, "%u\n", 0);
897 	usb_unlock_device(udev);
898 
899 	return ret;
900 }
901 static DEVICE_ATTR(supports_autosuspend, S_IRUGO, show_supports_autosuspend, NULL);
902 
903 static struct attribute *intf_attrs[] = {
904 	&dev_attr_bInterfaceNumber.attr,
905 	&dev_attr_bAlternateSetting.attr,
906 	&dev_attr_bNumEndpoints.attr,
907 	&dev_attr_bInterfaceClass.attr,
908 	&dev_attr_bInterfaceSubClass.attr,
909 	&dev_attr_bInterfaceProtocol.attr,
910 	&dev_attr_modalias.attr,
911 	&dev_attr_supports_autosuspend.attr,
912 	NULL,
913 };
914 static struct attribute_group intf_attr_grp = {
915 	.attrs = intf_attrs,
916 };
917 
918 static struct attribute *intf_assoc_attrs[] = {
919 	&dev_attr_iad_bFirstInterface.attr,
920 	&dev_attr_iad_bInterfaceCount.attr,
921 	&dev_attr_iad_bFunctionClass.attr,
922 	&dev_attr_iad_bFunctionSubClass.attr,
923 	&dev_attr_iad_bFunctionProtocol.attr,
924 	NULL,
925 };
926 
927 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
928 		struct attribute *a, int n)
929 {
930 	struct device *dev = container_of(kobj, struct device, kobj);
931 	struct usb_interface *intf = to_usb_interface(dev);
932 
933 	if (intf->intf_assoc == NULL)
934 		return 0;
935 	return a->mode;
936 }
937 
938 static struct attribute_group intf_assoc_attr_grp = {
939 	.attrs =	intf_assoc_attrs,
940 	.is_visible =	intf_assoc_attrs_are_visible,
941 };
942 
943 const struct attribute_group *usb_interface_groups[] = {
944 	&intf_attr_grp,
945 	&intf_assoc_attr_grp,
946 	NULL
947 };
948 
949 void usb_create_sysfs_intf_files(struct usb_interface *intf)
950 {
951 	struct usb_device *udev = interface_to_usbdev(intf);
952 	struct usb_host_interface *alt = intf->cur_altsetting;
953 
954 	if (intf->sysfs_files_created || intf->unregistering)
955 		return;
956 
957 	if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
958 		alt->string = usb_cache_string(udev, alt->desc.iInterface);
959 	if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
960 		;	/* We don't actually care if the function fails. */
961 	intf->sysfs_files_created = 1;
962 }
963 
964 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
965 {
966 	if (!intf->sysfs_files_created)
967 		return;
968 
969 	device_remove_file(&intf->dev, &dev_attr_interface);
970 	intf->sysfs_files_created = 0;
971 }
972