2 * Copyright (c) 2004, 2005 Philip Paeps <philip@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * Driver for extra ACPI-controlled gadgets (hotkeys, leds, etc) found on
32 * recent Asus (and Medion) laptops. Inspired by the acpi4asus project which
33 * implements these features in the Linux kernel.
35 * <http://sourceforge.net/projects/acpi4asus/>
37 * Currently should support most features, but could use some more testing.
38 * Particularly the display-switching stuff is a bit hairy. If you have an
39 * Asus laptop which doesn't appear to be supported, or strange things happen
40 * when using this driver, please report to <acpi@FreeBSD.org>.
44 #include <sys/param.h>
45 #include <sys/kernel.h>
46 #include <sys/module.h>
50 #include <contrib/dev/acpica/acpi.h>
51 #include <dev/acpica/acpivar.h>
52 #include <dev/led/led.h>
55 #define ACPI_ASUS_METHOD_BRN 1
56 #define ACPI_ASUS_METHOD_DISP 2
57 #define ACPI_ASUS_METHOD_LCD 3
59 #define _COMPONENT ACPI_OEM
60 ACPI_MODULE_NAME("ASUS")
62 struct acpi_asus_model {
82 struct acpi_asus_led {
83 struct acpi_asus_softc *sc;
95 struct acpi_asus_softc {
99 struct acpi_asus_model *model;
100 struct sysctl_ctx_list sysctl_ctx;
101 struct sysctl_oid *sysctl_tree;
103 struct acpi_asus_led s_bled;
104 struct acpi_asus_led s_mled;
105 struct acpi_asus_led s_tled;
106 struct acpi_asus_led s_wled;
114 * We can identify Asus laptops from the string they return
115 * as a result of calling the ATK0100 'INIT' method.
117 static struct acpi_asus_model acpi_asus_models[] = {
123 .lcd_set = "\\_SB.PCI0.SBRG.EC0._Q10",
126 .disp_get = "\\ADVG",
133 .lcd_set = "\\_SB.PCI0.ISA.EC0._Q10",
134 .brn_up = "\\_SB.PCI0.ISA.EC0._Q0E",
135 .brn_dn = "\\_SB.PCI0.ISA.EC0._Q0F"
145 .disp_get = "\\INFB",
155 .disp_get = "\\INFB",
174 .lcd_set = "\\_SB.PCI0.PX40.ECD0._Q10"
191 .lcd_get = "\\_SB.PCI0.PM.PBC",
193 .disp_get = "\\_SB.INFB",
202 .lcd_get = "\\_SB.PCI0.SBSM.SEO4",
203 .lcd_set = "\\_SB.PCI0.SBRG.EC0._Q10",
204 .disp_get = "\\_SB.PCI0.P0P1.VGA.GETD",
215 .disp_get = "\\INFB",
220 /* Only has hotkeys, apparantly */
225 .brn_up = "\\_SB.PCI0.PX40.EC0.Q0E",
226 .brn_dn = "\\_SB.PCI0.PX40.EC0.Q0F",
228 .lcd_set = "\\_SB.PCI0.PX40.EC0.Q10"
243 .lcd_set = "\\_SB.PCI0.SBRG.EC0._Q10",
244 .lcd_get = "\\_SB.BKLT",
256 .lcd_get = "\\_SB.PCI0.SBSM.SEO4",
257 .lcd_set = "\\_SB.PCI0.SBRG.EC0._Q10",
258 .disp_get = "\\SSTE",
266 .lcd_set = "\\_SB.PCI0.PX40.Q10",
274 .lcd_set = "\\_SB.PCI0.ISA.EC0._Q10",
275 .brn_up = "\\_SB.PCI0.ISA.EC0._Q0B",
276 .brn_dn = "\\_SB.PCI0.ISA.EC0._Q0A"
284 .lcd_set = "\\_SB.PCI0.SBRG.EC0._Q10",
287 .disp_get = "\\_SB.PCI0.P0P1.VGA.GETD",
295 * Samsung P30/P35 laptops have an Asus ATK0100 gadget interface,
296 * but they can't be probed quite the same way as Asus laptops.
298 static struct acpi_asus_model acpi_samsung_models[] = {
302 .brn_up = "\\_SB.PCI0.LPCB.EC0._Q68",
303 .brn_dn = "\\_SB.PCI0.LPCB.EC0._Q69",
305 .lcd_set = "\\_SB.PCI0.LPCB.EC0._Q0E"
315 } acpi_asus_sysctls[] = {
317 .name = "lcd_backlight",
318 .method = ACPI_ASUS_METHOD_LCD,
319 .description = "state of the lcd backlight"
322 .name = "lcd_brightness",
323 .method = ACPI_ASUS_METHOD_BRN,
324 .description = "brightness of the lcd panel"
327 .name = "video_output",
328 .method = ACPI_ASUS_METHOD_DISP,
329 .description = "display output state"
335 ACPI_SERIAL_DECL(asus, "ACPI ASUS extras");
337 /* Function prototypes */
338 static int acpi_asus_probe(device_t dev);
339 static int acpi_asus_attach(device_t dev);
340 static int acpi_asus_detach(device_t dev);
342 static void acpi_asus_led(struct acpi_asus_led *led, int state);
343 static void acpi_asus_led_task(struct acpi_asus_led *led, int pending __unused);
345 static int acpi_asus_sysctl(SYSCTL_HANDLER_ARGS);
346 static int acpi_asus_sysctl_init(struct acpi_asus_softc *sc, int method);
347 static int acpi_asus_sysctl_get(struct acpi_asus_softc *sc, int method);
348 static int acpi_asus_sysctl_set(struct acpi_asus_softc *sc, int method, int val);
350 static void acpi_asus_notify(ACPI_HANDLE h, UINT32 notify, void *context);
352 static device_method_t acpi_asus_methods[] = {
353 DEVMETHOD(device_probe, acpi_asus_probe),
354 DEVMETHOD(device_attach, acpi_asus_attach),
355 DEVMETHOD(device_detach, acpi_asus_detach),
360 static driver_t acpi_asus_driver = {
363 sizeof(struct acpi_asus_softc)
366 static devclass_t acpi_asus_devclass;
368 DRIVER_MODULE(acpi_asus, acpi, acpi_asus_driver, acpi_asus_devclass, 0, 0);
369 MODULE_DEPEND(acpi_asus, acpi, 1, 1, 1);
372 acpi_asus_probe(device_t dev)
374 struct acpi_asus_model *model;
375 struct acpi_asus_softc *sc;
378 ACPI_OBJECT Arg, *Obj;
379 ACPI_OBJECT_LIST Args;
380 static char *asus_ids[] = { "ATK0100", NULL };
382 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
384 if (acpi_disabled("asus") ||
385 ACPI_ID_PROBE(device_get_parent(dev), dev, asus_ids) == NULL)
388 sc = device_get_softc(dev);
390 sc->handle = acpi_get_handle(dev);
392 Arg.Type = ACPI_TYPE_INTEGER;
393 Arg.Integer.Value = 0;
399 Buf.Length = ACPI_ALLOCATE_BUFFER;
401 AcpiEvaluateObject(sc->handle, "INIT", &Args, &Buf);
405 * The Samsung P30 returns a null-pointer from INIT, we
406 * can identify it from the 'ODEM' string in the DSDT.
408 if (Obj->String.Pointer == NULL) {
410 ACPI_TABLE_HEADER th;
412 status = AcpiGetTableHeader(ACPI_TABLE_DSDT, 1, &th);
413 if (ACPI_FAILURE(status)) {
414 device_printf(dev, "Unsupported (Samsung?) laptop\n");
415 AcpiOsFree(Buf.Pointer);
419 if (strncmp("ODEM", th.OemTableId, 4) == 0) {
420 sc->model = &acpi_samsung_models[0];
421 device_set_desc(dev, "Samsung P30 Laptop Extras");
422 AcpiOsFree(Buf.Pointer);
427 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
432 * Asus laptops are simply identified by name, easy!
434 for (model = acpi_asus_models; model->name != NULL; model++) {
435 if (strncmp(Obj->String.Pointer, model->name, 3) == 0) {
438 sbuf_printf(sb, "Asus %s Laptop Extras",
439 Obj->String.Pointer);
443 device_set_desc_copy(dev, sbuf_data(sb));
446 AcpiOsFree(Buf.Pointer);
451 * Some models look exactly the same as other models, but have
452 * their own ids. If we spot these, set them up with the same
453 * details as the models they're like, possibly dealing with
456 * XXX: there must be a prettier way to do this!
458 else if (strncmp(model->name, "xxN", 3) == 0 &&
459 (strncmp(Obj->String.Pointer, "M3N", 3) == 0 ||
460 strncmp(Obj->String.Pointer, "S1N", 3) == 0))
462 else if (strncmp(model->name, "A1x", 3) == 0 &&
463 strncmp(Obj->String.Pointer, "A1", 2) == 0)
465 else if (strncmp(model->name, "A2x", 3) == 0 &&
466 strncmp(Obj->String.Pointer, "A2", 2) == 0)
468 else if (strncmp(model->name, "D1x", 3) == 0 &&
469 strncmp(Obj->String.Pointer, "D1", 2) == 0)
471 else if (strncmp(model->name, "L3H", 3) == 0 &&
472 strncmp(Obj->String.Pointer, "L2E", 3) == 0)
474 else if (strncmp(model->name, "L5x", 3) == 0 &&
475 strncmp(Obj->String.Pointer, "L5", 2) == 0)
477 else if (strncmp(model->name, "M2E", 3) == 0 &&
478 (strncmp(Obj->String.Pointer, "M2", 2) == 0 ||
479 strncmp(Obj->String.Pointer, "L4E", 3) == 0))
481 else if (strncmp(model->name, "S1x", 3) == 0 &&
482 (strncmp(Obj->String.Pointer, "L8", 2) == 0 ||
483 strncmp(Obj->String.Pointer, "S1", 2) == 0))
485 else if (strncmp(model->name, "S2x", 3) == 0 &&
486 (strncmp(Obj->String.Pointer, "J1", 2) == 0 ||
487 strncmp(Obj->String.Pointer, "S2", 2) == 0))
490 /* L2B is like L3C but has no lcd_get method */
491 else if (strncmp(model->name, "L3C", 3) == 0 &&
492 strncmp(Obj->String.Pointer, "L2B", 3) == 0) {
493 model->lcd_get = NULL;
497 /* A3G is like M6R but with a different lcd_get method */
498 else if (strncmp(model->name, "M6R", 3) == 0 &&
499 strncmp(Obj->String.Pointer, "A3G", 3) == 0) {
500 model->lcd_get = "\\BLFG";
504 /* M2N and W1N are like xxN with added WLED */
505 else if (strncmp(model->name, "xxN", 3) == 0 &&
506 (strncmp(Obj->String.Pointer, "M2N", 3) == 0 ||
507 strncmp(Obj->String.Pointer, "W1N", 3) == 0)) {
508 model->wled_set = "WLED";
512 /* M5N and S5N are like xxN without MLED */
513 else if (strncmp(model->name, "xxN", 3) == 0 &&
514 (strncmp(Obj->String.Pointer, "M5N", 3) == 0 ||
515 strncmp(Obj->String.Pointer, "S5N", 3) == 0)) {
516 model->mled_set = NULL;
521 sbuf_printf(sb, "Unsupported Asus laptop: %s\n", Obj->String.Pointer);
524 device_printf(dev, sbuf_data(sb));
527 AcpiOsFree(Buf.Pointer);
533 acpi_asus_attach(device_t dev)
535 struct acpi_asus_softc *sc;
536 struct acpi_softc *acpi_sc;
538 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
540 sc = device_get_softc(dev);
541 acpi_sc = acpi_device_get_parent_softc(dev);
543 /* Build sysctl tree */
544 sysctl_ctx_init(&sc->sysctl_ctx);
545 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
546 SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree),
547 OID_AUTO, "asus", CTLFLAG_RD, 0, "");
550 for (int i = 0; acpi_asus_sysctls[i].name != NULL; i++) {
551 if (!acpi_asus_sysctl_init(sc, acpi_asus_sysctls[i].method))
554 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
555 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
556 acpi_asus_sysctls[i].name,
557 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY,
558 sc, i, acpi_asus_sysctl, "I",
559 acpi_asus_sysctls[i].description);
563 if (sc->model->bled_set) {
566 sc->s_bled.type = ACPI_ASUS_LED_BLED;
568 led_create((led_t *)acpi_asus_led, &sc->s_bled, "bled");
571 if (sc->model->mled_set) {
574 sc->s_mled.type = ACPI_ASUS_LED_MLED;
576 led_create((led_t *)acpi_asus_led, &sc->s_mled, "mled");
579 if (sc->model->tled_set) {
582 sc->s_tled.type = ACPI_ASUS_LED_TLED;
584 led_create((led_t *)acpi_asus_led, &sc->s_tled, "tled");
587 if (sc->model->wled_set) {
590 sc->s_wled.type = ACPI_ASUS_LED_WLED;
592 led_create((led_t *)acpi_asus_led, &sc->s_wled, "wled");
595 /* Activate hotkeys */
596 AcpiEvaluateObject(sc->handle, "BSTS", NULL, NULL);
598 /* Handle notifies */
599 AcpiInstallNotifyHandler(sc->handle, ACPI_SYSTEM_NOTIFY,
600 acpi_asus_notify, dev);
606 acpi_asus_detach(device_t dev)
608 struct acpi_asus_softc *sc;
610 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
612 sc = device_get_softc(dev);
614 /* Turn the lights off */
615 if (sc->model->bled_set)
616 led_destroy(sc->s_bled.cdev);
618 if (sc->model->mled_set)
619 led_destroy(sc->s_mled.cdev);
621 if (sc->model->tled_set)
622 led_destroy(sc->s_tled.cdev);
624 if (sc->model->wled_set)
625 led_destroy(sc->s_wled.cdev);
627 /* Remove notify handler */
628 AcpiRemoveNotifyHandler(sc->handle, ACPI_SYSTEM_NOTIFY,
631 /* Free sysctl tree */
632 sysctl_ctx_free(&sc->sysctl_ctx);
638 acpi_asus_led_task(struct acpi_asus_led *led, int pending __unused)
640 struct acpi_asus_softc *sc;
644 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
649 case ACPI_ASUS_LED_BLED:
650 method = sc->model->bled_set;
653 case ACPI_ASUS_LED_MLED:
654 method = sc->model->mled_set;
659 case ACPI_ASUS_LED_TLED:
660 method = sc->model->tled_set;
663 case ACPI_ASUS_LED_WLED:
664 method = sc->model->wled_set;
668 printf("acpi_asus_led: invalid LED type %d\n",
673 acpi_SetInteger(sc->handle, method, state);
678 acpi_asus_led(struct acpi_asus_led *led, int state)
681 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
689 AcpiOsQueueForExecution(OSD_PRIORITY_LO,
690 (void *)acpi_asus_led_task, led);
694 acpi_asus_sysctl(SYSCTL_HANDLER_ARGS)
696 struct acpi_asus_softc *sc;
702 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
704 sc = (struct acpi_asus_softc *)oidp->oid_arg1;
705 function = oidp->oid_arg2;
706 method = acpi_asus_sysctls[function].method;
708 ACPI_SERIAL_BEGIN(asus);
709 arg = acpi_asus_sysctl_get(sc, method);
710 error = sysctl_handle_int(oidp, &arg, 0, req);
713 if (error != 0 || req->newptr == NULL)
717 error = acpi_asus_sysctl_set(sc, method, arg);
720 ACPI_SERIAL_END(asus);
725 acpi_asus_sysctl_get(struct acpi_asus_softc *sc, int method)
729 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
730 ACPI_SERIAL_ASSERT(asus);
733 case ACPI_ASUS_METHOD_BRN:
736 case ACPI_ASUS_METHOD_DISP:
739 case ACPI_ASUS_METHOD_LCD:
748 acpi_asus_sysctl_set(struct acpi_asus_softc *sc, int method, int arg)
750 ACPI_STATUS status = AE_OK;
752 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
753 ACPI_SERIAL_ASSERT(asus);
756 case ACPI_ASUS_METHOD_BRN:
757 if (arg < 0 || arg > 15)
760 if (sc->model->brn_set)
761 status = acpi_SetInteger(sc->handle,
762 sc->model->brn_set, arg);
765 status = AcpiEvaluateObject(sc->handle,
766 (arg > 0) ? sc->model->brn_up :
767 sc->model->brn_dn, NULL, NULL);
768 (arg > 0) ? arg-- : arg++;
772 if (ACPI_SUCCESS(status))
776 case ACPI_ASUS_METHOD_DISP:
777 if (arg < 0 || arg > 7)
780 status = acpi_SetInteger(sc->handle,
781 sc->model->disp_set, arg);
783 if (ACPI_SUCCESS(status))
787 case ACPI_ASUS_METHOD_LCD:
788 if (arg < 0 || arg > 1)
791 if (strncmp(sc->model->name, "L3H", 3) != 0)
792 status = AcpiEvaluateObject(sc->handle,
793 sc->model->lcd_set, NULL, NULL);
795 status = acpi_SetInteger(sc->handle,
796 sc->model->lcd_set, 0x7);
798 if (ACPI_SUCCESS(status))
808 acpi_asus_sysctl_init(struct acpi_asus_softc *sc, int method)
813 case ACPI_ASUS_METHOD_BRN:
814 if (sc->model->brn_get) {
815 /* GPLV/SPLV models */
816 status = acpi_GetInteger(sc->handle,
817 sc->model->brn_get, &sc->s_brn);
818 if (ACPI_SUCCESS(status))
820 } else if (sc->model->brn_up) {
821 /* Relative models */
822 status = AcpiEvaluateObject(sc->handle,
823 sc->model->brn_up, NULL, NULL);
824 if (ACPI_FAILURE(status))
827 status = AcpiEvaluateObject(sc->handle,
828 sc->model->brn_dn, NULL, NULL);
829 if (ACPI_FAILURE(status))
835 case ACPI_ASUS_METHOD_DISP:
836 if (sc->model->disp_get) {
837 status = acpi_GetInteger(sc->handle,
838 sc->model->disp_get, &sc->s_disp);
839 if (ACPI_SUCCESS(status))
843 case ACPI_ASUS_METHOD_LCD:
844 if (sc->model->lcd_get &&
845 strncmp(sc->model->name, "L3H", 3) != 0) {
846 status = acpi_GetInteger(sc->handle,
847 sc->model->lcd_get, &sc->s_lcd);
848 if (ACPI_SUCCESS(status))
851 else if (sc->model->lcd_get) {
853 ACPI_OBJECT Arg[2], Obj;
854 ACPI_OBJECT_LIST Args;
856 /* L3H is a bit special */
857 Arg[0].Type = ACPI_TYPE_INTEGER;
858 Arg[0].Integer.Value = 0x02;
859 Arg[1].Type = ACPI_TYPE_INTEGER;
860 Arg[1].Integer.Value = 0x03;
865 Buf.Length = sizeof(Obj);
868 status = AcpiEvaluateObject(sc->handle,
869 sc->model->lcd_get, &Args, &Buf);
870 if (ACPI_SUCCESS(status) &&
871 Obj.Type == ACPI_TYPE_INTEGER) {
872 sc->s_lcd = Obj.Integer.Value >> 8;
882 acpi_asus_notify(ACPI_HANDLE h, UINT32 notify, void *context)
884 struct acpi_asus_softc *sc;
885 struct acpi_softc *acpi_sc;
887 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
889 sc = device_get_softc((device_t)context);
890 acpi_sc = acpi_device_get_parent_softc(sc->dev);
892 ACPI_SERIAL_BEGIN(asus);
893 if ((notify & ~0x10) <= 15) {
894 sc->s_brn = notify & ~0x10;
895 ACPI_VPRINT(sc->dev, acpi_sc, "Brightness increased\n");
896 } else if ((notify & ~0x20) <= 15) {
897 sc->s_brn = notify & ~0x20;
898 ACPI_VPRINT(sc->dev, acpi_sc, "Brightness decreased\n");
899 } else if (notify == 0x33) {
901 ACPI_VPRINT(sc->dev, acpi_sc, "LCD turned on\n");
902 } else if (notify == 0x34) {
904 ACPI_VPRINT(sc->dev, acpi_sc, "LCD turned off\n");
907 acpi_UserNotify("ASUS", h, notify);
909 ACPI_SERIAL_END(asus);