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