toshiba_bluetooth.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Toshiba Bluetooth Enable Driver
  3. *
  4. * Copyright (C) 2009 Jes Sorensen <Jes.Sorensen@gmail.com>
  5. * Copyright (C) 2015 Azael Avalos <coproscefalo@gmail.com>
  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.
  13. *
  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
  18. * delivered.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/acpi.h>
  26. #define BT_KILLSWITCH_MASK 0x01
  27. #define BT_PLUGGED_MASK 0x40
  28. #define BT_POWER_MASK 0x80
  29. MODULE_AUTHOR("Jes Sorensen <Jes.Sorensen@gmail.com>");
  30. MODULE_DESCRIPTION("Toshiba Laptop ACPI Bluetooth Enable Driver");
  31. MODULE_LICENSE("GPL");
  32. static int toshiba_bt_rfkill_add(struct acpi_device *device);
  33. static int toshiba_bt_rfkill_remove(struct acpi_device *device);
  34. static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event);
  35. static const struct acpi_device_id bt_device_ids[] = {
  36. { "TOS6205", 0},
  37. { "", 0},
  38. };
  39. MODULE_DEVICE_TABLE(acpi, bt_device_ids);
  40. #ifdef CONFIG_PM_SLEEP
  41. static int toshiba_bt_resume(struct device *dev);
  42. #endif
  43. static SIMPLE_DEV_PM_OPS(toshiba_bt_pm, NULL, toshiba_bt_resume);
  44. static struct acpi_driver toshiba_bt_rfkill_driver = {
  45. .name = "Toshiba BT",
  46. .class = "Toshiba",
  47. .ids = bt_device_ids,
  48. .ops = {
  49. .add = toshiba_bt_rfkill_add,
  50. .remove = toshiba_bt_rfkill_remove,
  51. .notify = toshiba_bt_rfkill_notify,
  52. },
  53. .owner = THIS_MODULE,
  54. .drv.pm = &toshiba_bt_pm,
  55. };
  56. static int toshiba_bluetooth_present(acpi_handle handle)
  57. {
  58. acpi_status result;
  59. u64 bt_present;
  60. /*
  61. * Some Toshiba laptops may have a fake TOS6205 device in
  62. * their ACPI BIOS, so query the _STA method to see if there
  63. * is really anything there.
  64. */
  65. result = acpi_evaluate_integer(handle, "_STA", NULL, &bt_present);
  66. if (ACPI_FAILURE(result)) {
  67. pr_err("ACPI call to query Bluetooth presence failed");
  68. return -ENXIO;
  69. } else if (!bt_present) {
  70. pr_info("Bluetooth device not present\n");
  71. return -ENODEV;
  72. }
  73. return 0;
  74. }
  75. static int toshiba_bluetooth_status(acpi_handle handle)
  76. {
  77. acpi_status result;
  78. u64 status;
  79. result = acpi_evaluate_integer(handle, "BTST", NULL, &status);
  80. if (ACPI_FAILURE(result)) {
  81. pr_err("Could not get Bluetooth device status\n");
  82. return -ENXIO;
  83. }
  84. pr_info("Bluetooth status %llu\n", status);
  85. return status;
  86. }
  87. static int toshiba_bluetooth_enable(acpi_handle handle)
  88. {
  89. acpi_status result;
  90. bool killswitch;
  91. bool powered;
  92. bool plugged;
  93. int status;
  94. /*
  95. * Query ACPI to verify RFKill switch is set to 'on'.
  96. * If not, we return silently, no need to report it as
  97. * an error.
  98. */
  99. status = toshiba_bluetooth_status(handle);
  100. if (status < 0)
  101. return status;
  102. killswitch = (status & BT_KILLSWITCH_MASK) ? true : false;
  103. powered = (status & BT_POWER_MASK) ? true : false;
  104. plugged = (status & BT_PLUGGED_MASK) ? true : false;
  105. if (!killswitch)
  106. return 0;
  107. /*
  108. * This check ensures to only enable the device if it is powered
  109. * off or detached, as some recent devices somehow pass the killswitch
  110. * test, causing a loop enabling/disabling the device, see bug 93911.
  111. */
  112. if (powered || plugged)
  113. return 0;
  114. result = acpi_evaluate_object(handle, "AUSB", NULL, NULL);
  115. if (ACPI_FAILURE(result)) {
  116. pr_err("Could not attach USB Bluetooth device\n");
  117. return -ENXIO;
  118. }
  119. result = acpi_evaluate_object(handle, "BTPO", NULL, NULL);
  120. if (ACPI_FAILURE(result)) {
  121. pr_err("Could not power ON Bluetooth device\n");
  122. return -ENXIO;
  123. }
  124. return 0;
  125. }
  126. static int toshiba_bluetooth_disable(acpi_handle handle)
  127. {
  128. acpi_status result;
  129. result = acpi_evaluate_object(handle, "BTPF", NULL, NULL);
  130. if (ACPI_FAILURE(result)) {
  131. pr_err("Could not power OFF Bluetooth device\n");
  132. return -ENXIO;
  133. }
  134. result = acpi_evaluate_object(handle, "DUSB", NULL, NULL);
  135. if (ACPI_FAILURE(result)) {
  136. pr_err("Could not detach USB Bluetooth device\n");
  137. return -ENXIO;
  138. }
  139. return 0;
  140. }
  141. static void toshiba_bt_rfkill_notify(struct acpi_device *device, u32 event)
  142. {
  143. toshiba_bluetooth_enable(device->handle);
  144. }
  145. #ifdef CONFIG_PM_SLEEP
  146. static int toshiba_bt_resume(struct device *dev)
  147. {
  148. return toshiba_bluetooth_enable(to_acpi_device(dev)->handle);
  149. }
  150. #endif
  151. static int toshiba_bt_rfkill_add(struct acpi_device *device)
  152. {
  153. int result;
  154. result = toshiba_bluetooth_present(device->handle);
  155. if (result)
  156. return result;
  157. pr_info("Toshiba ACPI Bluetooth device driver\n");
  158. /* Enable the BT device */
  159. result = toshiba_bluetooth_enable(device->handle);
  160. if (result)
  161. return result;
  162. return result;
  163. }
  164. static int toshiba_bt_rfkill_remove(struct acpi_device *device)
  165. {
  166. /* clean up */
  167. return toshiba_bluetooth_disable(device->handle);
  168. }
  169. module_acpi_driver(toshiba_bt_rfkill_driver);