static int bbswitch_pm_handler(struct notifier_block *nbp, unsigned long event_type, void *p) { switch (event_type) { case PM_HIBERNATION_PREPARE: case PM_SUSPEND_PREPARE: dis_dev_get(); dis_before_suspend_disabled = is_card_disabled(); // enable the device before suspend to avoid the PCI config space from // being saved incorrectly if (dis_before_suspend_disabled) bbswitch_on(); dis_dev_put(); break; case PM_POST_HIBERNATION: case PM_POST_SUSPEND: case PM_POST_RESTORE: // after suspend, the card is on, but if it was off before suspend, // disable it again if (dis_before_suspend_disabled) { dis_dev_get(); bbswitch_off(); dis_dev_put(); } break; case PM_RESTORE_PREPARE: // deliberately don't do anything as it does not occur before suspend // nor hibernate, but before restoring a saved image. In that case, // either PM_POST_HIBERNATION or PM_POST_RESTORE will be called break; } return 0; }
static void __exit bbswitch_exit(void) { remove_proc_entry("bbswitch", acpi_root_dir); dis_dev_get(); if (unload_state == CARD_ON) bbswitch_on(); else if (unload_state == CARD_OFF) bbswitch_off(); pr_info("Unloaded. Discrete card %s is %s\n", dev_name(&dis_dev->dev), is_card_disabled() ? "off" : "on"); dis_dev_put(); if (nb.notifier_call) unregister_pm_notifier(&nb); }
static int bbswitch_write(struct file *filp, const char __user *buff, unsigned long len, void *data) { char cmd[8]; if (len >= sizeof(cmd)) { printk(KERN_ERR "bbswitch: Input too large (%lu)\n", len); return -ENOSPC; } if (copy_from_user(cmd, buff, len)) return -EFAULT; if (strncmp(cmd, "OFF", 3) == 0) bbswitch_off(); if (strncmp(cmd, "ON", 2) == 0) bbswitch_on(); return len; }
static ssize_t bbswitch_proc_write(struct file *fp, const char __user *buff, size_t len, loff_t *off) { char cmd[8]; if (len >= sizeof(cmd)) len = sizeof(cmd) - 1; if (copy_from_user(cmd, buff, len)) return -EFAULT; dis_dev_get(); if (strncmp(cmd, "OFF", 3) == 0) bbswitch_off(); if (strncmp(cmd, "ON", 2) == 0) bbswitch_on(); dis_dev_put(); return len; }
static int __init bbswitch_init(void) { struct proc_dir_entry *acpi_entry; struct pci_dev *pdev = NULL; acpi_handle igd_handle = 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 }; 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) { 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_handle = handle; pr_info("Found discrete VGA device %s: %s\n", dev_name(&pdev->dev), (char *)buf.pointer); } kfree(buf.pointer); } if (dis_dev == NULL) { pr_err("No discrete VGA device found\n"); return -ENODEV; } if (!skip_optimus_dsm && has_dsm_func(acpi_optimus_dsm_muid, 0x100, 0x1A)) { dsm_type = DSM_TYPE_OPTIMUS; pr_info("detected an Optimus _DSM function\n"); } else if (has_dsm_func(acpi_nvidia_dsm_muid, 0x102, 0x3)) { dsm_type = DSM_TYPE_NVIDIA; pr_info("detected a nVidia _DSM function\n"); } else { /* At least two Acer machines are known to use the intel ACPI handle * with the legacy nvidia DSM */ dis_handle = igd_handle; if (dis_handle && 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", 0664, acpi_root_dir, &bbswitch_fops); if (acpi_entry == NULL) { pr_err("Couldn't create proc entry\n"); return -ENOMEM; } dis_dev_get(); if (!is_card_disabled()) { /* We think the card is enabled, so ensure the kernel does as well */ if (pci_enable_device(dis_dev)) pr_warn("failed to enable %s\n", dev_name(&dis_dev->dev)); } if (load_state == CARD_ON) bbswitch_on(); else if (load_state == CARD_OFF) bbswitch_off(); pr_info("Succesfully loaded. Discrete card %s is %s\n", dev_name(&dis_dev->dev), is_card_disabled() ? "off" : "on"); dis_dev_put(); register_pm_notifier(&nb); return 0; }