Bring in YONETANI Tomokazu's acpi-update-2.patch (27-May-2004), a major
[dragonfly.git] / sys / platform / pc32 / acpica5 / acpi_toshiba.c
... / ...
CommitLineData
1/*-
2 * Copyright (c) 2003 Hiroyuki Aizu <aizu@navi.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: src/sys/i386/acpica/acpi_toshiba.c,v 1.7 2004/04/14 03:32:01 njl Exp $
27 * $DragonFly: src/sys/platform/pc32/acpica5/Attic/acpi_toshiba.c,v 1.2 2004/06/27 08:52:45 dillon Exp $
28 */
29
30#include "opt_acpi.h"
31#include <sys/param.h>
32#include <sys/kernel.h>
33#include <sys/bus.h>
34
35#include "acpi.h"
36#include "acpivar.h"
37
38/*
39 * Toshiba HCI interface definitions
40 *
41 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
42 * be uniform across all their models. Ideally we would just call
43 * dedicated ACPI methods instead of using this primitive interface.
44 * However, the ACPI methods seem to be incomplete in some areas (for
45 * example they allow setting, but not reading, the LCD brightness
46 * value), so this is still useful.
47 */
48
49#define METHOD_HCI "GHCI"
50#define METHOD_HCI_ENABLE "ENAB"
51#define METHOD_VIDEO "DSSX"
52
53/* Operations */
54#define HCI_SET 0xFF00
55#define HCI_GET 0xFE00
56
57/* Return codes */
58#define HCI_SUCCESS 0x0000
59#define HCI_FAILURE 0x1000
60#define HCI_NOT_SUPPORTED 0x8000
61#define HCI_EMPTY 0x8C00
62
63/* Functions */
64#define HCI_REG_LCD_BACKLIGHT 0x0002
65#define HCI_REG_FAN 0x0004
66#define HCI_REG_SYSTEM_EVENT 0x0016
67#define HCI_REG_VIDEO_OUTPUT 0x001C
68#define HCI_REG_HOTKEY_EVENT 0x001E
69#define HCI_REG_LCD_BRIGHTNESS 0x002A
70#define HCI_REG_CPU_SPEED 0x0032
71
72/* Field definitions */
73#define HCI_FAN_SHIFT 7
74#define HCI_LCD_BRIGHTNESS_BITS 3
75#define HCI_LCD_BRIGHTNESS_SHIFT (16 - HCI_LCD_BRIGHTNESS_BITS)
76#define HCI_LCD_BRIGHTNESS_MAX ((1 << HCI_LCD_BRIGHTNESS_BITS) - 1)
77#define HCI_VIDEO_OUTPUT_FLAG 0x0100
78#define HCI_VIDEO_OUTPUT_LCD 0x1
79#define HCI_VIDEO_OUTPUT_CRT 0x2
80#define HCI_VIDEO_OUTPUT_TV 0x4
81#define HCI_CPU_SPEED_BITS 3
82#define HCI_CPU_SPEED_SHIFT (16 - HCI_CPU_SPEED_BITS)
83#define HCI_CPU_SPEED_MAX ((1 << HCI_CPU_SPEED_BITS) - 1)
84
85/* Key press/release events. */
86#define FN_F1_PRESS 0x013B
87#define FN_F1_RELEASE 0x01BB
88#define FN_F2_PRESS 0x013C
89#define FN_F2_RELEASE 0x01BC
90#define FN_F3_PRESS 0x013D
91#define FN_F3_RELEASE 0x01BD
92#define FN_F4_PRESS 0x013E
93#define FN_F4_RELEASE 0x01BE
94#define FN_F5_PRESS 0x013F
95#define FN_F5_RELEASE 0x01BF
96#define FN_F6_PRESS 0x0140
97#define FN_F6_RELEASE 0x01C0
98#define FN_F7_PRESS 0x0141
99#define FN_F7_RELEASE 0x01C1
100#define FN_F8_PRESS 0x0142
101#define FN_F8_RELEASE 0x01C2
102#define FN_F9_PRESS 0x0143
103#define FN_F9_RELEASE 0x01C3
104#define FN_BS_PRESS 0x010E
105#define FN_BS_RELEASE 0x018E
106#define FN_ESC_PRESS 0x0101
107#define FN_ESC_RELEASE 0x0181
108#define FN_KNJ_PRESS 0x0129
109#define FN_KNJ_RELEASE 0x01A9
110
111/* HCI register definitions. */
112#define HCI_WORDS 6 /* Number of registers */
113#define HCI_REG_AX 0 /* Operation, then return value */
114#define HCI_REG_BX 1 /* Function */
115#define HCI_REG_CX 2 /* Argument (in or out) */
116#define HCI_REG_DX 3 /* Unused? */
117#define HCI_REG_SI 4 /* Unused? */
118#define HCI_REG_DI 5 /* Unused? */
119
120struct acpi_toshiba_softc {
121 device_t dev;
122 ACPI_HANDLE handle;
123 ACPI_HANDLE video_handle;
124 struct sysctl_ctx_list sysctl_ctx;
125 struct sysctl_oid *sysctl_tree;
126};
127
128/* Prototype for HCI functions for getting/setting a value. */
129typedef int hci_fn_t(ACPI_HANDLE, int, UINT32 *);
130
131static int acpi_toshiba_probe(device_t dev);
132static int acpi_toshiba_attach(device_t dev);
133static int acpi_toshiba_detach(device_t dev);
134static int acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS);
135static hci_fn_t hci_force_fan;
136static hci_fn_t hci_video_output;
137static hci_fn_t hci_lcd_brightness;
138static hci_fn_t hci_lcd_backlight;
139static hci_fn_t hci_cpu_speed;
140static int hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg);
141static void hci_key_action(struct acpi_toshiba_softc *sc, ACPI_HANDLE h,
142 UINT32 key);
143static void acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify,
144 void *context);
145static int acpi_toshiba_video_probe(device_t dev);
146static int acpi_toshiba_video_attach(device_t dev);
147
148/* Table of sysctl names and HCI functions to call. */
149static struct {
150 char *name;
151 hci_fn_t *handler;
152} sysctl_table[] = {
153 /* name, handler */
154 {"force_fan", hci_force_fan},
155 {"video_output", hci_video_output},
156 {"lcd_brightness", hci_lcd_brightness},
157 {"lcd_backlight", hci_lcd_backlight},
158 {"cpu_speed", hci_cpu_speed},
159 {NULL, NULL}
160};
161
162static device_method_t acpi_toshiba_methods[] = {
163 DEVMETHOD(device_probe, acpi_toshiba_probe),
164 DEVMETHOD(device_attach, acpi_toshiba_attach),
165 DEVMETHOD(device_detach, acpi_toshiba_detach),
166
167 {0, 0}
168};
169
170static driver_t acpi_toshiba_driver = {
171 "acpi_toshiba",
172 acpi_toshiba_methods,
173 sizeof(struct acpi_toshiba_softc),
174};
175
176static devclass_t acpi_toshiba_devclass;
177DRIVER_MODULE(acpi_toshiba, acpi, acpi_toshiba_driver, acpi_toshiba_devclass,
178 0, 0);
179MODULE_DEPEND(acpi_toshiba, acpi, 1, 1, 1);
180
181static device_method_t acpi_toshiba_video_methods[] = {
182 DEVMETHOD(device_probe, acpi_toshiba_video_probe),
183 DEVMETHOD(device_attach, acpi_toshiba_video_attach),
184
185 {0, 0}
186};
187
188static driver_t acpi_toshiba_video_driver = {
189 "acpi_toshiba_video",
190 acpi_toshiba_video_methods,
191 0,
192};
193
194static devclass_t acpi_toshiba_video_devclass;
195DRIVER_MODULE(acpi_toshiba_video, acpi, acpi_toshiba_video_driver,
196 acpi_toshiba_video_devclass, 0, 0);
197MODULE_DEPEND(acpi_toshiba_video, acpi, 1, 1, 1);
198
199static int enable_fn_keys = 1;
200TUNABLE_INT("hw.acpi.toshiba.enable_fn_keys", &enable_fn_keys);
201
202/*
203 * HID Model
204 * -------------------------------------
205 * TOS6200 Libretto L Series
206 * Dynabook Satellite 2455
207 * Dynabook SS 3500
208 * TOS6207 Dynabook SS2110 Series
209 */
210static int
211acpi_toshiba_probe(device_t dev)
212{
213 int ret = ENXIO;
214
215 if (!acpi_disabled("toshiba") &&
216 acpi_get_type(dev) == ACPI_TYPE_DEVICE &&
217 device_get_unit(dev) == 0 &&
218 (acpi_MatchHid(dev, "TOS6200") ||
219 acpi_MatchHid(dev, "TOS6207"))) {
220 device_set_desc(dev, "Toshiba HCI Extras");
221 ret = 0;
222 }
223
224 return (ret);
225}
226
227static int
228acpi_toshiba_attach(device_t dev)
229{
230 struct acpi_toshiba_softc *sc;
231 struct acpi_softc *acpi_sc;
232 ACPI_STATUS status;
233 int i;
234
235 sc = device_get_softc(dev);
236 sc->dev = dev;
237 sc->handle = acpi_get_handle(dev);
238
239 acpi_sc = acpi_device_get_parent_softc(dev);
240 sysctl_ctx_init(&sc->sysctl_ctx);
241 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
242 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
243 "toshiba", CTLFLAG_RD, 0, "");
244
245 for (i = 0; sysctl_table[i].name != NULL; i++) {
246 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
247 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
248 sysctl_table[i].name,
249 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
250 sc, i, acpi_toshiba_sysctl, "I", "");
251 }
252
253 if (enable_fn_keys != 0) {
254 status = AcpiEvaluateObject(sc->handle, METHOD_HCI_ENABLE,
255 NULL, NULL);
256 if (ACPI_FAILURE(status)) {
257 device_printf(dev, "enable FN keys failed\n");
258 sysctl_ctx_free(&sc->sysctl_ctx);
259 return (ENXIO);
260 }
261 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
262 acpi_toshiba_notify, sc);
263 }
264
265 return (0);
266}
267
268static int
269acpi_toshiba_detach(device_t dev)
270{
271 struct acpi_toshiba_softc *sc;
272
273 sc = device_get_softc(dev);
274 if (enable_fn_keys != 0) {
275 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
276 acpi_toshiba_notify);
277 }
278 sysctl_ctx_free(&sc->sysctl_ctx);
279
280 return (0);
281}
282
283static int
284acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS)
285{
286 struct acpi_toshiba_softc *sc;
287 UINT32 arg;
288 int function, error = 0;
289 hci_fn_t *handler;
290
291 sc = (struct acpi_toshiba_softc *)oidp->oid_arg1;
292 function = oidp->oid_arg2;
293 handler = sysctl_table[function].handler;
294
295 /* Get the current value from the appropriate function. */
296 error = handler(sc->handle, HCI_GET, &arg);
297 if (error != 0)
298 return (error);
299
300 /* Send the current value to the user and return if no new value. */
301 error = sysctl_handle_int(oidp, &arg, 0, req);
302 if (error != 0 || req->newptr == NULL)
303 return (error);
304
305 /* Set the new value via the appropriate function. */
306 error = handler(sc->handle, HCI_SET, &arg);
307
308 return (error);
309}
310
311static int
312hci_force_fan(ACPI_HANDLE h, int op, UINT32 *state)
313{
314 int ret;
315
316 if (op == HCI_SET) {
317 if (*state < 0 || *state > 1)
318 return (EINVAL);
319 *state <<= HCI_FAN_SHIFT;
320 }
321 ret = hci_call(h, op, HCI_REG_FAN, state);
322 if (ret == 0 && op == HCI_GET)
323 *state >>= HCI_FAN_SHIFT;
324 return (ret);
325}
326
327static int
328hci_video_output(ACPI_HANDLE h, int op, UINT32 *video_output)
329{
330 int ret;
331 ACPI_STATUS status;
332
333 if (op == HCI_SET) {
334 if (*video_output < 1 || *video_output > 7)
335 return (EINVAL);
336 if (h == NULL)
337 return (ENXIO);
338 *video_output |= HCI_VIDEO_OUTPUT_FLAG;
339 status = acpi_SetInteger(h, METHOD_VIDEO, *video_output);
340 if (ACPI_SUCCESS(status))
341 ret = 0;
342 else
343 ret = ENXIO;
344 } else {
345 ret = hci_call(h, op, HCI_REG_VIDEO_OUTPUT, video_output);
346 if (ret == 0)
347 *video_output &= 0xff;
348 }
349
350 return (ret);
351}
352
353static int
354hci_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *brightness)
355{
356 int ret;
357
358 if (op == HCI_SET) {
359 if (*brightness < 0 || *brightness > HCI_LCD_BRIGHTNESS_MAX)
360 return (EINVAL);
361 *brightness <<= HCI_LCD_BRIGHTNESS_SHIFT;
362 }
363 ret = hci_call(h, op, HCI_REG_LCD_BRIGHTNESS, brightness);
364 if (ret == 0 && op == HCI_GET)
365 *brightness >>= HCI_LCD_BRIGHTNESS_SHIFT;
366 return (ret);
367}
368
369static int
370hci_lcd_backlight(ACPI_HANDLE h, int op, UINT32 *backlight)
371{
372 if (op == HCI_SET) {
373 if (*backlight < 0 || *backlight > 1)
374 return (EINVAL);
375 }
376 return (hci_call(h, op, HCI_REG_LCD_BACKLIGHT, backlight));
377}
378
379static int
380hci_cpu_speed(ACPI_HANDLE h, int op, UINT32 *speed)
381{
382 int ret;
383
384 if (op == HCI_SET) {
385 if (*speed < 0 || *speed > HCI_CPU_SPEED_MAX)
386 return (EINVAL);
387 *speed <<= HCI_CPU_SPEED_SHIFT;
388 }
389 ret = hci_call(h, op, HCI_REG_CPU_SPEED, speed);
390 if (ret == 0 && op == HCI_GET)
391 *speed >>= HCI_CPU_SPEED_SHIFT;
392 return (ret);
393}
394
395static int
396hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg)
397{
398 ACPI_OBJECT_LIST args;
399 ACPI_BUFFER results;
400 ACPI_OBJECT obj[HCI_WORDS];
401 ACPI_OBJECT *res;
402 int status, i, ret;
403
404 status = ENXIO;
405
406 for (i = 0; i < HCI_WORDS; i++) {
407 obj[i].Type = ACPI_TYPE_INTEGER;
408 obj[i].Integer.Value = 0;
409 }
410 obj[HCI_REG_AX].Integer.Value = op;
411 obj[HCI_REG_BX].Integer.Value = function;
412 if (op == HCI_SET)
413 obj[HCI_REG_CX].Integer.Value = *arg;
414
415 args.Count = HCI_WORDS;
416 args.Pointer = obj;
417 results.Pointer = NULL;
418 results.Length = ACPI_ALLOCATE_BUFFER;
419 if (ACPI_FAILURE(AcpiEvaluateObject(h, METHOD_HCI, &args, &results)))
420 goto end;
421 res = (ACPI_OBJECT *)results.Pointer;
422 if (!ACPI_PKG_VALID(res, HCI_WORDS)) {
423 printf("toshiba: invalid package!\n");
424 return (ENXIO);
425 }
426
427 acpi_PkgInt32(res, HCI_REG_AX, &ret);
428 if (ret == HCI_SUCCESS) {
429 if (op == HCI_GET)
430 acpi_PkgInt32(res, HCI_REG_CX, arg);
431 status = 0;
432 } else if (function == HCI_REG_SYSTEM_EVENT && op == HCI_GET &&
433 ret == HCI_NOT_SUPPORTED) {
434 /*
435 * Sometimes system events are disabled without us requesting
436 * it. This workaround attempts to re-enable them.
437 */
438 i = 1;
439 hci_call(h, HCI_SET, HCI_REG_SYSTEM_EVENT, &i);
440 }
441
442end:
443 if (results.Pointer != NULL)
444 AcpiOsFree(results.Pointer);
445
446 return (status);
447}
448
449/*
450 * Perform a few actions based on the keypress. Users can extend this
451 * functionality by reading the keystrokes we send to devd(8).
452 */
453static void
454hci_key_action(struct acpi_toshiba_softc *sc, ACPI_HANDLE h, UINT32 key)
455{
456 UINT32 arg;
457
458 switch (key) {
459 case FN_F6_RELEASE:
460 /* Decrease LCD brightness. */
461 hci_lcd_brightness(h, HCI_GET, &arg);
462 if (arg-- == 0)
463 arg = 0;
464 else
465 hci_lcd_brightness(h, HCI_SET, &arg);
466 break;
467 case FN_F7_RELEASE:
468 /* Increase LCD brightness. */
469 hci_lcd_brightness(h, HCI_GET, &arg);
470 if (arg++ == 7)
471 arg = 7;
472 else
473 hci_lcd_brightness(h, HCI_SET, &arg);
474 break;
475 case FN_F5_RELEASE:
476 /* Cycle through video outputs. */
477 hci_video_output(h, HCI_GET, &arg);
478 arg = (arg + 1) % 7;
479 hci_video_output(sc->video_handle, HCI_SET, &arg);
480 break;
481 case FN_F8_RELEASE:
482 /* Toggle LCD backlight. */
483 hci_lcd_backlight(h, HCI_GET, &arg);
484 arg = (arg != 0) ? 0 : 1;
485 hci_lcd_backlight(h, HCI_SET, &arg);
486 break;
487 case FN_ESC_RELEASE:
488 /* Toggle forcing fan on. */
489 hci_force_fan(h, HCI_GET, &arg);
490 arg = (arg != 0) ? 0 : 1;
491 hci_force_fan(h, HCI_SET, &arg);
492 break;
493 }
494}
495
496static void
497acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify, void *context)
498{
499 struct acpi_toshiba_softc *sc;
500 UINT32 key;
501
502 sc = (struct acpi_toshiba_softc *)context;
503
504 if (notify == 0x80) {
505 while (hci_call(h, HCI_GET, HCI_REG_SYSTEM_EVENT, &key) == 0) {
506 hci_key_action(sc, h, key);
507 acpi_UserNotify("TOSHIBA", h, (uint8_t)key);
508 }
509 } else
510 device_printf(sc->dev, "unknown notify: 0x%x\n", notify);
511}
512
513/*
514 * Toshiba video pseudo-device to provide the DSSX method.
515 *
516 * HID Model
517 * -------------------------------------
518 * TOS6201 Libretto L Series
519 */
520static int
521acpi_toshiba_video_probe(device_t dev)
522{
523 int ret = ENXIO;
524
525 if (!acpi_disabled("toshiba") &&
526 acpi_get_type(dev) == ACPI_TYPE_DEVICE &&
527 device_get_unit(dev) == 0 &&
528 acpi_MatchHid(dev, "TOS6201")) {
529 device_quiet(dev);
530 device_set_desc(dev, "Toshiba Video");
531 ret = 0;
532 }
533
534 return (ret);
535}
536
537static int
538acpi_toshiba_video_attach(device_t dev)
539{
540 struct acpi_toshiba_softc *sc;
541
542 sc = devclass_get_softc(acpi_toshiba_devclass, 0);
543 if (sc == NULL)
544 return (ENXIO);
545 sc->video_handle = acpi_get_handle(dev);
546 return (0);
547}