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.3 2004/01/12 19:35:31 njl Exp $
27 * $DragonFly: src/sys/platform/pc32/acpica5/Attic/acpi_toshiba.c,v 1.1 2004/02/21 06:48:05 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
38 #define _COMPONENT ACPI_TOSHIBA
39 ACPI_MODULE_NAME("TOSHIBA")
42 * Toshiba HCI interface definitions
44 * HCI is Toshiba's "Hardware Control Interface" which is supposed to
45 * be uniform across all their models. Ideally we would just call
46 * dedicated ACPI methods instead of using this primitive interface.
47 * However, the ACPI methods seem to be incomplete in some areas (for
48 * example they allow setting, but not reading, the LCD brightness
49 * value), so this is still useful.
52 #define METHOD_HCI "GHCI"
53 #define METHOD_HCI_ENABLE "ENAB"
56 #define HCI_SET 0xFF00
57 #define HCI_GET 0xFE00
60 #define HCI_SUCCESS 0x0000
61 #define HCI_FAILURE 0x1000
62 #define HCI_NOT_SUPPORTED 0x8000
63 #define HCI_EMPTY 0x8C00
66 #define HCI_REG_LCD_BACKLIGHT 0x0002
67 #define HCI_REG_FAN 0x0004
68 #define HCI_REG_SYSTEM_EVENT 0x0016
69 #define HCI_REG_VIDEO_OUTPUT 0x001C
70 #define HCI_REG_HOTKEY_EVENT 0x001E
71 #define HCI_REG_LCD_BRIGHTNESS 0x002A
72 #define HCI_REG_CPU_SPEED 0x0032
74 /* Field definitions */
75 #define HCI_FAN_SHIFT 7
76 #define HCI_LCD_BRIGHTNESS_BITS 3
77 #define HCI_LCD_BRIGHTNESS_SHIFT (16 - HCI_LCD_BRIGHTNESS_BITS)
78 #define HCI_LCD_BRIGHTNESS_MAX ((1 << HCI_LCD_BRIGHTNESS_BITS) - 1)
79 #define HCI_VIDEO_OUTPUT_FLAG 0x0100
80 #define HCI_VIDEO_OUTPUT_LCD 0x1
81 #define HCI_VIDEO_OUTPUT_CRT 0x2
82 #define HCI_VIDEO_OUTPUT_TV 0x4
83 #define HCI_CPU_SPEED_BITS 3
84 #define HCI_CPU_SPEED_SHIFT (16 - HCI_CPU_SPEED_BITS)
85 #define HCI_CPU_SPEED_MAX ((1 << HCI_CPU_SPEED_BITS) - 1)
87 /* Key press/release events. */
88 #define FN_F1_PRESS 0x013B
89 #define FN_F1_RELEASE 0x01BB
90 #define FN_F2_PRESS 0x013C
91 #define FN_F2_RELEASE 0x01BC
92 #define FN_F3_PRESS 0x013D
93 #define FN_F3_RELEASE 0x01BD
94 #define FN_F4_PRESS 0x013E
95 #define FN_F4_RELEASE 0x01BE
96 #define FN_F5_PRESS 0x013F
97 #define FN_F5_RELEASE 0x01BF
98 #define FN_F6_PRESS 0x0140
99 #define FN_F6_RELEASE 0x01C0
100 #define FN_F7_PRESS 0x0141
101 #define FN_F7_RELEASE 0x01C1
102 #define FN_F8_PRESS 0x0142
103 #define FN_F8_RELEASE 0x01C2
104 #define FN_F9_PRESS 0x0143
105 #define FN_F9_RELEASE 0x01C3
106 #define FN_BS_PRESS 0x010E
107 #define FN_BS_RELEASE 0x018E
108 #define FN_ESC_PRESS 0x0101
109 #define FN_ESC_RELEASE 0x0181
110 #define FN_KNJ_PRESS 0x0129
111 #define FN_KNJ_RELEASE 0x01A9
113 /* HCI register definitions. */
114 #define HCI_WORDS 6 /* Number of registers */
115 #define HCI_REG_AX 0 /* Operation, then return value */
116 #define HCI_REG_BX 1 /* Function */
117 #define HCI_REG_CX 2 /* Argument (in or out) */
118 #define HCI_REG_DX 3 /* Unused? */
119 #define HCI_REG_SI 4 /* Unused? */
120 #define HCI_REG_DI 5 /* Unused? */
122 struct acpi_toshiba_softc {
125 struct sysctl_ctx_list sysctl_ctx;
126 struct sysctl_oid *sysctl_tree;
129 /* Prototype for HCI functions for getting/setting a value. */
130 typedef int hci_fn_t(ACPI_HANDLE, int, UINT32 *);
132 static int acpi_toshiba_probe(device_t dev);
133 static int acpi_toshiba_attach(device_t dev);
134 static int acpi_toshiba_detach(device_t dev);
135 static int acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS);
136 static hci_fn_t hci_force_fan;
137 static hci_fn_t hci_video_output;
138 static hci_fn_t hci_lcd_brightness;
139 static hci_fn_t hci_lcd_backlight;
140 static hci_fn_t hci_cpu_speed;
141 static int hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg);
142 static void hci_key_action(ACPI_HANDLE h, UINT32 key);
143 static void acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify,
146 /* Table of sysctl names and HCI functions to call. */
152 {"force_fan", hci_force_fan},
153 {"video_output", hci_video_output},
154 {"lcd_brightness", hci_lcd_brightness},
155 {"lcd_backlight", hci_lcd_backlight},
156 {"cpu_speed", hci_cpu_speed},
160 static device_method_t acpi_toshiba_methods[] = {
161 DEVMETHOD(device_probe, acpi_toshiba_probe),
162 DEVMETHOD(device_attach, acpi_toshiba_attach),
163 DEVMETHOD(device_detach, acpi_toshiba_detach),
168 static driver_t acpi_toshiba_driver = {
170 acpi_toshiba_methods,
171 sizeof(struct acpi_toshiba_softc),
174 static devclass_t acpi_toshiba_devclass;
175 DRIVER_MODULE(acpi_toshiba, acpi, acpi_toshiba_driver, acpi_toshiba_devclass,
177 MODULE_DEPEND(acpi_toshiba, acpi, 100, 100, 100);
179 static int enable_fn_keys = 1;
180 TUNABLE_INT("hw.acpi.toshiba.enable_fn_keys", &enable_fn_keys);
184 * -------------------------------------
185 * TOS6200 Libretto L Series
186 * Dynabook Satellite 2455
188 * TOS6207 Dynabook SS2110 Series
191 acpi_toshiba_probe(device_t dev)
195 if (!acpi_disabled("toshiba") &&
196 acpi_get_type(dev) == ACPI_TYPE_DEVICE &&
197 device_get_unit(dev) == 0 &&
198 (acpi_MatchHid(dev, "TOS6200") ||
199 acpi_MatchHid(dev, "TOS6207"))) {
200 device_set_desc(dev, "Toshiba HCI Extras");
208 acpi_toshiba_attach(device_t dev)
210 struct acpi_toshiba_softc *sc;
211 struct acpi_softc *acpi_sc;
215 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
217 sc = device_get_softc(dev);
219 sc->handle = acpi_get_handle(dev);
221 acpi_sc = acpi_device_get_parent_softc(dev);
222 sysctl_ctx_init(&sc->sysctl_ctx);
223 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
224 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
225 "toshiba", CTLFLAG_RD, 0, "");
227 for (i = 0; sysctl_table[i].name != NULL; i++) {
228 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
229 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
230 sysctl_table[i].name,
231 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
232 sc, i, acpi_toshiba_sysctl, "I", "");
235 if (enable_fn_keys != 0) {
236 status = AcpiEvaluateObject(sc->handle, METHOD_HCI_ENABLE,
238 if (ACPI_FAILURE(status)) {
239 device_printf(dev, "enable FN keys failed\n");
240 sysctl_ctx_free(&sc->sysctl_ctx);
243 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
244 acpi_toshiba_notify, sc);
251 acpi_toshiba_detach(device_t dev)
253 struct acpi_toshiba_softc *sc;
255 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
257 sc = device_get_softc(dev);
258 if (enable_fn_keys != 0) {
259 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
260 acpi_toshiba_notify);
262 sysctl_ctx_free(&sc->sysctl_ctx);
268 acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS)
270 struct acpi_toshiba_softc *sc;
272 int function, error = 0;
275 sc = (struct acpi_toshiba_softc *)oidp->oid_arg1;
276 function = oidp->oid_arg2;
277 handler = sysctl_table[function].handler;
279 /* Get the current value from the appropriate function. */
280 error = handler(sc->handle, HCI_GET, &arg);
284 /* Send the current value to the user and return if no new value. */
285 error = sysctl_handle_int(oidp, &arg, 0, req);
286 if (error != 0 || req->newptr == NULL)
289 /* Set the new value via the appropriate function. */
290 error = handler(sc->handle, HCI_SET, &arg);
296 hci_force_fan(ACPI_HANDLE h, int op, UINT32 *state)
301 if (*state < 0 || *state > 1)
303 *state <<= HCI_FAN_SHIFT;
305 ret = hci_call(h, op, HCI_REG_FAN, state);
306 if (ret == 0 && op == HCI_GET)
307 *state >>= HCI_FAN_SHIFT;
312 hci_video_output(ACPI_HANDLE h, int op, UINT32 *video_output)
317 if (*video_output < 1 || *video_output > 7)
319 *video_output |= HCI_VIDEO_OUTPUT_FLAG;
321 ret = hci_call(h, op, HCI_REG_VIDEO_OUTPUT, video_output);
322 if (ret == 0 && op == HCI_GET)
323 *video_output &= 0xff;
328 hci_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *brightness)
333 if (*brightness < 0 || *brightness > HCI_LCD_BRIGHTNESS_MAX)
335 *brightness <<= HCI_LCD_BRIGHTNESS_SHIFT;
337 ret = hci_call(h, op, HCI_REG_LCD_BRIGHTNESS, brightness);
338 if (ret == 0 && op == HCI_GET)
339 *brightness >>= HCI_LCD_BRIGHTNESS_SHIFT;
344 hci_lcd_backlight(ACPI_HANDLE h, int op, UINT32 *backlight)
347 if (*backlight < 0 || *backlight > 1)
350 return (hci_call(h, op, HCI_REG_LCD_BACKLIGHT, backlight));
354 hci_cpu_speed(ACPI_HANDLE h, int op, UINT32 *speed)
359 if (*speed < 0 || *speed > HCI_CPU_SPEED_MAX)
361 *speed <<= HCI_CPU_SPEED_SHIFT;
363 ret = hci_call(h, op, HCI_REG_CPU_SPEED, speed);
364 if (ret == 0 && op == HCI_GET)
365 *speed >>= HCI_CPU_SPEED_SHIFT;
370 hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg)
372 ACPI_OBJECT_LIST args;
374 ACPI_OBJECT obj[HCI_WORDS];
380 for (i = 0; i < HCI_WORDS; i++) {
381 obj[i].Type = ACPI_TYPE_INTEGER;
382 obj[i].Integer.Value = 0;
384 obj[HCI_REG_AX].Integer.Value = op;
385 obj[HCI_REG_BX].Integer.Value = function;
387 obj[HCI_REG_CX].Integer.Value = *arg;
389 args.Count = HCI_WORDS;
391 results.Pointer = NULL;
392 results.Length = ACPI_ALLOCATE_BUFFER;
393 if (ACPI_FAILURE(AcpiEvaluateObject(h, METHOD_HCI, &args, &results)))
395 res = (ACPI_OBJECT *)results.Pointer;
396 if (!ACPI_PKG_VALID(res, HCI_WORDS)) {
397 printf("toshiba: invalid package!\n");
401 acpi_PkgInt32(res, HCI_REG_AX, &ret);
402 if (ret == HCI_SUCCESS) {
404 acpi_PkgInt32(res, HCI_REG_CX, arg);
406 } else if (function == HCI_REG_SYSTEM_EVENT && op == HCI_GET &&
407 ret == HCI_NOT_SUPPORTED) {
409 * Sometimes system events are disabled without us requesting
410 * it. This workaround attempts to re-enable them.
413 hci_call(h, HCI_SET, HCI_REG_SYSTEM_EVENT, &i);
417 if (results.Pointer != NULL)
418 AcpiOsFree(results.Pointer);
424 * Perform a few actions based on the keypress. Users can extend this
425 * functionality by reading the keystrokes we send to devd(8).
428 hci_key_action(ACPI_HANDLE h, UINT32 key)
434 /* Decrease LCD brightness. */
435 hci_lcd_brightness(h, HCI_GET, &arg);
439 hci_lcd_brightness(h, HCI_SET, &arg);
442 /* Increase LCD brightness. */
443 hci_lcd_brightness(h, HCI_GET, &arg);
447 hci_lcd_brightness(h, HCI_SET, &arg);
450 /* Cycle through video outputs. */
451 hci_video_output(h, HCI_GET, &arg);
453 hci_video_output(h, HCI_SET, &arg);
456 /* Toggle LCD backlight. */
457 hci_lcd_backlight(h, HCI_GET, &arg);
458 arg = (arg != 0) ? 0 : 1;
459 hci_lcd_backlight(h, HCI_SET, &arg);
462 /* Toggle forcing fan on. */
463 hci_force_fan(h, HCI_GET, &arg);
464 arg = (arg != 0) ? 0 : 1;
465 hci_force_fan(h, HCI_SET, &arg);
471 acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify, void *context)
473 struct acpi_toshiba_softc *sc;
476 sc = (struct acpi_toshiba_softc *)context;
478 if (notify == 0x80) {
479 while (hci_call(h, HCI_GET, HCI_REG_SYSTEM_EVENT, &key) == 0) {
480 hci_key_action(h, key);
481 acpi_UserNotify("TOSHIBA", h, (uint8_t)key);
484 device_printf(sc->dev, "unknown notify: 0x%x\n", notify);