apm.c 14 KB

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