Merge from vendor branch AWK:
[dragonfly.git] / sys / i386 / acpica5 / acpi_toshiba.c
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.3 2004/01/12 19:35:31 njl Exp $
27  * $DragonFly: src/sys/i386/acpica5/Attic/acpi_toshiba.c,v 1.1 2004/02/21 06:48:05 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 #define _COMPONENT      ACPI_TOSHIBA
39 ACPI_MODULE_NAME("TOSHIBA")
40
41 /*
42  * Toshiba HCI interface definitions
43  *
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.
50  */
51
52 #define METHOD_HCI              "GHCI"
53 #define METHOD_HCI_ENABLE       "ENAB"
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
122 struct acpi_toshiba_softc {
123         device_t        dev;
124         ACPI_HANDLE     handle;
125         struct          sysctl_ctx_list sysctl_ctx;
126         struct          sysctl_oid *sysctl_tree;
127 };
128
129 /* Prototype for HCI functions for getting/setting a value. */
130 typedef int     hci_fn_t(ACPI_HANDLE, int, UINT32 *);
131
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,
144                                     void *context);
145
146 /* Table of sysctl names and HCI functions to call. */
147 static struct {
148         char            *name;
149         hci_fn_t        *handler;
150 } sysctl_table[] = {
151         /* name,                handler */
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},
157         {NULL, NULL}
158 };
159
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),
164
165         {0, 0}
166 };
167
168 static driver_t acpi_toshiba_driver = {
169         "acpi_toshiba",
170         acpi_toshiba_methods,
171         sizeof(struct acpi_toshiba_softc),
172 };
173
174 static devclass_t acpi_toshiba_devclass;
175 DRIVER_MODULE(acpi_toshiba, acpi, acpi_toshiba_driver, acpi_toshiba_devclass,
176               0, 0);
177 MODULE_DEPEND(acpi_toshiba, acpi, 100, 100, 100);
178
179 static int      enable_fn_keys = 1;
180 TUNABLE_INT("hw.acpi.toshiba.enable_fn_keys", &enable_fn_keys);
181
182 /*
183  * HID      Model
184  * -------------------------------------
185  * TOS6200  Libretto L Series
186  *          Dynabook Satellite 2455
187  *          Dynabook SS 3500
188  * TOS6207  Dynabook SS2110 Series
189  */
190 static int
191 acpi_toshiba_probe(device_t dev)
192 {
193         int ret = ENXIO;
194
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");
201                 ret = 0;
202         }
203
204         return (ret);
205 }
206
207 static int
208 acpi_toshiba_attach(device_t dev)
209 {
210         struct          acpi_toshiba_softc *sc;
211         struct          acpi_softc *acpi_sc;
212         ACPI_STATUS     status;
213         int             i;
214
215         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
216
217         sc = device_get_softc(dev);
218         sc->dev = dev;
219         sc->handle = acpi_get_handle(dev);
220
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, "");
226
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", "");
233         }
234
235         if (enable_fn_keys != 0) {
236                 status = AcpiEvaluateObject(sc->handle, METHOD_HCI_ENABLE,
237                                             NULL, NULL);
238                 if (ACPI_FAILURE(status)) {
239                         device_printf(dev, "enable FN keys failed\n");
240                         sysctl_ctx_free(&sc->sysctl_ctx);
241                         return (ENXIO);
242                 }
243                 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
244                                          acpi_toshiba_notify, sc);
245         }
246
247         return (0);
248 }
249
250 static int
251 acpi_toshiba_detach(device_t dev)
252 {
253         struct          acpi_toshiba_softc *sc;
254
255         ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
256
257         sc = device_get_softc(dev);
258         if (enable_fn_keys != 0) {
259                 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
260                                         acpi_toshiba_notify);
261         }
262         sysctl_ctx_free(&sc->sysctl_ctx);
263
264         return (0);
265 }
266
267 static int
268 acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS)
269 {
270         struct          acpi_toshiba_softc *sc;
271         UINT32          arg;
272         int             function, error = 0;
273         hci_fn_t        *handler;
274
275         sc = (struct acpi_toshiba_softc *)oidp->oid_arg1;
276         function = oidp->oid_arg2;
277         handler = sysctl_table[function].handler;
278
279         /* Get the current value from the appropriate function. */
280         error = handler(sc->handle, HCI_GET, &arg);
281         if (error != 0)
282                 return (error);
283
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)
287                 return (error);
288
289         /* Set the new value via the appropriate function. */
290         error = handler(sc->handle, HCI_SET, &arg);
291
292         return (error);
293 }
294
295 static int
296 hci_force_fan(ACPI_HANDLE h, int op, UINT32 *state)
297 {
298         int             ret;
299
300         if (op == HCI_SET) {
301                 if (*state < 0 || *state > 1)
302                         return (EINVAL);
303                 *state <<= HCI_FAN_SHIFT;
304         }
305         ret = hci_call(h, op, HCI_REG_FAN, state);
306         if (ret == 0 && op == HCI_GET)
307                 *state >>= HCI_FAN_SHIFT;
308         return (ret);
309 }
310
311 static int
312 hci_video_output(ACPI_HANDLE h, int op, UINT32 *video_output)
313 {
314         int             ret;
315
316         if (op == HCI_SET) {
317                 if (*video_output < 1 || *video_output > 7)
318                         return (EINVAL);
319                 *video_output |= HCI_VIDEO_OUTPUT_FLAG;
320         }
321         ret = hci_call(h, op, HCI_REG_VIDEO_OUTPUT, video_output);
322         if (ret == 0 && op == HCI_GET)
323                 *video_output &= 0xff;
324         return (ret);
325 }
326
327 static int
328 hci_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *brightness)
329 {
330         int             ret;
331
332         if (op == HCI_SET) {
333                 if (*brightness < 0 || *brightness > HCI_LCD_BRIGHTNESS_MAX)
334                         return (EINVAL);
335                 *brightness <<= HCI_LCD_BRIGHTNESS_SHIFT;
336         }
337         ret = hci_call(h, op, HCI_REG_LCD_BRIGHTNESS, brightness);
338         if (ret == 0 && op == HCI_GET)
339                 *brightness >>= HCI_LCD_BRIGHTNESS_SHIFT;
340         return (ret);
341 }
342
343 static int
344 hci_lcd_backlight(ACPI_HANDLE h, int op, UINT32 *backlight)
345 {
346         if (op == HCI_SET) {
347                 if (*backlight < 0 || *backlight > 1)
348                         return (EINVAL);
349         }
350         return (hci_call(h, op, HCI_REG_LCD_BACKLIGHT, backlight));
351 }
352
353 static int
354 hci_cpu_speed(ACPI_HANDLE h, int op, UINT32 *speed)
355 {
356         int             ret;
357
358         if (op == HCI_SET) {
359                 if (*speed < 0 || *speed > HCI_CPU_SPEED_MAX)
360                         return (EINVAL);
361                 *speed <<= HCI_CPU_SPEED_SHIFT;
362         }
363         ret = hci_call(h, op, HCI_REG_CPU_SPEED, speed);
364         if (ret == 0 && op == HCI_GET)
365                 *speed >>= HCI_CPU_SPEED_SHIFT;
366         return (ret);
367 }
368
369 static int
370 hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg)
371 {
372         ACPI_OBJECT_LIST args;
373         ACPI_BUFFER     results;
374         ACPI_OBJECT     obj[HCI_WORDS];
375         ACPI_OBJECT     *res;
376         int             status, i, ret;
377
378         status = ENXIO;
379
380         for (i = 0; i < HCI_WORDS; i++) {
381                 obj[i].Type = ACPI_TYPE_INTEGER;
382                 obj[i].Integer.Value = 0;
383         }
384         obj[HCI_REG_AX].Integer.Value = op;
385         obj[HCI_REG_BX].Integer.Value = function;
386         if (op == HCI_SET)
387                 obj[HCI_REG_CX].Integer.Value = *arg;
388
389         args.Count = HCI_WORDS;
390         args.Pointer = obj;
391         results.Pointer = NULL;
392         results.Length = ACPI_ALLOCATE_BUFFER;
393         if (ACPI_FAILURE(AcpiEvaluateObject(h, METHOD_HCI, &args, &results)))
394                 goto end;
395         res = (ACPI_OBJECT *)results.Pointer;
396         if (!ACPI_PKG_VALID(res, HCI_WORDS)) {
397                 printf("toshiba: invalid package!\n");
398                 return (ENXIO);
399         }
400
401         acpi_PkgInt32(res, HCI_REG_AX, &ret);
402         if (ret == HCI_SUCCESS) {
403                 if (op == HCI_GET)
404                         acpi_PkgInt32(res, HCI_REG_CX, arg);
405                 status = 0;
406         } else if (function == HCI_REG_SYSTEM_EVENT && op == HCI_GET &&
407             ret == HCI_NOT_SUPPORTED) {
408                 /*
409                  * Sometimes system events are disabled without us requesting
410                  * it.  This workaround attempts to re-enable them.
411                  */
412                  i = 1;
413                  hci_call(h, HCI_SET, HCI_REG_SYSTEM_EVENT, &i);
414         }
415
416 end:
417         if (results.Pointer != NULL)
418                 AcpiOsFree(results.Pointer);
419
420         return (status);
421 }
422
423 /*
424  * Perform a few actions based on the keypress.  Users can extend this
425  * functionality by reading the keystrokes we send to devd(8).
426  */
427 static void
428 hci_key_action(ACPI_HANDLE h, UINT32 key)
429 {
430         UINT32          arg;
431
432         switch (key) {
433         case FN_F6_RELEASE:
434                 /* Decrease LCD brightness. */
435                 hci_lcd_brightness(h, HCI_GET, &arg);
436                 if (arg-- == 0)
437                         arg = 0;
438                 else
439                         hci_lcd_brightness(h, HCI_SET, &arg);
440                 break;
441         case FN_F7_RELEASE:
442                 /* Increase LCD brightness. */
443                 hci_lcd_brightness(h, HCI_GET, &arg);
444                 if (arg++ == 7)
445                         arg = 7;
446                 else
447                         hci_lcd_brightness(h, HCI_SET, &arg);
448                 break;
449         case FN_F5_RELEASE:
450                 /* Cycle through video outputs. */
451                 hci_video_output(h, HCI_GET, &arg);
452                 arg = (arg + 1) % 7;
453                 hci_video_output(h, HCI_SET, &arg);
454                 break;
455         case FN_F8_RELEASE:
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);
460                 break;
461         case FN_ESC_RELEASE:
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);
466                 break;
467         }
468 }
469
470 static void
471 acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify, void *context)
472 {
473         struct          acpi_toshiba_softc *sc;
474         UINT32          key;
475
476         sc = (struct acpi_toshiba_softc *)context;
477
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);
482                 }
483         } else {
484                 device_printf(sc->dev, "unknown notify: 0x%x\n", notify);
485         }
486 }