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; \
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")
165 static struct attribute
*dev_attrs
[] = {
166 /* current configuration's attributes */
167 &dev_attr_bNumInterfaces
.attr
,
168 &dev_attr_bConfigurationValue
.attr
,
169 &dev_attr_bmAttributes
.attr
,
170 &dev_attr_bMaxPower
.attr
,
171 /* device attributes */
172 &dev_attr_idVendor
.attr
,
173 &dev_attr_idProduct
.attr
,
174 &dev_attr_bcdDevice
.attr
,
175 &dev_attr_bDeviceClass
.attr
,
176 &dev_attr_bDeviceSubClass
.attr
,
177 &dev_attr_bDeviceProtocol
.attr
,
178 &dev_attr_bNumConfigurations
.attr
,
179 &dev_attr_speed
.attr
,
180 &dev_attr_devnum
.attr
,
181 &dev_attr_version
.attr
,
182 &dev_attr_maxchild
.attr
,
185 static struct attribute_group dev_attr_grp
= {
189 void usb_create_sysfs_dev_files (struct usb_device
*udev
)
191 struct device
*dev
= &udev
->dev
;
193 sysfs_create_group(&dev
->kobj
, &dev_attr_grp
);
195 if (udev
->descriptor
.iManufacturer
)
196 device_create_file (dev
, &dev_attr_manufacturer
);
197 if (udev
->descriptor
.iProduct
)
198 device_create_file (dev
, &dev_attr_product
);
199 if (udev
->descriptor
.iSerialNumber
)
200 device_create_file (dev
, &dev_attr_serial
);
203 void usb_remove_sysfs_dev_files (struct usb_device
*udev
)
205 struct device
*dev
= &udev
->dev
;
207 sysfs_remove_group(&dev
->kobj
, &dev_attr_grp
);
209 if (udev
->descriptor
.iManufacturer
)
210 device_remove_file(dev
, &dev_attr_manufacturer
);
211 if (udev
->descriptor
.iProduct
)
212 device_remove_file(dev
, &dev_attr_product
);
213 if (udev
->descriptor
.iSerialNumber
)
214 device_remove_file(dev
, &dev_attr_serial
);
217 /* Interface fields */
218 #define usb_intf_attr(field, format_string) \
220 show_##field (struct device *dev, char *buf) \
222 struct usb_interface *intf = to_usb_interface (dev); \
224 return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
226 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
228 usb_intf_attr (bInterfaceNumber
, "%02x\n")
229 usb_intf_attr (bAlternateSetting
, "%2d\n")
230 usb_intf_attr (bNumEndpoints
, "%02x\n")
231 usb_intf_attr (bInterfaceClass
, "%02x\n")
232 usb_intf_attr (bInterfaceSubClass
, "%02x\n")
233 usb_intf_attr (bInterfaceProtocol
, "%02x\n")
234 usb_intf_attr (iInterface
, "%02x\n")
236 static struct attribute
*intf_attrs
[] = {
237 &dev_attr_bInterfaceNumber
.attr
,
238 &dev_attr_bAlternateSetting
.attr
,
239 &dev_attr_bNumEndpoints
.attr
,
240 &dev_attr_bInterfaceClass
.attr
,
241 &dev_attr_bInterfaceSubClass
.attr
,
242 &dev_attr_bInterfaceProtocol
.attr
,
243 &dev_attr_iInterface
.attr
,
246 static struct attribute_group intf_attr_grp
= {
250 void usb_create_sysfs_intf_files (struct usb_interface
*intf
)
252 sysfs_create_group(&intf
->dev
.kobj
, &intf_attr_grp
);
255 void usb_remove_sysfs_intf_files (struct usb_interface
*intf
)
257 sysfs_remove_group(&intf
->dev
.kobj
, &intf_attr_grp
);