hid-a4tech.c 4.0 KB

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