pm.c 7.1 KB

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