rtc-sysfs.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * RTC subsystem, sysfs interface
  3. *
  4. * Copyright (C) 2005 Tower Technologies
  5. * Author: Alessandro Zummo <a.zummo@towertech.it>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/rtc.h>
  13. #include "rtc-core.h"
  14. /* device attributes */
  15. /*
  16. * NOTE: RTC times displayed in sysfs use the RTC's timezone. That's
  17. * ideally UTC. However, PCs that also boot to MS-Windows normally use
  18. * the local time and change to match daylight savings time. That affects
  19. * attributes including date, time, since_epoch, and wakealarm.
  20. */
  21. static ssize_t
  22. name_show(struct device *dev, struct device_attribute *attr, char *buf)
  23. {
  24. return sprintf(buf, "%s %s\n", dev_driver_string(dev->parent),
  25. dev_name(dev->parent));
  26. }
  27. static DEVICE_ATTR_RO(name);
  28. static ssize_t
  29. date_show(struct device *dev, struct device_attribute *attr, char *buf)
  30. {
  31. ssize_t retval;
  32. struct rtc_time tm;
  33. retval = rtc_read_time(to_rtc_device(dev), &tm);
  34. if (retval == 0) {
  35. retval = sprintf(buf, "%04d-%02d-%02d\n",
  36. tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday);
  37. }
  38. return retval;
  39. }
  40. static DEVICE_ATTR_RO(date);
  41. static ssize_t
  42. time_show(struct device *dev, struct device_attribute *attr, char *buf)
  43. {
  44. ssize_t retval;
  45. struct rtc_time tm;
  46. retval = rtc_read_time(to_rtc_device(dev), &tm);
  47. if (retval == 0) {
  48. retval = sprintf(buf, "%02d:%02d:%02d\n",
  49. tm.tm_hour, tm.tm_min, tm.tm_sec);
  50. }
  51. return retval;
  52. }
  53. static DEVICE_ATTR_RO(time);
  54. static ssize_t
  55. since_epoch_show(struct device *dev, struct device_attribute *attr, char *buf)
  56. {
  57. ssize_t retval;
  58. struct rtc_time tm;
  59. retval = rtc_read_time(to_rtc_device(dev), &tm);
  60. if (retval == 0) {
  61. time64_t time;
  62. time = rtc_tm_to_time64(&tm);
  63. retval = sprintf(buf, "%lld\n", time);
  64. }
  65. return retval;
  66. }
  67. static DEVICE_ATTR_RO(since_epoch);
  68. static ssize_t
  69. max_user_freq_show(struct device *dev, struct device_attribute *attr, char *buf)
  70. {
  71. return sprintf(buf, "%d\n", to_rtc_device(dev)->max_user_freq);
  72. }
  73. static ssize_t
  74. max_user_freq_store(struct device *dev, struct device_attribute *attr,
  75. const char *buf, size_t n)
  76. {
  77. struct rtc_device *rtc = to_rtc_device(dev);
  78. unsigned long val;
  79. int err;
  80. err = kstrtoul(buf, 0, &val);
  81. if (err)
  82. return err;
  83. if (val >= 4096 || val == 0)
  84. return -EINVAL;
  85. rtc->max_user_freq = (int)val;
  86. return n;
  87. }
  88. static DEVICE_ATTR_RW(max_user_freq);
  89. /**
  90. * rtc_sysfs_show_hctosys - indicate if the given RTC set the system time
  91. *
  92. * Returns 1 if the system clock was set by this RTC at the last
  93. * boot or resume event.
  94. */
  95. static ssize_t
  96. hctosys_show(struct device *dev, struct device_attribute *attr, char *buf)
  97. {
  98. #ifdef CONFIG_RTC_HCTOSYS_DEVICE
  99. if (rtc_hctosys_ret == 0 &&
  100. strcmp(dev_name(&to_rtc_device(dev)->dev),
  101. CONFIG_RTC_HCTOSYS_DEVICE) == 0)
  102. return sprintf(buf, "1\n");
  103. else
  104. #endif
  105. return sprintf(buf, "0\n");
  106. }
  107. static DEVICE_ATTR_RO(hctosys);
  108. static ssize_t
  109. wakealarm_show(struct device *dev, struct device_attribute *attr, char *buf)
  110. {
  111. ssize_t retval;
  112. time64_t alarm;
  113. struct rtc_wkalrm alm;
  114. /* Don't show disabled alarms. For uniformity, RTC alarms are
  115. * conceptually one-shot, even though some common RTCs (on PCs)
  116. * don't actually work that way.
  117. *
  118. * NOTE: RTC implementations where the alarm doesn't match an
  119. * exact YYYY-MM-DD HH:MM[:SS] date *must* disable their RTC
  120. * alarms after they trigger, to ensure one-shot semantics.
  121. */
  122. retval = rtc_read_alarm(to_rtc_device(dev), &alm);
  123. if (retval == 0 && alm.enabled) {
  124. alarm = rtc_tm_to_time64(&alm.time);
  125. retval = sprintf(buf, "%lld\n", alarm);
  126. }
  127. return retval;
  128. }
  129. static ssize_t
  130. wakealarm_store(struct device *dev, struct device_attribute *attr,
  131. const char *buf, size_t n)
  132. {
  133. ssize_t retval;
  134. time64_t now, alarm;
  135. time64_t push = 0;
  136. struct rtc_wkalrm alm;
  137. struct rtc_device *rtc = to_rtc_device(dev);
  138. const char *buf_ptr;
  139. int adjust = 0;
  140. /* Only request alarms that trigger in the future. Disable them
  141. * by writing another time, e.g. 0 meaning Jan 1 1970 UTC.
  142. */
  143. retval = rtc_read_time(rtc, &alm.time);
  144. if (retval < 0)
  145. return retval;
  146. now = rtc_tm_to_time64(&alm.time);
  147. buf_ptr = buf;
  148. if (*buf_ptr == '+') {
  149. buf_ptr++;
  150. if (*buf_ptr == '=') {
  151. buf_ptr++;
  152. push = 1;
  153. } else
  154. adjust = 1;
  155. }
  156. retval = kstrtos64(buf_ptr, 0, &alarm);
  157. if (retval)
  158. return retval;
  159. if (adjust) {
  160. alarm += now;
  161. }
  162. if (alarm > now || push) {
  163. /* Avoid accidentally clobbering active alarms; we can't
  164. * entirely prevent that here, without even the minimal
  165. * locking from the /dev/rtcN api.
  166. */
  167. retval = rtc_read_alarm(rtc, &alm);
  168. if (retval < 0)
  169. return retval;
  170. if (alm.enabled) {
  171. if (push) {
  172. push = rtc_tm_to_time64(&alm.time);
  173. alarm += push;
  174. } else
  175. return -EBUSY;
  176. } else if (push)
  177. return -EINVAL;
  178. alm.enabled = 1;
  179. } else {
  180. alm.enabled = 0;
  181. /* Provide a valid future alarm time. Linux isn't EFI,
  182. * this time won't be ignored when disabling the alarm.
  183. */
  184. alarm = now + 300;
  185. }
  186. rtc_time64_to_tm(alarm, &alm.time);
  187. retval = rtc_set_alarm(rtc, &alm);
  188. return (retval < 0) ? retval : n;
  189. }
  190. static DEVICE_ATTR_RW(wakealarm);
  191. static ssize_t
  192. offset_show(struct device *dev, struct device_attribute *attr, char *buf)
  193. {
  194. ssize_t retval;
  195. long offset;
  196. retval = rtc_read_offset(to_rtc_device(dev), &offset);
  197. if (retval == 0)
  198. retval = sprintf(buf, "%ld\n", offset);
  199. return retval;
  200. }
  201. static ssize_t
  202. offset_store(struct device *dev, struct device_attribute *attr,
  203. const char *buf, size_t n)
  204. {
  205. ssize_t retval;
  206. long offset;
  207. retval = kstrtol(buf, 10, &offset);
  208. if (retval == 0)
  209. retval = rtc_set_offset(to_rtc_device(dev), offset);
  210. return (retval < 0) ? retval : n;
  211. }
  212. static DEVICE_ATTR_RW(offset);
  213. static ssize_t
  214. range_show(struct device *dev, struct device_attribute *attr, char *buf)
  215. {
  216. return sprintf(buf, "[%lld,%llu]\n", to_rtc_device(dev)->range_min,
  217. to_rtc_device(dev)->range_max);
  218. }
  219. static DEVICE_ATTR_RO(range);
  220. static struct attribute *rtc_attrs[] = {
  221. &dev_attr_name.attr,
  222. &dev_attr_date.attr,
  223. &dev_attr_time.attr,
  224. &dev_attr_since_epoch.attr,
  225. &dev_attr_max_user_freq.attr,
  226. &dev_attr_hctosys.attr,
  227. &dev_attr_wakealarm.attr,
  228. &dev_attr_offset.attr,
  229. &dev_attr_range.attr,
  230. NULL,
  231. };
  232. /* The reason to trigger an alarm with no process watching it (via sysfs)
  233. * is its side effect: waking from a system state like suspend-to-RAM or
  234. * suspend-to-disk. So: no attribute unless that side effect is possible.
  235. * (Userspace may disable that mechanism later.)
  236. */
  237. static bool rtc_does_wakealarm(struct rtc_device *rtc)
  238. {
  239. if (!device_can_wakeup(rtc->dev.parent))
  240. return false;
  241. return rtc->ops->set_alarm != NULL;
  242. }
  243. static umode_t rtc_attr_is_visible(struct kobject *kobj,
  244. struct attribute *attr, int n)
  245. {
  246. struct device *dev = container_of(kobj, struct device, kobj);
  247. struct rtc_device *rtc = to_rtc_device(dev);
  248. umode_t mode = attr->mode;
  249. if (attr == &dev_attr_wakealarm.attr) {
  250. if (!rtc_does_wakealarm(rtc))
  251. mode = 0;
  252. } else if (attr == &dev_attr_offset.attr) {
  253. if (!rtc->ops->set_offset)
  254. mode = 0;
  255. } else if (attr == &dev_attr_range.attr) {
  256. if (!(rtc->range_max - rtc->range_min))
  257. mode = 0;
  258. }
  259. return mode;
  260. }
  261. static struct attribute_group rtc_attr_group = {
  262. .is_visible = rtc_attr_is_visible,
  263. .attrs = rtc_attrs,
  264. };
  265. static const struct attribute_group *rtc_attr_groups[] = {
  266. &rtc_attr_group,
  267. NULL
  268. };
  269. const struct attribute_group **rtc_get_dev_attribute_groups(void)
  270. {
  271. return rtc_attr_groups;
  272. }
  273. int rtc_add_groups(struct rtc_device *rtc, const struct attribute_group **grps)
  274. {
  275. size_t old_cnt = 0, add_cnt = 0, new_cnt;
  276. const struct attribute_group **groups, **old;
  277. if (rtc->registered)
  278. return -EINVAL;
  279. if (!grps)
  280. return -EINVAL;
  281. groups = rtc->dev.groups;
  282. if (groups)
  283. for (; *groups; groups++)
  284. old_cnt++;
  285. for (groups = grps; *groups; groups++)
  286. add_cnt++;
  287. new_cnt = old_cnt + add_cnt + 1;
  288. groups = devm_kcalloc(&rtc->dev, new_cnt, sizeof(*groups), GFP_KERNEL);
  289. if (!groups)
  290. return -ENOMEM;
  291. memcpy(groups, rtc->dev.groups, old_cnt * sizeof(*groups));
  292. memcpy(groups + old_cnt, grps, add_cnt * sizeof(*groups));
  293. groups[old_cnt + add_cnt] = NULL;
  294. old = rtc->dev.groups;
  295. rtc->dev.groups = groups;
  296. if (old && old != rtc_attr_groups)
  297. devm_kfree(&rtc->dev, old);
  298. return 0;
  299. }
  300. EXPORT_SYMBOL(rtc_add_groups);
  301. int rtc_add_group(struct rtc_device *rtc, const struct attribute_group *grp)
  302. {
  303. const struct attribute_group *groups[] = { grp, NULL };
  304. return rtc_add_groups(rtc, groups);
  305. }
  306. EXPORT_SYMBOL(rtc_add_group);