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