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