2 * Copyright (c) 2003 Hiroyuki Aizu <aizu@navi.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/i386/acpica/acpi_toshiba.c,v 1.7 2004/04/14 03:32:01 njl Exp $
27 * $DragonFly: src/sys/i386/acpica5/Attic/acpi_toshiba.c,v 1.2 2004/06/27 08:52:45 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
39 * Toshiba HCI interface definitions
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.
49 #define METHOD_HCI "GHCI"
50 #define METHOD_HCI_ENABLE "ENAB"
51 #define METHOD_VIDEO "DSSX"
54 #define HCI_SET 0xFF00
55 #define HCI_GET 0xFE00
58 #define HCI_SUCCESS 0x0000
59 #define HCI_FAILURE 0x1000
60 #define HCI_NOT_SUPPORTED 0x8000
61 #define HCI_EMPTY 0x8C00
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
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)
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
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? */
120 struct acpi_toshiba_softc {
123 ACPI_HANDLE video_handle;
124 struct sysctl_ctx_list sysctl_ctx;
125 struct sysctl_oid *sysctl_tree;
128 /* Prototype for HCI functions for getting/setting a value. */
129 typedef int hci_fn_t(ACPI_HANDLE, int, UINT32 *);
131 static int acpi_toshiba_probe(device_t dev);
132 static int acpi_toshiba_attach(device_t dev);
133 static int acpi_toshiba_detach(device_t dev);
134 static int acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS);
135 static hci_fn_t hci_force_fan;
136 static hci_fn_t hci_video_output;
137 static hci_fn_t hci_lcd_brightness;
138 static hci_fn_t hci_lcd_backlight;
139 static hci_fn_t hci_cpu_speed;
140 static int hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg);
141 static void hci_key_action(struct acpi_toshiba_softc *sc, ACPI_HANDLE h,
143 static void acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify,
145 static int acpi_toshiba_video_probe(device_t dev);
146 static int acpi_toshiba_video_attach(device_t dev);
148 /* Table of sysctl names and HCI functions to call. */
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},
162 static 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),
170 static driver_t acpi_toshiba_driver = {
172 acpi_toshiba_methods,
173 sizeof(struct acpi_toshiba_softc),
176 static devclass_t acpi_toshiba_devclass;
177 DRIVER_MODULE(acpi_toshiba, acpi, acpi_toshiba_driver, acpi_toshiba_devclass,
179 MODULE_DEPEND(acpi_toshiba, acpi, 1, 1, 1);
181 static device_method_t acpi_toshiba_video_methods[] = {
182 DEVMETHOD(device_probe, acpi_toshiba_video_probe),
183 DEVMETHOD(device_attach, acpi_toshiba_video_attach),
188 static driver_t acpi_toshiba_video_driver = {
189 "acpi_toshiba_video",
190 acpi_toshiba_video_methods,
194 static devclass_t acpi_toshiba_video_devclass;
195 DRIVER_MODULE(acpi_toshiba_video, acpi, acpi_toshiba_video_driver,
196 acpi_toshiba_video_devclass, 0, 0);
197 MODULE_DEPEND(acpi_toshiba_video, acpi, 1, 1, 1);
199 static int enable_fn_keys = 1;
200 TUNABLE_INT("hw.acpi.toshiba.enable_fn_keys", &enable_fn_keys);
204 * -------------------------------------
205 * TOS6200 Libretto L Series
206 * Dynabook Satellite 2455
208 * TOS6207 Dynabook SS2110 Series
211 acpi_toshiba_probe(device_t dev)
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");
228 acpi_toshiba_attach(device_t dev)
230 struct acpi_toshiba_softc *sc;
231 struct acpi_softc *acpi_sc;
235 sc = device_get_softc(dev);
237 sc->handle = acpi_get_handle(dev);
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, "");
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", "");
253 if (enable_fn_keys != 0) {
254 status = AcpiEvaluateObject(sc->handle, METHOD_HCI_ENABLE,
256 if (ACPI_FAILURE(status)) {
257 device_printf(dev, "enable FN keys failed\n");
258 sysctl_ctx_free(&sc->sysctl_ctx);
261 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
262 acpi_toshiba_notify, sc);
269 acpi_toshiba_detach(device_t dev)
271 struct acpi_toshiba_softc *sc;
273 sc = device_get_softc(dev);
274 if (enable_fn_keys != 0) {
275 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
276 acpi_toshiba_notify);
278 sysctl_ctx_free(&sc->sysctl_ctx);
284 acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS)
286 struct acpi_toshiba_softc *sc;
288 int function, error = 0;
291 sc = (struct acpi_toshiba_softc *)oidp->oid_arg1;
292 function = oidp->oid_arg2;
293 handler = sysctl_table[function].handler;
295 /* Get the current value from the appropriate function. */
296 error = handler(sc->handle, HCI_GET, &arg);
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)
305 /* Set the new value via the appropriate function. */
306 error = handler(sc->handle, HCI_SET, &arg);
312 hci_force_fan(ACPI_HANDLE h, int op, UINT32 *state)
317 if (*state < 0 || *state > 1)
319 *state <<= HCI_FAN_SHIFT;
321 ret = hci_call(h, op, HCI_REG_FAN, state);
322 if (ret == 0 && op == HCI_GET)
323 *state >>= HCI_FAN_SHIFT;
328 hci_video_output(ACPI_HANDLE h, int op, UINT32 *video_output)
334 if (*video_output < 1 || *video_output > 7)
338 *video_output |= HCI_VIDEO_OUTPUT_FLAG;
339 status = acpi_SetInteger(h, METHOD_VIDEO, *video_output);
340 if (ACPI_SUCCESS(status))
345 ret = hci_call(h, op, HCI_REG_VIDEO_OUTPUT, video_output);
347 *video_output &= 0xff;
354 hci_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *brightness)
359 if (*brightness < 0 || *brightness > HCI_LCD_BRIGHTNESS_MAX)
361 *brightness <<= HCI_LCD_BRIGHTNESS_SHIFT;
363 ret = hci_call(h, op, HCI_REG_LCD_BRIGHTNESS, brightness);
364 if (ret == 0 && op == HCI_GET)
365 *brightness >>= HCI_LCD_BRIGHTNESS_SHIFT;
370 hci_lcd_backlight(ACPI_HANDLE h, int op, UINT32 *backlight)
373 if (*backlight < 0 || *backlight > 1)
376 return (hci_call(h, op, HCI_REG_LCD_BACKLIGHT, backlight));
380 hci_cpu_speed(ACPI_HANDLE h, int op, UINT32 *speed)
385 if (*speed < 0 || *speed > HCI_CPU_SPEED_MAX)
387 *speed <<= HCI_CPU_SPEED_SHIFT;
389 ret = hci_call(h, op, HCI_REG_CPU_SPEED, speed);
390 if (ret == 0 && op == HCI_GET)
391 *speed >>= HCI_CPU_SPEED_SHIFT;
396 hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg)
398 ACPI_OBJECT_LIST args;
400 ACPI_OBJECT obj[HCI_WORDS];
406 for (i = 0; i < HCI_WORDS; i++) {
407 obj[i].Type = ACPI_TYPE_INTEGER;
408 obj[i].Integer.Value = 0;
410 obj[HCI_REG_AX].Integer.Value = op;
411 obj[HCI_REG_BX].Integer.Value = function;
413 obj[HCI_REG_CX].Integer.Value = *arg;
415 args.Count = HCI_WORDS;
417 results.Pointer = NULL;
418 results.Length = ACPI_ALLOCATE_BUFFER;
419 if (ACPI_FAILURE(AcpiEvaluateObject(h, METHOD_HCI, &args, &results)))
421 res = (ACPI_OBJECT *)results.Pointer;
422 if (!ACPI_PKG_VALID(res, HCI_WORDS)) {
423 printf("toshiba: invalid package!\n");
427 acpi_PkgInt32(res, HCI_REG_AX, &ret);
428 if (ret == HCI_SUCCESS) {
430 acpi_PkgInt32(res, HCI_REG_CX, arg);
432 } else if (function == HCI_REG_SYSTEM_EVENT && op == HCI_GET &&
433 ret == HCI_NOT_SUPPORTED) {
435 * Sometimes system events are disabled without us requesting
436 * it. This workaround attempts to re-enable them.
439 hci_call(h, HCI_SET, HCI_REG_SYSTEM_EVENT, &i);
443 if (results.Pointer != NULL)
444 AcpiOsFree(results.Pointer);
450 * Perform a few actions based on the keypress. Users can extend this
451 * functionality by reading the keystrokes we send to devd(8).
454 hci_key_action(struct acpi_toshiba_softc *sc, ACPI_HANDLE h, UINT32 key)
460 /* Decrease LCD brightness. */
461 hci_lcd_brightness(h, HCI_GET, &arg);
465 hci_lcd_brightness(h, HCI_SET, &arg);
468 /* Increase LCD brightness. */
469 hci_lcd_brightness(h, HCI_GET, &arg);
473 hci_lcd_brightness(h, HCI_SET, &arg);
476 /* Cycle through video outputs. */
477 hci_video_output(h, HCI_GET, &arg);
479 hci_video_output(sc->video_handle, HCI_SET, &arg);
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);
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);
497 acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify, void *context)
499 struct acpi_toshiba_softc *sc;
502 sc = (struct acpi_toshiba_softc *)context;
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);
510 device_printf(sc->dev, "unknown notify: 0x%x\n", notify);
514 * Toshiba video pseudo-device to provide the DSSX method.
517 * -------------------------------------
518 * TOS6201 Libretto L Series
521 acpi_toshiba_video_probe(device_t dev)
525 if (!acpi_disabled("toshiba") &&
526 acpi_get_type(dev) == ACPI_TYPE_DEVICE &&
527 device_get_unit(dev) == 0 &&
528 acpi_MatchHid(dev, "TOS6201")) {
530 device_set_desc(dev, "Toshiba Video");
538 acpi_toshiba_video_attach(device_t dev)
540 struct acpi_toshiba_softc *sc;
542 sc = devclass_get_softc(acpi_toshiba_devclass, 0);
545 sc->video_handle = acpi_get_handle(dev);