hid-cmedia.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HID driver for CMedia CM6533 audio jack controls
  4. * and HS100B mute buttons
  5. *
  6. * Copyright (C) 2015 Ben Chen <[email protected]>
  7. * Copyright (C) 2021 Thomas Weißschuh <[email protected]>
  8. */
  9. #include <linux/device.h>
  10. #include <linux/hid.h>
  11. #include <linux/module.h>
  12. #include "hid-ids.h"
  13. MODULE_AUTHOR("Ben Chen");
  14. MODULE_AUTHOR("Thomas Weißschuh");
  15. MODULE_DESCRIPTION("CM6533 HID jack controls and HS100B mute button");
  16. MODULE_LICENSE("GPL");
  17. #define CM6533_JD_TYPE_COUNT 1
  18. #define CM6533_JD_RAWEV_LEN 16
  19. #define CM6533_JD_SFX_OFFSET 8
  20. #define HS100B_RDESC_ORIG_SIZE 60
  21. /* Fixed report descriptor of HS-100B audio chip
  22. * Bit 4 is an abolute Microphone mute usage instead of being unassigned.
  23. */
  24. static __u8 hs100b_rdesc_fixed[] = {
  25. 0x05, 0x0C, /* Usage Page (Consumer), */
  26. 0x09, 0x01, /* Usage (Consumer Control), */
  27. 0xA1, 0x01, /* Collection (Application), */
  28. 0x15, 0x00, /* Logical Minimum (0), */
  29. 0x25, 0x01, /* Logical Maximum (1), */
  30. 0x09, 0xE9, /* Usage (Volume Inc), */
  31. 0x09, 0xEA, /* Usage (Volume Dec), */
  32. 0x75, 0x01, /* Report Size (1), */
  33. 0x95, 0x02, /* Report Count (2), */
  34. 0x81, 0x02, /* Input (Variable), */
  35. 0x09, 0xE2, /* Usage (Mute), */
  36. 0x95, 0x01, /* Report Count (1), */
  37. 0x81, 0x06, /* Input (Variable, Relative), */
  38. 0x05, 0x0B, /* Usage Page (Telephony), */
  39. 0x09, 0x2F, /* Usage (2Fh), */
  40. 0x81, 0x02, /* Input (Variable), */
  41. 0x09, 0x20, /* Usage (20h), */
  42. 0x81, 0x06, /* Input (Variable, Relative), */
  43. 0x05, 0x0C, /* Usage Page (Consumer), */
  44. 0x09, 0x00, /* Usage (00h), */
  45. 0x95, 0x03, /* Report Count (3), */
  46. 0x81, 0x02, /* Input (Variable), */
  47. 0x26, 0xFF, 0x00, /* Logical Maximum (255), */
  48. 0x09, 0x00, /* Usage (00h), */
  49. 0x75, 0x08, /* Report Size (8), */
  50. 0x95, 0x03, /* Report Count (3), */
  51. 0x81, 0x02, /* Input (Variable), */
  52. 0x09, 0x00, /* Usage (00h), */
  53. 0x95, 0x04, /* Report Count (4), */
  54. 0x91, 0x02, /* Output (Variable), */
  55. 0xC0 /* End Collection */
  56. };
  57. /*
  58. *
  59. *CM6533 audio jack HID raw events:
  60. *
  61. *Plug in:
  62. *01000600 002083xx 080008c0 10000000
  63. *about 3 seconds later...
  64. *01000a00 002083xx 08000380 10000000
  65. *01000600 002083xx 08000380 10000000
  66. *
  67. *Plug out:
  68. *01000400 002083xx 080008c0 x0000000
  69. */
  70. static const u8 ji_sfx[] = { 0x08, 0x00, 0x08, 0xc0 };
  71. static const u8 ji_in[] = { 0x01, 0x00, 0x06, 0x00 };
  72. static const u8 ji_out[] = { 0x01, 0x00, 0x04, 0x00 };
  73. static int jack_switch_types[CM6533_JD_TYPE_COUNT] = {
  74. SW_HEADPHONE_INSERT,
  75. };
  76. struct cmhid {
  77. struct input_dev *input_dev;
  78. struct hid_device *hid;
  79. unsigned short switch_map[CM6533_JD_TYPE_COUNT];
  80. };
  81. static void hp_ev(struct hid_device *hid, struct cmhid *cm, int value)
  82. {
  83. input_report_switch(cm->input_dev, SW_HEADPHONE_INSERT, value);
  84. input_sync(cm->input_dev);
  85. }
  86. static int cmhid_raw_event(struct hid_device *hid, struct hid_report *report,
  87. u8 *data, int len)
  88. {
  89. struct cmhid *cm = hid_get_drvdata(hid);
  90. if (len != CM6533_JD_RAWEV_LEN)
  91. goto out;
  92. if (memcmp(data+CM6533_JD_SFX_OFFSET, ji_sfx, sizeof(ji_sfx)))
  93. goto out;
  94. if (!memcmp(data, ji_out, sizeof(ji_out))) {
  95. hp_ev(hid, cm, 0);
  96. goto out;
  97. }
  98. if (!memcmp(data, ji_in, sizeof(ji_in))) {
  99. hp_ev(hid, cm, 1);
  100. goto out;
  101. }
  102. out:
  103. return 0;
  104. }
  105. static int cmhid_input_configured(struct hid_device *hid,
  106. struct hid_input *hidinput)
  107. {
  108. struct input_dev *input_dev = hidinput->input;
  109. struct cmhid *cm = hid_get_drvdata(hid);
  110. int i;
  111. cm->input_dev = input_dev;
  112. memcpy(cm->switch_map, jack_switch_types, sizeof(cm->switch_map));
  113. input_dev->evbit[0] = BIT(EV_SW);
  114. for (i = 0; i < CM6533_JD_TYPE_COUNT; i++)
  115. input_set_capability(cm->input_dev,
  116. EV_SW, jack_switch_types[i]);
  117. return 0;
  118. }
  119. static int cmhid_input_mapping(struct hid_device *hid,
  120. struct hid_input *hi, struct hid_field *field,
  121. struct hid_usage *usage, unsigned long **bit, int *max)
  122. {
  123. return -1;
  124. }
  125. static int cmhid_probe(struct hid_device *hid, const struct hid_device_id *id)
  126. {
  127. int ret;
  128. struct cmhid *cm;
  129. cm = kzalloc(sizeof(struct cmhid), GFP_KERNEL);
  130. if (!cm) {
  131. ret = -ENOMEM;
  132. goto allocfail;
  133. }
  134. cm->hid = hid;
  135. hid->quirks |= HID_QUIRK_HIDINPUT_FORCE;
  136. hid_set_drvdata(hid, cm);
  137. ret = hid_parse(hid);
  138. if (ret) {
  139. hid_err(hid, "parse failed\n");
  140. goto fail;
  141. }
  142. ret = hid_hw_start(hid, HID_CONNECT_DEFAULT | HID_CONNECT_HIDDEV_FORCE);
  143. if (ret) {
  144. hid_err(hid, "hw start failed\n");
  145. goto fail;
  146. }
  147. return 0;
  148. fail:
  149. kfree(cm);
  150. allocfail:
  151. return ret;
  152. }
  153. static void cmhid_remove(struct hid_device *hid)
  154. {
  155. struct cmhid *cm = hid_get_drvdata(hid);
  156. hid_hw_stop(hid);
  157. kfree(cm);
  158. }
  159. static const struct hid_device_id cmhid_devices[] = {
  160. { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CM6533) },
  161. { }
  162. };
  163. MODULE_DEVICE_TABLE(hid, cmhid_devices);
  164. static struct hid_driver cmhid_driver = {
  165. .name = "cm6533_jd",
  166. .id_table = cmhid_devices,
  167. .raw_event = cmhid_raw_event,
  168. .input_configured = cmhid_input_configured,
  169. .probe = cmhid_probe,
  170. .remove = cmhid_remove,
  171. .input_mapping = cmhid_input_mapping,
  172. };
  173. static __u8 *cmhid_hs100b_report_fixup(struct hid_device *hid, __u8 *rdesc,
  174. unsigned int *rsize)
  175. {
  176. if (*rsize == HS100B_RDESC_ORIG_SIZE) {
  177. hid_info(hid, "Fixing CMedia HS-100B report descriptor\n");
  178. rdesc = hs100b_rdesc_fixed;
  179. *rsize = sizeof(hs100b_rdesc_fixed);
  180. }
  181. return rdesc;
  182. }
  183. static const struct hid_device_id cmhid_hs100b_devices[] = {
  184. { HID_USB_DEVICE(USB_VENDOR_ID_CMEDIA, USB_DEVICE_ID_CMEDIA_HS100B) },
  185. { }
  186. };
  187. MODULE_DEVICE_TABLE(hid, cmhid_hs100b_devices);
  188. static struct hid_driver cmhid_hs100b_driver = {
  189. .name = "cmedia_hs100b",
  190. .id_table = cmhid_hs100b_devices,
  191. .report_fixup = cmhid_hs100b_report_fixup,
  192. };
  193. static int cmedia_init(void)
  194. {
  195. int ret;
  196. ret = hid_register_driver(&cmhid_driver);
  197. if (ret)
  198. return ret;
  199. ret = hid_register_driver(&cmhid_hs100b_driver);
  200. if (ret)
  201. hid_unregister_driver(&cmhid_driver);
  202. return ret;
  203. }
  204. module_init(cmedia_init);
  205. static void cmedia_exit(void)
  206. {
  207. hid_unregister_driver(&cmhid_driver);
  208. hid_unregister_driver(&cmhid_hs100b_driver);
  209. }
  210. module_exit(cmedia_exit);