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;
154 udev = to_usb_device(dev);
155 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
156 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
158 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
161 show_maxchild (struct device *dev, char *buf)
163 struct usb_device *udev;
165 udev = to_usb_device (dev);
166 return sprintf (buf, "%d\n", udev->maxchild);
168 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
170 /* Descriptor fields */
171 #define usb_descriptor_attr_le16(field, format_string) \
173 show_##field (struct device *dev, char *buf) \
175 struct usb_device *udev; \
177 udev = to_usb_device (dev); \
178 return sprintf (buf, format_string, \
179 le16_to_cpu(udev->descriptor.field)); \
181 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
183 usb_descriptor_attr_le16(idVendor, "%04x\n")
184 usb_descriptor_attr_le16(idProduct, "%04x\n")
185 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
187 #define usb_descriptor_attr(field, format_string) \
189 show_##field (struct device *dev, char *buf) \
191 struct usb_device *udev; \
193 udev = to_usb_device (dev); \
194 return sprintf (buf, format_string, udev->descriptor.field); \
196 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
198 usb_descriptor_attr (bDeviceClass, "%02x\n")
199 usb_descriptor_attr (bDeviceSubClass, "%02x\n")
200 usb_descriptor_attr (bDeviceProtocol, "%02x\n")
201 usb_descriptor_attr (bNumConfigurations, "%d\n")
203 static struct attribute *dev_attrs[] = {
204 /* current configuration's attributes */
205 &dev_attr_bNumInterfaces.attr,
206 &dev_attr_bConfigurationValue.attr,
207 &dev_attr_bmAttributes.attr,
208 &dev_attr_bMaxPower.attr,
209 /* device attributes */
210 &dev_attr_idVendor.attr,
211 &dev_attr_idProduct.attr,
212 &dev_attr_bcdDevice.attr,
213 &dev_attr_bDeviceClass.attr,
214 &dev_attr_bDeviceSubClass.attr,
215 &dev_attr_bDeviceProtocol.attr,
216 &dev_attr_bNumConfigurations.attr,
217 &dev_attr_speed.attr,
218 &dev_attr_devnum.attr,
219 &dev_attr_version.attr,
220 &dev_attr_maxchild.attr,
223 static struct attribute_group dev_attr_grp = {
227 void usb_create_sysfs_dev_files (struct usb_device *udev)
229 struct device *dev = &udev->dev;
231 sysfs_create_group(&dev->kobj, &dev_attr_grp);
233 if (udev->descriptor.iManufacturer)
234 device_create_file (dev, &dev_attr_manufacturer);
235 if (udev->descriptor.iProduct)
236 device_create_file (dev, &dev_attr_product);
237 if (udev->descriptor.iSerialNumber)
238 device_create_file (dev, &dev_attr_serial);
239 device_create_file (dev, &dev_attr_configuration);
242 void usb_remove_sysfs_dev_files (struct usb_device *udev)
244 struct device *dev = &udev->dev;
246 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
248 if (udev->descriptor.iManufacturer)
249 device_remove_file(dev, &dev_attr_manufacturer);
250 if (udev->descriptor.iProduct)
251 device_remove_file(dev, &dev_attr_product);
252 if (udev->descriptor.iSerialNumber)
253 device_remove_file(dev, &dev_attr_serial);
254 device_remove_file (dev, &dev_attr_configuration);
257 /* Interface fields */
258 #define usb_intf_attr(field, format_string) \
260 show_##field (struct device *dev, char *buf) \
262 struct usb_interface *intf = to_usb_interface (dev); \
264 return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
266 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
268 usb_intf_attr (bInterfaceNumber, "%02x\n")
269 usb_intf_attr (bAlternateSetting, "%2d\n")
270 usb_intf_attr (bNumEndpoints, "%02x\n")
271 usb_intf_attr (bInterfaceClass, "%02x\n")
272 usb_intf_attr (bInterfaceSubClass, "%02x\n")
273 usb_intf_attr (bInterfaceProtocol, "%02x\n")
275 #define usb_intf_str(name, field) \
276 static ssize_t show_##name(struct device *dev, char *buf) \
278 struct usb_interface *intf; \
279 struct usb_device *udev; \
282 intf = to_usb_interface (dev); \
283 udev = interface_to_usbdev (intf); \
284 len = usb_string(udev, intf->cur_altsetting->desc.field, buf, PAGE_SIZE);\
291 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
293 usb_intf_str (interface, iInterface);
295 static struct attribute *intf_attrs[] = {
296 &dev_attr_bInterfaceNumber.attr,
297 &dev_attr_bAlternateSetting.attr,
298 &dev_attr_bNumEndpoints.attr,
299 &dev_attr_bInterfaceClass.attr,
300 &dev_attr_bInterfaceSubClass.attr,
301 &dev_attr_bInterfaceProtocol.attr,
304 static struct attribute_group intf_attr_grp = {
308 void usb_create_sysfs_intf_files (struct usb_interface *intf)
310 sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
312 if (intf->cur_altsetting->desc.iInterface)
313 device_create_file(&intf->dev, &dev_attr_interface);
317 void usb_remove_sysfs_intf_files (struct usb_interface *intf)
319 sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
321 if (intf->cur_altsetting->desc.iInterface)
322 device_remove_file(&intf->dev, &dev_attr_interface);