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/config.h>
14 #include <linux/kernel.h>
16 #ifdef CONFIG_USB_DEBUG
21 #include <linux/usb.h>
25 /* Active configuration fields */
26 #define usb_actconfig_show(field, multiplier, format_string) \
27 static ssize_t show_##field (struct device *dev, char *buf) \
29 struct usb_device *udev; \
30 struct usb_host_config *actconfig; \
32 udev = to_usb_device (dev); \
33 actconfig = udev->actconfig; \
35 return sprintf (buf, format_string, \
36 actconfig->desc.field * multiplier); \
41 #define usb_actconfig_attr(field, multiplier, format_string) \
42 usb_actconfig_show(field, multiplier, format_string) \
43 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
45 usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
46 usb_actconfig_attr (bmAttributes, 1, "%2x\n")
47 usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
49 #define usb_actconfig_str(name, field) \
50 static ssize_t show_##name(struct device *dev, char *buf) \
52 struct usb_device *udev; \
53 struct usb_host_config *actconfig; \
56 udev = to_usb_device (dev); \
57 actconfig = udev->actconfig; \
60 len = usb_string(udev, actconfig->desc.field, buf, PAGE_SIZE); \
67 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
69 usb_actconfig_str (configuration, iConfiguration)
71 /* configuration value is always present, and r/w */
72 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
75 set_bConfigurationValue (struct device *dev, const char *buf, size_t count)
77 struct usb_device *udev = udev = to_usb_device (dev);
80 if (sscanf (buf, "%u", &config) != 1 || config > 255)
82 usb_lock_device(udev);
83 value = usb_set_configuration (udev, config);
84 usb_unlock_device(udev);
85 return (value < 0) ? value : count;
88 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
89 show_bConfigurationValue, set_bConfigurationValue);
92 #define usb_string_attr(name, field) \
93 static ssize_t show_##name(struct device *dev, char *buf) \
95 struct usb_device *udev; \
98 udev = to_usb_device (dev); \
99 len = usb_string(udev, udev->descriptor.field, buf, PAGE_SIZE); \
106 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
108 usb_string_attr(product, iProduct);
109 usb_string_attr(manufacturer, iManufacturer);
110 usb_string_attr(serial, iSerialNumber);
113 show_speed (struct device *dev, char *buf)
115 struct usb_device *udev;
118 udev = to_usb_device (dev);
120 switch (udev->speed) {
124 case USB_SPEED_UNKNOWN:
134 return sprintf (buf, "%s\n", speed);
136 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
139 show_devnum (struct device *dev, char *buf)
141 struct usb_device *udev;
143 udev = to_usb_device (dev);
144 return sprintf (buf, "%d\n", udev->devnum);
146 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
149 show_version (struct device *dev, char *buf)
151 struct usb_device *udev;
153 udev = to_usb_device (dev);
154 return sprintf (buf, "%2x.%02x\n", udev->descriptor.bcdUSB >> 8,
155 udev->descriptor.bcdUSB & 0xff);
157 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
160 show_maxchild (struct device *dev, char *buf)
162 struct usb_device *udev;
164 udev = to_usb_device (dev);
165 return sprintf (buf, "%d\n", udev->maxchild);
167 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
169 /* Descriptor fields */
170 #define usb_descriptor_attr(field, format_string) \
172 show_##field (struct device *dev, char *buf) \
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 (idVendor, "%04x\n")
182 usb_descriptor_attr (idProduct, "%04x\n")
183 usb_descriptor_attr (bcdDevice, "%04x\n")
184 usb_descriptor_attr (bDeviceClass, "%02x\n")
185 usb_descriptor_attr (bDeviceSubClass, "%02x\n")
186 usb_descriptor_attr (bDeviceProtocol, "%02x\n")
187 usb_descriptor_attr (bNumConfigurations, "%d\n")
189 static struct attribute *dev_attrs[] = {
190 /* current configuration's attributes */
191 &dev_attr_bNumInterfaces.attr,
192 &dev_attr_bConfigurationValue.attr,
193 &dev_attr_bmAttributes.attr,
194 &dev_attr_bMaxPower.attr,
195 /* device attributes */
196 &dev_attr_idVendor.attr,
197 &dev_attr_idProduct.attr,
198 &dev_attr_bcdDevice.attr,
199 &dev_attr_bDeviceClass.attr,
200 &dev_attr_bDeviceSubClass.attr,
201 &dev_attr_bDeviceProtocol.attr,
202 &dev_attr_bNumConfigurations.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 void usb_create_sysfs_dev_files (struct usb_device *udev)
215 struct device *dev = &udev->dev;
217 sysfs_create_group(&dev->kobj, &dev_attr_grp);
219 if (udev->descriptor.iManufacturer)
220 device_create_file (dev, &dev_attr_manufacturer);
221 if (udev->descriptor.iProduct)
222 device_create_file (dev, &dev_attr_product);
223 if (udev->descriptor.iSerialNumber)
224 device_create_file (dev, &dev_attr_serial);
225 device_create_file (dev, &dev_attr_configuration);
228 void usb_remove_sysfs_dev_files (struct usb_device *udev)
230 struct device *dev = &udev->dev;
232 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
234 if (udev->descriptor.iManufacturer)
235 device_remove_file(dev, &dev_attr_manufacturer);
236 if (udev->descriptor.iProduct)
237 device_remove_file(dev, &dev_attr_product);
238 if (udev->descriptor.iSerialNumber)
239 device_remove_file(dev, &dev_attr_serial);
240 device_remove_file (dev, &dev_attr_configuration);
243 /* Interface fields */
244 #define usb_intf_attr(field, format_string) \
246 show_##field (struct device *dev, char *buf) \
248 struct usb_interface *intf = to_usb_interface (dev); \
250 return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
252 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
254 usb_intf_attr (bInterfaceNumber, "%02x\n")
255 usb_intf_attr (bAlternateSetting, "%2d\n")
256 usb_intf_attr (bNumEndpoints, "%02x\n")
257 usb_intf_attr (bInterfaceClass, "%02x\n")
258 usb_intf_attr (bInterfaceSubClass, "%02x\n")
259 usb_intf_attr (bInterfaceProtocol, "%02x\n")
261 #define usb_intf_str(name, field) \
262 static ssize_t show_##name(struct device *dev, char *buf) \
264 struct usb_interface *intf; \
265 struct usb_device *udev; \
268 intf = to_usb_interface (dev); \
269 udev = interface_to_usbdev (intf); \
270 len = usb_string(udev, intf->cur_altsetting->desc.field, buf, PAGE_SIZE);\
277 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
279 usb_intf_str (interface, iInterface);
281 static struct attribute *intf_attrs[] = {
282 &dev_attr_bInterfaceNumber.attr,
283 &dev_attr_bAlternateSetting.attr,
284 &dev_attr_bNumEndpoints.attr,
285 &dev_attr_bInterfaceClass.attr,
286 &dev_attr_bInterfaceSubClass.attr,
287 &dev_attr_bInterfaceProtocol.attr,
290 static struct attribute_group intf_attr_grp = {
294 void usb_create_sysfs_intf_files (struct usb_interface *intf)
296 sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
298 if (intf->cur_altsetting->desc.iInterface)
299 device_create_file(&intf->dev, &dev_attr_interface);
303 void usb_remove_sysfs_intf_files (struct usb_interface *intf)
305 sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
307 if (intf->cur_altsetting->desc.iInterface)
308 device_remove_file(&intf->dev, &dev_attr_interface);