main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  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. transition_started = false;
  465. while (!list_empty(&dpm_list)) {
  466. struct device *dev = to_device(dpm_list.prev);
  467. get_device(dev);
  468. if (dev->power.status > DPM_ON) {
  469. dev->power.status = DPM_ON;
  470. mutex_unlock(&dpm_list_mtx);
  471. device_complete(dev, state);
  472. pm_runtime_put_noidle(dev);
  473. mutex_lock(&dpm_list_mtx);
  474. }
  475. if (!list_empty(&dev->power.entry))
  476. list_move(&dev->power.entry, &list);
  477. put_device(dev);
  478. }
  479. list_splice(&list, &dpm_list);
  480. mutex_unlock(&dpm_list_mtx);
  481. }
  482. /**
  483. * dpm_resume_end - Execute "resume" callbacks and complete system transition.
  484. * @state: PM transition of the system being carried out.
  485. *
  486. * Execute "resume" callbacks for all devices and complete the PM transition of
  487. * the system.
  488. */
  489. void dpm_resume_end(pm_message_t state)
  490. {
  491. might_sleep();
  492. dpm_resume(state);
  493. dpm_complete(state);
  494. }
  495. EXPORT_SYMBOL_GPL(dpm_resume_end);
  496. /*------------------------- Suspend routines -------------------------*/
  497. /**
  498. * resume_event - Return a "resume" message for given "suspend" sleep state.
  499. * @sleep_state: PM message representing a sleep state.
  500. *
  501. * Return a PM message representing the resume event corresponding to given
  502. * sleep state.
  503. */
  504. static pm_message_t resume_event(pm_message_t sleep_state)
  505. {
  506. switch (sleep_state.event) {
  507. case PM_EVENT_SUSPEND:
  508. return PMSG_RESUME;
  509. case PM_EVENT_FREEZE:
  510. case PM_EVENT_QUIESCE:
  511. return PMSG_RECOVER;
  512. case PM_EVENT_HIBERNATE:
  513. return PMSG_RESTORE;
  514. }
  515. return PMSG_ON;
  516. }
  517. /**
  518. * device_suspend_noirq - Execute a "late suspend" callback for given device.
  519. * @dev: Device to handle.
  520. * @state: PM transition of the system being carried out.
  521. *
  522. * The driver of @dev will not receive interrupts while this function is being
  523. * executed.
  524. */
  525. static int device_suspend_noirq(struct device *dev, pm_message_t state)
  526. {
  527. int error = 0;
  528. if (!dev->bus)
  529. return 0;
  530. if (dev->bus->pm) {
  531. pm_dev_dbg(dev, state, "LATE ");
  532. error = pm_noirq_op(dev, dev->bus->pm, state);
  533. }
  534. return error;
  535. }
  536. /**
  537. * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
  538. * @state: PM transition of the system being carried out.
  539. *
  540. * Prevent device drivers from receiving interrupts and call the "noirq" suspend
  541. * handlers for all non-sysdev devices.
  542. */
  543. int dpm_suspend_noirq(pm_message_t state)
  544. {
  545. struct device *dev;
  546. int error = 0;
  547. suspend_device_irqs();
  548. mutex_lock(&dpm_list_mtx);
  549. list_for_each_entry_reverse(dev, &dpm_list, power.entry) {
  550. error = device_suspend_noirq(dev, state);
  551. if (error) {
  552. pm_dev_err(dev, state, " late", error);
  553. break;
  554. }
  555. dev->power.status = DPM_OFF_IRQ;
  556. }
  557. mutex_unlock(&dpm_list_mtx);
  558. if (error)
  559. dpm_resume_noirq(resume_event(state));
  560. return error;
  561. }
  562. EXPORT_SYMBOL_GPL(dpm_suspend_noirq);
  563. /**
  564. * device_suspend - Execute "suspend" callbacks for given device.
  565. * @dev: Device to handle.
  566. * @state: PM transition of the system being carried out.
  567. */
  568. static int device_suspend(struct device *dev, pm_message_t state)
  569. {
  570. int error = 0;
  571. down(&dev->sem);
  572. if (dev->class) {
  573. if (dev->class->pm) {
  574. pm_dev_dbg(dev, state, "class ");
  575. error = pm_op(dev, dev->class->pm, state);
  576. } else if (dev->class->suspend) {
  577. pm_dev_dbg(dev, state, "legacy class ");
  578. error = dev->class->suspend(dev, state);
  579. suspend_report_result(dev->class->suspend, error);
  580. }
  581. if (error)
  582. goto End;
  583. }
  584. if (dev->type) {
  585. if (dev->type->pm) {
  586. pm_dev_dbg(dev, state, "type ");
  587. error = pm_op(dev, dev->type->pm, state);
  588. }
  589. if (error)
  590. goto End;
  591. }
  592. if (dev->bus) {
  593. if (dev->bus->pm) {
  594. pm_dev_dbg(dev, state, "");
  595. error = pm_op(dev, dev->bus->pm, state);
  596. } else if (dev->bus->suspend) {
  597. pm_dev_dbg(dev, state, "legacy ");
  598. error = dev->bus->suspend(dev, state);
  599. suspend_report_result(dev->bus->suspend, error);
  600. }
  601. }
  602. End:
  603. up(&dev->sem);
  604. return error;
  605. }
  606. /**
  607. * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
  608. * @state: PM transition of the system being carried out.
  609. */
  610. static int dpm_suspend(pm_message_t state)
  611. {
  612. struct list_head list;
  613. int error = 0;
  614. INIT_LIST_HEAD(&list);
  615. mutex_lock(&dpm_list_mtx);
  616. while (!list_empty(&dpm_list)) {
  617. struct device *dev = to_device(dpm_list.prev);
  618. get_device(dev);
  619. mutex_unlock(&dpm_list_mtx);
  620. error = device_suspend(dev, state);
  621. mutex_lock(&dpm_list_mtx);
  622. if (error) {
  623. pm_dev_err(dev, state, "", error);
  624. put_device(dev);
  625. break;
  626. }
  627. dev->power.status = DPM_OFF;
  628. if (!list_empty(&dev->power.entry))
  629. list_move(&dev->power.entry, &list);
  630. put_device(dev);
  631. }
  632. list_splice(&list, dpm_list.prev);
  633. mutex_unlock(&dpm_list_mtx);
  634. return error;
  635. }
  636. /**
  637. * device_prepare - Prepare a device for system power transition.
  638. * @dev: Device to handle.
  639. * @state: PM transition of the system being carried out.
  640. *
  641. * Execute the ->prepare() callback(s) for given device. No new children of the
  642. * device may be registered after this function has returned.
  643. */
  644. static int device_prepare(struct device *dev, pm_message_t state)
  645. {
  646. int error = 0;
  647. down(&dev->sem);
  648. if (dev->bus && dev->bus->pm && dev->bus->pm->prepare) {
  649. pm_dev_dbg(dev, state, "preparing ");
  650. error = dev->bus->pm->prepare(dev);
  651. suspend_report_result(dev->bus->pm->prepare, error);
  652. if (error)
  653. goto End;
  654. }
  655. if (dev->type && dev->type->pm && dev->type->pm->prepare) {
  656. pm_dev_dbg(dev, state, "preparing type ");
  657. error = dev->type->pm->prepare(dev);
  658. suspend_report_result(dev->type->pm->prepare, error);
  659. if (error)
  660. goto End;
  661. }
  662. if (dev->class && dev->class->pm && dev->class->pm->prepare) {
  663. pm_dev_dbg(dev, state, "preparing class ");
  664. error = dev->class->pm->prepare(dev);
  665. suspend_report_result(dev->class->pm->prepare, error);
  666. }
  667. End:
  668. up(&dev->sem);
  669. return error;
  670. }
  671. /**
  672. * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
  673. * @state: PM transition of the system being carried out.
  674. *
  675. * Execute the ->prepare() callback(s) for all devices.
  676. */
  677. static int dpm_prepare(pm_message_t state)
  678. {
  679. struct list_head list;
  680. int error = 0;
  681. INIT_LIST_HEAD(&list);
  682. mutex_lock(&dpm_list_mtx);
  683. transition_started = true;
  684. while (!list_empty(&dpm_list)) {
  685. struct device *dev = to_device(dpm_list.next);
  686. get_device(dev);
  687. dev->power.status = DPM_PREPARING;
  688. mutex_unlock(&dpm_list_mtx);
  689. pm_runtime_get_noresume(dev);
  690. if (pm_runtime_barrier(dev) && device_may_wakeup(dev)) {
  691. /* Wake-up requested during system sleep transition. */
  692. pm_runtime_put_noidle(dev);
  693. error = -EBUSY;
  694. } else {
  695. error = device_prepare(dev, state);
  696. }
  697. mutex_lock(&dpm_list_mtx);
  698. if (error) {
  699. dev->power.status = DPM_ON;
  700. if (error == -EAGAIN) {
  701. put_device(dev);
  702. error = 0;
  703. continue;
  704. }
  705. printk(KERN_ERR "PM: Failed to prepare device %s "
  706. "for power transition: error %d\n",
  707. kobject_name(&dev->kobj), error);
  708. put_device(dev);
  709. break;
  710. }
  711. dev->power.status = DPM_SUSPENDING;
  712. if (!list_empty(&dev->power.entry))
  713. list_move_tail(&dev->power.entry, &list);
  714. put_device(dev);
  715. }
  716. list_splice(&list, &dpm_list);
  717. mutex_unlock(&dpm_list_mtx);
  718. return error;
  719. }
  720. /**
  721. * dpm_suspend_start - Prepare devices for PM transition and suspend them.
  722. * @state: PM transition of the system being carried out.
  723. *
  724. * Prepare all non-sysdev devices for system PM transition and execute "suspend"
  725. * callbacks for them.
  726. */
  727. int dpm_suspend_start(pm_message_t state)
  728. {
  729. int error;
  730. might_sleep();
  731. error = dpm_prepare(state);
  732. if (!error)
  733. error = dpm_suspend(state);
  734. return error;
  735. }
  736. EXPORT_SYMBOL_GPL(dpm_suspend_start);
  737. void __suspend_report_result(const char *function, void *fn, int ret)
  738. {
  739. if (ret)
  740. printk(KERN_ERR "%s(): %pF returns %d\n", function, fn, ret);
  741. }
  742. EXPORT_SYMBOL_GPL(__suspend_report_result);