proc.c 12 KB

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