pm.c 6.0 KB

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