treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / hid / hid-plantronics.c
blob85b685efc12f3819704b5ba88d99c4893f1463fb
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Plantronics USB HID Driver
5 * Copyright (c) 2014 JD Cole <jd.cole@plantronics.com>
6 * Copyright (c) 2015-2018 Terry Junge <terry.junge@plantronics.com>
7 */
9 /*
12 #include "hid-ids.h"
14 #include <linux/hid.h>
15 #include <linux/module.h>
17 #define PLT_HID_1_0_PAGE 0xffa00000
18 #define PLT_HID_2_0_PAGE 0xffa20000
20 #define PLT_BASIC_TELEPHONY 0x0003
21 #define PLT_BASIC_EXCEPTION 0x0005
23 #define PLT_VOL_UP 0x00b1
24 #define PLT_VOL_DOWN 0x00b2
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)
31 #define PLT_DA60 0xda60
32 #define PLT_BT300_MIN 0x0413
33 #define PLT_BT300_MAX 0x0418
36 #define PLT_ALLOW_CONSUMER (field->application == HID_CP_CONSUMERCONTROL && \
37 (usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER)
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)
45 unsigned short mapped_key;
46 unsigned long plt_type = (unsigned long)hid_get_drvdata(hdev);
48 /* special case for PTT products */
49 if (field->application == HID_GD_JOYSTICK)
50 goto defaulted;
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;
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;
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;
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
93 ignored:
94 return -1;
96 defaulted:
97 hid_dbg(hdev, "usage: %08x (appl: %08x) - defaulted\n",
98 usage->hid, field->application);
99 return 0;
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;
108 static unsigned long plantronics_device_type(struct hid_device *hdev)
110 unsigned i, col_page;
111 unsigned long plt_type = hdev->product;
113 /* multi-HID interfaces? - plt_type is PID */
114 if (plt_type >= PLT_BT300_MIN && plt_type <= PLT_BT300_MAX)
115 goto exit;
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;
124 if (col_page == PLT_HID_1_0_PAGE)
125 plt_type = hdev->collection[i].usage;
128 exit:
129 hid_dbg(hdev, "plt_type decoded as: %08lx\n", plt_type);
130 return plt_type;
133 static int plantronics_probe(struct hid_device *hdev,
134 const struct hid_device_id *id)
136 int ret;
138 ret = hid_parse(hdev);
139 if (ret) {
140 hid_err(hdev, "parse failed\n");
141 goto err;
144 hid_set_drvdata(hdev, (void *)plantronics_device_type(hdev));
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");
151 err:
152 return ret;
155 static const struct hid_device_id plantronics_devices[] = {
156 { HID_USB_DEVICE(USB_VENDOR_ID_PLANTRONICS, HID_ANY_ID) },
159 MODULE_DEVICE_TABLE(hid, plantronics_devices);
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,
167 module_hid_driver(plantronics_driver);
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");