interface.c 23 KB

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