blob: 5dfe31bc32bafb464cc2b22bacb4df9eccb1cd3f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "usb.h"
17
18/* Active configuration fields */
19#define usb_actconfig_show(field, multiplier, format_string) \
Oliver Neukum92516442007-01-23 15:55:28 -050020static ssize_t show_##field(struct device *dev, \
Alan Sternb724ae72005-10-24 15:36:00 -040021 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{ \
23 struct usb_device *udev; \
24 struct usb_host_config *actconfig; \
25 \
Oliver Neukum92516442007-01-23 15:55:28 -050026 udev = to_usb_device(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 actconfig = udev->actconfig; \
28 if (actconfig) \
Oliver Neukum92516442007-01-23 15:55:28 -050029 return sprintf(buf, format_string, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 actconfig->desc.field * multiplier); \
31 else \
32 return 0; \
33} \
34
35#define usb_actconfig_attr(field, multiplier, format_string) \
36usb_actconfig_show(field, multiplier, format_string) \
37static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
38
Oliver Neukum92516442007-01-23 15:55:28 -050039usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
40usb_actconfig_attr(bmAttributes, 1, "%2x\n")
41usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Alan Sternb724ae72005-10-24 15:36:00 -040043static ssize_t show_configuration_string(struct device *dev,
44 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
46 struct usb_device *udev;
47 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Oliver Neukum92516442007-01-23 15:55:28 -050049 udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 actconfig = udev->actconfig;
51 if ((!actconfig) || (!actconfig->string))
52 return 0;
Alan Stern4f62efe2005-10-24 16:24:14 -040053 return sprintf(buf, "%s\n", actconfig->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
56
57/* configuration value is always present, and r/w */
58usb_actconfig_show(bConfigurationValue, 1, "%u\n");
59
60static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -050061set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
Alan Sternb724ae72005-10-24 15:36:00 -040062 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
Oliver Neukum92516442007-01-23 15:55:28 -050064 struct usb_device *udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int config, value;
66
Alan Stern3f141e22007-02-08 16:40:43 -050067 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 return -EINVAL;
69 usb_lock_device(udev);
Oliver Neukum92516442007-01-23 15:55:28 -050070 value = usb_set_configuration(udev, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 usb_unlock_device(udev);
72 return (value < 0) ? value : count;
73}
74
75static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
76 show_bConfigurationValue, set_bConfigurationValue);
77
78/* String fields */
79#define usb_string_attr(name) \
Alan Sternb724ae72005-10-24 15:36:00 -040080static ssize_t show_##name(struct device *dev, \
81 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{ \
83 struct usb_device *udev; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 \
Oliver Neukum92516442007-01-23 15:55:28 -050085 udev = to_usb_device(dev); \
Alan Stern4f62efe2005-10-24 16:24:14 -040086 return sprintf(buf, "%s\n", udev->name); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070087} \
88static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
89
90usb_string_attr(product);
91usb_string_attr(manufacturer);
92usb_string_attr(serial);
93
94static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -050095show_speed(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct usb_device *udev;
98 char *speed;
99
Oliver Neukum92516442007-01-23 15:55:28 -0500100 udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 switch (udev->speed) {
103 case USB_SPEED_LOW:
104 speed = "1.5";
105 break;
106 case USB_SPEED_UNKNOWN:
107 case USB_SPEED_FULL:
108 speed = "12";
109 break;
110 case USB_SPEED_HIGH:
111 speed = "480";
112 break;
113 default:
114 speed = "unknown";
115 }
Oliver Neukum92516442007-01-23 15:55:28 -0500116 return sprintf(buf, "%s\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117}
118static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
119
120static ssize_t
Alan Stern83f7d952007-04-25 15:15:43 -0400121show_busnum(struct device *dev, struct device_attribute *attr, char *buf)
122{
123 struct usb_device *udev;
124
125 udev = to_usb_device(dev);
126 return sprintf(buf, "%d\n", udev->bus->busnum);
127}
128static DEVICE_ATTR(busnum, S_IRUGO, show_busnum, NULL);
129
130static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500131show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 struct usb_device *udev;
134
Oliver Neukum92516442007-01-23 15:55:28 -0500135 udev = to_usb_device(dev);
136 return sprintf(buf, "%d\n", udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
139
140static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500141show_version(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct usb_device *udev;
144 u16 bcdUSB;
145
146 udev = to_usb_device(dev);
147 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
148 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
149}
150static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
151
152static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500153show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct usb_device *udev;
156
Oliver Neukum92516442007-01-23 15:55:28 -0500157 udev = to_usb_device(dev);
158 return sprintf(buf, "%d\n", udev->maxchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
161
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100162static ssize_t
163show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
164{
165 struct usb_device *udev;
166
167 udev = to_usb_device(dev);
168 return sprintf(buf, "0x%x\n", udev->quirks);
169}
170static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
171
Alan Sternb41a60e2007-05-30 15:39:33 -0400172
173#if defined(CONFIG_USB_PERSIST) || defined(CONFIG_USB_SUSPEND)
174static const char power_group[] = "power";
175#endif
176
177#ifdef CONFIG_USB_PERSIST
178
179static ssize_t
180show_persist(struct device *dev, struct device_attribute *attr, char *buf)
181{
182 struct usb_device *udev = to_usb_device(dev);
183
184 return sprintf(buf, "%d\n", udev->persist_enabled);
185}
186
187static ssize_t
188set_persist(struct device *dev, struct device_attribute *attr,
189 const char *buf, size_t count)
190{
191 struct usb_device *udev = to_usb_device(dev);
192 int value;
193
194 /* Hubs are always enabled for USB_PERSIST */
195 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
196 return -EPERM;
197
198 if (sscanf(buf, "%d", &value) != 1)
199 return -EINVAL;
200 usb_pm_lock(udev);
201 udev->persist_enabled = !!value;
202 usb_pm_unlock(udev);
203 return count;
204}
205
206static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
207
208static int add_persist_attributes(struct device *dev)
209{
210 int rc = 0;
211
212 if (is_usb_device(dev)) {
213 struct usb_device *udev = to_usb_device(dev);
214
215 /* Hubs are automatically enabled for USB_PERSIST */
216 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
217 udev->persist_enabled = 1;
218 rc = sysfs_add_file_to_group(&dev->kobj,
219 &dev_attr_persist.attr,
220 power_group);
221 }
222 return rc;
223}
224
225static void remove_persist_attributes(struct device *dev)
226{
227 sysfs_remove_file_from_group(&dev->kobj,
228 &dev_attr_persist.attr,
229 power_group);
230}
231
232#else
233
234#define add_persist_attributes(dev) 0
235#define remove_persist_attributes(dev) do {} while (0)
236
237#endif /* CONFIG_USB_PERSIST */
238
Alan Stern19c26232007-02-20 15:03:32 -0500239#ifdef CONFIG_USB_SUSPEND
240
241static ssize_t
242show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
243{
244 struct usb_device *udev = to_usb_device(dev);
245
Alan Sterneaafbc32007-03-13 16:39:15 -0400246 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
Alan Stern19c26232007-02-20 15:03:32 -0500247}
248
249static ssize_t
250set_autosuspend(struct device *dev, struct device_attribute *attr,
251 const char *buf, size_t count)
252{
253 struct usb_device *udev = to_usb_device(dev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400254 int value;
Alan Stern19c26232007-02-20 15:03:32 -0500255
Alan Sterneaafbc32007-03-13 16:39:15 -0400256 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
257 value <= - INT_MAX/HZ)
Alan Stern19c26232007-02-20 15:03:32 -0500258 return -EINVAL;
259 value *= HZ;
260
Alan Stern19c26232007-02-20 15:03:32 -0500261 udev->autosuspend_delay = value;
Alan Sterneaafbc32007-03-13 16:39:15 -0400262 if (value >= 0)
Alan Stern19c26232007-02-20 15:03:32 -0500263 usb_try_autosuspend_device(udev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400264 else {
Alan Stern2add5222007-03-20 14:59:39 -0400265 if (usb_autoresume_device(udev) == 0)
266 usb_autosuspend_device(udev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400267 }
Alan Stern19c26232007-02-20 15:03:32 -0500268 return count;
269}
270
271static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
272 show_autosuspend, set_autosuspend);
273
Alan Stern2add5222007-03-20 14:59:39 -0400274static const char on_string[] = "on";
275static const char auto_string[] = "auto";
276static const char suspend_string[] = "suspend";
277
278static ssize_t
279show_level(struct device *dev, struct device_attribute *attr, char *buf)
280{
281 struct usb_device *udev = to_usb_device(dev);
282 const char *p = auto_string;
283
284 if (udev->state == USB_STATE_SUSPENDED) {
285 if (udev->autoresume_disabled)
286 p = suspend_string;
287 } else {
288 if (udev->autosuspend_disabled)
289 p = on_string;
290 }
291 return sprintf(buf, "%s\n", p);
292}
293
294static ssize_t
295set_level(struct device *dev, struct device_attribute *attr,
296 const char *buf, size_t count)
297{
298 struct usb_device *udev = to_usb_device(dev);
299 int len = count;
300 char *cp;
301 int rc = 0;
Alan Sterndd865572007-05-22 11:38:19 -0400302 int old_autosuspend_disabled, old_autoresume_disabled;
Alan Stern2add5222007-03-20 14:59:39 -0400303
304 cp = memchr(buf, '\n', count);
305 if (cp)
306 len = cp - buf;
307
308 usb_lock_device(udev);
Alan Sterndd865572007-05-22 11:38:19 -0400309 old_autosuspend_disabled = udev->autosuspend_disabled;
310 old_autoresume_disabled = udev->autoresume_disabled;
Alan Stern2add5222007-03-20 14:59:39 -0400311
312 /* Setting the flags without calling usb_pm_lock is a subject to
313 * races, but who cares...
314 */
315 if (len == sizeof on_string - 1 &&
316 strncmp(buf, on_string, len) == 0) {
317 udev->autosuspend_disabled = 1;
318 udev->autoresume_disabled = 0;
319 rc = usb_external_resume_device(udev);
320
321 } else if (len == sizeof auto_string - 1 &&
322 strncmp(buf, auto_string, len) == 0) {
323 udev->autosuspend_disabled = 0;
324 udev->autoresume_disabled = 0;
325 rc = usb_external_resume_device(udev);
326
327 } else if (len == sizeof suspend_string - 1 &&
328 strncmp(buf, suspend_string, len) == 0) {
329 udev->autosuspend_disabled = 0;
330 udev->autoresume_disabled = 1;
331 rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
332
333 } else
334 rc = -EINVAL;
335
Alan Sterndd865572007-05-22 11:38:19 -0400336 if (rc) {
337 udev->autosuspend_disabled = old_autosuspend_disabled;
338 udev->autoresume_disabled = old_autoresume_disabled;
339 }
Alan Stern2add5222007-03-20 14:59:39 -0400340 usb_unlock_device(udev);
341 return (rc < 0 ? rc : count);
342}
343
344static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
345
Alan Stern19c26232007-02-20 15:03:32 -0500346static int add_power_attributes(struct device *dev)
347{
348 int rc = 0;
349
Alan Stern2add5222007-03-20 14:59:39 -0400350 if (is_usb_device(dev)) {
Alan Stern19c26232007-02-20 15:03:32 -0500351 rc = sysfs_add_file_to_group(&dev->kobj,
352 &dev_attr_autosuspend.attr,
353 power_group);
Alan Stern2add5222007-03-20 14:59:39 -0400354 if (rc == 0)
355 rc = sysfs_add_file_to_group(&dev->kobj,
356 &dev_attr_level.attr,
357 power_group);
358 }
Alan Stern19c26232007-02-20 15:03:32 -0500359 return rc;
360}
361
362static void remove_power_attributes(struct device *dev)
363{
364 sysfs_remove_file_from_group(&dev->kobj,
Alan Stern2add5222007-03-20 14:59:39 -0400365 &dev_attr_level.attr,
366 power_group);
367 sysfs_remove_file_from_group(&dev->kobj,
Alan Stern19c26232007-02-20 15:03:32 -0500368 &dev_attr_autosuspend.attr,
369 power_group);
370}
371
372#else
373
374#define add_power_attributes(dev) 0
375#define remove_power_attributes(dev) do {} while (0)
376
377#endif /* CONFIG_USB_SUSPEND */
378
Alan Sternb41a60e2007-05-30 15:39:33 -0400379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/* Descriptor fields */
381#define usb_descriptor_attr_le16(field, format_string) \
382static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500383show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400384 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{ \
386 struct usb_device *udev; \
387 \
Oliver Neukum92516442007-01-23 15:55:28 -0500388 udev = to_usb_device(dev); \
389 return sprintf(buf, format_string, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 le16_to_cpu(udev->descriptor.field)); \
391} \
392static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
393
394usb_descriptor_attr_le16(idVendor, "%04x\n")
395usb_descriptor_attr_le16(idProduct, "%04x\n")
396usb_descriptor_attr_le16(bcdDevice, "%04x\n")
397
398#define usb_descriptor_attr(field, format_string) \
399static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500400show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400401 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{ \
403 struct usb_device *udev; \
404 \
Oliver Neukum92516442007-01-23 15:55:28 -0500405 udev = to_usb_device(dev); \
406 return sprintf(buf, format_string, udev->descriptor.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407} \
408static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
409
Oliver Neukum92516442007-01-23 15:55:28 -0500410usb_descriptor_attr(bDeviceClass, "%02x\n")
411usb_descriptor_attr(bDeviceSubClass, "%02x\n")
412usb_descriptor_attr(bDeviceProtocol, "%02x\n")
413usb_descriptor_attr(bNumConfigurations, "%d\n")
414usb_descriptor_attr(bMaxPacketSize0, "%d\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416static struct attribute *dev_attrs[] = {
417 /* current configuration's attributes */
Alan Sternb6eb2d82006-07-06 15:37:42 -0400418 &dev_attr_configuration.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 &dev_attr_bNumInterfaces.attr,
420 &dev_attr_bConfigurationValue.attr,
421 &dev_attr_bmAttributes.attr,
422 &dev_attr_bMaxPower.attr,
423 /* device attributes */
424 &dev_attr_idVendor.attr,
425 &dev_attr_idProduct.attr,
426 &dev_attr_bcdDevice.attr,
427 &dev_attr_bDeviceClass.attr,
428 &dev_attr_bDeviceSubClass.attr,
429 &dev_attr_bDeviceProtocol.attr,
430 &dev_attr_bNumConfigurations.attr,
Greg Kroah-Hartmancf5910b2005-06-29 16:53:29 -0700431 &dev_attr_bMaxPacketSize0.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 &dev_attr_speed.attr,
Alan Stern83f7d952007-04-25 15:15:43 -0400433 &dev_attr_busnum.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 &dev_attr_devnum.attr,
435 &dev_attr_version.attr,
436 &dev_attr_maxchild.attr,
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100437 &dev_attr_quirks.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 NULL,
439};
440static struct attribute_group dev_attr_grp = {
441 .attrs = dev_attrs,
442};
443
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700444int usb_create_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 struct device *dev = &udev->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700447 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700449 retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
450 if (retval)
451 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Alan Sternb41a60e2007-05-30 15:39:33 -0400453 retval = add_persist_attributes(dev);
454 if (retval)
455 goto error;
456
Alan Stern19c26232007-02-20 15:03:32 -0500457 retval = add_power_attributes(dev);
458 if (retval)
459 goto error;
460
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700461 if (udev->manufacturer) {
Oliver Neukum92516442007-01-23 15:55:28 -0500462 retval = device_create_file(dev, &dev_attr_manufacturer);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700463 if (retval)
464 goto error;
465 }
466 if (udev->product) {
Oliver Neukum92516442007-01-23 15:55:28 -0500467 retval = device_create_file(dev, &dev_attr_product);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700468 if (retval)
469 goto error;
470 }
471 if (udev->serial) {
Oliver Neukum92516442007-01-23 15:55:28 -0500472 retval = device_create_file(dev, &dev_attr_serial);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700473 if (retval)
474 goto error;
475 }
476 retval = usb_create_ep_files(dev, &udev->ep0, udev);
477 if (retval)
478 goto error;
479 return 0;
480error:
Alan Sternaa084f32007-02-20 14:59:59 -0500481 usb_remove_sysfs_dev_files(udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700482 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
Oliver Neukum92516442007-01-23 15:55:28 -0500485void usb_remove_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
487 struct device *dev = &udev->dev;
488
Alan Sternbe69e5b2005-10-25 15:56:06 -0400489 usb_remove_ep_files(&udev->ep0);
Alan Sternaa084f32007-02-20 14:59:59 -0500490 device_remove_file(dev, &dev_attr_manufacturer);
491 device_remove_file(dev, &dev_attr_product);
492 device_remove_file(dev, &dev_attr_serial);
Alan Stern19c26232007-02-20 15:03:32 -0500493 remove_power_attributes(dev);
Alan Sternb41a60e2007-05-30 15:39:33 -0400494 remove_persist_attributes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/* Interface fields */
499#define usb_intf_attr(field, format_string) \
500static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500501show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400502 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{ \
Oliver Neukum92516442007-01-23 15:55:28 -0500504 struct usb_interface *intf = to_usb_interface(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 \
Oliver Neukum92516442007-01-23 15:55:28 -0500506 return sprintf(buf, format_string, \
Alan Sternb724ae72005-10-24 15:36:00 -0400507 intf->cur_altsetting->desc.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508} \
509static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
510
Oliver Neukum92516442007-01-23 15:55:28 -0500511usb_intf_attr(bInterfaceNumber, "%02x\n")
512usb_intf_attr(bAlternateSetting, "%2d\n")
513usb_intf_attr(bNumEndpoints, "%02x\n")
514usb_intf_attr(bInterfaceClass, "%02x\n")
515usb_intf_attr(bInterfaceSubClass, "%02x\n")
516usb_intf_attr(bInterfaceProtocol, "%02x\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Alan Sternb724ae72005-10-24 15:36:00 -0400518static ssize_t show_interface_string(struct device *dev,
519 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct usb_interface *intf;
522 struct usb_device *udev;
523 int len;
524
Oliver Neukum92516442007-01-23 15:55:28 -0500525 intf = to_usb_interface(dev);
526 udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
528 if (len < 0)
529 return 0;
530 buf[len] = '\n';
531 buf[len+1] = 0;
532 return len+1;
533}
534static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
535
Alan Sternb724ae72005-10-24 15:36:00 -0400536static ssize_t show_modalias(struct device *dev,
537 struct device_attribute *attr, char *buf)
Greg KH360b52b2005-05-10 06:45:10 -0700538{
539 struct usb_interface *intf;
540 struct usb_device *udev;
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700541 struct usb_host_interface *alt;
Greg KH360b52b2005-05-10 06:45:10 -0700542
543 intf = to_usb_interface(dev);
544 udev = interface_to_usbdev(intf);
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700545 alt = intf->cur_altsetting;
Greg KH360b52b2005-05-10 06:45:10 -0700546
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700547 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
548 "ic%02Xisc%02Xip%02X\n",
549 le16_to_cpu(udev->descriptor.idVendor),
550 le16_to_cpu(udev->descriptor.idProduct),
551 le16_to_cpu(udev->descriptor.bcdDevice),
552 udev->descriptor.bDeviceClass,
553 udev->descriptor.bDeviceSubClass,
554 udev->descriptor.bDeviceProtocol,
555 alt->desc.bInterfaceClass,
556 alt->desc.bInterfaceSubClass,
557 alt->desc.bInterfaceProtocol);
Greg KH360b52b2005-05-10 06:45:10 -0700558}
559static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561static struct attribute *intf_attrs[] = {
562 &dev_attr_bInterfaceNumber.attr,
563 &dev_attr_bAlternateSetting.attr,
564 &dev_attr_bNumEndpoints.attr,
565 &dev_attr_bInterfaceClass.attr,
566 &dev_attr_bInterfaceSubClass.attr,
567 &dev_attr_bInterfaceProtocol.attr,
Greg KH360b52b2005-05-10 06:45:10 -0700568 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 NULL,
570};
571static struct attribute_group intf_attr_grp = {
572 .attrs = intf_attrs,
573};
574
Alan Stern4f62efe2005-10-24 16:24:14 -0400575static inline void usb_create_intf_ep_files(struct usb_interface *intf,
576 struct usb_device *udev)
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700577{
578 struct usb_host_interface *iface_desc;
579 int i;
580
581 iface_desc = intf->cur_altsetting;
582 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Greg Kroah-Hartman36679ea2006-06-14 12:14:34 -0700583 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
Alan Stern4f62efe2005-10-24 16:24:14 -0400584 udev);
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700585}
586
Alan Sternbe69e5b2005-10-25 15:56:06 -0400587static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700588{
589 struct usb_host_interface *iface_desc;
590 int i;
591
592 iface_desc = intf->cur_altsetting;
593 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Alan Sternbe69e5b2005-10-25 15:56:06 -0400594 usb_remove_ep_files(&iface_desc->endpoint[i]);
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700595}
596
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700597int usb_create_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Alan Sternaa084f32007-02-20 14:59:59 -0500599 struct device *dev = &intf->dev;
Alan Stern4f62efe2005-10-24 16:24:14 -0400600 struct usb_device *udev = interface_to_usbdev(intf);
601 struct usb_host_interface *alt = intf->cur_altsetting;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700602 int retval;
Alan Stern4f62efe2005-10-24 16:24:14 -0400603
Alan Sternaa084f32007-02-20 14:59:59 -0500604 retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700605 if (retval)
Alan Sternaa084f32007-02-20 14:59:59 -0500606 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Alan Stern4f62efe2005-10-24 16:24:14 -0400608 if (alt->string == NULL)
609 alt->string = usb_cache_string(udev, alt->desc.iInterface);
610 if (alt->string)
Alan Sternaa084f32007-02-20 14:59:59 -0500611 retval = device_create_file(dev, &dev_attr_interface);
Alan Stern4f62efe2005-10-24 16:24:14 -0400612 usb_create_intf_ep_files(intf, udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Oliver Neukum92516442007-01-23 15:55:28 -0500616void usb_remove_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Alan Sternaa084f32007-02-20 14:59:59 -0500618 struct device *dev = &intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Alan Sternaa084f32007-02-20 14:59:59 -0500620 usb_remove_intf_ep_files(intf);
621 device_remove_file(dev, &dev_attr_interface);
622 sysfs_remove_group(&dev->kobj, &intf_attr_grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623}