alarmtimer.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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. ktime_t (*gettime)(void);
  40. clockid_t base_clockid;
  41. } alarm_bases[ALARM_NUMTYPE];
  42. /* freezer delta & lock used to handle clock_nanosleep triggered wakeups */
  43. static ktime_t freezer_delta;
  44. static DEFINE_SPINLOCK(freezer_delta_lock);
  45. static struct wakeup_source *ws;
  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. static void alarmtimer_triggered_func(void *p)
  52. {
  53. struct rtc_device *rtc = rtcdev;
  54. if (!(rtc->irq_data & RTC_AF))
  55. return;
  56. __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
  57. }
  58. static struct rtc_task alarmtimer_rtc_task = {
  59. .func = alarmtimer_triggered_func
  60. };
  61. /**
  62. * alarmtimer_get_rtcdev - Return selected rtcdevice
  63. *
  64. * This function returns the rtc device to use for wakealarms.
  65. * If one has not already been chosen, it checks to see if a
  66. * functional rtc device is available.
  67. */
  68. struct rtc_device *alarmtimer_get_rtcdev(void)
  69. {
  70. unsigned long flags;
  71. struct rtc_device *ret = NULL;
  72. spin_lock_irqsave(&rtcdev_lock, flags);
  73. ret = rtcdev;
  74. spin_unlock_irqrestore(&rtcdev_lock, flags);
  75. return ret;
  76. }
  77. static int alarmtimer_rtc_add_device(struct device *dev,
  78. struct class_interface *class_intf)
  79. {
  80. unsigned long flags;
  81. int err = 0;
  82. struct rtc_device *rtc = to_rtc_device(dev);
  83. if (rtcdev)
  84. return -EBUSY;
  85. if (!rtc->ops->set_alarm)
  86. return -1;
  87. spin_lock_irqsave(&rtcdev_lock, flags);
  88. if (!rtcdev) {
  89. err = rtc_irq_register(rtc, &alarmtimer_rtc_task);
  90. if (err)
  91. goto rtc_irq_reg_err;
  92. rtcdev = rtc;
  93. /* hold a reference so it doesn't go away */
  94. get_device(dev);
  95. }
  96. rtc_irq_reg_err:
  97. spin_unlock_irqrestore(&rtcdev_lock, flags);
  98. return err;
  99. }
  100. static void alarmtimer_rtc_remove_device(struct device *dev,
  101. struct class_interface *class_intf)
  102. {
  103. if (rtcdev && dev == &rtcdev->dev) {
  104. rtc_irq_unregister(rtcdev, &alarmtimer_rtc_task);
  105. rtcdev = NULL;
  106. }
  107. }
  108. static inline void alarmtimer_rtc_timer_init(void)
  109. {
  110. rtc_timer_init(&rtctimer, NULL, NULL);
  111. }
  112. static struct class_interface alarmtimer_rtc_interface = {
  113. .add_dev = &alarmtimer_rtc_add_device,
  114. .remove_dev = &alarmtimer_rtc_remove_device,
  115. };
  116. static int alarmtimer_rtc_interface_setup(void)
  117. {
  118. alarmtimer_rtc_interface.class = rtc_class;
  119. return class_interface_register(&alarmtimer_rtc_interface);
  120. }
  121. static void alarmtimer_rtc_interface_remove(void)
  122. {
  123. class_interface_unregister(&alarmtimer_rtc_interface);
  124. }
  125. #else
  126. struct rtc_device *alarmtimer_get_rtcdev(void)
  127. {
  128. return NULL;
  129. }
  130. #define rtcdev (NULL)
  131. static inline int alarmtimer_rtc_interface_setup(void) { return 0; }
  132. static inline void alarmtimer_rtc_interface_remove(void) { }
  133. static inline void alarmtimer_rtc_timer_init(void) { }
  134. void set_power_on_alarm(long secs, bool enable) { }
  135. #endif
  136. /**
  137. * alarmtimer_enqueue - Adds an alarm timer to an alarm_base timerqueue
  138. * @base: pointer to the base where the timer is being run
  139. * @alarm: pointer to alarm being enqueued.
  140. *
  141. * Adds alarm to a alarm_base timerqueue
  142. *
  143. * Must hold base->lock when calling.
  144. */
  145. static void alarmtimer_enqueue(struct alarm_base *base, struct alarm *alarm)
  146. {
  147. if (alarm->state & ALARMTIMER_STATE_ENQUEUED)
  148. timerqueue_del(&base->timerqueue, &alarm->node);
  149. timerqueue_add(&base->timerqueue, &alarm->node);
  150. alarm->state |= ALARMTIMER_STATE_ENQUEUED;
  151. }
  152. /**
  153. * alarmtimer_dequeue - Removes an alarm timer from an alarm_base timerqueue
  154. * @base: pointer to the base where the timer is running
  155. * @alarm: pointer to alarm being removed
  156. *
  157. * Removes alarm to a alarm_base timerqueue
  158. *
  159. * Must hold base->lock when calling.
  160. */
  161. static void alarmtimer_dequeue(struct alarm_base *base, struct alarm *alarm)
  162. {
  163. if (!(alarm->state & ALARMTIMER_STATE_ENQUEUED))
  164. return;
  165. timerqueue_del(&base->timerqueue, &alarm->node);
  166. alarm->state &= ~ALARMTIMER_STATE_ENQUEUED;
  167. }
  168. /**
  169. * alarmtimer_fired - Handles alarm hrtimer being fired.
  170. * @timer: pointer to hrtimer being run
  171. *
  172. * When a alarm timer fires, this runs through the timerqueue to
  173. * see which alarms expired, and runs those. If there are more alarm
  174. * timers queued for the future, we set the hrtimer to fire when
  175. * when the next future alarm timer expires.
  176. */
  177. static enum hrtimer_restart alarmtimer_fired(struct hrtimer *timer)
  178. {
  179. struct alarm *alarm = container_of(timer, struct alarm, timer);
  180. struct alarm_base *base = &alarm_bases[alarm->type];
  181. unsigned long flags;
  182. int ret = HRTIMER_NORESTART;
  183. int restart = ALARMTIMER_NORESTART;
  184. spin_lock_irqsave(&base->lock, flags);
  185. alarmtimer_dequeue(base, alarm);
  186. spin_unlock_irqrestore(&base->lock, flags);
  187. if (alarm->function)
  188. restart = alarm->function(alarm, base->gettime());
  189. spin_lock_irqsave(&base->lock, flags);
  190. if (restart != ALARMTIMER_NORESTART) {
  191. hrtimer_set_expires(&alarm->timer, alarm->node.expires);
  192. alarmtimer_enqueue(base, alarm);
  193. ret = HRTIMER_RESTART;
  194. }
  195. spin_unlock_irqrestore(&base->lock, flags);
  196. return ret;
  197. }
  198. ktime_t alarm_expires_remaining(const struct alarm *alarm)
  199. {
  200. struct alarm_base *base = &alarm_bases[alarm->type];
  201. return ktime_sub(alarm->node.expires, base->gettime());
  202. }
  203. #ifdef CONFIG_RTC_CLASS
  204. /**
  205. * alarmtimer_suspend - Suspend time callback
  206. * @dev: unused
  207. * @state: unused
  208. *
  209. * When we are going into suspend, we look through the bases
  210. * to see which is the soonest timer to expire. We then
  211. * set an rtc timer to fire that far into the future, which
  212. * will wake us from suspend.
  213. */
  214. static int alarmtimer_suspend(struct device *dev)
  215. {
  216. struct rtc_time tm;
  217. ktime_t min, now;
  218. unsigned long flags;
  219. struct rtc_device *rtc;
  220. int i;
  221. int ret;
  222. spin_lock_irqsave(&freezer_delta_lock, flags);
  223. min = freezer_delta;
  224. freezer_delta = ktime_set(0, 0);
  225. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  226. rtc = alarmtimer_get_rtcdev();
  227. /* If we have no rtcdev, just return */
  228. if (!rtc)
  229. return 0;
  230. /* Find the soonest timer to expire*/
  231. for (i = 0; i < ALARM_NUMTYPE; i++) {
  232. struct alarm_base *base = &alarm_bases[i];
  233. struct timerqueue_node *next;
  234. ktime_t delta;
  235. spin_lock_irqsave(&base->lock, flags);
  236. next = timerqueue_getnext(&base->timerqueue);
  237. spin_unlock_irqrestore(&base->lock, flags);
  238. if (!next)
  239. continue;
  240. delta = ktime_sub(next->expires, base->gettime());
  241. if (!min.tv64 || (delta.tv64 < min.tv64))
  242. min = delta;
  243. }
  244. if (min.tv64 == 0)
  245. return 0;
  246. if (ktime_to_ns(min) < 2 * NSEC_PER_SEC) {
  247. __pm_wakeup_event(ws, 2 * MSEC_PER_SEC);
  248. return -EBUSY;
  249. }
  250. /* Setup an rtc timer to fire that far in the future */
  251. rtc_timer_cancel(rtc, &rtctimer);
  252. rtc_read_time(rtc, &tm);
  253. now = rtc_tm_to_ktime(tm);
  254. now = ktime_add(now, min);
  255. /* Set alarm, if in the past reject suspend briefly to handle */
  256. ret = rtc_timer_start(rtc, &rtctimer, now, ktime_set(0, 0));
  257. if (ret < 0)
  258. __pm_wakeup_event(ws, 1 * MSEC_PER_SEC);
  259. return ret;
  260. }
  261. static int alarmtimer_resume(struct device *dev)
  262. {
  263. struct rtc_device *rtc;
  264. rtc = alarmtimer_get_rtcdev();
  265. /* If we have no rtcdev, just return */
  266. if (!rtc)
  267. return 0;
  268. rtc_timer_cancel(rtc, &rtctimer);
  269. return 0;
  270. }
  271. #else
  272. static int alarmtimer_suspend(struct device *dev)
  273. {
  274. return 0;
  275. }
  276. static int alarmtimer_resume(struct device *dev)
  277. {
  278. return 0;
  279. }
  280. #endif
  281. static void alarmtimer_freezerset(ktime_t absexp, enum alarmtimer_type type)
  282. {
  283. ktime_t delta;
  284. unsigned long flags;
  285. struct alarm_base *base = &alarm_bases[type];
  286. delta = ktime_sub(absexp, base->gettime());
  287. spin_lock_irqsave(&freezer_delta_lock, flags);
  288. if (!freezer_delta.tv64 || (delta.tv64 < freezer_delta.tv64))
  289. freezer_delta = delta;
  290. spin_unlock_irqrestore(&freezer_delta_lock, flags);
  291. }
  292. /**
  293. * alarm_init - Initialize an alarm structure
  294. * @alarm: ptr to alarm to be initialized
  295. * @type: the type of the alarm
  296. * @function: callback that is run when the alarm fires
  297. */
  298. void alarm_init(struct alarm *alarm, enum alarmtimer_type type,
  299. enum alarmtimer_restart (*function)(struct alarm *, ktime_t))
  300. {
  301. timerqueue_init(&alarm->node);
  302. hrtimer_init(&alarm->timer, alarm_bases[type].base_clockid,
  303. HRTIMER_MODE_ABS);
  304. alarm->timer.function = alarmtimer_fired;
  305. alarm->function = function;
  306. alarm->type = type;
  307. alarm->state = ALARMTIMER_STATE_INACTIVE;
  308. }
  309. /**
  310. * alarm_start - Sets an absolute alarm to fire
  311. * @alarm: ptr to alarm to set
  312. * @start: time to run the alarm
  313. */
  314. int alarm_start(struct alarm *alarm, ktime_t start)
  315. {
  316. struct alarm_base *base = &alarm_bases[alarm->type];
  317. unsigned long flags;
  318. int ret;
  319. spin_lock_irqsave(&base->lock, flags);
  320. alarm->node.expires = start;
  321. alarmtimer_enqueue(base, alarm);
  322. ret = hrtimer_start(&alarm->timer, alarm->node.expires,
  323. HRTIMER_MODE_ABS);
  324. spin_unlock_irqrestore(&base->lock, flags);
  325. return ret;
  326. }
  327. /**
  328. * alarm_start_relative - Sets a relative alarm to fire
  329. * @alarm: ptr to alarm to set
  330. * @start: time relative to now to run the alarm
  331. */
  332. int alarm_start_relative(struct alarm *alarm, ktime_t start)
  333. {
  334. struct alarm_base *base = &alarm_bases[alarm->type];
  335. start = ktime_add(start, base->gettime());
  336. return alarm_start(alarm, start);
  337. }
  338. void alarm_restart(struct alarm *alarm)
  339. {
  340. struct alarm_base *base = &alarm_bases[alarm->type];
  341. unsigned long flags;
  342. spin_lock_irqsave(&base->lock, flags);
  343. hrtimer_set_expires(&alarm->timer, alarm->node.expires);
  344. hrtimer_restart(&alarm->timer);
  345. alarmtimer_enqueue(base, alarm);
  346. spin_unlock_irqrestore(&base->lock, flags);
  347. }
  348. /**
  349. * alarm_try_to_cancel - Tries to cancel an alarm timer
  350. * @alarm: ptr to alarm to be canceled
  351. *
  352. * Returns 1 if the timer was canceled, 0 if it was not running,
  353. * and -1 if the callback was running
  354. */
  355. int alarm_try_to_cancel(struct alarm *alarm)
  356. {
  357. struct alarm_base *base = &alarm_bases[alarm->type];
  358. unsigned long flags;
  359. int ret;
  360. spin_lock_irqsave(&base->lock, flags);
  361. ret = hrtimer_try_to_cancel(&alarm->timer);
  362. if (ret >= 0)
  363. alarmtimer_dequeue(base, alarm);
  364. spin_unlock_irqrestore(&base->lock, flags);
  365. return ret;
  366. }
  367. /**
  368. * alarm_cancel - Spins trying to cancel an alarm timer until it is done
  369. * @alarm: ptr to alarm to be canceled
  370. *
  371. * Returns 1 if the timer was canceled, 0 if it was not active.
  372. */
  373. int alarm_cancel(struct alarm *alarm)
  374. {
  375. for (;;) {
  376. int ret = alarm_try_to_cancel(alarm);
  377. if (ret >= 0)
  378. return ret;
  379. cpu_relax();
  380. }
  381. }
  382. u64 alarm_forward(struct alarm *alarm, ktime_t now, ktime_t interval)
  383. {
  384. u64 overrun = 1;
  385. ktime_t delta;
  386. delta = ktime_sub(now, alarm->node.expires);
  387. if (delta.tv64 < 0)
  388. return 0;
  389. if (unlikely(delta.tv64 >= interval.tv64)) {
  390. s64 incr = ktime_to_ns(interval);
  391. overrun = ktime_divns(delta, incr);
  392. alarm->node.expires = ktime_add_ns(alarm->node.expires,
  393. incr*overrun);
  394. if (alarm->node.expires.tv64 > now.tv64)
  395. return overrun;
  396. /*
  397. * This (and the ktime_add() below) is the
  398. * correction for exact:
  399. */
  400. overrun++;
  401. }
  402. alarm->node.expires = ktime_add(alarm->node.expires, interval);
  403. return overrun;
  404. }
  405. u64 alarm_forward_now(struct alarm *alarm, ktime_t interval)
  406. {
  407. struct alarm_base *base = &alarm_bases[alarm->type];
  408. return alarm_forward(alarm, base->gettime(), interval);
  409. }
  410. /**
  411. * clock2alarm - helper that converts from clockid to alarmtypes
  412. * @clockid: clockid.
  413. */
  414. static enum alarmtimer_type clock2alarm(clockid_t clockid)
  415. {
  416. if (clockid == CLOCK_REALTIME_ALARM)
  417. return ALARM_REALTIME;
  418. if (clockid == CLOCK_BOOTTIME_ALARM)
  419. return ALARM_BOOTTIME;
  420. return -1;
  421. }
  422. /**
  423. * alarm_handle_timer - Callback for posix timers
  424. * @alarm: alarm that fired
  425. *
  426. * Posix timer callback for expired alarm timers.
  427. */
  428. static enum alarmtimer_restart alarm_handle_timer(struct alarm *alarm,
  429. ktime_t now)
  430. {
  431. unsigned long flags;
  432. struct k_itimer *ptr = container_of(alarm, struct k_itimer,
  433. it.alarm.alarmtimer);
  434. enum alarmtimer_restart result = ALARMTIMER_NORESTART;
  435. spin_lock_irqsave(&ptr->it_lock, flags);
  436. if ((ptr->it_sigev_notify & ~SIGEV_THREAD_ID) != SIGEV_NONE) {
  437. if (posix_timer_event(ptr, 0) != 0)
  438. ptr->it_overrun++;
  439. }
  440. /* Re-add periodic timers */
  441. if (ptr->it.alarm.interval.tv64) {
  442. ptr->it_overrun += alarm_forward(alarm, now,
  443. ptr->it.alarm.interval);
  444. result = ALARMTIMER_RESTART;
  445. }
  446. spin_unlock_irqrestore(&ptr->it_lock, flags);
  447. return result;
  448. }
  449. /**
  450. * alarm_clock_getres - posix getres interface
  451. * @which_clock: clockid
  452. * @tp: timespec to fill
  453. *
  454. * Returns the granularity of underlying alarm base clock
  455. */
  456. static int alarm_clock_getres(const clockid_t which_clock, struct timespec *tp)
  457. {
  458. clockid_t baseid = alarm_bases[clock2alarm(which_clock)].base_clockid;
  459. if (!alarmtimer_get_rtcdev())
  460. return -EINVAL;
  461. return hrtimer_get_res(baseid, tp);
  462. }
  463. /**
  464. * alarm_clock_get - posix clock_get interface
  465. * @which_clock: clockid
  466. * @tp: timespec to fill.
  467. *
  468. * Provides the underlying alarm base time.
  469. */
  470. static int alarm_clock_get(clockid_t which_clock, struct timespec *tp)
  471. {
  472. struct alarm_base *base = &alarm_bases[clock2alarm(which_clock)];
  473. if (!alarmtimer_get_rtcdev())
  474. return -EINVAL;
  475. *tp = ktime_to_timespec(base->gettime());
  476. return 0;
  477. }
  478. /**
  479. * alarm_timer_create - posix timer_create interface
  480. * @new_timer: k_itimer pointer to manage
  481. *
  482. * Initializes the k_itimer structure.
  483. */
  484. static int alarm_timer_create(struct k_itimer *new_timer)
  485. {
  486. enum alarmtimer_type type;
  487. struct alarm_base *base;
  488. if (!alarmtimer_get_rtcdev())
  489. return -ENOTSUPP;
  490. if (!capable(CAP_WAKE_ALARM))
  491. return -EPERM;
  492. type = clock2alarm(new_timer->it_clock);
  493. base = &alarm_bases[type];
  494. alarm_init(&new_timer->it.alarm.alarmtimer, type, alarm_handle_timer);
  495. return 0;
  496. }
  497. /**
  498. * alarm_timer_get - posix timer_get interface
  499. * @new_timer: k_itimer pointer
  500. * @cur_setting: itimerspec data to fill
  501. *
  502. * Copies out the current itimerspec data
  503. */
  504. static void alarm_timer_get(struct k_itimer *timr,
  505. struct itimerspec *cur_setting)
  506. {
  507. ktime_t relative_expiry_time =
  508. alarm_expires_remaining(&(timr->it.alarm.alarmtimer));
  509. if (ktime_to_ns(relative_expiry_time) > 0) {
  510. cur_setting->it_value = ktime_to_timespec(relative_expiry_time);
  511. } else {
  512. cur_setting->it_value.tv_sec = 0;
  513. cur_setting->it_value.tv_nsec = 0;
  514. }
  515. cur_setting->it_interval = ktime_to_timespec(timr->it.alarm.interval);
  516. }
  517. /**
  518. * alarm_timer_del - posix timer_del interface
  519. * @timr: k_itimer pointer to be deleted
  520. *
  521. * Cancels any programmed alarms for the given timer.
  522. */
  523. static int alarm_timer_del(struct k_itimer *timr)
  524. {
  525. if (!rtcdev)
  526. return -ENOTSUPP;
  527. if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
  528. return TIMER_RETRY;
  529. return 0;
  530. }
  531. /**
  532. * alarm_timer_set - posix timer_set interface
  533. * @timr: k_itimer pointer to be deleted
  534. * @flags: timer flags
  535. * @new_setting: itimerspec to be used
  536. * @old_setting: itimerspec being replaced
  537. *
  538. * Sets the timer to new_setting, and starts the timer.
  539. */
  540. static int alarm_timer_set(struct k_itimer *timr, int flags,
  541. struct itimerspec *new_setting,
  542. struct itimerspec *old_setting)
  543. {
  544. ktime_t exp;
  545. if (!rtcdev)
  546. return -ENOTSUPP;
  547. if (flags & ~TIMER_ABSTIME)
  548. return -EINVAL;
  549. if (old_setting)
  550. alarm_timer_get(timr, old_setting);
  551. /* If the timer was already set, cancel it */
  552. if (alarm_try_to_cancel(&timr->it.alarm.alarmtimer) < 0)
  553. return TIMER_RETRY;
  554. /* start the timer */
  555. timr->it.alarm.interval = timespec_to_ktime(new_setting->it_interval);
  556. exp = timespec_to_ktime(new_setting->it_value);
  557. /* Convert (if necessary) to absolute time */
  558. if (flags != TIMER_ABSTIME) {
  559. ktime_t now;
  560. now = alarm_bases[timr->it.alarm.alarmtimer.type].gettime();
  561. exp = ktime_add(now, exp);
  562. }
  563. alarm_start(&timr->it.alarm.alarmtimer, exp);
  564. return 0;
  565. }
  566. /**
  567. * alarmtimer_nsleep_wakeup - Wakeup function for alarm_timer_nsleep
  568. * @alarm: ptr to alarm that fired
  569. *
  570. * Wakes up the task that set the alarmtimer
  571. */
  572. static enum alarmtimer_restart alarmtimer_nsleep_wakeup(struct alarm *alarm,
  573. ktime_t now)
  574. {
  575. struct task_struct *task = (struct task_struct *)alarm->data;
  576. alarm->data = NULL;
  577. if (task)
  578. wake_up_process(task);
  579. return ALARMTIMER_NORESTART;
  580. }
  581. /**
  582. * alarmtimer_do_nsleep - Internal alarmtimer nsleep implementation
  583. * @alarm: ptr to alarmtimer
  584. * @absexp: absolute expiration time
  585. *
  586. * Sets the alarm timer and sleeps until it is fired or interrupted.
  587. */
  588. static int alarmtimer_do_nsleep(struct alarm *alarm, ktime_t absexp)
  589. {
  590. alarm->data = (void *)current;
  591. do {
  592. set_current_state(TASK_INTERRUPTIBLE);
  593. alarm_start(alarm, absexp);
  594. if (likely(alarm->data))
  595. schedule();
  596. alarm_cancel(alarm);
  597. } while (alarm->data && !signal_pending(current));
  598. __set_current_state(TASK_RUNNING);
  599. return (alarm->data == NULL);
  600. }
  601. /**
  602. * update_rmtp - Update remaining timespec value
  603. * @exp: expiration time
  604. * @type: timer type
  605. * @rmtp: user pointer to remaining timepsec value
  606. *
  607. * Helper function that fills in rmtp value with time between
  608. * now and the exp value
  609. */
  610. static int update_rmtp(ktime_t exp, enum alarmtimer_type type,
  611. struct timespec __user *rmtp)
  612. {
  613. struct timespec rmt;
  614. ktime_t rem;
  615. rem = ktime_sub(exp, alarm_bases[type].gettime());
  616. if (rem.tv64 <= 0)
  617. return 0;
  618. rmt = ktime_to_timespec(rem);
  619. if (copy_to_user(rmtp, &rmt, sizeof(*rmtp)))
  620. return -EFAULT;
  621. return 1;
  622. }
  623. /**
  624. * alarm_timer_nsleep_restart - restartblock alarmtimer nsleep
  625. * @restart: ptr to restart block
  626. *
  627. * Handles restarted clock_nanosleep calls
  628. */
  629. static long __sched alarm_timer_nsleep_restart(struct restart_block *restart)
  630. {
  631. enum alarmtimer_type type = restart->nanosleep.clockid;
  632. ktime_t exp;
  633. struct timespec __user *rmtp;
  634. struct alarm alarm;
  635. int ret = 0;
  636. exp.tv64 = restart->nanosleep.expires;
  637. alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
  638. if (alarmtimer_do_nsleep(&alarm, exp))
  639. goto out;
  640. if (freezing(current))
  641. alarmtimer_freezerset(exp, type);
  642. rmtp = restart->nanosleep.rmtp;
  643. if (rmtp) {
  644. ret = update_rmtp(exp, type, rmtp);
  645. if (ret <= 0)
  646. goto out;
  647. }
  648. /* The other values in restart are already filled in */
  649. ret = -ERESTART_RESTARTBLOCK;
  650. out:
  651. return ret;
  652. }
  653. /**
  654. * alarm_timer_nsleep - alarmtimer nanosleep
  655. * @which_clock: clockid
  656. * @flags: determins abstime or relative
  657. * @tsreq: requested sleep time (abs or rel)
  658. * @rmtp: remaining sleep time saved
  659. *
  660. * Handles clock_nanosleep calls against _ALARM clockids
  661. */
  662. static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
  663. struct timespec *tsreq, struct timespec __user *rmtp)
  664. {
  665. enum alarmtimer_type type = clock2alarm(which_clock);
  666. struct alarm alarm;
  667. ktime_t exp;
  668. int ret = 0;
  669. struct restart_block *restart;
  670. if (!alarmtimer_get_rtcdev())
  671. return -ENOTSUPP;
  672. if (flags & ~TIMER_ABSTIME)
  673. return -EINVAL;
  674. if (!capable(CAP_WAKE_ALARM))
  675. return -EPERM;
  676. alarm_init(&alarm, type, alarmtimer_nsleep_wakeup);
  677. exp = timespec_to_ktime(*tsreq);
  678. /* Convert (if necessary) to absolute time */
  679. if (flags != TIMER_ABSTIME) {
  680. ktime_t now = alarm_bases[type].gettime();
  681. exp = ktime_add_safe(now, exp);
  682. }
  683. if (alarmtimer_do_nsleep(&alarm, exp))
  684. goto out;
  685. if (freezing(current))
  686. alarmtimer_freezerset(exp, type);
  687. /* abs timers don't set remaining time or restart */
  688. if (flags == TIMER_ABSTIME) {
  689. ret = -ERESTARTNOHAND;
  690. goto out;
  691. }
  692. if (rmtp) {
  693. ret = update_rmtp(exp, type, rmtp);
  694. if (ret <= 0)
  695. goto out;
  696. }
  697. restart = &current_thread_info()->restart_block;
  698. restart->fn = alarm_timer_nsleep_restart;
  699. restart->nanosleep.clockid = type;
  700. restart->nanosleep.expires = exp.tv64;
  701. restart->nanosleep.rmtp = rmtp;
  702. ret = -ERESTART_RESTARTBLOCK;
  703. out:
  704. return ret;
  705. }
  706. /* Suspend hook structures */
  707. static const struct dev_pm_ops alarmtimer_pm_ops = {
  708. .suspend = alarmtimer_suspend,
  709. .resume = alarmtimer_resume,
  710. };
  711. static struct platform_driver alarmtimer_driver = {
  712. .driver = {
  713. .name = "alarmtimer",
  714. .pm = &alarmtimer_pm_ops,
  715. }
  716. };
  717. /**
  718. * alarmtimer_init - Initialize alarm timer code
  719. *
  720. * This function initializes the alarm bases and registers
  721. * the posix clock ids.
  722. */
  723. static int __init alarmtimer_init(void)
  724. {
  725. struct platform_device *pdev;
  726. int error = 0;
  727. int i;
  728. struct k_clock alarm_clock = {
  729. .clock_getres = alarm_clock_getres,
  730. .clock_get = alarm_clock_get,
  731. .timer_create = alarm_timer_create,
  732. .timer_set = alarm_timer_set,
  733. .timer_del = alarm_timer_del,
  734. .timer_get = alarm_timer_get,
  735. .nsleep = alarm_timer_nsleep,
  736. };
  737. alarmtimer_rtc_timer_init();
  738. posix_timers_register_clock(CLOCK_REALTIME_ALARM, &alarm_clock);
  739. posix_timers_register_clock(CLOCK_BOOTTIME_ALARM, &alarm_clock);
  740. /* Initialize alarm bases */
  741. alarm_bases[ALARM_REALTIME].base_clockid = CLOCK_REALTIME;
  742. alarm_bases[ALARM_REALTIME].gettime = &ktime_get_real;
  743. alarm_bases[ALARM_BOOTTIME].base_clockid = CLOCK_BOOTTIME;
  744. alarm_bases[ALARM_BOOTTIME].gettime = &ktime_get_boottime;
  745. for (i = 0; i < ALARM_NUMTYPE; i++) {
  746. timerqueue_init_head(&alarm_bases[i].timerqueue);
  747. spin_lock_init(&alarm_bases[i].lock);
  748. }
  749. error = alarmtimer_rtc_interface_setup();
  750. if (error)
  751. return error;
  752. error = platform_driver_register(&alarmtimer_driver);
  753. if (error)
  754. goto out_if;
  755. pdev = platform_device_register_simple("alarmtimer", -1, NULL, 0);
  756. if (IS_ERR(pdev)) {
  757. error = PTR_ERR(pdev);
  758. goto out_drv;
  759. }
  760. ws = wakeup_source_register("alarmtimer");
  761. return 0;
  762. out_drv:
  763. platform_driver_unregister(&alarmtimer_driver);
  764. out_if:
  765. alarmtimer_rtc_interface_remove();
  766. return error;
  767. }
  768. device_initcall(alarmtimer_init);