wiimote.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011-2012 David Herrmann <dh.herrmann@googlemail.com>
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdbool.h>
  14. #include <glib.h>
  15. #include "bluetooth/bluetooth.h"
  16. #include "bluetooth/sdp.h"
  17. #include "src/plugin.h"
  18. #include "src/adapter.h"
  19. #include "src/device.h"
  20. #include "src/log.h"
  21. #include "src/storage.h"
  22. /*
  23. * Nintendo Wii Remote devices require the bdaddr of the host as pin input for
  24. * authentication. This plugin registers a pin-callback and forces this pin
  25. * to be used for authentication.
  26. *
  27. * There are two ways to place the wiimote into discoverable mode.
  28. * - Pressing the red-sync button on the back of the wiimote. This module
  29. * supports pairing via this method. Auto-reconnect should be possible after
  30. * the device was paired once.
  31. * - Pressing the 1+2 buttons on the front of the wiimote. This module does
  32. * not support this method since this method never enables auto-reconnect.
  33. * Hence, pairing is not needed. Use it without pairing if you want.
  34. * After connecting the wiimote you should immediately connect to the input
  35. * service of the wiimote. If you don't, the wiimote will close the connection.
  36. * The wiimote waits about 5 seconds until it turns off again.
  37. * Auto-reconnect is only enabled when pairing with the wiimote via the red
  38. * sync-button and then connecting to the input service. If you do not connect
  39. * to the input service, then auto-reconnect is not enabled.
  40. * If enabled, the wiimote connects to the host automatically when any button
  41. * is pressed.
  42. */
  43. static uint16_t wii_ids[][2] = {
  44. { 0x057e, 0x0306 }, /* 1st gen */
  45. { 0x054c, 0x0306 }, /* LEGO wiimote */
  46. { 0x057e, 0x0330 }, /* 2nd gen */
  47. };
  48. static const char *wii_names[] = {
  49. "Nintendo RVL-CNT-01", /* 1st gen */
  50. "Nintendo RVL-CNT-01-TR", /* 2nd gen */
  51. "Nintendo RVL-CNT-01-UC", /* Wii U Pro Controller */
  52. "Nintendo RVL-WBC-01", /* Balance Board */
  53. };
  54. static ssize_t wii_pincb(struct btd_adapter *adapter, struct btd_device *device,
  55. char *pinbuf, bool *display,
  56. unsigned int attempt)
  57. {
  58. uint16_t vendor, product;
  59. char addr[18], name[25];
  60. unsigned int i;
  61. /* Only try the pin code once per device. If it's not correct then it's
  62. * an unknown device. */
  63. if (attempt > 1)
  64. return 0;
  65. ba2str(device_get_address(device), addr);
  66. vendor = btd_device_get_vendor(device);
  67. product = btd_device_get_product(device);
  68. device_get_name(device, name, sizeof(name));
  69. for (i = 0; i < G_N_ELEMENTS(wii_ids); ++i) {
  70. if (vendor == wii_ids[i][0] && product == wii_ids[i][1])
  71. goto found;
  72. }
  73. for (i = 0; i < G_N_ELEMENTS(wii_names); ++i) {
  74. if (g_str_equal(name, wii_names[i]))
  75. goto found;
  76. }
  77. return 0;
  78. found:
  79. DBG("Forcing fixed pin on detected wiimote %s", addr);
  80. memcpy(pinbuf, btd_adapter_get_address(adapter), 6);
  81. return 6;
  82. }
  83. static int wii_probe(struct btd_adapter *adapter)
  84. {
  85. btd_adapter_register_pin_cb(adapter, wii_pincb);
  86. return 0;
  87. }
  88. static void wii_remove(struct btd_adapter *adapter)
  89. {
  90. btd_adapter_unregister_pin_cb(adapter, wii_pincb);
  91. }
  92. static struct btd_adapter_driver wii_driver = {
  93. .name = "wiimote",
  94. .probe = wii_probe,
  95. .remove = wii_remove,
  96. };
  97. static int wii_init(void)
  98. {
  99. return btd_register_adapter_driver(&wii_driver);
  100. }
  101. static void wii_exit(void)
  102. {
  103. btd_unregister_adapter_driver(&wii_driver);
  104. }
  105. BLUETOOTH_PLUGIN_DEFINE(wiimote, VERSION,
  106. BLUETOOTH_PLUGIN_PRIORITY_LOW, wii_init, wii_exit)