apm-emulation.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*
  2. * bios-less APM driver for ARM Linux
  3. * Jamey Hicks <jamey@crl.dec.com>
  4. * adapted from the APM BIOS driver for Linux by Stephen Rothwell (sfr@linuxcare.com)
  5. *
  6. * APM 1.2 Reference:
  7. * Intel Corporation, Microsoft Corporation. Advanced Power Management
  8. * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
  9. *
  10. * [This document is available from Microsoft at:
  11. * http://www.microsoft.com/hwdev/busbios/amp_12.htm]
  12. */
  13. #include <linux/module.h>
  14. #include <linux/poll.h>
  15. #include <linux/slab.h>
  16. #include <linux/smp_lock.h>
  17. #include <linux/proc_fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/miscdevice.h>
  20. #include <linux/apm_bios.h>
  21. #include <linux/capability.h>
  22. #include <linux/sched.h>
  23. #include <linux/suspend.h>
  24. #include <linux/apm-emulation.h>
  25. #include <linux/freezer.h>
  26. #include <linux/device.h>
  27. #include <linux/kernel.h>
  28. #include <linux/list.h>
  29. #include <linux/init.h>
  30. #include <linux/completion.h>
  31. #include <linux/kthread.h>
  32. #include <linux/delay.h>
  33. #include <asm/system.h>
  34. /*
  35. * The apm_bios device is one of the misc char devices.
  36. * This is its minor number.
  37. */
  38. #define APM_MINOR_DEV 134
  39. /*
  40. * See Documentation/Config.help for the configuration options.
  41. *
  42. * Various options can be changed at boot time as follows:
  43. * (We allow underscores for compatibility with the modules code)
  44. * apm=on/off enable/disable APM
  45. */
  46. /*
  47. * Maximum number of events stored
  48. */
  49. #define APM_MAX_EVENTS 16
  50. struct apm_queue {
  51. unsigned int event_head;
  52. unsigned int event_tail;
  53. apm_event_t events[APM_MAX_EVENTS];
  54. };
  55. /*
  56. * The per-file APM data
  57. */
  58. struct apm_user {
  59. struct list_head list;
  60. unsigned int suser: 1;
  61. unsigned int writer: 1;
  62. unsigned int reader: 1;
  63. int suspend_result;
  64. unsigned int suspend_state;
  65. #define SUSPEND_NONE 0 /* no suspend pending */
  66. #define SUSPEND_PENDING 1 /* suspend pending read */
  67. #define SUSPEND_READ 2 /* suspend read, pending ack */
  68. #define SUSPEND_ACKED 3 /* suspend acked */
  69. #define SUSPEND_WAIT 4 /* waiting for suspend */
  70. #define SUSPEND_DONE 5 /* suspend completed */
  71. struct apm_queue queue;
  72. };
  73. /*
  74. * Local variables
  75. */
  76. static int suspends_pending;
  77. static int apm_disabled;
  78. static struct task_struct *kapmd_tsk;
  79. static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
  80. static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
  81. /*
  82. * This is a list of everyone who has opened /dev/apm_bios
  83. */
  84. static DECLARE_RWSEM(user_list_lock);
  85. static LIST_HEAD(apm_user_list);
  86. /*
  87. * kapmd info. kapmd provides us a process context to handle
  88. * "APM" events within - specifically necessary if we're going
  89. * to be suspending the system.
  90. */
  91. static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
  92. static DEFINE_SPINLOCK(kapmd_queue_lock);
  93. static struct apm_queue kapmd_queue;
  94. static DEFINE_MUTEX(state_lock);
  95. static const char driver_version[] = "1.13"; /* no spaces */
  96. /*
  97. * Compatibility cruft until the IPAQ people move over to the new
  98. * interface.
  99. */
  100. static void __apm_get_power_status(struct apm_power_info *info)
  101. {
  102. }
  103. /*
  104. * This allows machines to provide their own "apm get power status" function.
  105. */
  106. void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
  107. EXPORT_SYMBOL(apm_get_power_status);
  108. /*
  109. * APM event queue management.
  110. */
  111. static inline int queue_empty(struct apm_queue *q)
  112. {
  113. return q->event_head == q->event_tail;
  114. }
  115. static inline apm_event_t queue_get_event(struct apm_queue *q)
  116. {
  117. q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
  118. return q->events[q->event_tail];
  119. }
  120. static void queue_add_event(struct apm_queue *q, apm_event_t event)
  121. {
  122. q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
  123. if (q->event_head == q->event_tail) {
  124. static int notified;
  125. if (notified++ == 0)
  126. printk(KERN_ERR "apm: an event queue overflowed\n");
  127. q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
  128. }
  129. q->events[q->event_head] = event;
  130. }
  131. static void queue_event(apm_event_t event)
  132. {
  133. struct apm_user *as;
  134. down_read(&user_list_lock);
  135. list_for_each_entry(as, &apm_user_list, list) {
  136. if (as->reader)
  137. queue_add_event(&as->queue, event);
  138. }
  139. up_read(&user_list_lock);
  140. wake_up_interruptible(&apm_waitqueue);
  141. }
  142. /*
  143. * queue_suspend_event - queue an APM suspend event.
  144. *
  145. * Check that we're in a state where we can suspend. If not,
  146. * return -EBUSY. Otherwise, queue an event to all "writer"
  147. * users. If there are no "writer" users, return '1' to
  148. * indicate that we can immediately suspend.
  149. */
  150. static int queue_suspend_event(apm_event_t event, struct apm_user *sender)
  151. {
  152. struct apm_user *as;
  153. int ret = 1;
  154. mutex_lock(&state_lock);
  155. down_read(&user_list_lock);
  156. /*
  157. * If a thread is still processing, we can't suspend, so reject
  158. * the request.
  159. */
  160. list_for_each_entry(as, &apm_user_list, list) {
  161. if (as != sender && as->reader && as->writer && as->suser &&
  162. as->suspend_state != SUSPEND_NONE) {
  163. ret = -EBUSY;
  164. goto out;
  165. }
  166. }
  167. list_for_each_entry(as, &apm_user_list, list) {
  168. if (as != sender && as->reader && as->writer && as->suser) {
  169. as->suspend_state = SUSPEND_PENDING;
  170. suspends_pending++;
  171. queue_add_event(&as->queue, event);
  172. ret = 0;
  173. }
  174. }
  175. out:
  176. up_read(&user_list_lock);
  177. mutex_unlock(&state_lock);
  178. wake_up_interruptible(&apm_waitqueue);
  179. return ret;
  180. }
  181. static void apm_suspend(void)
  182. {
  183. struct apm_user *as;
  184. int err = pm_suspend(PM_SUSPEND_MEM);
  185. /*
  186. * Anyone on the APM queues will think we're still suspended.
  187. * Send a message so everyone knows we're now awake again.
  188. */
  189. queue_event(APM_NORMAL_RESUME);
  190. /*
  191. * Finally, wake up anyone who is sleeping on the suspend.
  192. */
  193. mutex_lock(&state_lock);
  194. down_read(&user_list_lock);
  195. list_for_each_entry(as, &apm_user_list, list) {
  196. if (as->suspend_state == SUSPEND_WAIT ||
  197. as->suspend_state == SUSPEND_ACKED) {
  198. as->suspend_result = err;
  199. as->suspend_state = SUSPEND_DONE;
  200. }
  201. }
  202. up_read(&user_list_lock);
  203. mutex_unlock(&state_lock);
  204. wake_up(&apm_suspend_waitqueue);
  205. }
  206. static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
  207. {
  208. struct apm_user *as = fp->private_data;
  209. apm_event_t event;
  210. int i = count, ret = 0;
  211. if (count < sizeof(apm_event_t))
  212. return -EINVAL;
  213. if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
  214. return -EAGAIN;
  215. wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
  216. while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
  217. event = queue_get_event(&as->queue);
  218. ret = -EFAULT;
  219. if (copy_to_user(buf, &event, sizeof(event)))
  220. break;
  221. mutex_lock(&state_lock);
  222. if (as->suspend_state == SUSPEND_PENDING &&
  223. (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))
  224. as->suspend_state = SUSPEND_READ;
  225. mutex_unlock(&state_lock);
  226. buf += sizeof(event);
  227. i -= sizeof(event);
  228. }
  229. if (i < count)
  230. ret = count - i;
  231. return ret;
  232. }
  233. static unsigned int apm_poll(struct file *fp, poll_table * wait)
  234. {
  235. struct apm_user *as = fp->private_data;
  236. poll_wait(fp, &apm_waitqueue, wait);
  237. return queue_empty(&as->queue) ? 0 : POLLIN | POLLRDNORM;
  238. }
  239. /*
  240. * apm_ioctl - handle APM ioctl
  241. *
  242. * APM_IOC_SUSPEND
  243. * This IOCTL is overloaded, and performs two functions. It is used to:
  244. * - initiate a suspend
  245. * - acknowledge a suspend read from /dev/apm_bios.
  246. * Only when everyone who has opened /dev/apm_bios with write permission
  247. * has acknowledge does the actual suspend happen.
  248. */
  249. static int
  250. apm_ioctl(struct inode * inode, struct file *filp, u_int cmd, u_long arg)
  251. {
  252. struct apm_user *as = filp->private_data;
  253. int err = -EINVAL;
  254. if (!as->suser || !as->writer)
  255. return -EPERM;
  256. switch (cmd) {
  257. case APM_IOC_SUSPEND:
  258. mutex_lock(&state_lock);
  259. as->suspend_result = -EINTR;
  260. if (as->suspend_state == SUSPEND_READ) {
  261. int pending;
  262. /*
  263. * If we read a suspend command from /dev/apm_bios,
  264. * then the corresponding APM_IOC_SUSPEND ioctl is
  265. * interpreted as an acknowledge.
  266. */
  267. as->suspend_state = SUSPEND_ACKED;
  268. suspends_pending--;
  269. pending = suspends_pending == 0;
  270. mutex_unlock(&state_lock);
  271. /*
  272. * If there are no further acknowledges required,
  273. * suspend the system.
  274. */
  275. if (pending)
  276. apm_suspend();
  277. /*
  278. * Wait for the suspend/resume to complete. If there
  279. * are pending acknowledges, we wait here for them.
  280. */
  281. freezer_do_not_count();
  282. wait_event(apm_suspend_waitqueue,
  283. as->suspend_state == SUSPEND_DONE);
  284. /*
  285. * Since we are waiting until the suspend is done, the
  286. * try_to_freeze() in freezer_count() will not trigger
  287. */
  288. freezer_count();
  289. } else {
  290. as->suspend_state = SUSPEND_WAIT;
  291. mutex_unlock(&state_lock);
  292. /*
  293. * Otherwise it is a request to suspend the system.
  294. * Queue an event for all readers, and expect an
  295. * acknowledge from all writers who haven't already
  296. * acknowledged.
  297. */
  298. err = queue_suspend_event(APM_USER_SUSPEND, as);
  299. if (err < 0) {
  300. /*
  301. * Avoid taking the lock here - this
  302. * should be fine.
  303. */
  304. as->suspend_state = SUSPEND_NONE;
  305. break;
  306. }
  307. if (err > 0)
  308. apm_suspend();
  309. /*
  310. * Wait for the suspend/resume to complete. If there
  311. * are pending acknowledges, we wait here for them.
  312. */
  313. wait_event_freezable(apm_suspend_waitqueue,
  314. as->suspend_state == SUSPEND_DONE);
  315. }
  316. mutex_lock(&state_lock);
  317. err = as->suspend_result;
  318. as->suspend_state = SUSPEND_NONE;
  319. mutex_unlock(&state_lock);
  320. break;
  321. }
  322. return err;
  323. }
  324. static int apm_release(struct inode * inode, struct file * filp)
  325. {
  326. struct apm_user *as = filp->private_data;
  327. int pending = 0;
  328. filp->private_data = NULL;
  329. down_write(&user_list_lock);
  330. list_del(&as->list);
  331. up_write(&user_list_lock);
  332. /*
  333. * We are now unhooked from the chain. As far as new
  334. * events are concerned, we no longer exist. However, we
  335. * need to balance suspends_pending, which means the
  336. * possibility of sleeping.
  337. */
  338. mutex_lock(&state_lock);
  339. if (as->suspend_state != SUSPEND_NONE) {
  340. suspends_pending -= 1;
  341. pending = suspends_pending == 0;
  342. }
  343. mutex_unlock(&state_lock);
  344. if (pending)
  345. apm_suspend();
  346. kfree(as);
  347. return 0;
  348. }
  349. static int apm_open(struct inode * inode, struct file * filp)
  350. {
  351. struct apm_user *as;
  352. lock_kernel();
  353. as = kzalloc(sizeof(*as), GFP_KERNEL);
  354. if (as) {
  355. /*
  356. * XXX - this is a tiny bit broken, when we consider BSD
  357. * process accounting. If the device is opened by root, we
  358. * instantly flag that we used superuser privs. Who knows,
  359. * we might close the device immediately without doing a
  360. * privileged operation -- cevans
  361. */
  362. as->suser = capable(CAP_SYS_ADMIN);
  363. as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
  364. as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
  365. down_write(&user_list_lock);
  366. list_add(&as->list, &apm_user_list);
  367. up_write(&user_list_lock);
  368. filp->private_data = as;
  369. }
  370. unlock_kernel();
  371. return as ? 0 : -ENOMEM;
  372. }
  373. static struct file_operations apm_bios_fops = {
  374. .owner = THIS_MODULE,
  375. .read = apm_read,
  376. .poll = apm_poll,
  377. .ioctl = apm_ioctl,
  378. .open = apm_open,
  379. .release = apm_release,
  380. };
  381. static struct miscdevice apm_device = {
  382. .minor = APM_MINOR_DEV,
  383. .name = "apm_bios",
  384. .fops = &apm_bios_fops
  385. };
  386. #ifdef CONFIG_PROC_FS
  387. /*
  388. * Arguments, with symbols from linux/apm_bios.h.
  389. *
  390. * 0) Linux driver version (this will change if format changes)
  391. * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
  392. * 2) APM flags from APM Installation Check (0x00):
  393. * bit 0: APM_16_BIT_SUPPORT
  394. * bit 1: APM_32_BIT_SUPPORT
  395. * bit 2: APM_IDLE_SLOWS_CLOCK
  396. * bit 3: APM_BIOS_DISABLED
  397. * bit 4: APM_BIOS_DISENGAGED
  398. * 3) AC line status
  399. * 0x00: Off-line
  400. * 0x01: On-line
  401. * 0x02: On backup power (BIOS >= 1.1 only)
  402. * 0xff: Unknown
  403. * 4) Battery status
  404. * 0x00: High
  405. * 0x01: Low
  406. * 0x02: Critical
  407. * 0x03: Charging
  408. * 0x04: Selected battery not present (BIOS >= 1.2 only)
  409. * 0xff: Unknown
  410. * 5) Battery flag
  411. * bit 0: High
  412. * bit 1: Low
  413. * bit 2: Critical
  414. * bit 3: Charging
  415. * bit 7: No system battery
  416. * 0xff: Unknown
  417. * 6) Remaining battery life (percentage of charge):
  418. * 0-100: valid
  419. * -1: Unknown
  420. * 7) Remaining battery life (time units):
  421. * Number of remaining minutes or seconds
  422. * -1: Unknown
  423. * 8) min = minutes; sec = seconds
  424. */
  425. static int proc_apm_show(struct seq_file *m, void *v)
  426. {
  427. struct apm_power_info info;
  428. char *units;
  429. info.ac_line_status = 0xff;
  430. info.battery_status = 0xff;
  431. info.battery_flag = 0xff;
  432. info.battery_life = -1;
  433. info.time = -1;
  434. info.units = -1;
  435. if (apm_get_power_status)
  436. apm_get_power_status(&info);
  437. switch (info.units) {
  438. default: units = "?"; break;
  439. case 0: units = "min"; break;
  440. case 1: units = "sec"; break;
  441. }
  442. seq_printf(m, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
  443. driver_version, APM_32_BIT_SUPPORT,
  444. info.ac_line_status, info.battery_status,
  445. info.battery_flag, info.battery_life,
  446. info.time, units);
  447. return 0;
  448. }
  449. static int proc_apm_open(struct inode *inode, struct file *file)
  450. {
  451. return single_open(file, proc_apm_show, NULL);
  452. }
  453. static const struct file_operations apm_proc_fops = {
  454. .owner = THIS_MODULE,
  455. .open = proc_apm_open,
  456. .read = seq_read,
  457. .llseek = seq_lseek,
  458. .release = single_release,
  459. };
  460. #endif
  461. static int kapmd(void *arg)
  462. {
  463. do {
  464. apm_event_t event;
  465. int ret;
  466. wait_event_interruptible(kapmd_wait,
  467. !queue_empty(&kapmd_queue) || kthread_should_stop());
  468. if (kthread_should_stop())
  469. break;
  470. spin_lock_irq(&kapmd_queue_lock);
  471. event = 0;
  472. if (!queue_empty(&kapmd_queue))
  473. event = queue_get_event(&kapmd_queue);
  474. spin_unlock_irq(&kapmd_queue_lock);
  475. switch (event) {
  476. case 0:
  477. break;
  478. case APM_LOW_BATTERY:
  479. case APM_POWER_STATUS_CHANGE:
  480. queue_event(event);
  481. break;
  482. case APM_USER_SUSPEND:
  483. case APM_SYS_SUSPEND:
  484. ret = queue_suspend_event(event, NULL);
  485. if (ret < 0) {
  486. /*
  487. * We were busy. Try again in 50ms.
  488. */
  489. queue_add_event(&kapmd_queue, event);
  490. msleep(50);
  491. }
  492. if (ret > 0)
  493. apm_suspend();
  494. break;
  495. case APM_CRITICAL_SUSPEND:
  496. apm_suspend();
  497. break;
  498. }
  499. } while (1);
  500. return 0;
  501. }
  502. static int __init apm_init(void)
  503. {
  504. int ret;
  505. if (apm_disabled) {
  506. printk(KERN_NOTICE "apm: disabled on user request.\n");
  507. return -ENODEV;
  508. }
  509. kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");
  510. if (IS_ERR(kapmd_tsk)) {
  511. ret = PTR_ERR(kapmd_tsk);
  512. kapmd_tsk = NULL;
  513. return ret;
  514. }
  515. wake_up_process(kapmd_tsk);
  516. #ifdef CONFIG_PROC_FS
  517. proc_create("apm", 0, NULL, &apm_proc_fops);
  518. #endif
  519. ret = misc_register(&apm_device);
  520. if (ret != 0) {
  521. remove_proc_entry("apm", NULL);
  522. kthread_stop(kapmd_tsk);
  523. }
  524. return ret;
  525. }
  526. static void __exit apm_exit(void)
  527. {
  528. misc_deregister(&apm_device);
  529. remove_proc_entry("apm", NULL);
  530. kthread_stop(kapmd_tsk);
  531. }
  532. module_init(apm_init);
  533. module_exit(apm_exit);
  534. MODULE_AUTHOR("Stephen Rothwell");
  535. MODULE_DESCRIPTION("Advanced Power Management");
  536. MODULE_LICENSE("GPL");
  537. #ifndef MODULE
  538. static int __init apm_setup(char *str)
  539. {
  540. while ((str != NULL) && (*str != '\0')) {
  541. if (strncmp(str, "off", 3) == 0)
  542. apm_disabled = 1;
  543. if (strncmp(str, "on", 2) == 0)
  544. apm_disabled = 0;
  545. str = strchr(str, ',');
  546. if (str != NULL)
  547. str += strspn(str, ", \t");
  548. }
  549. return 1;
  550. }
  551. __setup("apm=", apm_setup);
  552. #endif
  553. /**
  554. * apm_queue_event - queue an APM event for kapmd
  555. * @event: APM event
  556. *
  557. * Queue an APM event for kapmd to process and ultimately take the
  558. * appropriate action. Only a subset of events are handled:
  559. * %APM_LOW_BATTERY
  560. * %APM_POWER_STATUS_CHANGE
  561. * %APM_USER_SUSPEND
  562. * %APM_SYS_SUSPEND
  563. * %APM_CRITICAL_SUSPEND
  564. */
  565. void apm_queue_event(apm_event_t event)
  566. {
  567. unsigned long flags;
  568. spin_lock_irqsave(&kapmd_queue_lock, flags);
  569. queue_add_event(&kapmd_queue, event);
  570. spin_unlock_irqrestore(&kapmd_queue_lock, flags);
  571. wake_up_interruptible(&kapmd_wait);
  572. }
  573. EXPORT_SYMBOL(apm_queue_event);