sysfs.c 8.3 KB

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