]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/acpi_support/acpi_toshiba.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sys / dev / acpi_support / 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  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
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 <contrib/dev/acpica/acpi.h>
38 #include <dev/acpica/acpivar.h>
39
40 #define _COMPONENT      ACPI_OEM
41 ACPI_MODULE_NAME("Toshiba")
42
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
125 struct 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. */
134 typedef int     hci_fn_t(ACPI_HANDLE, int, UINT32 *);
135
136 static int      acpi_toshiba_probe(device_t dev);
137 static int      acpi_toshiba_attach(device_t dev);
138 static int      acpi_toshiba_detach(device_t dev);
139 static int      acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS);
140 static hci_fn_t hci_force_fan;
141 static hci_fn_t hci_video_output;
142 static hci_fn_t hci_lcd_brightness;
143 static hci_fn_t hci_lcd_backlight;
144 static hci_fn_t hci_cpu_speed;
145 static int      hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg);
146 static void     hci_key_action(struct acpi_toshiba_softc *sc, ACPI_HANDLE h,
147                     UINT32 key);
148 static void     acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify,
149                     void *context);
150 static int      acpi_toshiba_video_probe(device_t dev);
151 static int      acpi_toshiba_video_attach(device_t dev);
152
153 ACPI_SERIAL_DECL(toshiba, "ACPI Toshiba Extras");
154
155 /* Table of sysctl names and HCI functions to call. */
156 static 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
169 static 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
177 static driver_t acpi_toshiba_driver = {
178         "acpi_toshiba",
179         acpi_toshiba_methods,
180         sizeof(struct acpi_toshiba_softc),
181 };
182
183 static devclass_t acpi_toshiba_devclass;
184 DRIVER_MODULE(acpi_toshiba, acpi, acpi_toshiba_driver, acpi_toshiba_devclass,
185     0, 0);
186 MODULE_DEPEND(acpi_toshiba, acpi, 1, 1, 1);
187
188 static 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
195 static driver_t acpi_toshiba_video_driver = {
196         "acpi_toshiba_video",
197         acpi_toshiba_video_methods,
198         0,
199 };
200
201 static devclass_t acpi_toshiba_video_devclass;
202 DRIVER_MODULE(acpi_toshiba_video, acpi, acpi_toshiba_video_driver,
203     acpi_toshiba_video_devclass, 0, 0);
204 MODULE_DEPEND(acpi_toshiba_video, acpi, 1, 1, 1);
205
206 static int      enable_fn_keys = 1;
207 TUNABLE_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  * TOS6208  SPA40
217  */
218 static int
219 acpi_toshiba_probe(device_t dev)
220 {
221         static char *tosh_ids[] = { "TOS6200", "TOS6207", "TOS6208", NULL };
222
223         if (acpi_disabled("toshiba") ||
224             ACPI_ID_PROBE(device_get_parent(dev), dev, tosh_ids) == NULL ||
225             device_get_unit(dev) != 0)
226                 return (ENXIO);
227
228         device_set_desc(dev, "Toshiba HCI Extras");
229         return (0);
230 }
231
232 static int
233 acpi_toshiba_attach(device_t dev)
234 {
235         struct          acpi_toshiba_softc *sc;
236         struct          acpi_softc *acpi_sc;
237         ACPI_STATUS     status;
238         int             i;
239
240         sc = device_get_softc(dev);
241         sc->dev = dev;
242         sc->handle = acpi_get_handle(dev);
243
244         acpi_sc = acpi_device_get_parent_softc(dev);
245         sysctl_ctx_init(&sc->sysctl_ctx);
246         sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
247             SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO,
248             "toshiba", CTLFLAG_RD, 0, "");
249
250         for (i = 0; sysctl_table[i].name != NULL; i++) {
251                 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
252                     SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
253                     sysctl_table[i].name,
254                     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
255                     sc, i, acpi_toshiba_sysctl, "I", "");
256         }
257
258         if (enable_fn_keys != 0) {
259                 status = AcpiEvaluateObject(sc->handle, METHOD_HCI_ENABLE,
260                                             NULL, NULL);
261                 if (ACPI_FAILURE(status)) {
262                         device_printf(dev, "enable FN keys failed\n");
263                         sysctl_ctx_free(&sc->sysctl_ctx);
264                         return (ENXIO);
265                 }
266                 AcpiInstallNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
267                                          acpi_toshiba_notify, sc);
268         }
269
270         return (0);
271 }
272
273 static int
274 acpi_toshiba_detach(device_t dev)
275 {
276         struct          acpi_toshiba_softc *sc;
277
278         sc = device_get_softc(dev);
279         if (enable_fn_keys != 0) {
280                 AcpiRemoveNotifyHandler(sc->handle, ACPI_DEVICE_NOTIFY,
281                                         acpi_toshiba_notify);
282         }
283         sysctl_ctx_free(&sc->sysctl_ctx);
284
285         return (0);
286 }
287
288 static int
289 acpi_toshiba_sysctl(SYSCTL_HANDLER_ARGS)
290 {
291         struct          acpi_toshiba_softc *sc;
292         UINT32          arg;
293         int             function, error = 0;
294         hci_fn_t        *handler;
295
296         sc = (struct acpi_toshiba_softc *)oidp->oid_arg1;
297         function = oidp->oid_arg2;
298         handler = sysctl_table[function].handler;
299
300         /* Get the current value from the appropriate function. */
301         ACPI_SERIAL_BEGIN(toshiba);
302         error = handler(sc->handle, HCI_GET, &arg);
303         if (error != 0)
304                 goto out;
305
306         /* Send the current value to the user and return if no new value. */
307         error = sysctl_handle_int(oidp, &arg, 0, req);
308         if (error != 0 || req->newptr == NULL)
309                 goto out;
310
311         /* Set the new value via the appropriate function. */
312         error = handler(sc->handle, HCI_SET, &arg);
313
314 out:
315         ACPI_SERIAL_END(toshiba);
316         return (error);
317 }
318
319 static int
320 hci_force_fan(ACPI_HANDLE h, int op, UINT32 *state)
321 {
322         int             ret;
323
324         ACPI_SERIAL_ASSERT(toshiba);
325         if (op == HCI_SET) {
326                 if (*state < 0 || *state > 1)
327                         return (EINVAL);
328                 *state <<= HCI_FAN_SHIFT;
329         }
330         ret = hci_call(h, op, HCI_REG_FAN, state);
331         if (ret == 0 && op == HCI_GET)
332                 *state >>= HCI_FAN_SHIFT;
333         return (ret);
334 }
335
336 static int
337 hci_video_output(ACPI_HANDLE h, int op, UINT32 *video_output)
338 {
339         int             ret;
340         ACPI_STATUS     status;
341
342         ACPI_SERIAL_ASSERT(toshiba);
343         if (op == HCI_SET) {
344                 if (*video_output < 1 || *video_output > 7)
345                         return (EINVAL);
346                 if (h == NULL)
347                         return (ENXIO);
348                 *video_output |= HCI_VIDEO_OUTPUT_FLAG;
349                 status = acpi_SetInteger(h, METHOD_VIDEO, *video_output);
350                 if (ACPI_SUCCESS(status))
351                         ret = 0;
352                 else
353                         ret = ENXIO;
354         } else {
355                 ret = hci_call(h, op, HCI_REG_VIDEO_OUTPUT, video_output);
356                 if (ret == 0)
357                         *video_output &= 0xff;
358         }
359
360         return (ret);
361 }
362
363 static int
364 hci_lcd_brightness(ACPI_HANDLE h, int op, UINT32 *brightness)
365 {
366         int             ret;
367
368         ACPI_SERIAL_ASSERT(toshiba);
369         if (op == HCI_SET) {
370                 if (*brightness < 0 || *brightness > HCI_LCD_BRIGHTNESS_MAX)
371                         return (EINVAL);
372                 *brightness <<= HCI_LCD_BRIGHTNESS_SHIFT;
373         }
374         ret = hci_call(h, op, HCI_REG_LCD_BRIGHTNESS, brightness);
375         if (ret == 0 && op == HCI_GET)
376                 *brightness >>= HCI_LCD_BRIGHTNESS_SHIFT;
377         return (ret);
378 }
379
380 static int
381 hci_lcd_backlight(ACPI_HANDLE h, int op, UINT32 *backlight)
382 {
383
384         ACPI_SERIAL_ASSERT(toshiba);
385         if (op == HCI_SET) {
386                 if (*backlight < 0 || *backlight > 1)
387                         return (EINVAL);
388         }
389         return (hci_call(h, op, HCI_REG_LCD_BACKLIGHT, backlight));
390 }
391
392 static int
393 hci_cpu_speed(ACPI_HANDLE h, int op, UINT32 *speed)
394 {
395         int             ret;
396
397         ACPI_SERIAL_ASSERT(toshiba);
398         if (op == HCI_SET) {
399                 if (*speed < 0 || *speed > HCI_CPU_SPEED_MAX)
400                         return (EINVAL);
401                 *speed <<= HCI_CPU_SPEED_SHIFT;
402         }
403         ret = hci_call(h, op, HCI_REG_CPU_SPEED, speed);
404         if (ret == 0 && op == HCI_GET)
405                 *speed >>= HCI_CPU_SPEED_SHIFT;
406         return (ret);
407 }
408
409 static int
410 hci_call(ACPI_HANDLE h, int op, int function, UINT32 *arg)
411 {
412         ACPI_OBJECT_LIST args;
413         ACPI_BUFFER     results;
414         ACPI_OBJECT     obj[HCI_WORDS];
415         ACPI_OBJECT     *res;
416         int             status, i, ret;
417
418         ACPI_SERIAL_ASSERT(toshiba);
419         status = ENXIO;
420
421         for (i = 0; i < HCI_WORDS; i++) {
422                 obj[i].Type = ACPI_TYPE_INTEGER;
423                 obj[i].Integer.Value = 0;
424         }
425         obj[HCI_REG_AX].Integer.Value = op;
426         obj[HCI_REG_BX].Integer.Value = function;
427         if (op == HCI_SET)
428                 obj[HCI_REG_CX].Integer.Value = *arg;
429
430         args.Count = HCI_WORDS;
431         args.Pointer = obj;
432         results.Pointer = NULL;
433         results.Length = ACPI_ALLOCATE_BUFFER;
434         if (ACPI_FAILURE(AcpiEvaluateObject(h, METHOD_HCI, &args, &results)))
435                 goto end;
436         res = (ACPI_OBJECT *)results.Pointer;
437         if (!ACPI_PKG_VALID(res, HCI_WORDS)) {
438                 printf("toshiba: invalid package!\n");
439                 return (ENXIO);
440         }
441
442         acpi_PkgInt32(res, HCI_REG_AX, &ret);
443         if (ret == HCI_SUCCESS) {
444                 if (op == HCI_GET)
445                         acpi_PkgInt32(res, HCI_REG_CX, arg);
446                 status = 0;
447         } else if (function == HCI_REG_SYSTEM_EVENT && op == HCI_GET &&
448             ret == HCI_NOT_SUPPORTED) {
449                 /*
450                  * Sometimes system events are disabled without us requesting
451                  * it.  This workaround attempts to re-enable them.
452                  *
453                  * XXX This call probably shouldn't be recursive.  Queueing
454                  * a task via AcpiOsQueueForExecution() might be better.
455                  */
456                  i = 1;
457                  hci_call(h, HCI_SET, HCI_REG_SYSTEM_EVENT, &i);
458         }
459
460 end:
461         if (results.Pointer != NULL)
462                 AcpiOsFree(results.Pointer);
463
464         return (status);
465 }
466
467 /*
468  * Perform a few actions based on the keypress.  Users can extend this
469  * functionality by reading the keystrokes we send to devd(8).
470  */
471 static void
472 hci_key_action(struct acpi_toshiba_softc *sc, ACPI_HANDLE h, UINT32 key)
473 {
474         UINT32          arg;
475
476         ACPI_SERIAL_ASSERT(toshiba);
477         switch (key) {
478         case FN_F6_RELEASE:
479                 /* Decrease LCD brightness. */
480                 hci_lcd_brightness(h, HCI_GET, &arg);
481                 if (arg-- == 0)
482                         arg = 0;
483                 else
484                         hci_lcd_brightness(h, HCI_SET, &arg);
485                 break;
486         case FN_F7_RELEASE:
487                 /* Increase LCD brightness. */
488                 hci_lcd_brightness(h, HCI_GET, &arg);
489                 if (arg++ == 7)
490                         arg = 7;
491                 else
492                         hci_lcd_brightness(h, HCI_SET, &arg);
493                 break;
494         case FN_F5_RELEASE:
495                 /* Cycle through video outputs. */
496                 hci_video_output(h, HCI_GET, &arg);
497                 arg = (arg + 1) % 7;
498                 hci_video_output(sc->video_handle, HCI_SET, &arg);
499                 break;
500         case FN_F8_RELEASE:
501                 /* Toggle LCD backlight. */
502                 hci_lcd_backlight(h, HCI_GET, &arg);
503                 arg = (arg != 0) ? 0 : 1;
504                 hci_lcd_backlight(h, HCI_SET, &arg);
505                 break;
506         case FN_ESC_RELEASE:
507                 /* Toggle forcing fan on. */
508                 hci_force_fan(h, HCI_GET, &arg);
509                 arg = (arg != 0) ? 0 : 1;
510                 hci_force_fan(h, HCI_SET, &arg);
511                 break;
512         }
513 }
514
515 static void
516 acpi_toshiba_notify(ACPI_HANDLE h, UINT32 notify, void *context)
517 {
518         struct          acpi_toshiba_softc *sc;
519         UINT32          key;
520
521         sc = (struct acpi_toshiba_softc *)context;
522
523         if (notify == 0x80) {
524                 ACPI_SERIAL_BEGIN(toshiba);
525                 while (hci_call(h, HCI_GET, HCI_REG_SYSTEM_EVENT, &key) == 0) {
526                         hci_key_action(sc, h, key);
527                         acpi_UserNotify("TOSHIBA", h, (uint8_t)key);
528                 }
529                 ACPI_SERIAL_END(toshiba);
530         } else
531                 device_printf(sc->dev, "unknown notify: 0x%x\n", notify);
532 }
533
534 /*
535  * Toshiba video pseudo-device to provide the DSSX method.
536  *
537  * HID      Model
538  * -------------------------------------
539  * TOS6201  Libretto L Series
540  */
541 static int
542 acpi_toshiba_video_probe(device_t dev)
543 {
544         static char *vid_ids[] = { "TOS6201", NULL };
545
546         if (acpi_disabled("toshiba") ||
547             ACPI_ID_PROBE(device_get_parent(dev), dev, vid_ids) == NULL ||
548             device_get_unit(dev) != 0)
549                 return (ENXIO);
550
551         device_quiet(dev);
552         device_set_desc(dev, "Toshiba Video");
553         return (0);
554 }
555
556 static int
557 acpi_toshiba_video_attach(device_t dev)
558 {
559         struct          acpi_toshiba_softc *sc;
560
561         sc = devclass_get_softc(acpi_toshiba_devclass, 0);
562         if (sc == NULL)
563                 return (ENXIO);
564         sc->video_handle = acpi_get_handle(dev);
565         return (0);
566 }