2 * drivers/usb/core/sysfs.c
4 * (C) Copyright 2002 David Brownell
5 * (C) Copyright 2002,2004 Greg Kroah-Hartman
6 * (C) Copyright 2002,2004 IBM Corp.
8 * All of the sysfs file attributes for usb devices and interfaces.
13 #include <linux/kernel.h>
14 #include <linux/usb.h>
17 /* Active configuration fields */
18 #define usb_actconfig_show(field, multiplier, format_string) \
19 static ssize_t show_##field(struct device *dev, \
20 struct device_attribute *attr, char *buf) \
22 struct usb_device *udev; \
23 struct usb_host_config *actconfig; \
25 udev = to_usb_device(dev); \
26 actconfig = udev->actconfig; \
28 return sprintf(buf, format_string, \
29 actconfig->desc.field * multiplier); \
34 #define usb_actconfig_attr(field, multiplier, format_string) \
35 usb_actconfig_show(field, multiplier, format_string) \
36 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
38 usb_actconfig_attr(bNumInterfaces, 1, "%2d\n")
39 usb_actconfig_attr(bmAttributes, 1, "%2x\n")
40 usb_actconfig_attr(bMaxPower, 2, "%3dmA\n")
42 static ssize_t show_configuration_string(struct device *dev,
43 struct device_attribute *attr, char *buf)
45 struct usb_device *udev;
46 struct usb_host_config *actconfig;
48 udev = to_usb_device(dev);
49 actconfig = udev->actconfig;
50 if ((!actconfig) || (!actconfig->string))
52 return sprintf(buf, "%s\n", actconfig->string);
54 static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
56 /* configuration value is always present, and r/w */
57 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
60 set_bConfigurationValue(struct device *dev, struct device_attribute *attr,
61 const char *buf, size_t count)
63 struct usb_device *udev = to_usb_device(dev);
66 if (sscanf(buf, "%u", &config) != 1 || config > 255)
68 usb_lock_device(udev);
69 value = usb_set_configuration(udev, config);
70 usb_unlock_device(udev);
71 return (value < 0) ? value : count;
74 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
75 show_bConfigurationValue, set_bConfigurationValue);
78 #define usb_string_attr(name) \
79 static ssize_t show_##name(struct device *dev, \
80 struct device_attribute *attr, char *buf) \
82 struct usb_device *udev; \
84 udev = to_usb_device(dev); \
85 return sprintf(buf, "%s\n", udev->name); \
87 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
89 usb_string_attr(product);
90 usb_string_attr(manufacturer);
91 usb_string_attr(serial);
94 show_speed(struct device *dev, struct device_attribute *attr, char *buf)
96 struct usb_device *udev;
99 udev = to_usb_device(dev);
101 switch (udev->speed) {
105 case USB_SPEED_UNKNOWN:
115 return sprintf(buf, "%s\n", speed);
117 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
120 show_devnum(struct device *dev, struct device_attribute *attr, char *buf)
122 struct usb_device *udev;
124 udev = to_usb_device(dev);
125 return sprintf(buf, "%d\n", udev->devnum);
127 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
130 show_version(struct device *dev, struct device_attribute *attr, char *buf)
132 struct usb_device *udev;
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);
139 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
142 show_maxchild(struct device *dev, struct device_attribute *attr, char *buf)
144 struct usb_device *udev;
146 udev = to_usb_device(dev);
147 return sprintf(buf, "%d\n", udev->maxchild);
149 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
151 /* Descriptor fields */
152 #define usb_descriptor_attr_le16(field, format_string) \
154 show_##field(struct device *dev, struct device_attribute *attr, \
157 struct usb_device *udev; \
159 udev = to_usb_device(dev); \
160 return sprintf(buf, format_string, \
161 le16_to_cpu(udev->descriptor.field)); \
163 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
165 usb_descriptor_attr_le16(idVendor, "%04x\n")
166 usb_descriptor_attr_le16(idProduct, "%04x\n")
167 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
169 #define usb_descriptor_attr(field, format_string) \
171 show_##field(struct device *dev, struct device_attribute *attr, \
174 struct usb_device *udev; \
176 udev = to_usb_device(dev); \
177 return sprintf(buf, format_string, udev->descriptor.field); \
179 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
181 usb_descriptor_attr(bDeviceClass, "%02x\n")
182 usb_descriptor_attr(bDeviceSubClass, "%02x\n")
183 usb_descriptor_attr(bDeviceProtocol, "%02x\n")
184 usb_descriptor_attr(bNumConfigurations, "%d\n")
185 usb_descriptor_attr(bMaxPacketSize0, "%d\n")
187 static struct attribute *dev_attrs[] = {
188 /* current configuration's attributes */
189 &dev_attr_configuration.attr,
190 &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,
202 &dev_attr_bMaxPacketSize0.attr,
203 &dev_attr_speed.attr,
204 &dev_attr_devnum.attr,
205 &dev_attr_version.attr,
206 &dev_attr_maxchild.attr,
209 static struct attribute_group dev_attr_grp = {
213 int usb_create_sysfs_dev_files(struct usb_device *udev)
215 struct device *dev = &udev->dev;
218 retval = sysfs_create_group(&dev->kobj, &dev_attr_grp);
222 if (udev->manufacturer) {
223 retval = device_create_file(dev, &dev_attr_manufacturer);
228 retval = device_create_file(dev, &dev_attr_product);
233 retval = device_create_file(dev, &dev_attr_serial);
237 retval = usb_create_ep_files(dev, &udev->ep0, udev);
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);
249 void usb_remove_sysfs_dev_files(struct usb_device *udev)
251 struct device *dev = &udev->dev;
253 usb_remove_ep_files(&udev->ep0);
254 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
256 if (udev->manufacturer)
257 device_remove_file(dev, &dev_attr_manufacturer);
259 device_remove_file(dev, &dev_attr_product);
261 device_remove_file(dev, &dev_attr_serial);
264 /* Interface fields */
265 #define usb_intf_attr(field, format_string) \
267 show_##field(struct device *dev, struct device_attribute *attr, \
270 struct usb_interface *intf = to_usb_interface(dev); \
272 return sprintf(buf, format_string, \
273 intf->cur_altsetting->desc.field); \
275 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
277 usb_intf_attr(bInterfaceNumber, "%02x\n")
278 usb_intf_attr(bAlternateSetting, "%2d\n")
279 usb_intf_attr(bNumEndpoints, "%02x\n")
280 usb_intf_attr(bInterfaceClass, "%02x\n")
281 usb_intf_attr(bInterfaceSubClass, "%02x\n")
282 usb_intf_attr(bInterfaceProtocol, "%02x\n")
284 static ssize_t show_interface_string(struct device *dev,
285 struct device_attribute *attr, char *buf)
287 struct usb_interface *intf;
288 struct usb_device *udev;
291 intf = to_usb_interface(dev);
292 udev = interface_to_usbdev(intf);
293 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
300 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
302 static ssize_t show_modalias(struct device *dev,
303 struct device_attribute *attr, char *buf)
305 struct usb_interface *intf;
306 struct usb_device *udev;
307 struct usb_host_interface *alt;
309 intf = to_usb_interface(dev);
310 udev = interface_to_usbdev(intf);
311 alt = intf->cur_altsetting;
313 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);
325 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
327 static 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,
334 &dev_attr_modalias.attr,
337 static struct attribute_group intf_attr_grp = {
341 static inline void usb_create_intf_ep_files(struct usb_interface *intf,
342 struct usb_device *udev)
344 struct usb_host_interface *iface_desc;
347 iface_desc = intf->cur_altsetting;
348 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
349 usb_create_ep_files(&intf->dev, &iface_desc->endpoint[i],
353 static inline void usb_remove_intf_ep_files(struct usb_interface *intf)
355 struct usb_host_interface *iface_desc;
358 iface_desc = intf->cur_altsetting;
359 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
360 usb_remove_ep_files(&iface_desc->endpoint[i]);
363 int usb_create_sysfs_intf_files(struct usb_interface *intf)
365 struct usb_device *udev = interface_to_usbdev(intf);
366 struct usb_host_interface *alt = intf->cur_altsetting;
369 retval = sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
373 if (alt->string == NULL)
374 alt->string = usb_cache_string(udev, alt->desc.iInterface);
376 retval = device_create_file(&intf->dev, &dev_attr_interface);
377 usb_create_intf_ep_files(intf, udev);
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);
387 void usb_remove_sysfs_intf_files(struct usb_interface *intf)
389 usb_remove_intf_ep_files(intf);
390 sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
392 if (intf->cur_altsetting->string)
393 device_remove_file(&intf->dev, &dev_attr_interface);