blob: fd806f590bf8af4db2d62cbd689da398e3a21040 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jiri Slaby14a21cd2008-06-23 23:31:09 +02002/*
3 * HID driver for some a4tech "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 Slaby14a21cd2008-06-23 23:31:09 +02009 * Copyright (c) 2008 Jiri Slaby
10 */
11
12/*
Jiri Slaby14a21cd2008-06-23 23:31:09 +020013 */
14
15#include <linux/device.h>
16#include <linux/input.h>
17#include <linux/hid.h>
18#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Jiri Slaby14a21cd2008-06-23 23:31:09 +020020
21#include "hid-ids.h"
22
23#define A4_2WHEEL_MOUSE_HACK_7 0x01
24#define A4_2WHEEL_MOUSE_HACK_B8 0x02
25
26struct a4tech_sc {
27 unsigned long quirks;
28 unsigned int hw_wheel;
29 __s32 delayed_value;
30};
31
32static int a4_input_mapped(struct hid_device *hdev, struct hid_input *hi,
33 struct hid_field *field, struct hid_usage *usage,
34 unsigned long **bit, int *max)
35{
36 struct a4tech_sc *a4 = hid_get_drvdata(hdev);
37
38 if (usage->type == EV_REL && usage->code == REL_WHEEL)
39 set_bit(REL_HWHEEL, *bit);
40
41 if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007)
42 return -1;
43
44 return 0;
45}
46
47static int a4_event(struct hid_device *hdev, struct hid_field *field,
48 struct hid_usage *usage, __s32 value)
49{
50 struct a4tech_sc *a4 = hid_get_drvdata(hdev);
51 struct input_dev *input;
52
53 if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
54 !usage->type)
55 return 0;
56
57 input = field->hidinput->input;
58
59 if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8) {
60 if (usage->type == EV_REL && usage->code == REL_WHEEL) {
61 a4->delayed_value = value;
62 return 1;
63 }
64
65 if (usage->hid == 0x000100b8) {
66 input_event(input, EV_REL, value ? REL_HWHEEL :
67 REL_WHEEL, a4->delayed_value);
68 return 1;
69 }
70 }
71
72 if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) {
73 a4->hw_wheel = !!value;
74 return 1;
75 }
76
77 if (usage->code == REL_WHEEL && a4->hw_wheel) {
78 input_event(input, usage->type, REL_HWHEEL, value);
79 return 1;
80 }
81
82 return 0;
83}
84
85static int a4_probe(struct hid_device *hdev, const struct hid_device_id *id)
86{
87 struct a4tech_sc *a4;
88 int ret;
89
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +020090 a4 = devm_kzalloc(&hdev->dev, sizeof(*a4), GFP_KERNEL);
Jiri Slaby14a21cd2008-06-23 23:31:09 +020091 if (a4 == NULL) {
Joe Perches4291ee32010-12-09 19:29:03 -080092 hid_err(hdev, "can't alloc device descriptor\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +020093 return -ENOMEM;
Jiri Slaby14a21cd2008-06-23 23:31:09 +020094 }
95
96 a4->quirks = id->driver_data;
97
98 hid_set_drvdata(hdev, a4);
99
100 ret = hid_parse(hdev);
101 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800102 hid_err(hdev, "parse failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +0200103 return ret;
Jiri Slaby14a21cd2008-06-23 23:31:09 +0200104 }
105
Jiri Slaby93c10132008-06-27 00:04:24 +0200106 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
Jiri Slaby14a21cd2008-06-23 23:31:09 +0200107 if (ret) {
Joe Perches4291ee32010-12-09 19:29:03 -0800108 hid_err(hdev, "hw start failed\n");
Benjamin Tissoiresabf832b2013-07-24 19:38:04 +0200109 return ret;
Jiri Slaby14a21cd2008-06-23 23:31:09 +0200110 }
111
112 return 0;
Jiri Slaby14a21cd2008-06-23 23:31:09 +0200113}
114
115static const struct hid_device_id a4_devices[] = {
116 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU),
117 .driver_data = A4_2WHEEL_MOUSE_HACK_7 },
118 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D),
119 .driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
Lech Perczakb7e1b202010-09-16 23:19:33 +0200120 { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649),
121 .driver_data = A4_2WHEEL_MOUSE_HACK_B8 },
Jiri Slaby14a21cd2008-06-23 23:31:09 +0200122 { }
123};
124MODULE_DEVICE_TABLE(hid, a4_devices);
125
126static struct hid_driver a4_driver = {
127 .name = "a4tech",
128 .id_table = a4_devices,
129 .input_mapped = a4_input_mapped,
130 .event = a4_event,
131 .probe = a4_probe,
Jiri Slaby14a21cd2008-06-23 23:31:09 +0200132};
H Hartley Sweetenf4254582012-12-17 15:28:26 -0700133module_hid_driver(a4_driver);
Jiri Slaby14a21cd2008-06-23 23:31:09 +0200134
Jiri Slaby14a21cd2008-06-23 23:31:09 +0200135MODULE_LICENSE("GPL");