MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / usb / core / sysfs.c
blob78c5ca2f1051a2eac7cdbbecd34cd7255431badc
1 /*
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
17 #define DEBUG
18 #else
19 #undef DEBUG
20 #endif
21 #include <linux/usb.h>
23 #include "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) \
28 { \
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); \
35 else \
36 return 0; \
37 } \
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");
50 static ssize_t
51 set_bConfigurationValue (struct device *dev, const char *buf, size_t count)
53 struct usb_device *udev = udev = to_usb_device (dev);
54 int config, value;
56 if (sscanf (buf, "%u", &config) != 1 || config > 255)
57 return -EINVAL;
58 down(&udev->serialize);
59 value = usb_set_configuration (udev, config);
60 up(&udev->serialize);
61 return (value < 0) ? value : count;
64 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
65 show_bConfigurationValue, set_bConfigurationValue);
67 /* String fields */
68 #define usb_string_attr(name, field) \
69 static ssize_t show_##name(struct device *dev, char *buf) \
70 { \
71 struct usb_device *udev; \
72 int len; \
74 udev = to_usb_device (dev); \
75 len = usb_string(udev, udev->descriptor.field, buf, PAGE_SIZE); \
76 if (len < 0) \
77 return 0; \
78 buf[len] = '\n'; \
79 buf[len+1] = 0; \
80 return len+1; \
81 } \
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);
88 static ssize_t
89 show_speed (struct device *dev, char *buf)
91 struct usb_device *udev;
92 char *speed;
94 udev = to_usb_device (dev);
96 switch (udev->speed) {
97 case USB_SPEED_LOW:
98 speed = "1.5";
99 break;
100 case USB_SPEED_UNKNOWN:
101 case USB_SPEED_FULL:
102 speed = "12";
103 break;
104 case USB_SPEED_HIGH:
105 speed = "480";
106 break;
107 default:
108 speed = "unknown";
110 return sprintf (buf, "%s\n", speed);
112 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
114 static ssize_t
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);
124 static ssize_t
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);
135 static ssize_t
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) \
147 static ssize_t \
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,
183 NULL,
185 static struct attribute_group dev_attr_grp = {
186 .attrs = dev_attrs,
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) \
219 static ssize_t \
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,
244 NULL,
246 static struct attribute_group intf_attr_grp = {
247 .attrs = intf_attrs,
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);