apm.c 14 KB

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