interface.c 23 KB

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