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