apm-emulation.c 15 KB

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