This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / usb / core / driverfs.c
1 /*
2  * drivers/usb/core/driverfs.c
3  *
4  * (C) Copyright 2002 David Brownell
5  * (C) Copyright 2002 Greg Kroah-Hartman
6  * (C) Copyright 2002 IBM Corp.
7  *
8  * All of the driverfs 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                                                                         \
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 }                                                                       \
38
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);
42
43 usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
44 usb_actconfig_attr (bmAttributes, 1, "%2x\n")
45 usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
46
47 /* configuration value is always present, and r/w */
48 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
49
50 static ssize_t
51 set_bConfigurationValue (struct device *dev, const char *buf, size_t count)
52 {
53         struct usb_device       *udev = udev = to_usb_device (dev);
54         int                     config, value;
55
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;
62 }
63
64 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR, 
65                 show_bConfigurationValue, set_bConfigurationValue);
66
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;                                                        \
73                                                                         \
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);
83
84 usb_string_attr(product, iProduct);
85 usb_string_attr(manufacturer, iManufacturer);
86 usb_string_attr(serial, iSerialNumber);
87
88 static ssize_t
89 show_speed (struct device *dev, char *buf)
90 {
91         struct usb_device *udev;
92         char *speed;
93
94         udev = to_usb_device (dev);
95
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";
109         }
110         return sprintf (buf, "%s\n", speed);
111 }
112 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
113
114 static ssize_t
115 show_devnum (struct device *dev, char *buf)
116 {
117         struct usb_device *udev;
118
119         udev = to_usb_device (dev);
120         return sprintf (buf, "%d\n", udev->devnum);
121 }
122 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
123
124 static ssize_t
125 show_version (struct device *dev, char *buf)
126 {
127         struct usb_device *udev;
128
129         udev = to_usb_device (dev);
130         return sprintf (buf, "%2x.%02x\n", udev->descriptor.bcdUSB >> 8, 
131                         udev->descriptor.bcdUSB & 0xff);
132 }
133 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
134
135 static ssize_t
136 show_maxchild (struct device *dev, char *buf)
137 {
138         struct usb_device *udev;
139
140         udev = to_usb_device (dev);
141         return sprintf (buf, "%d\n", udev->maxchild);
142 }
143 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
144
145 /* Descriptor fields */
146 #define usb_descriptor_attr(field, format_string)                       \
147 static ssize_t                                                          \
148 show_##field (struct device *dev, char *buf)                            \
149 {                                                                       \
150         struct usb_device *udev;                                        \
151                                                                         \
152         udev = to_usb_device (dev);                                     \
153         return sprintf (buf, format_string, udev->descriptor.field);    \
154 }                                                                       \
155 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
156
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")
164
165
166 void usb_create_driverfs_dev_files (struct usb_device *udev)
167 {
168         struct device *dev = &udev->dev;
169
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);
175
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);
184
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
188
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);
195
196         device_create_file (dev, &dev_attr_devnum);
197         device_create_file (dev, &dev_attr_version);
198         device_create_file (dev, &dev_attr_maxchild);
199 }
200
201 /* Interface fields */
202 #define usb_intf_attr(field, format_string)                             \
203 static ssize_t                                                          \
204 show_##field (struct device *dev, char *buf)                            \
205 {                                                                       \
206         struct usb_interface *intf = to_usb_interface (dev);            \
207                                                                         \
208         return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
209 }                                                                       \
210 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
211
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")
219
220 void usb_create_driverfs_intf_files (struct usb_interface *intf)
221 {
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);
229 }