blob: cad4fb323f6cab5d260649e2375dc026b4127d18 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/usb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include "usb.h"
16
17/* Active configuration fields */
18#define usb_actconfig_show(field, multiplier, format_string) \
Oliver Neukum92516442007-01-23 15:55:28 -050019static ssize_t show_##field(struct device *dev, \
Alan Sternb724ae72005-10-24 15:36:00 -040020 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{ \
22 struct usb_device *udev; \
23 struct usb_host_config *actconfig; \
24 \
Oliver Neukum92516442007-01-23 15:55:28 -050025 udev = to_usb_device(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 actconfig = udev->actconfig; \
27 if (actconfig) \
Oliver Neukum92516442007-01-23 15:55:28 -050028 return sprintf(buf, format_string, \
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 actconfig->desc.field * multiplier); \
30 else \
31 return 0; \
32} \
33
34#define usb_actconfig_attr(field, multiplier, format_string) \
35usb_actconfig_show(field, multiplier, format_string) \
36static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
37
Oliver Neukum92516442007-01-23 15:55:28 -050038usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
39usb_actconfig_attr(bmAttributes, 1, "%2x\n")
40usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Alan Sternb724ae72005-10-24 15:36:00 -040042static ssize_t show_configuration_string(struct device *dev,
43 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 struct usb_device *udev;
46 struct usb_host_config *actconfig;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Oliver Neukum92516442007-01-23 15:55:28 -050048 udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 actconfig = udev->actconfig;
50 if ((!actconfig) || (!actconfig->string))
51 return 0;
Alan Stern4f62efe2005-10-24 16:24:14 -040052 return sprintf(buf, "%s\n", actconfig->string);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
55
56/* configuration value is always present, and r/w */
57usb_actconfig_show(bConfigurationValue, 1, "%u\n");
58
59static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -050060set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
Alan Sternb724ae72005-10-24 15:36:00 -040061 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Oliver Neukum92516442007-01-23 15:55:28 -050063 struct usb_device *udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int config, value;
65
Alan Stern3f141e22007-02-08 16:40:43 -050066 if (sscanf(buf, "%d", &config) != 1 || config < -1 || config > 255)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 return -EINVAL;
68 usb_lock_device(udev);
Oliver Neukum92516442007-01-23 15:55:28 -050069 value = usb_set_configuration(udev, config);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 usb_unlock_device(udev);
71 return (value < 0) ? value : count;
72}
73
74static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
75 show_bConfigurationValue, set_bConfigurationValue);
76
77/* String fields */
78#define usb_string_attr(name) \
Alan Sternb724ae72005-10-24 15:36:00 -040079static ssize_t show_##name(struct device *dev, \
80 struct device_attribute *attr, char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{ \
82 struct usb_device *udev; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 \
Oliver Neukum92516442007-01-23 15:55:28 -050084 udev = to_usb_device(dev); \
Alan Stern4f62efe2005-10-24 16:24:14 -040085 return sprintf(buf, "%s\n", udev->name); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070086} \
87static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
88
89usb_string_attr(product);
90usb_string_attr(manufacturer);
91usb_string_attr(serial);
92
93static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -050094show_speed(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct usb_device *udev;
97 char *speed;
98
Oliver Neukum92516442007-01-23 15:55:28 -050099 udev = to_usb_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 switch (udev->speed) {
102 case USB_SPEED_LOW:
103 speed = "1.5";
104 break;
105 case USB_SPEED_UNKNOWN:
106 case USB_SPEED_FULL:
107 speed = "12";
108 break;
109 case USB_SPEED_HIGH:
110 speed = "480";
111 break;
112 default:
113 speed = "unknown";
114 }
Oliver Neukum92516442007-01-23 15:55:28 -0500115 return sprintf(buf, "%s\n", speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
118
119static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500120show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 struct usb_device *udev;
123
Oliver Neukum92516442007-01-23 15:55:28 -0500124 udev = to_usb_device(dev);
125 return sprintf(buf, "%d\n", udev->devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
128
129static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500130show_version(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 struct usb_device *udev;
133 u16 bcdUSB;
134
135 udev = to_usb_device(dev);
136 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
137 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
138}
139static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
140
141static ssize_t
Oliver Neukum92516442007-01-23 15:55:28 -0500142show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct usb_device *udev;
145
Oliver Neukum92516442007-01-23 15:55:28 -0500146 udev = to_usb_device(dev);
147 return sprintf(buf, "%d\n", udev->maxchild);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
150
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100151static ssize_t
152show_quirks(struct device *dev, struct device_attribute *attr, char *buf)
153{
154 struct usb_device *udev;
155
156 udev = to_usb_device(dev);
157 return sprintf(buf, "0x%x\n", udev->quirks);
158}
159static DEVICE_ATTR(quirks, S_IRUGO, show_quirks, NULL);
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161/* Descriptor fields */
162#define usb_descriptor_attr_le16(field, format_string) \
163static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500164show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400165 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{ \
167 struct usb_device *udev; \
168 \
Oliver Neukum92516442007-01-23 15:55:28 -0500169 udev = to_usb_device(dev); \
170 return sprintf(buf, format_string, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 le16_to_cpu(udev->descriptor.field)); \
172} \
173static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
174
175usb_descriptor_attr_le16(idVendor, "%04x\n")
176usb_descriptor_attr_le16(idProduct, "%04x\n")
177usb_descriptor_attr_le16(bcdDevice, "%04x\n")
178
179#define usb_descriptor_attr(field, format_string) \
180static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500181show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400182 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{ \
184 struct usb_device *udev; \
185 \
Oliver Neukum92516442007-01-23 15:55:28 -0500186 udev = to_usb_device(dev); \
187 return sprintf(buf, format_string, udev->descriptor.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188} \
189static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
190
Oliver Neukum92516442007-01-23 15:55:28 -0500191usb_descriptor_attr(bDeviceClass, "%02x\n")
192usb_descriptor_attr(bDeviceSubClass, "%02x\n")
193usb_descriptor_attr(bDeviceProtocol, "%02x\n")
194usb_descriptor_attr(bNumConfigurations, "%d\n")
195usb_descriptor_attr(bMaxPacketSize0, "%d\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197static struct attribute *dev_attrs[] = {
198 /* current configuration's attributes */
Alan Sternb6eb2d82006-07-06 15:37:42 -0400199 &dev_attr_configuration.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 &dev_attr_bNumInterfaces.attr,
201 &dev_attr_bConfigurationValue.attr,
202 &dev_attr_bmAttributes.attr,
203 &dev_attr_bMaxPower.attr,
204 /* device attributes */
205 &dev_attr_idVendor.attr,
206 &dev_attr_idProduct.attr,
207 &dev_attr_bcdDevice.attr,
208 &dev_attr_bDeviceClass.attr,
209 &dev_attr_bDeviceSubClass.attr,
210 &dev_attr_bDeviceProtocol.attr,
211 &dev_attr_bNumConfigurations.attr,
Greg Kroah-Hartmancf5910b2005-06-29 16:53:29 -0700212 &dev_attr_bMaxPacketSize0.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 &dev_attr_speed.attr,
214 &dev_attr_devnum.attr,
215 &dev_attr_version.attr,
216 &dev_attr_maxchild.attr,
Oliver Neukum7ceec1f2007-01-26 14:26:21 +0100217 &dev_attr_quirks.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 NULL,
219};
220static struct attribute_group dev_attr_grp = {
221 .attrs = dev_attrs,
222};
223
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700224int usb_create_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 struct device *dev = &udev->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700227 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700229 retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
230 if (retval)
231 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700233 if (udev->manufacturer) {
Oliver Neukum92516442007-01-23 15:55:28 -0500234 retval = device_create_file(dev, &dev_attr_manufacturer);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700235 if (retval)
236 goto error;
237 }
238 if (udev->product) {
Oliver Neukum92516442007-01-23 15:55:28 -0500239 retval = device_create_file(dev, &dev_attr_product);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700240 if (retval)
241 goto error;
242 }
243 if (udev->serial) {
Oliver Neukum92516442007-01-23 15:55:28 -0500244 retval = device_create_file(dev, &dev_attr_serial);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700245 if (retval)
246 goto error;
247 }
248 retval = usb_create_ep_files(dev, &udev->ep0, udev);
249 if (retval)
250 goto error;
251 return 0;
252error:
Alan Sternaa084f32007-02-20 14:59:59 -0500253 usb_remove_sysfs_dev_files(udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700254 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Oliver Neukum92516442007-01-23 15:55:28 -0500257void usb_remove_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct device *dev = &udev->dev;
260
Alan Sternbe69e5b2005-10-25 15:56:06 -0400261 usb_remove_ep_files(&udev->ep0);
Alan Sternaa084f32007-02-20 14:59:59 -0500262 device_remove_file(dev, &dev_attr_manufacturer);
263 device_remove_file(dev, &dev_attr_product);
264 device_remove_file(dev, &dev_attr_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/* Interface fields */
269#define usb_intf_attr(field, format_string) \
270static ssize_t \
Oliver Neukum92516442007-01-23 15:55:28 -0500271show_##field(struct device *dev, struct device_attribute *attr, \
Alan Sternb724ae72005-10-24 15:36:00 -0400272 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{ \
Oliver Neukum92516442007-01-23 15:55:28 -0500274 struct usb_interface *intf = to_usb_interface(dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 \
Oliver Neukum92516442007-01-23 15:55:28 -0500276 return sprintf(buf, format_string, \
Alan Sternb724ae72005-10-24 15:36:00 -0400277 intf->cur_altsetting->desc.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278} \
279static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
280
Oliver Neukum92516442007-01-23 15:55:28 -0500281usb_intf_attr(bInterfaceNumber, "%02x\n")
282usb_intf_attr(bAlternateSetting, "%2d\n")
283usb_intf_attr(bNumEndpoints, "%02x\n")
284usb_intf_attr(bInterfaceClass, "%02x\n")
285usb_intf_attr(bInterfaceSubClass, "%02x\n")
286usb_intf_attr(bInterfaceProtocol, "%02x\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Alan Sternb724ae72005-10-24 15:36:00 -0400288static ssize_t show_interface_string(struct device *dev,
289 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 struct usb_interface *intf;
292 struct usb_device *udev;
293 int len;
294
Oliver Neukum92516442007-01-23 15:55:28 -0500295 intf = to_usb_interface(dev);
296 udev = interface_to_usbdev(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
298 if (len < 0)
299 return 0;
300 buf[len] = '\n';
301 buf[len+1] = 0;
302 return len+1;
303}
304static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
305
Alan Sternb724ae72005-10-24 15:36:00 -0400306static ssize_t show_modalias(struct device *dev,
307 struct device_attribute *attr, char *buf)
Greg KH360b52b2005-05-10 06:45:10 -0700308{
309 struct usb_interface *intf;
310 struct usb_device *udev;
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700311 struct usb_host_interface *alt;
Greg KH360b52b2005-05-10 06:45:10 -0700312
313 intf = to_usb_interface(dev);
314 udev = interface_to_usbdev(intf);
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700315 alt = intf->cur_altsetting;
Greg KH360b52b2005-05-10 06:45:10 -0700316
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700317 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
318 "ic%02Xisc%02Xip%02X\n",
319 le16_to_cpu(udev->descriptor.idVendor),
320 le16_to_cpu(udev->descriptor.idProduct),
321 le16_to_cpu(udev->descriptor.bcdDevice),
322 udev->descriptor.bDeviceClass,
323 udev->descriptor.bDeviceSubClass,
324 udev->descriptor.bDeviceProtocol,
325 alt->desc.bInterfaceClass,
326 alt->desc.bInterfaceSubClass,
327 alt->desc.bInterfaceProtocol);
Greg KH360b52b2005-05-10 06:45:10 -0700328}
329static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static struct attribute *intf_attrs[] = {
332 &dev_attr_bInterfaceNumber.attr,
333 &dev_attr_bAlternateSetting.attr,
334 &dev_attr_bNumEndpoints.attr,
335 &dev_attr_bInterfaceClass.attr,
336 &dev_attr_bInterfaceSubClass.attr,
337 &dev_attr_bInterfaceProtocol.attr,
Greg KH360b52b2005-05-10 06:45:10 -0700338 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 NULL,
340};
341static struct attribute_group intf_attr_grp = {
342 .attrs = intf_attrs,
343};
344
Alan Stern4f62efe2005-10-24 16:24:14 -0400345static inline void usb_create_intf_ep_files(struct usb_interface *intf,
346 struct usb_device *udev)
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700347{
348 struct usb_host_interface *iface_desc;
349 int i;
350
351 iface_desc = intf->cur_altsetting;
352 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Greg Kroah-Hartman36679ea2006-06-14 12:14:34 -0700353 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
Alan Stern4f62efe2005-10-24 16:24:14 -0400354 udev);
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700355}
356
Alan Sternbe69e5b2005-10-25 15:56:06 -0400357static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700358{
359 struct usb_host_interface *iface_desc;
360 int i;
361
362 iface_desc = intf->cur_altsetting;
363 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Alan Sternbe69e5b2005-10-25 15:56:06 -0400364 usb_remove_ep_files(&iface_desc->endpoint[i]);
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700365}
366
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700367int usb_create_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
Alan Sternaa084f32007-02-20 14:59:59 -0500369 struct device *dev = &intf->dev;
Alan Stern4f62efe2005-10-24 16:24:14 -0400370 struct usb_device *udev = interface_to_usbdev(intf);
371 struct usb_host_interface *alt = intf->cur_altsetting;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700372 int retval;
Alan Stern4f62efe2005-10-24 16:24:14 -0400373
Alan Sternaa084f32007-02-20 14:59:59 -0500374 retval = sysfs_create_group(&dev->kobj, &intf_attr_grp);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700375 if (retval)
Alan Sternaa084f32007-02-20 14:59:59 -0500376 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Alan Stern4f62efe2005-10-24 16:24:14 -0400378 if (alt->string == NULL)
379 alt->string = usb_cache_string(udev, alt->desc.iInterface);
380 if (alt->string)
Alan Sternaa084f32007-02-20 14:59:59 -0500381 retval = device_create_file(dev, &dev_attr_interface);
Alan Stern4f62efe2005-10-24 16:24:14 -0400382 usb_create_intf_ep_files(intf, udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Oliver Neukum92516442007-01-23 15:55:28 -0500386void usb_remove_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Alan Sternaa084f32007-02-20 14:59:59 -0500388 struct device *dev = &intf->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Alan Sternaa084f32007-02-20 14:59:59 -0500390 usb_remove_intf_ep_files(intf);
391 device_remove_file(dev, &dev_attr_interface);
392 sysfs_remove_group(&dev->kobj, &intf_attr_grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}