1 // SPDX-License-Identifier: GPL-2.0-only
3 * Toshiba Bluetooth Enable Driver
8 * Thanks to Matthew Garrett for background info on ACPI innards which
9 * normal people aren't meant to understand :-)
12 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/types.h>
18 #include <linux/acpi.h>
19 #include <linux/rfkill.h>
21 #define BT_KILLSWITCH_MASK 0x01
22 #define BT_PLUGGED_MASK 0x40
23 #define BT_POWER_MASK 0x80
26 MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
27 MODULE_LICENSE("GPL");
29 struct toshiba_bluetooth_dev {
30 struct acpi_device *acpi_dev;
38 static int toshiba_bt_rfkill_add(struct acpi_device *device);
39 static void toshiba_bt_rfkill_remove(struct acpi_device *device);
40 static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);
42 static const struct acpi_device_id bt_device_ids[] = {
46 MODULE_DEVICE_TABLE(acpi, bt_device_ids);
48 #ifdef CONFIG_PM_SLEEP
49 static int toshiba_bt_resume(struct device *dev);
51 static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);
53 static struct acpi_driver toshiba_bt_rfkill_driver = {
58 .add = toshiba_bt_rfkill_add,
59 .remove = toshiba_bt_rfkill_remove,
60 .notify = toshiba_bt_rfkill_notify,
62 .drv.pm = &toshiba_bt_pm,
65 static int toshiba_bluetooth_present(acpi_handle handle)
71 * Some Toshiba laptops may have a fake TOS6205 device in
72 * their ACPI BIOS, so query the _STA method to see if there
73 * is really anything there.
75 result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
76 if (ACPI_FAILURE(result)) {
77 pr_err("ACPI call to query Bluetooth presence failed\n");
82 pr_info("Bluetooth device not present\n");
89 static int toshiba_bluetooth_status(acpi_handle handle)
94 result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
95 if (ACPI_FAILURE(result)) {
96 pr_err("Could not get Bluetooth device status\n");
103 static int toshiba_bluetooth_enable(acpi_handle handle)
107 result = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
108 if (ACPI_FAILURE(result)) {
109 pr_err("Could not attach USB Bluetooth device\n");
113 result = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
114 if (ACPI_FAILURE(result)) {
115 pr_err("Could not power ON Bluetooth device\n");
122 static int toshiba_bluetooth_disable(acpi_handle handle)
126 result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
127 if (ACPI_FAILURE(result)) {
128 pr_err("Could not power OFF Bluetooth device\n");
132 result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
133 if (ACPI_FAILURE(result)) {
134 pr_err("Could not detach USB Bluetooth device\n");
141 /* Helper function */
142 static int toshiba_bluetooth_sync_status(struct toshiba_bluetooth_dev *bt_dev)
146 status = toshiba_bluetooth_status(bt_dev->acpi_dev->handle);
148 pr_err("Could not sync bluetooth device status\n");
152 bt_dev->killswitch = (status & BT_KILLSWITCH_MASK) ? true : false;
153 bt_dev->plugged = (status & BT_PLUGGED_MASK) ? true : false;
154 bt_dev->powered = (status & BT_POWER_MASK) ? true : false;
156 pr_debug("Bluetooth status %d killswitch %d plugged %d powered %d\n",
157 status, bt_dev->killswitch, bt_dev->plugged, bt_dev->powered);
162 /* RFKill handlers */
163 static int bt_rfkill_set_block(void *data, bool blocked)
165 struct toshiba_bluetooth_dev *bt_dev = data;
168 ret = toshiba_bluetooth_sync_status(bt_dev);
172 if (!bt_dev->killswitch)
176 ret = toshiba_bluetooth_disable(bt_dev->acpi_dev->handle);
178 ret = toshiba_bluetooth_enable(bt_dev->acpi_dev->handle);
183 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
185 struct toshiba_bluetooth_dev *bt_dev = data;
187 if (toshiba_bluetooth_sync_status(bt_dev))
191 * Note the Toshiba Bluetooth RFKill switch seems to be a strange
192 * fish. It only provides a BT event when the switch is flipped to
193 * the 'on' position. When flipping it to 'off', the USB device is
194 * simply pulled away underneath us, without any BT event being
197 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
200 static const struct rfkill_ops rfk_ops = {
201 .set_block = bt_rfkill_set_block,
202 .poll = bt_rfkill_poll,
205 /* ACPI driver functions */
206 static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
208 struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
210 if (toshiba_bluetooth_sync_status(bt_dev))
213 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
216 #ifdef CONFIG_PM_SLEEP
217 static int toshiba_bt_resume(struct device *dev)
219 struct toshiba_bluetooth_dev *bt_dev;
222 bt_dev = acpi_driver_data(to_acpi_device(dev));
224 ret = toshiba_bluetooth_sync_status(bt_dev);
228 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
234 static int toshiba_bt_rfkill_add(struct acpi_device *device)
236 struct toshiba_bluetooth_dev *bt_dev;
239 result = toshiba_bluetooth_present(device->handle);
243 pr_info("Toshiba ACPI Bluetooth device driver\n");
245 bt_dev = kzalloc(sizeof(*bt_dev), GFP_KERNEL);
248 bt_dev->acpi_dev = device;
249 device->driver_data = bt_dev;
250 dev_set_drvdata(&device->dev, bt_dev);
252 result = toshiba_bluetooth_sync_status(bt_dev);
258 bt_dev->rfk = rfkill_alloc("Toshiba Bluetooth",
260 RFKILL_TYPE_BLUETOOTH,
264 pr_err("Unable to allocate rfkill device\n");
269 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
271 result = rfkill_register(bt_dev->rfk);
273 pr_err("Unable to register rfkill device\n");
274 rfkill_destroy(bt_dev->rfk);
281 static void toshiba_bt_rfkill_remove(struct acpi_device *device)
283 struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
287 rfkill_unregister(bt_dev->rfk);
288 rfkill_destroy(bt_dev->rfk);
293 toshiba_bluetooth_disable(device->handle);
296 module_acpi_driver(toshiba_bt_rfkill_driver);