xref: /openbmc/linux/drivers/hid/hid-plantronics.c (revision c94132bed52c6e775d75503b9f9a4fa14e6cf894)
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 #include "hid-ids.h"
10 
11 #include <linux/hid.h>
12 #include <linux/module.h>
13 #include <linux/jiffies.h>
14 
15 #define PLT_HID_1_0_PAGE	0xffa00000
16 #define PLT_HID_2_0_PAGE	0xffa20000
17 
18 #define PLT_BASIC_TELEPHONY	0x0003
19 #define PLT_BASIC_EXCEPTION	0x0005
20 
21 #define PLT_VOL_UP		0x00b1
22 #define PLT_VOL_DOWN		0x00b2
23 #define PLT_MIC_MUTE		0x00b5
24 
25 #define PLT1_VOL_UP		(PLT_HID_1_0_PAGE | PLT_VOL_UP)
26 #define PLT1_VOL_DOWN		(PLT_HID_1_0_PAGE | PLT_VOL_DOWN)
27 #define PLT1_MIC_MUTE		(PLT_HID_1_0_PAGE | PLT_MIC_MUTE)
28 #define PLT2_VOL_UP		(PLT_HID_2_0_PAGE | PLT_VOL_UP)
29 #define PLT2_VOL_DOWN		(PLT_HID_2_0_PAGE | PLT_VOL_DOWN)
30 #define PLT2_MIC_MUTE		(PLT_HID_2_0_PAGE | PLT_MIC_MUTE)
31 #define HID_TELEPHONY_MUTE	(HID_UP_TELEPHONY | 0x2f)
32 #define HID_CONSUMER_MUTE	(HID_UP_CONSUMER | 0xe2)
33 
34 #define PLT_DA60		0xda60
35 #define PLT_BT300_MIN		0x0413
36 #define PLT_BT300_MAX		0x0418
37 
38 #define PLT_DOUBLE_KEY_TIMEOUT 5 /* ms */
39 
40 struct plt_drv_data {
41 	unsigned long device_type;
42 	unsigned long last_key_ts;
43 	unsigned long double_key_to;
44 	__u16 last_key;
45 };
46 
plantronics_input_mapping(struct hid_device * hdev,struct hid_input * hi,struct hid_field * field,struct hid_usage * usage,unsigned long ** bit,int * max)47 static int plantronics_input_mapping(struct hid_device *hdev,
48 				     struct hid_input *hi,
49 				     struct hid_field *field,
50 				     struct hid_usage *usage,
51 				     unsigned long **bit, int *max)
52 {
53 	unsigned short mapped_key;
54 	struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
55 	unsigned long plt_type = drv_data->device_type;
56 	int allow_mute = usage->hid == HID_TELEPHONY_MUTE;
57 	int allow_consumer = field->application == HID_CP_CONSUMERCONTROL &&
58 			(usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER &&
59 			usage->hid != HID_CONSUMER_MUTE;
60 
61 	/* special case for PTT products */
62 	if (field->application == HID_GD_JOYSTICK)
63 		goto defaulted;
64 
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 (allow_consumer)
70 				goto defaulted;
71 			if (usage->hid == HID_CONSUMER_MUTE) {
72 				mapped_key = KEY_MICMUTE;
73 				goto mapped;
74 			}
75 			break;
76 		default:
77 			if (allow_consumer || allow_mute)
78 				goto defaulted;
79 		}
80 		goto ignored;
81 	}
82 
83 	/* handle standard consumer control mapping */
84 	/* and standard telephony mic mute mapping */
85 	if (allow_consumer || allow_mute)
86 		goto defaulted;
87 
88 	/* handle vendor unique types - plt_type is 0xffa0uuuu or 0xffa2uuuu */
89 	/* if not 'basic telephony compliant' - map vendor unique controls */
90 	if (!((plt_type & HID_USAGE) >= PLT_BASIC_TELEPHONY &&
91 	      (plt_type & HID_USAGE) != PLT_BASIC_EXCEPTION) &&
92 	      !((field->application ^ plt_type) & HID_USAGE_PAGE))
93 		switch (usage->hid) {
94 		case PLT1_VOL_UP:
95 		case PLT2_VOL_UP:
96 			mapped_key = KEY_VOLUMEUP;
97 			goto mapped;
98 		case PLT1_VOL_DOWN:
99 		case PLT2_VOL_DOWN:
100 			mapped_key = KEY_VOLUMEDOWN;
101 			goto mapped;
102 		case PLT1_MIC_MUTE:
103 		case PLT2_MIC_MUTE:
104 			mapped_key = KEY_MICMUTE;
105 			goto mapped;
106 		}
107 
108 /*
109  * Future mapping of call control or other usages,
110  * if and when keys are defined would go here
111  * otherwise, ignore everything else that was not mapped
112  */
113 
114 ignored:
115 	hid_dbg(hdev, "usage: %08x (appl: %08x) - ignored\n",
116 		usage->hid, field->application);
117 	return -1;
118 
119 defaulted:
120 	hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n",
121 		usage->hid, field->application);
122 	return 0;
123 
124 mapped:
125 	hid_map_usage_clear(hi, usage, bit, max, EV_KEY, mapped_key);
126 	hid_dbg(hdev, "usage: %08x (appl: %08x) - mapped to key %d\n",
127 		usage->hid, field->application, mapped_key);
128 	return 1;
129 }
130 
plantronics_event(struct hid_device * hdev,struct hid_field * field,struct hid_usage * usage,__s32 value)131 static int plantronics_event(struct hid_device *hdev, struct hid_field *field,
132 			     struct hid_usage *usage, __s32 value)
133 {
134 	struct plt_drv_data *drv_data = hid_get_drvdata(hdev);
135 	unsigned long prev_tsto, cur_ts;
136 	__u16 prev_key, cur_key;
137 
138 	/* Usages are filtered in plantronics_usages. */
139 
140 	/* HZ too low for ms resolution - double key detection disabled */
141 	/* or it is a key release - handle key presses only. */
142 	if (!drv_data->double_key_to || !value)
143 		return 0;
144 
145 	prev_tsto = drv_data->last_key_ts + drv_data->double_key_to;
146 	cur_ts = drv_data->last_key_ts = jiffies;
147 	prev_key = drv_data->last_key;
148 	cur_key = drv_data->last_key = usage->code;
149 
150 	/* If the same key occurs in <= double_key_to -- ignore it */
151 	if (prev_key == cur_key && time_before_eq(cur_ts, prev_tsto)) {
152 		hid_dbg(hdev, "double key %d ignored\n", cur_key);
153 		return 1; /* Ignore the repeated key. */
154 	}
155 	return 0;
156 }
157 
plantronics_device_type(struct hid_device * hdev)158 static unsigned long plantronics_device_type(struct hid_device *hdev)
159 {
160 	unsigned i, col_page;
161 	unsigned long plt_type = hdev->product;
162 
163 	/* multi-HID interfaces? - plt_type is PID */
164 	if (plt_type >= PLT_BT300_MIN && plt_type <= PLT_BT300_MAX)
165 		goto exit;
166 
167 	/* determine primary vendor page */
168 	for (i = 0; i < hdev->maxcollection; i++) {
169 		col_page = hdev->collection[i].usage & HID_USAGE_PAGE;
170 		if (col_page == PLT_HID_2_0_PAGE) {
171 			plt_type = hdev->collection[i].usage;
172 			break;
173 		}
174 		if (col_page == PLT_HID_1_0_PAGE)
175 			plt_type = hdev->collection[i].usage;
176 	}
177 
178 exit:
179 	hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type);
180 	return plt_type;
181 }
182 
plantronics_probe(struct hid_device * hdev,const struct hid_device_id * id)183 static int plantronics_probe(struct hid_device *hdev,
184 			     const struct hid_device_id *id)
185 {
186 	struct plt_drv_data *drv_data;
187 	int ret;
188 
189 	drv_data = devm_kzalloc(&hdev->dev, sizeof(*drv_data), GFP_KERNEL);
190 	if (!drv_data)
191 		return -ENOMEM;
192 
193 	ret = hid_parse(hdev);
194 	if (ret) {
195 		hid_err(hdev, "parse failed\n");
196 		return ret;
197 	}
198 
199 	drv_data->device_type = plantronics_device_type(hdev);
200 	drv_data->double_key_to = msecs_to_jiffies(PLT_DOUBLE_KEY_TIMEOUT);
201 	drv_data->last_key_ts = jiffies - drv_data->double_key_to;
202 
203 	/* if HZ does not allow ms resolution - disable double key detection */
204 	if (drv_data->double_key_to < PLT_DOUBLE_KEY_TIMEOUT)
205 		drv_data->double_key_to = 0;
206 
207 	hid_set_drvdata(hdev, drv_data);
208 
209 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
210 		HID_CONNECT_HIDINPUT_FORCE | HID_CONNECT_HIDDEV_FORCE);
211 	if (ret)
212 		hid_err(hdev, "hw start failed\n");
213 
214 	return ret;
215 }
216 
217 static const struct hid_device_id plantronics_devices[] = {
218 	{ HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) },
219 	{ }
220 };
221 MODULE_DEVICE_TABLE(hid, plantronics_devices);
222 
223 static const struct hid_usage_id plantronics_usages[] = {
224 	{ HID_CP_VOLUMEUP, EV_KEY, HID_ANY_ID },
225 	{ HID_CP_VOLUMEDOWN, EV_KEY, HID_ANY_ID },
226 	{ HID_TELEPHONY_MUTE, EV_KEY, HID_ANY_ID },
227 	{ HID_CONSUMER_MUTE, EV_KEY, HID_ANY_ID },
228 	{ PLT2_VOL_UP, EV_KEY, HID_ANY_ID },
229 	{ PLT2_VOL_DOWN, EV_KEY, HID_ANY_ID },
230 	{ PLT2_MIC_MUTE, EV_KEY, HID_ANY_ID },
231 	{ PLT1_VOL_UP, EV_KEY, HID_ANY_ID },
232 	{ PLT1_VOL_DOWN, EV_KEY, HID_ANY_ID },
233 	{ PLT1_MIC_MUTE, EV_KEY, HID_ANY_ID },
234 	{ HID_TERMINATOR, HID_TERMINATOR, HID_TERMINATOR }
235 };
236 
237 static struct hid_driver plantronics_driver = {
238 	.name = "plantronics",
239 	.id_table = plantronics_devices,
240 	.usage_table = plantronics_usages,
241 	.input_mapping = plantronics_input_mapping,
242 	.event = plantronics_event,
243 	.probe = plantronics_probe,
244 };
245 module_hid_driver(plantronics_driver);
246 
247 MODULE_AUTHOR("JD Cole <jd.cole@plantronics.com>");
248 MODULE_AUTHOR("Terry Junge <terry.junge@plantronics.com>");
249 MODULE_DESCRIPTION("Plantronics USB HID Driver");
250 MODULE_LICENSE("GPL");
251