proc.c 11 KB

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