interface.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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) != 0);
  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. int rtc_alarm_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  268. {
  269. int err = mutex_lock_interruptible(&rtc->ops_lock);
  270. if (err)
  271. return err;
  272. if (!rtc->ops)
  273. err = -ENODEV;
  274. else if (!rtc->ops->alarm_irq_enable)
  275. err = -EINVAL;
  276. else
  277. err = rtc->ops->alarm_irq_enable(rtc->dev.parent, enabled);
  278. mutex_unlock(&rtc->ops_lock);
  279. return err;
  280. }
  281. EXPORT_SYMBOL_GPL(rtc_alarm_irq_enable);
  282. int rtc_update_irq_enable(struct rtc_device *rtc, unsigned int enabled)
  283. {
  284. int err = mutex_lock_interruptible(&rtc->ops_lock);
  285. if (err)
  286. return err;
  287. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  288. if (enabled == 0 && rtc->uie_irq_active) {
  289. mutex_unlock(&rtc->ops_lock);
  290. return rtc_dev_update_irq_enable_emul(rtc, enabled);
  291. }
  292. #endif
  293. if (!rtc->ops)
  294. err = -ENODEV;
  295. else if (!rtc->ops->update_irq_enable)
  296. err = -EINVAL;
  297. else
  298. err = rtc->ops->update_irq_enable(rtc->dev.parent, enabled);
  299. mutex_unlock(&rtc->ops_lock);
  300. #ifdef CONFIG_RTC_INTF_DEV_UIE_EMUL
  301. /*
  302. * Enable emulation if the driver did not provide
  303. * the update_irq_enable function pointer or if returned
  304. * -EINVAL to signal that it has been configured without
  305. * interrupts or that are not available at the moment.
  306. */
  307. if (err == -EINVAL)
  308. err = rtc_dev_update_irq_enable_emul(rtc, enabled);
  309. #endif
  310. return err;
  311. }
  312. EXPORT_SYMBOL_GPL(rtc_update_irq_enable);
  313. /**
  314. * rtc_update_irq - report RTC periodic, alarm, and/or update irqs
  315. * @rtc: the rtc device
  316. * @num: how many irqs are being reported (usually one)
  317. * @events: mask of RTC_IRQF with one or more of RTC_PF, RTC_AF, RTC_UF
  318. * Context: in_interrupt(), irqs blocked
  319. */
  320. void rtc_update_irq(struct rtc_device *rtc,
  321. unsigned long num, unsigned long events)
  322. {
  323. spin_lock(&rtc->irq_lock);
  324. rtc->irq_data = (rtc->irq_data + (num << 8)) | events;
  325. spin_unlock(&rtc->irq_lock);
  326. spin_lock(&rtc->irq_task_lock);
  327. if (rtc->irq_task)
  328. rtc->irq_task->func(rtc->irq_task->private_data);
  329. spin_unlock(&rtc->irq_task_lock);
  330. wake_up_interruptible(&rtc->irq_queue);
  331. kill_fasync(&rtc->async_queue, SIGIO, POLL_IN);
  332. }
  333. EXPORT_SYMBOL_GPL(rtc_update_irq);
  334. static int __rtc_match(struct device *dev, void *data)
  335. {
  336. char *name = (char *)data;
  337. if (strncmp(dev->bus_id, name, BUS_ID_SIZE) == 0)
  338. return 1;
  339. return 0;
  340. }
  341. struct rtc_device *rtc_class_open(char *name)
  342. {
  343. struct device *dev;
  344. struct rtc_device *rtc = NULL;
  345. dev = class_find_device(rtc_class, NULL, name, __rtc_match);
  346. if (dev)
  347. rtc = to_rtc_device(dev);
  348. if (rtc) {
  349. if (!try_module_get(rtc->owner)) {
  350. put_device(dev);
  351. rtc = NULL;
  352. }
  353. }
  354. return rtc;
  355. }
  356. EXPORT_SYMBOL_GPL(rtc_class_open);
  357. void rtc_class_close(struct rtc_device *rtc)
  358. {
  359. module_put(rtc->owner);
  360. put_device(&rtc->dev);
  361. }
  362. EXPORT_SYMBOL_GPL(rtc_class_close);
  363. int rtc_irq_register(struct rtc_device *rtc, struct rtc_task *task)
  364. {
  365. int retval = -EBUSY;
  366. if (task == NULL || task->func == NULL)
  367. return -EINVAL;
  368. /* Cannot register while the char dev is in use */
  369. if (test_and_set_bit_lock(RTC_DEV_BUSY, &rtc->flags))
  370. return -EBUSY;
  371. spin_lock_irq(&rtc->irq_task_lock);
  372. if (rtc->irq_task == NULL) {
  373. rtc->irq_task = task;
  374. retval = 0;
  375. }
  376. spin_unlock_irq(&rtc->irq_task_lock);
  377. clear_bit_unlock(RTC_DEV_BUSY, &rtc->flags);
  378. return retval;
  379. }
  380. EXPORT_SYMBOL_GPL(rtc_irq_register);
  381. void rtc_irq_unregister(struct rtc_device *rtc, struct rtc_task *task)
  382. {
  383. spin_lock_irq(&rtc->irq_task_lock);
  384. if (rtc->irq_task == task)
  385. rtc->irq_task = NULL;
  386. spin_unlock_irq(&rtc->irq_task_lock);
  387. }
  388. EXPORT_SYMBOL_GPL(rtc_irq_unregister);
  389. /**
  390. * rtc_irq_set_state - enable/disable 2^N Hz periodic IRQs
  391. * @rtc: the rtc device
  392. * @task: currently registered with rtc_irq_register()
  393. * @enabled: true to enable periodic IRQs
  394. * Context: any
  395. *
  396. * Note that rtc_irq_set_freq() should previously have been used to
  397. * specify the desired frequency of periodic IRQ task->func() callbacks.
  398. */
  399. int rtc_irq_set_state(struct rtc_device *rtc, struct rtc_task *task, int enabled)
  400. {
  401. int err = 0;
  402. unsigned long flags;
  403. if (rtc->ops->irq_set_state == NULL)
  404. return -ENXIO;
  405. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  406. if (rtc->irq_task != NULL && task == NULL)
  407. err = -EBUSY;
  408. if (rtc->irq_task != task)
  409. err = -EACCES;
  410. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  411. if (err == 0)
  412. err = rtc->ops->irq_set_state(rtc->dev.parent, enabled);
  413. return err;
  414. }
  415. EXPORT_SYMBOL_GPL(rtc_irq_set_state);
  416. /**
  417. * rtc_irq_set_freq - set 2^N Hz periodic IRQ frequency for IRQ
  418. * @rtc: the rtc device
  419. * @task: currently registered with rtc_irq_register()
  420. * @freq: positive frequency with which task->func() will be called
  421. * Context: any
  422. *
  423. * Note that rtc_irq_set_state() is used to enable or disable the
  424. * periodic IRQs.
  425. */
  426. int rtc_irq_set_freq(struct rtc_device *rtc, struct rtc_task *task, int freq)
  427. {
  428. int err = 0;
  429. unsigned long flags;
  430. if (rtc->ops->irq_set_freq == NULL)
  431. return -ENXIO;
  432. if (!is_power_of_2(freq))
  433. return -EINVAL;
  434. spin_lock_irqsave(&rtc->irq_task_lock, flags);
  435. if (rtc->irq_task != NULL && task == NULL)
  436. err = -EBUSY;
  437. if (rtc->irq_task != task)
  438. err = -EACCES;
  439. spin_unlock_irqrestore(&rtc->irq_task_lock, flags);
  440. if (err == 0) {
  441. err = rtc->ops->irq_set_freq(rtc->dev.parent, freq);
  442. if (err == 0)
  443. rtc->irq_freq = freq;
  444. }
  445. return err;
  446. }
  447. EXPORT_SYMBOL_GPL(rtc_irq_set_freq);