sysfs.c 8.3 KB

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