proc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. cmos_bcd_write(yr / 100, acpi_gbl_FADT.century, rtc_control);
  268. /* enable the rtc alarm interrupt */
  269. rtc_control |= RTC_AIE;
  270. CMOS_WRITE(rtc_control, RTC_CONTROL);
  271. CMOS_READ(RTC_INTR_FLAGS);
  272. spin_unlock_irq(&rtc_lock);
  273. acpi_clear_event(ACPI_EVENT_RTC);
  274. acpi_enable_event(ACPI_EVENT_RTC, 0);
  275. *ppos += count;
  276. result = 0;
  277. end:
  278. return_VALUE(result ? result : count);
  279. }
  280. #endif /* HAVE_ACPI_LEGACY_ALARM */
  281. extern struct list_head acpi_wakeup_device_list;
  282. extern spinlock_t acpi_device_lock;
  283. static int
  284. acpi_system_wakeup_device_seq_show(struct seq_file *seq, void *offset)
  285. {
  286. struct list_head *node, *next;
  287. seq_printf(seq, "Device\tS-state\t Status Sysfs node\n");
  288. spin_lock(&acpi_device_lock);
  289. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  290. struct acpi_device *dev =
  291. container_of(node, struct acpi_device, wakeup_list);
  292. struct device *ldev;
  293. if (!dev->wakeup.flags.valid)
  294. continue;
  295. spin_unlock(&acpi_device_lock);
  296. ldev = acpi_get_physical_device(dev->handle);
  297. seq_printf(seq, "%s\t S%d\t%c%-8s ",
  298. dev->pnp.bus_id,
  299. (u32) dev->wakeup.sleep_state,
  300. dev->wakeup.flags.run_wake ? '*' : ' ',
  301. dev->wakeup.state.enabled ? "enabled" : "disabled");
  302. if (ldev)
  303. seq_printf(seq, "%s:%s",
  304. ldev->bus ? ldev->bus->name : "no-bus",
  305. ldev->bus_id);
  306. seq_printf(seq, "\n");
  307. put_device(ldev);
  308. spin_lock(&acpi_device_lock);
  309. }
  310. spin_unlock(&acpi_device_lock);
  311. return 0;
  312. }
  313. static ssize_t
  314. acpi_system_write_wakeup_device(struct file *file,
  315. const char __user * buffer,
  316. size_t count, loff_t * ppos)
  317. {
  318. struct list_head *node, *next;
  319. char strbuf[5];
  320. char str[5] = "";
  321. int len = count;
  322. struct acpi_device *found_dev = NULL;
  323. if (len > 4)
  324. len = 4;
  325. if (copy_from_user(strbuf, buffer, len))
  326. return -EFAULT;
  327. strbuf[len] = '\0';
  328. sscanf(strbuf, "%s", str);
  329. spin_lock(&acpi_device_lock);
  330. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  331. struct acpi_device *dev =
  332. container_of(node, struct acpi_device, wakeup_list);
  333. if (!dev->wakeup.flags.valid)
  334. continue;
  335. if (!strncmp(dev->pnp.bus_id, str, 4)) {
  336. dev->wakeup.state.enabled =
  337. dev->wakeup.state.enabled ? 0 : 1;
  338. found_dev = dev;
  339. break;
  340. }
  341. }
  342. if (found_dev) {
  343. list_for_each_safe(node, next, &acpi_wakeup_device_list) {
  344. struct acpi_device *dev = container_of(node,
  345. struct
  346. acpi_device,
  347. wakeup_list);
  348. if ((dev != found_dev) &&
  349. (dev->wakeup.gpe_number ==
  350. found_dev->wakeup.gpe_number)
  351. && (dev->wakeup.gpe_device ==
  352. found_dev->wakeup.gpe_device)) {
  353. printk(KERN_WARNING
  354. "ACPI: '%s' and '%s' have the same GPE, "
  355. "can't disable/enable one seperately\n",
  356. dev->pnp.bus_id, found_dev->pnp.bus_id);
  357. dev->wakeup.state.enabled =
  358. found_dev->wakeup.state.enabled;
  359. }
  360. }
  361. }
  362. spin_unlock(&acpi_device_lock);
  363. return count;
  364. }
  365. static int
  366. acpi_system_wakeup_device_open_fs(struct inode *inode, struct file *file)
  367. {
  368. return single_open(file, acpi_system_wakeup_device_seq_show,
  369. PDE(inode)->data);
  370. }
  371. static const struct file_operations acpi_system_wakeup_device_fops = {
  372. .open = acpi_system_wakeup_device_open_fs,
  373. .read = seq_read,
  374. .write = acpi_system_write_wakeup_device,
  375. .llseek = seq_lseek,
  376. .release = single_release,
  377. };
  378. #ifdef CONFIG_ACPI_PROCFS
  379. static const struct file_operations acpi_system_sleep_fops = {
  380. .open = acpi_system_sleep_open_fs,
  381. .read = seq_read,
  382. .write = acpi_system_write_sleep,
  383. .llseek = seq_lseek,
  384. .release = single_release,
  385. };
  386. #endif /* CONFIG_ACPI_PROCFS */
  387. #ifdef HAVE_ACPI_LEGACY_ALARM
  388. static const struct file_operations acpi_system_alarm_fops = {
  389. .open = acpi_system_alarm_open_fs,
  390. .read = seq_read,
  391. .write = acpi_system_write_alarm,
  392. .llseek = seq_lseek,
  393. .release = single_release,
  394. };
  395. static u32 rtc_handler(void *context)
  396. {
  397. acpi_clear_event(ACPI_EVENT_RTC);
  398. acpi_disable_event(ACPI_EVENT_RTC, 0);
  399. return ACPI_INTERRUPT_HANDLED;
  400. }
  401. #endif /* HAVE_ACPI_LEGACY_ALARM */
  402. static int __init acpi_sleep_proc_init(void)
  403. {
  404. struct proc_dir_entry *entry = NULL;
  405. if (acpi_disabled)
  406. return 0;
  407. #ifdef CONFIG_ACPI_PROCFS
  408. /* 'sleep' [R/W] */
  409. entry =
  410. create_proc_entry("sleep", S_IFREG | S_IRUGO | S_IWUSR,
  411. acpi_root_dir);
  412. if (entry)
  413. entry->proc_fops = &acpi_system_sleep_fops;
  414. #endif /* CONFIG_ACPI_PROCFS */
  415. #ifdef HAVE_ACPI_LEGACY_ALARM
  416. /* 'alarm' [R/W] */
  417. entry =
  418. create_proc_entry("alarm", S_IFREG | S_IRUGO | S_IWUSR,
  419. acpi_root_dir);
  420. if (entry)
  421. entry->proc_fops = &acpi_system_alarm_fops;
  422. acpi_install_fixed_event_handler(ACPI_EVENT_RTC, rtc_handler, NULL);
  423. #endif /* HAVE_ACPI_LEGACY_ALARM */
  424. /* 'wakeup device' [R/W] */
  425. entry =
  426. create_proc_entry("wakeup", S_IFREG | S_IRUGO | S_IWUSR,
  427. acpi_root_dir);
  428. if (entry)
  429. entry->proc_fops = &acpi_system_wakeup_device_fops;
  430. return 0;
  431. }
  432. late_initcall(acpi_sleep_proc_init);