hid-cougar.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * HID driver for Cougar 500k Gaming Keyboard
  4. *
  5. * Copyright (c) 2018 Daniel M. Lambea <[email protected]>
  6. */
  7. #include <linux/hid.h>
  8. #include <linux/module.h>
  9. #include <linux/printk.h>
  10. #include "hid-ids.h"
  11. MODULE_AUTHOR("Daniel M. Lambea <[email protected]>");
  12. MODULE_DESCRIPTION("Cougar 500k Gaming Keyboard");
  13. MODULE_LICENSE("GPL");
  14. MODULE_INFO(key_mappings, "G1-G6 are mapped to F13-F18");
  15. static bool g6_is_space = true;
  16. MODULE_PARM_DESC(g6_is_space,
  17. "If true, G6 programmable key sends SPACE instead of F18 (default=true)");
  18. #define COUGAR_VENDOR_USAGE 0xff00ff00
  19. #define COUGAR_FIELD_CODE 1
  20. #define COUGAR_FIELD_ACTION 2
  21. #define COUGAR_KEY_G1 0x83
  22. #define COUGAR_KEY_G2 0x84
  23. #define COUGAR_KEY_G3 0x85
  24. #define COUGAR_KEY_G4 0x86
  25. #define COUGAR_KEY_G5 0x87
  26. #define COUGAR_KEY_G6 0x78
  27. #define COUGAR_KEY_FN 0x0d
  28. #define COUGAR_KEY_MR 0x6f
  29. #define COUGAR_KEY_M1 0x80
  30. #define COUGAR_KEY_M2 0x81
  31. #define COUGAR_KEY_M3 0x82
  32. #define COUGAR_KEY_LEDS 0x67
  33. #define COUGAR_KEY_LOCK 0x6e
  34. /* Default key mappings. The special key COUGAR_KEY_G6 is defined first
  35. * because it is more frequent to use the spacebar rather than any other
  36. * special keys. Depending on the value of the parameter 'g6_is_space',
  37. * the mapping will be updated in the probe function.
  38. */
  39. static unsigned char cougar_mapping[][2] = {
  40. { COUGAR_KEY_G6, KEY_SPACE },
  41. { COUGAR_KEY_G1, KEY_F13 },
  42. { COUGAR_KEY_G2, KEY_F14 },
  43. { COUGAR_KEY_G3, KEY_F15 },
  44. { COUGAR_KEY_G4, KEY_F16 },
  45. { COUGAR_KEY_G5, KEY_F17 },
  46. { COUGAR_KEY_LOCK, KEY_SCREENLOCK },
  47. /* The following keys are handled by the hardware itself, so no special
  48. * treatment is required:
  49. { COUGAR_KEY_FN, KEY_RESERVED },
  50. { COUGAR_KEY_MR, KEY_RESERVED },
  51. { COUGAR_KEY_M1, KEY_RESERVED },
  52. { COUGAR_KEY_M2, KEY_RESERVED },
  53. { COUGAR_KEY_M3, KEY_RESERVED },
  54. { COUGAR_KEY_LEDS, KEY_RESERVED },
  55. */
  56. { 0, 0 },
  57. };
  58. struct cougar_shared {
  59. struct list_head list;
  60. struct kref kref;
  61. bool enabled;
  62. struct hid_device *dev;
  63. struct input_dev *input;
  64. };
  65. struct cougar {
  66. bool special_intf;
  67. struct cougar_shared *shared;
  68. };
  69. static LIST_HEAD(cougar_udev_list);
  70. static DEFINE_MUTEX(cougar_udev_list_lock);
  71. /**
  72. * cougar_fix_g6_mapping - configure the mapping for key G6/Spacebar
  73. */
  74. static void cougar_fix_g6_mapping(void)
  75. {
  76. int i;
  77. for (i = 0; cougar_mapping[i][0]; i++) {
  78. if (cougar_mapping[i][0] == COUGAR_KEY_G6) {
  79. cougar_mapping[i][1] =
  80. g6_is_space ? KEY_SPACE : KEY_F18;
  81. pr_info("cougar: G6 mapped to %s\n",
  82. g6_is_space ? "space" : "F18");
  83. return;
  84. }
  85. }
  86. pr_warn("cougar: no mappings defined for G6/spacebar");
  87. }
  88. /*
  89. * Constant-friendly rdesc fixup for mouse interface
  90. */
  91. static __u8 *cougar_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  92. unsigned int *rsize)
  93. {
  94. if (rdesc[2] == 0x09 && rdesc[3] == 0x02 &&
  95. (rdesc[115] | rdesc[116] << 8) >= HID_MAX_USAGES) {
  96. hid_info(hdev,
  97. "usage count exceeds max: fixing up report descriptor\n");
  98. rdesc[115] = ((HID_MAX_USAGES-1) & 0xff);
  99. rdesc[116] = ((HID_MAX_USAGES-1) >> 8);
  100. }
  101. return rdesc;
  102. }
  103. static struct cougar_shared *cougar_get_shared_data(struct hid_device *hdev)
  104. {
  105. struct cougar_shared *shared;
  106. /* Try to find an already-probed interface from the same device */
  107. list_for_each_entry(shared, &cougar_udev_list, list) {
  108. if (hid_compare_device_paths(hdev, shared->dev, '/')) {
  109. kref_get(&shared->kref);
  110. return shared;
  111. }
  112. }
  113. return NULL;
  114. }
  115. static void cougar_release_shared_data(struct kref *kref)
  116. {
  117. struct cougar_shared *shared = container_of(kref,
  118. struct cougar_shared, kref);
  119. mutex_lock(&cougar_udev_list_lock);
  120. list_del(&shared->list);
  121. mutex_unlock(&cougar_udev_list_lock);
  122. kfree(shared);
  123. }
  124. static void cougar_remove_shared_data(void *resource)
  125. {
  126. struct cougar *cougar = resource;
  127. if (cougar->shared) {
  128. kref_put(&cougar->shared->kref, cougar_release_shared_data);
  129. cougar->shared = NULL;
  130. }
  131. }
  132. /*
  133. * Bind the device group's shared data to this cougar struct.
  134. * If no shared data exists for this group, create and initialize it.
  135. */
  136. static int cougar_bind_shared_data(struct hid_device *hdev,
  137. struct cougar *cougar)
  138. {
  139. struct cougar_shared *shared;
  140. int error = 0;
  141. mutex_lock(&cougar_udev_list_lock);
  142. shared = cougar_get_shared_data(hdev);
  143. if (!shared) {
  144. shared = kzalloc(sizeof(*shared), GFP_KERNEL);
  145. if (!shared) {
  146. error = -ENOMEM;
  147. goto out;
  148. }
  149. kref_init(&shared->kref);
  150. shared->dev = hdev;
  151. list_add_tail(&shared->list, &cougar_udev_list);
  152. }
  153. cougar->shared = shared;
  154. error = devm_add_action_or_reset(&hdev->dev, cougar_remove_shared_data, cougar);
  155. if (error) {
  156. mutex_unlock(&cougar_udev_list_lock);
  157. return error;
  158. }
  159. out:
  160. mutex_unlock(&cougar_udev_list_lock);
  161. return error;
  162. }
  163. static int cougar_probe(struct hid_device *hdev,
  164. const struct hid_device_id *id)
  165. {
  166. struct cougar *cougar;
  167. struct hid_input *next, *hidinput = NULL;
  168. unsigned int connect_mask;
  169. int error;
  170. cougar = devm_kzalloc(&hdev->dev, sizeof(*cougar), GFP_KERNEL);
  171. if (!cougar)
  172. return -ENOMEM;
  173. hid_set_drvdata(hdev, cougar);
  174. error = hid_parse(hdev);
  175. if (error) {
  176. hid_err(hdev, "parse failed\n");
  177. return error;
  178. }
  179. if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
  180. cougar->special_intf = true;
  181. connect_mask = HID_CONNECT_HIDRAW;
  182. } else
  183. connect_mask = HID_CONNECT_DEFAULT;
  184. error = hid_hw_start(hdev, connect_mask);
  185. if (error) {
  186. hid_err(hdev, "hw start failed\n");
  187. return error;
  188. }
  189. error = cougar_bind_shared_data(hdev, cougar);
  190. if (error)
  191. goto fail_stop_and_cleanup;
  192. /* The custom vendor interface will use the hid_input registered
  193. * for the keyboard interface, in order to send translated key codes
  194. * to it.
  195. */
  196. if (hdev->collection->usage == HID_GD_KEYBOARD) {
  197. list_for_each_entry_safe(hidinput, next, &hdev->inputs, list) {
  198. if (hidinput->registered && hidinput->input != NULL) {
  199. cougar->shared->input = hidinput->input;
  200. cougar->shared->enabled = true;
  201. break;
  202. }
  203. }
  204. } else if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
  205. /* Preinit the mapping table */
  206. cougar_fix_g6_mapping();
  207. error = hid_hw_open(hdev);
  208. if (error)
  209. goto fail_stop_and_cleanup;
  210. }
  211. return 0;
  212. fail_stop_and_cleanup:
  213. hid_hw_stop(hdev);
  214. return error;
  215. }
  216. /*
  217. * Convert events from vendor intf to input key events
  218. */
  219. static int cougar_raw_event(struct hid_device *hdev, struct hid_report *report,
  220. u8 *data, int size)
  221. {
  222. struct cougar *cougar;
  223. struct cougar_shared *shared;
  224. unsigned char code, action;
  225. int i;
  226. cougar = hid_get_drvdata(hdev);
  227. shared = cougar->shared;
  228. if (!cougar->special_intf || !shared)
  229. return 0;
  230. if (!shared->enabled || !shared->input)
  231. return -EPERM;
  232. code = data[COUGAR_FIELD_CODE];
  233. action = data[COUGAR_FIELD_ACTION];
  234. for (i = 0; cougar_mapping[i][0]; i++) {
  235. if (code == cougar_mapping[i][0]) {
  236. input_event(shared->input, EV_KEY,
  237. cougar_mapping[i][1], action);
  238. input_sync(shared->input);
  239. return -EPERM;
  240. }
  241. }
  242. /* Avoid warnings on the same unmapped key twice */
  243. if (action != 0)
  244. hid_warn(hdev, "unmapped special key code %0x: ignoring\n", code);
  245. return -EPERM;
  246. }
  247. static void cougar_remove(struct hid_device *hdev)
  248. {
  249. struct cougar *cougar = hid_get_drvdata(hdev);
  250. if (cougar) {
  251. /* Stop the vendor intf to process more events */
  252. if (cougar->shared)
  253. cougar->shared->enabled = false;
  254. if (cougar->special_intf)
  255. hid_hw_close(hdev);
  256. }
  257. hid_hw_stop(hdev);
  258. }
  259. static int cougar_param_set_g6_is_space(const char *val,
  260. const struct kernel_param *kp)
  261. {
  262. int ret;
  263. ret = param_set_bool(val, kp);
  264. if (ret)
  265. return ret;
  266. cougar_fix_g6_mapping();
  267. return 0;
  268. }
  269. static const struct kernel_param_ops cougar_g6_is_space_ops = {
  270. .set = cougar_param_set_g6_is_space,
  271. .get = param_get_bool,
  272. };
  273. module_param_cb(g6_is_space, &cougar_g6_is_space_ops, &g6_is_space, 0644);
  274. static const struct hid_device_id cougar_id_table[] = {
  275. { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
  276. USB_DEVICE_ID_COUGAR_500K_GAMING_KEYBOARD) },
  277. { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
  278. USB_DEVICE_ID_COUGAR_700K_GAMING_KEYBOARD) },
  279. {}
  280. };
  281. MODULE_DEVICE_TABLE(hid, cougar_id_table);
  282. static struct hid_driver cougar_driver = {
  283. .name = "cougar",
  284. .id_table = cougar_id_table,
  285. .report_fixup = cougar_report_fixup,
  286. .probe = cougar_probe,
  287. .remove = cougar_remove,
  288. .raw_event = cougar_raw_event,
  289. };
  290. module_hid_driver(cougar_driver);