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