alarmtimer.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*
  2. * Alarmtimer interface
  3. *
  4. * This interface provides a timer which is similarto hrtimers,
  5. * but triggers a RTC alarm if the box is suspend.
  6. *
  7. * This interface is influenced by the Android RTC Alarm timer
  8. * interface.
  9. *
  10. * Copyright (C) 2010 IBM Corperation
  11. *
  12. * Author: John Stultz <john.stultz@linaro.org>
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/time.h>
  19. #include <linux/hrtimer.h>
  20. #include <linux/timerqueue.h>
  21. #include <linux/rtc.h>
  22. #include <linux/alarmtimer.h>
  23. #include <linux/mutex.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/posix-timers.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/freezer.h>
  28. static struct alarm_base {
  29. spinlock_t lock;
  30. struct timerqueue_head timerqueue;
  31. struct hrtimer timer;
  32. ktime_t (*gettime)(void);
  33. clockid_t base_clockid;
  34. struct work_struct irqwork;
  35. } alarm_bases[ALARM_NUMTYPE];
  36. static struct rtc_timer rtctimer;
  37. static struct rtc_device *rtcdev;
  38. static ktime_t freezer_delta;
  39. static DEFINE_SPINLOCK(freezer_delta_lock);
  40. /**************************************************************************
  41. * alarmtimer management code
  42. */
  43. /*
  44. * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
  45. * @base: pointer to the base where the timer is being run
  46. * @alarm: pointer to alarm being enqueued.
  47. *
  48. * Adds alarm to a alarm_base timerqueue and if necessary sets
  49. * an hrtimer to run.
  50. *
  51. * Must hold base->lock when calling.
  52. */
  53. static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
  54. {
  55. timerqueue_add(&base->timerqueue, &alarm->node);
  56. if (&alarm->node == timerqueue_getnext(&base->timerqueue)) {
  57. hrtimer_try_to_cancel(&base->timer);
  58. hrtimer_start(&base->timer, alarm->node.expires,
  59. HRTIMER_MODE_ABS);
  60. }
  61. }
  62. /*
  63. * alarmtimer_remove - Removes an alarm timer from an alarm_base timerqueue
  64. * @base: pointer to the base where the timer is running
  65. * @alarm: pointer to alarm being removed
  66. *
  67. * Removes alarm to a alarm_base timerqueue and if necessary sets
  68. * a new timer to run.
  69. *
  70. * Must hold base->lock when calling.
  71. */
  72. static void alarmtimer_remove(struct alarm_base *base, struct alarm *alarm)
  73. {
  74. struct timerqueue_node *next = timerqueue_getnext(&base->timerqueue);
  75. timerqueue_del(&base->timerqueue, &alarm->node);
  76. if (next == &alarm->node) {
  77. hrtimer_try_to_cancel(&base->timer);
  78. next = timerqueue_getnext(&base->timerqueue);
  79. if (!next)
  80. return;
  81. hrtimer_start(&base->timer, next->expires, HRTIMER_MODE_ABS);
  82. }
  83. }
  84. /*
  85. * alarmtimer_do_work - Handles alarm being fired.
  86. * @work: pointer to workqueue being run
  87. *
  88. * When a timer fires, this runs through the timerqueue to see
  89. * which alarm timers, and run those that expired. If there are
  90. * more alarm timers queued, we set the hrtimer to fire in the
  91. * future.
  92. */
  93. void alarmtimer_do_work(struct work_struct *work)
  94. {
  95. struct alarm_base *base = container_of(work, struct alarm_base,
  96. irqwork);
  97. struct timerqueue_node *next;
  98. unsigned long flags;
  99. ktime_t now;
  100. spin_lock_irqsave(&base->lock, flags);
  101. now = base->gettime();
  102. while ((next = timerqueue_getnext(&base->timerqueue))) {
  103. struct alarm *alarm;
  104. ktime_t expired = next->expires;
  105. if (expired.tv64 >= now.tv64)
  106. break;
  107. alarm = container_of(next, struct alarm, node);
  108. timerqueue_del(&base->timerqueue, &alarm->node);
  109. alarm->enabled = 0;
  110. /* Re-add periodic timers */
  111. if (alarm->period.tv64) {
  112. alarm->node.expires = ktime_add(expired, alarm->period);
  113. timerqueue_add(&base->timerqueue, &alarm->node);
  114. alarm->enabled = 1;
  115. }
  116. spin_unlock_irqrestore(&base->lock, flags);
  117. if (alarm->function)
  118. alarm->function(alarm);
  119. spin_lock_irqsave(&base->lock, flags);
  120. }
  121. if (next) {
  122. hrtimer_start(&base->timer, next->expires,
  123. HRTIMER_MODE_ABS);
  124. }
  125. spin_unlock_irqrestore(&base->lock, flags);
  126. }
  127. /*
  128. * alarmtimer_fired - Handles alarm hrtimer being fired.
  129. * @timer: pointer to hrtimer being run
  130. *
  131. * When a timer fires, this schedules the do_work function to
  132. * be run.
  133. */
  134. static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
  135. {
  136. struct alarm_base *base = container_of(timer, struct alarm_base, timer);
  137. schedule_work(&base->irqwork);
  138. return HRTIMER_NORESTART;
  139. }
  140. /*
  141. * alarmtimer_suspend - Suspend time callback
  142. * @dev: unused
  143. * @state: unused
  144. *
  145. * When we are going into suspend, we look through the bases
  146. * to see which is the soonest timer to expire. We then
  147. * set an rtc timer to fire that far into the future, which
  148. * will wake us from suspend.
  149. */
  150. static int alarmtimer_suspend(struct device *dev)
  151. {
  152. struct rtc_time tm;
  153. ktime_t min, now;
  154. unsigned long flags;
  155. int i;
  156. spin_lock_irqsave(&freezer_delta_lock, flags);
  157. min = freezer_delta;
  158. freezer_delta = ktime_set(0, 0);
  159. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  160. /* If we have no rtcdev, just return */
  161. if (!rtcdev)
  162. return 0;
  163. /* Find the soonest timer to expire*/
  164. for (i = 0; i < ALARM_NUMTYPE; i++) {
  165. struct alarm_base *base = &alarm_bases[i];
  166. struct timerqueue_node *next;
  167. ktime_t delta;
  168. spin_lock_irqsave(&base->lock, flags);
  169. next = timerqueue_getnext(&base->timerqueue);
  170. spin_unlock_irqrestore(&base->lock, flags);
  171. if (!next)
  172. continue;
  173. delta = ktime_sub(next->expires, base->gettime());
  174. if (!min.tv64 || (delta.tv64 < min.tv64))
  175. min = delta;
  176. }
  177. if (min.tv64 == 0)
  178. return 0;
  179. /* XXX - Should we enforce a minimum sleep time? */
  180. WARN_ON(min.tv64 < NSEC_PER_SEC);
  181. /* Setup an rtc timer to fire that far in the future */
  182. rtc_timer_cancel(rtcdev, &rtctimer);
  183. rtc_read_time(rtcdev, &tm);
  184. now = rtc_tm_to_ktime(tm);
  185. now = ktime_add(now, min);
  186. rtc_timer_start(rtcdev, &rtctimer, now, ktime_set(0, 0));
  187. return 0;
  188. }
  189. static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
  190. {
  191. ktime_t delta;
  192. unsigned long flags;
  193. struct alarm_base *base = &alarm_bases[type];
  194. delta = ktime_sub(absexp, base->gettime());
  195. spin_lock_irqsave(&freezer_delta_lock, flags);
  196. if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
  197. freezer_delta = delta;
  198. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  199. }
  200. /**************************************************************************
  201. * alarm kernel interface code
  202. */
  203. /*
  204. * alarm_init - Initialize an alarm structure
  205. * @alarm: ptr to alarm to be initialized
  206. * @type: the type of the alarm
  207. * @function: callback that is run when the alarm fires
  208. *
  209. * In-kernel interface to initializes the alarm structure.
  210. */
  211. void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
  212. void (*function)(struct alarm *))
  213. {
  214. timerqueue_init(&alarm->node);
  215. alarm->period = ktime_set(0, 0);
  216. alarm->function = function;
  217. alarm->type = type;
  218. alarm->enabled = 0;
  219. }
  220. /*
  221. * alarm_start - Sets an alarm to fire
  222. * @alarm: ptr to alarm to set
  223. * @start: time to run the alarm
  224. * @period: period at which the alarm will recur
  225. *
  226. * In-kernel interface set an alarm timer.
  227. */
  228. void alarm_start(struct alarm *alarm, ktime_t start, ktime_t period)
  229. {
  230. struct alarm_base *base = &alarm_bases[alarm->type];
  231. unsigned long flags;
  232. spin_lock_irqsave(&base->lock, flags);
  233. if (alarm->enabled)
  234. alarmtimer_remove(base, alarm);
  235. alarm->node.expires = start;
  236. alarm->period = period;
  237. alarmtimer_enqueue(base, alarm);
  238. alarm->enabled = 1;
  239. spin_unlock_irqrestore(&base->lock, flags);
  240. }
  241. /*
  242. * alarm_cancel - Tries to cancel an alarm timer
  243. * @alarm: ptr to alarm to be canceled
  244. *
  245. * In-kernel interface to cancel an alarm timer.
  246. */
  247. void alarm_cancel(struct alarm *alarm)
  248. {
  249. struct alarm_base *base = &alarm_bases[alarm->type];
  250. unsigned long flags;
  251. spin_lock_irqsave(&base->lock, flags);
  252. if (alarm->enabled)
  253. alarmtimer_remove(base, alarm);
  254. alarm->enabled = 0;
  255. spin_unlock_irqrestore(&base->lock, flags);
  256. }
  257. /**************************************************************************
  258. * alarm posix interface code
  259. */
  260. /*
  261. * clock2alarm - helper that converts from clockid to alarmtypes
  262. * @clockid: clockid.
  263. *
  264. * Helper function that converts from clockids to alarmtypes
  265. */
  266. static enum alarmtimer_type clock2alarm(clockid_t clockid)
  267. {
  268. if (clockid == CLOCK_REALTIME_ALARM)
  269. return ALARM_REALTIME;
  270. if (clockid == CLOCK_BOOTTIME_ALARM)
  271. return ALARM_BOOTTIME;
  272. return -1;
  273. }
  274. /*
  275. * alarm_handle_timer - Callback for posix timers
  276. * @alarm: alarm that fired
  277. *
  278. * Posix timer callback for expired alarm timers.
  279. */
  280. static void alarm_handle_timer(struct alarm *alarm)
  281. {
  282. struct k_itimer *ptr = container_of(alarm, struct k_itimer,
  283. it.alarmtimer);
  284. if (posix_timer_event(ptr, 0) != 0)
  285. ptr->it_overrun++;
  286. }
  287. /*
  288. * alarm_clock_getres - posix getres interface
  289. * @which_clock: clockid
  290. * @tp: timespec to fill
  291. *
  292. * Returns the granularity of underlying alarm base clock
  293. */
  294. static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
  295. {
  296. clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
  297. return hrtimer_get_res(baseid, tp);
  298. }
  299. /**
  300. * alarm_clock_get - posix clock_get interface
  301. * @which_clock: clockid
  302. * @tp: timespec to fill.
  303. *
  304. * Provides the underlying alarm base time.
  305. */
  306. static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
  307. {
  308. struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
  309. *tp = ktime_to_timespec(base->gettime());
  310. return 0;
  311. }
  312. /**
  313. * alarm_timer_create - posix timer_create interface
  314. * @new_timer: k_itimer pointer to manage
  315. *
  316. * Initializes the k_itimer structure.
  317. */
  318. static int alarm_timer_create(struct k_itimer *new_timer)
  319. {
  320. enum alarmtimer_type type;
  321. struct alarm_base *base;
  322. if (!capable(CAP_WAKE_ALARM))
  323. return -EPERM;
  324. type = clock2alarm(new_timer->it_clock);
  325. base = &alarm_bases[type];
  326. alarm_init(&new_timer->it.alarmtimer, type, alarm_handle_timer);
  327. return 0;
  328. }
  329. /**
  330. * alarm_timer_get - posix timer_get interface
  331. * @new_timer: k_itimer pointer
  332. * @cur_setting: itimerspec data to fill
  333. *
  334. * Copies the itimerspec data out from the k_itimer
  335. */
  336. static void alarm_timer_get(struct k_itimer *timr,
  337. struct itimerspec *cur_setting)
  338. {
  339. cur_setting->it_interval =
  340. ktime_to_timespec(timr->it.alarmtimer.period);
  341. cur_setting->it_value =
  342. ktime_to_timespec(timr->it.alarmtimer.node.expires);
  343. return;
  344. }
  345. /**
  346. * alarm_timer_del - posix timer_del interface
  347. * @timr: k_itimer pointer to be deleted
  348. *
  349. * Cancels any programmed alarms for the given timer.
  350. */
  351. static int alarm_timer_del(struct k_itimer *timr)
  352. {
  353. alarm_cancel(&timr->it.alarmtimer);
  354. return 0;
  355. }
  356. /**
  357. * alarm_timer_set - posix timer_set interface
  358. * @timr: k_itimer pointer to be deleted
  359. * @flags: timer flags
  360. * @new_setting: itimerspec to be used
  361. * @old_setting: itimerspec being replaced
  362. *
  363. * Sets the timer to new_setting, and starts the timer.
  364. */
  365. static int alarm_timer_set(struct k_itimer *timr, int flags,
  366. struct itimerspec *new_setting,
  367. struct itimerspec *old_setting)
  368. {
  369. /* Save old values */
  370. old_setting->it_interval =
  371. ktime_to_timespec(timr->it.alarmtimer.period);
  372. old_setting->it_value =
  373. ktime_to_timespec(timr->it.alarmtimer.node.expires);
  374. /* If the timer was already set, cancel it */
  375. alarm_cancel(&timr->it.alarmtimer);
  376. /* start the timer */
  377. alarm_start(&timr->it.alarmtimer,
  378. timespec_to_ktime(new_setting->it_value),
  379. timespec_to_ktime(new_setting->it_interval));
  380. return 0;
  381. }
  382. /**
  383. * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
  384. * @alarm: ptr to alarm that fired
  385. *
  386. * Wakes up the task that set the alarmtimer
  387. */
  388. static void alarmtimer_nsleep_wakeup(struct alarm *alarm)
  389. {
  390. struct task_struct *task = (struct task_struct *)alarm->data;
  391. alarm->data = NULL;
  392. if (task)
  393. wake_up_process(task);
  394. }
  395. /**
  396. * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
  397. * @alarm: ptr to alarmtimer
  398. * @absexp: absolute expiration time
  399. *
  400. * Sets the alarm timer and sleeps until it is fired or interrupted.
  401. */
  402. static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
  403. {
  404. alarm->data = (void *)current;
  405. do {
  406. set_current_state(TASK_INTERRUPTIBLE);
  407. alarm_start(alarm, absexp, ktime_set(0, 0));
  408. if (likely(alarm->data))
  409. schedule();
  410. alarm_cancel(alarm);
  411. } while (alarm->data && !signal_pending(current));
  412. __set_current_state(TASK_RUNNING);
  413. return (alarm->data == NULL);
  414. }
  415. /**
  416. * update_rmtp - Update remaining timespec value
  417. * @exp: expiration time
  418. * @type: timer type
  419. * @rmtp: user pointer to remaining timepsec value
  420. *
  421. * Helper function that fills in rmtp value with time between
  422. * now and the exp value
  423. */
  424. static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
  425. struct timespec __user *rmtp)
  426. {
  427. struct timespec rmt;
  428. ktime_t rem;
  429. rem = ktime_sub(exp, alarm_bases[type].gettime());
  430. if (rem.tv64 <= 0)
  431. return 0;
  432. rmt = ktime_to_timespec(rem);
  433. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  434. return -EFAULT;
  435. return 1;
  436. }
  437. /**
  438. * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
  439. * @restart: ptr to restart block
  440. *
  441. * Handles restarted clock_nanosleep calls
  442. */
  443. static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
  444. {
  445. enum alarmtimer_type type = restart->nanosleep.index;
  446. ktime_t exp;
  447. struct timespec __user *rmtp;
  448. struct alarm alarm;
  449. int ret = 0;
  450. exp.tv64 = restart->nanosleep.expires;
  451. alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
  452. if (alarmtimer_do_nsleep(&alarm, exp))
  453. goto out;
  454. if (freezing(current))
  455. alarmtimer_freezerset(exp, type);
  456. rmtp = restart->nanosleep.rmtp;
  457. if (rmtp) {
  458. ret = update_rmtp(exp, type, rmtp);
  459. if (ret <= 0)
  460. goto out;
  461. }
  462. /* The other values in restart are already filled in */
  463. ret = -ERESTART_RESTARTBLOCK;
  464. out:
  465. return ret;
  466. }
  467. /**
  468. * alarm_timer_nsleep - alarmtimer nanosleep
  469. * @which_clock: clockid
  470. * @flags: determins abstime or relative
  471. * @tsreq: requested sleep time (abs or rel)
  472. * @rmtp: remaining sleep time saved
  473. *
  474. * Handles clock_nanosleep calls against _ALARM clockids
  475. */
  476. static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
  477. struct timespec *tsreq, struct timespec __user *rmtp)
  478. {
  479. enum alarmtimer_type type = clock2alarm(which_clock);
  480. struct alarm alarm;
  481. ktime_t exp;
  482. int ret = 0;
  483. struct restart_block *restart;
  484. if (!capable(CAP_WAKE_ALARM))
  485. return -EPERM;
  486. alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
  487. exp = timespec_to_ktime(*tsreq);
  488. /* Convert (if necessary) to absolute time */
  489. if (flags != TIMER_ABSTIME) {
  490. ktime_t now = alarm_bases[type].gettime();
  491. exp = ktime_add(now, exp);
  492. }
  493. if (alarmtimer_do_nsleep(&alarm, exp))
  494. goto out;
  495. if (freezing(current))
  496. alarmtimer_freezerset(exp, type);
  497. /* abs timers don't set remaining time or restart */
  498. if (flags == TIMER_ABSTIME) {
  499. ret = -ERESTARTNOHAND;
  500. goto out;
  501. }
  502. if (rmtp) {
  503. ret = update_rmtp(exp, type, rmtp);
  504. if (ret <= 0)
  505. goto out;
  506. }
  507. restart = &current_thread_info()->restart_block;
  508. restart->fn = alarm_timer_nsleep_restart;
  509. restart->nanosleep.index = type;
  510. restart->nanosleep.expires = exp.tv64;
  511. restart->nanosleep.rmtp = rmtp;
  512. ret = -ERESTART_RESTARTBLOCK;
  513. out:
  514. return ret;
  515. }
  516. /**************************************************************************
  517. * alarmtimer initialization code
  518. */
  519. /* Suspend hook structures */
  520. static const struct dev_pm_ops alarmtimer_pm_ops = {
  521. .suspend = alarmtimer_suspend,
  522. };
  523. static struct platform_driver alarmtimer_driver = {
  524. .driver = {
  525. .name = "alarmtimer",
  526. .pm = &alarmtimer_pm_ops,
  527. }
  528. };
  529. /**
  530. * alarmtimer_init - Initialize alarm timer code
  531. *
  532. * This function initializes the alarm bases and registers
  533. * the posix clock ids.
  534. */
  535. static int __init alarmtimer_init(void)
  536. {
  537. int error = 0;
  538. int i;
  539. struct k_clock alarm_clock = {
  540. .clock_getres = alarm_clock_getres,
  541. .clock_get = alarm_clock_get,
  542. .timer_create = alarm_timer_create,
  543. .timer_set = alarm_timer_set,
  544. .timer_del = alarm_timer_del,
  545. .timer_get = alarm_timer_get,
  546. .nsleep = alarm_timer_nsleep,
  547. };
  548. posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
  549. posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
  550. /* Initialize alarm bases */
  551. alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
  552. alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
  553. alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
  554. alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
  555. for (i = 0; i < ALARM_NUMTYPE; i++) {
  556. timerqueue_init_head(&alarm_bases[i].timerqueue);
  557. spin_lock_init(&alarm_bases[i].lock);
  558. hrtimer_init(&alarm_bases[i].timer,
  559. alarm_bases[i].base_clockid,
  560. HRTIMER_MODE_ABS);
  561. alarm_bases[i].timer.function = alarmtimer_fired;
  562. INIT_WORK(&alarm_bases[i].irqwork, alarmtimer_do_work);
  563. }
  564. error = platform_driver_register(&alarmtimer_driver);
  565. platform_device_register_simple("alarmtimer", -1, NULL, 0);
  566. return error;
  567. }
  568. device_initcall(alarmtimer_init);
  569. /**
  570. * has_wakealarm - check rtc device has wakealarm ability
  571. * @dev: current device
  572. * @name_ptr: name to be returned
  573. *
  574. * This helper function checks to see if the rtc device can wake
  575. * from suspend.
  576. */
  577. static int __init has_wakealarm(struct device *dev, void *name_ptr)
  578. {
  579. struct rtc_device *candidate = to_rtc_device(dev);
  580. if (!candidate->ops->set_alarm)
  581. return 0;
  582. if (!device_may_wakeup(candidate->dev.parent))
  583. return 0;
  584. *(const char **)name_ptr = dev_name(dev);
  585. return 1;
  586. }
  587. /**
  588. * alarmtimer_init_late - Late initializing of alarmtimer code
  589. *
  590. * This function locates a rtc device to use for wakealarms.
  591. * Run as late_initcall to make sure rtc devices have been
  592. * registered.
  593. */
  594. static int __init alarmtimer_init_late(void)
  595. {
  596. char *str;
  597. /* Find an rtc device and init the rtc_timer */
  598. class_find_device(rtc_class, NULL, &str, has_wakealarm);
  599. if (str)
  600. rtcdev = rtc_class_open(str);
  601. if (!rtcdev) {
  602. printk(KERN_WARNING "No RTC device found, ALARM timers will"
  603. " not wake from suspend");
  604. }
  605. rtc_timer_init(&rtctimer, NULL, NULL);
  606. return 0;
  607. }
  608. late_initcall(alarmtimer_init_late);