proc.c 11 KB

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