interface.c 23 KB

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