Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Toshiba Bluetooth Enable Driver |
| 4 | * |
| 5 | * Copyright (C) 2009 Jes Sorensen <Jes.Sorensen@gmail.com> |
Azael Avalos | 5d3fc1d | 2015-03-26 14:56:07 -0600 | [diff] [blame] | 6 | * Copyright (C) 2015 Azael Avalos <coproscefalo@gmail.com> |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 7 | * |
| 8 | * Thanks to Matthew Garrett for background info on ACPI innards which |
| 9 | * normal people aren't meant to understand :-) |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 10 | */ |
| 11 | |
Joe Perches | 7e33460 | 2011-03-29 15:21:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 14 | #include <linux/kernel.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/types.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 18 | #include <linux/acpi.h> |
Azael Avalos | 7ee8cd3 | 2015-05-03 17:42:07 -0600 | [diff] [blame] | 19 | #include <linux/rfkill.h> |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 20 | |
Azael Avalos | 5d3fc1d | 2015-03-26 14:56:07 -0600 | [diff] [blame] | 21 | #define BT_KILLSWITCH_MASK 0x01 |
| 22 | #define BT_PLUGGED_MASK 0x40 |
| 23 | #define BT_POWER_MASK 0x80 |
| 24 | |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 25 | MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>"); |
| 26 | MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 29 | struct toshiba_bluetooth_dev { |
| 30 | struct acpi_device *acpi_dev; |
Azael Avalos | 7ee8cd3 | 2015-05-03 17:42:07 -0600 | [diff] [blame] | 31 | struct rfkill *rfk; |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 32 | |
| 33 | bool killswitch; |
| 34 | bool plugged; |
| 35 | bool powered; |
| 36 | }; |
| 37 | |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 38 | static int toshiba_bt_rfkill_add(struct acpi_device *device); |
Rafael J. Wysocki | 51fac83 | 2013-01-24 00:24:48 +0100 | [diff] [blame] | 39 | static int toshiba_bt_rfkill_remove(struct acpi_device *device); |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 40 | static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event); |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 41 | |
| 42 | static const struct acpi_device_id bt_device_ids[] = { |
| 43 | { "TOS6205", 0}, |
| 44 | { "", 0}, |
| 45 | }; |
| 46 | MODULE_DEVICE_TABLE(acpi, bt_device_ids); |
| 47 | |
Rafael J. Wysocki | 3567a4e2 | 2012-08-09 23:00:13 +0200 | [diff] [blame] | 48 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | d69239a | 2012-06-27 23:27:48 +0200 | [diff] [blame] | 49 | static int toshiba_bt_resume(struct device *dev); |
Rafael J. Wysocki | 3567a4e2 | 2012-08-09 23:00:13 +0200 | [diff] [blame] | 50 | #endif |
Rafael J. Wysocki | d69239a | 2012-06-27 23:27:48 +0200 | [diff] [blame] | 51 | static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume); |
| 52 | |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 53 | static struct acpi_driver toshiba_bt_rfkill_driver = { |
| 54 | .name = "Toshiba BT", |
| 55 | .class = "Toshiba", |
| 56 | .ids = bt_device_ids, |
| 57 | .ops = { |
| 58 | .add = toshiba_bt_rfkill_add, |
| 59 | .remove = toshiba_bt_rfkill_remove, |
| 60 | .notify = toshiba_bt_rfkill_notify, |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 61 | }, |
| 62 | .owner = THIS_MODULE, |
Rafael J. Wysocki | d69239a | 2012-06-27 23:27:48 +0200 | [diff] [blame] | 63 | .drv.pm = &toshiba_bt_pm, |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 64 | }; |
| 65 | |
Azael Avalos | bb2ea96 | 2015-03-26 14:56:05 -0600 | [diff] [blame] | 66 | static int toshiba_bluetooth_present(acpi_handle handle) |
| 67 | { |
| 68 | acpi_status result; |
| 69 | u64 bt_present; |
| 70 | |
Azael Avalos | 18b8696 | 2015-03-26 14:56:06 -0600 | [diff] [blame] | 71 | /* |
| 72 | * Some Toshiba laptops may have a fake TOS6205 device in |
| 73 | * their ACPI BIOS, so query the _STA method to see if there |
| 74 | * is really anything there. |
| 75 | */ |
Azael Avalos | bb2ea96 | 2015-03-26 14:56:05 -0600 | [diff] [blame] | 76 | result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present); |
| 77 | if (ACPI_FAILURE(result)) { |
Azael Avalos | 260e0ec | 2015-11-15 20:33:47 -0700 | [diff] [blame] | 78 | pr_err("ACPI call to query Bluetooth presence failed\n"); |
Azael Avalos | bb2ea96 | 2015-03-26 14:56:05 -0600 | [diff] [blame] | 79 | return -ENXIO; |
Azael Avalos | 28e476d | 2016-09-07 09:28:15 -0600 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | if (!bt_present) { |
Azael Avalos | bb2ea96 | 2015-03-26 14:56:05 -0600 | [diff] [blame] | 83 | pr_info("Bluetooth device not present\n"); |
| 84 | return -ENODEV; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int toshiba_bluetooth_status(acpi_handle handle) |
| 91 | { |
| 92 | acpi_status result; |
| 93 | u64 status; |
| 94 | |
| 95 | result = acpi_evaluate_integer(handle, "BTST", NULL, &status); |
| 96 | if (ACPI_FAILURE(result)) { |
| 97 | pr_err("Could not get Bluetooth device status\n"); |
| 98 | return -ENXIO; |
| 99 | } |
| 100 | |
Azael Avalos | bb2ea96 | 2015-03-26 14:56:05 -0600 | [diff] [blame] | 101 | return status; |
| 102 | } |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 103 | |
| 104 | static int toshiba_bluetooth_enable(acpi_handle handle) |
| 105 | { |
Azael Avalos | 5d3fc1d | 2015-03-26 14:56:07 -0600 | [diff] [blame] | 106 | acpi_status result; |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 107 | |
Azael Avalos | 5d3fc1d | 2015-03-26 14:56:07 -0600 | [diff] [blame] | 108 | result = acpi_evaluate_object(handle, "AUSB", NULL, NULL); |
| 109 | if (ACPI_FAILURE(result)) { |
| 110 | pr_err("Could not attach USB Bluetooth device\n"); |
| 111 | return -ENXIO; |
| 112 | } |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 113 | |
Azael Avalos | 5d3fc1d | 2015-03-26 14:56:07 -0600 | [diff] [blame] | 114 | result = acpi_evaluate_object(handle, "BTPO", NULL, NULL); |
| 115 | if (ACPI_FAILURE(result)) { |
| 116 | pr_err("Could not power ON Bluetooth device\n"); |
| 117 | return -ENXIO; |
| 118 | } |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 119 | |
Azael Avalos | 5d3fc1d | 2015-03-26 14:56:07 -0600 | [diff] [blame] | 120 | return 0; |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 121 | } |
| 122 | |
Azael Avalos | bb2ea96 | 2015-03-26 14:56:05 -0600 | [diff] [blame] | 123 | static int toshiba_bluetooth_disable(acpi_handle handle) |
| 124 | { |
| 125 | acpi_status result; |
| 126 | |
| 127 | result = acpi_evaluate_object(handle, "BTPF", NULL, NULL); |
| 128 | if (ACPI_FAILURE(result)) { |
| 129 | pr_err("Could not power OFF Bluetooth device\n"); |
| 130 | return -ENXIO; |
| 131 | } |
| 132 | |
| 133 | result = acpi_evaluate_object(handle, "DUSB", NULL, NULL); |
| 134 | if (ACPI_FAILURE(result)) { |
| 135 | pr_err("Could not detach USB Bluetooth device\n"); |
| 136 | return -ENXIO; |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 142 | /* Helper function */ |
| 143 | static int toshiba_bluetooth_sync_status(struct toshiba_bluetooth_dev *bt_dev) |
| 144 | { |
| 145 | int status; |
| 146 | |
| 147 | status = toshiba_bluetooth_status(bt_dev->acpi_dev->handle); |
| 148 | if (status < 0) { |
| 149 | pr_err("Could not sync bluetooth device status\n"); |
| 150 | return status; |
| 151 | } |
| 152 | |
| 153 | bt_dev->killswitch = (status & BT_KILLSWITCH_MASK) ? true : false; |
| 154 | bt_dev->plugged = (status & BT_PLUGGED_MASK) ? true : false; |
| 155 | bt_dev->powered = (status & BT_POWER_MASK) ? true : false; |
| 156 | |
Azael Avalos | 8798df8 | 2015-05-03 17:42:09 -0600 | [diff] [blame] | 157 | pr_debug("Bluetooth status %d killswitch %d plugged %d powered %d\n", |
| 158 | status, bt_dev->killswitch, bt_dev->plugged, bt_dev->powered); |
| 159 | |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
Azael Avalos | 7ee8cd3 | 2015-05-03 17:42:07 -0600 | [diff] [blame] | 163 | /* RFKill handlers */ |
| 164 | static int bt_rfkill_set_block(void *data, bool blocked) |
| 165 | { |
| 166 | struct toshiba_bluetooth_dev *bt_dev = data; |
| 167 | int ret; |
| 168 | |
| 169 | ret = toshiba_bluetooth_sync_status(bt_dev); |
| 170 | if (ret) |
| 171 | return ret; |
| 172 | |
| 173 | if (!bt_dev->killswitch) |
| 174 | return 0; |
| 175 | |
| 176 | if (blocked) |
| 177 | ret = toshiba_bluetooth_disable(bt_dev->acpi_dev->handle); |
| 178 | else |
| 179 | ret = toshiba_bluetooth_enable(bt_dev->acpi_dev->handle); |
| 180 | |
| 181 | return ret; |
| 182 | } |
| 183 | |
| 184 | static void bt_rfkill_poll(struct rfkill *rfkill, void *data) |
| 185 | { |
| 186 | struct toshiba_bluetooth_dev *bt_dev = data; |
| 187 | |
| 188 | if (toshiba_bluetooth_sync_status(bt_dev)) |
| 189 | return; |
| 190 | |
| 191 | /* |
| 192 | * Note the Toshiba Bluetooth RFKill switch seems to be a strange |
| 193 | * fish. It only provides a BT event when the switch is flipped to |
| 194 | * the 'on' position. When flipping it to 'off', the USB device is |
| 195 | * simply pulled away underneath us, without any BT event being |
| 196 | * delivered. |
| 197 | */ |
| 198 | rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch); |
| 199 | } |
| 200 | |
| 201 | static const struct rfkill_ops rfk_ops = { |
| 202 | .set_block = bt_rfkill_set_block, |
| 203 | .poll = bt_rfkill_poll, |
| 204 | }; |
| 205 | |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 206 | /* ACPI driver functions */ |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 207 | static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event) |
| 208 | { |
Azael Avalos | d85b11b | 2015-05-03 17:42:08 -0600 | [diff] [blame] | 209 | struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device); |
| 210 | |
| 211 | if (toshiba_bluetooth_sync_status(bt_dev)) |
| 212 | return; |
| 213 | |
| 214 | rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch); |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 215 | } |
| 216 | |
Rafael J. Wysocki | 3567a4e2 | 2012-08-09 23:00:13 +0200 | [diff] [blame] | 217 | #ifdef CONFIG_PM_SLEEP |
Rafael J. Wysocki | d69239a | 2012-06-27 23:27:48 +0200 | [diff] [blame] | 218 | static int toshiba_bt_resume(struct device *dev) |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 219 | { |
Azael Avalos | d85b11b | 2015-05-03 17:42:08 -0600 | [diff] [blame] | 220 | struct toshiba_bluetooth_dev *bt_dev; |
| 221 | int ret; |
| 222 | |
| 223 | bt_dev = acpi_driver_data(to_acpi_device(dev)); |
| 224 | |
| 225 | ret = toshiba_bluetooth_sync_status(bt_dev); |
| 226 | if (ret) |
| 227 | return ret; |
| 228 | |
| 229 | rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch); |
| 230 | |
| 231 | return 0; |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 232 | } |
Rafael J. Wysocki | 3567a4e2 | 2012-08-09 23:00:13 +0200 | [diff] [blame] | 233 | #endif |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 234 | |
| 235 | static int toshiba_bt_rfkill_add(struct acpi_device *device) |
| 236 | { |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 237 | struct toshiba_bluetooth_dev *bt_dev; |
Azael Avalos | 18b8696 | 2015-03-26 14:56:06 -0600 | [diff] [blame] | 238 | int result; |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 239 | |
Azael Avalos | 18b8696 | 2015-03-26 14:56:06 -0600 | [diff] [blame] | 240 | result = toshiba_bluetooth_present(device->handle); |
| 241 | if (result) |
| 242 | return result; |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 243 | |
Azael Avalos | 18b8696 | 2015-03-26 14:56:06 -0600 | [diff] [blame] | 244 | pr_info("Toshiba ACPI Bluetooth device driver\n"); |
| 245 | |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 246 | bt_dev = kzalloc(sizeof(*bt_dev), GFP_KERNEL); |
| 247 | if (!bt_dev) |
| 248 | return -ENOMEM; |
| 249 | bt_dev->acpi_dev = device; |
| 250 | device->driver_data = bt_dev; |
| 251 | dev_set_drvdata(&device->dev, bt_dev); |
| 252 | |
| 253 | result = toshiba_bluetooth_sync_status(bt_dev); |
| 254 | if (result) { |
| 255 | kfree(bt_dev); |
| 256 | return result; |
| 257 | } |
| 258 | |
Azael Avalos | 7ee8cd3 | 2015-05-03 17:42:07 -0600 | [diff] [blame] | 259 | bt_dev->rfk = rfkill_alloc("Toshiba Bluetooth", |
| 260 | &device->dev, |
| 261 | RFKILL_TYPE_BLUETOOTH, |
| 262 | &rfk_ops, |
| 263 | bt_dev); |
| 264 | if (!bt_dev->rfk) { |
| 265 | pr_err("Unable to allocate rfkill device\n"); |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 266 | kfree(bt_dev); |
Azael Avalos | 7ee8cd3 | 2015-05-03 17:42:07 -0600 | [diff] [blame] | 267 | return -ENOMEM; |
| 268 | } |
| 269 | |
| 270 | rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch); |
| 271 | |
| 272 | result = rfkill_register(bt_dev->rfk); |
| 273 | if (result) { |
| 274 | pr_err("Unable to register rfkill device\n"); |
| 275 | rfkill_destroy(bt_dev->rfk); |
| 276 | kfree(bt_dev); |
| 277 | } |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 278 | |
| 279 | return result; |
| 280 | } |
| 281 | |
Rafael J. Wysocki | 51fac83 | 2013-01-24 00:24:48 +0100 | [diff] [blame] | 282 | static int toshiba_bt_rfkill_remove(struct acpi_device *device) |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 283 | { |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 284 | struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device); |
| 285 | |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 286 | /* clean up */ |
Azael Avalos | 7ee8cd3 | 2015-05-03 17:42:07 -0600 | [diff] [blame] | 287 | if (bt_dev->rfk) { |
| 288 | rfkill_unregister(bt_dev->rfk); |
| 289 | rfkill_destroy(bt_dev->rfk); |
| 290 | } |
| 291 | |
Azael Avalos | 84c0691 | 2015-05-03 17:42:06 -0600 | [diff] [blame] | 292 | kfree(bt_dev); |
| 293 | |
Azael Avalos | 18b8696 | 2015-03-26 14:56:06 -0600 | [diff] [blame] | 294 | return toshiba_bluetooth_disable(device->handle); |
Jes Sorensen | 42b4e9e | 2009-12-16 12:08:15 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Mika Westerberg | 01d1775 | 2012-09-07 10:31:48 +0300 | [diff] [blame] | 297 | module_acpi_driver(toshiba_bt_rfkill_driver); |