hid-gfrm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for Google Fiber TV Box remote controls
  4. *
  5. * Copyright (c) 2014-2015 Google Inc.
  6. *
  7. * Author: Petri Gynther <pgynther@google.com>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/hid.h>
  11. #include <linux/input.h>
  12. #include <linux/module.h>
  13. #include "hid-ids.h"
  14. #define GFRM100 1 /* Google Fiber GFRM100 (Bluetooth classic) */
  15. #define GFRM200 2 /* Google Fiber GFRM200 (Bluetooth LE) */
  16. #define GFRM100_SEARCH_KEY_REPORT_ID 0xF7
  17. #define GFRM100_SEARCH_KEY_DOWN 0x0
  18. #define GFRM100_SEARCH_KEY_AUDIO_DATA 0x1
  19. #define GFRM100_SEARCH_KEY_UP 0x2
  20. static u8 search_key_dn[3] = {0x40, 0x21, 0x02};
  21. static u8 search_key_up[3] = {0x40, 0x00, 0x00};
  22. static int gfrm_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  23. struct hid_field *field, struct hid_usage *usage,
  24. unsigned long **bit, int *max)
  25. {
  26. unsigned long hdev_type = (unsigned long) hid_get_drvdata(hdev);
  27. if (hdev_type == GFRM100) {
  28. if (usage->hid == (HID_UP_CONSUMER | 0x4)) {
  29. /* Consumer.0004 -> KEY_INFO */
  30. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_INFO);
  31. return 1;
  32. }
  33. if (usage->hid == (HID_UP_CONSUMER | 0x41)) {
  34. /* Consumer.0041 -> KEY_OK */
  35. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_OK);
  36. return 1;
  37. }
  38. }
  39. return 0;
  40. }
  41. static int gfrm_raw_event(struct hid_device *hdev, struct hid_report *report,
  42. u8 *data, int size)
  43. {
  44. unsigned long hdev_type = (unsigned long) hid_get_drvdata(hdev);
  45. int ret = 0;
  46. if (hdev_type != GFRM100)
  47. return 0;
  48. if (size < 2 || data[0] != GFRM100_SEARCH_KEY_REPORT_ID)
  49. return 0;
  50. /*
  51. * Convert GFRM100 Search key reports into Consumer.0221 (Key.Search)
  52. * reports. Ignore audio data.
  53. */
  54. switch (data[1]) {
  55. case GFRM100_SEARCH_KEY_DOWN:
  56. ret = hid_report_raw_event(hdev, HID_INPUT_REPORT, search_key_dn,
  57. sizeof(search_key_dn), 1);
  58. break;
  59. case GFRM100_SEARCH_KEY_AUDIO_DATA:
  60. break;
  61. case GFRM100_SEARCH_KEY_UP:
  62. ret = hid_report_raw_event(hdev, HID_INPUT_REPORT, search_key_up,
  63. sizeof(search_key_up), 1);
  64. break;
  65. default:
  66. break;
  67. }
  68. return (ret < 0) ? ret : -1;
  69. }
  70. static int gfrm_input_configured(struct hid_device *hid, struct hid_input *hidinput)
  71. {
  72. /*
  73. * Enable software autorepeat with:
  74. * - repeat delay: 400 msec
  75. * - repeat period: 100 msec
  76. */
  77. input_enable_softrepeat(hidinput->input, 400, 100);
  78. return 0;
  79. }
  80. static int gfrm_probe(struct hid_device *hdev, const struct hid_device_id *id)
  81. {
  82. int ret;
  83. hid_set_drvdata(hdev, (void *) id->driver_data);
  84. ret = hid_parse(hdev);
  85. if (ret)
  86. goto done;
  87. if (id->driver_data == GFRM100) {
  88. /*
  89. * GFRM100 HID Report Descriptor does not describe the Search
  90. * key reports. Thus, we need to add it manually here, so that
  91. * those reports reach gfrm_raw_event() from hid_input_report().
  92. */
  93. if (!hid_register_report(hdev, HID_INPUT_REPORT,
  94. GFRM100_SEARCH_KEY_REPORT_ID, 0)) {
  95. ret = -ENOMEM;
  96. goto done;
  97. }
  98. }
  99. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  100. done:
  101. return ret;
  102. }
  103. static const struct hid_device_id gfrm_devices[] = {
  104. { HID_BLUETOOTH_DEVICE(0x58, 0x2000),
  105. .driver_data = GFRM100 },
  106. { HID_BLUETOOTH_DEVICE(0x471, 0x2210),
  107. .driver_data = GFRM200 },
  108. { }
  109. };
  110. MODULE_DEVICE_TABLE(hid, gfrm_devices);
  111. static struct hid_driver gfrm_driver = {
  112. .name = "gfrm",
  113. .id_table = gfrm_devices,
  114. .probe = gfrm_probe,
  115. .input_mapping = gfrm_input_mapping,
  116. .raw_event = gfrm_raw_event,
  117. .input_configured = gfrm_input_configured,
  118. };
  119. module_hid_driver(gfrm_driver);
  120. MODULE_AUTHOR("Petri Gynther <pgynther@google.com>");
  121. MODULE_DESCRIPTION("Google Fiber TV Box remote control driver");
  122. MODULE_LICENSE("GPL");