proc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. next = strpbrk(*p, "- :");
  152. if (next)
  153. *next++ = '\0';
  154. *value = simple_strtoul(*p, &string_end, 10);
  155. /* Signal success if we got a good digit */
  156. if (string_end != *p)
  157. result = 0;
  158. if (next)
  159. *p = next;
  160. return result;
  161. }
  162. /* Read a possibly BCD register, always return binary */
  163. static u32 cmos_bcd_read(int offset, int rtc_control)
  164. {
  165. u32 val = CMOS_READ(offset);
  166. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  167. BCD_TO_BIN(val);
  168. return val;
  169. }
  170. /* Write binary value into possibly BCD register */
  171. static void cmos_bcd_write(u32 val, int offset, int rtc_control)
  172. {
  173. if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD)
  174. BIN_TO_BCD(val);
  175. CMOS_WRITE(val, offset);
  176. }
  177. static ssize_t
  178. acpi_system_write_alarm(struct file *file,
  179. const char __user * buffer, size_t count, loff_t * ppos)
  180. {
  181. int result = 0;
  182. char alarm_string[30] = { '\0' };
  183. char *p = alarm_string;
  184. u32 sec, min, hr, day, mo, yr;
  185. int adjust = 0;
  186. unsigned char rtc_control = 0;
  187. ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
  188. if (count > sizeof(alarm_string) - 1)
  189. return_VALUE(-EINVAL);
  190. if (copy_from_user(alarm_string, buffer, count))
  191. return_VALUE(-EFAULT);
  192. alarm_string[count] = '\0';
  193. /* check for time adjustment */
  194. if (alarm_string[0] == '+') {
  195. p++;
  196. adjust = 1;
  197. }
  198. if ((result = get_date_field(&p, &yr)))
  199. goto end;
  200. if ((result = get_date_field(&p, &mo)))
  201. goto end;
  202. if ((result = get_date_field(&p, &day)))
  203. goto end;
  204. if ((result = get_date_field(&p, &hr)))
  205. goto end;
  206. if ((result = get_date_field(&p, &min)))
  207. goto end;
  208. if ((result = get_date_field(&p, &sec)))
  209. goto end;
  210. if (sec > 59) {
  211. min += 1;
  212. sec -= 60;
  213. }
  214. if (min > 59) {
  215. hr += 1;
  216. min -= 60;
  217. }
  218. if (hr > 23) {
  219. day += 1;
  220. hr -= 24;
  221. }
  222. if (day > 31) {
  223. mo += 1;
  224. day -= 31;
  225. }
  226. if (mo > 12) {
  227. yr += 1;
  228. mo -= 12;
  229. }
  230. spin_lock_irq(&rtc_lock);
  231. rtc_control = CMOS_READ(RTC_CONTROL);
  232. if (adjust) {
  233. yr += cmos_bcd_read(RTC_YEAR, rtc_control);
  234. mo += cmos_bcd_read(RTC_MONTH, rtc_control);
  235. day += cmos_bcd_read(RTC_DAY_OF_MONTH, rtc_control);
  236. hr += cmos_bcd_read(RTC_HOURS, rtc_control);
  237. min += cmos_bcd_read(RTC_MINUTES, rtc_control);
  238. sec += cmos_bcd_read(RTC_SECONDS, rtc_control);
  239. }
  240. spin_unlock_irq(&rtc_lock);
  241. if (sec > 59) {
  242. min++;
  243. sec -= 60;
  244. }
  245. if (min > 59) {
  246. hr++;
  247. min -= 60;
  248. }
  249. if (hr > 23) {
  250. day++;
  251. hr -= 24;
  252. }
  253. if (day > 31) {
  254. mo++;
  255. day -= 31;
  256. }
  257. if (mo > 12) {
  258. yr++;
  259. mo -= 12;
  260. }
  261. spin_lock_irq(&rtc_lock);
  262. /*
  263. * Disable alarm interrupt before setting alarm timer or else
  264. * when ACPI_EVENT_RTC is enabled, a spurious ACPI interrupt occurs
  265. */
  266. rtc_control &= ~RTC_AIE;
  267. CMOS_WRITE(rtc_control, RTC_CONTROL);
  268. CMOS_READ(RTC_INTR_FLAGS);
  269. /* write the fields the rtc knows about */
  270. cmos_bcd_write(hr, RTC_HOURS_ALARM, rtc_control);
  271. cmos_bcd_write(min, RTC_MINUTES_ALARM, rtc_control);
  272. cmos_bcd_write(sec, RTC_SECONDS_ALARM, rtc_control);
  273. /*
  274. * If the system supports an enhanced alarm it will have non-zero
  275. * offsets into the CMOS RAM here -- which for some reason are pointing
  276. * to the RTC area of memory.
  277. */
  278. if (acpi_gbl_FADT.day_alarm)
  279. cmos_bcd_write(day, acpi_gbl_FADT.day_alarm, rtc_control);
  280. if (acpi_gbl_FADT.month_alarm)
  281. cmos_bcd_write(mo, acpi_gbl_FADT.month_alarm, rtc_control);
  282. if (acpi_gbl_FADT.century)
  283. cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
  284. /* enable the rtc alarm interrupt */
  285. rtc_control |= RTC_AIE;
  286. CMOS_WRITE(rtc_control, RTC_CONTROL);
  287. CMOS_READ(RTC_INTR_FLAGS);
  288. spin_unlock_irq(&rtc_lock);
  289. acpi_clear_event(ACPI_EVENT_RTC);
  290. acpi_enable_event(ACPI_EVENT_RTC, 0);
  291. *ppos += count;
  292. result = 0;
  293. end:
  294. return_VALUE(result ? result : count);
  295. }
  296. #endif /* HAVE_ACPI_LEGACY_ALARM */
  297. extern struct list_head acpi_wakeup_device_list;
  298. extern spinlock_t acpi_device_lock;
  299. static int
  300. acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
  301. {
  302. struct list_head *node, *next;
  303. seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
  304. spin_lock(&acpi_device_lock);
  305. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  306. struct acpi_device *dev =
  307. container_of(node, struct acpi_device, wakeup_list);
  308. struct device *ldev;
  309. if (!dev->wakeup.flags.valid)
  310. continue;
  311. spin_unlock(&acpi_device_lock);
  312. ldev = acpi_get_physical_device(dev->handle);
  313. seq_printf(seq, "%s\t S%d\t%c%-8s ",
  314. dev->pnp.bus_id,
  315. (u32) dev->wakeup.sleep_state,
  316. dev->wakeup.flags.run_wake ? '*' : ' ',
  317. dev->wakeup.state.enabled ? "enabled" : "disabled");
  318. if (ldev)
  319. seq_printf(seq, "%s:%s",
  320. ldev->bus ? ldev->bus->name : "no-bus",
  321. ldev->bus_id);
  322. seq_printf(seq, "\n");
  323. put_device(ldev);
  324. spin_lock(&acpi_device_lock);
  325. }
  326. spin_unlock(&acpi_device_lock);
  327. return 0;
  328. }
  329. static ssize_t
  330. acpi_system_write_wakeup_device(struct file *file,
  331. const char __user * buffer,
  332. size_t count, loff_t * ppos)
  333. {
  334. struct list_head *node, *next;
  335. char strbuf[5];
  336. char str[5] = "";
  337. int len = count;
  338. struct acpi_device *found_dev = NULL;
  339. if (len > 4)
  340. len = 4;
  341. if (copy_from_user(strbuf, buffer, len))
  342. return -EFAULT;
  343. strbuf[len] = '\0';
  344. sscanf(strbuf, "%s", str);
  345. spin_lock(&acpi_device_lock);
  346. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  347. struct acpi_device *dev =
  348. container_of(node, struct acpi_device, wakeup_list);
  349. if (!dev->wakeup.flags.valid)
  350. continue;
  351. if (!strncmp(dev->pnp.bus_id, str, 4)) {
  352. dev->wakeup.state.enabled =
  353. dev->wakeup.state.enabled ? 0 : 1;
  354. found_dev = dev;
  355. break;
  356. }
  357. }
  358. if (found_dev) {
  359. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  360. struct acpi_device *dev = container_of(node,
  361. struct
  362. acpi_device,
  363. wakeup_list);
  364. if ((dev != found_dev) &&
  365. (dev->wakeup.gpe_number ==
  366. found_dev->wakeup.gpe_number)
  367. && (dev->wakeup.gpe_device ==
  368. found_dev->wakeup.gpe_device)) {
  369. printk(KERN_WARNING
  370. "ACPI: '%s' and '%s' have the same GPE, "
  371. "can't disable/enable one seperately\n",
  372. dev->pnp.bus_id, found_dev->pnp.bus_id);
  373. dev->wakeup.state.enabled =
  374. found_dev->wakeup.state.enabled;
  375. }
  376. }
  377. }
  378. spin_unlock(&acpi_device_lock);
  379. return count;
  380. }
  381. static int
  382. acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
  383. {
  384. return single_open(file, acpi_system_wakeup_device_seq_show,
  385. PDE(inode)->data);
  386. }
  387. static const struct file_operations acpi_system_wakeup_device_fops = {
  388. .open = acpi_system_wakeup_device_open_fs,
  389. .read = seq_read,
  390. .write = acpi_system_write_wakeup_device,
  391. .llseek = seq_lseek,
  392. .release = single_release,
  393. };
  394. #ifdef CONFIG_ACPI_PROCFS
  395. static const struct file_operations acpi_system_sleep_fops = {
  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. .open = acpi_system_alarm_open_fs,
  406. .read = seq_read,
  407. .write = acpi_system_write_alarm,
  408. .llseek = seq_lseek,
  409. .release = single_release,
  410. };
  411. static u32 rtc_handler(void *context)
  412. {
  413. acpi_clear_event(ACPI_EVENT_RTC);
  414. acpi_disable_event(ACPI_EVENT_RTC, 0);
  415. return ACPI_INTERRUPT_HANDLED;
  416. }
  417. #endif /* HAVE_ACPI_LEGACY_ALARM */
  418. static int __init acpi_sleep_proc_init(void)
  419. {
  420. struct proc_dir_entry *entry = NULL;
  421. if (acpi_disabled)
  422. return 0;
  423. #ifdef CONFIG_ACPI_PROCFS
  424. /* 'sleep' [R/W] */
  425. entry =
  426. create_proc_entry("sleep", S_IFREG | S_IRUGO | S_IWUSR,
  427. acpi_root_dir);
  428. if (entry)
  429. entry->proc_fops = &acpi_system_sleep_fops;
  430. #endif /* CONFIG_ACPI_PROCFS */
  431. #ifdef HAVE_ACPI_LEGACY_ALARM
  432. /* 'alarm' [R/W] */
  433. entry =
  434. create_proc_entry("alarm", S_IFREG | S_IRUGO | S_IWUSR,
  435. acpi_root_dir);
  436. if (entry)
  437. entry->proc_fops = &acpi_system_alarm_fops;
  438. acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
  439. #endif /* HAVE_ACPI_LEGACY_ALARM */
  440. /* 'wakeup device' [R/W] */
  441. entry =
  442. create_proc_entry("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
  443. acpi_root_dir);
  444. if (entry)
  445. entry->proc_fops = &acpi_system_wakeup_device_fops;
  446. return 0;
  447. }
  448. late_initcall(acpi_sleep_proc_init);