alarmtimer.c 17 KB

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