interface.c 24 KB

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