interface.c 23 KB

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