142b4e9eeSJes Sorensen /*
242b4e9eeSJes Sorensen  * Toshiba Bluetooth Enable Driver
342b4e9eeSJes Sorensen  *
442b4e9eeSJes Sorensen  * Copyright (C) 2009 Jes Sorensen <Jes.Sorensen@gmail.com>
542b4e9eeSJes Sorensen  *
642b4e9eeSJes Sorensen  * Thanks to Matthew Garrett for background info on ACPI innards which
742b4e9eeSJes Sorensen  * normal people aren't meant to understand :-)
842b4e9eeSJes Sorensen  *
942b4e9eeSJes Sorensen  * This program is free software; you can redistribute it and/or modify
1042b4e9eeSJes Sorensen  * it under the terms of the GNU General Public License version 2 as
1142b4e9eeSJes Sorensen  * published by the Free Software Foundation.
1242b4e9eeSJes Sorensen  *
1342b4e9eeSJes Sorensen  * Note the Toshiba Bluetooth RFKill switch seems to be a strange
1442b4e9eeSJes Sorensen  * fish. It only provides a BT event when the switch is flipped to
1542b4e9eeSJes Sorensen  * the 'on' position. When flipping it to 'off', the USB device is
1642b4e9eeSJes Sorensen  * simply pulled away underneath us, without any BT event being
1742b4e9eeSJes Sorensen  * delivered.
1842b4e9eeSJes Sorensen  */
1942b4e9eeSJes Sorensen 
207e33460dSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
217e33460dSJoe Perches 
2242b4e9eeSJes Sorensen #include <linux/kernel.h>
2342b4e9eeSJes Sorensen #include <linux/module.h>
2442b4e9eeSJes Sorensen #include <linux/init.h>
2542b4e9eeSJes Sorensen #include <linux/types.h>
268b48463fSLv Zheng #include <linux/acpi.h>
2742b4e9eeSJes Sorensen 
2842b4e9eeSJes Sorensen MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
2942b4e9eeSJes Sorensen MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
3042b4e9eeSJes Sorensen MODULE_LICENSE("GPL");
3142b4e9eeSJes Sorensen 
3242b4e9eeSJes Sorensen static int toshiba_bt_rfkill_add(struct acpi_device *device);
3351fac838SRafael J. Wysocki static int toshiba_bt_rfkill_remove(struct acpi_device *device);
3442b4e9eeSJes Sorensen static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);
3542b4e9eeSJes Sorensen 
3642b4e9eeSJes Sorensen static const struct acpi_device_id bt_device_ids[] = {
3742b4e9eeSJes Sorensen 	{ "TOS6205", 0},
3842b4e9eeSJes Sorensen 	{ "", 0},
3942b4e9eeSJes Sorensen };
4042b4e9eeSJes Sorensen MODULE_DEVICE_TABLE(acpi, bt_device_ids);
4142b4e9eeSJes Sorensen 
423567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
43d69239aeSRafael J. Wysocki static int toshiba_bt_resume(struct device *dev);
443567a4e2SRafael J. Wysocki #endif
45d69239aeSRafael J. Wysocki static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);
46d69239aeSRafael J. Wysocki 
4742b4e9eeSJes Sorensen static struct acpi_driver toshiba_bt_rfkill_driver = {
4842b4e9eeSJes Sorensen 	.name =		"Toshiba BT",
4942b4e9eeSJes Sorensen 	.class =	"Toshiba",
5042b4e9eeSJes Sorensen 	.ids =		bt_device_ids,
5142b4e9eeSJes Sorensen 	.ops =		{
5242b4e9eeSJes Sorensen 				.add =		toshiba_bt_rfkill_add,
5342b4e9eeSJes Sorensen 				.remove =	toshiba_bt_rfkill_remove,
5442b4e9eeSJes Sorensen 				.notify =	toshiba_bt_rfkill_notify,
5542b4e9eeSJes Sorensen 			},
5642b4e9eeSJes Sorensen 	.owner = 	THIS_MODULE,
57d69239aeSRafael J. Wysocki 	.drv.pm =	&toshiba_bt_pm,
5842b4e9eeSJes Sorensen };
5942b4e9eeSJes Sorensen 
60bb2ea96bSAzael Avalos static int toshiba_bluetooth_present(acpi_handle handle)
61bb2ea96bSAzael Avalos {
62bb2ea96bSAzael Avalos 	acpi_status result;
63bb2ea96bSAzael Avalos 	u64 bt_present;
64bb2ea96bSAzael Avalos 
65bb2ea96bSAzael Avalos 	result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
66bb2ea96bSAzael Avalos 	if (ACPI_FAILURE(result)) {
67bb2ea96bSAzael Avalos 		pr_err("ACPI call to query Bluetooth presence failed");
68bb2ea96bSAzael Avalos 		return -ENXIO;
69bb2ea96bSAzael Avalos 	} else if (!bt_present) {
70bb2ea96bSAzael Avalos 		pr_info("Bluetooth device not present\n");
71bb2ea96bSAzael Avalos 		return -ENODEV;
72bb2ea96bSAzael Avalos 	}
73bb2ea96bSAzael Avalos 
74bb2ea96bSAzael Avalos 	return 0;
75bb2ea96bSAzael Avalos }
76bb2ea96bSAzael Avalos 
77bb2ea96bSAzael Avalos static int toshiba_bluetooth_status(acpi_handle handle)
78bb2ea96bSAzael Avalos {
79bb2ea96bSAzael Avalos 	acpi_status result;
80bb2ea96bSAzael Avalos 	u64 status;
81bb2ea96bSAzael Avalos 
82bb2ea96bSAzael Avalos 	result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
83bb2ea96bSAzael Avalos 	if (ACPI_FAILURE(result)) {
84bb2ea96bSAzael Avalos 		pr_err("Could not get Bluetooth device status\n");
85bb2ea96bSAzael Avalos 		return -ENXIO;
86bb2ea96bSAzael Avalos 	}
87bb2ea96bSAzael Avalos 
88bb2ea96bSAzael Avalos 	pr_info("Bluetooth status %llu\n", status);
89bb2ea96bSAzael Avalos 
90bb2ea96bSAzael Avalos 	return status;
91bb2ea96bSAzael Avalos }
9242b4e9eeSJes Sorensen 
9342b4e9eeSJes Sorensen static int toshiba_bluetooth_enable(acpi_handle handle)
9442b4e9eeSJes Sorensen {
9542b4e9eeSJes Sorensen 	acpi_status res1, res2;
96439913ffSLin Ming 	u64 result;
9742b4e9eeSJes Sorensen 
9842b4e9eeSJes Sorensen 	/*
9942b4e9eeSJes Sorensen 	 * Query ACPI to verify RFKill switch is set to 'on'.
10042b4e9eeSJes Sorensen 	 * If not, we return silently, no need to report it as
10142b4e9eeSJes Sorensen 	 * an error.
10242b4e9eeSJes Sorensen 	 */
10342b4e9eeSJes Sorensen 	res1 = acpi_evaluate_integer(handle, "BTST", NULL, &result);
10442b4e9eeSJes Sorensen 	if (ACPI_FAILURE(res1))
10542b4e9eeSJes Sorensen 		return res1;
10642b4e9eeSJes Sorensen 	if (!(result & 0x01))
10742b4e9eeSJes Sorensen 		return 0;
10842b4e9eeSJes Sorensen 
1097e33460dSJoe Perches 	pr_info("Re-enabling Toshiba Bluetooth\n");
11042b4e9eeSJes Sorensen 	res1 = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
11142b4e9eeSJes Sorensen 	res2 = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
11242b4e9eeSJes Sorensen 	if (!ACPI_FAILURE(res1) || !ACPI_FAILURE(res2))
11342b4e9eeSJes Sorensen 		return 0;
11442b4e9eeSJes Sorensen 
1157e33460dSJoe Perches 	pr_warn("Failed to re-enable Toshiba Bluetooth\n");
11642b4e9eeSJes Sorensen 
11742b4e9eeSJes Sorensen 	return -ENODEV;
11842b4e9eeSJes Sorensen }
11942b4e9eeSJes Sorensen 
120bb2ea96bSAzael Avalos static int toshiba_bluetooth_disable(acpi_handle handle)
121bb2ea96bSAzael Avalos {
122bb2ea96bSAzael Avalos 	acpi_status result;
123bb2ea96bSAzael Avalos 
124bb2ea96bSAzael Avalos 	result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
125bb2ea96bSAzael Avalos 	if (ACPI_FAILURE(result)) {
126bb2ea96bSAzael Avalos 		pr_err("Could not power OFF Bluetooth device\n");
127bb2ea96bSAzael Avalos 		return -ENXIO;
128bb2ea96bSAzael Avalos 	}
129bb2ea96bSAzael Avalos 
130bb2ea96bSAzael Avalos 	result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
131bb2ea96bSAzael Avalos 	if (ACPI_FAILURE(result)) {
132bb2ea96bSAzael Avalos 		pr_err("Could not detach USB Bluetooth device\n");
133bb2ea96bSAzael Avalos 		return -ENXIO;
134bb2ea96bSAzael Avalos 	}
135bb2ea96bSAzael Avalos 
136bb2ea96bSAzael Avalos 	return 0;
137bb2ea96bSAzael Avalos }
138bb2ea96bSAzael Avalos 
13942b4e9eeSJes Sorensen static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
14042b4e9eeSJes Sorensen {
14142b4e9eeSJes Sorensen 	toshiba_bluetooth_enable(device->handle);
14242b4e9eeSJes Sorensen }
14342b4e9eeSJes Sorensen 
1443567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
145d69239aeSRafael J. Wysocki static int toshiba_bt_resume(struct device *dev)
14642b4e9eeSJes Sorensen {
147d69239aeSRafael J. Wysocki 	return toshiba_bluetooth_enable(to_acpi_device(dev)->handle);
14842b4e9eeSJes Sorensen }
1493567a4e2SRafael J. Wysocki #endif
15042b4e9eeSJes Sorensen 
15142b4e9eeSJes Sorensen static int toshiba_bt_rfkill_add(struct acpi_device *device)
15242b4e9eeSJes Sorensen {
15342b4e9eeSJes Sorensen 	acpi_status status;
154439913ffSLin Ming 	u64 bt_present;
15542b4e9eeSJes Sorensen 	int result = -ENODEV;
15642b4e9eeSJes Sorensen 
15742b4e9eeSJes Sorensen 	/*
15842b4e9eeSJes Sorensen 	 * Some Toshiba laptops may have a fake TOS6205 device in
15942b4e9eeSJes Sorensen 	 * their ACPI BIOS, so query the _STA method to see if there
16042b4e9eeSJes Sorensen 	 * is really anything there, before trying to enable it.
16142b4e9eeSJes Sorensen 	 */
16242b4e9eeSJes Sorensen 	status = acpi_evaluate_integer(device->handle, "_STA", NULL,
16342b4e9eeSJes Sorensen 				       &bt_present);
16442b4e9eeSJes Sorensen 
16542b4e9eeSJes Sorensen 	if (!ACPI_FAILURE(status) && bt_present) {
1667e33460dSJoe Perches 		pr_info("Detected Toshiba ACPI Bluetooth device - "
16742b4e9eeSJes Sorensen 			"installing RFKill handler\n");
16842b4e9eeSJes Sorensen 		result = toshiba_bluetooth_enable(device->handle);
16942b4e9eeSJes Sorensen 	}
17042b4e9eeSJes Sorensen 
17142b4e9eeSJes Sorensen 	return result;
17242b4e9eeSJes Sorensen }
17342b4e9eeSJes Sorensen 
17451fac838SRafael J. Wysocki static int toshiba_bt_rfkill_remove(struct acpi_device *device)
17542b4e9eeSJes Sorensen {
17642b4e9eeSJes Sorensen 	/* clean up */
17742b4e9eeSJes Sorensen 	return 0;
17842b4e9eeSJes Sorensen }
17942b4e9eeSJes Sorensen 
18001d17753SMika Westerberg module_acpi_driver(toshiba_bt_rfkill_driver);
181