1/*- 2 * Copyright (c) 2009 Michael Gmelin <freebsd@grem.de> 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 --- 11 unchanged lines hidden (view full) --- 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#include <sys/cdefs.h> |
28__FBSDID("$FreeBSD: head/sys/dev/acpi_support/acpi_hp.c 195325 2009-07-03 21:12:37Z rpaulo $"); |
29 30/* 31 * Driver for extra ACPI-controlled features found on HP laptops 32 * that use a WMI enabled BIOS (e.g. HP Compaq 8510p and 6510p). 33 * Allows to control and read status of integrated hardware and read 34 * BIOS settings through CMI. 35 * Inspired by the hp-wmi driver, which implements a subset of these 36 * features (hotkeys) on Linux. --- 50 unchanged lines hidden (view full) --- 87#define ACPI_HP_METHOD_WWAN_ON_AIR 13 88#define ACPI_HP_METHOD_WWAN_ENABLE_IF_RADIO_ON 14 89#define ACPI_HP_METHOD_WWAN_DISABLE_IF_RADIO_OFF 15 90#define ACPI_HP_METHOD_ALS 16 91#define ACPI_HP_METHOD_DISPLAY 17 92#define ACPI_HP_METHOD_HDDTEMP 18 93#define ACPI_HP_METHOD_DOCK 19 94#define ACPI_HP_METHOD_CMI_DETAIL 20 |
95#define ACPI_HP_METHOD_VERBOSE 21 |
96 97#define HP_MASK_WWAN_ON_AIR 0x1000000 98#define HP_MASK_BLUETOOTH_ON_AIR 0x10000 99#define HP_MASK_WLAN_ON_AIR 0x100 100#define HP_MASK_WWAN_RADIO 0x8000000 101#define HP_MASK_BLUETOOTH_RADIO 0x80000 102#define HP_MASK_WLAN_RADIO 0x800 103#define HP_MASK_WWAN_ENABLED 0x2000000 --- 13 unchanged lines hidden (view full) --- 117struct acpi_hp_softc { 118 device_t dev; 119 ACPI_HANDLE handle; 120 device_t wmi_dev; 121 int has_notify; /* notification GUID found */ 122 int has_cmi; /* CMI GUID found */ 123 int cmi_detail; /* CMI detail level 124 (set by sysctl) */ |
125 int verbose; /* add debug output */ |
126 int wlan_enable_if_radio_on; /* set by sysctl */ 127 int wlan_disable_if_radio_off; /* set by sysctl */ 128 int bluetooth_enable_if_radio_on; /* set by sysctl */ 129 int bluetooth_disable_if_radio_off; /* set by sysctl */ 130 int wwan_enable_if_radio_on; /* set by sysctl */ 131 int wwan_disable_if_radio_off; /* set by sysctl */ 132 int was_wlan_on_air; /* last known WLAN 133 on air status */ --- 138 unchanged lines hidden (view full) --- 272 }, 273 { 274 .name = "cmi_detail", 275 .method = ACPI_HP_METHOD_CMI_DETAIL, 276 .description = "Details shown in CMI output " 277 "(cat /dev/hpcmi)", 278 .access = CTLTYPE_INT | CTLFLAG_RW 279 }, |
280 { 281 .name = "verbose", 282 .method = ACPI_HP_METHOD_VERBOSE, 283 .description = "Verbosity level", 284 .access = CTLTYPE_INT | CTLFLAG_RW 285 }, |
286 287 { NULL, 0, NULL, 0 } 288}; 289 290ACPI_SERIAL_DECL(hp, "HP ACPI-WMI Mapping"); 291 292static int acpi_hp_probe(device_t dev); 293static int acpi_hp_attach(device_t dev); --- 43 unchanged lines hidden (view full) --- 337DRIVER_MODULE(acpi_hp, acpi, acpi_hp_driver, acpi_hp_devclass, 338 0, 0); 339MODULE_DEPEND(acpi_hp, acpi_wmi, 1, 1, 1); 340MODULE_DEPEND(acpi_hp, acpi, 1, 1, 1); 341 342static void 343acpi_hp_evaluate_auto_on_off(struct acpi_hp_softc *sc) 344{ |
345 int wireless; 346 int new_wlan_status; 347 int new_bluetooth_status; 348 int new_wwan_status; |
349 350 wireless = acpi_hp_exec_wmi_command(sc->wmi_dev, 351 ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0); 352 new_wlan_status = -1; 353 new_bluetooth_status = -1; 354 new_wwan_status = -1; 355 |
356 if (sc->verbose) 357 device_printf(sc->wmi_dev, "Wireless status is %x\n", wireless); |
358 if (sc->wlan_disable_if_radio_off && !(wireless & HP_MASK_WLAN_RADIO) 359 && (wireless & HP_MASK_WLAN_ENABLED)) { 360 acpi_hp_exec_wmi_command(sc->wmi_dev, 361 ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x100); 362 new_wlan_status = 0; 363 } 364 else if (sc->wlan_enable_if_radio_on && (wireless & HP_MASK_WLAN_RADIO) 365 && !(wireless & HP_MASK_WLAN_ENABLED)) { --- 29 unchanged lines hidden (view full) --- 395 ACPI_HP_WMI_WIRELESS_COMMAND, 1, 0x404); 396 new_wwan_status = 1; 397 } 398 399 if (new_wlan_status == -1) { 400 new_wlan_status = (wireless & HP_MASK_WLAN_ON_AIR); 401 if ((new_wlan_status?1:0) != sc->was_wlan_on_air) { 402 sc->was_wlan_on_air = sc->was_wlan_on_air?0:1; |
403 if (sc->verbose) 404 device_printf(sc->wmi_dev, 405 "WLAN on air changed to %i " 406 "(new_wlan_status is %i)\n", 407 sc->was_wlan_on_air, new_wlan_status); |
408 acpi_UserNotify("HP", sc->handle, 409 0xc0+sc->was_wlan_on_air); 410 } 411 } 412 if (new_bluetooth_status == -1) { 413 new_bluetooth_status = (wireless & HP_MASK_BLUETOOTH_ON_AIR); 414 if ((new_bluetooth_status?1:0) != sc->was_bluetooth_on_air) { 415 sc->was_bluetooth_on_air = sc->was_bluetooth_on_air? 416 0:1; |
417 if (sc->verbose) 418 device_printf(sc->wmi_dev, 419 "BLUETOOTH on air changed" 420 " to %i (new_bluetooth_status is %i)\n", 421 sc->was_bluetooth_on_air, 422 new_bluetooth_status); |
423 acpi_UserNotify("HP", sc->handle, 424 0xd0+sc->was_bluetooth_on_air); 425 } 426 } 427 if (new_wwan_status == -1) { 428 new_wwan_status = (wireless & HP_MASK_WWAN_ON_AIR); 429 if ((new_wwan_status?1:0) != sc->was_wwan_on_air) { 430 sc->was_wwan_on_air = sc->was_wwan_on_air?0:1; |
431 if (sc->verbose) 432 device_printf(sc->wmi_dev, 433 "WWAN on air changed to %i" 434 " (new_wwan_status is %i)\n", 435 sc->was_wwan_on_air, new_wwan_status); |
436 acpi_UserNotify("HP", sc->handle, 437 0xe0+sc->was_wwan_on_air); 438 } 439 } 440} 441 442static int 443acpi_hp_probe(device_t dev) --- 6 unchanged lines hidden (view full) --- 450} 451 452static int 453acpi_hp_attach(device_t dev) 454{ 455 struct acpi_hp_softc *sc; 456 struct acpi_softc *acpi_sc; 457 devclass_t wmi_devclass; |
458 int arg; |
459 460 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); 461 462 sc = device_get_softc(dev); 463 sc->dev = dev; 464 sc->handle = acpi_get_handle(dev); 465 sc->has_notify = 0; 466 sc->has_cmi = 0; 467 sc->bluetooth_enable_if_radio_on = 0; 468 sc->bluetooth_disable_if_radio_off = 0; 469 sc->wlan_enable_if_radio_on = 0; 470 sc->wlan_disable_if_radio_off = 0; 471 sc->wlan_enable_if_radio_on = 0; 472 sc->wlan_disable_if_radio_off = 0; 473 sc->was_wlan_on_air = 0; 474 sc->was_bluetooth_on_air = 0; 475 sc->was_wwan_on_air = 0; 476 sc->cmi_detail = 0; 477 sc->cmi_order_size = -1; |
478 sc->verbose = 0; |
479 memset(sc->cmi_order, 0, sizeof(sc->cmi_order)); 480 acpi_sc = acpi_device_get_parent_softc(dev); 481 482 if (!(wmi_devclass = devclass_find ("acpi_wmi"))) { 483 device_printf(dev, "Couldn't find acpi_wmi devclass\n"); 484 return (EINVAL); 485 } 486 if (!(sc->wmi_dev = devclass_get_device(wmi_devclass, 0))) { --- 77 unchanged lines hidden (view full) --- 564 ACPI_SERIAL_END(hp); 565 566 return (0); 567} 568 569static int 570acpi_hp_detach(device_t dev) 571{ |
572 int ret; |
573 574 ACPI_FUNCTION_TRACE((char *)(uintptr_t) __func__); 575 struct acpi_hp_softc *sc = device_get_softc(dev); 576 if (sc->has_cmi && sc->hpcmi_open_pid != 0) { 577 ret = EBUSY; 578 } 579 else { 580 if (sc->has_notify) { --- 10 unchanged lines hidden (view full) --- 591 } 592 593 return (ret); 594} 595 596static int 597acpi_hp_sysctl(SYSCTL_HANDLER_ARGS) 598{ |
599 struct acpi_hp_softc *sc; 600 int arg; 601 int oldarg; 602 int error = 0; 603 int function; 604 int method; |
605 606 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 607 608 sc = (struct acpi_hp_softc *)oidp->oid_arg1; 609 function = oidp->oid_arg2; 610 method = acpi_hp_sysctls[function].method; 611 612 ACPI_SERIAL_BEGIN(hp); --- 6 unchanged lines hidden (view full) --- 619 ACPI_SERIAL_END(hp); 620 621 return (error); 622} 623 624static int 625acpi_hp_sysctl_get(struct acpi_hp_softc *sc, int method) 626{ |
627 int val = 0; |
628 629 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 630 ACPI_SERIAL_ASSERT(hp); 631 632 switch (method) { 633 case ACPI_HP_METHOD_WLAN_ENABLED: 634 val = acpi_hp_exec_wmi_command(sc->wmi_dev, 635 ACPI_HP_WMI_WIRELESS_COMMAND, 0, 0); --- 71 unchanged lines hidden (view full) --- 707 break; 708 case ACPI_HP_METHOD_DOCK: 709 val = acpi_hp_exec_wmi_command(sc->wmi_dev, 710 ACPI_HP_WMI_DOCK_COMMAND, 0, 0); 711 break; 712 case ACPI_HP_METHOD_CMI_DETAIL: 713 val = sc->cmi_detail; 714 break; |
715 case ACPI_HP_METHOD_VERBOSE: 716 val = sc->verbose; 717 break; |
718 } 719 720 return (val); 721} 722 723static int 724acpi_hp_sysctl_set(struct acpi_hp_softc *sc, int method, int arg, int oldarg) 725{ 726 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 727 ACPI_SERIAL_ASSERT(hp); 728 |
729 if (method != ACPI_HP_METHOD_CMI_DETAIL && 730 method != ACPI_HP_METHOD_VERBOSE) |
731 arg = arg?1:0; 732 733 if (arg != oldarg) { 734 switch (method) { 735 case ACPI_HP_METHOD_WLAN_ENABLED: 736 return (acpi_hp_exec_wmi_command(sc->wmi_dev, 737 ACPI_HP_WMI_WIRELESS_COMMAND, 1, 738 arg?0x101:0x100)); --- 35 unchanged lines hidden (view full) --- 774 arg?1:0)); 775 case ACPI_HP_METHOD_CMI_DETAIL: 776 sc->cmi_detail = arg; 777 if ((arg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE) != 778 (oldarg & ACPI_HP_CMI_DETAIL_SHOW_MAX_INSTANCE)) { 779 sc->cmi_order_size = -1; 780 } 781 break; |
782 case ACPI_HP_METHOD_VERBOSE: 783 sc->verbose = arg; 784 break; |
785 } 786 } 787 788 return (0); 789} 790 791static __inline void 792acpi_hp_free_buffer(ACPI_BUFFER* buf) { --- 19 unchanged lines hidden (view full) --- 812 } 813 } 814 acpi_hp_free_buffer(&response); 815} 816 817static int 818acpi_hp_exec_wmi_command(device_t wmi_dev, int command, int is_write, int val) 819{ |
820 UINT32 params[5] = { 0x55434553, 821 is_write?2:1, 822 command, 823 is_write?4:0, 824 val}; 825 UINT32* result; 826 ACPI_OBJECT *obj; 827 ACPI_BUFFER in = { sizeof(params), ¶ms }; 828 ACPI_BUFFER out = { ACPI_ALLOCATE_BUFFER, NULL }; |
829 int retval; 830 831 if (ACPI_FAILURE(ACPI_WMI_EVALUATE_CALL(wmi_dev, ACPI_HP_WMI_BIOS_GUID, 832 0, 0x3, &in, &out))) { 833 acpi_hp_free_buffer(&out); 834 return (-EINVAL); 835 } 836 obj = out.Pointer; --- 8 unchanged lines hidden (view full) --- 845 } 846 acpi_hp_free_buffer(&out); 847 848 return (retval); 849} 850 851static __inline char* 852acpi_hp_get_string_from_object(ACPI_OBJECT* obj, char* dst, size_t size) { |
853 int length; 854 |
855 dst[0] = 0; 856 if (obj->Type == ACPI_TYPE_STRING) { 857 length = obj->String.Length+1; 858 if (length > size) { 859 length = size - 1; 860 } 861 strlcpy(dst, obj->String.Pointer, length); 862 acpi_hp_hex_decode(dst); 863 } 864 865 return (dst); 866} 867 868 869/* 870 * Read BIOS Setting block in instance "instance". 871 * The block returned is ACPI_TYPE_PACKAGE which should contain the following 872 * elements: 873 * Index Meaning |
874 * 0 Setting Name [string] 875 * 1 Value (comma separated, asterisk marks the current value) [string] 876 * 2 Path within the bios hierarchy [string] 877 * 3 IsReadOnly [int] 878 * 4 DisplayInUI [int] 879 * 5 RequiresPhysicalPresence [int] 880 * 6 Sequence for ordering within the bios settings (absolute) [int] 881 * 7 Length of prerequisites array [int] 882 * 8..8+[7] PrerequisiteN [string] 883 * 9+[7] Current value (in case of enum) [string] / Array length [int] 884 * 10+[7] Enum length [int] / Array values 885 * 11+[7]ff Enum value at index x [string] |
886 */ 887static int 888acpi_hp_get_cmi_block(device_t wmi_dev, const char* guid, UINT8 instance, 889 char* outbuf, size_t outsize, UINT32* sequence, int detail) 890{ |
891 ACPI_OBJECT *obj; 892 ACPI_BUFFER out = { ACPI_ALLOCATE_BUFFER, NULL }; 893 int i; 894 int outlen; 895 int size = 255; 896 int has_enums = 0; 897 int valuebase = 0; 898 char string_buffer[size]; 899 int enumbase; |
900 901 outlen = 0; 902 outbuf[0] = 0; 903 if (ACPI_FAILURE(ACPI_WMI_GET_BLOCK(wmi_dev, guid, instance, &out))) { 904 acpi_hp_free_buffer(&out); 905 return (-EINVAL); 906 } 907 obj = out.Pointer; 908 if (!obj && obj->Type != ACPI_TYPE_PACKAGE) { 909 acpi_hp_free_buffer(&out); 910 return (-EINVAL); 911 } 912 |
913 if (obj->Package.Count >= 8 && 914 obj->Package.Elements[7].Type == ACPI_TYPE_INTEGER) { 915 valuebase = 8 + obj->Package.Elements[7].Integer.Value; 916 } 917 |
918 /* check if this matches our expectations based on limited knowledge */ |
919 if (valuebase > 7 && obj->Package.Count > valuebase + 1 && |
920 obj->Package.Elements[0].Type == ACPI_TYPE_STRING && 921 obj->Package.Elements[1].Type == ACPI_TYPE_STRING && 922 obj->Package.Elements[2].Type == ACPI_TYPE_STRING && 923 obj->Package.Elements[3].Type == ACPI_TYPE_INTEGER && 924 obj->Package.Elements[4].Type == ACPI_TYPE_INTEGER && 925 obj->Package.Elements[5].Type == ACPI_TYPE_INTEGER && 926 obj->Package.Elements[6].Type == ACPI_TYPE_INTEGER && |
927 obj->Package.Elements[valuebase].Type == ACPI_TYPE_STRING && 928 obj->Package.Elements[valuebase+1].Type == ACPI_TYPE_INTEGER && 929 obj->Package.Count > valuebase + 930 obj->Package.Elements[valuebase+1].Integer.Value 931 ) { 932 enumbase = valuebase + 1; |
933 if (detail & ACPI_HP_CMI_DETAIL_PATHS) { 934 strlcat(outbuf, acpi_hp_get_string_from_object( 935 &obj->Package.Elements[2], string_buffer, size), 936 outsize); 937 outlen += 48; 938 while (strlen(outbuf) < outlen) 939 strlcat(outbuf, " ", outsize); 940 } 941 strlcat(outbuf, acpi_hp_get_string_from_object( 942 &obj->Package.Elements[0], string_buffer, size), 943 outsize); 944 outlen += 43; 945 while (strlen(outbuf) < outlen) 946 strlcat(outbuf, " ", outsize); |
947 strlcat(outbuf, acpi_hp_get_string_from_object( 948 &obj->Package.Elements[valuebase], string_buffer, 949 size), 950 outsize); |
951 outlen += 21; 952 while (strlen(outbuf) < outlen) 953 strlcat(outbuf, " ", outsize); 954 for (i = 0; i < strlen(outbuf); ++i) 955 if (outbuf[i] == '\\') 956 outbuf[i] = '/'; 957 if (detail & ACPI_HP_CMI_DETAIL_ENUMS) { |
958 for (i = enumbase + 1; i < enumbase + 1 + |
959 obj->Package.Elements[enumbase].Integer.Value; 960 ++i) { 961 acpi_hp_get_string_from_object( 962 &obj->Package.Elements[i], string_buffer, 963 size); 964 if (strlen(string_buffer) > 1 || 965 (strlen(string_buffer) == 1 && 966 string_buffer[0] != ' ')) { --- 27 unchanged lines hidden (view full) --- 994 995/* 996 * Convert given two digit hex string (hexin) to an UINT8 referenced 997 * by byteout. 998 * Return != 0 if the was a problem (invalid input) 999 */ 1000static __inline int acpi_hp_hex_to_int(const UINT8 *hexin, UINT8 *byteout) 1001{ |
1002 unsigned int hi; 1003 unsigned int lo; |
1004 1005 hi = hexin[0]; 1006 lo = hexin[1]; 1007 if ('0' <= hi && hi <= '9') 1008 hi -= '0'; 1009 else if ('A' <= hi && hi <= 'F') 1010 hi -= ('A' - 10); 1011 else if ('a' <= hi && hi <= 'f') --- 12 unchanged lines hidden (view full) --- 1024 1025 return (0); 1026} 1027 1028 1029static void 1030acpi_hp_hex_decode(char* buffer) 1031{ |
1032 int i; 1033 int length = strlen(buffer); 1034 UINT8 *uin; 1035 UINT8 uout; |
1036 1037 if (((int)length/2)*2 == length || length < 10) return; 1038 1039 for (i = 0; i<length; ++i) { 1040 if (!((i+1)%3)) { 1041 if (buffer[i] != ' ') 1042 return; 1043 } --- 14 unchanged lines hidden (view full) --- 1058 1059 1060/* 1061 * open hpcmi device 1062 */ 1063static int 1064acpi_hp_hpcmi_open(struct cdev* dev, int flags, int mode, struct thread *td) 1065{ |
1066 struct acpi_hp_softc *sc; 1067 int ret; |
1068 1069 if (dev == NULL || dev->si_drv1 == NULL) 1070 return (EBADF); 1071 sc = dev->si_drv1; 1072 1073 ACPI_SERIAL_BEGIN(hp); 1074 if (sc->hpcmi_open_pid != 0) { 1075 ret = EBUSY; --- 14 unchanged lines hidden (view full) --- 1090} 1091 1092/* 1093 * close hpcmi device 1094 */ 1095static int 1096acpi_hp_hpcmi_close(struct cdev* dev, int flags, int mode, struct thread *td) 1097{ |
1098 struct acpi_hp_softc *sc; 1099 int ret; |
1100 1101 if (dev == NULL || dev->si_drv1 == NULL) 1102 return (EBADF); 1103 sc = dev->si_drv1; 1104 1105 ACPI_SERIAL_BEGIN(hp); 1106 if (sc->hpcmi_open_pid == 0) { 1107 ret = EBADF; --- 12 unchanged lines hidden (view full) --- 1120} 1121 1122/* 1123 * Read from hpcmi bios information 1124 */ 1125static int 1126acpi_hp_hpcmi_read(struct cdev *dev, struct uio *buf, int flag) 1127{ |
1128 struct acpi_hp_softc *sc; 1129 int pos, i, l, ret; 1130 UINT8 instance; 1131 UINT8 maxInstance; 1132 UINT32 sequence; 1133 int linesize = 1025; 1134 char line[linesize]; |
1135 1136 if (dev == NULL || dev->si_drv1 == NULL) 1137 return (EBADF); 1138 sc = dev->si_drv1; 1139 1140 ACPI_SERIAL_BEGIN(hp); 1141 if (sc->hpcmi_open_pid != buf->uio_td->td_proc->p_pid 1142 || sc->hpcmi_bufptr == -1) { --- 73 unchanged lines hidden --- |