18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Toshiba Bluetooth Enable Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2009 Jes Sorensen <Jes.Sorensen@gmail.com>
68c2ecf20Sopenharmony_ci * Copyright (C) 2015 Azael Avalos <coproscefalo@gmail.com>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Thanks to Matthew Garrett for background info on ACPI innards which
98c2ecf20Sopenharmony_ci * normal people aren't meant to understand :-)
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/types.h>
188c2ecf20Sopenharmony_ci#include <linux/acpi.h>
198c2ecf20Sopenharmony_ci#include <linux/rfkill.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define BT_KILLSWITCH_MASK	0x01
228c2ecf20Sopenharmony_ci#define BT_PLUGGED_MASK		0x40
238c2ecf20Sopenharmony_ci#define BT_POWER_MASK		0x80
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
268c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
278c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct toshiba_bluetooth_dev {
308c2ecf20Sopenharmony_ci	struct acpi_device *acpi_dev;
318c2ecf20Sopenharmony_ci	struct rfkill *rfk;
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci	bool killswitch;
348c2ecf20Sopenharmony_ci	bool plugged;
358c2ecf20Sopenharmony_ci	bool powered;
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int toshiba_bt_rfkill_add(struct acpi_device *device);
398c2ecf20Sopenharmony_cistatic int toshiba_bt_rfkill_remove(struct acpi_device *device);
408c2ecf20Sopenharmony_cistatic void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistatic const struct acpi_device_id bt_device_ids[] = {
438c2ecf20Sopenharmony_ci	{ "TOS6205", 0},
448c2ecf20Sopenharmony_ci	{ "", 0},
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, bt_device_ids);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
498c2ecf20Sopenharmony_cistatic int toshiba_bt_resume(struct device *dev);
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic struct acpi_driver toshiba_bt_rfkill_driver = {
548c2ecf20Sopenharmony_ci	.name =		"Toshiba BT",
558c2ecf20Sopenharmony_ci	.class =	"Toshiba",
568c2ecf20Sopenharmony_ci	.ids =		bt_device_ids,
578c2ecf20Sopenharmony_ci	.ops =		{
588c2ecf20Sopenharmony_ci				.add =		toshiba_bt_rfkill_add,
598c2ecf20Sopenharmony_ci				.remove =	toshiba_bt_rfkill_remove,
608c2ecf20Sopenharmony_ci				.notify =	toshiba_bt_rfkill_notify,
618c2ecf20Sopenharmony_ci			},
628c2ecf20Sopenharmony_ci	.owner = 	THIS_MODULE,
638c2ecf20Sopenharmony_ci	.drv.pm =	&toshiba_bt_pm,
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic int toshiba_bluetooth_present(acpi_handle handle)
678c2ecf20Sopenharmony_ci{
688c2ecf20Sopenharmony_ci	acpi_status result;
698c2ecf20Sopenharmony_ci	u64 bt_present;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	/*
728c2ecf20Sopenharmony_ci	 * Some Toshiba laptops may have a fake TOS6205 device in
738c2ecf20Sopenharmony_ci	 * their ACPI BIOS, so query the _STA method to see if there
748c2ecf20Sopenharmony_ci	 * is really anything there.
758c2ecf20Sopenharmony_ci	 */
768c2ecf20Sopenharmony_ci	result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
778c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(result)) {
788c2ecf20Sopenharmony_ci		pr_err("ACPI call to query Bluetooth presence failed\n");
798c2ecf20Sopenharmony_ci		return -ENXIO;
808c2ecf20Sopenharmony_ci	}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (!bt_present) {
838c2ecf20Sopenharmony_ci		pr_info("Bluetooth device not present\n");
848c2ecf20Sopenharmony_ci		return -ENODEV;
858c2ecf20Sopenharmony_ci	}
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	return 0;
888c2ecf20Sopenharmony_ci}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_cistatic int toshiba_bluetooth_status(acpi_handle handle)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	acpi_status result;
938c2ecf20Sopenharmony_ci	u64 status;
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
968c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(result)) {
978c2ecf20Sopenharmony_ci		pr_err("Could not get Bluetooth device status\n");
988c2ecf20Sopenharmony_ci		return -ENXIO;
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return status;
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_cistatic int toshiba_bluetooth_enable(acpi_handle handle)
1058c2ecf20Sopenharmony_ci{
1068c2ecf20Sopenharmony_ci	acpi_status result;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	result = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
1098c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(result)) {
1108c2ecf20Sopenharmony_ci		pr_err("Could not attach USB Bluetooth device\n");
1118c2ecf20Sopenharmony_ci		return -ENXIO;
1128c2ecf20Sopenharmony_ci	}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	result = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
1158c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(result)) {
1168c2ecf20Sopenharmony_ci		pr_err("Could not power ON Bluetooth device\n");
1178c2ecf20Sopenharmony_ci		return -ENXIO;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	return 0;
1218c2ecf20Sopenharmony_ci}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic int toshiba_bluetooth_disable(acpi_handle handle)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	acpi_status result;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
1288c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(result)) {
1298c2ecf20Sopenharmony_ci		pr_err("Could not power OFF Bluetooth device\n");
1308c2ecf20Sopenharmony_ci		return -ENXIO;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
1348c2ecf20Sopenharmony_ci	if (ACPI_FAILURE(result)) {
1358c2ecf20Sopenharmony_ci		pr_err("Could not detach USB Bluetooth device\n");
1368c2ecf20Sopenharmony_ci		return -ENXIO;
1378c2ecf20Sopenharmony_ci	}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	return 0;
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci/* Helper function */
1438c2ecf20Sopenharmony_cistatic int toshiba_bluetooth_sync_status(struct toshiba_bluetooth_dev *bt_dev)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	int status;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	status = toshiba_bluetooth_status(bt_dev->acpi_dev->handle);
1488c2ecf20Sopenharmony_ci	if (status < 0) {
1498c2ecf20Sopenharmony_ci		pr_err("Could not sync bluetooth device status\n");
1508c2ecf20Sopenharmony_ci		return status;
1518c2ecf20Sopenharmony_ci	}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	bt_dev->killswitch = (status & BT_KILLSWITCH_MASK) ? true : false;
1548c2ecf20Sopenharmony_ci	bt_dev->plugged = (status & BT_PLUGGED_MASK) ? true : false;
1558c2ecf20Sopenharmony_ci	bt_dev->powered = (status & BT_POWER_MASK) ? true : false;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	pr_debug("Bluetooth status %d killswitch %d plugged %d powered %d\n",
1588c2ecf20Sopenharmony_ci		 status, bt_dev->killswitch, bt_dev->plugged, bt_dev->powered);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	return 0;
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/* RFKill handlers */
1648c2ecf20Sopenharmony_cistatic int bt_rfkill_set_block(void *data, bool blocked)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	struct toshiba_bluetooth_dev *bt_dev = data;
1678c2ecf20Sopenharmony_ci	int ret;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	ret = toshiba_bluetooth_sync_status(bt_dev);
1708c2ecf20Sopenharmony_ci	if (ret)
1718c2ecf20Sopenharmony_ci		return ret;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (!bt_dev->killswitch)
1748c2ecf20Sopenharmony_ci		return 0;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	if (blocked)
1778c2ecf20Sopenharmony_ci		ret = toshiba_bluetooth_disable(bt_dev->acpi_dev->handle);
1788c2ecf20Sopenharmony_ci	else
1798c2ecf20Sopenharmony_ci		ret = toshiba_bluetooth_enable(bt_dev->acpi_dev->handle);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return ret;
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_cistatic void bt_rfkill_poll(struct rfkill *rfkill, void *data)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	struct toshiba_bluetooth_dev *bt_dev = data;
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	if (toshiba_bluetooth_sync_status(bt_dev))
1898c2ecf20Sopenharmony_ci		return;
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	/*
1928c2ecf20Sopenharmony_ci	 * Note the Toshiba Bluetooth RFKill switch seems to be a strange
1938c2ecf20Sopenharmony_ci	 * fish. It only provides a BT event when the switch is flipped to
1948c2ecf20Sopenharmony_ci	 * the 'on' position. When flipping it to 'off', the USB device is
1958c2ecf20Sopenharmony_ci	 * simply pulled away underneath us, without any BT event being
1968c2ecf20Sopenharmony_ci	 * delivered.
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic const struct rfkill_ops rfk_ops = {
2028c2ecf20Sopenharmony_ci	.set_block = bt_rfkill_set_block,
2038c2ecf20Sopenharmony_ci	.poll = bt_rfkill_poll,
2048c2ecf20Sopenharmony_ci};
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci/* ACPI driver functions */
2078c2ecf20Sopenharmony_cistatic void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
2088c2ecf20Sopenharmony_ci{
2098c2ecf20Sopenharmony_ci	struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	if (toshiba_bluetooth_sync_status(bt_dev))
2128c2ecf20Sopenharmony_ci		return;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
2158c2ecf20Sopenharmony_ci}
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
2188c2ecf20Sopenharmony_cistatic int toshiba_bt_resume(struct device *dev)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct toshiba_bluetooth_dev *bt_dev;
2218c2ecf20Sopenharmony_ci	int ret;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	bt_dev = acpi_driver_data(to_acpi_device(dev));
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	ret = toshiba_bluetooth_sync_status(bt_dev);
2268c2ecf20Sopenharmony_ci	if (ret)
2278c2ecf20Sopenharmony_ci		return ret;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	return 0;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci#endif
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_cistatic int toshiba_bt_rfkill_add(struct acpi_device *device)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct toshiba_bluetooth_dev *bt_dev;
2388c2ecf20Sopenharmony_ci	int result;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	result = toshiba_bluetooth_present(device->handle);
2418c2ecf20Sopenharmony_ci	if (result)
2428c2ecf20Sopenharmony_ci		return result;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	pr_info("Toshiba ACPI Bluetooth device driver\n");
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	bt_dev = kzalloc(sizeof(*bt_dev), GFP_KERNEL);
2478c2ecf20Sopenharmony_ci	if (!bt_dev)
2488c2ecf20Sopenharmony_ci		return -ENOMEM;
2498c2ecf20Sopenharmony_ci	bt_dev->acpi_dev = device;
2508c2ecf20Sopenharmony_ci	device->driver_data = bt_dev;
2518c2ecf20Sopenharmony_ci	dev_set_drvdata(&device->dev, bt_dev);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	result = toshiba_bluetooth_sync_status(bt_dev);
2548c2ecf20Sopenharmony_ci	if (result) {
2558c2ecf20Sopenharmony_ci		kfree(bt_dev);
2568c2ecf20Sopenharmony_ci		return result;
2578c2ecf20Sopenharmony_ci	}
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	bt_dev->rfk = rfkill_alloc("Toshiba Bluetooth",
2608c2ecf20Sopenharmony_ci				   &device->dev,
2618c2ecf20Sopenharmony_ci				   RFKILL_TYPE_BLUETOOTH,
2628c2ecf20Sopenharmony_ci				   &rfk_ops,
2638c2ecf20Sopenharmony_ci				   bt_dev);
2648c2ecf20Sopenharmony_ci	if (!bt_dev->rfk) {
2658c2ecf20Sopenharmony_ci		pr_err("Unable to allocate rfkill device\n");
2668c2ecf20Sopenharmony_ci		kfree(bt_dev);
2678c2ecf20Sopenharmony_ci		return -ENOMEM;
2688c2ecf20Sopenharmony_ci	}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	result = rfkill_register(bt_dev->rfk);
2738c2ecf20Sopenharmony_ci	if (result) {
2748c2ecf20Sopenharmony_ci		pr_err("Unable to register rfkill device\n");
2758c2ecf20Sopenharmony_ci		rfkill_destroy(bt_dev->rfk);
2768c2ecf20Sopenharmony_ci		kfree(bt_dev);
2778c2ecf20Sopenharmony_ci	}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	return result;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int toshiba_bt_rfkill_remove(struct acpi_device *device)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	/* clean up */
2878c2ecf20Sopenharmony_ci	if (bt_dev->rfk) {
2888c2ecf20Sopenharmony_ci		rfkill_unregister(bt_dev->rfk);
2898c2ecf20Sopenharmony_ci		rfkill_destroy(bt_dev->rfk);
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	kfree(bt_dev);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return toshiba_bluetooth_disable(device->handle);
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cimodule_acpi_driver(toshiba_bt_rfkill_driver);
298