From 0988eed04e7237fdab524413e9c69bbf54cdf400 Mon Sep 17 00:00:00 2001 From: Matthew Garrett Date: Dec 16 2010 20:37:54 +0000 Subject: - applesmc_update.patch: Make the driver more generic. Should help Apples. - apple_backlight.patch: Make sure that this loads on all hardware. - efifb_update.patch: Fixes for the 11 inch Macbook Air - acpi_reboot.patch: Should make reboot work better on most hardware - efi_default_physical.patch: Some machines dislike EFI virtual mode --- diff --git a/acpi_reboot.patch b/acpi_reboot.patch new file mode 100644 index 0000000..e9fdef3 --- /dev/null +++ b/acpi_reboot.patch @@ -0,0 +1,102 @@ +Improve our reboot handling for compatibility with Windows. Upstream in .38? + +diff --git a/arch/x86/kernel/reboot.c b/arch/x86/kernel/reboot.c +index c495aa8..c770e66 100644 +--- a/arch/x86/kernel/reboot.c ++++ b/arch/x86/kernel/reboot.c +@@ -34,7 +34,7 @@ EXPORT_SYMBOL(pm_power_off); + + static const struct desc_ptr no_idt = {}; + static int reboot_mode; +-enum reboot_type reboot_type = BOOT_KBD; ++enum reboot_type reboot_type = BOOT_ACPI; + int reboot_force; + + #if defined(CONFIG_X86_32) && defined(CONFIG_SMP) +@@ -538,9 +538,23 @@ void __attribute__((weak)) mach_reboot_fixups(void) + { + } + ++/* ++ * Windows does the following on reboot: ++ * 1) If the FADT has the ACPI reboot register flag set, try it ++ * 2) If still alive, write to the keyboard controller ++ * 3) If still alive, write to the ACPI reboot register again ++ * 4) Ig still alive, write to the keyboard controller again ++ * ++ * If the machine is still alive at this stage, it gives up. We default to ++ * following the same pattern, except that if we're still alive after (4) we'll ++ * try to force a triple fault and then cycle between hitting the keyboard ++ * controller and doing that ++ */ + static void native_machine_emergency_restart(void) + { + int i; ++ int attempt = 0; ++ int orig_reboot_type = reboot_type; + + if (reboot_emergency) + emergency_vmx_disable_all(); +@@ -562,6 +576,13 @@ static void native_machine_emergency_restart(void) + outb(0xfe, 0x64); /* pulse reset low */ + udelay(50); + } ++ if (attempt == 0 && orig_reboot_type == BOOT_ACPI) { ++ attempt = 1; ++ reboot_type = BOOT_ACPI; ++ } else { ++ reboot_type = BOOT_TRIPLE; ++ } ++ break; + + case BOOT_TRIPLE: + load_idt(&no_idt); +diff --git a/drivers/acpi/acpica/hwxface.c b/drivers/acpi/acpica/hwxface.c +index 50cc3be..c6a4e63 100644 +--- a/drivers/acpi/acpica/hwxface.c ++++ b/drivers/acpi/acpica/hwxface.c +@@ -82,12 +82,11 @@ acpi_status acpi_reset(void) + /* + * For I/O space, write directly to the OSL. This bypasses the port + * validation mechanism, which may block a valid write to the reset +- * register. ++ * register. Spec section 4.7.3.6 requires register width to be 8. + */ + status = + acpi_os_write_port((acpi_io_address) reset_reg->address, +- acpi_gbl_FADT.reset_value, +- reset_reg->bit_width); ++ acpi_gbl_FADT.reset_value, 8) + } else { + /* Write the reset value to the reset register */ + +diff --git a/drivers/acpi/reboot.c b/drivers/acpi/reboot.c +index 93f9114..a6c77e8b 100644 +--- a/drivers/acpi/reboot.c ++++ b/drivers/acpi/reboot.c +@@ -15,9 +15,15 @@ void acpi_reboot(void) + + rr = &acpi_gbl_FADT.reset_register; + +- /* Is the reset register supported? */ +- if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) || +- rr->bit_width != 8 || rr->bit_offset != 0) ++ /* ACPI reset register was only introduced with v2 of the FADT */ ++ ++ if (acpi_gbl_FADT.header.revision < 2) ++ return; ++ ++ /* Is the reset register supported? The spec says we should be ++ * checking the bit width and bit offset, but Windows ignores ++ * these fields */ ++ if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER)) + return; + + reset_value = acpi_gbl_FADT.reset_value; +@@ -45,6 +51,4 @@ void acpi_reboot(void) + acpi_reset(); + break; + } +- /* Wait ten seconds */ +- acpi_os_stall(10000000); + } diff --git a/apple_backlight.patch b/apple_backlight.patch new file mode 100644 index 0000000..c7d5a81 --- /dev/null +++ b/apple_backlight.patch @@ -0,0 +1,688 @@ +Various fixes to the Apple backlight driver. Upstream in .38? + +diff --git a/drivers/video/backlight/Kconfig b/drivers/video/backlight/Kconfig +index e54a337..fb5df46 100644 +--- a/drivers/video/backlight/Kconfig ++++ b/drivers/video/backlight/Kconfig +@@ -236,12 +236,12 @@ config BACKLIGHT_MAX8925 + If you have a LCD backlight connected to the WLED output of MAX8925 + WLED output, say Y here to enable this driver. + +-config BACKLIGHT_MBP_NVIDIA +- tristate "MacBook Pro Nvidia Backlight Driver" ++config BACKLIGHT_APPLE ++ tristate "Apple Backlight Driver" + depends on X86 + help +- If you have an Apple Macbook Pro with Nvidia graphics hardware say Y +- to enable a driver for its backlight ++ If you have an Intel-based Apple say Y to enable a driver for its ++ backlight + + config BACKLIGHT_TOSA + tristate "Sharp SL-6000 Backlight Driver" +diff --git a/drivers/video/backlight/Makefile b/drivers/video/backlight/Makefile +index 44c0f81..ebaecc0 100644 +--- a/drivers/video/backlight/Makefile ++++ b/drivers/video/backlight/Makefile +@@ -26,7 +26,7 @@ obj-$(CONFIG_BACKLIGHT_CARILLO_RANCH) += cr_bllcd.o + obj-$(CONFIG_BACKLIGHT_PWM) += pwm_bl.o + obj-$(CONFIG_BACKLIGHT_DA903X) += da903x_bl.o + obj-$(CONFIG_BACKLIGHT_MAX8925) += max8925_bl.o +-obj-$(CONFIG_BACKLIGHT_MBP_NVIDIA) += mbp_nvidia_bl.o ++obj-$(CONFIG_BACKLIGHT_APPLE) += apple_bl.o + obj-$(CONFIG_BACKLIGHT_TOSA) += tosa_bl.o + obj-$(CONFIG_BACKLIGHT_SAHARA) += kb3886_bl.o + obj-$(CONFIG_BACKLIGHT_WM831X) += wm831x_bl.o +diff --git a/drivers/video/backlight/apple_bl.c b/drivers/video/backlight/apple_bl.c +new file mode 100644 +index 0000000..8f808c7 +--- /dev/null ++++ b/drivers/video/backlight/apple_bl.c +@@ -0,0 +1,240 @@ ++/* ++ * Backlight Driver for Intel-based Apples ++ * ++ * Copyright (c) Red Hat ++ * Based on code from Pommed: ++ * Copyright (C) 2006 Nicolas Boichat ++ * Copyright (C) 2006 Felipe Alfaro Solana ++ * Copyright (C) 2007 Julien BLACHE ++ * ++ * This program is free software; you can redistribute it and/or modify ++ * it under the terms of the GNU General Public License version 2 as ++ * published by the Free Software Foundation. ++ * ++ * This driver triggers SMIs which cause the firmware to change the ++ * backlight brightness. This is icky in many ways, but it's impractical to ++ * get at the firmware code in order to figure out what it's actually doing. ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static struct backlight_device *apple_backlight_device; ++ ++struct hw_data { ++ /* I/O resource to allocate. */ ++ unsigned long iostart; ++ unsigned long iolen; ++ /* Backlight operations structure. */ ++ const struct backlight_ops backlight_ops; ++ void (*set_brightness)(int); ++}; ++ ++static const struct hw_data *hw_data; ++ ++#define DRIVER "apple_backlight: " ++ ++/* Module parameters. */ ++static int debug; ++module_param_named(debug, debug, int, 0644); ++MODULE_PARM_DESC(debug, "Set to one to enable debugging messages."); ++ ++/* ++ * Implementation for machines with Intel chipset. ++ */ ++static void intel_chipset_set_brightness(int intensity) ++{ ++ outb(0x04 | (intensity << 4), 0xb3); ++ outb(0xbf, 0xb2); ++} ++ ++static int intel_chipset_send_intensity(struct backlight_device *bd) ++{ ++ int intensity = bd->props.brightness; ++ ++ if (debug) ++ printk(KERN_DEBUG DRIVER "setting brightness to %d\n", ++ intensity); ++ ++ intel_chipset_set_brightness(intensity); ++ return 0; ++} ++ ++static int intel_chipset_get_intensity(struct backlight_device *bd) ++{ ++ int intensity; ++ ++ outb(0x03, 0xb3); ++ outb(0xbf, 0xb2); ++ intensity = inb(0xb3) >> 4; ++ ++ if (debug) ++ printk(KERN_DEBUG DRIVER "read brightness of %d\n", ++ intensity); ++ ++ return intensity; ++} ++ ++static const struct hw_data intel_chipset_data = { ++ .iostart = 0xb2, ++ .iolen = 2, ++ .backlight_ops = { ++ .options = BL_CORE_SUSPENDRESUME, ++ .get_brightness = intel_chipset_get_intensity, ++ .update_status = intel_chipset_send_intensity, ++ }, ++ .set_brightness = intel_chipset_set_brightness, ++}; ++ ++/* ++ * Implementation for machines with Nvidia chipset. ++ */ ++static void nvidia_chipset_set_brightness(int intensity) ++{ ++ outb(0x04 | (intensity << 4), 0x52f); ++ outb(0xbf, 0x52e); ++} ++ ++static int nvidia_chipset_send_intensity(struct backlight_device *bd) ++{ ++ int intensity = bd->props.brightness; ++ ++ if (debug) ++ printk(KERN_DEBUG DRIVER "setting brightness to %d\n", ++ intensity); ++ ++ nvidia_chipset_set_brightness(intensity); ++ return 0; ++} ++ ++static int nvidia_chipset_get_intensity(struct backlight_device *bd) ++{ ++ int intensity; ++ ++ outb(0x03, 0x52f); ++ outb(0xbf, 0x52e); ++ intensity = inb(0x52f) >> 4; ++ ++ if (debug) ++ printk(KERN_DEBUG DRIVER "read brightness of %d\n", ++ intensity); ++ ++ return intensity; ++} ++ ++static const struct hw_data nvidia_chipset_data = { ++ .iostart = 0x52e, ++ .iolen = 2, ++ .backlight_ops = { ++ .options = BL_CORE_SUSPENDRESUME, ++ .get_brightness = nvidia_chipset_get_intensity, ++ .update_status = nvidia_chipset_send_intensity ++ }, ++ .set_brightness = nvidia_chipset_set_brightness, ++}; ++ ++static int __devinit apple_bl_add(struct acpi_device *dev) ++{ ++ struct backlight_properties props; ++ struct pci_dev *host; ++ int intensity; ++ ++ host = pci_get_bus_and_slot(0, 0); ++ ++ if (!host) { ++ printk(KERN_ERR DRIVER "unable to find PCI host\n"); ++ return -ENODEV; ++ } ++ ++ if (host->vendor == PCI_VENDOR_ID_INTEL) ++ hw_data = &intel_chipset_data; ++ else if (host->vendor == PCI_VENDOR_ID_NVIDIA) ++ hw_data = &nvidia_chipset_data; ++ ++ pci_dev_put(host); ++ ++ if (!hw_data) { ++ printk(KERN_ERR DRIVER "unknown hardware\n"); ++ return -ENODEV; ++ } ++ ++ /* Check that the hardware responds - this may not work under EFI */ ++ ++ intensity = hw_data->backlight_ops.get_brightness(NULL); ++ ++ if (!intensity) { ++ hw_data->set_brightness(1); ++ if (!hw_data->backlight_ops.get_brightness(NULL)) ++ return -ENODEV; ++ ++ hw_data->set_brightness(0); ++ } ++ ++ if (!request_region(hw_data->iostart, hw_data->iolen, ++ "Apple backlight")) ++ return -ENXIO; ++ ++ memset(&props, 0, sizeof(struct backlight_properties)); ++ props.max_brightness = 15; ++ apple_backlight_device = backlight_device_register("apple_backlight", ++ NULL, NULL, &hw_data->backlight_ops, &props); ++ ++ if (IS_ERR(apple_backlight_device)) { ++ release_region(hw_data->iostart, hw_data->iolen); ++ return PTR_ERR(apple_backlight_device); ++ } ++ ++ apple_backlight_device->props.brightness = ++ hw_data->backlight_ops.get_brightness(apple_backlight_device); ++ backlight_update_status(apple_backlight_device); ++ ++ return 0; ++} ++ ++static int __devexit apple_bl_remove(struct acpi_device *dev, int type) ++{ ++ backlight_device_unregister(apple_backlight_device); ++ ++ release_region(hw_data->iostart, hw_data->iolen); ++ hw_data = NULL; ++ return 0; ++} ++ ++static const struct acpi_device_id apple_bl_ids[] = { ++ {"APP0002", 0}, ++ {"", 0}, ++}; ++ ++static struct acpi_driver apple_bl_driver = { ++ .name = "Apple backlight", ++ .ids = apple_bl_ids, ++ .ops = { ++ .add = apple_bl_add, ++ .remove = apple_bl_remove, ++ }, ++}; ++ ++static int __init apple_bl_init(void) ++{ ++ return acpi_bus_register_driver(&apple_bl_driver); ++} ++ ++static void __exit apple_bl_exit(void) ++{ ++ acpi_bus_unregister_driver(&apple_bl_driver); ++} ++ ++module_init(apple_bl_init); ++module_exit(apple_bl_exit); ++ ++MODULE_AUTHOR("Matthew Garrett "); ++MODULE_DESCRIPTION("Apple Backlight Driver"); ++MODULE_LICENSE("GPL"); ++MODULE_DEVICE_TABLE(acpi, apple_bl_ids); ++MODULE_ALIAS("mbp_nvidia_bl"); +diff --git a/drivers/video/backlight/mbp_nvidia_bl.c b/drivers/video/backlight/mbp_nvidia_bl.c +deleted file mode 100644 +index 1485f73..0000000 +--- a/drivers/video/backlight/mbp_nvidia_bl.c ++++ /dev/null +@@ -1,400 +0,0 @@ +-/* +- * Backlight Driver for Nvidia 8600 in Macbook Pro +- * +- * Copyright (c) Red Hat +- * Based on code from Pommed: +- * Copyright (C) 2006 Nicolas Boichat +- * Copyright (C) 2006 Felipe Alfaro Solana +- * Copyright (C) 2007 Julien BLACHE +- * +- * This program is free software; you can redistribute it and/or modify +- * it under the terms of the GNU General Public License version 2 as +- * published by the Free Software Foundation. +- * +- * This driver triggers SMIs which cause the firmware to change the +- * backlight brightness. This is icky in many ways, but it's impractical to +- * get at the firmware code in order to figure out what it's actually doing. +- */ +- +-#include +-#include +-#include +-#include +-#include +-#include +-#include +-#include +- +-static struct backlight_device *mbp_backlight_device; +- +-/* Structure to be passed to the DMI_MATCH function. */ +-struct dmi_match_data { +- /* I/O resource to allocate. */ +- unsigned long iostart; +- unsigned long iolen; +- /* Backlight operations structure. */ +- const struct backlight_ops backlight_ops; +-}; +- +-/* Module parameters. */ +-static int debug; +-module_param_named(debug, debug, int, 0644); +-MODULE_PARM_DESC(debug, "Set to one to enable debugging messages."); +- +-/* +- * Implementation for MacBooks with Intel chipset. +- */ +-static int intel_chipset_send_intensity(struct backlight_device *bd) +-{ +- int intensity = bd->props.brightness; +- +- if (debug) +- printk(KERN_DEBUG "mbp_nvidia_bl: setting brightness to %d\n", +- intensity); +- +- outb(0x04 | (intensity << 4), 0xb3); +- outb(0xbf, 0xb2); +- return 0; +-} +- +-static int intel_chipset_get_intensity(struct backlight_device *bd) +-{ +- int intensity; +- +- outb(0x03, 0xb3); +- outb(0xbf, 0xb2); +- intensity = inb(0xb3) >> 4; +- +- if (debug) +- printk(KERN_DEBUG "mbp_nvidia_bl: read brightness of %d\n", +- intensity); +- +- return intensity; +-} +- +-static const struct dmi_match_data intel_chipset_data = { +- .iostart = 0xb2, +- .iolen = 2, +- .backlight_ops = { +- .options = BL_CORE_SUSPENDRESUME, +- .get_brightness = intel_chipset_get_intensity, +- .update_status = intel_chipset_send_intensity, +- } +-}; +- +-/* +- * Implementation for MacBooks with Nvidia chipset. +- */ +-static int nvidia_chipset_send_intensity(struct backlight_device *bd) +-{ +- int intensity = bd->props.brightness; +- +- if (debug) +- printk(KERN_DEBUG "mbp_nvidia_bl: setting brightness to %d\n", +- intensity); +- +- outb(0x04 | (intensity << 4), 0x52f); +- outb(0xbf, 0x52e); +- return 0; +-} +- +-static int nvidia_chipset_get_intensity(struct backlight_device *bd) +-{ +- int intensity; +- +- outb(0x03, 0x52f); +- outb(0xbf, 0x52e); +- intensity = inb(0x52f) >> 4; +- +- if (debug) +- printk(KERN_DEBUG "mbp_nvidia_bl: read brightness of %d\n", +- intensity); +- +- return intensity; +-} +- +-static const struct dmi_match_data nvidia_chipset_data = { +- .iostart = 0x52e, +- .iolen = 2, +- .backlight_ops = { +- .options = BL_CORE_SUSPENDRESUME, +- .get_brightness = nvidia_chipset_get_intensity, +- .update_status = nvidia_chipset_send_intensity +- } +-}; +- +-/* +- * DMI matching. +- */ +-static /* const */ struct dmi_match_data *driver_data; +- +-static int mbp_dmi_match(const struct dmi_system_id *id) +-{ +- driver_data = id->driver_data; +- +- printk(KERN_INFO "mbp_nvidia_bl: %s detected\n", id->ident); +- return 1; +-} +- +-static const struct dmi_system_id __initdata mbp_device_table[] = { +- { +- .callback = mbp_dmi_match, +- .ident = "MacBook 1,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Computer, Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook1,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBook 2,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook2,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBook 3,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook3,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBook 4,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBook 4,2", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4,2"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 1,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro1,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 1,2", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro1,2"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 2,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 2,2", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 3,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 3,2", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3,2"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 4,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookAir 1,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir1,1"), +- }, +- .driver_data = (void *)&intel_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBook 5,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5,1"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBook 5,2", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5,2"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBook 6,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook6,1"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookAir 2,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2,1"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 5,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,1"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 5,2", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,2"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 5,3", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 5,4", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookPro 5,5", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,5"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookAir 3,1", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir3,1"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { +- .callback = mbp_dmi_match, +- .ident = "MacBookAir 3,2", +- .matches = { +- DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir3,2"), +- }, +- .driver_data = (void *)&nvidia_chipset_data, +- }, +- { } +-}; +- +-static int __init mbp_init(void) +-{ +- struct backlight_properties props; +- if (!dmi_check_system(mbp_device_table)) +- return -ENODEV; +- +- if (!request_region(driver_data->iostart, driver_data->iolen, +- "Macbook Pro backlight")) +- return -ENXIO; +- +- memset(&props, 0, sizeof(struct backlight_properties)); +- props.max_brightness = 15; +- mbp_backlight_device = backlight_device_register("mbp_backlight", NULL, +- NULL, +- &driver_data->backlight_ops, +- &props); +- if (IS_ERR(mbp_backlight_device)) { +- release_region(driver_data->iostart, driver_data->iolen); +- return PTR_ERR(mbp_backlight_device); +- } +- +- mbp_backlight_device->props.brightness = +- driver_data->backlight_ops.get_brightness(mbp_backlight_device); +- backlight_update_status(mbp_backlight_device); +- +- return 0; +-} +- +-static void __exit mbp_exit(void) +-{ +- backlight_device_unregister(mbp_backlight_device); +- +- release_region(driver_data->iostart, driver_data->iolen); +-} +- +-module_init(mbp_init); +-module_exit(mbp_exit); +- +-MODULE_AUTHOR("Matthew Garrett "); +-MODULE_DESCRIPTION("Nvidia-based Macbook Pro Backlight Driver"); +-MODULE_LICENSE("GPL"); +-MODULE_DEVICE_TABLE(dmi, mbp_device_table); diff --git a/applesmc_update.patch b/applesmc_update.patch new file mode 100644 index 0000000..d421468 --- /dev/null +++ b/applesmc_update.patch @@ -0,0 +1,2293 @@ +Update the applesmc driver. Should work on all hardware. Upstream in .38? + +diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c +index b6598aa..0d7e9ae 100644 +--- a/drivers/hwmon/applesmc.c ++++ b/drivers/hwmon/applesmc.c +@@ -4,6 +4,7 @@ + * computers. + * + * Copyright (C) 2007 Nicolas Boichat ++ * Copyright (C) 2010 Henrik Rydberg + * + * Based on hdaps.c driver: + * Copyright (C) 2005 Robert Love +@@ -26,10 +27,12 @@ + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ + ++#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt ++ + #include +-#include + #include + #include ++#include + #include + #include + #include +@@ -39,16 +42,23 @@ + #include + #include + #include ++#include ++#include ++#include ++#include + + /* data port used by Apple SMC */ +-#define APPLESMC_DATA_PORT 0x300 ++#define APPLESMC_DATA_PORT 0x0 + /* command/status port used by Apple SMC */ +-#define APPLESMC_CMD_PORT 0x304 ++#define APPLESMC_CMD_PORT 0x4 ++/* interrupt status port */ ++#define APPLESMC_ISR_PORT 0x1f + + #define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */ + + #define APPLESMC_MAX_DATA_LENGTH 32 + ++/* wait up to 32 ms for a status change. */ + #define APPLESMC_MIN_WAIT 0x0040 + #define APPLESMC_MAX_WAIT 0x8000 + +@@ -71,106 +81,19 @@ + #define MOTION_SENSOR_Z_KEY "MO_Z" /* r-o sp78 (2 bytes) */ + #define MOTION_SENSOR_KEY "MOCN" /* r/w ui16 */ + ++#define NOTIFICATION_KEY "NTOK" ++ + #define FANS_COUNT "FNum" /* r-o ui8 */ + #define FANS_MANUAL "FS! " /* r-w ui16 */ +-#define FAN_ACTUAL_SPEED "F0Ac" /* r-o fpe2 (2 bytes) */ +-#define FAN_MIN_SPEED "F0Mn" /* r-o fpe2 (2 bytes) */ +-#define FAN_MAX_SPEED "F0Mx" /* r-o fpe2 (2 bytes) */ +-#define FAN_SAFE_SPEED "F0Sf" /* r-o fpe2 (2 bytes) */ +-#define FAN_TARGET_SPEED "F0Tg" /* r-w fpe2 (2 bytes) */ +-#define FAN_POSITION "F0ID" /* r-o char[16] */ +- +-/* +- * Temperature sensors keys (sp78 - 2 bytes). +- */ +-static const char *temperature_sensors_sets[][41] = { +-/* Set 0: Macbook Pro */ +- { "TA0P", "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "Th0H", +- "Th1H", "Tm0P", "Ts0P", "Ts1P", NULL }, +-/* Set 1: Macbook2 set */ +- { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "TTF0", "Th0H", +- "Th0S", "Th1H", NULL }, +-/* Set 2: Macbook set */ +- { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TN1P", "Th0H", "Th0S", +- "Th1H", "Ts0P", NULL }, +-/* Set 3: Macmini set */ +- { "TC0D", "TC0P", NULL }, +-/* Set 4: Mac Pro (2 x Quad-Core) */ +- { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P", +- "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "THTG", "TH0P", +- "TH1P", "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S", +- "TM1P", "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P", +- "TM9S", "TN0H", "TS0C", NULL }, +-/* Set 5: iMac */ +- { "TC0D", "TA0P", "TG0P", "TG0D", "TG0H", "TH0P", "Tm0P", "TO0P", +- "Tp0C", NULL }, +-/* Set 6: Macbook3 set */ +- { "TB0T", "TC0D", "TC0P", "TM0P", "TN0P", "TTF0", "TW0P", "Th0H", +- "Th0S", "Th1H", NULL }, +-/* Set 7: Macbook Air */ +- { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TC0P", "TCFP", +- "TTF0", "TW0P", "Th0H", "Tp0P", "TpFP", "Ts0P", "Ts0S", NULL }, +-/* Set 8: Macbook Pro 4,1 (Penryn) */ +- { "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P", "Th0H", +- "Th1H", "Th2H", "Tm0P", "Ts0P", NULL }, +-/* Set 9: Macbook Pro 3,1 (Santa Rosa) */ +- { "TALP", "TB0T", "TC0D", "TC0P", "TG0D", "TG0H", "TTF0", "TW0P", +- "Th0H", "Th1H", "Th2H", "Tm0P", "Ts0P", NULL }, +-/* Set 10: iMac 5,1 */ +- { "TA0P", "TC0D", "TC0P", "TG0D", "TH0P", "TO0P", "Tm0P", NULL }, +-/* Set 11: Macbook 5,1 */ +- { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0P", "TN0D", "TN0P", +- "TTF0", "Th0H", "Th1H", "ThFH", "Ts0P", "Ts0S", NULL }, +-/* Set 12: Macbook Pro 5,1 */ +- { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D", +- "TG0F", "TG0H", "TG0P", "TG0T", "TG1H", "TN0D", "TN0P", "TTF0", +- "Th2H", "Tm0P", "Ts0P", "Ts0S", NULL }, +-/* Set 13: iMac 8,1 */ +- { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P", +- "TL0P", "TO0P", "TW0P", "Tm0P", "Tp0P", NULL }, +-/* Set 14: iMac 6,1 */ +- { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TG0P", "TH0P", +- "TO0P", "Tp0P", NULL }, +-/* Set 15: MacBook Air 2,1 */ +- { "TB0T", "TB1S", "TB1T", "TB2S", "TB2T", "TC0D", "TN0D", "TTF0", +- "TV0P", "TVFP", "TW0P", "Th0P", "Tp0P", "Tp1P", "TpFP", "Ts0P", +- "Ts0S", NULL }, +-/* Set 16: Mac Pro 3,1 (2 x Quad-Core) */ +- { "TA0P", "TCAG", "TCAH", "TCBG", "TCBH", "TC0C", "TC0D", "TC0P", +- "TC1C", "TC1D", "TC2C", "TC2D", "TC3C", "TC3D", "TH0P", "TH1P", +- "TH2P", "TH3P", "TMAP", "TMAS", "TMBS", "TM0P", "TM0S", "TM1P", +- "TM1S", "TM2P", "TM2S", "TM3S", "TM8P", "TM8S", "TM9P", "TM9S", +- "TN0C", "TN0D", "TN0H", "TS0C", "Tp0C", "Tp1C", "Tv0S", "Tv1S", +- NULL }, +-/* Set 17: iMac 9,1 */ +- { "TA0P", "TC0D", "TC0H", "TC0P", "TG0D", "TG0H", "TH0P", "TL0P", +- "TN0D", "TN0H", "TN0P", "TO0P", "Tm0P", "Tp0P", NULL }, +-/* Set 18: MacBook Pro 2,2 */ +- { "TB0T", "TC0D", "TC0P", "TG0H", "TG0P", "TG0T", "TM0P", "TTF0", +- "Th0H", "Th1H", "Tm0P", "Ts0P", NULL }, +-/* Set 19: Macbook Pro 5,3 */ +- { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TG0D", +- "TG0F", "TG0H", "TG0P", "TG0T", "TN0D", "TN0P", "TTF0", "Th2H", +- "Tm0P", "Ts0P", "Ts0S", NULL }, +-/* Set 20: MacBook Pro 5,4 */ +- { "TB0T", "TB1T", "TB2T", "TB3T", "TC0D", "TC0F", "TC0P", "TN0D", +- "TN0P", "TTF0", "Th2H", "Ts0P", "Ts0S", NULL }, +-/* Set 21: MacBook Pro 6,2 */ +- { "TB0T", "TB1T", "TB2T", "TC0C", "TC0D", "TC0P", "TC1C", "TG0D", +- "TG0P", "TG0T", "TMCD", "TP0P", "TPCD", "Th1H", "Th2H", "Tm0P", +- "Ts0P", "Ts0S", NULL }, +-/* Set 22: MacBook Pro 7,1 */ +- { "TB0T", "TB1T", "TB2T", "TC0D", "TC0P", "TN0D", "TN0P", "TN0S", +- "TN1D", "TN1F", "TN1G", "TN1S", "Th1H", "Ts0P", "Ts0S", NULL }, +-}; ++#define FAN_ID_FMT "F%dID" /* r-o char[16] */ + + /* List of keys used to read/write fan speeds */ +-static const char* fan_speed_keys[] = { +- FAN_ACTUAL_SPEED, +- FAN_MIN_SPEED, +- FAN_MAX_SPEED, +- FAN_SAFE_SPEED, +- FAN_TARGET_SPEED ++static const char *const fan_speed_fmt[] = { ++ "F%dAc", /* actual speed */ ++ "F%dMn", /* minimum speed (rw) */ ++ "F%dMx", /* maximum speed */ ++ "F%dSf", /* safe speed - not all models */ ++ "F%dTg", /* target speed (manual: rw) */ + }; + + #define INIT_TIMEOUT_MSECS 5000 /* wait up to 5s for device init ... */ +@@ -184,18 +107,58 @@ static const char* fan_speed_keys[] = { + #define SENSOR_Y 1 + #define SENSOR_Z 2 + +-/* Structure to be passed to DMI_MATCH function */ +-struct dmi_match_data { +-/* Indicates whether this computer has an accelerometer. */ +- int accelerometer; +-/* Indicates whether this computer has light sensors and keyboard backlight. */ +- int light; +-/* Indicates which temperature sensors set to use. */ +- int temperature_set; ++#define to_index(attr) (to_sensor_dev_attr(attr)->index & 0xffff) ++#define to_option(attr) (to_sensor_dev_attr(attr)->index >> 16) ++ ++struct applesmc_pnp_device { ++ int iobase; ++ int iolen; ++ int irq; ++}; ++ ++struct pnp_dev *pdev; ++struct applesmc_pnp_device *pnp_device; ++ ++/* Dynamic device node attributes */ ++struct applesmc_dev_attr { ++ struct sensor_device_attribute sda; /* hwmon attributes */ ++ char name[32]; /* room for node file name */ ++}; ++ ++/* Dynamic device node group */ ++struct applesmc_node_group { ++ char *format; /* format string */ ++ void *show; /* show function */ ++ void *store; /* store function */ ++ int option; /* function argument */ ++ struct applesmc_dev_attr *nodes; /* dynamic node array */ + }; + ++/* AppleSMC entry - cached register information */ ++struct applesmc_entry { ++ char key[5]; /* four-letter key code */ ++ u8 valid; /* set when entry is successfully read once */ ++ u8 len; /* bounded by APPLESMC_MAX_DATA_LENGTH */ ++ char type[5]; /* four-letter type code */ ++ u8 flags; /* 0x10: func; 0x40: write; 0x80: read */ ++}; ++ ++/* Register lookup and registers common to all SMCs */ ++static struct applesmc_registers { ++ struct mutex mutex; /* register read/write mutex */ ++ unsigned int key_count; /* number of SMC registers */ ++ unsigned int fan_count; /* number of fans */ ++ unsigned int temp_count; /* number of temperature registers */ ++ unsigned int temp_begin; /* temperature lower index bound */ ++ unsigned int temp_end; /* temperature upper index bound */ ++ int num_light_sensors; /* number of light sensors */ ++ bool has_accelerometer; /* has motion sensor */ ++ bool has_key_backlight; /* has keyboard backlight */ ++ bool init_complete; /* true when fully initialized */ ++ struct applesmc_entry *cache; /* cached key entries */ ++} smcreg; ++ + static const int debug; +-static struct platform_device *pdev; + static s16 rest_x; + static s16 rest_y; + static u8 backlight_state[2]; +@@ -203,20 +166,6 @@ static u8 backlight_state[2]; + static struct device *hwmon_dev; + static struct input_polled_dev *applesmc_idev; + +-/* Indicates whether this computer has an accelerometer. */ +-static unsigned int applesmc_accelerometer; +- +-/* Indicates whether this computer has light sensors and keyboard backlight. */ +-static unsigned int applesmc_light; +- +-/* The number of fans handled by the driver */ +-static unsigned int fans_handled; +- +-/* Indicates which temperature sensors set to use. */ +-static unsigned int applesmc_temperature_set; +- +-static DEFINE_MUTEX(applesmc_lock); +- + /* + * Last index written to key_at_index sysfs file, and value to use for all other + * key_at_index_* sysfs files. +@@ -225,6 +174,16 @@ static unsigned int key_at_index; + + static struct workqueue_struct *applesmc_led_wq; + ++static u8 applesmc_read_reg(u8 reg) ++{ ++ return inb(pnp_device->iobase + reg); ++} ++ ++static void applesmc_write_reg(u8 val, u8 reg) ++{ ++ outb(val, pnp_device->iobase + reg); ++} ++ + /* + * __wait_status - Wait up to 32ms for the status port to get a certain value + * (masked with 0x0f), returning zero if the value is obtained. Callers must +@@ -238,18 +197,12 @@ static int __wait_status(u8 val) + + for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { + udelay(us); +- if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == val) { +- if (debug) +- printk(KERN_DEBUG +- "Waited %d us for status %x\n", +- 2 * us - APPLESMC_MIN_WAIT, val); ++ if ((applesmc_read_reg(APPLESMC_CMD_PORT) ++ & APPLESMC_STATUS_MASK) == val) { + return 0; + } + } + +- printk(KERN_WARNING "applesmc: wait status failed: %x != %x\n", +- val, inb(APPLESMC_CMD_PORT)); +- + return -EIO; + } + +@@ -262,162 +215,246 @@ static int send_command(u8 cmd) + { + int us; + for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) { +- outb(cmd, APPLESMC_CMD_PORT); ++ applesmc_write_reg(cmd, APPLESMC_CMD_PORT); + udelay(us); +- if ((inb(APPLESMC_CMD_PORT) & APPLESMC_STATUS_MASK) == 0x0c) ++ if ((applesmc_read_reg(APPLESMC_CMD_PORT) ++ & APPLESMC_STATUS_MASK) == 0x0c) + return 0; + } +- printk(KERN_WARNING "applesmc: command failed: %x -> %x\n", +- cmd, inb(APPLESMC_CMD_PORT)); + return -EIO; + } + +-/* +- * applesmc_read_key - reads len bytes from a given key, and put them in buffer. +- * Returns zero on success or a negative error on failure. Callers must +- * hold applesmc_lock. +- */ +-static int applesmc_read_key(const char* key, u8* buffer, u8 len) ++static int send_argument(const char *key) + { + int i; + +- if (len > APPLESMC_MAX_DATA_LENGTH) { +- printk(KERN_ERR "applesmc_read_key: cannot read more than " +- "%d bytes\n", APPLESMC_MAX_DATA_LENGTH); +- return -EINVAL; +- } +- +- if (send_command(APPLESMC_READ_CMD)) +- return -EIO; +- + for (i = 0; i < 4; i++) { +- outb(key[i], APPLESMC_DATA_PORT); ++ applesmc_write_reg(key[i], APPLESMC_DATA_PORT); + if (__wait_status(0x04)) + return -EIO; + } +- if (debug) +- printk(KERN_DEBUG "<%s", key); ++ return 0; ++} + +- outb(len, APPLESMC_DATA_PORT); +- if (debug) +- printk(KERN_DEBUG ">%x", len); ++static int read_smc(u8 cmd, const char *key, u8 *buffer, u8 len) ++{ ++ int i; ++ ++ if (send_command(cmd) || send_argument(key)) { ++ pr_warn("%s: read arg fail\n", key); ++ return -EIO; ++ } ++ ++ applesmc_write_reg(len, APPLESMC_DATA_PORT); + + for (i = 0; i < len; i++) { +- if (__wait_status(0x05)) ++ if (__wait_status(0x05)) { ++ pr_warn("%s: read data fail\n", key); + return -EIO; +- buffer[i] = inb(APPLESMC_DATA_PORT); +- if (debug) +- printk(KERN_DEBUG "<%x", buffer[i]); ++ } ++ buffer[i] = applesmc_read_reg(APPLESMC_DATA_PORT); + } +- if (debug) +- printk(KERN_DEBUG "\n"); + + return 0; + } + +-/* +- * applesmc_write_key - writes len bytes from buffer to a given key. +- * Returns zero on success or a negative error on failure. Callers must +- * hold applesmc_lock. +- */ +-static int applesmc_write_key(const char* key, u8* buffer, u8 len) ++static int write_smc(u8 cmd, const char *key, const u8 *buffer, u8 len) + { + int i; + +- if (len > APPLESMC_MAX_DATA_LENGTH) { +- printk(KERN_ERR "applesmc_write_key: cannot write more than " +- "%d bytes\n", APPLESMC_MAX_DATA_LENGTH); +- return -EINVAL; +- } +- +- if (send_command(APPLESMC_WRITE_CMD)) ++ if (send_command(cmd) || send_argument(key)) { ++ pr_warn("%s: write arg fail\n", key); + return -EIO; +- +- for (i = 0; i < 4; i++) { +- outb(key[i], APPLESMC_DATA_PORT); +- if (__wait_status(0x04)) +- return -EIO; + } + +- outb(len, APPLESMC_DATA_PORT); ++ applesmc_write_reg(len, APPLESMC_DATA_PORT); + + for (i = 0; i < len; i++) { +- if (__wait_status(0x04)) ++ if (__wait_status(0x04)) { ++ pr_warn("%s: write data fail\n", key); + return -EIO; +- outb(buffer[i], APPLESMC_DATA_PORT); ++ } ++ applesmc_write_reg(buffer[i], APPLESMC_DATA_PORT); + } + + return 0; + } + ++static int read_register_count(unsigned int *count) ++{ ++ __be32 be; ++ int ret; ++ ++ ret = read_smc(APPLESMC_READ_CMD, KEY_COUNT_KEY, (u8 *)&be, 4); ++ if (ret) ++ return ret; ++ ++ *count = be32_to_cpu(be); ++ return 0; ++} ++ + /* +- * applesmc_get_key_at_index - get key at index, and put the result in key +- * (char[6]). Returns zero on success or a negative error on failure. Callers +- * must hold applesmc_lock. ++ * Serialized I/O ++ * ++ * Returns zero on success or a negative error on failure. ++ * All functions below are concurrency safe - callers should NOT hold lock. + */ +-static int applesmc_get_key_at_index(int index, char* key) ++ ++static int applesmc_read_entry(const struct applesmc_entry *entry, ++ u8 *buf, u8 len) + { +- int i; +- u8 readkey[4]; +- readkey[0] = index >> 24; +- readkey[1] = index >> 16; +- readkey[2] = index >> 8; +- readkey[3] = index; ++ int ret; + +- if (send_command(APPLESMC_GET_KEY_BY_INDEX_CMD)) +- return -EIO; ++ if (entry->len != len) ++ return -EINVAL; ++ mutex_lock(&smcreg.mutex); ++ ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len); ++ mutex_unlock(&smcreg.mutex); + +- for (i = 0; i < 4; i++) { +- outb(readkey[i], APPLESMC_DATA_PORT); +- if (__wait_status(0x04)) +- return -EIO; ++ return ret; ++} ++ ++static int applesmc_write_entry(const struct applesmc_entry *entry, ++ const u8 *buf, u8 len) ++{ ++ int ret; ++ ++ if (entry->len != len) ++ return -EINVAL; ++ mutex_lock(&smcreg.mutex); ++ ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len); ++ mutex_unlock(&smcreg.mutex); ++ return ret; ++} ++ ++static const struct applesmc_entry *applesmc_get_entry_by_index(int index) ++{ ++ struct applesmc_entry *cache = &smcreg.cache[index]; ++ u8 key[4], info[6]; ++ __be32 be; ++ int ret = 0; ++ ++ if (cache->valid) ++ return cache; ++ ++ mutex_lock(&smcreg.mutex); ++ ++ if (cache->valid) ++ goto out; ++ be = cpu_to_be32(index); ++ ret = read_smc(APPLESMC_GET_KEY_BY_INDEX_CMD, (u8 *)&be, key, 4); ++ if (ret) ++ goto out; ++ ret = read_smc(APPLESMC_GET_KEY_TYPE_CMD, key, info, 6); ++ if (ret) ++ goto out; ++ ++ memcpy(cache->key, key, 4); ++ cache->len = info[0]; ++ memcpy(cache->type, &info[1], 4); ++ cache->flags = info[5]; ++ cache->valid = 1; ++ ++out: ++ mutex_unlock(&smcreg.mutex); ++ if (ret) ++ return ERR_PTR(ret); ++ return cache; ++} ++ ++static int applesmc_get_lower_bound(unsigned int *lo, const char *key) ++{ ++ int begin = 0, end = smcreg.key_count; ++ const struct applesmc_entry *entry; ++ ++ while (begin != end) { ++ int middle = begin + (end - begin) / 2; ++ entry = applesmc_get_entry_by_index(middle); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); ++ if (strcmp(entry->key, key) < 0) ++ begin = middle + 1; ++ else ++ end = middle; + } + +- outb(4, APPLESMC_DATA_PORT); ++ *lo = begin; ++ return 0; ++} + +- for (i = 0; i < 4; i++) { +- if (__wait_status(0x05)) +- return -EIO; +- key[i] = inb(APPLESMC_DATA_PORT); ++static int applesmc_get_upper_bound(unsigned int *hi, const char *key) ++{ ++ int begin = 0, end = smcreg.key_count; ++ const struct applesmc_entry *entry; ++ ++ while (begin != end) { ++ int middle = begin + (end - begin) / 2; ++ entry = applesmc_get_entry_by_index(middle); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); ++ if (strcmp(key, entry->key) < 0) ++ end = middle; ++ else ++ begin = middle + 1; + } +- key[4] = 0; + ++ *hi = begin; + return 0; + } + +-/* +- * applesmc_get_key_type - get key type, and put the result in type (char[6]). +- * Returns zero on success or a negative error on failure. Callers must +- * hold applesmc_lock. +- */ +-static int applesmc_get_key_type(char* key, char* type) ++static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key) + { +- int i; ++ int begin, end; ++ int ret; + +- if (send_command(APPLESMC_GET_KEY_TYPE_CMD)) +- return -EIO; ++ ret = applesmc_get_lower_bound(&begin, key); ++ if (ret) ++ return ERR_PTR(ret); ++ ret = applesmc_get_upper_bound(&end, key); ++ if (ret) ++ return ERR_PTR(ret); ++ if (end - begin != 1) ++ return ERR_PTR(-EINVAL); + +- for (i = 0; i < 4; i++) { +- outb(key[i], APPLESMC_DATA_PORT); +- if (__wait_status(0x04)) +- return -EIO; +- } ++ return applesmc_get_entry_by_index(begin); ++} + +- outb(6, APPLESMC_DATA_PORT); ++static int applesmc_read_key(const char *key, u8 *buffer, u8 len) ++{ ++ const struct applesmc_entry *entry; + +- for (i = 0; i < 6; i++) { +- if (__wait_status(0x05)) +- return -EIO; +- type[i] = inb(APPLESMC_DATA_PORT); +- } +- type[5] = 0; ++ entry = applesmc_get_entry_by_key(key); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); ++ ++ return applesmc_read_entry(entry, buffer, len); ++} ++ ++static int applesmc_write_key(const char *key, const u8 *buffer, u8 len) ++{ ++ const struct applesmc_entry *entry; ++ ++ entry = applesmc_get_entry_by_key(key); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); ++ ++ return applesmc_write_entry(entry, buffer, len); ++} ++ ++static int applesmc_has_key(const char *key, bool *value) ++{ ++ const struct applesmc_entry *entry; + ++ entry = applesmc_get_entry_by_key(key); ++ if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL) ++ return PTR_ERR(entry); ++ ++ *value = !IS_ERR(entry); + return 0; + } + + /* +- * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). Callers must +- * hold applesmc_lock. ++ * applesmc_read_motion_sensor - Read motion sensor (X, Y or Z). + */ + static int applesmc_read_motion_sensor(int index, s16* value) + { +@@ -444,100 +481,130 @@ static int applesmc_read_motion_sensor(int index, s16* value) + } + + /* +- * applesmc_device_init - initialize the accelerometer. Returns zero on success +- * and negative error code on failure. Can sleep. ++ * applesmc_device_init - initialize the accelerometer. Can sleep. + */ +-static int applesmc_device_init(void) ++static void applesmc_device_init(void) + { +- int total, ret = -ENXIO; ++ int total; + u8 buffer[2]; + +- if (!applesmc_accelerometer) +- return 0; +- +- mutex_lock(&applesmc_lock); ++ if (!smcreg.has_accelerometer) ++ return; + + for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) { +- if (debug) +- printk(KERN_DEBUG "applesmc try %d\n", total); + if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) && +- (buffer[0] != 0x00 || buffer[1] != 0x00)) { +- if (total == INIT_TIMEOUT_MSECS) { +- printk(KERN_DEBUG "applesmc: device has" +- " already been initialized" +- " (0x%02x, 0x%02x).\n", +- buffer[0], buffer[1]); +- } else { +- printk(KERN_DEBUG "applesmc: device" +- " successfully initialized" +- " (0x%02x, 0x%02x).\n", +- buffer[0], buffer[1]); +- } +- ret = 0; +- goto out; +- } ++ (buffer[0] != 0x00 || buffer[1] != 0x00)) ++ return; + buffer[0] = 0xe0; + buffer[1] = 0x00; + applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2); + msleep(INIT_WAIT_MSECS); + } + +- printk(KERN_WARNING "applesmc: failed to init the device\n"); +- +-out: +- mutex_unlock(&applesmc_lock); +- return ret; ++ pr_warn("failed to init the device\n"); + } + + /* +- * applesmc_get_fan_count - get the number of fans. Callers must NOT hold +- * applesmc_lock. ++ * applesmc_init_smcreg_try - Try to initialize register cache. Idempotent. + */ +-static int applesmc_get_fan_count(void) ++static int applesmc_init_smcreg_try(void) + { ++ struct applesmc_registers *s = &smcreg; ++ bool left_light_sensor, right_light_sensor; ++ u8 tmp[1]; + int ret; +- u8 buffer[1]; + +- mutex_lock(&applesmc_lock); ++ if (s->init_complete) ++ return 0; + +- ret = applesmc_read_key(FANS_COUNT, buffer, 1); ++ mutex_init(&s->mutex); + +- mutex_unlock(&applesmc_lock); ++ ret = read_register_count(&s->key_count); + if (ret) + return ret; +- else +- return buffer[0]; +-} + +-/* Device model stuff */ +-static int applesmc_probe(struct platform_device *dev) +-{ +- int ret; ++ if (!s->cache) ++ s->cache = kcalloc(s->key_count, sizeof(*s->cache), GFP_KERNEL); ++ if (!s->cache) ++ return -ENOMEM; + +- ret = applesmc_device_init(); ++ ret = applesmc_read_key(FANS_COUNT, tmp, 1); + if (ret) + return ret; ++ s->fan_count = tmp[0]; ++ ++ ret = applesmc_get_lower_bound(&s->temp_begin, "T"); ++ if (ret) ++ return ret; ++ ret = applesmc_get_lower_bound(&s->temp_end, "U"); ++ if (ret) ++ return ret; ++ s->temp_count = s->temp_end - s->temp_begin; ++ ++ ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor); ++ if (ret) ++ return ret; ++ ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor); ++ if (ret) ++ return ret; ++ ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer); ++ if (ret) ++ return ret; ++ ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight); ++ if (ret) ++ return ret; ++ ++ s->num_light_sensors = left_light_sensor + right_light_sensor; ++ s->init_complete = true; ++ ++ pr_info("key=%d fan=%d temp=%d acc=%d lux=%d kbd=%d\n", ++ s->key_count, s->fan_count, s->temp_count, ++ s->has_accelerometer, ++ s->num_light_sensors, ++ s->has_key_backlight); + +- printk(KERN_INFO "applesmc: device successfully initialized.\n"); + return 0; + } + ++/* ++ * applesmc_init_smcreg - Initialize register cache. ++ * ++ * Retries until initialization is successful, or the operation times out. ++ * ++ */ ++static int applesmc_init_smcreg(void) ++{ ++ int ms, ret; ++ ++ for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) { ++ ret = applesmc_init_smcreg_try(); ++ if (!ret) ++ return 0; ++ pr_warn("slow init, retrying\n"); ++ msleep(INIT_WAIT_MSECS); ++ } ++ ++ return ret; ++} ++ ++static void applesmc_destroy_smcreg(void) ++{ ++ kfree(smcreg.cache); ++ memset(&smcreg, 0, sizeof(smcreg)); ++} ++ + /* Synchronize device with memorized backlight state */ + static int applesmc_pm_resume(struct device *dev) + { +- mutex_lock(&applesmc_lock); +- if (applesmc_light) ++ if (smcreg.has_key_backlight) + applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); +- mutex_unlock(&applesmc_lock); + return 0; + } + + /* Reinitialize device on resume from hibernation */ + static int applesmc_pm_restore(struct device *dev) + { +- int ret = applesmc_device_init(); +- if (ret) +- return ret; ++ applesmc_device_init(); + return applesmc_pm_resume(dev); + } + +@@ -546,15 +613,6 @@ static const struct dev_pm_ops applesmc_pm_ops = { + .restore = applesmc_pm_restore, + }; + +-static struct platform_driver applesmc_driver = { +- .probe = applesmc_probe, +- .driver = { +- .name = "applesmc", +- .owner = THIS_MODULE, +- .pm = &applesmc_pm_ops, +- }, +-}; +- + /* + * applesmc_calibrate - Set our "resting" values. Callers must + * hold applesmc_lock. +@@ -571,20 +629,15 @@ static void applesmc_idev_poll(struct input_polled_dev *dev) + struct input_dev *idev = dev->input; + s16 x, y; + +- mutex_lock(&applesmc_lock); +- + if (applesmc_read_motion_sensor(SENSOR_X, &x)) +- goto out; ++ return; + if (applesmc_read_motion_sensor(SENSOR_Y, &y)) +- goto out; ++ return; + + x = -x; + input_report_abs(idev, ABS_X, x - rest_x); + input_report_abs(idev, ABS_Y, y - rest_y); + input_sync(idev); +- +-out: +- mutex_unlock(&applesmc_lock); + } + + /* Sysfs Files */ +@@ -601,8 +654,6 @@ static ssize_t applesmc_position_show(struct device *dev, + int ret; + s16 x, y, z; + +- mutex_lock(&applesmc_lock); +- + ret = applesmc_read_motion_sensor(SENSOR_X, &x); + if (ret) + goto out; +@@ -614,7 +665,6 @@ static ssize_t applesmc_position_show(struct device *dev, + goto out; + + out: +- mutex_unlock(&applesmc_lock); + if (ret) + return ret; + else +@@ -624,20 +674,20 @@ out: + static ssize_t applesmc_light_show(struct device *dev, + struct device_attribute *attr, char *sysfsbuf) + { ++ const struct applesmc_entry *entry; + static int data_length; + int ret; + u8 left = 0, right = 0; +- u8 buffer[10], query[6]; +- +- mutex_lock(&applesmc_lock); ++ u8 buffer[10]; + + if (!data_length) { +- ret = applesmc_get_key_type(LIGHT_SENSOR_LEFT_KEY, query); +- if (ret) +- goto out; +- data_length = clamp_val(query[0], 0, 10); +- printk(KERN_INFO "applesmc: light sensor data length set to " +- "%d\n", data_length); ++ entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); ++ if (entry->len > 10) ++ return -ENXIO; ++ data_length = entry->len; ++ pr_info("light sensor data length set to %d\n", data_length); + } + + ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length); +@@ -653,7 +703,6 @@ static ssize_t applesmc_light_show(struct device *dev, + right = buffer[2]; + + out: +- mutex_unlock(&applesmc_lock); + if (ret) + return ret; + else +@@ -664,36 +713,48 @@ out: + static ssize_t applesmc_show_sensor_label(struct device *dev, + struct device_attribute *devattr, char *sysfsbuf) + { +- struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); +- const char *key = +- temperature_sensors_sets[applesmc_temperature_set][attr->index]; ++ int index = smcreg.temp_begin + to_index(devattr); ++ const struct applesmc_entry *entry; ++ ++ entry = applesmc_get_entry_by_index(index); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); + +- return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key); ++ return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key); + } + + /* Displays degree Celsius * 1000 */ + static ssize_t applesmc_show_temperature(struct device *dev, + struct device_attribute *devattr, char *sysfsbuf) + { ++ int index = smcreg.temp_begin + to_index(devattr); ++ const struct applesmc_entry *entry; + int ret; + u8 buffer[2]; + unsigned int temp; +- struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); +- const char* key = +- temperature_sensors_sets[applesmc_temperature_set][attr->index]; + +- mutex_lock(&applesmc_lock); +- +- ret = applesmc_read_key(key, buffer, 2); +- temp = buffer[0]*1000; +- temp += (buffer[1] >> 6) * 250; +- +- mutex_unlock(&applesmc_lock); ++ entry = applesmc_get_entry_by_index(index); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); ++ if (entry->len > 2) ++ return -EINVAL; + ++ ret = applesmc_read_entry(entry, buffer, entry->len); + if (ret) + return ret; +- else +- return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp); ++ ++ if (entry->len == 2) { ++ if (buffer[0] >= 0x80) { ++ /* The two byte format is signed - ignore negative */ ++ return -EINVAL; ++ } ++ temp = buffer[0] * 1000; ++ temp += (buffer[1] >> 6) * 250; ++ } else { ++ temp = buffer[0] * 4000; ++ } ++ ++ return snprintf(sysfsbuf, PAGE_SIZE, "%u\n", temp); + } + + static ssize_t applesmc_show_fan_speed(struct device *dev, +@@ -703,21 +764,12 @@ static ssize_t applesmc_show_fan_speed(struct device *dev, + unsigned int speed = 0; + char newkey[5]; + u8 buffer[2]; +- struct sensor_device_attribute_2 *sensor_attr = +- to_sensor_dev_attr_2(attr); + +- newkey[0] = fan_speed_keys[sensor_attr->nr][0]; +- newkey[1] = '0' + sensor_attr->index; +- newkey[2] = fan_speed_keys[sensor_attr->nr][2]; +- newkey[3] = fan_speed_keys[sensor_attr->nr][3]; +- newkey[4] = 0; +- +- mutex_lock(&applesmc_lock); ++ sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr)); + + ret = applesmc_read_key(newkey, buffer, 2); + speed = ((buffer[0] << 8 | buffer[1]) >> 2); + +- mutex_unlock(&applesmc_lock); + if (ret) + return ret; + else +@@ -732,27 +784,18 @@ static ssize_t applesmc_store_fan_speed(struct device *dev, + u32 speed; + char newkey[5]; + u8 buffer[2]; +- struct sensor_device_attribute_2 *sensor_attr = +- to_sensor_dev_attr_2(attr); + + speed = simple_strtoul(sysfsbuf, NULL, 10); + + if (speed > 0x4000) /* Bigger than a 14-bit value */ + return -EINVAL; + +- newkey[0] = fan_speed_keys[sensor_attr->nr][0]; +- newkey[1] = '0' + sensor_attr->index; +- newkey[2] = fan_speed_keys[sensor_attr->nr][2]; +- newkey[3] = fan_speed_keys[sensor_attr->nr][3]; +- newkey[4] = 0; +- +- mutex_lock(&applesmc_lock); ++ sprintf(newkey, fan_speed_fmt[to_option(attr)], to_index(attr)); + + buffer[0] = (speed >> 6) & 0xff; + buffer[1] = (speed << 2) & 0xff; + ret = applesmc_write_key(newkey, buffer, 2); + +- mutex_unlock(&applesmc_lock); + if (ret) + return ret; + else +@@ -760,19 +803,15 @@ static ssize_t applesmc_store_fan_speed(struct device *dev, + } + + static ssize_t applesmc_show_fan_manual(struct device *dev, +- struct device_attribute *devattr, char *sysfsbuf) ++ struct device_attribute *attr, char *sysfsbuf) + { + int ret; + u16 manual = 0; + u8 buffer[2]; +- struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); +- +- mutex_lock(&applesmc_lock); + + ret = applesmc_read_key(FANS_MANUAL, buffer, 2); +- manual = ((buffer[0] << 8 | buffer[1]) >> attr->index) & 0x01; ++ manual = ((buffer[0] << 8 | buffer[1]) >> to_index(attr)) & 0x01; + +- mutex_unlock(&applesmc_lock); + if (ret) + return ret; + else +@@ -780,28 +819,25 @@ static ssize_t applesmc_show_fan_manual(struct device *dev, + } + + static ssize_t applesmc_store_fan_manual(struct device *dev, +- struct device_attribute *devattr, ++ struct device_attribute *attr, + const char *sysfsbuf, size_t count) + { + int ret; + u8 buffer[2]; + u32 input; + u16 val; +- struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); + + input = simple_strtoul(sysfsbuf, NULL, 10); + +- mutex_lock(&applesmc_lock); +- + ret = applesmc_read_key(FANS_MANUAL, buffer, 2); + val = (buffer[0] << 8 | buffer[1]); + if (ret) + goto out; + + if (input) +- val = val | (0x01 << attr->index); ++ val = val | (0x01 << to_index(attr)); + else +- val = val & ~(0x01 << attr->index); ++ val = val & ~(0x01 << to_index(attr)); + + buffer[0] = (val >> 8) & 0xFF; + buffer[1] = val & 0xFF; +@@ -809,7 +845,6 @@ static ssize_t applesmc_store_fan_manual(struct device *dev, + ret = applesmc_write_key(FANS_MANUAL, buffer, 2); + + out: +- mutex_unlock(&applesmc_lock); + if (ret) + return ret; + else +@@ -822,21 +857,12 @@ static ssize_t applesmc_show_fan_position(struct device *dev, + int ret; + char newkey[5]; + u8 buffer[17]; +- struct sensor_device_attribute_2 *sensor_attr = +- to_sensor_dev_attr_2(attr); + +- newkey[0] = FAN_POSITION[0]; +- newkey[1] = '0' + sensor_attr->index; +- newkey[2] = FAN_POSITION[2]; +- newkey[3] = FAN_POSITION[3]; +- newkey[4] = 0; +- +- mutex_lock(&applesmc_lock); ++ sprintf(newkey, FAN_ID_FMT, to_index(attr)); + + ret = applesmc_read_key(newkey, buffer, 16); + buffer[16] = 0; + +- mutex_unlock(&applesmc_lock); + if (ret) + return ret; + else +@@ -852,18 +878,14 @@ static ssize_t applesmc_calibrate_show(struct device *dev, + static ssize_t applesmc_calibrate_store(struct device *dev, + struct device_attribute *attr, const char *sysfsbuf, size_t count) + { +- mutex_lock(&applesmc_lock); + applesmc_calibrate(); +- mutex_unlock(&applesmc_lock); + + return count; + } + + static void applesmc_backlight_set(struct work_struct *work) + { +- mutex_lock(&applesmc_lock); + applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2); +- mutex_unlock(&applesmc_lock); + } + static DECLARE_WORK(backlight_work, &applesmc_backlight_set); + +@@ -886,13 +908,10 @@ static ssize_t applesmc_key_count_show(struct device *dev, + u8 buffer[4]; + u32 count; + +- mutex_lock(&applesmc_lock); +- + ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4); + count = ((u32)buffer[0]<<24) + ((u32)buffer[1]<<16) + + ((u32)buffer[2]<<8) + buffer[3]; + +- mutex_unlock(&applesmc_lock); + if (ret) + return ret; + else +@@ -902,113 +921,53 @@ static ssize_t applesmc_key_count_show(struct device *dev, + static ssize_t applesmc_key_at_index_read_show(struct device *dev, + struct device_attribute *attr, char *sysfsbuf) + { +- char key[5]; +- char info[6]; ++ const struct applesmc_entry *entry; + int ret; + +- mutex_lock(&applesmc_lock); +- +- ret = applesmc_get_key_at_index(key_at_index, key); +- +- if (ret || !key[0]) { +- mutex_unlock(&applesmc_lock); +- +- return -EINVAL; +- } +- +- ret = applesmc_get_key_type(key, info); +- +- if (ret) { +- mutex_unlock(&applesmc_lock); +- ++ entry = applesmc_get_entry_by_index(key_at_index); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); ++ ret = applesmc_read_entry(entry, sysfsbuf, entry->len); ++ if (ret) + return ret; +- } +- +- /* +- * info[0] maximum value (APPLESMC_MAX_DATA_LENGTH) is much lower than +- * PAGE_SIZE, so we don't need any checks before writing to sysfsbuf. +- */ +- ret = applesmc_read_key(key, sysfsbuf, info[0]); +- +- mutex_unlock(&applesmc_lock); + +- if (!ret) { +- return info[0]; +- } else { +- return ret; +- } ++ return entry->len; + } + + static ssize_t applesmc_key_at_index_data_length_show(struct device *dev, + struct device_attribute *attr, char *sysfsbuf) + { +- char key[5]; +- char info[6]; +- int ret; +- +- mutex_lock(&applesmc_lock); ++ const struct applesmc_entry *entry; + +- ret = applesmc_get_key_at_index(key_at_index, key); ++ entry = applesmc_get_entry_by_index(key_at_index); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); + +- if (ret || !key[0]) { +- mutex_unlock(&applesmc_lock); +- +- return -EINVAL; +- } +- +- ret = applesmc_get_key_type(key, info); +- +- mutex_unlock(&applesmc_lock); +- +- if (!ret) +- return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", info[0]); +- else +- return ret; ++ return snprintf(sysfsbuf, PAGE_SIZE, "%d\n", entry->len); + } + + static ssize_t applesmc_key_at_index_type_show(struct device *dev, + struct device_attribute *attr, char *sysfsbuf) + { +- char key[5]; +- char info[6]; +- int ret; ++ const struct applesmc_entry *entry; + +- mutex_lock(&applesmc_lock); ++ entry = applesmc_get_entry_by_index(key_at_index); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); + +- ret = applesmc_get_key_at_index(key_at_index, key); +- +- if (ret || !key[0]) { +- mutex_unlock(&applesmc_lock); +- +- return -EINVAL; +- } +- +- ret = applesmc_get_key_type(key, info); +- +- mutex_unlock(&applesmc_lock); +- +- if (!ret) +- return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", info+1); +- else +- return ret; ++ return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->type); + } + + static ssize_t applesmc_key_at_index_name_show(struct device *dev, + struct device_attribute *attr, char *sysfsbuf) + { +- char key[5]; +- int ret; +- +- mutex_lock(&applesmc_lock); +- +- ret = applesmc_get_key_at_index(key_at_index, key); ++ const struct applesmc_entry *entry; + +- mutex_unlock(&applesmc_lock); ++ entry = applesmc_get_entry_by_index(key_at_index); ++ if (IS_ERR(entry)) ++ return PTR_ERR(entry); + +- if (!ret && key[0]) +- return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", key); +- else +- return -EINVAL; ++ return snprintf(sysfsbuf, PAGE_SIZE, "%s\n", entry->key); + } + + static ssize_t applesmc_key_at_index_show(struct device *dev, +@@ -1020,12 +979,8 @@ static ssize_t applesmc_key_at_index_show(struct device *dev, + static ssize_t applesmc_key_at_index_store(struct device *dev, + struct device_attribute *attr, const char *sysfsbuf, size_t count) + { +- mutex_lock(&applesmc_lock); +- + key_at_index = simple_strtoul(sysfsbuf, NULL, 10); + +- mutex_unlock(&applesmc_lock); +- + return count; + } + +@@ -1035,387 +990,101 @@ static struct led_classdev applesmc_backlight = { + .brightness_set = applesmc_brightness_set, + }; + +-static DEVICE_ATTR(name, 0444, applesmc_name_show, NULL); +- +-static DEVICE_ATTR(position, 0444, applesmc_position_show, NULL); +-static DEVICE_ATTR(calibrate, 0644, +- applesmc_calibrate_show, applesmc_calibrate_store); +- +-static struct attribute *accelerometer_attributes[] = { +- &dev_attr_position.attr, +- &dev_attr_calibrate.attr, +- NULL +-}; +- +-static const struct attribute_group accelerometer_attributes_group = +- { .attrs = accelerometer_attributes }; +- +-static DEVICE_ATTR(light, 0444, applesmc_light_show, NULL); +- +-static DEVICE_ATTR(key_count, 0444, applesmc_key_count_show, NULL); +-static DEVICE_ATTR(key_at_index, 0644, +- applesmc_key_at_index_show, applesmc_key_at_index_store); +-static DEVICE_ATTR(key_at_index_name, 0444, +- applesmc_key_at_index_name_show, NULL); +-static DEVICE_ATTR(key_at_index_type, 0444, +- applesmc_key_at_index_type_show, NULL); +-static DEVICE_ATTR(key_at_index_data_length, 0444, +- applesmc_key_at_index_data_length_show, NULL); +-static DEVICE_ATTR(key_at_index_data, 0444, +- applesmc_key_at_index_read_show, NULL); +- +-static struct attribute *key_enumeration_attributes[] = { +- &dev_attr_key_count.attr, +- &dev_attr_key_at_index.attr, +- &dev_attr_key_at_index_name.attr, +- &dev_attr_key_at_index_type.attr, +- &dev_attr_key_at_index_data_length.attr, +- &dev_attr_key_at_index_data.attr, +- NULL +-}; +- +-static const struct attribute_group key_enumeration_group = +- { .attrs = key_enumeration_attributes }; +- +-/* +- * Macro defining SENSOR_DEVICE_ATTR for a fan sysfs entries. +- * - show actual speed +- * - show/store minimum speed +- * - show maximum speed +- * - show safe speed +- * - show/store target speed +- * - show/store manual mode +- */ +-#define sysfs_fan_speeds_offset(offset) \ +-static SENSOR_DEVICE_ATTR_2(fan##offset##_input, S_IRUGO, \ +- applesmc_show_fan_speed, NULL, 0, offset-1); \ +-\ +-static SENSOR_DEVICE_ATTR_2(fan##offset##_min, S_IRUGO | S_IWUSR, \ +- applesmc_show_fan_speed, applesmc_store_fan_speed, 1, offset-1); \ +-\ +-static SENSOR_DEVICE_ATTR_2(fan##offset##_max, S_IRUGO, \ +- applesmc_show_fan_speed, NULL, 2, offset-1); \ +-\ +-static SENSOR_DEVICE_ATTR_2(fan##offset##_safe, S_IRUGO, \ +- applesmc_show_fan_speed, NULL, 3, offset-1); \ +-\ +-static SENSOR_DEVICE_ATTR_2(fan##offset##_output, S_IRUGO | S_IWUSR, \ +- applesmc_show_fan_speed, applesmc_store_fan_speed, 4, offset-1); \ +-\ +-static SENSOR_DEVICE_ATTR(fan##offset##_manual, S_IRUGO | S_IWUSR, \ +- applesmc_show_fan_manual, applesmc_store_fan_manual, offset-1); \ +-\ +-static SENSOR_DEVICE_ATTR(fan##offset##_label, S_IRUGO, \ +- applesmc_show_fan_position, NULL, offset-1); \ +-\ +-static struct attribute *fan##offset##_attributes[] = { \ +- &sensor_dev_attr_fan##offset##_input.dev_attr.attr, \ +- &sensor_dev_attr_fan##offset##_min.dev_attr.attr, \ +- &sensor_dev_attr_fan##offset##_max.dev_attr.attr, \ +- &sensor_dev_attr_fan##offset##_safe.dev_attr.attr, \ +- &sensor_dev_attr_fan##offset##_output.dev_attr.attr, \ +- &sensor_dev_attr_fan##offset##_manual.dev_attr.attr, \ +- &sensor_dev_attr_fan##offset##_label.dev_attr.attr, \ +- NULL \ ++static struct applesmc_node_group info_group[] = { ++ { "name", applesmc_name_show }, ++ { "key_count", applesmc_key_count_show }, ++ { "key_at_index", applesmc_key_at_index_show, applesmc_key_at_index_store }, ++ { "key_at_index_name", applesmc_key_at_index_name_show }, ++ { "key_at_index_type", applesmc_key_at_index_type_show }, ++ { "key_at_index_data_length", applesmc_key_at_index_data_length_show }, ++ { "key_at_index_data", applesmc_key_at_index_read_show }, ++ { } + }; + +-/* +- * Create the needed functions for each fan using the macro defined above +- * (4 fans are supported) +- */ +-sysfs_fan_speeds_offset(1); +-sysfs_fan_speeds_offset(2); +-sysfs_fan_speeds_offset(3); +-sysfs_fan_speeds_offset(4); +- +-static const struct attribute_group fan_attribute_groups[] = { +- { .attrs = fan1_attributes }, +- { .attrs = fan2_attributes }, +- { .attrs = fan3_attributes }, +- { .attrs = fan4_attributes }, ++static struct applesmc_node_group accelerometer_group[] = { ++ { "position", applesmc_position_show }, ++ { "calibrate", applesmc_calibrate_show, applesmc_calibrate_store }, ++ { } + }; + +-/* +- * Temperature sensors sysfs entries. +- */ +-static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 0); +-static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 1); +-static SENSOR_DEVICE_ATTR(temp3_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 2); +-static SENSOR_DEVICE_ATTR(temp4_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 3); +-static SENSOR_DEVICE_ATTR(temp5_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 4); +-static SENSOR_DEVICE_ATTR(temp6_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 5); +-static SENSOR_DEVICE_ATTR(temp7_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 6); +-static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 7); +-static SENSOR_DEVICE_ATTR(temp9_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 8); +-static SENSOR_DEVICE_ATTR(temp10_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 9); +-static SENSOR_DEVICE_ATTR(temp11_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 10); +-static SENSOR_DEVICE_ATTR(temp12_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 11); +-static SENSOR_DEVICE_ATTR(temp13_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 12); +-static SENSOR_DEVICE_ATTR(temp14_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 13); +-static SENSOR_DEVICE_ATTR(temp15_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 14); +-static SENSOR_DEVICE_ATTR(temp16_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 15); +-static SENSOR_DEVICE_ATTR(temp17_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 16); +-static SENSOR_DEVICE_ATTR(temp18_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 17); +-static SENSOR_DEVICE_ATTR(temp19_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 18); +-static SENSOR_DEVICE_ATTR(temp20_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 19); +-static SENSOR_DEVICE_ATTR(temp21_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 20); +-static SENSOR_DEVICE_ATTR(temp22_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 21); +-static SENSOR_DEVICE_ATTR(temp23_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 22); +-static SENSOR_DEVICE_ATTR(temp24_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 23); +-static SENSOR_DEVICE_ATTR(temp25_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 24); +-static SENSOR_DEVICE_ATTR(temp26_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 25); +-static SENSOR_DEVICE_ATTR(temp27_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 26); +-static SENSOR_DEVICE_ATTR(temp28_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 27); +-static SENSOR_DEVICE_ATTR(temp29_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 28); +-static SENSOR_DEVICE_ATTR(temp30_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 29); +-static SENSOR_DEVICE_ATTR(temp31_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 30); +-static SENSOR_DEVICE_ATTR(temp32_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 31); +-static SENSOR_DEVICE_ATTR(temp33_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 32); +-static SENSOR_DEVICE_ATTR(temp34_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 33); +-static SENSOR_DEVICE_ATTR(temp35_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 34); +-static SENSOR_DEVICE_ATTR(temp36_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 35); +-static SENSOR_DEVICE_ATTR(temp37_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 36); +-static SENSOR_DEVICE_ATTR(temp38_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 37); +-static SENSOR_DEVICE_ATTR(temp39_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 38); +-static SENSOR_DEVICE_ATTR(temp40_label, S_IRUGO, +- applesmc_show_sensor_label, NULL, 39); +-static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, +- applesmc_show_temperature, NULL, 0); +-static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, +- applesmc_show_temperature, NULL, 1); +-static SENSOR_DEVICE_ATTR(temp3_input, S_IRUGO, +- applesmc_show_temperature, NULL, 2); +-static SENSOR_DEVICE_ATTR(temp4_input, S_IRUGO, +- applesmc_show_temperature, NULL, 3); +-static SENSOR_DEVICE_ATTR(temp5_input, S_IRUGO, +- applesmc_show_temperature, NULL, 4); +-static SENSOR_DEVICE_ATTR(temp6_input, S_IRUGO, +- applesmc_show_temperature, NULL, 5); +-static SENSOR_DEVICE_ATTR(temp7_input, S_IRUGO, +- applesmc_show_temperature, NULL, 6); +-static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, +- applesmc_show_temperature, NULL, 7); +-static SENSOR_DEVICE_ATTR(temp9_input, S_IRUGO, +- applesmc_show_temperature, NULL, 8); +-static SENSOR_DEVICE_ATTR(temp10_input, S_IRUGO, +- applesmc_show_temperature, NULL, 9); +-static SENSOR_DEVICE_ATTR(temp11_input, S_IRUGO, +- applesmc_show_temperature, NULL, 10); +-static SENSOR_DEVICE_ATTR(temp12_input, S_IRUGO, +- applesmc_show_temperature, NULL, 11); +-static SENSOR_DEVICE_ATTR(temp13_input, S_IRUGO, +- applesmc_show_temperature, NULL, 12); +-static SENSOR_DEVICE_ATTR(temp14_input, S_IRUGO, +- applesmc_show_temperature, NULL, 13); +-static SENSOR_DEVICE_ATTR(temp15_input, S_IRUGO, +- applesmc_show_temperature, NULL, 14); +-static SENSOR_DEVICE_ATTR(temp16_input, S_IRUGO, +- applesmc_show_temperature, NULL, 15); +-static SENSOR_DEVICE_ATTR(temp17_input, S_IRUGO, +- applesmc_show_temperature, NULL, 16); +-static SENSOR_DEVICE_ATTR(temp18_input, S_IRUGO, +- applesmc_show_temperature, NULL, 17); +-static SENSOR_DEVICE_ATTR(temp19_input, S_IRUGO, +- applesmc_show_temperature, NULL, 18); +-static SENSOR_DEVICE_ATTR(temp20_input, S_IRUGO, +- applesmc_show_temperature, NULL, 19); +-static SENSOR_DEVICE_ATTR(temp21_input, S_IRUGO, +- applesmc_show_temperature, NULL, 20); +-static SENSOR_DEVICE_ATTR(temp22_input, S_IRUGO, +- applesmc_show_temperature, NULL, 21); +-static SENSOR_DEVICE_ATTR(temp23_input, S_IRUGO, +- applesmc_show_temperature, NULL, 22); +-static SENSOR_DEVICE_ATTR(temp24_input, S_IRUGO, +- applesmc_show_temperature, NULL, 23); +-static SENSOR_DEVICE_ATTR(temp25_input, S_IRUGO, +- applesmc_show_temperature, NULL, 24); +-static SENSOR_DEVICE_ATTR(temp26_input, S_IRUGO, +- applesmc_show_temperature, NULL, 25); +-static SENSOR_DEVICE_ATTR(temp27_input, S_IRUGO, +- applesmc_show_temperature, NULL, 26); +-static SENSOR_DEVICE_ATTR(temp28_input, S_IRUGO, +- applesmc_show_temperature, NULL, 27); +-static SENSOR_DEVICE_ATTR(temp29_input, S_IRUGO, +- applesmc_show_temperature, NULL, 28); +-static SENSOR_DEVICE_ATTR(temp30_input, S_IRUGO, +- applesmc_show_temperature, NULL, 29); +-static SENSOR_DEVICE_ATTR(temp31_input, S_IRUGO, +- applesmc_show_temperature, NULL, 30); +-static SENSOR_DEVICE_ATTR(temp32_input, S_IRUGO, +- applesmc_show_temperature, NULL, 31); +-static SENSOR_DEVICE_ATTR(temp33_input, S_IRUGO, +- applesmc_show_temperature, NULL, 32); +-static SENSOR_DEVICE_ATTR(temp34_input, S_IRUGO, +- applesmc_show_temperature, NULL, 33); +-static SENSOR_DEVICE_ATTR(temp35_input, S_IRUGO, +- applesmc_show_temperature, NULL, 34); +-static SENSOR_DEVICE_ATTR(temp36_input, S_IRUGO, +- applesmc_show_temperature, NULL, 35); +-static SENSOR_DEVICE_ATTR(temp37_input, S_IRUGO, +- applesmc_show_temperature, NULL, 36); +-static SENSOR_DEVICE_ATTR(temp38_input, S_IRUGO, +- applesmc_show_temperature, NULL, 37); +-static SENSOR_DEVICE_ATTR(temp39_input, S_IRUGO, +- applesmc_show_temperature, NULL, 38); +-static SENSOR_DEVICE_ATTR(temp40_input, S_IRUGO, +- applesmc_show_temperature, NULL, 39); +- +-static struct attribute *label_attributes[] = { +- &sensor_dev_attr_temp1_label.dev_attr.attr, +- &sensor_dev_attr_temp2_label.dev_attr.attr, +- &sensor_dev_attr_temp3_label.dev_attr.attr, +- &sensor_dev_attr_temp4_label.dev_attr.attr, +- &sensor_dev_attr_temp5_label.dev_attr.attr, +- &sensor_dev_attr_temp6_label.dev_attr.attr, +- &sensor_dev_attr_temp7_label.dev_attr.attr, +- &sensor_dev_attr_temp8_label.dev_attr.attr, +- &sensor_dev_attr_temp9_label.dev_attr.attr, +- &sensor_dev_attr_temp10_label.dev_attr.attr, +- &sensor_dev_attr_temp11_label.dev_attr.attr, +- &sensor_dev_attr_temp12_label.dev_attr.attr, +- &sensor_dev_attr_temp13_label.dev_attr.attr, +- &sensor_dev_attr_temp14_label.dev_attr.attr, +- &sensor_dev_attr_temp15_label.dev_attr.attr, +- &sensor_dev_attr_temp16_label.dev_attr.attr, +- &sensor_dev_attr_temp17_label.dev_attr.attr, +- &sensor_dev_attr_temp18_label.dev_attr.attr, +- &sensor_dev_attr_temp19_label.dev_attr.attr, +- &sensor_dev_attr_temp20_label.dev_attr.attr, +- &sensor_dev_attr_temp21_label.dev_attr.attr, +- &sensor_dev_attr_temp22_label.dev_attr.attr, +- &sensor_dev_attr_temp23_label.dev_attr.attr, +- &sensor_dev_attr_temp24_label.dev_attr.attr, +- &sensor_dev_attr_temp25_label.dev_attr.attr, +- &sensor_dev_attr_temp26_label.dev_attr.attr, +- &sensor_dev_attr_temp27_label.dev_attr.attr, +- &sensor_dev_attr_temp28_label.dev_attr.attr, +- &sensor_dev_attr_temp29_label.dev_attr.attr, +- &sensor_dev_attr_temp30_label.dev_attr.attr, +- &sensor_dev_attr_temp31_label.dev_attr.attr, +- &sensor_dev_attr_temp32_label.dev_attr.attr, +- &sensor_dev_attr_temp33_label.dev_attr.attr, +- &sensor_dev_attr_temp34_label.dev_attr.attr, +- &sensor_dev_attr_temp35_label.dev_attr.attr, +- &sensor_dev_attr_temp36_label.dev_attr.attr, +- &sensor_dev_attr_temp37_label.dev_attr.attr, +- &sensor_dev_attr_temp38_label.dev_attr.attr, +- &sensor_dev_attr_temp39_label.dev_attr.attr, +- &sensor_dev_attr_temp40_label.dev_attr.attr, +- NULL ++static struct applesmc_node_group light_sensor_group[] = { ++ { "light", applesmc_light_show }, ++ { } + }; + +-static struct attribute *temperature_attributes[] = { +- &sensor_dev_attr_temp1_input.dev_attr.attr, +- &sensor_dev_attr_temp2_input.dev_attr.attr, +- &sensor_dev_attr_temp3_input.dev_attr.attr, +- &sensor_dev_attr_temp4_input.dev_attr.attr, +- &sensor_dev_attr_temp5_input.dev_attr.attr, +- &sensor_dev_attr_temp6_input.dev_attr.attr, +- &sensor_dev_attr_temp7_input.dev_attr.attr, +- &sensor_dev_attr_temp8_input.dev_attr.attr, +- &sensor_dev_attr_temp9_input.dev_attr.attr, +- &sensor_dev_attr_temp10_input.dev_attr.attr, +- &sensor_dev_attr_temp11_input.dev_attr.attr, +- &sensor_dev_attr_temp12_input.dev_attr.attr, +- &sensor_dev_attr_temp13_input.dev_attr.attr, +- &sensor_dev_attr_temp14_input.dev_attr.attr, +- &sensor_dev_attr_temp15_input.dev_attr.attr, +- &sensor_dev_attr_temp16_input.dev_attr.attr, +- &sensor_dev_attr_temp17_input.dev_attr.attr, +- &sensor_dev_attr_temp18_input.dev_attr.attr, +- &sensor_dev_attr_temp19_input.dev_attr.attr, +- &sensor_dev_attr_temp20_input.dev_attr.attr, +- &sensor_dev_attr_temp21_input.dev_attr.attr, +- &sensor_dev_attr_temp22_input.dev_attr.attr, +- &sensor_dev_attr_temp23_input.dev_attr.attr, +- &sensor_dev_attr_temp24_input.dev_attr.attr, +- &sensor_dev_attr_temp25_input.dev_attr.attr, +- &sensor_dev_attr_temp26_input.dev_attr.attr, +- &sensor_dev_attr_temp27_input.dev_attr.attr, +- &sensor_dev_attr_temp28_input.dev_attr.attr, +- &sensor_dev_attr_temp29_input.dev_attr.attr, +- &sensor_dev_attr_temp30_input.dev_attr.attr, +- &sensor_dev_attr_temp31_input.dev_attr.attr, +- &sensor_dev_attr_temp32_input.dev_attr.attr, +- &sensor_dev_attr_temp33_input.dev_attr.attr, +- &sensor_dev_attr_temp34_input.dev_attr.attr, +- &sensor_dev_attr_temp35_input.dev_attr.attr, +- &sensor_dev_attr_temp36_input.dev_attr.attr, +- &sensor_dev_attr_temp37_input.dev_attr.attr, +- &sensor_dev_attr_temp38_input.dev_attr.attr, +- &sensor_dev_attr_temp39_input.dev_attr.attr, +- &sensor_dev_attr_temp40_input.dev_attr.attr, +- NULL ++static struct applesmc_node_group fan_group[] = { ++ { "fan%d_label", applesmc_show_fan_position }, ++ { "fan%d_input", applesmc_show_fan_speed, NULL, 0 }, ++ { "fan%d_min", applesmc_show_fan_speed, applesmc_store_fan_speed, 1 }, ++ { "fan%d_max", applesmc_show_fan_speed, NULL, 2 }, ++ { "fan%d_safe", applesmc_show_fan_speed, NULL, 3 }, ++ { "fan%d_output", applesmc_show_fan_speed, applesmc_store_fan_speed, 4 }, ++ { "fan%d_manual", applesmc_show_fan_manual, applesmc_store_fan_manual }, ++ { } + }; + +-static const struct attribute_group temperature_attributes_group = +- { .attrs = temperature_attributes }; +- +-static const struct attribute_group label_attributes_group = { +- .attrs = label_attributes ++static struct applesmc_node_group temp_group[] = { ++ { "temp%d_label", applesmc_show_sensor_label }, ++ { "temp%d_input", applesmc_show_temperature }, ++ { } + }; + + /* Module stuff */ + + /* +- * applesmc_dmi_match - found a match. return one, short-circuiting the hunt. ++ * applesmc_destroy_nodes - remove files and free associated memory + */ +-static int applesmc_dmi_match(const struct dmi_system_id *id) ++static void applesmc_destroy_nodes(struct applesmc_node_group *groups) + { +- int i = 0; +- struct dmi_match_data* dmi_data = id->driver_data; +- printk(KERN_INFO "applesmc: %s detected:\n", id->ident); +- applesmc_accelerometer = dmi_data->accelerometer; +- printk(KERN_INFO "applesmc: - Model %s accelerometer\n", +- applesmc_accelerometer ? "with" : "without"); +- applesmc_light = dmi_data->light; +- printk(KERN_INFO "applesmc: - Model %s light sensors and backlight\n", +- applesmc_light ? "with" : "without"); +- +- applesmc_temperature_set = dmi_data->temperature_set; +- while (temperature_sensors_sets[applesmc_temperature_set][i] != NULL) +- i++; +- printk(KERN_INFO "applesmc: - Model with %d temperature sensors\n", i); +- return 1; ++ struct applesmc_node_group *grp; ++ struct applesmc_dev_attr *node; ++ ++ for (grp = groups; grp->nodes; grp++) { ++ for (node = grp->nodes; node->sda.dev_attr.attr.name; node++) ++ sysfs_remove_file(&pdev->dev.kobj, ++ &node->sda.dev_attr.attr); ++ kfree(grp->nodes); ++ grp->nodes = NULL; ++ } ++} ++ ++/* ++ * applesmc_create_nodes - create a two-dimensional group of sysfs files ++ */ ++static int applesmc_create_nodes(struct applesmc_node_group *groups, int num) ++{ ++ struct applesmc_node_group *grp; ++ struct applesmc_dev_attr *node; ++ struct attribute *attr; ++ int ret, i; ++ ++ for (grp = groups; grp->format; grp++) { ++ grp->nodes = kcalloc(num + 1, sizeof(*node), GFP_KERNEL); ++ if (!grp->nodes) { ++ ret = -ENOMEM; ++ goto out; ++ } ++ for (i = 0; i < num; i++) { ++ node = &grp->nodes[i]; ++ sprintf(node->name, grp->format, i + 1); ++ node->sda.index = (grp->option << 16) | (i & 0xffff); ++ node->sda.dev_attr.show = grp->show; ++ node->sda.dev_attr.store = grp->store; ++ attr = &node->sda.dev_attr.attr; ++ attr->name = node->name; ++ attr->mode = S_IRUGO | (grp->store ? S_IWUSR : 0); ++ ret = sysfs_create_file(&pdev->dev.kobj, attr); ++ if (ret) { ++ attr->name = NULL; ++ goto out; ++ } ++ } ++ } ++ ++ return 0; ++out: ++ applesmc_destroy_nodes(groups); ++ return ret; + } + + /* Create accelerometer ressources */ +@@ -1424,8 +1093,10 @@ static int applesmc_create_accelerometer(void) + struct input_dev *idev; + int ret; + +- ret = sysfs_create_group(&pdev->dev.kobj, +- &accelerometer_attributes_group); ++ if (!smcreg.has_accelerometer) ++ return 0; ++ ++ ret = applesmc_create_nodes(accelerometer_group, 1); + if (ret) + goto out; + +@@ -1462,282 +1133,132 @@ out_idev: + input_free_polled_device(applesmc_idev); + + out_sysfs: +- sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group); ++ applesmc_destroy_nodes(accelerometer_group); + + out: +- printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret); ++ pr_warn("driver init failed (ret=%d)!\n", ret); + return ret; + } + + /* Release all ressources used by the accelerometer */ + static void applesmc_release_accelerometer(void) + { ++ if (!smcreg.has_accelerometer) ++ return; + input_unregister_polled_device(applesmc_idev); + input_free_polled_device(applesmc_idev); +- sysfs_remove_group(&pdev->dev.kobj, &accelerometer_attributes_group); ++ applesmc_destroy_nodes(accelerometer_group); + } + +-static __initdata struct dmi_match_data applesmc_dmi_data[] = { +-/* MacBook Pro: accelerometer, backlight and temperature set 0 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 0 }, +-/* MacBook2: accelerometer and temperature set 1 */ +- { .accelerometer = 1, .light = 0, .temperature_set = 1 }, +-/* MacBook: accelerometer and temperature set 2 */ +- { .accelerometer = 1, .light = 0, .temperature_set = 2 }, +-/* MacMini: temperature set 3 */ +- { .accelerometer = 0, .light = 0, .temperature_set = 3 }, +-/* MacPro: temperature set 4 */ +- { .accelerometer = 0, .light = 0, .temperature_set = 4 }, +-/* iMac: temperature set 5 */ +- { .accelerometer = 0, .light = 0, .temperature_set = 5 }, +-/* MacBook3, MacBook4: accelerometer and temperature set 6 */ +- { .accelerometer = 1, .light = 0, .temperature_set = 6 }, +-/* MacBook Air: accelerometer, backlight and temperature set 7 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 7 }, +-/* MacBook Pro 4: accelerometer, backlight and temperature set 8 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 8 }, +-/* MacBook Pro 3: accelerometer, backlight and temperature set 9 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 9 }, +-/* iMac 5: light sensor only, temperature set 10 */ +- { .accelerometer = 0, .light = 0, .temperature_set = 10 }, +-/* MacBook 5: accelerometer, backlight and temperature set 11 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 11 }, +-/* MacBook Pro 5: accelerometer, backlight and temperature set 12 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 12 }, +-/* iMac 8: light sensor only, temperature set 13 */ +- { .accelerometer = 0, .light = 0, .temperature_set = 13 }, +-/* iMac 6: light sensor only, temperature set 14 */ +- { .accelerometer = 0, .light = 0, .temperature_set = 14 }, +-/* MacBook Air 2,1: accelerometer, backlight and temperature set 15 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 15 }, +-/* MacPro3,1: temperature set 16 */ +- { .accelerometer = 0, .light = 0, .temperature_set = 16 }, +-/* iMac 9,1: light sensor only, temperature set 17 */ +- { .accelerometer = 0, .light = 0, .temperature_set = 17 }, +-/* MacBook Pro 2,2: accelerometer, backlight and temperature set 18 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 18 }, +-/* MacBook Pro 5,3: accelerometer, backlight and temperature set 19 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 19 }, +-/* MacBook Pro 5,4: accelerometer, backlight and temperature set 20 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 20 }, +-/* MacBook Pro 6,2: accelerometer, backlight and temperature set 21 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 21 }, +-/* MacBook Pro 7,1: accelerometer, backlight and temperature set 22 */ +- { .accelerometer = 1, .light = 1, .temperature_set = 22 }, +-}; ++static int applesmc_create_light_sensor(void) ++{ ++ if (!smcreg.num_light_sensors) ++ return 0; ++ return applesmc_create_nodes(light_sensor_group, 1); ++} + +-/* Note that DMI_MATCH(...,"MacBook") will match "MacBookPro1,1". +- * So we need to put "Apple MacBook Pro" before "Apple MacBook". */ +-static __initdata struct dmi_system_id applesmc_whitelist[] = { +- { applesmc_dmi_match, "Apple MacBook Air 2", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir2") }, +- &applesmc_dmi_data[15]}, +- { applesmc_dmi_match, "Apple MacBook Air", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookAir") }, +- &applesmc_dmi_data[7]}, +- { applesmc_dmi_match, "Apple MacBook Pro 7", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro7") }, +- &applesmc_dmi_data[22]}, +- { applesmc_dmi_match, "Apple MacBook Pro 5,4", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,4") }, +- &applesmc_dmi_data[20]}, +- { applesmc_dmi_match, "Apple MacBook Pro 5,3", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5,3") }, +- &applesmc_dmi_data[19]}, +- { applesmc_dmi_match, "Apple MacBook Pro 6", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro6") }, +- &applesmc_dmi_data[21]}, +- { applesmc_dmi_match, "Apple MacBook Pro 5", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5") }, +- &applesmc_dmi_data[12]}, +- { applesmc_dmi_match, "Apple MacBook Pro 4", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro4") }, +- &applesmc_dmi_data[8]}, +- { applesmc_dmi_match, "Apple MacBook Pro 3", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro3") }, +- &applesmc_dmi_data[9]}, +- { applesmc_dmi_match, "Apple MacBook Pro 2,2", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple Computer, Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro2,2") }, +- &applesmc_dmi_data[18]}, +- { applesmc_dmi_match, "Apple MacBook Pro", { +- DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME,"MacBookPro") }, +- &applesmc_dmi_data[0]}, +- { applesmc_dmi_match, "Apple MacBook (v2)", { +- DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME,"MacBook2") }, +- &applesmc_dmi_data[1]}, +- { applesmc_dmi_match, "Apple MacBook (v3)", { +- DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME,"MacBook3") }, +- &applesmc_dmi_data[6]}, +- { applesmc_dmi_match, "Apple MacBook 4", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook4") }, +- &applesmc_dmi_data[6]}, +- { applesmc_dmi_match, "Apple MacBook 5", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5") }, +- &applesmc_dmi_data[11]}, +- { applesmc_dmi_match, "Apple MacBook", { +- DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME,"MacBook") }, +- &applesmc_dmi_data[2]}, +- { applesmc_dmi_match, "Apple Macmini", { +- DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME,"Macmini") }, +- &applesmc_dmi_data[3]}, +- { applesmc_dmi_match, "Apple MacPro2", { +- DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME,"MacPro2") }, +- &applesmc_dmi_data[4]}, +- { applesmc_dmi_match, "Apple MacPro3", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacPro3") }, +- &applesmc_dmi_data[16]}, +- { applesmc_dmi_match, "Apple MacPro", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "MacPro") }, +- &applesmc_dmi_data[4]}, +- { applesmc_dmi_match, "Apple iMac 9,1", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple Inc."), +- DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1") }, +- &applesmc_dmi_data[17]}, +- { applesmc_dmi_match, "Apple iMac 8", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "iMac8") }, +- &applesmc_dmi_data[13]}, +- { applesmc_dmi_match, "Apple iMac 6", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "iMac6") }, +- &applesmc_dmi_data[14]}, +- { applesmc_dmi_match, "Apple iMac 5", { +- DMI_MATCH(DMI_BOARD_VENDOR, "Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME, "iMac5") }, +- &applesmc_dmi_data[10]}, +- { applesmc_dmi_match, "Apple iMac", { +- DMI_MATCH(DMI_BOARD_VENDOR,"Apple"), +- DMI_MATCH(DMI_PRODUCT_NAME,"iMac") }, +- &applesmc_dmi_data[5]}, +- { .ident = NULL } +-}; ++static void applesmc_release_light_sensor(void) ++{ ++ if (!smcreg.num_light_sensors) ++ return; ++ applesmc_destroy_nodes(light_sensor_group); ++} + +-static int __init applesmc_init(void) ++static int applesmc_create_key_backlight(void) ++{ ++ if (!smcreg.has_key_backlight) ++ return 0; ++ applesmc_led_wq = create_singlethread_workqueue("applesmc-led"); ++ if (!applesmc_led_wq) ++ return -ENOMEM; ++ return led_classdev_register(&pdev->dev, &applesmc_backlight); ++} ++ ++static void applesmc_release_key_backlight(void) ++{ ++ if (!smcreg.has_key_backlight) ++ return; ++ led_classdev_unregister(&applesmc_backlight); ++ destroy_workqueue(applesmc_led_wq); ++} ++ ++static void applesmc_set_interrupt(u8 enable) ++{ ++ applesmc_write_key(NOTIFICATION_KEY, &enable, 1); ++} ++ ++static irqreturn_t applesmc_interrupt(int irq, void *dev) ++{ ++ printk("SMC Interrupt\n"); ++ return IRQ_HANDLED; ++} ++ ++static int __devinit applesmc_pnp_probe(struct pnp_dev *dev, ++ const struct pnp_device_id *dev_id) + { + int ret; +- int count; +- int i; ++ struct resource *res; ++ struct applesmc_pnp_device *applesmc_pnp_device; + +- if (!dmi_check_system(applesmc_whitelist)) { +- printk(KERN_WARNING "applesmc: supported laptop not found!\n"); +- ret = -ENODEV; ++ pdev = dev; ++ ++ applesmc_pnp_device = kzalloc(sizeof(struct applesmc_pnp_device), ++ GFP_KERNEL); ++ ++ if (!applesmc_pnp_device) { ++ ret = -ENOMEM; + goto out; + } + +- if (!request_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS, +- "applesmc")) { ++ pnp_device = applesmc_pnp_device; ++ ++ pnp_set_drvdata(dev, applesmc_pnp_device); ++ ++ res = pnp_get_resource(dev, IORESOURCE_IO, 0); ++ ++ if (!res) { + ret = -ENXIO; + goto out; + } + +- ret = platform_driver_register(&applesmc_driver); +- if (ret) +- goto out_region; ++ applesmc_pnp_device->iobase = res->start; ++ applesmc_pnp_device->iolen = res->end - res->start + 1; + +- pdev = platform_device_register_simple("applesmc", APPLESMC_DATA_PORT, +- NULL, 0); +- if (IS_ERR(pdev)) { +- ret = PTR_ERR(pdev); +- goto out_driver; ++ if (!request_region(pnp_device->iobase, pnp_device->iolen, "applesmc")) { ++ ret = -ENXIO; ++ goto out; + } + +- ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_name.attr); ++ /* create register cache */ ++ ret = applesmc_init_smcreg(); + if (ret) +- goto out_device; +- +- /* Create key enumeration sysfs files */ +- ret = sysfs_create_group(&pdev->dev.kobj, &key_enumeration_group); +- if (ret) +- goto out_name; +- +- /* create fan files */ +- count = applesmc_get_fan_count(); +- if (count < 0) +- printk(KERN_ERR "applesmc: Cannot get the number of fans.\n"); +- else +- printk(KERN_INFO "applesmc: %d fans found.\n", count); ++ goto out_region; + +- if (count > 4) { +- count = 4; +- printk(KERN_WARNING "applesmc: More than 4 fans found," +- " but at most 4 fans are supported" +- " by the driver.\n"); +- } ++ applesmc_device_init(); + +- while (fans_handled < count) { +- ret = sysfs_create_group(&pdev->dev.kobj, +- &fan_attribute_groups[fans_handled]); +- if (ret) +- goto out_fans; +- fans_handled++; +- } ++ ret = applesmc_create_nodes(info_group, 1); ++ if (ret) ++ goto out_smcreg; + +- for (i = 0; +- temperature_sensors_sets[applesmc_temperature_set][i] != NULL; +- i++) { +- if (temperature_attributes[i] == NULL || +- label_attributes[i] == NULL) { +- printk(KERN_ERR "applesmc: More temperature sensors " +- "in temperature_sensors_sets (at least %i)" +- "than available sysfs files in " +- "temperature_attributes (%i), please report " +- "this bug.\n", i, i-1); +- goto out_temperature; +- } +- ret = sysfs_create_file(&pdev->dev.kobj, +- temperature_attributes[i]); +- if (ret) +- goto out_temperature; +- ret = sysfs_create_file(&pdev->dev.kobj, +- label_attributes[i]); +- if (ret) +- goto out_temperature; +- } ++ ret = applesmc_create_nodes(fan_group, smcreg.fan_count); ++ if (ret) ++ goto out_info; + +- if (applesmc_accelerometer) { +- ret = applesmc_create_accelerometer(); +- if (ret) +- goto out_temperature; +- } ++ ret = applesmc_create_nodes(temp_group, smcreg.temp_count); ++ if (ret) ++ goto out_fans; + +- if (applesmc_light) { +- /* Add light sensor file */ +- ret = sysfs_create_file(&pdev->dev.kobj, &dev_attr_light.attr); +- if (ret) +- goto out_accelerometer; ++ ret = applesmc_create_accelerometer(); ++ if (ret) ++ goto out_temperature; + +- /* Create the workqueue */ +- applesmc_led_wq = create_singlethread_workqueue("applesmc-led"); +- if (!applesmc_led_wq) { +- ret = -ENOMEM; +- goto out_light_sysfs; +- } ++ ret = applesmc_create_light_sensor(); ++ if (ret) ++ goto out_accelerometer; + +- /* register as a led device */ +- ret = led_classdev_register(&pdev->dev, &applesmc_backlight); +- if (ret < 0) +- goto out_light_wq; +- } ++ ret = applesmc_create_key_backlight(); ++ if (ret) ++ goto out_light_sysfs; + + hwmon_dev = hwmon_device_register(&pdev->dev); + if (IS_ERR(hwmon_dev)) { +@@ -1745,65 +1266,85 @@ static int __init applesmc_init(void) + goto out_light_ledclass; + } + +- printk(KERN_INFO "applesmc: driver successfully loaded.\n"); ++ res = pnp_get_resource(dev, IORESOURCE_IRQ, 0); ++ ++ if (res) { ++ applesmc_pnp_device->irq = res->start; ++ ret = request_irq(res->start, applesmc_interrupt, IRQF_SHARED, ++ "applesmc", dev); ++ if (ret) ++ applesmc_pnp_device->irq = 0; ++ else ++ applesmc_set_interrupt(1); ++ } + + return 0; + + out_light_ledclass: +- if (applesmc_light) +- led_classdev_unregister(&applesmc_backlight); +-out_light_wq: +- if (applesmc_light) +- destroy_workqueue(applesmc_led_wq); ++ applesmc_release_key_backlight(); + out_light_sysfs: +- if (applesmc_light) +- sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr); ++ applesmc_release_light_sensor(); + out_accelerometer: +- if (applesmc_accelerometer) +- applesmc_release_accelerometer(); ++ applesmc_release_accelerometer(); + out_temperature: +- sysfs_remove_group(&pdev->dev.kobj, &label_attributes_group); +- sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group); ++ applesmc_destroy_nodes(temp_group); + out_fans: +- while (fans_handled) +- sysfs_remove_group(&pdev->dev.kobj, +- &fan_attribute_groups[--fans_handled]); +- sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group); +-out_name: +- sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr); +-out_device: +- platform_device_unregister(pdev); +-out_driver: +- platform_driver_unregister(&applesmc_driver); ++ applesmc_destroy_nodes(fan_group); ++out_info: ++ applesmc_destroy_nodes(info_group); ++out_smcreg: ++ applesmc_destroy_smcreg(); + out_region: +- release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); ++ release_region(pnp_device->iobase, pnp_device->iolen); + out: +- printk(KERN_WARNING "applesmc: driver init failed (ret=%d)!\n", ret); ++ pr_warn("driver init failed (ret=%d)!\n", ret); + return ret; + } + +-static void __exit applesmc_exit(void) ++static void __devexit applesmc_pnp_remove(struct pnp_dev *dev) + { +- hwmon_device_unregister(hwmon_dev); +- if (applesmc_light) { +- led_classdev_unregister(&applesmc_backlight); +- destroy_workqueue(applesmc_led_wq); +- sysfs_remove_file(&pdev->dev.kobj, &dev_attr_light.attr); ++ struct applesmc_pnp_device *applesmc_pnp_device = pnp_get_drvdata(dev); ++ ++ if (applesmc_pnp_device->irq) { ++ applesmc_set_interrupt(0); ++ free_irq(applesmc_pnp_device->irq, dev); + } +- if (applesmc_accelerometer) +- applesmc_release_accelerometer(); +- sysfs_remove_group(&pdev->dev.kobj, &label_attributes_group); +- sysfs_remove_group(&pdev->dev.kobj, &temperature_attributes_group); +- while (fans_handled) +- sysfs_remove_group(&pdev->dev.kobj, +- &fan_attribute_groups[--fans_handled]); +- sysfs_remove_group(&pdev->dev.kobj, &key_enumeration_group); +- sysfs_remove_file(&pdev->dev.kobj, &dev_attr_name.attr); +- platform_device_unregister(pdev); +- platform_driver_unregister(&applesmc_driver); +- release_region(APPLESMC_DATA_PORT, APPLESMC_NR_PORTS); +- +- printk(KERN_INFO "applesmc: driver unloaded.\n"); ++ ++ hwmon_device_unregister(hwmon_dev); ++ applesmc_release_key_backlight(); ++ applesmc_release_light_sensor(); ++ applesmc_release_accelerometer(); ++ applesmc_destroy_nodes(temp_group); ++ applesmc_destroy_nodes(fan_group); ++ applesmc_destroy_nodes(info_group); ++ applesmc_destroy_smcreg(); ++ release_region(pnp_device->iobase, pnp_device->iolen); ++ kfree(applesmc_pnp_device); ++} ++ ++static const struct pnp_device_id applesmc_dev_table[] = { ++ {"APP0001", 0}, ++ {"", 0}, ++}; ++ ++static struct pnp_driver applesmc_pnp_driver = { ++ .name = "Apple SMC", ++ .probe = applesmc_pnp_probe, ++ .remove = applesmc_pnp_remove, ++ .id_table = applesmc_dev_table, ++ .driver = { ++ .pm = &applesmc_pm_ops, ++ }, ++}; ++ ++static int __init applesmc_init(void) ++{ ++ return pnp_register_driver(&applesmc_pnp_driver); ++} ++ ++static void __exit applesmc_exit(void) ++{ ++ pnp_unregister_driver(&applesmc_pnp_driver); + } + + module_init(applesmc_init); +@@ -1812,4 +1353,4 @@ module_exit(applesmc_exit); + MODULE_AUTHOR("Nicolas Boichat"); + MODULE_DESCRIPTION("Apple SMC"); + MODULE_LICENSE("GPL v2"); +-MODULE_DEVICE_TABLE(dmi, applesmc_whitelist); ++MODULE_DEVICE_TABLE(pnp, applesmc_dev_table); diff --git a/config-x86-generic b/config-x86-generic index 68bd39b..d47bd0a 100644 --- a/config-x86-generic +++ b/config-x86-generic @@ -431,7 +431,7 @@ CONFIG_SYSPROF_TRACER=y CONFIG_HP_ILO=m -CONFIG_BACKLIGHT_MBP_NVIDIA=m +CONFIG_BACKLIGHT_APPLE=m CONFIG_OPROFILE_IBS=y CONFIG_MICROCODE_INTEL=y diff --git a/config-x86_64-generic b/config-x86_64-generic index b09e511..19ed749 100644 --- a/config-x86_64-generic +++ b/config-x86_64-generic @@ -342,7 +342,7 @@ CONFIG_SYSPROF_TRACER=y CONFIG_X86_MPPARSE=y -CONFIG_BACKLIGHT_MBP_NVIDIA=m +CONFIG_BACKLIGHT_APPLE=m CONFIG_OPROFILE_IBS=y CONFIG_MICROCODE_INTEL=y diff --git a/efi_default_physical.patch b/efi_default_physical.patch new file mode 100644 index 0000000..6e46560 --- /dev/null +++ b/efi_default_physical.patch @@ -0,0 +1,403 @@ +Default to physical mode in EFI. Fixes boot problems on some machines, +upstream will probably head in this direction. + +diff --git a/arch/x86/include/asm/efi.h b/arch/x86/include/asm/efi.h +index 8e4a165..3c62f15 100644 +--- a/arch/x86/include/asm/efi.h ++++ b/arch/x86/include/asm/efi.h +@@ -93,6 +93,9 @@ extern int add_efi_memmap; + extern void efi_memblock_x86_reserve_range(void); + extern void efi_call_phys_prelog(void); + extern void efi_call_phys_epilog(void); ++extern void efi_call_phys_prelog_in_physmode(void); ++extern void efi_call_phys_epilog_in_physmode(void); ++extern void efi_pagetable_init(void); + + #ifndef CONFIG_EFI + /* +diff --git a/arch/x86/platform/efi/efi.c b/arch/x86/platform/efi/efi.c +index 0fe27d7..e1158b0 100644 +--- a/arch/x86/platform/efi/efi.c ++++ b/arch/x86/platform/efi/efi.c +@@ -58,6 +58,7 @@ struct efi_memory_map memmap; + + static struct efi efi_phys __initdata; + static efi_system_table_t efi_systab __initdata; ++static efi_runtime_services_t phys_runtime; + + static int __init setup_noefi(char *arg) + { +@@ -172,7 +173,7 @@ static efi_status_t __init phys_efi_set_virtual_address_map( + return status; + } + +-static efi_status_t __init phys_efi_get_time(efi_time_t *tm, ++static efi_status_t __init phys_efi_get_time_early(efi_time_t *tm, + efi_time_cap_t *tc) + { + efi_status_t status; +@@ -183,6 +184,112 @@ static efi_status_t __init phys_efi_get_time(efi_time_t *tm, + return status; + } + ++static efi_status_t phys_efi_get_time(efi_time_t *tm, ++ efi_time_cap_t *tc) ++{ ++ efi_status_t status; ++ ++ efi_call_phys_prelog_in_physmode(); ++ status = efi_call_phys2((void*)phys_runtime.get_time, tm, tc); ++ efi_call_phys_epilog_in_physmode(); ++ return status; ++} ++ ++static efi_status_t __init phys_efi_set_time(efi_time_t *tm) ++{ ++ efi_status_t status; ++ ++ efi_call_phys_prelog_in_physmode(); ++ status = efi_call_phys1((void*)phys_runtime.set_time, tm); ++ efi_call_phys_epilog_in_physmode(); ++ return status; ++} ++ ++static efi_status_t phys_efi_get_wakeup_time(efi_bool_t *enabled, ++ efi_bool_t *pending, ++ efi_time_t *tm) ++{ ++ efi_status_t status; ++ ++ efi_call_phys_prelog_in_physmode(); ++ status = efi_call_phys3((void*)phys_runtime.get_wakeup_time, enabled, ++ pending, tm); ++ efi_call_phys_epilog_in_physmode(); ++ return status; ++} ++ ++static efi_status_t phys_efi_set_wakeup_time(efi_bool_t enabled, efi_time_t *tm) ++{ ++ efi_status_t status; ++ efi_call_phys_prelog_in_physmode(); ++ status = efi_call_phys2((void*)phys_runtime.set_wakeup_time, enabled, ++ tm); ++ efi_call_phys_epilog_in_physmode(); ++ return status; ++} ++ ++static efi_status_t phys_efi_get_variable(efi_char16_t *name, ++ efi_guid_t *vendor, ++ u32 *attr, ++ unsigned long *data_size, ++ void *data) ++{ ++ efi_status_t status; ++ efi_call_phys_prelog_in_physmode(); ++ status = efi_call_phys5((void*)phys_runtime.get_variable, name, vendor, ++ attr, data_size, data); ++ efi_call_phys_epilog_in_physmode(); ++ return status; ++} ++ ++static efi_status_t phys_efi_get_next_variable(unsigned long *name_size, ++ efi_char16_t *name, ++ efi_guid_t *vendor) ++{ ++ efi_status_t status; ++ ++ efi_call_phys_prelog_in_physmode(); ++ status = efi_call_phys3((void*)phys_runtime.get_next_variable, ++ name_size, name, vendor); ++ efi_call_phys_epilog_in_physmode(); ++ return status; ++} ++ ++static efi_status_t phys_efi_set_variable(efi_char16_t *name, ++ efi_guid_t *vendor, ++ unsigned long attr, ++ unsigned long data_size, ++ void *data) ++{ ++ efi_status_t status; ++ efi_call_phys_prelog_in_physmode(); ++ status = efi_call_phys5((void*)phys_runtime.set_variable, name, ++ vendor, attr, data_size, data); ++ efi_call_phys_epilog_in_physmode(); ++ return status; ++} ++ ++static efi_status_t phys_efi_get_next_high_mono_count(u32 *count) ++{ ++ efi_status_t status; ++ efi_call_phys_prelog_in_physmode(); ++ status = efi_call_phys1((void*)phys_runtime.get_next_high_mono_count, ++ count); ++ efi_call_phys_epilog_in_physmode(); ++ return status; ++} ++ ++static void phys_efi_reset_system(int reset_type, ++ efi_status_t status, ++ unsigned long data_size, ++ efi_char16_t *data) ++{ ++ efi_call_phys_prelog_in_physmode(); ++ efi_call_phys4((void*)phys_runtime.reset_system, reset_type, status, ++ data_size, data); ++ efi_call_phys_epilog_in_physmode(); ++} ++ + int efi_set_rtc_mmss(unsigned long nowtime) + { + int real_seconds, real_minutes; +@@ -435,7 +542,9 @@ void __init efi_init(void) + * Make efi_get_time can be called before entering + * virtual mode. + */ +- efi.get_time = phys_efi_get_time; ++ efi.get_time = phys_efi_get_time_early; ++ ++ memcpy(&phys_runtime, runtime, sizeof(efi_runtime_services_t)); + } else + printk(KERN_ERR "Could not map the EFI runtime service " + "table!\n"); +@@ -466,6 +575,14 @@ void __init efi_init(void) + #if EFI_DEBUG + print_efi_memmap(); + #endif ++ ++#ifndef CONFIG_X86_64 ++ /* ++ * Only x86_64 supports physical mode as of now. Use virtual mode ++ * forcibly. ++ */ ++ usevirtefi = 1; ++#endif + } + + static void __init runtime_code_page_mkexec(void) +@@ -579,6 +696,27 @@ void __init efi_enter_virtual_mode(void) + memmap.map = NULL; + } + ++void __init efi_setup_physical_mode(void) ++{ ++#ifdef CONFIG_X86_64 ++ efi_pagetable_init(); ++#endif ++ efi.get_time = phys_efi_get_time; ++ efi.set_time = phys_efi_set_time; ++ efi.get_wakeup_time = phys_efi_get_wakeup_time; ++ efi.set_wakeup_time = phys_efi_set_wakeup_time; ++ efi.get_variable = phys_efi_get_variable; ++ efi.get_next_variable = phys_efi_get_next_variable; ++ efi.set_variable = phys_efi_set_variable; ++ efi.get_next_high_mono_count = ++ phys_efi_get_next_high_mono_count; ++ efi.reset_system = phys_efi_reset_system; ++ efi.set_virtual_address_map = NULL; /* Not needed */ ++ ++ early_iounmap(memmap.map, memmap.nr_map * memmap.desc_size); ++ memmap.map = NULL; ++} ++ + /* + * Convenience functions to obtain memory types and attributes + */ +diff --git a/arch/x86/platform/efi/efi_32.c b/arch/x86/platform/efi/efi_32.c +index 5cab48e..90767b1 100644 +--- a/arch/x86/platform/efi/efi_32.c ++++ b/arch/x86/platform/efi/efi_32.c +@@ -110,3 +110,7 @@ void efi_call_phys_epilog(void) + + local_irq_restore(efi_rt_eflags); + } ++ ++void efi_call_phys_prelog_in_physmode(void) { /* Not supported */ } ++void efi_call_phys_epilog_in_physmode(void) { /* Not supported */ } ++ +diff --git a/arch/x86/platform/efi/efi_64.c b/arch/x86/platform/efi/efi_64.c +index ac0621a..33a8192 100644 +--- a/arch/x86/platform/efi/efi_64.c ++++ b/arch/x86/platform/efi/efi_64.c +@@ -39,7 +39,9 @@ + #include + + static pgd_t save_pgd __initdata; +-static unsigned long efi_flags __initdata; ++static DEFINE_PER_CPU(unsigned long, efi_flags); ++static DEFINE_PER_CPU(unsigned long, save_cr3); ++static pgd_t efi_pgd[PTRS_PER_PGD] __page_aligned_bss; + + static void __init early_mapping_set_exec(unsigned long start, + unsigned long end, +@@ -80,7 +82,7 @@ void __init efi_call_phys_prelog(void) + unsigned long vaddress; + + early_runtime_code_mapping_set_exec(1); +- local_irq_save(efi_flags); ++ local_irq_save(get_cpu_var(efi_flags)); + vaddress = (unsigned long)__va(0x0UL); + save_pgd = *pgd_offset_k(0x0UL); + set_pgd(pgd_offset_k(0x0UL), *pgd_offset_k(vaddress)); +@@ -94,10 +96,23 @@ void __init efi_call_phys_epilog(void) + */ + set_pgd(pgd_offset_k(0x0UL), save_pgd); + __flush_tlb_all(); +- local_irq_restore(efi_flags); ++ local_irq_restore(get_cpu_var(efi_flags)); + early_runtime_code_mapping_set_exec(0); + } + ++void efi_call_phys_prelog_in_physmode(void) ++{ ++ local_irq_save(get_cpu_var(efi_flags)); ++ get_cpu_var(save_cr3)= read_cr3(); ++ write_cr3(virt_to_phys(efi_pgd)); ++} ++ ++void efi_call_phys_epilog_in_physmode(void) ++{ ++ write_cr3(get_cpu_var(save_cr3)); ++ local_irq_restore(get_cpu_var(efi_flags)); ++} ++ + void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size, + u32 type) + { +@@ -112,3 +127,78 @@ void __iomem *__init efi_ioremap(unsigned long phys_addr, unsigned long size, + + return (void __iomem *)__va(phys_addr); + } ++ ++static pud_t *fill_pud(pgd_t *pgd, unsigned long vaddr) ++{ ++ if (pgd_none(*pgd)) { ++ pud_t *pud = (pud_t *)get_zeroed_page(GFP_ATOMIC); ++ set_pgd(pgd, __pgd(_PAGE_TABLE | __pa(pud))); ++ if (pud != pud_offset(pgd, 0)) ++ printk(KERN_ERR "EFI PAGETABLE BUG #00! %p <-> %p\n", ++ pud, pud_offset(pgd, 0)); ++ } ++ return pud_offset(pgd, vaddr); ++} ++ ++static pmd_t *fill_pmd(pud_t *pud, unsigned long vaddr) ++{ ++ if (pud_none(*pud)) { ++ pmd_t *pmd = (pmd_t *)get_zeroed_page(GFP_ATOMIC); ++ set_pud(pud, __pud(_PAGE_TABLE | __pa(pmd))); ++ if (pmd != pmd_offset(pud, 0)) ++ printk(KERN_ERR "EFI PAGETABLE BUG #01! %p <-> %p\n", ++ pmd, pmd_offset(pud, 0)); ++ } ++ return pmd_offset(pud, vaddr); ++} ++ ++static pte_t *fill_pte(pmd_t *pmd, unsigned long vaddr) ++{ ++ if (pmd_none(*pmd)) { ++ pte_t *pte = (pte_t *)get_zeroed_page(GFP_ATOMIC); ++ set_pmd(pmd, __pmd(_PAGE_TABLE | __pa(pte))); ++ if (pte != pte_offset_kernel(pmd, 0)) ++ printk(KERN_ERR "EFI PAGETABLE BUG #02!\n"); ++ } ++ return pte_offset_kernel(pmd, vaddr); ++} ++ ++void __init efi_pagetable_init(void) ++{ ++ efi_memory_desc_t *md; ++ unsigned long size; ++ u64 start_pfn, end_pfn, pfn, vaddr; ++ void *p; ++ pgd_t *pgd; ++ pud_t *pud; ++ pmd_t *pmd; ++ pte_t *pte; ++ ++ memset(efi_pgd, 0, sizeof(efi_pgd)); ++ for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) { ++ md = p; ++ if ((md->type != EFI_RUNTIME_SERVICES_CODE) && ++ (md->type != EFI_RUNTIME_SERVICES_DATA)) ++ continue; ++ ++ start_pfn = md->phys_addr >> PAGE_SHIFT; ++ size = md->num_pages << EFI_PAGE_SHIFT; ++ end_pfn = PFN_UP(md->phys_addr + size); ++ ++ for (pfn = start_pfn; pfn <= end_pfn; pfn++) { ++ vaddr = pfn << PAGE_SHIFT; ++ pgd = efi_pgd + pgd_index(vaddr); ++ pud = fill_pud(pgd, vaddr); ++ pmd = fill_pmd(pud, vaddr); ++ pte = fill_pte(pmd, vaddr); ++ if (md->type == EFI_RUNTIME_SERVICES_CODE) ++ set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC)); ++ else ++ set_pte(pte, pfn_pte(pfn, PAGE_KERNEL)); ++ } ++ } ++ pgd = efi_pgd + pgd_index(PAGE_OFFSET); ++ set_pgd(pgd, *pgd_offset_k(PAGE_OFFSET)); ++ pgd = efi_pgd + pgd_index(__START_KERNEL_map); ++ set_pgd(pgd, *pgd_offset_k(__START_KERNEL_map)); ++} +diff --git a/include/linux/efi.h b/include/linux/efi.h +index fb737bc..c4e310e 100644 +--- a/include/linux/efi.h ++++ b/include/linux/efi.h +@@ -290,6 +290,7 @@ extern void efi_map_pal_code (void); + extern void efi_memmap_walk (efi_freemem_callback_t callback, void *arg); + extern void efi_gettimeofday (struct timespec *ts); + extern void efi_enter_virtual_mode (void); /* switch EFI to virtual mode, if possible */ ++extern void efi_setup_physical_mode(void); + extern u64 efi_get_iobase (void); + extern u32 efi_mem_type (unsigned long phys_addr); + extern u64 efi_mem_attributes (unsigned long phys_addr); +diff --git a/include/linux/init.h b/include/linux/init.h +index 577671c..2f1b28f 100644 +--- a/include/linux/init.h ++++ b/include/linux/init.h +@@ -149,6 +149,7 @@ extern int do_one_initcall(initcall_t fn); + extern char __initdata boot_command_line[]; + extern char *saved_command_line; + extern unsigned int reset_devices; ++extern unsigned int usevirtefi; + + /* used by init/main.c */ + void setup_arch(char **); +diff --git a/init/main.c b/init/main.c +index 8646401..726025e 100644 +--- a/init/main.c ++++ b/init/main.c +@@ -196,6 +196,14 @@ static int __init set_reset_devices(char *str) + + __setup("reset_devices", set_reset_devices); + ++unsigned int usevirtefi; ++static int __init set_virt_efi(char *str) ++{ ++ usevirtefi = 1; ++ return 1; ++} ++__setup("virtefi", set_virt_efi); ++ + static const char * argv_init[MAX_INIT_ARGS+2] = { "init", NULL, }; + const char * envp_init[MAX_INIT_ENVS+2] = { "HOME=/", "TERM=linux", NULL, }; + static const char *panic_later, *panic_param; +@@ -668,8 +676,12 @@ asmlinkage void __init start_kernel(void) + pidmap_init(); + anon_vma_init(); + #ifdef CONFIG_X86 +- if (efi_enabled) +- efi_enter_virtual_mode(); ++ if (efi_enabled) { ++ if (usevirtefi) ++ efi_enter_virtual_mode(); ++ else ++ efi_setup_physical_mode(); ++ } + #endif + thread_info_cache_init(); + cred_init(); diff --git a/efifb_update.patch b/efifb_update.patch new file mode 100644 index 0000000..7058ba6 --- /dev/null +++ b/efifb_update.patch @@ -0,0 +1,217 @@ +Fix up efifb so it works properly on the 11" Macbook Air. Upstream in .38? + +diff --git a/drivers/video/efifb.c b/drivers/video/efifb.c +index 70477c2..476ffb1 100644 +--- a/drivers/video/efifb.c ++++ b/drivers/video/efifb.c +@@ -53,6 +53,7 @@ enum { + M_MB_7_1, /* MacBook, 7th rev. */ + M_MB_SR, /* MacBook, 2nd gen, (Santa Rosa) */ + M_MBA, /* MacBook Air */ ++ M_MBA_3, /* Macbook Air, 3rd rev */ + M_MBP, /* MacBook Pro */ + M_MBP_2, /* MacBook Pro 2nd gen */ + M_MBP_2_2, /* MacBook Pro 2,2nd gen */ +@@ -67,40 +68,49 @@ enum { + M_UNKNOWN /* placeholder */ + }; + ++#define OVERRIDE_NONE 0x0 ++#define OVERRIDE_BASE 0x1 ++#define OVERRIDE_STRIDE 0x2 ++#define OVERRIDE_HEIGHT 0x4 ++#define OVERRIDE_WIDTH 0x8 ++ + static struct efifb_dmi_info { + char *optname; + unsigned long base; + int stride; + int width; + int height; ++ int flags; + } dmi_list[] __initdata = { +- [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900 }, +- [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050 }, /* guess */ +- [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050 }, +- [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200 }, /* guess */ +- [M_I24_8_1] = { "imac8", 0xc0060000, 2048 * 4, 1920, 1200 }, +- [M_I24_10_1] = { "imac10", 0xc0010000, 2048 * 4, 1920, 1080 }, +- [M_I27_11_1] = { "imac11", 0xc0010000, 2560 * 4, 2560, 1440 }, +- [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768 }, +- [M_MINI_3_1] = { "mini31", 0x40010000, 1024 * 4, 1024, 768 }, +- [M_MINI_4_1] = { "mini41", 0xc0010000, 2048 * 4, 1920, 1200 }, +- [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800 }, +- [M_MB_5_1] = { "macbook51", 0x80010000, 2048 * 4, 1280, 800 }, +- [M_MB_6_1] = { "macbook61", 0x80010000, 2048 * 4, 1280, 800 }, +- [M_MB_7_1] = { "macbook71", 0x80010000, 2048 * 4, 1280, 800 }, +- [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800 }, +- [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900 }, +- [M_MBP_2] = { "mbp2", 0, 0, 0, 0 }, /* placeholder */ +- [M_MBP_2_2] = { "mbp22", 0x80010000, 1472 * 4, 1440, 900 }, +- [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900 }, +- [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200 }, +- [M_MBP_5_1] = { "mbp51", 0xc0010000, 2048 * 4, 1440, 900 }, +- [M_MBP_5_2] = { "mbp52", 0xc0010000, 2048 * 4, 1920, 1200 }, +- [M_MBP_5_3] = { "mbp53", 0xd0010000, 2048 * 4, 1440, 900 }, +- [M_MBP_6_1] = { "mbp61", 0x90030000, 2048 * 4, 1920, 1200 }, +- [M_MBP_6_2] = { "mbp62", 0x90030000, 2048 * 4, 1680, 1050 }, +- [M_MBP_7_1] = { "mbp71", 0xc0010000, 2048 * 4, 1280, 800 }, +- [M_UNKNOWN] = { NULL, 0, 0, 0, 0 } ++ [M_I17] = { "i17", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE }, ++ [M_I20] = { "i20", 0x80010000, 1728 * 4, 1680, 1050, OVERRIDE_NONE }, /* guess */ ++ [M_I20_SR] = { "imac7", 0x40010000, 1728 * 4, 1680, 1050, OVERRIDE_NONE }, ++ [M_I24] = { "i24", 0x80010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, /* guess */ ++ [M_I24_8_1] = { "imac8", 0xc0060000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, ++ [M_I24_10_1] = { "imac10", 0xc0010000, 2048 * 4, 1920, 1080, OVERRIDE_NONE }, ++ [M_I27_11_1] = { "imac11", 0xc0010000, 2560 * 4, 2560, 1440, OVERRIDE_NONE }, ++ [M_MINI]= { "mini", 0x80000000, 2048 * 4, 1024, 768, OVERRIDE_NONE }, ++ [M_MINI_3_1] = { "mini31", 0x40010000, 1024 * 4, 1024, 768, OVERRIDE_NONE }, ++ [M_MINI_4_1] = { "mini41", 0xc0010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, ++ [M_MB] = { "macbook", 0x80000000, 2048 * 4, 1280, 800, OVERRIDE_NONE }, ++ [M_MB_5_1] = { "macbook51", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE }, ++ [M_MB_6_1] = { "macbook61", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE }, ++ [M_MB_7_1] = { "macbook71", 0x80010000, 2048 * 4, 1280, 800, OVERRIDE_NONE }, ++ [M_MBA] = { "mba", 0x80000000, 2048 * 4, 1280, 800, OVERRIDE_NONE }, ++ /* 11" Macbook Air 3,1 passes the wrong stride */ ++ [M_MBA_3] = { "mba3", 0, 2048 * 4, 0, 0, OVERRIDE_STRIDE }, ++ [M_MBP] = { "mbp", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE }, ++ [M_MBP_2] = { "mbp2", 0, 0, 0, 0, OVERRIDE_NONE }, /* placeholder */ ++ [M_MBP_2_2] = { "mbp22", 0x80010000, 1472 * 4, 1440, 900, OVERRIDE_NONE }, ++ [M_MBP_SR] = { "mbp3", 0x80030000, 2048 * 4, 1440, 900, OVERRIDE_NONE }, ++ [M_MBP_4] = { "mbp4", 0xc0060000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, ++ [M_MBP_5_1] = { "mbp51", 0xc0010000, 2048 * 4, 1440, 900, OVERRIDE_NONE }, ++ [M_MBP_5_2] = { "mbp52", 0xc0010000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, ++ [M_MBP_5_3] = { "mbp53", 0xd0010000, 2048 * 4, 1440, 900, OVERRIDE_NONE }, ++ [M_MBP_6_1] = { "mbp61", 0x90030000, 2048 * 4, 1920, 1200, OVERRIDE_NONE }, ++ [M_MBP_6_2] = { "mbp62", 0x90030000, 2048 * 4, 1680, 1050, OVERRIDE_NONE }, ++ [M_MBP_7_1] = { "mbp71", 0xc0010000, 2048 * 4, 1280, 800, OVERRIDE_NONE }, ++ [M_UNKNOWN] = { NULL, 0, 0, 0, 0, OVERRIDE_NONE } + }; + + static int set_system(const struct dmi_system_id *id); +@@ -138,6 +148,7 @@ static const struct dmi_system_id dmi_system_table[] __initconst = { + EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook6,1", M_MB_6_1), + EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBook7,1", M_MB_7_1), + EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir1,1", M_MBA), ++ EFIFB_DMI_SYSTEM_ID("Apple Inc.", "MacBookAir3,1", M_MBA_3), + EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro1,1", M_MBP), + EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,1", M_MBP_2), + EFIFB_DMI_SYSTEM_ID("Apple Computer, Inc.", "MacBookPro2,2", M_MBP_2_2), +@@ -154,16 +165,22 @@ static const struct dmi_system_id dmi_system_table[] __initconst = { + {}, + }; + ++#define choose_value(dmivalue, fwvalue, field, flags) ({ \ ++ typeof(fwvalue) _ret_ = fwvalue; \ ++ if ((flags) & (field)) \ ++ _ret_ = dmivalue; \ ++ else if ((fwvalue) == 0) \ ++ _ret_ = dmivalue; \ ++ _ret_; \ ++ }) ++ + static int set_system(const struct dmi_system_id *id) + { + struct efifb_dmi_info *info = id->driver_data; +- if (info->base == 0) +- return 0; + +- printk(KERN_INFO "efifb: dmi detected %s - framebuffer at %p " +- "(%dx%d, stride %d)\n", id->ident, +- (void *)info->base, info->width, info->height, +- info->stride); ++ if (info->base == 0 && info->height == 0 && info->width == 0 ++ && info->stride == 0) ++ return 0; + + /* Trust the bootloader over the DMI tables */ + if (screen_info.lfb_base == 0) { +@@ -171,40 +188,47 @@ static int set_system(const struct dmi_system_id *id) + struct pci_dev *dev = NULL; + int found_bar = 0; + #endif +- screen_info.lfb_base = info->base; ++ if (info->base) { ++ screen_info.lfb_base = choose_value(info->base, ++ screen_info.lfb_base, OVERRIDE_BASE, ++ info->flags); + + #if defined(CONFIG_PCI) +- /* make sure that the address in the table is actually on a +- * VGA device's PCI BAR */ +- +- for_each_pci_dev(dev) { +- int i; +- if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) +- continue; +- for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { +- resource_size_t start, end; +- +- start = pci_resource_start(dev, i); +- if (start == 0) +- break; +- end = pci_resource_end(dev, i); +- if (screen_info.lfb_base >= start && +- screen_info.lfb_base < end) { +- found_bar = 1; ++ /* make sure that the address in the table is actually ++ * on a VGA device's PCI BAR */ ++ ++ for_each_pci_dev(dev) { ++ int i; ++ if ((dev->class >> 8) != PCI_CLASS_DISPLAY_VGA) ++ continue; ++ for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { ++ resource_size_t start, end; ++ ++ start = pci_resource_start(dev, i); ++ if (start == 0) ++ break; ++ end = pci_resource_end(dev, i); ++ if (screen_info.lfb_base >= start && ++ screen_info.lfb_base < end) { ++ found_bar = 1; ++ } + } + } +- } +- if (!found_bar) +- screen_info.lfb_base = 0; ++ if (!found_bar) ++ screen_info.lfb_base = 0; + #endif ++ } + } + if (screen_info.lfb_base) { +- if (screen_info.lfb_linelength == 0) +- screen_info.lfb_linelength = info->stride; +- if (screen_info.lfb_width == 0) +- screen_info.lfb_width = info->width; +- if (screen_info.lfb_height == 0) +- screen_info.lfb_height = info->height; ++ screen_info.lfb_linelength = choose_value(info->stride, ++ screen_info.lfb_linelength, OVERRIDE_STRIDE, ++ info->flags); ++ screen_info.lfb_width = choose_value(info->width, ++ screen_info.lfb_width, OVERRIDE_WIDTH, ++ info->flags); ++ screen_info.lfb_height = choose_value(info->height, ++ screen_info.lfb_height, OVERRIDE_HEIGHT, ++ info->flags); + if (screen_info.orig_video_isVGA == 0) + screen_info.orig_video_isVGA = VIDEO_TYPE_EFI; + } else { +@@ -214,6 +238,13 @@ static int set_system(const struct dmi_system_id *id) + screen_info.orig_video_isVGA = 0; + return 0; + } ++ ++ printk(KERN_INFO "efifb: dmi detected %s - framebuffer at %p " ++ "(%dx%d, stride %d)\n", id->ident, ++ (void *)screen_info.lfb_base, screen_info.lfb_width, ++ screen_info.lfb_height, screen_info.lfb_linelength); ++ ++ + return 1; + } + diff --git a/kernel.spec b/kernel.spec index 5dca017..4f8945d 100644 --- a/kernel.spec +++ b/kernel.spec @@ -697,6 +697,12 @@ Patch12018: neuter_intel_microcode_load.patch Patch12030: tpm-fix-stall-on-boot.patch +Patch12100: applesmc_update.patch +Patch12101: apple_backlight.patch +Patch12102: efifb_update.patch +Patch12200: acpi_reboot.patch +Patch12210: efi_default_physical.patch + # Runtime power management Patch12203: linux-2.6-usb-pci-autosuspend.patch Patch12204: linux-2.6-enable-more-pci-autosuspend.patch @@ -1296,6 +1302,13 @@ ApplyPatch neuter_intel_microcode_load.patch # try to fix stalls during boot (#530393) ApplyPatch tpm-fix-stall-on-boot.patch +# various fixes for Apple and EFI +ApplyPatch applesmc_update.patch +ApplyPatch apple_backlight.patch +ApplyPatch efifb_update.patch +ApplyPatch acpi_reboot.patch +ApplyPatch efi_default_physical.patch + # Runtime PM ApplyPatch linux-2.6-usb-pci-autosuspend.patch ApplyPatch linux-2.6-enable-more-pci-autosuspend.patch @@ -1927,6 +1940,13 @@ fi # || || %changelog +* Thu Dec 16 2010 Matthew Garrett 2.6.37-0.rc6.git0.2 +- applesmc_update.patch: Make the driver more generic. Should help Apples. +- apple_backlight.patch: Make sure that this loads on all hardware. +- efifb_update.patch: Fixes for the 11 inch Macbook Air +- acpi_reboot.patch: Should make reboot work better on most hardware +- efi_default_physical.patch: Some machines dislike EFI virtual mode + * Wed Dec 15 2010 Kyle McMartin 2.6.37-0.rc6.git0.1 - Linux 2.6.37-rc6 - Re-activate acpi patch which rejected on the previous commit.