hid-cypress.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * HID driver for some cypress "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) 2007 Paul Walmsley
  9. * Copyright (c) 2008 Jiri Slaby
  10. */
  11. /*
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. */
  17. #include <linux/device.h>
  18. #include <linux/hid.h>
  19. #include <linux/input.h>
  20. #include <linux/module.h>
  21. #include "hid-ids.h"
  22. #define CP_RDESC_SWAPPED_MIN_MAX 0x01
  23. #define CP_2WHEEL_MOUSE_HACK 0x02
  24. #define CP_2WHEEL_MOUSE_HACK_ON 0x04
  25. /*
  26. * Some USB barcode readers from cypress have usage min and usage max in
  27. * the wrong order
  28. */
  29. static __u8 *cp_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  30. unsigned int *rsize)
  31. {
  32. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  33. unsigned int i;
  34. if (!(quirks & CP_RDESC_SWAPPED_MIN_MAX))
  35. return rdesc;
  36. for (i = 0; i < *rsize - 4; i++)
  37. if (rdesc[i] == 0x29 && rdesc[i + 2] == 0x19) {
  38. __u8 tmp;
  39. rdesc[i] = 0x19;
  40. rdesc[i + 2] = 0x29;
  41. tmp = rdesc[i + 3];
  42. rdesc[i + 3] = rdesc[i + 1];
  43. rdesc[i + 1] = tmp;
  44. }
  45. return rdesc;
  46. }
  47. static int cp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  48. struct hid_field *field, struct hid_usage *usage,
  49. unsigned long **bit, int *max)
  50. {
  51. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  52. if (!(quirks & CP_2WHEEL_MOUSE_HACK))
  53. return 0;
  54. if (usage->type == EV_REL && usage->code == REL_WHEEL)
  55. set_bit(REL_HWHEEL, *bit);
  56. if (usage->hid == 0x00090005)
  57. return -1;
  58. return 0;
  59. }
  60. static int cp_event(struct hid_device *hdev, struct hid_field *field,
  61. struct hid_usage *usage, __s32 value)
  62. {
  63. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  64. if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
  65. !usage->type || !(quirks & CP_2WHEEL_MOUSE_HACK))
  66. return 0;
  67. if (usage->hid == 0x00090005) {
  68. if (value)
  69. quirks |= CP_2WHEEL_MOUSE_HACK_ON;
  70. else
  71. quirks &= ~CP_2WHEEL_MOUSE_HACK_ON;
  72. hid_set_drvdata(hdev, (void *)quirks);
  73. return 1;
  74. }
  75. if (usage->code == REL_WHEEL && (quirks & CP_2WHEEL_MOUSE_HACK_ON)) {
  76. struct input_dev *input = field->hidinput->input;
  77. input_event(input, usage->type, REL_HWHEEL, value);
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. static int cp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  83. {
  84. unsigned long quirks = id->driver_data;
  85. int ret;
  86. hid_set_drvdata(hdev, (void *)quirks);
  87. ret = hid_parse(hdev);
  88. if (ret) {
  89. hid_err(hdev, "parse failed\n");
  90. goto err_free;
  91. }
  92. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  93. if (ret) {
  94. hid_err(hdev, "hw start failed\n");
  95. goto err_free;
  96. }
  97. return 0;
  98. err_free:
  99. return ret;
  100. }
  101. static const struct hid_device_id cp_devices[] = {
  102. { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1),
  103. .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
  104. { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2),
  105. .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
  106. { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3),
  107. .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
  108. { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE),
  109. .driver_data = CP_2WHEEL_MOUSE_HACK },
  110. { }
  111. };
  112. MODULE_DEVICE_TABLE(hid, cp_devices);
  113. static struct hid_driver cp_driver = {
  114. .name = "cypress",
  115. .id_table = cp_devices,
  116. .report_fixup = cp_report_fixup,
  117. .input_mapped = cp_input_mapped,
  118. .event = cp_event,
  119. .probe = cp_probe,
  120. };
  121. static int __init cp_init(void)
  122. {
  123. return hid_register_driver(&cp_driver);
  124. }
  125. static void __exit cp_exit(void)
  126. {
  127. hid_unregister_driver(&cp_driver);
  128. }
  129. module_init(cp_init);
  130. module_exit(cp_exit);
  131. MODULE_LICENSE("GPL");