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 static int toshiba_bt_resume(struct acpi_device *device);
3842b4e9eeSJes Sorensen 
3942b4e9eeSJes Sorensen static const struct acpi_device_id bt_device_ids[] = {
4042b4e9eeSJes Sorensen 	{ "TOS6205", 0},
4142b4e9eeSJes Sorensen 	{ "", 0},
4242b4e9eeSJes Sorensen };
4342b4e9eeSJes Sorensen MODULE_DEVICE_TABLE(acpi, bt_device_ids);
4442b4e9eeSJes Sorensen 
4542b4e9eeSJes Sorensen static struct acpi_driver toshiba_bt_rfkill_driver = {
4642b4e9eeSJes Sorensen 	.name =		"Toshiba BT",
4742b4e9eeSJes Sorensen 	.class =	"Toshiba",
4842b4e9eeSJes Sorensen 	.ids =		bt_device_ids,
4942b4e9eeSJes Sorensen 	.ops =		{
5042b4e9eeSJes Sorensen 				.add =		toshiba_bt_rfkill_add,
5142b4e9eeSJes Sorensen 				.remove =	toshiba_bt_rfkill_remove,
5242b4e9eeSJes Sorensen 				.notify =	toshiba_bt_rfkill_notify,
5342b4e9eeSJes Sorensen 				.resume =	toshiba_bt_resume,
5442b4e9eeSJes Sorensen 			},
5542b4e9eeSJes Sorensen 	.owner = 	THIS_MODULE,
5642b4e9eeSJes Sorensen };
5742b4e9eeSJes Sorensen 
5842b4e9eeSJes Sorensen 
5942b4e9eeSJes Sorensen static int toshiba_bluetooth_enable(acpi_handle handle)
6042b4e9eeSJes Sorensen {
6142b4e9eeSJes Sorensen 	acpi_status res1, res2;
62439913ffSLin Ming 	u64 result;
6342b4e9eeSJes Sorensen 
6442b4e9eeSJes Sorensen 	/*
6542b4e9eeSJes Sorensen 	 * Query ACPI to verify RFKill switch is set to 'on'.
6642b4e9eeSJes Sorensen 	 * If not, we return silently, no need to report it as
6742b4e9eeSJes Sorensen 	 * an error.
6842b4e9eeSJes Sorensen 	 */
6942b4e9eeSJes Sorensen 	res1 = acpi_evaluate_integer(handle, "BTST", NULL, &result);
7042b4e9eeSJes Sorensen 	if (ACPI_FAILURE(res1))
7142b4e9eeSJes Sorensen 		return res1;
7242b4e9eeSJes Sorensen 	if (!(result & 0x01))
7342b4e9eeSJes Sorensen 		return 0;
7442b4e9eeSJes Sorensen 
757e33460dSJoe Perches 	pr_info("Re-enabling Toshiba Bluetooth\n");
7642b4e9eeSJes Sorensen 	res1 = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
7742b4e9eeSJes Sorensen 	res2 = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
7842b4e9eeSJes Sorensen 	if (!ACPI_FAILURE(res1) || !ACPI_FAILURE(res2))
7942b4e9eeSJes Sorensen 		return 0;
8042b4e9eeSJes Sorensen 
817e33460dSJoe Perches 	pr_warn("Failed to re-enable Toshiba Bluetooth\n");
8242b4e9eeSJes Sorensen 
8342b4e9eeSJes Sorensen 	return -ENODEV;
8442b4e9eeSJes Sorensen }
8542b4e9eeSJes Sorensen 
8642b4e9eeSJes Sorensen static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
8742b4e9eeSJes Sorensen {
8842b4e9eeSJes Sorensen 	toshiba_bluetooth_enable(device->handle);
8942b4e9eeSJes Sorensen }
9042b4e9eeSJes Sorensen 
9142b4e9eeSJes Sorensen static int toshiba_bt_resume(struct acpi_device *device)
9242b4e9eeSJes Sorensen {
9342b4e9eeSJes Sorensen 	return toshiba_bluetooth_enable(device->handle);
9442b4e9eeSJes Sorensen }
9542b4e9eeSJes Sorensen 
9642b4e9eeSJes Sorensen static int toshiba_bt_rfkill_add(struct acpi_device *device)
9742b4e9eeSJes Sorensen {
9842b4e9eeSJes Sorensen 	acpi_status status;
99439913ffSLin Ming 	u64 bt_present;
10042b4e9eeSJes Sorensen 	int result = -ENODEV;
10142b4e9eeSJes Sorensen 
10242b4e9eeSJes Sorensen 	/*
10342b4e9eeSJes Sorensen 	 * Some Toshiba laptops may have a fake TOS6205 device in
10442b4e9eeSJes Sorensen 	 * their ACPI BIOS, so query the _STA method to see if there
10542b4e9eeSJes Sorensen 	 * is really anything there, before trying to enable it.
10642b4e9eeSJes Sorensen 	 */
10742b4e9eeSJes Sorensen 	status = acpi_evaluate_integer(device->handle, "_STA", NULL,
10842b4e9eeSJes Sorensen 				       &bt_present);
10942b4e9eeSJes Sorensen 
11042b4e9eeSJes Sorensen 	if (!ACPI_FAILURE(status) && bt_present) {
1117e33460dSJoe Perches 		pr_info("Detected Toshiba ACPI Bluetooth device - "
11242b4e9eeSJes Sorensen 			"installing RFKill handler\n");
11342b4e9eeSJes Sorensen 		result = toshiba_bluetooth_enable(device->handle);
11442b4e9eeSJes Sorensen 	}
11542b4e9eeSJes Sorensen 
11642b4e9eeSJes Sorensen 	return result;
11742b4e9eeSJes Sorensen }
11842b4e9eeSJes Sorensen 
11942b4e9eeSJes Sorensen static int __init toshiba_bt_rfkill_init(void)
12042b4e9eeSJes Sorensen {
12142b4e9eeSJes Sorensen 	int result;
12242b4e9eeSJes Sorensen 
12342b4e9eeSJes Sorensen 	result = acpi_bus_register_driver(&toshiba_bt_rfkill_driver);
12442b4e9eeSJes Sorensen 	if (result < 0) {
12542b4e9eeSJes Sorensen 		ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
12642b4e9eeSJes Sorensen 				  "Error registering driver\n"));
12742b4e9eeSJes Sorensen 		return result;
12842b4e9eeSJes Sorensen 	}
12942b4e9eeSJes Sorensen 
13042b4e9eeSJes Sorensen 	return 0;
13142b4e9eeSJes Sorensen }
13242b4e9eeSJes Sorensen 
13342b4e9eeSJes Sorensen static int toshiba_bt_rfkill_remove(struct acpi_device *device, int type)
13442b4e9eeSJes Sorensen {
13542b4e9eeSJes Sorensen 	/* clean up */
13642b4e9eeSJes Sorensen 	return 0;
13742b4e9eeSJes Sorensen }
13842b4e9eeSJes Sorensen 
13942b4e9eeSJes Sorensen static void __exit toshiba_bt_rfkill_exit(void)
14042b4e9eeSJes Sorensen {
14142b4e9eeSJes Sorensen 	acpi_bus_unregister_driver(&toshiba_bt_rfkill_driver);
14242b4e9eeSJes Sorensen }
14342b4e9eeSJes Sorensen 
14442b4e9eeSJes Sorensen module_init(toshiba_bt_rfkill_init);
14542b4e9eeSJes Sorensen module_exit(toshiba_bt_rfkill_exit);
146