1 /*
2 * acpi_system.c - ACPI System Driver ($Revision: 57 $)
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 *
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 *
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24 */
25
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/types.h>
31 #include <linux/spinlock.h>
32 #include <linux/poll.h>
33 #include <linux/delay.h>
34 #include <linux/interrupt.h>
35 #include <linux/sysrq.h>
36 #include <linux/compatmac.h>
37 #include <linux/proc_fs.h>
38 #include <linux/pm.h>
39 #include <asm/uaccess.h>
40 #include <asm/acpi.h>
41 #include <acpi/acpi_bus.h>
42 #include <acpi/acpi_drivers.h>
43 #include <linux/sched.h>
44
45 #ifdef CONFIG_ACPI_SLEEP
46 #include <linux/mc146818rtc.h>
47 #include <linux/irq.h>
48 #include <asm/hw_irq.h>
49
50 acpi_status acpi_system_save_state(u32);
51 #else
acpi_system_save_state(u32 state)52 static inline acpi_status acpi_system_save_state(u32 state)
53 {
54 return AE_OK;
55 }
56 #endif /* !CONFIG_ACPI_SLEEP */
57
58 #define _COMPONENT ACPI_SYSTEM_COMPONENT
59 ACPI_MODULE_NAME ("acpi_system")
60
61 #define PREFIX "ACPI: "
62
63 extern FADT_DESCRIPTOR acpi_fadt;
64
65 static int acpi_system_add (struct acpi_device *device);
66 static int acpi_system_remove (struct acpi_device *device, int type);
67
68 acpi_status acpi_suspend (u32 state);
69
70 static struct acpi_driver acpi_system_driver = {
71 .name = ACPI_SYSTEM_DRIVER_NAME,
72 .class = ACPI_SYSTEM_CLASS,
73 .ids = ACPI_SYSTEM_HID,
74 .ops = {
75 .add = acpi_system_add,
76 .remove = acpi_system_remove
77 },
78 };
79
80 struct acpi_system
81 {
82 acpi_handle handle;
83 u8 states[ACPI_S_STATE_COUNT];
84 };
85
86 /* Global vars for handling event proc entry */
87 static spinlock_t acpi_system_event_lock = SPIN_LOCK_UNLOCKED;
88 int event_is_open = 0;
89 extern struct list_head acpi_bus_event_list;
90 extern wait_queue_head_t acpi_bus_event_queue;
91
92 /* --------------------------------------------------------------------------
93 System Sleep
94 -------------------------------------------------------------------------- */
95
96 #ifdef CONFIG_PM
97
98 static void
acpi_power_off(void)99 acpi_power_off (void)
100 {
101 if (unlikely(in_interrupt()))
102 BUG();
103 /* Some SMP machines only can poweroff in boot CPU */
104 set_cpus_allowed(current, 1UL << cpu_logical_map(0));
105 acpi_enter_sleep_state_prep(ACPI_STATE_S5);
106 ACPI_DISABLE_IRQS();
107 acpi_enter_sleep_state(ACPI_STATE_S5);
108
109 printk(KERN_EMERG "ACPI: can not power off machine\n");
110 }
111
112 #endif /*CONFIG_PM*/
113
114
115 #ifdef CONFIG_ACPI_SLEEP
116
117 /**
118 * acpi_system_restore_state - OS-specific restoration of state
119 * @state: sleep state we're exiting
120 *
121 * Note that if we're coming back from S4, the memory image should have
122 * already been loaded from the disk and is already in place. (Otherwise how
123 * else would we be here?).
124 */
125 acpi_status
acpi_system_restore_state(u32 state)126 acpi_system_restore_state(
127 u32 state)
128 {
129 /*
130 * We should only be here if we're coming back from STR or STD.
131 * And, in the case of the latter, the memory image should have already
132 * been loaded from disk.
133 */
134 if (state > ACPI_STATE_S1) {
135 acpi_restore_state_mem();
136
137 /* Do _early_ resume for irqs. Required by
138 * ACPI specs.
139 */
140 /* TBD: call arch dependant reinitialization of the
141 * interrupts.
142 */
143 #ifdef CONFIG_X86
144 init_8259A(0);
145 #endif
146 /* wait for power to come back */
147 mdelay(1000);
148
149 }
150
151 /* Be really sure that irqs are disabled. */
152 ACPI_DISABLE_IRQS();
153
154 /* Wait a little again, just in case... */
155 mdelay(1000);
156
157 /* enable interrupts once again */
158 ACPI_ENABLE_IRQS();
159
160 /* turn all the devices back on */
161 if (state > ACPI_STATE_S1)
162 pm_send_all(PM_RESUME, (void *)0);
163
164 return AE_OK;
165 }
166
167
168 /**
169 * acpi_system_save_state - save OS specific state and power down devices
170 * @state: sleep state we're entering.
171 *
172 * This handles saving all context to memory, and possibly disk.
173 * First, we call to the device driver layer to save device state.
174 * Once we have that, we save whatevery processor and kernel state we
175 * need to memory.
176 * If we're entering S4, we then write the memory image to disk.
177 *
178 * Only then it is safe for us to power down devices, since we may need
179 * the disks and upstream buses to write to.
180 */
181 acpi_status
acpi_system_save_state(u32 state)182 acpi_system_save_state(
183 u32 state)
184 {
185 int error = 0;
186
187 /* Send notification to devices that they will be suspended.
188 * If any device or driver cannot make the transition, either up
189 * or down, we'll get an error back.
190 */
191 if (state > ACPI_STATE_S1) {
192 error = pm_send_all(PM_SAVE_STATE, (void *)3);
193 if (error)
194 return AE_ERROR;
195 }
196
197 if (state <= ACPI_STATE_S5) {
198 /* Tell devices to stop I/O and actually save their state.
199 * It is theoretically possible that something could fail,
200 * so handle that gracefully..
201 */
202 if (state > ACPI_STATE_S1 && state != ACPI_STATE_S5) {
203 error = pm_send_all(PM_SUSPEND, (void *)3);
204 if (error) {
205 /* Tell devices to restore state if they have
206 * it saved and to start taking I/O requests.
207 */
208 pm_send_all(PM_RESUME, (void *)0);
209 return error;
210 }
211 }
212
213 /* flush caches */
214 ACPI_FLUSH_CPU_CACHE();
215
216 /* Do arch specific saving of state. */
217 if (state > ACPI_STATE_S1) {
218 error = acpi_save_state_mem();
219
220 /* TBD: if no s4bios, write codes for
221 * acpi_save_state_disk()...
222 */
223 #if 0
224 if (!error && (state == ACPI_STATE_S4))
225 error = acpi_save_state_disk();
226 #endif
227 if (error) {
228 pm_send_all(PM_RESUME, (void *)0);
229 return error;
230 }
231 }
232 }
233 /* disable interrupts
234 * Note that acpi_suspend -- our caller -- will do this once we return.
235 * But, we want it done early, so we don't get any suprises during
236 * the device suspend sequence.
237 */
238 ACPI_DISABLE_IRQS();
239
240 /* Unconditionally turn off devices.
241 * Obvious if we enter a sleep state.
242 * If entering S5 (soft off), this should put devices in a
243 * quiescent state.
244 */
245
246 if (state > ACPI_STATE_S1) {
247 error = pm_send_all(PM_SUSPEND, (void *)3);
248
249 /* We're pretty screwed if we got an error from this.
250 * We try to recover by simply calling our own restore_state
251 * function; see above for definition.
252 *
253 * If it's S5 though, go through with it anyway..
254 */
255 if (error && state != ACPI_STATE_S5)
256 acpi_system_restore_state(state);
257 }
258 return error ? AE_ERROR : AE_OK;
259 }
260
261
262 /****************************************************************************
263 *
264 * FUNCTION: acpi_system_suspend
265 *
266 * PARAMETERS: %state: Sleep state to enter.
267 *
268 * RETURN: acpi_status, whether or not we successfully entered and
269 * exited sleep.
270 *
271 * DESCRIPTION: Perform OS-specific action to enter sleep state.
272 * This is the final step in going to sleep, per spec. If we
273 * know we're coming back (i.e. not entering S5), we save the
274 * processor flags. [ We'll have to save and restore them anyway,
275 * so we use the arch-agnostic save_flags and restore_flags
276 * here.] We then set the place to return to in arch-specific
277 * globals using arch_set_return_point. Finally, we call the
278 * ACPI function to write the proper values to I/O ports.
279 *
280 ****************************************************************************/
281
282 acpi_status
acpi_system_suspend(u32 state)283 acpi_system_suspend(
284 u32 state)
285 {
286 acpi_status status = AE_ERROR;
287 unsigned long flags = 0;
288
289 local_irq_save(flags);
290 /* kernel_fpu_begin(); */
291
292 switch (state) {
293 case ACPI_STATE_S1:
294 case ACPI_STATE_S5:
295 barrier();
296 status = acpi_enter_sleep_state(state);
297 break;
298 case ACPI_STATE_S4:
299 do_suspend_lowlevel_s4bios(0);
300 break;
301 }
302
303 /* kernel_fpu_end(); */
304 local_irq_restore(flags);
305
306 return status;
307 }
308
309
310
311 /**
312 * acpi_suspend - OS-agnostic system suspend/resume support (S? states)
313 * @state: state we're entering
314 *
315 */
316 acpi_status
acpi_suspend(u32 state)317 acpi_suspend (
318 u32 state)
319 {
320 acpi_status status;
321
322 /* only support S1 and S5 on kernel 2.4 */
323 if (state != ACPI_STATE_S1 && state != ACPI_STATE_S4
324 && state != ACPI_STATE_S5)
325 return AE_ERROR;
326
327
328 if (ACPI_STATE_S4 == state) {
329 /* For s4bios, we need a wakeup address. */
330 if (1 == acpi_gbl_FACS->S4bios_f &&
331 0 != acpi_gbl_FADT->smi_cmd) {
332 if (!acpi_wakeup_address)
333 return AE_ERROR;
334 acpi_set_firmware_waking_vector((acpi_physical_address) acpi_wakeup_address);
335 } else
336 /* We don't support S4 under 2.4. Give up */
337 return AE_ERROR;
338 }
339
340 status = acpi_system_save_state(state);
341 if (!ACPI_SUCCESS(status) && state != ACPI_STATE_S5)
342 return status;
343
344 acpi_enter_sleep_state_prep(state);
345
346 /* disable interrupts and flush caches */
347 ACPI_DISABLE_IRQS();
348 ACPI_FLUSH_CPU_CACHE();
349
350 /* perform OS-specific sleep actions */
351 status = acpi_system_suspend(state);
352
353 /* Even if we failed to go to sleep, all of the devices are in an suspended
354 * mode. So, we run these unconditionaly to make sure we have a usable system
355 * no matter what.
356 */
357 acpi_leave_sleep_state(state);
358 acpi_system_restore_state(state);
359
360 /* make sure interrupts are enabled */
361 ACPI_ENABLE_IRQS();
362
363 /* reset firmware waking vector */
364 acpi_set_firmware_waking_vector((acpi_physical_address) 0);
365
366 return status;
367 }
368
369 #endif /* CONFIG_ACPI_SLEEP */
370
371
372 /* --------------------------------------------------------------------------
373 FS Interface (/proc)
374 -------------------------------------------------------------------------- */
375
376 static int
acpi_system_read_info(char * page,char ** start,off_t off,int count,int * eof,void * data)377 acpi_system_read_info (
378 char *page,
379 char **start,
380 off_t off,
381 int count,
382 int *eof,
383 void *data)
384 {
385 struct acpi_system *system = (struct acpi_system *) data;
386 char *p = page;
387 int size = 0;
388 u32 i = 0;
389
390 ACPI_FUNCTION_TRACE("acpi_system_read_info");
391
392 if (!system || (off != 0))
393 goto end;
394
395 p += sprintf(p, "version: %x\n", ACPI_CA_VERSION);
396
397 p += sprintf(p, "states: ");
398 for (i=0; i<ACPI_S_STATE_COUNT; i++) {
399 if (system->states[i]) {
400 p += sprintf(p, "S%d ", i);
401 if (i == ACPI_STATE_S4 &&
402 acpi_gbl_FACS->S4bios_f &&
403 0 != acpi_gbl_FADT->smi_cmd)
404 p += sprintf(p, "S4Bios ");
405 }
406 }
407 p += sprintf(p, "\n");
408
409 end:
410 size = (p - page);
411 if (size <= off+count) *eof = 1;
412 *start = page + off;
413 size -= off;
414 if (size>count) size = count;
415 if (size<0) size = 0;
416
417 return_VALUE(size);
418 }
419
420 static int acpi_system_open_event(struct inode *inode, struct file *file);
421 static ssize_t acpi_system_read_event (struct file*, char*, size_t, loff_t*);
422 static int acpi_system_close_event(struct inode *inode, struct file *file);
423 static unsigned int acpi_system_poll_event(struct file *file, poll_table *wait);
424
425
426 static struct file_operations acpi_system_event_ops = {
427 .open = acpi_system_open_event,
428 .read = acpi_system_read_event,
429 .release = acpi_system_close_event,
430 .poll = acpi_system_poll_event,
431 };
432
433 static int
acpi_system_open_event(struct inode * inode,struct file * file)434 acpi_system_open_event(struct inode *inode, struct file *file)
435 {
436 spin_lock_irq (&acpi_system_event_lock);
437
438 if(event_is_open)
439 goto out_busy;
440
441 event_is_open = 1;
442
443 spin_unlock_irq (&acpi_system_event_lock);
444 return 0;
445
446 out_busy:
447 spin_unlock_irq (&acpi_system_event_lock);
448 return -EBUSY;
449 }
450
451 static ssize_t
acpi_system_read_event(struct file * file,char * buffer,size_t count,loff_t * ppos)452 acpi_system_read_event (
453 struct file *file,
454 char *buffer,
455 size_t count,
456 loff_t *ppos)
457 {
458 int result = 0;
459 struct acpi_bus_event event;
460 static char str[ACPI_MAX_STRING];
461 static int chars_remaining = 0;
462 static char *ptr;
463
464
465 ACPI_FUNCTION_TRACE("acpi_system_read_event");
466
467 if (!chars_remaining) {
468 memset(&event, 0, sizeof(struct acpi_bus_event));
469
470 if ((file->f_flags & O_NONBLOCK)
471 && (list_empty(&acpi_bus_event_list)))
472 return_VALUE(-EAGAIN);
473
474 result = acpi_bus_receive_event(&event);
475 if (result) {
476 return_VALUE(-EIO);
477 }
478
479 chars_remaining = sprintf(str, "%s %s %08x %08x\n",
480 event.device_class?event.device_class:"<unknown>",
481 event.bus_id?event.bus_id:"<unknown>",
482 event.type, event.data);
483 ptr = str;
484 }
485
486 if (chars_remaining < count) {
487 count = chars_remaining;
488 }
489
490 if (copy_to_user(buffer, ptr, count))
491 return_VALUE(-EFAULT);
492
493 *ppos += count;
494 chars_remaining -= count;
495 ptr += count;
496
497 return_VALUE(count);
498 }
499
500 static int
acpi_system_close_event(struct inode * inode,struct file * file)501 acpi_system_close_event(struct inode *inode, struct file *file)
502 {
503 spin_lock_irq (&acpi_system_event_lock);
504 event_is_open = 0;
505 spin_unlock_irq (&acpi_system_event_lock);
506 return 0;
507 }
508
509 static unsigned int
acpi_system_poll_event(struct file * file,poll_table * wait)510 acpi_system_poll_event(
511 struct file *file,
512 poll_table *wait)
513 {
514 poll_wait(file, &acpi_bus_event_queue, wait);
515 if (!list_empty(&acpi_bus_event_list))
516 return POLLIN | POLLRDNORM;
517 return 0;
518 }
519
520 static ssize_t acpi_system_read_dsdt (struct file*, char*, size_t, loff_t*);
521
522 static struct file_operations acpi_system_dsdt_ops = {
523 .read = acpi_system_read_dsdt,
524 };
525
526 static ssize_t
acpi_system_read_dsdt(struct file * file,char * buffer,size_t count,loff_t * ppos)527 acpi_system_read_dsdt (
528 struct file *file,
529 char *buffer,
530 size_t count,
531 loff_t *ppos)
532 {
533 acpi_status status = AE_OK;
534 struct acpi_buffer dsdt = {ACPI_ALLOCATE_BUFFER, NULL};
535 void *data = 0;
536 size_t size = 0;
537
538 ACPI_FUNCTION_TRACE("acpi_system_read_dsdt");
539
540 status = acpi_get_table(ACPI_TABLE_DSDT, 1, &dsdt);
541 if (ACPI_FAILURE(status))
542 return_VALUE(-ENODEV);
543
544 if (*ppos < dsdt.length) {
545 data = dsdt.pointer + file->f_pos;
546 size = dsdt.length - file->f_pos;
547 if (size > count)
548 size = count;
549 if (copy_to_user(buffer, data, size)) {
550 acpi_os_free(dsdt.pointer);
551 return_VALUE(-EFAULT);
552 }
553 }
554
555 acpi_os_free(dsdt.pointer);
556
557 *ppos += size;
558
559 return_VALUE(size);
560 }
561
562
563 static ssize_t acpi_system_read_fadt (struct file*, char*, size_t, loff_t*);
564
565 static struct file_operations acpi_system_fadt_ops = {
566 .read = acpi_system_read_fadt,
567 };
568
569 static ssize_t
acpi_system_read_fadt(struct file * file,char * buffer,size_t count,loff_t * ppos)570 acpi_system_read_fadt (
571 struct file *file,
572 char *buffer,
573 size_t count,
574 loff_t *ppos)
575 {
576 acpi_status status = AE_OK;
577 struct acpi_buffer fadt = {ACPI_ALLOCATE_BUFFER, NULL};
578 void *data = 0;
579 size_t size = 0;
580
581 ACPI_FUNCTION_TRACE("acpi_system_read_fadt");
582
583 status = acpi_get_table(ACPI_TABLE_FADT, 1, &fadt);
584 if (ACPI_FAILURE(status))
585 return_VALUE(-ENODEV);
586
587 if (*ppos < fadt.length) {
588 data = fadt.pointer + file->f_pos;
589 size = fadt.length - file->f_pos;
590 if (size > count)
591 size = count;
592 if (copy_to_user(buffer, data, size)) {
593 acpi_os_free(fadt.pointer);
594 return_VALUE(-EFAULT);
595 }
596 }
597
598 acpi_os_free(fadt.pointer);
599
600 *ppos += size;
601
602 return_VALUE(size);
603 }
604
605
606 #ifdef ACPI_DEBUG_OUTPUT
607
608 static int
acpi_system_read_debug(char * page,char ** start,off_t off,int count,int * eof,void * data)609 acpi_system_read_debug (
610 char *page,
611 char **start,
612 off_t off,
613 int count,
614 int *eof,
615 void *data)
616 {
617 char *p = page;
618 int size = 0;
619
620 if (off != 0)
621 goto end;
622
623 switch ((unsigned long) data) {
624 case 0:
625 p += sprintf(p, "0x%08x\n", acpi_dbg_layer);
626 break;
627 case 1:
628 p += sprintf(p, "0x%08x\n", acpi_dbg_level);
629 break;
630 default:
631 p += sprintf(p, "Invalid debug option\n");
632 break;
633 }
634
635 end:
636 size = (p - page);
637 if (size <= off+count) *eof = 1;
638 *start = page + off;
639 size -= off;
640 if (size>count) size = count;
641 if (size<0) size = 0;
642
643 return size;
644 }
645
646
647 static int
acpi_system_write_debug(struct file * file,const char * buffer,unsigned long count,void * data)648 acpi_system_write_debug (
649 struct file *file,
650 const char *buffer,
651 unsigned long count,
652 void *data)
653 {
654 char debug_string[12] = {'\0'};
655
656 ACPI_FUNCTION_TRACE("acpi_system_write_debug");
657
658 if (count > sizeof(debug_string) - 1)
659 return_VALUE(-EINVAL);
660
661 if (copy_from_user(debug_string, buffer, count))
662 return_VALUE(-EFAULT);
663
664 debug_string[count] = '\0';
665
666 switch ((unsigned long) data) {
667 case 0:
668 acpi_dbg_layer = simple_strtoul(debug_string, NULL, 0);
669 break;
670 case 1:
671 acpi_dbg_level = simple_strtoul(debug_string, NULL, 0);
672 break;
673 default:
674 return_VALUE(-EINVAL);
675 }
676
677 return_VALUE(count);
678 }
679
680 #endif /* ACPI_DEBUG_OUTPUT */
681
682
683 #ifdef CONFIG_ACPI_SLEEP
684
685 static int
acpi_system_read_sleep(char * page,char ** start,off_t off,int count,int * eof,void * data)686 acpi_system_read_sleep (
687 char *page,
688 char **start,
689 off_t off,
690 int count,
691 int *eof,
692 void *data)
693 {
694 struct acpi_system *system = (struct acpi_system *) data;
695 char *p = page;
696 int size;
697 int i;
698
699 ACPI_FUNCTION_TRACE("acpi_system_read_sleep");
700
701 if (!system || (off != 0))
702 goto end;
703
704 for (i = 0; i <= ACPI_STATE_S5; i++) {
705 if (system->states[i]) {
706 p += sprintf(p,"S%d ", i);
707 if (i == ACPI_STATE_S4 && acpi_gbl_FACS->S4bios_f &&
708 acpi_gbl_FADT->smi_cmd != 0)
709 p += sprintf(p, "S4Bios ");
710 }
711 }
712
713 p += sprintf(p, "\n");
714
715 end:
716 size = (p - page);
717 if (size <= off+count) *eof = 1;
718 *start = page + off;
719 size -= off;
720 if (size>count) size = count;
721 if (size<0) size = 0;
722
723 return_VALUE(size);
724 }
725
726
727 static int
acpi_system_write_sleep(struct file * file,const char * buffer,unsigned long count,void * data)728 acpi_system_write_sleep (
729 struct file *file,
730 const char *buffer,
731 unsigned long count,
732 void *data)
733 {
734 acpi_status status = AE_OK;
735 struct acpi_system *system = (struct acpi_system *) data;
736 char state_string[12] = {'\0'};
737 u32 state = 0;
738
739 ACPI_FUNCTION_TRACE("acpi_system_write_sleep");
740
741 if (!system || (count > sizeof(state_string) - 1))
742 return_VALUE(-EINVAL);
743
744 if (copy_from_user(state_string, buffer, count))
745 return_VALUE(-EFAULT);
746
747 state_string[count] = '\0';
748
749 state = simple_strtoul(state_string, NULL, 0);
750
751 if (state >= ACPI_S_STATE_COUNT || !system->states[state])
752 return_VALUE(-ENODEV);
753
754 /*
755 * If S4 is supported by the OS, then we should assume that
756 * echo 4b > /proc/acpi/sleep is for s4bios.
757 * Since we have only s4bios, we assume that acpi_suspend failed
758 * if no s4bios support.
759 */
760 status = acpi_suspend(state);
761 if (ACPI_FAILURE(status))
762 return_VALUE(-ENODEV);
763
764 return_VALUE(count);
765 }
766
767
768 static int
acpi_system_read_alarm(char * page,char ** start,off_t off,int count,int * eof,void * context)769 acpi_system_read_alarm (
770 char *page,
771 char **start,
772 off_t off,
773 int count,
774 int *eof,
775 void *context)
776 {
777 char *p = page;
778 int size = 0;
779 u32 sec, min, hr;
780 u32 day, mo, yr;
781
782 ACPI_FUNCTION_TRACE("acpi_system_read_alarm");
783
784 if (off != 0)
785 goto end;
786
787 spin_lock(&rtc_lock);
788
789 sec = CMOS_READ(RTC_SECONDS_ALARM);
790 min = CMOS_READ(RTC_MINUTES_ALARM);
791 hr = CMOS_READ(RTC_HOURS_ALARM);
792
793 #if 0 /* If we ever get an FACP with proper values... */
794 if (acpi_gbl_FADT->day_alrm)
795 day = CMOS_READ(acpi_gbl_FADT->day_alrm);
796 else
797 day = CMOS_READ(RTC_DAY_OF_MONTH);
798 if (acpi_gbl_FADT->mon_alrm)
799 mo = CMOS_READ(acpi_gbl_FADT->mon_alrm);
800 else
801 mo = CMOS_READ(RTC_MONTH);;
802 if (acpi_gbl_FADT->century)
803 yr = CMOS_READ(acpi_gbl_FADT->century) * 100 + CMOS_READ(RTC_YEAR);
804 else
805 yr = CMOS_READ(RTC_YEAR);
806 #else
807 day = CMOS_READ(RTC_DAY_OF_MONTH);
808 mo = CMOS_READ(RTC_MONTH);
809 yr = CMOS_READ(RTC_YEAR);
810 #endif
811
812 spin_unlock(&rtc_lock);
813
814 BCD_TO_BIN(sec);
815 BCD_TO_BIN(min);
816 BCD_TO_BIN(hr);
817 BCD_TO_BIN(day);
818 BCD_TO_BIN(mo);
819 BCD_TO_BIN(yr);
820
821 #if 0
822 /* we're trusting the FADT (see above)*/
823 #else
824 /* If we're not trusting the FADT, we should at least make it
825 * right for _this_ century... ehm, what is _this_ century?
826 *
827 * TBD:
828 * ASAP: find piece of code in the kernel, e.g. star tracker driver,
829 * which we can trust to determine the century correctly. Atom
830 * watch driver would be nice, too...
831 *
832 * if that has not happened, change for first release in 2050:
833 * if (yr<50)
834 * yr += 2100;
835 * else
836 * yr += 2000; // current line of code
837 *
838 * if that has not happened either, please do on 2099/12/31:23:59:59
839 * s/2000/2100
840 *
841 */
842 yr += 2000;
843 #endif
844
845 p += sprintf(p,"%4.4u-", yr);
846 p += (mo > 12) ? sprintf(p, "**-") : sprintf(p, "%2.2u-", mo);
847 p += (day > 31) ? sprintf(p, "** ") : sprintf(p, "%2.2u ", day);
848 p += (hr > 23) ? sprintf(p, "**:") : sprintf(p, "%2.2u:", hr);
849 p += (min > 59) ? sprintf(p, "**:") : sprintf(p, "%2.2u:", min);
850 p += (sec > 59) ? sprintf(p, "**\n") : sprintf(p, "%2.2u\n", sec);
851
852 end:
853 size = p - page;
854 if (size < count) *eof = 1;
855 else if (size > count) size = count;
856 if (size < 0) size = 0;
857 *start = page;
858
859 return_VALUE(size);
860 }
861
862
863 static int
get_date_field(char ** p,u32 * value)864 get_date_field (
865 char **p,
866 u32 *value)
867 {
868 char *next = NULL;
869 char *string_end = NULL;
870 int result = -EINVAL;
871
872 /*
873 * Try to find delimeter, only to insert null. The end of the
874 * string won't have one, but is still valid.
875 */
876 next = strpbrk(*p, "- :");
877 if (next)
878 *next++ = '\0';
879
880 *value = simple_strtoul(*p, &string_end, 10);
881
882 /* Signal success if we got a good digit */
883 if (string_end != *p)
884 result = 0;
885
886 if (next)
887 *p = next;
888
889 return result;
890 }
891
892
893 static int
acpi_system_write_alarm(struct file * file,const char * buffer,unsigned long count,void * data)894 acpi_system_write_alarm (
895 struct file *file,
896 const char *buffer,
897 unsigned long count,
898 void *data)
899 {
900 int result = 0;
901 char alarm_string[30] = {'\0'};
902 char *p = alarm_string;
903 u32 sec, min, hr, day, mo, yr;
904 int adjust = 0;
905 unsigned char rtc_control = 0;
906
907 ACPI_FUNCTION_TRACE("acpi_system_write_alarm");
908
909 if (count > sizeof(alarm_string) - 1)
910 return_VALUE(-EINVAL);
911
912 if (copy_from_user(alarm_string, buffer, count))
913 return_VALUE(-EFAULT);
914
915 alarm_string[count] = '\0';
916
917 /* check for time adjustment */
918 if (alarm_string[0] == '+') {
919 p++;
920 adjust = 1;
921 }
922
923 if ((result = get_date_field(&p, &yr)))
924 goto end;
925 if ((result = get_date_field(&p, &mo)))
926 goto end;
927 if ((result = get_date_field(&p, &day)))
928 goto end;
929 if ((result = get_date_field(&p, &hr)))
930 goto end;
931 if ((result = get_date_field(&p, &min)))
932 goto end;
933 if ((result = get_date_field(&p, &sec)))
934 goto end;
935
936 if (sec > 59) {
937 min += 1;
938 sec -= 60;
939 }
940 if (min > 59) {
941 hr += 1;
942 min -= 60;
943 }
944 if (hr > 23) {
945 day += 1;
946 hr -= 24;
947 }
948 if (day > 31) {
949 mo += 1;
950 day -= 31;
951 }
952 if (mo > 12) {
953 yr += 1;
954 mo -= 12;
955 }
956
957 spin_lock_irq(&rtc_lock);
958
959 rtc_control = CMOS_READ(RTC_CONTROL);
960 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
961 BIN_TO_BCD(yr);
962 BIN_TO_BCD(mo);
963 BIN_TO_BCD(day);
964 BIN_TO_BCD(hr);
965 BIN_TO_BCD(min);
966 BIN_TO_BCD(sec);
967 }
968
969 if (adjust) {
970 yr += CMOS_READ(RTC_YEAR);
971 mo += CMOS_READ(RTC_MONTH);
972 day += CMOS_READ(RTC_DAY_OF_MONTH);
973 hr += CMOS_READ(RTC_HOURS);
974 min += CMOS_READ(RTC_MINUTES);
975 sec += CMOS_READ(RTC_SECONDS);
976 }
977
978 spin_unlock_irq(&rtc_lock);
979
980 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
981 BCD_TO_BIN(yr);
982 BCD_TO_BIN(mo);
983 BCD_TO_BIN(day);
984 BCD_TO_BIN(hr);
985 BCD_TO_BIN(min);
986 BCD_TO_BIN(sec);
987 }
988
989 if (sec > 59) {
990 min++;
991 sec -= 60;
992 }
993 if (min > 59) {
994 hr++;
995 min -= 60;
996 }
997 if (hr > 23) {
998 day++;
999 hr -= 24;
1000 }
1001 if (day > 31) {
1002 mo++;
1003 day -= 31;
1004 }
1005 if (mo > 12) {
1006 yr++;
1007 mo -= 12;
1008 }
1009 if (!(rtc_control & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
1010 BIN_TO_BCD(yr);
1011 BIN_TO_BCD(mo);
1012 BIN_TO_BCD(day);
1013 BIN_TO_BCD(hr);
1014 BIN_TO_BCD(min);
1015 BIN_TO_BCD(sec);
1016 }
1017
1018 spin_lock_irq(&rtc_lock);
1019
1020 /* write the fields the rtc knows about */
1021 CMOS_WRITE(hr, RTC_HOURS_ALARM);
1022 CMOS_WRITE(min, RTC_MINUTES_ALARM);
1023 CMOS_WRITE(sec, RTC_SECONDS_ALARM);
1024
1025 /*
1026 * If the system supports an enhanced alarm it will have non-zero
1027 * offsets into the CMOS RAM here -- which for some reason are pointing
1028 * to the RTC area of memory.
1029 */
1030 #if 0
1031 if (acpi_gbl_FADT->day_alrm)
1032 CMOS_WRITE(day, acpi_gbl_FADT->day_alrm);
1033 if (acpi_gbl_FADT->mon_alrm)
1034 CMOS_WRITE(mo, acpi_gbl_FADT->mon_alrm);
1035 if (acpi_gbl_FADT->century)
1036 CMOS_WRITE(yr/100, acpi_gbl_FADT->century);
1037 #endif
1038 /* enable the rtc alarm interrupt */
1039 if (!(rtc_control & RTC_AIE)) {
1040 rtc_control |= RTC_AIE;
1041 CMOS_WRITE(rtc_control,RTC_CONTROL);
1042 CMOS_READ(RTC_INTR_FLAGS);
1043 }
1044
1045 spin_unlock_irq(&rtc_lock);
1046
1047 acpi_set_register(ACPI_BITREG_RT_CLOCK_ENABLE, 1, ACPI_MTX_LOCK);
1048
1049 file->f_pos += count;
1050
1051 result = 0;
1052 end:
1053 return_VALUE(result ? result : count);
1054 }
1055
1056 #endif /*CONFIG_ACPI_SLEEP*/
1057
1058
1059 static int
acpi_system_add_fs(struct acpi_device * device)1060 acpi_system_add_fs (
1061 struct acpi_device *device)
1062 {
1063 struct proc_dir_entry *entry = NULL;
1064
1065 ACPI_FUNCTION_TRACE("acpi_system_add_fs");
1066
1067 if (!device)
1068 return_VALUE(-EINVAL);
1069
1070 /* 'info' [R] */
1071 entry = create_proc_entry(ACPI_SYSTEM_FILE_INFO,
1072 S_IRUGO, acpi_device_dir(device));
1073 if (!entry)
1074 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1075 "Unable to create '%s' fs entry\n",
1076 ACPI_SYSTEM_FILE_INFO));
1077 else {
1078 entry->read_proc = acpi_system_read_info;
1079 entry->data = acpi_driver_data(device);
1080 }
1081
1082 /* 'dsdt' [R] */
1083 entry = create_proc_entry(ACPI_SYSTEM_FILE_DSDT,
1084 S_IRUSR, acpi_device_dir(device));
1085 if (!entry)
1086 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1087 "Unable to create '%s' fs entry\n",
1088 ACPI_SYSTEM_FILE_DSDT));
1089 else
1090 entry->proc_fops = &acpi_system_dsdt_ops;
1091
1092 /* 'fadt' [R] */
1093 entry = create_proc_entry(ACPI_SYSTEM_FILE_FADT,
1094 S_IRUSR, acpi_device_dir(device));
1095 if (!entry)
1096 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1097 "Unable to create '%s' fs entry\n",
1098 ACPI_SYSTEM_FILE_FADT));
1099 else
1100 entry->proc_fops = &acpi_system_fadt_ops;
1101
1102 /* 'event' [R] */
1103 entry = create_proc_entry(ACPI_SYSTEM_FILE_EVENT,
1104 S_IRUSR, acpi_device_dir(device));
1105 if (!entry)
1106 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1107 "Unable to create '%s' fs entry\n",
1108 ACPI_SYSTEM_FILE_EVENT));
1109 else
1110 entry->proc_fops = &acpi_system_event_ops;
1111
1112 #ifdef CONFIG_ACPI_SLEEP
1113
1114 /* 'sleep' [R/W]*/
1115 entry = create_proc_entry(ACPI_SYSTEM_FILE_SLEEP,
1116 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
1117 if (!entry)
1118 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1119 "Unable to create '%s' fs entry\n",
1120 ACPI_SYSTEM_FILE_SLEEP));
1121 else {
1122 entry->read_proc = acpi_system_read_sleep;
1123 entry->write_proc = acpi_system_write_sleep;
1124 entry->data = acpi_driver_data(device);
1125 }
1126
1127 /* 'alarm' [R/W] */
1128 entry = create_proc_entry(ACPI_SYSTEM_FILE_ALARM,
1129 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
1130 if (!entry)
1131 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1132 "Unable to create '%s' fs entry\n",
1133 ACPI_SYSTEM_FILE_ALARM));
1134 else {
1135 entry->read_proc = acpi_system_read_alarm;
1136 entry->write_proc = acpi_system_write_alarm;
1137 entry->data = acpi_driver_data(device);
1138 }
1139
1140 #endif /*CONFIG_ACPI_SLEEP*/
1141
1142 #ifdef ACPI_DEBUG_OUTPUT
1143
1144 /* 'debug_layer' [R/W] */
1145 entry = create_proc_entry(ACPI_SYSTEM_FILE_DEBUG_LAYER,
1146 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
1147 if (!entry)
1148 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1149 "Unable to create '%s' fs entry\n",
1150 ACPI_SYSTEM_FILE_DEBUG_LAYER));
1151 else {
1152 entry->read_proc = acpi_system_read_debug;
1153 entry->write_proc = acpi_system_write_debug;
1154 entry->data = (void *) 0;
1155 }
1156
1157 /* 'debug_level' [R/W] */
1158 entry = create_proc_entry(ACPI_SYSTEM_FILE_DEBUG_LEVEL,
1159 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
1160 if (!entry)
1161 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1162 "Unable to create '%s' fs entry\n",
1163 ACPI_SYSTEM_FILE_DEBUG_LEVEL));
1164 else {
1165 entry->read_proc = acpi_system_read_debug;
1166 entry->write_proc = acpi_system_write_debug;
1167 entry->data = (void *) 1;
1168 }
1169
1170 #endif /*ACPI_DEBUG_OUTPUT */
1171
1172 return_VALUE(0);
1173 }
1174
1175
1176 static int
acpi_system_remove_fs(struct acpi_device * device)1177 acpi_system_remove_fs (
1178 struct acpi_device *device)
1179 {
1180 ACPI_FUNCTION_TRACE("acpi_system_remove_fs");
1181
1182 if (!device)
1183 return_VALUE(-EINVAL);
1184
1185 remove_proc_entry(ACPI_SYSTEM_FILE_INFO, acpi_device_dir(device));
1186 remove_proc_entry(ACPI_SYSTEM_FILE_DSDT, acpi_device_dir(device));
1187 remove_proc_entry(ACPI_SYSTEM_FILE_EVENT, acpi_device_dir(device));
1188 #ifdef CONFIG_ACPI_SLEEP
1189 remove_proc_entry(ACPI_SYSTEM_FILE_SLEEP, acpi_device_dir(device));
1190 remove_proc_entry(ACPI_SYSTEM_FILE_ALARM, acpi_device_dir(device));
1191 #endif
1192 #ifdef ACPI_DEBUG_OUTPUT
1193 remove_proc_entry(ACPI_SYSTEM_FILE_DEBUG_LAYER,
1194 acpi_device_dir(device));
1195 remove_proc_entry(ACPI_SYSTEM_FILE_DEBUG_LEVEL,
1196 acpi_device_dir(device));
1197 #endif
1198
1199 return_VALUE(0);
1200 }
1201
1202
1203 /* --------------------------------------------------------------------------
1204 Driver Interface
1205 -------------------------------------------------------------------------- */
1206
1207 #if defined(CONFIG_MAGIC_SYSRQ) && defined(CONFIG_PM)
1208
1209 static int po_cb_active;
1210
acpi_po_tramp(void * x)1211 static void acpi_po_tramp(void *x)
1212 {
1213 acpi_power_off();
1214 }
1215
1216 /* Simple wrapper calling power down function. */
acpi_sysrq_power_off(int key,struct pt_regs * pt_regs,struct kbd_struct * kbd,struct tty_struct * tty)1217 static void acpi_sysrq_power_off(int key, struct pt_regs *pt_regs,
1218 struct kbd_struct *kbd, struct tty_struct *tty)
1219 {
1220 static struct tq_struct tq = { .routine = acpi_po_tramp };
1221 if (po_cb_active++)
1222 return;
1223 schedule_task(&tq);
1224 }
1225
1226 struct sysrq_key_op sysrq_acpi_poweroff_op = {
1227 .handler = &acpi_sysrq_power_off,
1228 .help_msg = "Off",
1229 .action_msg = "Power Off\n"
1230 };
1231
1232 #endif /* CONFIG_MAGIC_SYSRQ */
1233
1234 static int
acpi_system_add(struct acpi_device * device)1235 acpi_system_add (
1236 struct acpi_device *device)
1237 {
1238 int result = 0;
1239 acpi_status status = AE_OK;
1240 struct acpi_system *system = NULL;
1241 u8 i = 0;
1242
1243 ACPI_FUNCTION_TRACE("acpi_system_add");
1244
1245 if (!device)
1246 return_VALUE(-EINVAL);
1247
1248 system = kmalloc(sizeof(struct acpi_system), GFP_KERNEL);
1249 if (!system)
1250 return_VALUE(-ENOMEM);
1251 memset(system, 0, sizeof(struct acpi_system));
1252
1253 system->handle = device->handle;
1254 sprintf(acpi_device_name(device), "%s", ACPI_SYSTEM_DEVICE_NAME);
1255 sprintf(acpi_device_class(device), "%s", ACPI_SYSTEM_CLASS);
1256 acpi_driver_data(device) = system;
1257
1258 result = acpi_system_add_fs(device);
1259 if (result)
1260 goto end;
1261
1262 printk(KERN_INFO PREFIX "%s [%s] (supports",
1263 acpi_device_name(device), acpi_device_bid(device));
1264 for (i=0; i<ACPI_S_STATE_COUNT; i++) {
1265 u8 type_a, type_b;
1266 status = acpi_get_sleep_type_data(i, &type_a, &type_b);
1267 switch (i) {
1268 case ACPI_STATE_S4:
1269 if (acpi_gbl_FACS->S4bios_f &&
1270 0 != acpi_gbl_FADT->smi_cmd) {
1271 printk(" S4bios");
1272 system->states[i] = 1;
1273 }
1274 /* no break */
1275 default:
1276 if (ACPI_SUCCESS(status)) {
1277 system->states[i] = 1;
1278 printk(" S%d", i);
1279 }
1280 }
1281 }
1282 printk(")\n");
1283
1284 #ifdef CONFIG_PM
1285 /* Install the soft-off (S5) handler. */
1286 if (system->states[ACPI_STATE_S5]) {
1287 pm_power_off = acpi_power_off;
1288 register_sysrq_key('o', &sysrq_acpi_poweroff_op);
1289 }
1290 #endif
1291
1292 end:
1293 if (result)
1294 kfree(system);
1295
1296 return_VALUE(result);
1297 }
1298
1299
1300 static int
acpi_system_remove(struct acpi_device * device,int type)1301 acpi_system_remove (
1302 struct acpi_device *device,
1303 int type)
1304 {
1305 struct acpi_system *system = NULL;
1306
1307 ACPI_FUNCTION_TRACE("acpi_system_remove");
1308
1309 if (!device || !acpi_driver_data(device))
1310 return_VALUE(-EINVAL);
1311
1312 system = (struct acpi_system *) acpi_driver_data(device);
1313
1314 #ifdef CONFIG_PM
1315 /* Remove the soft-off (S5) handler. */
1316 if (system->states[ACPI_STATE_S5]) {
1317 unregister_sysrq_key('o', &sysrq_acpi_poweroff_op);
1318 pm_power_off = NULL;
1319 }
1320 #endif
1321
1322 acpi_system_remove_fs(device);
1323
1324 kfree(system);
1325
1326 return 0;
1327 }
1328
1329
1330 int __init
acpi_system_init(void)1331 acpi_system_init (void)
1332 {
1333 int result = 0;
1334
1335 ACPI_FUNCTION_TRACE("acpi_system_init");
1336
1337 result = acpi_bus_register_driver(&acpi_system_driver);
1338 if (result < 0)
1339 return_VALUE(-ENODEV);
1340
1341 return_VALUE(0);
1342 }
1343
1344
1345 void __exit
acpi_system_exit(void)1346 acpi_system_exit (void)
1347 {
1348 ACPI_FUNCTION_TRACE("acpi_system_exit");
1349 acpi_bus_unregister_driver(&acpi_system_driver);
1350 return_VOID;
1351 }
1352