pm.c 7.3 KB

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