blob: 5b6760e0f9571d20e34dcb47346c354ad6dff116 [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>
32#include <linux/device.h>
33#include <linux/proc_fs.h>
Harvey Harrison6697c052008-02-09 23:24:08 +010034#include <linux/acpi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#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 Brownf52fd662007-02-12 22:42:12 -050042ACPI_MODULE_NAME("bus");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Len Brown4be44fc2005-08-05 00:44:28 -040044struct acpi_device *acpi_root;
45struct proc_dir_entry *acpi_root_dir;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046EXPORT_SYMBOL(acpi_root_dir);
47
48#define STRUCT_TO_INT(s) (*((int*)&s))
49
50/* --------------------------------------------------------------------------
51 Device Management
52 -------------------------------------------------------------------------- */
53
Len Brown4be44fc2005-08-05 00:44:28 -040054int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Len Brown4be44fc2005-08-05 00:44:28 -040056 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -040060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /* TBD: Support fixed-feature devices */
63
Len Brown4be44fc2005-08-05 00:44:28 -040064 status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (ACPI_FAILURE(status) || !*device) {
Len Brown9805cb72006-07-25 13:30:57 -040066 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
67 handle));
Patrick Mocheld550d982006-06-27 00:41:40 -040068 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 }
70
Patrick Mocheld550d982006-06-27 00:41:40 -040071 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
Len Brown4be44fc2005-08-05 00:44:28 -040073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074EXPORT_SYMBOL(acpi_bus_get_device);
75
Len Brown4be44fc2005-08-05 00:44:28 -040076int acpi_bus_get_status(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Len Brown4be44fc2005-08-05 00:44:28 -040078 acpi_status status = AE_OK;
79 unsigned long sta = 0;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -040083 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /*
86 * Evaluate _STA if present.
87 */
88 if (device->flags.dynamic_status) {
Len Brown4be44fc2005-08-05 00:44:28 -040089 status =
90 acpi_evaluate_integer(device->handle, "_STA", NULL, &sta);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -040092 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -040093 STRUCT_TO_INT(device->status) = (int)sta;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
96 /*
97 * Otherwise we assume the status of our parent (unless we don't
98 * have one, in which case status is implied).
99 */
100 else if (device->parent)
101 device->status = device->parent->status;
102 else
Bjorn Helgaas0c0e8922007-04-25 14:20:58 -0400103 STRUCT_TO_INT(device->status) =
104 ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
105 ACPI_STA_DEVICE_UI | ACPI_STA_DEVICE_FUNCTIONING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 if (device->status.functional && !device->status.present) {
108 printk(KERN_WARNING PREFIX "Device [%s] status [%08x]: "
Len Brown4be44fc2005-08-05 00:44:28 -0400109 "functional but not present; setting present\n",
110 device->pnp.bus_id, (u32) STRUCT_TO_INT(device->status));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 device->status.present = 1;
112 }
113
Len Brown4be44fc2005-08-05 00:44:28 -0400114 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
115 device->pnp.bus_id,
116 (u32) STRUCT_TO_INT(device->status)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Patrick Mocheld550d982006-06-27 00:41:40 -0400118 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Len Brown4be44fc2005-08-05 00:44:28 -0400121EXPORT_SYMBOL(acpi_bus_get_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Zhang Rui20733932008-01-17 15:51:21 +0800123void acpi_bus_private_data_handler(acpi_handle handle,
124 u32 function, void *context)
125{
126 return;
127}
128EXPORT_SYMBOL(acpi_bus_private_data_handler);
129
130int acpi_bus_get_private_data(acpi_handle handle, void **data)
131{
132 acpi_status status = AE_OK;
133
134 if (!*data)
135 return -EINVAL;
136
137 status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
138 if (ACPI_FAILURE(status) || !*data) {
139 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
140 handle));
141 return -ENODEV;
142 }
143
144 return 0;
145}
146EXPORT_SYMBOL(acpi_bus_get_private_data);
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148/* --------------------------------------------------------------------------
149 Power Management
150 -------------------------------------------------------------------------- */
151
Len Brown4be44fc2005-08-05 00:44:28 -0400152int acpi_bus_get_power(acpi_handle handle, int *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Len Brown4be44fc2005-08-05 00:44:28 -0400154 int result = 0;
155 acpi_status status = 0;
156 struct acpi_device *device = NULL;
157 unsigned long psc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 result = acpi_bus_get_device(handle, &device);
161 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400162 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 *state = ACPI_STATE_UNKNOWN;
165
166 if (!device->flags.power_manageable) {
167 /* TBD: Non-recursive algorithm for walking up hierarchy */
168 if (device->parent)
169 *state = device->parent->power.state;
170 else
171 *state = ACPI_STATE_D0;
Len Brown4be44fc2005-08-05 00:44:28 -0400172 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 /*
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500174 * Get the device's power state either directly (via _PSC) or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 * indirectly (via power resources).
176 */
177 if (device->power.flags.explicit_get) {
Len Brown4be44fc2005-08-05 00:44:28 -0400178 status = acpi_evaluate_integer(device->handle, "_PSC",
179 NULL, &psc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400181 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400182 device->power.state = (int)psc;
183 } else if (device->power.flags.power_resources) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 result = acpi_power_get_inferred_state(device);
185 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400186 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 *state = device->power.state;
190 }
191
192 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400193 device->pnp.bus_id, device->power.state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Patrick Mocheld550d982006-06-27 00:41:40 -0400195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
Len Brown4be44fc2005-08-05 00:44:28 -0400197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198EXPORT_SYMBOL(acpi_bus_get_power);
199
Len Brown4be44fc2005-08-05 00:44:28 -0400200int acpi_bus_set_power(acpi_handle handle, int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Len Brown4be44fc2005-08-05 00:44:28 -0400202 int result = 0;
203 acpi_status status = AE_OK;
204 struct acpi_device *device = NULL;
205 char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 result = acpi_bus_get_device(handle, &device);
209 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400210 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 if ((state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
Patrick Mocheld550d982006-06-27 00:41:40 -0400213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Make sure this is a valid target state */
216
217 if (!device->flags.power_manageable) {
Len Brown9805cb72006-07-25 13:30:57 -0400218 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device `[%s]' is not power manageable\n",
Greg Kroah-Hartman19c38de2007-09-12 15:06:57 -0700219 kobject_name(&device->dev.kobj)));
Patrick Mocheld550d982006-06-27 00:41:40 -0400220 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
David Shaohua Lib9131002005-03-19 00:16:18 -0500222 /*
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400223 * Get device's current power state
David Shaohua Lib9131002005-03-19 00:16:18 -0500224 */
Alexey Starikovskiyc35923b2007-10-22 14:19:09 +0400225 acpi_bus_get_power(device->handle, &device->power.state);
Len Brownec683732008-01-23 22:41:20 -0500226 if ((state == device->power.state) && !device->flags.force_power_state) {
Konstantin Karasyovb1028c52007-02-16 02:23:07 -0500227 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
228 state));
229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
Konstantin Karasyovb1028c52007-02-16 02:23:07 -0500231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if (!device->power.states[state].flags.valid) {
Len Browncece9292006-06-26 23:04:31 -0400233 printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
Patrick Mocheld550d982006-06-27 00:41:40 -0400234 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 if (device->parent && (state < device->parent->power.state)) {
Len Browncece9292006-06-26 23:04:31 -0400237 printk(KERN_WARNING PREFIX
Thomas Renningera6fc6722006-06-26 23:58:43 -0400238 "Cannot set device to a higher-powered"
Len Browncece9292006-06-26 23:04:31 -0400239 " state than parent\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400240 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 /*
244 * Transition Power
245 * ----------------
246 * On transitions to a high-powered state we first apply power (via
247 * power resources) then evalute _PSx. Conversly for transitions to
248 * a lower-powered state.
David Shaohua Lib9131002005-03-19 00:16:18 -0500249 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (state < device->power.state) {
251 if (device->power.flags.power_resources) {
252 result = acpi_power_transition(device, state);
253 if (result)
254 goto end;
255 }
256 if (device->power.states[state].flags.explicit_set) {
Len Brown4be44fc2005-08-05 00:44:28 -0400257 status = acpi_evaluate_object(device->handle,
258 object_name, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 if (ACPI_FAILURE(status)) {
260 result = -ENODEV;
261 goto end;
262 }
263 }
Len Brown4be44fc2005-08-05 00:44:28 -0400264 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (device->power.states[state].flags.explicit_set) {
Len Brown4be44fc2005-08-05 00:44:28 -0400266 status = acpi_evaluate_object(device->handle,
267 object_name, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (ACPI_FAILURE(status)) {
269 result = -ENODEV;
270 goto end;
271 }
272 }
273 if (device->power.flags.power_resources) {
274 result = acpi_power_transition(device, state);
275 if (result)
276 goto end;
277 }
278 }
279
Len Brown4be44fc2005-08-05 00:44:28 -0400280 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 if (result)
Len Browncece9292006-06-26 23:04:31 -0400282 printk(KERN_WARNING PREFIX
283 "Transitioning device [%s] to D%d\n",
284 device->pnp.bus_id, state);
Shaohua Li5e321322007-10-11 23:53:58 +0200285 else {
286 device->power.state = state;
Len Brown4be44fc2005-08-05 00:44:28 -0400287 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
288 "Device [%s] transitioned to D%d\n",
289 device->pnp.bus_id, state));
Shaohua Li5e321322007-10-11 23:53:58 +0200290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Patrick Mocheld550d982006-06-27 00:41:40 -0400292 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293}
Len Brown4be44fc2005-08-05 00:44:28 -0400294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295EXPORT_SYMBOL(acpi_bus_set_power);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/* --------------------------------------------------------------------------
298 Event Management
299 -------------------------------------------------------------------------- */
300
Len Brown14e04fb2007-08-23 15:20:26 -0400301#ifdef CONFIG_ACPI_PROC_EVENT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static DEFINE_SPINLOCK(acpi_bus_event_lock);
303
304LIST_HEAD(acpi_bus_event_list);
305DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
306
Len Brown4be44fc2005-08-05 00:44:28 -0400307extern int event_is_open;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Alexey Starikovskiy8db85d42007-09-26 19:43:16 +0400309int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Alexey Starikovskiy8db85d42007-09-26 19:43:16 +0400311 struct acpi_bus_event *event;
Len Brown4be44fc2005-08-05 00:44:28 -0400312 unsigned long flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 /* drop event on the floor if no one's listening */
315 if (!event_is_open)
Patrick Mocheld550d982006-06-27 00:41:40 -0400316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 event = kmalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
319 if (!event)
Patrick Mocheld550d982006-06-27 00:41:40 -0400320 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Alexey Starikovskiy8db85d42007-09-26 19:43:16 +0400322 strcpy(event->device_class, device_class);
323 strcpy(event->bus_id, bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 event->type = type;
325 event->data = data;
326
327 spin_lock_irqsave(&acpi_bus_event_lock, flags);
328 list_add_tail(&event->node, &acpi_bus_event_list);
329 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
330
331 wake_up_interruptible(&acpi_bus_event_queue);
332
Patrick Mocheld550d982006-06-27 00:41:40 -0400333 return 0;
Alexey Starikovskiy8db85d42007-09-26 19:43:16 +0400334
335}
336
337EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
338
339int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
340{
341 if (!device)
342 return -EINVAL;
343 return acpi_bus_generate_proc_event4(device->pnp.device_class,
344 device->pnp.bus_id, type, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Len Brown4be44fc2005-08-05 00:44:28 -0400346
Len Brown14e04fb2007-08-23 15:20:26 -0400347EXPORT_SYMBOL(acpi_bus_generate_proc_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Len Brown4be44fc2005-08-05 00:44:28 -0400349int acpi_bus_receive_event(struct acpi_bus_event *event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Len Brown4be44fc2005-08-05 00:44:28 -0400351 unsigned long flags = 0;
352 struct acpi_bus_event *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 DECLARE_WAITQUEUE(wait, current);
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (!event)
Patrick Mocheld550d982006-06-27 00:41:40 -0400358 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (list_empty(&acpi_bus_event_list)) {
361
362 set_current_state(TASK_INTERRUPTIBLE);
363 add_wait_queue(&acpi_bus_event_queue, &wait);
364
365 if (list_empty(&acpi_bus_event_list))
366 schedule();
367
368 remove_wait_queue(&acpi_bus_event_queue, &wait);
369 set_current_state(TASK_RUNNING);
370
371 if (signal_pending(current))
Patrick Mocheld550d982006-06-27 00:41:40 -0400372 return -ERESTARTSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 spin_lock_irqsave(&acpi_bus_event_lock, flags);
Len Brown4be44fc2005-08-05 00:44:28 -0400376 entry =
377 list_entry(acpi_bus_event_list.next, struct acpi_bus_event, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (entry)
379 list_del(&entry->node);
380 spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
381
382 if (!entry)
Patrick Mocheld550d982006-06-27 00:41:40 -0400383 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 memcpy(event, entry, sizeof(struct acpi_bus_event));
386
387 kfree(entry);
388
Patrick Mocheld550d982006-06-27 00:41:40 -0400389 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Len Brown14e04fb2007-08-23 15:20:26 -0400392#endif /* CONFIG_ACPI_PROC_EVENT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394/* --------------------------------------------------------------------------
395 Notification Handling
396 -------------------------------------------------------------------------- */
397
398static int
Len Brown4be44fc2005-08-05 00:44:28 -0400399acpi_bus_check_device(struct acpi_device *device, int *status_changed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Len Brown4be44fc2005-08-05 00:44:28 -0400401 acpi_status status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct acpi_device_status old_status;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400406 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 if (status_changed)
409 *status_changed = 0;
410
411 old_status = device->status;
412
413 /*
414 * Make sure this device's parent is present before we go about
415 * messing with the device.
416 */
417 if (device->parent && !device->parent->status.present) {
418 device->status = device->parent->status;
419 if (STRUCT_TO_INT(old_status) != STRUCT_TO_INT(device->status)) {
420 if (status_changed)
421 *status_changed = 1;
422 }
Patrick Mocheld550d982006-06-27 00:41:40 -0400423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
426 status = acpi_bus_get_status(device);
427 if (ACPI_FAILURE(status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400428 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
Patrick Mocheld550d982006-06-27 00:41:40 -0400431 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 if (status_changed)
434 *status_changed = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /*
437 * Device Insertion/Removal
438 */
439 if ((device->status.present) && !(old_status.present)) {
440 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
441 /* TBD: Handle device insertion */
Len Brown4be44fc2005-08-05 00:44:28 -0400442 } else if (!(device->status.present) && (old_status.present)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
444 /* TBD: Handle device removal */
445 }
446
Patrick Mocheld550d982006-06-27 00:41:40 -0400447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Len Brown4be44fc2005-08-05 00:44:28 -0400450static int acpi_bus_check_scope(struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Len Brown4be44fc2005-08-05 00:44:28 -0400452 int result = 0;
453 int status_changed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 if (!device)
Patrick Mocheld550d982006-06-27 00:41:40 -0400457 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 /* Status Change? */
460 result = acpi_bus_check_device(device, &status_changed);
461 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -0400462 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464 if (!status_changed)
Patrick Mocheld550d982006-06-27 00:41:40 -0400465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /*
468 * TBD: Enumerate child devices within this device's scope and
469 * run acpi_bus_check_device()'s on them.
470 */
471
Patrick Mocheld550d982006-06-27 00:41:40 -0400472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475/**
476 * acpi_bus_notify
477 * ---------------
478 * Callback for all 'system-level' device notifications (values 0x00-0x7F).
479 */
Len Brown4be44fc2005-08-05 00:44:28 -0400480static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Len Brown4be44fc2005-08-05 00:44:28 -0400482 int result = 0;
483 struct acpi_device *device = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 if (acpi_bus_get_device(handle, &device))
Patrick Mocheld550d982006-06-27 00:41:40 -0400487 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 switch (type) {
490
491 case ACPI_NOTIFY_BUS_CHECK:
Len Brown4be44fc2005-08-05 00:44:28 -0400492 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
493 "Received BUS CHECK notification for device [%s]\n",
494 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 result = acpi_bus_check_scope(device);
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500496 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 * TBD: We'll need to outsource certain events to non-ACPI
Len Brown4be44fc2005-08-05 00:44:28 -0400498 * drivers via the device manager (device.c).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
500 break;
501
502 case ACPI_NOTIFY_DEVICE_CHECK:
Len Brown4be44fc2005-08-05 00:44:28 -0400503 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
504 "Received DEVICE CHECK notification for device [%s]\n",
505 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 result = acpi_bus_check_device(device, NULL);
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500507 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 * TBD: We'll need to outsource certain events to non-ACPI
Len Brown4be44fc2005-08-05 00:44:28 -0400509 * drivers via the device manager (device.c).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
511 break;
512
513 case ACPI_NOTIFY_DEVICE_WAKE:
Len Brown4be44fc2005-08-05 00:44:28 -0400514 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
515 "Received DEVICE WAKE 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_EJECT_REQUEST:
Len Brown4be44fc2005-08-05 00:44:28 -0400521 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
522 "Received EJECT REQUEST notification for device [%s]\n",
523 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 /* TBD */
525 break;
526
527 case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
Len Brown4be44fc2005-08-05 00:44:28 -0400528 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
529 "Received DEVICE CHECK LIGHT notification for device [%s]\n",
530 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /* TBD: Exactly what does 'light' mean? */
532 break;
533
534 case ACPI_NOTIFY_FREQUENCY_MISMATCH:
Len Brown4be44fc2005-08-05 00:44:28 -0400535 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
536 "Received FREQUENCY MISMATCH notification for device [%s]\n",
537 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* TBD */
539 break;
540
541 case ACPI_NOTIFY_BUS_MODE_MISMATCH:
Len Brown4be44fc2005-08-05 00:44:28 -0400542 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
543 "Received BUS MODE MISMATCH notification for device [%s]\n",
544 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 /* TBD */
546 break;
547
548 case ACPI_NOTIFY_POWER_FAULT:
Len Brown4be44fc2005-08-05 00:44:28 -0400549 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
550 "Received POWER FAULT notification for device [%s]\n",
551 device->pnp.bus_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 /* TBD */
553 break;
554
555 default:
Len Brown4be44fc2005-08-05 00:44:28 -0400556 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
557 "Received unknown/unsupported notification [%08x]\n",
558 type));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
560 }
561
Patrick Mocheld550d982006-06-27 00:41:40 -0400562 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
565/* --------------------------------------------------------------------------
566 Initialization/Cleanup
567 -------------------------------------------------------------------------- */
568
Len Brown4be44fc2005-08-05 00:44:28 -0400569static int __init acpi_bus_init_irq(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Len Brown4be44fc2005-08-05 00:44:28 -0400571 acpi_status status = AE_OK;
572 union acpi_object arg = { ACPI_TYPE_INTEGER };
573 struct acpi_object_list arg_list = { 1, &arg };
574 char *message = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500577 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * Let the system know what interrupt model we are using by
579 * evaluating the \_PIC object, if exists.
580 */
581
582 switch (acpi_irq_model) {
583 case ACPI_IRQ_MODEL_PIC:
584 message = "PIC";
585 break;
586 case ACPI_IRQ_MODEL_IOAPIC:
587 message = "IOAPIC";
588 break;
589 case ACPI_IRQ_MODEL_IOSAPIC:
590 message = "IOSAPIC";
591 break;
John Keller3948ec92006-12-22 11:50:04 -0600592 case ACPI_IRQ_MODEL_PLATFORM:
593 message = "platform specific model";
594 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 default:
596 printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400597 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599
600 printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
601
602 arg.integer.value = acpi_irq_model;
603
604 status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
605 if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -0400606 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400607 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
Patrick Mocheld550d982006-06-27 00:41:40 -0400610 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300613acpi_native_uint acpi_gbl_permanent_mmap;
614
615
Len Brown4be44fc2005-08-05 00:44:28 -0400616void __init acpi_early_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Len Brown4be44fc2005-08-05 00:44:28 -0400618 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (acpi_disabled)
Patrick Mocheld550d982006-06-27 00:41:40 -0400621 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Bob Moore616861242006-03-17 16:44:00 -0500623 printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 /* enable workarounds, unless strict ACPI spec. compliance */
626 if (!acpi_strict)
627 acpi_gbl_enable_interpreter_slack = TRUE;
628
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300629 acpi_gbl_permanent_mmap = 1;
630
631 status = acpi_reallocate_root_table();
632 if (ACPI_FAILURE(status)) {
633 printk(KERN_ERR PREFIX
634 "Unable to reallocate ACPI tables\n");
635 goto error0;
636 }
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 status = acpi_initialize_subsystem();
639 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400640 printk(KERN_ERR PREFIX
641 "Unable to initialize the ACPI Interpreter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 goto error0;
643 }
644
645 status = acpi_load_tables();
646 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400647 printk(KERN_ERR PREFIX
648 "Unable to load the System Description Tables\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 goto error0;
650 }
651
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652#ifdef CONFIG_X86
653 if (!acpi_ioapic) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 /* compatible (0) means level (3) */
Alexey Starikovskiy5f3b1a82007-02-02 19:48:22 +0300655 if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
656 acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
657 acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /* Set PIC-mode SCI trigger type */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300660 acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
Alexey Starikovskiy5f3b1a82007-02-02 19:48:22 +0300661 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300664 * now that acpi_gbl_FADT is initialized,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * update it with result from INT_SRC_OVR parsing
666 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300667 acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669#endif
670
Len Brown4be44fc2005-08-05 00:44:28 -0400671 status =
672 acpi_enable_subsystem(~
673 (ACPI_NO_HARDWARE_INIT |
674 ACPI_NO_ACPI_ENABLE));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (ACPI_FAILURE(status)) {
676 printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
677 goto error0;
678 }
679
Patrick Mocheld550d982006-06-27 00:41:40 -0400680 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Len Brown4be44fc2005-08-05 00:44:28 -0400682 error0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 disable_acpi();
Patrick Mocheld550d982006-06-27 00:41:40 -0400684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Len Brown4be44fc2005-08-05 00:44:28 -0400687static int __init acpi_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Len Brown4be44fc2005-08-05 00:44:28 -0400689 int result = 0;
690 acpi_status status = AE_OK;
691 extern acpi_status acpi_os_initialize1(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 status = acpi_os_initialize1();
695
Len Brown4be44fc2005-08-05 00:44:28 -0400696 status =
697 acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400699 printk(KERN_ERR PREFIX
700 "Unable to start the ACPI Interpreter\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 goto error1;
702 }
703
704 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400705 printk(KERN_ERR PREFIX
706 "Unable to initialize ACPI OS objects\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto error1;
708 }
709#ifdef CONFIG_ACPI_EC
710 /*
711 * ACPI 2.0 requires the EC driver to be loaded and work before
712 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
713 * is called).
714 *
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500715 * This is accomplished by looking for the ECDT table, and getting
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 * the EC parameters out of that.
717 */
718 status = acpi_ec_ecdt_probe();
719 /* Ignore result. Not having an ECDT is not fatal. */
720#endif
721
722 status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
723 if (ACPI_FAILURE(status)) {
724 printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
725 goto error1;
726 }
727
728 printk(KERN_INFO PREFIX "Interpreter enabled\n");
729
Alexey Starikovskiyaafbcd12007-02-10 01:32:16 -0500730 /* Initialize sleep structures */
731 acpi_sleep_init();
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 /*
734 * Get the system interrupt model and evaluate \_PIC.
735 */
736 result = acpi_bus_init_irq();
737 if (result)
738 goto error1;
739
740 /*
741 * Register the for all standard device notifications.
742 */
Len Brown4be44fc2005-08-05 00:44:28 -0400743 status =
744 acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
745 &acpi_bus_notify, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (ACPI_FAILURE(status)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400747 printk(KERN_ERR PREFIX
748 "Unable to register for device notifications\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 goto error1;
750 }
751
752 /*
753 * Create the top ACPI proc directory
754 */
755 acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
756
Patrick Mocheld550d982006-06-27 00:41:40 -0400757 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* Mimic structured exception handling */
Len Brown4be44fc2005-08-05 00:44:28 -0400760 error1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 acpi_terminate();
Patrick Mocheld550d982006-06-27 00:41:40 -0400762 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
Greg Kroah-Hartman99e0d2f2007-11-02 16:19:59 -0700765struct kobject *acpi_kobj;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Len Brown4be44fc2005-08-05 00:44:28 -0400767static int __init acpi_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Len Brown4be44fc2005-08-05 00:44:28 -0400769 int result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (acpi_disabled) {
773 printk(KERN_INFO PREFIX "Interpreter disabled.\n");
Patrick Mocheld550d982006-06-27 00:41:40 -0400774 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
Greg Kroah-Hartmanf62ed9e2007-11-05 13:16:15 -0800777 acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
Greg Kroah-Hartman99e0d2f2007-11-02 16:19:59 -0700778 if (!acpi_kobj) {
Harvey Harrison96b2dd12008-03-05 18:24:51 -0800779 printk(KERN_WARNING "%s: kset create error\n", __func__);
Greg Kroah-Hartman99e0d2f2007-11-02 16:19:59 -0700780 acpi_kobj = NULL;
781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 result = acpi_bus_init();
784
785 if (!result) {
Len Brown9f9adec2007-12-13 17:38:03 -0500786 if (!(pm_flags & PM_APM))
787 pm_flags |= PM_ACPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 else {
Len Brown4be44fc2005-08-05 00:44:28 -0400789 printk(KERN_INFO PREFIX
790 "APM is already active, exiting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 disable_acpi();
792 result = -ENODEV;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 } else
795 disable_acpi();
796
Patrick Mocheld550d982006-06-27 00:41:40 -0400797 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
800subsys_initcall(acpi_init);