interface.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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/log2.h>
  15. int rtc_read_time(struct rtc_device *rtc, struct rtc_time *tm)
  16. {
  17. int err;
  18. err = mutex_lock_interruptible(&rtc->ops_lock);
  19. if (err)
  20. return err;
  21. if (!rtc->ops)
  22. err = -ENODEV;
  23. else if (!rtc->ops->read_time)
  24. err = -EINVAL;
  25. else {
  26. memset(tm, 0, sizeof(struct rtc_time));
  27. err = rtc->ops->read_time(rtc->dev.parent, tm);
  28. }
  29. mutex_unlock(&rtc->ops_lock);
  30. return err;
  31. }
  32. EXPORT_SYMBOL_GPL(rtc_read_time);
  33. int rtc_set_time(struct rtc_device *rtc, struct rtc_time *tm)
  34. {
  35. int err;
  36. err = rtc_valid_tm(tm);
  37. if (err != 0)
  38. return err;
  39. err = mutex_lock_interruptible(&rtc->ops_lock);
  40. if (err)
  41. return err;
  42. if (!rtc->ops)
  43. err = -ENODEV;
  44. else if (rtc->ops->set_time)
  45. err = rtc->ops->set_time(rtc->dev.parent, tm);
  46. else if (rtc->ops->set_mmss) {
  47. unsigned long secs;
  48. err = rtc_tm_to_time(tm, &secs);
  49. if (err == 0)
  50. err = rtc->ops->set_mmss(rtc->dev.parent, secs);
  51. } else
  52. err = -EINVAL;
  53. mutex_unlock(&rtc->ops_lock);
  54. return err;
  55. }
  56. EXPORT_SYMBOL_GPL(rtc_set_time);
  57. int rtc_set_mmss(struct rtc_device *rtc, unsigned long secs)
  58. {
  59. int err;
  60. err = mutex_lock_interruptible(&rtc->ops_lock);
  61. if (err)
  62. return err;
  63. if (!rtc->ops)
  64. err = -ENODEV;
  65. else if (rtc->ops->set_mmss)
  66. err = rtc->ops->set_mmss(rtc->dev.parent, secs);
  67. else if (rtc->ops->read_time && rtc->ops->set_time) {
  68. struct rtc_time new, old;
  69. err = rtc->ops->read_time(rtc->dev.parent, &old);
  70. if (err == 0) {
  71. rtc_time_to_tm(secs, &new);
  72. /*
  73. * avoid writing when we're going to change the day of
  74. * the month. We will retry in the next minute. This
  75. * basically means that if the RTC must not drift
  76. * by more than 1 minute in 11 minutes.
  77. */
  78. if (!((old.tm_hour == 23 && old.tm_min == 59) ||
  79. (new.tm_hour == 23 && new.tm_min == 59)))
  80. err = rtc->ops->set_time(rtc->dev.parent,
  81. &new);
  82. }
  83. }
  84. else
  85. err = -EINVAL;
  86. mutex_unlock(&rtc->ops_lock);
  87. return err;
  88. }
  89. EXPORT_SYMBOL_GPL(rtc_set_mmss);
  90. static int rtc_read_alarm_internal(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  91. {
  92. int err;
  93. err = mutex_lock_interruptible(&rtc->ops_lock);
  94. if (err)
  95. return err;
  96. if (rtc->ops == NULL)
  97. err = -ENODEV;
  98. else if (!rtc->ops->read_alarm)
  99. err = -EINVAL;
  100. else {
  101. memset(alarm, 0, sizeof(struct rtc_wkalrm));
  102. err = rtc->ops->read_alarm(rtc->dev.parent, alarm);
  103. }
  104. mutex_unlock(&rtc->ops_lock);
  105. return err;
  106. }
  107. int rtc_read_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  108. {
  109. int err;
  110. struct rtc_time before, now;
  111. int first_time = 1;
  112. unsigned long t_now, t_alm;
  113. enum { none, day, month, year } missing = none;
  114. unsigned days;
  115. /* The lower level RTC driver may return -1 in some fields,
  116. * creating invalid alarm->time values, for reasons like:
  117. *
  118. * - The hardware may not be capable of filling them in;
  119. * many alarms match only on time-of-day fields, not
  120. * day/month/year calendar data.
  121. *
  122. * - Some hardware uses illegal values as "wildcard" match
  123. * values, which non-Linux firmware (like a BIOS) may try
  124. * to set up as e.g. "alarm 15 minutes after each hour".
  125. * Linux uses only oneshot alarms.
  126. *
  127. * When we see that here, we deal with it by using values from
  128. * a current RTC timestamp for any missing (-1) values. The
  129. * RTC driver prevents "periodic alarm" modes.
  130. *
  131. * But this can be racey, because some fields of the RTC timestamp
  132. * may have wrapped in the interval since we read the RTC alarm,
  133. * which would lead to us inserting inconsistent values in place
  134. * of the -1 fields.
  135. *
  136. * Reading the alarm and timestamp in the reverse sequence
  137. * would have the same race condition, and not solve the issue.
  138. *
  139. * So, we must first read the RTC timestamp,
  140. * then read the RTC alarm value,
  141. * and then read a second RTC timestamp.
  142. *
  143. * If any fields of the second timestamp have changed
  144. * when compared with the first timestamp, then we know
  145. * our timestamp may be inconsistent with that used by
  146. * the low-level rtc_read_alarm_internal() function.
  147. *
  148. * So, when the two timestamps disagree, we just loop and do
  149. * the process again to get a fully consistent set of values.
  150. *
  151. * This could all instead be done in the lower level driver,
  152. * but since more than one lower level RTC implementation needs it,
  153. * then it's probably best best to do it here instead of there..
  154. */
  155. /* Get the "before" timestamp */
  156. err = rtc_read_time(rtc, &before);
  157. if (err < 0)
  158. return err;
  159. do {
  160. if (!first_time)
  161. memcpy(&before, &now, sizeof(struct rtc_time));
  162. first_time = 0;
  163. /* get the RTC alarm values, which may be incomplete */
  164. err = rtc_read_alarm_internal(rtc, alarm);
  165. if (err)
  166. return err;
  167. if (!alarm->enabled)
  168. return 0;
  169. /* full-function RTCs won't have such missing fields */
  170. if (rtc_valid_tm(&alarm->time) == 0)
  171. return 0;
  172. /* get the "after" timestamp, to detect wrapped fields */
  173. err = rtc_read_time(rtc, &now);
  174. if (err < 0)
  175. return err;
  176. /* note that tm_sec is a "don't care" value here: */
  177. } while ( before.tm_min != now.tm_min
  178. || before.tm_hour != now.tm_hour
  179. || before.tm_mon != now.tm_mon
  180. || before.tm_year != now.tm_year);
  181. /* Fill in the missing alarm fields using the timestamp; we
  182. * know there's at least one since alarm->time is invalid.
  183. */
  184. if (alarm->time.tm_sec == -1)
  185. alarm->time.tm_sec = now.tm_sec;
  186. if (alarm->time.tm_min == -1)
  187. alarm->time.tm_min = now.tm_min;
  188. if (alarm->time.tm_hour == -1)
  189. alarm->time.tm_hour = now.tm_hour;
  190. /* For simplicity, only support date rollover for now */
  191. if (alarm->time.tm_mday == -1) {
  192. alarm->time.tm_mday = now.tm_mday;
  193. missing = day;
  194. }
  195. if (alarm->time.tm_mon == -1) {
  196. alarm->time.tm_mon = now.tm_mon;
  197. if (missing == none)
  198. missing = month;
  199. }
  200. if (alarm->time.tm_year == -1) {
  201. alarm->time.tm_year = now.tm_year;
  202. if (missing == none)
  203. missing = year;
  204. }
  205. /* with luck, no rollover is needed */
  206. rtc_tm_to_time(&now, &t_now);
  207. rtc_tm_to_time(&alarm->time, &t_alm);
  208. if (t_now < t_alm)
  209. goto done;
  210. switch (missing) {
  211. /* 24 hour rollover ... if it's now 10am Monday, an alarm that
  212. * that will trigger at 5am will do so at 5am Tuesday, which
  213. * could also be in the next month or year. This is a common
  214. * case, especially for PCs.
  215. */
  216. case day:
  217. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "day");
  218. t_alm += 24 * 60 * 60;
  219. rtc_time_to_tm(t_alm, &alarm->time);
  220. break;
  221. /* Month rollover ... if it's the 31th, an alarm on the 3rd will
  222. * be next month. An alarm matching on the 30th, 29th, or 28th
  223. * may end up in the month after that! Many newer PCs support
  224. * this type of alarm.
  225. */
  226. case month:
  227. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "month");
  228. do {
  229. if (alarm->time.tm_mon < 11)
  230. alarm->time.tm_mon++;
  231. else {
  232. alarm->time.tm_mon = 0;
  233. alarm->time.tm_year++;
  234. }
  235. days = rtc_month_days(alarm->time.tm_mon,
  236. alarm->time.tm_year);
  237. } while (days < alarm->time.tm_mday);
  238. break;
  239. /* Year rollover ... easy except for leap years! */
  240. case year:
  241. dev_dbg(&rtc->dev, "alarm rollover: %s\n", "year");
  242. do {
  243. alarm->time.tm_year++;
  244. } while (rtc_valid_tm(&alarm->time) != 0);
  245. break;
  246. default:
  247. dev_warn(&rtc->dev, "alarm rollover not handled\n");
  248. }
  249. done:
  250. return 0;
  251. }
  252. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  253. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  254. {
  255. int err;
  256. err = rtc_valid_tm(&alarm->time);
  257. if (err != 0)
  258. return err;
  259. err = mutex_lock_interruptible(&rtc->ops_lock);
  260. if (err)
  261. return err;
  262. if (!rtc->ops)
  263. err = -ENODEV;
  264. else if (!rtc->ops->set_alarm)
  265. err = -EINVAL;
  266. else
  267. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  268. mutex_unlock(&rtc->ops_lock);
  269. return err;
  270. }
  271. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  272. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  273. {
  274. int err = mutex_lock_interruptible(&rtc->ops_lock);
  275. if (err)
  276. return err;
  277. if (!rtc->ops)
  278. err = -ENODEV;
  279. else if (!rtc->ops->alarm_irq_enable)
  280. err = -EINVAL;
  281. else
  282. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  283. mutex_unlock(&rtc->ops_lock);
  284. return err;
  285. }
  286. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  287. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  288. {
  289. int err = mutex_lock_interruptible(&rtc->ops_lock);
  290. if (err)
  291. return err;
  292. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  293. if (enabled == 0 && rtc->uie_irq_active) {
  294. mutex_unlock(&rtc->ops_lock);
  295. return rtc_dev_update_irq_enable_emul(rtc, enabled);
  296. }
  297. #endif
  298. if (!rtc->ops)
  299. err = -ENODEV;
  300. else if (!rtc->ops->update_irq_enable)
  301. err = -EINVAL;
  302. else
  303. err = rtc->ops->update_irq_enable(rtc->dev.parent, enabled);
  304. mutex_unlock(&rtc->ops_lock);
  305. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  306. /*
  307. * Enable emulation if the driver did not provide
  308. * the update_irq_enable function pointer or if returned
  309. * -EINVAL to signal that it has been configured without
  310. * interrupts or that are not available at the moment.
  311. */
  312. if (err == -EINVAL)
  313. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  314. #endif
  315. return err;
  316. }
  317. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  318. /**
  319. * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
  320. * @rtc: the rtc device
  321. * @num: how many irqs are being reported (usually one)
  322. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  323. * Context: any
  324. */
  325. void rtc_update_irq(struct rtc_device *rtc,
  326. unsigned long num, unsigned long events)
  327. {
  328. unsigned long flags;
  329. spin_lock_irqsave(&rtc->irq_lock, flags);
  330. rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
  331. spin_unlock_irqrestore(&rtc->irq_lock, flags);
  332. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  333. if (rtc->irq_task)
  334. rtc->irq_task->func(rtc->irq_task->private_data);
  335. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  336. wake_up_interruptible(&rtc->irq_queue);
  337. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  338. }
  339. EXPORT_SYMBOL_GPL(rtc_update_irq);
  340. static int __rtc_match(struct device *dev, void *data)
  341. {
  342. char *name = (char *)data;
  343. if (strcmp(dev_name(dev), name) == 0)
  344. return 1;
  345. return 0;
  346. }
  347. struct rtc_device *rtc_class_open(char *name)
  348. {
  349. struct device *dev;
  350. struct rtc_device *rtc = NULL;
  351. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  352. if (dev)
  353. rtc = to_rtc_device(dev);
  354. if (rtc) {
  355. if (!try_module_get(rtc->owner)) {
  356. put_device(dev);
  357. rtc = NULL;
  358. }
  359. }
  360. return rtc;
  361. }
  362. EXPORT_SYMBOL_GPL(rtc_class_open);
  363. void rtc_class_close(struct rtc_device *rtc)
  364. {
  365. module_put(rtc->owner);
  366. put_device(&rtc->dev);
  367. }
  368. EXPORT_SYMBOL_GPL(rtc_class_close);
  369. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  370. {
  371. int retval = -EBUSY;
  372. if (task == NULL || task->func == NULL)
  373. return -EINVAL;
  374. /* Cannot register while the char dev is in use */
  375. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  376. return -EBUSY;
  377. spin_lock_irq(&rtc->irq_task_lock);
  378. if (rtc->irq_task == NULL) {
  379. rtc->irq_task = task;
  380. retval = 0;
  381. }
  382. spin_unlock_irq(&rtc->irq_task_lock);
  383. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  384. return retval;
  385. }
  386. EXPORT_SYMBOL_GPL(rtc_irq_register);
  387. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  388. {
  389. spin_lock_irq(&rtc->irq_task_lock);
  390. if (rtc->irq_task == task)
  391. rtc->irq_task = NULL;
  392. spin_unlock_irq(&rtc->irq_task_lock);
  393. }
  394. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  395. /**
  396. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  397. * @rtc: the rtc device
  398. * @task: currently registered with rtc_irq_register()
  399. * @enabled: true to enable periodic IRQs
  400. * Context: any
  401. *
  402. * Note that rtc_irq_set_freq() should previously have been used to
  403. * specify the desired frequency of periodic IRQ task->func() callbacks.
  404. */
  405. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  406. {
  407. int err = 0;
  408. unsigned long flags;
  409. if (rtc->ops->irq_set_state == NULL)
  410. return -ENXIO;
  411. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  412. if (rtc->irq_task != NULL && task == NULL)
  413. err = -EBUSY;
  414. if (rtc->irq_task != task)
  415. err = -EACCES;
  416. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  417. if (err == 0)
  418. err = rtc->ops->irq_set_state(rtc->dev.parent, enabled);
  419. return err;
  420. }
  421. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  422. /**
  423. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  424. * @rtc: the rtc device
  425. * @task: currently registered with rtc_irq_register()
  426. * @freq: positive frequency with which task->func() will be called
  427. * Context: any
  428. *
  429. * Note that rtc_irq_set_state() is used to enable or disable the
  430. * periodic IRQs.
  431. */
  432. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  433. {
  434. int err = 0;
  435. unsigned long flags;
  436. if (rtc->ops->irq_set_freq == NULL)
  437. return -ENXIO;
  438. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  439. if (rtc->irq_task != NULL && task == NULL)
  440. err = -EBUSY;
  441. if (rtc->irq_task != task)
  442. err = -EACCES;
  443. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  444. if (err == 0) {
  445. err = rtc->ops->irq_set_freq(rtc->dev.parent, freq);
  446. if (err == 0)
  447. rtc->irq_freq = freq;
  448. }
  449. return err;
  450. }
  451. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);