]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/sparc64/kernel/power.c
[PATCH] introduce kernel_execve
[net-next-2.6.git] / arch / sparc64 / kernel / power.c
CommitLineData
1da177e4
LT
1/* $Id: power.c,v 1.10 2001/12/11 01:57:16 davem Exp $
2 * power.c: Power management driver.
3 *
4 * Copyright (C) 1999 David S. Miller (davem@redhat.com)
5 */
6
1da177e4
LT
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/init.h>
10#include <linux/sched.h>
11#include <linux/signal.h>
12#include <linux/delay.h>
13#include <linux/interrupt.h>
90bf8116 14#include <linux/pm.h>
67608567 15#include <linux/syscalls.h>
1da177e4
LT
16
17#include <asm/system.h>
1da177e4 18#include <asm/auxio.h>
abbce6e2
DM
19#include <asm/prom.h>
20#include <asm/of_device.h>
21#include <asm/io.h>
1da177e4 22
1da177e4
LT
23#include <linux/unistd.h>
24
25/*
26 * sysctl - toggle power-off restriction for serial console
27 * systems in machine_power_off()
28 */
29int scons_pwroff = 1;
30
31#ifdef CONFIG_PCI
abbce6e2 32#include <linux/pci.h>
1da177e4
LT
33static void __iomem *power_reg;
34
35static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
36static int button_pressed;
37
38static irqreturn_t power_handler(int irq, void *dev_id, struct pt_regs *regs)
39{
40 if (button_pressed == 0) {
41 button_pressed = 1;
42 wake_up(&powerd_wait);
43 }
44
45 /* FIXME: Check registers for status... */
46 return IRQ_HANDLED;
47}
48#endif /* CONFIG_PCI */
49
50extern void machine_halt(void);
51extern void machine_alt_power_off(void);
52static void (*poweroff_method)(void) = machine_alt_power_off;
53
54void machine_power_off(void)
55{
56 if (!serial_console || scons_pwroff) {
57#ifdef CONFIG_PCI
58 if (power_reg) {
59 /* Both register bits seem to have the
60 * same effect, so until I figure out
61 * what the difference is...
62 */
63 writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
64 } else
65#endif /* CONFIG_PCI */
66 if (poweroff_method != NULL) {
67 poweroff_method();
68 /* not reached */
69 }
70 }
71 machine_halt();
72}
73
90bf8116
DM
74void (*pm_power_off)(void) = machine_power_off;
75EXPORT_SYMBOL(pm_power_off);
76
1da177e4
LT
77#ifdef CONFIG_PCI
78static int powerd(void *__unused)
79{
80 static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
81 char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
82 DECLARE_WAITQUEUE(wait, current);
83
84 daemonize("powerd");
85
86 add_wait_queue(&powerd_wait, &wait);
87again:
88 for (;;) {
89 set_task_state(current, TASK_INTERRUPTIBLE);
90 if (button_pressed)
91 break;
92 flush_signals(current);
93 schedule();
94 }
95 __set_current_state(TASK_RUNNING);
96 remove_wait_queue(&powerd_wait, &wait);
97
98 /* Ok, down we go... */
99 button_pressed = 0;
67608567 100 if (kernel_execve("/sbin/shutdown", argv, envp) < 0) {
1da177e4
LT
101 printk("powerd: shutdown execution failed\n");
102 add_wait_queue(&powerd_wait, &wait);
103 goto again;
104 }
105 return 0;
106}
107
690c8fd3 108static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
1da177e4 109{
2256c13b 110 if (irq == PCI_IRQ_NONE)
1da177e4 111 return 0;
690c8fd3 112 if (!of_find_property(dp, "button", NULL))
1da177e4
LT
113 return 0;
114
115 return 1;
116}
117
abbce6e2 118static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
1da177e4 119{
abbce6e2
DM
120 struct resource *res = &op->resource[0];
121 unsigned int irq= op->irqs[0];
a2bd4fd1 122
abbce6e2
DM
123 power_reg = of_ioremap(res, 0, 0x4, "power");
124
125 printk("%s: Control reg at %lx ... ",
126 op->node->name, res->start);
a2bd4fd1 127
1da177e4 128 poweroff_method = machine_halt; /* able to use the standard halt */
a2bd4fd1 129
abbce6e2 130 if (has_button_interrupt(irq, op->node)) {
1da177e4
LT
131 if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
132 printk("Failed to start power daemon.\n");
abbce6e2 133 return 0;
1da177e4
LT
134 }
135 printk("powerd running.\n");
136
2256c13b 137 if (request_irq(irq,
00cde674 138 power_handler, 0, "power", NULL) < 0)
1da177e4
LT
139 printk("power: Error, cannot register IRQ handler.\n");
140 } else {
141 printk("not using powerd.\n");
142 }
abbce6e2
DM
143
144 return 0;
1da177e4 145}
a2bd4fd1
DM
146
147static struct of_device_id power_match[] = {
148 {
149 .name = "power",
150 },
151 {},
152};
153
abbce6e2 154static struct of_platform_driver power_driver = {
a2bd4fd1
DM
155 .name = "power",
156 .match_table = power_match,
abbce6e2 157 .probe = power_probe,
a2bd4fd1
DM
158};
159
160void __init power_init(void)
161{
abbce6e2 162 of_register_driver(&power_driver, &of_bus_type);
a2bd4fd1
DM
163 return;
164}
1da177e4 165#endif /* CONFIG_PCI */