xref: /openbmc/linux/drivers/acpi/container.c (revision 737f1a9f808280c481681b1f46254fd67023ec2f)
1 /*
2  * acpi_container.c  - ACPI Generic Container Driver
3  * ($Revision: )
4  *
5  * Copyright (C) 2004 Anil S Keshavamurthy (anil.s.keshavamurthy@intel.com)
6  * Copyright (C) 2004 Keiichiro Tokunaga (tokunaga.keiich@jp.fujitsu.com)
7  * Copyright (C) 2004 Motoyuki Ito (motoyuki@soft.fujitsu.com)
8  * Copyright (C) 2004 Intel Corp.
9  * Copyright (C) 2004 FUJITSU LIMITED
10  *
11  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
12  *
13  *  This program is free software; you can redistribute it and/or modify
14  *  it under the terms of the GNU General Public License as published by
15  *  the Free Software Foundation; either version 2 of the License, or (at
16  *  your option) any later version.
17  *
18  *  This program is distributed in the hope that it will be useful, but
19  *  WITHOUT ANY WARRANTY; without even the implied warranty of
20  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21  *  General Public License for more details.
22  *
23  *  You should have received a copy of the GNU General Public License along
24  *  with this program; if not, write to the Free Software Foundation, Inc.,
25  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26  *
27  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28  */
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/slab.h>
33 #include <linux/types.h>
34 #include <linux/acpi.h>
35 #include <acpi/acpi_bus.h>
36 #include <acpi/acpi_drivers.h>
37 #include <acpi/container.h>
38 
39 #define PREFIX "ACPI: "
40 
41 #define _COMPONENT			ACPI_CONTAINER_COMPONENT
42 ACPI_MODULE_NAME("container");
43 
44 static const struct acpi_device_id container_device_ids[] = {
45 	{"ACPI0004", 0},
46 	{"PNP0A05", 0},
47 	{"PNP0A06", 0},
48 	{"", 0},
49 };
50 
51 static int container_device_attach(struct acpi_device *device,
52 				   const struct acpi_device_id *not_used)
53 {
54 	/*
55 	 * FIXME: This is necessary, so that acpi_eject_store() doesn't return
56 	 * -ENODEV for containers.
57 	 */
58 	return 1;
59 }
60 
61 static struct acpi_scan_handler container_device_handler = {
62 	.ids = container_device_ids,
63 	.attach = container_device_attach,
64 };
65 
66 static int is_device_present(acpi_handle handle)
67 {
68 	acpi_handle temp;
69 	acpi_status status;
70 	unsigned long long sta;
71 
72 
73 	status = acpi_get_handle(handle, "_STA", &temp);
74 	if (ACPI_FAILURE(status))
75 		return 1;	/* _STA not found, assume device present */
76 
77 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
78 	if (ACPI_FAILURE(status))
79 		return 0;	/* Firmware error */
80 
81 	return ((sta & ACPI_STA_DEVICE_PRESENT) == ACPI_STA_DEVICE_PRESENT);
82 }
83 
84 static void container_notify_cb(acpi_handle handle, u32 type, void *context)
85 {
86 	struct acpi_device *device = NULL;
87 	int result;
88 	int present;
89 	acpi_status status;
90 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
91 
92 	switch (type) {
93 	case ACPI_NOTIFY_BUS_CHECK:
94 		/* Fall through */
95 	case ACPI_NOTIFY_DEVICE_CHECK:
96 		pr_debug("Container driver received %s event\n",
97 		       (type == ACPI_NOTIFY_BUS_CHECK) ?
98 		       "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK");
99 
100 		present = is_device_present(handle);
101 		status = acpi_bus_get_device(handle, &device);
102 		if (!present) {
103 			if (ACPI_SUCCESS(status)) {
104 				/* device exist and this is a remove request */
105 				device->flags.eject_pending = 1;
106 				kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
107 				return;
108 			}
109 			break;
110 		}
111 
112 		if (!ACPI_FAILURE(status) || device)
113 			break;
114 
115 		result = acpi_bus_scan(handle);
116 		if (result) {
117 			acpi_handle_warn(handle, "Failed to add container\n");
118 			break;
119 		}
120 		result = acpi_bus_get_device(handle, &device);
121 		if (result) {
122 			acpi_handle_warn(handle, "Missing device object\n");
123 			break;
124 		}
125 
126 		kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
127 		ost_code = ACPI_OST_SC_SUCCESS;
128 		break;
129 
130 	case ACPI_NOTIFY_EJECT_REQUEST:
131 		if (!acpi_bus_get_device(handle, &device) && device) {
132 			device->flags.eject_pending = 1;
133 			kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
134 			return;
135 		}
136 		break;
137 
138 	default:
139 		/* non-hotplug event; possibly handled by other handler */
140 		return;
141 	}
142 
143 	/* Inform firmware that the hotplug operation has completed */
144 	(void) acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
145 	return;
146 }
147 
148 static bool is_container(acpi_handle handle)
149 {
150 	struct acpi_device_info *info;
151 	bool ret = false;
152 
153 	if (ACPI_FAILURE(acpi_get_object_info(handle, &info)))
154 		return false;
155 
156 	if (info->valid & ACPI_VALID_HID) {
157 		const struct acpi_device_id *id;
158 
159 		for (id = container_device_ids; id->id[0]; id++) {
160 			ret = !strcmp((char *)id->id, info->hardware_id.string);
161 			if (ret)
162 				break;
163 		}
164 	}
165 	kfree(info);
166 	return ret;
167 }
168 
169 static acpi_status acpi_container_register_notify_handler(acpi_handle handle,
170 							  u32 lvl, void *ctxt,
171 							  void **retv)
172 {
173 	if (is_container(handle))
174 		acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
175 					    container_notify_cb, NULL);
176 
177 	return AE_OK;
178 }
179 
180 void __init acpi_container_init(void)
181 {
182 	acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT, ACPI_UINT32_MAX,
183 			    acpi_container_register_notify_handler, NULL,
184 			    NULL, NULL);
185 
186 	acpi_scan_add_handler(&container_device_handler);
187 }
188