alarmtimer.c 18 KB

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