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