1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
242b4e9eeSJes Sorensen /*
342b4e9eeSJes Sorensen * Toshiba Bluetooth Enable Driver
442b4e9eeSJes Sorensen *
542b4e9eeSJes Sorensen * Copyright (C) 2009 Jes Sorensen <Jes.Sorensen@gmail.com>
65d3fc1d5SAzael Avalos * Copyright (C) 2015 Azael Avalos <coproscefalo@gmail.com>
742b4e9eeSJes Sorensen *
842b4e9eeSJes Sorensen * Thanks to Matthew Garrett for background info on ACPI innards which
942b4e9eeSJes Sorensen * normal people aren't meant to understand :-)
1042b4e9eeSJes Sorensen */
1142b4e9eeSJes Sorensen
127e33460dSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
137e33460dSJoe Perches
1442b4e9eeSJes Sorensen #include <linux/kernel.h>
1542b4e9eeSJes Sorensen #include <linux/module.h>
1642b4e9eeSJes Sorensen #include <linux/init.h>
1742b4e9eeSJes Sorensen #include <linux/types.h>
188b48463fSLv Zheng #include <linux/acpi.h>
197ee8cd33SAzael Avalos #include <linux/rfkill.h>
2042b4e9eeSJes Sorensen
215d3fc1d5SAzael Avalos #define BT_KILLSWITCH_MASK 0x01
225d3fc1d5SAzael Avalos #define BT_PLUGGED_MASK 0x40
235d3fc1d5SAzael Avalos #define BT_POWER_MASK 0x80
245d3fc1d5SAzael Avalos
2542b4e9eeSJes Sorensen MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
2642b4e9eeSJes Sorensen MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
2742b4e9eeSJes Sorensen MODULE_LICENSE("GPL");
2842b4e9eeSJes Sorensen
2984c0691eSAzael Avalos struct toshiba_bluetooth_dev {
3084c0691eSAzael Avalos struct acpi_device *acpi_dev;
317ee8cd33SAzael Avalos struct rfkill *rfk;
3284c0691eSAzael Avalos
3384c0691eSAzael Avalos bool killswitch;
3484c0691eSAzael Avalos bool plugged;
3584c0691eSAzael Avalos bool powered;
3684c0691eSAzael Avalos };
3784c0691eSAzael Avalos
3842b4e9eeSJes Sorensen static int toshiba_bt_rfkill_add(struct acpi_device *device);
39*6c0eb5baSDawei Li static void toshiba_bt_rfkill_remove(struct acpi_device *device);
4042b4e9eeSJes Sorensen static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);
4142b4e9eeSJes Sorensen
4242b4e9eeSJes Sorensen static const struct acpi_device_id bt_device_ids[] = {
4342b4e9eeSJes Sorensen { "TOS6205", 0},
4442b4e9eeSJes Sorensen { "", 0},
4542b4e9eeSJes Sorensen };
4642b4e9eeSJes Sorensen MODULE_DEVICE_TABLE(acpi, bt_device_ids);
4742b4e9eeSJes Sorensen
483567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
49d69239aeSRafael J. Wysocki static int toshiba_bt_resume(struct device *dev);
503567a4e2SRafael J. Wysocki #endif
51d69239aeSRafael J. Wysocki static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);
52d69239aeSRafael J. Wysocki
5342b4e9eeSJes Sorensen static struct acpi_driver toshiba_bt_rfkill_driver = {
5442b4e9eeSJes Sorensen .name = "Toshiba BT",
5542b4e9eeSJes Sorensen .class = "Toshiba",
5642b4e9eeSJes Sorensen .ids = bt_device_ids,
5742b4e9eeSJes Sorensen .ops = {
5842b4e9eeSJes Sorensen .add = toshiba_bt_rfkill_add,
5942b4e9eeSJes Sorensen .remove = toshiba_bt_rfkill_remove,
6042b4e9eeSJes Sorensen .notify = toshiba_bt_rfkill_notify,
6142b4e9eeSJes Sorensen },
6242b4e9eeSJes Sorensen .owner = THIS_MODULE,
63d69239aeSRafael J. Wysocki .drv.pm = &toshiba_bt_pm,
6442b4e9eeSJes Sorensen };
6542b4e9eeSJes Sorensen
toshiba_bluetooth_present(acpi_handle handle)66bb2ea96bSAzael Avalos static int toshiba_bluetooth_present(acpi_handle handle)
67bb2ea96bSAzael Avalos {
68bb2ea96bSAzael Avalos acpi_status result;
69bb2ea96bSAzael Avalos u64 bt_present;
70bb2ea96bSAzael Avalos
7118b8696bSAzael Avalos /*
7218b8696bSAzael Avalos * Some Toshiba laptops may have a fake TOS6205 device in
7318b8696bSAzael Avalos * their ACPI BIOS, so query the _STA method to see if there
7418b8696bSAzael Avalos * is really anything there.
7518b8696bSAzael Avalos */
76bb2ea96bSAzael Avalos result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
77bb2ea96bSAzael Avalos if (ACPI_FAILURE(result)) {
78260e0ecaSAzael Avalos pr_err("ACPI call to query Bluetooth presence failed\n");
79bb2ea96bSAzael Avalos return -ENXIO;
8028e476d7SAzael Avalos }
8128e476d7SAzael Avalos
8228e476d7SAzael Avalos if (!bt_present) {
83bb2ea96bSAzael Avalos pr_info("Bluetooth device not present\n");
84bb2ea96bSAzael Avalos return -ENODEV;
85bb2ea96bSAzael Avalos }
86bb2ea96bSAzael Avalos
87bb2ea96bSAzael Avalos return 0;
88bb2ea96bSAzael Avalos }
89bb2ea96bSAzael Avalos
toshiba_bluetooth_status(acpi_handle handle)90bb2ea96bSAzael Avalos static int toshiba_bluetooth_status(acpi_handle handle)
91bb2ea96bSAzael Avalos {
92bb2ea96bSAzael Avalos acpi_status result;
93bb2ea96bSAzael Avalos u64 status;
94bb2ea96bSAzael Avalos
95bb2ea96bSAzael Avalos result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
96bb2ea96bSAzael Avalos if (ACPI_FAILURE(result)) {
97bb2ea96bSAzael Avalos pr_err("Could not get Bluetooth device status\n");
98bb2ea96bSAzael Avalos return -ENXIO;
99bb2ea96bSAzael Avalos }
100bb2ea96bSAzael Avalos
101bb2ea96bSAzael Avalos return status;
102bb2ea96bSAzael Avalos }
10342b4e9eeSJes Sorensen
toshiba_bluetooth_enable(acpi_handle handle)10442b4e9eeSJes Sorensen static int toshiba_bluetooth_enable(acpi_handle handle)
10542b4e9eeSJes Sorensen {
1065d3fc1d5SAzael Avalos acpi_status result;
10742b4e9eeSJes Sorensen
1085d3fc1d5SAzael Avalos result = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
1095d3fc1d5SAzael Avalos if (ACPI_FAILURE(result)) {
1105d3fc1d5SAzael Avalos pr_err("Could not attach USB Bluetooth device\n");
1115d3fc1d5SAzael Avalos return -ENXIO;
1125d3fc1d5SAzael Avalos }
1135d3fc1d5SAzael Avalos
1145d3fc1d5SAzael Avalos result = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
1155d3fc1d5SAzael Avalos if (ACPI_FAILURE(result)) {
1165d3fc1d5SAzael Avalos pr_err("Could not power ON Bluetooth device\n");
1175d3fc1d5SAzael Avalos return -ENXIO;
1185d3fc1d5SAzael Avalos }
1195d3fc1d5SAzael Avalos
12042b4e9eeSJes Sorensen return 0;
12142b4e9eeSJes Sorensen }
12242b4e9eeSJes Sorensen
toshiba_bluetooth_disable(acpi_handle handle)123bb2ea96bSAzael Avalos static int toshiba_bluetooth_disable(acpi_handle handle)
124bb2ea96bSAzael Avalos {
125bb2ea96bSAzael Avalos acpi_status result;
126bb2ea96bSAzael Avalos
127bb2ea96bSAzael Avalos result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
128bb2ea96bSAzael Avalos if (ACPI_FAILURE(result)) {
129bb2ea96bSAzael Avalos pr_err("Could not power OFF Bluetooth device\n");
130bb2ea96bSAzael Avalos return -ENXIO;
131bb2ea96bSAzael Avalos }
132bb2ea96bSAzael Avalos
133bb2ea96bSAzael Avalos result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
134bb2ea96bSAzael Avalos if (ACPI_FAILURE(result)) {
135bb2ea96bSAzael Avalos pr_err("Could not detach USB Bluetooth device\n");
136bb2ea96bSAzael Avalos return -ENXIO;
137bb2ea96bSAzael Avalos }
138bb2ea96bSAzael Avalos
139bb2ea96bSAzael Avalos return 0;
140bb2ea96bSAzael Avalos }
141bb2ea96bSAzael Avalos
14284c0691eSAzael Avalos /* Helper function */
toshiba_bluetooth_sync_status(struct toshiba_bluetooth_dev * bt_dev)14384c0691eSAzael Avalos static int toshiba_bluetooth_sync_status(struct toshiba_bluetooth_dev *bt_dev)
14484c0691eSAzael Avalos {
14584c0691eSAzael Avalos int status;
14684c0691eSAzael Avalos
14784c0691eSAzael Avalos status = toshiba_bluetooth_status(bt_dev->acpi_dev->handle);
14884c0691eSAzael Avalos if (status < 0) {
14984c0691eSAzael Avalos pr_err("Could not sync bluetooth device status\n");
15084c0691eSAzael Avalos return status;
15184c0691eSAzael Avalos }
15284c0691eSAzael Avalos
15384c0691eSAzael Avalos bt_dev->killswitch = (status & BT_KILLSWITCH_MASK) ? true : false;
15484c0691eSAzael Avalos bt_dev->plugged = (status & BT_PLUGGED_MASK) ? true : false;
15584c0691eSAzael Avalos bt_dev->powered = (status & BT_POWER_MASK) ? true : false;
15684c0691eSAzael Avalos
1578798df88SAzael Avalos pr_debug("Bluetooth status %d killswitch %d plugged %d powered %d\n",
1588798df88SAzael Avalos status, bt_dev->killswitch, bt_dev->plugged, bt_dev->powered);
1598798df88SAzael Avalos
16084c0691eSAzael Avalos return 0;
16184c0691eSAzael Avalos }
16284c0691eSAzael Avalos
1637ee8cd33SAzael Avalos /* RFKill handlers */
bt_rfkill_set_block(void * data,bool blocked)1647ee8cd33SAzael Avalos static int bt_rfkill_set_block(void *data, bool blocked)
1657ee8cd33SAzael Avalos {
1667ee8cd33SAzael Avalos struct toshiba_bluetooth_dev *bt_dev = data;
1677ee8cd33SAzael Avalos int ret;
1687ee8cd33SAzael Avalos
1697ee8cd33SAzael Avalos ret = toshiba_bluetooth_sync_status(bt_dev);
1707ee8cd33SAzael Avalos if (ret)
1717ee8cd33SAzael Avalos return ret;
1727ee8cd33SAzael Avalos
1737ee8cd33SAzael Avalos if (!bt_dev->killswitch)
1747ee8cd33SAzael Avalos return 0;
1757ee8cd33SAzael Avalos
1767ee8cd33SAzael Avalos if (blocked)
1777ee8cd33SAzael Avalos ret = toshiba_bluetooth_disable(bt_dev->acpi_dev->handle);
1787ee8cd33SAzael Avalos else
1797ee8cd33SAzael Avalos ret = toshiba_bluetooth_enable(bt_dev->acpi_dev->handle);
1807ee8cd33SAzael Avalos
1817ee8cd33SAzael Avalos return ret;
1827ee8cd33SAzael Avalos }
1837ee8cd33SAzael Avalos
bt_rfkill_poll(struct rfkill * rfkill,void * data)1847ee8cd33SAzael Avalos static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
1857ee8cd33SAzael Avalos {
1867ee8cd33SAzael Avalos struct toshiba_bluetooth_dev *bt_dev = data;
1877ee8cd33SAzael Avalos
1887ee8cd33SAzael Avalos if (toshiba_bluetooth_sync_status(bt_dev))
1897ee8cd33SAzael Avalos return;
1907ee8cd33SAzael Avalos
1917ee8cd33SAzael Avalos /*
1927ee8cd33SAzael Avalos * Note the Toshiba Bluetooth RFKill switch seems to be a strange
1937ee8cd33SAzael Avalos * fish. It only provides a BT event when the switch is flipped to
1947ee8cd33SAzael Avalos * the 'on' position. When flipping it to 'off', the USB device is
1957ee8cd33SAzael Avalos * simply pulled away underneath us, without any BT event being
1967ee8cd33SAzael Avalos * delivered.
1977ee8cd33SAzael Avalos */
1987ee8cd33SAzael Avalos rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
1997ee8cd33SAzael Avalos }
2007ee8cd33SAzael Avalos
2017ee8cd33SAzael Avalos static const struct rfkill_ops rfk_ops = {
2027ee8cd33SAzael Avalos .set_block = bt_rfkill_set_block,
2037ee8cd33SAzael Avalos .poll = bt_rfkill_poll,
2047ee8cd33SAzael Avalos };
2057ee8cd33SAzael Avalos
20684c0691eSAzael Avalos /* ACPI driver functions */
toshiba_bt_rfkill_notify(struct acpi_device * device,u32 event)20742b4e9eeSJes Sorensen static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
20842b4e9eeSJes Sorensen {
209d85b11b1SAzael Avalos struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
210d85b11b1SAzael Avalos
211d85b11b1SAzael Avalos if (toshiba_bluetooth_sync_status(bt_dev))
212d85b11b1SAzael Avalos return;
213d85b11b1SAzael Avalos
214d85b11b1SAzael Avalos rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
21542b4e9eeSJes Sorensen }
21642b4e9eeSJes Sorensen
2173567a4e2SRafael J. Wysocki #ifdef CONFIG_PM_SLEEP
toshiba_bt_resume(struct device * dev)218d69239aeSRafael J. Wysocki static int toshiba_bt_resume(struct device *dev)
21942b4e9eeSJes Sorensen {
220d85b11b1SAzael Avalos struct toshiba_bluetooth_dev *bt_dev;
221d85b11b1SAzael Avalos int ret;
222d85b11b1SAzael Avalos
223d85b11b1SAzael Avalos bt_dev = acpi_driver_data(to_acpi_device(dev));
224d85b11b1SAzael Avalos
225d85b11b1SAzael Avalos ret = toshiba_bluetooth_sync_status(bt_dev);
226d85b11b1SAzael Avalos if (ret)
227d85b11b1SAzael Avalos return ret;
228d85b11b1SAzael Avalos
229d85b11b1SAzael Avalos rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
230d85b11b1SAzael Avalos
231d85b11b1SAzael Avalos return 0;
23242b4e9eeSJes Sorensen }
2333567a4e2SRafael J. Wysocki #endif
23442b4e9eeSJes Sorensen
toshiba_bt_rfkill_add(struct acpi_device * device)23542b4e9eeSJes Sorensen static int toshiba_bt_rfkill_add(struct acpi_device *device)
23642b4e9eeSJes Sorensen {
23784c0691eSAzael Avalos struct toshiba_bluetooth_dev *bt_dev;
23818b8696bSAzael Avalos int result;
23942b4e9eeSJes Sorensen
24018b8696bSAzael Avalos result = toshiba_bluetooth_present(device->handle);
24118b8696bSAzael Avalos if (result)
24218b8696bSAzael Avalos return result;
24342b4e9eeSJes Sorensen
24418b8696bSAzael Avalos pr_info("Toshiba ACPI Bluetooth device driver\n");
24518b8696bSAzael Avalos
24684c0691eSAzael Avalos bt_dev = kzalloc(sizeof(*bt_dev), GFP_KERNEL);
24784c0691eSAzael Avalos if (!bt_dev)
24884c0691eSAzael Avalos return -ENOMEM;
24984c0691eSAzael Avalos bt_dev->acpi_dev = device;
25084c0691eSAzael Avalos device->driver_data = bt_dev;
25184c0691eSAzael Avalos dev_set_drvdata(&device->dev, bt_dev);
25284c0691eSAzael Avalos
25384c0691eSAzael Avalos result = toshiba_bluetooth_sync_status(bt_dev);
25484c0691eSAzael Avalos if (result) {
25584c0691eSAzael Avalos kfree(bt_dev);
25684c0691eSAzael Avalos return result;
25784c0691eSAzael Avalos }
25884c0691eSAzael Avalos
2597ee8cd33SAzael Avalos bt_dev->rfk = rfkill_alloc("Toshiba Bluetooth",
2607ee8cd33SAzael Avalos &device->dev,
2617ee8cd33SAzael Avalos RFKILL_TYPE_BLUETOOTH,
2627ee8cd33SAzael Avalos &rfk_ops,
2637ee8cd33SAzael Avalos bt_dev);
2647ee8cd33SAzael Avalos if (!bt_dev->rfk) {
2657ee8cd33SAzael Avalos pr_err("Unable to allocate rfkill device\n");
26684c0691eSAzael Avalos kfree(bt_dev);
2677ee8cd33SAzael Avalos return -ENOMEM;
2687ee8cd33SAzael Avalos }
2697ee8cd33SAzael Avalos
2707ee8cd33SAzael Avalos rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
2717ee8cd33SAzael Avalos
2727ee8cd33SAzael Avalos result = rfkill_register(bt_dev->rfk);
2737ee8cd33SAzael Avalos if (result) {
2747ee8cd33SAzael Avalos pr_err("Unable to register rfkill device\n");
2757ee8cd33SAzael Avalos rfkill_destroy(bt_dev->rfk);
2767ee8cd33SAzael Avalos kfree(bt_dev);
2777ee8cd33SAzael Avalos }
27842b4e9eeSJes Sorensen
27942b4e9eeSJes Sorensen return result;
28042b4e9eeSJes Sorensen }
28142b4e9eeSJes Sorensen
toshiba_bt_rfkill_remove(struct acpi_device * device)282*6c0eb5baSDawei Li static void toshiba_bt_rfkill_remove(struct acpi_device *device)
28342b4e9eeSJes Sorensen {
28484c0691eSAzael Avalos struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
28584c0691eSAzael Avalos
28642b4e9eeSJes Sorensen /* clean up */
2877ee8cd33SAzael Avalos if (bt_dev->rfk) {
2887ee8cd33SAzael Avalos rfkill_unregister(bt_dev->rfk);
2897ee8cd33SAzael Avalos rfkill_destroy(bt_dev->rfk);
2907ee8cd33SAzael Avalos }
2917ee8cd33SAzael Avalos
29284c0691eSAzael Avalos kfree(bt_dev);
29384c0691eSAzael Avalos
294*6c0eb5baSDawei Li toshiba_bluetooth_disable(device->handle);
29542b4e9eeSJes Sorensen }
29642b4e9eeSJes Sorensen
29701d17753SMika Westerberg module_acpi_driver(toshiba_bt_rfkill_driver);
298