proc.c 11 KB

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