1 /*-
2  * Copyright (c) 2003 OGAWA Takaya <t-ogawa@triaez.kaisei.org>
3  * Copyright (c) 2004 TAKAHASHI Yoshihiro <nyan@FreeBSD.org>
4  * All rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/acpi_support/acpi_panasonic.c,v 1.15 2009/06/05 18:44:36 jkim
28  */
29 
30 #include "opt_acpi.h"
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/power.h>
37 
38 #include "acpi.h"
39 
40 #include <dev/acpica/acpivar.h>
41 
42 #define _COMPONENT	ACPI_OEM
43 ACPI_MODULE_NAME("Panasonic")
44 
45 /* Debug */
46 #undef	ACPI_PANASONIC_DEBUG
47 
48 /* Operations */
49 #define	HKEY_SET	0
50 #define	HKEY_GET	1
51 
52 /* Functions */
53 #define	HKEY_REG_LCD_BRIGHTNESS_MAX_AC	0x02
54 #define	HKEY_REG_LCD_BRIGHTNESS_MIN_AC	0x03
55 #define	HKEY_REG_LCD_BRIGHTNESS_AC	0x04
56 #define	HKEY_REG_LCD_BRIGHTNESS_MAX_DC	0x05
57 #define	HKEY_REG_LCD_BRIGHTNESS_MIN_DC	0x06
58 #define	HKEY_REG_LCD_BRIGHTNESS_DC	0x07
59 #define	HKEY_REG_SOUND_MUTE		0x08
60 
61 /* Field definitions */
62 #define	HKEY_LCD_BRIGHTNESS_BITS	4
63 #define	HKEY_LCD_BRIGHTNESS_DIV		((1 << HKEY_LCD_BRIGHTNESS_BITS) - 1)
64 
65 struct acpi_panasonic_softc {
66 	device_t	dev;
67 	ACPI_HANDLE	handle;
68 
69 	struct sysctl_ctx_list	sysctl_ctx;
70 	struct sysctl_oid	*sysctl_tree;
71 
72 	eventhandler_tag	power_evh;
73 };
74 
75 /* Prototype for HKEY functions for getting/setting a value. */
76 typedef int hkey_fn_t(ACPI_HANDLE, int, UINT32 *);
77 
78 static int	acpi_panasonic_probe(device_t dev);
79 static int	acpi_panasonic_attach(device_t dev);
80 static int	acpi_panasonic_detach(device_t dev);
81 static int	acpi_panasonic_shutdown(device_t dev);
82 static int	acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS);
83 static UINT64	acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index);
84 static void	acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index, UINT64 val);
85 static int	acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc,
86 		    ACPI_HANDLE h, UINT32 *arg);
87 static void	acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc,
88 		    ACPI_HANDLE h, UINT32 key);
89 static void	acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify,
90 		    void *context);
91 static void	acpi_panasonic_power_profile(void *arg);
92 
93 static hkey_fn_t	hkey_lcd_brightness_max;
94 static hkey_fn_t	hkey_lcd_brightness_min;
95 static hkey_fn_t	hkey_lcd_brightness;
96 static hkey_fn_t	hkey_sound_mute;
97 ACPI_SERIAL_DECL(panasonic, "ACPI Panasonic extras");
98 
99 /* Table of sysctl names and HKEY functions to call. */
100 static struct {
101 	char		*name;
102 	hkey_fn_t	*handler;
103 } sysctl_table[] = {
104 	/* name,		handler */
105 	{"lcd_brightness_max",	hkey_lcd_brightness_max},
106 	{"lcd_brightness_min",	hkey_lcd_brightness_min},
107 	{"lcd_brightness",	hkey_lcd_brightness},
108 	{"sound_mute",		hkey_sound_mute},
109 	{NULL, NULL}
110 };
111 
112 static device_method_t acpi_panasonic_methods[] = {
113 	DEVMETHOD(device_probe,		acpi_panasonic_probe),
114 	DEVMETHOD(device_attach,	acpi_panasonic_attach),
115 	DEVMETHOD(device_detach,	acpi_panasonic_detach),
116 	DEVMETHOD(device_shutdown,	acpi_panasonic_shutdown),
117 
118 	DEVMETHOD_END
119 };
120 
121 static driver_t acpi_panasonic_driver = {
122 	"acpi_panasonic",
123 	acpi_panasonic_methods,
124 	sizeof(struct acpi_panasonic_softc),
125 };
126 
127 static devclass_t acpi_panasonic_devclass;
128 
129 DRIVER_MODULE(acpi_panasonic, acpi, acpi_panasonic_driver,
130     acpi_panasonic_devclass, 0, 0);
131 MODULE_DEPEND(acpi_panasonic, acpi, 1, 1, 1);
132 
133 static int
134 acpi_panasonic_probe(device_t dev)
135 {
136 	static char *mat_ids[] = { "MAT0019", NULL };
137 
138 	if (acpi_disabled("panasonic") ||
139 	    ACPI_ID_PROBE(device_get_parent(dev), dev, mat_ids) == NULL ||
140 	    device_get_unit(dev) != 0)
141 		return (ENXIO);
142 
143 	device_set_desc(dev, "Panasonic Notebook Hotkeys");
144 	return (0);
145 }
146 
147 static int
148 acpi_panasonic_attach(device_t dev)
149 {
150 	struct acpi_panasonic_softc *sc;
151 	struct acpi_softc *acpi_sc;
152 	ACPI_STATUS status;
153 	int i;
154 
155 	sc = device_get_softc(dev);
156 	sc->dev = dev;
157 	sc->handle = acpi_get_handle(dev);
158 
159 	acpi_sc = acpi_device_get_parent_softc(dev);
160 
161 	/* Build sysctl tree */
162 	sysctl_ctx_init(&sc->sysctl_ctx);
163 	sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
164 	    SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
165 	    "panasonic", CTLFLAG_RD, 0, "");
166 	for (i = 0; sysctl_table[i].name != NULL; i++) {
167 		SYSCTL_ADD_PROC(&sc->sysctl_ctx,
168 		    SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
169 		    sysctl_table[i].name,
170 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
171 		    sc, i, acpi_panasonic_sysctl, "I", "");
172 	}
173 
174 #if 0
175 	/* Activate hotkeys */
176 	status = AcpiEvaluateObject(sc->handle, "", NULL, NULL);
177 	if (ACPI_FAILURE(status)) {
178 		device_printf(dev, "enable FN keys failed\n");
179 		sysctl_ctx_free(&sc->sysctl_ctx);
180 		return (ENXIO);
181 	}
182 #endif
183 
184 	/* Handle notifies */
185 	status = AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
186 	    acpi_panasonic_notify, sc);
187 	if (ACPI_FAILURE(status)) {
188 		device_printf(dev, "couldn't install notify handler - %s\n",
189 		    AcpiFormatException(status));
190 		sysctl_ctx_free(&sc->sysctl_ctx);
191 		return (ENXIO);
192 	}
193 
194 	/* Install power profile event handler */
195 	sc->power_evh = EVENTHANDLER_REGISTER(power_profile_change,
196 	    acpi_panasonic_power_profile, sc->handle, 0);
197 
198 	return (0);
199 }
200 
201 static int
202 acpi_panasonic_detach(device_t dev)
203 {
204 	struct acpi_panasonic_softc *sc;
205 
206 	sc = device_get_softc(dev);
207 
208 	/* Remove power profile event handler */
209 	EVENTHANDLER_DEREGISTER(power_profile_change, sc->power_evh);
210 
211 	/* Remove notify handler */
212 	AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
213 	    acpi_panasonic_notify);
214 
215 	/* Free sysctl tree */
216 	sysctl_ctx_free(&sc->sysctl_ctx);
217 
218 	return (0);
219 }
220 
221 static int
222 acpi_panasonic_shutdown(device_t dev)
223 {
224 	struct acpi_panasonic_softc *sc;
225 	int mute;
226 
227 	/* Mute the main audio during reboot to prevent static burst to speaker. */
228 	sc = device_get_softc(dev);
229 	mute = 1;
230 	hkey_sound_mute(sc->handle, HKEY_SET, &mute);
231 	return (0);
232 }
233 
234 static int
235 acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS)
236 {
237 	struct acpi_panasonic_softc *sc;
238 	UINT32 arg;
239 	int function, error;
240 	hkey_fn_t *handler;
241 
242 	sc = (struct acpi_panasonic_softc *)oidp->oid_arg1;
243 	function = oidp->oid_arg2;
244 	handler = sysctl_table[function].handler;
245 
246 	/* Get the current value from the appropriate function. */
247 	ACPI_SERIAL_BEGIN(panasonic);
248 	error = handler(sc->handle, HKEY_GET, &arg);
249 	if (error != 0)
250 		goto out;
251 
252 	/* Send the current value to the user and return if no new value. */
253 	error = sysctl_handle_int(oidp, &arg, 0, req);
254 	if (error != 0 || req->newptr == NULL)
255 		goto out;
256 
257 	/* Set the new value via the appropriate function. */
258 	error = handler(sc->handle, HKEY_SET, &arg);
259 
260 out:
261 	ACPI_SERIAL_END(panasonic);
262 	return (error);
263 }
264 
265 static UINT64
266 acpi_panasonic_sinf(ACPI_HANDLE h, UINT64 index)
267 {
268 	ACPI_BUFFER buf;
269 	ACPI_OBJECT *res;
270 	UINT64 ret;
271 
272 	ACPI_SERIAL_ASSERT(panasonic);
273 	ret = -1;
274 	buf.Length = ACPI_ALLOCATE_BUFFER;
275 	buf.Pointer = NULL;
276 	AcpiEvaluateObject(h, "SINF", NULL, &buf);
277 	res = (ACPI_OBJECT *)buf.Pointer;
278 	if (res->Type == ACPI_TYPE_PACKAGE)
279 		ret = res->Package.Elements[index].Integer.Value;
280 	AcpiOsFree(buf.Pointer);
281 
282 	return (ret);
283 }
284 
285 static void
286 acpi_panasonic_sset(ACPI_HANDLE h, UINT64 index, UINT64 val)
287 {
288 	ACPI_OBJECT_LIST args;
289 	ACPI_OBJECT obj[2];
290 
291 	ACPI_SERIAL_ASSERT(panasonic);
292 	obj[0].Type = ACPI_TYPE_INTEGER;
293 	obj[0].Integer.Value = index;
294 	obj[1].Type = ACPI_TYPE_INTEGER;
295 	obj[1].Integer.Value = val;
296 	args.Count = 2;
297 	args.Pointer = obj;
298 	AcpiEvaluateObject(h, "SSET", &args, NULL);
299 }
300 
301 static int
302 hkey_lcd_brightness_max(ACPI_HANDLE h, int op, UINT32 *val)
303 {
304 	int reg;
305 
306 	ACPI_SERIAL_ASSERT(panasonic);
307 	reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
308 	    HKEY_REG_LCD_BRIGHTNESS_MAX_AC : HKEY_REG_LCD_BRIGHTNESS_MAX_DC;
309 
310 	switch (op) {
311 	case HKEY_SET:
312 		return (EPERM);
313 		break;
314 	case HKEY_GET:
315 		*val = acpi_panasonic_sinf(h, reg);
316 		break;
317 	}
318 
319 	return (0);
320 }
321 
322 static int
323 hkey_lcd_brightness_min(ACPI_HANDLE h, int op, UINT32 *val)
324 {
325 	int reg;
326 
327 	ACPI_SERIAL_ASSERT(panasonic);
328 	reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
329 	    HKEY_REG_LCD_BRIGHTNESS_MIN_AC : HKEY_REG_LCD_BRIGHTNESS_MIN_DC;
330 
331 	switch (op) {
332 	case HKEY_SET:
333 		return (EPERM);
334 		break;
335 	case HKEY_GET:
336 		*val = acpi_panasonic_sinf(h, reg);
337 		break;
338 	}
339 
340 	return (0);
341 }
342 
343 static int
344 hkey_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *val)
345 {
346 	int reg;
347 	UINT32 max, min;
348 
349 	reg = (power_profile_get_state() == POWER_PROFILE_PERFORMANCE) ?
350 	    HKEY_REG_LCD_BRIGHTNESS_AC : HKEY_REG_LCD_BRIGHTNESS_DC;
351 
352 	ACPI_SERIAL_ASSERT(panasonic);
353 	switch (op) {
354 	case HKEY_SET:
355 		hkey_lcd_brightness_max(h, HKEY_GET, &max);
356 		hkey_lcd_brightness_min(h, HKEY_GET, &min);
357 		if (*val < min || *val > max)
358 			return (EINVAL);
359 		acpi_panasonic_sset(h, reg, *val);
360 		break;
361 	case HKEY_GET:
362 		*val = acpi_panasonic_sinf(h, reg);
363 		break;
364 	}
365 
366 	return (0);
367 }
368 
369 static int
370 hkey_sound_mute(ACPI_HANDLE h, int op, UINT32 *val)
371 {
372 
373 	ACPI_SERIAL_ASSERT(panasonic);
374 	switch (op) {
375 	case HKEY_SET:
376 		if (*val != 0 && *val != 1)
377 			return (EINVAL);
378 		acpi_panasonic_sset(h, HKEY_REG_SOUND_MUTE, *val);
379 		break;
380 	case HKEY_GET:
381 		*val = acpi_panasonic_sinf(h, HKEY_REG_SOUND_MUTE);
382 		break;
383 	}
384 
385 	return (0);
386 }
387 
388 static int
389 acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
390     UINT32 *arg)
391 {
392 	ACPI_BUFFER buf;
393 	ACPI_OBJECT *res;
394 	UINT64 val;
395 	int status;
396 
397 	ACPI_SERIAL_ASSERT(panasonic);
398 	status = ENXIO;
399 
400 	buf.Length = ACPI_ALLOCATE_BUFFER;
401 	buf.Pointer = NULL;
402 	AcpiEvaluateObject(h, "HINF", NULL, &buf);
403 	res = (ACPI_OBJECT *)buf.Pointer;
404 	if (res->Type != ACPI_TYPE_INTEGER) {
405 		device_printf(sc->dev, "HINF returned non-integer\n");
406 		goto end;
407 	}
408 	val = res->Integer.Value;
409 #ifdef ACPI_PANASONIC_DEBUG
410 	device_printf(sc->dev, "%s button Fn+F%d\n",
411 		      (val & 0x80) ? "Pressed" : "Released",
412 		      (int)(val & 0x7f));
413 #endif
414 	if ((val & 0x7f) > 0 && (val & 0x7f) < 11) {
415 		*arg = val;
416 		status = 0;
417 	}
418 end:
419 	if (buf.Pointer)
420 		AcpiOsFree(buf.Pointer);
421 
422 	return (status);
423 }
424 
425 static void
426 acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
427     UINT32 key)
428 {
429 	struct acpi_softc *acpi_sc;
430 	int arg, max, min;
431 
432 	acpi_sc = acpi_device_get_parent_softc(sc->dev);
433 
434 	ACPI_SERIAL_ASSERT(panasonic);
435 	switch (key) {
436 	case 1:
437 		/* Decrease LCD brightness. */
438 		hkey_lcd_brightness_max(h, HKEY_GET, &max);
439 		hkey_lcd_brightness_min(h, HKEY_GET, &min);
440 		hkey_lcd_brightness(h, HKEY_GET, &arg);
441 		arg -= max / HKEY_LCD_BRIGHTNESS_DIV;
442 		if (arg < min)
443 			arg = min;
444 		else if (arg > max)
445 			arg = max;
446 		hkey_lcd_brightness(h, HKEY_SET, &arg);
447 		break;
448 	case 2:
449 		/* Increase LCD brightness. */
450 		hkey_lcd_brightness_max(h, HKEY_GET, &max);
451 		hkey_lcd_brightness_min(h, HKEY_GET, &min);
452 		hkey_lcd_brightness(h, HKEY_GET, &arg);
453 		arg += max / HKEY_LCD_BRIGHTNESS_DIV;
454 		if (arg < min)
455 			arg = min;
456 		else if (arg > max)
457 			arg = max;
458 		hkey_lcd_brightness(h, HKEY_SET, &arg);
459 		break;
460 	case 4:
461 		/* Toggle sound mute. */
462 		hkey_sound_mute(h, HKEY_GET, &arg);
463 		if (arg)
464 			arg = 0;
465 		else
466 			arg = 1;
467 		hkey_sound_mute(h, HKEY_SET, &arg);
468 		break;
469 	case 7:
470 		/* Suspend. */
471 		acpi_event_sleep_button_sleep(acpi_sc);
472 		break;
473 	}
474 }
475 
476 static void
477 acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify, void *context)
478 {
479 	struct acpi_panasonic_softc *sc;
480 	UINT32 key = 0;
481 
482 	sc = (struct acpi_panasonic_softc *)context;
483 
484 	switch (notify) {
485 	case 0x80:
486 		ACPI_SERIAL_BEGIN(panasonic);
487 		if (acpi_panasonic_hkey_event(sc, h, &key) == 0) {
488 			acpi_panasonic_hkey_action(sc, h, key);
489 			acpi_UserNotify("Panasonic", h, (uint8_t)key);
490 		}
491 		ACPI_SERIAL_END(panasonic);
492 		break;
493 	default:
494 		device_printf(sc->dev, "unknown notify: %#x\n", notify);
495 		break;
496 	}
497 }
498 
499 static void
500 acpi_panasonic_power_profile(void *arg)
501 {
502 	ACPI_HANDLE handle;
503 	UINT32 brightness;
504 
505 	handle = (ACPI_HANDLE)arg;
506 
507 	/* Reset current brightness according to new power state. */
508 	ACPI_SERIAL_BEGIN(panasonic);
509 	hkey_lcd_brightness(handle, HKEY_GET, &brightness);
510 	hkey_lcd_brightness(handle, HKEY_SET, &brightness);
511 	ACPI_SERIAL_END(panasonic);
512 }
513