1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Plantronics USB HID Driver 4 * 5 * Copyright (c) 2014 JD Cole <jd.cole@plantronics.com> 6 * Copyright (c) 2015-2018 Terry Junge <terry.junge@plantronics.com> 7 */ 8 9 /* 10 */ 11 12 #include "hid-ids.h" 13 14 #include <linux/hid.h> 15 #include <linux/module.h> 16 #include <linux/jiffies.h> 17 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 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 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 { 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; 59 60 /* special case for PTT products */ 61 if (field->application == HID_GD_JOYSTICK) 62 goto defaulted; 63 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 } 97 } 98 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 */ 104 105 ignored: 106 return -1; 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 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 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 { 172 struct plt_drv_data *drv_data; 173 int ret; 174 175 drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL); 176 if (!drv_data) 177 return -ENOMEM; 178 179 ret = hid_parse(hdev); 180 if (ret) { 181 hid_err(hdev, "parse failed\n"); 182 goto err; 183 } 184 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); 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; 198 } 199 200 static const struct hid_device_id plantronics_devices[] = { 201 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, 202 USB_DEVICE_ID_PLANTRONICS_BLACKWIRE_3220_SERIES), 203 .driver_data = PLT_QUIRK_DOUBLE_VOLUME_KEYS }, 204 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) }, 205 { } 206 }; 207 MODULE_DEVICE_TABLE(hid, plantronics_devices); 208 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 215 static struct hid_driver plantronics_driver = { 216 .name = "plantronics", 217 .id_table = plantronics_devices, 218 .usage_table = plantronics_usages, 219 .input_mapping = plantronics_input_mapping, 220 .event = plantronics_event, 221 .probe = plantronics_probe, 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"); 229