apm-emulation.c 18 KB

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