blob: 766332e4559212aa9c253ddf3723e65367487b96 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * acpi_bus.c - ACPI Bus Driver ($Revision: 80 $)
3 *
4 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
5 *
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 */
24
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/ioport.h>
Randy Dunlapd568df82006-07-12 01:47:00 -040028#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/list.h>
30#include <linux/sched.h>
31#include <linux/pm.h>
Jeff Garzikbca73e42005-11-13 16:06:25 -080032#include <linux/pm_legacy.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/device.h>
34#include <linux/proc_fs.h>
35#ifdef CONFIG_X86
36#include <asm/mpspec.h>
37#endif
38#include <acpi/acpi_bus.h>
39#include <acpi/acpi_drivers.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define _COMPONENT ACPI_BUS_COMPONENT
Len Brown4be44fc2005-08-05 00:44:28 -040042ACPI_MODULE_NAME("acpi_bus")
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#ifdef CONFIG_X86
44extern void __init acpi_pic_sci_set_trigger(unsigned int irq, u16 trigger);
45#endif
46
Bob Moore793c2382006-03-31 00:00:00 -050047struct fadt_descriptor acpi_fadt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048EXPORT_SYMBOL(acpi_fadt);
49
Len Brown4be44fc2005-08-05 00:44:28 -040050struct acpi_device *acpi_root;
51struct proc_dir_entry *acpi_root_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052EXPORT_SYMBOL(acpi_root_dir);
53
54#define STRUCT_TO_INT(s) (*((int*)&s))
55
56/* --------------------------------------------------------------------------
57 Device Management
58 -------------------------------------------------------------------------- */
59
Len Brown4be44fc2005-08-05 00:44:28 -040060int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Len Brown4be44fc2005-08-05 00:44:28 -040062 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -040066 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 /* TBD: Support fixed-feature devices */
69
Len Brown4be44fc2005-08-05 00:44:28 -040070 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (ACPI_FAILURE(status) || !*device) {
Len Brown9805cb72006-07-25 13:30:57 -040072 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
73 handle));
Patrick Mocheld550d982006-06-27 00:41:40 -040074 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76
Patrick Mocheld550d982006-06-27 00:41:40 -040077 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
Len Brown4be44fc2005-08-05 00:44:28 -040079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080EXPORT_SYMBOL(acpi_bus_get_device);
81
Len Brown4be44fc2005-08-05 00:44:28 -040082int acpi_bus_get_status(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070083{
Len Brown4be44fc2005-08-05 00:44:28 -040084 acpi_status status = AE_OK;
85 unsigned long sta = 0;
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -040089 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 /*
92 * Evaluate _STA if present.
93 */
94 if (device->flags.dynamic_status) {
Len Brown4be44fc2005-08-05 00:44:28 -040095 status =
96 acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -040098 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -040099 STRUCT_TO_INT(device->status) = (int)sta;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101
102 /*
103 * Otherwise we assume the status of our parent (unless we don't
104 * have one, in which case status is implied).
105 */
106 else if (device->parent)
107 device->status = device->parent->status;
108 else
109 STRUCT_TO_INT(device->status) = 0x0F;
110
111 if (device->status.functional && !device->status.present) {
112 printk(KERN_WARNING PREFIX "Device [%s] status [%08x]: "
Len Brown4be44fc2005-08-05 00:44:28 -0400113 "functional but not present; setting present\n",
114 device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 device->status.present = 1;
116 }
117
Len Brown4be44fc2005-08-05 00:44:28 -0400118 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
119 device->pnp.bus_id,
120 (u32) STRUCT_TO_INT(device->status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Patrick Mocheld550d982006-06-27 00:41:40 -0400122 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Len Brown4be44fc2005-08-05 00:44:28 -0400125EXPORT_SYMBOL(acpi_bus_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127/* --------------------------------------------------------------------------
128 Power Management
129 -------------------------------------------------------------------------- */
130
Len Brown4be44fc2005-08-05 00:44:28 -0400131int acpi_bus_get_power(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Len Brown4be44fc2005-08-05 00:44:28 -0400133 int result = 0;
134 acpi_status status = 0;
135 struct acpi_device *device = NULL;
136 unsigned long psc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 result = acpi_bus_get_device(handle, &device);
140 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400141 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 *state = ACPI_STATE_UNKNOWN;
144
145 if (!device->flags.power_manageable) {
146 /* TBD: Non-recursive algorithm for walking up hierarchy */
147 if (device->parent)
148 *state = device->parent->power.state;
149 else
150 *state = ACPI_STATE_D0;
Len Brown4be44fc2005-08-05 00:44:28 -0400151 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 /*
153 * Get the device's power state either directly (via _PSC) or
154 * indirectly (via power resources).
155 */
156 if (device->power.flags.explicit_get) {
Len Brown4be44fc2005-08-05 00:44:28 -0400157 status = acpi_evaluate_integer(device->handle, "_PSC",
158 NULL, &psc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400160 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400161 device->power.state = (int)psc;
162 } else if (device->power.flags.power_resources) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 result = acpi_power_get_inferred_state(device);
164 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400165 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 }
167
168 *state = device->power.state;
169 }
170
171 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400172 device->pnp.bus_id, device->power.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Patrick Mocheld550d982006-06-27 00:41:40 -0400174 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
Len Brown4be44fc2005-08-05 00:44:28 -0400176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177EXPORT_SYMBOL(acpi_bus_get_power);
178
Len Brown4be44fc2005-08-05 00:44:28 -0400179int acpi_bus_set_power(acpi_handle handle, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Len Brown4be44fc2005-08-05 00:44:28 -0400181 int result = 0;
182 acpi_status status = AE_OK;
183 struct acpi_device *device = NULL;
184 char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 result = acpi_bus_get_device(handle, &device);
188 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400189 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
Patrick Mocheld550d982006-06-27 00:41:40 -0400192 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 /* Make sure this is a valid target state */
195
196 if (!device->flags.power_manageable) {
Len Brown9805cb72006-07-25 13:30:57 -0400197 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device `[%s]' is not power manageable\n",
Len Brownaf4f9492006-07-09 16:33:26 -0400198 device->kobj.name));
Patrick Mocheld550d982006-06-27 00:41:40 -0400199 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
David Shaohua Lib9131002005-03-19 00:16:18 -0500201 /*
202 * Get device's current power state if it's unknown
203 * This means device power state isn't initialized or previous setting failed
204 */
Konstantin Karasyov0feabb02006-05-08 00:00:00 -0400205 if (!device->flags.force_power_state) {
206 if (device->power.state == ACPI_STATE_UNKNOWN)
207 acpi_bus_get_power(device->handle, &device->power.state);
208 if (state == device->power.state) {
209 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
210 state));
Patrick Mocheld550d982006-06-27 00:41:40 -0400211 return 0;
Konstantin Karasyov0feabb02006-05-08 00:00:00 -0400212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 if (!device->power.states[state].flags.valid) {
Len Browncece9292006-06-26 23:04:31 -0400215 printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
Patrick Mocheld550d982006-06-27 00:41:40 -0400216 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 if (device->parent && (state < device->parent->power.state)) {
Len Browncece9292006-06-26 23:04:31 -0400219 printk(KERN_WARNING PREFIX
Thomas Renningera6fc6722006-06-26 23:58:43 -0400220 "Cannot set device to a higher-powered"
Len Browncece9292006-06-26 23:04:31 -0400221 " state than parent\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400222 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
224
225 /*
226 * Transition Power
227 * ----------------
228 * On transitions to a high-powered state we first apply power (via
229 * power resources) then evalute _PSx. Conversly for transitions to
230 * a lower-powered state.
David Shaohua Lib9131002005-03-19 00:16:18 -0500231 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (state < device->power.state) {
233 if (device->power.flags.power_resources) {
234 result = acpi_power_transition(device, state);
235 if (result)
236 goto end;
237 }
238 if (device->power.states[state].flags.explicit_set) {
Len Brown4be44fc2005-08-05 00:44:28 -0400239 status = acpi_evaluate_object(device->handle,
240 object_name, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (ACPI_FAILURE(status)) {
242 result = -ENODEV;
243 goto end;
244 }
245 }
Len Brown4be44fc2005-08-05 00:44:28 -0400246 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 if (device->power.states[state].flags.explicit_set) {
Len Brown4be44fc2005-08-05 00:44:28 -0400248 status = acpi_evaluate_object(device->handle,
249 object_name, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (ACPI_FAILURE(status)) {
251 result = -ENODEV;
252 goto end;
253 }
254 }
255 if (device->power.flags.power_resources) {
256 result = acpi_power_transition(device, state);
257 if (result)
258 goto end;
259 }
260 }
261
Len Brown4be44fc2005-08-05 00:44:28 -0400262 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (result)
Len Browncece9292006-06-26 23:04:31 -0400264 printk(KERN_WARNING PREFIX
265 "Transitioning device [%s] to D%d\n",
266 device->pnp.bus_id, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 else
Len Brown4be44fc2005-08-05 00:44:28 -0400268 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
269 "Device [%s] transitioned to D%d\n",
270 device->pnp.bus_id, state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Patrick Mocheld550d982006-06-27 00:41:40 -0400272 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Len Brown4be44fc2005-08-05 00:44:28 -0400274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275EXPORT_SYMBOL(acpi_bus_set_power);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/* --------------------------------------------------------------------------
278 Event Management
279 -------------------------------------------------------------------------- */
280
281static DEFINE_SPINLOCK(acpi_bus_event_lock);
282
283LIST_HEAD(acpi_bus_event_list);
284DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
285
Len Brown4be44fc2005-08-05 00:44:28 -0400286extern int event_is_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Len Brown4be44fc2005-08-05 00:44:28 -0400288int acpi_bus_generate_event(struct acpi_device *device, u8 type, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Len Brown4be44fc2005-08-05 00:44:28 -0400290 struct acpi_bus_event *event = NULL;
291 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
297 /* drop event on the floor if no one's listening */
298 if (!event_is_open)
Patrick Mocheld550d982006-06-27 00:41:40 -0400299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
302 if (!event)
Patrick Mocheld550d982006-06-27 00:41:40 -0400303 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 strcpy(event->device_class, device->pnp.device_class);
306 strcpy(event->bus_id, device->pnp.bus_id);
307 event->type = type;
308 event->data = data;
309
310 spin_lock_irqsave(&acpi_bus_event_lock, flags);
311 list_add_tail(&event->node, &acpi_bus_event_list);
312 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
313
314 wake_up_interruptible(&acpi_bus_event_queue);
315
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
Len Brown4be44fc2005-08-05 00:44:28 -0400318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319EXPORT_SYMBOL(acpi_bus_generate_event);
320
Len Brown4be44fc2005-08-05 00:44:28 -0400321int acpi_bus_receive_event(struct acpi_bus_event *event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Len Brown4be44fc2005-08-05 00:44:28 -0400323 unsigned long flags = 0;
324 struct acpi_bus_event *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 DECLARE_WAITQUEUE(wait, current);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 if (!event)
Patrick Mocheld550d982006-06-27 00:41:40 -0400330 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (list_empty(&acpi_bus_event_list)) {
333
334 set_current_state(TASK_INTERRUPTIBLE);
335 add_wait_queue(&acpi_bus_event_queue, &wait);
336
337 if (list_empty(&acpi_bus_event_list))
338 schedule();
339
340 remove_wait_queue(&acpi_bus_event_queue, &wait);
341 set_current_state(TASK_RUNNING);
342
343 if (signal_pending(current))
Patrick Mocheld550d982006-06-27 00:41:40 -0400344 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
346
347 spin_lock_irqsave(&acpi_bus_event_lock, flags);
Len Brown4be44fc2005-08-05 00:44:28 -0400348 entry =
349 list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (entry)
351 list_del(&entry->node);
352 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
353
354 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400355 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 memcpy(event, entry, sizeof(struct acpi_bus_event));
358
359 kfree(entry);
360
Patrick Mocheld550d982006-06-27 00:41:40 -0400361 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Len Brown4be44fc2005-08-05 00:44:28 -0400364EXPORT_SYMBOL(acpi_bus_receive_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366/* --------------------------------------------------------------------------
367 Notification Handling
368 -------------------------------------------------------------------------- */
369
370static int
Len Brown4be44fc2005-08-05 00:44:28 -0400371acpi_bus_check_device(struct acpi_device *device, int *status_changed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
Len Brown4be44fc2005-08-05 00:44:28 -0400373 acpi_status status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 struct acpi_device_status old_status;
375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400378 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 if (status_changed)
381 *status_changed = 0;
382
383 old_status = device->status;
384
385 /*
386 * Make sure this device's parent is present before we go about
387 * messing with the device.
388 */
389 if (device->parent && !device->parent->status.present) {
390 device->status = device->parent->status;
391 if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
392 if (status_changed)
393 *status_changed = 1;
394 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400395 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
398 status = acpi_bus_get_status(device);
399 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400400 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400403 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (status_changed)
406 *status_changed = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 /*
409 * Device Insertion/Removal
410 */
411 if ((device->status.present) && !(old_status.present)) {
412 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
413 /* TBD: Handle device insertion */
Len Brown4be44fc2005-08-05 00:44:28 -0400414 } else if (!(device->status.present) && (old_status.present)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
416 /* TBD: Handle device removal */
417 }
418
Patrick Mocheld550d982006-06-27 00:41:40 -0400419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Len Brown4be44fc2005-08-05 00:44:28 -0400422static int acpi_bus_check_scope(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Len Brown4be44fc2005-08-05 00:44:28 -0400424 int result = 0;
425 int status_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400429 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Status Change? */
432 result = acpi_bus_check_device(device, &status_changed);
433 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400434 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (!status_changed)
Patrick Mocheld550d982006-06-27 00:41:40 -0400437 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /*
440 * TBD: Enumerate child devices within this device's scope and
441 * run acpi_bus_check_device()'s on them.
442 */
443
Patrick Mocheld550d982006-06-27 00:41:40 -0400444 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447/**
448 * acpi_bus_notify
449 * ---------------
450 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
451 */
Len Brown4be44fc2005-08-05 00:44:28 -0400452static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Len Brown4be44fc2005-08-05 00:44:28 -0400454 int result = 0;
455 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 if (acpi_bus_get_device(handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400459 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 switch (type) {
462
463 case ACPI_NOTIFY_BUS_CHECK:
Len Brown4be44fc2005-08-05 00:44:28 -0400464 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
465 "Received BUS CHECK notification for device [%s]\n",
466 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 result = acpi_bus_check_scope(device);
468 /*
469 * TBD: We'll need to outsource certain events to non-ACPI
Len Brown4be44fc2005-08-05 00:44:28 -0400470 * drivers via the device manager (device.c).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 */
472 break;
473
474 case ACPI_NOTIFY_DEVICE_CHECK:
Len Brown4be44fc2005-08-05 00:44:28 -0400475 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
476 "Received DEVICE CHECK notification for device [%s]\n",
477 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 result = acpi_bus_check_device(device, NULL);
479 /*
480 * TBD: We'll need to outsource certain events to non-ACPI
Len Brown4be44fc2005-08-05 00:44:28 -0400481 * drivers via the device manager (device.c).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483 break;
484
485 case ACPI_NOTIFY_DEVICE_WAKE:
Len Brown4be44fc2005-08-05 00:44:28 -0400486 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
487 "Received DEVICE WAKE notification for device [%s]\n",
488 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 /* TBD */
490 break;
491
492 case ACPI_NOTIFY_EJECT_REQUEST:
Len Brown4be44fc2005-08-05 00:44:28 -0400493 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
494 "Received EJECT REQUEST notification for device [%s]\n",
495 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* TBD */
497 break;
498
499 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
Len Brown4be44fc2005-08-05 00:44:28 -0400500 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
501 "Received DEVICE CHECK LIGHT notification for device [%s]\n",
502 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* TBD: Exactly what does 'light' mean? */
504 break;
505
506 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
Len Brown4be44fc2005-08-05 00:44:28 -0400507 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
508 "Received FREQUENCY MISMATCH notification for device [%s]\n",
509 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* TBD */
511 break;
512
513 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
Len Brown4be44fc2005-08-05 00:44:28 -0400514 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
515 "Received BUS MODE MISMATCH notification for device [%s]\n",
516 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /* TBD */
518 break;
519
520 case ACPI_NOTIFY_POWER_FAULT:
Len Brown4be44fc2005-08-05 00:44:28 -0400521 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
522 "Received POWER FAULT notification for device [%s]\n",
523 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* TBD */
525 break;
526
527 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400528 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
529 "Received unknown/unsupported notification [%08x]\n",
530 type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 break;
532 }
533
Patrick Mocheld550d982006-06-27 00:41:40 -0400534 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
537/* --------------------------------------------------------------------------
538 Initialization/Cleanup
539 -------------------------------------------------------------------------- */
540
Len Brown4be44fc2005-08-05 00:44:28 -0400541static int __init acpi_bus_init_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Len Brown4be44fc2005-08-05 00:44:28 -0400543 acpi_status status = AE_OK;
544 union acpi_object arg = { ACPI_TYPE_INTEGER };
545 struct acpi_object_list arg_list = { 1, &arg };
546 char *message = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /*
550 * Let the system know what interrupt model we are using by
551 * evaluating the \_PIC object, if exists.
552 */
553
554 switch (acpi_irq_model) {
555 case ACPI_IRQ_MODEL_PIC:
556 message = "PIC";
557 break;
558 case ACPI_IRQ_MODEL_IOAPIC:
559 message = "IOAPIC";
560 break;
561 case ACPI_IRQ_MODEL_IOSAPIC:
562 message = "IOSAPIC";
563 break;
John Keller3948ec92006-12-22 11:50:04 -0600564 case ACPI_IRQ_MODEL_PLATFORM:
565 message = "platform specific model";
566 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 default:
568 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400569 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571
572 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
573
574 arg.integer.value = acpi_irq_model;
575
576 status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
577 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400578 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400579 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
Patrick Mocheld550d982006-06-27 00:41:40 -0400582 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Len Brown4be44fc2005-08-05 00:44:28 -0400585void __init acpi_early_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Len Brown4be44fc2005-08-05 00:44:28 -0400587 acpi_status status = AE_OK;
588 struct acpi_buffer buffer = { sizeof(acpi_fadt), &acpi_fadt };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400592 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Bob Moore61686122006-03-17 16:44:00 -0500594 printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 /* enable workarounds, unless strict ACPI spec. compliance */
597 if (!acpi_strict)
598 acpi_gbl_enable_interpreter_slack = TRUE;
599
600 status = acpi_initialize_subsystem();
601 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400602 printk(KERN_ERR PREFIX
603 "Unable to initialize the ACPI Interpreter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 goto error0;
605 }
606
607 status = acpi_load_tables();
608 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400609 printk(KERN_ERR PREFIX
610 "Unable to load the System Description Tables\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 goto error0;
612 }
613
614 /*
615 * Get a separate copy of the FADT for use by other drivers.
616 */
Bob Mooreb229cf92006-04-21 17:15:00 -0400617 status = acpi_get_table(ACPI_TABLE_ID_FADT, 1, &buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (ACPI_FAILURE(status)) {
619 printk(KERN_ERR PREFIX "Unable to get the FADT\n");
620 goto error0;
621 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622#ifdef CONFIG_X86
623 if (!acpi_ioapic) {
624 extern acpi_interrupt_flags acpi_sci_flags;
625
626 /* compatible (0) means level (3) */
627 if (acpi_sci_flags.trigger == 0)
628 acpi_sci_flags.trigger = 3;
629
630 /* Set PIC-mode SCI trigger type */
Len Brown4be44fc2005-08-05 00:44:28 -0400631 acpi_pic_sci_set_trigger(acpi_fadt.sci_int,
632 acpi_sci_flags.trigger);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 } else {
634 extern int acpi_sci_override_gsi;
635 /*
636 * now that acpi_fadt is initialized,
637 * update it with result from INT_SRC_OVR parsing
638 */
639 acpi_fadt.sci_int = acpi_sci_override_gsi;
640 }
641#endif
642
Len Brown4be44fc2005-08-05 00:44:28 -0400643 status =
644 acpi_enable_subsystem(~
645 (ACPI_NO_HARDWARE_INIT |
646 ACPI_NO_ACPI_ENABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 if (ACPI_FAILURE(status)) {
648 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
649 goto error0;
650 }
651
Patrick Mocheld550d982006-06-27 00:41:40 -0400652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Len Brown4be44fc2005-08-05 00:44:28 -0400654 error0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 disable_acpi();
Patrick Mocheld550d982006-06-27 00:41:40 -0400656 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657}
658
Len Brown4be44fc2005-08-05 00:44:28 -0400659static int __init acpi_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Len Brown4be44fc2005-08-05 00:44:28 -0400661 int result = 0;
662 acpi_status status = AE_OK;
663 extern acpi_status acpi_os_initialize1(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 status = acpi_os_initialize1();
667
Len Brown4be44fc2005-08-05 00:44:28 -0400668 status =
669 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400671 printk(KERN_ERR PREFIX
672 "Unable to start the ACPI Interpreter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto error1;
674 }
675
676 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400677 printk(KERN_ERR PREFIX
678 "Unable to initialize ACPI OS objects\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto error1;
680 }
681#ifdef CONFIG_ACPI_EC
682 /*
683 * ACPI 2.0 requires the EC driver to be loaded and work before
684 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
685 * is called).
686 *
687 * This is accomplished by looking for the ECDT table, and getting
688 * the EC parameters out of that.
689 */
690 status = acpi_ec_ecdt_probe();
691 /* Ignore result. Not having an ECDT is not fatal. */
692#endif
693
694 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
695 if (ACPI_FAILURE(status)) {
696 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
697 goto error1;
698 }
699
700 printk(KERN_INFO PREFIX "Interpreter enabled\n");
701
702 /*
703 * Get the system interrupt model and evaluate \_PIC.
704 */
705 result = acpi_bus_init_irq();
706 if (result)
707 goto error1;
708
709 /*
710 * Register the for all standard device notifications.
711 */
Len Brown4be44fc2005-08-05 00:44:28 -0400712 status =
713 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
714 &acpi_bus_notify, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400716 printk(KERN_ERR PREFIX
717 "Unable to register for device notifications\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto error1;
719 }
720
721 /*
722 * Create the top ACPI proc directory
723 */
724 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
725
Patrick Mocheld550d982006-06-27 00:41:40 -0400726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 /* Mimic structured exception handling */
Len Brown4be44fc2005-08-05 00:44:28 -0400729 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 acpi_terminate();
Patrick Mocheld550d982006-06-27 00:41:40 -0400731 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Len Brown4be44fc2005-08-05 00:44:28 -0400734decl_subsys(acpi, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Len Brown4be44fc2005-08-05 00:44:28 -0400736static int __init acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
Len Brown4be44fc2005-08-05 00:44:28 -0400738 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (acpi_disabled) {
742 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400743 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
Randy Dunlapd568df82006-07-12 01:47:00 -0400746 result = firmware_register(&acpi_subsys);
747 if (result < 0)
748 printk(KERN_WARNING "%s: firmware_register error: %d\n",
749 __FUNCTION__, result);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 result = acpi_bus_init();
752
753 if (!result) {
Jeff Garzikbca73e42005-11-13 16:06:25 -0800754#ifdef CONFIG_PM_LEGACY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!PM_IS_ACTIVE())
756 pm_active = 1;
757 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400758 printk(KERN_INFO PREFIX
759 "APM is already active, exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 disable_acpi();
761 result = -ENODEV;
762 }
763#endif
764 } else
765 disable_acpi();
766
Patrick Mocheld550d982006-06-27 00:41:40 -0400767 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
769
770subsys_initcall(acpi_init);