hid-a4tech.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * HID driver for some a4tech "special" devices
  4. *
  5. * Copyright (c) 1999 Andreas Gal
  6. * Copyright (c) 2000-2005 Vojtech Pavlik <[email protected]>
  7. * Copyright (c) 2005 Michael Haboustak <[email protected]> for Concept2, Inc
  8. * Copyright (c) 2006-2007 Jiri Kosina
  9. * Copyright (c) 2008 Jiri Slaby
  10. */
  11. /*
  12. */
  13. #include <linux/device.h>
  14. #include <linux/input.h>
  15. #include <linux/hid.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include "hid-ids.h"
  19. #define A4_2WHEEL_MOUSE_HACK_7 0x01
  20. #define A4_2WHEEL_MOUSE_HACK_B8 0x02
  21. #define A4_WHEEL_ORIENTATION (HID_UP_GENDESK | 0x000000b8)
  22. struct a4tech_sc {
  23. unsigned long quirks;
  24. unsigned int hw_wheel;
  25. __s32 delayed_value;
  26. };
  27. static int a4_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  28. struct hid_field *field, struct hid_usage *usage,
  29. unsigned long **bit, int *max)
  30. {
  31. struct a4tech_sc *a4 = hid_get_drvdata(hdev);
  32. if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8 &&
  33. usage->hid == A4_WHEEL_ORIENTATION) {
  34. /*
  35. * We do not want to have this usage mapped to anything as it's
  36. * nonstandard and doesn't really behave like an HID report.
  37. * It's only selecting the orientation (vertical/horizontal) of
  38. * the previous mouse wheel report. The input_events will be
  39. * generated once both reports are recorded in a4_event().
  40. */
  41. return -1;
  42. }
  43. return 0;
  44. }
  45. static int a4_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  46. struct hid_field *field, struct hid_usage *usage,
  47. unsigned long **bit, int *max)
  48. {
  49. struct a4tech_sc *a4 = hid_get_drvdata(hdev);
  50. if (usage->type == EV_REL && usage->code == REL_WHEEL_HI_RES) {
  51. set_bit(REL_HWHEEL, *bit);
  52. set_bit(REL_HWHEEL_HI_RES, *bit);
  53. }
  54. if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007)
  55. return -1;
  56. return 0;
  57. }
  58. static int a4_event(struct hid_device *hdev, struct hid_field *field,
  59. struct hid_usage *usage, __s32 value)
  60. {
  61. struct a4tech_sc *a4 = hid_get_drvdata(hdev);
  62. struct input_dev *input;
  63. if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput)
  64. return 0;
  65. input = field->hidinput->input;
  66. if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8) {
  67. if (usage->type == EV_REL && usage->code == REL_WHEEL_HI_RES) {
  68. a4->delayed_value = value;
  69. return 1;
  70. }
  71. if (usage->hid == A4_WHEEL_ORIENTATION) {
  72. input_event(input, EV_REL, value ? REL_HWHEEL :
  73. REL_WHEEL, a4->delayed_value);
  74. input_event(input, EV_REL, value ? REL_HWHEEL_HI_RES :
  75. REL_WHEEL_HI_RES, a4->delayed_value * 120);
  76. return 1;
  77. }
  78. }
  79. if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) {
  80. a4->hw_wheel = !!value;
  81. return 1;
  82. }
  83. if (usage->code == REL_WHEEL_HI_RES && a4->hw_wheel) {
  84. input_event(input, usage->type, REL_HWHEEL, value);
  85. input_event(input, usage->type, REL_HWHEEL_HI_RES, value * 120);
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. static int a4_probe(struct hid_device *hdev, const struct hid_device_id *id)
  91. {
  92. struct a4tech_sc *a4;
  93. int ret;
  94. a4 = devm_kzalloc(&hdev->dev, sizeof(*a4), GFP_KERNEL);
  95. if (a4 == NULL) {
  96. hid_err(hdev, "can't alloc device descriptor\n");
  97. return -ENOMEM;
  98. }
  99. a4->quirks = id->driver_data;
  100. hid_set_drvdata(hdev, a4);
  101. ret = hid_parse(hdev);
  102. if (ret) {
  103. hid_err(hdev, "parse failed\n");
  104. return ret;
  105. }
  106. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  107. if (ret) {
  108. hid_err(hdev, "hw start failed\n");
  109. return ret;
  110. }
  111. return 0;
  112. }
  113. static const struct hid_device_id a4_devices[] = {
  114. { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU),
  115. .driver_data = A4_2WHEEL_MOUSE_HACK_7 },
  116. { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D),
  117. .driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
  118. { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649),
  119. .driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
  120. { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_NB_95),
  121. .driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
  122. { }
  123. };
  124. MODULE_DEVICE_TABLE(hid, a4_devices);
  125. static struct hid_driver a4_driver = {
  126. .name = "a4tech",
  127. .id_table = a4_devices,
  128. .input_mapping = a4_input_mapping,
  129. .input_mapped = a4_input_mapped,
  130. .event = a4_event,
  131. .probe = a4_probe,
  132. };
  133. module_hid_driver(a4_driver);
  134. MODULE_LICENSE("GPL");