apm-emulation.c 18 KB

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