8de680c
Bugzilla: 958826
8de680c
Upstream-status: 3.13
8de680c
8de680c
From 4cc8a57425c623753b10b77b15392e5b83baa5a3 Mon Sep 17 00:00:00 2001
8de680c
From: Hans de Goede <hdegoede@redhat.com>
8de680c
Date: Sun, 17 Nov 2013 14:00:16 +0100
8de680c
Subject: [PATCH 1/2] Revert "dell-laptop: Remove rfkill code"
8de680c
8de680c
Without rfkill functionality in dell-laptop I have the following problems:
8de680c
-If the hardware radio switch is set to disable the radio, then userspace
8de680c
 will still think it can use wireless and bluetooth.
8de680c
-The wwan / 3g modem cannot be soft blocked without the dell-laptop rfkill
8de680c
 functionality
8de680c
8de680c
I know the rfkill functionality was removed from the dell-laptop driver because
8de680c
it caused more problems then it fixed, and the blacklist for it was growing out
8de680c
of control.
8de680c
8de680c
But in the thread discussing this Dell mentioned that they only QA the rfkill
8de680c
acpi interface on Latitudes and indeed there have been no blacklist entries
8de680c
for Latitudes. Therefor I would like to bring the rfkill functionality back
8de680c
only for Latitudes. This patch is a straight-forward revert. The next patch
8de680c
in this set will drop the blacklist and replace it with a Latitude check.
8de680c
8de680c
This reverts commit a6c2390cd6d2083d27a2359658e08f2d3df375ac.
8de680c
8de680c
Conflicts:
8de680c
	drivers/platform/x86/dell-laptop.c
8de680c
8de680c
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
8de680c
Signed-off-by: Matthew Garrett <matthew.garrett@nebula.com>
8de680c
---
8de680c
 drivers/platform/x86/dell-laptop.c | 289 +++++++++++++++++++++++++++++++++++++
8de680c
 1 file changed, 289 insertions(+)
8de680c
8de680c
diff --git a/drivers/platform/x86/dell-laptop.c b/drivers/platform/x86/dell-laptop.c
8de680c
index bb77e18..55f75a2 100644
8de680c
--- a/drivers/platform/x86/dell-laptop.c
8de680c
+++ b/drivers/platform/x86/dell-laptop.c
8de680c
@@ -21,6 +21,7 @@
8de680c
 #include <linux/err.h>
8de680c
 #include <linux/dmi.h>
8de680c
 #include <linux/io.h>
8de680c
+#include <linux/rfkill.h>
8de680c
 #include <linux/power_supply.h>
8de680c
 #include <linux/acpi.h>
8de680c
 #include <linux/mm.h>
