apm-emulation.c 18 KB

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