main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * drivers/base/power/main.c - Where the driver meets power management.
  3. *
  4. * Copyright (c) 2003 Patrick Mochel
  5. * Copyright (c) 2003 Open Source Development Lab
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. *
  10. * The driver model core calls device_pm_add() when a device is registered.
  11. * This will intialize the embedded device_pm_info object in the device
  12. * and add it to the list of power-controlled devices. sysfs entries for
  13. * controlling device power management will also be added.
  14. *
  15. * A separate list is used for keeping track of power info, because the power
  16. * domain dependencies may differ from the ancestral dependencies that the
  17. * subsystem list maintains.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/kallsyms.h>
  21. #include <linux/mutex.h>
  22. #include <linux/pm.h>
  23. #include <linux/pm_runtime.h>
  24. #include <linux/resume-trace.h>
  25. #include <linux/rwsem.h>
  26. #include <linux/interrupt.h>
  27. #include "../base.h"
  28. #include "power.h"
  29. /*
  30. * The entries in the dpm_list list are in a depth first order, simply
  31. * because children are guaranteed to be discovered after parents, and
  32. * are inserted at the back of the list on discovery.
  33. *
  34. * Since device_pm_add() may be called with a device semaphore held,
  35. * we must never try to acquire a device semaphore while holding
  36. * dpm_list_mutex.
  37. */
  38. LIST_HEAD(dpm_list);
  39. static DEFINE_MUTEX(dpm_list_mtx);
  40. /*
  41. * Set once the preparation of devices for a PM transition has started, reset
  42. * before starting to resume devices. Protected by dpm_list_mtx.
  43. */
  44. static bool transition_started;
  45. /**
  46. * device_pm_init - Initialize the PM-related part of a device object.
  47. * @dev: Device object being initialized.
  48. */
  49. void device_pm_init(struct device *dev)
  50. {
  51. dev->power.status = DPM_ON;
  52. pm_runtime_init(dev);
  53. }
  54. /**
  55. * device_pm_lock - Lock the list of active devices used by the PM core.
  56. */
  57. void device_pm_lock(void)
  58. {
  59. mutex_lock(&dpm_list_mtx);
  60. }
  61. /**
  62. * device_pm_unlock - Unlock the list of active devices used by the PM core.
  63. */
  64. void device_pm_unlock(void)
  65. {
  66. mutex_unlock(&dpm_list_mtx);
  67. }
  68. /**
  69. * device_pm_add - Add a device to the PM core's list of active devices.
  70. * @dev: Device to add to the list.
  71. */
  72. void device_pm_add(struct device *dev)
  73. {
  74. pr_debug("PM: Adding info for %s:%s\n",
  75. dev->bus ? dev->bus->name : "No Bus",
  76. kobject_name(&dev->kobj));
  77. mutex_lock(&dpm_list_mtx);
  78. if (dev->parent) {
  79. if (dev->parent->power.status >= DPM_SUSPENDING)
  80. dev_warn(dev, "parent %s should not be sleeping\n",
  81. dev_name(dev->parent));
  82. } else if (transition_started) {
  83. /*
  84. * We refuse to register parentless devices while a PM
  85. * transition is in progress in order to avoid leaving them
  86. * unhandled down the road
  87. */
  88. dev_WARN(dev, "Parentless device registered during a PM transaction\n");
  89. }
  90. list_add_tail(&dev->power.entry, &dpm_list);
  91. mutex_unlock(&dpm_list_mtx);
  92. }
  93. /**
  94. * device_pm_remove - Remove a device from the PM core's list of active devices.
  95. * @dev: Device to be removed from the list.
  96. */
  97. void device_pm_remove(struct device *dev)
  98. {
  99. pr_debug("PM: Removing info for %s:%s\n",
  100. dev->bus ? dev->bus->name : "No Bus",
  101. kobject_name(&dev->kobj));
  102. mutex_lock(&dpm_list_mtx);
  103. list_del_init(&dev->power.entry);
  104. mutex_unlock(&dpm_list_mtx);
  105. pm_runtime_remove(dev);
  106. }
  107. /**
  108. * device_pm_move_before - Move device in the PM core's list of active devices.
  109. * @deva: Device to move in dpm_list.
  110. * @devb: Device @deva should come before.
  111. */
  112. void device_pm_move_before(struct device *deva, struct device *devb)
  113. {
  114. pr_debug("PM: Moving %s:%s before %s:%s\n",
  115. deva->bus ? deva->bus->name : "No Bus",
  116. kobject_name(&deva->kobj),
  117. devb->bus ? devb->bus->name : "No Bus",
  118. kobject_name(&devb->kobj));
  119. /* Delete deva from dpm_list and reinsert before devb. */
  120. list_move_tail(&deva->power.entry, &devb->power.entry);
  121. }
  122. /**
  123. * device_pm_move_after - Move device in the PM core's list of active devices.
  124. * @deva: Device to move in dpm_list.
  125. * @devb: Device @deva should come after.
  126. */
  127. void device_pm_move_after(struct device *deva, struct device *devb)
  128. {
  129. pr_debug("PM: Moving %s:%s after %s:%s\n",
  130. deva->bus ? deva->bus->name : "No Bus",
  131. kobject_name(&deva->kobj),
  132. devb->bus ? devb->bus->name : "No Bus",
  133. kobject_name(&devb->kobj));
  134. /* Delete deva from dpm_list and reinsert after devb. */
  135. list_move(&deva->power.entry, &devb->power.entry);
  136. }
  137. /**
  138. * device_pm_move_last - Move device to end of the PM core's list of devices.
  139. * @dev: Device to move in dpm_list.
  140. */
  141. void device_pm_move_last(struct device *dev)
  142. {
  143. pr_debug("PM: Moving %s:%s to end of list\n",
  144. dev->bus ? dev->bus->name : "No Bus",
  145. kobject_name(&dev->kobj));
  146. list_move_tail(&dev->power.entry, &dpm_list);
  147. }
  148. /**
  149. * pm_op - Execute the PM operation appropriate for given PM event.
  150. * @dev: Device to handle.
  151. * @ops: PM operations to choose from.
  152. * @state: PM transition of the system being carried out.
  153. */
  154. static int pm_op(struct device *dev,
  155. const struct dev_pm_ops *ops,
  156. pm_message_t state)
  157. {
  158. int error = 0;
  159. switch (state.event) {
  160. #ifdef CONFIG_SUSPEND
  161. case PM_EVENT_SUSPEND:
  162. if (ops->suspend) {
  163. error = ops->suspend(dev);
  164. suspend_report_result(ops->suspend, error);
  165. }
  166. break;
  167. case PM_EVENT_RESUME:
  168. if (ops->resume) {
  169. error = ops->resume(dev);
  170. suspend_report_result(ops->resume, error);
  171. }
  172. break;
  173. #endif /* CONFIG_SUSPEND */
  174. #ifdef CONFIG_HIBERNATION
  175. case PM_EVENT_FREEZE:
  176. case PM_EVENT_QUIESCE:
  177. if (ops->freeze) {
  178. error = ops->freeze(dev);
  179. suspend_report_result(ops->freeze, error);
  180. }
  181. break;
  182. case PM_EVENT_HIBERNATE:
  183. if (ops->poweroff) {
  184. error = ops->poweroff(dev);
  185. suspend_report_result(ops->poweroff, error);
  186. }
  187. break;
  188. case PM_EVENT_THAW:
  189. case PM_EVENT_RECOVER:
  190. if (ops->thaw) {
  191. error = ops->thaw(dev);
  192. suspend_report_result(ops->thaw, error);
  193. }
  194. break;
  195. case PM_EVENT_RESTORE:
  196. if (ops->restore) {
  197. error = ops->restore(dev);
  198. suspend_report_result(ops->restore, error);
  199. }
  200. break;
  201. #endif /* CONFIG_HIBERNATION */
  202. default:
  203. error = -EINVAL;
  204. }
  205. return error;
  206. }
  207. /**
  208. * pm_noirq_op - Execute the PM operation appropriate for given PM event.
  209. * @dev: Device to handle.
  210. * @ops: PM operations to choose from.
  211. * @state: PM transition of the system being carried out.
  212. *
  213. * The driver of @dev will not receive interrupts while this function is being
  214. * executed.
  215. */
  216. static int pm_noirq_op(struct device *dev,
  217. const struct dev_pm_ops *ops,
  218. pm_message_t state)
  219. {
  220. int error = 0;
  221. switch (state.event) {
  222. #ifdef CONFIG_SUSPEND
  223. case PM_EVENT_SUSPEND:
  224. if (ops->suspend_noirq) {
  225. error = ops->suspend_noirq(dev);
  226. suspend_report_result(ops->suspend_noirq, error);
  227. }
  228. break;
  229. case PM_EVENT_RESUME:
  230. if (ops->resume_noirq) {
  231. error = ops->resume_noirq(dev);
  232. suspend_report_result(ops->resume_noirq, error);
  233. }
  234. break;
  235. #endif /* CONFIG_SUSPEND */
  236. #ifdef CONFIG_HIBERNATION
  237. case PM_EVENT_FREEZE:
  238. case PM_EVENT_QUIESCE:
  239. if (ops->freeze_noirq) {
  240. error = ops->freeze_noirq(dev);
  241. suspend_report_result(ops->freeze_noirq, error);
  242. }
  243. break;
  244. case PM_EVENT_HIBERNATE:
  245. if (ops->poweroff_noirq) {
  246. error = ops->poweroff_noirq(dev);
  247. suspend_report_result(ops->poweroff_noirq, error);
  248. }
  249. break;
  250. case PM_EVENT_THAW:
  251. case PM_EVENT_RECOVER:
  252. if (ops->thaw_noirq) {
  253. error = ops->thaw_noirq(dev);
  254. suspend_report_result(ops->thaw_noirq, error);
  255. }
  256. break;
  257. case PM_EVENT_RESTORE:
  258. if (ops->restore_noirq) {
  259. error = ops->restore_noirq(dev);
  260. suspend_report_result(ops->restore_noirq, error);
  261. }
  262. break;
  263. #endif /* CONFIG_HIBERNATION */
  264. default:
  265. error = -EINVAL;
  266. }
  267. return error;
  268. }
  269. static char *pm_verb(int event)
  270. {
  271. switch (event) {
  272. case PM_EVENT_SUSPEND:
  273. return "suspend";
  274. case PM_EVENT_RESUME:
  275. return "resume";
  276. case PM_EVENT_FREEZE:
  277. return "freeze";
  278. case PM_EVENT_QUIESCE:
  279. return "quiesce";
  280. case PM_EVENT_HIBERNATE:
  281. return "hibernate";
  282. case PM_EVENT_THAW:
  283. return "thaw";
  284. case PM_EVENT_RESTORE:
  285. return "restore";
  286. case PM_EVENT_RECOVER:
  287. return "recover";
  288. default:
  289. return "(unknown PM event)";
  290. }
  291. }
  292. static void pm_dev_dbg(struct device *dev, pm_message_t state, char *info)
  293. {
  294. dev_dbg(dev, "%s%s%s\n", info, pm_verb(state.event),
  295. ((state.event & PM_EVENT_SLEEP) && device_may_wakeup(dev)) ?
  296. ", may wakeup" : "");
  297. }
  298. static void pm_dev_err(struct device *dev, pm_message_t state, char *info,
  299. int error)
  300. {
  301. printk(KERN_ERR "PM: Device %s failed to %s%s: error %d\n",
  302. kobject_name(&dev->kobj), pm_verb(state.event), info, error);
  303. }
  304. /*------------------------- Resume routines -------------------------*/
  305. /**
  306. * device_resume_noirq - Execute an "early resume" callback for given device.
  307. * @dev: Device to handle.
  308. * @state: PM transition of the system being carried out.
  309. *
  310. * The driver of @dev will not receive interrupts while this function is being
  311. * executed.
  312. */
  313. static int device_resume_noirq(struct device *dev, pm_message_t state)
  314. {
  315. int error = 0;
  316. TRACE_DEVICE(dev);
  317. TRACE_RESUME(0);
  318. if (!dev->bus)
  319. goto End;
  320. if (dev->bus->pm) {
  321. pm_dev_dbg(dev, state, "EARLY ");
  322. error = pm_noirq_op(dev, dev->bus->pm, state);
  323. }
  324. End:
  325. TRACE_RESUME(error);
  326. return error;
  327. }
  328. /**
  329. * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
  330. * @state: PM transition of the system being carried out.
  331. *
  332. * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
  333. * enable device drivers to receive interrupts.
  334. */
  335. void dpm_resume_noirq(pm_message_t state)
  336. {
  337. struct device *dev;
  338. mutex_lock(&dpm_list_mtx);
  339. transition_started = false;
  340. list_for_each_entry(dev, &dpm_list, power.entry)
  341. if (dev->power.status > DPM_OFF) {
  342. int error;
  343. dev->power.status = DPM_OFF;
  344. error = device_resume_noirq(dev, state);
  345. if (error)
  346. pm_dev_err(dev, state, " early", error);
  347. }
  348. mutex_unlock(&dpm_list_mtx);
  349. resume_device_irqs();
  350. }
  351. EXPORT_SYMBOL_GPL(dpm_resume_noirq);
  352. /**
  353. * device_resume - Execute "resume" callbacks for given device.
  354. * @dev: Device to handle.
  355. * @state: PM transition of the system being carried out.
  356. */
  357. static int device_resume(struct device *dev, pm_message_t state)
  358. {
  359. int error = 0;
  360. TRACE_DEVICE(dev);
  361. TRACE_RESUME(0);
  362. down(&dev->sem);
  363. if (dev->bus) {
  364. if (dev->bus->pm) {
  365. pm_dev_dbg(dev, state, "");
  366. error = pm_op(dev, dev->bus->pm, state);
  367. } else if (dev->bus->resume) {
  368. pm_dev_dbg(dev, state, "legacy ");
  369. error = dev->bus->resume(dev);
  370. }
  371. if (error)
  372. goto End;
  373. }
  374. if (dev->type) {
  375. if (dev->type->pm) {
  376. pm_dev_dbg(dev, state, "type ");
  377. error = pm_op(dev, dev->type->pm, state);
  378. }
  379. if (error)
  380. goto End;
  381. }
  382. if (dev->class) {
  383. if (dev->class->pm) {
  384. pm_dev_dbg(dev, state, "class ");
  385. error = pm_op(dev, dev->class->pm, state);
  386. } else if (dev->class->resume) {
  387. pm_dev_dbg(dev, state, "legacy class ");
  388. error = dev->class->resume(dev);
  389. }
  390. }
  391. End:
  392. up(&dev->sem);
  393. TRACE_RESUME(error);
  394. return error;
  395. }
  396. /**
  397. * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
  398. * @state: PM transition of the system being carried out.
  399. *
  400. * Execute the appropriate "resume" callback for all devices whose status
  401. * indicates that they are suspended.
  402. */
  403. static void dpm_resume(pm_message_t state)
  404. {
  405. struct list_head list;
  406. INIT_LIST_HEAD(&list);
  407. mutex_lock(&dpm_list_mtx);
  408. while (!list_empty(&dpm_list)) {
  409. struct device *dev = to_device(dpm_list.next);
  410. get_device(dev);
  411. if (dev->power.status >= DPM_OFF) {
  412. int error;
  413. dev->power.status = DPM_RESUMING;
  414. mutex_unlock(&dpm_list_mtx);
  415. error = device_resume(dev, state);
  416. mutex_lock(&dpm_list_mtx);
  417. if (error)
  418. pm_dev_err(dev, state, "", error);
  419. } else if (dev->power.status == DPM_SUSPENDING) {
  420. /* Allow new children of the device to be registered */
  421. dev->power.status = DPM_RESUMING;
  422. }
  423. if (!list_empty(&dev->power.entry))
  424. list_move_tail(&dev->power.entry, &list);
  425. put_device(dev);
  426. }
  427. list_splice(&list, &dpm_list);
  428. mutex_unlock(&dpm_list_mtx);
  429. }
  430. /**
  431. * device_complete - Complete a PM transition for given device.
  432. * @dev: Device to handle.
  433. * @state: PM transition of the system being carried out.
  434. */
  435. static void device_complete(struct device *dev, pm_message_t state)
  436. {
  437. down(&dev->sem);
  438. if (dev->class && dev->class->pm && dev->class->pm->complete) {
  439. pm_dev_dbg(dev, state, "completing class ");
  440. dev->class->pm->complete(dev);
  441. }
  442. if (dev->type && dev->type->pm && dev->type->pm->complete) {
  443. pm_dev_dbg(dev, state, "completing type ");
  444. dev->type->pm->complete(dev);
  445. }
  446. if (dev->bus && dev->bus->pm && dev->bus->pm->complete) {
  447. pm_dev_dbg(dev, state, "completing ");
  448. dev->bus->pm->complete(dev);
  449. }
  450. up(&dev->sem);
  451. }
  452. /**
  453. * dpm_complete - Complete a PM transition for all non-sysdev devices.
  454. * @state: PM transition of the system being carried out.
  455. *
  456. * Execute the ->complete() callbacks for all devices whose PM status is not
  457. * DPM_ON (this allows new devices to be registered).
  458. */
  459. static void dpm_complete(pm_message_t state)
  460. {
  461. struct list_head list;
  462. INIT_LIST_HEAD(&list);
  463. mutex_lock(&dpm_list_mtx);
  464. while (!list_empty(&dpm_list)) {
  465. struct device *dev = to_device(dpm_list.prev);
  466. get_device(dev);
  467. if (dev->power.status > DPM_ON) {
  468. dev->power.status = DPM_ON;
  469. mutex_unlock(&dpm_list_mtx);
  470. device_complete(dev, state);
  471. pm_runtime_put_noidle(dev);
  472. mutex_lock(&dpm_list_mtx);
  473. }
  474. if (!list_empty(&dev->power.entry))
  475. list_move(&dev->power.entry, &list);
  476. put_device(dev);
  477. }
  478. list_splice(&list, &dpm_list);
  479. mutex_unlock(&dpm_list_mtx);
  480. }
  481. /**
  482. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  483. * @state: PM transition of the system being carried out.
  484. *
  485. * Execute "resume" callbacks for all devices and complete the PM transition of
  486. * the system.
  487. */
  488. void dpm_resume_end(pm_message_t state)
  489. {
  490. might_sleep();
  491. dpm_resume(state);
  492. dpm_complete(state);
  493. }
  494. EXPORT_SYMBOL_GPL(dpm_resume_end);
  495. /*------------------------- Suspend routines -------------------------*/
  496. /**
  497. * resume_event - Return a "resume" message for given "suspend" sleep state.
  498. * @sleep_state: PM message representing a sleep state.
  499. *
  500. * Return a PM message representing the resume event corresponding to given
  501. * sleep state.
  502. */
  503. static pm_message_t resume_event(pm_message_t sleep_state)
  504. {
  505. switch (sleep_state.event) {
  506. case PM_EVENT_SUSPEND:
  507. return PMSG_RESUME;
  508. case PM_EVENT_FREEZE:
  509. case PM_EVENT_QUIESCE:
  510. return PMSG_RECOVER;
  511. case PM_EVENT_HIBERNATE:
  512. return PMSG_RESTORE;
  513. }
  514. return PMSG_ON;
  515. }
  516. /**
  517. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  518. * @dev: Device to handle.
  519. * @state: PM transition of the system being carried out.
  520. *
  521. * The driver of @dev will not receive interrupts while this function is being
  522. * executed.
  523. */
  524. static int device_suspend_noirq(struct device *dev, pm_message_t state)
  525. {
  526. int error = 0;
  527. if (!dev->bus)
  528. return 0;
  529. if (dev->bus->pm) {
  530. pm_dev_dbg(dev, state, "LATE ");
  531. error = pm_noirq_op(dev, dev->bus->pm, state);
  532. }
  533. return error;
  534. }
  535. /**
  536. * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
  537. * @state: PM transition of the system being carried out.
  538. *
  539. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  540. * handlers for all non-sysdev devices.
  541. */
  542. int dpm_suspend_noirq(pm_message_t state)
  543. {
  544. struct device *dev;
  545. int error = 0;
  546. suspend_device_irqs();
  547. mutex_lock(&dpm_list_mtx);
  548. list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
  549. error = device_suspend_noirq(dev, state);
  550. if (error) {
  551. pm_dev_err(dev, state, " late", error);
  552. break;
  553. }
  554. dev->power.status = DPM_OFF_IRQ;
  555. }
  556. mutex_unlock(&dpm_list_mtx);
  557. if (error)
  558. dpm_resume_noirq(resume_event(state));
  559. return error;
  560. }
  561. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
  562. /**
  563. * device_suspend - Execute "suspend" callbacks for given device.
  564. * @dev: Device to handle.
  565. * @state: PM transition of the system being carried out.
  566. */
  567. static int device_suspend(struct device *dev, pm_message_t state)
  568. {
  569. int error = 0;
  570. down(&dev->sem);
  571. if (dev->class) {
  572. if (dev->class->pm) {
  573. pm_dev_dbg(dev, state, "class ");
  574. error = pm_op(dev, dev->class->pm, state);
  575. } else if (dev->class->suspend) {
  576. pm_dev_dbg(dev, state, "legacy class ");
  577. error = dev->class->suspend(dev, state);
  578. suspend_report_result(dev->class->suspend, error);
  579. }
  580. if (error)
  581. goto End;
  582. }
  583. if (dev->type) {
  584. if (dev->type->pm) {
  585. pm_dev_dbg(dev, state, "type ");
  586. error = pm_op(dev, dev->type->pm, state);
  587. }
  588. if (error)
  589. goto End;
  590. }
  591. if (dev->bus) {
  592. if (dev->bus->pm) {
  593. pm_dev_dbg(dev, state, "");
  594. error = pm_op(dev, dev->bus->pm, state);
  595. } else if (dev->bus->suspend) {
  596. pm_dev_dbg(dev, state, "legacy ");
  597. error = dev->bus->suspend(dev, state);
  598. suspend_report_result(dev->bus->suspend, error);
  599. }
  600. }
  601. End:
  602. up(&dev->sem);
  603. return error;
  604. }
  605. /**
  606. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  607. * @state: PM transition of the system being carried out.
  608. */
  609. static int dpm_suspend(pm_message_t state)
  610. {
  611. struct list_head list;
  612. int error = 0;
  613. INIT_LIST_HEAD(&list);
  614. mutex_lock(&dpm_list_mtx);
  615. while (!list_empty(&dpm_list)) {
  616. struct device *dev = to_device(dpm_list.prev);
  617. get_device(dev);
  618. mutex_unlock(&dpm_list_mtx);
  619. error = device_suspend(dev, state);
  620. mutex_lock(&dpm_list_mtx);
  621. if (error) {
  622. pm_dev_err(dev, state, "", error);
  623. put_device(dev);
  624. break;
  625. }
  626. dev->power.status = DPM_OFF;
  627. if (!list_empty(&dev->power.entry))
  628. list_move(&dev->power.entry, &list);
  629. put_device(dev);
  630. }
  631. list_splice(&list, dpm_list.prev);
  632. mutex_unlock(&dpm_list_mtx);
  633. return error;
  634. }
  635. /**
  636. * device_prepare - Prepare a device for system power transition.
  637. * @dev: Device to handle.
  638. * @state: PM transition of the system being carried out.
  639. *
  640. * Execute the ->prepare() callback(s) for given device. No new children of the
  641. * device may be registered after this function has returned.
  642. */
  643. static int device_prepare(struct device *dev, pm_message_t state)
  644. {
  645. int error = 0;
  646. down(&dev->sem);
  647. if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
  648. pm_dev_dbg(dev, state, "preparing ");
  649. error = dev->bus->pm->prepare(dev);
  650. suspend_report_result(dev->bus->pm->prepare, error);
  651. if (error)
  652. goto End;
  653. }
  654. if (dev->type && dev->type->pm && dev->type->pm->prepare) {
  655. pm_dev_dbg(dev, state, "preparing type ");
  656. error = dev->type->pm->prepare(dev);
  657. suspend_report_result(dev->type->pm->prepare, error);
  658. if (error)
  659. goto End;
  660. }
  661. if (dev->class && dev->class->pm && dev->class->pm->prepare) {
  662. pm_dev_dbg(dev, state, "preparing class ");
  663. error = dev->class->pm->prepare(dev);
  664. suspend_report_result(dev->class->pm->prepare, error);
  665. }
  666. End:
  667. up(&dev->sem);
  668. return error;
  669. }
  670. /**
  671. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  672. * @state: PM transition of the system being carried out.
  673. *
  674. * Execute the ->prepare() callback(s) for all devices.
  675. */
  676. static int dpm_prepare(pm_message_t state)
  677. {
  678. struct list_head list;
  679. int error = 0;
  680. INIT_LIST_HEAD(&list);
  681. mutex_lock(&dpm_list_mtx);
  682. transition_started = true;
  683. while (!list_empty(&dpm_list)) {
  684. struct device *dev = to_device(dpm_list.next);
  685. get_device(dev);
  686. dev->power.status = DPM_PREPARING;
  687. mutex_unlock(&dpm_list_mtx);
  688. pm_runtime_get_noresume(dev);
  689. if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
  690. /* Wake-up requested during system sleep transition. */
  691. pm_runtime_put_noidle(dev);
  692. error = -EBUSY;
  693. } else {
  694. error = device_prepare(dev, state);
  695. }
  696. mutex_lock(&dpm_list_mtx);
  697. if (error) {
  698. dev->power.status = DPM_ON;
  699. if (error == -EAGAIN) {
  700. put_device(dev);
  701. error = 0;
  702. continue;
  703. }
  704. printk(KERN_ERR "PM: Failed to prepare device %s "
  705. "for power transition: error %d\n",
  706. kobject_name(&dev->kobj), error);
  707. put_device(dev);
  708. break;
  709. }
  710. dev->power.status = DPM_SUSPENDING;
  711. if (!list_empty(&dev->power.entry))
  712. list_move_tail(&dev->power.entry, &list);
  713. put_device(dev);
  714. }
  715. list_splice(&list, &dpm_list);
  716. mutex_unlock(&dpm_list_mtx);
  717. return error;
  718. }
  719. /**
  720. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  721. * @state: PM transition of the system being carried out.
  722. *
  723. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  724. * callbacks for them.
  725. */
  726. int dpm_suspend_start(pm_message_t state)
  727. {
  728. int error;
  729. might_sleep();
  730. error = dpm_prepare(state);
  731. if (!error)
  732. error = dpm_suspend(state);
  733. return error;
  734. }
  735. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  736. void __suspend_report_result(const char *function, void *fn, int ret)
  737. {
  738. if (ret)
  739. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  740. }
  741. EXPORT_SYMBOL_GPL(__suspend_report_result);