1 /*-
2 * Copyright (c) 2003 OGAWA Takaya <t-ogawa@triaez.kaisei.org>
3 * Copyright (c) 2004 Yoshihiro TAKAHASHI <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 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include "opt_acpi.h"
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/bus.h>
38
39 #include "acpi.h"
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 0x04
54 #define HKEY_REG_SOUND_MUTE 0x08
55
56 /* Field definitions */
57 #define HKEY_LCD_BRIGHTNESS_BITS 4
58 #define HKEY_LCD_BRIGHTNESS_DIV ((1 << HKEY_LCD_BRIGHTNESS_BITS) - 1)
59
60 struct acpi_panasonic_softc {
61 device_t dev;
62 ACPI_HANDLE handle;
63
64 struct sysctl_ctx_list sysctl_ctx;
65 struct sysctl_oid *sysctl_tree;
66 };
67
68 /* Prototype for HKEY functions for getting/setting a value. */
69 typedef int hkey_fn_t(ACPI_HANDLE, int, UINT32 *);
70
71 static int acpi_panasonic_probe(device_t dev);
72 static int acpi_panasonic_attach(device_t dev);
73 static int acpi_panasonic_detach(device_t dev);
74 static int acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS);
75 static ACPI_INTEGER acpi_panasonic_sinf(ACPI_HANDLE h, ACPI_INTEGER index);
76 static void acpi_panasonic_sset(ACPI_HANDLE h, ACPI_INTEGER index,
77 ACPI_INTEGER val);
78 static int acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc,
79 ACPI_HANDLE h, UINT32 *arg);
80 static void acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc,
81 ACPI_HANDLE h, UINT32 key);
82 static void acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify,
83 void *context);
84
85 static hkey_fn_t hkey_lcd_brightness_max;
86 static hkey_fn_t hkey_lcd_brightness;
87 static hkey_fn_t hkey_sound_mute;
88 static int lcd_brightness_max = 255;
89 ACPI_SERIAL_DECL(panasonic, "ACPI Panasonic extras");
90
91 /* Table of sysctl names and HKEY functions to call. */
92 static struct {
93 char *name;
94 hkey_fn_t *handler;
95 } sysctl_table[] = {
96 /* name, handler */
97 {"lcd_brightness_max", hkey_lcd_brightness_max},
98 {"lcd_brightness", hkey_lcd_brightness},
99 {"sound_mute", hkey_sound_mute},
100 {NULL, NULL}
101 };
102
103 static device_method_t acpi_panasonic_methods[] = {
104 DEVMETHOD(device_probe, acpi_panasonic_probe),
105 DEVMETHOD(device_attach, acpi_panasonic_attach),
106 DEVMETHOD(device_detach, acpi_panasonic_detach),
107
108 {0, 0}
109 };
110
111 static driver_t acpi_panasonic_driver = {
112 "acpi_panasonic",
113 acpi_panasonic_methods,
114 sizeof(struct acpi_panasonic_softc),
115 };
116
117 static devclass_t acpi_panasonic_devclass;
118
119 DRIVER_MODULE(acpi_panasonic, acpi, acpi_panasonic_driver,
120 acpi_panasonic_devclass, 0, 0);
121 MODULE_DEPEND(acpi_panasonic, acpi, 1, 1, 1);
122
123 static int
124 acpi_panasonic_probe(device_t dev)
125 {
126 static char *mat_ids[] = { "MAT0019", NULL };
127
128 if (acpi_disabled("panasonic") ||
129 ACPI_ID_PROBE(device_get_parent(dev), dev, mat_ids) == NULL ||
130 device_get_unit(dev) != 0)
131 return (ENXIO);
132
133 device_set_desc(dev, "Panasonic Notebook Hotkeys");
134 return (0);
135 }
136
137 static int
138 acpi_panasonic_attach(device_t dev)
139 {
140 struct acpi_panasonic_softc *sc;
141 struct acpi_softc *acpi_sc;
142 ACPI_STATUS status;
143 int i;
144
145 sc = device_get_softc(dev);
146 sc->dev = dev;
147 sc->handle = acpi_get_handle(dev);
148
149 acpi_sc = acpi_device_get_parent_softc(dev);
150
151 /* Build sysctl tree */
152 sysctl_ctx_init(&sc->sysctl_ctx);
153 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
154 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
155 "panasonic", CTLFLAG_RD, 0, "");
156 for (i = 0; sysctl_table[i].name != NULL; i++) {
157 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
158 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
159 sysctl_table[i].name,
160 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
161 sc, i, acpi_panasonic_sysctl, "I", "");
162 }
163
164 #if 0
165 /* Activate hotkeys */
166 status = AcpiEvaluateObject(sc->handle, "", NULL, NULL);
167 if (ACPI_FAILURE(status)) {
168 device_printf(dev, "enable FN keys failed\n");
169 sysctl_ctx_free(&sc->sysctl_ctx);
170 return (ENXIO);
171 }
172 #endif
173
174 /* Handle notifies */
175 status = AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
176 acpi_panasonic_notify, sc);
177 if (ACPI_FAILURE(status)) {
178 device_printf(dev, "couldn't install notify handler - %s\n",
179 AcpiFormatException(status));
180 sysctl_ctx_free(&sc->sysctl_ctx);
181 return (ENXIO);
182 }
183
184 return (0);
185 }
186
187 static int
188 acpi_panasonic_detach(device_t dev)
189 {
190 struct acpi_panasonic_softc *sc;
191
192 sc = device_get_softc(dev);
193
194 /* Remove notify handler */
195 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
196 acpi_panasonic_notify);
197
198 /* Free sysctl tree */
199 sysctl_ctx_free(&sc->sysctl_ctx);
200
201 return (0);
202 }
203
204 static int
205 acpi_panasonic_sysctl(SYSCTL_HANDLER_ARGS)
206 {
207 struct acpi_panasonic_softc *sc;
208 UINT32 arg;
209 int function, error;
210 hkey_fn_t *handler;
211
212 sc = (struct acpi_panasonic_softc *)oidp->oid_arg1;
213 function = oidp->oid_arg2;
214 handler = sysctl_table[function].handler;
215
216 /* Get the current value from the appropriate function. */
217 ACPI_SERIAL_BEGIN(panasonic);
218 error = handler(sc->handle, HKEY_GET, &arg);
219 if (error != 0)
220 goto out;
221
222 /* Send the current value to the user and return if no new value. */
223 error = sysctl_handle_int(oidp, &arg, 0, req);
224 if (error != 0 || req->newptr == NULL)
225 goto out;
226
227 /* Set the new value via the appropriate function. */
228 error = handler(sc->handle, HKEY_SET, &arg);
229
230 out:
231 ACPI_SERIAL_END(panasonic);
232 return (error);
233 }
234
235 static ACPI_INTEGER
236 acpi_panasonic_sinf(ACPI_HANDLE h, ACPI_INTEGER index)
237 {
238 ACPI_BUFFER buf;
239 ACPI_OBJECT *res;
240 ACPI_INTEGER ret;
241
242 ACPI_SERIAL_ASSERT(panasonic);
243 ret = -1;
244 buf.Length = ACPI_ALLOCATE_BUFFER;
245 buf.Pointer = NULL;
246 AcpiEvaluateObject(h, "SINF", NULL, &buf);
247 res = (ACPI_OBJECT *)buf.Pointer;
248 if (res->Type == ACPI_TYPE_PACKAGE)
249 ret = res->Package.Elements[index].Integer.Value;
250 AcpiOsFree(buf.Pointer);
251
252 return (ret);
253 }
254
255 static void
256 acpi_panasonic_sset(ACPI_HANDLE h, ACPI_INTEGER index, ACPI_INTEGER val)
257 {
258 ACPI_OBJECT_LIST args;
259 ACPI_OBJECT obj[2];
260
261 ACPI_SERIAL_ASSERT(panasonic);
262 obj[0].Type = ACPI_TYPE_INTEGER;
263 obj[0].Integer.Value = index;
264 obj[1].Type = ACPI_TYPE_INTEGER;
265 obj[1].Integer.Value = val;
266 args.Count = 2;
267 args.Pointer = obj;
268 AcpiEvaluateObject(h, "SSET", &args, NULL);
269 }
270
271 static int
272 hkey_lcd_brightness_max(ACPI_HANDLE h, int op, UINT32 *val)
273 {
274
275 ACPI_SERIAL_ASSERT(panasonic);
276 switch (op) {
277 case HKEY_SET:
278 if (*val < 0 || *val > 255)
279 return (EINVAL);
280 lcd_brightness_max = *val;
281 break;
282 case HKEY_GET:
283 *val = lcd_brightness_max;
284 break;
285 }
286
287 return (0);
288 }
289
290 static int
291 hkey_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *val)
292 {
293
294 ACPI_SERIAL_ASSERT(panasonic);
295 switch (op) {
296 case HKEY_SET:
297 if (*val < 0 || *val > lcd_brightness_max)
298 return (EINVAL);
299 acpi_panasonic_sset(h, HKEY_REG_LCD_BRIGHTNESS, *val);
300 break;
301 case HKEY_GET:
302 *val = acpi_panasonic_sinf(h, HKEY_REG_LCD_BRIGHTNESS);
303 break;
304 }
305
306 return (0);
307 }
308
309 static int
310 hkey_sound_mute(ACPI_HANDLE h, int op, UINT32 *val)
311 {
312
313 ACPI_SERIAL_ASSERT(panasonic);
314 switch (op) {
315 case HKEY_SET:
316 if (*val != 0 && *val != 1)
317 return (EINVAL);
318 acpi_panasonic_sset(h, HKEY_REG_SOUND_MUTE, *val);
319 break;
320 case HKEY_GET:
321 *val = acpi_panasonic_sinf(h, HKEY_REG_SOUND_MUTE);
322 break;
323 }
324
325 return (0);
326 }
327
328 static int
329 acpi_panasonic_hkey_event(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
330 UINT32 *arg)
331 {
332 ACPI_BUFFER buf;
333 ACPI_OBJECT *res;
334 ACPI_INTEGER val;
335 int status;
336
337 ACPI_SERIAL_ASSERT(panasonic);
338 status = ENXIO;
339
340 buf.Length = ACPI_ALLOCATE_BUFFER;
341 buf.Pointer = NULL;
342 AcpiEvaluateObject(h, "HINF", NULL, &buf);
343 res = (ACPI_OBJECT *)buf.Pointer;
344 if (res->Type != ACPI_TYPE_INTEGER) {
345 device_printf(sc->dev, "HINF returned non-integer\n");
346 goto end;
347 }
348 val = res->Integer.Value;
349 #ifdef ACPI_PANASONIC_DEBUG
350 device_printf(sc->dev, "%s button Fn+F%d\n",
351 (val & 0x80) ? "Pressed" : "Released",
352 (int)(val & 0x7f));
353 #endif
354 if ((val & 0x7f) > 0 && (val & 0x7f) < 11) {
355 *arg = val;
356 status = 0;
357 }
358 end:
359 if (buf.Pointer)
360 AcpiOsFree(buf.Pointer);
361
362 return (status);
363 }
364
365 static void
366 acpi_panasonic_hkey_action(struct acpi_panasonic_softc *sc, ACPI_HANDLE h,
367 UINT32 key)
368 {
369 int arg;
370
371 ACPI_SERIAL_ASSERT(panasonic);
372 switch (key) {
373 case 1:
374 /* Decrease LCD brightness. */
375 hkey_lcd_brightness(h, HKEY_GET, &arg);
376 arg -= lcd_brightness_max / HKEY_LCD_BRIGHTNESS_DIV;
377 if (arg < 0)
378 arg = 0;
379 else if (arg > lcd_brightness_max)
380 arg = lcd_brightness_max;
381 hkey_lcd_brightness(h, HKEY_SET, &arg);
382 break;
383 case 2:
384 /* Increase LCD brightness. */
385 hkey_lcd_brightness(h, HKEY_GET, &arg);
386 arg += lcd_brightness_max / HKEY_LCD_BRIGHTNESS_DIV;
387 if (arg < 0)
388 arg = 0;
389 else if (arg > lcd_brightness_max)
390 arg = lcd_brightness_max;
391 hkey_lcd_brightness(h, HKEY_SET, &arg);
392 break;
393 case 4:
394 /* Toggle sound mute. */
395 hkey_sound_mute(h, HKEY_GET, &arg);
396 if (arg)
397 arg = 0;
398 else
399 arg = 1;
400 hkey_sound_mute(h, HKEY_SET, &arg);
401 break;
402 }
403 }
404
405 static void
406 acpi_panasonic_notify(ACPI_HANDLE h, UINT32 notify, void *context)
407 {
408 struct acpi_panasonic_softc *sc;
409 UINT32 key;
410
411 sc = (struct acpi_panasonic_softc *)context;
412
413 switch (notify) {
414 case 0x80:
415 ACPI_SERIAL_BEGIN(panasonic);
416 if (acpi_panasonic_hkey_event(sc, h, &key) == 0) {
417 acpi_panasonic_hkey_action(sc, h, key);
418 acpi_UserNotify("Panasonic", h, (uint8_t)key);
419 }
420 ACPI_SERIAL_END(panasonic);
421 break;
422 default:
423 device_printf(sc->dev, "unknown notify: %#x\n", notify);
424 break;
425 }
426 }
Cache object: 18b3c23633e52b72b1aa14759968ad17
|