hid-plantronics.c 4.5 KB

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