apm-emulation.c 15 KB

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