hid-plantronics.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Plantronics USB HID Driver
  3. *
  4. * Copyright (c) 2014 JD Cole <jd.cole@plantronics.com>
  5. * Copyright (c) 2015-2018 Terry Junge <terry.junge@plantronics.com>
  6. */
  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 "hid-ids.h"
  14. #include <linux/hid.h>
  15. #include <linux/module.h>
  16. #define PLT_HID_1_0_PAGE 0xffa00000
  17. #define PLT_HID_2_0_PAGE 0xffa20000
  18. #define PLT_BASIC_TELEPHONY 0x0003
  19. #define PLT_BASIC_EXCEPTION 0x0005
  20. #define PLT_VOL_UP 0x00b1
  21. #define PLT_VOL_DOWN 0x00b2
  22. #define PLT1_VOL_UP (PLT_HID_1_0_PAGE | PLT_VOL_UP)
  23. #define PLT1_VOL_DOWN (PLT_HID_1_0_PAGE | PLT_VOL_DOWN)
  24. #define PLT2_VOL_UP (PLT_HID_2_0_PAGE | PLT_VOL_UP)
  25. #define PLT2_VOL_DOWN (PLT_HID_2_0_PAGE | PLT_VOL_DOWN)
  26. #define PLT_DA60 0xda60
  27. #define PLT_BT300_MIN 0x0413
  28. #define PLT_BT300_MAX 0x0418
  29. #define PLT_ALLOW_CONSUMER (field->application == HID_CP_CONSUMERCONTROL && \
  30. (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
  31. static int plantronics_input_mapping(struct hid_device *hdev,
  32. struct hid_input *hi,
  33. struct hid_field *field,
  34. struct hid_usage *usage,
  35. unsigned long **bit, int *max)
  36. {
  37. unsigned short mapped_key;
  38. unsigned long plt_type = (unsigned long)hid_get_drvdata(hdev);
  39. /* special case for PTT products */
  40. if (field->application == HID_GD_JOYSTICK)
  41. goto defaulted;
  42. /* handle volume up/down mapping */
  43. /* non-standard types or multi-HID interfaces - plt_type is PID */
  44. if (!(plt_type & HID_USAGE_PAGE)) {
  45. switch (plt_type) {
  46. case PLT_DA60:
  47. if (PLT_ALLOW_CONSUMER)
  48. goto defaulted;
  49. goto ignored;
  50. default:
  51. if (PLT_ALLOW_CONSUMER)
  52. goto defaulted;
  53. }
  54. }
  55. /* handle standard types - plt_type is 0xffa0uuuu or 0xffa2uuuu */
  56. /* 'basic telephony compliant' - allow default consumer page map */
  57. else if ((plt_type & HID_USAGE) >= PLT_BASIC_TELEPHONY &&
  58. (plt_type & HID_USAGE) != PLT_BASIC_EXCEPTION) {
  59. if (PLT_ALLOW_CONSUMER)
  60. goto defaulted;
  61. }
  62. /* not 'basic telephony' - apply legacy mapping */
  63. /* only map if the field is in the device's primary vendor page */
  64. else if (!((field->application ^ plt_type) & HID_USAGE_PAGE)) {
  65. switch (usage->hid) {
  66. case PLT1_VOL_UP:
  67. case PLT2_VOL_UP:
  68. mapped_key = KEY_VOLUMEUP;
  69. goto mapped;
  70. case PLT1_VOL_DOWN:
  71. case PLT2_VOL_DOWN:
  72. mapped_key = KEY_VOLUMEDOWN;
  73. goto mapped;
  74. }
  75. }
  76. /*
  77. * Future mapping of call control or other usages,
  78. * if and when keys are defined would go here
  79. * otherwise, ignore everything else that was not mapped
  80. */
  81. ignored:
  82. return -1;
  83. defaulted:
  84. hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n",
  85. usage->hid, field->application);
  86. return 0;
  87. mapped:
  88. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, mapped_key);
  89. hid_dbg(hdev, "usage: %08x (appl: %08x) - mapped to key %d\n",
  90. usage->hid, field->application, mapped_key);
  91. return 1;
  92. }
  93. static unsigned long plantronics_device_type(struct hid_device *hdev)
  94. {
  95. unsigned i, col_page;
  96. unsigned long plt_type = hdev->product;
  97. /* multi-HID interfaces? - plt_type is PID */
  98. if (plt_type >= PLT_BT300_MIN && plt_type <= PLT_BT300_MAX)
  99. goto exit;
  100. /* determine primary vendor page */
  101. for (i = 0; i < hdev->maxcollection; i++) {
  102. col_page = hdev->collection[i].usage & HID_USAGE_PAGE;
  103. if (col_page == PLT_HID_2_0_PAGE) {
  104. plt_type = hdev->collection[i].usage;
  105. break;
  106. }
  107. if (col_page == PLT_HID_1_0_PAGE)
  108. plt_type = hdev->collection[i].usage;
  109. }
  110. exit:
  111. hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type);
  112. return plt_type;
  113. }
  114. static int plantronics_probe(struct hid_device *hdev,
  115. const struct hid_device_id *id)
  116. {
  117. int ret;
  118. ret = hid_parse(hdev);
  119. if (ret) {
  120. hid_err(hdev, "parse failed\n");
  121. goto err;
  122. }
  123. hid_set_drvdata(hdev, (void *)plantronics_device_type(hdev));
  124. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
  125. HID_CONNECT_HIDINPUT_FORCE | HID_CONNECT_HIDDEV_FORCE);
  126. if (ret)
  127. hid_err(hdev, "hw start failed\n");
  128. err:
  129. return ret;
  130. }
  131. static const struct hid_device_id plantronics_devices[] = {
  132. { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) },
  133. { }
  134. };
  135. MODULE_DEVICE_TABLE(hid, plantronics_devices);
  136. static struct hid_driver plantronics_driver = {
  137. .name = "plantronics",
  138. .id_table = plantronics_devices,
  139. .input_mapping = plantronics_input_mapping,
  140. .probe = plantronics_probe,
  141. };
  142. module_hid_driver(plantronics_driver);
  143. MODULE_AUTHOR("JD Cole <jd.cole@plantronics.com>");
  144. MODULE_AUTHOR("Terry Junge <terry.junge@plantronics.com>");
  145. MODULE_DESCRIPTION("Plantronics USB HID Driver");
  146. MODULE_LICENSE("GPL");