blob: 55d8f575206d6b1dcbfb0338e48a79b735a7ef9d [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) \
Alan Sternb724ae72005-10-24 15:36:00 -040019static ssize_t show_##field (struct device *dev, \
20 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 \
25 udev = to_usb_device (dev); \
26 actconfig = udev->actconfig; \
27 if (actconfig) \
28 return sprintf (buf, format_string, \
29 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
38usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
39usb_actconfig_attr (bmAttributes, 1, "%2x\n")
40usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
41
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
48 udev = to_usb_device (dev);
49 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
Alan Sternb724ae72005-10-24 15:36:00 -040060set_bConfigurationValue (struct device *dev, struct device_attribute *attr,
61 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Alan Sternb6eb2d82006-07-06 15:37:42 -040063 struct usb_device *udev = to_usb_device (dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 int config, value;
65
66 if (sscanf (buf, "%u", &config) != 1 || config > 255)
67 return -EINVAL;
68 usb_lock_device(udev);
69 value = usb_set_configuration (udev, config);
70 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 \
84 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
Yani Ioannou10523b32005-05-17 06:43:37 -040094show_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
99 udev = to_usb_device (dev);
100
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 }
115 return sprintf (buf, "%s\n", speed);
116}
117static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
118
119static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400120show_devnum (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 struct usb_device *udev;
123
124 udev = to_usb_device (dev);
125 return sprintf (buf, "%d\n", udev->devnum);
126}
127static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
128
129static ssize_t
Yani Ioannou10523b32005-05-17 06:43:37 -0400130show_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
Yani Ioannou10523b32005-05-17 06:43:37 -0400142show_maxchild (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
144 struct usb_device *udev;
145
146 udev = to_usb_device (dev);
147 return sprintf (buf, "%d\n", udev->maxchild);
148}
149static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
150
151/* Descriptor fields */
152#define usb_descriptor_attr_le16(field, format_string) \
153static ssize_t \
Alan Sternb724ae72005-10-24 15:36:00 -0400154show_##field (struct device *dev, struct device_attribute *attr, \
155 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{ \
157 struct usb_device *udev; \
158 \
159 udev = to_usb_device (dev); \
160 return sprintf (buf, format_string, \
161 le16_to_cpu(udev->descriptor.field)); \
162} \
163static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
164
165usb_descriptor_attr_le16(idVendor, "%04x\n")
166usb_descriptor_attr_le16(idProduct, "%04x\n")
167usb_descriptor_attr_le16(bcdDevice, "%04x\n")
168
169#define usb_descriptor_attr(field, format_string) \
170static ssize_t \
Alan Sternb724ae72005-10-24 15:36:00 -0400171show_##field (struct device *dev, struct device_attribute *attr, \
172 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{ \
174 struct usb_device *udev; \
175 \
176 udev = to_usb_device (dev); \
177 return sprintf (buf, format_string, udev->descriptor.field); \
178} \
179static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
180
181usb_descriptor_attr (bDeviceClass, "%02x\n")
182usb_descriptor_attr (bDeviceSubClass, "%02x\n")
183usb_descriptor_attr (bDeviceProtocol, "%02x\n")
184usb_descriptor_attr (bNumConfigurations, "%d\n")
Greg Kroah-Hartmancf5910b2005-06-29 16:53:29 -0700185usb_descriptor_attr (bMaxPacketSize0, "%d\n")
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187static struct attribute *dev_attrs[] = {
188 /* current configuration's attributes */
Alan Sternb6eb2d82006-07-06 15:37:42 -0400189 &dev_attr_configuration.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 &dev_attr_bNumInterfaces.attr,
191 &dev_attr_bConfigurationValue.attr,
192 &dev_attr_bmAttributes.attr,
193 &dev_attr_bMaxPower.attr,
194 /* device attributes */
195 &dev_attr_idVendor.attr,
196 &dev_attr_idProduct.attr,
197 &dev_attr_bcdDevice.attr,
198 &dev_attr_bDeviceClass.attr,
199 &dev_attr_bDeviceSubClass.attr,
200 &dev_attr_bDeviceProtocol.attr,
201 &dev_attr_bNumConfigurations.attr,
Greg Kroah-Hartmancf5910b2005-06-29 16:53:29 -0700202 &dev_attr_bMaxPacketSize0.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 &dev_attr_speed.attr,
204 &dev_attr_devnum.attr,
205 &dev_attr_version.attr,
206 &dev_attr_maxchild.attr,
207 NULL,
208};
209static struct attribute_group dev_attr_grp = {
210 .attrs = dev_attrs,
211};
212
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700213int usb_create_sysfs_dev_files(struct usb_device *udev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct device *dev = &udev->dev;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700216 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700218 retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
219 if (retval)
220 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700222 if (udev->manufacturer) {
223 retval = device_create_file (dev, &dev_attr_manufacturer);
224 if (retval)
225 goto error;
226 }
227 if (udev->product) {
228 retval = device_create_file (dev, &dev_attr_product);
229 if (retval)
230 goto error;
231 }
232 if (udev->serial) {
233 retval = device_create_file (dev, &dev_attr_serial);
234 if (retval)
235 goto error;
236 }
237 retval = usb_create_ep_files(dev, &udev->ep0, udev);
238 if (retval)
239 goto error;
240 return 0;
241error:
242 usb_remove_ep_files(&udev->ep0);
243 device_remove_file(dev, &dev_attr_manufacturer);
244 device_remove_file(dev, &dev_attr_product);
245 device_remove_file(dev, &dev_attr_serial);
246 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249void usb_remove_sysfs_dev_files (struct usb_device *udev)
250{
251 struct device *dev = &udev->dev;
252
Alan Sternbe69e5b2005-10-25 15:56:06 -0400253 usb_remove_ep_files(&udev->ep0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
255
Alan Stern4f62efe2005-10-24 16:24:14 -0400256 if (udev->manufacturer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 device_remove_file(dev, &dev_attr_manufacturer);
Alan Stern4f62efe2005-10-24 16:24:14 -0400258 if (udev->product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 device_remove_file(dev, &dev_attr_product);
Alan Stern4f62efe2005-10-24 16:24:14 -0400260 if (udev->serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 device_remove_file(dev, &dev_attr_serial);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
264/* Interface fields */
265#define usb_intf_attr(field, format_string) \
266static ssize_t \
Alan Sternb724ae72005-10-24 15:36:00 -0400267show_##field (struct device *dev, struct device_attribute *attr, \
268 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{ \
270 struct usb_interface *intf = to_usb_interface (dev); \
271 \
Alan Sternb724ae72005-10-24 15:36:00 -0400272 return sprintf (buf, format_string, \
273 intf->cur_altsetting->desc.field); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274} \
275static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
276
277usb_intf_attr (bInterfaceNumber, "%02x\n")
278usb_intf_attr (bAlternateSetting, "%2d\n")
279usb_intf_attr (bNumEndpoints, "%02x\n")
280usb_intf_attr (bInterfaceClass, "%02x\n")
281usb_intf_attr (bInterfaceSubClass, "%02x\n")
282usb_intf_attr (bInterfaceProtocol, "%02x\n")
283
Alan Sternb724ae72005-10-24 15:36:00 -0400284static ssize_t show_interface_string(struct device *dev,
285 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct usb_interface *intf;
288 struct usb_device *udev;
289 int len;
290
291 intf = to_usb_interface (dev);
292 udev = interface_to_usbdev (intf);
293 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
294 if (len < 0)
295 return 0;
296 buf[len] = '\n';
297 buf[len+1] = 0;
298 return len+1;
299}
300static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
301
Alan Sternb724ae72005-10-24 15:36:00 -0400302static ssize_t show_modalias(struct device *dev,
303 struct device_attribute *attr, char *buf)
Greg KH360b52b2005-05-10 06:45:10 -0700304{
305 struct usb_interface *intf;
306 struct usb_device *udev;
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700307 struct usb_host_interface *alt;
Greg KH360b52b2005-05-10 06:45:10 -0700308
309 intf = to_usb_interface(dev);
310 udev = interface_to_usbdev(intf);
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700311 alt = intf->cur_altsetting;
Greg KH360b52b2005-05-10 06:45:10 -0700312
Greg Kroah-Hartman75218032005-06-20 21:15:16 -0700313 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
314 "ic%02Xisc%02Xip%02X\n",
315 le16_to_cpu(udev->descriptor.idVendor),
316 le16_to_cpu(udev->descriptor.idProduct),
317 le16_to_cpu(udev->descriptor.bcdDevice),
318 udev->descriptor.bDeviceClass,
319 udev->descriptor.bDeviceSubClass,
320 udev->descriptor.bDeviceProtocol,
321 alt->desc.bInterfaceClass,
322 alt->desc.bInterfaceSubClass,
323 alt->desc.bInterfaceProtocol);
Greg KH360b52b2005-05-10 06:45:10 -0700324}
325static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327static struct attribute *intf_attrs[] = {
328 &dev_attr_bInterfaceNumber.attr,
329 &dev_attr_bAlternateSetting.attr,
330 &dev_attr_bNumEndpoints.attr,
331 &dev_attr_bInterfaceClass.attr,
332 &dev_attr_bInterfaceSubClass.attr,
333 &dev_attr_bInterfaceProtocol.attr,
Greg KH360b52b2005-05-10 06:45:10 -0700334 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 NULL,
336};
337static struct attribute_group intf_attr_grp = {
338 .attrs = intf_attrs,
339};
340
Alan Stern4f62efe2005-10-24 16:24:14 -0400341static inline void usb_create_intf_ep_files(struct usb_interface *intf,
342 struct usb_device *udev)
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700343{
344 struct usb_host_interface *iface_desc;
345 int i;
346
347 iface_desc = intf->cur_altsetting;
348 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Greg Kroah-Hartman36679ea2006-06-14 12:14:34 -0700349 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
Alan Stern4f62efe2005-10-24 16:24:14 -0400350 udev);
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700351}
352
Alan Sternbe69e5b2005-10-25 15:56:06 -0400353static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700354{
355 struct usb_host_interface *iface_desc;
356 int i;
357
358 iface_desc = intf->cur_altsetting;
359 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
Alan Sternbe69e5b2005-10-25 15:56:06 -0400360 usb_remove_ep_files(&iface_desc->endpoint[i]);
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700361}
362
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700363int usb_create_sysfs_intf_files(struct usb_interface *intf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Alan Stern4f62efe2005-10-24 16:24:14 -0400365 struct usb_device *udev = interface_to_usbdev(intf);
366 struct usb_host_interface *alt = intf->cur_altsetting;
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700367 int retval;
Alan Stern4f62efe2005-10-24 16:24:14 -0400368
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700369 retval = sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
370 if (retval)
371 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Alan Stern4f62efe2005-10-24 16:24:14 -0400373 if (alt->string == NULL)
374 alt->string = usb_cache_string(udev, alt->desc.iInterface);
375 if (alt->string)
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700376 retval = device_create_file(&intf->dev, &dev_attr_interface);
Alan Stern4f62efe2005-10-24 16:24:14 -0400377 usb_create_intf_ep_files(intf, udev);
Greg Kroah-Hartman1b21d5e2006-08-28 11:43:25 -0700378 return 0;
379error:
380 if (alt->string)
381 device_remove_file(&intf->dev, &dev_attr_interface);
382 sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
383 usb_remove_intf_ep_files(intf);
384 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
387void usb_remove_sysfs_intf_files (struct usb_interface *intf)
388{
Greg Kroah-Hartman094f16492005-06-20 21:15:16 -0700389 usb_remove_intf_ep_files(intf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
391
392 if (intf->cur_altsetting->string)
393 device_remove_file(&intf->dev, &dev_attr_interface);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}