blob: a3b2d3883dd64f6d584d74136fd00becc72cf1d4 [file] [log] [blame]
Jes Sorensen42b4e9e2009-12-16 12:08:15 -05001/*
2 * Toshiba Bluetooth Enable Driver
3 *
4 * Copyright (C) 2009 Jes Sorensen <Jes.Sorensen@gmail.com>
Azael Avalos5d3fc1d2015-03-26 14:56:07 -06005 * Copyright (C) 2015 Azael Avalos <coproscefalo@gmail.com>
Jes Sorensen42b4e9e2009-12-16 12:08:15 -05006 *
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 Sorensen42b4e9e2009-12-16 12:08:15 -050013 */
14
Joe Perches7e334602011-03-29 15:21:52 -070015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Jes Sorensen42b4e9e2009-12-16 12:08:15 -050017#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/init.h>
20#include <linux/types.h>
Lv Zheng8b484632013-12-03 08:49:16 +080021#include <linux/acpi.h>
Azael Avalos7ee8cd32015-05-03 17:42:07 -060022#include <linux/rfkill.h>
Jes Sorensen42b4e9e2009-12-16 12:08:15 -050023
Azael Avalos5d3fc1d2015-03-26 14:56:07 -060024#define BT_KILLSWITCH_MASK 0x01
25#define BT_PLUGGED_MASK 0x40
26#define BT_POWER_MASK 0x80
27
Jes Sorensen42b4e9e2009-12-16 12:08:15 -050028MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
29MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
30MODULE_LICENSE("GPL");
31
Azael Avalos84c06912015-05-03 17:42:06 -060032struct toshiba_bluetooth_dev {
33 struct acpi_device *acpi_dev;
Azael Avalos7ee8cd32015-05-03 17:42:07 -060034 struct rfkill *rfk;
Azael Avalos84c06912015-05-03 17:42:06 -060035
36 bool killswitch;
37 bool plugged;
38 bool powered;
39};
40
Jes Sorensen42b4e9e2009-12-16 12:08:15 -050041static int toshiba_bt_rfkill_add(struct acpi_device *device);
Rafael J. Wysocki51fac832013-01-24 00:24:48 +010042static int toshiba_bt_rfkill_remove(struct acpi_device *device);
Jes Sorensen42b4e9e2009-12-16 12:08:15 -050043static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);
Jes Sorensen42b4e9e2009-12-16 12:08:15 -050044
45static const struct acpi_device_id bt_device_ids[] = {
46 { "TOS6205", 0},
47 { "", 0},
48};
49MODULE_DEVICE_TABLE(acpi, bt_device_ids);
50
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +020051#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockid69239a2012-06-27 23:27:48 +020052static int toshiba_bt_resume(struct device *dev);
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +020053#endif
Rafael J. Wysockid69239a2012-06-27 23:27:48 +020054static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);
55
Jes Sorensen42b4e9e2009-12-16 12:08:15 -050056static 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 Sorensen42b4e9e2009-12-16 12:08:15 -050064 },
65 .owner = THIS_MODULE,
Rafael J. Wysockid69239a2012-06-27 23:27:48 +020066 .drv.pm = &toshiba_bt_pm,
Jes Sorensen42b4e9e2009-12-16 12:08:15 -050067};
68
Azael Avalosbb2ea962015-03-26 14:56:05 -060069static int toshiba_bluetooth_present(acpi_handle handle)
70{
71 acpi_status result;
72 u64 bt_present;
73
Azael Avalos18b86962015-03-26 14:56:06 -060074 /*
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 Avalosbb2ea962015-03-26 14:56:05 -060079 result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
80 if (ACPI_FAILURE(result)) {
81 pr_err("ACPI call to query Bluetooth presence failed");
82 return -ENXIO;
83 } else if (!bt_present) {
84 pr_info("Bluetooth device not present\n");
85 return -ENODEV;
86 }
87
88 return 0;
89}
90
91static int toshiba_bluetooth_status(acpi_handle handle)
92{
93 acpi_status result;
94 u64 status;
95
96 result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
97 if (ACPI_FAILURE(result)) {
98 pr_err("Could not get Bluetooth device status\n");
99 return -ENXIO;
100 }
101
102 pr_info("Bluetooth status %llu\n", status);
103
104 return status;
105}
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500106
107static int toshiba_bluetooth_enable(acpi_handle handle)
108{
Azael Avalos5d3fc1d2015-03-26 14:56:07 -0600109 acpi_status result;
110 bool killswitch;
111 bool powered;
112 bool plugged;
113 int status;
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500114
115 /*
116 * Query ACPI to verify RFKill switch is set to 'on'.
117 * If not, we return silently, no need to report it as
118 * an error.
119 */
Azael Avalos5d3fc1d2015-03-26 14:56:07 -0600120 status = toshiba_bluetooth_status(handle);
121 if (status < 0)
122 return status;
123
124 killswitch = (status & BT_KILLSWITCH_MASK) ? true : false;
125 powered = (status & BT_POWER_MASK) ? true : false;
126 plugged = (status & BT_PLUGGED_MASK) ? true : false;
127
128 if (!killswitch)
129 return 0;
130 /*
131 * This check ensures to only enable the device if it is powered
132 * off or detached, as some recent devices somehow pass the killswitch
133 * test, causing a loop enabling/disabling the device, see bug 93911.
134 */
135 if (powered || plugged)
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500136 return 0;
137
Azael Avalos5d3fc1d2015-03-26 14:56:07 -0600138 result = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
139 if (ACPI_FAILURE(result)) {
140 pr_err("Could not attach USB Bluetooth device\n");
141 return -ENXIO;
142 }
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500143
Azael Avalos5d3fc1d2015-03-26 14:56:07 -0600144 result = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
145 if (ACPI_FAILURE(result)) {
146 pr_err("Could not power ON Bluetooth device\n");
147 return -ENXIO;
148 }
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500149
Azael Avalos5d3fc1d2015-03-26 14:56:07 -0600150 return 0;
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500151}
152
Azael Avalosbb2ea962015-03-26 14:56:05 -0600153static int toshiba_bluetooth_disable(acpi_handle handle)
154{
155 acpi_status result;
156
157 result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
158 if (ACPI_FAILURE(result)) {
159 pr_err("Could not power OFF Bluetooth device\n");
160 return -ENXIO;
161 }
162
163 result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
164 if (ACPI_FAILURE(result)) {
165 pr_err("Could not detach USB Bluetooth device\n");
166 return -ENXIO;
167 }
168
169 return 0;
170}
171
Azael Avalos84c06912015-05-03 17:42:06 -0600172/* Helper function */
173static int toshiba_bluetooth_sync_status(struct toshiba_bluetooth_dev *bt_dev)
174{
175 int status;
176
177 status = toshiba_bluetooth_status(bt_dev->acpi_dev->handle);
178 if (status < 0) {
179 pr_err("Could not sync bluetooth device status\n");
180 return status;
181 }
182
183 bt_dev->killswitch = (status & BT_KILLSWITCH_MASK) ? true : false;
184 bt_dev->plugged = (status & BT_PLUGGED_MASK) ? true : false;
185 bt_dev->powered = (status & BT_POWER_MASK) ? true : false;
186
187 return 0;
188}
189
Azael Avalos7ee8cd32015-05-03 17:42:07 -0600190/* RFKill handlers */
191static int bt_rfkill_set_block(void *data, bool blocked)
192{
193 struct toshiba_bluetooth_dev *bt_dev = data;
194 int ret;
195
196 ret = toshiba_bluetooth_sync_status(bt_dev);
197 if (ret)
198 return ret;
199
200 if (!bt_dev->killswitch)
201 return 0;
202
203 if (blocked)
204 ret = toshiba_bluetooth_disable(bt_dev->acpi_dev->handle);
205 else
206 ret = toshiba_bluetooth_enable(bt_dev->acpi_dev->handle);
207
208 return ret;
209}
210
211static void bt_rfkill_poll(struct rfkill *rfkill, void *data)
212{
213 struct toshiba_bluetooth_dev *bt_dev = data;
214
215 if (toshiba_bluetooth_sync_status(bt_dev))
216 return;
217
218 /*
219 * Note the Toshiba Bluetooth RFKill switch seems to be a strange
220 * fish. It only provides a BT event when the switch is flipped to
221 * the 'on' position. When flipping it to 'off', the USB device is
222 * simply pulled away underneath us, without any BT event being
223 * delivered.
224 */
225 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
226}
227
228static const struct rfkill_ops rfk_ops = {
229 .set_block = bt_rfkill_set_block,
230 .poll = bt_rfkill_poll,
231};
232
Azael Avalos84c06912015-05-03 17:42:06 -0600233/* ACPI driver functions */
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500234static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
235{
236 toshiba_bluetooth_enable(device->handle);
237}
238
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200239#ifdef CONFIG_PM_SLEEP
Rafael J. Wysockid69239a2012-06-27 23:27:48 +0200240static int toshiba_bt_resume(struct device *dev)
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500241{
Rafael J. Wysockid69239a2012-06-27 23:27:48 +0200242 return toshiba_bluetooth_enable(to_acpi_device(dev)->handle);
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500243}
Rafael J. Wysocki3567a4e22012-08-09 23:00:13 +0200244#endif
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500245
246static int toshiba_bt_rfkill_add(struct acpi_device *device)
247{
Azael Avalos84c06912015-05-03 17:42:06 -0600248 struct toshiba_bluetooth_dev *bt_dev;
Azael Avalos18b86962015-03-26 14:56:06 -0600249 int result;
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500250
Azael Avalos18b86962015-03-26 14:56:06 -0600251 result = toshiba_bluetooth_present(device->handle);
252 if (result)
253 return result;
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500254
Azael Avalos18b86962015-03-26 14:56:06 -0600255 pr_info("Toshiba ACPI Bluetooth device driver\n");
256
Azael Avalos84c06912015-05-03 17:42:06 -0600257 bt_dev = kzalloc(sizeof(*bt_dev), GFP_KERNEL);
258 if (!bt_dev)
259 return -ENOMEM;
260 bt_dev->acpi_dev = device;
261 device->driver_data = bt_dev;
262 dev_set_drvdata(&device->dev, bt_dev);
263
264 result = toshiba_bluetooth_sync_status(bt_dev);
265 if (result) {
266 kfree(bt_dev);
267 return result;
268 }
269
Azael Avalos7ee8cd32015-05-03 17:42:07 -0600270 bt_dev->rfk = rfkill_alloc("Toshiba Bluetooth",
271 &device->dev,
272 RFKILL_TYPE_BLUETOOTH,
273 &rfk_ops,
274 bt_dev);
275 if (!bt_dev->rfk) {
276 pr_err("Unable to allocate rfkill device\n");
Azael Avalos84c06912015-05-03 17:42:06 -0600277 kfree(bt_dev);
Azael Avalos7ee8cd32015-05-03 17:42:07 -0600278 return -ENOMEM;
279 }
280
281 rfkill_set_hw_state(bt_dev->rfk, !bt_dev->killswitch);
282
283 result = rfkill_register(bt_dev->rfk);
284 if (result) {
285 pr_err("Unable to register rfkill device\n");
286 rfkill_destroy(bt_dev->rfk);
287 kfree(bt_dev);
288 }
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500289
290 return result;
291}
292
Rafael J. Wysocki51fac832013-01-24 00:24:48 +0100293static int toshiba_bt_rfkill_remove(struct acpi_device *device)
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500294{
Azael Avalos84c06912015-05-03 17:42:06 -0600295 struct toshiba_bluetooth_dev *bt_dev = acpi_driver_data(device);
296
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500297 /* clean up */
Azael Avalos7ee8cd32015-05-03 17:42:07 -0600298 if (bt_dev->rfk) {
299 rfkill_unregister(bt_dev->rfk);
300 rfkill_destroy(bt_dev->rfk);
301 }
302
Azael Avalos84c06912015-05-03 17:42:06 -0600303 kfree(bt_dev);
304
Azael Avalos18b86962015-03-26 14:56:06 -0600305 return toshiba_bluetooth_disable(device->handle);
Jes Sorensen42b4e9e2009-12-16 12:08:15 -0500306}
307
Mika Westerberg01d17752012-09-07 10:31:48 +0300308module_acpi_driver(toshiba_bt_rfkill_driver);