proc.c 13 KB

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