Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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         u16 bcdUSB;
153 
154         udev = to_usb_device(dev);
155         bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
156         return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
157 }
158 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
159 
160 static ssize_t
161 show_maxchild (struct device *dev, char *buf)
162 {
163         struct usb_device *udev;
164 
165         udev = to_usb_device (dev);
166         return sprintf (buf, "%d\n", udev->maxchild);
167 }
168 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
169 
170 /* Descriptor fields */
171 #define usb_descriptor_attr_le16(field, format_string)                  \
172 static ssize_t                                                          \
173 show_##field (struct device *dev, char *buf)                            \
174 {                                                                       \
175         struct usb_device *udev;                                        \
176                                                                         \
177         udev = to_usb_device (dev);                                     \
178         return sprintf (buf, format_string,                             \
179                         le16_to_cpu(udev->descriptor.field));           \
180 }                                                                       \
181 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
182 
183 usb_descriptor_attr_le16(idVendor, "%04x\n")
184 usb_descriptor_attr_le16(idProduct, "%04x\n")
185 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
186 
187 #define usb_descriptor_attr(field, format_string)                       \
188 static ssize_t                                                          \
189 show_##field (struct device *dev, char *buf)                            \
190 {                                                                       \
191         struct usb_device *udev;                                        \
192                                                                         \
193         udev = to_usb_device (dev);                                     \
194         return sprintf (buf, format_string, udev->descriptor.field);    \
195 }                                                                       \
196 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
197 
198 usb_descriptor_attr (bDeviceClass, "%02x\n")
199 usb_descriptor_attr (bDeviceSubClass, "%02x\n")
200 usb_descriptor_attr (bDeviceProtocol, "%02x\n")
201 usb_descriptor_attr (bNumConfigurations, "%d\n")
202 
203 static struct attribute *dev_attrs[] = {
204         /* current configuration's attributes */
205         &dev_attr_bNumInterfaces.attr,
206         &dev_attr_bConfigurationValue.attr,
207         &dev_attr_bmAttributes.attr,
208         &dev_attr_bMaxPower.attr,
209         /* device attributes */
210         &dev_attr_idVendor.attr,
211         &dev_attr_idProduct.attr,
212         &dev_attr_bcdDevice.attr,
213         &dev_attr_bDeviceClass.attr,
214         &dev_attr_bDeviceSubClass.attr,
215         &dev_attr_bDeviceProtocol.attr,
216         &dev_attr_bNumConfigurations.attr,
217         &dev_attr_speed.attr,
218         &dev_attr_devnum.attr,
219         &dev_attr_version.attr,
220         &dev_attr_maxchild.attr,
221         NULL,
222 };
223 static struct attribute_group dev_attr_grp = {
224         .attrs = dev_attrs,
225 };
226 
227 void usb_create_sysfs_dev_files (struct usb_device *udev)
228 {
229         struct device *dev = &udev->dev;
230 
231         sysfs_create_group(&dev->kobj, &dev_attr_grp);
232 
233         if (udev->descriptor.iManufacturer)
234                 device_create_file (dev, &dev_attr_manufacturer);
235         if (udev->descriptor.iProduct)
236                 device_create_file (dev, &dev_attr_product);
237         if (udev->descriptor.iSerialNumber)
238                 device_create_file (dev, &dev_attr_serial);
239         device_create_file (dev, &dev_attr_configuration);
240 }
241 
242 void usb_remove_sysfs_dev_files (struct usb_device *udev)
243 {
244         struct device *dev = &udev->dev;
245 
246         sysfs_remove_group(&dev->kobj, &dev_attr_grp);
247 
248         if (udev->descriptor.iManufacturer)
249                 device_remove_file(dev, &dev_attr_manufacturer);
250         if (udev->descriptor.iProduct)
251                 device_remove_file(dev, &dev_attr_product);
252         if (udev->descriptor.iSerialNumber)
253                 device_remove_file(dev, &dev_attr_serial);
254         device_remove_file (dev, &dev_attr_configuration);
255 }
256 
257 /* Interface fields */
258 #define usb_intf_attr(field, format_string)                             \
259 static ssize_t                                                          \
260 show_##field (struct device *dev, char *buf)                            \
261 {                                                                       \
262         struct usb_interface *intf = to_usb_interface (dev);            \
263                                                                         \
264         return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
265 }                                                                       \
266 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
267 
268 usb_intf_attr (bInterfaceNumber, "%02x\n")
269 usb_intf_attr (bAlternateSetting, "%2d\n")
270 usb_intf_attr (bNumEndpoints, "%02x\n")
271 usb_intf_attr (bInterfaceClass, "%02x\n")
272 usb_intf_attr (bInterfaceSubClass, "%02x\n")
273 usb_intf_attr (bInterfaceProtocol, "%02x\n")
274 
275 #define usb_intf_str(name, field)                                       \
276 static ssize_t  show_##name(struct device *dev, char *buf)              \
277 {                                                                       \
278         struct usb_interface *intf;                                     \
279         struct usb_device *udev;                                        \
280         int len;                                                        \
281                                                                         \
282         intf = to_usb_interface (dev);                                  \
283         udev = interface_to_usbdev (intf);                              \
284         len = usb_string(udev, intf->cur_altsetting->desc.field, buf, PAGE_SIZE);\
285         if (len < 0)                                                    \
286                 return 0;                                               \
287         buf[len] = '\n';                                                \
288         buf[len+1] = 0;                                                 \
289         return len+1;                                                   \
290 }                                                                       \
291 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
292 
293 usb_intf_str (interface, iInterface);
294 
295 static struct attribute *intf_attrs[] = {
296         &dev_attr_bInterfaceNumber.attr,
297         &dev_attr_bAlternateSetting.attr,
298         &dev_attr_bNumEndpoints.attr,
299         &dev_attr_bInterfaceClass.attr,
300         &dev_attr_bInterfaceSubClass.attr,
301         &dev_attr_bInterfaceProtocol.attr,
302         NULL,
303 };
304 static struct attribute_group intf_attr_grp = {
305         .attrs = intf_attrs,
306 };
307 
308 void usb_create_sysfs_intf_files (struct usb_interface *intf)
309 {
310         sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
311 
312         if (intf->cur_altsetting->desc.iInterface)
313                 device_create_file(&intf->dev, &dev_attr_interface);
314                 
315 }
316 
317 void usb_remove_sysfs_intf_files (struct usb_interface *intf)
318 {
319         sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
320 
321         if (intf->cur_altsetting->desc.iInterface)
322                 device_remove_file(&intf->dev, &dev_attr_interface);
323 
324 }
325 
  This page was automatically generated by the LXR engine.