apm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * bios-less APM driver for hp680
  3. *
  4. * Copyright 2005 (c) Andriy Skulysh <askulysh@gmail.com>
  5. *
  6. * based on ARM APM driver by
  7. * Jamey Hicks <jamey@crl.dec.com>
  8. *
  9. * adapted from the APM BIOS driver for Linux by
  10. * Stephen Rothwell (sfr@linuxcare.com)
  11. *
  12. * APM 1.2 Reference:
  13. * Intel Corporation, Microsoft Corporation. Advanced Power Management
  14. * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
  15. *
  16. * [This document is available from Microsoft at:
  17. * http://www.microsoft.com/hwdev/busbios/amp_12.htm]
  18. */
  19. #include <linux/config.h>
  20. #include <linux/module.h>
  21. #include <linux/poll.h>
  22. #include <linux/timer.h>
  23. #include <linux/slab.h>
  24. #include <linux/proc_fs.h>
  25. #include <linux/miscdevice.h>
  26. #include <linux/apm_bios.h>
  27. #include <linux/pm.h>
  28. #include <linux/pm_legacy.h>
  29. #include <asm/apm.h>
  30. #define MODNAME "apm"
  31. /*
  32. * The apm_bios device is one of the misc char devices.
  33. * This is its minor number.
  34. */
  35. #define APM_MINOR_DEV 134
  36. /*
  37. * Maximum number of events stored
  38. */
  39. #define APM_MAX_EVENTS 16
  40. struct apm_queue {
  41. unsigned int event_head;
  42. unsigned int event_tail;
  43. apm_event_t events[APM_MAX_EVENTS];
  44. };
  45. /*
  46. * The per-file APM data
  47. */
  48. struct apm_user {
  49. struct list_head list;
  50. unsigned int suser: 1;
  51. unsigned int writer: 1;
  52. unsigned int reader: 1;
  53. int suspend_result;
  54. unsigned int suspend_state;
  55. #define SUSPEND_NONE 0 /* no suspend pending */
  56. #define SUSPEND_PENDING 1 /* suspend pending read */
  57. #define SUSPEND_READ 2 /* suspend read, pending ack */
  58. #define SUSPEND_ACKED 3 /* suspend acked */
  59. #define SUSPEND_DONE 4 /* suspend completed */
  60. struct apm_queue queue;
  61. };
  62. /*
  63. * Local variables
  64. */
  65. static int suspends_pending;
  66. static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
  67. static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
  68. /*
  69. * This is a list of everyone who has opened /dev/apm_bios
  70. */
  71. static DECLARE_RWSEM(user_list_lock);
  72. static LIST_HEAD(apm_user_list);
  73. /*
  74. * kapmd info. kapmd provides us a process context to handle
  75. * "APM" events within - specifically necessary if we're going
  76. * to be suspending the system.
  77. */
  78. static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
  79. static DECLARE_COMPLETION(kapmd_exit);
  80. static DEFINE_SPINLOCK(kapmd_queue_lock);
  81. static struct apm_queue kapmd_queue;
  82. int apm_suspended;
  83. EXPORT_SYMBOL(apm_suspended);
  84. /* Platform-specific apm_read_proc(). */
  85. int (*apm_get_info)(char *buf, char **start, off_t fpos, int length);
  86. EXPORT_SYMBOL(apm_get_info);
  87. /*
  88. * APM event queue management.
  89. */
  90. static inline int queue_empty(struct apm_queue *q)
  91. {
  92. return q->event_head == q->event_tail;
  93. }
  94. static inline apm_event_t queue_get_event(struct apm_queue *q)
  95. {
  96. q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
  97. return q->events[q->event_tail];
  98. }
  99. static void queue_add_event(struct apm_queue *q, apm_event_t event)
  100. {
  101. q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
  102. if (q->event_head == q->event_tail) {
  103. static int notified;
  104. if (notified++ == 0)
  105. printk(KERN_ERR "apm: an event queue overflowed\n");
  106. q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
  107. }
  108. q->events[q->event_head] = event;
  109. }
  110. static void queue_event_one_user(struct apm_user *as, apm_event_t event)
  111. {
  112. if (as->suser && as->writer) {
  113. switch (event) {
  114. case APM_SYS_SUSPEND:
  115. case APM_USER_SUSPEND:
  116. /*
  117. * If this user already has a suspend pending,
  118. * don't queue another one.
  119. */
  120. if (as->suspend_state != SUSPEND_NONE)
  121. return;
  122. as->suspend_state = SUSPEND_PENDING;
  123. suspends_pending++;
  124. break;
  125. }
  126. }
  127. queue_add_event(&as->queue, event);
  128. }
  129. static void queue_event(apm_event_t event, struct apm_user *sender)
  130. {
  131. struct apm_user *as;
  132. down_read(&user_list_lock);
  133. list_for_each_entry(as, &apm_user_list, list)
  134. if (as != sender && as->reader)
  135. queue_event_one_user(as, event);
  136. up_read(&user_list_lock);
  137. wake_up_interruptible(&apm_waitqueue);
  138. }
  139. /**
  140. * apm_queue_event - queue an APM event for kapmd
  141. * @event: APM event
  142. *
  143. * Queue an APM event for kapmd to process and ultimately take the
  144. * appropriate action. Only a subset of events are handled:
  145. * %APM_LOW_BATTERY
  146. * %APM_POWER_STATUS_CHANGE
  147. * %APM_USER_SUSPEND
  148. * %APM_SYS_SUSPEND
  149. * %APM_CRITICAL_SUSPEND
  150. */
  151. void apm_queue_event(apm_event_t event)
  152. {
  153. spin_lock_irq(&kapmd_queue_lock);
  154. queue_add_event(&kapmd_queue, event);
  155. spin_unlock_irq(&kapmd_queue_lock);
  156. wake_up_interruptible(&kapmd_wait);
  157. }
  158. EXPORT_SYMBOL(apm_queue_event);
  159. static void apm_suspend(void)
  160. {
  161. struct apm_user *as;
  162. int err;
  163. apm_suspended = 1;
  164. err = pm_suspend(PM_SUSPEND_MEM);
  165. /*
  166. * Anyone on the APM queues will think we're still suspended.
  167. * Send a message so everyone knows we're now awake again.
  168. */
  169. queue_event(APM_NORMAL_RESUME, NULL);
  170. /*
  171. * Finally, wake up anyone who is sleeping on the suspend.
  172. */
  173. down_read(&user_list_lock);
  174. list_for_each_entry(as, &apm_user_list, list) {
  175. as->suspend_result = err;
  176. as->suspend_state = SUSPEND_DONE;
  177. }
  178. up_read(&user_list_lock);
  179. wake_up(&apm_suspend_waitqueue);
  180. apm_suspended = 0;
  181. }
  182. static ssize_t apm_read(struct file *fp, char __user *buf,
  183. size_t count, loff_t *ppos)
  184. {
  185. struct apm_user *as = fp->private_data;
  186. apm_event_t event;
  187. int i = count, ret = 0;
  188. if (count < sizeof(apm_event_t))
  189. return -EINVAL;
  190. if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
  191. return -EAGAIN;
  192. wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
  193. while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
  194. event = queue_get_event(&as->queue);
  195. ret = -EFAULT;
  196. if (copy_to_user(buf, &event, sizeof(event)))
  197. break;
  198. if (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND)
  199. as->suspend_state = SUSPEND_READ;
  200. buf += sizeof(event);
  201. i -= sizeof(event);
  202. }
  203. if (i < count)
  204. ret = count - i;
  205. return ret;
  206. }
  207. static unsigned int apm_poll(struct file *fp, poll_table * wait)
  208. {
  209. struct apm_user *as = fp->private_data;
  210. poll_wait(fp, &apm_waitqueue, wait);
  211. return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
  212. }
  213. /*
  214. * apm_ioctl - handle APM ioctl
  215. *
  216. * APM_IOC_SUSPEND
  217. * This IOCTL is overloaded, and performs two functions. It is used to:
  218. * - initiate a suspend
  219. * - acknowledge a suspend read from /dev/apm_bios.
  220. * Only when everyone who has opened /dev/apm_bios with write permission
  221. * has acknowledge does the actual suspend happen.
  222. */
  223. static int
  224. apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
  225. {
  226. struct apm_user *as = filp->private_data;
  227. unsigned long flags;
  228. int err = -EINVAL;
  229. if (!as->suser || !as->writer)
  230. return -EPERM;
  231. switch (cmd) {
  232. case APM_IOC_SUSPEND:
  233. as->suspend_result = -EINTR;
  234. if (as->suspend_state == SUSPEND_READ) {
  235. /*
  236. * If we read a suspend command from /dev/apm_bios,
  237. * then the corresponding APM_IOC_SUSPEND ioctl is
  238. * interpreted as an acknowledge.
  239. */
  240. as->suspend_state = SUSPEND_ACKED;
  241. suspends_pending--;
  242. } else {
  243. /*
  244. * Otherwise it is a request to suspend the system.
  245. * Queue an event for all readers, and expect an
  246. * acknowledge from all writers who haven't already
  247. * acknowledged.
  248. */
  249. queue_event(APM_USER_SUSPEND, as);
  250. }
  251. /*
  252. * If there are no further acknowledges required, suspend
  253. * the system.
  254. */
  255. if (suspends_pending == 0)
  256. apm_suspend();
  257. /*
  258. * Wait for the suspend/resume to complete. If there are
  259. * pending acknowledges, we wait here for them.
  260. *
  261. * Note that we need to ensure that the PM subsystem does
  262. * not kick us out of the wait when it suspends the threads.
  263. */
  264. flags = current->flags;
  265. current->flags |= PF_NOFREEZE;
  266. /*
  267. * Note: do not allow a thread which is acking the suspend
  268. * to escape until the resume is complete.
  269. */
  270. if (as->suspend_state == SUSPEND_ACKED)
  271. wait_event(apm_suspend_waitqueue,
  272. as->suspend_state == SUSPEND_DONE);
  273. else
  274. wait_event_interruptible(apm_suspend_waitqueue,
  275. as->suspend_state == SUSPEND_DONE);
  276. current->flags = flags;
  277. err = as->suspend_result;
  278. as->suspend_state = SUSPEND_NONE;
  279. break;
  280. }
  281. return err;
  282. }
  283. static int apm_release(struct inode * inode, struct file * filp)
  284. {
  285. struct apm_user *as = filp->private_data;
  286. filp->private_data = NULL;
  287. down_write(&user_list_lock);
  288. list_del(&as->list);
  289. up_write(&user_list_lock);
  290. /*
  291. * We are now unhooked from the chain. As far as new
  292. * events are concerned, we no longer exist. However, we
  293. * need to balance suspends_pending, which means the
  294. * possibility of sleeping.
  295. */
  296. if (as->suspend_state != SUSPEND_NONE) {
  297. suspends_pending -= 1;
  298. if (suspends_pending == 0)
  299. apm_suspend();
  300. }
  301. kfree(as);
  302. return 0;
  303. }
  304. static int apm_open(struct inode * inode, struct file * filp)
  305. {
  306. struct apm_user *as;
  307. as = kzalloc(sizeof(*as), GFP_KERNEL);
  308. if (as) {
  309. /*
  310. * XXX - this is a tiny bit broken, when we consider BSD
  311. * process accounting. If the device is opened by root, we
  312. * instantly flag that we used superuser privs. Who knows,
  313. * we might close the device immediately without doing a
  314. * privileged operation -- cevans
  315. */
  316. as->suser = capable(CAP_SYS_ADMIN);
  317. as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
  318. as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
  319. down_write(&user_list_lock);
  320. list_add(&as->list, &apm_user_list);
  321. up_write(&user_list_lock);
  322. filp->private_data = as;
  323. }
  324. return as ? 0 : -ENOMEM;
  325. }
  326. static struct file_operations apm_bios_fops = {
  327. .owner = THIS_MODULE,
  328. .read = apm_read,
  329. .poll = apm_poll,
  330. .ioctl = apm_ioctl,
  331. .open = apm_open,
  332. .release = apm_release,
  333. };
  334. static struct miscdevice apm_device = {
  335. .minor = APM_MINOR_DEV,
  336. .name = "apm_bios",
  337. .fops = &apm_bios_fops
  338. };
  339. #ifdef CONFIG_PROC_FS
  340. /*
  341. * Arguments, with symbols from linux/apm_bios.h.
  342. *
  343. * 0) Linux driver version (this will change if format changes)
  344. * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
  345. * 2) APM flags from APM Installation Check (0x00):
  346. * bit 0: APM_16_BIT_SUPPORT
  347. * bit 1: APM_32_BIT_SUPPORT
  348. * bit 2: APM_IDLE_SLOWS_CLOCK
  349. * bit 3: APM_BIOS_DISABLED
  350. * bit 4: APM_BIOS_DISENGAGED
  351. * 3) AC line status
  352. * 0x00: Off-line
  353. * 0x01: On-line
  354. * 0x02: On backup power (BIOS >= 1.1 only)
  355. * 0xff: Unknown
  356. * 4) Battery status
  357. * 0x00: High
  358. * 0x01: Low
  359. * 0x02: Critical
  360. * 0x03: Charging
  361. * 0x04: Selected battery not present (BIOS >= 1.2 only)
  362. * 0xff: Unknown
  363. * 5) Battery flag
  364. * bit 0: High
  365. * bit 1: Low
  366. * bit 2: Critical
  367. * bit 3: Charging
  368. * bit 7: No system battery
  369. * 0xff: Unknown
  370. * 6) Remaining battery life (percentage of charge):
  371. * 0-100: valid
  372. * -1: Unknown
  373. * 7) Remaining battery life (time units):
  374. * Number of remaining minutes or seconds
  375. * -1: Unknown
  376. * 8) min = minutes; sec = seconds
  377. */
  378. static int apm_read_proc(char *buf, char **start, off_t fpos, int length)
  379. {
  380. if (likely(apm_get_info))
  381. return apm_get_info(buf, start, fpos, length);
  382. return -EINVAL;
  383. }
  384. #endif
  385. static int kapmd(void *arg)
  386. {
  387. daemonize("kapmd");
  388. current->flags |= PF_NOFREEZE;
  389. do {
  390. apm_event_t event;
  391. wait_event_interruptible(kapmd_wait,
  392. !queue_empty(&kapmd_queue) || !pm_active);
  393. if (!pm_active)
  394. break;
  395. spin_lock_irq(&kapmd_queue_lock);
  396. event = 0;
  397. if (!queue_empty(&kapmd_queue))
  398. event = queue_get_event(&kapmd_queue);
  399. spin_unlock_irq(&kapmd_queue_lock);
  400. switch (event) {
  401. case 0:
  402. break;
  403. case APM_LOW_BATTERY:
  404. case APM_POWER_STATUS_CHANGE:
  405. queue_event(event, NULL);
  406. break;
  407. case APM_USER_SUSPEND:
  408. case APM_SYS_SUSPEND:
  409. queue_event(event, NULL);
  410. if (suspends_pending == 0)
  411. apm_suspend();
  412. break;
  413. case APM_CRITICAL_SUSPEND:
  414. apm_suspend();
  415. break;
  416. }
  417. } while (1);
  418. complete_and_exit(&kapmd_exit, 0);
  419. }
  420. static int __init apm_init(void)
  421. {
  422. int ret;
  423. pm_active = 1;
  424. ret = kernel_thread(kapmd, NULL, CLONE_KERNEL);
  425. if (unlikely(ret < 0)) {
  426. pm_active = 0;
  427. return ret;
  428. }
  429. create_proc_info_entry("apm", 0, NULL, apm_read_proc);
  430. ret = misc_register(&apm_device);
  431. if (unlikely(ret != 0)) {
  432. remove_proc_entry("apm", NULL);
  433. pm_active = 0;
  434. wake_up(&kapmd_wait);
  435. wait_for_completion(&kapmd_exit);
  436. }
  437. return ret;
  438. }
  439. static void __exit apm_exit(void)
  440. {
  441. misc_deregister(&apm_device);
  442. remove_proc_entry("apm", NULL);
  443. pm_active = 0;
  444. wake_up(&kapmd_wait);
  445. wait_for_completion(&kapmd_exit);
  446. }
  447. module_init(apm_init);
  448. module_exit(apm_exit);
  449. MODULE_AUTHOR("Stephen Rothwell, Andriy Skulysh");
  450. MODULE_DESCRIPTION("Advanced Power Management");
  451. MODULE_LICENSE("GPL");