blob: be37c863fdfb18e872e553af1b8c5778a67e67a6 [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 Stern19c26232007-02-20 15:03:32 -0500172#ifdef CONFIG_USB_SUSPEND
173
174static ssize_t
175show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
176{
177 struct usb_device *udev = to_usb_device(dev);
178
Alan Sterneaafbc32007-03-13 16:39:15 -0400179 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
Alan Stern19c26232007-02-20 15:03:32 -0500180}
181
182static ssize_t
183set_autosuspend(struct device *dev, struct device_attribute *attr,
184 const char *buf, size_t count)
185{
186 struct usb_device *udev = to_usb_device(dev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400187 int value;
Alan Stern19c26232007-02-20 15:03:32 -0500188
Alan Sterneaafbc32007-03-13 16:39:15 -0400189 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
190 value <= - INT_MAX/HZ)
Alan Stern19c26232007-02-20 15:03:32 -0500191 return -EINVAL;
192 value *= HZ;
193
Alan Stern19c26232007-02-20 15:03:32 -0500194 udev->autosuspend_delay = value;
Alan Sterneaafbc32007-03-13 16:39:15 -0400195 if (value >= 0)
Alan Stern19c26232007-02-20 15:03:32 -0500196 usb_try_autosuspend_device(udev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400197 else {
Alan Stern2add5222007-03-20 14:59:39 -0400198 if (usb_autoresume_device(udev) == 0)
199 usb_autosuspend_device(udev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400200 }
Alan Stern19c26232007-02-20 15:03:32 -0500201 return count;
202}
203
204static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
205 show_autosuspend, set_autosuspend);
206
Alan Stern2add5222007-03-20 14:59:39 -0400207static const char on_string[] = "on";
208static const char auto_string[] = "auto";
209static const char suspend_string[] = "suspend";
210
211static ssize_t
212show_level(struct device *dev, struct device_attribute *attr, char *buf)
213{
214 struct usb_device *udev = to_usb_device(dev);
215 const char *p = auto_string;
216
217 if (udev->state == USB_STATE_SUSPENDED) {
218 if (udev->autoresume_disabled)
219 p = suspend_string;
220 } else {
221 if (udev->autosuspend_disabled)
222 p = on_string;
223 }
224 return sprintf(buf, "%s\n", p);
225}
226
227static ssize_t
228set_level(struct device *dev, struct device_attribute *attr,
229 const char *buf, size_t count)
230{
231 struct usb_device *udev = to_usb_device(dev);
232 int len = count;
233 char *cp;
234 int rc = 0;
Alan Sterndd865572007-05-22 11:38:19 -0400235 int old_autosuspend_disabled, old_autoresume_disabled;
Alan Stern2add5222007-03-20 14:59:39 -0400236
237 cp = memchr(buf, '\n', count);
238 if (cp)
239 len = cp - buf;
240
241 usb_lock_device(udev);
Alan Sterndd865572007-05-22 11:38:19 -0400242 old_autosuspend_disabled = udev->autosuspend_disabled;
243 old_autoresume_disabled = udev->autoresume_disabled;
Alan Stern2add5222007-03-20 14:59:39 -0400244
245 /* Setting the flags without calling usb_pm_lock is a subject to
246 * races, but who cares...
247 */
248 if (len == sizeof on_string - 1 &&
249 strncmp(buf, on_string, len) == 0) {
250 udev->autosuspend_disabled = 1;
251 udev->autoresume_disabled = 0;
252 rc = usb_external_resume_device(udev);
253
254 } else if (len == sizeof auto_string - 1 &&
255 strncmp(buf, auto_string, len) == 0) {
256 udev->autosuspend_disabled = 0;
257 udev->autoresume_disabled = 0;
258 rc = usb_external_resume_device(udev);
259
260 } else if (len == sizeof suspend_string - 1 &&
261 strncmp(buf, suspend_string, len) == 0) {
262 udev->autosuspend_disabled = 0;
263 udev->autoresume_disabled = 1;
264 rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
265
266 } else
267 rc = -EINVAL;
268
Alan Sterndd865572007-05-22 11:38:19 -0400269 if (rc) {
270 udev->autosuspend_disabled = old_autosuspend_disabled;
271 udev->autoresume_disabled = old_autoresume_disabled;
272 }
Alan Stern2add5222007-03-20 14:59:39 -0400273 usb_unlock_device(udev);
274 return (rc < 0 ? rc : count);
275}
276
277static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
278
Alan Stern19c26232007-02-20 15:03:32 -0500279static char power_group[] = "power";
280
281static int add_power_attributes(struct device *dev)
282{
283 int rc = 0;
284
Alan Stern2add5222007-03-20 14:59:39 -0400285 if (is_usb_device(dev)) {
Alan Stern19c26232007-02-20 15:03:32 -0500286 rc = sysfs_add_file_to_group(&dev->kobj,
287 &dev_attr_autosuspend.attr,
288 power_group);
Alan Stern2add5222007-03-20 14:59:39 -0400289 if (rc == 0)
290 rc = sysfs_add_file_to_group(&dev->kobj,
291 &dev_attr_level.attr,
292 power_group);
293 }
Alan Stern19c26232007-02-20 15:03:32 -0500294 return rc;
295}
296
297static void remove_power_attributes(struct device *dev)
298{
299 sysfs_remove_file_from_group(&dev->kobj,
Alan Stern2add5222007-03-20 14:59:39 -0400300 &dev_attr_level.attr,
301 power_group);
302 sysfs_remove_file_from_group(&dev->kobj,
Alan Stern19c26232007-02-20 15:03:32 -0500303 &dev_attr_autosuspend.attr,
304 power_group);
305}
306
307#else
308
309#define add_power_attributes(dev) 0
310#define remove_power_attributes(dev) do {} while (0)
311
312#endif /* CONFIG_USB_SUSPEND */
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/* Descriptor fields */
315#define usb_descriptor_attr_le16(field, format_string) \
316static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500317show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400318 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{ \
320 struct usb_device *udev; \
321 \
Oliver Neukum92516442007-01-23 15:55:28 -0500322 udev = to_usb_device(dev); \
323 return sprintf(buf, format_string, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 le16_to_cpu(udev->descriptor.field)); \
325} \
326static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
327
328usb_descriptor_attr_le16(idVendor, "%04x\n")
329usb_descriptor_attr_le16(idProduct, "%04x\n")
330usb_descriptor_attr_le16(bcdDevice, "%04x\n")
331
332#define usb_descriptor_attr(field, format_string) \
333static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500334show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400335 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{ \
337 struct usb_device *udev; \
338 \
Oliver Neukum92516442007-01-23 15:55:28 -0500339 udev = to_usb_device(dev); \
340 return sprintf(buf, format_string, udev->descriptor.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341} \
342static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
343
Oliver Neukum92516442007-01-23 15:55:28 -0500344usb_descriptor_attr(bDeviceClass, "%02x\n")
345usb_descriptor_attr(bDeviceSubClass, "%02x\n")
346usb_descriptor_attr(bDeviceProtocol, "%02x\n")
347usb_descriptor_attr(bNumConfigurations, "%d\n")
348usb_descriptor_attr(bMaxPacketSize0, "%d\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350static struct attribute *dev_attrs[] = {
351 /* current configuration's attributes */
Alan Sternb6eb2d82006-07-06 15:37:42 -0400352 &dev_attr_configuration.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 &dev_attr_bNumInterfaces.attr,
354 &dev_attr_bConfigurationValue.attr,
355 &dev_attr_bmAttributes.attr,
356 &dev_attr_bMaxPower.attr,
357 /* device attributes */
358 &dev_attr_idVendor.attr,
359 &dev_attr_idProduct.attr,
360 &dev_attr_bcdDevice.attr,
361 &dev_attr_bDeviceClass.attr,
362 &dev_attr_bDeviceSubClass.attr,
363 &dev_attr_bDeviceProtocol.attr,
364 &dev_attr_bNumConfigurations.attr,
Greg Kroah-Hartmancf5910b2005-06-29 16:53:29 -0700365 &dev_attr_bMaxPacketSize0.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 &dev_attr_speed.attr,
Alan Stern83f7d952007-04-25 15:15:43 -0400367 &dev_attr_busnum.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 &dev_attr_devnum.attr,
369 &dev_attr_version.attr,
370 &dev_attr_maxchild.attr,
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100371 &dev_attr_quirks.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 NULL,
373};
374static struct attribute_group dev_attr_grp = {
375 .attrs = dev_attrs,
376};
377
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700378int usb_create_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct device *dev = &udev->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700381 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700383 retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
384 if (retval)
385 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Alan Stern19c26232007-02-20 15:03:32 -0500387 retval = add_power_attributes(dev);
388 if (retval)
389 goto error;
390
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700391 if (udev->manufacturer) {
Oliver Neukum92516442007-01-23 15:55:28 -0500392 retval = device_create_file(dev, &dev_attr_manufacturer);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700393 if (retval)
394 goto error;
395 }
396 if (udev->product) {
Oliver Neukum92516442007-01-23 15:55:28 -0500397 retval = device_create_file(dev, &dev_attr_product);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700398 if (retval)
399 goto error;
400 }
401 if (udev->serial) {
Oliver Neukum92516442007-01-23 15:55:28 -0500402 retval = device_create_file(dev, &dev_attr_serial);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700403 if (retval)
404 goto error;
405 }
406 retval = usb_create_ep_files(dev, &udev->ep0, udev);
407 if (retval)
408 goto error;
409 return 0;
410error:
Alan Sternaa084f32007-02-20 14:59:59 -0500411 usb_remove_sysfs_dev_files(udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700412 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413}
414
Oliver Neukum92516442007-01-23 15:55:28 -0500415void usb_remove_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
417 struct device *dev = &udev->dev;
418
Alan Sternbe69e5b2005-10-25 15:56:06 -0400419 usb_remove_ep_files(&udev->ep0);
Alan Sternaa084f32007-02-20 14:59:59 -0500420 device_remove_file(dev, &dev_attr_manufacturer);
421 device_remove_file(dev, &dev_attr_product);
422 device_remove_file(dev, &dev_attr_serial);
Alan Stern19c26232007-02-20 15:03:32 -0500423 remove_power_attributes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
427/* Interface fields */
428#define usb_intf_attr(field, format_string) \
429static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500430show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400431 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{ \
Oliver Neukum92516442007-01-23 15:55:28 -0500433 struct usb_interface *intf = to_usb_interface(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 \
Oliver Neukum92516442007-01-23 15:55:28 -0500435 return sprintf(buf, format_string, \
Alan Sternb724ae72005-10-24 15:36:00 -0400436 intf->cur_altsetting->desc.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437} \
438static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
439
Oliver Neukum92516442007-01-23 15:55:28 -0500440usb_intf_attr(bInterfaceNumber, "%02x\n")
441usb_intf_attr(bAlternateSetting, "%2d\n")
442usb_intf_attr(bNumEndpoints, "%02x\n")
443usb_intf_attr(bInterfaceClass, "%02x\n")
444usb_intf_attr(bInterfaceSubClass, "%02x\n")
445usb_intf_attr(bInterfaceProtocol, "%02x\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Alan Sternb724ae72005-10-24 15:36:00 -0400447static ssize_t show_interface_string(struct device *dev,
448 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
450 struct usb_interface *intf;
451 struct usb_device *udev;
452 int len;
453
Oliver Neukum92516442007-01-23 15:55:28 -0500454 intf = to_usb_interface(dev);
455 udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
457 if (len < 0)
458 return 0;
459 buf[len] = '\n';
460 buf[len+1] = 0;
461 return len+1;
462}
463static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
464
Alan Sternb724ae72005-10-24 15:36:00 -0400465static ssize_t show_modalias(struct device *dev,
466 struct device_attribute *attr, char *buf)
Greg KH360b52b2005-05-10 06:45:10 -0700467{
468 struct usb_interface *intf;
469 struct usb_device *udev;
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700470 struct usb_host_interface *alt;
Greg KH360b52b2005-05-10 06:45:10 -0700471
472 intf = to_usb_interface(dev);
473 udev = interface_to_usbdev(intf);
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700474 alt = intf->cur_altsetting;
Greg KH360b52b2005-05-10 06:45:10 -0700475
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700476 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
477 "ic%02Xisc%02Xip%02X\n",
478 le16_to_cpu(udev->descriptor.idVendor),
479 le16_to_cpu(udev->descriptor.idProduct),
480 le16_to_cpu(udev->descriptor.bcdDevice),
481 udev->descriptor.bDeviceClass,
482 udev->descriptor.bDeviceSubClass,
483 udev->descriptor.bDeviceProtocol,
484 alt->desc.bInterfaceClass,
485 alt->desc.bInterfaceSubClass,
486 alt->desc.bInterfaceProtocol);
Greg KH360b52b2005-05-10 06:45:10 -0700487}
488static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static struct attribute *intf_attrs[] = {
491 &dev_attr_bInterfaceNumber.attr,
492 &dev_attr_bAlternateSetting.attr,
493 &dev_attr_bNumEndpoints.attr,
494 &dev_attr_bInterfaceClass.attr,
495 &dev_attr_bInterfaceSubClass.attr,
496 &dev_attr_bInterfaceProtocol.attr,
Greg KH360b52b2005-05-10 06:45:10 -0700497 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 NULL,
499};
500static struct attribute_group intf_attr_grp = {
501 .attrs = intf_attrs,
502};
503
Alan Stern4f62efe2005-10-24 16:24:14 -0400504static inline void usb_create_intf_ep_files(struct usb_interface *intf,
505 struct usb_device *udev)
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700506{
507 struct usb_host_interface *iface_desc;
508 int i;
509
510 iface_desc = intf->cur_altsetting;
511 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Greg Kroah-Hartman36679ea2006-06-14 12:14:34 -0700512 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
Alan Stern4f62efe2005-10-24 16:24:14 -0400513 udev);
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700514}
515
Alan Sternbe69e5b2005-10-25 15:56:06 -0400516static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700517{
518 struct usb_host_interface *iface_desc;
519 int i;
520
521 iface_desc = intf->cur_altsetting;
522 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Alan Sternbe69e5b2005-10-25 15:56:06 -0400523 usb_remove_ep_files(&iface_desc->endpoint[i]);
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700524}
525
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700526int usb_create_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Alan Sternaa084f32007-02-20 14:59:59 -0500528 struct device *dev = &intf->dev;
Alan Stern4f62efe2005-10-24 16:24:14 -0400529 struct usb_device *udev = interface_to_usbdev(intf);
530 struct usb_host_interface *alt = intf->cur_altsetting;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700531 int retval;
Alan Stern4f62efe2005-10-24 16:24:14 -0400532
Alan Sternaa084f32007-02-20 14:59:59 -0500533 retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700534 if (retval)
Alan Sternaa084f32007-02-20 14:59:59 -0500535 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Alan Stern4f62efe2005-10-24 16:24:14 -0400537 if (alt->string == NULL)
538 alt->string = usb_cache_string(udev, alt->desc.iInterface);
539 if (alt->string)
Alan Sternaa084f32007-02-20 14:59:59 -0500540 retval = device_create_file(dev, &dev_attr_interface);
Alan Stern4f62efe2005-10-24 16:24:14 -0400541 usb_create_intf_ep_files(intf, udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Oliver Neukum92516442007-01-23 15:55:28 -0500545void usb_remove_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Alan Sternaa084f32007-02-20 14:59:59 -0500547 struct device *dev = &intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Alan Sternaa084f32007-02-20 14:59:59 -0500549 usb_remove_intf_ep_files(intf);
550 device_remove_file(dev, &dev_attr_interface);
551 sysfs_remove_group(&dev->kobj, &intf_attr_grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}