proc.c 13 KB

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