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