hid-cypress.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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) 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/hid.h>
  18. #include <linux/input.h>
  19. #include <linux/module.h>
  20. #include "hid-ids.h"
  21. #define CP_RDESC_SWAPPED_MIN_MAX 0x01
  22. #define CP_2WHEEL_MOUSE_HACK 0x02
  23. #define CP_2WHEEL_MOUSE_HACK_ON 0x04
  24. /*
  25. * Some USB barcode readers from cypress have usage min and usage max in
  26. * the wrong order
  27. */
  28. static __u8 *cp_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  29. unsigned int *rsize)
  30. {
  31. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  32. unsigned int i;
  33. if (!(quirks & CP_RDESC_SWAPPED_MIN_MAX))
  34. return rdesc;
  35. if (*rsize < 4)
  36. return rdesc;
  37. for (i = 0; i < *rsize - 4; i++)
  38. if (rdesc[i] == 0x29 && rdesc[i + 2] == 0x19) {
  39. rdesc[i] = 0x19;
  40. rdesc[i + 2] = 0x29;
  41. swap(rdesc[i + 3], rdesc[i + 1]);
  42. }
  43. return rdesc;
  44. }
  45. static int cp_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. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  50. if (!(quirks & CP_2WHEEL_MOUSE_HACK))
  51. return 0;
  52. if (usage->type == EV_REL && usage->code == REL_WHEEL)
  53. set_bit(REL_HWHEEL, *bit);
  54. if (usage->hid == 0x00090005)
  55. return -1;
  56. return 0;
  57. }
  58. static int cp_event(struct hid_device *hdev, struct hid_field *field,
  59. struct hid_usage *usage, __s32 value)
  60. {
  61. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  62. if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
  63. !usage->type || !(quirks & CP_2WHEEL_MOUSE_HACK))
  64. return 0;
  65. if (usage->hid == 0x00090005) {
  66. if (value)
  67. quirks |= CP_2WHEEL_MOUSE_HACK_ON;
  68. else
  69. quirks &= ~CP_2WHEEL_MOUSE_HACK_ON;
  70. hid_set_drvdata(hdev, (void *)quirks);
  71. return 1;
  72. }
  73. if (usage->code == REL_WHEEL && (quirks & CP_2WHEEL_MOUSE_HACK_ON)) {
  74. struct input_dev *input = field->hidinput->input;
  75. input_event(input, usage->type, REL_HWHEEL, value);
  76. return 1;
  77. }
  78. return 0;
  79. }
  80. static int cp_probe(struct hid_device *hdev, const struct hid_device_id *id)
  81. {
  82. unsigned long quirks = id->driver_data;
  83. int ret;
  84. hid_set_drvdata(hdev, (void *)quirks);
  85. ret = hid_parse(hdev);
  86. if (ret) {
  87. hid_err(hdev, "parse failed\n");
  88. goto err_free;
  89. }
  90. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  91. if (ret) {
  92. hid_err(hdev, "hw start failed\n");
  93. goto err_free;
  94. }
  95. return 0;
  96. err_free:
  97. return ret;
  98. }
  99. static const struct hid_device_id cp_devices[] = {
  100. { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1),
  101. .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
  102. { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2),
  103. .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
  104. { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3),
  105. .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
  106. { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_4),
  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. module_hid_driver(cp_driver);
  122. MODULE_LICENSE("GPL");