xref: /openbmc/linux/drivers/acpi/container.c (revision 8fdff1dc)
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 ACPI_CONTAINER_DEVICE_NAME	"ACPI container device"
42 #define ACPI_CONTAINER_CLASS		"container"
43 
44 #define INSTALL_NOTIFY_HANDLER		1
45 #define UNINSTALL_NOTIFY_HANDLER	2
46 
47 #define _COMPONENT			ACPI_CONTAINER_COMPONENT
48 ACPI_MODULE_NAME("container");
49 
50 MODULE_AUTHOR("Anil S Keshavamurthy");
51 MODULE_DESCRIPTION("ACPI container driver");
52 MODULE_LICENSE("GPL");
53 
54 static int acpi_container_add(struct acpi_device *device);
55 static int acpi_container_remove(struct acpi_device *device, int type);
56 
57 static const struct acpi_device_id container_device_ids[] = {
58 	{"ACPI0004", 0},
59 	{"PNP0A05", 0},
60 	{"PNP0A06", 0},
61 	{"", 0},
62 };
63 MODULE_DEVICE_TABLE(acpi, container_device_ids);
64 
65 static struct acpi_driver acpi_container_driver = {
66 	.name = "container",
67 	.class = ACPI_CONTAINER_CLASS,
68 	.ids = container_device_ids,
69 	.ops = {
70 		.add = acpi_container_add,
71 		.remove = acpi_container_remove,
72 		},
73 };
74 
75 /*******************************************************************/
76 
77 static int is_device_present(acpi_handle handle)
78 {
79 	acpi_handle temp;
80 	acpi_status status;
81 	unsigned long long sta;
82 
83 
84 	status = acpi_get_handle(handle, "_STA", &temp);
85 	if (ACPI_FAILURE(status))
86 		return 1;	/* _STA not found, assume device present */
87 
88 	status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
89 	if (ACPI_FAILURE(status))
90 		return 0;	/* Firmware error */
91 
92 	return ((sta & ACPI_STA_DEVICE_PRESENT) == ACPI_STA_DEVICE_PRESENT);
93 }
94 
95 static bool is_container_device(const char *hid)
96 {
97 	const struct acpi_device_id *container_id;
98 
99 	for (container_id = container_device_ids;
100 	     container_id->id[0]; container_id++) {
101 		if (!strcmp((char *)container_id->id, hid))
102 			return true;
103 	}
104 
105 	return false;
106 }
107 
108 /*******************************************************************/
109 static int acpi_container_add(struct acpi_device *device)
110 {
111 	struct acpi_container *container;
112 
113 	container = kzalloc(sizeof(struct acpi_container), GFP_KERNEL);
114 	if (!container)
115 		return -ENOMEM;
116 
117 	container->handle = device->handle;
118 	strcpy(acpi_device_name(device), ACPI_CONTAINER_DEVICE_NAME);
119 	strcpy(acpi_device_class(device), ACPI_CONTAINER_CLASS);
120 	device->driver_data = container;
121 
122 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Device <%s> bid <%s>\n",
123 			  acpi_device_name(device), acpi_device_bid(device)));
124 
125 	return 0;
126 }
127 
128 static int acpi_container_remove(struct acpi_device *device, int type)
129 {
130 	acpi_status status = AE_OK;
131 	struct acpi_container *pc = NULL;
132 
133 	pc = acpi_driver_data(device);
134 	kfree(pc);
135 	return status;
136 }
137 
138 static int container_device_add(struct acpi_device **device, acpi_handle handle)
139 {
140 	acpi_handle phandle;
141 	struct acpi_device *pdev;
142 	int result;
143 
144 
145 	if (acpi_get_parent(handle, &phandle)) {
146 		return -ENODEV;
147 	}
148 
149 	if (acpi_bus_get_device(phandle, &pdev)) {
150 		return -ENODEV;
151 	}
152 
153 	if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_DEVICE)) {
154 		return -ENODEV;
155 	}
156 
157 	result = acpi_bus_start(*device);
158 
159 	return result;
160 }
161 
162 static void container_notify_cb(acpi_handle handle, u32 type, void *context)
163 {
164 	struct acpi_device *device = NULL;
165 	int result;
166 	int present;
167 	acpi_status status;
168 	u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
169 
170 	switch (type) {
171 	case ACPI_NOTIFY_BUS_CHECK:
172 		/* Fall through */
173 	case ACPI_NOTIFY_DEVICE_CHECK:
174 		pr_debug("Container driver received %s event\n",
175 		       (type == ACPI_NOTIFY_BUS_CHECK) ?
176 		       "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK");
177 
178 		present = is_device_present(handle);
179 		status = acpi_bus_get_device(handle, &device);
180 		if (!present) {
181 			if (ACPI_SUCCESS(status)) {
182 				/* device exist and this is a remove request */
183 				device->flags.eject_pending = 1;
184 				kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
185 				return;
186 			}
187 			break;
188 		}
189 
190 		if (!ACPI_FAILURE(status) || device)
191 			break;
192 
193 		result = container_device_add(&device, handle);
194 		if (result) {
195 			acpi_handle_warn(handle, "Failed to add container\n");
196 			break;
197 		}
198 
199 		kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
200 		ost_code = ACPI_OST_SC_SUCCESS;
201 		break;
202 
203 	case ACPI_NOTIFY_EJECT_REQUEST:
204 		if (!acpi_bus_get_device(handle, &device) && device) {
205 			device->flags.eject_pending = 1;
206 			kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
207 			return;
208 		}
209 		break;
210 
211 	default:
212 		/* non-hotplug event; possibly handled by other handler */
213 		return;
214 	}
215 
216 	/* Inform firmware that the hotplug operation has completed */
217 	(void) acpi_evaluate_hotplug_ost(handle, type, ost_code, NULL);
218 	return;
219 }
220 
221 static acpi_status
222 container_walk_namespace_cb(acpi_handle handle,
223 			    u32 lvl, void *context, void **rv)
224 {
225 	char *hid = NULL;
226 	struct acpi_device_info *info;
227 	acpi_status status;
228 	int *action = context;
229 
230 	status = acpi_get_object_info(handle, &info);
231 	if (ACPI_FAILURE(status)) {
232 		return AE_OK;
233 	}
234 
235 	if (info->valid & ACPI_VALID_HID)
236 		hid = info->hardware_id.string;
237 
238 	if (hid == NULL) {
239 		goto end;
240 	}
241 
242 	if (!is_container_device(hid))
243 		goto end;
244 
245 	switch (*action) {
246 	case INSTALL_NOTIFY_HANDLER:
247 		acpi_install_notify_handler(handle,
248 					    ACPI_SYSTEM_NOTIFY,
249 					    container_notify_cb, NULL);
250 		break;
251 	case UNINSTALL_NOTIFY_HANDLER:
252 		acpi_remove_notify_handler(handle,
253 					   ACPI_SYSTEM_NOTIFY,
254 					   container_notify_cb);
255 		break;
256 	default:
257 		break;
258 	}
259 
260       end:
261 	kfree(info);
262 
263 	return AE_OK;
264 }
265 
266 static int __init acpi_container_init(void)
267 {
268 	int result = 0;
269 	int action = INSTALL_NOTIFY_HANDLER;
270 
271 	result = acpi_bus_register_driver(&acpi_container_driver);
272 	if (result < 0) {
273 		return (result);
274 	}
275 
276 	/* register notify handler to every container device */
277 	acpi_walk_namespace(ACPI_TYPE_DEVICE,
278 			    ACPI_ROOT_OBJECT,
279 			    ACPI_UINT32_MAX,
280 			    container_walk_namespace_cb, NULL, &action, NULL);
281 
282 	return (0);
283 }
284 
285 static void __exit acpi_container_exit(void)
286 {
287 	int action = UNINSTALL_NOTIFY_HANDLER;
288 
289 
290 	acpi_walk_namespace(ACPI_TYPE_DEVICE,
291 			    ACPI_ROOT_OBJECT,
292 			    ACPI_UINT32_MAX,
293 			    container_walk_namespace_cb, NULL, &action, NULL);
294 
295 	acpi_bus_unregister_driver(&acpi_container_driver);
296 
297 	return;
298 }
299 
300 module_init(acpi_container_init);
301 module_exit(acpi_container_exit);
302