alarmtimer.c 18 KB

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