interface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. struct rtc_time tm;
  283. long now, scheduled;
  284. int err;
  285. err = rtc_valid_tm(&alarm->time);
  286. if (err)
  287. return err;
  288. rtc_tm_to_time(&alarm->time, &scheduled);
  289. /* Make sure we're not setting alarms in the past */
  290. err = __rtc_read_time(rtc, &tm);
  291. rtc_tm_to_time(&tm, &now);
  292. if (scheduled <= now)
  293. return -ETIME;
  294. /*
  295. * XXX - We just checked to make sure the alarm time is not
  296. * in the past, but there is still a race window where if
  297. * the is alarm set for the next second and the second ticks
  298. * over right here, before we set the alarm.
  299. */
  300. if (!rtc->ops)
  301. err = -ENODEV;
  302. else if (!rtc->ops->set_alarm)
  303. err = -EINVAL;
  304. else
  305. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  306. return err;
  307. }
  308. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  309. {
  310. int err;
  311. err = rtc_valid_tm(&alarm->time);
  312. if (err != 0)
  313. return err;
  314. err = mutex_lock_interruptible(&rtc->ops_lock);
  315. if (err)
  316. return err;
  317. if (rtc->aie_timer.enabled) {
  318. rtc_timer_remove(rtc, &rtc->aie_timer);
  319. }
  320. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  321. rtc->aie_timer.period = ktime_set(0, 0);
  322. if (alarm->enabled) {
  323. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  324. }
  325. mutex_unlock(&rtc->ops_lock);
  326. return err;
  327. }
  328. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  329. /* Called once per device from rtc_device_register */
  330. int rtc_initialize_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  331. {
  332. int err;
  333. err = rtc_valid_tm(&alarm->time);
  334. if (err != 0)
  335. return err;
  336. err = mutex_lock_interruptible(&rtc->ops_lock);
  337. if (err)
  338. return err;
  339. rtc->aie_timer.node.expires = rtc_tm_to_ktime(alarm->time);
  340. rtc->aie_timer.period = ktime_set(0, 0);
  341. if (alarm->enabled) {
  342. rtc->aie_timer.enabled = 1;
  343. timerqueue_add(&rtc->timerqueue, &rtc->aie_timer.node);
  344. }
  345. mutex_unlock(&rtc->ops_lock);
  346. return err;
  347. }
  348. EXPORT_SYMBOL_GPL(rtc_initialize_alarm);
  349. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  350. {
  351. int err = mutex_lock_interruptible(&rtc->ops_lock);
  352. if (err)
  353. return err;
  354. if (rtc->aie_timer.enabled != enabled) {
  355. if (enabled)
  356. err = rtc_timer_enqueue(rtc, &rtc->aie_timer);
  357. else
  358. rtc_timer_remove(rtc, &rtc->aie_timer);
  359. }
  360. if (err)
  361. /* nothing */;
  362. else if (!rtc->ops)
  363. err = -ENODEV;
  364. else if (!rtc->ops->alarm_irq_enable)
  365. err = -EINVAL;
  366. else
  367. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  368. mutex_unlock(&rtc->ops_lock);
  369. return err;
  370. }
  371. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  372. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  373. {
  374. int err = mutex_lock_interruptible(&rtc->ops_lock);
  375. if (err)
  376. return err;
  377. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  378. if (enabled == 0 && rtc->uie_irq_active) {
  379. mutex_unlock(&rtc->ops_lock);
  380. return rtc_dev_update_irq_enable_emul(rtc, 0);
  381. }
  382. #endif
  383. /* make sure we're changing state */
  384. if (rtc->uie_rtctimer.enabled == enabled)
  385. goto out;
  386. if (enabled) {
  387. struct rtc_time tm;
  388. ktime_t now, onesec;
  389. __rtc_read_time(rtc, &tm);
  390. onesec = ktime_set(1, 0);
  391. now = rtc_tm_to_ktime(tm);
  392. rtc->uie_rtctimer.node.expires = ktime_add(now, onesec);
  393. rtc->uie_rtctimer.period = ktime_set(1, 0);
  394. err = rtc_timer_enqueue(rtc, &rtc->uie_rtctimer);
  395. } else
  396. rtc_timer_remove(rtc, &rtc->uie_rtctimer);
  397. out:
  398. mutex_unlock(&rtc->ops_lock);
  399. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  400. /*
  401. * Enable emulation if the driver did not provide
  402. * the update_irq_enable function pointer or if returned
  403. * -EINVAL to signal that it has been configured without
  404. * interrupts or that are not available at the moment.
  405. */
  406. if (err == -EINVAL)
  407. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  408. #endif
  409. return err;
  410. }
  411. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  412. /**
  413. * rtc_handle_legacy_irq - AIE, UIE and PIE event hook
  414. * @rtc: pointer to the rtc device
  415. *
  416. * This function is called when an AIE, UIE or PIE mode interrupt
  417. * has occurred (or been emulated).
  418. *
  419. * Triggers the registered irq_task function callback.
  420. */
  421. void rtc_handle_legacy_irq(struct rtc_device *rtc, int num, int mode)
  422. {
  423. unsigned long flags;
  424. /* mark one irq of the appropriate mode */
  425. spin_lock_irqsave(&rtc->irq_lock, flags);
  426. rtc->irq_data = (rtc->irq_data + (num << 8)) | (RTC_IRQF|mode);
  427. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  428. /* call the task func */
  429. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  430. if (rtc->irq_task)
  431. rtc->irq_task->func(rtc->irq_task->private_data);
  432. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  433. wake_up_interruptible(&rtc->irq_queue);
  434. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  435. }
  436. /**
  437. * rtc_aie_update_irq - AIE mode rtctimer hook
  438. * @private: pointer to the rtc_device
  439. *
  440. * This functions is called when the aie_timer expires.
  441. */
  442. void rtc_aie_update_irq(void *private)
  443. {
  444. struct rtc_device *rtc = (struct rtc_device *)private;
  445. rtc_handle_legacy_irq(rtc, 1, RTC_AF);
  446. }
  447. /**
  448. * rtc_uie_update_irq - UIE mode rtctimer hook
  449. * @private: pointer to the rtc_device
  450. *
  451. * This functions is called when the uie_timer expires.
  452. */
  453. void rtc_uie_update_irq(void *private)
  454. {
  455. struct rtc_device *rtc = (struct rtc_device *)private;
  456. rtc_handle_legacy_irq(rtc, 1, RTC_UF);
  457. }
  458. /**
  459. * rtc_pie_update_irq - PIE mode hrtimer hook
  460. * @timer: pointer to the pie mode hrtimer
  461. *
  462. * This function is used to emulate PIE mode interrupts
  463. * using an hrtimer. This function is called when the periodic
  464. * hrtimer expires.
  465. */
  466. enum hrtimer_restart rtc_pie_update_irq(struct hrtimer *timer)
  467. {
  468. struct rtc_device *rtc;
  469. ktime_t period;
  470. int count;
  471. rtc = container_of(timer, struct rtc_device, pie_timer);
  472. period = ktime_set(0, NSEC_PER_SEC/rtc->irq_freq);
  473. count = hrtimer_forward_now(timer, period);
  474. rtc_handle_legacy_irq(rtc, count, RTC_PF);
  475. return HRTIMER_RESTART;
  476. }
  477. /**
  478. * rtc_update_irq - Triggered when a RTC interrupt occurs.
  479. * @rtc: the rtc device
  480. * @num: how many irqs are being reported (usually one)
  481. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  482. * Context: any
  483. */
  484. void rtc_update_irq(struct rtc_device *rtc,
  485. unsigned long num, unsigned long events)
  486. {
  487. schedule_work(&rtc->irqwork);
  488. }
  489. EXPORT_SYMBOL_GPL(rtc_update_irq);
  490. static int __rtc_match(struct device *dev, void *data)
  491. {
  492. char *name = (char *)data;
  493. if (strcmp(dev_name(dev), name) == 0)
  494. return 1;
  495. return 0;
  496. }
  497. struct rtc_device *rtc_class_open(char *name)
  498. {
  499. struct device *dev;
  500. struct rtc_device *rtc = NULL;
  501. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  502. if (dev)
  503. rtc = to_rtc_device(dev);
  504. if (rtc) {
  505. if (!try_module_get(rtc->owner)) {
  506. put_device(dev);
  507. rtc = NULL;
  508. }
  509. }
  510. return rtc;
  511. }
  512. EXPORT_SYMBOL_GPL(rtc_class_open);
  513. void rtc_class_close(struct rtc_device *rtc)
  514. {
  515. module_put(rtc->owner);
  516. put_device(&rtc->dev);
  517. }
  518. EXPORT_SYMBOL_GPL(rtc_class_close);
  519. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  520. {
  521. int retval = -EBUSY;
  522. if (task == NULL || task->func == NULL)
  523. return -EINVAL;
  524. /* Cannot register while the char dev is in use */
  525. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  526. return -EBUSY;
  527. spin_lock_irq(&rtc->irq_task_lock);
  528. if (rtc->irq_task == NULL) {
  529. rtc->irq_task = task;
  530. retval = 0;
  531. }
  532. spin_unlock_irq(&rtc->irq_task_lock);
  533. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  534. return retval;
  535. }
  536. EXPORT_SYMBOL_GPL(rtc_irq_register);
  537. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  538. {
  539. spin_lock_irq(&rtc->irq_task_lock);
  540. if (rtc->irq_task == task)
  541. rtc->irq_task = NULL;
  542. spin_unlock_irq(&rtc->irq_task_lock);
  543. }
  544. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  545. static int rtc_update_hrtimer(struct rtc_device *rtc, int enabled)
  546. {
  547. /*
  548. * We always cancel the timer here first, because otherwise
  549. * we could run into BUG_ON(timer->state != HRTIMER_STATE_CALLBACK);
  550. * when we manage to start the timer before the callback
  551. * returns HRTIMER_RESTART.
  552. *
  553. * We cannot use hrtimer_cancel() here as a running callback
  554. * could be blocked on rtc->irq_task_lock and hrtimer_cancel()
  555. * would spin forever.
  556. */
  557. if (hrtimer_try_to_cancel(&rtc->pie_timer) < 0)
  558. return -1;
  559. if (enabled) {
  560. ktime_t period = ktime_set(0, NSEC_PER_SEC / rtc->irq_freq);
  561. hrtimer_start(&rtc->pie_timer, period, HRTIMER_MODE_REL);
  562. }
  563. return 0;
  564. }
  565. /**
  566. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  567. * @rtc: the rtc device
  568. * @task: currently registered with rtc_irq_register()
  569. * @enabled: true to enable periodic IRQs
  570. * Context: any
  571. *
  572. * Note that rtc_irq_set_freq() should previously have been used to
  573. * specify the desired frequency of periodic IRQ task->func() callbacks.
  574. */
  575. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  576. {
  577. int err = 0;
  578. unsigned long flags;
  579. retry:
  580. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  581. if (rtc->irq_task != NULL && task == NULL)
  582. err = -EBUSY;
  583. if (rtc->irq_task != task)
  584. err = -EACCES;
  585. if (!err) {
  586. if (rtc_update_hrtimer(rtc, enabled) < 0) {
  587. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  588. cpu_relax();
  589. goto retry;
  590. }
  591. rtc->pie_enabled = enabled;
  592. }
  593. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  594. return err;
  595. }
  596. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  597. /**
  598. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  599. * @rtc: the rtc device
  600. * @task: currently registered with rtc_irq_register()
  601. * @freq: positive frequency with which task->func() will be called
  602. * Context: any
  603. *
  604. * Note that rtc_irq_set_state() is used to enable or disable the
  605. * periodic IRQs.
  606. */
  607. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  608. {
  609. int err = 0;
  610. unsigned long flags;
  611. if (freq <= 0 || freq > RTC_MAX_FREQ)
  612. return -EINVAL;
  613. retry:
  614. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  615. if (rtc->irq_task != NULL && task == NULL)
  616. err = -EBUSY;
  617. if (rtc->irq_task != task)
  618. err = -EACCES;
  619. if (!err) {
  620. rtc->irq_freq = freq;
  621. if (rtc->pie_enabled && rtc_update_hrtimer(rtc, 1) < 0) {
  622. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  623. cpu_relax();
  624. goto retry;
  625. }
  626. }
  627. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  628. return err;
  629. }
  630. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);
  631. /**
  632. * rtc_timer_enqueue - Adds a rtc_timer to the rtc_device timerqueue
  633. * @rtc rtc device
  634. * @timer timer being added.
  635. *
  636. * Enqueues a timer onto the rtc devices timerqueue and sets
  637. * the next alarm event appropriately.
  638. *
  639. * Sets the enabled bit on the added timer.
  640. *
  641. * Must hold ops_lock for proper serialization of timerqueue
  642. */
  643. static int rtc_timer_enqueue(struct rtc_device *rtc, struct rtc_timer *timer)
  644. {
  645. timer->enabled = 1;
  646. timerqueue_add(&rtc->timerqueue, &timer->node);
  647. if (&timer->node == timerqueue_getnext(&rtc->timerqueue)) {
  648. struct rtc_wkalrm alarm;
  649. int err;
  650. alarm.time = rtc_ktime_to_tm(timer->node.expires);
  651. alarm.enabled = 1;
  652. err = __rtc_set_alarm(rtc, &alarm);
  653. if (err == -ETIME)
  654. schedule_work(&rtc->irqwork);
  655. else if (err) {
  656. timerqueue_del(&rtc->timerqueue, &timer->node);
  657. timer->enabled = 0;
  658. return err;
  659. }
  660. }
  661. return 0;
  662. }
  663. /**
  664. * rtc_timer_remove - Removes a rtc_timer from the rtc_device timerqueue
  665. * @rtc rtc device
  666. * @timer timer being removed.
  667. *
  668. * Removes a timer onto the rtc devices timerqueue and sets
  669. * the next alarm event appropriately.
  670. *
  671. * Clears the enabled bit on the removed timer.
  672. *
  673. * Must hold ops_lock for proper serialization of timerqueue
  674. */
  675. static void rtc_timer_remove(struct rtc_device *rtc, struct rtc_timer *timer)
  676. {
  677. struct timerqueue_node *next = timerqueue_getnext(&rtc->timerqueue);
  678. timerqueue_del(&rtc->timerqueue, &timer->node);
  679. timer->enabled = 0;
  680. if (next == &timer->node) {
  681. struct rtc_wkalrm alarm;
  682. int err;
  683. next = timerqueue_getnext(&rtc->timerqueue);
  684. if (!next)
  685. return;
  686. alarm.time = rtc_ktime_to_tm(next->expires);
  687. alarm.enabled = 1;
  688. err = __rtc_set_alarm(rtc, &alarm);
  689. if (err == -ETIME)
  690. schedule_work(&rtc->irqwork);
  691. }
  692. }
  693. /**
  694. * rtc_timer_do_work - Expires rtc timers
  695. * @rtc rtc device
  696. * @timer timer being removed.
  697. *
  698. * Expires rtc timers. Reprograms next alarm event if needed.
  699. * Called via worktask.
  700. *
  701. * Serializes access to timerqueue via ops_lock mutex
  702. */
  703. void rtc_timer_do_work(struct work_struct *work)
  704. {
  705. struct rtc_timer *timer;
  706. struct timerqueue_node *next;
  707. ktime_t now;
  708. struct rtc_time tm;
  709. struct rtc_device *rtc =
  710. container_of(work, struct rtc_device, irqwork);
  711. mutex_lock(&rtc->ops_lock);
  712. again:
  713. __rtc_read_time(rtc, &tm);
  714. now = rtc_tm_to_ktime(tm);
  715. while ((next = timerqueue_getnext(&rtc->timerqueue))) {
  716. if (next->expires.tv64 > now.tv64)
  717. break;
  718. /* expire timer */
  719. timer = container_of(next, struct rtc_timer, node);
  720. timerqueue_del(&rtc->timerqueue, &timer->node);
  721. timer->enabled = 0;
  722. if (timer->task.func)
  723. timer->task.func(timer->task.private_data);
  724. /* Re-add/fwd periodic timers */
  725. if (ktime_to_ns(timer->period)) {
  726. timer->node.expires = ktime_add(timer->node.expires,
  727. timer->period);
  728. timer->enabled = 1;
  729. timerqueue_add(&rtc->timerqueue, &timer->node);
  730. }
  731. }
  732. /* Set next alarm */
  733. if (next) {
  734. struct rtc_wkalrm alarm;
  735. int err;
  736. alarm.time = rtc_ktime_to_tm(next->expires);
  737. alarm.enabled = 1;
  738. err = __rtc_set_alarm(rtc, &alarm);
  739. if (err == -ETIME)
  740. goto again;
  741. }
  742. mutex_unlock(&rtc->ops_lock);
  743. }
  744. /* rtc_timer_init - Initializes an rtc_timer
  745. * @timer: timer to be intiialized
  746. * @f: function pointer to be called when timer fires
  747. * @data: private data passed to function pointer
  748. *
  749. * Kernel interface to initializing an rtc_timer.
  750. */
  751. void rtc_timer_init(struct rtc_timer *timer, void (*f)(void* p), void* data)
  752. {
  753. timerqueue_init(&timer->node);
  754. timer->enabled = 0;
  755. timer->task.func = f;
  756. timer->task.private_data = data;
  757. }
  758. /* rtc_timer_start - Sets an rtc_timer to fire in the future
  759. * @ rtc: rtc device to be used
  760. * @ timer: timer being set
  761. * @ expires: time at which to expire the timer
  762. * @ period: period that the timer will recur
  763. *
  764. * Kernel interface to set an rtc_timer
  765. */
  766. int rtc_timer_start(struct rtc_device *rtc, struct rtc_timer* timer,
  767. ktime_t expires, ktime_t period)
  768. {
  769. int ret = 0;
  770. mutex_lock(&rtc->ops_lock);
  771. if (timer->enabled)
  772. rtc_timer_remove(rtc, timer);
  773. timer->node.expires = expires;
  774. timer->period = period;
  775. ret = rtc_timer_enqueue(rtc, timer);
  776. mutex_unlock(&rtc->ops_lock);
  777. return ret;
  778. }
  779. /* rtc_timer_cancel - Stops an rtc_timer
  780. * @ rtc: rtc device to be used
  781. * @ timer: timer being set
  782. *
  783. * Kernel interface to cancel an rtc_timer
  784. */
  785. int rtc_timer_cancel(struct rtc_device *rtc, struct rtc_timer* timer)
  786. {
  787. int ret = 0;
  788. mutex_lock(&rtc->ops_lock);
  789. if (timer->enabled)
  790. rtc_timer_remove(rtc, timer);
  791. mutex_unlock(&rtc->ops_lock);
  792. return ret;
  793. }