blob: 1689568b597d4e5bb8824ccbe679f627525bf2a4 [file] [log] [blame]
Jiri Slaby0f221322008-06-23 22:54:08 +02001/*
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
Jiri Slaby0f221322008-06-23 22:54:08 +02008 * Copyright (c) 2008 Jiri Slaby
9 */
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
18#include <linux/device.h>
19#include <linux/hid.h>
20#include <linux/input.h>
21#include <linux/module.h>
22
23#include "hid-ids.h"
24
25#define CP_RDESC_SWAPPED_MIN_MAX 0x01
26#define CP_2WHEEL_MOUSE_HACK 0x02
27#define CP_2WHEEL_MOUSE_HACK_ON 0x04
28
29/*
30 * Some USB barcode readers from cypress have usage min and usage max in
31 * the wrong order
32 */
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040033static __u8 *cp_report_fixup(struct hid_device *hdev, __u8 *rdesc,
34 unsigned int *rsize)
Jiri Slaby0f221322008-06-23 22:54:08 +020035{
36 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
37 unsigned int i;
38
39 if (!(quirks & CP_RDESC_SWAPPED_MIN_MAX))
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040040 return rdesc;
Jiri Slaby0f221322008-06-23 22:54:08 +020041
Greg Kroah-Hartman2c867212017-01-06 15:33:36 +010042 if (*rsize < 4)
43 return rdesc;
44
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040045 for (i = 0; i < *rsize - 4; i++)
Jiri Slaby0f221322008-06-23 22:54:08 +020046 if (rdesc[i] == 0x29 && rdesc[i + 2] == 0x19) {
Jiri Slaby0f221322008-06-23 22:54:08 +020047 rdesc[i] = 0x19;
48 rdesc[i + 2] = 0x29;
Fabian Frederick74a3e0c2015-06-10 18:32:05 +020049 swap(rdesc[i + 3], rdesc[i + 1]);
Jiri Slaby0f221322008-06-23 22:54:08 +020050 }
Nikolai Kondrashov73e40082010-08-06 23:03:06 +040051 return rdesc;
Jiri Slaby0f221322008-06-23 22:54:08 +020052}
53
54static int cp_input_mapped(struct hid_device *hdev, struct hid_input *hi,
55 struct hid_field *field, struct hid_usage *usage,
56 unsigned long **bit, int *max)
57{
58 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
59
60 if (!(quirks & CP_2WHEEL_MOUSE_HACK))
61 return 0;
62
63 if (usage->type == EV_REL && usage->code == REL_WHEEL)
64 set_bit(REL_HWHEEL, *bit);
65 if (usage->hid == 0x00090005)
66 return -1;
67
68 return 0;
69}
70
71static int cp_event(struct hid_device *hdev, struct hid_field *field,
72 struct hid_usage *usage, __s32 value)
73{
74 unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
75
76 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
77 !usage->type || !(quirks & CP_2WHEEL_MOUSE_HACK))
78 return 0;
79
80 if (usage->hid == 0x00090005) {
81 if (value)
82 quirks |= CP_2WHEEL_MOUSE_HACK_ON;
83 else
84 quirks &= ~CP_2WHEEL_MOUSE_HACK_ON;
85 hid_set_drvdata(hdev, (void *)quirks);
86 return 1;
87 }
88
89 if (usage->code == REL_WHEEL && (quirks & CP_2WHEEL_MOUSE_HACK_ON)) {
90 struct input_dev *input = field->hidinput->input;
91
92 input_event(input, usage->type, REL_HWHEEL, value);
93 return 1;
94 }
95
96 return 0;
97}
98
99static int cp_probe(struct hid_device *hdev, const struct hid_device_id *id)
100{
101 unsigned long quirks = id->driver_data;
102 int ret;
103
104 hid_set_drvdata(hdev, (void *)quirks);
105
106 ret = hid_parse(hdev);
107 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800108 hid_err(hdev, "parse failed\n");
Jiri Slaby0f221322008-06-23 22:54:08 +0200109 goto err_free;
110 }
111
Jiri Slaby93c10132008-06-27 00:04:24 +0200112 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Jiri Slaby0f221322008-06-23 22:54:08 +0200113 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800114 hid_err(hdev, "hw start failed\n");
Jiri Slaby0f221322008-06-23 22:54:08 +0200115 goto err_free;
116 }
117
118 return 0;
119err_free:
120 return ret;
121}
122
123static const struct hid_device_id cp_devices[] = {
124 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_1),
125 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
126 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_2),
127 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
Jiri Kosinae8d0eab2009-12-02 22:54:11 +0100128 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_3),
129 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
Lionel Vaux76c9d8f2012-07-22 11:32:20 +0200130 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_BARCODE_4),
131 .driver_data = CP_RDESC_SWAPPED_MIN_MAX },
Jiri Slaby0f221322008-06-23 22:54:08 +0200132 { HID_USB_DEVICE(USB_VENDOR_ID_CYPRESS, USB_DEVICE_ID_CYPRESS_MOUSE),
133 .driver_data = CP_2WHEEL_MOUSE_HACK },
134 { }
135};
136MODULE_DEVICE_TABLE(hid, cp_devices);
137
138static struct hid_driver cp_driver = {
139 .name = "cypress",
140 .id_table = cp_devices,
141 .report_fixup = cp_report_fixup,
142 .input_mapped = cp_input_mapped,
143 .event = cp_event,
144 .probe = cp_probe,
145};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700146module_hid_driver(cp_driver);
Jiri Slaby0f221322008-06-23 22:54:08 +0200147
Jiri Slaby0f221322008-06-23 22:54:08 +0200148MODULE_LICENSE("GPL");