2 * Plantronics USB HID Driver
4 * Copyright (c) 2014 JD Cole <jd.cole@plantronics.com>
5 * Copyright (c) 2015-2018 Terry Junge <terry.junge@plantronics.com>
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)
17 #include <linux/hid.h>
18 #include <linux/module.h>
19 #include <linux/jiffies.h>
21 #define PLT_HID_1_0_PAGE 0xffa00000
22 #define PLT_HID_2_0_PAGE 0xffa20000
24 #define PLT_BASIC_TELEPHONY 0x0003
25 #define PLT_BASIC_EXCEPTION 0x0005
27 #define PLT_VOL_UP 0x00b1
28 #define PLT_VOL_DOWN 0x00b2
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)
35 #define PLT_DA60 0xda60
36 #define PLT_BT300_MIN 0x0413
37 #define PLT_BT300_MAX 0x0418
40 #define PLT_ALLOW_CONSUMER (field->application == HID_CP_CONSUMERCONTROL && \
41 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
43 #define PLT_QUIRK_DOUBLE_VOLUME_KEYS BIT(0)
45 #define PLT_DOUBLE_KEY_TIMEOUT 5 /* ms */
48 unsigned long device_type;
49 unsigned long last_volume_key_ts;
53 static int plantronics_input_mapping(struct hid_device *hdev,
55 struct hid_field *field,
56 struct hid_usage *usage,
57 unsigned long **bit, int *max)
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;
63 /* special case for PTT products */
64 if (field->application == HID_GD_JOYSTICK)
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)) {
72 if (PLT_ALLOW_CONSUMER)
76 if (PLT_ALLOW_CONSUMER)
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)
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)) {
93 mapped_key = KEY_VOLUMEUP;
97 mapped_key = KEY_VOLUMEDOWN;
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
112 hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n",
113 usage->hid, field->application);
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);
123 static int plantronics_event(struct hid_device *hdev, struct hid_field *field,
124 struct hid_usage *usage, __s32 value)
126 struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
128 if (drv_data->quirks & PLT_QUIRK_DOUBLE_VOLUME_KEYS) {
129 unsigned long prev_ts, cur_ts;
131 /* Usages are filtered in plantronics_usages. */
133 if (!value) /* Handle key presses only. */
136 prev_ts = drv_data->last_volume_key_ts;
138 if (jiffies_to_msecs(cur_ts - prev_ts) <= PLT_DOUBLE_KEY_TIMEOUT)
139 return 1; /* Ignore the repeated key. */
141 drv_data->last_volume_key_ts = cur_ts;
147 static unsigned long plantronics_device_type(struct hid_device *hdev)
149 unsigned i, col_page;
150 unsigned long plt_type = hdev->product;
152 /* multi-HID interfaces? - plt_type is PID */
153 if (plt_type >= PLT_BT300_MIN && plt_type <= PLT_BT300_MAX)
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;
163 if (col_page == PLT_HID_1_0_PAGE)
164 plt_type = hdev->collection[i].usage;
168 hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type);
172 static int plantronics_probe(struct hid_device *hdev,
173 const struct hid_device_id *id)
175 struct plt_drv_data *drv_data;
178 drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL);
182 ret = hid_parse(hdev);
184 hid_err(hdev, "parse failed\n");
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);
192 hid_set_drvdata(hdev, drv_data);
194 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
195 HID_CONNECT_HIDINPUT_FORCE | HID_CONNECT_HIDDEV_FORCE);
197 hid_err(hdev, "hw start failed\n");
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) },
210 MODULE_DEVICE_TABLE(hid, plantronics_devices);
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 }
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,
226 module_hid_driver(plantronics_driver);
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");