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
40 usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
41 usb_actconfig_attr(bmAttributes, 1, "%2x\n")
42 usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
43
44 static ssize_t show_configuration_string(struct device *dev,
45 struct device_attribute *attr, char *buf)
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 */
59 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
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
76 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
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 \
86 udev = to_usb_device(dev); \
87 return sprintf(buf, "%s\n", udev->name); \
88 } \
89 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
90
91 usb_string_attr(product);
92 usb_string_attr(manufacturer);
93 usb_string_attr(serial);
94
95 static ssize_t
96 show_speed(struct device *dev, struct device_attribute *attr, char *buf)
97 {
98 struct usb_device *udev;
99 char *speed;
100
101 udev = to_usb_device(dev);
102
103 switch (udev->speed) {
104 case USB_SPEED_LOW:
105 speed = "1.5";
106 break;
107 case USB_SPEED_UNKNOWN:
108 case USB_SPEED_FULL:
109 speed = "12";
110 break;
111 case USB_SPEED_HIGH:
112 speed = "480";
113 break;
114 case USB_SPEED_VARIABLE:
115 speed = "480";
116 break;
117 case USB_SPEED_SUPER:
118 speed = "5000";
119 break;
120 default:
121 speed = "unknown";
122 }
123 return sprintf(buf, "%s\n", speed);
124 }
125 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
126
127 static ssize_t
128 show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
129 {
130 struct usb_device *udev;
131
132 udev = to_usb_device(dev);
133 return sprintf(buf, "%d\n", udev->bus->busnum);
134 }
135 static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
136
137 static ssize_t
138 show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
139 {
140 struct usb_device *udev;
141
142 udev = to_usb_device(dev);
143 return sprintf(buf, "%d\n", udev->devnum);
144 }
145 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
146
147 static ssize_t
148 show_version(struct device *dev, struct device_attribute *attr, char *buf)
149 {
150 struct usb_device *udev;
151 u16 bcdUSB;
152
153 udev = to_usb_device(dev);
154 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
155 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
156 }
157 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
158
159 static ssize_t
160 show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
161 {
162 struct usb_device *udev;
163
164 udev = to_usb_device(dev);
165 return sprintf(buf, "%d\n", udev->maxchild);
166 }
167 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
168
169 static ssize_t
170 show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
171 {
172 struct usb_device *udev;
173
174 udev = to_usb_device(dev);
175 return sprintf(buf, "0x%x\n", udev->quirks);
176 }
177 static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
178
179 static ssize_t
180 show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
181 {
182 struct usb_device *udev;
183
184 udev = to_usb_device(dev);
185 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
186 }
187 static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
188
189
190 #ifdef CONFIG_PM
191
192 static const char power_group[] = "power";
193
194 static ssize_t
195 show_persist(struct device *dev, struct device_attribute *attr, char *buf)
196 {
197 struct usb_device *udev = to_usb_device(dev);
198
199 return sprintf(buf, "%d\n", udev->persist_enabled);
200 }
201
202 static ssize_t
203 set_persist(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 value;
208
209 /* Hubs are always enabled for USB_PERSIST */
210 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
211 return -EPERM;
212
213 if (sscanf(buf, "%d", &value) != 1)
214 return -EINVAL;
215 usb_pm_lock(udev);
216 udev->persist_enabled = !!value;
217 usb_pm_unlock(udev);
218 return count;
219 }
220
221 static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
222
223 static int add_persist_attributes(struct device *dev)
224 {
225 int rc = 0;
226
227 if (is_usb_device(dev)) {
228 struct usb_device *udev = to_usb_device(dev);
229
230 /* Hubs are automatically enabled for USB_PERSIST,
231 * no point in creating the attribute file.
232 */
233 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
234 rc = sysfs_add_file_to_group(&dev->kobj,
235 &dev_attr_persist.attr,
236 power_group);
237 }
238 return rc;
239 }
240
241 static void remove_persist_attributes(struct device *dev)
242 {
243 sysfs_remove_file_from_group(&dev->kobj,
244 &dev_attr_persist.attr,
245 power_group);
246 }
247 #else
248
249 #define add_persist_attributes(dev) 0
250 #define remove_persist_attributes(dev) do {} while (0)
251
252 #endif /* CONFIG_PM */
253
254 #ifdef CONFIG_USB_SUSPEND
255
256 static ssize_t
257 show_connected_duration(struct device *dev, struct device_attribute *attr,
258 char *buf)
259 {
260 struct usb_device *udev = to_usb_device(dev);
261
262 return sprintf(buf, "%u\n",
263 jiffies_to_msecs(jiffies - udev->connect_time));
264 }
265
266 static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL);
267
268 /*
269 * If the device is resumed, the last time the device was suspended has
270 * been pre-subtracted from active_duration. We add the current time to
271 * get the duration that the device was actually active.
272 *
273 * If the device is suspended, the active_duration is up-to-date.
274 */
275 static ssize_t
276 show_active_duration(struct device *dev, struct device_attribute *attr,
277 char *buf)
278 {
279 struct usb_device *udev = to_usb_device(dev);
280 int duration;
281
282 if (udev->state != USB_STATE_SUSPENDED)
283 duration = jiffies_to_msecs(jiffies + udev->active_duration);
284 else
285 duration = jiffies_to_msecs(udev->active_duration);
286 return sprintf(buf, "%u\n", duration);
287 }
288
289 static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL);
290
291 static ssize_t
292 show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
293 {
294 struct usb_device *udev = to_usb_device(dev);
295
296 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
297 }
298
299 static ssize_t
300 set_autosuspend(struct device *dev, struct device_attribute *attr,
301 const char *buf, size_t count)
302 {
303 struct usb_device *udev = to_usb_device(dev);
304 int value;
305
306 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
307 value <= - INT_MAX/HZ)
308 return -EINVAL;
309 value *= HZ;
310
311 udev->autosuspend_delay = value;
312 if (value >= 0)
313 usb_try_autosuspend_device(udev);
314 else {
315 if (usb_autoresume_device(udev) == 0)
316 usb_autosuspend_device(udev);
317 }
318 return count;
319 }
320
321 static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
322 show_autosuspend, set_autosuspend);
323
324 static const char on_string[] = "on";
325 static const char auto_string[] = "auto";
326 static const char suspend_string[] = "suspend";
327
328 static ssize_t
329 show_level(struct device *dev, struct device_attribute *attr, char *buf)
330 {
331 struct usb_device *udev = to_usb_device(dev);
332 const char *p = auto_string;
333
334 if (udev->state == USB_STATE_SUSPENDED) {
335 if (udev->autoresume_disabled)
336 p = suspend_string;
337 } else {
338 if (udev->autosuspend_disabled)
339 p = on_string;
340 }
341 return sprintf(buf, "%s\n", p);
342 }
343
344 static ssize_t
345 set_level(struct device *dev, struct device_attribute *attr,
346 const char *buf, size_t count)
347 {
348 struct usb_device *udev = to_usb_device(dev);
349 int len = count;
350 char *cp;
351 int rc = 0;
352 int old_autosuspend_disabled, old_autoresume_disabled;
353
354 cp = memchr(buf, '\n', count);
355 if (cp)
356 len = cp - buf;
357
358 usb_lock_device(udev);
359 old_autosuspend_disabled = udev->autosuspend_disabled;
360 old_autoresume_disabled = udev->autoresume_disabled;
361
362 /* Setting the flags without calling usb_pm_lock is a subject to
363 * races, but who cares...
364 */
365 if (len == sizeof on_string - 1 &&
366 strncmp(buf, on_string, len) == 0) {
367 udev->autosuspend_disabled = 1;
368 udev->autoresume_disabled = 0;
369 rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
370
371 } else if (len == sizeof auto_string - 1 &&
372 strncmp(buf, auto_string, len) == 0) {
373 udev->autosuspend_disabled = 0;
374 udev->autoresume_disabled = 0;
375 rc = usb_external_resume_device(udev, PMSG_USER_RESUME);
376
377 } else if (len == sizeof suspend_string - 1 &&
378 strncmp(buf, suspend_string, len) == 0) {
379 udev->autosuspend_disabled = 0;
380 udev->autoresume_disabled = 1;
381 rc = usb_external_suspend_device(udev, PMSG_USER_SUSPEND);
382
383 } else
384 rc = -EINVAL;
385
386 if (rc) {
387 udev->autosuspend_disabled = old_autosuspend_disabled;
388 udev->autoresume_disabled = old_autoresume_disabled;
389 }
390 usb_unlock_device(udev);
391 return (rc < 0 ? rc : count);
392 }
393
394 static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
395
396 static int add_power_attributes(struct device *dev)
397 {
398 int rc = 0;
399
400 if (is_usb_device(dev)) {
401 rc = sysfs_add_file_to_group(&dev->kobj,
402 &dev_attr_autosuspend.attr,
403 power_group);
404 if (rc == 0)
405 rc = sysfs_add_file_to_group(&dev->kobj,
406 &dev_attr_level.attr,
407 power_group);
408 if (rc == 0)
409 rc = sysfs_add_file_to_group(&dev->kobj,
410 &dev_attr_connected_duration.attr,
411 power_group);
412 if (rc == 0)
413 rc = sysfs_add_file_to_group(&dev->kobj,
414 &dev_attr_active_duration.attr,
415 power_group);
416 }
417 return rc;
418 }
419
420 static void remove_power_attributes(struct device *dev)
421 {
422 sysfs_remove_file_from_group(&dev->kobj,
423 &dev_attr_active_duration.attr,
424 power_group);
425 sysfs_remove_file_from_group(&dev->kobj,
426 &dev_attr_connected_duration.attr,
427 power_group);
428 sysfs_remove_file_from_group(&dev->kobj,
429 &dev_attr_level.attr,
430 power_group);
431 sysfs_remove_file_from_group(&dev->kobj,
432 &dev_attr_autosuspend.attr,
433 power_group);
434 }
435
436 #else
437
438 #define add_power_attributes(dev) 0
439 #define remove_power_attributes(dev) do {} while (0)
440
441 #endif /* CONFIG_USB_SUSPEND */
442
443
444 /* Descriptor fields */
445 #define usb_descriptor_attr_le16(field, format_string) \
446 static ssize_t \
447 show_##field(struct device *dev, struct device_attribute *attr, \
448 char *buf) \
449 { \
450 struct usb_device *udev; \
451 \
452 udev = to_usb_device(dev); \
453 return sprintf(buf, format_string, \
454 le16_to_cpu(udev->descriptor.field)); \
455 } \
456 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
457
458 usb_descriptor_attr_le16(idVendor, "%04x\n")
459 usb_descriptor_attr_le16(idProduct, "%04x\n")
460 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
461
462 #define usb_descriptor_attr(field, format_string) \
463 static ssize_t \
464 show_##field(struct device *dev, struct device_attribute *attr, \
465 char *buf) \
466 { \
467 struct usb_device *udev; \
468 \
469 udev = to_usb_device(dev); \
470 return sprintf(buf, format_string, udev->descriptor.field); \
471 } \
472 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
473
474 usb_descriptor_attr(bDeviceClass, "%02x\n")
475 usb_descriptor_attr(bDeviceSubClass, "%02x\n")
476 usb_descriptor_attr(bDeviceProtocol, "%02x\n")
477 usb_descriptor_attr(bNumConfigurations, "%d\n")
478 usb_descriptor_attr(bMaxPacketSize0, "%d\n")
479
480
481
482 /* show if the device is authorized (1) or not (0) */
483 static ssize_t usb_dev_authorized_show(struct device *dev,
484 struct device_attribute *attr,
485 char *buf)
486 {
487 struct usb_device *usb_dev = to_usb_device(dev);
488 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
489 }
490
491
492 /*
493 * Authorize a device to be used in the system
494 *
495 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
496 */
497 static ssize_t usb_dev_authorized_store(struct device *dev,
498 struct device_attribute *attr,
499 const char *buf, size_t size)
500 {
501 ssize_t result;
502 struct usb_device *usb_dev = to_usb_device(dev);
503 unsigned val;
504 result = sscanf(buf, "%u\n", &val);
505 if (result != 1)
506 result = -EINVAL;
507 else if (val == 0)
508 result = usb_deauthorize_device(usb_dev);
509 else
510 result = usb_authorize_device(usb_dev);
511 return result < 0? result : size;
512 }
513
514 static DEVICE_ATTR(authorized, 0644,
515 usb_dev_authorized_show, usb_dev_authorized_store);
516
517
518 static struct attribute *dev_attrs[] = {
519 /* current configuration's attributes */
520 &dev_attr_configuration.attr,
521 &dev_attr_bNumInterfaces.attr,
522 &dev_attr_bConfigurationValue.attr,
523 &dev_attr_bmAttributes.attr,
524 &dev_attr_bMaxPower.attr,
525 &dev_attr_urbnum.attr,
526 /* device attributes */
527 &dev_attr_idVendor.attr,
528 &dev_attr_idProduct.attr,
529 &dev_attr_bcdDevice.attr,
530 &dev_attr_bDeviceClass.attr,
531 &dev_attr_bDeviceSubClass.attr,
532 &dev_attr_bDeviceProtocol.attr,
533 &dev_attr_bNumConfigurations.attr,
534 &dev_attr_bMaxPacketSize0.attr,
535 &dev_attr_speed.attr,
536 &dev_attr_busnum.attr,
537 &dev_attr_devnum.attr,
538 &dev_attr_version.attr,
539 &dev_attr_maxchild.attr,
540 &dev_attr_quirks.attr,
541 &dev_attr_authorized.attr,
542 NULL,
543 };
544 static struct attribute_group dev_attr_grp = {
545 .attrs = dev_attrs,
546 };
547
548 /* When modifying this list, be sure to modify dev_string_attrs_are_visible()
549 * accordingly.
550 */
551 static struct attribute *dev_string_attrs[] = {
552 &dev_attr_manufacturer.attr,
553 &dev_attr_product.attr,
554 &dev_attr_serial.attr,
555 NULL
556 };
557
558 static mode_t dev_string_attrs_are_visible(struct kobject *kobj,
559 struct attribute *a, int n)
560 {
561 struct device *dev = container_of(kobj, struct device, kobj);
562 struct usb_device *udev = to_usb_device(dev);
563
564 if (a == &dev_attr_manufacturer.attr) {
565 if (udev->manufacturer == NULL)
566 return 0;
567 } else if (a == &dev_attr_product.attr) {
568 if (udev->product == NULL)
569 return 0;
570 } else if (a == &dev_attr_serial.attr) {
571 if (udev->serial == NULL)
572 return 0;
573 }
574 return a->mode;
575 }
576
577 static struct attribute_group dev_string_attr_grp = {
578 .attrs = dev_string_attrs,
579 .is_visible = dev_string_attrs_are_visible,
580 };
581
582 struct attribute_group *usb_device_groups[] = {
583 &dev_attr_grp,
584 &dev_string_attr_grp,
585 NULL
586 };
587
588 /* Binary descriptors */
589
590 static ssize_t
591 read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
592 char *buf, loff_t off, size_t count)
593 {
594 struct device *dev = container_of(kobj, struct device, kobj);
595 struct usb_device *udev = to_usb_device(dev);
596 size_t nleft = count;
597 size_t srclen, n;
598 int cfgno;
599 void *src;
600
601 /* The binary attribute begins with the device descriptor.
602 * Following that are the raw descriptor entries for all the
603 * configurations (config plus subsidiary descriptors).
604 */
605 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
606 nleft > 0; ++cfgno) {
607 if (cfgno < 0) {
608 src = &udev->descriptor;
609 srclen = sizeof(struct usb_device_descriptor);
610 } else {
611 src = udev->rawdescriptors[cfgno];
612 srclen = __le16_to_cpu(udev->config[cfgno].desc.
613 wTotalLength);
614 }
615 if (off < srclen) {
616 n = min(nleft, srclen - (size_t) off);
617 memcpy(buf, src + off, n);
618 nleft -= n;
619 buf += n;
620 off = 0;
621 } else {
622 off -= srclen;
623 }
624 }
625 return count - nleft;
626 }
627
628 static struct bin_attribute dev_bin_attr_descriptors = {
629 .attr = {.name = "descriptors", .mode = 0444},
630 .read = read_descriptors,
631 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
632 };
633
634 int usb_create_sysfs_dev_files(struct usb_device *udev)
635 {
636 struct device *dev = &udev->dev;
637 int retval;
638
639 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
640 if (retval)
641 goto error;
642
643 retval = add_persist_attributes(dev);
644 if (retval)
645 goto error;
646
647 retval = add_power_attributes(dev);
648 if (retval)
649 goto error;
650 return retval;
651 error:
652 usb_remove_sysfs_dev_files(udev);
653 return retval;
654 }
655
656 void usb_remove_sysfs_dev_files(struct usb_device *udev)
657 {
658 struct device *dev = &udev->dev;
659
660 remove_power_attributes(dev);
661 remove_persist_attributes(dev);
662 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
663 }
664
665 /* Interface Accociation Descriptor fields */
666 #define usb_intf_assoc_attr(field, format_string) \
667 static ssize_t \
668 show_iad_##field(struct device *dev, struct device_attribute *attr, \
669 char *buf) \
670 { \
671 struct usb_interface *intf = to_usb_interface(dev); \
672 \
673 return sprintf(buf, format_string, \
674 intf->intf_assoc->field); \
675 } \
676 static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
677
678 usb_intf_assoc_attr(bFirstInterface, "%02x\n")
679 usb_intf_assoc_attr(bInterfaceCount, "%02d\n")
680 usb_intf_assoc_attr(bFunctionClass, "%02x\n")
681 usb_intf_assoc_attr(bFunctionSubClass, "%02x\n")
682 usb_intf_assoc_attr(bFunctionProtocol, "%02x\n")
683
684 /* Interface fields */
685 #define usb_intf_attr(field, format_string) \
686 static ssize_t \
687 show_##field(struct device *dev, struct device_attribute *attr, \
688 char *buf) \
689 { \
690 struct usb_interface *intf = to_usb_interface(dev); \
691 \
692 return sprintf(buf, format_string, \
693 intf->cur_altsetting->desc.field); \
694 } \
695 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
696
697 usb_intf_attr(bInterfaceNumber, "%02x\n")
698 usb_intf_attr(bAlternateSetting, "%2d\n")
699 usb_intf_attr(bNumEndpoints, "%02x\n")
700 usb_intf_attr(bInterfaceClass, "%02x\n")
701 usb_intf_attr(bInterfaceSubClass, "%02x\n")
702 usb_intf_attr(bInterfaceProtocol, "%02x\n")
703
704 static ssize_t show_interface_string(struct device *dev,
705 struct device_attribute *attr, char *buf)
706 {
707 struct usb_interface *intf;
708 char *string;
709
710 intf = to_usb_interface(dev);
711 string = intf->cur_altsetting->string;
712 barrier(); /* The altsetting might change! */
713
714 if (!string)
715 return 0;
716 return sprintf(buf, "%s\n", string);
717 }
718 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
719
720 static ssize_t show_modalias(struct device *dev,
721 struct device_attribute *attr, char *buf)
722 {
723 struct usb_interface *intf;
724 struct usb_device *udev;
725 struct usb_host_interface *alt;
726
727 intf = to_usb_interface(dev);
728 udev = interface_to_usbdev(intf);
729 alt = intf->cur_altsetting;
730
731 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
732 "ic%02Xisc%02Xip%02X\n",
733 le16_to_cpu(udev->descriptor.idVendor),
734 le16_to_cpu(udev->descriptor.idProduct),
735 le16_to_cpu(udev->descriptor.bcdDevice),
736 udev->descriptor.bDeviceClass,
737 udev->descriptor.bDeviceSubClass,
738 udev->descriptor.bDeviceProtocol,
739 alt->desc.bInterfaceClass,
740 alt->desc.bInterfaceSubClass,
741 alt->desc.bInterfaceProtocol);
742 }
743 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
744
745 static ssize_t show_supports_autosuspend(struct device *dev,
746 struct device_attribute *attr, char *buf)
747 {
748 struct usb_interface *intf;
749 struct usb_device *udev;
750 int ret;
751
752 intf = to_usb_interface(dev);
753 udev = interface_to_usbdev(intf);
754
755 usb_lock_device(udev);
756 /* Devices will be autosuspended even when an interface isn't claimed */
757 if (!intf->dev.driver ||
758 to_usb_driver(intf->dev.driver)->supports_autosuspend)
759 ret = sprintf(buf, "%u\n", 1);
760 else
761 ret = sprintf(buf, "%u\n", 0);
762 usb_unlock_device(udev);
763
764 return ret;
765 }
766 static DEVICE_ATTR(supports_autosuspend, S_IRUGO, show_supports_autosuspend, NULL);
767
768 static struct attribute *intf_attrs[] = {
769 &dev_attr_bInterfaceNumber.attr,
770 &dev_attr_bAlternateSetting.attr,
771 &dev_attr_bNumEndpoints.attr,
772 &dev_attr_bInterfaceClass.attr,
773 &dev_attr_bInterfaceSubClass.attr,
774 &dev_attr_bInterfaceProtocol.attr,
775 &dev_attr_modalias.attr,
776 &dev_attr_supports_autosuspend.attr,
777 NULL,
778 };
779 static struct attribute_group intf_attr_grp = {
780 .attrs = intf_attrs,
781 };
782
783 static struct attribute *intf_assoc_attrs[] = {
784 &dev_attr_iad_bFirstInterface.attr,
785 &dev_attr_iad_bInterfaceCount.attr,
786 &dev_attr_iad_bFunctionClass.attr,
787 &dev_attr_iad_bFunctionSubClass.attr,
788 &dev_attr_iad_bFunctionProtocol.attr,
789 NULL,
790 };
791
792 static mode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
793 struct attribute *a, int n)
794 {
795 struct device *dev = container_of(kobj, struct device, kobj);
796 struct usb_interface *intf = to_usb_interface(dev);
797
798 if (intf->intf_assoc == NULL)
799 return 0;
800 return a->mode;
801 }
802
803 static struct attribute_group intf_assoc_attr_grp = {
804 .attrs = intf_assoc_attrs,
805 .is_visible = intf_assoc_attrs_are_visible,
806 };
807
808 struct attribute_group *usb_interface_groups[] = {
809 &intf_attr_grp,
810 &intf_assoc_attr_grp,
811 NULL
812 };
813
814 int usb_create_sysfs_intf_files(struct usb_interface *intf)
815 {
816 struct usb_device *udev = interface_to_usbdev(intf);
817 struct usb_host_interface *alt = intf->cur_altsetting;
818 int retval;
819
820 if (intf->sysfs_files_created || intf->unregistering)
821 return 0;
822
823 if (alt->string == NULL &&
824 !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
825 alt->string = usb_cache_string(udev, alt->desc.iInterface);
826 if (alt->string)
827 retval = device_create_file(&intf->dev, &dev_attr_interface);
828 intf->sysfs_files_created = 1;
829 return 0;
830 }
831
832 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
833 {
834 if (!intf->sysfs_files_created)
835 return;
836
837 device_remove_file(&intf->dev, &dev_attr_interface);
838 intf->sysfs_files_created = 0;
839 }
840
|
This page was automatically generated by the
LXR engine.
|