blob: a50ba4a4a1d71be06763f9ec0d23300b303d10d3 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Slaby0f221322008-06-23 22:54:08 +02002/*
3 * HID driver for some cypress "special" devices
4 *
5 * Copyright (c) 1999 Andreas Gal
6 * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
7 * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
8 * Copyright (c) 2006-2007 Jiri Kosina
Jiri Slaby0f221322008-06-23 22:54:08 +02009 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
Jiri Slaby0f221322008-06-23 22:54:08 +020013 */
14
15#include <linux/device.h>
16#include <linux/hid.h>
17#include <linux/input.h>
18#include <linux/module.h>
19
20#include "hid-ids.h"
21
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/*
27 * Some USB barcode readers from cypress have usage min and usage max in
28 * the wrong order
29 */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040030static __u8 *cp_report_fixup(struct hid_device *hdev, __u8 *rdesc,
31 unsigned int *rsize)
Jiri Slaby0f221322008-06-23 22:54:08 +020032{
33 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
34 unsigned int i;
35
36 if (!(quirks & CP_RDESC_SWAPPED_MIN_MAX))
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040037 return rdesc;
Jiri Slaby0f221322008-06-23 22:54:08 +020038
Greg Kroah-Hartman1ebb7112017-01-06 15:33:36 +010039 if (*rsize < 4)
40 return rdesc;
41
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040042 for (i = 0; i < *rsize - 4; i++)
Jiri Slaby0f221322008-06-23 22:54:08 +020043 if (rdesc[i] == 0x29 && rdesc[i + 2] == 0x19) {
Jiri Slaby0f221322008-06-23 22:54:08 +020044 rdesc[i] = 0x19;
45 rdesc[i + 2] = 0x29;
Fabian Frederick74a3e0c2015-06-10 18:32:05 +020046 swap(rdesc[i + 3], rdesc[i + 1]);
Jiri Slaby0f221322008-06-23 22:54:08 +020047 }
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040048 return rdesc;
Jiri Slaby0f221322008-06-23 22:54:08 +020049}
50
51static int cp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
52 struct hid_field *field, struct hid_usage *usage,
53 unsigned long **bit, int *max)
54{
55 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
56
57 if (!(quirks & CP_2WHEEL_MOUSE_HACK))
58 return 0;
59
60 if (usage->type == EV_REL && usage->code == REL_WHEEL)
61 set_bit(REL_HWHEEL, *bit);
62 if (usage->hid == 0x00090005)
63 return -1;
64
65 return 0;
66}
67
68static int cp_event(struct hid_device *hdev, struct hid_field *field,
69 struct hid_usage *usage, __s32 value)
70{
71 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
72
73 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
74 !usage->type || !(quirks & CP_2WHEEL_MOUSE_HACK))
75 return 0;
76
77 if (usage->hid == 0x00090005) {
78 if (value)
79 quirks |= CP_2WHEEL_MOUSE_HACK_ON;
80 else
81 quirks &= ~CP_2WHEEL_MOUSE_HACK_ON;
82 hid_set_drvdata(hdev, (void *)quirks);
83 return 1;
84 }
85
86 if (usage->code == REL_WHEEL && (quirks & CP_2WHEEL_MOUSE_HACK_ON)) {
87 struct input_dev *input = field->hidinput->input;
88
89 input_event(input, usage->type, REL_HWHEEL, value);
90 return 1;
91 }
92
93 return 0;
94}
95
96static int cp_probe(struct hid_device *hdev, const struct hid_device_id *id)
97{
98 unsigned long quirks = id->driver_data;
99 int ret;
100
101 hid_set_drvdata(hdev, (void *)quirks);
102
103 ret = hid_parse(hdev);
104 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800105 hid_err(hdev, "parse failed\n");
Jiri Slaby0f221322008-06-23 22:54:08 +0200106 goto err_free;
107 }
108
Jiri Slaby93c10132008-06-27 00:04:24 +0200109 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Jiri Slaby0f221322008-06-23 22:54:08 +0200110 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800111 hid_err(hdev, "hw start failed\n");
Jiri Slaby0f221322008-06-23 22:54:08 +0200112 goto err_free;
113 }
114
115 return 0;
116err_free:
117 return ret;
118}
119
120static const struct hid_device_id cp_devices[] = {
121 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1),
122 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
123 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2),
124 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
Jiri Kosinae8d0eab2009-12-02 22:54:11 +0100125 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3),
126 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
Lionel Vaux76c9d8f2012-07-22 11:32:20 +0200127 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_4),
128 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
Jiri Slaby0f221322008-06-23 22:54:08 +0200129 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE),
130 .driver_data = CP_2WHEEL_MOUSE_HACK },
131 { }
132};
133MODULE_DEVICE_TABLE(hid, cp_devices);
134
135static struct hid_driver cp_driver = {
136 .name = "cypress",
137 .id_table = cp_devices,
138 .report_fixup = cp_report_fixup,
139 .input_mapped = cp_input_mapped,
140 .event = cp_event,
141 .probe = cp_probe,
142};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700143module_hid_driver(cp_driver);
Jiri Slaby0f221322008-06-23 22:54:08 +0200144
Jiri Slaby0f221322008-06-23 22:54:08 +0200145MODULE_LICENSE("GPL");