toshiba_bluetooth.c 4.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Toshiba Bluetooth Enable Driver
 *
 * Copyright (C) 2009 Jes Sorensen <Jes.Sorensen@gmail.com>
 *
 * Thanks to Matthew Garrett for background info on ACPI innards which
 * normal people aren't meant to understand :-)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Note the Toshiba Bluetooth RFKill switch seems to be a strange
 * fish. It only provides a BT event when the switch is flipped to
 * the 'on' position. When flipping it to 'off', the USB device is
 * simply pulled away underneath us, without any BT event being
 * delivered.
 */

20 21
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

22 23 24 25
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
26
#include <linux/acpi.h>
27 28 29 30 31 32

MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
MODULE_LICENSE("GPL");

static int toshiba_bt_rfkill_add(struct acpi_device *device);
33
static int toshiba_bt_rfkill_remove(struct acpi_device *device);
34 35 36 37 38 39 40 41
static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);

static const struct acpi_device_id bt_device_ids[] = {
	{ "TOS6205", 0},
	{ "", 0},
};
MODULE_DEVICE_TABLE(acpi, bt_device_ids);

42
#ifdef CONFIG_PM_SLEEP
43
static int toshiba_bt_resume(struct device *dev);
44
#endif
45 46
static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);

47 48 49 50 51 52 53 54 55 56
static struct acpi_driver toshiba_bt_rfkill_driver = {
	.name =		"Toshiba BT",
	.class =	"Toshiba",
	.ids =		bt_device_ids,
	.ops =		{
				.add =		toshiba_bt_rfkill_add,
				.remove =	toshiba_bt_rfkill_remove,
				.notify =	toshiba_bt_rfkill_notify,
			},
	.owner = 	THIS_MODULE,
57
	.drv.pm =	&toshiba_bt_pm,
58 59
};

60 61 62 63 64
static int toshiba_bluetooth_present(acpi_handle handle)
{
	acpi_status result;
	u64 bt_present;

65 66 67 68 69
	/*
	 * Some Toshiba laptops may have a fake TOS6205 device in
	 * their ACPI BIOS, so query the _STA method to see if there
	 * is really anything there.
	 */
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
	if (ACPI_FAILURE(result)) {
		pr_err("ACPI call to query Bluetooth presence failed");
		return -ENXIO;
	} else if (!bt_present) {
		pr_info("Bluetooth device not present\n");
		return -ENODEV;
	}

	return 0;
}

static int toshiba_bluetooth_status(acpi_handle handle)
{
	acpi_status result;
	u64 status;

	result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
	if (ACPI_FAILURE(result)) {
		pr_err("Could not get Bluetooth device status\n");
		return -ENXIO;
	}

	pr_info("Bluetooth status %llu\n", status);

	return status;
}
97 98 99 100

static int toshiba_bluetooth_enable(acpi_handle handle)
{
	acpi_status res1, res2;
L
Lin Ming 已提交
101
	u64 result;
102 103 104 105 106 107 108 109 110 111 112 113

	/*
	 * Query ACPI to verify RFKill switch is set to 'on'.
	 * If not, we return silently, no need to report it as
	 * an error.
	 */
	res1 = acpi_evaluate_integer(handle, "BTST", NULL, &result);
	if (ACPI_FAILURE(res1))
		return res1;
	if (!(result & 0x01))
		return 0;

114
	pr_info("Re-enabling Toshiba Bluetooth\n");
115 116 117 118 119
	res1 = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
	res2 = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
	if (!ACPI_FAILURE(res1) || !ACPI_FAILURE(res2))
		return 0;

120
	pr_warn("Failed to re-enable Toshiba Bluetooth\n");
121 122 123 124

	return -ENODEV;
}

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
static int toshiba_bluetooth_disable(acpi_handle handle)
{
	acpi_status result;

	result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
	if (ACPI_FAILURE(result)) {
		pr_err("Could not power OFF Bluetooth device\n");
		return -ENXIO;
	}

	result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
	if (ACPI_FAILURE(result)) {
		pr_err("Could not detach USB Bluetooth device\n");
		return -ENXIO;
	}

	return 0;
}

144 145 146 147 148
static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
{
	toshiba_bluetooth_enable(device->handle);
}

149
#ifdef CONFIG_PM_SLEEP
150
static int toshiba_bt_resume(struct device *dev)
151
{
152
	return toshiba_bluetooth_enable(to_acpi_device(dev)->handle);
153
}
154
#endif
155 156 157

static int toshiba_bt_rfkill_add(struct acpi_device *device)
{
158
	int result;
159

160 161 162
	result = toshiba_bluetooth_present(device->handle);
	if (result)
		return result;
163

164 165 166 167 168 169
	pr_info("Toshiba ACPI Bluetooth device driver\n");

	/* Enable the BT device */
	result = toshiba_bluetooth_enable(device->handle);
	if (result)
		return result;
170 171 172 173

	return result;
}

174
static int toshiba_bt_rfkill_remove(struct acpi_device *device)
175 176
{
	/* clean up */
177
	return toshiba_bluetooth_disable(device->handle);
178 179
}

180
module_acpi_driver(toshiba_bt_rfkill_driver);