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>
18 /* Active configuration fields */
19 #define usb_actconfig_show(field, multiplier, format_string) \
20 static ssize_t show_##field(struct device *dev, \
21 struct device_attribute *attr, char *buf) \
23 struct usb_device *udev; \
24 struct usb_host_config *actconfig; \
26 udev = to_usb_device(dev); \
27 actconfig = udev->actconfig; \
29 return sprintf(buf, format_string, \
30 actconfig->desc.field * multiplier); \
35 #define usb_actconfig_attr(field, multiplier, format_string) \
36 usb_actconfig_show(field, multiplier, format_string) \
37 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
39 usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
40 usb_actconfig_attr(bmAttributes, 1, "%2x\n")
41 usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
43 static ssize_t show_configuration_string(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) || (!actconfig->string))
53 return sprintf(buf, "%s\n", actconfig->string);
55 static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
57 /* configuration value is always present, and r/w */
58 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
61 set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
62 const char *buf, size_t count)
64 struct usb_device *udev = to_usb_device(dev);
67 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
69 usb_lock_device(udev);
70 value = usb_set_configuration(udev, config);
71 usb_unlock_device(udev);
72 return (value < 0) ? value : count;
75 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
76 show_bConfigurationValue, set_bConfigurationValue);
79 #define usb_string_attr(name) \
80 static ssize_t show_##name(struct device *dev, \
81 struct device_attribute *attr, char *buf) \
83 struct usb_device *udev; \
85 udev = to_usb_device(dev); \
86 return sprintf(buf, "%s\n", udev->name); \
88 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
90 usb_string_attr(product);
91 usb_string_attr(manufacturer);
92 usb_string_attr(serial);
95 show_speed(struct device *dev, struct device_attribute *attr, char *buf)
97 struct usb_device *udev;
100 udev = to_usb_device(dev);
102 switch (udev->speed) {
106 case USB_SPEED_UNKNOWN:
116 return sprintf(buf, "%s\n", speed);
118 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
121 show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
123 struct usb_device *udev;
125 udev = to_usb_device(dev);
126 return sprintf(buf, "%d\n", udev->bus->busnum);
128 static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
131 show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
133 struct usb_device *udev;
135 udev = to_usb_device(dev);
136 return sprintf(buf, "%d\n", udev->devnum);
138 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
141 show_version(struct device *dev, struct device_attribute *attr, char *buf)
143 struct usb_device *udev;
146 udev = to_usb_device(dev);
147 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
148 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
150 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
153 show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
155 struct usb_device *udev;
157 udev = to_usb_device(dev);
158 return sprintf(buf, "%d\n", udev->maxchild);
160 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
163 show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
165 struct usb_device *udev;
167 udev = to_usb_device(dev);
168 return sprintf(buf, "0x%x\n", udev->quirks);
170 static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
172 #ifdef CONFIG_USB_SUSPEND
175 show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
177 struct usb_device *udev = to_usb_device(dev);
179 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
183 set_autosuspend(struct device *dev, struct device_attribute *attr,
184 const char *buf, size_t count)
186 struct usb_device *udev = to_usb_device(dev);
189 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
190 value <= - INT_MAX/HZ)
194 udev->autosuspend_delay = value;
196 usb_try_autosuspend_device(udev);
198 if (usb_autoresume_device(udev) == 0)
199 usb_autosuspend_device(udev);
204 static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
205 show_autosuspend, set_autosuspend);
207 static const char on_string[] = "on";
208 static const char auto_string[] = "auto";
209 static const char suspend_string[] = "suspend";
212 show_level(struct device *dev, struct device_attribute *attr, char *buf)
214 struct usb_device *udev = to_usb_device(dev);
215 const char *p = auto_string;
217 if (udev->state == USB_STATE_SUSPENDED) {
218 if (udev->autoresume_disabled)
221 if (udev->autosuspend_disabled)
224 return sprintf(buf, "%s\n", p);
228 set_level(struct device *dev, struct device_attribute *attr,
229 const char *buf, size_t count)
231 struct usb_device *udev = to_usb_device(dev);
235 int old_autosuspend_disabled, old_autoresume_disabled;
237 cp = memchr(buf, '\n', count);
241 usb_lock_device(udev);
242 old_autosuspend_disabled = udev->autosuspend_disabled;
243 old_autoresume_disabled = udev->autoresume_disabled;
245 /* Setting the flags without calling usb_pm_lock is a subject to
246 * races, but who cares...
248 if (len == sizeof on_string - 1 &&
249 strncmp(buf, on_string, len) == 0) {
250 udev->autosuspend_disabled = 1;
251 udev->autoresume_disabled = 0;
252 rc = usb_external_resume_device(udev);
254 } else if (len == sizeof auto_string - 1 &&
255 strncmp(buf, auto_string, len) == 0) {
256 udev->autosuspend_disabled = 0;
257 udev->autoresume_disabled = 0;
258 rc = usb_external_resume_device(udev);
260 } else if (len == sizeof suspend_string - 1 &&
261 strncmp(buf, suspend_string, len) == 0) {
262 udev->autosuspend_disabled = 0;
263 udev->autoresume_disabled = 1;
264 rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
270 udev->autosuspend_disabled = old_autosuspend_disabled;
271 udev->autoresume_disabled = old_autoresume_disabled;
273 usb_unlock_device(udev);
274 return (rc < 0 ? rc : count);
277 static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
279 static char power_group[] = "power";
281 static int add_power_attributes(struct device *dev)
285 if (is_usb_device(dev)) {
286 rc = sysfs_add_file_to_group(&dev->kobj,
287 &dev_attr_autosuspend.attr,
290 rc = sysfs_add_file_to_group(&dev->kobj,
291 &dev_attr_level.attr,
297 static void remove_power_attributes(struct device *dev)
299 sysfs_remove_file_from_group(&dev->kobj,
300 &dev_attr_level.attr,
302 sysfs_remove_file_from_group(&dev->kobj,
303 &dev_attr_autosuspend.attr,
309 #define add_power_attributes(dev) 0
310 #define remove_power_attributes(dev) do {} while (0)
312 #endif /* CONFIG_USB_SUSPEND */
314 /* Descriptor fields */
315 #define usb_descriptor_attr_le16(field, format_string) \
317 show_##field(struct device *dev, struct device_attribute *attr, \
320 struct usb_device *udev; \
322 udev = to_usb_device(dev); \
323 return sprintf(buf, format_string, \
324 le16_to_cpu(udev->descriptor.field)); \
326 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
328 usb_descriptor_attr_le16(idVendor, "%04x\n")
329 usb_descriptor_attr_le16(idProduct, "%04x\n")
330 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
332 #define usb_descriptor_attr(field, format_string) \
334 show_##field(struct device *dev, struct device_attribute *attr, \
337 struct usb_device *udev; \
339 udev = to_usb_device(dev); \
340 return sprintf(buf, format_string, udev->descriptor.field); \
342 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
344 usb_descriptor_attr(bDeviceClass, "%02x\n")
345 usb_descriptor_attr(bDeviceSubClass, "%02x\n")
346 usb_descriptor_attr(bDeviceProtocol, "%02x\n")
347 usb_descriptor_attr(bNumConfigurations, "%d\n")
348 usb_descriptor_attr(bMaxPacketSize0, "%d\n")
350 static struct attribute *dev_attrs[] = {
351 /* current configuration's attributes */
352 &dev_attr_configuration.attr,
353 &dev_attr_bNumInterfaces.attr,
354 &dev_attr_bConfigurationValue.attr,
355 &dev_attr_bmAttributes.attr,
356 &dev_attr_bMaxPower.attr,
357 /* device attributes */
358 &dev_attr_idVendor.attr,
359 &dev_attr_idProduct.attr,
360 &dev_attr_bcdDevice.attr,
361 &dev_attr_bDeviceClass.attr,
362 &dev_attr_bDeviceSubClass.attr,
363 &dev_attr_bDeviceProtocol.attr,
364 &dev_attr_bNumConfigurations.attr,
365 &dev_attr_bMaxPacketSize0.attr,
366 &dev_attr_speed.attr,
367 &dev_attr_busnum.attr,
368 &dev_attr_devnum.attr,
369 &dev_attr_version.attr,
370 &dev_attr_maxchild.attr,
371 &dev_attr_quirks.attr,
374 static struct attribute_group dev_attr_grp = {
378 int usb_create_sysfs_dev_files(struct usb_device *udev)
380 struct device *dev = &udev->dev;
383 retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
387 retval = add_power_attributes(dev);
391 if (udev->manufacturer) {
392 retval = device_create_file(dev, &dev_attr_manufacturer);
397 retval = device_create_file(dev, &dev_attr_product);
402 retval = device_create_file(dev, &dev_attr_serial);
406 retval = usb_create_ep_files(dev, &udev->ep0, udev);
411 usb_remove_sysfs_dev_files(udev);
415 void usb_remove_sysfs_dev_files(struct usb_device *udev)
417 struct device *dev = &udev->dev;
419 usb_remove_ep_files(&udev->ep0);
420 device_remove_file(dev, &dev_attr_manufacturer);
421 device_remove_file(dev, &dev_attr_product);
422 device_remove_file(dev, &dev_attr_serial);
423 remove_power_attributes(dev);
424 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
427 /* Interface fields */
428 #define usb_intf_attr(field, format_string) \
430 show_##field(struct device *dev, struct device_attribute *attr, \
433 struct usb_interface *intf = to_usb_interface(dev); \
435 return sprintf(buf, format_string, \
436 intf->cur_altsetting->desc.field); \
438 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
440 usb_intf_attr(bInterfaceNumber, "%02x\n")
441 usb_intf_attr(bAlternateSetting, "%2d\n")
442 usb_intf_attr(bNumEndpoints, "%02x\n")
443 usb_intf_attr(bInterfaceClass, "%02x\n")
444 usb_intf_attr(bInterfaceSubClass, "%02x\n")
445 usb_intf_attr(bInterfaceProtocol, "%02x\n")
447 static ssize_t show_interface_string(struct device *dev,
448 struct device_attribute *attr, char *buf)
450 struct usb_interface *intf;
451 struct usb_device *udev;
454 intf = to_usb_interface(dev);
455 udev = interface_to_usbdev(intf);
456 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
463 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
465 static ssize_t show_modalias(struct device *dev,
466 struct device_attribute *attr, char *buf)
468 struct usb_interface *intf;
469 struct usb_device *udev;
470 struct usb_host_interface *alt;
472 intf = to_usb_interface(dev);
473 udev = interface_to_usbdev(intf);
474 alt = intf->cur_altsetting;
476 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
477 "ic%02Xisc%02Xip%02X\n",
478 le16_to_cpu(udev->descriptor.idVendor),
479 le16_to_cpu(udev->descriptor.idProduct),
480 le16_to_cpu(udev->descriptor.bcdDevice),
481 udev->descriptor.bDeviceClass,
482 udev->descriptor.bDeviceSubClass,
483 udev->descriptor.bDeviceProtocol,
484 alt->desc.bInterfaceClass,
485 alt->desc.bInterfaceSubClass,
486 alt->desc.bInterfaceProtocol);
488 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
490 static struct attribute *intf_attrs[] = {
491 &dev_attr_bInterfaceNumber.attr,
492 &dev_attr_bAlternateSetting.attr,
493 &dev_attr_bNumEndpoints.attr,
494 &dev_attr_bInterfaceClass.attr,
495 &dev_attr_bInterfaceSubClass.attr,
496 &dev_attr_bInterfaceProtocol.attr,
497 &dev_attr_modalias.attr,
500 static struct attribute_group intf_attr_grp = {
504 static inline void usb_create_intf_ep_files(struct usb_interface *intf,
505 struct usb_device *udev)
507 struct usb_host_interface *iface_desc;
510 iface_desc = intf->cur_altsetting;
511 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
512 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
516 static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
518 struct usb_host_interface *iface_desc;
521 iface_desc = intf->cur_altsetting;
522 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
523 usb_remove_ep_files(&iface_desc->endpoint[i]);
526 int usb_create_sysfs_intf_files(struct usb_interface *intf)
528 struct device *dev = &intf->dev;
529 struct usb_device *udev = interface_to_usbdev(intf);
530 struct usb_host_interface *alt = intf->cur_altsetting;
533 retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
537 if (alt->string == NULL)
538 alt->string = usb_cache_string(udev, alt->desc.iInterface);
540 retval = device_create_file(dev, &dev_attr_interface);
541 usb_create_intf_ep_files(intf, udev);
545 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
547 struct device *dev = &intf->dev;
549 usb_remove_intf_ep_files(intf);
550 device_remove_file(dev, &dev_attr_interface);
551 sysfs_remove_group(&dev->kobj, &intf_attr_grp);