1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Plantronics USB HID Driver
5 * Copyright (c) 2014 JD Cole <jd.cole@plantronics.com>
6 * Copyright (c) 2015-2018 Terry Junge <terry.junge@plantronics.com>
14 #include <linux/hid.h>
15 #include <linux/module.h>
16 #include <linux/jiffies.h>
18 #define PLT_HID_1_0_PAGE 0xffa00000
19 #define PLT_HID_2_0_PAGE 0xffa20000
21 #define PLT_BASIC_TELEPHONY 0x0003
22 #define PLT_BASIC_EXCEPTION 0x0005
24 #define PLT_VOL_UP 0x00b1
25 #define PLT_VOL_DOWN 0x00b2
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)
32 #define PLT_DA60 0xda60
33 #define PLT_BT300_MIN 0x0413
34 #define PLT_BT300_MAX 0x0418
37 #define PLT_ALLOW_CONSUMER (field->application == HID_CP_CONSUMERCONTROL && \
38 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
40 #define PLT_QUIRK_DOUBLE_VOLUME_KEYS BIT(0)
42 #define PLT_DOUBLE_KEY_TIMEOUT 5 /* ms */
45 unsigned long device_type;
46 unsigned long last_volume_key_ts;
50 static int plantronics_input_mapping(struct hid_device *hdev,
52 struct hid_field *field,
53 struct hid_usage *usage,
54 unsigned long **bit, int *max)
56 unsigned short mapped_key;
57 struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
58 unsigned long plt_type = drv_data->device_type;
60 /* special case for PTT products */
61 if (field->application == HID_GD_JOYSTICK)
64 /* handle volume up/down mapping */
65 /* non-standard types or multi-HID interfaces - plt_type is PID */
66 if (!(plt_type & HID_USAGE_PAGE)) {
69 if (PLT_ALLOW_CONSUMER)
73 if (PLT_ALLOW_CONSUMER)
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)
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)) {
90 mapped_key = KEY_VOLUMEUP;
94 mapped_key = KEY_VOLUMEDOWN;
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
109 hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n",
110 usage->hid, field->application);
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);
120 static int plantronics_event(struct hid_device *hdev, struct hid_field *field,
121 struct hid_usage *usage, __s32 value)
123 struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
125 if (drv_data->quirks & PLT_QUIRK_DOUBLE_VOLUME_KEYS) {
126 unsigned long prev_ts, cur_ts;
128 /* Usages are filtered in plantronics_usages. */
130 if (!value) /* Handle key presses only. */
133 prev_ts = drv_data->last_volume_key_ts;
135 if (jiffies_to_msecs(cur_ts - prev_ts) <= PLT_DOUBLE_KEY_TIMEOUT)
136 return 1; /* Ignore the repeated key. */
138 drv_data->last_volume_key_ts = cur_ts;
144 static unsigned long plantronics_device_type(struct hid_device *hdev)
146 unsigned i, col_page;
147 unsigned long plt_type = hdev->product;
149 /* multi-HID interfaces? - plt_type is PID */
150 if (plt_type >= PLT_BT300_MIN && plt_type <= PLT_BT300_MAX)
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;
160 if (col_page == PLT_HID_1_0_PAGE)
161 plt_type = hdev->collection[i].usage;
165 hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type);
169 static int plantronics_probe(struct hid_device *hdev,
170 const struct hid_device_id *id)
172 struct plt_drv_data *drv_data;
175 drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL);
179 ret = hid_parse(hdev);
181 hid_err(hdev, "parse failed\n");
185 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);
189 hid_set_drvdata(hdev, drv_data);
191 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
192 HID_CONNECT_HIDINPUT_FORCE | HID_CONNECT_HIDDEV_FORCE);
194 hid_err(hdev, "hw start failed\n");
200 static const struct hid_device_id plantronics_devices[] = {
201 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS,
202 USB_DEVICE_ID_PLANTRONICS_BLACKWIRE_3210_SERIES),
203 .driver_data = PLT_QUIRK_DOUBLE_VOLUME_KEYS },
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,
208 USB_DEVICE_ID_PLANTRONICS_BLACKWIRE_3215_SERIES),
209 .driver_data = PLT_QUIRK_DOUBLE_VOLUME_KEYS },
210 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS,
211 USB_DEVICE_ID_PLANTRONICS_BLACKWIRE_3225_SERIES),
212 .driver_data = PLT_QUIRK_DOUBLE_VOLUME_KEYS },
213 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) },
216 MODULE_DEVICE_TABLE(hid, plantronics_devices);
218 static const struct hid_usage_id plantronics_usages[] = {
219 { HID_CP_VOLUMEUP, EV_KEY, HID_ANY_ID },
220 { HID_CP_VOLUMEDOWN, EV_KEY, HID_ANY_ID },
221 { HID_TERMINATOR, HID_TERMINATOR, HID_TERMINATOR }
224 static struct hid_driver plantronics_driver = {
225 .name = "plantronics",
226 .id_table = plantronics_devices,
227 .usage_table = plantronics_usages,
228 .input_mapping = plantronics_input_mapping,
229 .event = plantronics_event,
230 .probe = plantronics_probe,
232 module_hid_driver(plantronics_driver);
234 MODULE_AUTHOR("JD Cole <jd.cole@plantronics.com>");
235 MODULE_AUTHOR("Terry Junge <terry.junge@plantronics.com>");
236 MODULE_DESCRIPTION("Plantronics USB HID Driver");
237 MODULE_LICENSE("GPL");