1 /*
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>
28 #include <linux/kernel.h>
29 #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>
34 #include <linux/acpi.h>
35 #include <linux/slab.h>
36 #ifdef CONFIG_X86
37 #include <asm/mpspec.h>
38 #endif
39 #include <linux/pci.h>
40 #include <acpi/acpi_bus.h>
41 #include <acpi/acpi_drivers.h>
42 #include <linux/dmi.h>
43 #include <linux/suspend.h>
44 
45 #include "internal.h"
46 
47 #define _COMPONENT		ACPI_BUS_COMPONENT
48 ACPI_MODULE_NAME("bus");
49 
50 struct acpi_device *acpi_root;
51 struct proc_dir_entry *acpi_root_dir;
52 EXPORT_SYMBOL(acpi_root_dir);
53 
54 #define STRUCT_TO_INT(s)	(*((int*)&s))
55 
56 
57 #ifdef CONFIG_X86
set_copy_dsdt(const struct dmi_system_id * id)58 static int set_copy_dsdt(const struct dmi_system_id *id)
59 {
60 	printk(KERN_NOTICE "%s detected - "
61 		"force copy of DSDT to local memory\n", id->ident);
62 	acpi_gbl_copy_dsdt_locally = 1;
63 	return 0;
64 }
65 
66 static struct dmi_system_id dsdt_dmi_table[] __initdata = {
67 	/*
68 	 * Invoke DSDT corruption work-around on all Toshiba Satellite.
69 	 * https://bugzilla.kernel.org/show_bug.cgi?id=14679
70 	 */
71 	{
72 	 .callback = set_copy_dsdt,
73 	 .ident = "TOSHIBA Satellite",
74 	 .matches = {
75 		DMI_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
76 		DMI_MATCH(DMI_PRODUCT_NAME, "Satellite"),
77 		},
78 	},
79 	{}
80 };
81 #else
82 static struct dmi_system_id dsdt_dmi_table[] __initdata = {
83 	{}
84 };
85 #endif
86 
87 /* --------------------------------------------------------------------------
88                                 Device Management
89    -------------------------------------------------------------------------- */
90 
acpi_bus_get_device(acpi_handle handle,struct acpi_device ** device)91 int acpi_bus_get_device(acpi_handle handle, struct acpi_device **device)
92 {
93 	acpi_status status = AE_OK;
94 
95 
96 	if (!device)
97 		return -EINVAL;
98 
99 	/* TBD: Support fixed-feature devices */
100 
101 	status = acpi_get_data(handle, acpi_bus_data_handler, (void **)device);
102 	if (ACPI_FAILURE(status) || !*device) {
103 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
104 				  handle));
105 		return -ENODEV;
106 	}
107 
108 	return 0;
109 }
110 
111 EXPORT_SYMBOL(acpi_bus_get_device);
112 
acpi_bus_get_status_handle(acpi_handle handle,unsigned long long * sta)113 acpi_status acpi_bus_get_status_handle(acpi_handle handle,
114 				       unsigned long long *sta)
115 {
116 	acpi_status status;
117 
118 	status = acpi_evaluate_integer(handle, "_STA", NULL, sta);
119 	if (ACPI_SUCCESS(status))
120 		return AE_OK;
121 
122 	if (status == AE_NOT_FOUND) {
123 		*sta = ACPI_STA_DEVICE_PRESENT | ACPI_STA_DEVICE_ENABLED |
124 		       ACPI_STA_DEVICE_UI      | ACPI_STA_DEVICE_FUNCTIONING;
125 		return AE_OK;
126 	}
127 	return status;
128 }
129 
acpi_bus_get_status(struct acpi_device * device)130 int acpi_bus_get_status(struct acpi_device *device)
131 {
132 	acpi_status status;
133 	unsigned long long sta;
134 
135 	status = acpi_bus_get_status_handle(device->handle, &sta);
136 	if (ACPI_FAILURE(status))
137 		return -ENODEV;
138 
139 	STRUCT_TO_INT(device->status) = (int) sta;
140 
141 	if (device->status.functional && !device->status.present) {
142 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]: "
143 		       "functional but not present;\n",
144 			device->pnp.bus_id,
145 			(u32) STRUCT_TO_INT(device->status)));
146 	}
147 
148 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] status [%08x]\n",
149 			  device->pnp.bus_id,
150 			  (u32) STRUCT_TO_INT(device->status)));
151 	return 0;
152 }
153 EXPORT_SYMBOL(acpi_bus_get_status);
154 
acpi_bus_private_data_handler(acpi_handle handle,void * context)155 void acpi_bus_private_data_handler(acpi_handle handle,
156 				   void *context)
157 {
158 	return;
159 }
160 EXPORT_SYMBOL(acpi_bus_private_data_handler);
161 
acpi_bus_get_private_data(acpi_handle handle,void ** data)162 int acpi_bus_get_private_data(acpi_handle handle, void **data)
163 {
164 	acpi_status status = AE_OK;
165 
166 	if (!*data)
167 		return -EINVAL;
168 
169 	status = acpi_get_data(handle, acpi_bus_private_data_handler, data);
170 	if (ACPI_FAILURE(status) || !*data) {
171 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No context for object [%p]\n",
172 				handle));
173 		return -ENODEV;
174 	}
175 
176 	return 0;
177 }
178 EXPORT_SYMBOL(acpi_bus_get_private_data);
179 
180 /* --------------------------------------------------------------------------
181                                  Power Management
182    -------------------------------------------------------------------------- */
183 
__acpi_bus_get_power(struct acpi_device * device,int * state)184 static int __acpi_bus_get_power(struct acpi_device *device, int *state)
185 {
186 	int result = 0;
187 	acpi_status status = 0;
188 	unsigned long long psc = 0;
189 
190 	if (!device || !state)
191 		return -EINVAL;
192 
193 	*state = ACPI_STATE_UNKNOWN;
194 
195 	if (device->flags.power_manageable) {
196 		/*
197 		 * Get the device's power state either directly (via _PSC) or
198 		 * indirectly (via power resources).
199 		 */
200 		if (device->power.flags.power_resources) {
201 			result = acpi_power_get_inferred_state(device, state);
202 			if (result)
203 				return result;
204 		} else if (device->power.flags.explicit_get) {
205 			status = acpi_evaluate_integer(device->handle, "_PSC",
206 						       NULL, &psc);
207 			if (ACPI_FAILURE(status))
208 				return -ENODEV;
209 			*state = (int)psc;
210 		}
211 	} else {
212 		/* TBD: Non-recursive algorithm for walking up hierarchy. */
213 		*state = device->parent ?
214 			device->parent->power.state : ACPI_STATE_D0;
215 	}
216 
217 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device [%s] power state is D%d\n",
218 			  device->pnp.bus_id, *state));
219 
220 	return 0;
221 }
222 
223 
__acpi_bus_set_power(struct acpi_device * device,int state)224 static int __acpi_bus_set_power(struct acpi_device *device, int state)
225 {
226 	int result = 0;
227 	acpi_status status = AE_OK;
228 	char object_name[5] = { '_', 'P', 'S', '0' + state, '\0' };
229 
230 	if (!device || (state < ACPI_STATE_D0) || (state > ACPI_STATE_D3))
231 		return -EINVAL;
232 
233 	/* Make sure this is a valid target state */
234 
235 	if (state == device->power.state) {
236 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device is already at D%d\n",
237 				  state));
238 		return 0;
239 	}
240 
241 	if (!device->power.states[state].flags.valid) {
242 		printk(KERN_WARNING PREFIX "Device does not support D%d\n", state);
243 		return -ENODEV;
244 	}
245 	if (device->parent && (state < device->parent->power.state)) {
246 		printk(KERN_WARNING PREFIX
247 			      "Cannot set device to a higher-powered"
248 			      " state than parent\n");
249 		return -ENODEV;
250 	}
251 
252 	/*
253 	 * Transition Power
254 	 * ----------------
255 	 * On transitions to a high-powered state we first apply power (via
256 	 * power resources) then evalute _PSx.  Conversly for transitions to
257 	 * a lower-powered state.
258 	 */
259 	if (state < device->power.state) {
260 		if (device->power.flags.power_resources) {
261 			result = acpi_power_transition(device, state);
262 			if (result)
263 				goto end;
264 		}
265 		if (device->power.states[state].flags.explicit_set) {
266 			status = acpi_evaluate_object(device->handle,
267 						      object_name, NULL, NULL);
268 			if (ACPI_FAILURE(status)) {
269 				result = -ENODEV;
270 				goto end;
271 			}
272 		}
273 	} else {
274 		if (device->power.states[state].flags.explicit_set) {
275 			status = acpi_evaluate_object(device->handle,
276 						      object_name, NULL, NULL);
277 			if (ACPI_FAILURE(status)) {
278 				result = -ENODEV;
279 				goto end;
280 			}
281 		}
282 		if (device->power.flags.power_resources) {
283 			result = acpi_power_transition(device, state);
284 			if (result)
285 				goto end;
286 		}
287 	}
288 
289       end:
290 	if (result)
291 		printk(KERN_WARNING PREFIX
292 			      "Device [%s] failed to transition to D%d\n",
293 			      device->pnp.bus_id, state);
294 	else {
295 		device->power.state = state;
296 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
297 				  "Device [%s] transitioned to D%d\n",
298 				  device->pnp.bus_id, state));
299 	}
300 
301 	return result;
302 }
303 
304 
acpi_bus_set_power(acpi_handle handle,int state)305 int acpi_bus_set_power(acpi_handle handle, int state)
306 {
307 	struct acpi_device *device;
308 	int result;
309 
310 	result = acpi_bus_get_device(handle, &device);
311 	if (result)
312 		return result;
313 
314 	if (!device->flags.power_manageable) {
315 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
316 				"Device [%s] is not power manageable\n",
317 				dev_name(&device->dev)));
318 		return -ENODEV;
319 	}
320 
321 	return __acpi_bus_set_power(device, state);
322 }
323 EXPORT_SYMBOL(acpi_bus_set_power);
324 
325 
acpi_bus_init_power(struct acpi_device * device)326 int acpi_bus_init_power(struct acpi_device *device)
327 {
328 	int state;
329 	int result;
330 
331 	if (!device)
332 		return -EINVAL;
333 
334 	device->power.state = ACPI_STATE_UNKNOWN;
335 
336 	result = __acpi_bus_get_power(device, &state);
337 	if (result)
338 		return result;
339 
340 	if (device->power.flags.power_resources)
341 		result = acpi_power_on_resources(device, state);
342 
343 	if (!result)
344 		device->power.state = state;
345 
346 	return result;
347 }
348 
349 
acpi_bus_update_power(acpi_handle handle,int * state_p)350 int acpi_bus_update_power(acpi_handle handle, int *state_p)
351 {
352 	struct acpi_device *device;
353 	int state;
354 	int result;
355 
356 	result = acpi_bus_get_device(handle, &device);
357 	if (result)
358 		return result;
359 
360 	result = __acpi_bus_get_power(device, &state);
361 	if (result)
362 		return result;
363 
364 	result = __acpi_bus_set_power(device, state);
365 	if (!result && state_p)
366 		*state_p = state;
367 
368 	return result;
369 }
370 EXPORT_SYMBOL_GPL(acpi_bus_update_power);
371 
372 
acpi_bus_power_manageable(acpi_handle handle)373 bool acpi_bus_power_manageable(acpi_handle handle)
374 {
375 	struct acpi_device *device;
376 	int result;
377 
378 	result = acpi_bus_get_device(handle, &device);
379 	return result ? false : device->flags.power_manageable;
380 }
381 
382 EXPORT_SYMBOL(acpi_bus_power_manageable);
383 
acpi_bus_can_wakeup(acpi_handle handle)384 bool acpi_bus_can_wakeup(acpi_handle handle)
385 {
386 	struct acpi_device *device;
387 	int result;
388 
389 	result = acpi_bus_get_device(handle, &device);
390 	return result ? false : device->wakeup.flags.valid;
391 }
392 
393 EXPORT_SYMBOL(acpi_bus_can_wakeup);
394 
acpi_print_osc_error(acpi_handle handle,struct acpi_osc_context * context,char * error)395 static void acpi_print_osc_error(acpi_handle handle,
396 	struct acpi_osc_context *context, char *error)
397 {
398 	struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER};
399 	int i;
400 
401 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer)))
402 		printk(KERN_DEBUG "%s\n", error);
403 	else {
404 		printk(KERN_DEBUG "%s:%s\n", (char *)buffer.pointer, error);
405 		kfree(buffer.pointer);
406 	}
407 	printk(KERN_DEBUG"_OSC request data:");
408 	for (i = 0; i < context->cap.length; i += sizeof(u32))
409 		printk("%x ", *((u32 *)(context->cap.pointer + i)));
410 	printk("\n");
411 }
412 
acpi_str_to_uuid(char * str,u8 * uuid)413 static acpi_status acpi_str_to_uuid(char *str, u8 *uuid)
414 {
415 	int i;
416 	static int opc_map_to_uuid[16] = {6, 4, 2, 0, 11, 9, 16, 14, 19, 21,
417 		24, 26, 28, 30, 32, 34};
418 
419 	if (strlen(str) != 36)
420 		return AE_BAD_PARAMETER;
421 	for (i = 0; i < 36; i++) {
422 		if (i == 8 || i == 13 || i == 18 || i == 23) {
423 			if (str[i] != '-')
424 				return AE_BAD_PARAMETER;
425 		} else if (!isxdigit(str[i]))
426 			return AE_BAD_PARAMETER;
427 	}
428 	for (i = 0; i < 16; i++) {
429 		uuid[i] = hex_to_bin(str[opc_map_to_uuid[i]]) << 4;
430 		uuid[i] |= hex_to_bin(str[opc_map_to_uuid[i] + 1]);
431 	}
432 	return AE_OK;
433 }
434 
acpi_run_osc(acpi_handle handle,struct acpi_osc_context * context)435 acpi_status acpi_run_osc(acpi_handle handle, struct acpi_osc_context *context)
436 {
437 	acpi_status status;
438 	struct acpi_object_list input;
439 	union acpi_object in_params[4];
440 	union acpi_object *out_obj;
441 	u8 uuid[16];
442 	u32 errors;
443 	struct acpi_buffer output = {ACPI_ALLOCATE_BUFFER, NULL};
444 
445 	if (!context)
446 		return AE_ERROR;
447 	if (ACPI_FAILURE(acpi_str_to_uuid(context->uuid_str, uuid)))
448 		return AE_ERROR;
449 	context->ret.length = ACPI_ALLOCATE_BUFFER;
450 	context->ret.pointer = NULL;
451 
452 	/* Setting up input parameters */
453 	input.count = 4;
454 	input.pointer = in_params;
455 	in_params[0].type 		= ACPI_TYPE_BUFFER;
456 	in_params[0].buffer.length 	= 16;
457 	in_params[0].buffer.pointer	= uuid;
458 	in_params[1].type 		= ACPI_TYPE_INTEGER;
459 	in_params[1].integer.value 	= context->rev;
460 	in_params[2].type 		= ACPI_TYPE_INTEGER;
461 	in_params[2].integer.value	= context->cap.length/sizeof(u32);
462 	in_params[3].type		= ACPI_TYPE_BUFFER;
463 	in_params[3].buffer.length 	= context->cap.length;
464 	in_params[3].buffer.pointer 	= context->cap.pointer;
465 
466 	status = acpi_evaluate_object(handle, "_OSC", &input, &output);
467 	if (ACPI_FAILURE(status))
468 		return status;
469 
470 	if (!output.length)
471 		return AE_NULL_OBJECT;
472 
473 	out_obj = output.pointer;
474 	if (out_obj->type != ACPI_TYPE_BUFFER
475 		|| out_obj->buffer.length != context->cap.length) {
476 		acpi_print_osc_error(handle, context,
477 			"_OSC evaluation returned wrong type");
478 		status = AE_TYPE;
479 		goto out_kfree;
480 	}
481 	/* Need to ignore the bit0 in result code */
482 	errors = *((u32 *)out_obj->buffer.pointer) & ~(1 << 0);
483 	if (errors) {
484 		if (errors & OSC_REQUEST_ERROR)
485 			acpi_print_osc_error(handle, context,
486 				"_OSC request failed");
487 		if (errors & OSC_INVALID_UUID_ERROR)
488 			acpi_print_osc_error(handle, context,
489 				"_OSC invalid UUID");
490 		if (errors & OSC_INVALID_REVISION_ERROR)
491 			acpi_print_osc_error(handle, context,
492 				"_OSC invalid revision");
493 		if (errors & OSC_CAPABILITIES_MASK_ERROR) {
494 			if (((u32 *)context->cap.pointer)[OSC_QUERY_TYPE]
495 			    & OSC_QUERY_ENABLE)
496 				goto out_success;
497 			status = AE_SUPPORT;
498 			goto out_kfree;
499 		}
500 		status = AE_ERROR;
501 		goto out_kfree;
502 	}
503 out_success:
504 	context->ret.length = out_obj->buffer.length;
505 	context->ret.pointer = kmalloc(context->ret.length, GFP_KERNEL);
506 	if (!context->ret.pointer) {
507 		status =  AE_NO_MEMORY;
508 		goto out_kfree;
509 	}
510 	memcpy(context->ret.pointer, out_obj->buffer.pointer,
511 		context->ret.length);
512 	status =  AE_OK;
513 
514 out_kfree:
515 	kfree(output.pointer);
516 	if (status != AE_OK)
517 		context->ret.pointer = NULL;
518 	return status;
519 }
520 EXPORT_SYMBOL(acpi_run_osc);
521 
522 static u8 sb_uuid_str[] = "0811B06E-4A27-44F9-8D60-3CBBC22E7B48";
acpi_bus_osc_support(void)523 static void acpi_bus_osc_support(void)
524 {
525 	u32 capbuf[2];
526 	struct acpi_osc_context context = {
527 		.uuid_str = sb_uuid_str,
528 		.rev = 1,
529 		.cap.length = 8,
530 		.cap.pointer = capbuf,
531 	};
532 	acpi_handle handle;
533 
534 	capbuf[OSC_QUERY_TYPE] = OSC_QUERY_ENABLE;
535 	capbuf[OSC_SUPPORT_TYPE] = OSC_SB_PR3_SUPPORT; /* _PR3 is in use */
536 #if defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR) ||\
537 			defined(CONFIG_ACPI_PROCESSOR_AGGREGATOR_MODULE)
538 	capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PAD_SUPPORT;
539 #endif
540 
541 #if defined(CONFIG_ACPI_PROCESSOR) || defined(CONFIG_ACPI_PROCESSOR_MODULE)
542 	capbuf[OSC_SUPPORT_TYPE] |= OSC_SB_PPC_OST_SUPPORT;
543 #endif
544 	if (ACPI_FAILURE(acpi_get_handle(NULL, "\\_SB", &handle)))
545 		return;
546 	if (ACPI_SUCCESS(acpi_run_osc(handle, &context)))
547 		kfree(context.ret.pointer);
548 	/* do we need to check the returned cap? Sounds no */
549 }
550 
551 /* --------------------------------------------------------------------------
552                                 Event Management
553    -------------------------------------------------------------------------- */
554 
555 #ifdef CONFIG_ACPI_PROC_EVENT
556 static DEFINE_SPINLOCK(acpi_bus_event_lock);
557 
558 LIST_HEAD(acpi_bus_event_list);
559 DECLARE_WAIT_QUEUE_HEAD(acpi_bus_event_queue);
560 
561 extern int event_is_open;
562 
acpi_bus_generate_proc_event4(const char * device_class,const char * bus_id,u8 type,int data)563 int acpi_bus_generate_proc_event4(const char *device_class, const char *bus_id, u8 type, int data)
564 {
565 	struct acpi_bus_event *event;
566 	unsigned long flags = 0;
567 
568 	/* drop event on the floor if no one's listening */
569 	if (!event_is_open)
570 		return 0;
571 
572 	event = kzalloc(sizeof(struct acpi_bus_event), GFP_ATOMIC);
573 	if (!event)
574 		return -ENOMEM;
575 
576 	strcpy(event->device_class, device_class);
577 	strcpy(event->bus_id, bus_id);
578 	event->type = type;
579 	event->data = data;
580 
581 	spin_lock_irqsave(&acpi_bus_event_lock, flags);
582 	list_add_tail(&event->node, &acpi_bus_event_list);
583 	spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
584 
585 	wake_up_interruptible(&acpi_bus_event_queue);
586 
587 	return 0;
588 
589 }
590 
591 EXPORT_SYMBOL_GPL(acpi_bus_generate_proc_event4);
592 
acpi_bus_generate_proc_event(struct acpi_device * device,u8 type,int data)593 int acpi_bus_generate_proc_event(struct acpi_device *device, u8 type, int data)
594 {
595 	if (!device)
596 		return -EINVAL;
597 	return acpi_bus_generate_proc_event4(device->pnp.device_class,
598 					     device->pnp.bus_id, type, data);
599 }
600 
601 EXPORT_SYMBOL(acpi_bus_generate_proc_event);
602 
acpi_bus_receive_event(struct acpi_bus_event * event)603 int acpi_bus_receive_event(struct acpi_bus_event *event)
604 {
605 	unsigned long flags = 0;
606 	struct acpi_bus_event *entry = NULL;
607 
608 	DECLARE_WAITQUEUE(wait, current);
609 
610 
611 	if (!event)
612 		return -EINVAL;
613 
614 	if (list_empty(&acpi_bus_event_list)) {
615 
616 		set_current_state(TASK_INTERRUPTIBLE);
617 		add_wait_queue(&acpi_bus_event_queue, &wait);
618 
619 		if (list_empty(&acpi_bus_event_list))
620 			schedule();
621 
622 		remove_wait_queue(&acpi_bus_event_queue, &wait);
623 		set_current_state(TASK_RUNNING);
624 
625 		if (signal_pending(current))
626 			return -ERESTARTSYS;
627 	}
628 
629 	spin_lock_irqsave(&acpi_bus_event_lock, flags);
630 	if (!list_empty(&acpi_bus_event_list)) {
631 		entry = list_entry(acpi_bus_event_list.next,
632 				   struct acpi_bus_event, node);
633 		list_del(&entry->node);
634 	}
635 	spin_unlock_irqrestore(&acpi_bus_event_lock, flags);
636 
637 	if (!entry)
638 		return -ENODEV;
639 
640 	memcpy(event, entry, sizeof(struct acpi_bus_event));
641 
642 	kfree(entry);
643 
644 	return 0;
645 }
646 
647 #endif	/* CONFIG_ACPI_PROC_EVENT */
648 
649 /* --------------------------------------------------------------------------
650                              Notification Handling
651    -------------------------------------------------------------------------- */
652 
acpi_bus_check_device(acpi_handle handle)653 static void acpi_bus_check_device(acpi_handle handle)
654 {
655 	struct acpi_device *device;
656 	acpi_status status;
657 	struct acpi_device_status old_status;
658 
659 	if (acpi_bus_get_device(handle, &device))
660 		return;
661 	if (!device)
662 		return;
663 
664 	old_status = device->status;
665 
666 	/*
667 	 * Make sure this device's parent is present before we go about
668 	 * messing with the device.
669 	 */
670 	if (device->parent && !device->parent->status.present) {
671 		device->status = device->parent->status;
672 		return;
673 	}
674 
675 	status = acpi_bus_get_status(device);
676 	if (ACPI_FAILURE(status))
677 		return;
678 
679 	if (STRUCT_TO_INT(old_status) == STRUCT_TO_INT(device->status))
680 		return;
681 
682 	/*
683 	 * Device Insertion/Removal
684 	 */
685 	if ((device->status.present) && !(old_status.present)) {
686 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device insertion detected\n"));
687 		/* TBD: Handle device insertion */
688 	} else if (!(device->status.present) && (old_status.present)) {
689 		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device removal detected\n"));
690 		/* TBD: Handle device removal */
691 	}
692 }
693 
acpi_bus_check_scope(acpi_handle handle)694 static void acpi_bus_check_scope(acpi_handle handle)
695 {
696 	/* Status Change? */
697 	acpi_bus_check_device(handle);
698 
699 	/*
700 	 * TBD: Enumerate child devices within this device's scope and
701 	 *       run acpi_bus_check_device()'s on them.
702 	 */
703 }
704 
705 static BLOCKING_NOTIFIER_HEAD(acpi_bus_notify_list);
register_acpi_bus_notifier(struct notifier_block * nb)706 int register_acpi_bus_notifier(struct notifier_block *nb)
707 {
708 	return blocking_notifier_chain_register(&acpi_bus_notify_list, nb);
709 }
710 EXPORT_SYMBOL_GPL(register_acpi_bus_notifier);
711 
unregister_acpi_bus_notifier(struct notifier_block * nb)712 void unregister_acpi_bus_notifier(struct notifier_block *nb)
713 {
714 	blocking_notifier_chain_unregister(&acpi_bus_notify_list, nb);
715 }
716 EXPORT_SYMBOL_GPL(unregister_acpi_bus_notifier);
717 
718 /**
719  * acpi_bus_notify
720  * ---------------
721  * Callback for all 'system-level' device notifications (values 0x00-0x7F).
722  */
acpi_bus_notify(acpi_handle handle,u32 type,void * data)723 static void acpi_bus_notify(acpi_handle handle, u32 type, void *data)
724 {
725 	struct acpi_device *device = NULL;
726 	struct acpi_driver *driver;
727 
728 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Notification %#02x to handle %p\n",
729 			  type, handle));
730 
731 	blocking_notifier_call_chain(&acpi_bus_notify_list,
732 		type, (void *)handle);
733 
734 	switch (type) {
735 
736 	case ACPI_NOTIFY_BUS_CHECK:
737 		acpi_bus_check_scope(handle);
738 		/*
739 		 * TBD: We'll need to outsource certain events to non-ACPI
740 		 *      drivers via the device manager (device.c).
741 		 */
742 		break;
743 
744 	case ACPI_NOTIFY_DEVICE_CHECK:
745 		acpi_bus_check_device(handle);
746 		/*
747 		 * TBD: We'll need to outsource certain events to non-ACPI
748 		 *      drivers via the device manager (device.c).
749 		 */
750 		break;
751 
752 	case ACPI_NOTIFY_DEVICE_WAKE:
753 		/* TBD */
754 		break;
755 
756 	case ACPI_NOTIFY_EJECT_REQUEST:
757 		/* TBD */
758 		break;
759 
760 	case ACPI_NOTIFY_DEVICE_CHECK_LIGHT:
761 		/* TBD: Exactly what does 'light' mean? */
762 		break;
763 
764 	case ACPI_NOTIFY_FREQUENCY_MISMATCH:
765 		/* TBD */
766 		break;
767 
768 	case ACPI_NOTIFY_BUS_MODE_MISMATCH:
769 		/* TBD */
770 		break;
771 
772 	case ACPI_NOTIFY_POWER_FAULT:
773 		/* TBD */
774 		break;
775 
776 	default:
777 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
778 				  "Received unknown/unsupported notification [%08x]\n",
779 				  type));
780 		break;
781 	}
782 
783 	acpi_bus_get_device(handle, &device);
784 	if (device) {
785 		driver = device->driver;
786 		if (driver && driver->ops.notify &&
787 		    (driver->flags & ACPI_DRIVER_ALL_NOTIFY_EVENTS))
788 			driver->ops.notify(device, type);
789 	}
790 }
791 
792 /* --------------------------------------------------------------------------
793                              Initialization/Cleanup
794    -------------------------------------------------------------------------- */
795 
acpi_bus_init_irq(void)796 static int __init acpi_bus_init_irq(void)
797 {
798 	acpi_status status = AE_OK;
799 	union acpi_object arg = { ACPI_TYPE_INTEGER };
800 	struct acpi_object_list arg_list = { 1, &arg };
801 	char *message = NULL;
802 
803 
804 	/*
805 	 * Let the system know what interrupt model we are using by
806 	 * evaluating the \_PIC object, if exists.
807 	 */
808 
809 	switch (acpi_irq_model) {
810 	case ACPI_IRQ_MODEL_PIC:
811 		message = "PIC";
812 		break;
813 	case ACPI_IRQ_MODEL_IOAPIC:
814 		message = "IOAPIC";
815 		break;
816 	case ACPI_IRQ_MODEL_IOSAPIC:
817 		message = "IOSAPIC";
818 		break;
819 	case ACPI_IRQ_MODEL_PLATFORM:
820 		message = "platform specific model";
821 		break;
822 	default:
823 		printk(KERN_WARNING PREFIX "Unknown interrupt routing model\n");
824 		return -ENODEV;
825 	}
826 
827 	printk(KERN_INFO PREFIX "Using %s for interrupt routing\n", message);
828 
829 	arg.integer.value = acpi_irq_model;
830 
831 	status = acpi_evaluate_object(NULL, "\\_PIC", &arg_list, NULL);
832 	if (ACPI_FAILURE(status) && (status != AE_NOT_FOUND)) {
833 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PIC"));
834 		return -ENODEV;
835 	}
836 
837 	return 0;
838 }
839 
840 u8 acpi_gbl_permanent_mmap;
841 
842 
acpi_early_init(void)843 void __init acpi_early_init(void)
844 {
845 	acpi_status status = AE_OK;
846 
847 	if (acpi_disabled)
848 		return;
849 
850 	printk(KERN_INFO PREFIX "Core revision %08x\n", ACPI_CA_VERSION);
851 
852 	/* enable workarounds, unless strict ACPI spec. compliance */
853 	if (!acpi_strict)
854 		acpi_gbl_enable_interpreter_slack = TRUE;
855 
856 	acpi_gbl_permanent_mmap = 1;
857 
858 	/*
859 	 * If the machine falls into the DMI check table,
860 	 * DSDT will be copied to memory
861 	 */
862 	dmi_check_system(dsdt_dmi_table);
863 
864 	status = acpi_reallocate_root_table();
865 	if (ACPI_FAILURE(status)) {
866 		printk(KERN_ERR PREFIX
867 		       "Unable to reallocate ACPI tables\n");
868 		goto error0;
869 	}
870 
871 	status = acpi_initialize_subsystem();
872 	if (ACPI_FAILURE(status)) {
873 		printk(KERN_ERR PREFIX
874 		       "Unable to initialize the ACPI Interpreter\n");
875 		goto error0;
876 	}
877 
878 	status = acpi_load_tables();
879 	if (ACPI_FAILURE(status)) {
880 		printk(KERN_ERR PREFIX
881 		       "Unable to load the System Description Tables\n");
882 		goto error0;
883 	}
884 
885 #ifdef CONFIG_X86
886 	if (!acpi_ioapic) {
887 		/* compatible (0) means level (3) */
888 		if (!(acpi_sci_flags & ACPI_MADT_TRIGGER_MASK)) {
889 			acpi_sci_flags &= ~ACPI_MADT_TRIGGER_MASK;
890 			acpi_sci_flags |= ACPI_MADT_TRIGGER_LEVEL;
891 		}
892 		/* Set PIC-mode SCI trigger type */
893 		acpi_pic_sci_set_trigger(acpi_gbl_FADT.sci_interrupt,
894 					 (acpi_sci_flags & ACPI_MADT_TRIGGER_MASK) >> 2);
895 	} else {
896 		/*
897 		 * now that acpi_gbl_FADT is initialized,
898 		 * update it with result from INT_SRC_OVR parsing
899 		 */
900 		acpi_gbl_FADT.sci_interrupt = acpi_sci_override_gsi;
901 	}
902 #endif
903 
904 	status =
905 	    acpi_enable_subsystem(~
906 				  (ACPI_NO_HARDWARE_INIT |
907 				   ACPI_NO_ACPI_ENABLE));
908 	if (ACPI_FAILURE(status)) {
909 		printk(KERN_ERR PREFIX "Unable to enable ACPI\n");
910 		goto error0;
911 	}
912 
913 	return;
914 
915       error0:
916 	disable_acpi();
917 	return;
918 }
919 
acpi_bus_init(void)920 static int __init acpi_bus_init(void)
921 {
922 	int result = 0;
923 	acpi_status status = AE_OK;
924 	extern acpi_status acpi_os_initialize1(void);
925 
926 	acpi_os_initialize1();
927 
928 	status =
929 	    acpi_enable_subsystem(ACPI_NO_HARDWARE_INIT | ACPI_NO_ACPI_ENABLE);
930 	if (ACPI_FAILURE(status)) {
931 		printk(KERN_ERR PREFIX
932 		       "Unable to start the ACPI Interpreter\n");
933 		goto error1;
934 	}
935 
936 	/*
937 	 * ACPI 2.0 requires the EC driver to be loaded and work before
938 	 * the EC device is found in the namespace (i.e. before acpi_initialize_objects()
939 	 * is called).
940 	 *
941 	 * This is accomplished by looking for the ECDT table, and getting
942 	 * the EC parameters out of that.
943 	 */
944 	status = acpi_ec_ecdt_probe();
945 	/* Ignore result. Not having an ECDT is not fatal. */
946 
947 	acpi_bus_osc_support();
948 
949 	status = acpi_initialize_objects(ACPI_FULL_INITIALIZATION);
950 	if (ACPI_FAILURE(status)) {
951 		printk(KERN_ERR PREFIX "Unable to initialize ACPI objects\n");
952 		goto error1;
953 	}
954 
955 	/*
956 	 * _PDC control method may load dynamic SSDT tables,
957 	 * and we need to install the table handler before that.
958 	 */
959 	acpi_sysfs_init();
960 
961 	acpi_early_processor_set_pdc();
962 
963 	/*
964 	 * Maybe EC region is required at bus_scan/acpi_get_devices. So it
965 	 * is necessary to enable it as early as possible.
966 	 */
967 	acpi_boot_ec_enable();
968 
969 	printk(KERN_INFO PREFIX "Interpreter enabled\n");
970 
971 	/* Initialize sleep structures */
972 	acpi_sleep_init();
973 
974 	/*
975 	 * Get the system interrupt model and evaluate \_PIC.
976 	 */
977 	result = acpi_bus_init_irq();
978 	if (result)
979 		goto error1;
980 
981 	/*
982 	 * Register the for all standard device notifications.
983 	 */
984 	status =
985 	    acpi_install_notify_handler(ACPI_ROOT_OBJECT, ACPI_SYSTEM_NOTIFY,
986 					&acpi_bus_notify, NULL);
987 	if (ACPI_FAILURE(status)) {
988 		printk(KERN_ERR PREFIX
989 		       "Unable to register for device notifications\n");
990 		goto error1;
991 	}
992 
993 	/*
994 	 * Create the top ACPI proc directory
995 	 */
996 	acpi_root_dir = proc_mkdir(ACPI_BUS_FILE_ROOT, NULL);
997 
998 	return 0;
999 
1000 	/* Mimic structured exception handling */
1001       error1:
1002 	acpi_terminate();
1003 	return -ENODEV;
1004 }
1005 
1006 struct kobject *acpi_kobj;
1007 
acpi_init(void)1008 static int __init acpi_init(void)
1009 {
1010 	int result;
1011 
1012 	if (acpi_disabled) {
1013 		printk(KERN_INFO PREFIX "Interpreter disabled.\n");
1014 		return -ENODEV;
1015 	}
1016 
1017 	acpi_kobj = kobject_create_and_add("acpi", firmware_kobj);
1018 	if (!acpi_kobj) {
1019 		printk(KERN_WARNING "%s: kset create error\n", __func__);
1020 		acpi_kobj = NULL;
1021 	}
1022 
1023 	init_acpi_device_notify();
1024 	result = acpi_bus_init();
1025 	if (result) {
1026 		disable_acpi();
1027 		return result;
1028 	}
1029 
1030 	pci_mmcfg_late_init();
1031 	acpi_scan_init();
1032 	acpi_ec_init();
1033 	acpi_debugfs_init();
1034 	acpi_sleep_proc_init();
1035 	acpi_wakeup_device_init();
1036 	return 0;
1037 }
1038 
1039 subsys_initcall(acpi_init);
1040