2 * Toshiba Bluetooth Enable Driver
4 * Copyright (C) 2009 Jes Sorensen <Jes.Sorensen@gmail.com>
5 * Copyright (C) 2015 Azael Avalos <coproscefalo@gmail.com>
7 * Thanks to Matthew Garrett for background info on ACPI innards which
8 * normal people aren't meant to understand :-)
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/types.h>
21 #include <linux/acpi.h>
22 #include <linux/rfkill.h>
24 #define BT_KILLSWITCH_MASK 0x01
25 #define BT_PLUGGED_MASK 0x40
26 #define BT_POWER_MASK 0x80
28 MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
29 MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
30 MODULE_LICENSE("GPL");
32 struct toshiba_bluetooth_dev {
33 struct acpi_device *acpi_dev;
41 static int toshiba_bt_rfkill_add(struct acpi_device *device);
42 static int toshiba_bt_rfkill_remove(struct acpi_device *device);
43 static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);
45 static const struct acpi_device_id bt_device_ids[] = {
49 MODULE_DEVICE_TABLE(acpi, bt_device_ids);
51 #ifdef CONFIG_PM_SLEEP
52 static int toshiba_bt_resume(struct device *dev);
54 static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);
56 static struct acpi_driver toshiba_bt_rfkill_driver = {
61 .add = toshiba_bt_rfkill_add,
62 .remove = toshiba_bt_rfkill_remove,
63 .notify = toshiba_bt_rfkill_notify,
66 .drv.pm = &toshiba_bt_pm,
69 static int toshiba_bluetooth_present(acpi_handle handle)
75 * Some Toshiba laptops may have a fake TOS6205 device in
76 * their ACPI BIOS, so query the _STA method to see if there
77 * is really anything there.
79 result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
80 if (ACPI_FAILURE(result)) {
81 pr_err("ACPI call to query Bluetooth presence failed");
83 } else if (!bt_present) {
84 pr_info("Bluetooth device not present\n");
91 static int toshiba_bluetooth_status(acpi_handle handle)
96 result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
97 if (ACPI_FAILURE(result)) {
98 pr_err("Could not get Bluetooth device status\n");
105 static int toshiba_bluetooth_enable(acpi_handle handle)
109 result = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
110 if (ACPI_FAILURE(result)) {
111 pr_err("Could not attach USB Bluetooth device\n");
115 result = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
116 if (ACPI_FAILURE(result)) {
117 pr_err("Could not power ON Bluetooth device\n");
124 static int toshiba_bluetooth_disable(acpi_handle handle)
128 result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
129 if (ACPI_FAILURE(result)) {
130 pr_err("Could not power OFF Bluetooth device\n");
134 result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
135 if (ACPI_FAILURE(result)) {
136 pr_err("Could not detach USB Bluetooth device\n");
143 /* Helper function */
144 static int toshiba_bluetooth_sync_status(struct toshiba_bluetooth_dev *bt_dev)
148 status = toshiba_bluetooth_status(bt_dev->acpi_dev->handle);
150 pr_err("Could not sync bluetooth device status\n");
154 bt_dev->killswitch = (status & BT_KILLSWITCH_MASK) ? true : false;
155 bt_dev->plugged = (status & BT_PLUGGED_MASK) ? true : false;
156 bt_dev->powered = (status & BT_POWER_MASK) ? true : false;
158 pr_debug("Bluetooth status %d killswitch %d plugged %d powered %d\n",
159 status, bt_dev->killswitch, bt_dev->plugged, bt_dev->powered);
164 /* RFKill handlers */
165 static int bt_rfkill_set_block(void *data, bool blocked)
167 struct toshiba_bluetooth_dev *bt_dev = data;
170 ret = toshiba_bluetooth_sync_status(bt_dev);
174 if (!bt_dev->killswitch)
178 ret = toshiba_bluetooth_disable(bt_dev->acpi_dev->handle);
180 ret = toshiba_bluetooth_enable(bt_dev->acpi_dev->handle);
185 static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
187 struct toshiba_bluetooth_dev *bt_dev = data;
189 if (toshiba_bluetooth_sync_status(bt_dev))
193 * Note the Toshiba Bluetooth RFKill switch seems to be a strange
194 * fish. It only provides a BT event when the switch is flipped to
195 * the 'on' position. When flipping it to 'off', the USB device is
196 * simply pulled away underneath us, without any BT event being
199 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
202 static const struct rfkill_ops rfk_ops = {
203 .set_block = bt_rfkill_set_block,
204 .poll = bt_rfkill_poll,
207 /* ACPI driver functions */
208 static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
210 struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
212 if (toshiba_bluetooth_sync_status(bt_dev))
215 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
218 #ifdef CONFIG_PM_SLEEP
219 static int toshiba_bt_resume(struct device *dev)
221 struct toshiba_bluetooth_dev *bt_dev;
224 bt_dev = acpi_driver_data(to_acpi_device(dev));
226 ret = toshiba_bluetooth_sync_status(bt_dev);
230 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
236 static int toshiba_bt_rfkill_add(struct acpi_device *device)
238 struct toshiba_bluetooth_dev *bt_dev;
241 result = toshiba_bluetooth_present(device->handle);
245 pr_info("Toshiba ACPI Bluetooth device driver\n");
247 bt_dev = kzalloc(sizeof(*bt_dev), GFP_KERNEL);
250 bt_dev->acpi_dev = device;
251 device->driver_data = bt_dev;
252 dev_set_drvdata(&device->dev, bt_dev);
254 result = toshiba_bluetooth_sync_status(bt_dev);
260 bt_dev->rfk = rfkill_alloc("Toshiba Bluetooth",
262 RFKILL_TYPE_BLUETOOTH,
266 pr_err("Unable to allocate rfkill device\n");
271 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
273 result = rfkill_register(bt_dev->rfk);
275 pr_err("Unable to register rfkill device\n");
276 rfkill_destroy(bt_dev->rfk);
283 static int toshiba_bt_rfkill_remove(struct acpi_device *device)
285 struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
289 rfkill_unregister(bt_dev->rfk);
290 rfkill_destroy(bt_dev->rfk);
295 return toshiba_bluetooth_disable(device->handle);
298 module_acpi_driver(toshiba_bt_rfkill_driver);