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 
6518b8696bSAzael Avalos 	/*
6618b8696bSAzael Avalos 	 * Some Toshiba laptops may have a fake TOS6205 device in
6718b8696bSAzael Avalos 	 * their ACPI BIOS, so query the _STA method to see if there
6818b8696bSAzael Avalos 	 * is really anything there.
6918b8696bSAzael Avalos 	 */
70bb2ea96bSAzael Avalos 	result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
71bb2ea96bSAzael Avalos 	if (ACPI_FAILURE(result)) {
72bb2ea96bSAzael Avalos 		pr_err("ACPI call to query Bluetooth presence failed");
73bb2ea96bSAzael Avalos 		return -ENXIO;
74bb2ea96bSAzael Avalos 	} else if (!bt_present) {
75bb2ea96bSAzael Avalos 		pr_info("Bluetooth device not present\n");
76bb2ea96bSAzael Avalos 		return -ENODEV;
77bb2ea96bSAzael Avalos 	}
78bb2ea96bSAzael Avalos 
79bb2ea96bSAzael Avalos 	return 0;
80bb2ea96bSAzael Avalos }
81bb2ea96bSAzael Avalos 
82bb2ea96bSAzael Avalos static int toshiba_bluetooth_status(acpi_handle handle)
83bb2ea96bSAzael Avalos {
84bb2ea96bSAzael Avalos 	acpi_status result;
85bb2ea96bSAzael Avalos 	u64 status;
86bb2ea96bSAzael Avalos 
87bb2ea96bSAzael Avalos 	result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
88bb2ea96bSAzael Avalos 	if (ACPI_FAILURE(result)) {
89bb2ea96bSAzael Avalos 		pr_err("Could not get Bluetooth device status\n");
90bb2ea96bSAzael Avalos 		return -ENXIO;
91bb2ea96bSAzael Avalos 	}
92bb2ea96bSAzael Avalos 
93bb2ea96bSAzael Avalos 	pr_info("Bluetooth status %llu\n", status);
94bb2ea96bSAzael Avalos 
95bb2ea96bSAzael Avalos 	return status;
96bb2ea96bSAzael Avalos }
9742b4e9eeSJes Sorensen 
9842b4e9eeSJes Sorensen static int toshiba_bluetooth_enable(acpi_handle handle)
9942b4e9eeSJes Sorensen {
10042b4e9eeSJes Sorensen 	acpi_status res1, res2;
101439913ffSLin Ming 	u64 result;
10242b4e9eeSJes Sorensen 
10342b4e9eeSJes Sorensen 	/*
10442b4e9eeSJes Sorensen 	 * Query ACPI to verify RFKill switch is set to 'on'.
10542b4e9eeSJes Sorensen 	 * If not, we return silently, no need to report it as
10642b4e9eeSJes Sorensen 	 * an error.
10742b4e9eeSJes Sorensen 	 */
10842b4e9eeSJes Sorensen 	res1 = acpi_evaluate_integer(handle, "BTST", NULL, &result);
10942b4e9eeSJes Sorensen 	if (ACPI_FAILURE(res1))
11042b4e9eeSJes Sorensen 		return res1;
11142b4e9eeSJes Sorensen 	if (!(result & 0x01))
11242b4e9eeSJes Sorensen 		return 0;
11342b4e9eeSJes Sorensen 
1147e33460dSJoe Perches 	pr_info("Re-enabling Toshiba Bluetooth\n");
11542b4e9eeSJes Sorensen 	res1 = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
11642b4e9eeSJes Sorensen 	res2 = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
11742b4e9eeSJes Sorensen 	if (!ACPI_FAILURE(res1) || !ACPI_FAILURE(res2))
11842b4e9eeSJes Sorensen 		return 0;
11942b4e9eeSJes Sorensen 
1207e33460dSJoe Perches 	pr_warn("Failed to re-enable Toshiba Bluetooth\n");
12142b4e9eeSJes Sorensen 
12242b4e9eeSJes Sorensen 	return -ENODEV;
12342b4e9eeSJes Sorensen }
12442b4e9eeSJes Sorensen 
125bb2ea96bSAzael Avalos static int toshiba_bluetooth_disable(acpi_handle handle)
126bb2ea96bSAzael Avalos {
127bb2ea96bSAzael Avalos 	acpi_status result;
128bb2ea96bSAzael Avalos 
129bb2ea96bSAzael Avalos 	result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
130bb2ea96bSAzael Avalos 	if (ACPI_FAILURE(result)) {
131bb2ea96bSAzael Avalos 		pr_err("Could not power OFF Bluetooth device\n");
132bb2ea96bSAzael Avalos 		return -ENXIO;
133bb2ea96bSAzael Avalos 	}
134bb2ea96bSAzael Avalos 
135bb2ea96bSAzael Avalos 	result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
136bb2ea96bSAzael Avalos 	if (ACPI_FAILURE(result)) {
137bb2ea96bSAzael Avalos 		pr_err("Could not detach USB Bluetooth device\n");
138bb2ea96bSAzael Avalos 		return -ENXIO;
139bb2ea96bSAzael Avalos 	}
140bb2ea96bSAzael Avalos 
141bb2ea96bSAzael Avalos 	return 0;
142bb2ea96bSAzael Avalos }
143bb2ea96bSAzael Avalos 
14442b4e9eeSJes Sorensen static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
14542b4e9eeSJes Sorensen {
14642b4e9eeSJes Sorensen 	toshiba_bluetooth_enable(device->handle);
14742b4e9eeSJes Sorensen }
14842b4e9eeSJes Sorensen 
1493567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
150d69239aeSRafael J. Wysocki static int toshiba_bt_resume(struct device *dev)
15142b4e9eeSJes Sorensen {
152d69239aeSRafael J. Wysocki 	return toshiba_bluetooth_enable(to_acpi_device(dev)->handle);
15342b4e9eeSJes Sorensen }
1543567a4e2SRafael J. Wysocki #endif
15542b4e9eeSJes Sorensen 
15642b4e9eeSJes Sorensen static int toshiba_bt_rfkill_add(struct acpi_device *device)
15742b4e9eeSJes Sorensen {
15818b8696bSAzael Avalos 	int result;
15942b4e9eeSJes Sorensen 
16018b8696bSAzael Avalos 	result = toshiba_bluetooth_present(device->handle);
16118b8696bSAzael Avalos 	if (result)
16218b8696bSAzael Avalos 		return result;
16342b4e9eeSJes Sorensen 
16418b8696bSAzael Avalos 	pr_info("Toshiba ACPI Bluetooth device driver\n");
16518b8696bSAzael Avalos 
16618b8696bSAzael Avalos 	/* Enable the BT device */
16742b4e9eeSJes Sorensen 	result = toshiba_bluetooth_enable(device->handle);
16818b8696bSAzael Avalos 	if (result)
16918b8696bSAzael Avalos 		return result;
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 */
17718b8696bSAzael Avalos 	return toshiba_bluetooth_disable(device->handle);
17842b4e9eeSJes Sorensen }
17942b4e9eeSJes Sorensen 
18001d17753SMika Westerberg module_acpi_driver(toshiba_bt_rfkill_driver);
181