xref: /freebsd/sys/dev/acpica/acpi_dock.c (revision 38a52bd3b5cac3da6f7f6eef3dd050e6aa08ebb3)
1 /*-
2  * Copyright (c) 2005-2006 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include "opt_acpi.h"
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 
35 #include <contrib/dev/acpica/include/acpi.h>
36 #include <contrib/dev/acpica/include/accommon.h>
37 
38 #include <dev/acpica/acpivar.h>
39 #include <dev/acpica/acpiio.h>
40 
41 /* Hooks for the ACPI CA debugging infrastructure */
42 #define _COMPONENT	ACPI_DOCK
43 ACPI_MODULE_NAME("DOCK")
44 
45 /* For Docking status */
46 #define ACPI_DOCK_STATUS_UNKNOWN	-1
47 #define ACPI_DOCK_STATUS_UNDOCKED	0
48 #define ACPI_DOCK_STATUS_DOCKED		1
49 
50 #define ACPI_DOCK_UNLOCK		0 /* Allow device to be ejected */
51 #define ACPI_DOCK_LOCK			1 /* Prevent dev from being removed */
52 
53 #define ACPI_DOCK_ISOLATE		0 /* Isolate from dock connector */
54 #define ACPI_DOCK_CONNECT		1 /* Connect to dock */
55 
56 struct acpi_dock_softc {
57 	int		_sta;
58 	int		_bdn;
59 	int		_uid;
60 	int		status;
61 	struct sysctl_ctx_list	*sysctl_ctx;
62 	struct sysctl_oid	*sysctl_tree;
63 };
64 
65 ACPI_SERIAL_DECL(dock, "ACPI Docking Station");
66 
67 static char *acpi_dock_pnp_ids[] = {"PNP0C15", NULL};
68 
69 /*
70  * Utility functions
71  */
72 
73 static void
74 acpi_dock_get_info(device_t dev)
75 {
76 	struct acpi_dock_softc *sc;
77 	ACPI_HANDLE	h;
78 
79 	sc = device_get_softc(dev);
80 	h = acpi_get_handle(dev);
81 
82 	if (ACPI_FAILURE(acpi_GetInteger(h, "_STA", &sc->_sta)))
83 		sc->_sta = ACPI_DOCK_STATUS_UNKNOWN;
84 	if (ACPI_FAILURE(acpi_GetInteger(h, "_BDN", &sc->_bdn)))
85 		sc->_bdn = ACPI_DOCK_STATUS_UNKNOWN;
86 	if (ACPI_FAILURE(acpi_GetInteger(h, "_UID", &sc->_uid)))
87 		sc->_uid = ACPI_DOCK_STATUS_UNKNOWN;
88 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
89 		    "_STA: %04x, _BDN: %04x, _UID: %04x\n", sc->_sta,
90 		    sc->_bdn, sc->_uid);
91 }
92 
93 static int
94 acpi_dock_execute_dck(device_t dev, int dock)
95 {
96 	ACPI_HANDLE	h;
97 	ACPI_OBJECT	argobj;
98 	ACPI_OBJECT_LIST args;
99 	ACPI_BUFFER	buf;
100 	ACPI_OBJECT	retobj;
101 	ACPI_STATUS	status;
102 
103 	h = acpi_get_handle(dev);
104 
105 	argobj.Type = ACPI_TYPE_INTEGER;
106 	argobj.Integer.Value = dock;
107 	args.Count = 1;
108 	args.Pointer = &argobj;
109 	buf.Pointer = &retobj;
110 	buf.Length = sizeof(retobj);
111 	status = AcpiEvaluateObject(h, "_DCK", &args, &buf);
112 
113 	/*
114 	 * When _DCK is called with 0, OSPM will ignore the return value.
115 	 */
116 	if (dock == ACPI_DOCK_ISOLATE)
117 		return (0);
118 
119 	/* If _DCK returned 1, the request succeeded. */
120 	if (ACPI_SUCCESS(status) && retobj.Type == ACPI_TYPE_INTEGER &&
121 	    retobj.Integer.Value == 1)
122 		return (0);
123 
124 	return (-1);
125 }
126 
127 /* Lock devices while docked to prevent surprise removal. */
128 static void
129 acpi_dock_execute_lck(device_t dev, int lock)
130 {
131 	ACPI_HANDLE	h;
132 
133 	h = acpi_get_handle(dev);
134 	acpi_SetInteger(h, "_LCK", lock);
135 }
136 
137 /* Eject a device (i.e., motorized). */
138 static int
139 acpi_dock_execute_ejx(device_t dev, int eject, int state)
140 {
141 	ACPI_HANDLE	h;
142 	ACPI_STATUS	status;
143 	char		ejx[5];
144 
145 	h = acpi_get_handle(dev);
146 	snprintf(ejx, sizeof(ejx), "_EJ%d", state);
147 	status = acpi_SetInteger(h, ejx, eject);
148 	if (ACPI_SUCCESS(status))
149 		return (0);
150 
151 	return (-1);
152 }
153 
154 /* Find dependent devices.  When their parent is removed, so are they. */
155 static int
156 acpi_dock_is_ejd_device(ACPI_HANDLE dock_handle, ACPI_HANDLE handle)
157 {
158 	int		ret;
159 	ACPI_STATUS	ret_status;
160 	ACPI_BUFFER	ejd_buffer;
161 	ACPI_OBJECT	*obj;
162 
163 	ret = 0;
164 
165 	ejd_buffer.Pointer = NULL;
166 	ejd_buffer.Length = ACPI_ALLOCATE_BUFFER;
167 	ret_status = AcpiEvaluateObject(handle, "_EJD", NULL, &ejd_buffer);
168 	if (ACPI_FAILURE(ret_status))
169 		goto out;
170 
171 	obj = (ACPI_OBJECT *)ejd_buffer.Pointer;
172 	if (dock_handle == acpi_GetReference(NULL, obj))
173 		ret = 1;
174 
175 out:
176 	if (ejd_buffer.Pointer != NULL)
177 		AcpiOsFree(ejd_buffer.Pointer);
178 
179 	return (ret);
180 }
181 
182 /*
183  * Docking functions
184  */
185 
186 static void
187 acpi_dock_attach_later(void *context)
188 {
189 	device_t	dev;
190 
191 	dev = (device_t)context;
192 
193 	if (!device_is_enabled(dev))
194 		device_enable(dev);
195 
196 	bus_topo_lock();
197 	device_probe_and_attach(dev);
198 	bus_topo_unlock();
199 }
200 
201 static ACPI_STATUS
202 acpi_dock_insert_child(ACPI_HANDLE handle, UINT32 level, void *context,
203     void **status)
204 {
205 	device_t	dock_dev, dev;
206 	ACPI_HANDLE	dock_handle;
207 
208 	dock_dev = (device_t)context;
209 	dock_handle = acpi_get_handle(dock_dev);
210 
211 	if (!acpi_dock_is_ejd_device(dock_handle, handle))
212 		goto out;
213 
214 	ACPI_VPRINT(dock_dev, acpi_device_get_parent_softc(dock_dev),
215 		    "inserting device for %s\n", acpi_name(handle));
216 
217 #if 0
218 	/*
219 	 * If the system boot up w/o Docking, the devices under the dock
220 	 * still un-initialized, also control methods such as _INI, _STA
221 	 * are not executed.
222 	 * Normal devices are initialized at booting by calling
223 	 * AcpiInitializeObjects(), however the devices under the dock
224 	 * need to be initialized here on the scheme of ACPICA.
225 	 */
226 	ACPI_INIT_WALK_INFO	Info;
227 
228 	AcpiNsWalkNamespace(ACPI_TYPE_ANY, handle,
229 	    100, TRUE, AcpiNsInitOneDevice, NULL, &Info, NULL);
230 #endif
231 
232 	dev = acpi_get_device(handle);
233 	if (dev == NULL) {
234 		device_printf(dock_dev, "error: %s has no associated device\n",
235 		    acpi_name(handle));
236 		goto out;
237 	}
238 
239 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_dock_attach_later, dev);
240 
241 out:
242 	return (AE_OK);
243 }
244 
245 static void
246 acpi_dock_insert_children(device_t dev)
247 {
248 	ACPI_STATUS	status;
249 	ACPI_HANDLE	sb_handle;
250 
251 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle);
252 	if (ACPI_SUCCESS(status)) {
253 		AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle,
254 		    100, acpi_dock_insert_child, NULL, dev, NULL);
255 	}
256 }
257 
258 static void
259 acpi_dock_insert(device_t dev)
260 {
261 	struct acpi_dock_softc	*sc;
262 	ACPI_HANDLE		h;
263 
264 	ACPI_SERIAL_ASSERT(dock);
265 
266 	sc = device_get_softc(dev);
267 	h = acpi_get_handle(dev);
268 
269 	if (sc->status == ACPI_DOCK_STATUS_UNDOCKED ||
270 	    sc->status == ACPI_DOCK_STATUS_UNKNOWN) {
271 		acpi_dock_execute_lck(dev, ACPI_DOCK_LOCK);
272 		if (acpi_dock_execute_dck(dev, ACPI_DOCK_CONNECT) != 0) {
273 			device_printf(dev, "_DCK failed\n");
274 			return;
275 		}
276 
277 		if (!cold) {
278 			acpi_dock_insert_children(dev);
279 
280 			acpi_UserNotify("Dock", h, 1);
281 		}
282 
283 		sc->status = ACPI_DOCK_STATUS_DOCKED;
284 	}
285 }
286 
287 /*
288  * Undock
289  */
290 
291 static ACPI_STATUS
292 acpi_dock_eject_child(ACPI_HANDLE handle, UINT32 level, void *context,
293     void **status)
294 {
295 	device_t	dock_dev, dev;
296 	ACPI_HANDLE	dock_handle;
297 
298 	dock_dev = *(device_t *)context;
299 	dock_handle = acpi_get_handle(dock_dev);
300 
301 	if (!acpi_dock_is_ejd_device(dock_handle, handle))
302 		goto out;
303 
304 	ACPI_VPRINT(dock_dev, acpi_device_get_parent_softc(dock_dev),
305 	    "ejecting device for %s\n", acpi_name(handle));
306 
307 	dev = acpi_get_device(handle);
308 	if (dev != NULL && device_is_attached(dev)) {
309 		bus_topo_lock();
310 		device_detach(dev);
311 		bus_topo_unlock();
312 	}
313 
314 	acpi_SetInteger(handle, "_EJ0", 0);
315 out:
316 	return (AE_OK);
317 }
318 
319 static void
320 acpi_dock_eject_children(device_t dev)
321 {
322 	ACPI_HANDLE	sb_handle;
323 	ACPI_STATUS	status;
324 
325 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle);
326 	if (ACPI_SUCCESS(status)) {
327 		AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle,
328 		    100, acpi_dock_eject_child, NULL, &dev, NULL);
329 	}
330 }
331 
332 static void
333 acpi_dock_removal(device_t dev)
334 {
335 	struct acpi_dock_softc *sc;
336 	ACPI_HANDLE		h;
337 
338 	ACPI_SERIAL_ASSERT(dock);
339 
340 	sc = device_get_softc(dev);
341 	h = acpi_get_handle(dev);
342 
343 	if (sc->status == ACPI_DOCK_STATUS_DOCKED ||
344 	    sc->status == ACPI_DOCK_STATUS_UNKNOWN) {
345 		acpi_dock_eject_children(dev);
346 		if (acpi_dock_execute_dck(dev, ACPI_DOCK_ISOLATE) != 0)
347 			return;
348 
349 		acpi_dock_execute_lck(dev, ACPI_DOCK_UNLOCK);
350 
351 		if (acpi_dock_execute_ejx(dev, 1, 0) != 0) {
352 			device_printf(dev, "_EJ0 failed\n");
353 			return;
354 		}
355 
356 		acpi_UserNotify("Dock", h, 0);
357 
358 		sc->status = ACPI_DOCK_STATUS_UNDOCKED;
359 	}
360 
361 	acpi_dock_get_info(dev);
362 	if (sc->_sta != 0)
363 		device_printf(dev, "mechanical failure (%#x).\n", sc->_sta);
364 }
365 
366 /*
367  * Device/Bus check
368  */
369 
370 static void
371 acpi_dock_device_check(device_t dev)
372 {
373 	struct acpi_dock_softc *sc;
374 
375 	ACPI_SERIAL_ASSERT(dock);
376 
377 	sc = device_get_softc(dev);
378 	acpi_dock_get_info(dev);
379 
380 	/*
381 	 * If the _STA method indicates 'present' and 'functioning', the
382 	 * system is docked.  If _STA does not exist for this device, it
383 	 * is always present.
384 	 */
385 	if (sc->_sta == ACPI_DOCK_STATUS_UNKNOWN ||
386 	    ACPI_DEVICE_PRESENT(sc->_sta))
387 		acpi_dock_insert(dev);
388 	else if (sc->_sta == 0)
389 		acpi_dock_removal(dev);
390 }
391 
392 /*
393  * Notify Handler
394  */
395 
396 static void
397 acpi_dock_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
398 {
399 	device_t	dev;
400 
401 	dev = (device_t) context;
402 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
403 		    "got notification %#x\n", notify);
404 
405 	ACPI_SERIAL_BEGIN(dock);
406 	switch (notify) {
407 	case ACPI_NOTIFY_BUS_CHECK:
408 	case ACPI_NOTIFY_DEVICE_CHECK:
409 		acpi_dock_device_check(dev);
410 		break;
411 	case ACPI_NOTIFY_EJECT_REQUEST:
412 		acpi_dock_removal(dev);
413 		break;
414 	default:
415 		device_printf(dev, "unknown notify %#x\n", notify);
416 		break;
417 	}
418 	ACPI_SERIAL_END(dock);
419 }
420 
421 static int
422 acpi_dock_status_sysctl(SYSCTL_HANDLER_ARGS)
423 {
424 	struct acpi_dock_softc *sc;
425 	device_t	dev;
426 	int		status, err;
427 
428 	dev = (device_t)arg1;
429 
430 	sc = device_get_softc(dev);
431 	status = sc->status;
432 
433 	ACPI_SERIAL_BEGIN(dock);
434 	err = sysctl_handle_int(oidp, &status, 0, req);
435 	if (err != 0 || req->newptr == NULL)
436 		goto out;
437 
438 	if (status != ACPI_DOCK_STATUS_UNDOCKED &&
439 	    status != ACPI_DOCK_STATUS_DOCKED) {
440 		err = EINVAL;
441 		goto out;
442 	}
443 
444 	if (status == sc->status)
445 		goto out;
446 
447 	switch (status) {
448 	case ACPI_DOCK_STATUS_UNDOCKED:
449 		acpi_dock_removal(dev);
450 		break;
451 	case ACPI_DOCK_STATUS_DOCKED:
452 		acpi_dock_device_check(dev);
453 		break;
454 	default:
455 		err = EINVAL;
456 		break;
457 	}
458 out:
459 	ACPI_SERIAL_END(dock);
460 	return (err);
461 }
462 
463 static int
464 acpi_dock_probe(device_t dev)
465 {
466 	ACPI_HANDLE	h, tmp;
467 
468 	h = acpi_get_handle(dev);
469 	if (acpi_disabled("dock") ||
470 	    ACPI_FAILURE(AcpiGetHandle(h, "_DCK", &tmp)))
471 		return (ENXIO);
472 
473 	device_set_desc(dev, "ACPI Docking Station");
474 
475 	/*
476 	 * XXX Somewhere else in the kernel panics on "sysctl kern" if we
477 	 * return a negative value here (reprobe ok).
478 	 */
479 	return (0);
480 }
481 
482 static int
483 acpi_dock_attach(device_t dev)
484 {
485 	struct acpi_dock_softc *sc;
486 	ACPI_HANDLE	h;
487 
488 	sc = device_get_softc(dev);
489 	h = acpi_get_handle(dev);
490 	if (sc == NULL || h == NULL)
491 		return (ENXIO);
492 
493 	sc->status = ACPI_DOCK_STATUS_UNKNOWN;
494 
495 	AcpiEvaluateObject(h, "_INI", NULL, NULL);
496 
497 	ACPI_SERIAL_BEGIN(dock);
498 
499 	acpi_dock_device_check(dev);
500 
501 	/* Get the sysctl tree */
502 	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
503 	sc->sysctl_tree = device_get_sysctl_tree(dev);
504 
505 	SYSCTL_ADD_INT(sc->sysctl_ctx,
506 		SYSCTL_CHILDREN(sc->sysctl_tree),
507 		OID_AUTO, "_sta", CTLFLAG_RD,
508 		&sc->_sta, 0, "Dock _STA");
509 	SYSCTL_ADD_INT(sc->sysctl_ctx,
510 		SYSCTL_CHILDREN(sc->sysctl_tree),
511 		OID_AUTO, "_bdn", CTLFLAG_RD,
512 		&sc->_bdn, 0, "Dock _BDN");
513 	SYSCTL_ADD_INT(sc->sysctl_ctx,
514 		SYSCTL_CHILDREN(sc->sysctl_tree),
515 		OID_AUTO, "_uid", CTLFLAG_RD,
516 		&sc->_uid, 0, "Dock _UID");
517 	SYSCTL_ADD_PROC(sc->sysctl_ctx,
518 		SYSCTL_CHILDREN(sc->sysctl_tree),
519 		OID_AUTO, "status",
520 		CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, dev, 0,
521 		acpi_dock_status_sysctl, "I",
522 		"Dock/Undock operation");
523 
524 	ACPI_SERIAL_END(dock);
525 
526 	AcpiInstallNotifyHandler(h, ACPI_ALL_NOTIFY,
527 				 acpi_dock_notify_handler, dev);
528 
529 	return (0);
530 }
531 
532 static device_method_t acpi_dock_methods[] = {
533 	/* Device interface */
534 	DEVMETHOD(device_probe, acpi_dock_probe),
535 	DEVMETHOD(device_attach, acpi_dock_attach),
536 
537 	DEVMETHOD_END
538 };
539 
540 static driver_t	acpi_dock_driver = {
541 	"acpi_dock",
542 	acpi_dock_methods,
543 	sizeof(struct acpi_dock_softc),
544 };
545 
546 DRIVER_MODULE(acpi_dock, acpi, acpi_dock_driver, 0, 0);
547 MODULE_DEPEND(acpi_dock, acpi, 1, 1, 1);
548 ACPI_PNP_INFO(acpi_dock_pnp_ids);
549