aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/usb/core/driver.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/usb/core/driver.c')
-rw-r--r--drivers/usb/core/driver.c845
1 files changed, 368 insertions, 477 deletions
diff --git a/drivers/usb/core/driver.c b/drivers/usb/core/driver.c
index 638d54693a1c..6850ec6576f8 100644
--- a/drivers/usb/core/driver.c
+++ b/drivers/usb/core/driver.c
@@ -25,7 +25,7 @@
25#include <linux/device.h> 25#include <linux/device.h>
26#include <linux/usb.h> 26#include <linux/usb.h>
27#include <linux/usb/quirks.h> 27#include <linux/usb/quirks.h>
28#include <linux/workqueue.h> 28#include <linux/pm_runtime.h>
29#include "hcd.h" 29#include "hcd.h"
30#include "usb.h" 30#include "usb.h"
31 31
@@ -221,7 +221,7 @@ static int usb_probe_device(struct device *dev)
221{ 221{
222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 222 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver);
223 struct usb_device *udev = to_usb_device(dev); 223 struct usb_device *udev = to_usb_device(dev);
224 int error = -ENODEV; 224 int error = 0;
225 225
226 dev_dbg(dev, "%s\n", __func__); 226 dev_dbg(dev, "%s\n", __func__);
227 227
@@ -230,18 +230,23 @@ static int usb_probe_device(struct device *dev)
230 /* The device should always appear to be in use 230 /* The device should always appear to be in use
231 * unless the driver suports autosuspend. 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 return error; 238 return error;
237} 239}
238 240
239/* called from driver core with dev locked */ 241/* called from driver core with dev locked */
240static int usb_unbind_device(struct device *dev) 242static int usb_unbind_device(struct device *dev)
241{ 243{
244 struct usb_device *udev = to_usb_device(dev);
242 struct usb_device_driver *udriver = to_usb_device_driver(dev->driver); 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 return 0; 250 return 0;
246} 251}
247 252
@@ -293,17 +298,16 @@ static int usb_probe_interface(struct device *dev)
293 if (error) 298 if (error)
294 return error; 299 return error;
295 300
296 /* Interface "power state" doesn't correspond to any hardware
297 * state whatsoever. We use it to record when it's bound to
298 * a driver that may start I/0: it's not frozen/quiesced.
299 */
300 mark_active(intf);
301 intf->condition = USB_INTERFACE_BINDING; 301 intf->condition = USB_INTERFACE_BINDING;
302 302
303 /* The interface should always appear to be in use 303 /* Bound interfaces are initially active. They are
304 * unless the driver suports autosuspend. 304 * runtime-PM-enabled only if the driver has autosuspend support.
305 * They are sensitive to their children's power states.
305 */ 306 */
306 atomic_set(&intf->pm_usage_cnt, !driver->supports_autosuspend); 307 pm_runtime_set_active(dev);
308 pm_suspend_ignore_children(dev, false);
309 if (driver->supports_autosuspend)
310 pm_runtime_enable(dev);
307 311
308 /* Carry out a deferred switch to altsetting 0 */ 312 /* Carry out a deferred switch to altsetting 0 */
309 if (intf->needs_altsetting0) { 313 if (intf->needs_altsetting0) {
@@ -323,10 +327,14 @@ static int usb_probe_interface(struct device *dev)
323 return error; 327 return error;
324 328
325 err: 329 err:
326 mark_quiesced(intf);
327 intf->needs_remote_wakeup = 0; 330 intf->needs_remote_wakeup = 0;
328 intf->condition = USB_INTERFACE_UNBOUND; 331 intf->condition = USB_INTERFACE_UNBOUND;
329 usb_cancel_queued_reset(intf); 332 usb_cancel_queued_reset(intf);
333
334 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
335 pm_runtime_disable(dev);
336 pm_runtime_set_suspended(dev);
337
330 usb_autosuspend_device(udev); 338 usb_autosuspend_device(udev);
331 return error; 339 return error;
332} 340}
@@ -376,9 +384,17 @@ static int usb_unbind_interface(struct device *dev)
376 usb_set_intfdata(intf, NULL); 384 usb_set_intfdata(intf, NULL);
377 385
378 intf->condition = USB_INTERFACE_UNBOUND; 386 intf->condition = USB_INTERFACE_UNBOUND;
379 mark_quiesced(intf);
380 intf->needs_remote_wakeup = 0; 387 intf->needs_remote_wakeup = 0;
381 388
389 /* Unbound interfaces are always runtime-PM-disabled and -suspended */
390 pm_runtime_disable(dev);
391 pm_runtime_set_suspended(dev);
392
393 /* Undo any residual pm_autopm_get_interface_* calls */
394 for (r = atomic_read(&intf->pm_usage_cnt); r > 0; --r)
395 usb_autopm_put_interface_no_suspend(intf);
396 atomic_set(&intf->pm_usage_cnt, 0);
397
382 if (!error) 398 if (!error)
383 usb_autosuspend_device(udev); 399 usb_autosuspend_device(udev);
384 400
@@ -409,7 +425,6 @@ int usb_driver_claim_interface(struct usb_driver *driver,
409 struct usb_interface *iface, void *priv) 425 struct usb_interface *iface, void *priv)
410{ 426{
411 struct device *dev = &iface->dev; 427 struct device *dev = &iface->dev;
412 struct usb_device *udev = interface_to_usbdev(iface);
413 int retval = 0; 428 int retval = 0;
414 429
415 if (dev->driver) 430 if (dev->driver)
@@ -419,11 +434,16 @@ int usb_driver_claim_interface(struct usb_driver *driver,
419 usb_set_intfdata(iface, priv); 434 usb_set_intfdata(iface, priv);
420 iface->needs_binding = 0; 435 iface->needs_binding = 0;
421 436
422 usb_pm_lock(udev);
423 iface->condition = USB_INTERFACE_BOUND; 437 iface->condition = USB_INTERFACE_BOUND;
424 mark_active(iface); 438
425 atomic_set(&iface->pm_usage_cnt, !driver->supports_autosuspend); 439 /* Bound interfaces are initially active. They are
426 usb_pm_unlock(udev); 440 * runtime-PM-enabled only if the driver has autosuspend support.
441 * They are sensitive to their children's power states.
442 */
443 pm_runtime_set_active(dev);
444 pm_suspend_ignore_children(dev, false);
445 if (driver->supports_autosuspend)
446 pm_runtime_enable(dev);
427 447
428 /* if interface was already added, bind now; else let 448 /* if interface was already added, bind now; else let
429 * the future device_add() bind it, bypassing probe() 449 * the future device_add() bind it, bypassing probe()
@@ -982,7 +1002,6 @@ static void do_unbind_rebind(struct usb_device *udev, int action)
982 } 1002 }
983} 1003}
984 1004
985/* Caller has locked udev's pm_mutex */
986static int usb_suspend_device(struct usb_device *udev, pm_message_t msg) 1005static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
987{ 1006{
988 struct usb_device_driver *udriver; 1007 struct usb_device_driver *udriver;
@@ -1006,7 +1025,6 @@ static int usb_suspend_device(struct usb_device *udev, pm_message_t msg)
1006 return status; 1025 return status;
1007} 1026}
1008 1027
1009/* Caller has locked udev's pm_mutex */
1010static int usb_resume_device(struct usb_device *udev, pm_message_t msg) 1028static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1011{ 1029{
1012 struct usb_device_driver *udriver; 1030 struct usb_device_driver *udriver;
@@ -1040,27 +1058,20 @@ static int usb_resume_device(struct usb_device *udev, pm_message_t msg)
1040 return status; 1058 return status;
1041} 1059}
1042 1060
1043/* Caller has locked intf's usb_device's pm mutex */
1044static int usb_suspend_interface(struct usb_device *udev, 1061static int usb_suspend_interface(struct usb_device *udev,
1045 struct usb_interface *intf, pm_message_t msg) 1062 struct usb_interface *intf, pm_message_t msg)
1046{ 1063{
1047 struct usb_driver *driver; 1064 struct usb_driver *driver;
1048 int status = 0; 1065 int status = 0;
1049 1066
1050 /* with no hardware, USB interfaces only use FREEZE and ON states */ 1067 if (udev->state == USB_STATE_NOTATTACHED ||
1051 if (udev->state == USB_STATE_NOTATTACHED || !is_active(intf)) 1068 intf->condition == USB_INTERFACE_UNBOUND)
1052 goto done;
1053
1054 /* This can happen; see usb_driver_release_interface() */
1055 if (intf->condition == USB_INTERFACE_UNBOUND)
1056 goto done; 1069 goto done;
1057 driver = to_usb_driver(intf->dev.driver); 1070 driver = to_usb_driver(intf->dev.driver);
1058 1071
1059 if (driver->suspend) { 1072 if (driver->suspend) {
1060 status = driver->suspend(intf, msg); 1073 status = driver->suspend(intf, msg);
1061 if (status == 0) 1074 if (status && !(msg.event & PM_EVENT_AUTO))
1062 mark_quiesced(intf);
1063 else if (!(msg.event & PM_EVENT_AUTO))
1064 dev_err(&intf->dev, "%s error %d\n", 1075 dev_err(&intf->dev, "%s error %d\n",
1065 "suspend", status); 1076 "suspend", status);
1066 } else { 1077 } else {
@@ -1068,7 +1079,6 @@ static int usb_suspend_interface(struct usb_device *udev,
1068 intf->needs_binding = 1; 1079 intf->needs_binding = 1;
1069 dev_warn(&intf->dev, "no %s for driver %s?\n", 1080 dev_warn(&intf->dev, "no %s for driver %s?\n",
1070 "suspend", driver->name); 1081 "suspend", driver->name);
1071 mark_quiesced(intf);
1072 } 1082 }
1073 1083
1074 done: 1084 done:
@@ -1076,14 +1086,13 @@ static int usb_suspend_interface(struct usb_device *udev,
1076 return status; 1086 return status;
1077} 1087}
1078 1088
1079/* Caller has locked intf's usb_device's pm_mutex */
1080static int usb_resume_interface(struct usb_device *udev, 1089static int usb_resume_interface(struct usb_device *udev,
1081 struct usb_interface *intf, pm_message_t msg, int reset_resume) 1090 struct usb_interface *intf, pm_message_t msg, int reset_resume)
1082{ 1091{
1083 struct usb_driver *driver; 1092 struct usb_driver *driver;
1084 int status = 0; 1093 int status = 0;
1085 1094
1086 if (udev->state == USB_STATE_NOTATTACHED || is_active(intf)) 1095 if (udev->state == USB_STATE_NOTATTACHED)
1087 goto done; 1096 goto done;
1088 1097
1089 /* Don't let autoresume interfere with unbinding */ 1098 /* Don't let autoresume interfere with unbinding */
@@ -1134,90 +1143,11 @@ static int usb_resume_interface(struct usb_device *udev,
1134 1143
1135done: 1144done:
1136 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status); 1145 dev_vdbg(&intf->dev, "%s: status %d\n", __func__, status);
1137 if (status == 0 && intf->condition == USB_INTERFACE_BOUND)
1138 mark_active(intf);
1139 1146
1140 /* Later we will unbind the driver and/or reprobe, if necessary */ 1147 /* Later we will unbind the driver and/or reprobe, if necessary */
1141 return status; 1148 return status;
1142} 1149}
1143 1150
1144#ifdef CONFIG_USB_SUSPEND
1145
1146/* Internal routine to check whether we may autosuspend a device. */
1147static int autosuspend_check(struct usb_device *udev, int reschedule)
1148{
1149 int i;
1150 struct usb_interface *intf;
1151 unsigned long suspend_time, j;
1152
1153 /* For autosuspend, fail fast if anything is in use or autosuspend
1154 * is disabled. Also fail if any interfaces require remote wakeup
1155 * but it isn't available.
1156 */
1157 if (udev->pm_usage_cnt > 0)
1158 return -EBUSY;
1159 if (udev->autosuspend_delay < 0 || udev->autosuspend_disabled)
1160 return -EPERM;
1161
1162 suspend_time = udev->last_busy + udev->autosuspend_delay;
1163 if (udev->actconfig) {
1164 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1165 intf = udev->actconfig->interface[i];
1166 if (!is_active(intf))
1167 continue;
1168 if (atomic_read(&intf->pm_usage_cnt) > 0)
1169 return -EBUSY;
1170 if (intf->needs_remote_wakeup &&
1171 !udev->do_remote_wakeup) {
1172 dev_dbg(&udev->dev, "remote wakeup needed "
1173 "for autosuspend\n");
1174 return -EOPNOTSUPP;
1175 }
1176
1177 /* Don't allow autosuspend if the device will need
1178 * a reset-resume and any of its interface drivers
1179 * doesn't include support.
1180 */
1181 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1182 struct usb_driver *driver;
1183
1184 driver = to_usb_driver(intf->dev.driver);
1185 if (!driver->reset_resume ||
1186 intf->needs_remote_wakeup)
1187 return -EOPNOTSUPP;
1188 }
1189 }
1190 }
1191
1192 /* If everything is okay but the device hasn't been idle for long
1193 * enough, queue a delayed autosuspend request. If the device
1194 * _has_ been idle for long enough and the reschedule flag is set,
1195 * likewise queue a delayed (1 second) autosuspend request.
1196 */
1197 j = jiffies;
1198 if (time_before(j, suspend_time))
1199 reschedule = 1;
1200 else
1201 suspend_time = j + HZ;
1202 if (reschedule) {
1203 if (!timer_pending(&udev->autosuspend.timer)) {
1204 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend,
1205 round_jiffies_up_relative(suspend_time - j));
1206 }
1207 return -EAGAIN;
1208 }
1209 return 0;
1210}
1211
1212#else
1213
1214static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1215{
1216 return 0;
1217}
1218
1219#endif /* CONFIG_USB_SUSPEND */
1220
1221/** 1151/**
1222 * usb_suspend_both - suspend a USB device and its interfaces 1152 * usb_suspend_both - suspend a USB device and its interfaces
1223 * @udev: the usb_device to suspend 1153 * @udev: the usb_device to suspend
@@ -1229,27 +1159,12 @@ static inline int autosuspend_check(struct usb_device *udev, int reschedule)
1229 * all the interfaces which were suspended are resumed so that they remain 1159 * all the interfaces which were suspended are resumed so that they remain
1230 * in the same state as the device. 1160 * in the same state as the device.
1231 * 1161 *
1232 * If an autosuspend is in progress the routine checks first to make sure 1162 * Autosuspend requests originating from a child device or an interface
1233 * that neither the device itself or any of its active interfaces is in use 1163 * driver may be made without the protection of @udev's device lock, but
1234 * (pm_usage_cnt is greater than 0). If they are, the autosuspend fails. 1164 * all other suspend calls will hold the lock. Usbcore will insure that
1235 * 1165 * method calls do not arrive during bind, unbind, or reset operations.
1236 * If the suspend succeeds, the routine recursively queues an autosuspend 1166 * However drivers must be prepared to handle suspend calls arriving at
1237 * request for @udev's parent device, thereby propagating the change up 1167 * unpredictable times.
1238 * the device tree. If all of the parent's children are now suspended,
1239 * the parent will autosuspend in turn.
1240 *
1241 * The suspend method calls are subject to mutual exclusion under control
1242 * of @udev's pm_mutex. Many of these calls are also under the protection
1243 * of @udev's device lock (including all requests originating outside the
1244 * USB subsystem), but autosuspend requests generated by a child device or
1245 * interface driver may not be. Usbcore will insure that the method calls
1246 * do not arrive during bind, unbind, or reset operations. However, drivers
1247 * must be prepared to handle suspend calls arriving at unpredictable times.
1248 * The only way to block such calls is to do an autoresume (preventing
1249 * autosuspends) while holding @udev's device lock (preventing outside
1250 * suspends).
1251 *
1252 * The caller must hold @udev->pm_mutex.
1253 * 1168 *
1254 * This routine can run only in process context. 1169 * This routine can run only in process context.
1255 */ 1170 */
@@ -1258,20 +1173,11 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1258 int status = 0; 1173 int status = 0;
1259 int i = 0; 1174 int i = 0;
1260 struct usb_interface *intf; 1175 struct usb_interface *intf;
1261 struct usb_device *parent = udev->parent;
1262 1176
1263 if (udev->state == USB_STATE_NOTATTACHED || 1177 if (udev->state == USB_STATE_NOTATTACHED ||
1264 udev->state == USB_STATE_SUSPENDED) 1178 udev->state == USB_STATE_SUSPENDED)
1265 goto done; 1179 goto done;
1266 1180
1267 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1268
1269 if (msg.event & PM_EVENT_AUTO) {
1270 status = autosuspend_check(udev, 0);
1271 if (status < 0)
1272 goto done;
1273 }
1274
1275 /* Suspend all the interfaces and then udev itself */ 1181 /* Suspend all the interfaces and then udev itself */
1276 if (udev->actconfig) { 1182 if (udev->actconfig) {
1277 for (; i < udev->actconfig->desc.bNumInterfaces; i++) { 1183 for (; i < udev->actconfig->desc.bNumInterfaces; i++) {
@@ -1286,35 +1192,21 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1286 1192
1287 /* If the suspend failed, resume interfaces that did get suspended */ 1193 /* If the suspend failed, resume interfaces that did get suspended */
1288 if (status != 0) { 1194 if (status != 0) {
1289 pm_message_t msg2; 1195 msg.event ^= (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1290
1291 msg2.event = msg.event ^ (PM_EVENT_SUSPEND | PM_EVENT_RESUME);
1292 while (--i >= 0) { 1196 while (--i >= 0) {
1293 intf = udev->actconfig->interface[i]; 1197 intf = udev->actconfig->interface[i];
1294 usb_resume_interface(udev, intf, msg2, 0); 1198 usb_resume_interface(udev, intf, msg, 0);
1295 } 1199 }
1296 1200
1297 /* Try another autosuspend when the interfaces aren't busy */ 1201 /* If the suspend succeeded then prevent any more URB submissions
1298 if (msg.event & PM_EVENT_AUTO) 1202 * and flush any outstanding URBs.
1299 autosuspend_check(udev, status == -EBUSY);
1300
1301 /* If the suspend succeeded then prevent any more URB submissions,
1302 * flush any outstanding URBs, and propagate the suspend up the tree.
1303 */ 1203 */
1304 } else { 1204 } else {
1305 cancel_delayed_work(&udev->autosuspend);
1306 udev->can_submit = 0; 1205 udev->can_submit = 0;
1307 for (i = 0; i < 16; ++i) { 1206 for (i = 0; i < 16; ++i) {
1308 usb_hcd_flush_endpoint(udev, udev->ep_out[i]); 1207 usb_hcd_flush_endpoint(udev, udev->ep_out[i]);
1309 usb_hcd_flush_endpoint(udev, udev->ep_in[i]); 1208 usb_hcd_flush_endpoint(udev, udev->ep_in[i]);
1310 } 1209 }
1311
1312 /* If this is just a FREEZE or a PRETHAW, udev might
1313 * not really be suspended. Only true suspends get
1314 * propagated up the device tree.
1315 */
1316 if (parent && udev->state == USB_STATE_SUSPENDED)
1317 usb_autosuspend_device(parent);
1318 } 1210 }
1319 1211
1320 done: 1212 done:
@@ -1331,23 +1223,12 @@ static int usb_suspend_both(struct usb_device *udev, pm_message_t msg)
1331 * the resume method for @udev and then calls the resume methods for all 1223 * the resume method for @udev and then calls the resume methods for all
1332 * the interface drivers in @udev. 1224 * the interface drivers in @udev.
1333 * 1225 *
1334 * Before starting the resume, the routine calls itself recursively for 1226 * Autoresume requests originating from a child device or an interface
1335 * the parent device of @udev, thereby propagating the change up the device 1227 * driver may be made without the protection of @udev's device lock, but
1336 * tree and assuring that @udev will be able to resume. If the parent is 1228 * all other resume calls will hold the lock. Usbcore will insure that
1337 * unable to resume successfully, the routine fails. 1229 * method calls do not arrive during bind, unbind, or reset operations.
1338 * 1230 * However drivers must be prepared to handle resume calls arriving at
1339 * The resume method calls are subject to mutual exclusion under control 1231 * unpredictable times.
1340 * of @udev's pm_mutex. Many of these calls are also under the protection
1341 * of @udev's device lock (including all requests originating outside the
1342 * USB subsystem), but autoresume requests generated by a child device or
1343 * interface driver may not be. Usbcore will insure that the method calls
1344 * do not arrive during bind, unbind, or reset operations. However, drivers
1345 * must be prepared to handle resume calls arriving at unpredictable times.
1346 * The only way to block such calls is to do an autoresume (preventing
1347 * other autoresumes) while holding @udev's device lock (preventing outside
1348 * resumes).
1349 *
1350 * The caller must hold @udev->pm_mutex.
1351 * 1232 *
1352 * This routine can run only in process context. 1233 * This routine can run only in process context.
1353 */ 1234 */
@@ -1356,48 +1237,18 @@ static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1356 int status = 0; 1237 int status = 0;
1357 int i; 1238 int i;
1358 struct usb_interface *intf; 1239 struct usb_interface *intf;
1359 struct usb_device *parent = udev->parent;
1360 1240
1361 cancel_delayed_work(&udev->autosuspend);
1362 if (udev->state == USB_STATE_NOTATTACHED) { 1241 if (udev->state == USB_STATE_NOTATTACHED) {
1363 status = -ENODEV; 1242 status = -ENODEV;
1364 goto done; 1243 goto done;
1365 } 1244 }
1366 udev->can_submit = 1; 1245 udev->can_submit = 1;
1367 1246
1368 /* Propagate the resume up the tree, if necessary */ 1247 /* Resume the device */
1369 if (udev->state == USB_STATE_SUSPENDED) { 1248 if (udev->state == USB_STATE_SUSPENDED || udev->reset_resume)
1370 if (parent) {
1371 status = usb_autoresume_device(parent);
1372 if (status == 0) {
1373 status = usb_resume_device(udev, msg);
1374 if (status || udev->state ==
1375 USB_STATE_NOTATTACHED) {
1376 usb_autosuspend_device(parent);
1377
1378 /* It's possible usb_resume_device()
1379 * failed after the port was
1380 * unsuspended, causing udev to be
1381 * logically disconnected. We don't
1382 * want usb_disconnect() to autosuspend
1383 * the parent again, so tell it that
1384 * udev disconnected while still
1385 * suspended. */
1386 if (udev->state ==
1387 USB_STATE_NOTATTACHED)
1388 udev->discon_suspended = 1;
1389 }
1390 }
1391 } else {
1392
1393 /* We can't progagate beyond the USB subsystem,
1394 * so if a root hub's controller is suspended
1395 * then we're stuck. */
1396 status = usb_resume_device(udev, msg);
1397 }
1398 } else if (udev->reset_resume)
1399 status = usb_resume_device(udev, msg); 1249 status = usb_resume_device(udev, msg);
1400 1250
1251 /* Resume the interfaces */
1401 if (status == 0 && udev->actconfig) { 1252 if (status == 0 && udev->actconfig) {
1402 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) { 1253 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1403 intf = udev->actconfig->interface[i]; 1254 intf = udev->actconfig->interface[i];
@@ -1413,104 +1264,46 @@ static int usb_resume_both(struct usb_device *udev, pm_message_t msg)
1413 return status; 1264 return status;
1414} 1265}
1415 1266
1416/** 1267/* The device lock is held by the PM core */
1417 * usb_external_suspend_device - external suspend of a USB device and its interfaces
1418 * @udev: the usb_device to suspend
1419 * @msg: Power Management message describing this state transition
1420 *
1421 * This routine handles external suspend requests: ones not generated
1422 * internally by a USB driver (autosuspend) but rather coming from the user
1423 * (via sysfs) or the PM core (system sleep). The suspend will be carried
1424 * out regardless of @udev's usage counter or those of its interfaces,
1425 * and regardless of whether or not remote wakeup is enabled. Of course,
1426 * interface drivers still have the option of failing the suspend (if
1427 * there are unsuspended children, for example).
1428 *
1429 * The caller must hold @udev's device lock.
1430 */
1431int usb_external_suspend_device(struct usb_device *udev, pm_message_t msg)
1432{
1433 int status;
1434
1435 do_unbind_rebind(udev, DO_UNBIND);
1436 usb_pm_lock(udev);
1437 status = usb_suspend_both(udev, msg);
1438 usb_pm_unlock(udev);
1439 return status;
1440}
1441
1442/**
1443 * usb_external_resume_device - external resume of a USB device and its interfaces
1444 * @udev: the usb_device to resume
1445 * @msg: Power Management message describing this state transition
1446 *
1447 * This routine handles external resume requests: ones not generated
1448 * internally by a USB driver (autoresume) but rather coming from the user
1449 * (via sysfs), the PM core (system resume), or the device itself (remote
1450 * wakeup). @udev's usage counter is unaffected.
1451 *
1452 * The caller must hold @udev's device lock.
1453 */
1454int usb_external_resume_device(struct usb_device *udev, pm_message_t msg)
1455{
1456 int status;
1457
1458 usb_pm_lock(udev);
1459 status = usb_resume_both(udev, msg);
1460 udev->last_busy = jiffies;
1461 usb_pm_unlock(udev);
1462 if (status == 0)
1463 do_unbind_rebind(udev, DO_REBIND);
1464
1465 /* Now that the device is awake, we can start trying to autosuspend
1466 * it again. */
1467 if (status == 0)
1468 usb_try_autosuspend_device(udev);
1469 return status;
1470}
1471
1472int usb_suspend(struct device *dev, pm_message_t msg) 1268int usb_suspend(struct device *dev, pm_message_t msg)
1473{ 1269{
1474 struct usb_device *udev; 1270 struct usb_device *udev = to_usb_device(dev);
1475
1476 udev = to_usb_device(dev);
1477 1271
1478 /* If udev is already suspended, we can skip this suspend and 1272 do_unbind_rebind(udev, DO_UNBIND);
1479 * we should also skip the upcoming system resume. High-speed 1273 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1480 * root hubs are an exception; they need to resume whenever the 1274 return usb_suspend_both(udev, msg);
1481 * system wakes up in order for USB-PERSIST port handover to work
1482 * properly.
1483 */
1484 if (udev->state == USB_STATE_SUSPENDED) {
1485 if (udev->parent || udev->speed != USB_SPEED_HIGH)
1486 udev->skip_sys_resume = 1;
1487 return 0;
1488 }
1489
1490 udev->skip_sys_resume = 0;
1491 return usb_external_suspend_device(udev, msg);
1492} 1275}
1493 1276
1277/* The device lock is held by the PM core */
1494int usb_resume(struct device *dev, pm_message_t msg) 1278int usb_resume(struct device *dev, pm_message_t msg)
1495{ 1279{
1496 struct usb_device *udev; 1280 struct usb_device *udev = to_usb_device(dev);
1497 int status; 1281 int status;
1498 1282
1499 udev = to_usb_device(dev); 1283 /* For PM complete calls, all we do is rebind interfaces */
1284 if (msg.event == PM_EVENT_ON) {
1285 if (udev->state != USB_STATE_NOTATTACHED)
1286 do_unbind_rebind(udev, DO_REBIND);
1287 status = 0;
1500 1288
1501 /* If udev->skip_sys_resume is set then udev was already suspended 1289 /* For all other calls, take the device back to full power and
1502 * when the system sleep started, so we don't want to resume it 1290 * tell the PM core in case it was autosuspended previously.
1503 * during this system wakeup.
1504 */ 1291 */
1505 if (udev->skip_sys_resume) 1292 } else {
1506 return 0; 1293 status = usb_resume_both(udev, msg);
1507 status = usb_external_resume_device(udev, msg); 1294 if (status == 0) {
1295 pm_runtime_disable(dev);
1296 pm_runtime_set_active(dev);
1297 pm_runtime_enable(dev);
1298 udev->last_busy = jiffies;
1299 }
1300 }
1508 1301
1509 /* Avoid PM error messages for devices disconnected while suspended 1302 /* Avoid PM error messages for devices disconnected while suspended
1510 * as we'll display regular disconnect messages just a bit later. 1303 * as we'll display regular disconnect messages just a bit later.
1511 */ 1304 */
1512 if (status == -ENODEV) 1305 if (status == -ENODEV)
1513 return 0; 1306 status = 0;
1514 return status; 1307 return status;
1515} 1308}
1516 1309
@@ -1560,54 +1353,6 @@ int usb_disable_autosuspend(struct usb_device *udev)
1560} 1353}
1561EXPORT_SYMBOL_GPL(usb_disable_autosuspend); 1354EXPORT_SYMBOL_GPL(usb_disable_autosuspend);
1562 1355
1563/* Internal routine to adjust a device's usage counter and change
1564 * its autosuspend state.
1565 */
1566static int usb_autopm_do_device(struct usb_device *udev, int inc_usage_cnt)
1567{
1568 int status = 0;
1569
1570 usb_pm_lock(udev);
1571 udev->pm_usage_cnt += inc_usage_cnt;
1572 WARN_ON(udev->pm_usage_cnt < 0);
1573 if (inc_usage_cnt)
1574 udev->last_busy = jiffies;
1575 if (inc_usage_cnt >= 0 && udev->pm_usage_cnt > 0) {
1576 if (udev->state == USB_STATE_SUSPENDED)
1577 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1578 if (status != 0)
1579 udev->pm_usage_cnt -= inc_usage_cnt;
1580 else if (inc_usage_cnt)
1581 udev->last_busy = jiffies;
1582 } else if (inc_usage_cnt <= 0 && udev->pm_usage_cnt <= 0) {
1583 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1584 }
1585 usb_pm_unlock(udev);
1586 return status;
1587}
1588
1589/* usb_autosuspend_work - callback routine to autosuspend a USB device */
1590void usb_autosuspend_work(struct work_struct *work)
1591{
1592 struct usb_device *udev =
1593 container_of(work, struct usb_device, autosuspend.work);
1594
1595 usb_autopm_do_device(udev, 0);
1596}
1597
1598/* usb_autoresume_work - callback routine to autoresume a USB device */
1599void usb_autoresume_work(struct work_struct *work)
1600{
1601 struct usb_device *udev =
1602 container_of(work, struct usb_device, autoresume);
1603
1604 /* Wake it up, let the drivers do their thing, and then put it
1605 * back to sleep.
1606 */
1607 if (usb_autopm_do_device(udev, 1) == 0)
1608 usb_autopm_do_device(udev, -1);
1609}
1610
1611/** 1356/**
1612 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces 1357 * usb_autosuspend_device - delayed autosuspend of a USB device and its interfaces
1613 * @udev: the usb_device to autosuspend 1358 * @udev: the usb_device to autosuspend
@@ -1616,12 +1361,9 @@ void usb_autoresume_work(struct work_struct *work)
1616 * @udev and wants to allow it to autosuspend. Examples would be when 1361 * @udev and wants to allow it to autosuspend. Examples would be when
1617 * @udev's device file in usbfs is closed or after a configuration change. 1362 * @udev's device file in usbfs is closed or after a configuration change.
1618 * 1363 *
1619 * @udev's usage counter is decremented. If it or any of the usage counters 1364 * @udev's usage counter is decremented; if it drops to 0 and all the
1620 * for an active interface is greater than 0, no autosuspend request will be 1365 * interfaces are inactive then a delayed autosuspend will be attempted.
1621 * queued. (If an interface driver does not support autosuspend then its 1366 * The attempt may fail (see autosuspend_check()).
1622 * usage counter is permanently positive.) Furthermore, if an interface
1623 * driver requires remote-wakeup capability during autosuspend but remote
1624 * wakeup is disabled, the autosuspend will fail.
1625 * 1367 *
1626 * The caller must hold @udev's device lock. 1368 * The caller must hold @udev's device lock.
1627 * 1369 *
@@ -1631,9 +1373,11 @@ void usb_autosuspend_device(struct usb_device *udev)
1631{ 1373{
1632 int status; 1374 int status;
1633 1375
1634 status = usb_autopm_do_device(udev, -1); 1376 udev->last_busy = jiffies;
1635 dev_vdbg(&udev->dev, "%s: cnt %d\n", 1377 status = pm_runtime_put_sync(&udev->dev);
1636 __func__, udev->pm_usage_cnt); 1378 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1379 __func__, atomic_read(&udev->dev.power.usage_count),
1380 status);
1637} 1381}
1638 1382
1639/** 1383/**
@@ -1643,9 +1387,9 @@ void usb_autosuspend_device(struct usb_device *udev)
1643 * This routine should be called when a core subsystem thinks @udev may 1387 * This routine should be called when a core subsystem thinks @udev may
1644 * be ready to autosuspend. 1388 * be ready to autosuspend.
1645 * 1389 *
1646 * @udev's usage counter left unchanged. If it or any of the usage counters 1390 * @udev's usage counter left unchanged. If it is 0 and all the interfaces
1647 * for an active interface is greater than 0, or autosuspend is not allowed 1391 * are inactive then an autosuspend will be attempted. The attempt may
1648 * for any other reason, no autosuspend request will be queued. 1392 * fail or be delayed.
1649 * 1393 *
1650 * The caller must hold @udev's device lock. 1394 * The caller must hold @udev's device lock.
1651 * 1395 *
@@ -1653,9 +1397,12 @@ void usb_autosuspend_device(struct usb_device *udev)
1653 */ 1397 */
1654void usb_try_autosuspend_device(struct usb_device *udev) 1398void usb_try_autosuspend_device(struct usb_device *udev)
1655{ 1399{
1656 usb_autopm_do_device(udev, 0); 1400 int status;
1657 dev_vdbg(&udev->dev, "%s: cnt %d\n", 1401
1658 __func__, udev->pm_usage_cnt); 1402 status = pm_runtime_idle(&udev->dev);
1403 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1404 __func__, atomic_read(&udev->dev.power.usage_count),
1405 status);
1659} 1406}
1660 1407
1661/** 1408/**
@@ -1664,9 +1411,9 @@ void usb_try_autosuspend_device(struct usb_device *udev)
1664 * 1411 *
1665 * This routine should be called when a core subsystem wants to use @udev 1412 * This routine should be called when a core subsystem wants to use @udev
1666 * and needs to guarantee that it is not suspended. No autosuspend will 1413 * and needs to guarantee that it is not suspended. No autosuspend will
1667 * occur until usb_autosuspend_device is called. (Note that this will not 1414 * occur until usb_autosuspend_device() is called. (Note that this will
1668 * prevent suspend events originating in the PM core.) Examples would be 1415 * not prevent suspend events originating in the PM core.) Examples would
1669 * when @udev's device file in usbfs is opened or when a remote-wakeup 1416 * be when @udev's device file in usbfs is opened or when a remote-wakeup
1670 * request is received. 1417 * request is received.
1671 * 1418 *
1672 * @udev's usage counter is incremented to prevent subsequent autosuspends. 1419 * @udev's usage counter is incremented to prevent subsequent autosuspends.
@@ -1680,42 +1427,14 @@ int usb_autoresume_device(struct usb_device *udev)
1680{ 1427{
1681 int status; 1428 int status;
1682 1429
1683 status = usb_autopm_do_device(udev, 1); 1430 status = pm_runtime_get_sync(&udev->dev);
1684 dev_vdbg(&udev->dev, "%s: status %d cnt %d\n", 1431 if (status < 0)
1685 __func__, status, udev->pm_usage_cnt); 1432 pm_runtime_put_sync(&udev->dev);
1686 return status; 1433 dev_vdbg(&udev->dev, "%s: cnt %d -> %d\n",
1687} 1434 __func__, atomic_read(&udev->dev.power.usage_count),
1688 1435 status);
1689/* Internal routine to adjust an interface's usage counter and change 1436 if (status > 0)
1690 * its device's autosuspend state. 1437 status = 0;
1691 */
1692static int usb_autopm_do_interface(struct usb_interface *intf,
1693 int inc_usage_cnt)
1694{
1695 struct usb_device *udev = interface_to_usbdev(intf);
1696 int status = 0;
1697
1698 usb_pm_lock(udev);
1699 if (intf->condition == USB_INTERFACE_UNBOUND)
1700 status = -ENODEV;
1701 else {
1702 atomic_add(inc_usage_cnt, &intf->pm_usage_cnt);
1703 udev->last_busy = jiffies;
1704 if (inc_usage_cnt >= 0 &&
1705 atomic_read(&intf->pm_usage_cnt) > 0) {
1706 if (udev->state == USB_STATE_SUSPENDED)
1707 status = usb_resume_both(udev,
1708 PMSG_AUTO_RESUME);
1709 if (status != 0)
1710 atomic_sub(inc_usage_cnt, &intf->pm_usage_cnt);
1711 else
1712 udev->last_busy = jiffies;
1713 } else if (inc_usage_cnt <= 0 &&
1714 atomic_read(&intf->pm_usage_cnt) <= 0) {
1715 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1716 }
1717 }
1718 usb_pm_unlock(udev);
1719 return status; 1438 return status;
1720} 1439}
1721 1440
@@ -1729,34 +1448,25 @@ static int usb_autopm_do_interface(struct usb_interface *intf,
1729 * closed. 1448 * closed.
1730 * 1449 *
1731 * The routine decrements @intf's usage counter. When the counter reaches 1450 * The routine decrements @intf's usage counter. When the counter reaches
1732 * 0, a delayed autosuspend request for @intf's device is queued. When 1451 * 0, a delayed autosuspend request for @intf's device is attempted. The
1733 * the delay expires, if @intf->pm_usage_cnt is still <= 0 along with all 1452 * attempt may fail (see autosuspend_check()).
1734 * the other usage counters for the sibling interfaces and @intf's
1735 * usb_device, the device and all its interfaces will be autosuspended.
1736 *
1737 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1738 * core will not change its value other than the increment and decrement
1739 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1740 * may use this simple counter-oriented discipline or may set the value
1741 * any way it likes.
1742 * 1453 *
1743 * If the driver has set @intf->needs_remote_wakeup then autosuspend will 1454 * If the driver has set @intf->needs_remote_wakeup then autosuspend will
1744 * take place only if the device's remote-wakeup facility is enabled. 1455 * take place only if the device's remote-wakeup facility is enabled.
1745 * 1456 *
1746 * Suspend method calls queued by this routine can arrive at any time
1747 * while @intf is resumed and its usage counter is equal to 0. They are
1748 * not protected by the usb_device's lock but only by its pm_mutex.
1749 * Drivers must provide their own synchronization.
1750 *
1751 * This routine can run only in process context. 1457 * This routine can run only in process context.
1752 */ 1458 */
1753void usb_autopm_put_interface(struct usb_interface *intf) 1459void usb_autopm_put_interface(struct usb_interface *intf)
1754{ 1460{
1755 int status; 1461 struct usb_device *udev = interface_to_usbdev(intf);
1462 int status;
1756 1463
1757 status = usb_autopm_do_interface(intf, -1); 1464 udev->last_busy = jiffies;
1758 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1465 atomic_dec(&intf->pm_usage_cnt);
1759 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1466 status = pm_runtime_put_sync(&intf->dev);
1467 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1468 __func__, atomic_read(&intf->dev.power.usage_count),
1469 status);
1760} 1470}
1761EXPORT_SYMBOL_GPL(usb_autopm_put_interface); 1471EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1762 1472
@@ -1764,11 +1474,11 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1764 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter 1474 * usb_autopm_put_interface_async - decrement a USB interface's PM-usage counter
1765 * @intf: the usb_interface whose counter should be decremented 1475 * @intf: the usb_interface whose counter should be decremented
1766 * 1476 *
1767 * This routine does essentially the same thing as 1477 * This routine does much the same thing as usb_autopm_put_interface():
1768 * usb_autopm_put_interface(): it decrements @intf's usage counter and 1478 * It decrements @intf's usage counter and schedules a delayed
1769 * queues a delayed autosuspend request if the counter is <= 0. The 1479 * autosuspend request if the counter is <= 0. The difference is that it
1770 * difference is that it does not acquire the device's pm_mutex; 1480 * does not perform any synchronization; callers should hold a private
1771 * callers must handle all synchronization issues themselves. 1481 * lock and handle all synchronization issues themselves.
1772 * 1482 *
1773 * Typically a driver would call this routine during an URB's completion 1483 * Typically a driver would call this routine during an URB's completion
1774 * handler, if no more URBs were pending. 1484 * handler, if no more URBs were pending.
@@ -1778,28 +1488,58 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface);
1778void usb_autopm_put_interface_async(struct usb_interface *intf) 1488void usb_autopm_put_interface_async(struct usb_interface *intf)
1779{ 1489{
1780 struct usb_device *udev = interface_to_usbdev(intf); 1490 struct usb_device *udev = interface_to_usbdev(intf);
1491 unsigned long last_busy;
1781 int status = 0; 1492 int status = 0;
1782 1493
1783 if (intf->condition == USB_INTERFACE_UNBOUND) { 1494 last_busy = udev->last_busy;
1784 status = -ENODEV; 1495 udev->last_busy = jiffies;
1785 } else { 1496 atomic_dec(&intf->pm_usage_cnt);
1786 udev->last_busy = jiffies; 1497 pm_runtime_put_noidle(&intf->dev);
1787 atomic_dec(&intf->pm_usage_cnt); 1498
1788 if (udev->autosuspend_disabled || udev->autosuspend_delay < 0) 1499 if (!udev->autosuspend_disabled) {
1789 status = -EPERM; 1500 /* Optimization: Don't schedule a delayed autosuspend if
1790 else if (atomic_read(&intf->pm_usage_cnt) <= 0 && 1501 * the timer is already running and the expiration time
1791 !timer_pending(&udev->autosuspend.timer)) { 1502 * wouldn't change.
1792 queue_delayed_work(ksuspend_usb_wq, &udev->autosuspend, 1503 *
1504 * We have to use the interface's timer. Attempts to
1505 * schedule a suspend for the device would fail because
1506 * the interface is still active.
1507 */
1508 if (intf->dev.power.timer_expires == 0 ||
1509 round_jiffies_up(last_busy) !=
1510 round_jiffies_up(jiffies)) {
1511 status = pm_schedule_suspend(&intf->dev,
1512 jiffies_to_msecs(
1793 round_jiffies_up_relative( 1513 round_jiffies_up_relative(
1794 udev->autosuspend_delay)); 1514 udev->autosuspend_delay)));
1795 } 1515 }
1796 } 1516 }
1797 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1517 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1798 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1518 __func__, atomic_read(&intf->dev.power.usage_count),
1519 status);
1799} 1520}
1800EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async); 1521EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1801 1522
1802/** 1523/**
1524 * usb_autopm_put_interface_no_suspend - decrement a USB interface's PM-usage counter
1525 * @intf: the usb_interface whose counter should be decremented
1526 *
1527 * This routine decrements @intf's usage counter but does not carry out an
1528 * autosuspend.
1529 *
1530 * This routine can run in atomic context.
1531 */
1532void usb_autopm_put_interface_no_suspend(struct usb_interface *intf)
1533{
1534 struct usb_device *udev = interface_to_usbdev(intf);
1535
1536 udev->last_busy = jiffies;
1537 atomic_dec(&intf->pm_usage_cnt);
1538 pm_runtime_put_noidle(&intf->dev);
1539}
1540EXPORT_SYMBOL_GPL(usb_autopm_put_interface_no_suspend);
1541
1542/**
1803 * usb_autopm_get_interface - increment a USB interface's PM-usage counter 1543 * usb_autopm_get_interface - increment a USB interface's PM-usage counter
1804 * @intf: the usb_interface whose counter should be incremented 1544 * @intf: the usb_interface whose counter should be incremented
1805 * 1545 *
@@ -1811,25 +1551,8 @@ EXPORT_SYMBOL_GPL(usb_autopm_put_interface_async);
1811 * or @intf is unbound. A typical example would be a character-device 1551 * or @intf is unbound. A typical example would be a character-device
1812 * driver when its device file is opened. 1552 * driver when its device file is opened.
1813 * 1553 *
1814 * 1554 * @intf's usage counter is incremented to prevent subsequent autosuspends.
1815 * The routine increments @intf's usage counter. (However if the 1555 * However if the autoresume fails then the counter is re-decremented.
1816 * autoresume fails then the counter is re-decremented.) So long as the
1817 * counter is greater than 0, autosuspend will not be allowed for @intf
1818 * or its usb_device. When the driver is finished using @intf it should
1819 * call usb_autopm_put_interface() to decrement the usage counter and
1820 * queue a delayed autosuspend request (if the counter is <= 0).
1821 *
1822 *
1823 * Note that @intf->pm_usage_cnt is owned by the interface driver. The
1824 * core will not change its value other than the increment and decrement
1825 * in usb_autopm_get_interface and usb_autopm_put_interface. The driver
1826 * may use this simple counter-oriented discipline or may set the value
1827 * any way it likes.
1828 *
1829 * Resume method calls generated by this routine can arrive at any time
1830 * while @intf is suspended. They are not protected by the usb_device's
1831 * lock but only by its pm_mutex. Drivers must provide their own
1832 * synchronization.
1833 * 1556 *
1834 * This routine can run only in process context. 1557 * This routine can run only in process context.
1835 */ 1558 */
@@ -1837,9 +1560,16 @@ int usb_autopm_get_interface(struct usb_interface *intf)
1837{ 1560{
1838 int status; 1561 int status;
1839 1562
1840 status = usb_autopm_do_interface(intf, 1); 1563 status = pm_runtime_get_sync(&intf->dev);
1841 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1564 if (status < 0)
1842 __func__, status, atomic_read(&intf->pm_usage_cnt)); 1565 pm_runtime_put_sync(&intf->dev);
1566 else
1567 atomic_inc(&intf->pm_usage_cnt);
1568 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1569 __func__, atomic_read(&intf->dev.power.usage_count),
1570 status);
1571 if (status > 0)
1572 status = 0;
1843 return status; 1573 return status;
1844} 1574}
1845EXPORT_SYMBOL_GPL(usb_autopm_get_interface); 1575EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
@@ -1849,41 +1579,201 @@ EXPORT_SYMBOL_GPL(usb_autopm_get_interface);
1849 * @intf: the usb_interface whose counter should be incremented 1579 * @intf: the usb_interface whose counter should be incremented
1850 * 1580 *
1851 * This routine does much the same thing as 1581 * This routine does much the same thing as
1852 * usb_autopm_get_interface(): it increments @intf's usage counter and 1582 * usb_autopm_get_interface(): It increments @intf's usage counter and
1853 * queues an autoresume request if the result is > 0. The differences 1583 * queues an autoresume request if the device is suspended. The
1854 * are that it does not acquire the device's pm_mutex (callers must 1584 * differences are that it does not perform any synchronization (callers
1855 * handle all synchronization issues themselves), and it does not 1585 * should hold a private lock and handle all synchronization issues
1856 * autoresume the device directly (it only queues a request). After a 1586 * themselves), and it does not autoresume the device directly (it only
1857 * successful call, the device will generally not yet be resumed. 1587 * queues a request). After a successful call, the device may not yet be
1588 * resumed.
1858 * 1589 *
1859 * This routine can run in atomic context. 1590 * This routine can run in atomic context.
1860 */ 1591 */
1861int usb_autopm_get_interface_async(struct usb_interface *intf) 1592int usb_autopm_get_interface_async(struct usb_interface *intf)
1862{ 1593{
1863 struct usb_device *udev = interface_to_usbdev(intf); 1594 int status = 0;
1864 int status = 0; 1595 enum rpm_status s;
1865 1596
1866 if (intf->condition == USB_INTERFACE_UNBOUND) 1597 /* Don't request a resume unless the interface is already suspending
1867 status = -ENODEV; 1598 * or suspended. Doing so would force a running suspend timer to be
1868 else { 1599 * cancelled.
1600 */
1601 pm_runtime_get_noresume(&intf->dev);
1602 s = ACCESS_ONCE(intf->dev.power.runtime_status);
1603 if (s == RPM_SUSPENDING || s == RPM_SUSPENDED)
1604 status = pm_request_resume(&intf->dev);
1605
1606 if (status < 0 && status != -EINPROGRESS)
1607 pm_runtime_put_noidle(&intf->dev);
1608 else
1869 atomic_inc(&intf->pm_usage_cnt); 1609 atomic_inc(&intf->pm_usage_cnt);
1870 if (atomic_read(&intf->pm_usage_cnt) > 0 && 1610 dev_vdbg(&intf->dev, "%s: cnt %d -> %d\n",
1871 udev->state == USB_STATE_SUSPENDED) 1611 __func__, atomic_read(&intf->dev.power.usage_count),
1872 queue_work(ksuspend_usb_wq, &udev->autoresume); 1612 status);
1873 } 1613 if (status > 0)
1874 dev_vdbg(&intf->dev, "%s: status %d cnt %d\n", 1614 status = 0;
1875 __func__, status, atomic_read(&intf->pm_usage_cnt));
1876 return status; 1615 return status;
1877} 1616}
1878EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async); 1617EXPORT_SYMBOL_GPL(usb_autopm_get_interface_async);
1879 1618
1880#else 1619/**
1620 * usb_autopm_get_interface_no_resume - increment a USB interface's PM-usage counter
1621 * @intf: the usb_interface whose counter should be incremented
1622 *
1623 * This routine increments @intf's usage counter but does not carry out an
1624 * autoresume.
1625 *
1626 * This routine can run in atomic context.
1627 */
1628void usb_autopm_get_interface_no_resume(struct usb_interface *intf)
1629{
1630 struct usb_device *udev = interface_to_usbdev(intf);
1631
1632 udev->last_busy = jiffies;
1633 atomic_inc(&intf->pm_usage_cnt);
1634 pm_runtime_get_noresume(&intf->dev);
1635}
1636EXPORT_SYMBOL_GPL(usb_autopm_get_interface_no_resume);
1637
1638/* Internal routine to check whether we may autosuspend a device. */
1639static int autosuspend_check(struct usb_device *udev)
1640{
1641 int i;
1642 struct usb_interface *intf;
1643 unsigned long suspend_time, j;
1644
1645 /* Fail if autosuspend is disabled, or any interfaces are in use, or
1646 * any interface drivers require remote wakeup but it isn't available.
1647 */
1648 udev->do_remote_wakeup = device_may_wakeup(&udev->dev);
1649 if (udev->actconfig) {
1650 for (i = 0; i < udev->actconfig->desc.bNumInterfaces; i++) {
1651 intf = udev->actconfig->interface[i];
1652
1653 /* We don't need to check interfaces that are
1654 * disabled for runtime PM. Either they are unbound
1655 * or else their drivers don't support autosuspend
1656 * and so they are permanently active.
1657 */
1658 if (intf->dev.power.disable_depth)
1659 continue;
1660 if (atomic_read(&intf->dev.power.usage_count) > 0)
1661 return -EBUSY;
1662 if (intf->needs_remote_wakeup &&
1663 !udev->do_remote_wakeup) {
1664 dev_dbg(&udev->dev, "remote wakeup needed "
1665 "for autosuspend\n");
1666 return -EOPNOTSUPP;
1667 }
1668
1669 /* Don't allow autosuspend if the device will need
1670 * a reset-resume and any of its interface drivers
1671 * doesn't include support or needs remote wakeup.
1672 */
1673 if (udev->quirks & USB_QUIRK_RESET_RESUME) {
1674 struct usb_driver *driver;
1675
1676 driver = to_usb_driver(intf->dev.driver);
1677 if (!driver->reset_resume ||
1678 intf->needs_remote_wakeup)
1679 return -EOPNOTSUPP;
1680 }
1681 }
1682 }
1683
1684 /* If everything is okay but the device hasn't been idle for long
1685 * enough, queue a delayed autosuspend request.
1686 */
1687 j = ACCESS_ONCE(jiffies);
1688 suspend_time = udev->last_busy + udev->autosuspend_delay;
1689 if (time_before(j, suspend_time)) {
1690 pm_schedule_suspend(&udev->dev, jiffies_to_msecs(
1691 round_jiffies_up_relative(suspend_time - j)));
1692 return -EAGAIN;
1693 }
1694 return 0;
1695}
1696
1697static int usb_runtime_suspend(struct device *dev)
1698{
1699 int status = 0;
1881 1700
1882void usb_autosuspend_work(struct work_struct *work) 1701 /* A USB device can be suspended if it passes the various autosuspend
1883{} 1702 * checks. Runtime suspend for a USB device means suspending all the
1703 * interfaces and then the device itself.
1704 */
1705 if (is_usb_device(dev)) {
1706 struct usb_device *udev = to_usb_device(dev);
1707
1708 if (autosuspend_check(udev) != 0)
1709 return -EAGAIN;
1710
1711 status = usb_suspend_both(udev, PMSG_AUTO_SUSPEND);
1712
1713 /* If an interface fails the suspend, adjust the last_busy
1714 * time so that we don't get another suspend attempt right
1715 * away.
1716 */
1717 if (status) {
1718 udev->last_busy = jiffies +
1719 (udev->autosuspend_delay == 0 ?
1720 HZ/2 : 0);
1721 }
1722
1723 /* Prevent the parent from suspending immediately after */
1724 else if (udev->parent) {
1725 udev->parent->last_busy = jiffies;
1726 }
1727 }
1728
1729 /* Runtime suspend for a USB interface doesn't mean anything. */
1730 return status;
1731}
1732
1733static int usb_runtime_resume(struct device *dev)
1734{
1735 /* Runtime resume for a USB device means resuming both the device
1736 * and all its interfaces.
1737 */
1738 if (is_usb_device(dev)) {
1739 struct usb_device *udev = to_usb_device(dev);
1740 int status;
1741
1742 status = usb_resume_both(udev, PMSG_AUTO_RESUME);
1743 udev->last_busy = jiffies;
1744 return status;
1745 }
1746
1747 /* Runtime resume for a USB interface doesn't mean anything. */
1748 return 0;
1749}
1750
1751static int usb_runtime_idle(struct device *dev)
1752{
1753 /* An idle USB device can be suspended if it passes the various
1754 * autosuspend checks. An idle interface can be suspended at
1755 * any time.
1756 */
1757 if (is_usb_device(dev)) {
1758 struct usb_device *udev = to_usb_device(dev);
1759
1760 if (autosuspend_check(udev) != 0)
1761 return 0;
1762 }
1763
1764 pm_runtime_suspend(dev);
1765 return 0;
1766}
1767
1768static struct dev_pm_ops usb_bus_pm_ops = {
1769 .runtime_suspend = usb_runtime_suspend,
1770 .runtime_resume = usb_runtime_resume,
1771 .runtime_idle = usb_runtime_idle,
1772};
1773
1774#else
1884 1775
1885void usb_autoresume_work(struct work_struct *work) 1776#define usb_bus_pm_ops (*(struct dev_pm_ops *) NULL)
1886{}
1887 1777
1888#endif /* CONFIG_USB_SUSPEND */ 1778#endif /* CONFIG_USB_SUSPEND */
1889 1779
@@ -1891,4 +1781,5 @@ struct bus_type usb_bus_type = {
1891 .name = "usb", 1781 .name = "usb",
1892 .match = usb_device_match, 1782 .match = usb_device_match,
1893 .uevent = usb_uevent, 1783 .uevent = usb_uevent,
1784 .pm = &usb_bus_pm_ops,
1894}; 1785};