blob: b04afd06e502131f97024360fd5e1ad3279af9f4 [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
Sarah Sharp4d59d8a2007-10-03 14:56:03 -0700172static ssize_t
173show_urbnum(struct device *dev, struct device_attribute *attr, char *buf)
174{
175 struct usb_device *udev;
176
177 udev = to_usb_device(dev);
178 return sprintf(buf, "%d\n", atomic_read(&udev->urbnum));
179}
180static DEVICE_ATTR(urbnum, S_IRUGO, show_urbnum, NULL);
181
Alan Sternb41a60e2007-05-30 15:39:33 -0400182
183#if defined(CONFIG_USB_PERSIST) || defined(CONFIG_USB_SUSPEND)
184static const char power_group[] = "power";
185#endif
186
187#ifdef CONFIG_USB_PERSIST
188
189static ssize_t
190show_persist(struct device *dev, struct device_attribute *attr, char *buf)
191{
192 struct usb_device *udev = to_usb_device(dev);
193
194 return sprintf(buf, "%d\n", udev->persist_enabled);
195}
196
197static ssize_t
198set_persist(struct device *dev, struct device_attribute *attr,
199 const char *buf, size_t count)
200{
201 struct usb_device *udev = to_usb_device(dev);
202 int value;
203
204 /* Hubs are always enabled for USB_PERSIST */
205 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
206 return -EPERM;
207
208 if (sscanf(buf, "%d", &value) != 1)
209 return -EINVAL;
210 usb_pm_lock(udev);
211 udev->persist_enabled = !!value;
212 usb_pm_unlock(udev);
213 return count;
214}
215
216static DEVICE_ATTR(persist, S_IRUGO | S_IWUSR, show_persist, set_persist);
217
218static int add_persist_attributes(struct device *dev)
219{
220 int rc = 0;
221
222 if (is_usb_device(dev)) {
223 struct usb_device *udev = to_usb_device(dev);
224
225 /* Hubs are automatically enabled for USB_PERSIST */
226 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB)
227 udev->persist_enabled = 1;
228 rc = sysfs_add_file_to_group(&dev->kobj,
229 &dev_attr_persist.attr,
230 power_group);
231 }
232 return rc;
233}
234
235static void remove_persist_attributes(struct device *dev)
236{
237 sysfs_remove_file_from_group(&dev->kobj,
238 &dev_attr_persist.attr,
239 power_group);
240}
241
242#else
243
244#define add_persist_attributes(dev) 0
245#define remove_persist_attributes(dev) do {} while (0)
246
247#endif /* CONFIG_USB_PERSIST */
248
Alan Stern19c26232007-02-20 15:03:32 -0500249#ifdef CONFIG_USB_SUSPEND
250
251static ssize_t
252show_autosuspend(struct device *dev, struct device_attribute *attr, char *buf)
253{
254 struct usb_device *udev = to_usb_device(dev);
255
Alan Sterneaafbc32007-03-13 16:39:15 -0400256 return sprintf(buf, "%d\n", udev->autosuspend_delay / HZ);
Alan Stern19c26232007-02-20 15:03:32 -0500257}
258
259static ssize_t
260set_autosuspend(struct device *dev, struct device_attribute *attr,
261 const char *buf, size_t count)
262{
263 struct usb_device *udev = to_usb_device(dev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400264 int value;
Alan Stern19c26232007-02-20 15:03:32 -0500265
Alan Sterneaafbc32007-03-13 16:39:15 -0400266 if (sscanf(buf, "%d", &value) != 1 || value >= INT_MAX/HZ ||
267 value <= - INT_MAX/HZ)
Alan Stern19c26232007-02-20 15:03:32 -0500268 return -EINVAL;
269 value *= HZ;
270
Alan Stern19c26232007-02-20 15:03:32 -0500271 udev->autosuspend_delay = value;
Alan Sterneaafbc32007-03-13 16:39:15 -0400272 if (value >= 0)
Alan Stern19c26232007-02-20 15:03:32 -0500273 usb_try_autosuspend_device(udev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400274 else {
Alan Stern2add5222007-03-20 14:59:39 -0400275 if (usb_autoresume_device(udev) == 0)
276 usb_autosuspend_device(udev);
Alan Sterneaafbc32007-03-13 16:39:15 -0400277 }
Alan Stern19c26232007-02-20 15:03:32 -0500278 return count;
279}
280
281static DEVICE_ATTR(autosuspend, S_IRUGO | S_IWUSR,
282 show_autosuspend, set_autosuspend);
283
Alan Stern2add5222007-03-20 14:59:39 -0400284static const char on_string[] = "on";
285static const char auto_string[] = "auto";
286static const char suspend_string[] = "suspend";
287
288static ssize_t
289show_level(struct device *dev, struct device_attribute *attr, char *buf)
290{
291 struct usb_device *udev = to_usb_device(dev);
292 const char *p = auto_string;
293
294 if (udev->state == USB_STATE_SUSPENDED) {
295 if (udev->autoresume_disabled)
296 p = suspend_string;
297 } else {
298 if (udev->autosuspend_disabled)
299 p = on_string;
300 }
301 return sprintf(buf, "%s\n", p);
302}
303
304static ssize_t
305set_level(struct device *dev, struct device_attribute *attr,
306 const char *buf, size_t count)
307{
308 struct usb_device *udev = to_usb_device(dev);
309 int len = count;
310 char *cp;
311 int rc = 0;
Alan Sterndd865572007-05-22 11:38:19 -0400312 int old_autosuspend_disabled, old_autoresume_disabled;
Alan Stern2add5222007-03-20 14:59:39 -0400313
314 cp = memchr(buf, '\n', count);
315 if (cp)
316 len = cp - buf;
317
318 usb_lock_device(udev);
Alan Sterndd865572007-05-22 11:38:19 -0400319 old_autosuspend_disabled = udev->autosuspend_disabled;
320 old_autoresume_disabled = udev->autoresume_disabled;
Alan Stern2add5222007-03-20 14:59:39 -0400321
322 /* Setting the flags without calling usb_pm_lock is a subject to
323 * races, but who cares...
324 */
325 if (len == sizeof on_string - 1 &&
326 strncmp(buf, on_string, len) == 0) {
327 udev->autosuspend_disabled = 1;
328 udev->autoresume_disabled = 0;
329 rc = usb_external_resume_device(udev);
330
331 } else if (len == sizeof auto_string - 1 &&
332 strncmp(buf, auto_string, len) == 0) {
333 udev->autosuspend_disabled = 0;
334 udev->autoresume_disabled = 0;
335 rc = usb_external_resume_device(udev);
336
337 } else if (len == sizeof suspend_string - 1 &&
338 strncmp(buf, suspend_string, len) == 0) {
339 udev->autosuspend_disabled = 0;
340 udev->autoresume_disabled = 1;
341 rc = usb_external_suspend_device(udev, PMSG_SUSPEND);
342
343 } else
344 rc = -EINVAL;
345
Alan Sterndd865572007-05-22 11:38:19 -0400346 if (rc) {
347 udev->autosuspend_disabled = old_autosuspend_disabled;
348 udev->autoresume_disabled = old_autoresume_disabled;
349 }
Alan Stern2add5222007-03-20 14:59:39 -0400350 usb_unlock_device(udev);
351 return (rc < 0 ? rc : count);
352}
353
354static DEVICE_ATTR(level, S_IRUGO | S_IWUSR, show_level, set_level);
355
Alan Stern19c26232007-02-20 15:03:32 -0500356static int add_power_attributes(struct device *dev)
357{
358 int rc = 0;
359
Alan Stern2add5222007-03-20 14:59:39 -0400360 if (is_usb_device(dev)) {
Alan Stern19c26232007-02-20 15:03:32 -0500361 rc = sysfs_add_file_to_group(&dev->kobj,
362 &dev_attr_autosuspend.attr,
363 power_group);
Alan Stern2add5222007-03-20 14:59:39 -0400364 if (rc == 0)
365 rc = sysfs_add_file_to_group(&dev->kobj,
366 &dev_attr_level.attr,
367 power_group);
368 }
Alan Stern19c26232007-02-20 15:03:32 -0500369 return rc;
370}
371
372static void remove_power_attributes(struct device *dev)
373{
374 sysfs_remove_file_from_group(&dev->kobj,
Alan Stern2add5222007-03-20 14:59:39 -0400375 &dev_attr_level.attr,
376 power_group);
377 sysfs_remove_file_from_group(&dev->kobj,
Alan Stern19c26232007-02-20 15:03:32 -0500378 &dev_attr_autosuspend.attr,
379 power_group);
380}
381
382#else
383
384#define add_power_attributes(dev) 0
385#define remove_power_attributes(dev) do {} while (0)
386
387#endif /* CONFIG_USB_SUSPEND */
388
Alan Sternb41a60e2007-05-30 15:39:33 -0400389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/* Descriptor fields */
391#define usb_descriptor_attr_le16(field, format_string) \
392static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500393show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400394 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{ \
396 struct usb_device *udev; \
397 \
Oliver Neukum92516442007-01-23 15:55:28 -0500398 udev = to_usb_device(dev); \
399 return sprintf(buf, format_string, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 le16_to_cpu(udev->descriptor.field)); \
401} \
402static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
403
404usb_descriptor_attr_le16(idVendor, "%04x\n")
405usb_descriptor_attr_le16(idProduct, "%04x\n")
406usb_descriptor_attr_le16(bcdDevice, "%04x\n")
407
408#define usb_descriptor_attr(field, format_string) \
409static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500410show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400411 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{ \
413 struct usb_device *udev; \
414 \
Oliver Neukum92516442007-01-23 15:55:28 -0500415 udev = to_usb_device(dev); \
416 return sprintf(buf, format_string, udev->descriptor.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417} \
418static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
419
Oliver Neukum92516442007-01-23 15:55:28 -0500420usb_descriptor_attr(bDeviceClass, "%02x\n")
421usb_descriptor_attr(bDeviceSubClass, "%02x\n")
422usb_descriptor_attr(bDeviceProtocol, "%02x\n")
423usb_descriptor_attr(bNumConfigurations, "%d\n")
424usb_descriptor_attr(bMaxPacketSize0, "%d\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700426
427
428/* show if the device is authorized (1) or not (0) */
429static ssize_t usb_dev_authorized_show(struct device *dev,
430 struct device_attribute *attr,
431 char *buf)
432{
433 struct usb_device *usb_dev = to_usb_device(dev);
434 return snprintf(buf, PAGE_SIZE, "%u\n", usb_dev->authorized);
435}
436
437
438/*
439 * Authorize a device to be used in the system
440 *
441 * Writing a 0 deauthorizes the device, writing a 1 authorizes it.
442 */
443static ssize_t usb_dev_authorized_store(struct device *dev,
444 struct device_attribute *attr,
445 const char *buf, size_t size)
446{
447 ssize_t result;
448 struct usb_device *usb_dev = to_usb_device(dev);
449 unsigned val;
450 result = sscanf(buf, "%u\n", &val);
451 if (result != 1)
452 result = -EINVAL;
453 else if (val == 0)
454 result = usb_deauthorize_device(usb_dev);
455 else
456 result = usb_authorize_device(usb_dev);
457 return result < 0? result : size;
458}
459
460static DEVICE_ATTR(authorized, 0644,
461 usb_dev_authorized_show, usb_dev_authorized_store);
462
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static struct attribute *dev_attrs[] = {
465 /* current configuration's attributes */
Alan Sternb6eb2d82006-07-06 15:37:42 -0400466 &dev_attr_configuration.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 &dev_attr_bNumInterfaces.attr,
468 &dev_attr_bConfigurationValue.attr,
469 &dev_attr_bmAttributes.attr,
470 &dev_attr_bMaxPower.attr,
Sarah Sharp4d59d8a2007-10-03 14:56:03 -0700471 &dev_attr_urbnum.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /* device attributes */
473 &dev_attr_idVendor.attr,
474 &dev_attr_idProduct.attr,
475 &dev_attr_bcdDevice.attr,
476 &dev_attr_bDeviceClass.attr,
477 &dev_attr_bDeviceSubClass.attr,
478 &dev_attr_bDeviceProtocol.attr,
479 &dev_attr_bNumConfigurations.attr,
Greg Kroah-Hartmancf5910b2005-06-29 16:53:29 -0700480 &dev_attr_bMaxPacketSize0.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 &dev_attr_speed.attr,
Alan Stern83f7d952007-04-25 15:15:43 -0400482 &dev_attr_busnum.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 &dev_attr_devnum.attr,
484 &dev_attr_version.attr,
485 &dev_attr_maxchild.attr,
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100486 &dev_attr_quirks.attr,
Inaky Perez-Gonzaleze03f2e82007-07-31 20:34:07 -0700487 &dev_attr_authorized.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 NULL,
489};
490static struct attribute_group dev_attr_grp = {
491 .attrs = dev_attrs,
492};
493
Alan Stern69d42a72007-07-12 17:06:23 -0400494/* Binary descriptors */
495
496static ssize_t
497read_descriptors(struct kobject *kobj, struct bin_attribute *attr,
498 char *buf, loff_t off, size_t count)
499{
500 struct usb_device *udev = to_usb_device(
501 container_of(kobj, struct device, kobj));
502 size_t nleft = count;
503 size_t srclen, n;
504
505 usb_lock_device(udev);
506
507 /* The binary attribute begins with the device descriptor */
508 srclen = sizeof(struct usb_device_descriptor);
509 if (off < srclen) {
510 n = min_t(size_t, nleft, srclen - off);
511 memcpy(buf, off + (char *) &udev->descriptor, n);
512 nleft -= n;
513 buf += n;
514 off = 0;
515 } else {
516 off -= srclen;
517 }
518
519 /* Then follows the raw descriptor entry for the current
520 * configuration (config plus subsidiary descriptors).
521 */
522 if (udev->actconfig) {
523 int cfgno = udev->actconfig - udev->config;
524
525 srclen = __le16_to_cpu(udev->actconfig->desc.wTotalLength);
526 if (off < srclen) {
527 n = min_t(size_t, nleft, srclen - off);
528 memcpy(buf, off + udev->rawdescriptors[cfgno], n);
529 nleft -= n;
530 }
531 }
532 usb_unlock_device(udev);
533 return count - nleft;
534}
535
536static struct bin_attribute dev_bin_attr_descriptors = {
537 .attr = {.name = "descriptors", .mode = 0444},
538 .read = read_descriptors,
539 .size = 18 + 65535, /* dev descr + max-size raw descriptor */
540};
541
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700542int usb_create_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct device *dev = &udev->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700545 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700547 retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
548 if (retval)
549 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Alan Stern69d42a72007-07-12 17:06:23 -0400551 retval = device_create_bin_file(dev, &dev_bin_attr_descriptors);
552 if (retval)
553 goto error;
554
Alan Sternb41a60e2007-05-30 15:39:33 -0400555 retval = add_persist_attributes(dev);
556 if (retval)
557 goto error;
558
Alan Stern19c26232007-02-20 15:03:32 -0500559 retval = add_power_attributes(dev);
560 if (retval)
561 goto error;
562
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700563 if (udev->manufacturer) {
Oliver Neukum92516442007-01-23 15:55:28 -0500564 retval = device_create_file(dev, &dev_attr_manufacturer);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700565 if (retval)
566 goto error;
567 }
568 if (udev->product) {
Oliver Neukum92516442007-01-23 15:55:28 -0500569 retval = device_create_file(dev, &dev_attr_product);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700570 if (retval)
571 goto error;
572 }
573 if (udev->serial) {
Oliver Neukum92516442007-01-23 15:55:28 -0500574 retval = device_create_file(dev, &dev_attr_serial);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700575 if (retval)
576 goto error;
577 }
578 retval = usb_create_ep_files(dev, &udev->ep0, udev);
579 if (retval)
580 goto error;
581 return 0;
582error:
Alan Sternaa084f32007-02-20 14:59:59 -0500583 usb_remove_sysfs_dev_files(udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700584 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Oliver Neukum92516442007-01-23 15:55:28 -0500587void usb_remove_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 struct device *dev = &udev->dev;
590
Alan Sternbe69e5b2005-10-25 15:56:06 -0400591 usb_remove_ep_files(&udev->ep0);
Alan Sternaa084f32007-02-20 14:59:59 -0500592 device_remove_file(dev, &dev_attr_manufacturer);
593 device_remove_file(dev, &dev_attr_product);
594 device_remove_file(dev, &dev_attr_serial);
Alan Stern19c26232007-02-20 15:03:32 -0500595 remove_power_attributes(dev);
Alan Sternb41a60e2007-05-30 15:39:33 -0400596 remove_persist_attributes(dev);
Alan Stern69d42a72007-07-12 17:06:23 -0400597 device_remove_bin_file(dev, &dev_bin_attr_descriptors);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
Craig W. Nadler165fe972007-06-15 23:14:35 -0400601/* Interface Accociation Descriptor fields */
602#define usb_intf_assoc_attr(field, format_string) \
603static ssize_t \
604show_iad_##field (struct device *dev, struct device_attribute *attr, \
605 char *buf) \
606{ \
607 struct usb_interface *intf = to_usb_interface (dev); \
608 \
609 return sprintf (buf, format_string, \
610 intf->intf_assoc->field); \
611} \
612static DEVICE_ATTR(iad_##field, S_IRUGO, show_iad_##field, NULL);
613
614usb_intf_assoc_attr (bFirstInterface, "%02x\n")
615usb_intf_assoc_attr (bInterfaceCount, "%02d\n")
616usb_intf_assoc_attr (bFunctionClass, "%02x\n")
617usb_intf_assoc_attr (bFunctionSubClass, "%02x\n")
618usb_intf_assoc_attr (bFunctionProtocol, "%02x\n")
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/* Interface fields */
621#define usb_intf_attr(field, format_string) \
622static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500623show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400624 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{ \
Oliver Neukum92516442007-01-23 15:55:28 -0500626 struct usb_interface *intf = to_usb_interface(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 \
Oliver Neukum92516442007-01-23 15:55:28 -0500628 return sprintf(buf, format_string, \
Alan Sternb724ae72005-10-24 15:36:00 -0400629 intf->cur_altsetting->desc.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630} \
631static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
632
Oliver Neukum92516442007-01-23 15:55:28 -0500633usb_intf_attr(bInterfaceNumber, "%02x\n")
634usb_intf_attr(bAlternateSetting, "%2d\n")
635usb_intf_attr(bNumEndpoints, "%02x\n")
636usb_intf_attr(bInterfaceClass, "%02x\n")
637usb_intf_attr(bInterfaceSubClass, "%02x\n")
638usb_intf_attr(bInterfaceProtocol, "%02x\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Alan Sternb724ae72005-10-24 15:36:00 -0400640static ssize_t show_interface_string(struct device *dev,
641 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 struct usb_interface *intf;
644 struct usb_device *udev;
645 int len;
646
Oliver Neukum92516442007-01-23 15:55:28 -0500647 intf = to_usb_interface(dev);
648 udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
650 if (len < 0)
651 return 0;
652 buf[len] = '\n';
653 buf[len+1] = 0;
654 return len+1;
655}
656static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
657
Alan Sternb724ae72005-10-24 15:36:00 -0400658static ssize_t show_modalias(struct device *dev,
659 struct device_attribute *attr, char *buf)
Greg KH360b52b2005-05-10 06:45:10 -0700660{
661 struct usb_interface *intf;
662 struct usb_device *udev;
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700663 struct usb_host_interface *alt;
Greg KH360b52b2005-05-10 06:45:10 -0700664
665 intf = to_usb_interface(dev);
666 udev = interface_to_usbdev(intf);
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700667 alt = intf->cur_altsetting;
Greg KH360b52b2005-05-10 06:45:10 -0700668
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700669 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
670 "ic%02Xisc%02Xip%02X\n",
671 le16_to_cpu(udev->descriptor.idVendor),
672 le16_to_cpu(udev->descriptor.idProduct),
673 le16_to_cpu(udev->descriptor.bcdDevice),
674 udev->descriptor.bDeviceClass,
675 udev->descriptor.bDeviceSubClass,
676 udev->descriptor.bDeviceProtocol,
677 alt->desc.bInterfaceClass,
678 alt->desc.bInterfaceSubClass,
679 alt->desc.bInterfaceProtocol);
Greg KH360b52b2005-05-10 06:45:10 -0700680}
681static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
682
Craig W. Nadler165fe972007-06-15 23:14:35 -0400683static struct attribute *intf_assoc_attrs[] = {
684 &dev_attr_iad_bFirstInterface.attr,
685 &dev_attr_iad_bInterfaceCount.attr,
686 &dev_attr_iad_bFunctionClass.attr,
687 &dev_attr_iad_bFunctionSubClass.attr,
688 &dev_attr_iad_bFunctionProtocol.attr,
689 NULL,
690};
691static struct attribute_group intf_assoc_attr_grp = {
692 .attrs = intf_assoc_attrs,
693};
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695static struct attribute *intf_attrs[] = {
696 &dev_attr_bInterfaceNumber.attr,
697 &dev_attr_bAlternateSetting.attr,
698 &dev_attr_bNumEndpoints.attr,
699 &dev_attr_bInterfaceClass.attr,
700 &dev_attr_bInterfaceSubClass.attr,
701 &dev_attr_bInterfaceProtocol.attr,
Greg KH360b52b2005-05-10 06:45:10 -0700702 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 NULL,
704};
705static struct attribute_group intf_attr_grp = {
706 .attrs = intf_attrs,
707};
708
Alan Stern4f62efe2005-10-24 16:24:14 -0400709static inline void usb_create_intf_ep_files(struct usb_interface *intf,
710 struct usb_device *udev)
Greg Kroah-Hartman094f1642005-06-20 21:15:16 -0700711{
712 struct usb_host_interface *iface_desc;
713 int i;
714
715 iface_desc = intf->cur_altsetting;
716 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Greg Kroah-Hartman36679ea2006-06-14 12:14:34 -0700717 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
Alan Stern4f62efe2005-10-24 16:24:14 -0400718 udev);
Greg Kroah-Hartman094f1642005-06-20 21:15:16 -0700719}
720
Alan Sternbe69e5b2005-10-25 15:56:06 -0400721static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
Greg Kroah-Hartman094f1642005-06-20 21:15:16 -0700722{
723 struct usb_host_interface *iface_desc;
724 int i;
725
726 iface_desc = intf->cur_altsetting;
727 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Alan Sternbe69e5b2005-10-25 15:56:06 -0400728 usb_remove_ep_files(&iface_desc->endpoint[i]);
Greg Kroah-Hartman094f1642005-06-20 21:15:16 -0700729}
730
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700731int usb_create_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Alan Sternaa084f32007-02-20 14:59:59 -0500733 struct device *dev = &intf->dev;
Alan Stern4f62efe2005-10-24 16:24:14 -0400734 struct usb_device *udev = interface_to_usbdev(intf);
735 struct usb_host_interface *alt = intf->cur_altsetting;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700736 int retval;
Alan Stern4f62efe2005-10-24 16:24:14 -0400737
Alan Sternaa084f32007-02-20 14:59:59 -0500738 retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700739 if (retval)
Alan Sternaa084f32007-02-20 14:59:59 -0500740 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Alan Stern4f62efe2005-10-24 16:24:14 -0400742 if (alt->string == NULL)
743 alt->string = usb_cache_string(udev, alt->desc.iInterface);
744 if (alt->string)
Alan Sternaa084f32007-02-20 14:59:59 -0500745 retval = device_create_file(dev, &dev_attr_interface);
Craig W. Nadler165fe972007-06-15 23:14:35 -0400746 if (intf->intf_assoc)
747 retval = sysfs_create_group(&dev->kobj, &intf_assoc_attr_grp);
Alan Stern4f62efe2005-10-24 16:24:14 -0400748 usb_create_intf_ep_files(intf, udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700749 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Oliver Neukum92516442007-01-23 15:55:28 -0500752void usb_remove_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Alan Sternaa084f32007-02-20 14:59:59 -0500754 struct device *dev = &intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Alan Sternaa084f32007-02-20 14:59:59 -0500756 usb_remove_intf_ep_files(intf);
757 device_remove_file(dev, &dev_attr_interface);
758 sysfs_remove_group(&dev->kobj, &intf_attr_grp);
Craig W. Nadler165fe972007-06-15 23:14:35 -0400759 sysfs_remove_group(&intf->dev.kobj, &intf_assoc_attr_grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}