apm-emulation.c 15 KB

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