interface.c 24 KB

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