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_devpath(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
143 struct usb_device
*udev
;
145 udev
= to_usb_device(dev
);
146 return sprintf(buf
, "%s\n", udev
->devpath
);
148 static DEVICE_ATTR(devpath
, S_IRUGO
, show_devpath
, NULL
);
151 show_version(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
153 struct usb_device
*udev
;
156 udev
= to_usb_device(dev
);
157 bcdUSB
= le16_to_cpu(udev
->descriptor
.bcdUSB
);
158 return sprintf(buf
, "%2x.%02x\n", bcdUSB
>> 8, bcdUSB
& 0xff);
160 static DEVICE_ATTR(version
, S_IRUGO
, show_version
, NULL
);
163 show_maxchild(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
165 struct usb_device
*udev
;
167 udev
= to_usb_device(dev
);
168 return sprintf(buf
, "%d\n", udev
->maxchild
);
170 static DEVICE_ATTR(maxchild
, S_IRUGO
, show_maxchild
, NULL
);
173 show_quirks(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
175 struct usb_device
*udev
;
177 udev
= to_usb_device(dev
);
178 return sprintf(buf
, "0x%x\n", udev
->quirks
);
180 static DEVICE_ATTR(quirks
, S_IRUGO
, show_quirks
, NULL
);
182 #ifdef CONFIG_USB_SUSPEND
185 show_autosuspend(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
187 struct usb_device
*udev
= to_usb_device(dev
);
189 return sprintf(buf
, "%d\n", udev
->autosuspend_delay
/ HZ
);
193 set_autosuspend(struct device
*dev
, struct device_attribute
*attr
,
194 const char *buf
, size_t count
)
196 struct usb_device
*udev
= to_usb_device(dev
);
199 if (sscanf(buf
, "%d", &value
) != 1 || value
>= INT_MAX
/HZ
||
200 value
<= - INT_MAX
/HZ
)
204 udev
->autosuspend_delay
= value
;
206 usb_try_autosuspend_device(udev
);
208 if (usb_autoresume_device(udev
) == 0)
209 usb_autosuspend_device(udev
);
214 static DEVICE_ATTR(autosuspend
, S_IRUGO
| S_IWUSR
,
215 show_autosuspend
, set_autosuspend
);
217 static const char on_string
[] = "on";
218 static const char auto_string
[] = "auto";
219 static const char suspend_string
[] = "suspend";
222 show_level(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
224 struct usb_device
*udev
= to_usb_device(dev
);
225 const char *p
= auto_string
;
227 if (udev
->state
== USB_STATE_SUSPENDED
) {
228 if (udev
->autoresume_disabled
)
231 if (udev
->autosuspend_disabled
)
234 return sprintf(buf
, "%s\n", p
);
238 set_level(struct device
*dev
, struct device_attribute
*attr
,
239 const char *buf
, size_t count
)
241 struct usb_device
*udev
= to_usb_device(dev
);
245 int old_autosuspend_disabled
, old_autoresume_disabled
;
247 cp
= memchr(buf
, '\n', count
);
251 usb_lock_device(udev
);
252 old_autosuspend_disabled
= udev
->autosuspend_disabled
;
253 old_autoresume_disabled
= udev
->autoresume_disabled
;
255 /* Setting the flags without calling usb_pm_lock is a subject to
256 * races, but who cares...
258 if (len
== sizeof on_string
- 1 &&
259 strncmp(buf
, on_string
, len
) == 0) {
260 udev
->autosuspend_disabled
= 1;
261 udev
->autoresume_disabled
= 0;
262 rc
= usb_external_resume_device(udev
);
264 } else if (len
== sizeof auto_string
- 1 &&
265 strncmp(buf
, auto_string
, len
) == 0) {
266 udev
->autosuspend_disabled
= 0;
267 udev
->autoresume_disabled
= 0;
268 rc
= usb_external_resume_device(udev
);
270 } else if (len
== sizeof suspend_string
- 1 &&
271 strncmp(buf
, suspend_string
, len
) == 0) {
272 udev
->autosuspend_disabled
= 0;
273 udev
->autoresume_disabled
= 1;
274 rc
= usb_external_suspend_device(udev
, PMSG_SUSPEND
);
280 udev
->autosuspend_disabled
= old_autosuspend_disabled
;
281 udev
->autoresume_disabled
= old_autoresume_disabled
;
283 usb_unlock_device(udev
);
284 return (rc
< 0 ? rc
: count
);
287 static DEVICE_ATTR(level
, S_IRUGO
| S_IWUSR
, show_level
, set_level
);
289 static char power_group
[] = "power";
291 static int add_power_attributes(struct device
*dev
)
295 if (is_usb_device(dev
)) {
296 rc
= sysfs_add_file_to_group(&dev
->kobj
,
297 &dev_attr_autosuspend
.attr
,
300 rc
= sysfs_add_file_to_group(&dev
->kobj
,
301 &dev_attr_level
.attr
,
307 static void remove_power_attributes(struct device
*dev
)
309 sysfs_remove_file_from_group(&dev
->kobj
,
310 &dev_attr_level
.attr
,
312 sysfs_remove_file_from_group(&dev
->kobj
,
313 &dev_attr_autosuspend
.attr
,
319 #define add_power_attributes(dev) 0
320 #define remove_power_attributes(dev) do {} while (0)
322 #endif /* CONFIG_USB_SUSPEND */
324 /* Descriptor fields */
325 #define usb_descriptor_attr_le16(field, format_string) \
327 show_##field(struct device *dev, struct device_attribute *attr, \
330 struct usb_device *udev; \
332 udev = to_usb_device(dev); \
333 return sprintf(buf, format_string, \
334 le16_to_cpu(udev->descriptor.field)); \
336 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
338 usb_descriptor_attr_le16(idVendor
, "%04x\n")
339 usb_descriptor_attr_le16(idProduct
, "%04x\n")
340 usb_descriptor_attr_le16(bcdDevice
, "%04x\n")
342 #define usb_descriptor_attr(field, format_string) \
344 show_##field(struct device *dev, struct device_attribute *attr, \
347 struct usb_device *udev; \
349 udev = to_usb_device(dev); \
350 return sprintf(buf, format_string, udev->descriptor.field); \
352 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
354 usb_descriptor_attr(bDeviceClass
, "%02x\n")
355 usb_descriptor_attr(bDeviceSubClass
, "%02x\n")
356 usb_descriptor_attr(bDeviceProtocol
, "%02x\n")
357 usb_descriptor_attr(bNumConfigurations
, "%d\n")
358 usb_descriptor_attr(bMaxPacketSize0
, "%d\n")
360 static struct attribute
*dev_attrs
[] = {
361 /* current configuration's attributes */
362 &dev_attr_configuration
.attr
,
363 &dev_attr_bNumInterfaces
.attr
,
364 &dev_attr_bConfigurationValue
.attr
,
365 &dev_attr_bmAttributes
.attr
,
366 &dev_attr_bMaxPower
.attr
,
367 /* device attributes */
368 &dev_attr_idVendor
.attr
,
369 &dev_attr_idProduct
.attr
,
370 &dev_attr_bcdDevice
.attr
,
371 &dev_attr_bDeviceClass
.attr
,
372 &dev_attr_bDeviceSubClass
.attr
,
373 &dev_attr_bDeviceProtocol
.attr
,
374 &dev_attr_bNumConfigurations
.attr
,
375 &dev_attr_bMaxPacketSize0
.attr
,
376 &dev_attr_speed
.attr
,
377 &dev_attr_busnum
.attr
,
378 &dev_attr_devnum
.attr
,
379 &dev_attr_devpath
.attr
,
380 &dev_attr_version
.attr
,
381 &dev_attr_maxchild
.attr
,
382 &dev_attr_quirks
.attr
,
385 static struct attribute_group dev_attr_grp
= {
389 int usb_create_sysfs_dev_files(struct usb_device
*udev
)
391 struct device
*dev
= &udev
->dev
;
394 retval
= sysfs_create_group(&dev
->kobj
, &dev_attr_grp
);
398 retval
= add_power_attributes(dev
);
402 if (udev
->manufacturer
) {
403 retval
= device_create_file(dev
, &dev_attr_manufacturer
);
408 retval
= device_create_file(dev
, &dev_attr_product
);
413 retval
= device_create_file(dev
, &dev_attr_serial
);
417 retval
= usb_create_ep_files(dev
, &udev
->ep0
, udev
);
422 usb_remove_sysfs_dev_files(udev
);
426 void usb_remove_sysfs_dev_files(struct usb_device
*udev
)
428 struct device
*dev
= &udev
->dev
;
430 usb_remove_ep_files(&udev
->ep0
);
431 device_remove_file(dev
, &dev_attr_manufacturer
);
432 device_remove_file(dev
, &dev_attr_product
);
433 device_remove_file(dev
, &dev_attr_serial
);
434 remove_power_attributes(dev
);
435 sysfs_remove_group(&dev
->kobj
, &dev_attr_grp
);
438 /* Interface fields */
439 #define usb_intf_attr(field, format_string) \
441 show_##field(struct device *dev, struct device_attribute *attr, \
444 struct usb_interface *intf = to_usb_interface(dev); \
446 return sprintf(buf, format_string, \
447 intf->cur_altsetting->desc.field); \
449 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
451 usb_intf_attr(bInterfaceNumber
, "%02x\n")
452 usb_intf_attr(bAlternateSetting
, "%2d\n")
453 usb_intf_attr(bNumEndpoints
, "%02x\n")
454 usb_intf_attr(bInterfaceClass
, "%02x\n")
455 usb_intf_attr(bInterfaceSubClass
, "%02x\n")
456 usb_intf_attr(bInterfaceProtocol
, "%02x\n")
458 static ssize_t
show_interface_string(struct device
*dev
,
459 struct device_attribute
*attr
, char *buf
)
461 struct usb_interface
*intf
;
462 struct usb_device
*udev
;
465 intf
= to_usb_interface(dev
);
466 udev
= interface_to_usbdev(intf
);
467 len
= snprintf(buf
, 256, "%s", intf
->cur_altsetting
->string
);
474 static DEVICE_ATTR(interface
, S_IRUGO
, show_interface_string
, NULL
);
476 static ssize_t
show_modalias(struct device
*dev
,
477 struct device_attribute
*attr
, char *buf
)
479 struct usb_interface
*intf
;
480 struct usb_device
*udev
;
481 struct usb_host_interface
*alt
;
483 intf
= to_usb_interface(dev
);
484 udev
= interface_to_usbdev(intf
);
485 alt
= intf
->cur_altsetting
;
487 return sprintf(buf
, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
488 "ic%02Xisc%02Xip%02X\n",
489 le16_to_cpu(udev
->descriptor
.idVendor
),
490 le16_to_cpu(udev
->descriptor
.idProduct
),
491 le16_to_cpu(udev
->descriptor
.bcdDevice
),
492 udev
->descriptor
.bDeviceClass
,
493 udev
->descriptor
.bDeviceSubClass
,
494 udev
->descriptor
.bDeviceProtocol
,
495 alt
->desc
.bInterfaceClass
,
496 alt
->desc
.bInterfaceSubClass
,
497 alt
->desc
.bInterfaceProtocol
);
499 static DEVICE_ATTR(modalias
, S_IRUGO
, show_modalias
, NULL
);
501 static struct attribute
*intf_attrs
[] = {
502 &dev_attr_bInterfaceNumber
.attr
,
503 &dev_attr_bAlternateSetting
.attr
,
504 &dev_attr_bNumEndpoints
.attr
,
505 &dev_attr_bInterfaceClass
.attr
,
506 &dev_attr_bInterfaceSubClass
.attr
,
507 &dev_attr_bInterfaceProtocol
.attr
,
508 &dev_attr_modalias
.attr
,
511 static struct attribute_group intf_attr_grp
= {
515 static inline void usb_create_intf_ep_files(struct usb_interface
*intf
,
516 struct usb_device
*udev
)
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_create_ep_files(&intf
->dev
, &iface_desc
->endpoint
[i
],
527 static inline void usb_remove_intf_ep_files(struct usb_interface
*intf
)
529 struct usb_host_interface
*iface_desc
;
532 iface_desc
= intf
->cur_altsetting
;
533 for (i
= 0; i
< iface_desc
->desc
.bNumEndpoints
; ++i
)
534 usb_remove_ep_files(&iface_desc
->endpoint
[i
]);
537 int usb_create_sysfs_intf_files(struct usb_interface
*intf
)
539 struct device
*dev
= &intf
->dev
;
540 struct usb_device
*udev
= interface_to_usbdev(intf
);
541 struct usb_host_interface
*alt
= intf
->cur_altsetting
;
544 retval
= sysfs_create_group(&dev
->kobj
, &intf_attr_grp
);
548 if (alt
->string
== NULL
)
549 alt
->string
= usb_cache_string(udev
, alt
->desc
.iInterface
);
551 retval
= device_create_file(dev
, &dev_attr_interface
);
552 usb_create_intf_ep_files(intf
, udev
);
556 void usb_remove_sysfs_intf_files(struct usb_interface
*intf
)
558 struct device
*dev
= &intf
->dev
;
560 usb_remove_intf_ep_files(intf
);
561 device_remove_file(dev
, &dev_attr_interface
);
562 sysfs_remove_group(&dev
->kobj
, &intf_attr_grp
);