proc.c 12 KB

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