Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Plantronics USB HID Driver |
| 4 | * |
| 5 | * Copyright (c) 2014 JD Cole <jd.cole@plantronics.com> |
Terry Junge | 37e376d | 2018-04-30 13:32:46 -0700 | [diff] [blame] | 6 | * Copyright (c) 2015-2018 Terry Junge <terry.junge@plantronics.com> |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include "hid-ids.h" |
| 13 | |
| 14 | #include <linux/hid.h> |
| 15 | #include <linux/module.h> |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 16 | #include <linux/jiffies.h> |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 17 | |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 18 | #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 Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 40 | #define PLT_QUIRK_DOUBLE_VOLUME_KEYS BIT(0) |
| 41 | |
| 42 | #define PLT_DOUBLE_KEY_TIMEOUT 5 /* ms */ |
| 43 | |
| 44 | struct plt_drv_data { |
| 45 | unsigned long device_type; |
| 46 | unsigned long last_volume_key_ts; |
| 47 | u32 quirks; |
| 48 | }; |
| 49 | |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 50 | static 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 Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 56 | unsigned short mapped_key; |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 57 | struct plt_drv_data *drv_data = hid_get_drvdata(hdev); |
| 58 | unsigned long plt_type = drv_data->device_type; |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 59 | |
Terry Junge | 37e376d | 2018-04-30 13:32:46 -0700 | [diff] [blame] | 60 | /* special case for PTT products */ |
| 61 | if (field->application == HID_GD_JOYSTICK) |
| 62 | goto defaulted; |
| 63 | |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 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)) { |
| 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 Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 99 | /* |
| 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 Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 104 | |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 105 | ignored: |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 106 | return -1; |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 107 | |
| 108 | defaulted: |
| 109 | hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n", |
| 110 | usage->hid, field->application); |
| 111 | return 0; |
| 112 | |
| 113 | mapped: |
| 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 Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 120 | static 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 Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 144 | static 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 | |
| 164 | exit: |
| 165 | hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type); |
| 166 | return plt_type; |
| 167 | } |
| 168 | |
| 169 | static int plantronics_probe(struct hid_device *hdev, |
| 170 | const struct hid_device_id *id) |
| 171 | { |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 172 | struct plt_drv_data *drv_data; |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 173 | int ret; |
| 174 | |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 175 | drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL); |
| 176 | if (!drv_data) |
| 177 | return -ENOMEM; |
| 178 | |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 179 | ret = hid_parse(hdev); |
| 180 | if (ret) { |
| 181 | hid_err(hdev, "parse failed\n"); |
| 182 | goto err; |
| 183 | } |
| 184 | |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 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); |
| 188 | |
| 189 | hid_set_drvdata(hdev, drv_data); |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 190 | |
| 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 | |
| 196 | err: |
| 197 | return ret; |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 198 | } |
| 199 | |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 200 | static const struct hid_device_id plantronics_devices[] = { |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 201 | { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, |
| 202 | USB_DEVICE_ID_PLANTRONICS_BLACKWIRE_3220_SERIES), |
| 203 | .driver_data = PLT_QUIRK_DOUBLE_VOLUME_KEYS }, |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 204 | { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) }, |
| 205 | { } |
| 206 | }; |
| 207 | MODULE_DEVICE_TABLE(hid, plantronics_devices); |
| 208 | |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 209 | static 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 Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 215 | static struct hid_driver plantronics_driver = { |
| 216 | .name = "plantronics", |
| 217 | .id_table = plantronics_devices, |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 218 | .usage_table = plantronics_usages, |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 219 | .input_mapping = plantronics_input_mapping, |
Maxim Mikityanskiy | f567d6e | 2021-02-07 16:47:40 +0200 | [diff] [blame] | 220 | .event = plantronics_event, |
Terry Junge | 81bb773fa | 2015-06-08 14:27:57 -0700 | [diff] [blame] | 221 | .probe = plantronics_probe, |
JD Cole | 1a3f83f | 2014-10-31 17:34:42 -0700 | [diff] [blame] | 222 | }; |
| 223 | module_hid_driver(plantronics_driver); |
| 224 | |
| 225 | MODULE_AUTHOR("JD Cole <jd.cole@plantronics.com>"); |
| 226 | MODULE_AUTHOR("Terry Junge <terry.junge@plantronics.com>"); |
| 227 | MODULE_DESCRIPTION("Plantronics USB HID Driver"); |
| 228 | MODULE_LICENSE("GPL"); |