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.
14 * Note the Toshiba Bluetooth RFKill switch seems to be a strange
15 * fish. It only provides a BT event when the switch is flipped to
16 * the 'on' position. When flipping it to 'off', the USB device is
17 * simply pulled away underneath us, without any BT event being
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/acpi.h>
29 #define BT_KILLSWITCH_MASK 0x01
30 #define BT_PLUGGED_MASK 0x40
31 #define BT_POWER_MASK 0x80
33 MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
34 MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
35 MODULE_LICENSE("GPL");
37 static int toshiba_bt_rfkill_add(struct acpi_device
*device
);
38 static int toshiba_bt_rfkill_remove(struct acpi_device
*device
);
39 static void toshiba_bt_rfkill_notify(struct acpi_device
*device
, u32 event
);
41 static const struct acpi_device_id bt_device_ids
[] = {
45 MODULE_DEVICE_TABLE(acpi
, bt_device_ids
);
47 #ifdef CONFIG_PM_SLEEP
48 static int toshiba_bt_resume(struct device
*dev
);
50 static SIMPLE_DEV_PM_OPS(toshiba_bt_pm
, NULL
, toshiba_bt_resume
);
52 static struct acpi_driver toshiba_bt_rfkill_driver
= {
57 .add
= toshiba_bt_rfkill_add
,
58 .remove
= toshiba_bt_rfkill_remove
,
59 .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");
79 } else if (!bt_present
) {
80 pr_info("Bluetooth device not present\n");
87 static int toshiba_bluetooth_status(acpi_handle handle
)
92 result
= acpi_evaluate_integer(handle
, "BTST", NULL
, &status
);
93 if (ACPI_FAILURE(result
)) {
94 pr_err("Could not get Bluetooth device status\n");
98 pr_info("Bluetooth status %llu\n", status
);
103 static int toshiba_bluetooth_enable(acpi_handle handle
)
112 * Query ACPI to verify RFKill switch is set to 'on'.
113 * If not, we return silently, no need to report it as
116 status
= toshiba_bluetooth_status(handle
);
120 killswitch
= (status
& BT_KILLSWITCH_MASK
) ? true : false;
121 powered
= (status
& BT_POWER_MASK
) ? true : false;
122 plugged
= (status
& BT_PLUGGED_MASK
) ? true : false;
127 * This check ensures to only enable the device if it is powered
128 * off or detached, as some recent devices somehow pass the killswitch
129 * test, causing a loop enabling/disabling the device, see bug 93911.
131 if (powered
|| plugged
)
134 result
= acpi_evaluate_object(handle
, "AUSB", NULL
, NULL
);
135 if (ACPI_FAILURE(result
)) {
136 pr_err("Could not attach USB Bluetooth device\n");
140 result
= acpi_evaluate_object(handle
, "BTPO", NULL
, NULL
);
141 if (ACPI_FAILURE(result
)) {
142 pr_err("Could not power ON Bluetooth device\n");
149 static int toshiba_bluetooth_disable(acpi_handle handle
)
153 result
= acpi_evaluate_object(handle
, "BTPF", NULL
, NULL
);
154 if (ACPI_FAILURE(result
)) {
155 pr_err("Could not power OFF Bluetooth device\n");
159 result
= acpi_evaluate_object(handle
, "DUSB", NULL
, NULL
);
160 if (ACPI_FAILURE(result
)) {
161 pr_err("Could not detach USB Bluetooth device\n");
168 static void toshiba_bt_rfkill_notify(struct acpi_device
*device
, u32 event
)
170 toshiba_bluetooth_enable(device
->handle
);
173 #ifdef CONFIG_PM_SLEEP
174 static int toshiba_bt_resume(struct device
*dev
)
176 return toshiba_bluetooth_enable(to_acpi_device(dev
)->handle
);
180 static int toshiba_bt_rfkill_add(struct acpi_device
*device
)
184 result
= toshiba_bluetooth_present(device
->handle
);
188 pr_info("Toshiba ACPI Bluetooth device driver\n");
190 /* Enable the BT device */
191 result
= toshiba_bluetooth_enable(device
->handle
);
198 static int toshiba_bt_rfkill_remove(struct acpi_device
*device
)
201 return toshiba_bluetooth_disable(device
->handle
);
204 module_acpi_driver(toshiba_bt_rfkill_driver
);