blob: e81b7cec2d1248e1600ce90028cad5cf12e5fbb7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
JD Cole1a3f83f2014-10-31 17:34:42 -07002/*
3 * Plantronics USB HID Driver
4 *
5 * Copyright (c) 2014 JD Cole <jd.cole@plantronics.com>
Terry Junge37e376d2018-04-30 13:32:46 -07006 * Copyright (c) 2015-2018 Terry Junge <terry.junge@plantronics.com>
JD Cole1a3f83f2014-10-31 17:34:42 -07007 */
8
9/*
JD Cole1a3f83f2014-10-31 17:34:42 -070010 */
11
12#include "hid-ids.h"
13
14#include <linux/hid.h>
15#include <linux/module.h>
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +020016#include <linux/jiffies.h>
JD Cole1a3f83f2014-10-31 17:34:42 -070017
Terry Junge81bb773fa2015-06-08 14:27:57 -070018#define PLT_HID_1_0_PAGE 0xffa00000
19#define PLT_HID_2_0_PAGE 0xffa20000
20
21#define PLT_BASIC_TELEPHONY 0x0003
22#define PLT_BASIC_EXCEPTION 0x0005
23
24#define PLT_VOL_UP 0x00b1
25#define PLT_VOL_DOWN 0x00b2
26
27#define PLT1_VOL_UP (PLT_HID_1_0_PAGE | PLT_VOL_UP)
28#define PLT1_VOL_DOWN (PLT_HID_1_0_PAGE | PLT_VOL_DOWN)
29#define PLT2_VOL_UP (PLT_HID_2_0_PAGE | PLT_VOL_UP)
30#define PLT2_VOL_DOWN (PLT_HID_2_0_PAGE | PLT_VOL_DOWN)
31
32#define PLT_DA60 0xda60
33#define PLT_BT300_MIN 0x0413
34#define PLT_BT300_MAX 0x0418
35
36
37#define PLT_ALLOW_CONSUMER (field->application == HID_CP_CONSUMERCONTROL && \
38 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
39
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +020040#define PLT_QUIRK_DOUBLE_VOLUME_KEYS BIT(0)
41
42#define PLT_DOUBLE_KEY_TIMEOUT 5 /* ms */
43
44struct plt_drv_data {
45 unsigned long device_type;
46 unsigned long last_volume_key_ts;
47 u32 quirks;
48};
49
JD Cole1a3f83f2014-10-31 17:34:42 -070050static int plantronics_input_mapping(struct hid_device *hdev,
51 struct hid_input *hi,
52 struct hid_field *field,
53 struct hid_usage *usage,
54 unsigned long **bit, int *max)
55{
Terry Junge81bb773fa2015-06-08 14:27:57 -070056 unsigned short mapped_key;
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +020057 struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
58 unsigned long plt_type = drv_data->device_type;
Terry Junge81bb773fa2015-06-08 14:27:57 -070059
Terry Junge37e376d2018-04-30 13:32:46 -070060 /* special case for PTT products */
61 if (field->application == HID_GD_JOYSTICK)
62 goto defaulted;
63
Terry Junge81bb773fa2015-06-08 14:27:57 -070064 /* handle volume up/down mapping */
65 /* non-standard types or multi-HID interfaces - plt_type is PID */
66 if (!(plt_type & HID_USAGE_PAGE)) {
67 switch (plt_type) {
68 case PLT_DA60:
69 if (PLT_ALLOW_CONSUMER)
70 goto defaulted;
71 goto ignored;
72 default:
73 if (PLT_ALLOW_CONSUMER)
74 goto defaulted;
75 }
76 }
77 /* handle standard types - plt_type is 0xffa0uuuu or 0xffa2uuuu */
78 /* 'basic telephony compliant' - allow default consumer page map */
79 else if ((plt_type & HID_USAGE) >= PLT_BASIC_TELEPHONY &&
80 (plt_type & HID_USAGE) != PLT_BASIC_EXCEPTION) {
81 if (PLT_ALLOW_CONSUMER)
82 goto defaulted;
83 }
84 /* not 'basic telephony' - apply legacy mapping */
85 /* only map if the field is in the device's primary vendor page */
86 else if (!((field->application ^ plt_type) & HID_USAGE_PAGE)) {
87 switch (usage->hid) {
88 case PLT1_VOL_UP:
89 case PLT2_VOL_UP:
90 mapped_key = KEY_VOLUMEUP;
91 goto mapped;
92 case PLT1_VOL_DOWN:
93 case PLT2_VOL_DOWN:
94 mapped_key = KEY_VOLUMEDOWN;
95 goto mapped;
96 }
JD Cole1a3f83f2014-10-31 17:34:42 -070097 }
98
Terry Junge81bb773fa2015-06-08 14:27:57 -070099/*
100 * Future mapping of call control or other usages,
101 * if and when keys are defined would go here
102 * otherwise, ignore everything else that was not mapped
103 */
JD Cole1a3f83f2014-10-31 17:34:42 -0700104
Terry Junge81bb773fa2015-06-08 14:27:57 -0700105ignored:
JD Cole1a3f83f2014-10-31 17:34:42 -0700106 return -1;
Terry Junge81bb773fa2015-06-08 14:27:57 -0700107
108defaulted:
109 hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n",
110 usage->hid, field->application);
111 return 0;
112
113mapped:
114 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, mapped_key);
115 hid_dbg(hdev, "usage: %08x (appl: %08x) - mapped to key %d\n",
116 usage->hid, field->application, mapped_key);
117 return 1;
118}
119
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +0200120static int plantronics_event(struct hid_device *hdev, struct hid_field *field,
121 struct hid_usage *usage, __s32 value)
122{
123 struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
124
125 if (drv_data->quirks & PLT_QUIRK_DOUBLE_VOLUME_KEYS) {
126 unsigned long prev_ts, cur_ts;
127
128 /* Usages are filtered in plantronics_usages. */
129
130 if (!value) /* Handle key presses only. */
131 return 0;
132
133 prev_ts = drv_data->last_volume_key_ts;
134 cur_ts = jiffies;
135 if (jiffies_to_msecs(cur_ts - prev_ts) <= PLT_DOUBLE_KEY_TIMEOUT)
136 return 1; /* Ignore the repeated key. */
137
138 drv_data->last_volume_key_ts = cur_ts;
139 }
140
141 return 0;
142}
143
Terry Junge81bb773fa2015-06-08 14:27:57 -0700144static unsigned long plantronics_device_type(struct hid_device *hdev)
145{
146 unsigned i, col_page;
147 unsigned long plt_type = hdev->product;
148
149 /* multi-HID interfaces? - plt_type is PID */
150 if (plt_type >= PLT_BT300_MIN && plt_type <= PLT_BT300_MAX)
151 goto exit;
152
153 /* determine primary vendor page */
154 for (i = 0; i < hdev->maxcollection; i++) {
155 col_page = hdev->collection[i].usage & HID_USAGE_PAGE;
156 if (col_page == PLT_HID_2_0_PAGE) {
157 plt_type = hdev->collection[i].usage;
158 break;
159 }
160 if (col_page == PLT_HID_1_0_PAGE)
161 plt_type = hdev->collection[i].usage;
162 }
163
164exit:
165 hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type);
166 return plt_type;
167}
168
169static int plantronics_probe(struct hid_device *hdev,
170 const struct hid_device_id *id)
171{
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +0200172 struct plt_drv_data *drv_data;
Terry Junge81bb773fa2015-06-08 14:27:57 -0700173 int ret;
174
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +0200175 drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL);
176 if (!drv_data)
177 return -ENOMEM;
178
Terry Junge81bb773fa2015-06-08 14:27:57 -0700179 ret = hid_parse(hdev);
180 if (ret) {
181 hid_err(hdev, "parse failed\n");
182 goto err;
183 }
184
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +0200185 drv_data->device_type = plantronics_device_type(hdev);
186 drv_data->quirks = id->driver_data;
187 drv_data->last_volume_key_ts = jiffies - msecs_to_jiffies(PLT_DOUBLE_KEY_TIMEOUT);
188
189 hid_set_drvdata(hdev, drv_data);
Terry Junge81bb773fa2015-06-08 14:27:57 -0700190
191 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
192 HID_CONNECT_HIDINPUT_FORCE | HID_CONNECT_HIDDEV_FORCE);
193 if (ret)
194 hid_err(hdev, "hw start failed\n");
195
196err:
197 return ret;
JD Cole1a3f83f2014-10-31 17:34:42 -0700198}
199
JD Cole1a3f83f2014-10-31 17:34:42 -0700200static const struct hid_device_id plantronics_devices[] = {
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +0200201 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS,
202 USB_DEVICE_ID_PLANTRONICS_BLACKWIRE_3220_SERIES),
203 .driver_data = PLT_QUIRK_DOUBLE_VOLUME_KEYS },
JD Cole1a3f83f2014-10-31 17:34:42 -0700204 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) },
205 { }
206};
207MODULE_DEVICE_TABLE(hid, plantronics_devices);
208
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +0200209static const struct hid_usage_id plantronics_usages[] = {
210 { HID_CP_VOLUMEUP, EV_KEY, HID_ANY_ID },
211 { HID_CP_VOLUMEDOWN, EV_KEY, HID_ANY_ID },
212 { HID_TERMINATOR, HID_TERMINATOR, HID_TERMINATOR }
213};
214
JD Cole1a3f83f2014-10-31 17:34:42 -0700215static struct hid_driver plantronics_driver = {
216 .name = "plantronics",
217 .id_table = plantronics_devices,
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +0200218 .usage_table = plantronics_usages,
JD Cole1a3f83f2014-10-31 17:34:42 -0700219 .input_mapping = plantronics_input_mapping,
Maxim Mikityanskiyf567d6e2021-02-07 16:47:40 +0200220 .event = plantronics_event,
Terry Junge81bb773fa2015-06-08 14:27:57 -0700221 .probe = plantronics_probe,
JD Cole1a3f83f2014-10-31 17:34:42 -0700222};
223module_hid_driver(plantronics_driver);
224
225MODULE_AUTHOR("JD Cole <jd.cole@plantronics.com>");
226MODULE_AUTHOR("Terry Junge <terry.junge@plantronics.com>");
227MODULE_DESCRIPTION("Plantronics USB HID Driver");
228MODULE_LICENSE("GPL");