apm-emulation.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * bios-less APM driver for ARM Linux
  4. * Jamey Hicks <jamey@crl.dec.com>
  5. * adapted from the APM BIOS driver for Linux by Stephen Rothwell (sfr@linuxcare.com)
  6. *
  7. * APM 1.2 Reference:
  8. * Intel Corporation, Microsoft Corporation. Advanced Power Management
  9. * (APM) BIOS Interface Specification, Revision 1.2, February 1996.
  10. *
  11. * This document is available from Microsoft at:
  12. * http://www.microsoft.com/whdc/archive/amp_12.mspx
  13. */
  14. #include <linux/module.h>
  15. #include <linux/poll.h>
  16. #include <linux/slab.h>
  17. #include <linux/mutex.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/miscdevice.h>
  21. #include <linux/apm_bios.h>
  22. #include <linux/capability.h>
  23. #include <linux/sched.h>
  24. #include <linux/suspend.h>
  25. #include <linux/apm-emulation.h>
  26. #include <linux/freezer.h>
  27. #include <linux/device.h>
  28. #include <linux/kernel.h>
  29. #include <linux/list.h>
  30. #include <linux/init.h>
  31. #include <linux/completion.h>
  32. #include <linux/kthread.h>
  33. #include <linux/delay.h>
  34. /*
  35. * One option can be changed at boot time as follows:
  36. * apm=on/off enable/disable APM
  37. */
  38. /*
  39. * Maximum number of events stored
  40. */
  41. #define APM_MAX_EVENTS 16
  42. struct apm_queue {
  43. unsigned int event_head;
  44. unsigned int event_tail;
  45. apm_event_t events[APM_MAX_EVENTS];
  46. };
  47. /*
  48. * thread states (for threads using a writable /dev/apm_bios fd):
  49. *
  50. * SUSPEND_NONE: nothing happening
  51. * SUSPEND_PENDING: suspend event queued for thread and pending to be read
  52. * SUSPEND_READ: suspend event read, pending acknowledgement
  53. * SUSPEND_ACKED: acknowledgement received from thread (via ioctl),
  54. * waiting for resume
  55. * SUSPEND_ACKTO: acknowledgement timeout
  56. * SUSPEND_DONE: thread had acked suspend and is now notified of
  57. * resume
  58. *
  59. * SUSPEND_WAIT: this thread invoked suspend and is waiting for resume
  60. *
  61. * A thread migrates in one of three paths:
  62. * NONE -1-> PENDING -2-> READ -3-> ACKED -4-> DONE -5-> NONE
  63. * -6-> ACKTO -7-> NONE
  64. * NONE -8-> WAIT -9-> NONE
  65. *
  66. * While in PENDING or READ, the thread is accounted for in the
  67. * suspend_acks_pending counter.
  68. *
  69. * The transitions are invoked as follows:
  70. * 1: suspend event is signalled from the core PM code
  71. * 2: the suspend event is read from the fd by the userspace thread
  72. * 3: userspace thread issues the APM_IOC_SUSPEND ioctl (as ack)
  73. * 4: core PM code signals that we have resumed
  74. * 5: APM_IOC_SUSPEND ioctl returns
  75. *
  76. * 6: the notifier invoked from the core PM code timed out waiting
  77. * for all relevant threds to enter ACKED state and puts those
  78. * that haven't into ACKTO
  79. * 7: those threads issue APM_IOC_SUSPEND ioctl too late,
  80. * get an error
  81. *
  82. * 8: userspace thread issues the APM_IOC_SUSPEND ioctl (to suspend),
  83. * ioctl code invokes pm_suspend()
  84. * 9: pm_suspend() returns indicating resume
  85. */
  86. enum apm_suspend_state {
  87. SUSPEND_NONE,
  88. SUSPEND_PENDING,
  89. SUSPEND_READ,
  90. SUSPEND_ACKED,
  91. SUSPEND_ACKTO,
  92. SUSPEND_WAIT,
  93. SUSPEND_DONE,
  94. };
  95. /*
  96. * The per-file APM data
  97. */
  98. struct apm_user {
  99. struct list_head list;
  100. unsigned int suser: 1;
  101. unsigned int writer: 1;
  102. unsigned int reader: 1;
  103. int suspend_result;
  104. enum apm_suspend_state suspend_state;
  105. struct apm_queue queue;
  106. };
  107. /*
  108. * Local variables
  109. */
  110. static atomic_t suspend_acks_pending = ATOMIC_INIT(0);
  111. static atomic_t userspace_notification_inhibit = ATOMIC_INIT(0);
  112. static int apm_disabled;
  113. static struct task_struct *kapmd_tsk;
  114. static DECLARE_WAIT_QUEUE_HEAD(apm_waitqueue);
  115. static DECLARE_WAIT_QUEUE_HEAD(apm_suspend_waitqueue);
  116. /*
  117. * This is a list of everyone who has opened /dev/apm_bios
  118. */
  119. static DECLARE_RWSEM(user_list_lock);
  120. static LIST_HEAD(apm_user_list);
  121. /*
  122. * kapmd info. kapmd provides us a process context to handle
  123. * "APM" events within - specifically necessary if we're going
  124. * to be suspending the system.
  125. */
  126. static DECLARE_WAIT_QUEUE_HEAD(kapmd_wait);
  127. static DEFINE_SPINLOCK(kapmd_queue_lock);
  128. static struct apm_queue kapmd_queue;
  129. static DEFINE_MUTEX(state_lock);
  130. static const char driver_version[] = "1.13"; /* no spaces */
  131. /*
  132. * Compatibility cruft until the IPAQ people move over to the new
  133. * interface.
  134. */
  135. static void __apm_get_power_status(struct apm_power_info *info)
  136. {
  137. }
  138. /*
  139. * This allows machines to provide their own "apm get power status" function.
  140. */
  141. void (*apm_get_power_status)(struct apm_power_info *) = __apm_get_power_status;
  142. EXPORT_SYMBOL(apm_get_power_status);
  143. /*
  144. * APM event queue management.
  145. */
  146. static inline int queue_empty(struct apm_queue *q)
  147. {
  148. return q->event_head == q->event_tail;
  149. }
  150. static inline apm_event_t queue_get_event(struct apm_queue *q)
  151. {
  152. q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
  153. return q->events[q->event_tail];
  154. }
  155. static void queue_add_event(struct apm_queue *q, apm_event_t event)
  156. {
  157. q->event_head = (q->event_head + 1) % APM_MAX_EVENTS;
  158. if (q->event_head == q->event_tail) {
  159. static int notified;
  160. if (notified++ == 0)
  161. printk(KERN_ERR "apm: an event queue overflowed\n");
  162. q->event_tail = (q->event_tail + 1) % APM_MAX_EVENTS;
  163. }
  164. q->events[q->event_head] = event;
  165. }
  166. static void queue_event(apm_event_t event)
  167. {
  168. struct apm_user *as;
  169. down_read(&user_list_lock);
  170. list_for_each_entry(as, &apm_user_list, list) {
  171. if (as->reader)
  172. queue_add_event(&as->queue, event);
  173. }
  174. up_read(&user_list_lock);
  175. wake_up_interruptible(&apm_waitqueue);
  176. }
  177. static ssize_t apm_read(struct file *fp, char __user *buf, size_t count, loff_t *ppos)
  178. {
  179. struct apm_user *as = fp->private_data;
  180. apm_event_t event;
  181. int i = count, ret = 0;
  182. if (count < sizeof(apm_event_t))
  183. return -EINVAL;
  184. if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK)
  185. return -EAGAIN;
  186. wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue));
  187. while ((i >= sizeof(event)) && !queue_empty(&as->queue)) {
  188. event = queue_get_event(&as->queue);
  189. ret = -EFAULT;
  190. if (copy_to_user(buf, &event, sizeof(event)))
  191. break;
  192. mutex_lock(&state_lock);
  193. if (as->suspend_state == SUSPEND_PENDING &&
  194. (event == APM_SYS_SUSPEND || event == APM_USER_SUSPEND))
  195. as->suspend_state = SUSPEND_READ;
  196. mutex_unlock(&state_lock);
  197. buf += sizeof(event);
  198. i -= sizeof(event);
  199. }
  200. if (i < count)
  201. ret = count - i;
  202. return ret;
  203. }
  204. static __poll_t apm_poll(struct file *fp, poll_table * wait)
  205. {
  206. struct apm_user *as = fp->private_data;
  207. poll_wait(fp, &apm_waitqueue, wait);
  208. return queue_empty(&as->queue) ? 0 : EPOLLIN | EPOLLRDNORM;
  209. }
  210. /*
  211. * apm_ioctl - handle APM ioctl
  212. *
  213. * APM_IOC_SUSPEND
  214. * This IOCTL is overloaded, and performs two functions. It is used to:
  215. * - initiate a suspend
  216. * - acknowledge a suspend read from /dev/apm_bios.
  217. * Only when everyone who has opened /dev/apm_bios with write permission
  218. * has acknowledge does the actual suspend happen.
  219. */
  220. static long
  221. apm_ioctl(struct file *filp, u_int cmd, u_long arg)
  222. {
  223. struct apm_user *as = filp->private_data;
  224. int err = -EINVAL;
  225. if (!as->suser || !as->writer)
  226. return -EPERM;
  227. switch (cmd) {
  228. case APM_IOC_SUSPEND:
  229. mutex_lock(&state_lock);
  230. as->suspend_result = -EINTR;
  231. switch (as->suspend_state) {
  232. case SUSPEND_READ:
  233. /*
  234. * If we read a suspend command from /dev/apm_bios,
  235. * then the corresponding APM_IOC_SUSPEND ioctl is
  236. * interpreted as an acknowledge.
  237. */
  238. as->suspend_state = SUSPEND_ACKED;
  239. atomic_dec(&suspend_acks_pending);
  240. mutex_unlock(&state_lock);
  241. /*
  242. * suspend_acks_pending changed, the notifier needs to
  243. * be woken up for this
  244. */
  245. wake_up(&apm_suspend_waitqueue);
  246. /*
  247. * Wait for the suspend/resume to complete. If there
  248. * are pending acknowledges, we wait here for them.
  249. * wait_event_freezable() is interruptible and pending
  250. * signal can cause busy looping. We aren't doing
  251. * anything critical, chill a bit on each iteration.
  252. */
  253. while (wait_event_freezable(apm_suspend_waitqueue,
  254. as->suspend_state != SUSPEND_ACKED))
  255. msleep(10);
  256. break;
  257. case SUSPEND_ACKTO:
  258. as->suspend_result = -ETIMEDOUT;
  259. mutex_unlock(&state_lock);
  260. break;
  261. default:
  262. as->suspend_state = SUSPEND_WAIT;
  263. mutex_unlock(&state_lock);
  264. /*
  265. * Otherwise it is a request to suspend the system.
  266. * Just invoke pm_suspend(), we'll handle it from
  267. * there via the notifier.
  268. */
  269. as->suspend_result = pm_suspend(PM_SUSPEND_MEM);
  270. }
  271. mutex_lock(&state_lock);
  272. err = as->suspend_result;
  273. as->suspend_state = SUSPEND_NONE;
  274. mutex_unlock(&state_lock);
  275. break;
  276. }
  277. return err;
  278. }
  279. static int apm_release(struct inode * inode, struct file * filp)
  280. {
  281. struct apm_user *as = filp->private_data;
  282. filp->private_data = NULL;
  283. down_write(&user_list_lock);
  284. list_del(&as->list);
  285. up_write(&user_list_lock);
  286. /*
  287. * We are now unhooked from the chain. As far as new
  288. * events are concerned, we no longer exist.
  289. */
  290. mutex_lock(&state_lock);
  291. if (as->suspend_state == SUSPEND_PENDING ||
  292. as->suspend_state == SUSPEND_READ)
  293. atomic_dec(&suspend_acks_pending);
  294. mutex_unlock(&state_lock);
  295. wake_up(&apm_suspend_waitqueue);
  296. kfree(as);
  297. return 0;
  298. }
  299. static int apm_open(struct inode * inode, struct file * filp)
  300. {
  301. struct apm_user *as;
  302. as = kzalloc(sizeof(*as), GFP_KERNEL);
  303. if (as) {
  304. /*
  305. * XXX - this is a tiny bit broken, when we consider BSD
  306. * process accounting. If the device is opened by root, we
  307. * instantly flag that we used superuser privs. Who knows,
  308. * we might close the device immediately without doing a
  309. * privileged operation -- cevans
  310. */
  311. as->suser = capable(CAP_SYS_ADMIN);
  312. as->writer = (filp->f_mode & FMODE_WRITE) == FMODE_WRITE;
  313. as->reader = (filp->f_mode & FMODE_READ) == FMODE_READ;
  314. down_write(&user_list_lock);
  315. list_add(&as->list, &apm_user_list);
  316. up_write(&user_list_lock);
  317. filp->private_data = as;
  318. }
  319. return as ? 0 : -ENOMEM;
  320. }
  321. static const struct file_operations apm_bios_fops = {
  322. .owner = THIS_MODULE,
  323. .read = apm_read,
  324. .poll = apm_poll,
  325. .unlocked_ioctl = apm_ioctl,
  326. .open = apm_open,
  327. .release = apm_release,
  328. .llseek = noop_llseek,
  329. };
  330. static struct miscdevice apm_device = {
  331. .minor = APM_MINOR_DEV,
  332. .name = "apm_bios",
  333. .fops = &apm_bios_fops
  334. };
  335. #ifdef CONFIG_PROC_FS
  336. /*
  337. * Arguments, with symbols from linux/apm_bios.h.
  338. *
  339. * 0) Linux driver version (this will change if format changes)
  340. * 1) APM BIOS Version. Usually 1.0, 1.1 or 1.2.
  341. * 2) APM flags from APM Installation Check (0x00):
  342. * bit 0: APM_16_BIT_SUPPORT
  343. * bit 1: APM_32_BIT_SUPPORT
  344. * bit 2: APM_IDLE_SLOWS_CLOCK
  345. * bit 3: APM_BIOS_DISABLED
  346. * bit 4: APM_BIOS_DISENGAGED
  347. * 3) AC line status
  348. * 0x00: Off-line
  349. * 0x01: On-line
  350. * 0x02: On backup power (BIOS >= 1.1 only)
  351. * 0xff: Unknown
  352. * 4) Battery status
  353. * 0x00: High
  354. * 0x01: Low
  355. * 0x02: Critical
  356. * 0x03: Charging
  357. * 0x04: Selected battery not present (BIOS >= 1.2 only)
  358. * 0xff: Unknown
  359. * 5) Battery flag
  360. * bit 0: High
  361. * bit 1: Low
  362. * bit 2: Critical
  363. * bit 3: Charging
  364. * bit 7: No system battery
  365. * 0xff: Unknown
  366. * 6) Remaining battery life (percentage of charge):
  367. * 0-100: valid
  368. * -1: Unknown
  369. * 7) Remaining battery life (time units):
  370. * Number of remaining minutes or seconds
  371. * -1: Unknown
  372. * 8) min = minutes; sec = seconds
  373. */
  374. static int proc_apm_show(struct seq_file *m, void *v)
  375. {
  376. struct apm_power_info info;
  377. char *units;
  378. info.ac_line_status = 0xff;
  379. info.battery_status = 0xff;
  380. info.battery_flag = 0xff;
  381. info.battery_life = -1;
  382. info.time = -1;
  383. info.units = -1;
  384. if (apm_get_power_status)
  385. apm_get_power_status(&info);
  386. switch (info.units) {
  387. default: units = "?"; break;
  388. case 0: units = "min"; break;
  389. case 1: units = "sec"; break;
  390. }
  391. seq_printf(m, "%s 1.2 0x%02x 0x%02x 0x%02x 0x%02x %d%% %d %s\n",
  392. driver_version, APM_32_BIT_SUPPORT,
  393. info.ac_line_status, info.battery_status,
  394. info.battery_flag, info.battery_life,
  395. info.time, units);
  396. return 0;
  397. }
  398. #endif
  399. static int kapmd(void *arg)
  400. {
  401. do {
  402. apm_event_t event;
  403. wait_event_interruptible(kapmd_wait,
  404. !queue_empty(&kapmd_queue) || kthread_should_stop());
  405. if (kthread_should_stop())
  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);
  418. break;
  419. case APM_USER_SUSPEND:
  420. case APM_SYS_SUSPEND:
  421. pm_suspend(PM_SUSPEND_MEM);
  422. break;
  423. case APM_CRITICAL_SUSPEND:
  424. atomic_inc(&userspace_notification_inhibit);
  425. pm_suspend(PM_SUSPEND_MEM);
  426. atomic_dec(&userspace_notification_inhibit);
  427. break;
  428. }
  429. } while (1);
  430. return 0;
  431. }
  432. static int apm_suspend_notifier(struct notifier_block *nb,
  433. unsigned long event,
  434. void *dummy)
  435. {
  436. struct apm_user *as;
  437. int err;
  438. unsigned long apm_event;
  439. /* short-cut emergency suspends */
  440. if (atomic_read(&userspace_notification_inhibit))
  441. return NOTIFY_DONE;
  442. switch (event) {
  443. case PM_SUSPEND_PREPARE:
  444. case PM_HIBERNATION_PREPARE:
  445. apm_event = (event == PM_SUSPEND_PREPARE) ?
  446. APM_USER_SUSPEND : APM_USER_HIBERNATION;
  447. /*
  448. * Queue an event to all "writer" users that we want
  449. * to suspend and need their ack.
  450. */
  451. mutex_lock(&state_lock);
  452. down_read(&user_list_lock);
  453. list_for_each_entry(as, &apm_user_list, list) {
  454. if (as->suspend_state != SUSPEND_WAIT && as->reader &&
  455. as->writer && as->suser) {
  456. as->suspend_state = SUSPEND_PENDING;
  457. atomic_inc(&suspend_acks_pending);
  458. queue_add_event(&as->queue, apm_event);
  459. }
  460. }
  461. up_read(&user_list_lock);
  462. mutex_unlock(&state_lock);
  463. wake_up_interruptible(&apm_waitqueue);
  464. /*
  465. * Wait for the the suspend_acks_pending variable to drop to
  466. * zero, meaning everybody acked the suspend event (or the
  467. * process was killed.)
  468. *
  469. * If the app won't answer within a short while we assume it
  470. * locked up and ignore it.
  471. */
  472. err = wait_event_interruptible_timeout(
  473. apm_suspend_waitqueue,
  474. atomic_read(&suspend_acks_pending) == 0,
  475. 5*HZ);
  476. /* timed out */
  477. if (err == 0) {
  478. /*
  479. * Move anybody who timed out to "ack timeout" state.
  480. *
  481. * We could time out and the userspace does the ACK
  482. * right after we time out but before we enter the
  483. * locked section here, but that's fine.
  484. */
  485. mutex_lock(&state_lock);
  486. down_read(&user_list_lock);
  487. list_for_each_entry(as, &apm_user_list, list) {
  488. if (as->suspend_state == SUSPEND_PENDING ||
  489. as->suspend_state == SUSPEND_READ) {
  490. as->suspend_state = SUSPEND_ACKTO;
  491. atomic_dec(&suspend_acks_pending);
  492. }
  493. }
  494. up_read(&user_list_lock);
  495. mutex_unlock(&state_lock);
  496. }
  497. /* let suspend proceed */
  498. if (err >= 0)
  499. return NOTIFY_OK;
  500. /* interrupted by signal */
  501. return notifier_from_errno(err);
  502. case PM_POST_SUSPEND:
  503. case PM_POST_HIBERNATION:
  504. apm_event = (event == PM_POST_SUSPEND) ?
  505. APM_NORMAL_RESUME : APM_HIBERNATION_RESUME;
  506. /*
  507. * Anyone on the APM queues will think we're still suspended.
  508. * Send a message so everyone knows we're now awake again.
  509. */
  510. queue_event(apm_event);
  511. /*
  512. * Finally, wake up anyone who is sleeping on the suspend.
  513. */
  514. mutex_lock(&state_lock);
  515. down_read(&user_list_lock);
  516. list_for_each_entry(as, &apm_user_list, list) {
  517. if (as->suspend_state == SUSPEND_ACKED) {
  518. /*
  519. * TODO: maybe grab error code, needs core
  520. * changes to push the error to the notifier
  521. * chain (could use the second parameter if
  522. * implemented)
  523. */
  524. as->suspend_result = 0;
  525. as->suspend_state = SUSPEND_DONE;
  526. }
  527. }
  528. up_read(&user_list_lock);
  529. mutex_unlock(&state_lock);
  530. wake_up(&apm_suspend_waitqueue);
  531. return NOTIFY_OK;
  532. default:
  533. return NOTIFY_DONE;
  534. }
  535. }
  536. static struct notifier_block apm_notif_block = {
  537. .notifier_call = apm_suspend_notifier,
  538. };
  539. static int __init apm_init(void)
  540. {
  541. int ret;
  542. if (apm_disabled) {
  543. printk(KERN_NOTICE "apm: disabled on user request.\n");
  544. return -ENODEV;
  545. }
  546. kapmd_tsk = kthread_create(kapmd, NULL, "kapmd");
  547. if (IS_ERR(kapmd_tsk)) {
  548. ret = PTR_ERR(kapmd_tsk);
  549. kapmd_tsk = NULL;
  550. goto out;
  551. }
  552. wake_up_process(kapmd_tsk);
  553. #ifdef CONFIG_PROC_FS
  554. proc_create_single("apm", 0, NULL, proc_apm_show);
  555. #endif
  556. ret = misc_register(&apm_device);
  557. if (ret)
  558. goto out_stop;
  559. ret = register_pm_notifier(&apm_notif_block);
  560. if (ret)
  561. goto out_unregister;
  562. return 0;
  563. out_unregister:
  564. misc_deregister(&apm_device);
  565. out_stop:
  566. remove_proc_entry("apm", NULL);
  567. kthread_stop(kapmd_tsk);
  568. out:
  569. return ret;
  570. }
  571. static void __exit apm_exit(void)
  572. {
  573. unregister_pm_notifier(&apm_notif_block);
  574. misc_deregister(&apm_device);
  575. remove_proc_entry("apm", NULL);
  576. kthread_stop(kapmd_tsk);
  577. }
  578. module_init(apm_init);
  579. module_exit(apm_exit);
  580. MODULE_AUTHOR("Stephen Rothwell");
  581. MODULE_DESCRIPTION("Advanced Power Management");
  582. MODULE_LICENSE("GPL");
  583. #ifndef MODULE
  584. static int __init apm_setup(char *str)
  585. {
  586. while ((str != NULL) && (*str != '\0')) {
  587. if (strncmp(str, "off", 3) == 0)
  588. apm_disabled = 1;
  589. if (strncmp(str, "on", 2) == 0)
  590. apm_disabled = 0;
  591. str = strchr(str, ',');
  592. if (str != NULL)
  593. str += strspn(str, ", \t");
  594. }
  595. return 1;
  596. }
  597. __setup("apm=", apm_setup);
  598. #endif
  599. /**
  600. * apm_queue_event - queue an APM event for kapmd
  601. * @event: APM event
  602. *
  603. * Queue an APM event for kapmd to process and ultimately take the
  604. * appropriate action. Only a subset of events are handled:
  605. * %APM_LOW_BATTERY
  606. * %APM_POWER_STATUS_CHANGE
  607. * %APM_USER_SUSPEND
  608. * %APM_SYS_SUSPEND
  609. * %APM_CRITICAL_SUSPEND
  610. */
  611. void apm_queue_event(apm_event_t event)
  612. {
  613. unsigned long flags;
  614. spin_lock_irqsave(&kapmd_queue_lock, flags);
  615. queue_add_event(&kapmd_queue, event);
  616. spin_unlock_irqrestore(&kapmd_queue_lock, flags);
  617. wake_up_interruptible(&kapmd_wait);
  618. }
  619. EXPORT_SYMBOL(apm_queue_event);