blob: 3f81a3dc6867638a17ab4b0e2850aa1d79ec33c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Alan Stern2add5222007-03-20 14:59:39 -040014#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/usb.h>
Alan Stern1662e3a2009-03-18 14:28:53 -040016#include <linux/usb/quirks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "usb.h"
18
19/* Active configuration fields */
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010020#define usb_actconfig_show(field, format_string) \
Oliver Neukum92516442007-01-23 15:55:28 -050021static ssize_t show_##field(struct device *dev, \
Alan Sternb724ae72005-10-24 15:36:00 -040022 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070023{ \
24 struct usb_device *udev; \
25 struct usb_host_config *actconfig; \
26 \
Oliver Neukum92516442007-01-23 15:55:28 -050027 udev = to_usb_device(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 actconfig = udev->actconfig; \
29 if (actconfig) \
Oliver Neukum92516442007-01-23 15:55:28 -050030 return sprintf(buf, format_string, \
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010031 actconfig->desc.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 else \
33 return 0; \
34} \
35
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010036#define usb_actconfig_attr(field, format_string) \
37 usb_actconfig_show(field, format_string) \
38 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010040usb_actconfig_attr(bNumInterfaces, "%2d\n")
41usb_actconfig_attr(bmAttributes, "%2x\n")
42
43static ssize_t show_bMaxPower(struct device *dev,
44 struct device_attribute *attr, char *buf)
45{
46 struct usb_device *udev;
47 struct usb_host_config *actconfig;
48
49 udev = to_usb_device(dev);
50 actconfig = udev->actconfig;
51 if (!actconfig)
52 return 0;
53 return sprintf(buf, "%dmA\n", usb_get_max_power(udev, actconfig));
54}
55static DEVICE_ATTR(bMaxPower, S_IRUGO, show_bMaxPower, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Alan Sternb724ae72005-10-24 15:36:00 -040057static ssize_t show_configuration_string(struct device *dev,
58 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 struct usb_device *udev;
61 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Oliver Neukum92516442007-01-23 15:55:28 -050063 udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 actconfig = udev->actconfig;
65 if ((!actconfig) || (!actconfig->string))
66 return 0;
Alan Stern4f62efe2005-10-24 16:24:14 -040067 return sprintf(buf, "%s\n", actconfig->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
70
71/* configuration value is always present, and r/w */
Sebastian Andrzej Siewior8d8479d2012-12-18 15:25:46 +010072usb_actconfig_show(bConfigurationValue, "%u\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -050075set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
Alan Sternb724ae72005-10-24 15:36:00 -040076 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Oliver Neukum92516442007-01-23 15:55:28 -050078 struct usb_device *udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 int config, value;
80
Alan Stern3f141e22007-02-08 16:40:43 -050081 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 return -EINVAL;
83 usb_lock_device(udev);
Oliver Neukum92516442007-01-23 15:55:28 -050084 value = usb_set_configuration(udev, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 usb_unlock_device(udev);
86 return (value < 0) ? value : count;
87}
88
Alan Stern356c05d2012-05-14 13:30:03 -040089static DEVICE_ATTR_IGNORE_LOCKDEP(bConfigurationValue, S_IRUGO | S_IWUSR,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 show_bConfigurationValue, set_bConfigurationValue);
91
92/* String fields */
93#define usb_string_attr(name) \
Alan Sternb724ae72005-10-24 15:36:00 -040094static ssize_t show_##name(struct device *dev, \
95 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{ \
97 struct usb_device *udev; \
Alan Sternda307122009-12-08 15:54:44 -050098 int retval; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 \
Oliver Neukum92516442007-01-23 15:55:28 -0500100 udev = to_usb_device(dev); \
Alan Sternda307122009-12-08 15:54:44 -0500101 usb_lock_device(udev); \
102 retval = sprintf(buf, "%s\n", udev->name); \
103 usb_unlock_device(udev); \
104 return retval; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105} \
106static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
107
108usb_string_attr(product);
109usb_string_attr(manufacturer);
110usb_string_attr(serial);
111
112static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500113show_speed(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 struct usb_device *udev;
116 char *speed;
117
Oliver Neukum92516442007-01-23 15:55:28 -0500118 udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 switch (udev->speed) {
121 case USB_SPEED_LOW:
122 speed = "1.5";
123 break;
124 case USB_SPEED_UNKNOWN:
125 case USB_SPEED_FULL:
126 speed = "12";
127 break;
128 case USB_SPEED_HIGH:
129 speed = "480";
130 break;
Greg Kroah-Hartman551cdbb2010-01-14 11:08:04 -0800131 case USB_SPEED_WIRELESS:
Greg Kroah-Hartmanb132b042010-01-14 10:33:19 -0800132 speed = "480";
133 break;
134 case USB_SPEED_SUPER:
135 speed = "5000";
136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 default:
138 speed = "unknown";
139 }
Oliver Neukum92516442007-01-23 15:55:28 -0500140 return sprintf(buf, "%s\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
143
144static ssize_t
Alan Stern83f7d952007-04-25 15:15:43 -0400145show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
146{
147 struct usb_device *udev;
148
149 udev = to_usb_device(dev);
150 return sprintf(buf, "%d\n", udev->bus->busnum);
151}
152static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
153
154static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500155show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct usb_device *udev;
158
Oliver Neukum92516442007-01-23 15:55:28 -0500159 udev = to_usb_device(dev);
160 return sprintf(buf, "%d\n", udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
163
164static ssize_t
Greg Kroah-Hartman9af23622009-11-30 11:15:02 -0800165show_devpath(struct device *dev, struct device_attribute *attr, char *buf)
166{
167 struct usb_device *udev;
168
169 udev = to_usb_device(dev);
170 return sprintf(buf, "%s\n", udev->devpath);
171}
172static DEVICE_ATTR(devpath, S_IRUGO, show_devpath, NULL);
173
174static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500175show_version(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct usb_device *udev;
178 u16 bcdUSB;
179
180 udev = to_usb_device(dev);
181 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
182 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
183}
184static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
185
186static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500187show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct usb_device *udev;
190
Oliver Neukum92516442007-01-23 15:55:28 -0500191 udev = to_usb_device(dev);
192 return sprintf(buf, "%d\n", udev->maxchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
195
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100196static ssize_t
197show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
198{
199 struct usb_device *udev;
200
201 udev = to_usb_device(dev);
202 return sprintf(buf, "0x%x\n", udev->quirks);
203}
204static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
205
Sarah Sharp4d59d8a2007-10-03 14:56:03 -0700206static ssize_t
Oliver Neukumef955342010-01-16 01:33:03 +0100207show_avoid_reset_quirk(struct device *dev, struct device_attribute *attr, char *buf)
208{
209 struct usb_device *udev;
210
211 udev = to_usb_device(dev);
Lan Tianyu7fda9532012-08-17 16:44:56 +0800212 return sprintf(buf, "%d\n", !!(udev->quirks & USB_QUIRK_RESET));
Oliver Neukumef955342010-01-16 01:33:03 +0100213}
214
215static ssize_t
216set_avoid_reset_quirk(struct device *dev, struct device_attribute *attr,
217 const char *buf, size_t count)
218{
219 struct usb_device *udev = to_usb_device(dev);
Lan Tianyu7fc2cc32012-08-03 16:30:35 +0800220 int val;
Oliver Neukumef955342010-01-16 01:33:03 +0100221
Lan Tianyu7fc2cc32012-08-03 16:30:35 +0800222 if (sscanf(buf, "%d", &val) != 1 || val < 0 || val > 1)
Oliver Neukumef955342010-01-16 01:33:03 +0100223 return -EINVAL;
224 usb_lock_device(udev);
Lan Tianyu7fc2cc32012-08-03 16:30:35 +0800225 if (val)
Lan Tianyu7fda9532012-08-17 16:44:56 +0800226 udev->quirks |= USB_QUIRK_RESET;
Oliver Neukumef955342010-01-16 01:33:03 +0100227 else
Lan Tianyu7fda9532012-08-17 16:44:56 +0800228 udev->quirks &= ~USB_QUIRK_RESET;
Oliver Neukumef955342010-01-16 01:33:03 +0100229 usb_unlock_device(udev);
230 return count;
231}
232
233static DEVICE_ATTR(avoid_reset_quirk, S_IRUGO | S_IWUSR,
234 show_avoid_reset_quirk, set_avoid_reset_quirk);
235
236static ssize_t
Sarah Sharp4d59d8a2007-10-03 14:56:03 -0700237show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
238{
239 struct usb_device *udev;
240
241 udev = to_usb_device(dev);
242 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
243}
244static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
245
Matthew Garrett0846e7e2012-02-03 17:11:54 -0500246static ssize_t
247show_removable(struct device *dev, struct device_attribute *attr, char *buf)
248{
249 struct usb_device *udev;
250 char *state;
251
252 udev = to_usb_device(dev);
253
254 switch (udev->removable) {
255 case USB_DEVICE_REMOVABLE:
256 state = "removable";
257 break;
258 case USB_DEVICE_FIXED:
259 state = "fixed";
260 break;
261 default:
262 state = "unknown";
263 }
264
265 return sprintf(buf, "%s\n", state);
266}
267static DEVICE_ATTR(removable, S_IRUGO, show_removable, NULL);
Alan Sternb41a60e2007-05-30 15:39:33 -0400268
Sarah Sharp024f1172012-07-05 17:17:24 -0700269static ssize_t
270show_ltm_capable(struct device *dev, struct device_attribute *attr, char *buf)
271{
272 if (usb_device_supports_ltm(to_usb_device(dev)))
273 return sprintf(buf, "%s\n", "yes");
274 return sprintf(buf, "%s\n", "no");
275}
276static DEVICE_ATTR(ltm_capable, S_IRUGO, show_ltm_capable, NULL);
277
Alan Sternfeccc302008-03-03 15:15:59 -0500278#ifdef CONFIG_PM
Alan Sternb41a60e2007-05-30 15:39:33 -0400279
Alan Sternb41a60e2007-05-30 15:39:33 -0400280static ssize_t
281show_persist(struct device *dev, struct device_attribute *attr, char *buf)
282{
283 struct usb_device *udev = to_usb_device(dev);
284
285 return sprintf(buf, "%d\n", udev->persist_enabled);
286}
287
288static ssize_t
289set_persist(struct device *dev, struct device_attribute *attr,
290 const char *buf, size_t count)
291{
292 struct usb_device *udev = to_usb_device(dev);
293 int value;
294
295 /* Hubs are always enabled for USB_PERSIST */
296 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
297 return -EPERM;
298
299 if (sscanf(buf, "%d", &value) != 1)
300 return -EINVAL;
Alan Stern0c4db6d2010-01-08 12:56:42 -0500301
302 usb_lock_device(udev);
Alan Sternb41a60e2007-05-30 15:39:33 -0400303 udev->persist_enabled = !!value;
Alan Stern0c4db6d2010-01-08 12:56:42 -0500304 usb_unlock_device(udev);
Alan Sternb41a60e2007-05-30 15:39:33 -0400305 return count;
306}
307
308static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
309
310static int add_persist_attributes(struct device *dev)
311{
312 int rc = 0;
313
314 if (is_usb_device(dev)) {
315 struct usb_device *udev = to_usb_device(dev);
316
Alan Sternfeccc302008-03-03 15:15:59 -0500317 /* Hubs are automatically enabled for USB_PERSIST,
318 * no point in creating the attribute file.
319 */
320 if (udev->descriptor.bDeviceClass != USB_CLASS_HUB)
321 rc = sysfs_add_file_to_group(&dev->kobj,
322 &dev_attr_persist.attr,
Alan Stern045cac62010-11-15 15:57:07 -0500323 power_group_name);
Alan Sternb41a60e2007-05-30 15:39:33 -0400324 }
325 return rc;
326}
327
328static void remove_persist_attributes(struct device *dev)
329{
330 sysfs_remove_file_from_group(&dev->kobj,
331 &dev_attr_persist.attr,
Alan Stern045cac62010-11-15 15:57:07 -0500332 power_group_name);
Alan Sternb41a60e2007-05-30 15:39:33 -0400333}
Alan Sternb41a60e2007-05-30 15:39:33 -0400334#else
335
336#define add_persist_attributes(dev) 0
337#define remove_persist_attributes(dev) do {} while (0)
338
Alan Sternfeccc302008-03-03 15:15:59 -0500339#endif /* CONFIG_PM */
Alan Sternb41a60e2007-05-30 15:39:33 -0400340
Alan Stern19c26232007-02-20 15:03:32 -0500341#ifdef CONFIG_USB_SUSPEND
342
343static ssize_t
Sarah Sharp15123002007-12-21 16:54:15 -0800344show_connected_duration(struct device *dev, struct device_attribute *attr,
345 char *buf)
346{
347 struct usb_device *udev = to_usb_device(dev);
348
349 return sprintf(buf, "%u\n",
350 jiffies_to_msecs(jiffies - udev->connect_time));
351}
352
353static DEVICE_ATTR(connected_duration, S_IRUGO, show_connected_duration, NULL);
354
355/*
356 * If the device is resumed, the last time the device was suspended has
357 * been pre-subtracted from active_duration. We add the current time to
358 * get the duration that the device was actually active.
359 *
360 * If the device is suspended, the active_duration is up-to-date.
361 */
362static ssize_t
363show_active_duration(struct device *dev, struct device_attribute *attr,
364 char *buf)
365{
366 struct usb_device *udev = to_usb_device(dev);
367 int duration;
368
369 if (udev->state != USB_STATE_SUSPENDED)
370 duration = jiffies_to_msecs(jiffies + udev->active_duration);
371 else
372 duration = jiffies_to_msecs(udev->active_duration);
373 return sprintf(buf, "%u\n", duration);
374}
375
376static DEVICE_ATTR(active_duration, S_IRUGO, show_active_duration, NULL);
377
378static ssize_t
Alan Stern19c26232007-02-20 15:03:32 -0500379show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
380{
Alan Sternfcc4a012010-11-15 15:57:51 -0500381 return sprintf(buf, "%d\n", dev->power.autosuspend_delay / 1000);
Alan Stern19c26232007-02-20 15:03:32 -0500382}
383
384static ssize_t
385set_autosuspend(struct device *dev, struct device_attribute *attr,
386 const char *buf, size_t count)
387{
Alan Sternfcc4a012010-11-15 15:57:51 -0500388 int value;
Alan Stern19c26232007-02-20 15:03:32 -0500389
Alan Sternfcc4a012010-11-15 15:57:51 -0500390 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/1000 ||
391 value <= -INT_MAX/1000)
Alan Stern19c26232007-02-20 15:03:32 -0500392 return -EINVAL;
Alan Stern19c26232007-02-20 15:03:32 -0500393
Alan Sternfcc4a012010-11-15 15:57:51 -0500394 pm_runtime_set_autosuspend_delay(dev, value * 1000);
Alan Stern19c26232007-02-20 15:03:32 -0500395 return count;
396}
397
398static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
399 show_autosuspend, set_autosuspend);
400
Alan Stern2add5222007-03-20 14:59:39 -0400401static const char on_string[] = "on";
402static const char auto_string[] = "auto";
Alan Stern2add5222007-03-20 14:59:39 -0400403
Alan Sterna9030982010-04-02 13:22:16 -0400404static void warn_level(void) {
405 static int level_warned;
406
407 if (!level_warned) {
408 level_warned = 1;
409 printk(KERN_WARNING "WARNING! power/level is deprecated; "
410 "use power/control instead\n");
411 }
412}
413
Alan Stern2add5222007-03-20 14:59:39 -0400414static ssize_t
415show_level(struct device *dev, struct device_attribute *attr, char *buf)
416{
417 struct usb_device *udev = to_usb_device(dev);
418 const char *p = auto_string;
419
Alan Sterna9030982010-04-02 13:22:16 -0400420 warn_level();
Alan Stern9e18c822010-04-02 13:22:09 -0400421 if (udev->state != USB_STATE_SUSPENDED && !udev->dev.power.runtime_auto)
Alan Stern8e4ceb32009-12-07 13:01:37 -0500422 p = on_string;
Alan Stern2add5222007-03-20 14:59:39 -0400423 return sprintf(buf, "%s\n", p);
424}
425
426static ssize_t
427set_level(struct device *dev, struct device_attribute *attr,
428 const char *buf, size_t count)
429{
430 struct usb_device *udev = to_usb_device(dev);
431 int len = count;
432 char *cp;
Alan Stern9e18c822010-04-02 13:22:09 -0400433 int rc = count;
Alan Stern2add5222007-03-20 14:59:39 -0400434
Alan Sterna9030982010-04-02 13:22:16 -0400435 warn_level();
Alan Stern2add5222007-03-20 14:59:39 -0400436 cp = memchr(buf, '\n', count);
437 if (cp)
438 len = cp - buf;
439
440 usb_lock_device(udev);
441
Alan Stern2add5222007-03-20 14:59:39 -0400442 if (len == sizeof on_string - 1 &&
Alan Stern088f7fe2010-01-08 12:56:54 -0500443 strncmp(buf, on_string, len) == 0)
Alan Stern9e18c822010-04-02 13:22:09 -0400444 usb_disable_autosuspend(udev);
Alan Stern2add5222007-03-20 14:59:39 -0400445
Alan Stern088f7fe2010-01-08 12:56:54 -0500446 else if (len == sizeof auto_string - 1 &&
447 strncmp(buf, auto_string, len) == 0)
Alan Stern9e18c822010-04-02 13:22:09 -0400448 usb_enable_autosuspend(udev);
Alan Stern2add5222007-03-20 14:59:39 -0400449
Alan Stern088f7fe2010-01-08 12:56:54 -0500450 else
Alan Stern2add5222007-03-20 14:59:39 -0400451 rc = -EINVAL;
452
453 usb_unlock_device(udev);
Alan Stern9e18c822010-04-02 13:22:09 -0400454 return rc;
Alan Stern2add5222007-03-20 14:59:39 -0400455}
456
457static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
458
Andiry Xuc1045e82011-09-23 14:19:53 -0700459static ssize_t
460show_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
461 char *buf)
462{
463 struct usb_device *udev = to_usb_device(dev);
464 const char *p;
465
466 if (udev->usb2_hw_lpm_enabled == 1)
467 p = "enabled";
468 else
469 p = "disabled";
470
471 return sprintf(buf, "%s\n", p);
472}
473
474static ssize_t
475set_usb2_hardware_lpm(struct device *dev, struct device_attribute *attr,
476 const char *buf, size_t count)
477{
478 struct usb_device *udev = to_usb_device(dev);
479 bool value;
480 int ret;
481
482 usb_lock_device(udev);
483
484 ret = strtobool(buf, &value);
485
486 if (!ret)
487 ret = usb_set_usb2_hardware_lpm(udev, value);
488
489 usb_unlock_device(udev);
490
491 if (!ret)
492 return count;
493
494 return ret;
495}
496
497static DEVICE_ATTR(usb2_hardware_lpm, S_IRUGO | S_IWUSR, show_usb2_hardware_lpm,
498 set_usb2_hardware_lpm);
499
500static struct attribute *usb2_hardware_lpm_attr[] = {
501 &dev_attr_usb2_hardware_lpm.attr,
502 NULL,
503};
504static struct attribute_group usb2_hardware_lpm_attr_group = {
505 .name = power_group_name,
506 .attrs = usb2_hardware_lpm_attr,
507};
508
Alan Stern045cac62010-11-15 15:57:07 -0500509static struct attribute *power_attrs[] = {
510 &dev_attr_autosuspend.attr,
511 &dev_attr_level.attr,
512 &dev_attr_connected_duration.attr,
513 &dev_attr_active_duration.attr,
514 NULL,
515};
516static struct attribute_group power_attr_group = {
517 .name = power_group_name,
518 .attrs = power_attrs,
519};
520
Alan Stern19c26232007-02-20 15:03:32 -0500521static int add_power_attributes(struct device *dev)
522{
523 int rc = 0;
524
Andiry Xuc1045e82011-09-23 14:19:53 -0700525 if (is_usb_device(dev)) {
526 struct usb_device *udev = to_usb_device(dev);
Alan Stern045cac62010-11-15 15:57:07 -0500527 rc = sysfs_merge_group(&dev->kobj, &power_attr_group);
Andiry Xuc1045e82011-09-23 14:19:53 -0700528 if (udev->usb2_hw_lpm_capable == 1)
529 rc = sysfs_merge_group(&dev->kobj,
530 &usb2_hardware_lpm_attr_group);
531 }
532
Alan Stern19c26232007-02-20 15:03:32 -0500533 return rc;
534}
535
536static void remove_power_attributes(struct device *dev)
537{
Andiry Xuc1045e82011-09-23 14:19:53 -0700538 sysfs_unmerge_group(&dev->kobj, &usb2_hardware_lpm_attr_group);
Alan Stern045cac62010-11-15 15:57:07 -0500539 sysfs_unmerge_group(&dev->kobj, &power_attr_group);
Alan Stern19c26232007-02-20 15:03:32 -0500540}
541
542#else
543
544#define add_power_attributes(dev) 0
545#define remove_power_attributes(dev) do {} while (0)
546
547#endif /* CONFIG_USB_SUSPEND */
548
Alan Sternb41a60e2007-05-30 15:39:33 -0400549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/* Descriptor fields */
551#define usb_descriptor_attr_le16(field, format_string) \
552static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500553show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400554 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{ \
556 struct usb_device *udev; \
557 \
Oliver Neukum92516442007-01-23 15:55:28 -0500558 udev = to_usb_device(dev); \
559 return sprintf(buf, format_string, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 le16_to_cpu(udev->descriptor.field)); \
561} \
562static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
563
564usb_descriptor_attr_le16(idVendor, "%04x\n")
565usb_descriptor_attr_le16(idProduct, "%04x\n")
566usb_descriptor_attr_le16(bcdDevice, "%04x\n")
567
568#define usb_descriptor_attr(field, format_string) \
569static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500570show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400571 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{ \
573 struct usb_device *udev; \
574 \
Oliver Neukum92516442007-01-23 15:55:28 -0500575 udev = to_usb_device(dev); \
576 return sprintf(buf, format_string, udev->descriptor.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577} \
578static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
579
Oliver Neukum92516442007-01-23 15:55:28 -0500580usb_descriptor_attr(bDeviceClass, "%02x\n")
581usb_descriptor_attr(bDeviceSubClass, "%02x\n")
582usb_descriptor_attr(bDeviceProtocol, "%02x\n")
583usb_descriptor_attr(bNumConfigurations, "%d\n")
584usb_descriptor_attr(bMaxPacketSize0, "%d\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700586
587
588/* show if the device is authorized (1) or not (0) */
589static ssize_t usb_dev_authorized_show(struct device *dev,
590 struct device_attribute *attr,
591 char *buf)
592{
593 struct usb_device *usb_dev = to_usb_device(dev);
594 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
595}
596
597
598/*
599 * Authorize a device to be used in the system
600 *
601 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
602 */
603static ssize_t usb_dev_authorized_store(struct device *dev,
604 struct device_attribute *attr,
605 const char *buf, size_t size)
606{
607 ssize_t result;
608 struct usb_device *usb_dev = to_usb_device(dev);
609 unsigned val;
610 result = sscanf(buf, "%u\n", &val);
611 if (result != 1)
612 result = -EINVAL;
613 else if (val == 0)
614 result = usb_deauthorize_device(usb_dev);
615 else
616 result = usb_authorize_device(usb_dev);
617 return result < 0? result : size;
618}
619
Alan Stern356c05d2012-05-14 13:30:03 -0400620static DEVICE_ATTR_IGNORE_LOCKDEP(authorized, 0644,
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700621 usb_dev_authorized_show, usb_dev_authorized_store);
622
Alan Stern253e0572009-10-27 15:20:13 -0400623/* "Safely remove a device" */
624static ssize_t usb_remove_store(struct device *dev,
625 struct device_attribute *attr,
626 const char *buf, size_t count)
627{
628 struct usb_device *udev = to_usb_device(dev);
629 int rc = 0;
630
631 usb_lock_device(udev);
632 if (udev->state != USB_STATE_NOTATTACHED) {
633
634 /* To avoid races, first unconfigure and then remove */
635 usb_set_configuration(udev, -1);
636 rc = usb_remove_device(udev);
637 }
638 if (rc == 0)
639 rc = count;
640 usb_unlock_device(udev);
641 return rc;
642}
Alan Stern356c05d2012-05-14 13:30:03 -0400643static DEVICE_ATTR_IGNORE_LOCKDEP(remove, 0200, NULL, usb_remove_store);
Alan Stern253e0572009-10-27 15:20:13 -0400644
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static struct attribute *dev_attrs[] = {
647 /* current configuration's attributes */
Alan Sternb6eb2d82006-07-06 15:37:42 -0400648 &dev_attr_configuration.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 &dev_attr_bNumInterfaces.attr,
650 &dev_attr_bConfigurationValue.attr,
651 &dev_attr_bmAttributes.attr,
652 &dev_attr_bMaxPower.attr,
653 /* device attributes */
Greg Kroah-Hartman9af23622009-11-30 11:15:02 -0800654 &dev_attr_urbnum.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 &dev_attr_idVendor.attr,
656 &dev_attr_idProduct.attr,
657 &dev_attr_bcdDevice.attr,
658 &dev_attr_bDeviceClass.attr,
659 &dev_attr_bDeviceSubClass.attr,
660 &dev_attr_bDeviceProtocol.attr,
661 &dev_attr_bNumConfigurations.attr,
Greg Kroah-Hartmancf5910b2005-06-29 16:53:29 -0700662 &dev_attr_bMaxPacketSize0.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 &dev_attr_speed.attr,
Alan Stern83f7d952007-04-25 15:15:43 -0400664 &dev_attr_busnum.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 &dev_attr_devnum.attr,
Greg Kroah-Hartman9af23622009-11-30 11:15:02 -0800666 &dev_attr_devpath.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 &dev_attr_version.attr,
668 &dev_attr_maxchild.attr,
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100669 &dev_attr_quirks.attr,
Oliver Neukumef955342010-01-16 01:33:03 +0100670 &dev_attr_avoid_reset_quirk.attr,
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700671 &dev_attr_authorized.attr,
Alan Stern253e0572009-10-27 15:20:13 -0400672 &dev_attr_remove.attr,
Matthew Garrett0846e7e2012-02-03 17:11:54 -0500673 &dev_attr_removable.attr,
Sarah Sharp024f1172012-07-05 17:17:24 -0700674 &dev_attr_ltm_capable.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 NULL,
676};
677static struct attribute_group dev_attr_grp = {
678 .attrs = dev_attrs,
679};
680
Alan Stern2e5f10e2008-04-30 15:37:19 -0400681/* When modifying this list, be sure to modify dev_string_attrs_are_visible()
682 * accordingly.
683 */
684static struct attribute *dev_string_attrs[] = {
685 &dev_attr_manufacturer.attr,
686 &dev_attr_product.attr,
687 &dev_attr_serial.attr,
688 NULL
689};
690
Al Viro587a1f12011-07-23 23:11:19 -0400691static umode_t dev_string_attrs_are_visible(struct kobject *kobj,
Alan Stern2e5f10e2008-04-30 15:37:19 -0400692 struct attribute *a, int n)
693{
H Hartley Sweetena864e3aa52009-04-15 21:34:40 -0400694 struct device *dev = container_of(kobj, struct device, kobj);
695 struct usb_device *udev = to_usb_device(dev);
Alan Stern2e5f10e2008-04-30 15:37:19 -0400696
697 if (a == &dev_attr_manufacturer.attr) {
698 if (udev->manufacturer == NULL)
699 return 0;
700 } else if (a == &dev_attr_product.attr) {
701 if (udev->product == NULL)
702 return 0;
703 } else if (a == &dev_attr_serial.attr) {
704 if (udev->serial == NULL)
705 return 0;
706 }
707 return a->mode;
708}
709
710static struct attribute_group dev_string_attr_grp = {
711 .attrs = dev_string_attrs,
712 .is_visible = dev_string_attrs_are_visible,
713};
714
David Brownella4dbd672009-06-24 10:06:31 -0700715const struct attribute_group *usb_device_groups[] = {
Alan Stern2e5f10e2008-04-30 15:37:19 -0400716 &dev_attr_grp,
717 &dev_string_attr_grp,
718 NULL
719};
720
Alan Stern69d42a72007-07-12 17:06:23 -0400721/* Binary descriptors */
722
723static ssize_t
Chris Wright2c3c8be2010-05-12 18:28:57 -0700724read_descriptors(struct file *filp, struct kobject *kobj,
725 struct bin_attribute *attr,
Alan Stern69d42a72007-07-12 17:06:23 -0400726 char *buf, loff_t off, size_t count)
727{
H Hartley Sweetena864e3aa52009-04-15 21:34:40 -0400728 struct device *dev = container_of(kobj, struct device, kobj);
729 struct usb_device *udev = to_usb_device(dev);
Alan Stern69d42a72007-07-12 17:06:23 -0400730 size_t nleft = count;
731 size_t srclen, n;
Alan Stern217a9082008-05-20 16:40:42 -0400732 int cfgno;
733 void *src;
Alan Stern69d42a72007-07-12 17:06:23 -0400734
Alan Stern217a9082008-05-20 16:40:42 -0400735 /* The binary attribute begins with the device descriptor.
736 * Following that are the raw descriptor entries for all the
737 * configurations (config plus subsidiary descriptors).
Alan Stern69d42a72007-07-12 17:06:23 -0400738 */
Alan Stern217a9082008-05-20 16:40:42 -0400739 for (cfgno = -1; cfgno < udev->descriptor.bNumConfigurations &&
740 nleft > 0; ++cfgno) {
741 if (cfgno < 0) {
742 src = &udev->descriptor;
743 srclen = sizeof(struct usb_device_descriptor);
744 } else {
745 src = udev->rawdescriptors[cfgno];
746 srclen = __le16_to_cpu(udev->config[cfgno].desc.
747 wTotalLength);
748 }
Alan Stern69d42a72007-07-12 17:06:23 -0400749 if (off < srclen) {
Alan Stern217a9082008-05-20 16:40:42 -0400750 n = min(nleft, srclen - (size_t) off);
751 memcpy(buf, src + off, n);
Alan Stern69d42a72007-07-12 17:06:23 -0400752 nleft -= n;
Alan Stern217a9082008-05-20 16:40:42 -0400753 buf += n;
754 off = 0;
755 } else {
756 off -= srclen;
Alan Stern69d42a72007-07-12 17:06:23 -0400757 }
758 }
Alan Stern69d42a72007-07-12 17:06:23 -0400759 return count - nleft;
760}
761
762static struct bin_attribute dev_bin_attr_descriptors = {
763 .attr = {.name = "descriptors", .mode = 0444},
764 .read = read_descriptors,
765 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
766};
767
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700768int usb_create_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct device *dev = &udev->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700771 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Alan Stern69d42a72007-07-12 17:06:23 -0400773 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
774 if (retval)
775 goto error;
776
Alan Sternb41a60e2007-05-30 15:39:33 -0400777 retval = add_persist_attributes(dev);
778 if (retval)
779 goto error;
780
Alan Stern19c26232007-02-20 15:03:32 -0500781 retval = add_power_attributes(dev);
782 if (retval)
783 goto error;
Alan Stern3b23dd62008-12-05 14:10:34 -0500784 return retval;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700785error:
Alan Sternaa084f32007-02-20 14:59:59 -0500786 usb_remove_sysfs_dev_files(udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700787 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788}
789
Oliver Neukum92516442007-01-23 15:55:28 -0500790void usb_remove_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 struct device *dev = &udev->dev;
793
Alan Stern19c26232007-02-20 15:03:32 -0500794 remove_power_attributes(dev);
Alan Sternb41a60e2007-05-30 15:39:33 -0400795 remove_persist_attributes(dev);
Alan Stern69d42a72007-07-12 17:06:23 -0400796 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Craig W. Nadler165fe972007-06-15 23:14:35 -0400799/* Interface Accociation Descriptor fields */
800#define usb_intf_assoc_attr(field, format_string) \
801static ssize_t \
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800802show_iad_##field(struct device *dev, struct device_attribute *attr, \
Craig W. Nadler165fe972007-06-15 23:14:35 -0400803 char *buf) \
804{ \
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800805 struct usb_interface *intf = to_usb_interface(dev); \
Craig W. Nadler165fe972007-06-15 23:14:35 -0400806 \
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800807 return sprintf(buf, format_string, \
808 intf->intf_assoc->field); \
Craig W. Nadler165fe972007-06-15 23:14:35 -0400809} \
810static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
811
Greg Kroah-Hartman2c044a42008-01-30 15:21:33 -0800812usb_intf_assoc_attr(bFirstInterface, "%02x\n")
813usb_intf_assoc_attr(bInterfaceCount, "%02d\n")
814usb_intf_assoc_attr(bFunctionClass, "%02x\n")
815usb_intf_assoc_attr(bFunctionSubClass, "%02x\n")
816usb_intf_assoc_attr(bFunctionProtocol, "%02x\n")
Craig W. Nadler165fe972007-06-15 23:14:35 -0400817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818/* Interface fields */
819#define usb_intf_attr(field, format_string) \
820static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500821show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400822 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{ \
Oliver Neukum92516442007-01-23 15:55:28 -0500824 struct usb_interface *intf = to_usb_interface(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 \
Oliver Neukum92516442007-01-23 15:55:28 -0500826 return sprintf(buf, format_string, \
Alan Sternb724ae72005-10-24 15:36:00 -0400827 intf->cur_altsetting->desc.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828} \
829static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
830
Oliver Neukum92516442007-01-23 15:55:28 -0500831usb_intf_attr(bInterfaceNumber, "%02x\n")
832usb_intf_attr(bAlternateSetting, "%2d\n")
833usb_intf_attr(bNumEndpoints, "%02x\n")
834usb_intf_attr(bInterfaceClass, "%02x\n")
835usb_intf_attr(bInterfaceSubClass, "%02x\n")
836usb_intf_attr(bInterfaceProtocol, "%02x\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Alan Sternb724ae72005-10-24 15:36:00 -0400838static ssize_t show_interface_string(struct device *dev,
839 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct usb_interface *intf;
Alan Stern2e5f10e2008-04-30 15:37:19 -0400842 char *string;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Oliver Neukum92516442007-01-23 15:55:28 -0500844 intf = to_usb_interface(dev);
Alan Stern2e5f10e2008-04-30 15:37:19 -0400845 string = intf->cur_altsetting->string;
846 barrier(); /* The altsetting might change! */
847
848 if (!string)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return 0;
Alan Stern2e5f10e2008-04-30 15:37:19 -0400850 return sprintf(buf, "%s\n", string);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
853
Alan Sternb724ae72005-10-24 15:36:00 -0400854static ssize_t show_modalias(struct device *dev,
855 struct device_attribute *attr, char *buf)
Greg KH360b52b2005-05-10 06:45:10 -0700856{
857 struct usb_interface *intf;
858 struct usb_device *udev;
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700859 struct usb_host_interface *alt;
Greg KH360b52b2005-05-10 06:45:10 -0700860
861 intf = to_usb_interface(dev);
862 udev = interface_to_usbdev(intf);
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700863 alt = intf->cur_altsetting;
Greg KH360b52b2005-05-10 06:45:10 -0700864
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700865 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
Bjørn Mork81df2d52012-05-18 21:27:43 +0200866 "ic%02Xisc%02Xip%02Xin%02X\n",
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700867 le16_to_cpu(udev->descriptor.idVendor),
868 le16_to_cpu(udev->descriptor.idProduct),
869 le16_to_cpu(udev->descriptor.bcdDevice),
870 udev->descriptor.bDeviceClass,
871 udev->descriptor.bDeviceSubClass,
872 udev->descriptor.bDeviceProtocol,
873 alt->desc.bInterfaceClass,
874 alt->desc.bInterfaceSubClass,
Bjørn Mork81df2d52012-05-18 21:27:43 +0200875 alt->desc.bInterfaceProtocol,
876 alt->desc.bInterfaceNumber);
Greg KH360b52b2005-05-10 06:45:10 -0700877}
878static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
879
Sarah Sharp49e7cc82008-10-06 14:45:46 -0700880static ssize_t show_supports_autosuspend(struct device *dev,
881 struct device_attribute *attr, char *buf)
882{
883 struct usb_interface *intf;
884 struct usb_device *udev;
885 int ret;
886
887 intf = to_usb_interface(dev);
888 udev = interface_to_usbdev(intf);
889
890 usb_lock_device(udev);
891 /* Devices will be autosuspended even when an interface isn't claimed */
892 if (!intf->dev.driver ||
893 to_usb_driver(intf->dev.driver)->supports_autosuspend)
894 ret = sprintf(buf, "%u\n", 1);
895 else
896 ret = sprintf(buf, "%u\n", 0);
897 usb_unlock_device(udev);
898
899 return ret;
900}
901static DEVICE_ATTR(supports_autosuspend, S_IRUGO, show_supports_autosuspend, NULL);
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903static struct attribute *intf_attrs[] = {
904 &dev_attr_bInterfaceNumber.attr,
905 &dev_attr_bAlternateSetting.attr,
906 &dev_attr_bNumEndpoints.attr,
907 &dev_attr_bInterfaceClass.attr,
908 &dev_attr_bInterfaceSubClass.attr,
909 &dev_attr_bInterfaceProtocol.attr,
Greg KH360b52b2005-05-10 06:45:10 -0700910 &dev_attr_modalias.attr,
Sarah Sharp49e7cc82008-10-06 14:45:46 -0700911 &dev_attr_supports_autosuspend.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 NULL,
913};
914static struct attribute_group intf_attr_grp = {
915 .attrs = intf_attrs,
916};
917
Alan Stern2e5f10e2008-04-30 15:37:19 -0400918static struct attribute *intf_assoc_attrs[] = {
919 &dev_attr_iad_bFirstInterface.attr,
920 &dev_attr_iad_bInterfaceCount.attr,
921 &dev_attr_iad_bFunctionClass.attr,
922 &dev_attr_iad_bFunctionSubClass.attr,
923 &dev_attr_iad_bFunctionProtocol.attr,
924 NULL,
925};
926
Al Viro587a1f12011-07-23 23:11:19 -0400927static umode_t intf_assoc_attrs_are_visible(struct kobject *kobj,
Alan Stern2e5f10e2008-04-30 15:37:19 -0400928 struct attribute *a, int n)
929{
H Hartley Sweetena864e3aa52009-04-15 21:34:40 -0400930 struct device *dev = container_of(kobj, struct device, kobj);
931 struct usb_interface *intf = to_usb_interface(dev);
Alan Stern2e5f10e2008-04-30 15:37:19 -0400932
933 if (intf->intf_assoc == NULL)
934 return 0;
935 return a->mode;
936}
937
938static struct attribute_group intf_assoc_attr_grp = {
939 .attrs = intf_assoc_attrs,
940 .is_visible = intf_assoc_attrs_are_visible,
941};
942
David Brownella4dbd672009-06-24 10:06:31 -0700943const struct attribute_group *usb_interface_groups[] = {
Alan Stern2e5f10e2008-04-30 15:37:19 -0400944 &intf_attr_grp,
945 &intf_assoc_attr_grp,
946 NULL
947};
948
Michal Nazarewicz643de622011-04-14 17:47:09 +0200949void usb_create_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Alan Stern4f62efe2005-10-24 16:24:14 -0400951 struct usb_device *udev = interface_to_usbdev(intf);
952 struct usb_host_interface *alt = intf->cur_altsetting;
953
Alan Stern352d0262008-10-29 15:16:58 -0400954 if (intf->sysfs_files_created || intf->unregistering)
Michal Nazarewicz643de622011-04-14 17:47:09 +0200955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Michal Nazarewicz643de622011-04-14 17:47:09 +0200957 if (!alt->string && !(udev->quirks & USB_QUIRK_CONFIG_INTF_STRINGS))
Alan Stern4f62efe2005-10-24 16:24:14 -0400958 alt->string = usb_cache_string(udev, alt->desc.iInterface);
Michal Nazarewicz643de622011-04-14 17:47:09 +0200959 if (alt->string && device_create_file(&intf->dev, &dev_attr_interface))
960 ; /* We don't actually care if the function fails. */
Alan Stern7e615592007-11-06 11:43:42 -0500961 intf->sysfs_files_created = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962}
963
Oliver Neukum92516442007-01-23 15:55:28 -0500964void usb_remove_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Alan Stern7e615592007-11-06 11:43:42 -0500966 if (!intf->sysfs_files_created)
967 return;
Alan Stern92b0da12008-10-29 15:18:50 -0400968
Alan Stern92b0da12008-10-29 15:18:50 -0400969 device_remove_file(&intf->dev, &dev_attr_interface);
Alan Stern7e615592007-11-06 11:43:42 -0500970 intf->sysfs_files_created = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}