apm.c 12 KB

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