Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

USB: convert to the runtime PM framework

This patch (as1329) converts the USB stack over to the PM core's
runtime PM framework. This involves numerous changes throughout
usbcore, especially to hub.c and driver.c. Perhaps the most notable
change is that CONFIG_USB_SUSPEND now depends on CONFIG_PM_RUNTIME
instead of CONFIG_PM.

Several fields in the usb_device and usb_interface structures are no
longer needed. Some code which used to depend on CONFIG_USB_PM now
depends on CONFIG_USB_SUSPEND (requiring some rearrangement of header
files).

The only visible change in behavior should be that following a system
sleep (resume from RAM or resume from hibernation), autosuspended USB
devices will be resumed just like everything else. They won't remain
suspended. But if they aren't in use then they will naturally
autosuspend again in a few seconds.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Alan Stern and committed by
Greg Kroah-Hartman
9bbdf1e0 0c590e23

+486 -780
+73 -136
Documentation/usb/power-management.txt
··· 2 2 3 3 Alan Stern <stern@rowland.harvard.edu> 4 4 5 - November 10, 2009 5 + December 11, 2009 6 6 7 7 8 8 ··· 29 29 information about system PM). 30 30 31 31 Note: Dynamic PM support for USB is present only if the kernel was 32 - built with CONFIG_USB_SUSPEND enabled. System PM support is present 33 - only if the kernel was built with CONFIG_SUSPEND or CONFIG_HIBERNATION 34 - enabled. 32 + built with CONFIG_USB_SUSPEND enabled (which depends on 33 + CONFIG_PM_RUNTIME). System PM support is present only if the kernel 34 + was built with CONFIG_SUSPEND or CONFIG_HIBERNATION enabled. 35 35 36 36 37 37 What is Remote Wakeup? ··· 326 326 void usb_autopm_get_interface_no_resume(struct usb_interface *intf); 327 327 void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); 328 328 329 - The functions work by maintaining a counter in the usb_interface 330 - structure. When intf->pm_usage_count is > 0 then the interface is 331 - deemed to be busy, and the kernel will not autosuspend the interface's 332 - device. When intf->pm_usage_count is <= 0 then the interface is 333 - considered to be idle, and the kernel may autosuspend the device. 329 + The functions work by maintaining a usage counter in the 330 + usb_interface's embedded device structure. When the counter is > 0 331 + then the interface is deemed to be busy, and the kernel will not 332 + autosuspend the interface's device. When the usage counter is = 0 333 + then the interface is considered to be idle, and the kernel may 334 + autosuspend the device. 334 335 335 - (There is a similar pm_usage_count field in struct usb_device, 336 + (There is a similar usage counter field in struct usb_device, 336 337 associated with the device itself rather than any of its interfaces. 337 - This field is used only by the USB core.) 338 + This counter is used only by the USB core.) 338 339 339 - Drivers must not modify intf->pm_usage_count directly; its value 340 - should be changed only be using the functions listed above. Drivers 341 - are responsible for insuring that the overall change to pm_usage_count 342 - during their lifetime balances out to 0 (it may be necessary for the 343 - disconnect method to call usb_autopm_put_interface() one or more times 344 - to fulfill this requirement). The first two routines use the PM mutex 345 - in struct usb_device for mutual exclusion; drivers using the async 346 - routines are responsible for their own synchronization and mutual 347 - exclusion. 340 + Drivers need not be concerned about balancing changes to the usage 341 + counter; the USB core will undo any remaining "get"s when a driver 342 + is unbound from its interface. As a corollary, drivers must not call 343 + any of the usb_autopm_* functions after their diconnect() routine has 344 + returned. 348 345 349 - usb_autopm_get_interface() increments pm_usage_count and 350 - attempts an autoresume if the new value is > 0 and the 351 - device is suspended. 346 + Drivers using the async routines are responsible for their own 347 + synchronization and mutual exclusion. 352 348 353 - usb_autopm_put_interface() decrements pm_usage_count and 354 - attempts an autosuspend if the new value is <= 0 and the 355 - device isn't suspended. 349 + usb_autopm_get_interface() increments the usage counter and 350 + does an autoresume if the device is suspended. If the 351 + autoresume fails, the counter is decremented back. 352 + 353 + usb_autopm_put_interface() decrements the usage counter and 354 + attempts an autosuspend if the new value is = 0. 356 355 357 356 usb_autopm_get_interface_async() and 358 357 usb_autopm_put_interface_async() do almost the same things as 359 - their non-async counterparts. The differences are: they do 360 - not acquire the PM mutex, and they use a workqueue to do their 358 + their non-async counterparts. The big difference is that they 359 + use a workqueue to do the resume or suspend part of their 361 360 jobs. As a result they can be called in an atomic context, 362 361 such as an URB's completion handler, but when they return the 363 - device will not generally not yet be in the desired state. 362 + device will generally not yet be in the desired state. 364 363 365 364 usb_autopm_get_interface_no_resume() and 366 365 usb_autopm_put_interface_no_suspend() merely increment or 367 - decrement the pm_usage_count value; they do not attempt to 368 - carry out an autoresume or an autosuspend. Hence they can be 369 - called in an atomic context. 366 + decrement the usage counter; they do not attempt to carry out 367 + an autoresume or an autosuspend. Hence they can be called in 368 + an atomic context. 370 369 371 - The conventional usage pattern is that a driver calls 370 + The simplest usage pattern is that a driver calls 372 371 usb_autopm_get_interface() in its open routine and 373 - usb_autopm_put_interface() in its close or release routine. But 374 - other patterns are possible. 372 + usb_autopm_put_interface() in its close or release routine. But other 373 + patterns are possible. 375 374 376 375 The autosuspend attempts mentioned above will often fail for one 377 376 reason or another. For example, the power/level attribute might be 378 377 set to "on", or another interface in the same device might not be 379 378 idle. This is perfectly normal. If the reason for failure was that 380 - the device hasn't been idle for long enough, a delayed workqueue 381 - routine is automatically set up to carry out the operation when the 382 - autosuspend idle-delay has expired. 379 + the device hasn't been idle for long enough, a timer is scheduled to 380 + carry out the operation automatically when the autosuspend idle-delay 381 + has expired. 383 382 384 383 Autoresume attempts also can fail, although failure would mean that 385 384 the device is no longer present or operating properly. Unlike 386 - autosuspend, there's no delay for an autoresume. 385 + autosuspend, there's no idle-delay for an autoresume. 387 386 388 387 389 388 Other parts of the driver interface ··· 412 413 Normally a driver would set this flag in its probe method, at which 413 414 time the device is guaranteed not to be autosuspended.) 414 415 415 - The synchronous usb_autopm_* routines have to run in a sleepable 416 - process context; they must not be called from an interrupt handler or 417 - while holding a spinlock. In fact, the entire autosuspend mechanism 418 - is not well geared toward interrupt-driven operation. However there 419 - is one thing a driver can do in an interrupt handler: 416 + If a driver does its I/O asynchronously in interrupt context, it 417 + should call usb_autopm_get_interface_async() before starting output and 418 + usb_autopm_put_interface_async() when the output queue drains. When 419 + it receives an input event, it should call 420 420 421 421 usb_mark_last_busy(struct usb_device *udev); 422 422 423 - This sets udev->last_busy to the current time. udev->last_busy is the 424 - field used for idle-delay calculations; updating it will cause any 425 - pending autosuspend to be moved back. The usb_autopm_* routines will 426 - also set the last_busy field to the current time. 423 + in the event handler. This sets udev->last_busy to the current time. 424 + udev->last_busy is the field used for idle-delay calculations; 425 + updating it will cause any pending autosuspend to be moved back. Most 426 + of the usb_autopm_* routines will also set the last_busy field to the 427 + current time. 427 428 428 - Calling urb_mark_last_busy() from within an URB completion handler is 429 - subject to races: The kernel may have just finished deciding the 430 - device has been idle for long enough but not yet gotten around to 431 - calling the driver's suspend method. The driver would have to be 432 - responsible for synchronizing its suspend method with its URB 433 - completion handler and causing the autosuspend to fail with -EBUSY if 434 - an URB had completed too recently. 429 + Asynchronous operation is always subject to races. For example, a 430 + driver may call one of the usb_autopm_*_interface_async() routines at 431 + a time when the core has just finished deciding the device has been 432 + idle for long enough but not yet gotten around to calling the driver's 433 + suspend method. The suspend method must be responsible for 434 + synchronizing with the output request routine and the URB completion 435 + handler; it should cause autosuspends to fail with -EBUSY if the 436 + driver needs to use the device. 435 437 436 438 External suspend calls should never be allowed to fail in this way, 437 439 only autosuspend calls. The driver can tell them apart by checking ··· 440 440 method; this bit will be set for internal PM events (autosuspend) and 441 441 clear for external PM events. 442 442 443 - Many of the ingredients in the autosuspend framework are oriented 444 - towards interfaces: The usb_interface structure contains the 445 - pm_usage_cnt field, and the usb_autopm_* routines take an interface 446 - pointer as their argument. But somewhat confusingly, a few of the 447 - pieces (i.e., usb_mark_last_busy()) use the usb_device structure 448 - instead. Drivers need to keep this straight; they can call 449 - interface_to_usbdev() to find the device structure for a given 450 - interface. 451 443 444 + Mutual exclusion 445 + ---------------- 452 446 453 - Locking requirements 454 - -------------------- 455 - 456 - All three suspend/resume methods are always called while holding the 457 - usb_device's PM mutex. For external events -- but not necessarily for 458 - autosuspend or autoresume -- the device semaphore (udev->dev.sem) will 459 - also be held. This implies that external suspend/resume events are 460 - mutually exclusive with calls to probe, disconnect, pre_reset, and 461 - post_reset; the USB core guarantees that this is true of internal 462 - suspend/resume events as well. 447 + For external events -- but not necessarily for autosuspend or 448 + autoresume -- the device semaphore (udev->dev.sem) will be held when a 449 + suspend or resume method is called. This implies that external 450 + suspend/resume events are mutually exclusive with calls to probe, 451 + disconnect, pre_reset, and post_reset; the USB core guarantees that 452 + this is true of autosuspend/autoresume events as well. 463 453 464 454 If a driver wants to block all suspend/resume calls during some 465 - critical section, it can simply acquire udev->pm_mutex. Note that 466 - calls to resume may be triggered indirectly. Block IO due to memory 467 - allocations can make the vm subsystem resume a device. Thus while 468 - holding this lock you must not allocate memory with GFP_KERNEL or 469 - GFP_NOFS. 470 - 471 - Alternatively, if the critical section might call some of the 472 - usb_autopm_* routines, the driver can avoid deadlock by doing: 473 - 474 - down(&udev->dev.sem); 475 - rc = usb_autopm_get_interface(intf); 476 - 477 - and at the end of the critical section: 478 - 479 - if (!rc) 480 - usb_autopm_put_interface(intf); 481 - up(&udev->dev.sem); 482 - 483 - Holding the device semaphore will block all external PM calls, and the 484 - usb_autopm_get_interface() will prevent any internal PM calls, even if 485 - it fails. (Exercise: Why?) 486 - 487 - The rules for locking order are: 488 - 489 - Never acquire any device semaphore while holding any PM mutex. 490 - 491 - Never acquire udev->pm_mutex while holding the PM mutex for 492 - a device that isn't a descendant of udev. 493 - 494 - In other words, PM mutexes should only be acquired going up the device 495 - tree, and they should be acquired only after locking all the device 496 - semaphores you need to hold. These rules don't matter to drivers very 497 - much; they usually affect just the USB core. 498 - 499 - Still, drivers do need to be careful. For example, many drivers use a 500 - private mutex to synchronize their normal I/O activities with their 501 - disconnect method. Now if the driver supports autosuspend then it 502 - must call usb_autopm_put_interface() from somewhere -- maybe from its 503 - close method. It should make the call while holding the private mutex, 504 - since a driver shouldn't call any of the usb_autopm_* functions for an 505 - interface from which it has been unbound. 506 - 507 - But the usb_autpm_* routines always acquire the device's PM mutex, and 508 - consequently the locking order has to be: private mutex first, PM 509 - mutex second. Since the suspend method is always called with the PM 510 - mutex held, it mustn't try to acquire the private mutex. It has to 511 - synchronize with the driver's I/O activities in some other way. 455 + critical section, the best way is to lock the device and call 456 + usb_autopm_get_interface() (and do the reverse at the end of the 457 + critical section). Holding the device semaphore will block all 458 + external PM calls, and the usb_autopm_get_interface() will prevent any 459 + internal PM calls, even if it fails. (Exercise: Why?) 512 460 513 461 514 462 Interaction between dynamic PM and system PM ··· 465 517 Dynamic power management and system power management can interact in 466 518 a couple of ways. 467 519 468 - Firstly, a device may already be manually suspended or autosuspended 469 - when a system suspend occurs. Since system suspends are supposed to 470 - be as transparent as possible, the device should remain suspended 471 - following the system resume. The 2.6.23 kernel obeys this principle 472 - for manually suspended devices but not for autosuspended devices; they 473 - do get resumed when the system wakes up. (Presumably they will be 474 - autosuspended again after their idle-delay time expires.) In later 475 - kernels this behavior will be fixed. 476 - 477 - (There is an exception. If a device would undergo a reset-resume 478 - instead of a normal resume, and the device is enabled for remote 479 - wakeup, then the reset-resume takes place even if the device was 480 - already suspended when the system suspend began. The justification is 481 - that a reset-resume is a kind of remote-wakeup event. Or to put it 482 - another way, a device which needs a reset won't be able to generate 483 - normal remote-wakeup signals, so it ought to be resumed immediately.) 520 + Firstly, a device may already be autosuspended when a system suspend 521 + occurs. Since system suspends are supposed to be as transparent as 522 + possible, the device should remain suspended following the system 523 + resume. But this theory may not work out well in practice; over time 524 + the kernel's behavior in this regard has changed. 484 525 485 526 Secondly, a dynamic power-management event may occur as a system 486 527 suspend is underway. The window for this is short, since system
+2 -2
drivers/usb/core/Kconfig
··· 91 91 If you are unsure about this, say N here. 92 92 93 93 config USB_SUSPEND 94 - bool "USB selective suspend/resume and wakeup" 95 - depends on USB && PM 94 + bool "USB runtime power management (suspend/resume and wakeup)" 95 + depends on USB && PM_RUNTIME 96 96 help 97 97 If you say Y here, you can use driver calls or the sysfs 98 98 "power/level" file to suspend or resume individual USB
+368 -477
drivers/usb/core/driver.c
··· 25 25 #include <linux/device.h> 26 26 #include <linux/usb.h> 27 27 #include <linux/usb/quirks.h> 28 - #include <linux/workqueue.h> 28 + #include <linux/pm_runtime.h> 29 29 #include "hcd.h" 30 30 #include "usb.h" 31 31 ··· 221 221 { 222 222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 223 223 struct usb_device *udev = to_usb_device(dev); 224 - int error = -ENODEV; 224 + int error = 0; 225 225 226 226 dev_dbg(dev, "%s\n", __func__); 227 227 ··· 230 230 /* The device should always appear to be in use 231 231 * unless the driver suports autosuspend. 232 232 */ 233 - udev->pm_usage_cnt = !(udriver->supports_autosuspend); 233 + if (!udriver->supports_autosuspend) 234 + error = usb_autoresume_device(udev); 234 235 235 - error = udriver->probe(udev); 236 + if (!error) 237 + error = udriver->probe(udev); 236 238 return error; 237 239 } 238 240 239 241 /* called from driver core with dev locked */ 240 242 static int usb_unbind_device(struct device *dev) 241 243 { 244 + struct usb_device *udev = to_usb_device(dev); 242 245 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 243 246 244 - udriver->disconnect(to_usb_device(dev)); 247 + udriver->disconnect(udev); 248 + if (!udriver->supports_autosuspend) 249 + usb_autosuspend_device(udev); 245 250 return 0; 246 251 } 247 252 ··· 298 293 if (error) 299 294 return error; 300 295 301 - /* Interface "power state" doesn't correspond to any hardware 302 - * state whatsoever. We use it to record when it's bound to 303 - * a driver that may start I/0: it's not frozen/quiesced. 304 - */ 305 - mark_active(intf); 306 296 intf->condition = USB_INTERFACE_BINDING; 307 297 308 - /* The interface should always appear to be in use 309 - * unless the driver suports autosuspend. 298 + /* Bound interfaces are initially active. They are 299 + * runtime-PM-enabled only if the driver has autosuspend support. 300 + * They are sensitive to their children's power states. 310 301 */ 311 - atomic_set(&intf->pm_usage_cnt, !driver->supports_autosuspend); 302 + pm_runtime_set_active(dev); 303 + pm_suspend_ignore_children(dev, false); 304 + if (driver->supports_autosuspend) 305 + pm_runtime_enable(dev); 312 306 313 307 /* Carry out a deferred switch to altsetting 0 */ 314 308 if (intf->needs_altsetting0) { ··· 327 323 return error; 328 324 329 325 err: 330 - mark_quiesced(intf); 331 326 intf->needs_remote_wakeup = 0; 332 327 intf->condition = USB_INTERFACE_UNBOUND; 333 328 usb_cancel_queued_reset(intf); 329 + 330 + /* Unbound interfaces are always runtime-PM-disabled and -suspended */ 331 + pm_runtime_disable(dev); 332 + pm_runtime_set_suspended(dev); 333 + 334 334 usb_autosuspend_device(udev); 335 335 return error; 336 336 } ··· 384 376 usb_set_intfdata(intf, NULL); 385 377 386 378 intf->condition = USB_INTERFACE_UNBOUND; 387 - mark_quiesced(intf); 388 379 intf->needs_remote_wakeup = 0; 380 + 381 + /* Unbound interfaces are always runtime-PM-disabled and -suspended */ 382 + pm_runtime_disable(dev); 383 + pm_runtime_set_suspended(dev); 384 + 385 + /* Undo any residual pm_autopm_get_interface_* calls */ 386 + for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r) 387 + usb_autopm_put_interface_no_suspend(intf); 388 + atomic_set(&intf->pm_usage_cnt, 0); 389 389 390 390 if (!error) 391 391 usb_autosuspend_device(udev); ··· 425 409 struct usb_interface *iface, void *priv) 426 410 { 427 411 struct device *dev = &iface->dev; 428 - struct usb_device *udev = interface_to_usbdev(iface); 429 412 int retval = 0; 430 413 431 414 if (dev->driver) ··· 434 419 usb_set_intfdata(iface, priv); 435 420 iface->needs_binding = 0; 436 421 437 - usb_pm_lock(udev); 438 422 iface->condition = USB_INTERFACE_BOUND; 439 - mark_active(iface); 440 - atomic_set(&iface->pm_usage_cnt, !driver->supports_autosuspend); 441 - usb_pm_unlock(udev); 423 + 424 + /* Bound interfaces are initially active. They are 425 + * runtime-PM-enabled only if the driver has autosuspend support. 426 + * They are sensitive to their children's power states. 427 + */ 428 + pm_runtime_set_active(dev); 429 + pm_suspend_ignore_children(dev, false); 430 + if (driver->supports_autosuspend) 431 + pm_runtime_enable(dev); 442 432 443 433 /* if interface was already added, bind now; else let 444 434 * the future device_add() bind it, bypassing probe() ··· 1002 982 } 1003 983 } 1004 984 1005 - /* Caller has locked udev's pm_mutex */ 1006 985 static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) 1007 986 { 1008 987 struct usb_device_driver *udriver; ··· 1025 1006 return status; 1026 1007 } 1027 1008 1028 - /* Caller has locked udev's pm_mutex */ 1029 1009 static int usb_resume_device(struct usb_device *udev, pm_message_t msg) 1030 1010 { 1031 1011 struct usb_device_driver *udriver; ··· 1058 1040 return status; 1059 1041 } 1060 1042 1061 - /* Caller has locked intf's usb_device's pm mutex */ 1062 1043 static int usb_suspend_interface(struct usb_device *udev, 1063 1044 struct usb_interface *intf, pm_message_t msg) 1064 1045 { 1065 1046 struct usb_driver *driver; 1066 1047 int status = 0; 1067 1048 1068 - /* with no hardware, USB interfaces only use FREEZE and ON states */ 1069 - if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf)) 1070 - goto done; 1071 - 1072 - /* This can happen; see usb_driver_release_interface() */ 1073 - if (intf->condition == USB_INTERFACE_UNBOUND) 1049 + if (udev->state == USB_STATE_NOTATTACHED || 1050 + intf->condition == USB_INTERFACE_UNBOUND) 1074 1051 goto done; 1075 1052 driver = to_usb_driver(intf->dev.driver); 1076 1053 1077 1054 if (driver->suspend) { 1078 1055 status = driver->suspend(intf, msg); 1079 - if (status == 0) 1080 - mark_quiesced(intf); 1081 - else if (!(msg.event & PM_EVENT_AUTO)) 1056 + if (status && !(msg.event & PM_EVENT_AUTO)) 1082 1057 dev_err(&intf->dev, "%s error %d\n", 1083 1058 "suspend", status); 1084 1059 } else { ··· 1079 1068 intf->needs_binding = 1; 1080 1069 dev_warn(&intf->dev, "no %s for driver %s?\n", 1081 1070 "suspend", driver->name); 1082 - mark_quiesced(intf); 1083 1071 } 1084 1072 1085 1073 done: ··· 1086 1076 return status; 1087 1077 } 1088 1078 1089 - /* Caller has locked intf's usb_device's pm_mutex */ 1090 1079 static int usb_resume_interface(struct usb_device *udev, 1091 1080 struct usb_interface *intf, pm_message_t msg, int reset_resume) 1092 1081 { 1093 1082 struct usb_driver *driver; 1094 1083 int status = 0; 1095 1084 1096 - if (udev->state == USB_STATE_NOTATTACHED || is_active(intf)) 1085 + if (udev->state == USB_STATE_NOTATTACHED) 1097 1086 goto done; 1098 1087 1099 1088 /* Don't let autoresume interfere with unbinding */ ··· 1143 1134 1144 1135 done: 1145 1136 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1146 - if (status == 0 && intf->condition == USB_INTERFACE_BOUND) 1147 - mark_active(intf); 1148 1137 1149 1138 /* Later we will unbind the driver and/or reprobe, if necessary */ 1150 1139 return status; 1151 1140 } 1152 - 1153 - #ifdef CONFIG_USB_SUSPEND 1154 - 1155 - /* Internal routine to check whether we may autosuspend a device. */ 1156 - static int autosuspend_check(struct usb_device *udev, int reschedule) 1157 - { 1158 - int i; 1159 - struct usb_interface *intf; 1160 - unsigned long suspend_time, j; 1161 - 1162 - /* For autosuspend, fail fast if anything is in use or autosuspend 1163 - * is disabled. Also fail if any interfaces require remote wakeup 1164 - * but it isn't available. 1165 - */ 1166 - if (udev->pm_usage_cnt > 0) 1167 - return -EBUSY; 1168 - if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled) 1169 - return -EPERM; 1170 - 1171 - suspend_time = udev->last_busy + udev->autosuspend_delay; 1172 - if (udev->actconfig) { 1173 - for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1174 - intf = udev->actconfig->interface[i]; 1175 - if (!is_active(intf)) 1176 - continue; 1177 - if (atomic_read(&intf->pm_usage_cnt) > 0) 1178 - return -EBUSY; 1179 - if (intf->needs_remote_wakeup && 1180 - !udev->do_remote_wakeup) { 1181 - dev_dbg(&udev->dev, "remote wakeup needed " 1182 - "for autosuspend\n"); 1183 - return -EOPNOTSUPP; 1184 - } 1185 - 1186 - /* Don't allow autosuspend if the device will need 1187 - * a reset-resume and any of its interface drivers 1188 - * doesn't include support. 1189 - */ 1190 - if (udev->quirks & USB_QUIRK_RESET_RESUME) { 1191 - struct usb_driver *driver; 1192 - 1193 - driver = to_usb_driver(intf->dev.driver); 1194 - if (!driver->reset_resume || 1195 - intf->needs_remote_wakeup) 1196 - return -EOPNOTSUPP; 1197 - } 1198 - } 1199 - } 1200 - 1201 - /* If everything is okay but the device hasn't been idle for long 1202 - * enough, queue a delayed autosuspend request. If the device 1203 - * _has_ been idle for long enough and the reschedule flag is set, 1204 - * likewise queue a delayed (1 second) autosuspend request. 1205 - */ 1206 - j = jiffies; 1207 - if (time_before(j, suspend_time)) 1208 - reschedule = 1; 1209 - else 1210 - suspend_time = j + HZ; 1211 - if (reschedule) { 1212 - if (!timer_pending(&udev->autosuspend.timer)) { 1213 - queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, 1214 - round_jiffies_up_relative(suspend_time - j)); 1215 - } 1216 - return -EAGAIN; 1217 - } 1218 - return 0; 1219 - } 1220 - 1221 - #else 1222 - 1223 - static inline int autosuspend_check(struct usb_device *udev, int reschedule) 1224 - { 1225 - return 0; 1226 - } 1227 - 1228 - #endif /* CONFIG_USB_SUSPEND */ 1229 1141 1230 1142 /** 1231 1143 * usb_suspend_both - suspend a USB device and its interfaces ··· 1159 1229 * all the interfaces which were suspended are resumed so that they remain 1160 1230 * in the same state as the device. 1161 1231 * 1162 - * If an autosuspend is in progress the routine checks first to make sure 1163 - * that neither the device itself or any of its active interfaces is in use 1164 - * (pm_usage_cnt is greater than 0). If they are, the autosuspend fails. 1165 - * 1166 - * If the suspend succeeds, the routine recursively queues an autosuspend 1167 - * request for @udev's parent device, thereby propagating the change up 1168 - * the device tree. If all of the parent's children are now suspended, 1169 - * the parent will autosuspend in turn. 1170 - * 1171 - * The suspend method calls are subject to mutual exclusion under control 1172 - * of @udev's pm_mutex. Many of these calls are also under the protection 1173 - * of @udev's device lock (including all requests originating outside the 1174 - * USB subsystem), but autosuspend requests generated by a child device or 1175 - * interface driver may not be. Usbcore will insure that the method calls 1176 - * do not arrive during bind, unbind, or reset operations. However, drivers 1177 - * must be prepared to handle suspend calls arriving at unpredictable times. 1178 - * The only way to block such calls is to do an autoresume (preventing 1179 - * autosuspends) while holding @udev's device lock (preventing outside 1180 - * suspends). 1181 - * 1182 - * The caller must hold @udev->pm_mutex. 1232 + * Autosuspend requests originating from a child device or an interface 1233 + * driver may be made without the protection of @udev's device lock, but 1234 + * all other suspend calls will hold the lock. Usbcore will insure that 1235 + * method calls do not arrive during bind, unbind, or reset operations. 1236 + * However drivers must be prepared to handle suspend calls arriving at 1237 + * unpredictable times. 1183 1238 * 1184 1239 * This routine can run only in process context. 1185 1240 */ ··· 1173 1258 int status = 0; 1174 1259 int i = 0; 1175 1260 struct usb_interface *intf; 1176 - struct usb_device *parent = udev->parent; 1177 1261 1178 1262 if (udev->state == USB_STATE_NOTATTACHED || 1179 1263 udev->state == USB_STATE_SUSPENDED) 1180 1264 goto done; 1181 - 1182 - udev->do_remote_wakeup = device_may_wakeup(&udev->dev); 1183 - 1184 - if (msg.event & PM_EVENT_AUTO) { 1185 - status = autosuspend_check(udev, 0); 1186 - if (status < 0) 1187 - goto done; 1188 - } 1189 1265 1190 1266 /* Suspend all the interfaces and then udev itself */ 1191 1267 if (udev->actconfig) { ··· 1192 1286 1193 1287 /* If the suspend failed, resume interfaces that did get suspended */ 1194 1288 if (status != 0) { 1195 - pm_message_t msg2; 1196 - 1197 - msg2.event = msg.event ^ (PM_EVENT_SUSPEND | PM_EVENT_RESUME); 1289 + msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME); 1198 1290 while (--i >= 0) { 1199 1291 intf = udev->actconfig->interface[i]; 1200 - usb_resume_interface(udev, intf, msg2, 0); 1292 + usb_resume_interface(udev, intf, msg, 0); 1201 1293 } 1202 1294 1203 - /* Try another autosuspend when the interfaces aren't busy */ 1204 - if (msg.event & PM_EVENT_AUTO) 1205 - autosuspend_check(udev, status == -EBUSY); 1206 - 1207 - /* If the suspend succeeded then prevent any more URB submissions, 1208 - * flush any outstanding URBs, and propagate the suspend up the tree. 1295 + /* If the suspend succeeded then prevent any more URB submissions 1296 + * and flush any outstanding URBs. 1209 1297 */ 1210 1298 } else { 1211 - cancel_delayed_work(&udev->autosuspend); 1212 1299 udev->can_submit = 0; 1213 1300 for (i = 0; i < 16; ++i) { 1214 1301 usb_hcd_flush_endpoint(udev, udev->ep_out[i]); 1215 1302 usb_hcd_flush_endpoint(udev, udev->ep_in[i]); 1216 1303 } 1217 - 1218 - /* If this is just a FREEZE or a PRETHAW, udev might 1219 - * not really be suspended. Only true suspends get 1220 - * propagated up the device tree. 1221 - */ 1222 - if (parent && udev->state == USB_STATE_SUSPENDED) 1223 - usb_autosuspend_device(parent); 1224 1304 } 1225 1305 1226 1306 done: ··· 1223 1331 * the resume method for @udev and then calls the resume methods for all 1224 1332 * the interface drivers in @udev. 1225 1333 * 1226 - * Before starting the resume, the routine calls itself recursively for 1227 - * the parent device of @udev, thereby propagating the change up the device 1228 - * tree and assuring that @udev will be able to resume. If the parent is 1229 - * unable to resume successfully, the routine fails. 1230 - * 1231 - * The resume method calls are subject to mutual exclusion under control 1232 - * of @udev's pm_mutex. Many of these calls are also under the protection 1233 - * of @udev's device lock (including all requests originating outside the 1234 - * USB subsystem), but autoresume requests generated by a child device or 1235 - * interface driver may not be. Usbcore will insure that the method calls 1236 - * do not arrive during bind, unbind, or reset operations. However, drivers 1237 - * must be prepared to handle resume calls arriving at unpredictable times. 1238 - * The only way to block such calls is to do an autoresume (preventing 1239 - * other autoresumes) while holding @udev's device lock (preventing outside 1240 - * resumes). 1241 - * 1242 - * The caller must hold @udev->pm_mutex. 1334 + * Autoresume requests originating from a child device or an interface 1335 + * driver may be made without the protection of @udev's device lock, but 1336 + * all other resume calls will hold the lock. Usbcore will insure that 1337 + * method calls do not arrive during bind, unbind, or reset operations. 1338 + * However drivers must be prepared to handle resume calls arriving at 1339 + * unpredictable times. 1243 1340 * 1244 1341 * This routine can run only in process context. 1245 1342 */ ··· 1237 1356 int status = 0; 1238 1357 int i; 1239 1358 struct usb_interface *intf; 1240 - struct usb_device *parent = udev->parent; 1241 1359 1242 - cancel_delayed_work(&udev->autosuspend); 1243 1360 if (udev->state == USB_STATE_NOTATTACHED) { 1244 1361 status = -ENODEV; 1245 1362 goto done; 1246 1363 } 1247 1364 udev->can_submit = 1; 1248 1365 1249 - /* Propagate the resume up the tree, if necessary */ 1250 - if (udev->state == USB_STATE_SUSPENDED) { 1251 - if (parent) { 1252 - status = usb_autoresume_device(parent); 1253 - if (status == 0) { 1254 - status = usb_resume_device(udev, msg); 1255 - if (status || udev->state == 1256 - USB_STATE_NOTATTACHED) { 1257 - usb_autosuspend_device(parent); 1258 - 1259 - /* It's possible usb_resume_device() 1260 - * failed after the port was 1261 - * unsuspended, causing udev to be 1262 - * logically disconnected. We don't 1263 - * want usb_disconnect() to autosuspend 1264 - * the parent again, so tell it that 1265 - * udev disconnected while still 1266 - * suspended. */ 1267 - if (udev->state == 1268 - USB_STATE_NOTATTACHED) 1269 - udev->discon_suspended = 1; 1270 - } 1271 - } 1272 - } else { 1273 - 1274 - /* We can't progagate beyond the USB subsystem, 1275 - * so if a root hub's controller is suspended 1276 - * then we're stuck. */ 1277 - status = usb_resume_device(udev, msg); 1278 - } 1279 - } else if (udev->reset_resume) 1366 + /* Resume the device */ 1367 + if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume) 1280 1368 status = usb_resume_device(udev, msg); 1281 1369 1370 + /* Resume the interfaces */ 1282 1371 if (status == 0 && udev->actconfig) { 1283 1372 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1284 1373 intf = udev->actconfig->interface[i]; ··· 1264 1413 return status; 1265 1414 } 1266 1415 1267 - /** 1268 - * usb_external_suspend_device - external suspend of a USB device and its interfaces 1269 - * @udev: the usb_device to suspend 1270 - * @msg: Power Management message describing this state transition 1271 - * 1272 - * This routine handles external suspend requests: ones not generated 1273 - * internally by a USB driver (autosuspend) but rather coming from the user 1274 - * (via sysfs) or the PM core (system sleep). The suspend will be carried 1275 - * out regardless of @udev's usage counter or those of its interfaces, 1276 - * and regardless of whether or not remote wakeup is enabled. Of course, 1277 - * interface drivers still have the option of failing the suspend (if 1278 - * there are unsuspended children, for example). 1279 - * 1280 - * The caller must hold @udev's device lock. 1281 - */ 1282 - int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg) 1283 - { 1284 - int status; 1285 - 1286 - do_unbind_rebind(udev, DO_UNBIND); 1287 - usb_pm_lock(udev); 1288 - status = usb_suspend_both(udev, msg); 1289 - usb_pm_unlock(udev); 1290 - return status; 1291 - } 1292 - 1293 - /** 1294 - * usb_external_resume_device - external resume of a USB device and its interfaces 1295 - * @udev: the usb_device to resume 1296 - * @msg: Power Management message describing this state transition 1297 - * 1298 - * This routine handles external resume requests: ones not generated 1299 - * internally by a USB driver (autoresume) but rather coming from the user 1300 - * (via sysfs), the PM core (system resume), or the device itself (remote 1301 - * wakeup). @udev's usage counter is unaffected. 1302 - * 1303 - * The caller must hold @udev's device lock. 1304 - */ 1305 - int usb_external_resume_device(struct usb_device *udev, pm_message_t msg) 1306 - { 1307 - int status; 1308 - 1309 - usb_pm_lock(udev); 1310 - status = usb_resume_both(udev, msg); 1311 - udev->last_busy = jiffies; 1312 - usb_pm_unlock(udev); 1313 - if (status == 0) 1314 - do_unbind_rebind(udev, DO_REBIND); 1315 - 1316 - /* Now that the device is awake, we can start trying to autosuspend 1317 - * it again. */ 1318 - if (status == 0) 1319 - usb_try_autosuspend_device(udev); 1320 - return status; 1321 - } 1322 - 1416 + /* The device lock is held by the PM core */ 1323 1417 int usb_suspend(struct device *dev, pm_message_t msg) 1324 1418 { 1325 - struct usb_device *udev; 1419 + struct usb_device *udev = to_usb_device(dev); 1326 1420 1327 - udev = to_usb_device(dev); 1328 - 1329 - /* If udev is already suspended, we can skip this suspend and 1330 - * we should also skip the upcoming system resume. High-speed 1331 - * root hubs are an exception; they need to resume whenever the 1332 - * system wakes up in order for USB-PERSIST port handover to work 1333 - * properly. 1334 - */ 1335 - if (udev->state == USB_STATE_SUSPENDED) { 1336 - if (udev->parent || udev->speed != USB_SPEED_HIGH) 1337 - udev->skip_sys_resume = 1; 1338 - return 0; 1339 - } 1340 - 1341 - udev->skip_sys_resume = 0; 1342 - return usb_external_suspend_device(udev, msg); 1421 + do_unbind_rebind(udev, DO_UNBIND); 1422 + udev->do_remote_wakeup = device_may_wakeup(&udev->dev); 1423 + return usb_suspend_both(udev, msg); 1343 1424 } 1344 1425 1426 + /* The device lock is held by the PM core */ 1345 1427 int usb_resume(struct device *dev, pm_message_t msg) 1346 1428 { 1347 - struct usb_device *udev; 1429 + struct usb_device *udev = to_usb_device(dev); 1348 1430 int status; 1349 1431 1350 - udev = to_usb_device(dev); 1432 + /* For PM complete calls, all we do is rebind interfaces */ 1433 + if (msg.event == PM_EVENT_ON) { 1434 + if (udev->state != USB_STATE_NOTATTACHED) 1435 + do_unbind_rebind(udev, DO_REBIND); 1436 + status = 0; 1351 1437 1352 - /* If udev->skip_sys_resume is set then udev was already suspended 1353 - * when the system sleep started, so we don't want to resume it 1354 - * during this system wakeup. 1438 + /* For all other calls, take the device back to full power and 1439 + * tell the PM core in case it was autosuspended previously. 1355 1440 */ 1356 - if (udev->skip_sys_resume) 1357 - return 0; 1358 - status = usb_external_resume_device(udev, msg); 1441 + } else { 1442 + status = usb_resume_both(udev, msg); 1443 + if (status == 0) { 1444 + pm_runtime_disable(dev); 1445 + pm_runtime_set_active(dev); 1446 + pm_runtime_enable(dev); 1447 + udev->last_busy = jiffies; 1448 + } 1449 + } 1359 1450 1360 1451 /* Avoid PM error messages for devices disconnected while suspended 1361 1452 * as we'll display regular disconnect messages just a bit later. 1362 1453 */ 1363 1454 if (status == -ENODEV) 1364 - return 0; 1455 + status = 0; 1365 1456 return status; 1366 1457 } 1367 1458 ··· 1353 1560 } 1354 1561 EXPORT_SYMBOL_GPL(usb_disable_autosuspend); 1355 1562 1356 - /* Internal routine to adjust a device's usage counter and change 1357 - * its autosuspend state. 1358 - */ 1359 - static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt) 1360 - { 1361 - int status = 0; 1362 - 1363 - usb_pm_lock(udev); 1364 - udev->pm_usage_cnt += inc_usage_cnt; 1365 - WARN_ON(udev->pm_usage_cnt < 0); 1366 - if (inc_usage_cnt) 1367 - udev->last_busy = jiffies; 1368 - if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) { 1369 - if (udev->state == USB_STATE_SUSPENDED) 1370 - status = usb_resume_both(udev, PMSG_AUTO_RESUME); 1371 - if (status != 0) 1372 - udev->pm_usage_cnt -= inc_usage_cnt; 1373 - else if (inc_usage_cnt) 1374 - udev->last_busy = jiffies; 1375 - } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) { 1376 - status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND); 1377 - } 1378 - usb_pm_unlock(udev); 1379 - return status; 1380 - } 1381 - 1382 - /* usb_autosuspend_work - callback routine to autosuspend a USB device */ 1383 - void usb_autosuspend_work(struct work_struct *work) 1384 - { 1385 - struct usb_device *udev = 1386 - container_of(work, struct usb_device, autosuspend.work); 1387 - 1388 - usb_autopm_do_device(udev, 0); 1389 - } 1390 - 1391 - /* usb_autoresume_work - callback routine to autoresume a USB device */ 1392 - void usb_autoresume_work(struct work_struct *work) 1393 - { 1394 - struct usb_device *udev = 1395 - container_of(work, struct usb_device, autoresume); 1396 - 1397 - /* Wake it up, let the drivers do their thing, and then put it 1398 - * back to sleep. 1399 - */ 1400 - if (usb_autopm_do_device(udev, 1) == 0) 1401 - usb_autopm_do_device(udev, -1); 1402 - } 1403 - 1404 1563 /** 1405 1564 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces 1406 1565 * @udev: the usb_device to autosuspend ··· 1361 1616 * @udev and wants to allow it to autosuspend. Examples would be when 1362 1617 * @udev's device file in usbfs is closed or after a configuration change. 1363 1618 * 1364 - * @udev's usage counter is decremented. If it or any of the usage counters 1365 - * for an active interface is greater than 0, no autosuspend request will be 1366 - * queued. (If an interface driver does not support autosuspend then its 1367 - * usage counter is permanently positive.) Furthermore, if an interface 1368 - * driver requires remote-wakeup capability during autosuspend but remote 1369 - * wakeup is disabled, the autosuspend will fail. 1619 + * @udev's usage counter is decremented; if it drops to 0 and all the 1620 + * interfaces are inactive then a delayed autosuspend will be attempted. 1621 + * The attempt may fail (see autosuspend_check()). 1370 1622 * 1371 1623 * The caller must hold @udev's device lock. 1372 1624 * ··· 1373 1631 { 1374 1632 int status; 1375 1633 1376 - status = usb_autopm_do_device(udev, -1); 1377 - dev_vdbg(&udev->dev, "%s: cnt %d\n", 1378 - __func__, udev->pm_usage_cnt); 1634 + udev->last_busy = jiffies; 1635 + status = pm_runtime_put_sync(&udev->dev); 1636 + dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", 1637 + __func__, atomic_read(&udev->dev.power.usage_count), 1638 + status); 1379 1639 } 1380 1640 1381 1641 /** ··· 1387 1643 * This routine should be called when a core subsystem thinks @udev may 1388 1644 * be ready to autosuspend. 1389 1645 * 1390 - * @udev's usage counter left unchanged. If it or any of the usage counters 1391 - * for an active interface is greater than 0, or autosuspend is not allowed 1392 - * for any other reason, no autosuspend request will be queued. 1646 + * @udev's usage counter left unchanged. If it is 0 and all the interfaces 1647 + * are inactive then an autosuspend will be attempted. The attempt may 1648 + * fail or be delayed. 1393 1649 * 1394 1650 * The caller must hold @udev's device lock. 1395 1651 * ··· 1397 1653 */ 1398 1654 void usb_try_autosuspend_device(struct usb_device *udev) 1399 1655 { 1400 - usb_autopm_do_device(udev, 0); 1401 - dev_vdbg(&udev->dev, "%s: cnt %d\n", 1402 - __func__, udev->pm_usage_cnt); 1656 + int status; 1657 + 1658 + status = pm_runtime_idle(&udev->dev); 1659 + dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", 1660 + __func__, atomic_read(&udev->dev.power.usage_count), 1661 + status); 1403 1662 } 1404 1663 1405 1664 /** ··· 1411 1664 * 1412 1665 * This routine should be called when a core subsystem wants to use @udev 1413 1666 * and needs to guarantee that it is not suspended. No autosuspend will 1414 - * occur until usb_autosuspend_device is called. (Note that this will not 1415 - * prevent suspend events originating in the PM core.) Examples would be 1416 - * when @udev's device file in usbfs is opened or when a remote-wakeup 1667 + * occur until usb_autosuspend_device() is called. (Note that this will 1668 + * not prevent suspend events originating in the PM core.) Examples would 1669 + * be when @udev's device file in usbfs is opened or when a remote-wakeup 1417 1670 * request is received. 1418 1671 * 1419 1672 * @udev's usage counter is incremented to prevent subsequent autosuspends. ··· 1427 1680 { 1428 1681 int status; 1429 1682 1430 - status = usb_autopm_do_device(udev, 1); 1431 - dev_vdbg(&udev->dev, "%s: status %d cnt %d\n", 1432 - __func__, status, udev->pm_usage_cnt); 1433 - return status; 1434 - } 1435 - 1436 - /* Internal routine to adjust an interface's usage counter and change 1437 - * its device's autosuspend state. 1438 - */ 1439 - static int usb_autopm_do_interface(struct usb_interface *intf, 1440 - int inc_usage_cnt) 1441 - { 1442 - struct usb_device *udev = interface_to_usbdev(intf); 1443 - int status = 0; 1444 - 1445 - usb_pm_lock(udev); 1446 - if (intf->condition == USB_INTERFACE_UNBOUND) 1447 - status = -ENODEV; 1448 - else { 1449 - atomic_add(inc_usage_cnt, &intf->pm_usage_cnt); 1450 - udev->last_busy = jiffies; 1451 - if (inc_usage_cnt >= 0 && 1452 - atomic_read(&intf->pm_usage_cnt) > 0) { 1453 - if (udev->state == USB_STATE_SUSPENDED) 1454 - status = usb_resume_both(udev, 1455 - PMSG_AUTO_RESUME); 1456 - if (status != 0) 1457 - atomic_sub(inc_usage_cnt, &intf->pm_usage_cnt); 1458 - else 1459 - udev->last_busy = jiffies; 1460 - } else if (inc_usage_cnt <= 0 && 1461 - atomic_read(&intf->pm_usage_cnt) <= 0) { 1462 - status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND); 1463 - } 1464 - } 1465 - usb_pm_unlock(udev); 1683 + status = pm_runtime_get_sync(&udev->dev); 1684 + if (status < 0) 1685 + pm_runtime_put_sync(&udev->dev); 1686 + dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n", 1687 + __func__, atomic_read(&udev->dev.power.usage_count), 1688 + status); 1689 + if (status > 0) 1690 + status = 0; 1466 1691 return status; 1467 1692 } 1468 1693 ··· 1448 1729 * closed. 1449 1730 * 1450 1731 * The routine decrements @intf's usage counter. When the counter reaches 1451 - * 0, a delayed autosuspend request for @intf's device is queued. When 1452 - * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all 1453 - * the other usage counters for the sibling interfaces and @intf's 1454 - * usb_device, the device and all its interfaces will be autosuspended. 1455 - * 1456 - * Note that @intf->pm_usage_cnt is owned by the interface driver. The 1457 - * core will not change its value other than the increment and decrement 1458 - * in usb_autopm_get_interface and usb_autopm_put_interface. The driver 1459 - * may use this simple counter-oriented discipline or may set the value 1460 - * any way it likes. 1732 + * 0, a delayed autosuspend request for @intf's device is attempted. The 1733 + * attempt may fail (see autosuspend_check()). 1461 1734 * 1462 1735 * If the driver has set @intf->needs_remote_wakeup then autosuspend will 1463 1736 * take place only if the device's remote-wakeup facility is enabled. 1464 - * 1465 - * Suspend method calls queued by this routine can arrive at any time 1466 - * while @intf is resumed and its usage counter is equal to 0. They are 1467 - * not protected by the usb_device's lock but only by its pm_mutex. 1468 - * Drivers must provide their own synchronization. 1469 1737 * 1470 1738 * This routine can run only in process context. 1471 1739 */ 1472 1740 void usb_autopm_put_interface(struct usb_interface *intf) 1473 1741 { 1474 - int status; 1742 + struct usb_device *udev = interface_to_usbdev(intf); 1743 + int status; 1475 1744 1476 - status = usb_autopm_do_interface(intf, -1); 1477 - dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1478 - __func__, status, atomic_read(&intf->pm_usage_cnt)); 1745 + udev->last_busy = jiffies; 1746 + atomic_dec(&intf->pm_usage_cnt); 1747 + status = pm_runtime_put_sync(&intf->dev); 1748 + dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1749 + __func__, atomic_read(&intf->dev.power.usage_count), 1750 + status); 1479 1751 } 1480 1752 EXPORT_SYMBOL_GPL(usb_autopm_put_interface); 1481 1753 ··· 1474 1764 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter 1475 1765 * @intf: the usb_interface whose counter should be decremented 1476 1766 * 1477 - * This routine does essentially the same thing as 1478 - * usb_autopm_put_interface(): it decrements @intf's usage counter and 1479 - * queues a delayed autosuspend request if the counter is <= 0. The 1480 - * difference is that it does not acquire the device's pm_mutex; 1481 - * callers must handle all synchronization issues themselves. 1767 + * This routine does much the same thing as usb_autopm_put_interface(): 1768 + * It decrements @intf's usage counter and schedules a delayed 1769 + * autosuspend request if the counter is <= 0. The difference is that it 1770 + * does not perform any synchronization; callers should hold a private 1771 + * lock and handle all synchronization issues themselves. 1482 1772 * 1483 1773 * Typically a driver would call this routine during an URB's completion 1484 1774 * handler, if no more URBs were pending. ··· 1488 1778 void usb_autopm_put_interface_async(struct usb_interface *intf) 1489 1779 { 1490 1780 struct usb_device *udev = interface_to_usbdev(intf); 1781 + unsigned long last_busy; 1491 1782 int status = 0; 1492 1783 1493 - if (intf->condition == USB_INTERFACE_UNBOUND) { 1494 - status = -ENODEV; 1495 - } else { 1496 - udev->last_busy = jiffies; 1497 - atomic_dec(&intf->pm_usage_cnt); 1498 - if (udev->autosuspend_disabled || udev->autosuspend_delay < 0) 1499 - status = -EPERM; 1500 - else if (atomic_read(&intf->pm_usage_cnt) <= 0 && 1501 - !timer_pending(&udev->autosuspend.timer)) { 1502 - queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, 1784 + last_busy = udev->last_busy; 1785 + udev->last_busy = jiffies; 1786 + atomic_dec(&intf->pm_usage_cnt); 1787 + pm_runtime_put_noidle(&intf->dev); 1788 + 1789 + if (!udev->autosuspend_disabled) { 1790 + /* Optimization: Don't schedule a delayed autosuspend if 1791 + * the timer is already running and the expiration time 1792 + * wouldn't change. 1793 + * 1794 + * We have to use the interface's timer. Attempts to 1795 + * schedule a suspend for the device would fail because 1796 + * the interface is still active. 1797 + */ 1798 + if (intf->dev.power.timer_expires == 0 || 1799 + round_jiffies_up(last_busy) != 1800 + round_jiffies_up(jiffies)) { 1801 + status = pm_schedule_suspend(&intf->dev, 1802 + jiffies_to_msecs( 1503 1803 round_jiffies_up_relative( 1504 - udev->autosuspend_delay)); 1804 + udev->autosuspend_delay))); 1505 1805 } 1506 1806 } 1507 - dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1508 - __func__, status, atomic_read(&intf->pm_usage_cnt)); 1807 + dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1808 + __func__, atomic_read(&intf->dev.power.usage_count), 1809 + status); 1509 1810 } 1510 1811 EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async); 1812 + 1813 + /** 1814 + * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter 1815 + * @intf: the usb_interface whose counter should be decremented 1816 + * 1817 + * This routine decrements @intf's usage counter but does not carry out an 1818 + * autosuspend. 1819 + * 1820 + * This routine can run in atomic context. 1821 + */ 1822 + void usb_autopm_put_interface_no_suspend(struct usb_interface *intf) 1823 + { 1824 + struct usb_device *udev = interface_to_usbdev(intf); 1825 + 1826 + udev->last_busy = jiffies; 1827 + atomic_dec(&intf->pm_usage_cnt); 1828 + pm_runtime_put_noidle(&intf->dev); 1829 + } 1830 + EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend); 1511 1831 1512 1832 /** 1513 1833 * usb_autopm_get_interface - increment a USB interface's PM-usage counter ··· 1551 1811 * or @intf is unbound. A typical example would be a character-device 1552 1812 * driver when its device file is opened. 1553 1813 * 1554 - * 1555 - * The routine increments @intf's usage counter. (However if the 1556 - * autoresume fails then the counter is re-decremented.) So long as the 1557 - * counter is greater than 0, autosuspend will not be allowed for @intf 1558 - * or its usb_device. When the driver is finished using @intf it should 1559 - * call usb_autopm_put_interface() to decrement the usage counter and 1560 - * queue a delayed autosuspend request (if the counter is <= 0). 1561 - * 1562 - * 1563 - * Note that @intf->pm_usage_cnt is owned by the interface driver. The 1564 - * core will not change its value other than the increment and decrement 1565 - * in usb_autopm_get_interface and usb_autopm_put_interface. The driver 1566 - * may use this simple counter-oriented discipline or may set the value 1567 - * any way it likes. 1568 - * 1569 - * Resume method calls generated by this routine can arrive at any time 1570 - * while @intf is suspended. They are not protected by the usb_device's 1571 - * lock but only by its pm_mutex. Drivers must provide their own 1572 - * synchronization. 1814 + * @intf's usage counter is incremented to prevent subsequent autosuspends. 1815 + * However if the autoresume fails then the counter is re-decremented. 1573 1816 * 1574 1817 * This routine can run only in process context. 1575 1818 */ ··· 1560 1837 { 1561 1838 int status; 1562 1839 1563 - status = usb_autopm_do_interface(intf, 1); 1564 - dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1565 - __func__, status, atomic_read(&intf->pm_usage_cnt)); 1840 + status = pm_runtime_get_sync(&intf->dev); 1841 + if (status < 0) 1842 + pm_runtime_put_sync(&intf->dev); 1843 + else 1844 + atomic_inc(&intf->pm_usage_cnt); 1845 + dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1846 + __func__, atomic_read(&intf->dev.power.usage_count), 1847 + status); 1848 + if (status > 0) 1849 + status = 0; 1566 1850 return status; 1567 1851 } 1568 1852 EXPORT_SYMBOL_GPL(usb_autopm_get_interface); ··· 1579 1849 * @intf: the usb_interface whose counter should be incremented 1580 1850 * 1581 1851 * This routine does much the same thing as 1582 - * usb_autopm_get_interface(): it increments @intf's usage counter and 1583 - * queues an autoresume request if the result is > 0. The differences 1584 - * are that it does not acquire the device's pm_mutex (callers must 1585 - * handle all synchronization issues themselves), and it does not 1586 - * autoresume the device directly (it only queues a request). After a 1587 - * successful call, the device will generally not yet be resumed. 1852 + * usb_autopm_get_interface(): It increments @intf's usage counter and 1853 + * queues an autoresume request if the device is suspended. The 1854 + * differences are that it does not perform any synchronization (callers 1855 + * should hold a private lock and handle all synchronization issues 1856 + * themselves), and it does not autoresume the device directly (it only 1857 + * queues a request). After a successful call, the device may not yet be 1858 + * resumed. 1588 1859 * 1589 1860 * This routine can run in atomic context. 1590 1861 */ 1591 1862 int usb_autopm_get_interface_async(struct usb_interface *intf) 1592 1863 { 1593 - struct usb_device *udev = interface_to_usbdev(intf); 1594 - int status = 0; 1864 + int status = 0; 1865 + enum rpm_status s; 1595 1866 1596 - if (intf->condition == USB_INTERFACE_UNBOUND) 1597 - status = -ENODEV; 1598 - else { 1867 + /* Don't request a resume unless the interface is already suspending 1868 + * or suspended. Doing so would force a running suspend timer to be 1869 + * cancelled. 1870 + */ 1871 + pm_runtime_get_noresume(&intf->dev); 1872 + s = ACCESS_ONCE(intf->dev.power.runtime_status); 1873 + if (s == RPM_SUSPENDING || s == RPM_SUSPENDED) 1874 + status = pm_request_resume(&intf->dev); 1875 + 1876 + if (status < 0 && status != -EINPROGRESS) 1877 + pm_runtime_put_noidle(&intf->dev); 1878 + else 1599 1879 atomic_inc(&intf->pm_usage_cnt); 1600 - if (atomic_read(&intf->pm_usage_cnt) > 0 && 1601 - udev->state == USB_STATE_SUSPENDED) 1602 - queue_work(ksuspend_usb_wq, &udev->autoresume); 1603 - } 1604 - dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1605 - __func__, status, atomic_read(&intf->pm_usage_cnt)); 1880 + dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n", 1881 + __func__, atomic_read(&intf->dev.power.usage_count), 1882 + status); 1883 + if (status > 0) 1884 + status = 0; 1606 1885 return status; 1607 1886 } 1608 1887 EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); 1609 1888 1889 + /** 1890 + * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter 1891 + * @intf: the usb_interface whose counter should be incremented 1892 + * 1893 + * This routine increments @intf's usage counter but does not carry out an 1894 + * autoresume. 1895 + * 1896 + * This routine can run in atomic context. 1897 + */ 1898 + void usb_autopm_get_interface_no_resume(struct usb_interface *intf) 1899 + { 1900 + struct usb_device *udev = interface_to_usbdev(intf); 1901 + 1902 + udev->last_busy = jiffies; 1903 + atomic_inc(&intf->pm_usage_cnt); 1904 + pm_runtime_get_noresume(&intf->dev); 1905 + } 1906 + EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume); 1907 + 1908 + /* Internal routine to check whether we may autosuspend a device. */ 1909 + static int autosuspend_check(struct usb_device *udev) 1910 + { 1911 + int i; 1912 + struct usb_interface *intf; 1913 + unsigned long suspend_time, j; 1914 + 1915 + /* Fail if autosuspend is disabled, or any interfaces are in use, or 1916 + * any interface drivers require remote wakeup but it isn't available. 1917 + */ 1918 + udev->do_remote_wakeup = device_may_wakeup(&udev->dev); 1919 + if (udev->actconfig) { 1920 + for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1921 + intf = udev->actconfig->interface[i]; 1922 + 1923 + /* We don't need to check interfaces that are 1924 + * disabled for runtime PM. Either they are unbound 1925 + * or else their drivers don't support autosuspend 1926 + * and so they are permanently active. 1927 + */ 1928 + if (intf->dev.power.disable_depth) 1929 + continue; 1930 + if (atomic_read(&intf->dev.power.usage_count) > 0) 1931 + return -EBUSY; 1932 + if (intf->needs_remote_wakeup && 1933 + !udev->do_remote_wakeup) { 1934 + dev_dbg(&udev->dev, "remote wakeup needed " 1935 + "for autosuspend\n"); 1936 + return -EOPNOTSUPP; 1937 + } 1938 + 1939 + /* Don't allow autosuspend if the device will need 1940 + * a reset-resume and any of its interface drivers 1941 + * doesn't include support or needs remote wakeup. 1942 + */ 1943 + if (udev->quirks & USB_QUIRK_RESET_RESUME) { 1944 + struct usb_driver *driver; 1945 + 1946 + driver = to_usb_driver(intf->dev.driver); 1947 + if (!driver->reset_resume || 1948 + intf->needs_remote_wakeup) 1949 + return -EOPNOTSUPP; 1950 + } 1951 + } 1952 + } 1953 + 1954 + /* If everything is okay but the device hasn't been idle for long 1955 + * enough, queue a delayed autosuspend request. 1956 + */ 1957 + j = ACCESS_ONCE(jiffies); 1958 + suspend_time = udev->last_busy + udev->autosuspend_delay; 1959 + if (time_before(j, suspend_time)) { 1960 + pm_schedule_suspend(&udev->dev, jiffies_to_msecs( 1961 + round_jiffies_up_relative(suspend_time - j))); 1962 + return -EAGAIN; 1963 + } 1964 + return 0; 1965 + } 1966 + 1967 + static int usb_runtime_suspend(struct device *dev) 1968 + { 1969 + int status = 0; 1970 + 1971 + /* A USB device can be suspended if it passes the various autosuspend 1972 + * checks. Runtime suspend for a USB device means suspending all the 1973 + * interfaces and then the device itself. 1974 + */ 1975 + if (is_usb_device(dev)) { 1976 + struct usb_device *udev = to_usb_device(dev); 1977 + 1978 + if (autosuspend_check(udev) != 0) 1979 + return -EAGAIN; 1980 + 1981 + status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND); 1982 + 1983 + /* If an interface fails the suspend, adjust the last_busy 1984 + * time so that we don't get another suspend attempt right 1985 + * away. 1986 + */ 1987 + if (status) { 1988 + udev->last_busy = jiffies + 1989 + (udev->autosuspend_delay == 0 ? 1990 + HZ/2 : 0); 1991 + } 1992 + 1993 + /* Prevent the parent from suspending immediately after */ 1994 + else if (udev->parent) { 1995 + udev->parent->last_busy = jiffies; 1996 + } 1997 + } 1998 + 1999 + /* Runtime suspend for a USB interface doesn't mean anything. */ 2000 + return status; 2001 + } 2002 + 2003 + static int usb_runtime_resume(struct device *dev) 2004 + { 2005 + /* Runtime resume for a USB device means resuming both the device 2006 + * and all its interfaces. 2007 + */ 2008 + if (is_usb_device(dev)) { 2009 + struct usb_device *udev = to_usb_device(dev); 2010 + int status; 2011 + 2012 + status = usb_resume_both(udev, PMSG_AUTO_RESUME); 2013 + udev->last_busy = jiffies; 2014 + return status; 2015 + } 2016 + 2017 + /* Runtime resume for a USB interface doesn't mean anything. */ 2018 + return 0; 2019 + } 2020 + 2021 + static int usb_runtime_idle(struct device *dev) 2022 + { 2023 + /* An idle USB device can be suspended if it passes the various 2024 + * autosuspend checks. An idle interface can be suspended at 2025 + * any time. 2026 + */ 2027 + if (is_usb_device(dev)) { 2028 + struct usb_device *udev = to_usb_device(dev); 2029 + 2030 + if (autosuspend_check(udev) != 0) 2031 + return 0; 2032 + } 2033 + 2034 + pm_runtime_suspend(dev); 2035 + return 0; 2036 + } 2037 + 2038 + static struct dev_pm_ops usb_bus_pm_ops = { 2039 + .runtime_suspend = usb_runtime_suspend, 2040 + .runtime_resume = usb_runtime_resume, 2041 + .runtime_idle = usb_runtime_idle, 2042 + }; 2043 + 1610 2044 #else 1611 2045 1612 - void usb_autosuspend_work(struct work_struct *work) 1613 - {} 1614 - 1615 - void usb_autoresume_work(struct work_struct *work) 1616 - {} 2046 + #define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL) 1617 2047 1618 2048 #endif /* CONFIG_USB_SUSPEND */ 1619 2049 ··· 1781 1891 .name = "usb", 1782 1892 .match = usb_device_match, 1783 1893 .uevent = usb_uevent, 1894 + .pm = &usb_bus_pm_ops, 1784 1895 };
+9 -4
drivers/usb/core/hcd.c
··· 39 39 #include <linux/platform_device.h> 40 40 #include <linux/workqueue.h> 41 41 #include <linux/mutex.h> 42 + #include <linux/pm_runtime.h> 42 43 43 44 #include <linux/usb.h> 44 45 ··· 1859 1858 return status; 1860 1859 } 1861 1860 1861 + #endif /* CONFIG_PM */ 1862 + 1863 + #ifdef CONFIG_USB_SUSPEND 1864 + 1862 1865 /* Workqueue routine for root-hub remote wakeup */ 1863 1866 static void hcd_resume_work(struct work_struct *work) 1864 1867 { ··· 1889 1884 1890 1885 spin_lock_irqsave (&hcd_root_hub_lock, flags); 1891 1886 if (hcd->rh_registered) 1892 - queue_work(ksuspend_usb_wq, &hcd->wakeup_work); 1887 + queue_work(pm_wq, &hcd->wakeup_work); 1893 1888 spin_unlock_irqrestore (&hcd_root_hub_lock, flags); 1894 1889 } 1895 1890 EXPORT_SYMBOL_GPL(usb_hcd_resume_root_hub); 1896 1891 1897 - #endif 1892 + #endif /* CONFIG_USB_SUSPEND */ 1898 1893 1899 1894 /*-------------------------------------------------------------------------*/ 1900 1895 ··· 2039 2034 init_timer(&hcd->rh_timer); 2040 2035 hcd->rh_timer.function = rh_timer_func; 2041 2036 hcd->rh_timer.data = (unsigned long) hcd; 2042 - #ifdef CONFIG_PM 2037 + #ifdef CONFIG_USB_SUSPEND 2043 2038 INIT_WORK(&hcd->wakeup_work, hcd_resume_work); 2044 2039 #endif 2045 2040 mutex_init(&hcd->bandwidth_mutex); ··· 2239 2234 hcd->rh_registered = 0; 2240 2235 spin_unlock_irq (&hcd_root_hub_lock); 2241 2236 2242 - #ifdef CONFIG_PM 2237 + #ifdef CONFIG_USB_SUSPEND 2243 2238 cancel_work_sync(&hcd->wakeup_work); 2244 2239 #endif 2245 2240
+7 -3
drivers/usb/core/hcd.h
··· 80 80 81 81 struct timer_list rh_timer; /* drives root-hub polling */ 82 82 struct urb *status_urb; /* the current status urb */ 83 - #ifdef CONFIG_PM 83 + #ifdef CONFIG_USB_SUSPEND 84 84 struct work_struct wakeup_work; /* for remote wakeup */ 85 85 #endif 86 86 ··· 464 464 #define usb_endpoint_out(ep_dir) (!((ep_dir) & USB_DIR_IN)) 465 465 466 466 #ifdef CONFIG_PM 467 - extern void usb_hcd_resume_root_hub(struct usb_hcd *hcd); 468 467 extern void usb_root_hub_lost_power(struct usb_device *rhdev); 469 468 extern int hcd_bus_suspend(struct usb_device *rhdev, pm_message_t msg); 470 469 extern int hcd_bus_resume(struct usb_device *rhdev, pm_message_t msg); 470 + #endif /* CONFIG_PM */ 471 + 472 + #ifdef CONFIG_USB_SUSPEND 473 + extern void usb_hcd_resume_root_hub(struct usb_hcd *hcd); 471 474 #else 472 475 static inline void usb_hcd_resume_root_hub(struct usb_hcd *hcd) 473 476 { 474 477 return; 475 478 } 476 - #endif /* CONFIG_PM */ 479 + #endif /* CONFIG_USB_SUSPEND */ 480 + 477 481 478 482 /* 479 483 * USB device fs stuff
+15 -50
drivers/usb/core/hub.c
··· 22 22 #include <linux/kthread.h> 23 23 #include <linux/mutex.h> 24 24 #include <linux/freezer.h> 25 + #include <linux/pm_runtime.h> 25 26 26 27 #include <asm/uaccess.h> 27 28 #include <asm/byteorder.h> ··· 72 71 73 72 unsigned mA_per_port; /* current for each child */ 74 73 75 - unsigned init_done:1; 76 74 unsigned limited_power:1; 77 75 unsigned quiescing:1; 78 76 unsigned disconnected:1; ··· 820 820 } 821 821 init3: 822 822 hub->quiescing = 0; 823 - hub->init_done = 1; 824 823 825 824 status = usb_submit_urb(hub->urb, GFP_NOIO); 826 825 if (status < 0) ··· 860 861 int i; 861 862 862 863 cancel_delayed_work_sync(&hub->init_work); 863 - if (!hub->init_done) { 864 - hub->init_done = 1; 865 - usb_autopm_put_interface_no_suspend( 866 - to_usb_interface(hub->intfdev)); 867 - } 868 864 869 865 /* khubd and related activity won't re-trigger */ 870 866 hub->quiescing = 1; ··· 1399 1405 if (udev->children[i]) 1400 1406 recursively_mark_NOTATTACHED(udev->children[i]); 1401 1407 } 1402 - if (udev->state == USB_STATE_SUSPENDED) { 1403 - udev->discon_suspended = 1; 1408 + if (udev->state == USB_STATE_SUSPENDED) 1404 1409 udev->active_duration -= jiffies; 1405 - } 1406 1410 udev->state = USB_STATE_NOTATTACHED; 1407 1411 } 1408 1412 ··· 1524 1532 udev->devnum = devnum; 1525 1533 } 1526 1534 1527 - #ifdef CONFIG_USB_SUSPEND 1528 - 1529 - static void usb_stop_pm(struct usb_device *udev) 1530 - { 1531 - /* Synchronize with the ksuspend thread to prevent any more 1532 - * autosuspend requests from being submitted, and decrement 1533 - * the parent's count of unsuspended children. 1534 - */ 1535 - usb_pm_lock(udev); 1536 - if (udev->parent && !udev->discon_suspended) 1537 - usb_autosuspend_device(udev->parent); 1538 - usb_pm_unlock(udev); 1539 - 1540 - /* Stop any autosuspend or autoresume requests already submitted */ 1541 - cancel_delayed_work_sync(&udev->autosuspend); 1542 - cancel_work_sync(&udev->autoresume); 1543 - } 1544 - 1545 - #else 1546 - 1547 - static inline void usb_stop_pm(struct usb_device *udev) 1548 - { } 1549 - 1550 - #endif 1551 - 1552 1535 /** 1553 1536 * usb_disconnect - disconnect a device (usbcore-internal) 1554 1537 * @pdev: pointer to device being disconnected ··· 1591 1624 spin_lock_irq(&device_state_lock); 1592 1625 *pdev = NULL; 1593 1626 spin_unlock_irq(&device_state_lock); 1594 - 1595 - usb_stop_pm(udev); 1596 1627 1597 1628 put_device(&udev->dev); 1598 1629 } ··· 1768 1803 int err; 1769 1804 1770 1805 if (udev->parent) { 1771 - /* Increment the parent's count of unsuspended children */ 1772 - usb_autoresume_device(udev->parent); 1773 - 1774 1806 /* Initialize non-root-hub device wakeup to disabled; 1775 1807 * device (un)configuration controls wakeup capable 1776 1808 * sysfs power/wakeup controls wakeup enabled/disabled ··· 1775 1813 device_init_wakeup(&udev->dev, 0); 1776 1814 device_set_wakeup_enable(&udev->dev, 1); 1777 1815 } 1816 + 1817 + /* Tell the runtime-PM framework the device is active */ 1818 + pm_runtime_set_active(&udev->dev); 1819 + pm_runtime_enable(&udev->dev); 1778 1820 1779 1821 usb_detect_quirks(udev); 1780 1822 err = usb_enumerate_device(udev); /* Read descriptors */ ··· 1810 1844 1811 1845 fail: 1812 1846 usb_set_device_state(udev, USB_STATE_NOTATTACHED); 1813 - usb_stop_pm(udev); 1847 + pm_runtime_disable(&udev->dev); 1848 + pm_runtime_set_suspended(&udev->dev); 1814 1849 return err; 1815 1850 } 1816 1851 ··· 2375 2408 2376 2409 if (udev->state == USB_STATE_SUSPENDED) { 2377 2410 dev_dbg(&udev->dev, "usb %sresume\n", "wakeup-"); 2378 - usb_mark_last_busy(udev); 2379 - status = usb_external_resume_device(udev, PMSG_REMOTE_RESUME); 2411 + status = usb_autoresume_device(udev); 2412 + if (status == 0) { 2413 + /* Let the drivers do their thing, then... */ 2414 + usb_autosuspend_device(udev); 2415 + } 2380 2416 } 2381 2417 return status; 2382 2418 } ··· 2414 2444 status = usb_reset_and_verify_device(udev); 2415 2445 } 2416 2446 return status; 2417 - } 2418 - 2419 - int usb_remote_wakeup(struct usb_device *udev) 2420 - { 2421 - return 0; 2422 2447 } 2423 2448 2424 2449 #endif ··· 3233 3268 * disconnected while waiting for the lock to succeed. */ 3234 3269 usb_lock_device(hdev); 3235 3270 if (unlikely(hub->disconnected)) 3236 - goto loop2; 3271 + goto loop_disconnected; 3237 3272 3238 3273 /* If the hub has died, clean up after it */ 3239 3274 if (hdev->state == USB_STATE_NOTATTACHED) { ··· 3393 3428 * kick_khubd() and allow autosuspend. 3394 3429 */ 3395 3430 usb_autopm_put_interface(intf); 3396 - loop2: 3431 + loop_disconnected: 3397 3432 usb_unlock_device(hdev); 3398 3433 kref_put(&hub->kref, hub_release); 3399 3434
-1
drivers/usb/core/message.c
··· 1843 1843 intf->dev.dma_mask = dev->dev.dma_mask; 1844 1844 INIT_WORK(&intf->reset_ws, __usb_queue_reset_device); 1845 1845 device_initialize(&intf->dev); 1846 - mark_quiesced(intf); 1847 1846 dev_set_name(&intf->dev, "%d-%s:%d.%d", 1848 1847 dev->bus->busnum, dev->devpath, 1849 1848 configuration, alt->desc.bInterfaceNumber);
+3 -32
drivers/usb/core/usb.c
··· 49 49 50 50 static int nousb; /* Disable USB when built into kernel image */ 51 51 52 - /* Workqueue for autosuspend and for remote wakeup of root hubs */ 53 - struct workqueue_struct *ksuspend_usb_wq; 54 - 55 52 #ifdef CONFIG_USB_SUSPEND 56 53 static int usb_autosuspend_delay = 2; /* Default delay value, 57 54 * in seconds */ ··· 261 264 262 265 #ifdef CONFIG_PM 263 266 264 - static int ksuspend_usb_init(void) 265 - { 266 - /* This workqueue is supposed to be both freezable and 267 - * singlethreaded. Its job doesn't justify running on more 268 - * than one CPU. 269 - */ 270 - ksuspend_usb_wq = create_freezeable_workqueue("ksuspend_usbd"); 271 - if (!ksuspend_usb_wq) 272 - return -ENOMEM; 273 - return 0; 274 - } 275 - 276 - static void ksuspend_usb_cleanup(void) 277 - { 278 - destroy_workqueue(ksuspend_usb_wq); 279 - } 280 - 281 267 /* USB device Power-Management thunks. 282 268 * There's no need to distinguish here between quiescing a USB device 283 269 * and powering it down; the generic_suspend() routine takes care of ··· 276 296 static void usb_dev_complete(struct device *dev) 277 297 { 278 298 /* Currently used only for rebinding interfaces */ 279 - usb_resume(dev, PMSG_RESUME); /* Message event is meaningless */ 299 + usb_resume(dev, PMSG_ON); /* FIXME: change to PMSG_COMPLETE */ 280 300 } 281 301 282 302 static int usb_dev_suspend(struct device *dev) ··· 322 342 323 343 #else 324 344 325 - #define ksuspend_usb_init() 0 326 - #define ksuspend_usb_cleanup() do {} while (0) 327 - #define usb_device_pm_ops (*(struct dev_pm_ops *)0) 345 + #define usb_device_pm_ops (*(struct dev_pm_ops *) NULL) 328 346 329 347 #endif /* CONFIG_PM */ 330 348 ··· 450 472 INIT_LIST_HEAD(&dev->filelist); 451 473 452 474 #ifdef CONFIG_PM 453 - mutex_init(&dev->pm_mutex); 454 - INIT_DELAYED_WORK(&dev->autosuspend, usb_autosuspend_work); 455 - INIT_WORK(&dev->autoresume, usb_autoresume_work); 456 475 dev->autosuspend_delay = usb_autosuspend_delay * HZ; 457 476 dev->connect_time = jiffies; 458 477 dev->active_duration = -jiffies; ··· 1092 1117 if (retval) 1093 1118 goto out; 1094 1119 1095 - retval = ksuspend_usb_init(); 1096 - if (retval) 1097 - goto out; 1098 1120 retval = bus_register(&usb_bus_type); 1099 1121 if (retval) 1100 1122 goto bus_register_failed; ··· 1131 1159 bus_notifier_failed: 1132 1160 bus_unregister(&usb_bus_type); 1133 1161 bus_register_failed: 1134 - ksuspend_usb_cleanup(); 1162 + usb_debugfs_cleanup(); 1135 1163 out: 1136 1164 return retval; 1137 1165 } ··· 1153 1181 usb_hub_cleanup(); 1154 1182 bus_unregister_notifier(&usb_bus_type, &usb_bus_nb); 1155 1183 bus_unregister(&usb_bus_type); 1156 - ksuspend_usb_cleanup(); 1157 1184 usb_debugfs_cleanup(); 1158 1185 } 1159 1186
+6 -43
drivers/usb/core/usb.h
··· 55 55 extern int usb_suspend(struct device *dev, pm_message_t msg); 56 56 extern int usb_resume(struct device *dev, pm_message_t msg); 57 57 58 - extern void usb_autosuspend_work(struct work_struct *work); 59 - extern void usb_autoresume_work(struct work_struct *work); 60 58 extern int usb_port_suspend(struct usb_device *dev, pm_message_t msg); 61 59 extern int usb_port_resume(struct usb_device *dev, pm_message_t msg); 62 - extern int usb_external_suspend_device(struct usb_device *udev, 63 - pm_message_t msg); 64 - extern int usb_external_resume_device(struct usb_device *udev, 65 - pm_message_t msg); 66 - extern int usb_remote_wakeup(struct usb_device *dev); 67 - 68 - static inline void usb_pm_lock(struct usb_device *udev) 69 - { 70 - mutex_lock_nested(&udev->pm_mutex, udev->level); 71 - } 72 - 73 - static inline void usb_pm_unlock(struct usb_device *udev) 74 - { 75 - mutex_unlock(&udev->pm_mutex); 76 - } 77 60 78 61 #else 79 62 ··· 70 87 return 0; 71 88 } 72 89 73 - static inline int usb_remote_wakeup(struct usb_device *udev) 74 - { 75 - return 0; 76 - } 77 - 78 - static inline void usb_pm_lock(struct usb_device *udev) {} 79 - static inline void usb_pm_unlock(struct usb_device *udev) {} 80 - 81 90 #endif 82 91 83 92 #ifdef CONFIG_USB_SUSPEND ··· 77 102 extern void usb_autosuspend_device(struct usb_device *udev); 78 103 extern void usb_try_autosuspend_device(struct usb_device *udev); 79 104 extern int usb_autoresume_device(struct usb_device *udev); 105 + extern int usb_remote_wakeup(struct usb_device *dev); 80 106 81 107 #else 82 108 ··· 88 112 return 0; 89 113 } 90 114 115 + static inline int usb_remote_wakeup(struct usb_device *udev) 116 + { 117 + return 0; 118 + } 119 + 91 120 #endif 92 121 93 - extern struct workqueue_struct *ksuspend_usb_wq; 94 122 extern struct bus_type usb_bus_type; 95 123 extern struct device_type usb_device_type; 96 124 extern struct device_type usb_if_device_type; ··· 122 142 { 123 143 return container_of(drv, struct usbdrv_wrap, driver)-> 124 144 for_devices; 125 - } 126 - 127 - /* Interfaces and their "power state" are owned by usbcore */ 128 - 129 - static inline void mark_active(struct usb_interface *f) 130 - { 131 - f->is_active = 1; 132 - } 133 - 134 - static inline void mark_quiesced(struct usb_interface *f) 135 - { 136 - f->is_active = 0; 137 - } 138 - 139 - static inline int is_active(const struct usb_interface *f) 140 - { 141 - return f->is_active; 142 145 } 143 146 144 147
-4
drivers/usb/misc/usbtest.c
··· 1580 1580 return -ERESTARTSYS; 1581 1581 1582 1582 /* FIXME: What if a system sleep starts while a test is running? */ 1583 - if (!intf->is_active) { 1584 - mutex_unlock(&dev->lock); 1585 - return -EHOSTUNREACH; 1586 - } 1587 1583 1588 1584 /* some devices, like ez-usb default devices, need a non-default 1589 1585 * altsetting to have any active endpoints. some tests change
+3 -28
include/linux/usb.h
··· 122 122 * number from the USB core by calling usb_register_dev(). 123 123 * @condition: binding state of the interface: not bound, binding 124 124 * (in probe()), bound to a driver, or unbinding (in disconnect()) 125 - * @is_active: flag set when the interface is bound and not suspended. 126 125 * @sysfs_files_created: sysfs attributes exist 127 126 * @ep_devs_created: endpoint child pseudo-devices exist 128 127 * @unregistering: flag set when the interface is being unregistered ··· 134 135 * @dev: driver model's view of this device 135 136 * @usb_dev: if an interface is bound to the USB major, this will point 136 137 * to the sysfs representation for that device. 137 - * @pm_usage_cnt: PM usage counter for this interface; autosuspend is not 138 - * allowed unless the counter is 0. 138 + * @pm_usage_cnt: PM usage counter for this interface 139 139 * @reset_ws: Used for scheduling resets from atomic context. 140 140 * @reset_running: set to 1 if the interface is currently running a 141 141 * queued reset so that usb_cancel_queued_reset() doesn't try to ··· 182 184 int minor; /* minor number this interface is 183 185 * bound to */ 184 186 enum usb_interface_condition condition; /* state of binding */ 185 - unsigned is_active:1; /* the interface is not suspended */ 186 187 unsigned sysfs_files_created:1; /* the sysfs attributes exist */ 187 188 unsigned ep_devs_created:1; /* endpoint "devices" exist */ 188 189 unsigned unregistering:1; /* unregistration is in progress */ ··· 398 401 * @portnum: parent port number (origin 1) 399 402 * @level: number of USB hub ancestors 400 403 * @can_submit: URBs may be submitted 401 - * @discon_suspended: disconnected while suspended 402 404 * @persist_enabled: USB_PERSIST enabled for this device 403 405 * @have_langid: whether string_langid is valid 404 406 * @authorized: policy has said we can use it; ··· 417 421 * @usbfs_dentry: usbfs dentry entry for the device 418 422 * @maxchild: number of ports if hub 419 423 * @children: child devices - USB devices that are attached to this hub 420 - * @pm_usage_cnt: usage counter for autosuspend 421 424 * @quirks: quirks of the whole device 422 425 * @urbnum: number of URBs submitted for the whole device 423 426 * @active_duration: total time device is not suspended 424 - * @autosuspend: for delayed autosuspends 425 - * @autoresume: for autoresumes requested while in_interrupt 426 - * @pm_mutex: protects PM operations 427 427 * @last_busy: time of last use 428 428 * @autosuspend_delay: in jiffies 429 429 * @connect_time: time device was first connected 430 430 * @do_remote_wakeup: remote wakeup should be enabled 431 431 * @reset_resume: needs reset instead of resume 432 432 * @autosuspend_disabled: autosuspend disabled by the user 433 - * @skip_sys_resume: skip the next system resume 434 433 * @wusb_dev: if this is a Wireless USB device, link to the WUSB 435 434 * specific data for the device. 436 435 * @slot_id: Slot ID assigned by xHCI ··· 466 475 u8 level; 467 476 468 477 unsigned can_submit:1; 469 - unsigned discon_suspended:1; 470 478 unsigned persist_enabled:1; 471 479 unsigned have_langid:1; 472 480 unsigned authorized:1; ··· 489 499 int maxchild; 490 500 struct usb_device *children[USB_MAXCHILDREN]; 491 501 492 - int pm_usage_cnt; 493 502 u32 quirks; 494 503 atomic_t urbnum; 495 504 496 505 unsigned long active_duration; 497 506 498 507 #ifdef CONFIG_PM 499 - struct delayed_work autosuspend; 500 - struct work_struct autoresume; 501 - struct mutex pm_mutex; 502 - 503 508 unsigned long last_busy; 504 509 int autosuspend_delay; 505 510 unsigned long connect_time; ··· 502 517 unsigned do_remote_wakeup:1; 503 518 unsigned reset_resume:1; 504 519 unsigned autosuspend_disabled:1; 505 - unsigned skip_sys_resume:1; 506 520 #endif 507 521 struct wusb_dev *wusb_dev; 508 522 int slot_id; ··· 533 549 extern void usb_autopm_put_interface(struct usb_interface *intf); 534 550 extern int usb_autopm_get_interface_async(struct usb_interface *intf); 535 551 extern void usb_autopm_put_interface_async(struct usb_interface *intf); 536 - 537 - static inline void usb_autopm_get_interface_no_resume( 538 - struct usb_interface *intf) 539 - { 540 - atomic_inc(&intf->pm_usage_cnt); 541 - } 542 - static inline void usb_autopm_put_interface_no_suspend( 543 - struct usb_interface *intf) 544 - { 545 - atomic_dec(&intf->pm_usage_cnt); 546 - } 552 + extern void usb_autopm_get_interface_no_resume(struct usb_interface *intf); 553 + extern void usb_autopm_put_interface_no_suspend(struct usb_interface *intf); 547 554 548 555 static inline void usb_mark_last_busy(struct usb_device *udev) 549 556 {