aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb
diff options
context:
space:
mode:
authorAlan Stern <stern@rowland.harvard.edu>2007-07-12 17:06:23 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2007-07-19 20:46:04 -0400
commit69d42a78f935d19384d1f6e4f94b65bb162b36df (patch)
tree03c7721d241f1db39671546db123b57def296b1d /drivers/usb
parent1b4cd43bd3f9aa7a794e29b80b0d984a8e144df4 (diff)
USB: add "descriptors" binary sysfs attribute
This patch (as934) adds a new readonly binary sysfs attribute file called "descriptors" for each USB device. The attribute contains the device descriptor followed by the raw descriptor entry (config plug subsidiary descriptors) for the current configuration. Having this information available in fixed-format binary makes life a lot easier for user programs by avoiding the need to open, read, and parse multiple sysfs text files. The information in this attribute file is much like that in usbfs's device file, but there are some significant differences: The 2-byte fields in the device descriptor are left in little-endian byte order, as they appear on the bus and in the kernel. Only one raw descriptor set is presented, that of the current configuration. Opening this file will not cause a suspended device to be autoresumed. The last item in particular should be a big selling point for libusb, which currently forces all USB devices to be resumed as it scans the device tree. Signed-off-by: Alan Stern <stern@rowland.harvard.edu> Cc: Dave Mielke <dave@mielke.cc> Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/usb')
-rw-r--r--drivers/usb/core/sysfs.c53
1 files changed, 53 insertions, 0 deletions
diff --git a/drivers/usb/core/sysfs.c b/drivers/usb/core/sysfs.c
index d47ae89154a7..2ab222be8fd1 100644
--- a/drivers/usb/core/sysfs.c
+++ b/drivers/usb/core/sysfs.c
@@ -441,6 +441,54 @@ static struct attribute_group dev_attr_grp = {
441 .attrs = dev_attrs, 441 .attrs = dev_attrs,
442}; 442};
443 443
444/* Binary descriptors */
445
446static ssize_t
447read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
448 char *buf, loff_t off, size_t count)
449{
450 struct usb_device *udev = to_usb_device(
451 container_of(kobj, struct device, kobj));
452 size_t nleft = count;
453 size_t srclen, n;
454
455 usb_lock_device(udev);
456
457 /* The binary attribute begins with the device descriptor */
458 srclen = sizeof(struct usb_device_descriptor);
459 if (off < srclen) {
460 n = min_t(size_t, nleft, srclen - off);
461 memcpy(buf, off + (char *) &udev->descriptor, n);
462 nleft -= n;
463 buf += n;
464 off = 0;
465 } else {
466 off -= srclen;
467 }
468
469 /* Then follows the raw descriptor entry for the current
470 * configuration (config plus subsidiary descriptors).
471 */
472 if (udev->actconfig) {
473 int cfgno = udev->actconfig - udev->config;
474
475 srclen = __le16_to_cpu(udev->actconfig->desc.wTotalLength);
476 if (off < srclen) {
477 n = min_t(size_t, nleft, srclen - off);
478 memcpy(buf, off + udev->rawdescriptors[cfgno], n);
479 nleft -= n;
480 }
481 }
482 usb_unlock_device(udev);
483 return count - nleft;
484}
485
486static struct bin_attribute dev_bin_attr_descriptors = {
487 .attr = {.name = "descriptors", .mode = 0444},
488 .read = read_descriptors,
489 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
490};
491
444int usb_create_sysfs_dev_files(struct usb_device *udev) 492int usb_create_sysfs_dev_files(struct usb_device *udev)
445{ 493{
446 struct device *dev = &udev->dev; 494 struct device *dev = &udev->dev;
@@ -450,6 +498,10 @@ int usb_create_sysfs_dev_files(struct usb_device *udev)
450 if (retval) 498 if (retval)
451 return retval; 499 return retval;
452 500
501 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
502 if (retval)
503 goto error;
504
453 retval = add_persist_attributes(dev); 505 retval = add_persist_attributes(dev);
454 if (retval) 506 if (retval)
455 goto error; 507 goto error;
@@ -492,6 +544,7 @@ void usb_remove_sysfs_dev_files(struct usb_device *udev)
492 device_remove_file(dev, &dev_attr_serial); 544 device_remove_file(dev, &dev_attr_serial);
493 remove_power_attributes(dev); 545 remove_power_attributes(dev);
494 remove_persist_attributes(dev); 546 remove_persist_attributes(dev);
547 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
495 sysfs_remove_group(&dev->kobj, &dev_attr_grp); 548 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
496} 549}
497 550