hid-cougar.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * HID driver for Cougar 500k Gaming Keyboard
  4. *
  5. * Copyright (c) 2018 Daniel M. Lambea <dmlambea@gmail.com>
  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 <dmlambea@gmail.com>");
  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(&hdev->dev, cougar_remove_shared_data, cougar);
  155. if (error) {
  156. mutex_unlock(&cougar_udev_list_lock);
  157. cougar_remove_shared_data(cougar);
  158. return error;
  159. }
  160. out:
  161. mutex_unlock(&cougar_udev_list_lock);
  162. return error;
  163. }
  164. static int cougar_probe(struct hid_device *hdev,
  165. const struct hid_device_id *id)
  166. {
  167. struct cougar *cougar;
  168. struct hid_input *next, *hidinput = NULL;
  169. unsigned int connect_mask;
  170. int error;
  171. cougar = devm_kzalloc(&hdev->dev, sizeof(*cougar), GFP_KERNEL);
  172. if (!cougar)
  173. return -ENOMEM;
  174. hid_set_drvdata(hdev, cougar);
  175. error = hid_parse(hdev);
  176. if (error) {
  177. hid_err(hdev, "parse failed\n");
  178. return error;
  179. }
  180. if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
  181. cougar->special_intf = true;
  182. connect_mask = HID_CONNECT_HIDRAW;
  183. } else
  184. connect_mask = HID_CONNECT_DEFAULT;
  185. error = hid_hw_start(hdev, connect_mask);
  186. if (error) {
  187. hid_err(hdev, "hw start failed\n");
  188. return error;
  189. }
  190. error = cougar_bind_shared_data(hdev, cougar);
  191. if (error)
  192. goto fail_stop_and_cleanup;
  193. /* The custom vendor interface will use the hid_input registered
  194. * for the keyboard interface, in order to send translated key codes
  195. * to it.
  196. */
  197. if (hdev->collection->usage == HID_GD_KEYBOARD) {
  198. list_for_each_entry_safe(hidinput, next, &hdev->inputs, list) {
  199. if (hidinput->registered && hidinput->input != NULL) {
  200. cougar->shared->input = hidinput->input;
  201. cougar->shared->enabled = true;
  202. break;
  203. }
  204. }
  205. } else if (hdev->collection->usage == COUGAR_VENDOR_USAGE) {
  206. /* Preinit the mapping table */
  207. cougar_fix_g6_mapping();
  208. error = hid_hw_open(hdev);
  209. if (error)
  210. goto fail_stop_and_cleanup;
  211. }
  212. return 0;
  213. fail_stop_and_cleanup:
  214. hid_hw_stop(hdev);
  215. return error;
  216. }
  217. /*
  218. * Convert events from vendor intf to input key events
  219. */
  220. static int cougar_raw_event(struct hid_device *hdev, struct hid_report *report,
  221. u8 *data, int size)
  222. {
  223. struct cougar *cougar;
  224. struct cougar_shared *shared;
  225. unsigned char code, action;
  226. int i;
  227. cougar = hid_get_drvdata(hdev);
  228. shared = cougar->shared;
  229. if (!cougar->special_intf || !shared)
  230. return 0;
  231. if (!shared->enabled || !shared->input)
  232. return -EPERM;
  233. code = data[COUGAR_FIELD_CODE];
  234. action = data[COUGAR_FIELD_ACTION];
  235. for (i = 0; cougar_mapping[i][0]; i++) {
  236. if (code == cougar_mapping[i][0]) {
  237. input_event(shared->input, EV_KEY,
  238. cougar_mapping[i][1], action);
  239. input_sync(shared->input);
  240. return -EPERM;
  241. }
  242. }
  243. /* Avoid warnings on the same unmapped key twice */
  244. if (action != 0)
  245. hid_warn(hdev, "unmapped special key code %0x: ignoring\n", code);
  246. return -EPERM;
  247. }
  248. static void cougar_remove(struct hid_device *hdev)
  249. {
  250. struct cougar *cougar = hid_get_drvdata(hdev);
  251. if (cougar) {
  252. /* Stop the vendor intf to process more events */
  253. if (cougar->shared)
  254. cougar->shared->enabled = false;
  255. if (cougar->special_intf)
  256. hid_hw_close(hdev);
  257. }
  258. hid_hw_stop(hdev);
  259. }
  260. static int cougar_param_set_g6_is_space(const char *val,
  261. const struct kernel_param *kp)
  262. {
  263. int ret;
  264. ret = param_set_bool(val, kp);
  265. if (ret)
  266. return ret;
  267. cougar_fix_g6_mapping();
  268. return 0;
  269. }
  270. static const struct kernel_param_ops cougar_g6_is_space_ops = {
  271. .set = cougar_param_set_g6_is_space,
  272. .get = param_get_bool,
  273. };
  274. module_param_cb(g6_is_space, &cougar_g6_is_space_ops, &g6_is_space, 0644);
  275. static const struct hid_device_id cougar_id_table[] = {
  276. { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
  277. USB_DEVICE_ID_COUGAR_500K_GAMING_KEYBOARD) },
  278. { HID_USB_DEVICE(USB_VENDOR_ID_SOLID_YEAR,
  279. USB_DEVICE_ID_COUGAR_700K_GAMING_KEYBOARD) },
  280. {}
  281. };
  282. MODULE_DEVICE_TABLE(hid, cougar_id_table);
  283. static struct hid_driver cougar_driver = {
  284. .name = "cougar",
  285. .id_table = cougar_id_table,
  286. .report_fixup = cougar_report_fixup,
  287. .probe = cougar_probe,
  288. .remove = cougar_remove,
  289. .raw_event = cougar_raw_event,
  290. };
  291. module_hid_driver(cougar_driver);