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