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