bae974d587aedc89a3f9eba7c06492ccaca1c8a2
[linux-2.6.git] / drivers / usb / core / sysfs.c
1 /*
2  * drivers/usb/core/sysfs.c
3  *
4  * (C) Copyright 2002 David Brownell
5  * (C) Copyright 2002,2004 Greg Kroah-Hartman
6  * (C) Copyright 2002,2004 IBM Corp.
7  *
8  * All of the sysfs file attributes for usb devices and interfaces.
9  *
10  */
11
12
13 #include <linux/config.h>
14 #include <linux/kernel.h>
15
16 #ifdef CONFIG_USB_DEBUG
17         #define DEBUG
18 #else
19         #undef DEBUG
20 #endif
21 #include <linux/usb.h>
22
23 #include "usb.h"
24
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;                                        \
30         struct usb_host_config *actconfig;                              \
31                                                                         \
32         udev = to_usb_device (dev);                                     \
33         actconfig = udev->actconfig;                                    \
34         if (actconfig)                                                  \
35                 return sprintf (buf, format_string,                     \
36                                 actconfig->desc.field * multiplier);    \
37         else                                                            \
38                 return 0;                                               \
39 }                                                                       \
40
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);
44
45 usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
46 usb_actconfig_attr (bmAttributes, 1, "%2x\n")
47 usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
48
49 #define usb_actconfig_str(name, field)                                  \
50 static ssize_t  show_##name(struct device *dev, char *buf)              \
51 {                                                                       \
52         struct usb_device *udev;                                        \
53         struct usb_host_config *actconfig;                              \
54         int len;                                                        \
55                                                                         \
56         udev = to_usb_device (dev);                                     \
57         actconfig = udev->actconfig;                                    \
58         if (!actconfig)                                                 \
59                 return 0;                                               \
60         len = usb_string(udev, actconfig->desc.field, buf, PAGE_SIZE);  \
61         if (len < 0)                                                    \
62                 return 0;                                               \
63         buf[len] = '\n';                                                \
64         buf[len+1] = 0;                                                 \
65         return len+1;                                                   \
66 }                                                                       \
67 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
68
69 usb_actconfig_str (configuration, iConfiguration)
70
71 /* configuration value is always present, and r/w */
72 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
73
74 static ssize_t
75 set_bConfigurationValue (struct device *dev, const char *buf, size_t count)
76 {
77         struct usb_device       *udev = udev = to_usb_device (dev);
78         int                     config, value;
79
80         if (sscanf (buf, "%u", &config) != 1 || config > 255)
81                 return -EINVAL;
82         usb_lock_device(udev);
83         value = usb_set_configuration (udev, config);
84         usb_unlock_device(udev);
85         return (value < 0) ? value : count;
86 }
87
88 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR, 
89                 show_bConfigurationValue, set_bConfigurationValue);
90
91 /* String fields */
92 #define usb_string_attr(name, field)            \
93 static ssize_t  show_##name(struct device *dev, char *buf)              \
94 {                                                                       \
95         struct usb_device *udev;                                        \
96         int len;                                                        \
97                                                                         \
98         udev = to_usb_device (dev);                                     \
99         len = usb_string(udev, udev->descriptor.field, buf, PAGE_SIZE); \
100         if (len < 0)                                                    \
101                 return 0;                                               \
102         buf[len] = '\n';                                                \
103         buf[len+1] = 0;                                                 \
104         return len+1;                                                   \
105 }                                                                       \
106 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
107
108 usb_string_attr(product, iProduct);
109 usb_string_attr(manufacturer, iManufacturer);
110 usb_string_attr(serial, iSerialNumber);
111
112 static ssize_t
113 show_speed (struct device *dev, char *buf)
114 {
115         struct usb_device *udev;
116         char *speed;
117
118         udev = to_usb_device (dev);
119
120         switch (udev->speed) {
121         case USB_SPEED_LOW:
122                 speed = "1.5";
123                 break;
124         case USB_SPEED_UNKNOWN:
125         case USB_SPEED_FULL:
126                 speed = "12";
127                 break;
128         case USB_SPEED_HIGH:
129                 speed = "480";
130                 break;
131         default:
132                 speed = "unknown";
133         }
134         return sprintf (buf, "%s\n", speed);
135 }
136 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
137
138 static ssize_t
139 show_devnum (struct device *dev, char *buf)
140 {
141         struct usb_device *udev;
142
143         udev = to_usb_device (dev);
144         return sprintf (buf, "%d\n", udev->devnum);
145 }
146 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
147
148 static ssize_t
149 show_version (struct device *dev, char *buf)
150 {
151         struct usb_device *udev;
152
153         udev = to_usb_device (dev);
154         return sprintf (buf, "%2x.%02x\n", udev->descriptor.bcdUSB >> 8, 
155                         udev->descriptor.bcdUSB & 0xff);
156 }
157 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
158
159 static ssize_t
160 show_maxchild (struct device *dev, char *buf)
161 {
162         struct usb_device *udev;
163
164         udev = to_usb_device (dev);
165         return sprintf (buf, "%d\n", udev->maxchild);
166 }
167 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
168
169 /* Descriptor fields */
170 #define usb_descriptor_attr(field, format_string)                       \
171 static ssize_t                                                          \
172 show_##field (struct device *dev, char *buf)                            \
173 {                                                                       \
174         struct usb_device *udev;                                        \
175                                                                         \
176         udev = to_usb_device (dev);                                     \
177         return sprintf (buf, format_string, udev->descriptor.field);    \
178 }                                                                       \
179 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
180
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")
188
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,
207         NULL,
208 };
209 static struct attribute_group dev_attr_grp = {
210         .attrs = dev_attrs,
211 };
212
213 void usb_create_sysfs_dev_files (struct usb_device *udev)
214 {
215         struct device *dev = &udev->dev;
216
217         sysfs_create_group(&dev->kobj, &dev_attr_grp);
218
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);
226 }
227
228 void usb_remove_sysfs_dev_files (struct usb_device *udev)
229 {
230         struct device *dev = &udev->dev;
231
232         sysfs_remove_group(&dev->kobj, &dev_attr_grp);
233
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);
241 }
242
243 /* Interface fields */
244 #define usb_intf_attr(field, format_string)                             \
245 static ssize_t                                                          \
246 show_##field (struct device *dev, char *buf)                            \
247 {                                                                       \
248         struct usb_interface *intf = to_usb_interface (dev);            \
249                                                                         \
250         return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
251 }                                                                       \
252 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
253
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")
260
261 #define usb_intf_str(name, field)                                       \
262 static ssize_t  show_##name(struct device *dev, char *buf)              \
263 {                                                                       \
264         struct usb_interface *intf;                                     \
265         struct usb_device *udev;                                        \
266         int len;                                                        \
267                                                                         \
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);\
271         if (len < 0)                                                    \
272                 return 0;                                               \
273         buf[len] = '\n';                                                \
274         buf[len+1] = 0;                                                 \
275         return len+1;                                                   \
276 }                                                                       \
277 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
278
279 usb_intf_str (interface, iInterface);
280
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,
288         NULL,
289 };
290 static struct attribute_group intf_attr_grp = {
291         .attrs = intf_attrs,
292 };
293
294 void usb_create_sysfs_intf_files (struct usb_interface *intf)
295 {
296         sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
297
298         if (intf->cur_altsetting->desc.iInterface)
299                 device_create_file(&intf->dev, &dev_attr_interface);
300                 
301 }
302
303 void usb_remove_sysfs_intf_files (struct usb_interface *intf)
304 {
305         sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
306
307         if (intf->cur_altsetting->desc.iInterface)
308                 device_remove_file(&intf->dev, &dev_attr_interface);
309
310 }