interface.c 12 KB

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