proc.c 13 KB

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