Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sysfs.c
Go to the documentation of this file.
1 /*
2  * drivers/usb/core/sysfs.c
3  *
4  * (C) Copyright 2002 David Brownell
5  * (C) Copyright 2002,2004 Greg Kroah-Hartman
6  * (C) Copyright 2002,2004 IBM Corp.
7  *
8  * All of the sysfs file attributes for usb devices and interfaces.
9  *
10  */
11 
12 
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"
18 
19 /* Active configuration fields */
20 #define usb_actconfig_show(field, multiplier, format_string) \
21 static ssize_t show_##field(struct device *dev, \
22  struct device_attribute *attr, char *buf) \
23 { \
24  struct usb_device *udev; \
25  struct usb_host_config *actconfig; \
26  \
27  udev = to_usb_device(dev); \
28  actconfig = udev->actconfig; \
29  if (actconfig) \
30  return sprintf(buf, format_string, \
31  actconfig->desc.field * multiplier); \
32  else \
33  return 0; \
34 } \
35 
36 #define usb_actconfig_attr(field, multiplier, format_string) \
37 usb_actconfig_show(field, multiplier, format_string) \
38 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
39 
42 usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
43 
44 static ssize_t show_configuration_string(struct device *dev,
46 {
47  struct usb_device *udev;
48  struct usb_host_config *actconfig;
49 
50  udev = to_usb_device(dev);
51  actconfig = udev->actconfig;
52  if ((!actconfig) || (!actconfig->string))
53  return 0;
54  return sprintf(buf, "%s\n", actconfig->string);
55 }
56 static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
57 
58 /* configuration value is always present, and r/w */
60 
61 static ssize_t
62 set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
63  const char *buf, size_t count)
64 {
65  struct usb_device *udev = to_usb_device(dev);
66  int config, value;
67 
68  if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
69  return -EINVAL;
70  usb_lock_device(udev);
71  value = usb_set_configuration(udev, config);
72  usb_unlock_device(udev);
73  return (value < 0) ? value : count;
74 }
75 
77  show_bConfigurationValue, set_bConfigurationValue);
78 
79 /* String fields */
80 #define usb_string_attr(name) \
81 static ssize_t show_##name(struct device *dev, \
82  struct device_attribute *attr, char *buf) \
83 { \
84  struct usb_device *udev; \
85  int retval; \
86  \
87  udev = to_usb_device(dev); \
88  usb_lock_device(udev); \
89  retval = sprintf(buf, "%s\n", udev->name); \
90  usb_unlock_device(udev); \
91  return retval; \
92 } \
93 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
94 
98 
99 static ssize_t
100 show_speed(struct device *dev, struct device_attribute *attr, char *buf)
101 {
102  struct usb_device *udev;
103  char *speed;
104 
105  udev = to_usb_device(dev);
106 
107  switch (udev->speed) {
108  case USB_SPEED_LOW:
109  speed = "1.5";
110  break;
111  case USB_SPEED_UNKNOWN:
112  case USB_SPEED_FULL:
113  speed = "12";
114  break;
115  case USB_SPEED_HIGH:
116  speed = "480";
117  break;
118  case USB_SPEED_WIRELESS:
119  speed = "480";
120  break;
121  case USB_SPEED_SUPER:
122  speed = "5000";
123  break;
124  default:
125  speed = "unknown";
126  }
127  return sprintf(buf, "%s\n", speed);
128 }
129 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
130 
131 static ssize_t
132 show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
133 {
134  struct usb_device *udev;
135 
136  udev = to_usb_device(dev);
137  return sprintf(buf, "%d\n", udev->bus->busnum);
138 }
139 static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
140 
141 static ssize_t
142 show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
143 {
144  struct usb_device *udev;
145 
146  udev = to_usb_device(dev);
147  return sprintf(buf, "%d\n", udev->devnum);
148 }
149 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
150 
151 static ssize_t
152 show_devpath(struct device *dev, struct device_attribute *attr, char *buf)
153 {
154  struct usb_device *udev;
155 
156  udev = to_usb_device(dev);
157  return sprintf(buf, "%s\n", udev->devpath);
158 }
159 static DEVICE_ATTR(devpath, S_IRUGO, show_devpath, NULL);
160 
161 static ssize_t
162 show_version(struct device *dev, struct device_attribute *attr, char *buf)
163 {
164  struct usb_device *udev;
165  u16 bcdUSB;
166 
167  udev = to_usb_device(dev);
168  bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
169  return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
170 }
171 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
172 
173 static ssize_t
174 show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
175 {
176  struct usb_device *udev;
177 
178  udev = to_usb_device(dev);
179  return sprintf(buf, "%d\n", udev->maxchild);
180 }
181 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
182 
183 static ssize_t
184 show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
185 {
186  struct usb_device *udev;
187 
188  udev = to_usb_device(dev);
189  return sprintf(buf, "0x%x\n", udev->quirks);
190 }
191 static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
192 
193 static ssize_t
194 show_avoid_reset_quirk(struct device *dev, struct device_attribute *attr, char *buf)
195 {
196  struct usb_device *udev;
197 
198  udev = to_usb_device(dev);
199  return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
200 }
201 
202 static ssize_t
203 set_avoid_reset_quirk(struct device *dev, struct device_attribute *attr,
204  const char *buf, size_t count)
205 {
206  struct usb_device *udev = to_usb_device(dev);
207  int val;
208 
209  if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
210  return -EINVAL;
211  usb_lock_device(udev);
212  if (val)
213  udev->quirks |= USB_QUIRK_RESET;
214  else
215  udev->quirks &= ~USB_QUIRK_RESET;
216  usb_unlock_device(udev);
217  return count;
218 }
219 
220 static DEVICE_ATTR(avoid_reset_quirk, S_IRUGO | S_IWUSR,
221  show_avoid_reset_quirk, set_avoid_reset_quirk);
222 
223 static ssize_t
224 show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
225 {
226  struct usb_device *udev;
227 
228  udev = to_usb_device(dev);
229  return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
230 }
231 static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
232 
233 static ssize_t
234 show_removable(struct device *dev, struct device_attribute *attr, char *buf)
235 {
236  struct usb_device *udev;
237  char *state;
238 
239  udev = to_usb_device(dev);
240 
241  switch (udev->removable) {
242  case USB_DEVICE_REMOVABLE:
243  state = "removable";
244  break;
245  case USB_DEVICE_FIXED:
246  state = "fixed";
247  break;
248  default:
249  state = "unknown";
250  }
251 
252  return sprintf(buf, "%s\n", state);
253 }
254 static DEVICE_ATTR(removable, S_IRUGO, show_removable, NULL);
255 
256 static ssize_t
257 show_ltm_capable(struct device *dev, struct device_attribute *attr, char *buf)
258 {
259  if (usb_device_supports_ltm(to_usb_device(dev)))
260  return sprintf(buf, "%s\n", "yes");
261  return sprintf(buf, "%s\n", "no");
262 }
263 static DEVICE_ATTR(ltm_capable, S_IRUGO, show_ltm_capable, NULL);
264 
265 #ifdef CONFIG_PM
266 
267 static ssize_t
268 show_persist(struct device *dev, struct device_attribute *attr, char *buf)
269 {
270  struct usb_device *udev = to_usb_device(dev);
271 
272  return sprintf(buf, "%d\n", udev->persist_enabled);
273 }
274 
275 static ssize_t
276 set_persist(struct device *dev, struct device_attribute *attr,
277  const char *buf, size_t count)
278 {
279  struct usb_device *udev = to_usb_device(dev);
280  int value;
281 
282  /* Hubs are always enabled for USB_PERSIST */
283  if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
284  return -EPERM;
285 
286  if (sscanf(buf, "%d", &value) != 1)
287  return -EINVAL;
288 
289  usb_lock_device(udev);
290  udev->persist_enabled = !!value;
291  usb_unlock_device(udev);
292  return count;
293 }
294 
295 static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
296 
297 static int add_persist_attributes(struct device *dev)
298 {
299  int rc = 0;
300 
301  if (is_usb_device(dev)) {
302  struct usb_device *udev = to_usb_device(dev);
303 
304  /* Hubs are automatically enabled for USB_PERSIST,
305  * no point in creating the attribute file.
306  */
307  if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
308  rc = sysfs_add_file_to_group(&dev->kobj,
309  &dev_attr_persist.attr,
311  }
312  return rc;
313 }
314 
315 static void remove_persist_attributes(struct device *dev)
316 {
318  &dev_attr_persist.attr,
320 }
321 #else
322 
323 #define add_persist_attributes(dev) 0
324 #define remove_persist_attributes(dev) do {} while (0)
325 
326 #endif /* CONFIG_PM */
327 
328 #ifdef CONFIG_USB_SUSPEND
329 
330 static ssize_t
331 show_connected_duration(struct device *dev, struct device_attribute *attr,
332  char *buf)
333 {
334  struct usb_device *udev = to_usb_device(dev);
335 
336  return sprintf(buf, "%u\n",
337  jiffies_to_msecs(jiffies - udev->connect_time));
338 }
339 
340 static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL);
341 
342 /*
343  * If the device is resumed, the last time the device was suspended has
344  * been pre-subtracted from active_duration. We add the current time to
345  * get the duration that the device was actually active.
346  *
347  * If the device is suspended, the active_duration is up-to-date.
348  */
349 static ssize_t
350 show_active_duration(struct device *dev, struct device_attribute *attr,
351  char *buf)
352 {
353  struct usb_device *udev = to_usb_device(dev);
354  int duration;
355 
356  if (udev->state != USB_STATE_SUSPENDED)
357  duration = jiffies_to_msecs(jiffies + udev->active_duration);
358  else
359  duration = jiffies_to_msecs(udev->active_duration);
360  return sprintf(buf, "%u\n", duration);
361 }
362 
363 static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL);
364 
365 static ssize_t
366 show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
367 {
368  return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
369 }
370 
371 static ssize_t
372 set_autosuspend(struct device *dev, struct device_attribute *attr,
373  const char *buf, size_t count)
374 {
375  int value;
376 
377  if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
378  value <= -INT_MAX/1000)
379  return -EINVAL;
380 
381  pm_runtime_set_autosuspend_delay(dev, value * 1000);
382  return count;
383 }
384 
385 static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
386  show_autosuspend, set_autosuspend);
387 
388 static const char on_string[] = "on";
389 static const char auto_string[] = "auto";
390 
391 static void warn_level(void) {
392  static int level_warned;
393 
394  if (!level_warned) {
395  level_warned = 1;
396  printk(KERN_WARNING "WARNING! power/level is deprecated; "
397  "use power/control instead\n");
398  }
399 }
400 
401 static ssize_t
402 show_level(struct device *dev, struct device_attribute *attr, char *buf)
403 {
404  struct usb_device *udev = to_usb_device(dev);
405  const char *p = auto_string;
406 
407  warn_level();
408  if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
409  p = on_string;
410  return sprintf(buf, "%s\n", p);
411 }
412 
413 static ssize_t
414 set_level(struct device *dev, struct device_attribute *attr,
415  const char *buf, size_t count)
416 {
417  struct usb_device *udev = to_usb_device(dev);
418  int len = count;
419  char *cp;
420  int rc = count;
421 
422  warn_level();
423  cp = memchr(buf, '\n', count);
424  if (cp)
425  len = cp - buf;
426 
427  usb_lock_device(udev);
428 
429  if (len == sizeof on_string - 1 &&
430  strncmp(buf, on_string, len) == 0)
431  usb_disable_autosuspend(udev);
432 
433  else if (len == sizeof auto_string - 1 &&
434  strncmp(buf, auto_string, len) == 0)
435  usb_enable_autosuspend(udev);
436 
437  else
438  rc = -EINVAL;
439 
440  usb_unlock_device(udev);
441  return rc;
442 }
443 
444 static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
445 
446 static ssize_t
447 show_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
448  char *buf)
449 {
450  struct usb_device *udev = to_usb_device(dev);
451  const char *p;
452 
453  if (udev->usb2_hw_lpm_enabled == 1)
454  p = "enabled";
455  else
456  p = "disabled";
457 
458  return sprintf(buf, "%s\n", p);
459 }
460 
461 static ssize_t
462 set_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
463  const char *buf, size_t count)
464 {
465  struct usb_device *udev = to_usb_device(dev);
466  bool value;
467  int ret;
468 
469  usb_lock_device(udev);
470 
471  ret = strtobool(buf, &value);
472 
473  if (!ret)
474  ret = usb_set_usb2_hardware_lpm(udev, value);
475 
476  usb_unlock_device(udev);
477 
478  if (!ret)
479  return count;
480 
481  return ret;
482 }
483 
484 static DEVICE_ATTR(usb2_hardware_lpm, S_IRUGO | S_IWUSR, show_usb2_hardware_lpm,
485  set_usb2_hardware_lpm);
486 
487 static struct attribute *usb2_hardware_lpm_attr[] = {
488  &dev_attr_usb2_hardware_lpm.attr,
489  NULL,
490 };
491 static struct attribute_group usb2_hardware_lpm_attr_group = {
493  .attrs = usb2_hardware_lpm_attr,
494 };
495 
496 static struct attribute *power_attrs[] = {
497  &dev_attr_autosuspend.attr,
498  &dev_attr_level.attr,
499  &dev_attr_connected_duration.attr,
500  &dev_attr_active_duration.attr,
501  NULL,
502 };
503 static struct attribute_group power_attr_group = {
505  .attrs = power_attrs,
506 };
507 
508 static int add_power_attributes(struct device *dev)
509 {
510  int rc = 0;
511 
512  if (is_usb_device(dev)) {
513  struct usb_device *udev = to_usb_device(dev);
514  rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
515  if (udev->usb2_hw_lpm_capable == 1)
516  rc = sysfs_merge_group(&dev->kobj,
517  &usb2_hardware_lpm_attr_group);
518  }
519 
520  return rc;
521 }
522 
523 static void remove_power_attributes(struct device *dev)
524 {
525  sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
526  sysfs_unmerge_group(&dev->kobj, &power_attr_group);
527 }
528 
529 #else
530 
531 #define add_power_attributes(dev) 0
532 #define remove_power_attributes(dev) do {} while (0)
533 
534 #endif /* CONFIG_USB_SUSPEND */
535 
536 
537 /* Descriptor fields */
538 #define usb_descriptor_attr_le16(field, format_string) \
539 static ssize_t \
540 show_##field(struct device *dev, struct device_attribute *attr, \
541  char *buf) \
542 { \
543  struct usb_device *udev; \
544  \
545  udev = to_usb_device(dev); \
546  return sprintf(buf, format_string, \
547  le16_to_cpu(udev->descriptor.field)); \
548 } \
549 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
550 
551 usb_descriptor_attr_le16(idVendor, "%04x\n")
552 usb_descriptor_attr_le16(idProduct, "%04x\n")
553 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
554 
555 #define usb_descriptor_attr(field, format_string) \
556 static ssize_t \
557 show_##field(struct device *dev, struct device_attribute *attr, \
558  char *buf) \
559 { \
560  struct usb_device *udev; \
561  \
562  udev = to_usb_device(dev); \
563  return sprintf(buf, format_string, udev->descriptor.field); \
564 } \
565 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
566 
568 usb_descriptor_attr(bDeviceSubClass, "%02x\n")
569 usb_descriptor_attr(bDeviceProtocol, "%02x\n")
570 usb_descriptor_attr(bNumConfigurations, "%d\n")
571 usb_descriptor_attr(bMaxPacketSize0, "%d\n")
572 
573 
574 
575 /* show if the device is authorized (1) or not (0) */
576 static ssize_t usb_dev_authorized_show(struct device *dev,
577  struct device_attribute *attr,
578  char *buf)
579 {
580  struct usb_device *usb_dev = to_usb_device(dev);
581  return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
582 }
583 
584 
585 /*
586  * Authorize a device to be used in the system
587  *
588  * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
589  */
590 static ssize_t usb_dev_authorized_store(struct device *dev,
591  struct device_attribute *attr,
592  const char *buf, size_t size)
593 {
594  ssize_t result;
595  struct usb_device *usb_dev = to_usb_device(dev);
596  unsigned val;
597  result = sscanf(buf, "%u\n", &val);
598  if (result != 1)
599  result = -EINVAL;
600  else if (val == 0)
601  result = usb_deauthorize_device(usb_dev);
602  else
603  result = usb_authorize_device(usb_dev);
604  return result < 0? result : size;
605 }
606 
607 static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, 0644,
608  usb_dev_authorized_show, usb_dev_authorized_store);
609 
610 /* "Safely remove a device" */
611 static ssize_t usb_remove_store(struct device *dev,
612  struct device_attribute *attr,
613  const char *buf, size_t count)
614 {
615  struct usb_device *udev = to_usb_device(dev);
616  int rc = 0;
617 
618  usb_lock_device(udev);
619  if (udev->state != USB_STATE_NOTATTACHED) {
620 
621  /* To avoid races, first unconfigure and then remove */
622  usb_set_configuration(udev, -1);
623  rc = usb_remove_device(udev);
624  }
625  if (rc == 0)
626  rc = count;
627  usb_unlock_device(udev);
628  return rc;
629 }
630 static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0200, NULL, usb_remove_store);
631 
632 
633 static struct attribute *dev_attrs[] = {
634  /* current configuration's attributes */
635  &dev_attr_configuration.attr,
636  &dev_attr_bNumInterfaces.attr,
637  &dev_attr_bConfigurationValue.attr,
638  &dev_attr_bmAttributes.attr,
639  &dev_attr_bMaxPower.attr,
640  /* device attributes */
641  &dev_attr_urbnum.attr,
642  &dev_attr_idVendor.attr,
643  &dev_attr_idProduct.attr,
644  &dev_attr_bcdDevice.attr,
645  &dev_attr_bDeviceClass.attr,
646  &dev_attr_bDeviceSubClass.attr,
647  &dev_attr_bDeviceProtocol.attr,
648  &dev_attr_bNumConfigurations.attr,
649  &dev_attr_bMaxPacketSize0.attr,
650  &dev_attr_speed.attr,
651  &dev_attr_busnum.attr,
652  &dev_attr_devnum.attr,
653  &dev_attr_devpath.attr,
654  &dev_attr_version.attr,
655  &dev_attr_maxchild.attr,
656  &dev_attr_quirks.attr,
657  &dev_attr_avoid_reset_quirk.attr,
658  &dev_attr_authorized.attr,
659  &dev_attr_remove.attr,
660  &dev_attr_removable.attr,
661  &dev_attr_ltm_capable.attr,
662  NULL,
663 };
664 static struct attribute_group dev_attr_grp = {
665  .attrs = dev_attrs,
666 };
667 
668 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
669  * accordingly.
670  */
671 static struct attribute *dev_string_attrs[] = {
672  &dev_attr_manufacturer.attr,
673  &dev_attr_product.attr,
674  &dev_attr_serial.attr,
675  NULL
676 };
677 
678 static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
679  struct attribute *a, int n)
680 {
681  struct device *dev = container_of(kobj, struct device, kobj);
682  struct usb_device *udev = to_usb_device(dev);
683 
684  if (a == &dev_attr_manufacturer.attr) {
685  if (udev->manufacturer == NULL)
686  return 0;
687  } else if (a == &dev_attr_product.attr) {
688  if (udev->product == NULL)
689  return 0;
690  } else if (a == &dev_attr_serial.attr) {
691  if (udev->serial == NULL)
692  return 0;
693  }
694  return a->mode;
695 }
696 
697 static struct attribute_group dev_string_attr_grp = {
698  .attrs = dev_string_attrs,
699  .is_visible = dev_string_attrs_are_visible,
700 };
701 
703  &dev_attr_grp,
704  &dev_string_attr_grp,
705  NULL
706 };
707 
708 /* Binary descriptors */
709 
710 static ssize_t
711 read_descriptors(struct file *filp, struct kobject *kobj,
712  struct bin_attribute *attr,
713  char *buf, loff_t off, size_t count)
714 {
715  struct device *dev = container_of(kobj, struct device, kobj);
716  struct usb_device *udev = to_usb_device(dev);
717  size_t nleft = count;
718  size_t srclen, n;
719  int cfgno;
720  void *src;
721 
722  /* The binary attribute begins with the device descriptor.
723  * Following that are the raw descriptor entries for all the
724  * configurations (config plus subsidiary descriptors).
725  */
726  for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
727  nleft > 0; ++cfgno) {
728  if (cfgno < 0) {
729  src = &udev->descriptor;
730  srclen = sizeof(struct usb_device_descriptor);
731  } else {
732  src = udev->rawdescriptors[cfgno];
733  srclen = __le16_to_cpu(udev->config[cfgno].desc.
734  wTotalLength);
735  }
736  if (off < srclen) {
737  n = min(nleft, srclen - (size_t) off);
738  memcpy(buf, src + off, n);
739  nleft -= n;
740  buf += n;
741  off = 0;
742  } else {
743  off -= srclen;
744  }
745  }
746  return count - nleft;
747 }
748 
749 static struct bin_attribute dev_bin_attr_descriptors = {
750  .attr = {.name = "descriptors", .mode = 0444},
751  .read = read_descriptors,
752  .size = 18 + 65535, /* dev descr + max-size raw descriptor */
753 };
754 
755 int usb_create_sysfs_dev_files(struct usb_device *udev)
756 {
757  struct device *dev = &udev->dev;
758  int retval;
759 
760  retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
761  if (retval)
762  goto error;
763 
764  retval = add_persist_attributes(dev);
765  if (retval)
766  goto error;
767 
768  retval = add_power_attributes(dev);
769  if (retval)
770  goto error;
771  return retval;
772 error:
774  return retval;
775 }
776 
777 void usb_remove_sysfs_dev_files(struct usb_device *udev)
778 {
779  struct device *dev = &udev->dev;
780 
783  device_remove_bin_file(dev, &dev_bin_attr_descriptors);
784 }
785 
786 /* Interface Accociation Descriptor fields */
787 #define usb_intf_assoc_attr(field, format_string) \
788 static ssize_t \
789 show_iad_##field(struct device *dev, struct device_attribute *attr, \
790  char *buf) \
791 { \
792  struct usb_interface *intf = to_usb_interface(dev); \
793  \
794  return sprintf(buf, format_string, \
795  intf->intf_assoc->field); \
796 } \
797 static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
798 
804 
805 /* Interface fields */
806 #define usb_intf_attr(field, format_string) \
807 static ssize_t \
808 show_##field(struct device *dev, struct device_attribute *attr, \
809  char *buf) \
810 { \
811  struct usb_interface *intf = to_usb_interface(dev); \
812  \
813  return sprintf(buf, format_string, \
814  intf->cur_altsetting->desc.field); \
815 } \
816 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
817 
819 usb_intf_attr(bAlternateSetting, "%2d\n")
820 usb_intf_attr(bNumEndpoints, "%02x\n")
821 usb_intf_attr(bInterfaceClass, "%02x\n")
822 usb_intf_attr(bInterfaceSubClass, "%02x\n")
823 usb_intf_attr(bInterfaceProtocol, "%02x\n")
824 
825 static ssize_t show_interface_string(struct device *dev,
826  struct device_attribute *attr, char *buf)
827 {
828  struct usb_interface *intf;
829  char *string;
830 
831  intf = to_usb_interface(dev);
832  string = intf->cur_altsetting->string;
833  barrier(); /* The altsetting might change! */
834 
835  if (!string)
836  return 0;
837  return sprintf(buf, "%s\n", string);
838 }
839 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
840 
841 static ssize_t show_modalias(struct device *dev,
842  struct device_attribute *attr, char *buf)
843 {
844  struct usb_interface *intf;
845  struct usb_device *udev;
846  struct usb_host_interface *alt;
847 
848  intf = to_usb_interface(dev);
849  udev = interface_to_usbdev(intf);
850  alt = intf->cur_altsetting;
851 
852  return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
853  "ic%02Xisc%02Xip%02Xin%02X\n",
854  le16_to_cpu(udev->descriptor.idVendor),
855  le16_to_cpu(udev->descriptor.idProduct),
856  le16_to_cpu(udev->descriptor.bcdDevice),
857  udev->descriptor.bDeviceClass,
858  udev->descriptor.bDeviceSubClass,
859  udev->descriptor.bDeviceProtocol,
860  alt->desc.bInterfaceClass,
861  alt->desc.bInterfaceSubClass,
862  alt->desc.bInterfaceProtocol,
863  alt->desc.bInterfaceNumber);
864 }
865 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
866 
867 static ssize_t show_supports_autosuspend(struct device *dev,
868  struct device_attribute *attr, char *buf)
869 {
870  struct usb_interface *intf;
871  struct usb_device *udev;
872  int ret;
873 
874  intf = to_usb_interface(dev);
875  udev = interface_to_usbdev(intf);
876 
877  usb_lock_device(udev);
878  /* Devices will be autosuspended even when an interface isn't claimed */
879  if (!intf->dev.driver ||
880  to_usb_driver(intf->dev.driver)->supports_autosuspend)
881  ret = sprintf(buf, "%u\n", 1);
882  else
883  ret = sprintf(buf, "%u\n", 0);
884  usb_unlock_device(udev);
885 
886  return ret;
887 }
888 static DEVICE_ATTR(supports_autosuspend, S_IRUGO, show_supports_autosuspend, NULL);
889 
890 static struct attribute *intf_attrs[] = {
891  &dev_attr_bInterfaceNumber.attr,
892  &dev_attr_bAlternateSetting.attr,
893  &dev_attr_bNumEndpoints.attr,
894  &dev_attr_bInterfaceClass.attr,
895  &dev_attr_bInterfaceSubClass.attr,
896  &dev_attr_bInterfaceProtocol.attr,
897  &dev_attr_modalias.attr,
898  &dev_attr_supports_autosuspend.attr,
899  NULL,
900 };
901 static struct attribute_group intf_attr_grp = {
902  .attrs = intf_attrs,
903 };
904 
905 static struct attribute *intf_assoc_attrs[] = {
906  &dev_attr_iad_bFirstInterface.attr,
907  &dev_attr_iad_bInterfaceCount.attr,
908  &dev_attr_iad_bFunctionClass.attr,
909  &dev_attr_iad_bFunctionSubClass.attr,
910  &dev_attr_iad_bFunctionProtocol.attr,
911  NULL,
912 };
913 
914 static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
915  struct attribute *a, int n)
916 {
917  struct device *dev = container_of(kobj, struct device, kobj);
918  struct usb_interface *intf = to_usb_interface(dev);
919 
920  if (intf->intf_assoc == NULL)
921  return 0;
922  return a->mode;
923 }
924 
925 static struct attribute_group intf_assoc_attr_grp = {
926  .attrs = intf_assoc_attrs,
927  .is_visible = intf_assoc_attrs_are_visible,
928 };
929 
931  &intf_attr_grp,
932  &intf_assoc_attr_grp,
933  NULL
934 };
935 
937 {
938  struct usb_device *udev = interface_to_usbdev(intf);
939  struct usb_host_interface *alt = intf->cur_altsetting;
940 
941  if (intf->sysfs_files_created || intf->unregistering)
942  return;
943 
944  if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
945  alt->string = usb_cache_string(udev, alt->desc.iInterface);
946  if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
947  ; /* We don't actually care if the function fails. */
948  intf->sysfs_files_created = 1;
949 }
950 
952 {
953  if (!intf->sysfs_files_created)
954  return;
955 
956  device_remove_file(&intf->dev, &dev_attr_interface);
957  intf->sysfs_files_created = 0;
958 }