Compare commits
43 Commits
Author | SHA1 | Date | |
---|---|---|---|
b12b017f3a | |||
2adcb24207 | |||
8beb9e3f88 | |||
da55a70331 | |||
7f9fb7adf9 | |||
b8a6029692 | |||
aeff596524 | |||
dbf8aaf8e2 | |||
d54dd454b6 | |||
5f2db6c26a | |||
8ada8849f2 | |||
979fbcf552 | |||
d12ce73a0d | |||
0d1429e8bb | |||
af91008deb | |||
e13e6ffc54 | |||
f034252a13 | |||
647e3dddea | |||
804773eb22 | |||
7c30e38bd1 | |||
e321debb5b | |||
f4c2fadaf5 | |||
fd74ba5bd8 | |||
0ec41fa2da | |||
7fd02221b2 | |||
11ddd3469f | |||
4f73396ba7 | |||
8c0c5aa863 | |||
d5ff9a1fb9 | |||
b0712e5185 | |||
2d1effc903 | |||
c1dd7140df | |||
8bd07d14af | |||
ebf86f0617 | |||
a904b442af | |||
fd4dabf37e | |||
c8b303c36c | |||
9f0ae8f723 | |||
031bcfe8a6 | |||
5e297c27ad | |||
358ce15bc1 | |||
04cdd2d5f9 | |||
6ed65463c8 |
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
*.cmd
|
||||||
|
.tmp_versions
|
||||||
|
Module.symvers
|
||||||
|
*.ko
|
||||||
|
*.mod.c
|
||||||
|
*.o
|
||||||
|
modules.order
|
21
Makefile
21
Makefile
@ -5,12 +5,27 @@ KVERSION := $(shell uname -r)
|
|||||||
KDIR := /lib/modules/$(KVERSION)/build
|
KDIR := /lib/modules/$(KVERSION)/build
|
||||||
PWD := "$$(pwd)"
|
PWD := "$$(pwd)"
|
||||||
|
|
||||||
|
ifdef DEBUG
|
||||||
|
CFLAGS_$(obj-m) := -DDEBUG
|
||||||
|
endif
|
||||||
|
|
||||||
default:
|
default:
|
||||||
$(MAKE) -C $(KDIR) M=$(PWD) modules
|
$(MAKE) -C $(KDIR) M=$(PWD) modules
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(MAKE) -C $(KDIR) M=$(PWD) clean
|
$(MAKE) O=$(PWD) -C $(KDIR) M=$(PWD) clean
|
||||||
|
|
||||||
load:
|
load:
|
||||||
-/sbin/rmmod $(modname)
|
-rmmod $(modname)
|
||||||
/sbin/insmod $(modname).ko
|
insmod $(modname).ko
|
||||||
|
|
||||||
|
install:
|
||||||
|
mkdir -p /lib/modules/$(KVERSION)/misc/$(modname)
|
||||||
|
install -m 0755 -o root -g root $(modname).ko /lib/modules/$(KVERSION)/misc/$(modname)
|
||||||
|
depmod -a
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
rm /lib/modules/$(KVERSION)/misc/$(modname)/$(modname).ko
|
||||||
|
rmdir /lib/modules/$(KVERSION)/misc/$(modname)
|
||||||
|
rmdir /lib/modules/$(KVERSION)/misc
|
||||||
|
depmod -a
|
||||||
|
24
Makefile.dkms
Normal file
24
Makefile.dkms
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
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 -m bbswitch -v $(modver)
|
||||||
|
|
||||||
|
install: build
|
||||||
|
$(DKMS) install -m bbswitch -v $(modver)
|
||||||
|
|
||||||
|
uninstall:
|
||||||
|
$(DKMS) remove -m bbswitch -v $(modver) --all
|
||||||
|
|
||||||
|
.PHONY: all src_install build install uninstall
|
35
NEWS
Normal file
35
NEWS
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
Version 0.5 - 22 October 2012
|
||||||
|
|
||||||
|
* Improved compatibility with older DKMS versions.
|
||||||
|
* Set device state to D3cold instead of D3hot.
|
||||||
|
* Only print DSM method call results when debugging is enabled.
|
||||||
|
* Fixed runtime power management regression in Linux 3.6.
|
||||||
|
|
||||||
|
Version 0.4.2 - 26 April 2012
|
||||||
|
|
||||||
|
* Fixed a documentation error on unload_state.
|
||||||
|
* Added Makefile.dkms and documentation for easier installation using DKMS.
|
||||||
|
* Make /proc/acpi/bbswitch world-writable
|
||||||
|
* Fix NULL pointer dereference when reporting a failure during ACPI method
|
||||||
|
evaluation.
|
||||||
|
|
||||||
|
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
|
||||||
|
video card instead of the nvidia one. This includes at the Acer Travelmate
|
||||||
|
8472TG and Acer Aspire 5745G.
|
||||||
|
|
||||||
|
Version 0.2 - 2 January 2012
|
||||||
|
|
||||||
|
* Initial release, adding a kernel module that can disable discrete nvidia cards
|
||||||
|
on Optimus systems.
|
70
README.md
70
README.md
@ -38,10 +38,16 @@ information.
|
|||||||
DKMS support
|
DKMS support
|
||||||
------------
|
------------
|
||||||
|
|
||||||
Change `#MODULE_VERSION#` to the current version of bbswitch. Copy the
|
If you have DKMS installed, you can install bbswitch in such a way that it
|
||||||
Makefile, C source and dkms.conf file to `/usr/src/bbswitch-VERSION/` (replace
|
survives kernel upgrades. It is recommended to remove older versions of bbswitch
|
||||||
VERSION with the current version of bbswitch which has been inserted for
|
by running `dkms remove -m bbswitch -v OLDVERSION --all` as root. To install
|
||||||
`#MODULE_VERSION#`.
|
the new version, simply run:
|
||||||
|
|
||||||
|
# make -f Makefile.dkms
|
||||||
|
|
||||||
|
To uninstall it, run:
|
||||||
|
|
||||||
|
# make -f Makefile.dkms uninstall
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
-----
|
-----
|
||||||
@ -64,6 +70,43 @@ unload the driver,
|
|||||||
$ dmesg |tail -1
|
$ dmesg |tail -1
|
||||||
bbswitch: device 0000:01:00.0 is in use by driver 'nouveau', refusing OFF
|
bbswitch: device 0000:01:00.0 is in use by driver 'nouveau', refusing OFF
|
||||||
|
|
||||||
|
Do **not** attempt to load a driver while the card is off or the card won't be
|
||||||
|
usable until the PCI configuration space has been recovered (for example, after
|
||||||
|
writing the contents manually or rebooting).
|
||||||
|
|
||||||
|
### Module options
|
||||||
|
|
||||||
|
The module has some options that control the behavior on loading and unloading:
|
||||||
|
`load_state` and `unload_state`. Valid values are `-1`, `0` and `1` meaning "do
|
||||||
|
not change the card state", "turn the card off" and "turn the card on"
|
||||||
|
respectively. For example, if you want to have `bbswitch` disable the card
|
||||||
|
immediately when loading the module while enabling the card on unload, load the
|
||||||
|
module with:
|
||||||
|
|
||||||
|
# modprobe bbswitch load_state=0 unload_state=1
|
||||||
|
|
||||||
|
The `unload_state` value can be changed on runtime, the above command yields the
|
||||||
|
same behavior as:
|
||||||
|
|
||||||
|
# modprobe bbswitch load_state=0
|
||||||
|
# echo 1 | tee /sys/module/bbswitch/parameters/unload_state
|
||||||
|
|
||||||
|
If not explictly set, the default behavior is not to change the power state of
|
||||||
|
the discrete video card which equals to `load_state=-1 unload_state=-1`.
|
||||||
|
|
||||||
|
### Disable card on boot
|
||||||
|
|
||||||
|
These options can be useful to disable the card on boot time. Depending on your
|
||||||
|
distribution, `/etc/modules`, `/etc/modules.conf` or some other file can be used
|
||||||
|
to load modules on boot time. Adding the below line to the file makes the card
|
||||||
|
get disabled on boot:
|
||||||
|
|
||||||
|
bbswitch load_state=0
|
||||||
|
|
||||||
|
You have to update your initial ramdisk (initrd) for the changes propagate to
|
||||||
|
the boot process. On Debian and Ubuntu, this can performed by running
|
||||||
|
`update-initramfs -u` as root.
|
||||||
|
|
||||||
Reporting bugs
|
Reporting bugs
|
||||||
--------------
|
--------------
|
||||||
|
|
||||||
@ -73,6 +116,21 @@ issues on this module in the issue tracker and provide the following details:
|
|||||||
- The output of `dmesg | grep -C 10 bbswitch:`
|
- The output of `dmesg | grep -C 10 bbswitch:`
|
||||||
- The kernel version `uname -a`
|
- The kernel version `uname -a`
|
||||||
- Your distribution and version (if applicable)
|
- Your distribution and version (if applicable)
|
||||||
- The output of `lspci -d10de: -vvv`
|
|
||||||
- The version of your Xorg and the driver
|
- The version of your Xorg and the driver
|
||||||
- The output of `acpidump` (run it as root, e.g. `sudo acpidump > acpidump.txt`)
|
- Submit your machine information on https://bugs.launchpad.net/bugs/752542;
|
||||||
|
the instructions are listed in the bug description. Summary: install the
|
||||||
|
packages containing `dmidecode`, `acpidump` and `iasl` and then run:
|
||||||
|
|
||||||
|
wget http://lekensteyn.nl/files/get-acpi-info.sh
|
||||||
|
sh get-acpi-info.sh
|
||||||
|
- Information about the ACPI handles associated with PCI devices. Since this is
|
||||||
|
a kernel module, you'll need kernel headers, gcc and automake. Commands:
|
||||||
|
|
||||||
|
git clone git://github.com/Lekensteyn/acpi-stuff.git --depth 1
|
||||||
|
cd acpi-stuff/acpi_dump_info
|
||||||
|
make
|
||||||
|
sudo make load
|
||||||
|
cat /proc/acpi/dump_info
|
||||||
|
|
||||||
|
Upload the generated tarball on the above Launchpad URL and provide a link to
|
||||||
|
the comment containing your report.
|
||||||
|
153
bbswitch.c
153
bbswitch.c
@ -9,17 +9,22 @@
|
|||||||
* Get status
|
* Get status
|
||||||
* # cat /proc/acpi/bbswitch
|
* # cat /proc/acpi/bbswitch
|
||||||
*/
|
*/
|
||||||
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
||||||
|
|
||||||
#include <linux/pci.h>
|
#include <linux/pci.h>
|
||||||
#include <linux/acpi.h>
|
#include <linux/acpi.h>
|
||||||
#include <linux/module.h>
|
#include <linux/module.h>
|
||||||
#include <asm/uaccess.h>
|
#include <asm/uaccess.h>
|
||||||
#include <linux/suspend.h>
|
#include <linux/suspend.h>
|
||||||
#include <linux/seq_file.h>
|
#include <linux/seq_file.h>
|
||||||
|
#include <linux/pm_runtime.h>
|
||||||
|
|
||||||
|
#define BBSWITCH_VERSION "0.5"
|
||||||
|
|
||||||
MODULE_LICENSE("GPL");
|
MODULE_LICENSE("GPL");
|
||||||
MODULE_DESCRIPTION("Toggle the discrete graphics card");
|
MODULE_DESCRIPTION("Toggle the discrete graphics card");
|
||||||
MODULE_AUTHOR("Peter Lekensteyn <lekensteyn@gmail.com>");
|
MODULE_AUTHOR("Peter Lekensteyn <lekensteyn@gmail.com>");
|
||||||
MODULE_VERSION("0.2");
|
MODULE_VERSION(BBSWITCH_VERSION);
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
CARD_UNCHANGED = -1,
|
CARD_UNCHANGED = -1,
|
||||||
@ -67,14 +72,13 @@ static acpi_handle dis_handle;
|
|||||||
/* used for keeping the PM event handler */
|
/* used for keeping the PM event handler */
|
||||||
static struct notifier_block nb;
|
static struct notifier_block nb;
|
||||||
/* whether the card was off before suspend or not; on: 0, off: 1 */
|
/* whether the card was off before suspend or not; on: 0, off: 1 */
|
||||||
int dis_before_suspend_disabled;
|
static int dis_before_suspend_disabled;
|
||||||
|
|
||||||
static char *buffer_to_string(const char buffer[], char *target) {
|
static char *buffer_to_string(const char *buffer, size_t n, char *target) {
|
||||||
int i;
|
int i;
|
||||||
for (i=0; i<sizeof(buffer); i++) {
|
for (i=0; i<n; i++) {
|
||||||
sprintf(target + i * 5, "%02X,", buffer[i]);
|
snprintf(target + i * 5, 5 * (n - i), "0x%02X,", buffer ? buffer[i] & 0xFF : 0);
|
||||||
}
|
}
|
||||||
target[sizeof(buffer) * 5] = '\0';
|
|
||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,12 +114,12 @@ static int acpi_call_dsm(acpi_handle handle, const char muid[16], int revid,
|
|||||||
|
|
||||||
err = acpi_evaluate_object(handle, "_DSM", &input, &output);
|
err = acpi_evaluate_object(handle, "_DSM", &input, &output);
|
||||||
if (err) {
|
if (err) {
|
||||||
char tmp[5 * max(sizeof(muid), sizeof(args))];
|
char muid_str[5 * 16];
|
||||||
|
char args_str[5 * 4];
|
||||||
|
|
||||||
printk(KERN_WARNING "bbswitch: failed to evaluate _DSM {%s} %X %X"
|
pr_warn("failed to evaluate _DSM {%s} 0x%X 0x%X {%s}: %s\n",
|
||||||
" {%s}: %s\n",
|
buffer_to_string(muid, 16, muid_str), revid, func,
|
||||||
buffer_to_string(muid, tmp), revid, func,
|
buffer_to_string(args, 4, args_str), acpi_format_exception(err));
|
||||||
buffer_to_string(args, tmp), acpi_format_exception(err));
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,8 +136,8 @@ static int acpi_call_dsm(acpi_handle handle, const char muid[16], int revid,
|
|||||||
*result |= (obj->buffer.pointer[3] << 24);
|
*result |= (obj->buffer.pointer[3] << 24);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printk(KERN_WARNING "bbswitch: _DSM call yields an unsupported result"
|
pr_warn("_DSM call yields an unsupported result type: %#x\n",
|
||||||
" type: %X\n", obj->type);
|
obj->type);
|
||||||
}
|
}
|
||||||
|
|
||||||
kfree(output.pointer);
|
kfree(output.pointer);
|
||||||
@ -163,8 +167,7 @@ static int bbswitch_optimus_dsm(void) {
|
|||||||
// failure
|
// failure
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printk(KERN_DEBUG "bbswitch: Result of Optimus _DSM call: %08X\n",
|
pr_debug("Result of Optimus _DSM call: %08X\n", result);
|
||||||
result);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -179,8 +182,7 @@ static int bbswitch_acpi_off(void) {
|
|||||||
// failure
|
// failure
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printk(KERN_DEBUG "bbswitch: Result of _DSM call for OFF: %08X\n",
|
pr_debug("Result of _DSM call for OFF: %08X\n", result);
|
||||||
result);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -190,13 +192,12 @@ static int bbswitch_acpi_on(void) {
|
|||||||
char args[] = {1, 0, 0, 0};
|
char args[] = {1, 0, 0, 0};
|
||||||
u32 result = 0;
|
u32 result = 0;
|
||||||
|
|
||||||
if (!acpi_call_dsm(dis_handle, acpi_nvidia_dsm_muid, 0x102, 0x3, args,
|
if (acpi_call_dsm(dis_handle, acpi_nvidia_dsm_muid, 0x102, 0x3, args,
|
||||||
&result)) {
|
&result)) {
|
||||||
// failure
|
// failure
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
printk(KERN_DEBUG "bbswitch: Result of _DSM call for ON: %08X\n",
|
pr_debug("Result of _DSM call for ON: %08X\n", result);
|
||||||
result);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -219,47 +220,54 @@ static void bbswitch_off(void) {
|
|||||||
// to prevent the system from possibly locking up, don't disable the device
|
// to prevent the system from possibly locking up, don't disable the device
|
||||||
// if it's still in use by a driver (i.e. nouveau or nvidia)
|
// if it's still in use by a driver (i.e. nouveau or nvidia)
|
||||||
if (dis_dev->driver) {
|
if (dis_dev->driver) {
|
||||||
printk(KERN_WARNING "bbswitch: device %s is in use by driver '%s', "
|
pr_warn("device %s is in use by driver '%s', refusing OFF\n",
|
||||||
"refusing OFF\n", dev_name(&dis_dev->dev), dis_dev->driver->name);
|
dev_name(&dis_dev->dev), dis_dev->driver->name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
printk(KERN_INFO "bbswitch: disabling discrete graphics\n");
|
pr_info("disabling discrete graphics\n");
|
||||||
|
|
||||||
if (bbswitch_optimus_dsm()) {
|
if (bbswitch_optimus_dsm()) {
|
||||||
printk(KERN_WARNING "bbswitch: Optimus ACPI call failed, the device is"
|
pr_warn("Optimus ACPI call failed, the device is not disabled\n");
|
||||||
" not disabled\n");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
pci_save_state(dis_dev);
|
pci_save_state(dis_dev);
|
||||||
pci_clear_master(dis_dev);
|
pci_clear_master(dis_dev);
|
||||||
pci_disable_device(dis_dev);
|
pci_disable_device(dis_dev);
|
||||||
pci_set_power_state(dis_dev, PCI_D3hot);
|
pci_set_power_state(dis_dev, PCI_D3cold);
|
||||||
|
|
||||||
if (bbswitch_acpi_off())
|
if (bbswitch_acpi_off())
|
||||||
printk(KERN_WARNING "bbswitch: The discrete card could not be disabled"
|
pr_warn("The discrete card could not be disabled by a _DSM call\n");
|
||||||
" by a _DSM call\n");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void bbswitch_on(void) {
|
static void bbswitch_on(void) {
|
||||||
if (!is_card_disabled())
|
if (!is_card_disabled())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
printk(KERN_INFO "bbswitch: enabling discrete graphics\n");
|
pr_info("enabling discrete graphics\n");
|
||||||
|
|
||||||
if (bbswitch_acpi_on())
|
if (bbswitch_acpi_on())
|
||||||
printk(KERN_WARNING "bbswitch: The discrete card could not be enabled"
|
pr_warn("The discrete card could not be enabled by a _DSM call\n");
|
||||||
" by a _DSM call\n");
|
|
||||||
|
|
||||||
pci_set_power_state(dis_dev, PCI_D0);
|
pci_set_power_state(dis_dev, PCI_D0);
|
||||||
pci_restore_state(dis_dev);
|
pci_restore_state(dis_dev);
|
||||||
if (pci_enable_device(dis_dev))
|
if (pci_enable_device(dis_dev))
|
||||||
printk(KERN_WARNING "bbswitch: failed to enable %s\n",
|
pr_warn("failed to enable %s\n", dev_name(&dis_dev->dev));
|
||||||
dev_name(&dis_dev->dev));
|
|
||||||
pci_set_master(dis_dev);
|
pci_set_master(dis_dev);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* power bus so we can read PCI configuration space */
|
||||||
|
static void dis_dev_get(void) {
|
||||||
|
if (dis_dev->bus && dis_dev->bus->self)
|
||||||
|
pm_runtime_get_sync(&dis_dev->bus->self->dev);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void dis_dev_put(void) {
|
||||||
|
if (dis_dev->bus && dis_dev->bus->self)
|
||||||
|
pm_runtime_put_sync(&dis_dev->bus->self->dev);
|
||||||
|
}
|
||||||
|
|
||||||
static ssize_t bbswitch_proc_write(struct file *fp, const char __user *buff,
|
static ssize_t bbswitch_proc_write(struct file *fp, const char __user *buff,
|
||||||
size_t len, loff_t *off) {
|
size_t len, loff_t *off) {
|
||||||
char cmd[8];
|
char cmd[8];
|
||||||
@ -270,19 +278,25 @@ static ssize_t bbswitch_proc_write(struct file *fp, const char __user *buff,
|
|||||||
if (copy_from_user(cmd, buff, len))
|
if (copy_from_user(cmd, buff, len))
|
||||||
return -EFAULT;
|
return -EFAULT;
|
||||||
|
|
||||||
|
dis_dev_get();
|
||||||
|
|
||||||
if (strncmp(cmd, "OFF", 3) == 0)
|
if (strncmp(cmd, "OFF", 3) == 0)
|
||||||
bbswitch_off();
|
bbswitch_off();
|
||||||
|
|
||||||
if (strncmp(cmd, "ON", 2) == 0)
|
if (strncmp(cmd, "ON", 2) == 0)
|
||||||
bbswitch_on();
|
bbswitch_on();
|
||||||
|
|
||||||
|
dis_dev_put();
|
||||||
|
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int bbswitch_proc_show(struct seq_file *seqfp, void *p) {
|
static int bbswitch_proc_show(struct seq_file *seqfp, void *p) {
|
||||||
// show the card state. Example output: 0000:01:00:00 ON
|
// show the card state. Example output: 0000:01:00:00 ON
|
||||||
|
dis_dev_get();
|
||||||
seq_printf(seqfp, "%s %s\n", dev_name(&dis_dev->dev),
|
seq_printf(seqfp, "%s %s\n", dev_name(&dis_dev->dev),
|
||||||
is_card_disabled() ? "OFF" : "ON");
|
is_card_disabled() ? "OFF" : "ON");
|
||||||
|
dis_dev_put();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
static int bbswitch_proc_open(struct inode *inode, struct file *file) {
|
static int bbswitch_proc_open(struct inode *inode, struct file *file) {
|
||||||
@ -294,19 +308,24 @@ static int bbswitch_pm_handler(struct notifier_block *nbp,
|
|||||||
switch (event_type) {
|
switch (event_type) {
|
||||||
case PM_HIBERNATION_PREPARE:
|
case PM_HIBERNATION_PREPARE:
|
||||||
case PM_SUSPEND_PREPARE:
|
case PM_SUSPEND_PREPARE:
|
||||||
|
dis_dev_get();
|
||||||
dis_before_suspend_disabled = is_card_disabled();
|
dis_before_suspend_disabled = is_card_disabled();
|
||||||
// enable the device before suspend to avoid the PCI config space from
|
// enable the device before suspend to avoid the PCI config space from
|
||||||
// being saved incorrectly
|
// being saved incorrectly
|
||||||
if (dis_before_suspend_disabled)
|
if (dis_before_suspend_disabled)
|
||||||
bbswitch_on();
|
bbswitch_on();
|
||||||
|
dis_dev_put();
|
||||||
break;
|
break;
|
||||||
case PM_POST_HIBERNATION:
|
case PM_POST_HIBERNATION:
|
||||||
case PM_POST_SUSPEND:
|
case PM_POST_SUSPEND:
|
||||||
case PM_POST_RESTORE:
|
case PM_POST_RESTORE:
|
||||||
// after suspend, the card is on, but if it was off before suspend,
|
// after suspend, the card is on, but if it was off before suspend,
|
||||||
// disable it again
|
// disable it again
|
||||||
if (dis_before_suspend_disabled)
|
if (dis_before_suspend_disabled) {
|
||||||
|
dis_dev_get();
|
||||||
bbswitch_off();
|
bbswitch_off();
|
||||||
|
dis_dev_put();
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case PM_RESTORE_PREPARE:
|
case PM_RESTORE_PREPARE:
|
||||||
// deliberately don't do anything as it does not occur before suspend
|
// deliberately don't do anything as it does not occur before suspend
|
||||||
@ -328,56 +347,84 @@ static struct file_operations bbswitch_fops = {
|
|||||||
static int __init bbswitch_init(void) {
|
static int __init bbswitch_init(void) {
|
||||||
struct proc_dir_entry *acpi_entry;
|
struct proc_dir_entry *acpi_entry;
|
||||||
struct pci_dev *pdev = NULL;
|
struct pci_dev *pdev = NULL;
|
||||||
int class = PCI_CLASS_DISPLAY_VGA << 8;
|
acpi_handle igd_handle = NULL;
|
||||||
|
|
||||||
while ((pdev = pci_get_class(class, pdev)) != NULL) {
|
pr_info("version %s\n", BBSWITCH_VERSION);
|
||||||
|
|
||||||
|
while ((pdev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, pdev)) != NULL) {
|
||||||
struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
|
struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER, NULL };
|
||||||
acpi_handle handle;
|
acpi_handle handle;
|
||||||
|
int pci_class = pdev->class >> 8;
|
||||||
|
|
||||||
handle = DEVICE_ACPI_HANDLE(&pdev->dev);
|
if (pci_class != PCI_CLASS_DISPLAY_VGA &&
|
||||||
if (!handle)
|
pci_class != PCI_CLASS_DISPLAY_3D)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (pdev->vendor != PCI_VENDOR_ID_INTEL) {
|
handle = DEVICE_ACPI_HANDLE(&pdev->dev);
|
||||||
|
if (!handle) {
|
||||||
|
pr_warn("cannot find ACPI handle for VGA device %s\n",
|
||||||
|
dev_name(&pdev->dev));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf);
|
||||||
|
|
||||||
|
if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
|
||||||
|
igd_handle = handle;
|
||||||
|
pr_info("Found integrated VGA device %s: %s\n",
|
||||||
|
dev_name(&pdev->dev), (char *)buf.pointer);
|
||||||
|
} else {
|
||||||
dis_dev = pdev;
|
dis_dev = pdev;
|
||||||
dis_handle = handle;
|
dis_handle = handle;
|
||||||
acpi_get_name(handle, ACPI_FULL_PATHNAME, &buf);
|
pr_info("Found discrete VGA device %s: %s\n",
|
||||||
printk(KERN_INFO "bbswitch: Found discrete VGA device %s: %s\n",
|
|
||||||
dev_name(&pdev->dev), (char *)buf.pointer);
|
dev_name(&pdev->dev), (char *)buf.pointer);
|
||||||
}
|
}
|
||||||
kfree(buf.pointer);
|
kfree(buf.pointer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dis_dev == NULL) {
|
if (dis_dev == NULL) {
|
||||||
printk(KERN_ERR "bbswitch: No discrete VGA device found\n");
|
pr_err("No discrete VGA device found\n");
|
||||||
return -ENODEV;
|
return -ENODEV;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (has_dsm_func(acpi_optimus_dsm_muid, 0x100, 0x1A)) {
|
if (has_dsm_func(acpi_optimus_dsm_muid, 0x100, 0x1A)) {
|
||||||
dsm_type = DSM_TYPE_OPTIMUS;
|
dsm_type = DSM_TYPE_OPTIMUS;
|
||||||
printk(KERN_INFO "bbswitch: detected an Optimus _DSM function\n");
|
pr_info("detected an Optimus _DSM function\n");
|
||||||
} else if (has_dsm_func(acpi_nvidia_dsm_muid, 0x102, 0x3)) {
|
} else if (has_dsm_func(acpi_nvidia_dsm_muid, 0x102, 0x3)) {
|
||||||
dsm_type = DSM_TYPE_NVIDIA;
|
dsm_type = DSM_TYPE_NVIDIA;
|
||||||
printk(KERN_INFO "bbswitch: detected a nVidia _DSM function\n");
|
pr_info("detected a nVidia _DSM function\n");
|
||||||
} else {
|
} else {
|
||||||
printk(KERN_ERR "bbswitch: No suitable _DSM call found.\n");
|
/* At least two Acer machines are known to use the intel ACPI handle
|
||||||
return -ENODEV;
|
* with the legacy nvidia DSM */
|
||||||
|
dis_handle = igd_handle;
|
||||||
|
if (has_dsm_func(acpi_nvidia_dsm_muid, 0x102, 0x3)) {
|
||||||
|
dsm_type = DSM_TYPE_NVIDIA;
|
||||||
|
pr_info("detected a nVidia _DSM function on the"
|
||||||
|
" integrated video card\n");
|
||||||
|
} else {
|
||||||
|
pr_err("No suitable _DSM call found.\n");
|
||||||
|
return -ENODEV;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
acpi_entry = proc_create("bbswitch", 0660, acpi_root_dir, &bbswitch_fops);
|
acpi_entry = proc_create("bbswitch", 0664, acpi_root_dir, &bbswitch_fops);
|
||||||
if (acpi_entry == NULL) {
|
if (acpi_entry == NULL) {
|
||||||
printk(KERN_ERR "bbswitch: Couldn't create proc entry\n");
|
pr_err("Couldn't create proc entry\n");
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dis_dev_get();
|
||||||
|
|
||||||
if (load_state == CARD_ON)
|
if (load_state == CARD_ON)
|
||||||
bbswitch_on();
|
bbswitch_on();
|
||||||
else if (load_state == CARD_OFF)
|
else if (load_state == CARD_OFF)
|
||||||
bbswitch_off();
|
bbswitch_off();
|
||||||
|
|
||||||
printk(KERN_INFO "bbswitch: Succesfully loaded. Discrete card %s is %s\n",
|
pr_info("Succesfully loaded. Discrete card %s is %s\n",
|
||||||
dev_name(&dis_dev->dev), is_card_disabled() ? "off" : "on");
|
dev_name(&dis_dev->dev), is_card_disabled() ? "off" : "on");
|
||||||
|
|
||||||
|
dis_dev_put();
|
||||||
|
|
||||||
nb.notifier_call = &bbswitch_pm_handler;
|
nb.notifier_call = &bbswitch_pm_handler;
|
||||||
register_pm_notifier(&nb);
|
register_pm_notifier(&nb);
|
||||||
|
|
||||||
@ -387,17 +434,23 @@ static int __init bbswitch_init(void) {
|
|||||||
static void __exit bbswitch_exit(void) {
|
static void __exit bbswitch_exit(void) {
|
||||||
remove_proc_entry("bbswitch", acpi_root_dir);
|
remove_proc_entry("bbswitch", acpi_root_dir);
|
||||||
|
|
||||||
|
dis_dev_get();
|
||||||
|
|
||||||
if (unload_state == CARD_ON)
|
if (unload_state == CARD_ON)
|
||||||
bbswitch_on();
|
bbswitch_on();
|
||||||
else if (unload_state == CARD_OFF)
|
else if (unload_state == CARD_OFF)
|
||||||
bbswitch_off();
|
bbswitch_off();
|
||||||
|
|
||||||
printk(KERN_INFO "bbswitch: Unloaded. Discrete card %s is %s\n",
|
pr_info("Unloaded. Discrete card %s is %s\n",
|
||||||
dev_name(&dis_dev->dev), is_card_disabled() ? "off" : "on");
|
dev_name(&dis_dev->dev), is_card_disabled() ? "off" : "on");
|
||||||
|
|
||||||
|
dis_dev_put();
|
||||||
|
|
||||||
if (nb.notifier_call)
|
if (nb.notifier_call)
|
||||||
unregister_pm_notifier(&nb);
|
unregister_pm_notifier(&nb);
|
||||||
}
|
}
|
||||||
|
|
||||||
module_init(bbswitch_init);
|
module_init(bbswitch_init);
|
||||||
module_exit(bbswitch_exit);
|
module_exit(bbswitch_exit);
|
||||||
|
|
||||||
|
/* vim: set sw=4 ts=4 et: */
|
||||||
|
Reference in New Issue
Block a user