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