sysfs.c 8.3 KB

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