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