xref: /freebsd/sys/dev/acpica/acpi_dock.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
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 /*
68  * Utility functions
69  */
70 
71 static void
72 acpi_dock_get_info(device_t dev)
73 {
74 	struct acpi_dock_softc *sc;
75 	ACPI_HANDLE	h;
76 
77 	sc = device_get_softc(dev);
78 	h = acpi_get_handle(dev);
79 
80 	if (ACPI_FAILURE(acpi_GetInteger(h, "_STA", &sc->_sta)))
81 		sc->_sta = ACPI_DOCK_STATUS_UNKNOWN;
82 	if (ACPI_FAILURE(acpi_GetInteger(h, "_BDN", &sc->_bdn)))
83 		sc->_bdn = ACPI_DOCK_STATUS_UNKNOWN;
84 	if (ACPI_FAILURE(acpi_GetInteger(h, "_UID", &sc->_uid)))
85 		sc->_uid = ACPI_DOCK_STATUS_UNKNOWN;
86 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
87 		    "_STA: %04x, _BDN: %04x, _UID: %04x\n", sc->_sta,
88 		    sc->_bdn, sc->_uid);
89 }
90 
91 static int
92 acpi_dock_execute_dck(device_t dev, int dock)
93 {
94 	ACPI_HANDLE	h;
95 	ACPI_OBJECT	argobj;
96 	ACPI_OBJECT_LIST args;
97 	ACPI_BUFFER	buf;
98 	ACPI_OBJECT	retobj;
99 	ACPI_STATUS	status;
100 
101 	h = acpi_get_handle(dev);
102 
103 	argobj.Type = ACPI_TYPE_INTEGER;
104 	argobj.Integer.Value = dock;
105 	args.Count = 1;
106 	args.Pointer = &argobj;
107 	buf.Pointer = &retobj;
108 	buf.Length = sizeof(retobj);
109 	status = AcpiEvaluateObject(h, "_DCK", &args, &buf);
110 
111 	/*
112 	 * When _DCK is called with 0, OSPM will ignore the return value.
113 	 */
114 	if (dock == ACPI_DOCK_ISOLATE)
115 		return (0);
116 
117 	/* If _DCK returned 1, the request succeeded. */
118 	if (ACPI_SUCCESS(status) && retobj.Type == ACPI_TYPE_INTEGER &&
119 	    retobj.Integer.Value == 1)
120 		return (0);
121 
122 	return (-1);
123 }
124 
125 /* Lock devices while docked to prevent surprise removal. */
126 static void
127 acpi_dock_execute_lck(device_t dev, int lock)
128 {
129 	ACPI_HANDLE	h;
130 
131 	h = acpi_get_handle(dev);
132 	acpi_SetInteger(h, "_LCK", lock);
133 }
134 
135 /* Eject a device (i.e., motorized). */
136 static int
137 acpi_dock_execute_ejx(device_t dev, int eject, int state)
138 {
139 	ACPI_HANDLE	h;
140 	ACPI_STATUS	status;
141 	char		ejx[5];
142 
143 	h = acpi_get_handle(dev);
144 	snprintf(ejx, sizeof(ejx), "_EJ%d", state);
145 	status = acpi_SetInteger(h, ejx, eject);
146 	if (ACPI_SUCCESS(status))
147 		return (0);
148 
149 	return (-1);
150 }
151 
152 /* Find dependent devices.  When their parent is removed, so are they. */
153 static int
154 acpi_dock_is_ejd_device(ACPI_HANDLE dock_handle, ACPI_HANDLE handle)
155 {
156 	int		ret;
157 	ACPI_STATUS	ret_status;
158 	ACPI_BUFFER	ejd_buffer;
159 	ACPI_OBJECT	*obj;
160 
161 	ret = 0;
162 
163 	ejd_buffer.Pointer = NULL;
164 	ejd_buffer.Length = ACPI_ALLOCATE_BUFFER;
165 	ret_status = AcpiEvaluateObject(handle, "_EJD", NULL, &ejd_buffer);
166 	if (ACPI_FAILURE(ret_status))
167 		goto out;
168 
169 	obj = (ACPI_OBJECT *)ejd_buffer.Pointer;
170 	if (dock_handle == acpi_GetReference(NULL, obj))
171 		ret = 1;
172 
173 out:
174 	if (ejd_buffer.Pointer != NULL)
175 		AcpiOsFree(ejd_buffer.Pointer);
176 
177 	return (ret);
178 }
179 
180 /*
181  * Docking functions
182  */
183 
184 static void
185 acpi_dock_attach_later(void *context)
186 {
187 	device_t	dev;
188 
189 	dev = (device_t)context;
190 
191 	if (!device_is_enabled(dev))
192 		device_enable(dev);
193 
194 	mtx_lock(&Giant);
195 	device_probe_and_attach(dev);
196 	mtx_unlock(&Giant);
197 }
198 
199 static ACPI_STATUS
200 acpi_dock_insert_child(ACPI_HANDLE handle, UINT32 level, void *context,
201     void **status)
202 {
203 	device_t	dock_dev, dev;
204 	ACPI_HANDLE	dock_handle;
205 
206 	dock_dev = (device_t)context;
207 	dock_handle = acpi_get_handle(dock_dev);
208 
209 	if (!acpi_dock_is_ejd_device(dock_handle, handle))
210 		goto out;
211 
212 	ACPI_VPRINT(dock_dev, acpi_device_get_parent_softc(dock_dev),
213 		    "inserting device for %s\n", acpi_name(handle));
214 
215 #if 0
216 	/*
217 	 * If the system boot up w/o Docking, the devices under the dock
218 	 * still un-initialized, also control methods such as _INI, _STA
219 	 * are not executed.
220 	 * Normal devices are initialized at booting by calling
221 	 * AcpiInitializeObjects(), however the devices under the dock
222 	 * need to be initialized here on the scheme of ACPICA.
223 	 */
224 	ACPI_INIT_WALK_INFO	Info;
225 
226 	AcpiNsWalkNamespace(ACPI_TYPE_ANY, handle,
227 	    100, TRUE, AcpiNsInitOneDevice, NULL, &Info, NULL);
228 #endif
229 
230 	dev = acpi_get_device(handle);
231 	if (dev == NULL) {
232 		device_printf(dock_dev, "error: %s has no associated device\n",
233 		    acpi_name(handle));
234 		goto out;
235 	}
236 
237 	AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_dock_attach_later, dev);
238 
239 out:
240 	return (AE_OK);
241 }
242 
243 static void
244 acpi_dock_insert_children(device_t dev)
245 {
246 	ACPI_STATUS	status;
247 	ACPI_HANDLE	sb_handle;
248 
249 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle);
250 	if (ACPI_SUCCESS(status)) {
251 		AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle,
252 		    100, acpi_dock_insert_child, NULL, dev, NULL);
253 	}
254 }
255 
256 static void
257 acpi_dock_insert(device_t dev)
258 {
259 	struct acpi_dock_softc *sc;
260 
261 	ACPI_SERIAL_ASSERT(dock);
262 
263 	sc = device_get_softc(dev);
264 
265 	if (sc->status == ACPI_DOCK_STATUS_UNDOCKED ||
266 	    sc->status == ACPI_DOCK_STATUS_UNKNOWN) {
267 		acpi_dock_execute_lck(dev, ACPI_DOCK_LOCK);
268 		if (acpi_dock_execute_dck(dev, ACPI_DOCK_CONNECT) != 0) {
269 			device_printf(dev, "_DCK failed\n");
270 			return;
271 		}
272 
273 		if (!cold)
274 			acpi_dock_insert_children(dev);
275 		sc->status = ACPI_DOCK_STATUS_DOCKED;
276 	}
277 }
278 
279 /*
280  * Undock
281  */
282 
283 static ACPI_STATUS
284 acpi_dock_eject_child(ACPI_HANDLE handle, UINT32 level, void *context,
285     void **status)
286 {
287 	device_t	dock_dev, dev;
288 	ACPI_HANDLE	dock_handle;
289 
290 	dock_dev = *(device_t *)context;
291 	dock_handle = acpi_get_handle(dock_dev);
292 
293 	if (!acpi_dock_is_ejd_device(dock_handle, handle))
294 		goto out;
295 
296 	ACPI_VPRINT(dock_dev, acpi_device_get_parent_softc(dock_dev),
297 	    "ejecting device for %s\n", acpi_name(handle));
298 
299 	dev = acpi_get_device(handle);
300 	if (dev != NULL && device_is_attached(dev)) {
301 		mtx_lock(&Giant);
302 		device_detach(dev);
303 		mtx_unlock(&Giant);
304 	}
305 
306 	acpi_SetInteger(handle, "_EJ0", 0);
307 out:
308 	return (AE_OK);
309 }
310 
311 static void
312 acpi_dock_eject_children(device_t dev)
313 {
314 	ACPI_HANDLE	sb_handle;
315 	ACPI_STATUS	status;
316 
317 	status = AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle);
318 	if (ACPI_SUCCESS(status)) {
319 		AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle,
320 		    100, acpi_dock_eject_child, NULL, &dev, NULL);
321 	}
322 }
323 
324 static void
325 acpi_dock_removal(device_t dev)
326 {
327 	struct acpi_dock_softc *sc;
328 
329 	ACPI_SERIAL_ASSERT(dock);
330 
331 	sc = device_get_softc(dev);
332 	if (sc->status == ACPI_DOCK_STATUS_DOCKED ||
333 	    sc->status == ACPI_DOCK_STATUS_UNKNOWN) {
334 		acpi_dock_eject_children(dev);
335 		if (acpi_dock_execute_dck(dev, ACPI_DOCK_ISOLATE) != 0)
336 			return;
337 
338 		acpi_dock_execute_lck(dev, ACPI_DOCK_UNLOCK);
339 
340 		if (acpi_dock_execute_ejx(dev, 1, 0) != 0) {
341 			device_printf(dev, "_EJ0 failed\n");
342 			return;
343 		}
344 
345 		sc->status = ACPI_DOCK_STATUS_UNDOCKED;
346 	}
347 
348 	acpi_dock_get_info(dev);
349 	if (sc->_sta != 0)
350 		device_printf(dev, "mechanical failure (%#x).\n", sc->_sta);
351 }
352 
353 /*
354  * Device/Bus check
355  */
356 
357 static void
358 acpi_dock_device_check(device_t dev)
359 {
360 	struct acpi_dock_softc *sc;
361 
362 	ACPI_SERIAL_ASSERT(dock);
363 
364 	sc = device_get_softc(dev);
365 	acpi_dock_get_info(dev);
366 
367 	/*
368 	 * If the _STA method indicates 'present' and 'functioning', the
369 	 * system is docked.  If _STA does not exist for this device, it
370 	 * is always present.
371 	 */
372 	if (sc->_sta == ACPI_DOCK_STATUS_UNKNOWN ||
373 	    ACPI_DEVICE_PRESENT(sc->_sta))
374 		acpi_dock_insert(dev);
375 	else if (sc->_sta == 0)
376 		acpi_dock_removal(dev);
377 }
378 
379 /*
380  * Notify Handler
381  */
382 
383 static void
384 acpi_dock_notify_handler(ACPI_HANDLE h, UINT32 notify, void *context)
385 {
386 	device_t	dev;
387 
388 	dev = (device_t) context;
389 	ACPI_VPRINT(dev, acpi_device_get_parent_softc(dev),
390 		    "got notification %#x\n", notify);
391 
392 	ACPI_SERIAL_BEGIN(dock);
393 	switch (notify) {
394 	case ACPI_NOTIFY_BUS_CHECK:
395 	case ACPI_NOTIFY_DEVICE_CHECK:
396 		acpi_dock_device_check(dev);
397 		break;
398 	case ACPI_NOTIFY_EJECT_REQUEST:
399 		acpi_dock_removal(dev);
400 		break;
401 	default:
402 		device_printf(dev, "unknown notify %#x\n", notify);
403 		break;
404 	}
405 	ACPI_SERIAL_END(dock);
406 }
407 
408 static int
409 acpi_dock_status_sysctl(SYSCTL_HANDLER_ARGS)
410 {
411 	struct acpi_dock_softc *sc;
412 	device_t	dev;
413 	int		status, err;
414 
415 	dev = (device_t)arg1;
416 
417 	sc = device_get_softc(dev);
418 	status = sc->status;
419 
420 	ACPI_SERIAL_BEGIN(dock);
421 	err = sysctl_handle_int(oidp, &status, 0, req);
422 	if (err != 0 || req->newptr == NULL)
423 		goto out;
424 
425 	if (status != ACPI_DOCK_STATUS_UNDOCKED &&
426 	    status != ACPI_DOCK_STATUS_DOCKED) {
427 		err = EINVAL;
428 		goto out;
429 	}
430 
431 	if (status == sc->status)
432 		goto out;
433 
434 	switch (status) {
435 	case ACPI_DOCK_STATUS_UNDOCKED:
436 		acpi_dock_removal(dev);
437 		break;
438 	case ACPI_DOCK_STATUS_DOCKED:
439 		acpi_dock_device_check(dev);
440 		break;
441 	default:
442 		err = EINVAL;
443 		break;
444 	}
445 out:
446 	ACPI_SERIAL_END(dock);
447 	return (err);
448 }
449 
450 static int
451 acpi_dock_probe(device_t dev)
452 {
453 	ACPI_HANDLE	h, tmp;
454 
455 	h = acpi_get_handle(dev);
456 	if (acpi_disabled("dock") ||
457 	    ACPI_FAILURE(AcpiGetHandle(h, "_DCK", &tmp)))
458 		return (ENXIO);
459 
460 	device_set_desc(dev, "ACPI Docking Station");
461 
462 	/*
463 	 * XXX Somewhere else in the kernel panics on "sysctl kern" if we
464 	 * return a negative value here (reprobe ok).
465 	 */
466 	return (0);
467 }
468 
469 static int
470 acpi_dock_attach(device_t dev)
471 {
472 	struct acpi_dock_softc *sc;
473 	ACPI_HANDLE	h;
474 
475 	sc = device_get_softc(dev);
476 	h = acpi_get_handle(dev);
477 	if (sc == NULL || h == NULL)
478 		return (ENXIO);
479 
480 	sc->status = ACPI_DOCK_STATUS_UNKNOWN;
481 
482 	AcpiEvaluateObject(h, "_INI", NULL, NULL);
483 
484 	ACPI_SERIAL_BEGIN(dock);
485 
486 	acpi_dock_device_check(dev);
487 
488 	/* Get the sysctl tree */
489 	sc->sysctl_ctx = device_get_sysctl_ctx(dev);
490 	sc->sysctl_tree = device_get_sysctl_tree(dev);
491 
492 	SYSCTL_ADD_INT(sc->sysctl_ctx,
493 		SYSCTL_CHILDREN(sc->sysctl_tree),
494 		OID_AUTO, "_sta", CTLFLAG_RD,
495 		&sc->_sta, 0, "Dock _STA");
496 	SYSCTL_ADD_INT(sc->sysctl_ctx,
497 		SYSCTL_CHILDREN(sc->sysctl_tree),
498 		OID_AUTO, "_bdn", CTLFLAG_RD,
499 		&sc->_bdn, 0, "Dock _BDN");
500 	SYSCTL_ADD_INT(sc->sysctl_ctx,
501 		SYSCTL_CHILDREN(sc->sysctl_tree),
502 		OID_AUTO, "_uid", CTLFLAG_RD,
503 		&sc->_uid, 0, "Dock _UID");
504 	SYSCTL_ADD_PROC(sc->sysctl_ctx,
505 		SYSCTL_CHILDREN(sc->sysctl_tree),
506 		OID_AUTO, "status",
507 		CTLTYPE_INT|CTLFLAG_RW, dev, 0,
508 		acpi_dock_status_sysctl, "I",
509 		"Dock/Undock operation");
510 
511 	ACPI_SERIAL_END(dock);
512 
513 	AcpiInstallNotifyHandler(h, ACPI_ALL_NOTIFY,
514 				 acpi_dock_notify_handler, dev);
515 
516 	return (0);
517 }
518 
519 static device_method_t acpi_dock_methods[] = {
520 	/* Device interface */
521 	DEVMETHOD(device_probe, acpi_dock_probe),
522 	DEVMETHOD(device_attach, acpi_dock_attach),
523 
524 	DEVMETHOD_END
525 };
526 
527 static driver_t	acpi_dock_driver = {
528 	"acpi_dock",
529 	acpi_dock_methods,
530 	sizeof(struct acpi_dock_softc),
531 };
532 
533 static devclass_t acpi_dock_devclass;
534 
535 DRIVER_MODULE(acpi_dock, acpi, acpi_dock_driver, acpi_dock_devclass, 0, 0);
536 MODULE_DEPEND(acpi_dock, acpi, 1, 1, 1);
537 
538