hid-gfrm.c 3.9 KB

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