/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Gregoire Sutre.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ACPI Display Adapter Driver.
*
* Appendix B of the ACPI specification presents ACPI extensions for display
* adapters. Systems containing a built-in display adapter are required to
* implement these extensions (in their ACPI BIOS). This driver uses these
* extensions to provide generic support for brightness control and display
* switching.
*
* If brightness control methods are absent or non-functional, ACPI brightness
* notifications are relayed to the PMF framework.
*
* This driver sets the BIOS switch policy (_DOS method) as follows:
* - The BIOS should automatically switch the active display output, with no
* interaction required on the OS part.
* - The BIOS should not automatically control the brightness levels.
*
* Brightness and BIOS switch policy can be adjusted from userland, via the
* sysctl variables acpivga<n>.policy and acpiout<n>.brightness under hw.acpi.
*/
/*
* The driver uses mutex(9) protection since changes to the hardware/software
* state may be initiated both by the BIOS (ACPI notifications) and by the user
* (sysctl). The ACPI display adapter's mutex is shared with all ACPI display
* output devices attached to it.
*
* The mutex only prevents undesired interleavings of ACPI notify handlers,
* sysctl callbacks, and pmf(9) suspend/resume routines. Race conditions with
* autoconf(9) detachment routines could, in theory, still occur.
*
* The array of connected output devices (sc_odinfo) is, after attachment, only
* used in ACPI notify handler callbacks. Since two such callbacks cannot be
* running simultaneously, this information does not need protection.
*/
/* Notifications specific to display adapter devices (ACPI 4.0a, Sec. B.5). */
#define ACPI_NOTIFY_CycleOutputDevice 0x80
#define ACPI_NOTIFY_OutputDeviceStatusChange 0x81
#define ACPI_NOTIFY_CycleDisplayOutputHotkeyPressed 0x82
#define ACPI_NOTIFY_NextDisplayOutputHotkeyPressed 0x83
#define ACPI_NOTIFY_PreviousDisplayOutputHotkeyPressed 0x84
/* Notifications specific to display output devices (ACPI 4.0a, Sec. B.7). */
#define ACPI_NOTIFY_CycleBrightness 0x85
#define ACPI_NOTIFY_IncreaseBrightness 0x86
#define ACPI_NOTIFY_DecreaseBrightness 0x87
#define ACPI_NOTIFY_ZeroBrightness 0x88
#define ACPI_NOTIFY_DisplayDeviceOff 0x89
/* Format of the BIOS switch policy set by _DOS (ACPI 4.0a, Sec. B.4.1). */
typedef union acpidisp_bios_policy_t {
uint8_t raw;
struct {
uint8_t output:2;
uint8_t brightness:1;
uint8_t reserved:5;
} __packed fmt;
} acpidisp_bios_policy_t;
/* Format of output device status (ACPI 4.0a, Table B-4). */
typedef union acpidisp_od_status_t {
uint32_t raw;
struct {
uint8_t exists:1;
uint8_t activated:1;
uint8_t ready:1;
uint8_t not_defective:1;
uint8_t attached:1;
uint32_t reserved:27;
} __packed fmt;
} acpidisp_od_status_t;
/* Format of output device state (ACPI 4.0a, Table B-6). */
typedef union acpidisp_od_state_t {
uint32_t raw;
struct {
uint8_t active:1;
uint32_t reserved:29;
uint8_t no_switch:1;
uint8_t commit:1;
} __packed fmt;
} acpidisp_od_state_t;
/*
* acpidisp_outdev:
*
* Description of an ACPI display output device. This structure groups
* together:
* - the output device attributes, given in the display adapter's _DOD
* method (ACPI 4.0a, Sec. B.4.2).
* - the corresponding instance of the acpiout driver (if any).
*/
struct acpidisp_outdev {
acpidisp_od_attrs_t od_attrs; /* Attributes */
device_t od_device; /* Matching base device */
};
/*
* acpidisp_odinfo:
*
* Information on connected output devices (ACPI 4.0a, Sec. B.4.2). This
* structure enumerates all devices (_DOD package) connected to a display
* adapter. Currently, this information is only used for display output
* switching via hotkey.
*
* Invariants (after initialization):
*
* (oi_dev != NULL) && (oi_dev_count > 0)
*/
struct acpidisp_odinfo {
struct acpidisp_outdev *oi_dev; /* Array of output devices */
uint32_t oi_dev_count; /* Number of output devices */
};
/*
* acpidisp_acpivga_attach_args:
*
* Attachment structure for the acpivga interface. Used to attach display
* output devices under a display adapter.
*/
struct acpidisp_acpivga_attach_args {
struct acpi_devnode *aa_node; /* ACPI device node */
kmutex_t *aa_mtx; /* Shared mutex */
};
/*
* acpidisp_brctl:
*
* Brightness control (ACPI 4.0a, Sec. B.6.2 to B.6.4). This structure
* contains the supported brightness levels (_BCL package) and the current
* level. Following Windows 7 brightness control, we ignore the fullpower
* and battery levels (as it simplifies the code).
*
* The array bc_level is sorted in strictly ascending order.
*
* Invariants (after initialization):
*
* (bc_level != NULL) && (bc_level_count > 0)
*/
struct acpidisp_brctl {
uint8_t *bc_level; /* Array of levels */
uint16_t bc_level_count; /* Number of levels */
uint8_t bc_current; /* Current level */
/*
* Quirk if firmware returns wrong values for _BQC
* (acpidisp_get_brightness)
*/
bool bc_bqc_broken;
};
/*
* Minimum brightness increment/decrement in response to increase/decrease
* brightness hotkey notifications. Must be strictly positive.
*/
#define ACPI_DISP_BRCTL_STEP 5
/*
* Check that the PCI device is present, verify
* the class of the PCI device, and finally see
* if the ACPI device is capable of something.
*/
tag = pci_make_tag(aa->aa_pc, ap->ap_bus,
ap->ap_device, ap->ap_function);
id = pci_conf_read(aa->aa_pc, tag, PCI_ID_REG);
if (PCI_VENDOR(id) == PCI_VENDOR_INVALID || PCI_VENDOR(id) == 0)
return 0;
class = pci_conf_read(aa->aa_pc, tag, PCI_CLASS_REG);
if (PCI_CLASS(class) != PCI_CLASS_DISPLAY)
return 0;
/*
* Enumerate connected output devices, attach
* output display devices, and bind the attached
* output devices to the enumerated ones.
*/
asc->sc_odinfo = acpidisp_init_odinfo(asc);
acpidisp_vga_scan_outdevs(asc);
if (asc->sc_odinfo != NULL) {
acpidisp_vga_bind_outdevs(asc);
acpidisp_print_odinfo(self, asc->sc_odinfo);
}
/*
* Set BIOS automatic switch policy.
*
* Many laptops do not support output device switching with
* the methods specified in the ACPI extensions for display
* adapters. Therefore, we leave the BIOS output switch policy
* on "auto" instead of setting it to "normal".
*/
asc->sc_policy.fmt.output = ACPI_DISP_POLICY_OUTPUT_AUTO;
asc->sc_policy.fmt.brightness = ACPI_DISP_POLICY_BRIGHTNESS_NORMAL;
if (acpidisp_set_policy(asc, asc->sc_policy.raw))
asc->sc_policy = acpidisp_default_bios_policy;
/*
* The method _ADR is required for display output
* devices (ACPI 4.0a, Sec. B.6.1).
*/
if (!(acpidisp_has_method(ad->ad_handle, "_ADR", ACPI_TYPE_INTEGER)))
return 0;
/* Power management. */
if (!pmf_device_register(self, acpidisp_out_suspend,
acpidisp_out_resume))
aprint_error_dev(self, "couldn't establish power handler\n");
}
static int
acpidisp_out_detach(device_t self, int flags)
{
struct acpidisp_out_softc *osc = device_private(self);
struct acpidisp_brctl *bc = osc->sc_brctl;
pmf_device_deregister(self);
if (osc->sc_log != NULL)
sysctl_teardown(&osc->sc_log);
/*
* ACPI notify callbacks.
*
* Exclusive access to the sc_odinfo field of struct acpidisp_vga_softc is
* guaranteed since:
*
* (a) this field is only used in ACPI display notify callbacks,
* (b) ACPI display notify callbacks are scheduled with AcpiOsExecute,
* (c) callbacks scheduled with AcpiOsExecute are executed sequentially.
*/
last_osc = NULL;
for (i = 0, od = oi->oi_dev; i < oi->oi_dev_count; i++, od++) {
if (od->od_device == NULL)
continue;
osc = device_private(od->od_device);
if (!(osc->sc_caps & ACPI_DISP_OUT_CAP__DSS))
continue;
if (acpidisp_get_state(osc, &state.raw))
continue;
if (bc == NULL) {
/* Fallback to pmf(9). */
/* XXX Is this the intended meaning of PMFE_DISPLAY_REDUCED? */
pmf_event_inject(NULL, PMFE_DISPLAY_REDUCED);
return;
}
/*
* Initialization of acpidisp_odinfo (_DOD) and acpidisp_brctl (_BCL).
*/
/*
* Regarding _DOD (ACPI 4.0a, Sec. B.4.2):
*
* "The _DOD method returns a list of devices attached to the graphics adapter,
* along with device-specific configuration information."
*
* "Every child device enumerated in the ACPI namespace under the graphics
* adapter must be specified in this list of devices. Each display device
* must have its own ID, which is unique with respect to any other attachable
* devices enumerated."
*
* "Return value: a package containing a variable-length list of integers,
* each of which contains the 32-bit device attribute of a child device."
*/
if (!(asc->sc_caps & ACPI_DISP_VGA_CAP__DOD))
return NULL;
oi = NULL;
pkg = NULL;
rv = acpidisp_eval_package(hdl, "_DOD", &pkg, 1);
if (ACPI_FAILURE(rv))
goto fail;
/*
* Allocate and fill the struct acpidisp_odinfo to be returned.
*/
oi = kmem_zalloc(sizeof(*oi), KM_SLEEP);
oi->oi_dev_count = pkg->Package.Count;
oi->oi_dev = kmem_zalloc(oi->oi_dev_count * sizeof(*oi->oi_dev),
KM_SLEEP);
/*
* Fill the array oi->oi_dev.
*/
for (count = 0, i = 0; i < pkg->Package.Count; i++) {
/* List of 32-bit integers (ACPI 4.0a, Sec. B.4.2). */
if (pkg->Package.Elements[i].Type != ACPI_TYPE_INTEGER ||
pkg->Package.Elements[i].Integer.Value > UINT32_MAX)
continue;
/* Reset all bindings. */
for (i = 0, od = oi->oi_dev; i < oi->oi_dev_count; i++, od++)
od->od_device = NULL;
/*
* Iterate over all ACPI children that have been attached under this
* acpivga device (as acpiout devices).
*/
SIMPLEQ_FOREACH(ad, &asc->sc_node->ad_child_head, ad_child_list) {
if ((ad->ad_device == NULL) ||
(device_parent(ad->ad_device) != asc->sc_dev))
continue;
KASSERT(device_is_a(ad->ad_device, "acpiout"));
osc = device_private(ad->ad_device);
/*
* For display output devices, the method _ADR returns
* the device's ID (ACPI 4.0a, Sec. B.6.1). We do not
* cache the result of _ADR since it may vary.
*/
hdl = osc->sc_node->ad_handle;
rv = acpi_eval_integer(hdl, "_ADR", &val);
if (ACPI_FAILURE(rv)) {
aprint_error_dev(asc->sc_dev,
"failed to evaluate %s.%s: %s\n",
acpi_name(hdl), "_ADR", AcpiFormatException(rv));
continue;
}
/* The device ID is a 16-bit integer (ACPI 4.0a, Table B-2). */
devid = (uint16_t)val;
/*
* The device ID must be unique (among output devices), and must
* appear in the list returned by _DOD (ACPI 4.0a, Sec. B.6.1).
*/
for (i = 0, od = oi->oi_dev; i < oi->oi_dev_count; i++, od++) {
if (devid == od->od_attrs.device_id) {
if (od->od_device != NULL)
aprint_error_dev(asc->sc_dev,
"%s has same device ID as %s\n",
device_xname(osc->sc_dev),
device_xname(od->od_device));
else
od->od_device = osc->sc_dev;
break;
}
}
if (i == oi->oi_dev_count)
aprint_debug_dev(asc->sc_dev,
"output device %s not connected\n",
device_xname(osc->sc_dev));
}
}
/*
* Regarding _BCL (ACPI 4.0a, Sec. B.6.2):
*
* "This method allows the OS to query a list of brightness levels supported by
* built-in display output devices."
*
* "Return value: a variable-length package containing a list of integers
* representing the supported brightness levels. Each integer has 8 bits of
* significant data."
*/
/*
* Fill the array bc->bc_level with an insertion sort.
*/
for (count = 0, i = 0; i < pkg->Package.Count; i++) {
/* List of 8-bit integers (ACPI 4.0a, Sec. B.6.2). */
if (pkg->Package.Elements[i].Type != ACPI_TYPE_INTEGER ||
pkg->Package.Elements[i].Integer.Value > UINT8_MAX)
continue;
/* Find the correct slot but do not modify the array yet. */
for (j = count; --j >= 0 && bc->bc_level[j] > level; );
if (j >= 0 && bc->bc_level[j] == level)
continue;
j++;
/* Make room for the new level. */
for (k = count; k > j; k--)
bc->bc_level[k] = bc->bc_level[k-1];
/* Insert the new level. */
bc->bc_level[j] = level;
count++;
}
if (!(osc->sc_caps & ACPI_DISP_OUT_CAP__BQC))
return ENODEV;
if (osc->sc_brctl->bc_bqc_broken) {
*valuep = osc->sc_brctl->bc_current;
return 0;
}
rv = acpi_eval_integer(hdl, "_BQC", &val);
if (ACPI_FAILURE(rv)) {
aprint_error_dev(osc->sc_dev, "failed to evaluate %s.%s: %s\n",
acpi_name(hdl), "_BQC", AcpiFormatException(rv));
return EIO;
}
if (val > UINT8_MAX)
return ERANGE;
*valuep = (uint8_t)val;
ACPI_DEBUG_PRINT((ACPI_DB_INFO, "%s: get %s: %"PRIu8"\n",
device_xname(osc->sc_dev), "brightness", *valuep));
return 0;
}
/*
* Quirk for when getting the brightness value always returns the same
* result, which breaks brightness controls which try to lower the
* brightness by a specific value and then check if it worked.
*/
static int
acpidisp_quirk_get_brightness(const struct acpidisp_out_softc *osc)
{
struct acpidisp_brctl *bc;
uint8_t original_brightness, test_brightness;
int error;
/* Find a different brightness value */
test_brightness = bc->bc_level[bc->bc_level_count - 1];
if (test_brightness == original_brightness)
test_brightness = bc->bc_level[0];
if (test_brightness == original_brightness) {
aprint_error_dev(osc->sc_dev,
"couldn't find different brightness levels"
" for _BQC quirk test\n");
return 0;
}
error = acpidisp_get_brightness(osc, &bc->bc_current);
if (error)
return error;
/* We set a different value, but got the original value back */
if (bc->bc_current == original_brightness) {
aprint_normal_dev(osc->sc_dev, "_BQC broken, enabling quirk\n");
bc->bc_bqc_broken = true;
}
/* Restore original value */
bc->bc_current = original_brightness;
return acpidisp_set_brightness(osc, bc->bc_current);
}
for (size_t i = 0; i < l; i++) {
for (b = e = a[i]; i + 1 < l && a[i + 1] == e + 1; i++, e++)
continue;
(*pr)("%"PRIu8, b);
if (b != e)
(*pr)("-%"PRIu8, e);
if (i < l - 1)
(*pr)(",");
}
if (oda.fmt.device_id_scheme == 1) {
/* Uses the device ID scheme introduced in ACPI 3.0. */
switch (oda.fmt.type) {
case ACPI_DISP_OUT_ATTR_TYPE_OTHER:
type = "Other";
break;
case ACPI_DISP_OUT_ATTR_TYPE_VGA:
type = "VGA Analog Monitor";
break;
case ACPI_DISP_OUT_ATTR_TYPE_TV:
type = "TV/HDTV Monitor";
break;
case ACPI_DISP_OUT_ATTR_TYPE_EXTDIG:
type = "Ext. Digital Monitor";
break;
case ACPI_DISP_OUT_ATTR_TYPE_INTDFP:
type = "Int. Digital Flat Panel";
break;
default:
type = "Invalid";
break;
}
aprint_verbose("%s, index %d, port %d",
type, oda.fmt.index, oda.fmt.port);
} else {
/* Uses vendor-specific device IDs. */
switch (oda.device_id) {
case ACPI_DISP_OUT_LEGACY_DEVID_MONITOR:
type = "Ext. Monitor";
break;
case ACPI_DISP_OUT_LEGACY_DEVID_PANEL:
type = "LCD Panel";
break;
case ACPI_DISP_OUT_LEGACY_DEVID_TV:
type = "TV";
break;
default:
type = "Unknown Output Device";
break;
}
aprint_verbose("%s", type);
}
aprint_verbose(", head %d", oda.fmt.head_id);
if (oda.fmt.bios_detect)
aprint_verbose(", bios detect");
if (oda.fmt.non_vga)
aprint_verbose(", non vga");
}
/*
* General-purpose utility functions.
*/
/*
* acpidisp_has_method:
*
* Returns true if and only if (a) the object handle.path exists and
* (b) this object is a method or has the given type.
*/
static bool
acpidisp_has_method(ACPI_HANDLE handle, const char *path, ACPI_OBJECT_TYPE type)
{
ACPI_HANDLE hdl;
ACPI_OBJECT_TYPE typ;
KASSERT(handle != NULL);
if (ACPI_FAILURE(AcpiGetHandle(handle, path, &hdl)))
return false;
if (ACPI_FAILURE(AcpiGetType(hdl, &typ)))
return false;
if (typ != ACPI_TYPE_METHOD && typ != type)
return false;
return true;
}
/*
* acpidisp_eval_package:
*
* Evaluate a package (with an expected minimum number of elements).
* Caller must free *pkg by ACPI_FREE().
*/
static ACPI_STATUS
acpidisp_eval_package(ACPI_HANDLE handle, const char *path, ACPI_OBJECT **pkg,
unsigned int mincount)
{
ACPI_BUFFER buf;
ACPI_OBJECT *obj;
ACPI_STATUS rv;
rv = acpi_eval_struct(handle, path, &buf);
if (ACPI_FAILURE(rv))
return rv;
if (buf.Length == 0 || buf.Pointer == NULL)
return AE_NULL_OBJECT;
obj = buf.Pointer;
if (obj->Type != ACPI_TYPE_PACKAGE) {
ACPI_FREE(obj);
return AE_TYPE;
}
if (obj->Package.Count < mincount) {
ACPI_FREE(obj);
return AE_BAD_DATA;
}
*pkg = obj;
return rv;
}
/*
* acpidisp_array_search:
*
* Look for a value v in a sorted array a of n integers (n > 0). Fill *l
* and *u as follows:
*
* *l = Max {a[i] | a[i] <= v or i = 0}
* *u = Min {a[i] | a[i] >= v or i = n-1}
*/
static void
acpidisp_array_search(const uint8_t *a, uint16_t n, int v, uint8_t *l, uint8_t *u)
{
uint16_t i, j, m;
if (v <= a[0]) {
*l = a[0];
*u = a[0];
return;
}
if (v >= a[n-1]) {
*l = a[n-1];
*u = a[n-1];
return;
}
for (i = 0, j = n - 1; j - i > 1; ) {
m = (i + j) / 2;
if (a[m] == v) {
*l = v;
*u = v;
return;
}
if (a[m] < v)
i = m;
else
j = m;
}
/* Here a[i] < v < a[j] and j = i + 1. */
*l = a[i];
*u = a[j];
return;
}
MODULE(MODULE_CLASS_DRIVER, acpivga, NULL);
#ifdef _MODULE
#include "ioconf.c"
#endif
static int
acpivga_modcmd(modcmd_t cmd, void *aux)
{
int rv = 0;