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