xref: /linux/drivers/hid/hid-gfrm.c (revision bba2c3615bd6cfee7456d1130f2e6b01b3f4e9ba)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * HID driver for Google Fiber TV Box remote controls
4  *
5  * Copyright (c) 2014-2015 Google Inc.
6  *
7  * Author: Petri Gynther <pgynther@google.com>
8  */
9 #include <linux/device.h>
10 #include <linux/hid.h>
11 #include <linux/input.h>
12 #include <linux/module.h>
13 
14 #include "hid-ids.h"
15 
16 #define GFRM100  1  /* Google Fiber GFRM100 (Bluetooth classic) */
17 #define GFRM200  2  /* Google Fiber GFRM200 (Bluetooth LE) */
18 
19 #define GFRM100_SEARCH_KEY_REPORT_ID   0xF7
20 #define GFRM100_SEARCH_KEY_DOWN        0x0
21 #define GFRM100_SEARCH_KEY_AUDIO_DATA  0x1
22 #define GFRM100_SEARCH_KEY_UP          0x2
23 
24 static u8 search_key_dn[3] = {0x40, 0x21, 0x02};
25 static u8 search_key_up[3] = {0x40, 0x00, 0x00};
26 
27 static int gfrm_input_mapping(struct hid_device *hdev, struct hid_input *hi,
28 		struct hid_field *field, struct hid_usage *usage,
29 		unsigned long **bit, int *max)
30 {
31 	const struct hid_device_id *id = hid_get_drvdata(hdev);
32 	unsigned long hdev_type = id->driver_data;
33 
34 	if (hdev_type == GFRM100) {
35 		if (usage->hid == (HID_UP_CONSUMER | 0x4)) {
36 			/* Consumer.0004 -> KEY_INFO */
37 			hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_INFO);
38 			return 1;
39 		}
40 
41 		if (usage->hid == (HID_UP_CONSUMER | 0x41)) {
42 			/* Consumer.0041 -> KEY_OK */
43 			hid_map_usage_clear(hi, usage, bit, max, EV_KEY, KEY_OK);
44 			return 1;
45 		}
46 	}
47 
48 	return 0;
49 }
50 
51 static int gfrm_raw_event(struct hid_device *hdev, struct hid_report *report,
52 		u8 *data, int size)
53 {
54 	const struct hid_device_id *id = hid_get_drvdata(hdev);
55 	unsigned long hdev_type = id->driver_data;
56 	int ret = 0;
57 
58 	if (hdev_type != GFRM100)
59 		return 0;
60 
61 	if (size < 2 || data[0] != GFRM100_SEARCH_KEY_REPORT_ID)
62 		return 0;
63 
64 	/*
65 	 * Convert GFRM100 Search key reports into Consumer.0221 (Key.Search)
66 	 * reports. Ignore audio data.
67 	 */
68 	switch (data[1]) {
69 	case GFRM100_SEARCH_KEY_DOWN:
70 		ret = hid_report_raw_event(hdev, HID_INPUT_REPORT, search_key_dn,
71 					   sizeof(search_key_dn), sizeof(search_key_dn), 1);
72 		break;
73 
74 	case GFRM100_SEARCH_KEY_AUDIO_DATA:
75 		break;
76 
77 	case GFRM100_SEARCH_KEY_UP:
78 		ret = hid_report_raw_event(hdev, HID_INPUT_REPORT, search_key_up,
79 					   sizeof(search_key_up), sizeof(search_key_up), 1);
80 		break;
81 
82 	default:
83 		break;
84 	}
85 
86 	return (ret < 0) ? ret : -1;
87 }
88 
89 static int gfrm_input_configured(struct hid_device *hid, struct hid_input *hidinput)
90 {
91 	/*
92 	 * Enable software autorepeat with:
93 	 * - repeat delay: 400 msec
94 	 * - repeat period: 100 msec
95 	 */
96 	input_enable_softrepeat(hidinput->input, 400, 100);
97 	return 0;
98 }
99 
100 static int gfrm_probe(struct hid_device *hdev, const struct hid_device_id *id)
101 {
102 	int ret;
103 
104 	hid_set_drvdata(hdev, (void *)id);
105 
106 	ret = hid_parse(hdev);
107 	if (ret)
108 		goto done;
109 
110 	if (id->driver_data == GFRM100) {
111 		/*
112 		 * GFRM100 HID Report Descriptor does not describe the Search
113 		 * key reports. Thus, we need to add it manually here, so that
114 		 * those reports reach gfrm_raw_event() from hid_input_report().
115 		 */
116 		if (!hid_register_report(hdev, HID_INPUT_REPORT,
117 					 GFRM100_SEARCH_KEY_REPORT_ID, 0)) {
118 			ret = -ENOMEM;
119 			goto done;
120 		}
121 	}
122 
123 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
124 done:
125 	return ret;
126 }
127 
128 static const struct hid_device_id gfrm_devices[] = {
129 	{ HID_BLUETOOTH_DEVICE(0x58, 0x2000),
130 		.driver_data = GFRM100 },
131 	{ HID_BLUETOOTH_DEVICE(0x471, 0x2210),
132 		.driver_data = GFRM200 },
133 	{ }
134 };
135 MODULE_DEVICE_TABLE(hid, gfrm_devices);
136 
137 static struct hid_driver gfrm_driver = {
138 	.name = "gfrm",
139 	.id_table = gfrm_devices,
140 	.probe = gfrm_probe,
141 	.input_mapping = gfrm_input_mapping,
142 	.raw_event = gfrm_raw_event,
143 	.input_configured = gfrm_input_configured,
144 };
145 
146 module_hid_driver(gfrm_driver);
147 
148 MODULE_AUTHOR("Petri Gynther <pgynther@google.com>");
149 MODULE_DESCRIPTION("Google Fiber TV Box remote control driver");
150 MODULE_LICENSE("GPL");
151