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/config.h>
14 #include <linux/kernel.h>
16 #ifdef CONFIG_USB_DEBUG
21 #include <linux/usb.h>
27 struct usb_endpoint_descriptor *desc;
28 struct usb_device *udev;
31 #define to_ep_object(_kobj) \
32 container_of(_kobj, struct ep_object, kobj)
35 struct attribute attr;
36 ssize_t (*show)(struct usb_device *,
37 struct usb_endpoint_descriptor *, char *);
39 #define to_ep_attribute(_attr) \
40 container_of(_attr, struct ep_attribute, attr)
42 #define EP_ATTR(_name) \
43 struct ep_attribute ep_##_name = { \
44 .attr = {.name = #_name, .owner = THIS_MODULE, \
46 .show = show_ep_##_name}
48 #define usb_ep_attr(field, format_string) \
49 static ssize_t show_ep_##field(struct usb_device *udev, \
50 struct usb_endpoint_descriptor *desc, \
53 return sprintf(buf, format_string, desc->field); \
55 static EP_ATTR(field);
57 usb_ep_attr(bLength, "%02x\n")
58 usb_ep_attr(bEndpointAddress, "%02x\n")
59 usb_ep_attr(bmAttributes, "%02x\n")
60 usb_ep_attr(bInterval, "%02x\n")
62 static ssize_t show_ep_wMaxPacketSize(struct usb_device *udev,
63 struct usb_endpoint_descriptor *desc, char *buf)
65 return sprintf(buf, "%04x\n",
66 le16_to_cpu(desc->wMaxPacketSize) & 0x07ff);
68 static EP_ATTR(wMaxPacketSize);
70 static ssize_t show_ep_type(struct usb_device *udev,
71 struct usb_endpoint_descriptor *desc, char *buf)
73 char *type = "unknown";
75 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
76 case USB_ENDPOINT_XFER_CONTROL:
79 case USB_ENDPOINT_XFER_ISOC:
82 case USB_ENDPOINT_XFER_BULK:
85 case USB_ENDPOINT_XFER_INT:
89 return sprintf(buf, "%s\n", type);
93 static ssize_t show_ep_interval(struct usb_device *udev,
94 struct usb_endpoint_descriptor *desc, char *buf)
97 unsigned interval = 0;
100 in = (desc->bEndpointAddress & USB_DIR_IN);
102 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
103 case USB_ENDPOINT_XFER_CONTROL:
104 if (udev->speed == USB_SPEED_HIGH) /* uframes per NAK */
105 interval = desc->bInterval;
107 case USB_ENDPOINT_XFER_ISOC:
108 interval = 1 << (desc->bInterval - 1);
110 case USB_ENDPOINT_XFER_BULK:
111 if (udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */
112 interval = desc->bInterval;
114 case USB_ENDPOINT_XFER_INT:
115 if (udev->speed == USB_SPEED_HIGH)
116 interval = 1 << (desc->bInterval - 1);
118 interval = desc->bInterval;
121 interval *= (udev->speed == USB_SPEED_HIGH) ? 125 : 1000;
129 return sprintf(buf, "%d%cs\n", interval, unit);
131 static EP_ATTR(interval);
133 static ssize_t show_ep_direction(struct usb_device *udev,
134 struct usb_endpoint_descriptor *desc, char *buf)
138 if ((desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
139 USB_ENDPOINT_XFER_CONTROL)
141 else if (desc->bEndpointAddress & USB_DIR_IN)
145 return sprintf(buf, "%s\n", direction);
147 static EP_ATTR(direction);
149 static struct attribute *ep_attrs[] = {
151 &ep_bEndpointAddress.attr,
152 &ep_bmAttributes.attr,
154 &ep_wMaxPacketSize.attr,
161 static void ep_object_release(struct kobject *kobj)
163 kfree(to_ep_object(kobj));
166 static ssize_t ep_object_show(struct kobject *kobj, struct attribute *attr,
169 struct ep_object *ep_obj = to_ep_object(kobj);
170 struct ep_attribute *ep_attr = to_ep_attribute(attr);
172 return (ep_attr->show)(ep_obj->udev, ep_obj->desc, buf);
175 static struct sysfs_ops ep_object_sysfs_ops = {
176 .show = ep_object_show,
179 static struct kobj_type ep_object_ktype = {
180 .release = ep_object_release,
181 .sysfs_ops = &ep_object_sysfs_ops,
182 .default_attrs = ep_attrs,
185 static void usb_create_ep_files(struct kobject *parent,
186 struct usb_host_endpoint *endpoint,
187 struct usb_device *udev)
189 struct ep_object *ep_obj;
190 struct kobject *kobj;
192 ep_obj = kzalloc(sizeof(struct ep_object), GFP_KERNEL);
196 ep_obj->desc = &endpoint->desc;
199 kobj = &ep_obj->kobj;
200 kobject_set_name(kobj, "ep_%02x", endpoint->desc.bEndpointAddress);
201 kobj->parent = parent;
202 kobj->ktype = &ep_object_ktype;
204 /* Don't use kobject_register, because it generates a hotplug event */
206 if (kobject_add(kobj) == 0)
207 endpoint->kobj = kobj;
212 static void usb_remove_ep_files(struct usb_host_endpoint *endpoint)
215 if (endpoint->kobj) {
216 kobject_del(endpoint->kobj);
217 kobject_put(endpoint->kobj);
218 endpoint->kobj = NULL;
222 /* Active configuration fields */
223 #define usb_actconfig_show(field, multiplier, format_string) \
224 static ssize_t show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
226 struct usb_device *udev; \
227 struct usb_host_config *actconfig; \
229 udev = to_usb_device (dev); \
230 actconfig = udev->actconfig; \
232 return sprintf (buf, format_string, \
233 actconfig->desc.field * multiplier); \
238 #define usb_actconfig_attr(field, multiplier, format_string) \
239 usb_actconfig_show(field, multiplier, format_string) \
240 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
242 usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
243 usb_actconfig_attr (bmAttributes, 1, "%2x\n")
244 usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
246 static ssize_t show_configuration_string(struct device *dev, struct device_attribute *attr, char *buf)
248 struct usb_device *udev;
249 struct usb_host_config *actconfig;
252 udev = to_usb_device (dev);
253 actconfig = udev->actconfig;
254 if ((!actconfig) || (!actconfig->string))
256 len = sprintf(buf, actconfig->string, PAGE_SIZE);
263 static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
265 /* configuration value is always present, and r/w */
266 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
269 set_bConfigurationValue (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
271 struct usb_device *udev = udev = to_usb_device (dev);
274 if (sscanf (buf, "%u", &config) != 1 || config > 255)
276 usb_lock_device(udev);
277 value = usb_set_configuration (udev, config);
278 usb_unlock_device(udev);
279 return (value < 0) ? value : count;
282 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
283 show_bConfigurationValue, set_bConfigurationValue);
286 #define usb_string_attr(name) \
287 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
289 struct usb_device *udev; \
292 udev = to_usb_device (dev); \
293 len = snprintf(buf, 256, "%s", udev->name); \
300 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
302 usb_string_attr(product);
303 usb_string_attr(manufacturer);
304 usb_string_attr(serial);
307 show_speed (struct device *dev, struct device_attribute *attr, char *buf)
309 struct usb_device *udev;
312 udev = to_usb_device (dev);
314 switch (udev->speed) {
318 case USB_SPEED_UNKNOWN:
328 return sprintf (buf, "%s\n", speed);
330 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
333 show_devnum (struct device *dev, struct device_attribute *attr, char *buf)
335 struct usb_device *udev;
337 udev = to_usb_device (dev);
338 return sprintf (buf, "%d\n", udev->devnum);
340 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
343 show_version (struct device *dev, struct device_attribute *attr, char *buf)
345 struct usb_device *udev;
348 udev = to_usb_device(dev);
349 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
350 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
352 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
355 show_maxchild (struct device *dev, struct device_attribute *attr, char *buf)
357 struct usb_device *udev;
359 udev = to_usb_device (dev);
360 return sprintf (buf, "%d\n", udev->maxchild);
362 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
364 /* Descriptor fields */
365 #define usb_descriptor_attr_le16(field, format_string) \
367 show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
369 struct usb_device *udev; \
371 udev = to_usb_device (dev); \
372 return sprintf (buf, format_string, \
373 le16_to_cpu(udev->descriptor.field)); \
375 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
377 usb_descriptor_attr_le16(idVendor, "%04x\n")
378 usb_descriptor_attr_le16(idProduct, "%04x\n")
379 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
381 #define usb_descriptor_attr(field, format_string) \
383 show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
385 struct usb_device *udev; \
387 udev = to_usb_device (dev); \
388 return sprintf (buf, format_string, udev->descriptor.field); \
390 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
392 usb_descriptor_attr (bDeviceClass, "%02x\n")
393 usb_descriptor_attr (bDeviceSubClass, "%02x\n")
394 usb_descriptor_attr (bDeviceProtocol, "%02x\n")
395 usb_descriptor_attr (bNumConfigurations, "%d\n")
396 usb_descriptor_attr (bMaxPacketSize0, "%d\n")
398 static struct attribute *dev_attrs[] = {
399 /* current configuration's attributes */
400 &dev_attr_bNumInterfaces.attr,
401 &dev_attr_bConfigurationValue.attr,
402 &dev_attr_bmAttributes.attr,
403 &dev_attr_bMaxPower.attr,
404 /* device attributes */
405 &dev_attr_idVendor.attr,
406 &dev_attr_idProduct.attr,
407 &dev_attr_bcdDevice.attr,
408 &dev_attr_bDeviceClass.attr,
409 &dev_attr_bDeviceSubClass.attr,
410 &dev_attr_bDeviceProtocol.attr,
411 &dev_attr_bNumConfigurations.attr,
412 &dev_attr_bMaxPacketSize0.attr,
413 &dev_attr_speed.attr,
414 &dev_attr_devnum.attr,
415 &dev_attr_version.attr,
416 &dev_attr_maxchild.attr,
419 static struct attribute_group dev_attr_grp = {
423 void usb_create_sysfs_dev_files (struct usb_device *udev)
425 struct device *dev = &udev->dev;
427 sysfs_create_group(&dev->kobj, &dev_attr_grp);
429 if (udev->manufacturer)
430 device_create_file (dev, &dev_attr_manufacturer);
432 device_create_file (dev, &dev_attr_product);
434 device_create_file (dev, &dev_attr_serial);
435 device_create_file (dev, &dev_attr_configuration);
436 usb_create_ep_files(&dev->kobj, &udev->ep0, udev);
439 void usb_remove_sysfs_dev_files (struct usb_device *udev)
441 struct device *dev = &udev->dev;
443 usb_remove_ep_files(&udev->ep0);
444 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
446 if (udev->descriptor.iManufacturer)
447 device_remove_file(dev, &dev_attr_manufacturer);
448 if (udev->descriptor.iProduct)
449 device_remove_file(dev, &dev_attr_product);
450 if (udev->descriptor.iSerialNumber)
451 device_remove_file(dev, &dev_attr_serial);
452 device_remove_file (dev, &dev_attr_configuration);
455 /* Interface fields */
456 #define usb_intf_attr(field, format_string) \
458 show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
460 struct usb_interface *intf = to_usb_interface (dev); \
462 return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
464 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
466 usb_intf_attr (bInterfaceNumber, "%02x\n")
467 usb_intf_attr (bAlternateSetting, "%2d\n")
468 usb_intf_attr (bNumEndpoints, "%02x\n")
469 usb_intf_attr (bInterfaceClass, "%02x\n")
470 usb_intf_attr (bInterfaceSubClass, "%02x\n")
471 usb_intf_attr (bInterfaceProtocol, "%02x\n")
473 static ssize_t show_interface_string(struct device *dev, struct device_attribute *attr, char *buf)
475 struct usb_interface *intf;
476 struct usb_device *udev;
479 intf = to_usb_interface (dev);
480 udev = interface_to_usbdev (intf);
481 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
488 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
490 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
492 struct usb_interface *intf;
493 struct usb_device *udev;
494 struct usb_host_interface *alt;
496 intf = to_usb_interface(dev);
497 udev = interface_to_usbdev(intf);
498 alt = intf->cur_altsetting;
500 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
501 "ic%02Xisc%02Xip%02X\n",
502 le16_to_cpu(udev->descriptor.idVendor),
503 le16_to_cpu(udev->descriptor.idProduct),
504 le16_to_cpu(udev->descriptor.bcdDevice),
505 udev->descriptor.bDeviceClass,
506 udev->descriptor.bDeviceSubClass,
507 udev->descriptor.bDeviceProtocol,
508 alt->desc.bInterfaceClass,
509 alt->desc.bInterfaceSubClass,
510 alt->desc.bInterfaceProtocol);
512 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
514 static struct attribute *intf_attrs[] = {
515 &dev_attr_bInterfaceNumber.attr,
516 &dev_attr_bAlternateSetting.attr,
517 &dev_attr_bNumEndpoints.attr,
518 &dev_attr_bInterfaceClass.attr,
519 &dev_attr_bInterfaceSubClass.attr,
520 &dev_attr_bInterfaceProtocol.attr,
521 &dev_attr_modalias.attr,
524 static struct attribute_group intf_attr_grp = {
528 static inline void usb_create_intf_ep_files(struct usb_interface *intf)
530 struct usb_host_interface *iface_desc;
533 iface_desc = intf->cur_altsetting;
534 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
535 usb_create_ep_files(&intf->dev.kobj, &iface_desc->endpoint[i],
536 interface_to_usbdev(intf));
539 static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
541 struct usb_host_interface *iface_desc;
544 iface_desc = intf->cur_altsetting;
545 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
546 usb_remove_ep_files(&iface_desc->endpoint[i]);
549 void usb_create_sysfs_intf_files (struct usb_interface *intf)
551 sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
553 if (intf->cur_altsetting->string)
554 device_create_file(&intf->dev, &dev_attr_interface);
555 usb_create_intf_ep_files(intf);
558 void usb_remove_sysfs_intf_files (struct usb_interface *intf)
560 usb_remove_intf_ep_files(intf);
561 sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
563 if (intf->cur_altsetting->string)
564 device_remove_file(&intf->dev, &dev_attr_interface);