hid-zydacron.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * HID driver for zydacron remote control
  3. *
  4. * Copyright (c) 2010 Don Prince <dhprince.devel@yahoo.co.uk>
  5. */
  6. /*
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. */
  12. #include <linux/device.h>
  13. #include <linux/hid.h>
  14. #include <linux/module.h>
  15. #include "hid-ids.h"
  16. struct zc_device {
  17. struct input_dev *input_ep81;
  18. unsigned short last_key[4];
  19. };
  20. /*
  21. * Zydacron remote control has an invalid HID report descriptor,
  22. * that needs fixing before we can parse it.
  23. */
  24. static __u8 *zc_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  25. unsigned int *rsize)
  26. {
  27. if (*rsize >= 253 &&
  28. rdesc[0x96] == 0xbc && rdesc[0x97] == 0xff &&
  29. rdesc[0xca] == 0xbc && rdesc[0xcb] == 0xff &&
  30. rdesc[0xe1] == 0xbc && rdesc[0xe2] == 0xff) {
  31. hid_info(hdev,
  32. "fixing up zydacron remote control report descriptor\n");
  33. rdesc[0x96] = rdesc[0xca] = rdesc[0xe1] = 0x0c;
  34. rdesc[0x97] = rdesc[0xcb] = rdesc[0xe2] = 0x00;
  35. }
  36. return rdesc;
  37. }
  38. #define zc_map_key_clear(c) \
  39. hid_map_usage_clear(hi, usage, bit, max, EV_KEY, (c))
  40. static int zc_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  41. struct hid_field *field, struct hid_usage *usage,
  42. unsigned long **bit, int *max)
  43. {
  44. int i;
  45. struct zc_device *zc = hid_get_drvdata(hdev);
  46. zc->input_ep81 = hi->input;
  47. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_CONSUMER)
  48. return 0;
  49. dbg_hid("zynacron input mapping event [0x%x]\n",
  50. usage->hid & HID_USAGE);
  51. switch (usage->hid & HID_USAGE) {
  52. /* report 2 */
  53. case 0x10:
  54. zc_map_key_clear(KEY_MODE);
  55. break;
  56. case 0x30:
  57. zc_map_key_clear(KEY_SCREEN);
  58. break;
  59. case 0x70:
  60. zc_map_key_clear(KEY_INFO);
  61. break;
  62. /* report 3 */
  63. case 0x04:
  64. zc_map_key_clear(KEY_RADIO);
  65. break;
  66. /* report 4 */
  67. case 0x0d:
  68. zc_map_key_clear(KEY_PVR);
  69. break;
  70. case 0x25:
  71. zc_map_key_clear(KEY_TV);
  72. break;
  73. case 0x47:
  74. zc_map_key_clear(KEY_AUDIO);
  75. break;
  76. case 0x49:
  77. zc_map_key_clear(KEY_AUX);
  78. break;
  79. case 0x4a:
  80. zc_map_key_clear(KEY_VIDEO);
  81. break;
  82. case 0x48:
  83. zc_map_key_clear(KEY_DVD);
  84. break;
  85. case 0x24:
  86. zc_map_key_clear(KEY_MENU);
  87. break;
  88. case 0x32:
  89. zc_map_key_clear(KEY_TEXT);
  90. break;
  91. default:
  92. return 0;
  93. }
  94. for (i = 0; i < 4; i++)
  95. zc->last_key[i] = 0;
  96. return 1;
  97. }
  98. static int zc_raw_event(struct hid_device *hdev, struct hid_report *report,
  99. u8 *data, int size)
  100. {
  101. struct zc_device *zc = hid_get_drvdata(hdev);
  102. int ret = 0;
  103. unsigned key;
  104. unsigned short index;
  105. if (report->id == data[0]) {
  106. /* break keys */
  107. for (index = 0; index < 4; index++) {
  108. key = zc->last_key[index];
  109. if (key) {
  110. input_event(zc->input_ep81, EV_KEY, key, 0);
  111. zc->last_key[index] = 0;
  112. }
  113. }
  114. key = 0;
  115. switch (report->id) {
  116. case 0x02:
  117. case 0x03:
  118. switch (data[1]) {
  119. case 0x10:
  120. key = KEY_MODE;
  121. index = 0;
  122. break;
  123. case 0x30:
  124. key = KEY_SCREEN;
  125. index = 1;
  126. break;
  127. case 0x70:
  128. key = KEY_INFO;
  129. index = 2;
  130. break;
  131. case 0x04:
  132. key = KEY_RADIO;
  133. index = 3;
  134. break;
  135. }
  136. if (key) {
  137. input_event(zc->input_ep81, EV_KEY, key, 1);
  138. zc->last_key[index] = key;
  139. }
  140. ret = 1;
  141. break;
  142. }
  143. }
  144. return ret;
  145. }
  146. static int zc_probe(struct hid_device *hdev, const struct hid_device_id *id)
  147. {
  148. int ret;
  149. struct zc_device *zc;
  150. zc = devm_kzalloc(&hdev->dev, sizeof(*zc), GFP_KERNEL);
  151. if (zc == NULL) {
  152. hid_err(hdev, "can't alloc descriptor\n");
  153. return -ENOMEM;
  154. }
  155. hid_set_drvdata(hdev, zc);
  156. ret = hid_parse(hdev);
  157. if (ret) {
  158. hid_err(hdev, "parse failed\n");
  159. return ret;
  160. }
  161. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  162. if (ret) {
  163. hid_err(hdev, "hw start failed\n");
  164. return ret;
  165. }
  166. return 0;
  167. }
  168. static const struct hid_device_id zc_devices[] = {
  169. { HID_USB_DEVICE(USB_VENDOR_ID_ZYDACRON, USB_DEVICE_ID_ZYDACRON_REMOTE_CONTROL) },
  170. { }
  171. };
  172. MODULE_DEVICE_TABLE(hid, zc_devices);
  173. static struct hid_driver zc_driver = {
  174. .name = "zydacron",
  175. .id_table = zc_devices,
  176. .report_fixup = zc_report_fixup,
  177. .input_mapping = zc_input_mapping,
  178. .raw_event = zc_raw_event,
  179. .probe = zc_probe,
  180. };
  181. module_hid_driver(zc_driver);
  182. MODULE_LICENSE("GPL");