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>
2642b4e9eeSJes Sorensen #include <acpi/acpi_bus.h>
2742b4e9eeSJes Sorensen #include <acpi/acpi_drivers.h>
2842b4e9eeSJes Sorensen 
2942b4e9eeSJes Sorensen MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
3042b4e9eeSJes Sorensen MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
3142b4e9eeSJes Sorensen MODULE_LICENSE("GPL");
3242b4e9eeSJes Sorensen 
3342b4e9eeSJes Sorensen 
3442b4e9eeSJes Sorensen static int toshiba_bt_rfkill_add(struct acpi_device *device);
3542b4e9eeSJes Sorensen static int toshiba_bt_rfkill_remove(struct acpi_device *device, int type);
3642b4e9eeSJes Sorensen static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);
3742b4e9eeSJes Sorensen 
3842b4e9eeSJes Sorensen static const struct acpi_device_id bt_device_ids[] = {
3942b4e9eeSJes Sorensen 	{ "TOS6205", 0},
4042b4e9eeSJes Sorensen 	{ "", 0},
4142b4e9eeSJes Sorensen };
4242b4e9eeSJes Sorensen MODULE_DEVICE_TABLE(acpi, bt_device_ids);
4342b4e9eeSJes Sorensen 
443567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
45d69239aeSRafael J. Wysocki static int toshiba_bt_resume(struct device *dev);
463567a4e2SRafael J. Wysocki #endif
47d69239aeSRafael J. Wysocki static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);
48d69239aeSRafael J. Wysocki 
4942b4e9eeSJes Sorensen static struct acpi_driver toshiba_bt_rfkill_driver = {
5042b4e9eeSJes Sorensen 	.name =		"Toshiba BT",
5142b4e9eeSJes Sorensen 	.class =	"Toshiba",
5242b4e9eeSJes Sorensen 	.ids =		bt_device_ids,
5342b4e9eeSJes Sorensen 	.ops =		{
5442b4e9eeSJes Sorensen 				.add =		toshiba_bt_rfkill_add,
5542b4e9eeSJes Sorensen 				.remove =	toshiba_bt_rfkill_remove,
5642b4e9eeSJes Sorensen 				.notify =	toshiba_bt_rfkill_notify,
5742b4e9eeSJes Sorensen 			},
5842b4e9eeSJes Sorensen 	.owner = 	THIS_MODULE,
59d69239aeSRafael J. Wysocki 	.drv.pm =	&toshiba_bt_pm,
6042b4e9eeSJes Sorensen };
6142b4e9eeSJes Sorensen 
6242b4e9eeSJes Sorensen 
6342b4e9eeSJes Sorensen static int toshiba_bluetooth_enable(acpi_handle handle)
6442b4e9eeSJes Sorensen {
6542b4e9eeSJes Sorensen 	acpi_status res1, res2;
66439913ffSLin Ming 	u64 result;
6742b4e9eeSJes Sorensen 
6842b4e9eeSJes Sorensen 	/*
6942b4e9eeSJes Sorensen 	 * Query ACPI to verify RFKill switch is set to 'on'.
7042b4e9eeSJes Sorensen 	 * If not, we return silently, no need to report it as
7142b4e9eeSJes Sorensen 	 * an error.
7242b4e9eeSJes Sorensen 	 */
7342b4e9eeSJes Sorensen 	res1 = acpi_evaluate_integer(handle, "BTST", NULL, &result);
7442b4e9eeSJes Sorensen 	if (ACPI_FAILURE(res1))
7542b4e9eeSJes Sorensen 		return res1;
7642b4e9eeSJes Sorensen 	if (!(result & 0x01))
7742b4e9eeSJes Sorensen 		return 0;
7842b4e9eeSJes Sorensen 
797e33460dSJoe Perches 	pr_info("Re-enabling Toshiba Bluetooth\n");
8042b4e9eeSJes Sorensen 	res1 = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
8142b4e9eeSJes Sorensen 	res2 = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
8242b4e9eeSJes Sorensen 	if (!ACPI_FAILURE(res1) || !ACPI_FAILURE(res2))
8342b4e9eeSJes Sorensen 		return 0;
8442b4e9eeSJes Sorensen 
857e33460dSJoe Perches 	pr_warn("Failed to re-enable Toshiba Bluetooth\n");
8642b4e9eeSJes Sorensen 
8742b4e9eeSJes Sorensen 	return -ENODEV;
8842b4e9eeSJes Sorensen }
8942b4e9eeSJes Sorensen 
9042b4e9eeSJes Sorensen static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
9142b4e9eeSJes Sorensen {
9242b4e9eeSJes Sorensen 	toshiba_bluetooth_enable(device->handle);
9342b4e9eeSJes Sorensen }
9442b4e9eeSJes Sorensen 
953567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
96d69239aeSRafael J. Wysocki static int toshiba_bt_resume(struct device *dev)
9742b4e9eeSJes Sorensen {
98d69239aeSRafael J. Wysocki 	return toshiba_bluetooth_enable(to_acpi_device(dev)->handle);
9942b4e9eeSJes Sorensen }
1003567a4e2SRafael J. Wysocki #endif
10142b4e9eeSJes Sorensen 
10242b4e9eeSJes Sorensen static int toshiba_bt_rfkill_add(struct acpi_device *device)
10342b4e9eeSJes Sorensen {
10442b4e9eeSJes Sorensen 	acpi_status status;
105439913ffSLin Ming 	u64 bt_present;
10642b4e9eeSJes Sorensen 	int result = -ENODEV;
10742b4e9eeSJes Sorensen 
10842b4e9eeSJes Sorensen 	/*
10942b4e9eeSJes Sorensen 	 * Some Toshiba laptops may have a fake TOS6205 device in
11042b4e9eeSJes Sorensen 	 * their ACPI BIOS, so query the _STA method to see if there
11142b4e9eeSJes Sorensen 	 * is really anything there, before trying to enable it.
11242b4e9eeSJes Sorensen 	 */
11342b4e9eeSJes Sorensen 	status = acpi_evaluate_integer(device->handle, "_STA", NULL,
11442b4e9eeSJes Sorensen 				       &bt_present);
11542b4e9eeSJes Sorensen 
11642b4e9eeSJes Sorensen 	if (!ACPI_FAILURE(status) && bt_present) {
1177e33460dSJoe Perches 		pr_info("Detected Toshiba ACPI Bluetooth device - "
11842b4e9eeSJes Sorensen 			"installing RFKill handler\n");
11942b4e9eeSJes Sorensen 		result = toshiba_bluetooth_enable(device->handle);
12042b4e9eeSJes Sorensen 	}
12142b4e9eeSJes Sorensen 
12242b4e9eeSJes Sorensen 	return result;
12342b4e9eeSJes Sorensen }
12442b4e9eeSJes Sorensen 
12542b4e9eeSJes Sorensen static int __init toshiba_bt_rfkill_init(void)
12642b4e9eeSJes Sorensen {
12742b4e9eeSJes Sorensen 	int result;
12842b4e9eeSJes Sorensen 
12942b4e9eeSJes Sorensen 	result = acpi_bus_register_driver(&toshiba_bt_rfkill_driver);
13042b4e9eeSJes Sorensen 	if (result < 0) {
13142b4e9eeSJes Sorensen 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
13242b4e9eeSJes Sorensen 				  "Error registering driver\n"));
13342b4e9eeSJes Sorensen 		return result;
13442b4e9eeSJes Sorensen 	}
13542b4e9eeSJes Sorensen 
13642b4e9eeSJes Sorensen 	return 0;
13742b4e9eeSJes Sorensen }
13842b4e9eeSJes Sorensen 
13942b4e9eeSJes Sorensen static int toshiba_bt_rfkill_remove(struct acpi_device *device, int type)
14042b4e9eeSJes Sorensen {
14142b4e9eeSJes Sorensen 	/* clean up */
14242b4e9eeSJes Sorensen 	return 0;
14342b4e9eeSJes Sorensen }
14442b4e9eeSJes Sorensen 
14542b4e9eeSJes Sorensen static void __exit toshiba_bt_rfkill_exit(void)
14642b4e9eeSJes Sorensen {
14742b4e9eeSJes Sorensen 	acpi_bus_unregister_driver(&toshiba_bt_rfkill_driver);
14842b4e9eeSJes Sorensen }
14942b4e9eeSJes Sorensen 
15042b4e9eeSJes Sorensen module_init(toshiba_bt_rfkill_init);
15142b4e9eeSJes Sorensen module_exit(toshiba_bt_rfkill_exit);
152