proc.c 12 KB

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