xref: /openbmc/linux/drivers/hid/hid-plantronics.c (revision 3a83e4e6)
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 
17 #define PLT_HID_1_0_PAGE	0xffa00000
18 #define PLT_HID_2_0_PAGE	0xffa20000
19 
20 #define PLT_BASIC_TELEPHONY	0x0003
21 #define PLT_BASIC_EXCEPTION	0x0005
22 
23 #define PLT_VOL_UP		0x00b1
24 #define PLT_VOL_DOWN		0x00b2
25 
26 #define PLT1_VOL_UP		(PLT_HID_1_0_PAGE | PLT_VOL_UP)
27 #define PLT1_VOL_DOWN		(PLT_HID_1_0_PAGE | PLT_VOL_DOWN)
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 
31 #define PLT_DA60		0xda60
32 #define PLT_BT300_MIN		0x0413
33 #define PLT_BT300_MAX		0x0418
34 
35 
36 #define PLT_ALLOW_CONSUMER (field->application == HID_CP_CONSUMERCONTROL && \
37 			    (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
38 
39 static int plantronics_input_mapping(struct hid_device *hdev,
40 				     struct hid_input *hi,
41 				     struct hid_field *field,
42 				     struct hid_usage *usage,
43 				     unsigned long **bit, int *max)
44 {
45 	unsigned short mapped_key;
46 	unsigned long plt_type = (unsigned long)hid_get_drvdata(hdev);
47 
48 	/* special case for PTT products */
49 	if (field->application == HID_GD_JOYSTICK)
50 		goto defaulted;
51 
52 	/* handle volume up/down mapping */
53 	/* non-standard types or multi-HID interfaces - plt_type is PID */
54 	if (!(plt_type & HID_USAGE_PAGE)) {
55 		switch (plt_type) {
56 		case PLT_DA60:
57 			if (PLT_ALLOW_CONSUMER)
58 				goto defaulted;
59 			goto ignored;
60 		default:
61 			if (PLT_ALLOW_CONSUMER)
62 				goto defaulted;
63 		}
64 	}
65 	/* handle standard types - plt_type is 0xffa0uuuu or 0xffa2uuuu */
66 	/* 'basic telephony compliant' - allow default consumer page map */
67 	else if ((plt_type & HID_USAGE) >= PLT_BASIC_TELEPHONY &&
68 		 (plt_type & HID_USAGE) != PLT_BASIC_EXCEPTION) {
69 		if (PLT_ALLOW_CONSUMER)
70 			goto defaulted;
71 	}
72 	/* not 'basic telephony' - apply legacy mapping */
73 	/* only map if the field is in the device's primary vendor page */
74 	else if (!((field->application ^ plt_type) & HID_USAGE_PAGE)) {
75 		switch (usage->hid) {
76 		case PLT1_VOL_UP:
77 		case PLT2_VOL_UP:
78 			mapped_key = KEY_VOLUMEUP;
79 			goto mapped;
80 		case PLT1_VOL_DOWN:
81 		case PLT2_VOL_DOWN:
82 			mapped_key = KEY_VOLUMEDOWN;
83 			goto mapped;
84 		}
85 	}
86 
87 /*
88  * Future mapping of call control or other usages,
89  * if and when keys are defined would go here
90  * otherwise, ignore everything else that was not mapped
91  */
92 
93 ignored:
94 	return -1;
95 
96 defaulted:
97 	hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n",
98 		usage->hid, field->application);
99 	return 0;
100 
101 mapped:
102 	hid_map_usage_clear(hi, usage, bit, max, EV_KEY, mapped_key);
103 	hid_dbg(hdev, "usage: %08x (appl: %08x) - mapped to key %d\n",
104 		usage->hid, field->application, mapped_key);
105 	return 1;
106 }
107 
108 static unsigned long plantronics_device_type(struct hid_device *hdev)
109 {
110 	unsigned i, col_page;
111 	unsigned long plt_type = hdev->product;
112 
113 	/* multi-HID interfaces? - plt_type is PID */
114 	if (plt_type >= PLT_BT300_MIN && plt_type <= PLT_BT300_MAX)
115 		goto exit;
116 
117 	/* determine primary vendor page */
118 	for (i = 0; i < hdev->maxcollection; i++) {
119 		col_page = hdev->collection[i].usage & HID_USAGE_PAGE;
120 		if (col_page == PLT_HID_2_0_PAGE) {
121 			plt_type = hdev->collection[i].usage;
122 			break;
123 		}
124 		if (col_page == PLT_HID_1_0_PAGE)
125 			plt_type = hdev->collection[i].usage;
126 	}
127 
128 exit:
129 	hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type);
130 	return plt_type;
131 }
132 
133 static int plantronics_probe(struct hid_device *hdev,
134 			     const struct hid_device_id *id)
135 {
136 	int ret;
137 
138 	ret = hid_parse(hdev);
139 	if (ret) {
140 		hid_err(hdev, "parse failed\n");
141 		goto err;
142 	}
143 
144 	hid_set_drvdata(hdev, (void *)plantronics_device_type(hdev));
145 
146 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT |
147 		HID_CONNECT_HIDINPUT_FORCE | HID_CONNECT_HIDDEV_FORCE);
148 	if (ret)
149 		hid_err(hdev, "hw start failed\n");
150 
151 err:
152 	return ret;
153 }
154 
155 static const struct hid_device_id plantronics_devices[] = {
156 	{ HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) },
157 	{ }
158 };
159 MODULE_DEVICE_TABLE(hid, plantronics_devices);
160 
161 static struct hid_driver plantronics_driver = {
162 	.name = "plantronics",
163 	.id_table = plantronics_devices,
164 	.input_mapping = plantronics_input_mapping,
165 	.probe = plantronics_probe,
166 };
167 module_hid_driver(plantronics_driver);
168 
169 MODULE_AUTHOR("JD Cole <jd.cole@plantronics.com>");
170 MODULE_AUTHOR("Terry Junge <terry.junge@plantronics.com>");
171 MODULE_DESCRIPTION("Plantronics USB HID Driver");
172 MODULE_LICENSE("GPL");
173