proc.c 12 KB

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