8de680c
@@ -89,6 +90,9 @@ static struct platform_driver platform_driver = {
8de680c
 
8de680c
 static struct platform_device *platform_device;
8de680c
 static struct backlight_device *dell_backlight_device;
8de680c
+static struct rfkill *wifi_rfkill;
8de680c
+static struct rfkill *bluetooth_rfkill;
8de680c
+static struct rfkill *wwan_rfkill;
8de680c
 
8de680c
 static const struct dmi_system_id dell_device_table[] __initconst = {
8de680c
 	{
8de680c
@@ -115,6 +119,53 @@ static const struct dmi_system_id dell_device_table[] __initconst = {
8de680c
 };
8de680c
 MODULE_DEVICE_TABLE(dmi, dell_device_table);
8de680c
 
8de680c
+static struct dmi_system_id dell_blacklist[] = {
8de680c
+	/* Supported by compal-laptop */
8de680c
+	{
8de680c
+		.ident = "Dell Mini 9",
8de680c
+		.matches = {
8de680c
+			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
8de680c
+			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 910"),
8de680c
+		},
8de680c
+	},
8de680c
+	{
8de680c
+		.ident = "Dell Mini 10",
8de680c
+		.matches = {
8de680c
+			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
8de680c
+			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1010"),
8de680c
+		},
8de680c
+	},
8de680c
+	{
8de680c
+		.ident = "Dell Mini 10v",
8de680c
+		.matches = {
8de680c
+			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
8de680c
+			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1011"),
8de680c
+		},
8de680c
+	},
8de680c
+	{
8de680c
+		.ident = "Dell Mini 1012",
8de680c
+		.matches = {
8de680c
+			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
8de680c
+			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1012"),
8de680c
+		},
8de680c
+	},
8de680c
+	{
8de680c
+		.ident = "Dell Inspiron 11z",
8de680c
+		.matches = {
8de680c
+			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
8de680c
+			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1110"),
8de680c
+		},
8de680c
+	},
8de680c
+	{
8de680c
+		.ident = "Dell Mini 12",
8de680c
+		.matches = {
8de680c
+			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
8de680c
+			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 1210"),
8de680c
+		},
8de680c
+	},
8de680c
+	{}
8de680c
+};
8de680c
+
8de680c
 static struct dmi_system_id dell_quirks[] = {
8de680c
 	{
8de680c
 		.callback = dmi_matched,
8de680c
@@ -355,6 +406,94 @@ dell_send_request(struct calling_interface_buffer *buffer, int class,
8de680c
 	return buffer;
8de680c
 }
8de680c
 
8de680c
+/* Derived from information in DellWirelessCtl.cpp:
8de680c
+   Class 17, select 11 is radio control. It returns an array of 32-bit values.
8de680c
+
8de680c
+   Input byte 0 = 0: Wireless information
8de680c
+
8de680c
+   result[0]: return code
8de680c
+   result[1]:
8de680c
+     Bit 0:      Hardware switch supported
8de680c
+     Bit 1:      Wifi locator supported
8de680c
+     Bit 2:      Wifi is supported
8de680c
+     Bit 3:      Bluetooth is supported
8de680c
+     Bit 4:      WWAN is supported
8de680c
+     Bit 5:      Wireless keyboard supported
8de680c
+     Bits 6-7:   Reserved
8de680c
+     Bit 8:      Wifi is installed
8de680c
+     Bit 9:      Bluetooth is installed
8de680c
+     Bit 10:     WWAN is installed
8de680c
+     Bits 11-15: Reserved
8de680c
+     Bit 16:     Hardware switch is on
8de680c
+     Bit 17:     Wifi is blocked
8de680c
+     Bit 18:     Bluetooth is blocked
8de680c
+     Bit 19:     WWAN is blocked
8de680c
+     Bits 20-31: Reserved
8de680c
+   result[2]: NVRAM size in bytes
8de680c
+   result[3]: NVRAM format version number
8de680c
+
8de680c
+   Input byte 0 = 2: Wireless switch configuration
8de680c
+   result[0]: return code
8de680c
+   result[1]:
8de680c
+     Bit 0:      Wifi controlled by switch
8de680c
+     Bit 1:      Bluetooth controlled by switch
8de680c
+     Bit 2:      WWAN controlled by switch
8de680c
+     Bits 3-6:   Reserved
8de680c
+     Bit 7:      Wireless switch config locked
8de680c
+     Bit 8:      Wifi locator enabled
8de680c
+     Bits 9-14:  Reserved
8de680c
+     Bit 15:     Wifi locator setting locked
8de680c
+     Bits 16-31: Reserved
8de680c
+*/
8de680c
+
8de680c
+static int dell_rfkill_set(void *data, bool blocked)
8de680c
+{
8de680c
+	int disable = blocked ? 1 : 0;
8de680c
+	unsigned long radio = (unsigned long)data;
8de680c
+	int hwswitch_bit = (unsigned long)data - 1;
8de680c
+	int ret = 0;
8de680c
+
8de680c
+	get_buffer();
8de680c
+	dell_send_request(buffer, 17, 11);
8de680c
+
8de680c
+	/* If the hardware switch controls this radio, and the hardware
8de680c
+	   switch is disabled, don't allow changing the software state */
8de680c
+	if ((hwswitch_state & BIT(hwswitch_bit)) &&
8de680c
+	    !(buffer->output[1] & BIT(16))) {
8de680c
+		ret = -EINVAL;
8de680c
+		goto out;
8de680c
+	}
8de680c
+
8de680c
+	buffer->input[0] = (1 | (radio<<8) | (disable << 16));
8de680c
+	dell_send_request(buffer, 17, 11);
8de680c
+
8de680c
+out:
8de680c
+	release_buffer();
8de680c
+	return ret;
8de680c
+}
8de680c
+
8de680c
+static void dell_rfkill_query(struct rfkill *rfkill, void *data)
8de680c
+{
8de680c
+	int status;
8de680c
+	int bit = (unsigned long)data + 16;
8de680c
+	int hwswitch_bit = (unsigned long)data - 1;
8de680c
+
8de680c
+	get_buffer();
8de680c
+	dell_send_request(buffer, 17, 11);
8de680c
+	status = buffer->output[1];
8de680c
+	release_buffer();
8de680c
+
8de680c
+	rfkill_set_sw_state(rfkill, !!(status & BIT(bit)));
8de680c
+
8de680c
+	if (hwswitch_state & (BIT(hwswitch_bit)))
8de680c
+		rfkill_set_hw_state(rfkill, !(status & BIT(16)));
8de680c
+}
8de680c
+
8de680c
+static const struct rfkill_ops dell_rfkill_ops = {
8de680c
+	.set_block = dell_rfkill_set,
8de680c
+	.query = dell_rfkill_query,
8de680c
+};
8de680c
+
8de680c
 static struct dentry *dell_laptop_dir;
8de680c
 
8de680c
 static int dell_debugfs_show(struct seq_file *s, void *data)
8de680c
@@ -424,6 +563,108 @@ static const struct file_operations dell_debugfs_fops = {
8de680c
 	.release = single_release,
8de680c
 };
8de680c
 
8de680c
+static void dell_update_rfkill(struct work_struct *ignored)
8de680c
+{
8de680c
+	if (wifi_rfkill)
8de680c
+		dell_rfkill_query(wifi_rfkill, (void *)1);
8de680c
+	if (bluetooth_rfkill)
8de680c
+		dell_rfkill_query(bluetooth_rfkill, (void *)2);
8de680c
+	if (wwan_rfkill)
8de680c
+		dell_rfkill_query(wwan_rfkill, (void *)3);
8de680c
+}
8de680c
+static DECLARE_DELAYED_WORK(dell_rfkill_work, dell_update_rfkill);
8de680c
+
8de680c
+
8de680c
+static int __init dell_setup_rfkill(void)
8de680c
+{
8de680c
+	int status;
8de680c
+	int ret;
8de680c
+
8de680c
+	if (dmi_check_system(dell_blacklist)) {
8de680c
+		pr_info("Blacklisted hardware detected - not enabling rfkill\n");
8de680c
+		return 0;
8de680c
+	}
8de680c
+
8de680c
+	get_buffer();
8de680c
+	dell_send_request(buffer, 17, 11);
8de680c
+	status = buffer->output[1];
8de680c
+	buffer->input[0] = 0x2;
8de680c
+	dell_send_request(buffer, 17, 11);
8de680c
+	hwswitch_state = buffer->output[1];
8de680c
+	release_buffer();
8de680c
+
8de680c
+	if ((status & (1<<2|1<<8)) == (1<<2|1<<8)) {
8de680c
+		wifi_rfkill = rfkill_alloc("dell-wifi", &platform_device->dev,
8de680c
+					   RFKILL_TYPE_WLAN,
8de680c
+					   &dell_rfkill_ops, (void *) 1);
8de680c
+		if (!wifi_rfkill) {
8de680c
+			ret = -ENOMEM;
8de680c
+			goto err_wifi;
8de680c
+		}
8de680c
+		ret = rfkill_register(wifi_rfkill);
8de680c
+		if (ret)
8de680c
+			goto err_wifi;
8de680c
+	}
8de680c
+
8de680c
+	if ((status & (1<<3|1<<9)) == (1<<3|1<<9)) {
8de680c
+		bluetooth_rfkill = rfkill_alloc("dell-bluetooth",
8de680c
+						&platform_device->dev,
8de680c
+						RFKILL_TYPE_BLUETOOTH,
8de680c
+						&dell_rfkill_ops, (void *) 2);
8de680c
+		if (!bluetooth_rfkill) {
8de680c
+			ret = -ENOMEM;
8de680c
+			goto err_bluetooth;
8de680c
+		}
8de680c
+		ret = rfkill_register(bluetooth_rfkill);
8de680c
+		if (ret)
8de680c
+			goto err_bluetooth;
8de680c
+	}
8de680c
+
8de680c
+	if ((status & (1<<4|1<<10)) == (1<<4|1<<10)) {
8de680c
+		wwan_rfkill = rfkill_alloc("dell-wwan",
8de680c
+					   &platform_device->dev,
8de680c
+					   RFKILL_TYPE_WWAN,
8de680c
+					   &dell_rfkill_ops, (void *) 3);
8de680c
+		if (!wwan_rfkill) {
8de680c
+			ret = -ENOMEM;
8de680c
+			goto err_wwan;
8de680c
+		}
8de680c
+		ret = rfkill_register(wwan_rfkill);
8de680c
+		if (ret)
8de680c
+			goto err_wwan;
8de680c
+	}
8de680c
+
8de680c
+	return 0;
8de680c
+err_wwan:
8de680c
+	rfkill_destroy(wwan_rfkill);
8de680c
+	if (bluetooth_rfkill)
8de680c
+		rfkill_unregister(bluetooth_rfkill);
8de680c
+err_bluetooth:
8de680c
+	rfkill_destroy(bluetooth_rfkill);
8de680c
+	if (wifi_rfkill)
8de680c
+		rfkill_unregister(wifi_rfkill);
8de680c
+err_wifi:
8de680c
+	rfkill_destroy(wifi_rfkill);
8de680c
+
8de680c
+	return ret;
8de680c
+}
8de680c
+
8de680c
+static void dell_cleanup_rfkill(void)
8de680c
+{
8de680c
+	if (wifi_rfkill) {
8de680c
+		rfkill_unregister(wifi_rfkill);
8de680c
+		rfkill_destroy(wifi_rfkill);
8de680c
+	}
8de680c
+	if (bluetooth_rfkill) {
8de680c
+		rfkill_unregister(bluetooth_rfkill);
8de680c
+		rfkill_destroy(bluetooth_rfkill);
8de680c
+	}
8de680c
+	if (wwan_rfkill) {
8de680c
+		rfkill_unregister(wwan_rfkill);
8de680c
+		rfkill_destroy(wwan_rfkill);
8de680c
+	}
8de680c
+}
8de680c
+
8de680c
 static int dell_send_intensity(struct backlight_device *bd)
8de680c
 {
8de680c
 	int ret = 0;
8de680c
@@ -515,6 +756,30 @@ static void touchpad_led_exit(void)
8de680c
 	led_classdev_unregister(&touchpad_led);
8de680c
 }
8de680c
 
8de680c
+static bool dell_laptop_i8042_filter(unsigned char data, unsigned char str,
8de680c
+			      struct serio *port)
8de680c
+{
8de680c
+	static bool extended;
8de680c
+
8de680c
+	if (str & 0x20)
8de680c
+		return false;
8de680c
+
8de680c
+	if (unlikely(data == 0xe0)) {
8de680c
+		extended = true;
8de680c
+		return false;
8de680c
+	} else if (unlikely(extended)) {
8de680c
+		switch (data) {
8de680c
+		case 0x8:
8de680c
+			schedule_delayed_work(&dell_rfkill_work,
8de680c
+					      round_jiffies_relative(HZ));
8de680c
+			break;
8de680c
+		}
8de680c
+		extended = false;
8de680c
+	}
8de680c
+
8de680c
+	return false;
8de680c
+}
8de680c
+
8de680c
 static int __init dell_init(void)
8de680c
 {
8de680c
 	int max_intensity = 0;
8de680c
@@ -557,10 +822,26 @@ static int __init dell_init(void)
8de680c
 	}
8de680c
 	buffer = page_address(bufferpage);
8de680c
 
8de680c
+	ret = dell_setup_rfkill();
8de680c
+
8de680c
+	if (ret) {
8de680c
+		pr_warn("Unable to setup rfkill\n");
8de680c
+		goto fail_rfkill;
8de680c
+	}
8de680c
+
8de680c
+	ret = i8042_install_filter(dell_laptop_i8042_filter);
8de680c
+	if (ret) {
8de680c
+		pr_warn("Unable to install key filter\n");
8de680c
+		goto fail_filter;
8de680c
+	}
8de680c
+
8de680c
 	if (quirks && quirks->touchpad_led)
8de680c
 		touchpad_led_init(&platform_device->dev);
8de680c
 
8de680c
 	dell_laptop_dir = debugfs_create_dir("dell_laptop", NULL);
8de680c
+	if (dell_laptop_dir != NULL)
8de680c
+		debugfs_create_file("rfkill", 0444, dell_laptop_dir, NULL,
8de680c
+				    &dell_debugfs_fops);
8de680c
 
8de680c
 #ifdef CONFIG_ACPI
8de680c
 	/* In the event of an ACPI backlight being available, don't
8de680c
@@ -603,6 +884,11 @@ static int __init dell_init(void)
8de680c
 	return 0;
8de680c
 
8de680c
 fail_backlight:
8de680c
+	i8042_remove_filter(dell_laptop_i8042_filter);
8de680c
+	cancel_delayed_work_sync(&dell_rfkill_work);
8de680c
+fail_filter:
8de680c
+	dell_cleanup_rfkill();
8de680c
+fail_rfkill:
8de680c
 	free_page((unsigned long)bufferpage);
8de680c
 fail_buffer:
8de680c
 	platform_device_del(platform_device);
8de680c
@@ -620,7 +906,10 @@ static void __exit dell_exit(void)
8de680c
 	debugfs_remove_recursive(dell_laptop_dir);
8de680c
 	if (quirks && quirks->touchpad_led)
8de680c
 		touchpad_led_exit();
8de680c
+	i8042_remove_filter(dell_laptop_i8042_filter);
8de680c
+	cancel_delayed_work_sync(&dell_rfkill_work);
8de680c
 	backlight_device_unregister(dell_backlight_device);
8de680c
+	dell_cleanup_rfkill();
8de680c
 	if (platform_device) {
8de680c
 		platform_device_unregister(platform_device);
8de680c
 		platform_driver_unregister(&platform_driver);
8de680c
-- 
8de680c
1.8.3.1
8de680c