pm.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * pm.c - Power management interface
  3. *
  4. * Copyright (C) 2000 Andrew Henroid
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/init.h>
  21. #include <linux/module.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/mm.h>
  24. #include <linux/slab.h>
  25. #include <linux/pm.h>
  26. #include <linux/pm_legacy.h>
  27. #include <linux/interrupt.h>
  28. int pm_active;
  29. /*
  30. * Locking notes:
  31. * pm_devs_lock can be a semaphore providing pm ops are not called
  32. * from an interrupt handler (already a bad idea so no change here). Each
  33. * change must be protected so that an unlink of an entry doesn't clash
  34. * with a pm send - which is permitted to sleep in the current architecture
  35. *
  36. * Module unloads clashing with pm events now work out safely, the module
  37. * unload path will block until the event has been sent. It may well block
  38. * until a resume but that will be fine.
  39. */
  40. static DECLARE_MUTEX(pm_devs_lock);
  41. static LIST_HEAD(pm_devs);
  42. /**
  43. * pm_register - register a device with power management
  44. * @type: device type
  45. * @id: device ID
  46. * @callback: callback function
  47. *
  48. * Add a device to the list of devices that wish to be notified about
  49. * power management events. A &pm_dev structure is returned on success,
  50. * on failure the return is %NULL.
  51. *
  52. * The callback function will be called in process context and
  53. * it may sleep.
  54. */
  55. struct pm_dev *pm_register(pm_dev_t type,
  56. unsigned long id,
  57. pm_callback callback)
  58. {
  59. struct pm_dev *dev = kzalloc(sizeof(struct pm_dev), GFP_KERNEL);
  60. if (dev) {
  61. dev->type = type;
  62. dev->id = id;
  63. dev->callback = callback;
  64. down(&pm_devs_lock);
  65. list_add(&dev->entry, &pm_devs);
  66. up(&pm_devs_lock);
  67. }
  68. return dev;
  69. }
  70. /**
  71. * pm_unregister - unregister a device with power management
  72. * @dev: device to unregister
  73. *
  74. * Remove a device from the power management notification lists. The
  75. * dev passed must be a handle previously returned by pm_register.
  76. */
  77. void pm_unregister(struct pm_dev *dev)
  78. {
  79. if (dev) {
  80. down(&pm_devs_lock);
  81. list_del(&dev->entry);
  82. up(&pm_devs_lock);
  83. kfree(dev);
  84. }
  85. }
  86. static void __pm_unregister(struct pm_dev *dev)
  87. {
  88. if (dev) {
  89. list_del(&dev->entry);
  90. kfree(dev);
  91. }
  92. }
  93. /**
  94. * pm_unregister_all - unregister all devices with matching callback
  95. * @callback: callback function pointer
  96. *
  97. * Unregister every device that would call the callback passed. This
  98. * is primarily meant as a helper function for loadable modules. It
  99. * enables a module to give up all its managed devices without keeping
  100. * its own private list.
  101. */
  102. void pm_unregister_all(pm_callback callback)
  103. {
  104. struct list_head *entry;
  105. if (!callback)
  106. return;
  107. down(&pm_devs_lock);
  108. entry = pm_devs.next;
  109. while (entry != &pm_devs) {
  110. struct pm_dev *dev = list_entry(entry, struct pm_dev, entry);
  111. entry = entry->next;
  112. if (dev->callback == callback)
  113. __pm_unregister(dev);
  114. }
  115. up(&pm_devs_lock);
  116. }
  117. /**
  118. * pm_send - send request to a single device
  119. * @dev: device to send to
  120. * @rqst: power management request
  121. * @data: data for the callback
  122. *
  123. * Issue a power management request to a given device. The
  124. * %PM_SUSPEND and %PM_RESUME events are handled specially. The
  125. * data field must hold the intended next state. No call is made
  126. * if the state matches.
  127. *
  128. * BUGS: what stops two power management requests occurring in parallel
  129. * and conflicting.
  130. *
  131. * WARNING: Calling pm_send directly is not generally recommended, in
  132. * particular there is no locking against the pm_dev going away. The
  133. * caller must maintain all needed locking or have 'inside knowledge'
  134. * on the safety. Also remember that this function is not locked against
  135. * pm_unregister. This means that you must handle SMP races on callback
  136. * execution and unload yourself.
  137. */
  138. static int pm_send(struct pm_dev *dev, pm_request_t rqst, void *data)
  139. {
  140. int status = 0;
  141. unsigned long prev_state, next_state;
  142. if (in_interrupt())
  143. BUG();
  144. switch (rqst) {
  145. case PM_SUSPEND:
  146. case PM_RESUME:
  147. prev_state = dev->state;
  148. next_state = (unsigned long) data;
  149. if (prev_state != next_state) {
  150. if (dev->callback)
  151. status = (*dev->callback)(dev, rqst, data);
  152. if (!status) {
  153. dev->state = next_state;
  154. dev->prev_state = prev_state;
  155. }
  156. }
  157. else {
  158. dev->prev_state = prev_state;
  159. }
  160. break;
  161. default:
  162. if (dev->callback)
  163. status = (*dev->callback)(dev, rqst, data);
  164. break;
  165. }
  166. return status;
  167. }
  168. /*
  169. * Undo incomplete request
  170. */
  171. static void pm_undo_all(struct pm_dev *last)
  172. {
  173. struct list_head *entry = last->entry.prev;
  174. while (entry != &pm_devs) {
  175. struct pm_dev *dev = list_entry(entry, struct pm_dev, entry);
  176. if (dev->state != dev->prev_state) {
  177. /* previous state was zero (running) resume or
  178. * previous state was non-zero (suspended) suspend
  179. */
  180. pm_request_t undo = (dev->prev_state
  181. ? PM_SUSPEND:PM_RESUME);
  182. pm_send(dev, undo, (void*) dev->prev_state);
  183. }
  184. entry = entry->prev;
  185. }
  186. }
  187. /**
  188. * pm_send_all - send request to all managed devices
  189. * @rqst: power management request
  190. * @data: data for the callback
  191. *
  192. * Issue a power management request to a all devices. The
  193. * %PM_SUSPEND events are handled specially. Any device is
  194. * permitted to fail a suspend by returning a non zero (error)
  195. * value from its callback function. If any device vetoes a
  196. * suspend request then all other devices that have suspended
  197. * during the processing of this request are restored to their
  198. * previous state.
  199. *
  200. * WARNING: This function takes the pm_devs_lock. The lock is not dropped until
  201. * the callbacks have completed. This prevents races against pm locking
  202. * functions, races against module unload pm_unregister code. It does
  203. * mean however that you must not issue pm_ functions within the callback
  204. * or you will deadlock and users will hate you.
  205. *
  206. * Zero is returned on success. If a suspend fails then the status
  207. * from the device that vetoes the suspend is returned.
  208. *
  209. * BUGS: what stops two power management requests occurring in parallel
  210. * and conflicting.
  211. */
  212. int pm_send_all(pm_request_t rqst, void *data)
  213. {
  214. struct list_head *entry;
  215. down(&pm_devs_lock);
  216. entry = pm_devs.next;
  217. while (entry != &pm_devs) {
  218. struct pm_dev *dev = list_entry(entry, struct pm_dev, entry);
  219. if (dev->callback) {
  220. int status = pm_send(dev, rqst, data);
  221. if (status) {
  222. /* return devices to previous state on
  223. * failed suspend request
  224. */
  225. if (rqst == PM_SUSPEND)
  226. pm_undo_all(dev);
  227. up(&pm_devs_lock);
  228. return status;
  229. }
  230. }
  231. entry = entry->next;
  232. }
  233. up(&pm_devs_lock);
  234. return 0;
  235. }
  236. EXPORT_SYMBOL(pm_register);
  237. EXPORT_SYMBOL(pm_unregister);
  238. EXPORT_SYMBOL(pm_unregister_all);
  239. EXPORT_SYMBOL(pm_send_all);
  240. EXPORT_SYMBOL(pm_active);