interface.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 -EBUSY;
  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 -EBUSY;
  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 -EBUSY;
  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 -EBUSY;
  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. /* The lower level RTC driver may not be capable of filling
  108. * in all fields of the rtc_time struct (eg. rtc-cmos),
  109. * and so might instead return -1 in some fields.
  110. * We deal with that here by grabbing a current RTC timestamp
  111. * and using values from that for any missing (-1) values.
  112. *
  113. * But this can be racey, because some fields of the RTC timestamp
  114. * may have wrapped in the interval since we read the RTC alarm,
  115. * which would lead to us inserting inconsistent values in place
  116. * of the -1 fields.
  117. *
  118. * Reading the alarm and timestamp in the reverse sequence
  119. * would have the same race condition, and not solve the issue.
  120. *
  121. * So, we must first read the RTC timestamp,
  122. * then read the RTC alarm value,
  123. * and then read a second RTC timestamp.
  124. *
  125. * If any fields of the second timestamp have changed
  126. * when compared with the first timestamp, then we know
  127. * our timestamp may be inconsistent with that used by
  128. * the low-level rtc_read_alarm_internal() function.
  129. *
  130. * So, when the two timestamps disagree, we just loop and do
  131. * the process again to get a fully consistent set of values.
  132. *
  133. * This could all instead be done in the lower level driver,
  134. * but since more than one lower level RTC implementation needs it,
  135. * then it's probably best best to do it here instead of there..
  136. */
  137. /* Get the "before" timestamp */
  138. err = rtc_read_time(rtc, &before);
  139. if (err < 0)
  140. return err;
  141. do {
  142. if (!first_time)
  143. memcpy(&before, &now, sizeof(struct rtc_time));
  144. first_time = 0;
  145. /* get the RTC alarm values, which may be incomplete */
  146. err = rtc_read_alarm_internal(rtc, alarm);
  147. if (err)
  148. return err;
  149. if (!alarm->enabled)
  150. return 0;
  151. /* get the "after" timestamp, to detect wrapped fields */
  152. err = rtc_read_time(rtc, &now);
  153. if (err < 0)
  154. return err;
  155. /* note that tm_sec is a "don't care" value here: */
  156. } while ( before.tm_min != now.tm_min
  157. || before.tm_hour != now.tm_hour
  158. || before.tm_mon != now.tm_mon
  159. || before.tm_year != now.tm_year
  160. || before.tm_isdst != now.tm_isdst);
  161. /* Fill in any missing alarm fields using the timestamp */
  162. if (alarm->time.tm_sec == -1)
  163. alarm->time.tm_sec = now.tm_sec;
  164. if (alarm->time.tm_min == -1)
  165. alarm->time.tm_min = now.tm_min;
  166. if (alarm->time.tm_hour == -1)
  167. alarm->time.tm_hour = now.tm_hour;
  168. if (alarm->time.tm_mday == -1)
  169. alarm->time.tm_mday = now.tm_mday;
  170. if (alarm->time.tm_mon == -1)
  171. alarm->time.tm_mon = now.tm_mon;
  172. if (alarm->time.tm_year == -1)
  173. alarm->time.tm_year = now.tm_year;
  174. return 0;
  175. }
  176. EXPORT_SYMBOL_GPL(rtc_read_alarm);
  177. int rtc_set_alarm(struct rtc_device *rtc, struct rtc_wkalrm *alarm)
  178. {
  179. int err;
  180. err = rtc_valid_tm(&alarm->time);
  181. if (err != 0)
  182. return err;
  183. err = mutex_lock_interruptible(&rtc->ops_lock);
  184. if (err)
  185. return -EBUSY;
  186. if (!rtc->ops)
  187. err = -ENODEV;
  188. else if (!rtc->ops->set_alarm)
  189. err = -EINVAL;
  190. else
  191. err = rtc->ops->set_alarm(rtc->dev.parent, alarm);
  192. mutex_unlock(&rtc->ops_lock);
  193. return err;
  194. }
  195. EXPORT_SYMBOL_GPL(rtc_set_alarm);
  196. /**
  197. * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
  198. * @rtc: the rtc device
  199. * @num: how many irqs are being reported (usually one)
  200. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  201. * Context: in_interrupt(), irqs blocked
  202. */
  203. void rtc_update_irq(struct rtc_device *rtc,
  204. unsigned long num, unsigned long events)
  205. {
  206. spin_lock(&rtc->irq_lock);
  207. rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
  208. spin_unlock(&rtc->irq_lock);
  209. spin_lock(&rtc->irq_task_lock);
  210. if (rtc->irq_task)
  211. rtc->irq_task->func(rtc->irq_task->private_data);
  212. spin_unlock(&rtc->irq_task_lock);
  213. wake_up_interruptible(&rtc->irq_queue);
  214. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  215. }
  216. EXPORT_SYMBOL_GPL(rtc_update_irq);
  217. static int __rtc_match(struct device *dev, void *data)
  218. {
  219. char *name = (char *)data;
  220. if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0)
  221. return 1;
  222. return 0;
  223. }
  224. struct rtc_device *rtc_class_open(char *name)
  225. {
  226. struct device *dev;
  227. struct rtc_device *rtc = NULL;
  228. dev = class_find_device(rtc_class, name, __rtc_match);
  229. if (dev)
  230. rtc = to_rtc_device(dev);
  231. if (rtc) {
  232. if (!try_module_get(rtc->owner)) {
  233. put_device(dev);
  234. rtc = NULL;
  235. }
  236. }
  237. return rtc;
  238. }
  239. EXPORT_SYMBOL_GPL(rtc_class_open);
  240. void rtc_class_close(struct rtc_device *rtc)
  241. {
  242. module_put(rtc->owner);
  243. put_device(&rtc->dev);
  244. }
  245. EXPORT_SYMBOL_GPL(rtc_class_close);
  246. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  247. {
  248. int retval = -EBUSY;
  249. if (task == NULL || task->func == NULL)
  250. return -EINVAL;
  251. /* Cannot register while the char dev is in use */
  252. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  253. return -EBUSY;
  254. spin_lock_irq(&rtc->irq_task_lock);
  255. if (rtc->irq_task == NULL) {
  256. rtc->irq_task = task;
  257. retval = 0;
  258. }
  259. spin_unlock_irq(&rtc->irq_task_lock);
  260. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  261. return retval;
  262. }
  263. EXPORT_SYMBOL_GPL(rtc_irq_register);
  264. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  265. {
  266. spin_lock_irq(&rtc->irq_task_lock);
  267. if (rtc->irq_task == task)
  268. rtc->irq_task = NULL;
  269. spin_unlock_irq(&rtc->irq_task_lock);
  270. }
  271. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  272. /**
  273. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  274. * @rtc: the rtc device
  275. * @task: currently registered with rtc_irq_register()
  276. * @enabled: true to enable periodic IRQs
  277. * Context: any
  278. *
  279. * Note that rtc_irq_set_freq() should previously have been used to
  280. * specify the desired frequency of periodic IRQ task->func() callbacks.
  281. */
  282. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  283. {
  284. int err = 0;
  285. unsigned long flags;
  286. if (rtc->ops->irq_set_state == NULL)
  287. return -ENXIO;
  288. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  289. if (rtc->irq_task != NULL && task == NULL)
  290. err = -EBUSY;
  291. if (rtc->irq_task != task)
  292. err = -EACCES;
  293. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  294. if (err == 0)
  295. err = rtc->ops->irq_set_state(rtc->dev.parent, enabled);
  296. return err;
  297. }
  298. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  299. /**
  300. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  301. * @rtc: the rtc device
  302. * @task: currently registered with rtc_irq_register()
  303. * @freq: positive frequency with which task->func() will be called
  304. * Context: any
  305. *
  306. * Note that rtc_irq_set_state() is used to enable or disable the
  307. * periodic IRQs.
  308. */
  309. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  310. {
  311. int err = 0;
  312. unsigned long flags;
  313. if (rtc->ops->irq_set_freq == NULL)
  314. return -ENXIO;
  315. if (!is_power_of_2(freq))
  316. return -EINVAL;
  317. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  318. if (rtc->irq_task != NULL && task == NULL)
  319. err = -EBUSY;
  320. if (rtc->irq_task != task)
  321. err = -EACCES;
  322. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  323. if (err == 0) {
  324. err = rtc->ops->irq_set_freq(rtc->dev.parent, freq);
  325. if (err == 0)
  326. rtc->irq_freq = freq;
  327. }
  328. return err;
  329. }
  330. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);