4bdbc9df6e03a599cea97892b34a7f14591bc3e7
[deliverable/linux.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 /* endpoint stuff */
26 struct endpoint_attribute {
27 struct device_attribute dev_attr;
28 struct usb_endpoint_descriptor *endpoint;
29 struct usb_device *udev;
30 };
31 #define to_endpoint_attr(_dev_attr) \
32 container_of(_dev_attr, struct endpoint_attribute, dev_attr)
33
34 #define usb_ep_attr(field, format_string) \
35 static ssize_t show_ep_##field(struct device *dev, struct device_attribute *attr, \
36 char *buf) \
37 { \
38 struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr); \
39 \
40 return sprintf(buf, format_string, endpoint_attr->endpoint->field); \
41 }
42 usb_ep_attr(bLength, "%02x\n")
43 usb_ep_attr(bDescriptorType, "%02x\n")
44 usb_ep_attr(bEndpointAddress, "%02x\n")
45 usb_ep_attr(bmAttributes, "%02x\n")
46 usb_ep_attr(bInterval, "%02x\n")
47
48 static ssize_t show_ep_wMaxPacketSize(struct device *dev,
49 struct device_attribute *attr, char *buf)
50 {
51 struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr);
52
53 return sprintf(buf, "%04x\n",
54 le16_to_cpu(endpoint_attr->endpoint->wMaxPacketSize) & 0x07ff);
55 }
56
57 static ssize_t show_ep_type(struct device *dev, struct device_attribute *attr, char *buf)
58 {
59 struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr);
60 char *type = "unknown";
61
62 switch (endpoint_attr->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
63 case USB_ENDPOINT_XFER_CONTROL:
64 type = "Control";
65 break;
66 case USB_ENDPOINT_XFER_ISOC:
67 type = "Isoc";
68 break;
69 case USB_ENDPOINT_XFER_BULK:
70 type = "Bulk";
71 break;
72 case USB_ENDPOINT_XFER_INT:
73 type = "Interrupt";
74 break;
75 }
76 return sprintf(buf, "%s\n", type);
77 }
78
79 static ssize_t show_ep_interval(struct device *dev, struct device_attribute *attr, char *buf)
80 {
81 struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr);
82 struct usb_device *udev = endpoint_attr->udev;
83 struct usb_endpoint_descriptor *endpoint = endpoint_attr->endpoint;
84 char unit;
85 unsigned interval = 0;
86 unsigned in;
87
88 in = (endpoint->bEndpointAddress & USB_DIR_IN);
89
90 switch (endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
91 case USB_ENDPOINT_XFER_CONTROL:
92 if (udev->speed == USB_SPEED_HIGH) /* uframes per NAK */
93 interval = endpoint->bInterval;
94 break;
95 case USB_ENDPOINT_XFER_ISOC:
96 interval = 1 << (endpoint->bInterval - 1);
97 break;
98 case USB_ENDPOINT_XFER_BULK:
99 if (udev->speed == USB_SPEED_HIGH && !in) /* uframes per NAK */
100 interval = endpoint->bInterval;
101 break;
102 case USB_ENDPOINT_XFER_INT:
103 if (udev->speed == USB_SPEED_HIGH) {
104 interval = 1 << (endpoint->bInterval - 1);
105 } else
106 interval = endpoint->bInterval;
107 break;
108 }
109 interval *= (udev->speed == USB_SPEED_HIGH) ? 125 : 1000;
110 if (interval % 1000)
111 unit = 'u';
112 else {
113 unit = 'm';
114 interval /= 1000;
115 }
116
117 return sprintf(buf, "%d%cs\n", interval, unit);
118 }
119
120 static ssize_t show_ep_direction(struct device *dev, struct device_attribute *attr, char *buf)
121 {
122 struct endpoint_attribute *endpoint_attr = to_endpoint_attr(attr);
123 char *direction;
124
125 if ((endpoint_attr->endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
126 USB_ENDPOINT_XFER_CONTROL)
127 direction = "both";
128 else if (endpoint_attr->endpoint->bEndpointAddress & USB_DIR_IN)
129 direction = "in";
130 else
131 direction = "out";
132 return sprintf(buf, "%s\n", direction);
133 }
134
135 static struct endpoint_attribute *create_ep_attr(struct usb_endpoint_descriptor *endpoint,
136 struct usb_device *udev, char *name,
137 ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf))
138 {
139 struct endpoint_attribute *ep_attr;
140
141 ep_attr = kzalloc(sizeof(*ep_attr), GFP_KERNEL);
142 if (ep_attr) {
143 ep_attr->endpoint = endpoint;
144 ep_attr->udev = udev;
145 ep_attr->dev_attr.attr.name = name;
146 ep_attr->dev_attr.attr.mode = 0444;
147 ep_attr->dev_attr.attr.owner = THIS_MODULE;
148 ep_attr->dev_attr.show = show;
149 }
150 return ep_attr;
151 }
152
153 static void usb_create_ep_files(struct kobject *kobj, struct usb_host_endpoint *endpoint, struct usb_device *udev)
154 {
155 struct usb_endpoint_descriptor *ep;
156
157 ep = &endpoint->desc;
158
159 endpoint->attrs = kzalloc(sizeof(struct attribute *) * 10, GFP_KERNEL);
160 endpoint->attrs[0] = &(create_ep_attr(ep, udev, "direction", show_ep_direction)->dev_attr.attr);
161 endpoint->attrs[1] = &(create_ep_attr(ep, udev, "type", show_ep_type)->dev_attr.attr);
162 endpoint->attrs[2] = &(create_ep_attr(ep, udev, "bLength", show_ep_bLength)->dev_attr.attr);
163 endpoint->attrs[3] = &(create_ep_attr(ep, udev, "bDescriptorType", show_ep_bDescriptorType)->dev_attr.attr);
164 endpoint->attrs[4] = &(create_ep_attr(ep, udev, "bEndpointAddress", show_ep_bEndpointAddress)->dev_attr.attr);
165 endpoint->attrs[5] = &(create_ep_attr(ep, udev, "bmAttributes", show_ep_bmAttributes)->dev_attr.attr);
166 endpoint->attrs[6] = &(create_ep_attr(ep, udev, "wMaxPacketSize", show_ep_wMaxPacketSize)->dev_attr.attr);
167 endpoint->attrs[7] = &(create_ep_attr(ep, udev, "bInterval", show_ep_bInterval)->dev_attr.attr);
168 endpoint->attrs[8] = &(create_ep_attr(ep, udev, "interval", show_ep_interval)->dev_attr.attr);
169 endpoint->attrs[9] = NULL;
170 endpoint->num_attrs = 9;
171
172 endpoint->attr_group = kzalloc(sizeof(*endpoint->attr_group), GFP_KERNEL);
173 endpoint->attr_name = kzalloc(10, GFP_KERNEL);
174 sprintf(endpoint->attr_name, "ep_%02x", endpoint->desc.bEndpointAddress);
175
176 endpoint->attr_group->attrs = endpoint->attrs;
177 endpoint->attr_group->name = endpoint->attr_name;
178 sysfs_create_group(kobj, endpoint->attr_group);
179 }
180
181 static void usb_remove_ep_files(struct kobject *kobj, struct usb_host_endpoint *endpoint)
182 {
183 int i;
184
185 sysfs_remove_group(kobj, endpoint->attr_group);
186 kfree(endpoint->attr_group);
187 kfree(endpoint->attr_name);
188 for (i = 0; i < endpoint->num_attrs; ++i)
189 kfree(endpoint->attrs[i]);
190 kfree(endpoint->attrs);
191 }
192
193 /* Active configuration fields */
194 #define usb_actconfig_show(field, multiplier, format_string) \
195 static ssize_t show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
196 { \
197 struct usb_device *udev; \
198 struct usb_host_config *actconfig; \
199 \
200 udev = to_usb_device (dev); \
201 actconfig = udev->actconfig; \
202 if (actconfig) \
203 return sprintf (buf, format_string, \
204 actconfig->desc.field * multiplier); \
205 else \
206 return 0; \
207 } \
208
209 #define usb_actconfig_attr(field, multiplier, format_string) \
210 usb_actconfig_show(field, multiplier, format_string) \
211 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
212
213 usb_actconfig_attr (bNumInterfaces, 1, "%2d\n")
214 usb_actconfig_attr (bmAttributes, 1, "%2x\n")
215 usb_actconfig_attr (bMaxPower, 2, "%3dmA\n")
216
217 static ssize_t show_configuration_string(struct device *dev, struct device_attribute *attr, char *buf)
218 {
219 struct usb_device *udev;
220 struct usb_host_config *actconfig;
221 int len;
222
223 udev = to_usb_device (dev);
224 actconfig = udev->actconfig;
225 if ((!actconfig) || (!actconfig->string))
226 return 0;
227 len = sprintf(buf, actconfig->string, PAGE_SIZE);
228 if (len < 0)
229 return 0;
230 buf[len] = '\n';
231 buf[len+1] = 0;
232 return len+1;
233 }
234 static DEVICE_ATTR(configuration, S_IRUGO, show_configuration_string, NULL);
235
236 /* configuration value is always present, and r/w */
237 usb_actconfig_show(bConfigurationValue, 1, "%u\n");
238
239 static ssize_t
240 set_bConfigurationValue (struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
241 {
242 struct usb_device *udev = udev = to_usb_device (dev);
243 int config, value;
244
245 if (sscanf (buf, "%u", &config) != 1 || config > 255)
246 return -EINVAL;
247 usb_lock_device(udev);
248 value = usb_set_configuration (udev, config);
249 usb_unlock_device(udev);
250 return (value < 0) ? value : count;
251 }
252
253 static DEVICE_ATTR(bConfigurationValue, S_IRUGO | S_IWUSR,
254 show_bConfigurationValue, set_bConfigurationValue);
255
256 /* String fields */
257 #define usb_string_attr(name) \
258 static ssize_t show_##name(struct device *dev, struct device_attribute *attr, char *buf) \
259 { \
260 struct usb_device *udev; \
261 int len; \
262 \
263 udev = to_usb_device (dev); \
264 len = snprintf(buf, 256, "%s", udev->name); \
265 if (len < 0) \
266 return 0; \
267 buf[len] = '\n'; \
268 buf[len+1] = 0; \
269 return len+1; \
270 } \
271 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
272
273 usb_string_attr(product);
274 usb_string_attr(manufacturer);
275 usb_string_attr(serial);
276
277 static ssize_t
278 show_speed (struct device *dev, struct device_attribute *attr, char *buf)
279 {
280 struct usb_device *udev;
281 char *speed;
282
283 udev = to_usb_device (dev);
284
285 switch (udev->speed) {
286 case USB_SPEED_LOW:
287 speed = "1.5";
288 break;
289 case USB_SPEED_UNKNOWN:
290 case USB_SPEED_FULL:
291 speed = "12";
292 break;
293 case USB_SPEED_HIGH:
294 speed = "480";
295 break;
296 default:
297 speed = "unknown";
298 }
299 return sprintf (buf, "%s\n", speed);
300 }
301 static DEVICE_ATTR(speed, S_IRUGO, show_speed, NULL);
302
303 static ssize_t
304 show_devnum (struct device *dev, struct device_attribute *attr, char *buf)
305 {
306 struct usb_device *udev;
307
308 udev = to_usb_device (dev);
309 return sprintf (buf, "%d\n", udev->devnum);
310 }
311 static DEVICE_ATTR(devnum, S_IRUGO, show_devnum, NULL);
312
313 static ssize_t
314 show_version (struct device *dev, struct device_attribute *attr, char *buf)
315 {
316 struct usb_device *udev;
317 u16 bcdUSB;
318
319 udev = to_usb_device(dev);
320 bcdUSB = le16_to_cpu(udev->descriptor.bcdUSB);
321 return sprintf(buf, "%2x.%02x\n", bcdUSB >> 8, bcdUSB & 0xff);
322 }
323 static DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
324
325 static ssize_t
326 show_maxchild (struct device *dev, struct device_attribute *attr, char *buf)
327 {
328 struct usb_device *udev;
329
330 udev = to_usb_device (dev);
331 return sprintf (buf, "%d\n", udev->maxchild);
332 }
333 static DEVICE_ATTR(maxchild, S_IRUGO, show_maxchild, NULL);
334
335 /* Descriptor fields */
336 #define usb_descriptor_attr_le16(field, format_string) \
337 static ssize_t \
338 show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
339 { \
340 struct usb_device *udev; \
341 \
342 udev = to_usb_device (dev); \
343 return sprintf (buf, format_string, \
344 le16_to_cpu(udev->descriptor.field)); \
345 } \
346 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
347
348 usb_descriptor_attr_le16(idVendor, "%04x\n")
349 usb_descriptor_attr_le16(idProduct, "%04x\n")
350 usb_descriptor_attr_le16(bcdDevice, "%04x\n")
351
352 #define usb_descriptor_attr(field, format_string) \
353 static ssize_t \
354 show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
355 { \
356 struct usb_device *udev; \
357 \
358 udev = to_usb_device (dev); \
359 return sprintf (buf, format_string, udev->descriptor.field); \
360 } \
361 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
362
363 usb_descriptor_attr (bDeviceClass, "%02x\n")
364 usb_descriptor_attr (bDeviceSubClass, "%02x\n")
365 usb_descriptor_attr (bDeviceProtocol, "%02x\n")
366 usb_descriptor_attr (bNumConfigurations, "%d\n")
367 usb_descriptor_attr (bMaxPacketSize0, "%d\n")
368
369 static struct attribute *dev_attrs[] = {
370 /* current configuration's attributes */
371 &dev_attr_bNumInterfaces.attr,
372 &dev_attr_bConfigurationValue.attr,
373 &dev_attr_bmAttributes.attr,
374 &dev_attr_bMaxPower.attr,
375 /* device attributes */
376 &dev_attr_idVendor.attr,
377 &dev_attr_idProduct.attr,
378 &dev_attr_bcdDevice.attr,
379 &dev_attr_bDeviceClass.attr,
380 &dev_attr_bDeviceSubClass.attr,
381 &dev_attr_bDeviceProtocol.attr,
382 &dev_attr_bNumConfigurations.attr,
383 &dev_attr_bMaxPacketSize0.attr,
384 &dev_attr_speed.attr,
385 &dev_attr_devnum.attr,
386 &dev_attr_version.attr,
387 &dev_attr_maxchild.attr,
388 NULL,
389 };
390 static struct attribute_group dev_attr_grp = {
391 .attrs = dev_attrs,
392 };
393
394 void usb_create_sysfs_dev_files (struct usb_device *udev)
395 {
396 struct device *dev = &udev->dev;
397
398 sysfs_create_group(&dev->kobj, &dev_attr_grp);
399
400 if (udev->manufacturer)
401 device_create_file (dev, &dev_attr_manufacturer);
402 if (udev->product)
403 device_create_file (dev, &dev_attr_product);
404 if (udev->serial)
405 device_create_file (dev, &dev_attr_serial);
406 device_create_file (dev, &dev_attr_configuration);
407 usb_create_ep_files(&dev->kobj, &udev->ep0, udev);
408 }
409
410 void usb_remove_sysfs_dev_files (struct usb_device *udev)
411 {
412 struct device *dev = &udev->dev;
413
414 usb_remove_ep_files(&dev->kobj, &udev->ep0);
415 sysfs_remove_group(&dev->kobj, &dev_attr_grp);
416
417 if (udev->descriptor.iManufacturer)
418 device_remove_file(dev, &dev_attr_manufacturer);
419 if (udev->descriptor.iProduct)
420 device_remove_file(dev, &dev_attr_product);
421 if (udev->descriptor.iSerialNumber)
422 device_remove_file(dev, &dev_attr_serial);
423 device_remove_file (dev, &dev_attr_configuration);
424 }
425
426 /* Interface fields */
427 #define usb_intf_attr(field, format_string) \
428 static ssize_t \
429 show_##field (struct device *dev, struct device_attribute *attr, char *buf) \
430 { \
431 struct usb_interface *intf = to_usb_interface (dev); \
432 \
433 return sprintf (buf, format_string, intf->cur_altsetting->desc.field); \
434 } \
435 static DEVICE_ATTR(field, S_IRUGO, show_##field, NULL);
436
437 usb_intf_attr (bInterfaceNumber, "%02x\n")
438 usb_intf_attr (bAlternateSetting, "%2d\n")
439 usb_intf_attr (bNumEndpoints, "%02x\n")
440 usb_intf_attr (bInterfaceClass, "%02x\n")
441 usb_intf_attr (bInterfaceSubClass, "%02x\n")
442 usb_intf_attr (bInterfaceProtocol, "%02x\n")
443
444 static ssize_t show_interface_string(struct device *dev, struct device_attribute *attr, char *buf)
445 {
446 struct usb_interface *intf;
447 struct usb_device *udev;
448 int len;
449
450 intf = to_usb_interface (dev);
451 udev = interface_to_usbdev (intf);
452 len = snprintf(buf, 256, "%s", intf->cur_altsetting->string);
453 if (len < 0)
454 return 0;
455 buf[len] = '\n';
456 buf[len+1] = 0;
457 return len+1;
458 }
459 static DEVICE_ATTR(interface, S_IRUGO, show_interface_string, NULL);
460
461 static ssize_t show_modalias(struct device *dev, struct device_attribute *attr, char *buf)
462 {
463 struct usb_interface *intf;
464 struct usb_device *udev;
465 struct usb_host_interface *alt;
466
467 intf = to_usb_interface(dev);
468 udev = interface_to_usbdev(intf);
469 alt = intf->cur_altsetting;
470
471 return sprintf(buf, "usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02X"
472 "ic%02Xisc%02Xip%02X\n",
473 le16_to_cpu(udev->descriptor.idVendor),
474 le16_to_cpu(udev->descriptor.idProduct),
475 le16_to_cpu(udev->descriptor.bcdDevice),
476 udev->descriptor.bDeviceClass,
477 udev->descriptor.bDeviceSubClass,
478 udev->descriptor.bDeviceProtocol,
479 alt->desc.bInterfaceClass,
480 alt->desc.bInterfaceSubClass,
481 alt->desc.bInterfaceProtocol);
482 }
483 static DEVICE_ATTR(modalias, S_IRUGO, show_modalias, NULL);
484
485 static struct attribute *intf_attrs[] = {
486 &dev_attr_bInterfaceNumber.attr,
487 &dev_attr_bAlternateSetting.attr,
488 &dev_attr_bNumEndpoints.attr,
489 &dev_attr_bInterfaceClass.attr,
490 &dev_attr_bInterfaceSubClass.attr,
491 &dev_attr_bInterfaceProtocol.attr,
492 &dev_attr_modalias.attr,
493 NULL,
494 };
495 static struct attribute_group intf_attr_grp = {
496 .attrs = intf_attrs,
497 };
498
499 static void usb_create_intf_ep_files(struct usb_interface *intf)
500 {
501 struct usb_host_interface *iface_desc;
502 int i;
503
504 iface_desc = intf->cur_altsetting;
505 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
506 usb_create_ep_files(&intf->dev.kobj, &iface_desc->endpoint[i],
507 interface_to_usbdev(intf));
508 }
509
510 static void usb_remove_intf_ep_files(struct usb_interface *intf)
511 {
512 struct usb_host_interface *iface_desc;
513 int i;
514
515 iface_desc = intf->cur_altsetting;
516 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i)
517 usb_remove_ep_files(&intf->dev.kobj, &iface_desc->endpoint[i]);
518 }
519
520 void usb_create_sysfs_intf_files (struct usb_interface *intf)
521 {
522 sysfs_create_group(&intf->dev.kobj, &intf_attr_grp);
523
524 if (intf->cur_altsetting->string)
525 device_create_file(&intf->dev, &dev_attr_interface);
526 usb_create_intf_ep_files(intf);
527 }
528
529 void usb_remove_sysfs_intf_files (struct usb_interface *intf)
530 {
531 usb_remove_intf_ep_files(intf);
532 sysfs_remove_group(&intf->dev.kobj, &intf_attr_grp);
533
534 if (intf->cur_altsetting->string)
535 device_remove_file(&intf->dev, &dev_attr_interface);
536 }
This page took 0.041978 seconds and 5 git commands to generate.