1 /*
2 * Plantronics USB HID Driver
3 *
4 * Copyright (c) 2014 JD Cole <jd.cole@plantronics.com>
5 * Copyright (c) 2015-2018 Terry Junge <terry.junge@plantronics.com>
6 */
7
8 /*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 */
14
15 #include "hid-ids.h"
16
17 #include <linux/hid.h>
18 #include <linux/module.h>
19 #include <linux/jiffies.h>
20
21 #define PLT_HID_1_0_PAGE 0xffa00000
22 #define PLT_HID_2_0_PAGE 0xffa20000
23
24 #define PLT_BASIC_TELEPHONY 0x0003
25 #define PLT_BASIC_EXCEPTION 0x0005
26
27 #define PLT_VOL_UP 0x00b1
28 #define PLT_VOL_DOWN 0x00b2
29
30 #define PLT1_VOL_UP (PLT_HID_1_0_PAGE | PLT_VOL_UP)
31 #define PLT1_VOL_DOWN (PLT_HID_1_0_PAGE | PLT_VOL_DOWN)
32 #define PLT2_VOL_UP (PLT_HID_2_0_PAGE | PLT_VOL_UP)
33 #define PLT2_VOL_DOWN (PLT_HID_2_0_PAGE | PLT_VOL_DOWN)
34
35 #define PLT_DA60 0xda60
36 #define PLT_BT300_MIN 0x0413
37 #define PLT_BT300_MAX 0x0418
38
39
40 #define PLT_ALLOW_CONSUMER (field->application == HID_CP_CONSUMERCONTROL && \
41 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
42
43 #define PLT_QUIRK_DOUBLE_VOLUME_KEYS BIT(0)
44
45 #define PLT_DOUBLE_KEY_TIMEOUT 5 /* ms */
46
47 struct plt_drv_data {
48 unsigned long device_type;
49 unsigned long last_volume_key_ts;
50 u32 quirks;
51 };
52
plantronics_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)53 static int plantronics_input_mapping(struct hid_device *hdev,
54 struct hid_input *hi,
55 struct hid_field *field,
56 struct hid_usage *usage,
57 unsigned long **bit, int *max)
58 {
59 unsigned short mapped_key;
60 struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
61 unsigned long plt_type = drv_data->device_type;
62
63 /* special case for PTT products */
64 if (field->application == HID_GD_JOYSTICK)
65 goto defaulted;
66
67 /* handle volume up/down mapping */
68 /* non-standard types or multi-HID interfaces - plt_type is PID */
69 if (!(plt_type & HID_USAGE_PAGE)) {
70 switch (plt_type) {
71 case PLT_DA60:
72 if (PLT_ALLOW_CONSUMER)
73 goto defaulted;
74 goto ignored;
75 default:
76 if (PLT_ALLOW_CONSUMER)
77 goto defaulted;
78 }
79 }
80 /* handle standard types - plt_type is 0xffa0uuuu or 0xffa2uuuu */
81 /* 'basic telephony compliant' - allow default consumer page map */
82 else if ((plt_type & HID_USAGE) >= PLT_BASIC_TELEPHONY &&
83 (plt_type & HID_USAGE) != PLT_BASIC_EXCEPTION) {
84 if (PLT_ALLOW_CONSUMER)
85 goto defaulted;
86 }
87 /* not 'basic telephony' - apply legacy mapping */
88 /* only map if the field is in the device's primary vendor page */
89 else if (!((field->application ^ plt_type) & HID_USAGE_PAGE)) {
90 switch (usage->hid) {
91 case PLT1_VOL_UP:
92 case PLT2_VOL_UP:
93 mapped_key = KEY_VOLUMEUP;
94 goto mapped;
95 case PLT1_VOL_DOWN:
96 case PLT2_VOL_DOWN:
97 mapped_key = KEY_VOLUMEDOWN;
98 goto mapped;
99 }
100 }
101
102 /*
103 * Future mapping of call control or other usages,
104 * if and when keys are defined would go here
105 * otherwise, ignore everything else that was not mapped
106 */
107
108 ignored:
109 return -1;
110
111 defaulted:
112 hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n",
113 usage->hid, field->application);
114 return 0;
115
116 mapped:
117 hid_map_usage_clear(hi, usage, bit, max, EV_KEY, mapped_key);
118 hid_dbg(hdev, "usage: %08x (appl: %08x) - mapped to key %d\n",
119 usage->hid, field->application, mapped_key);
120 return 1;
121 }
122
plantronics_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)123 static int plantronics_event(struct hid_device *hdev, struct hid_field *field,
124 struct hid_usage *usage, __s32 value)
125 {
126 struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
127
128 if (drv_data->quirks & PLT_QUIRK_DOUBLE_VOLUME_KEYS) {
129 unsigned long prev_ts, cur_ts;
130
131 /* Usages are filtered in plantronics_usages. */
132
133 if (!value) /* Handle key presses only. */
134 return 0;
135
136 prev_ts = drv_data->last_volume_key_ts;
137 cur_ts = jiffies;
138 if (jiffies_to_msecs(cur_ts - prev_ts) <= PLT_DOUBLE_KEY_TIMEOUT)
139 return 1; /* Ignore the repeated key. */
140
141 drv_data->last_volume_key_ts = cur_ts;
142 }
143
144 return 0;
145 }
146
plantronics_device_type(struct hid_device * hdev)147 static unsigned long plantronics_device_type(struct hid_device *hdev)
148 {
149 unsigned i, col_page;
150 unsigned long plt_type = hdev->product;
151
152 /* multi-HID interfaces? - plt_type is PID */
153 if (plt_type >= PLT_BT300_MIN && plt_type <= PLT_BT300_MAX)
154 goto exit;
155
156 /* determine primary vendor page */
157 for (i = 0; i < hdev->maxcollection; i++) {
158 col_page = hdev->collection[i].usage & HID_USAGE_PAGE;
159 if (col_page == PLT_HID_2_0_PAGE) {
160 plt_type = hdev->collection[i].usage;
161 break;
162 }
163 if (col_page == PLT_HID_1_0_PAGE)
164 plt_type = hdev->collection[i].usage;
165 }
166
167 exit:
168 hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type);
169 return plt_type;
170 }
171
plantronics_probe(struct hid_device * hdev,const struct hid_device_id * id)172 static int plantronics_probe(struct hid_device *hdev,
173 const struct hid_device_id *id)
174 {
175 struct plt_drv_data *drv_data;
176 int ret;
177
178 drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL);
179 if (!drv_data)
180 return -ENOMEM;
181
182 ret = hid_parse(hdev);
183 if (ret) {
184 hid_err(hdev, "parse failed\n");
185 goto err;
186 }
187
188 drv_data->device_type = plantronics_device_type(hdev);
189 drv_data->quirks = id->driver_data;
190 drv_data->last_volume_key_ts = jiffies - msecs_to_jiffies(PLT_DOUBLE_KEY_TIMEOUT);
191
192 hid_set_drvdata(hdev, drv_data);
193
194 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
195 HID_CONNECT_HIDINPUT_FORCE | HID_CONNECT_HIDDEV_FORCE);
196 if (ret)
197 hid_err(hdev, "hw start failed\n");
198
199 err:
200 return ret;
201 }
202
203 static const struct hid_device_id plantronics_devices[] = {
204 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS,
205 USB_DEVICE_ID_PLANTRONICS_BLACKWIRE_3220_SERIES),
206 .driver_data = PLT_QUIRK_DOUBLE_VOLUME_KEYS },
207 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) },
208 { }
209 };
210 MODULE_DEVICE_TABLE(hid, plantronics_devices);
211
212 static const struct hid_usage_id plantronics_usages[] = {
213 { HID_CP_VOLUMEUP, EV_KEY, HID_ANY_ID },
214 { HID_CP_VOLUMEDOWN, EV_KEY, HID_ANY_ID },
215 { HID_TERMINATOR, HID_TERMINATOR, HID_TERMINATOR }
216 };
217
218 static struct hid_driver plantronics_driver = {
219 .name = "plantronics",
220 .id_table = plantronics_devices,
221 .usage_table = plantronics_usages,
222 .input_mapping = plantronics_input_mapping,
223 .event = plantronics_event,
224 .probe = plantronics_probe,
225 };
226 module_hid_driver(plantronics_driver);
227
228 MODULE_AUTHOR("JD Cole <jd.cole@plantronics.com>");
229 MODULE_AUTHOR("Terry Junge <terry.junge@plantronics.com>");
230 MODULE_DESCRIPTION("Plantronics USB HID Driver");
231 MODULE_LICENSE("GPL");
232