mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / usb / core / sysfs.c
blobca516ac0f23445880f4ca9bab6b25fcf8931bc99
1 /*
2 * drivers/usb/core/sysfs.c
4 * (C) Copyright 2002 David Brownell
5 * (C) Copyright 2002,2004 Greg Kroah-Hartman
6 * (C) Copyright 2002,2004 IBM Corp.
8 * All of the sysfs file attributes for usb devices and interfaces.
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"
19 /* Active configuration fields */
20 #define usb_actconfig_show(field, format_string) \
21 static ssize_t field##_show(struct device *dev, \
22 struct device_attribute *attr, char *buf) \
23 { \
24 struct usb_device *udev; \
25 struct usb_host_config *actconfig; \
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 } \
36 #define usb_actconfig_attr(field, format_string) \
37 usb_actconfig_show(field, format_string) \
38 static DEVICE_ATTR_RO(field)
40 usb_actconfig_attr(bNumInterfaces, "%2d\n");
41 usb_actconfig_attr(bmAttributes, "%2x\n");
43 static ssize_t bMaxPower_show(struct device *dev,
44 struct device_attribute *attr, char *buf)
46 struct usb_device *udev;
47 struct usb_host_config *actconfig;
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));
55 static DEVICE_ATTR_RO(bMaxPower);
57 static ssize_t configuration_show(struct device *dev,
58 struct device_attribute *attr, char *buf)
60 struct usb_device *udev;
61 struct usb_host_config *actconfig;
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);
69 static DEVICE_ATTR_RO(configuration);
71 /* configuration value is always present, and r/w */
72 usb_actconfig_show(bConfigurationValue, "%u\n");
74 static ssize_t bConfigurationValue_store(struct device *dev,
75 struct device_attribute *attr,
76 const char *buf, size_t count)
78 struct usb_device *udev = to_usb_device(dev);
79 int config, value;
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;
88 static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
89 bConfigurationValue_show, bConfigurationValue_store);
91 /* String fields */
92 #define usb_string_attr(name) \
93 static ssize_t name##_show(struct device *dev, \
94 struct device_attribute *attr, char *buf) \
95 { \
96 struct usb_device *udev; \
97 int retval; \
99 udev = to_usb_device(dev); \
100 usb_lock_device(udev); \
101 retval = sprintf(buf, "%s\n", udev->name); \
102 usb_unlock_device(udev); \
103 return retval; \
105 static DEVICE_ATTR_RO(name)
107 usb_string_attr(product);
108 usb_string_attr(manufacturer);
109 usb_string_attr(serial);
111 static ssize_t speed_show(struct device *dev, struct device_attribute *attr,
112 char *buf)
114 struct usb_device *udev;
115 char *speed;
117 udev = to_usb_device(dev);
119 switch (udev->speed) {
120 case USB_SPEED_LOW:
121 speed = "1.5";
122 break;
123 case USB_SPEED_UNKNOWN:
124 case USB_SPEED_FULL:
125 speed = "12";
126 break;
127 case USB_SPEED_HIGH:
128 speed = "480";
129 break;
130 case USB_SPEED_WIRELESS:
131 speed = "480";
132 break;
133 case USB_SPEED_SUPER:
134 speed = "5000";
135 break;
136 default:
137 speed = "unknown";
139 return sprintf(buf, "%s\n", speed);
141 static DEVICE_ATTR_RO(speed);
143 static ssize_t busnum_show(struct device *dev, struct device_attribute *attr,
144 char *buf)
146 struct usb_device *udev;
148 udev = to_usb_device(dev);
149 return sprintf(buf, "%d\n", udev->bus->busnum);
151 static DEVICE_ATTR_RO(busnum);
153 static ssize_t devnum_show(struct device *dev, struct device_attribute *attr,
154 char *buf)
156 struct usb_device *udev;
158 udev = to_usb_device(dev);
159 return sprintf(buf, "%d\n", udev->devnum);
161 static DEVICE_ATTR_RO(devnum);
163 static ssize_t devpath_show(struct device *dev, struct device_attribute *attr,
164 char *buf)
166 struct usb_device *udev;
168 udev = to_usb_device(dev);
169 return sprintf(buf, "%s\n", udev->devpath);
171 static DEVICE_ATTR_RO(devpath);
173 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
174 char *buf)
176 struct usb_device *udev;
177 u16 bcdUSB;
179 udev = to_usb_device(dev);
180 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
181 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
183 static DEVICE_ATTR_RO(version);
185 static ssize_t maxchild_show(struct device *dev, struct device_attribute *attr,
186 char *buf)
188 struct usb_device *udev;
190 udev = to_usb_device(dev);
191 return sprintf(buf, "%d\n", udev->maxchild);
193 static DEVICE_ATTR_RO(maxchild);
195 static ssize_t quirks_show(struct device *dev, struct device_attribute *attr,
196 char *buf)
198 struct usb_device *udev;
200 udev = to_usb_device(dev);
201 return sprintf(buf, "0x%x\n", udev->quirks);
203 static DEVICE_ATTR_RO(quirks);
205 static ssize_t avoid_reset_quirk_show(struct device *dev,
206 struct device_attribute *attr, char *buf)
208 struct usb_device *udev;
210 udev = to_usb_device(dev);
211 return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
214 static ssize_t avoid_reset_quirk_store(struct device *dev,
215 struct device_attribute *attr,
216 const char *buf, size_t count)
218 struct usb_device *udev = to_usb_device(dev);
219 int val;
221 if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
222 return -EINVAL;
223 usb_lock_device(udev);
224 if (val)
225 udev->quirks |= USB_QUIRK_RESET;
226 else
227 udev->quirks &= ~USB_QUIRK_RESET;
228 usb_unlock_device(udev);
229 return count;
231 static DEVICE_ATTR_RW(avoid_reset_quirk);
233 static ssize_t urbnum_show(struct device *dev, struct device_attribute *attr,
234 char *buf)
236 struct usb_device *udev;
238 udev = to_usb_device(dev);
239 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
241 static DEVICE_ATTR_RO(urbnum);
243 static ssize_t removable_show(struct device *dev, struct device_attribute *attr,
244 char *buf)
246 struct usb_device *udev;
247 char *state;
249 udev = to_usb_device(dev);
251 switch (udev->removable) {
252 case USB_DEVICE_REMOVABLE:
253 state = "removable";
254 break;
255 case USB_DEVICE_FIXED:
256 state = "fixed";
257 break;
258 default:
259 state = "unknown";
262 return sprintf(buf, "%s\n", state);
264 static DEVICE_ATTR_RO(removable);
266 static ssize_t ltm_capable_show(struct device *dev,
267 struct device_attribute *attr, char *buf)
269 if (usb_device_supports_ltm(to_usb_device(dev)))
270 return sprintf(buf, "%s\n", "yes");
271 return sprintf(buf, "%s\n", "no");
273 static DEVICE_ATTR_RO(ltm_capable);
275 #ifdef CONFIG_PM
277 static ssize_t persist_show(struct device *dev, struct device_attribute *attr,
278 char *buf)
280 struct usb_device *udev = to_usb_device(dev);
282 return sprintf(buf, "%d\n", udev->persist_enabled);
285 static ssize_t persist_store(struct device *dev, struct device_attribute *attr,
286 const char *buf, size_t count)
288 struct usb_device *udev = to_usb_device(dev);
289 int value;
291 /* Hubs are always enabled for USB_PERSIST */
292 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
293 return -EPERM;
295 if (sscanf(buf, "%d", &value) != 1)
296 return -EINVAL;
298 usb_lock_device(udev);
299 udev->persist_enabled = !!value;
300 usb_unlock_device(udev);
301 return count;
303 static DEVICE_ATTR_RW(persist);
305 static int add_persist_attributes(struct device *dev)
307 int rc = 0;
309 if (is_usb_device(dev)) {
310 struct usb_device *udev = to_usb_device(dev);
312 /* Hubs are automatically enabled for USB_PERSIST,
313 * no point in creating the attribute file.
315 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
316 rc = sysfs_add_file_to_group(&dev->kobj,
317 &dev_attr_persist.attr,
318 power_group_name);
320 return rc;
323 static void remove_persist_attributes(struct device *dev)
325 sysfs_remove_file_from_group(&dev->kobj,
326 &dev_attr_persist.attr,
327 power_group_name);
329 #else
331 #define add_persist_attributes(dev) 0
332 #define remove_persist_attributes(dev) do {} while (0)
334 #endif /* CONFIG_PM */
336 #ifdef CONFIG_PM_RUNTIME
338 static ssize_t connected_duration_show(struct device *dev,
339 struct device_attribute *attr, char *buf)
341 struct usb_device *udev = to_usb_device(dev);
343 return sprintf(buf, "%u\n",
344 jiffies_to_msecs(jiffies - udev->connect_time));
346 static DEVICE_ATTR_RO(connected_duration);
349 * If the device is resumed, the last time the device was suspended has
350 * been pre-subtracted from active_duration. We add the current time to
351 * get the duration that the device was actually active.
353 * If the device is suspended, the active_duration is up-to-date.
355 static ssize_t active_duration_show(struct device *dev,
356 struct device_attribute *attr, char *buf)
358 struct usb_device *udev = to_usb_device(dev);
359 int duration;
361 if (udev->state != USB_STATE_SUSPENDED)
362 duration = jiffies_to_msecs(jiffies + udev->active_duration);
363 else
364 duration = jiffies_to_msecs(udev->active_duration);
365 return sprintf(buf, "%u\n", duration);
367 static DEVICE_ATTR_RO(active_duration);
369 static ssize_t autosuspend_show(struct device *dev,
370 struct device_attribute *attr, char *buf)
372 return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
375 static ssize_t autosuspend_store(struct device *dev,
376 struct device_attribute *attr, const char *buf,
377 size_t count)
379 int value;
381 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
382 value <= -INT_MAX/1000)
383 return -EINVAL;
385 pm_runtime_set_autosuspend_delay(dev, value * 1000);
386 return count;
388 static DEVICE_ATTR_RW(autosuspend);
390 static const char on_string[] = "on";
391 static const char auto_string[] = "auto";
393 static void warn_level(void) {
394 static int level_warned;
396 if (!level_warned) {
397 level_warned = 1;
398 printk(KERN_WARNING "WARNING! power/level is deprecated; "
399 "use power/control instead\n");
403 static ssize_t level_show(struct device *dev, struct device_attribute *attr,
404 char *buf)
406 struct usb_device *udev = to_usb_device(dev);
407 const char *p = auto_string;
409 warn_level();
410 if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
411 p = on_string;
412 return sprintf(buf, "%s\n", p);
415 static ssize_t level_store(struct device *dev, struct device_attribute *attr,
416 const char *buf, size_t count)
418 struct usb_device *udev = to_usb_device(dev);
419 int len = count;
420 char *cp;
421 int rc = count;
423 warn_level();
424 cp = memchr(buf, '\n', count);
425 if (cp)
426 len = cp - buf;
428 usb_lock_device(udev);
430 if (len == sizeof on_string - 1 &&
431 strncmp(buf, on_string, len) == 0)
432 usb_disable_autosuspend(udev);
434 else if (len == sizeof auto_string - 1 &&
435 strncmp(buf, auto_string, len) == 0)
436 usb_enable_autosuspend(udev);
438 else
439 rc = -EINVAL;
441 usb_unlock_device(udev);
442 return rc;
444 static DEVICE_ATTR_RW(level);
446 static ssize_t usb2_hardware_lpm_show(struct device *dev,
447 struct device_attribute *attr, char *buf)
449 struct usb_device *udev = to_usb_device(dev);
450 const char *p;
452 if (udev->usb2_hw_lpm_allowed == 1)
453 p = "enabled";
454 else
455 p = "disabled";
457 return sprintf(buf, "%s\n", p);
460 static ssize_t usb2_hardware_lpm_store(struct device *dev,
461 struct device_attribute *attr,
462 const char *buf, size_t count)
464 struct usb_device *udev = to_usb_device(dev);
465 bool value;
466 int ret;
468 usb_lock_device(udev);
470 ret = strtobool(buf, &value);
472 if (!ret) {
473 udev->usb2_hw_lpm_allowed = value;
474 ret = usb_set_usb2_hardware_lpm(udev, value);
477 usb_unlock_device(udev);
479 if (!ret)
480 return count;
482 return ret;
484 static DEVICE_ATTR_RW(usb2_hardware_lpm);
486 static ssize_t usb2_lpm_l1_timeout_show(struct device *dev,
487 struct device_attribute *attr,
488 char *buf)
490 struct usb_device *udev = to_usb_device(dev);
491 return sprintf(buf, "%d\n", udev->l1_params.timeout);
494 static ssize_t usb2_lpm_l1_timeout_store(struct device *dev,
495 struct device_attribute *attr,
496 const char *buf, size_t count)
498 struct usb_device *udev = to_usb_device(dev);
499 u16 timeout;
501 if (kstrtou16(buf, 0, &timeout))
502 return -EINVAL;
504 udev->l1_params.timeout = timeout;
506 return count;
508 static DEVICE_ATTR_RW(usb2_lpm_l1_timeout);
510 static ssize_t usb2_lpm_besl_show(struct device *dev,
511 struct device_attribute *attr, char *buf)
513 struct usb_device *udev = to_usb_device(dev);
514 return sprintf(buf, "%d\n", udev->l1_params.besl);
517 static ssize_t usb2_lpm_besl_store(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf, size_t count)
521 struct usb_device *udev = to_usb_device(dev);
522 u8 besl;
524 if (kstrtou8(buf, 0, &besl) || besl > 15)
525 return -EINVAL;
527 udev->l1_params.besl = besl;
529 return count;
531 static DEVICE_ATTR_RW(usb2_lpm_besl);
533 static struct attribute *usb2_hardware_lpm_attr[] = {
534 &dev_attr_usb2_hardware_lpm.attr,
535 &dev_attr_usb2_lpm_l1_timeout.attr,
536 &dev_attr_usb2_lpm_besl.attr,
537 NULL,
539 static struct attribute_group usb2_hardware_lpm_attr_group = {
540 .name = power_group_name,
541 .attrs = usb2_hardware_lpm_attr,
544 static struct attribute *power_attrs[] = {
545 &dev_attr_autosuspend.attr,
546 &dev_attr_level.attr,
547 &dev_attr_connected_duration.attr,
548 &dev_attr_active_duration.attr,
549 NULL,
551 static struct attribute_group power_attr_group = {
552 .name = power_group_name,
553 .attrs = power_attrs,
556 static int add_power_attributes(struct device *dev)
558 int rc = 0;
560 if (is_usb_device(dev)) {
561 struct usb_device *udev = to_usb_device(dev);
562 rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
563 if (udev->usb2_hw_lpm_capable == 1)
564 rc = sysfs_merge_group(&dev->kobj,
565 &usb2_hardware_lpm_attr_group);
568 return rc;
571 static void remove_power_attributes(struct device *dev)
573 sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
574 sysfs_unmerge_group(&dev->kobj, &power_attr_group);
577 #else
579 #define add_power_attributes(dev) 0
580 #define remove_power_attributes(dev) do {} while (0)
582 #endif /* CONFIG_PM_RUNTIME */
585 /* Descriptor fields */
586 #define usb_descriptor_attr_le16(field, format_string) \
587 static ssize_t \
588 field##_show(struct device *dev, struct device_attribute *attr, \
589 char *buf) \
591 struct usb_device *udev; \
593 udev = to_usb_device(dev); \
594 return sprintf(buf, format_string, \
595 le16_to_cpu(udev->descriptor.field)); \
597 static DEVICE_ATTR_RO(field)
599 usb_descriptor_attr_le16(idVendor, "%04x\n");
600 usb_descriptor_attr_le16(idProduct, "%04x\n");
601 usb_descriptor_attr_le16(bcdDevice, "%04x\n");
603 #define usb_descriptor_attr(field, format_string) \
604 static ssize_t \
605 field##_show(struct device *dev, struct device_attribute *attr, \
606 char *buf) \
608 struct usb_device *udev; \
610 udev = to_usb_device(dev); \
611 return sprintf(buf, format_string, udev->descriptor.field); \
613 static DEVICE_ATTR_RO(field)
615 usb_descriptor_attr(bDeviceClass, "%02x\n");
616 usb_descriptor_attr(bDeviceSubClass, "%02x\n");
617 usb_descriptor_attr(bDeviceProtocol, "%02x\n");
618 usb_descriptor_attr(bNumConfigurations, "%d\n");
619 usb_descriptor_attr(bMaxPacketSize0, "%d\n");
622 /* show if the device is authorized (1) or not (0) */
623 static ssize_t authorized_show(struct device *dev,
624 struct device_attribute *attr, char *buf)
626 struct usb_device *usb_dev = to_usb_device(dev);
627 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
631 * Authorize a device to be used in the system
633 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
635 static ssize_t authorized_store(struct device *dev,
636 struct device_attribute *attr, const char *buf,
637 size_t size)
639 ssize_t result;
640 struct usb_device *usb_dev = to_usb_device(dev);
641 unsigned val;
642 result = sscanf(buf, "%u\n", &val);
643 if (result != 1)
644 result = -EINVAL;
645 else if (val == 0)
646 result = usb_deauthorize_device(usb_dev);
647 else
648 result = usb_authorize_device(usb_dev);
649 return result < 0? result : size;
651 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, S_IRUGO | S_IWUSR,
652 authorized_show, authorized_store);
654 /* "Safely remove a device" */
655 static ssize_t remove_store(struct device *dev, struct device_attribute *attr,
656 const char *buf, size_t count)
658 struct usb_device *udev = to_usb_device(dev);
659 int rc = 0;
661 usb_lock_device(udev);
662 if (udev->state != USB_STATE_NOTATTACHED) {
664 /* To avoid races, first unconfigure and then remove */
665 usb_set_configuration(udev, -1);
666 rc = usb_remove_device(udev);
668 if (rc == 0)
669 rc = count;
670 usb_unlock_device(udev);
671 return rc;
673 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, S_IWUSR, NULL, remove_store);
676 static struct attribute *dev_attrs[] = {
677 /* current configuration's attributes */
678 &dev_attr_configuration.attr,
679 &dev_attr_bNumInterfaces.attr,
680 &dev_attr_bConfigurationValue.attr,
681 &dev_attr_bmAttributes.attr,
682 &dev_attr_bMaxPower.attr,
683 /* device attributes */
684 &dev_attr_urbnum.attr,
685 &dev_attr_idVendor.attr,
686 &dev_attr_idProduct.attr,
687 &dev_attr_bcdDevice.attr,
688 &dev_attr_bDeviceClass.attr,
689 &dev_attr_bDeviceSubClass.attr,
690 &dev_attr_bDeviceProtocol.attr,
691 &dev_attr_bNumConfigurations.attr,
692 &dev_attr_bMaxPacketSize0.attr,
693 &dev_attr_speed.attr,
694 &dev_attr_busnum.attr,
695 &dev_attr_devnum.attr,
696 &dev_attr_devpath.attr,
697 &dev_attr_version.attr,
698 &dev_attr_maxchild.attr,
699 &dev_attr_quirks.attr,
700 &dev_attr_avoid_reset_quirk.attr,
701 &dev_attr_authorized.attr,
702 &dev_attr_remove.attr,
703 &dev_attr_removable.attr,
704 &dev_attr_ltm_capable.attr,
705 NULL,
707 static struct attribute_group dev_attr_grp = {
708 .attrs = dev_attrs,
711 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
712 * accordingly.
714 static struct attribute *dev_string_attrs[] = {
715 &dev_attr_manufacturer.attr,
716 &dev_attr_product.attr,
717 &dev_attr_serial.attr,
718 NULL
721 static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
722 struct attribute *a, int n)
724 struct device *dev = container_of(kobj, struct device, kobj);
725 struct usb_device *udev = to_usb_device(dev);
727 if (a == &dev_attr_manufacturer.attr) {
728 if (udev->manufacturer == NULL)
729 return 0;
730 } else if (a == &dev_attr_product.attr) {
731 if (udev->product == NULL)
732 return 0;
733 } else if (a == &dev_attr_serial.attr) {
734 if (udev->serial == NULL)
735 return 0;
737 return a->mode;
740 static struct attribute_group dev_string_attr_grp = {
741 .attrs = dev_string_attrs,
742 .is_visible = dev_string_attrs_are_visible,
745 const struct attribute_group *usb_device_groups[] = {
746 &dev_attr_grp,
747 &dev_string_attr_grp,
748 NULL
751 /* Binary descriptors */
753 static ssize_t
754 read_descriptors(struct file *filp, struct kobject *kobj,
755 struct bin_attribute *attr,
756 char *buf, loff_t off, size_t count)
758 struct device *dev = container_of(kobj, struct device, kobj);
759 struct usb_device *udev = to_usb_device(dev);
760 size_t nleft = count;
761 size_t srclen, n;
762 int cfgno;
763 void *src;
765 /* The binary attribute begins with the device descriptor.
766 * Following that are the raw descriptor entries for all the
767 * configurations (config plus subsidiary descriptors).
769 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
770 nleft > 0; ++cfgno) {
771 if (cfgno < 0) {
772 src = &udev->descriptor;
773 srclen = sizeof(struct usb_device_descriptor);
774 } else {
775 src = udev->rawdescriptors[cfgno];
776 srclen = __le16_to_cpu(udev->config[cfgno].desc.
777 wTotalLength);
779 if (off < srclen) {
780 n = min(nleft, srclen - (size_t) off);
781 memcpy(buf, src + off, n);
782 nleft -= n;
783 buf += n;
784 off = 0;
785 } else {
786 off -= srclen;
789 return count - nleft;
792 static struct bin_attribute dev_bin_attr_descriptors = {
793 .attr = {.name = "descriptors", .mode = 0444},
794 .read = read_descriptors,
795 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
798 int usb_create_sysfs_dev_files(struct usb_device *udev)
800 struct device *dev = &udev->dev;
801 int retval;
803 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
804 if (retval)
805 goto error;
807 retval = add_persist_attributes(dev);
808 if (retval)
809 goto error;
811 retval = add_power_attributes(dev);
812 if (retval)
813 goto error;
814 return retval;
815 error:
816 usb_remove_sysfs_dev_files(udev);
817 return retval;
820 void usb_remove_sysfs_dev_files(struct usb_device *udev)
822 struct device *dev = &udev->dev;
824 remove_power_attributes(dev);
825 remove_persist_attributes(dev);
826 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
829 /* Interface Accociation Descriptor fields */
830 #define usb_intf_assoc_attr(field, format_string) \
831 static ssize_t \
832 iad_##field##_show(struct device *dev, struct device_attribute *attr, \
833 char *buf) \
835 struct usb_interface *intf = to_usb_interface(dev); \
837 return sprintf(buf, format_string, \
838 intf->intf_assoc->field); \
840 static DEVICE_ATTR_RO(iad_##field)
842 usb_intf_assoc_attr(bFirstInterface, "%02x\n");
843 usb_intf_assoc_attr(bInterfaceCount, "%02d\n");
844 usb_intf_assoc_attr(bFunctionClass, "%02x\n");
845 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n");
846 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n");
848 /* Interface fields */
849 #define usb_intf_attr(field, format_string) \
850 static ssize_t \
851 field##_show(struct device *dev, struct device_attribute *attr, \
852 char *buf) \
854 struct usb_interface *intf = to_usb_interface(dev); \
856 return sprintf(buf, format_string, \
857 intf->cur_altsetting->desc.field); \
859 static DEVICE_ATTR_RO(field)
861 usb_intf_attr(bInterfaceNumber, "%02x\n");
862 usb_intf_attr(bAlternateSetting, "%2d\n");
863 usb_intf_attr(bNumEndpoints, "%02x\n");
864 usb_intf_attr(bInterfaceClass, "%02x\n");
865 usb_intf_attr(bInterfaceSubClass, "%02x\n");
866 usb_intf_attr(bInterfaceProtocol, "%02x\n");
868 static ssize_t interface_show(struct device *dev, struct device_attribute *attr,
869 char *buf)
871 struct usb_interface *intf;
872 char *string;
874 intf = to_usb_interface(dev);
875 string = intf->cur_altsetting->string;
876 barrier(); /* The altsetting might change! */
878 if (!string)
879 return 0;
880 return sprintf(buf, "%s\n", string);
882 static DEVICE_ATTR_RO(interface);
884 static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
885 char *buf)
887 struct usb_interface *intf;
888 struct usb_device *udev;
889 struct usb_host_interface *alt;
891 intf = to_usb_interface(dev);
892 udev = interface_to_usbdev(intf);
893 alt = intf->cur_altsetting;
895 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
896 "ic%02Xisc%02Xip%02Xin%02X\n",
897 le16_to_cpu(udev->descriptor.idVendor),
898 le16_to_cpu(udev->descriptor.idProduct),
899 le16_to_cpu(udev->descriptor.bcdDevice),
900 udev->descriptor.bDeviceClass,
901 udev->descriptor.bDeviceSubClass,
902 udev->descriptor.bDeviceProtocol,
903 alt->desc.bInterfaceClass,
904 alt->desc.bInterfaceSubClass,
905 alt->desc.bInterfaceProtocol,
906 alt->desc.bInterfaceNumber);
908 static DEVICE_ATTR_RO(modalias);
910 static ssize_t supports_autosuspend_show(struct device *dev,
911 struct device_attribute *attr,
912 char *buf)
914 struct usb_interface *intf;
915 struct usb_device *udev;
916 int ret;
918 intf = to_usb_interface(dev);
919 udev = interface_to_usbdev(intf);
921 usb_lock_device(udev);
922 /* Devices will be autosuspended even when an interface isn't claimed */
923 if (!intf->dev.driver ||
924 to_usb_driver(intf->dev.driver)->supports_autosuspend)
925 ret = sprintf(buf, "%u\n", 1);
926 else
927 ret = sprintf(buf, "%u\n", 0);
928 usb_unlock_device(udev);
930 return ret;
932 static DEVICE_ATTR_RO(supports_autosuspend);
934 static struct attribute *intf_attrs[] = {
935 &dev_attr_bInterfaceNumber.attr,
936 &dev_attr_bAlternateSetting.attr,
937 &dev_attr_bNumEndpoints.attr,
938 &dev_attr_bInterfaceClass.attr,
939 &dev_attr_bInterfaceSubClass.attr,
940 &dev_attr_bInterfaceProtocol.attr,
941 &dev_attr_modalias.attr,
942 &dev_attr_supports_autosuspend.attr,
943 NULL,
945 static struct attribute_group intf_attr_grp = {
946 .attrs = intf_attrs,
949 static struct attribute *intf_assoc_attrs[] = {
950 &dev_attr_iad_bFirstInterface.attr,
951 &dev_attr_iad_bInterfaceCount.attr,
952 &dev_attr_iad_bFunctionClass.attr,
953 &dev_attr_iad_bFunctionSubClass.attr,
954 &dev_attr_iad_bFunctionProtocol.attr,
955 NULL,
958 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
959 struct attribute *a, int n)
961 struct device *dev = container_of(kobj, struct device, kobj);
962 struct usb_interface *intf = to_usb_interface(dev);
964 if (intf->intf_assoc == NULL)
965 return 0;
966 return a->mode;
969 static struct attribute_group intf_assoc_attr_grp = {
970 .attrs = intf_assoc_attrs,
971 .is_visible = intf_assoc_attrs_are_visible,
974 const struct attribute_group *usb_interface_groups[] = {
975 &intf_attr_grp,
976 &intf_assoc_attr_grp,
977 NULL
980 void usb_create_sysfs_intf_files(struct usb_interface *intf)
982 struct usb_device *udev = interface_to_usbdev(intf);
983 struct usb_host_interface *alt = intf->cur_altsetting;
985 if (intf->sysfs_files_created || intf->unregistering)
986 return;
988 if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
989 alt->string = usb_cache_string(udev, alt->desc.iInterface);
990 if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
991 ; /* We don't actually care if the function fails. */
992 intf->sysfs_files_created = 1;
995 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
997 if (!intf->sysfs_files_created)
998 return;
1000 device_remove_file(&intf->dev, &dev_attr_interface);
1001 intf->sysfs_files_created = 0;