interface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * RTC subsystem, interface functions
  3. *
  4. * Copyright (C) 2005 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * based on arch/arm/common/rtctime.c
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <linux/rtc.h>
  14. #include <linux/sched.h>
  15. #include <linux/module.h>
  16. #include <linux/log2.h>
  17. #include <linux/workqueue.h>
  18. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer);
  19. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer);
  20. static int __rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  21. {
  22. int err;
  23. if (!rtc->ops)
  24. err = -ENODEV;
  25. else if (!rtc->ops->read_time)
  26. err = -EINVAL;
  27. else {
  28. memset(tm, 0, sizeof(struct rtc_time));
  29. err = rtc->ops->read_time(rtc->dev.parent, tm);
  30. }
  31. return err;
  32. }
  33. int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  34. {
  35. int err;
  36. err = mutex_lock_interruptible(&rtc->ops_lock);
  37. if (err)
  38. return err;
  39. err = __rtc_read_time(rtc, tm);
  40. mutex_unlock(&rtc->ops_lock);
  41. return err;
  42. }
  43. EXPORT_SYMBOL_GPL(rtc_read_time);
  44. int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
  45. {
  46. int err;
  47. err = rtc_valid_tm(tm);
  48. if (err != 0)
  49. return err;
  50. err = mutex_lock_interruptible(&rtc->ops_lock);
  51. if (err)
  52. return err;
  53. if (!rtc->ops)
  54. err = -ENODEV;
  55. else if (rtc->ops->set_time)
  56. err = rtc->ops->set_time(rtc->dev.parent, tm);
  57. else if (rtc->ops->set_mmss) {
  58. unsigned long secs;
  59. err = rtc_tm_to_time(tm, &secs);
  60. if (err == 0)
  61. err = rtc->ops->set_mmss(rtc->dev.parent, secs);
  62. } else
  63. err = -EINVAL;
  64. mutex_unlock(&rtc->ops_lock);
  65. return err;
  66. }
  67. EXPORT_SYMBOL_GPL(rtc_set_time);
  68. int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
  69. {
  70. int err;
  71. err = mutex_lock_interruptible(&rtc->ops_lock);
  72. if (err)
  73. return err;
  74. if (!rtc->ops)
  75. err = -ENODEV;
  76. else if (rtc->ops->set_mmss)
  77. err = rtc->ops->set_mmss(rtc->dev.parent, secs);
  78. else if (rtc->ops->read_time && rtc->ops->set_time) {
  79. struct rtc_time new, old;
  80. err = rtc->ops->read_time(rtc->dev.parent, &old);
  81. if (err == 0) {
  82. rtc_time_to_tm(secs, &new);
  83. /*
  84. * avoid writing when we're going to change the day of
  85. * the month. We will retry in the next minute. This
  86. * basically means that if the RTC must not drift
  87. * by more than 1 minute in 11 minutes.
  88. */
  89. if (!((old.tm_hour == 23 && old.tm_min == 59) ||
  90. (new.tm_hour == 23 && new.tm_min == 59)))
  91. err = rtc->ops->set_time(rtc->dev.parent,
  92. &new);
  93. }
  94. }
  95. else
  96. err = -EINVAL;
  97. mutex_unlock(&rtc->ops_lock);
  98. return err;
  99. }
  100. EXPORT_SYMBOL_GPL(rtc_set_mmss);
  101. static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  102. {
  103. int err;
  104. err = mutex_lock_interruptible(&rtc->ops_lock);
  105. if (err)
  106. return err;
  107. if (rtc->ops == NULL)
  108. err = -ENODEV;
  109. else if (!rtc->ops->read_alarm)
  110. err = -EINVAL;
  111. else {
  112. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  113. err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
  114. }
  115. mutex_unlock(&rtc->ops_lock);
  116. return err;
  117. }
  118. int __rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  119. {
  120. int err;
  121. struct rtc_time before, now;
  122. int first_time = 1;
  123. unsigned long t_now, t_alm;
  124. enum { none, day, month, year } missing = none;
  125. unsigned days;
  126. /* The lower level RTC driver may return -1 in some fields,
  127. * creating invalid alarm->time values, for reasons like:
  128. *
  129. * - The hardware may not be capable of filling them in;
  130. * many alarms match only on time-of-day fields, not
  131. * day/month/year calendar data.
  132. *
  133. * - Some hardware uses illegal values as "wildcard" match
  134. * values, which non-Linux firmware (like a BIOS) may try
  135. * to set up as e.g. "alarm 15 minutes after each hour".
  136. * Linux uses only oneshot alarms.
  137. *
  138. * When we see that here, we deal with it by using values from
  139. * a current RTC timestamp for any missing (-1) values. The
  140. * RTC driver prevents "periodic alarm" modes.
  141. *
  142. * But this can be racey, because some fields of the RTC timestamp
  143. * may have wrapped in the interval since we read the RTC alarm,
  144. * which would lead to us inserting inconsistent values in place
  145. * of the -1 fields.
  146. *
  147. * Reading the alarm and timestamp in the reverse sequence
  148. * would have the same race condition, and not solve the issue.
  149. *
  150. * So, we must first read the RTC timestamp,
  151. * then read the RTC alarm value,
  152. * and then read a second RTC timestamp.
  153. *
  154. * If any fields of the second timestamp have changed
  155. * when compared with the first timestamp, then we know
  156. * our timestamp may be inconsistent with that used by
  157. * the low-level rtc_read_alarm_internal() function.
  158. *
  159. * So, when the two timestamps disagree, we just loop and do
  160. * the process again to get a fully consistent set of values.
  161. *
  162. * This could all instead be done in the lower level driver,
  163. * but since more than one lower level RTC implementation needs it,
  164. * then it's probably best best to do it here instead of there..
  165. */
  166. /* Get the "before" timestamp */
  167. err = rtc_read_time(rtc, &before);
  168. if (err < 0)
  169. return err;
  170. do {
  171. if (!first_time)
  172. memcpy(&before, &now, sizeof(struct rtc_time));
  173. first_time = 0;
  174. /* get the RTC alarm values, which may be incomplete */
  175. err = rtc_read_alarm_internal(rtc, alarm);
  176. if (err)
  177. return err;
  178. /* full-function RTCs won't have such missing fields */
  179. if (rtc_valid_tm(&alarm->time) == 0)
  180. return 0;
  181. /* get the "after" timestamp, to detect wrapped fields */
  182. err = rtc_read_time(rtc, &now);
  183. if (err < 0)
  184. return err;
  185. /* note that tm_sec is a "don't care" value here: */
  186. } while ( before.tm_min != now.tm_min
  187. || before.tm_hour != now.tm_hour
  188. || before.tm_mon != now.tm_mon
  189. || before.tm_year != now.tm_year);
  190. /* Fill in the missing alarm fields using the timestamp; we
  191. * know there's at least one since alarm->time is invalid.
  192. */
  193. if (alarm->time.tm_sec == -1)
  194. alarm->time.tm_sec = now.tm_sec;
  195. if (alarm->time.tm_min == -1)
  196. alarm->time.tm_min = now.tm_min;
  197. if (alarm->time.tm_hour == -1)
  198. alarm->time.tm_hour = now.tm_hour;
  199. /* For simplicity, only support date rollover for now */
  200. if (alarm->time.tm_mday == -1) {
  201. alarm->time.tm_mday = now.tm_mday;
  202. missing = day;
  203. }
  204. if (alarm->time.tm_mon == -1) {
  205. alarm->time.tm_mon = now.tm_mon;
  206. if (missing == none)
  207. missing = month;
  208. }
  209. if (alarm->time.tm_year == -1) {
  210. alarm->time.tm_year = now.tm_year;
  211. if (missing == none)
  212. missing = year;
  213. }
  214. /* with luck, no rollover is needed */
  215. rtc_tm_to_time(&now, &t_now);
  216. rtc_tm_to_time(&alarm->time, &t_alm);
  217. if (t_now < t_alm)
  218. goto done;
  219. switch (missing) {
  220. /* 24 hour rollover ... if it's now 10am Monday, an alarm that
  221. * that will trigger at 5am will do so at 5am Tuesday, which
  222. * could also be in the next month or year. This is a common
  223. * case, especially for PCs.
  224. */
  225. case day:
  226. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
  227. t_alm += 24 * 60 * 60;
  228. rtc_time_to_tm(t_alm, &alarm->time);
  229. break;
  230. /* Month rollover ... if it's the 31th, an alarm on the 3rd will
  231. * be next month. An alarm matching on the 30th, 29th, or 28th
  232. * may end up in the month after that! Many newer PCs support
  233. * this type of alarm.
  234. */
  235. case month:
  236. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
  237. do {
  238. if (alarm->time.tm_mon < 11)
  239. alarm->time.tm_mon++;
  240. else {
  241. alarm->time.tm_mon = 0;
  242. alarm->time.tm_year++;
  243. }
  244. days = rtc_month_days(alarm->time.tm_mon,
  245. alarm->time.tm_year);
  246. } while (days < alarm->time.tm_mday);
  247. break;
  248. /* Year rollover ... easy except for leap years! */
  249. case year:
  250. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
  251. do {
  252. alarm->time.tm_year++;
  253. } while (rtc_valid_tm(&alarm->time) != 0);
  254. break;
  255. default:
  256. dev_warn(&rtc->dev, "alarm rollover not handled\n");
  257. }
  258. done:
  259. return 0;
  260. }
  261. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  262. {
  263. int err;
  264. err = mutex_lock_interruptible(&rtc->ops_lock);
  265. if (err)
  266. return err;
  267. if (rtc->ops == NULL)
  268. err = -ENODEV;
  269. else if (!rtc->ops->read_alarm)
  270. err = -EINVAL;
  271. else {
  272. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  273. alarm->enabled = rtc->aie_timer.enabled;
  274. alarm->time = rtc_ktime_to_tm(rtc->aie_timer.node.expires);
  275. }
  276. mutex_unlock(&rtc->ops_lock);
  277. return err;
  278. }
  279. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  280. static int ___rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  281. {
  282. int err;
  283. if (!rtc->ops)
  284. err = -ENODEV;
  285. else if (!rtc->ops->set_alarm)
  286. err = -EINVAL;
  287. else
  288. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  289. return err;
  290. }
  291. static int __rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  292. {
  293. struct rtc_time tm;
  294. long now, scheduled;
  295. int err;
  296. err = rtc_valid_tm(&alarm->time);
  297. if (err)
  298. return err;
  299. rtc_tm_to_time(&alarm->time, &scheduled);
  300. /* Make sure we're not setting alarms in the past */
  301. err = __rtc_read_time(rtc, &tm);
  302. rtc_tm_to_time(&tm, &now);
  303. if (scheduled <= now)
  304. return -ETIME;
  305. /*
  306. * XXX - We just checked to make sure the alarm time is not
  307. * in the past, but there is still a race window where if
  308. * the is alarm set for the next second and the second ticks
  309. * over right here, before we set the alarm.
  310. */
  311. return ___rtc_set_alarm(rtc, alarm);
  312. }
  313. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  314. {
  315. int err;
  316. err = rtc_valid_tm(&alarm->time);
  317. if (err != 0)
  318. return err;
  319. err = mutex_lock_interruptible(&rtc->ops_lock);
  320. if (err)
  321. return err;
  322. if (rtc->aie_timer.enabled) {
  323. rtc_timer_remove(rtc, &rtc->aie_timer);
  324. }
  325. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  326. rtc->aie_timer.period = ktime_set(0, 0);
  327. if (alarm->enabled) {
  328. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  329. }
  330. mutex_unlock(&rtc->ops_lock);
  331. return err;
  332. }
  333. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  334. /* Called once per device from rtc_device_register */
  335. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  336. {
  337. int err;
  338. err = rtc_valid_tm(&alarm->time);
  339. if (err != 0)
  340. return err;
  341. err = mutex_lock_interruptible(&rtc->ops_lock);
  342. if (err)
  343. return err;
  344. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  345. rtc->aie_timer.period = ktime_set(0, 0);
  346. if (alarm->enabled) {
  347. rtc->aie_timer.enabled = 1;
  348. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  349. }
  350. mutex_unlock(&rtc->ops_lock);
  351. return err;
  352. }
  353. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  354. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  355. {
  356. int err = mutex_lock_interruptible(&rtc->ops_lock);
  357. if (err)
  358. return err;
  359. if (rtc->aie_timer.enabled != enabled) {
  360. if (enabled)
  361. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  362. else
  363. rtc_timer_remove(rtc, &rtc->aie_timer);
  364. }
  365. if (err)
  366. /* nothing */;
  367. else if (!rtc->ops)
  368. err = -ENODEV;
  369. else if (!rtc->ops->alarm_irq_enable)
  370. err = -EINVAL;
  371. else
  372. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  373. mutex_unlock(&rtc->ops_lock);
  374. return err;
  375. }
  376. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  377. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  378. {
  379. int err = mutex_lock_interruptible(&rtc->ops_lock);
  380. if (err)
  381. return err;
  382. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  383. if (enabled == 0 && rtc->uie_irq_active) {
  384. mutex_unlock(&rtc->ops_lock);
  385. return rtc_dev_update_irq_enable_emul(rtc, 0);
  386. }
  387. #endif
  388. /* make sure we're changing state */
  389. if (rtc->uie_rtctimer.enabled == enabled)
  390. goto out;
  391. if (enabled) {
  392. struct rtc_time tm;
  393. ktime_t now, onesec;
  394. __rtc_read_time(rtc, &tm);
  395. onesec = ktime_set(1, 0);
  396. now = rtc_tm_to_ktime(tm);
  397. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  398. rtc->uie_rtctimer.period = ktime_set(1, 0);
  399. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  400. } else
  401. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  402. out:
  403. mutex_unlock(&rtc->ops_lock);
  404. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  405. /*
  406. * Enable emulation if the driver did not provide
  407. * the update_irq_enable function pointer or if returned
  408. * -EINVAL to signal that it has been configured without
  409. * interrupts or that are not available at the moment.
  410. */
  411. if (err == -EINVAL)
  412. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  413. #endif
  414. return err;
  415. }
  416. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  417. /**
  418. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  419. * @rtc: pointer to the rtc device
  420. *
  421. * This function is called when an AIE, UIE or PIE mode interrupt
  422. * has occurred (or been emulated).
  423. *
  424. * Triggers the registered irq_task function callback.
  425. */
  426. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  427. {
  428. unsigned long flags;
  429. /* mark one irq of the appropriate mode */
  430. spin_lock_irqsave(&rtc->irq_lock, flags);
  431. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  432. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  433. /* call the task func */
  434. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  435. if (rtc->irq_task)
  436. rtc->irq_task->func(rtc->irq_task->private_data);
  437. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  438. wake_up_interruptible(&rtc->irq_queue);
  439. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  440. }
  441. /**
  442. * rtc_aie_update_irq - AIE mode rtctimer hook
  443. * @private: pointer to the rtc_device
  444. *
  445. * This functions is called when the aie_timer expires.
  446. */
  447. void rtc_aie_update_irq(void *private)
  448. {
  449. struct rtc_device *rtc = (struct rtc_device *)private;
  450. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  451. }
  452. /**
  453. * rtc_uie_update_irq - UIE mode rtctimer hook
  454. * @private: pointer to the rtc_device
  455. *
  456. * This functions is called when the uie_timer expires.
  457. */
  458. void rtc_uie_update_irq(void *private)
  459. {
  460. struct rtc_device *rtc = (struct rtc_device *)private;
  461. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  462. }
  463. /**
  464. * rtc_pie_update_irq - PIE mode hrtimer hook
  465. * @timer: pointer to the pie mode hrtimer
  466. *
  467. * This function is used to emulate PIE mode interrupts
  468. * using an hrtimer. This function is called when the periodic
  469. * hrtimer expires.
  470. */
  471. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  472. {
  473. struct rtc_device *rtc;
  474. ktime_t period;
  475. int count;
  476. rtc = container_of(timer, struct rtc_device, pie_timer);
  477. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  478. count = hrtimer_forward_now(timer, period);
  479. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  480. return HRTIMER_RESTART;
  481. }
  482. /**
  483. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  484. * @rtc: the rtc device
  485. * @num: how many irqs are being reported (usually one)
  486. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  487. * Context: any
  488. */
  489. void rtc_update_irq(struct rtc_device *rtc,
  490. unsigned long num, unsigned long events)
  491. {
  492. schedule_work(&rtc->irqwork);
  493. }
  494. EXPORT_SYMBOL_GPL(rtc_update_irq);
  495. static int __rtc_match(struct device *dev, void *data)
  496. {
  497. char *name = (char *)data;
  498. if (strcmp(dev_name(dev), name) == 0)
  499. return 1;
  500. return 0;
  501. }
  502. struct rtc_device *rtc_class_open(char *name)
  503. {
  504. struct device *dev;
  505. struct rtc_device *rtc = NULL;
  506. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  507. if (dev)
  508. rtc = to_rtc_device(dev);
  509. if (rtc) {
  510. if (!try_module_get(rtc->owner)) {
  511. put_device(dev);
  512. rtc = NULL;
  513. }
  514. }
  515. return rtc;
  516. }
  517. EXPORT_SYMBOL_GPL(rtc_class_open);
  518. void rtc_class_close(struct rtc_device *rtc)
  519. {
  520. module_put(rtc->owner);
  521. put_device(&rtc->dev);
  522. }
  523. EXPORT_SYMBOL_GPL(rtc_class_close);
  524. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  525. {
  526. int retval = -EBUSY;
  527. if (task == NULL || task->func == NULL)
  528. return -EINVAL;
  529. /* Cannot register while the char dev is in use */
  530. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  531. return -EBUSY;
  532. spin_lock_irq(&rtc->irq_task_lock);
  533. if (rtc->irq_task == NULL) {
  534. rtc->irq_task = task;
  535. retval = 0;
  536. }
  537. spin_unlock_irq(&rtc->irq_task_lock);
  538. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  539. return retval;
  540. }
  541. EXPORT_SYMBOL_GPL(rtc_irq_register);
  542. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  543. {
  544. spin_lock_irq(&rtc->irq_task_lock);
  545. if (rtc->irq_task == task)
  546. rtc->irq_task = NULL;
  547. spin_unlock_irq(&rtc->irq_task_lock);
  548. }
  549. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  550. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  551. {
  552. /*
  553. * We always cancel the timer here first, because otherwise
  554. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  555. * when we manage to start the timer before the callback
  556. * returns HRTIMER_RESTART.
  557. *
  558. * We cannot use hrtimer_cancel() here as a running callback
  559. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  560. * would spin forever.
  561. */
  562. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  563. return -1;
  564. if (enabled) {
  565. ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
  566. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  567. }
  568. return 0;
  569. }
  570. /**
  571. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  572. * @rtc: the rtc device
  573. * @task: currently registered with rtc_irq_register()
  574. * @enabled: true to enable periodic IRQs
  575. * Context: any
  576. *
  577. * Note that rtc_irq_set_freq() should previously have been used to
  578. * specify the desired frequency of periodic IRQ task->func() callbacks.
  579. */
  580. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  581. {
  582. int err = 0;
  583. unsigned long flags;
  584. retry:
  585. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  586. if (rtc->irq_task != NULL && task == NULL)
  587. err = -EBUSY;
  588. if (rtc->irq_task != task)
  589. err = -EACCES;
  590. if (!err) {
  591. if (rtc_update_hrtimer(rtc, enabled) < 0) {
  592. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  593. cpu_relax();
  594. goto retry;
  595. }
  596. rtc->pie_enabled = enabled;
  597. }
  598. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  599. return err;
  600. }
  601. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  602. /**
  603. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  604. * @rtc: the rtc device
  605. * @task: currently registered with rtc_irq_register()
  606. * @freq: positive frequency with which task->func() will be called
  607. * Context: any
  608. *
  609. * Note that rtc_irq_set_state() is used to enable or disable the
  610. * periodic IRQs.
  611. */
  612. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  613. {
  614. int err = 0;
  615. unsigned long flags;
  616. if (freq <= 0 || freq > RTC_MAX_FREQ)
  617. return -EINVAL;
  618. retry:
  619. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  620. if (rtc->irq_task != NULL && task == NULL)
  621. err = -EBUSY;
  622. if (rtc->irq_task != task)
  623. err = -EACCES;
  624. if (!err) {
  625. rtc->irq_freq = freq;
  626. if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
  627. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  628. cpu_relax();
  629. goto retry;
  630. }
  631. }
  632. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  633. return err;
  634. }
  635. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  636. /**
  637. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  638. * @rtc rtc device
  639. * @timer timer being added.
  640. *
  641. * Enqueues a timer onto the rtc devices timerqueue and sets
  642. * the next alarm event appropriately.
  643. *
  644. * Sets the enabled bit on the added timer.
  645. *
  646. * Must hold ops_lock for proper serialization of timerqueue
  647. */
  648. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  649. {
  650. timer->enabled = 1;
  651. timerqueue_add(&rtc->timerqueue, &timer->node);
  652. if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
  653. struct rtc_wkalrm alarm;
  654. int err;
  655. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  656. alarm.enabled = 1;
  657. err = __rtc_set_alarm(rtc, &alarm);
  658. if (err == -ETIME)
  659. schedule_work(&rtc->irqwork);
  660. else if (err) {
  661. timerqueue_del(&rtc->timerqueue, &timer->node);
  662. timer->enabled = 0;
  663. return err;
  664. }
  665. }
  666. return 0;
  667. }
  668. static void rtc_alarm_disable(struct rtc_device *rtc)
  669. {
  670. struct rtc_wkalrm alarm;
  671. struct rtc_time tm;
  672. __rtc_read_time(rtc, &tm);
  673. alarm.time = rtc_ktime_to_tm(ktime_add(rtc_tm_to_ktime(tm),
  674. ktime_set(300, 0)));
  675. alarm.enabled = 0;
  676. ___rtc_set_alarm(rtc, &alarm);
  677. }
  678. /**
  679. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  680. * @rtc rtc device
  681. * @timer timer being removed.
  682. *
  683. * Removes a timer onto the rtc devices timerqueue and sets
  684. * the next alarm event appropriately.
  685. *
  686. * Clears the enabled bit on the removed timer.
  687. *
  688. * Must hold ops_lock for proper serialization of timerqueue
  689. */
  690. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  691. {
  692. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  693. timerqueue_del(&rtc->timerqueue, &timer->node);
  694. timer->enabled = 0;
  695. if (next == &timer->node) {
  696. struct rtc_wkalrm alarm;
  697. int err;
  698. next = timerqueue_getnext(&rtc->timerqueue);
  699. if (!next) {
  700. rtc_alarm_disable(rtc);
  701. return;
  702. }
  703. alarm.time = rtc_ktime_to_tm(next->expires);
  704. alarm.enabled = 1;
  705. err = __rtc_set_alarm(rtc, &alarm);
  706. if (err == -ETIME)
  707. schedule_work(&rtc->irqwork);
  708. }
  709. }
  710. /**
  711. * rtc_timer_do_work - Expires rtc timers
  712. * @rtc rtc device
  713. * @timer timer being removed.
  714. *
  715. * Expires rtc timers. Reprograms next alarm event if needed.
  716. * Called via worktask.
  717. *
  718. * Serializes access to timerqueue via ops_lock mutex
  719. */
  720. void rtc_timer_do_work(struct work_struct *work)
  721. {
  722. struct rtc_timer *timer;
  723. struct timerqueue_node *next;
  724. ktime_t now;
  725. struct rtc_time tm;
  726. struct rtc_device *rtc =
  727. container_of(work, struct rtc_device, irqwork);
  728. mutex_lock(&rtc->ops_lock);
  729. again:
  730. __rtc_read_time(rtc, &tm);
  731. now = rtc_tm_to_ktime(tm);
  732. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  733. if (next->expires.tv64 > now.tv64)
  734. break;
  735. /* expire timer */
  736. timer = container_of(next, struct rtc_timer, node);
  737. timerqueue_del(&rtc->timerqueue, &timer->node);
  738. timer->enabled = 0;
  739. if (timer->task.func)
  740. timer->task.func(timer->task.private_data);
  741. /* Re-add/fwd periodic timers */
  742. if (ktime_to_ns(timer->period)) {
  743. timer->node.expires = ktime_add(timer->node.expires,
  744. timer->period);
  745. timer->enabled = 1;
  746. timerqueue_add(&rtc->timerqueue, &timer->node);
  747. }
  748. }
  749. /* Set next alarm */
  750. if (next) {
  751. struct rtc_wkalrm alarm;
  752. int err;
  753. alarm.time = rtc_ktime_to_tm(next->expires);
  754. alarm.enabled = 1;
  755. err = __rtc_set_alarm(rtc, &alarm);
  756. if (err == -ETIME)
  757. goto again;
  758. } else
  759. rtc_alarm_disable(rtc);
  760. mutex_unlock(&rtc->ops_lock);
  761. }
  762. /* rtc_timer_init - Initializes an rtc_timer
  763. * @timer: timer to be intiialized
  764. * @f: function pointer to be called when timer fires
  765. * @data: private data passed to function pointer
  766. *
  767. * Kernel interface to initializing an rtc_timer.
  768. */
  769. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
  770. {
  771. timerqueue_init(&timer->node);
  772. timer->enabled = 0;
  773. timer->task.func = f;
  774. timer->task.private_data = data;
  775. }
  776. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  777. * @ rtc: rtc device to be used
  778. * @ timer: timer being set
  779. * @ expires: time at which to expire the timer
  780. * @ period: period that the timer will recur
  781. *
  782. * Kernel interface to set an rtc_timer
  783. */
  784. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
  785. ktime_t expires, ktime_t period)
  786. {
  787. int ret = 0;
  788. mutex_lock(&rtc->ops_lock);
  789. if (timer->enabled)
  790. rtc_timer_remove(rtc, timer);
  791. timer->node.expires = expires;
  792. timer->period = period;
  793. ret = rtc_timer_enqueue(rtc, timer);
  794. mutex_unlock(&rtc->ops_lock);
  795. return ret;
  796. }
  797. /* rtc_timer_cancel - Stops an rtc_timer
  798. * @ rtc: rtc device to be used
  799. * @ timer: timer being set
  800. *
  801. * Kernel interface to cancel an rtc_timer
  802. */
  803. int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
  804. {
  805. int ret = 0;
  806. mutex_lock(&rtc->ops_lock);
  807. if (timer->enabled)
  808. rtc_timer_remove(rtc, timer);
  809. mutex_unlock(&rtc->ops_lock);
  810. return ret;
  811. }