pm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_send - send request to a single device
  73. * @dev: device to send to
  74. * @rqst: power management request
  75. * @data: data for the callback
  76. *
  77. * Issue a power management request to a given device. The
  78. * %PM_SUSPEND and %PM_RESUME events are handled specially. The
  79. * data field must hold the intended next state. No call is made
  80. * if the state matches.
  81. *
  82. * BUGS: what stops two power management requests occurring in parallel
  83. * and conflicting.
  84. *
  85. * WARNING: Calling pm_send directly is not generally recommended, in
  86. * particular there is no locking against the pm_dev going away. The
  87. * caller must maintain all needed locking or have 'inside knowledge'
  88. * on the safety. Also remember that this function is not locked against
  89. * pm_unregister. This means that you must handle SMP races on callback
  90. * execution and unload yourself.
  91. */
  92. static int pm_send(struct pm_dev *dev, pm_request_t rqst, void *data)
  93. {
  94. int status = 0;
  95. unsigned long prev_state, next_state;
  96. if (in_interrupt())
  97. BUG();
  98. switch (rqst) {
  99. case PM_SUSPEND:
  100. case PM_RESUME:
  101. prev_state = dev->state;
  102. next_state = (unsigned long) data;
  103. if (prev_state != next_state) {
  104. if (dev->callback)
  105. status = (*dev->callback)(dev, rqst, data);
  106. if (!status) {
  107. dev->state = next_state;
  108. dev->prev_state = prev_state;
  109. }
  110. }
  111. else {
  112. dev->prev_state = prev_state;
  113. }
  114. break;
  115. default:
  116. if (dev->callback)
  117. status = (*dev->callback)(dev, rqst, data);
  118. break;
  119. }
  120. return status;
  121. }
  122. /*
  123. * Undo incomplete request
  124. */
  125. static void pm_undo_all(struct pm_dev *last)
  126. {
  127. struct list_head *entry = last->entry.prev;
  128. while (entry != &pm_devs) {
  129. struct pm_dev *dev = list_entry(entry, struct pm_dev, entry);
  130. if (dev->state != dev->prev_state) {
  131. /* previous state was zero (running) resume or
  132. * previous state was non-zero (suspended) suspend
  133. */
  134. pm_request_t undo = (dev->prev_state
  135. ? PM_SUSPEND:PM_RESUME);
  136. pm_send(dev, undo, (void*) dev->prev_state);
  137. }
  138. entry = entry->prev;
  139. }
  140. }
  141. /**
  142. * pm_send_all - send request to all managed devices
  143. * @rqst: power management request
  144. * @data: data for the callback
  145. *
  146. * Issue a power management request to a all devices. The
  147. * %PM_SUSPEND events are handled specially. Any device is
  148. * permitted to fail a suspend by returning a non zero (error)
  149. * value from its callback function. If any device vetoes a
  150. * suspend request then all other devices that have suspended
  151. * during the processing of this request are restored to their
  152. * previous state.
  153. *
  154. * WARNING: This function takes the pm_devs_lock. The lock is not dropped until
  155. * the callbacks have completed. This prevents races against pm locking
  156. * functions, races against module unload pm_unregister code. It does
  157. * mean however that you must not issue pm_ functions within the callback
  158. * or you will deadlock and users will hate you.
  159. *
  160. * Zero is returned on success. If a suspend fails then the status
  161. * from the device that vetoes the suspend is returned.
  162. *
  163. * BUGS: what stops two power management requests occurring in parallel
  164. * and conflicting.
  165. */
  166. int pm_send_all(pm_request_t rqst, void *data)
  167. {
  168. struct list_head *entry;
  169. mutex_lock(&pm_devs_lock);
  170. entry = pm_devs.next;
  171. while (entry != &pm_devs) {
  172. struct pm_dev *dev = list_entry(entry, struct pm_dev, entry);
  173. if (dev->callback) {
  174. int status = pm_send(dev, rqst, data);
  175. if (status) {
  176. /* return devices to previous state on
  177. * failed suspend request
  178. */
  179. if (rqst == PM_SUSPEND)
  180. pm_undo_all(dev);
  181. mutex_unlock(&pm_devs_lock);
  182. return status;
  183. }
  184. }
  185. entry = entry->next;
  186. }
  187. mutex_unlock(&pm_devs_lock);
  188. return 0;
  189. }
  190. EXPORT_SYMBOL(pm_register);
  191. EXPORT_SYMBOL(pm_send_all);
  192. EXPORT_SYMBOL(pm_active);