Compare commits
1 Commits
common-wmi
...
v0.3
Author | SHA1 | Date | |
---|---|---|---|
fd4dabf37e |
1
Makefile
1
Makefile
@ -13,5 +13,4 @@ clean:
|
||||
|
||||
load:
|
||||
-/sbin/rmmod $(modname)
|
||||
/sbin/modprobe wmi
|
||||
/sbin/insmod $(modname).ko
|
||||
|
@ -1,24 +0,0 @@
|
||||
modname := bbswitch
|
||||
DKMS := dkms
|
||||
modver := $(shell awk -F'"' '/define *BBSWITCH_VERSION/{print $$2}' < bbswitch.c)
|
||||
|
||||
# directory in which generated files are stored
|
||||
DKMS_DEST := /usr/src/$(modname)-$(modver)
|
||||
|
||||
all: install
|
||||
|
||||
src_install:
|
||||
mkdir -p '$(DKMS_DEST)'
|
||||
cp Makefile bbswitch.c '$(DKMS_DEST)'
|
||||
sed 's/#MODULE_VERSION#/$(modver)/' dkms/dkms.conf > '$(DKMS_DEST)/dkms.conf'
|
||||
|
||||
build: src_install
|
||||
$(DKMS) build 'bbswitch/$(modver)'
|
||||
|
||||
install: build
|
||||
$(DKMS) install 'bbswitch/$(modver)'
|
||||
|
||||
uninstall:
|
||||
$(DKMS) remove bbswitch/$(modver) --all
|
||||
|
||||
.PHONY: all src_install build install uninstall
|
10
NEWS
10
NEWS
@ -1,13 +1,3 @@
|
||||
Version 0.4.1 - 16 January 2012
|
||||
|
||||
* Corrected a small error that yielded an confusing error message "The discrete
|
||||
card could not be enabled by a _DSM call"
|
||||
|
||||
Version 0.4 - 15 January 2012
|
||||
|
||||
* Support for models that have a "3D controller" instead of "VGA compatible
|
||||
controller".
|
||||
|
||||
Version 0.3 - 14 January 2012
|
||||
|
||||
* Support for models that have the nvidia DSM method on the integrated Intel
|
||||
|
14
README.md
14
README.md
@ -38,16 +38,10 @@ information.
|
||||
DKMS support
|
||||
------------
|
||||
|
||||
If you have DKMS installed, you can install bbswitch in such a way that it
|
||||
survives kernel upgrades. It is recommended to remove older versions of bbswitch
|
||||
by running `dkms remove bbswitch/OLDVERSION --all` as root. To install the new
|
||||
version, simply run:
|
||||
|
||||
# make -f Makefile.dkms
|
||||
|
||||
To uninstall it, run:
|
||||
|
||||
# make -f Makefile.dkms uninstall
|
||||
Change `#MODULE_VERSION#` to the current version of bbswitch. Copy the
|
||||
Makefile, C source and dkms.conf file to `/usr/src/bbswitch-VERSION/` (replace
|
||||
VERSION with the current version of bbswitch which has been inserted for
|
||||
`#MODULE_VERSION#`.
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
150
bbswitch.c
150
bbswitch.c
@ -16,12 +16,10 @@
|
||||
#include <linux/suspend.h>
|
||||
#include <linux/seq_file.h>
|
||||
|
||||
#define BBSWITCH_VERSION "0.4.1"
|
||||
|
||||
MODULE_LICENSE("GPL");
|
||||
MODULE_DESCRIPTION("Toggle the discrete graphics card");
|
||||
MODULE_AUTHOR("Peter Lekensteyn <lekensteyn@gmail.com>");
|
||||
MODULE_VERSION(BBSWITCH_VERSION);
|
||||
MODULE_VERSION("0.3");
|
||||
|
||||
enum {
|
||||
CARD_UNCHANGED = -1,
|
||||
@ -43,8 +41,6 @@ static const char acpi_optimus_dsm_muid[16] = {
|
||||
0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0,
|
||||
};
|
||||
|
||||
#define MXM_WMMX_GUID "F6CB5C3C-9CAE-4EBD-B577-931EA32A2CC0"
|
||||
#define MXM_WMMX_FUNC_DSM 0x4D53445F /* NVIDIA/Optimus DSM */
|
||||
static const char acpi_nvidia_dsm_muid[16] = {
|
||||
0xA0, 0xA0, 0x95, 0x9D, 0x60, 0x00, 0x48, 0x4D,
|
||||
0xB3, 0x4D, 0x7E, 0x5F, 0xEA, 0x12, 0x9F, 0xD4
|
||||
@ -60,23 +56,10 @@ It looks like something for Intel GPU:
|
||||
http://lxr.linux.no/#linux+v3.1.5/drivers/gpu/drm/i915/intel_acpi.c
|
||||
*/
|
||||
|
||||
struct mxdsm_args {
|
||||
u32 func;
|
||||
char muid[16];
|
||||
u32 revid;
|
||||
u32 sfnc;
|
||||
char args[4];
|
||||
};
|
||||
|
||||
enum dsm_type {
|
||||
DSM_TYPE_UNSUPPORTED,
|
||||
DSM_TYPE_OPTIMUS,
|
||||
DSM_TYPE_NVIDIA,
|
||||
/* _DSM call through a WMI MX method */
|
||||
DSM_TYPE_OPTIMUS_WMI,
|
||||
DSM_TYPE_NVIDIA_WMI,
|
||||
};
|
||||
static enum dsm_type dsm_type = DSM_TYPE_UNSUPPORTED;
|
||||
#define DSM_TYPE_UNSUPPORTED 0
|
||||
#define DSM_TYPE_OPTIMUS 1
|
||||
#define DSM_TYPE_NVIDIA 2
|
||||
static int dsm_type = DSM_TYPE_UNSUPPORTED;
|
||||
|
||||
static struct pci_dev *dis_dev;
|
||||
static acpi_handle dis_handle;
|
||||
@ -95,8 +78,6 @@ static char *buffer_to_string(const char buffer[], char *target) {
|
||||
return target;
|
||||
}
|
||||
|
||||
static int parse_dsm_result(struct acpi_buffer *output, uint32_t *result);
|
||||
|
||||
// Returns 0 if the call succeeded and non-zero otherwise. If the call
|
||||
// succeeded, the result is stored in "result" providing that the result is an
|
||||
// integer or a buffer containing 4 values
|
||||
@ -105,6 +86,7 @@ static int acpi_call_dsm(acpi_handle handle, const char muid[16], int revid,
|
||||
struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
|
||||
struct acpi_object_list input;
|
||||
union acpi_object params[4];
|
||||
union acpi_object *obj;
|
||||
int err;
|
||||
|
||||
input.count = 4;
|
||||
@ -136,12 +118,8 @@ static int acpi_call_dsm(acpi_handle handle, const char muid[16], int revid,
|
||||
buffer_to_string(args, tmp), acpi_format_exception(err));
|
||||
return err;
|
||||
}
|
||||
return parse_dsm_result(&output, result);
|
||||
}
|
||||
|
||||
// Processes the result of an ACPI _DSM call and deallocates the result memory
|
||||
static int parse_dsm_result(struct acpi_buffer *output, uint32_t *result) {
|
||||
union acpi_object *obj = output->pointer;
|
||||
obj = (union acpi_object *)output.pointer;
|
||||
|
||||
if (obj->type == ACPI_TYPE_INTEGER && result) {
|
||||
*result = obj->integer.value;
|
||||
@ -158,7 +136,7 @@ static int parse_dsm_result(struct acpi_buffer *output, uint32_t *result) {
|
||||
" type: %X\n", obj->type);
|
||||
}
|
||||
|
||||
kfree(output->pointer);
|
||||
kfree(output.pointer);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -175,55 +153,11 @@ static int has_dsm_func(const char muid[16], int revid, int sfnc) {
|
||||
return result & 1 && result & (1 << sfnc);
|
||||
}
|
||||
|
||||
// Returns 0 if the call succeeded and non-zero otherwise. If the call
|
||||
// succeeded, the result is stored in "result" providing that the result is an
|
||||
// integer or a buffer containing 4 values
|
||||
static int wmmx_call(const char muid[16], int revid, int func, char args[4],
|
||||
uint32_t *result) {
|
||||
struct mxdsm_args mx_args = {
|
||||
.func = MXM_WMMX_FUNC_DSM,
|
||||
.revid = revid,
|
||||
.sfnc = func,
|
||||
};
|
||||
struct acpi_buffer input = { (acpi_size)sizeof(mx_args), &mx_args };
|
||||
struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
|
||||
acpi_status status;
|
||||
|
||||
memcpy(mx_args.muid, muid, sizeof(mx_args.muid));
|
||||
if (args) {
|
||||
memcpy(mx_args.args, args, sizeof(mx_args.args));
|
||||
} else {
|
||||
// some implementations do not like empty values
|
||||
memset(mx_args.args, 0, sizeof(mx_args.args));
|
||||
}
|
||||
|
||||
// 1, > 1 <-- required by at least Acer Travelmate 8472TG, seen on others
|
||||
status = wmi_evaluate_method(MXM_WMMX_GUID, 1, 1, &input, &output);
|
||||
if (ACPI_FAILURE(status)) {
|
||||
printk(KERN_WARNING "bbswitch: failed to evaluate WMMX: %s\n",
|
||||
acpi_format_exception(status));
|
||||
return status;
|
||||
}
|
||||
return parse_dsm_result(&output, result);
|
||||
}
|
||||
|
||||
// Returns 1 if a _DSM function and its function index exists and 0 otherwise
|
||||
static int has_wmi_func(const char muid[16], int revid, int sfnc) {
|
||||
u32 result = 0;
|
||||
|
||||
// fail if the _DSM call failed
|
||||
if (wmmx_call(muid, revid, 0, 0, &result))
|
||||
return 0;
|
||||
|
||||
// ACPI Spec v4 9.14.1: if bit 0 is zero, no function is supported. If
|
||||
// the n-th bit is enabled, function n is supported
|
||||
return result & 1 && result & (1 << sfnc);
|
||||
}
|
||||
|
||||
static int bbswitch_optimus_dsm(void) {
|
||||
char args[] = {1, 0, 0, 3};
|
||||
u32 result = 0;
|
||||
if (dsm_type == DSM_TYPE_OPTIMUS) {
|
||||
char args[] = {1, 0, 0, 3};
|
||||
u32 result = 0;
|
||||
|
||||
if (acpi_call_dsm(dis_handle, acpi_optimus_dsm_muid, 0x100, 0x1A, args,
|
||||
&result)) {
|
||||
// failure
|
||||
@ -231,21 +165,15 @@ static int bbswitch_optimus_dsm(void) {
|
||||
}
|
||||
printk(KERN_DEBUG "bbswitch: Result of Optimus _DSM call: %08X\n",
|
||||
result);
|
||||
} else if (dsm_type == DSM_TYPE_OPTIMUS_WMI) {
|
||||
if (wmmx_call(acpi_optimus_dsm_muid, 0x100, 0x1A, args, &result)) {
|
||||
// failure
|
||||
return 1;
|
||||
}
|
||||
printk(KERN_DEBUG "bbswitch: Result of WMMX call for Optimus: %08X\n",
|
||||
result);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bbswitch_acpi_off(void) {
|
||||
char args[] = {2, 0, 0, 0};
|
||||
u32 result = 0;
|
||||
if (dsm_type == DSM_TYPE_NVIDIA) {
|
||||
char args[] = {2, 0, 0, 0};
|
||||
u32 result = 0;
|
||||
|
||||
if (acpi_call_dsm(dis_handle, acpi_nvidia_dsm_muid, 0x102, 0x3, args,
|
||||
&result)) {
|
||||
// failure
|
||||
@ -253,35 +181,22 @@ static int bbswitch_acpi_off(void) {
|
||||
}
|
||||
printk(KERN_DEBUG "bbswitch: Result of _DSM call for OFF: %08X\n",
|
||||
result);
|
||||
} else if (dsm_type == DSM_TYPE_NVIDIA_WMI) {
|
||||
if (wmmx_call(acpi_nvidia_dsm_muid, 0x102, 0x3, args, &result)) {
|
||||
// failure
|
||||
return 1;
|
||||
}
|
||||
printk(KERN_DEBUG "bbswitch: Result of WMMX call for OFF: %08X\n",
|
||||
result);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int bbswitch_acpi_on(void) {
|
||||
char args[] = {1, 0, 0, 0};
|
||||
u32 result = 0;
|
||||
if (dsm_type == DSM_TYPE_NVIDIA) {
|
||||
if (acpi_call_dsm(dis_handle, acpi_nvidia_dsm_muid, 0x102, 0x3, args,
|
||||
char args[] = {1, 0, 0, 0};
|
||||
u32 result = 0;
|
||||
|
||||
if (!acpi_call_dsm(dis_handle, acpi_nvidia_dsm_muid, 0x102, 0x3, args,
|
||||
&result)) {
|
||||
// failure
|
||||
return 1;
|
||||
}
|
||||
printk(KERN_DEBUG "bbswitch: Result of _DSM call for ON: %08X\n",
|
||||
result);
|
||||
} else if (dsm_type == DSM_TYPE_NVIDIA_WMI) {
|
||||
if (wmmx_call(acpi_nvidia_dsm_muid, 0x102, 0x3, args, &result)) {
|
||||
// failure
|
||||
return 1;
|
||||
}
|
||||
printk(KERN_DEBUG "bbswitch: Result of WMMX call for ON: %08X\n",
|
||||
result);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@ -414,24 +329,15 @@ static int __init bbswitch_init(void) {
|
||||
struct proc_dir_entry *acpi_entry;
|
||||
struct pci_dev *pdev = NULL;
|
||||
acpi_handle igd_handle = NULL;
|
||||
int class = PCI_CLASS_DISPLAY_VGA << 8;
|
||||
|
||||
printk(KERN_INFO "bbswitch: version %s\n", BBSWITCH_VERSION);
|
||||
|
||||
while ((pdev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
|
||||
while ((pdev = pci_get_class(class, pdev)) != NULL) {
|
||||
struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
|
||||
acpi_handle handle;
|
||||
int pci_class = pdev->class >> 8;
|
||||
|
||||
if (pci_class != PCI_CLASS_DISPLAY_VGA &&
|
||||
pci_class != PCI_CLASS_DISPLAY_3D)
|
||||
continue;
|
||||
|
||||
handle = DEVICE_ACPI_HANDLE(&pdev->dev);
|
||||
if (!handle) {
|
||||
printk(KERN_WARNING "bbswitch: cannot find ACPI handle for VGA"
|
||||
" device %s\n", dev_name(&pdev->dev));
|
||||
if (!handle)
|
||||
continue;
|
||||
}
|
||||
|
||||
acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf);
|
||||
|
||||
@ -453,20 +359,10 @@ static int __init bbswitch_init(void) {
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (has_wmi_func(acpi_optimus_dsm_muid, 0x100, 0x1A)) {
|
||||
dsm_type = DSM_TYPE_OPTIMUS_WMI;
|
||||
printk(KERN_INFO "bbswitch: detected an Optimus WMMX function\n");
|
||||
} else if (has_wmi_func(acpi_nvidia_dsm_muid, 0x102, 0x3)) {
|
||||
dsm_type = DSM_TYPE_NVIDIA_WMI;
|
||||
printk(KERN_INFO "bbswitch: detected a nVidia WMMX function\n");
|
||||
} else if (has_dsm_func(acpi_optimus_dsm_muid, 0x100, 0x1A)) {
|
||||
/* necessary for at least Lenovo Ideapad Z570 which does not have the
|
||||
* WMMX method */
|
||||
if (has_dsm_func(acpi_optimus_dsm_muid, 0x100, 0x1A)) {
|
||||
dsm_type = DSM_TYPE_OPTIMUS;
|
||||
printk(KERN_INFO "bbswitch: detected an Optimus _DSM function\n");
|
||||
} else if (has_dsm_func(acpi_nvidia_dsm_muid, 0x102, 0x3)) {
|
||||
/* necessary for at least Dell XPS L501X bios A08 which does not have
|
||||
* the WMMX method */
|
||||
dsm_type = DSM_TYPE_NVIDIA;
|
||||
printk(KERN_INFO "bbswitch: detected a nVidia _DSM function\n");
|
||||
} else {
|
||||
@ -483,7 +379,7 @@ static int __init bbswitch_init(void) {
|
||||
}
|
||||
}
|
||||
|
||||
acpi_entry = proc_create("bbswitch", 0664, acpi_root_dir, &bbswitch_fops);
|
||||
acpi_entry = proc_create("bbswitch", 0660, acpi_root_dir, &bbswitch_fops);
|
||||
if (acpi_entry == NULL) {
|
||||
printk(KERN_ERR "bbswitch: Couldn't create proc entry\n");
|
||||
return -ENOMEM;
|
||||
|
Reference in New Issue
Block a user