2 * drivers/usb/core/driverfs.c
4 * (C) Copyright 2002 David Brownell
5 * (C) Copyright 2002 Greg Kroah-Hartman
6 * (C) Copyright 2002 IBM Corp.
8 * All of the driverfs 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; \
31 udev = to_usb_device (dev); \
32 if (udev->actconfig) \
33 return sprintf (buf, format_string, \
34 udev->actconfig->desc.field * multiplier); \
39 #define usb_actconfig_attr(field, multiplier, format_string) \
40 usb_actconfig_show(field, multiplier, format_string) \
41 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
43 usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
44 usb_actconfig_attr (bmAttributes, 1, "%2x\n")
45 usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
47 /* configuration value is always present, and r/w */
48 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
51 set_bConfigurationValue (struct device *dev, const char *buf, size_t count)
53 struct usb_device *udev = udev = to_usb_device (dev);
56 if (sscanf (buf, "%u", &config) != 1 || config > 255)
58 down(&udev->serialize);
59 value = usb_set_configuration (udev, config);
61 return (value < 0) ? value : count;
64 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
65 show_bConfigurationValue, set_bConfigurationValue);
68 #define usb_string_attr(name, field) \
69 static ssize_t show_##name(struct device *dev, char *buf) \
71 struct usb_device *udev; \
74 udev = to_usb_device (dev); \
75 len = usb_string(udev, udev->descriptor.field, buf, PAGE_SIZE); \
82 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
84 usb_string_attr(product, iProduct);
85 usb_string_attr(manufacturer, iManufacturer);
86 usb_string_attr(serial, iSerialNumber);
89 show_speed (struct device *dev, char *buf)
91 struct usb_device *udev;
94 udev = to_usb_device (dev);
96 switch (udev->speed) {
100 case USB_SPEED_UNKNOWN:
110 return sprintf (buf, "%s\n", speed);
112 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
115 show_devnum (struct device *dev, char *buf)
117 struct usb_device *udev;
119 udev = to_usb_device (dev);
120 return sprintf (buf, "%d\n", udev->devnum);
122 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
125 show_version (struct device *dev, char *buf)
127 struct usb_device *udev;
129 udev = to_usb_device (dev);
130 return sprintf (buf, "%2x.%02x\n", udev->descriptor.bcdUSB >> 8,
131 udev->descriptor.bcdUSB & 0xff);
133 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
136 show_maxchild (struct device *dev, char *buf)
138 struct usb_device *udev;
140 udev = to_usb_device (dev);
141 return sprintf (buf, "%d\n", udev->maxchild);
143 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
145 /* Descriptor fields */
146 #define usb_descriptor_attr(field, format_string) \
148 show_##field (struct device *dev, char *buf) \
150 struct usb_device *udev; \
152 udev = to_usb_device (dev); \
153 return sprintf (buf, format_string, udev->descriptor.field); \
155 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
157 usb_descriptor_attr (idVendor, "%04x\n")
158 usb_descriptor_attr (idProduct, "%04x\n")
159 usb_descriptor_attr (bcdDevice, "%04x\n")
160 usb_descriptor_attr (bDeviceClass, "%02x\n")
161 usb_descriptor_attr (bDeviceSubClass, "%02x\n")
162 usb_descriptor_attr (bDeviceProtocol, "%02x\n")
163 usb_descriptor_attr (bNumConfigurations, "%d\n")
166 void usb_create_driverfs_dev_files (struct usb_device *udev)
168 struct device *dev = &udev->dev;
170 /* current configuration's attributes */
171 device_create_file (dev, &dev_attr_bNumInterfaces);
172 device_create_file (dev, &dev_attr_bConfigurationValue);
173 device_create_file (dev, &dev_attr_bmAttributes);
174 device_create_file (dev, &dev_attr_bMaxPower);
176 /* device attributes */
177 device_create_file (dev, &dev_attr_idVendor);
178 device_create_file (dev, &dev_attr_idProduct);
179 device_create_file (dev, &dev_attr_bcdDevice);
180 device_create_file (dev, &dev_attr_bDeviceClass);
181 device_create_file (dev, &dev_attr_bDeviceSubClass);
182 device_create_file (dev, &dev_attr_bDeviceProtocol);
183 device_create_file (dev, &dev_attr_bNumConfigurations);
185 /* speed varies depending on how you connect the device */
186 device_create_file (dev, &dev_attr_speed);
187 // FIXME iff there are other speed configs, show how many
189 if (udev->descriptor.iManufacturer)
190 device_create_file (dev, &dev_attr_manufacturer);
191 if (udev->descriptor.iProduct)
192 device_create_file (dev, &dev_attr_product);
193 if (udev->descriptor.iSerialNumber)
194 device_create_file (dev, &dev_attr_serial);
196 device_create_file (dev, &dev_attr_devnum);
197 device_create_file (dev, &dev_attr_version);
198 device_create_file (dev, &dev_attr_maxchild);
201 /* Interface fields */
202 #define usb_intf_attr(field, format_string) \
204 show_##field (struct device *dev, char *buf) \
206 struct usb_interface *intf = to_usb_interface (dev); \
208 return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
210 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
212 usb_intf_attr (bInterfaceNumber, "%02x\n")
213 usb_intf_attr (bAlternateSetting, "%2d\n")
214 usb_intf_attr (bNumEndpoints, "%02x\n")
215 usb_intf_attr (bInterfaceClass, "%02x\n")
216 usb_intf_attr (bInterfaceSubClass, "%02x\n")
217 usb_intf_attr (bInterfaceProtocol, "%02x\n")
218 usb_intf_attr (iInterface, "%02x\n")
220 void usb_create_driverfs_intf_files (struct usb_interface *intf)
222 device_create_file (&intf->dev, &dev_attr_bInterfaceNumber);
223 device_create_file (&intf->dev, &dev_attr_bAlternateSetting);
224 device_create_file (&intf->dev, &dev_attr_bNumEndpoints);
225 device_create_file (&intf->dev, &dev_attr_bInterfaceClass);
226 device_create_file (&intf->dev, &dev_attr_bInterfaceSubClass);
227 device_create_file (&intf->dev, &dev_attr_bInterfaceProtocol);
228 device_create_file (&intf->dev, &dev_attr_iInterface);