Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 2 | /* |
| 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 Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 9 | * Copyright (c) 2008 Jiri Slaby |
| 10 | */ |
| 11 | |
| 12 | /* |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/input.h> |
| 17 | #include <linux/hid.h> |
| 18 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 20 | |
| 21 | #include "hid-ids.h" |
| 22 | |
| 23 | #define A4_2WHEEL_MOUSE_HACK_7 0x01 |
| 24 | #define A4_2WHEEL_MOUSE_HACK_B8 0x02 |
| 25 | |
Nicolas Saenz Julienne | 1c703b5 | 2019-06-11 14:13:20 +0200 | [diff] [blame] | 26 | #define A4_WHEEL_ORIENTATION (HID_UP_GENDESK | 0x000000b8) |
| 27 | |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 28 | struct a4tech_sc { |
| 29 | unsigned long quirks; |
| 30 | unsigned int hw_wheel; |
| 31 | __s32 delayed_value; |
| 32 | }; |
| 33 | |
Nicolas Saenz Julienne | 1c703b5 | 2019-06-11 14:13:20 +0200 | [diff] [blame] | 34 | static int a4_input_mapping(struct hid_device *hdev, struct hid_input *hi, |
| 35 | struct hid_field *field, struct hid_usage *usage, |
| 36 | unsigned long **bit, int *max) |
| 37 | { |
| 38 | struct a4tech_sc *a4 = hid_get_drvdata(hdev); |
| 39 | |
| 40 | if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8 && |
| 41 | usage->hid == A4_WHEEL_ORIENTATION) { |
| 42 | /* |
| 43 | * We do not want to have this usage mapped to anything as it's |
| 44 | * nonstandard and doesn't really behave like an HID report. |
| 45 | * It's only selecting the orientation (vertical/horizontal) of |
| 46 | * the previous mouse wheel report. The input_events will be |
| 47 | * generated once both reports are recorded in a4_event(). |
| 48 | */ |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | return 0; |
| 53 | |
| 54 | } |
| 55 | |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 56 | static int a4_input_mapped(struct hid_device *hdev, struct hid_input *hi, |
| 57 | struct hid_field *field, struct hid_usage *usage, |
| 58 | unsigned long **bit, int *max) |
| 59 | { |
| 60 | struct a4tech_sc *a4 = hid_get_drvdata(hdev); |
| 61 | |
Błażej Szczygieł | abf82e8f | 2019-05-12 22:33:13 +0200 | [diff] [blame] | 62 | if (usage->type == EV_REL && usage->code == REL_WHEEL_HI_RES) { |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 63 | set_bit(REL_HWHEEL, *bit); |
Błażej Szczygieł | abf82e8f | 2019-05-12 22:33:13 +0200 | [diff] [blame] | 64 | set_bit(REL_HWHEEL_HI_RES, *bit); |
| 65 | } |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 66 | |
| 67 | if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) |
| 68 | return -1; |
| 69 | |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | static int a4_event(struct hid_device *hdev, struct hid_field *field, |
| 74 | struct hid_usage *usage, __s32 value) |
| 75 | { |
| 76 | struct a4tech_sc *a4 = hid_get_drvdata(hdev); |
| 77 | struct input_dev *input; |
| 78 | |
Nicolas Saenz Julienne | 1c703b5 | 2019-06-11 14:13:20 +0200 | [diff] [blame] | 79 | if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput) |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 80 | return 0; |
| 81 | |
| 82 | input = field->hidinput->input; |
| 83 | |
| 84 | if (a4->quirks & A4_2WHEEL_MOUSE_HACK_B8) { |
Błażej Szczygieł | abf82e8f | 2019-05-12 22:33:13 +0200 | [diff] [blame] | 85 | if (usage->type == EV_REL && usage->code == REL_WHEEL_HI_RES) { |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 86 | a4->delayed_value = value; |
| 87 | return 1; |
| 88 | } |
| 89 | |
Nicolas Saenz Julienne | 1c703b5 | 2019-06-11 14:13:20 +0200 | [diff] [blame] | 90 | if (usage->hid == A4_WHEEL_ORIENTATION) { |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 91 | input_event(input, EV_REL, value ? REL_HWHEEL : |
| 92 | REL_WHEEL, a4->delayed_value); |
Błażej Szczygieł | abf82e8f | 2019-05-12 22:33:13 +0200 | [diff] [blame] | 93 | input_event(input, EV_REL, value ? REL_HWHEEL_HI_RES : |
| 94 | REL_WHEEL_HI_RES, a4->delayed_value * 120); |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if ((a4->quirks & A4_2WHEEL_MOUSE_HACK_7) && usage->hid == 0x00090007) { |
| 100 | a4->hw_wheel = !!value; |
| 101 | return 1; |
| 102 | } |
| 103 | |
Błażej Szczygieł | abf82e8f | 2019-05-12 22:33:13 +0200 | [diff] [blame] | 104 | if (usage->code == REL_WHEEL_HI_RES && a4->hw_wheel) { |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 105 | input_event(input, usage->type, REL_HWHEEL, value); |
Błażej Szczygieł | abf82e8f | 2019-05-12 22:33:13 +0200 | [diff] [blame] | 106 | input_event(input, usage->type, REL_HWHEEL_HI_RES, value * 120); |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 107 | return 1; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int a4_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 114 | { |
| 115 | struct a4tech_sc *a4; |
| 116 | int ret; |
| 117 | |
Benjamin Tissoires | abf832b | 2013-07-24 19:38:04 +0200 | [diff] [blame] | 118 | a4 = devm_kzalloc(&hdev->dev, sizeof(*a4), GFP_KERNEL); |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 119 | if (a4 == NULL) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 120 | hid_err(hdev, "can't alloc device descriptor\n"); |
Benjamin Tissoires | abf832b | 2013-07-24 19:38:04 +0200 | [diff] [blame] | 121 | return -ENOMEM; |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | a4->quirks = id->driver_data; |
| 125 | |
| 126 | hid_set_drvdata(hdev, a4); |
| 127 | |
| 128 | ret = hid_parse(hdev); |
| 129 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 130 | hid_err(hdev, "parse failed\n"); |
Benjamin Tissoires | abf832b | 2013-07-24 19:38:04 +0200 | [diff] [blame] | 131 | return ret; |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Jiri Slaby | 93c1013 | 2008-06-27 00:04:24 +0200 | [diff] [blame] | 134 | ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 135 | if (ret) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 136 | hid_err(hdev, "hw start failed\n"); |
Benjamin Tissoires | abf832b | 2013-07-24 19:38:04 +0200 | [diff] [blame] | 137 | return ret; |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | return 0; |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static const struct hid_device_id a4_devices[] = { |
| 144 | { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_WCP32PU), |
| 145 | .driver_data = A4_2WHEEL_MOUSE_HACK_7 }, |
| 146 | { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_X5_005D), |
| 147 | .driver_data = A4_2WHEEL_MOUSE_HACK_B8 }, |
Lech Perczak | b7e1b20 | 2010-09-16 23:19:33 +0200 | [diff] [blame] | 148 | { HID_USB_DEVICE(USB_VENDOR_ID_A4TECH, USB_DEVICE_ID_A4TECH_RP_649), |
| 149 | .driver_data = A4_2WHEEL_MOUSE_HACK_B8 }, |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 150 | { } |
| 151 | }; |
| 152 | MODULE_DEVICE_TABLE(hid, a4_devices); |
| 153 | |
| 154 | static struct hid_driver a4_driver = { |
| 155 | .name = "a4tech", |
| 156 | .id_table = a4_devices, |
Nicolas Saenz Julienne | 1c703b5 | 2019-06-11 14:13:20 +0200 | [diff] [blame] | 157 | .input_mapping = a4_input_mapping, |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 158 | .input_mapped = a4_input_mapped, |
| 159 | .event = a4_event, |
| 160 | .probe = a4_probe, |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 161 | }; |
H Hartley Sweeten | f425458 | 2012-12-17 15:28:26 -0700 | [diff] [blame] | 162 | module_hid_driver(a4_driver); |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 163 | |
Jiri Slaby | 14a21cd | 2008-06-23 23:31:09 +0200 | [diff] [blame] | 164 | MODULE_LICENSE("GPL"); |