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