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