proc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. #include <linux/proc_fs.h>
  2. #include <linux/seq_file.h>
  3. #include <linux/export.h>
  4. #include <linux/suspend.h>
  5. #include <linux/bcd.h>
  6. #include <asm/uaccess.h>
  7. #include <acpi/acpi_bus.h>
  8. #include <acpi/acpi_drivers.h>
  9. #ifdef CONFIG_X86
  10. #include <linux/mc146818rtc.h>
  11. #endif
  12. #include "sleep.h"
  13. #define _COMPONENT ACPI_SYSTEM_COMPONENT
  14. /*
  15. * this file provides support for:
  16. * /proc/acpi/alarm
  17. * /proc/acpi/wakeup
  18. */
  19. ACPI_MODULE_NAME("sleep")
  20. #if defined(CONFIG_RTC_DRV_CMOS) || defined(CONFIG_RTC_DRV_CMOS_MODULE) || !defined(CONFIG_X86)
  21. /* use /sys/class/rtc/rtcX/wakealarm instead; it's not ACPI-specific */
  22. #else
  23. #define HAVE_ACPI_LEGACY_ALARM
  24. #endif
  25. #ifdef HAVE_ACPI_LEGACY_ALARM
  26. static u32 cmos_bcd_read(int offset, int rtc_control);
  27. static int acpi_system_alarm_seq_show(struct seq_file *seq, void *offset)
  28. {
  29. u32 sec, min, hr;
  30. u32 day, mo, yr, cent = 0;
  31. u32 today = 0;
  32. unsigned char rtc_control = 0;
  33. unsigned long flags;
  34. spin_lock_irqsave(&rtc_lock, flags);
  35. rtc_control = CMOS_READ(RTC_CONTROL);
  36. sec = cmos_bcd_read(RTC_SECONDS_ALARM, rtc_control);
  37. min = cmos_bcd_read(RTC_MINUTES_ALARM, rtc_control);
  38. hr = cmos_bcd_read(RTC_HOURS_ALARM, rtc_control);
  39. /* If we ever get an FACP with proper values... */
  40. if (acpi_gbl_FADT.day_alarm) {
  41. /* ACPI spec: only low 6 its should be cared */
  42. day = CMOS_READ(acpi_gbl_FADT.day_alarm) & 0x3F;
  43. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  44. day = bcd2bin(day);
  45. } else
  46. day = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
  47. if (acpi_gbl_FADT.month_alarm)
  48. mo = cmos_bcd_read(acpi_gbl_FADT.month_alarm, rtc_control);
  49. else {
  50. mo = cmos_bcd_read(RTC_MONTH, rtc_control);
  51. today = cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
  52. }
  53. if (acpi_gbl_FADT.century)
  54. cent = cmos_bcd_read(acpi_gbl_FADT.century, rtc_control);
  55. yr = cmos_bcd_read(RTC_YEAR, rtc_control);
  56. spin_unlock_irqrestore(&rtc_lock, flags);
  57. /* we're trusting the FADT (see above) */
  58. if (!acpi_gbl_FADT.century)
  59. /* If we're not trusting the FADT, we should at least make it
  60. * right for _this_ century... ehm, what is _this_ century?
  61. *
  62. * TBD:
  63. * ASAP: find piece of code in the kernel, e.g. star tracker driver,
  64. * which we can trust to determine the century correctly. Atom
  65. * watch driver would be nice, too...
  66. *
  67. * if that has not happened, change for first release in 2050:
  68. * if (yr<50)
  69. * yr += 2100;
  70. * else
  71. * yr += 2000; // current line of code
  72. *
  73. * if that has not happened either, please do on 2099/12/31:23:59:59
  74. * s/2000/2100
  75. *
  76. */
  77. yr += 2000;
  78. else
  79. yr += cent * 100;
  80. /*
  81. * Show correct dates for alarms up to a month into the future.
  82. * This solves issues for nearly all situations with the common
  83. * 30-day alarm clocks in PC hardware.
  84. */
  85. if (day < today) {
  86. if (mo < 12) {
  87. mo += 1;
  88. } else {
  89. mo = 1;
  90. yr += 1;
  91. }
  92. }
  93. seq_printf(seq, "%4.4u-", yr);
  94. (mo > 12) ? seq_puts(seq, "**-") : seq_printf(seq, "%2.2u-", mo);
  95. (day > 31) ? seq_puts(seq, "** ") : seq_printf(seq, "%2.2u ", day);
  96. (hr > 23) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", hr);
  97. (min > 59) ? seq_puts(seq, "**:") : seq_printf(seq, "%2.2u:", min);
  98. (sec > 59) ? seq_puts(seq, "**\n") : seq_printf(seq, "%2.2u\n", sec);
  99. return 0;
  100. }
  101. static int acpi_system_alarm_open_fs(struct inode *inode, struct file *file)
  102. {
  103. return single_open(file, acpi_system_alarm_seq_show, PDE(inode)->data);
  104. }
  105. static int get_date_field(char **p, u32 * value)
  106. {
  107. char *next = NULL;
  108. char *string_end = NULL;
  109. int result = -EINVAL;
  110. /*
  111. * Try to find delimeter, only to insert null. The end of the
  112. * string won't have one, but is still valid.
  113. */
  114. if (*p == NULL)
  115. return result;
  116. next = strpbrk(*p, "- :");
  117. if (next)
  118. *next++ = '\0';
  119. *value = simple_strtoul(*p, &string_end, 10);
  120. /* Signal success if we got a good digit */
  121. if (string_end != *p)
  122. result = 0;
  123. if (next)
  124. *p = next;
  125. else
  126. *p = NULL;
  127. return result;
  128. }
  129. /* Read a possibly BCD register, always return binary */
  130. static u32 cmos_bcd_read(int offset, int rtc_control)
  131. {
  132. u32 val = CMOS_READ(offset);
  133. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  134. val = bcd2bin(val);
  135. return val;
  136. }
  137. /* Write binary value into possibly BCD register */
  138. static void cmos_bcd_write(u32 val, int offset, int rtc_control)
  139. {
  140. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  141. val = bin2bcd(val);
  142. CMOS_WRITE(val, offset);
  143. }
  144. static ssize_t
  145. acpi_system_write_alarm(struct file *file,
  146. const char __user * buffer, size_t count, loff_t * ppos)
  147. {
  148. int result = 0;
  149. char alarm_string[30] = { '\0' };
  150. char *p = alarm_string;
  151. u32 sec, min, hr, day, mo, yr;
  152. int adjust = 0;
  153. unsigned char rtc_control = 0;
  154. if (count > sizeof(alarm_string) - 1)
  155. return -EINVAL;
  156. if (copy_from_user(alarm_string, buffer, count))
  157. return -EFAULT;
  158. alarm_string[count] = '\0';
  159. /* check for time adjustment */
  160. if (alarm_string[0] == '+') {
  161. p++;
  162. adjust = 1;
  163. }
  164. if ((result = get_date_field(&p, &yr)))
  165. goto end;
  166. if ((result = get_date_field(&p, &mo)))
  167. goto end;
  168. if ((result = get_date_field(&p, &day)))
  169. goto end;
  170. if ((result = get_date_field(&p, &hr)))
  171. goto end;
  172. if ((result = get_date_field(&p, &min)))
  173. goto end;
  174. if ((result = get_date_field(&p, &sec)))
  175. goto end;
  176. spin_lock_irq(&rtc_lock);
  177. rtc_control = CMOS_READ(RTC_CONTROL);
  178. if (adjust) {
  179. yr += cmos_bcd_read(RTC_YEAR, rtc_control);
  180. mo += cmos_bcd_read(RTC_MONTH, rtc_control);
  181. day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
  182. hr += cmos_bcd_read(RTC_HOURS, rtc_control);
  183. min += cmos_bcd_read(RTC_MINUTES, rtc_control);
  184. sec += cmos_bcd_read(RTC_SECONDS, rtc_control);
  185. }
  186. spin_unlock_irq(&rtc_lock);
  187. if (sec > 59) {
  188. min += sec/60;
  189. sec = sec%60;
  190. }
  191. if (min > 59) {
  192. hr += min/60;
  193. min = min%60;
  194. }
  195. if (hr > 23) {
  196. day += hr/24;
  197. hr = hr%24;
  198. }
  199. if (day > 31) {
  200. mo += day/32;
  201. day = day%32;
  202. }
  203. if (mo > 12) {
  204. yr += mo/13;
  205. mo = mo%13;
  206. }
  207. spin_lock_irq(&rtc_lock);
  208. /*
  209. * Disable alarm interrupt before setting alarm timer or else
  210. * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
  211. */
  212. rtc_control &= ~RTC_AIE;
  213. CMOS_WRITE(rtc_control, RTC_CONTROL);
  214. CMOS_READ(RTC_INTR_FLAGS);
  215. /* write the fields the rtc knows about */
  216. cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control);
  217. cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control);
  218. cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control);
  219. /*
  220. * If the system supports an enhanced alarm it will have non-zero
  221. * offsets into the CMOS RAM here -- which for some reason are pointing
  222. * to the RTC area of memory.
  223. */
  224. if (acpi_gbl_FADT.day_alarm)
  225. cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control);
  226. if (acpi_gbl_FADT.month_alarm)
  227. cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control);
  228. if (acpi_gbl_FADT.century) {
  229. if (adjust)
  230. yr += cmos_bcd_read(acpi_gbl_FADT.century, rtc_control) * 100;
  231. cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
  232. }
  233. /* enable the rtc alarm interrupt */
  234. rtc_control |= RTC_AIE;
  235. CMOS_WRITE(rtc_control, RTC_CONTROL);
  236. CMOS_READ(RTC_INTR_FLAGS);
  237. spin_unlock_irq(&rtc_lock);
  238. acpi_clear_event(ACPI_EVENT_RTC);
  239. acpi_enable_event(ACPI_EVENT_RTC, 0);
  240. *ppos += count;
  241. result = 0;
  242. end:
  243. return result ? result : count;
  244. }
  245. #endif /* HAVE_ACPI_LEGACY_ALARM */
  246. static int
  247. acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
  248. {
  249. struct list_head *node, *next;
  250. seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
  251. mutex_lock(&acpi_device_lock);
  252. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  253. struct acpi_device *dev =
  254. container_of(node, struct acpi_device, wakeup_list);
  255. struct device *ldev;
  256. if (!dev->wakeup.flags.valid)
  257. continue;
  258. ldev = acpi_get_physical_device(dev->handle);
  259. seq_printf(seq, "%s\t S%d\t%c%-8s ",
  260. dev->pnp.bus_id,
  261. (u32) dev->wakeup.sleep_state,
  262. dev->wakeup.flags.run_wake ? '*' : ' ',
  263. (device_may_wakeup(&dev->dev)
  264. || (ldev && device_may_wakeup(ldev))) ?
  265. "enabled" : "disabled");
  266. if (ldev)
  267. seq_printf(seq, "%s:%s",
  268. ldev->bus ? ldev->bus->name : "no-bus",
  269. dev_name(ldev));
  270. seq_printf(seq, "\n");
  271. put_device(ldev);
  272. }
  273. mutex_unlock(&acpi_device_lock);
  274. return 0;
  275. }
  276. static void physical_device_enable_wakeup(struct acpi_device *adev)
  277. {
  278. struct device *dev = acpi_get_physical_device(adev->handle);
  279. if (dev && device_can_wakeup(dev)) {
  280. bool enable = !device_may_wakeup(dev);
  281. device_set_wakeup_enable(dev, enable);
  282. }
  283. }
  284. static ssize_t
  285. acpi_system_write_wakeup_device(struct file *file,
  286. const char __user * buffer,
  287. size_t count, loff_t * ppos)
  288. {
  289. struct list_head *node, *next;
  290. char strbuf[5];
  291. char str[5] = "";
  292. unsigned int len = count;
  293. if (len > 4)
  294. len = 4;
  295. if (len < 0)
  296. return -EFAULT;
  297. if (copy_from_user(strbuf, buffer, len))
  298. return -EFAULT;
  299. strbuf[len] = '\0';
  300. sscanf(strbuf, "%s", str);
  301. mutex_lock(&acpi_device_lock);
  302. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  303. struct acpi_device *dev =
  304. container_of(node, struct acpi_device, wakeup_list);
  305. if (!dev->wakeup.flags.valid)
  306. continue;
  307. if (!strncmp(dev->pnp.bus_id, str, 4)) {
  308. if (device_can_wakeup(&dev->dev)) {
  309. bool enable = !device_may_wakeup(&dev->dev);
  310. device_set_wakeup_enable(&dev->dev, enable);
  311. } else {
  312. physical_device_enable_wakeup(dev);
  313. }
  314. break;
  315. }
  316. }
  317. mutex_unlock(&acpi_device_lock);
  318. return count;
  319. }
  320. static int
  321. acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
  322. {
  323. return single_open(file, acpi_system_wakeup_device_seq_show,
  324. PDE(inode)->data);
  325. }
  326. static const struct file_operations acpi_system_wakeup_device_fops = {
  327. .owner = THIS_MODULE,
  328. .open = acpi_system_wakeup_device_open_fs,
  329. .read = seq_read,
  330. .write = acpi_system_write_wakeup_device,
  331. .llseek = seq_lseek,
  332. .release = single_release,
  333. };
  334. #ifdef HAVE_ACPI_LEGACY_ALARM
  335. static const struct file_operations acpi_system_alarm_fops = {
  336. .owner = THIS_MODULE,
  337. .open = acpi_system_alarm_open_fs,
  338. .read = seq_read,
  339. .write = acpi_system_write_alarm,
  340. .llseek = seq_lseek,
  341. .release = single_release,
  342. };
  343. static u32 rtc_handler(void *context)
  344. {
  345. acpi_clear_event(ACPI_EVENT_RTC);
  346. acpi_disable_event(ACPI_EVENT_RTC, 0);
  347. return ACPI_INTERRUPT_HANDLED;
  348. }
  349. #endif /* HAVE_ACPI_LEGACY_ALARM */
  350. int __init acpi_sleep_proc_init(void)
  351. {
  352. #ifdef HAVE_ACPI_LEGACY_ALARM
  353. /* 'alarm' [R/W] */
  354. proc_create("alarm", S_IFREG | S_IRUGO | S_IWUSR,
  355. acpi_root_dir, &acpi_system_alarm_fops);
  356. acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
  357. /*
  358. * Disable the RTC event after installing RTC handler.
  359. * Only when RTC alarm is set will it be enabled.
  360. */
  361. acpi_clear_event(ACPI_EVENT_RTC);
  362. acpi_disable_event(ACPI_EVENT_RTC, 0);
  363. #endif /* HAVE_ACPI_LEGACY_ALARM */
  364. /* 'wakeup device' [R/W] */
  365. proc_create("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
  366. acpi_root_dir, &acpi_system_wakeup_device_fops);
  367. return 0;
  368. }