1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <functional>
18 #include <stdint.h>
19 #include <sys/types.h>
20 #include <unordered_map>
21 #include <vector>
22
23 #define LOG_TAG "InputDriver"
24
25 #define LOG_NDEBUG 0
26
27 #include "InputDriver.h"
28 #include "InputHost.h"
29
30 #include <hardware/input.h>
31 #include <input/InputDevice.h>
32 #include <input/PropertyMap.h>
33 #include <utils/Log.h>
34 #include <utils/String8.h>
35
36 #define INDENT2 " "
37
38 struct input_property_map {
39 std::unique_ptr<android::PropertyMap> propertyMap;
40 };
41
42 struct input_property {
43 android::String8 key;
44 android::String8 value;
45 };
46
47 struct input_device_identifier {
48 const char* name;
49 const char* uniqueId;
50 input_bus_t bus;
51 int32_t vendorId;
52 int32_t productId;
53 int32_t version;
54 };
55
56 struct input_device_definition {
57 std::vector<input_report_definition*> reportDefs;
58 };
59
60 struct input_device_handle {
61 input_device_identifier_t* id;
62 input_device_definition_t* def;
63 };
64
65 struct input_int_usage {
66 input_usage_t usage;
67 int32_t min;
68 int32_t max;
69 float resolution;
70 };
71
72 struct input_collection {
73 int32_t arity;
74 std::vector<input_int_usage> intUsages;
75 std::vector<input_usage_t> boolUsages;
76 };
77
78 struct InputCollectionIdHasher {
operator ()InputCollectionIdHasher79 std::size_t operator()(const input_collection_id& id) const {
80 return std::hash<int>()(static_cast<int>(id));
81 }
82 };
83
84 struct input_report_definition {
85 std::unordered_map<input_collection_id_t, input_collection, InputCollectionIdHasher> collections;
86 };
87
88
89 namespace android {
90
91 static input_host_callbacks_t kCallbacks = {
92 .create_device_identifier = create_device_identifier,
93 .create_device_definition = create_device_definition,
94 .create_input_report_definition = create_input_report_definition,
95 .create_output_report_definition = create_output_report_definition,
96 .free_report_definition = free_report_definition,
97 .input_device_definition_add_report = input_device_definition_add_report,
98 .input_report_definition_add_collection = input_report_definition_add_collection,
99 .input_report_definition_declare_usage_int = input_report_definition_declare_usage_int,
100 .input_report_definition_declare_usages_bool = input_report_definition_declare_usages_bool,
101 .register_device = register_device,
102 .input_allocate_report = input_allocate_report,
103 .input_report_set_usage_int = input_report_set_usage_int,
104 .input_report_set_usage_bool = input_report_set_usage_bool,
105 .report_event = report_event,
106 .input_get_device_property_map = input_get_device_property_map,
107 .input_get_device_property = input_get_device_property,
108 .input_get_property_key = input_get_property_key,
109 .input_get_property_value = input_get_property_value,
110 .input_free_device_property = input_free_device_property,
111 .input_free_device_property_map = input_free_device_property_map,
112 };
113
InputDriver(const char * name)114 InputDriver::InputDriver(const char* name) : mName(String8(name)) {
115 const hw_module_t* module;
116 int err = input_open(&module, name);
117 LOG_ALWAYS_FATAL_IF(err != 0, "Input module %s not found", name);
118 mHal = reinterpret_cast<const input_module_t*>(module);
119 }
120
init()121 void InputDriver::init() {
122 mHal->init(mHal, static_cast<input_host_t*>(this), kCallbacks);
123 }
124
createDeviceIdentifier(const char * name,int32_t productId,int32_t vendorId,input_bus_t bus,const char * uniqueId)125 input_device_identifier_t* InputDriver::createDeviceIdentifier(
126 const char* name, int32_t productId, int32_t vendorId,
127 input_bus_t bus, const char* uniqueId) {
128 auto identifier = new ::input_device_identifier {
129 .name = name,
130 .uniqueId = uniqueId,
131 .bus = bus,
132 .vendorId = vendorId,
133 .productId = productId,
134 };
135 // TODO: store this identifier somewhere
136 return identifier;
137 }
138
createDeviceDefinition()139 input_device_definition_t* InputDriver::createDeviceDefinition() {
140 return new ::input_device_definition;
141 }
142
createInputReportDefinition()143 input_report_definition_t* InputDriver::createInputReportDefinition() {
144 return new ::input_report_definition;
145 }
146
createOutputReportDefinition()147 input_report_definition_t* InputDriver::createOutputReportDefinition() {
148 return new ::input_report_definition;
149 }
150
freeReportDefinition(input_report_definition_t * reportDef)151 void InputDriver::freeReportDefinition(input_report_definition_t* reportDef) {
152 delete reportDef;
153 }
154
inputDeviceDefinitionAddReport(input_device_definition_t * d,input_report_definition_t * r)155 void InputDriver::inputDeviceDefinitionAddReport(input_device_definition_t* d,
156 input_report_definition_t* r) {
157 d->reportDefs.push_back(r);
158 }
159
inputReportDefinitionAddCollection(input_report_definition_t * report,input_collection_id_t id,int32_t arity)160 void InputDriver::inputReportDefinitionAddCollection(input_report_definition_t* report,
161 input_collection_id_t id, int32_t arity) {
162 report->collections[id] = {.arity = arity};
163 }
164
inputReportDefinitionDeclareUsageInt(input_report_definition_t * report,input_collection_id_t id,input_usage_t usage,int32_t min,int32_t max,float resolution)165 void InputDriver::inputReportDefinitionDeclareUsageInt(input_report_definition_t* report,
166 input_collection_id_t id, input_usage_t usage, int32_t min, int32_t max,
167 float resolution) {
168 if (report->collections.find(id) != report->collections.end()) {
169 report->collections[id].intUsages.push_back({
170 .usage = usage, .min = min, .max = max, .resolution = resolution});
171 }
172 }
173
inputReportDefinitionDeclareUsagesBool(input_report_definition_t * report,input_collection_id_t id,input_usage_t * usage,size_t usageCount)174 void InputDriver::inputReportDefinitionDeclareUsagesBool(input_report_definition_t* report,
175 input_collection_id_t id, input_usage_t* usage, size_t usageCount) {
176 if (report->collections.find(id) != report->collections.end()) {
177 for (size_t i = 0; i < usageCount; ++i) {
178 report->collections[id].boolUsages.push_back(usage[i]);
179 }
180 }
181 }
182
registerDevice(input_device_identifier_t * id,input_device_definition_t * d)183 input_device_handle_t* InputDriver::registerDevice(input_device_identifier_t* id,
184 input_device_definition_t* d) {
185 ALOGD("Registering device %s with %zu input reports", id->name, d->reportDefs.size());
186 // TODO: save this device handle
187 return new input_device_handle{ .id = id, .def = d };
188 }
189
unregisterDevice(input_device_handle_t * handle)190 void InputDriver::unregisterDevice(input_device_handle_t* handle) {
191 delete handle;
192 }
193
inputAllocateReport(input_report_definition_t * r)194 input_report_t* InputDriver::inputAllocateReport(input_report_definition_t* r) {
195 ALOGD("Allocating input report for definition %p", r);
196 return nullptr;
197 }
198
inputReportSetUsageInt(input_report_t * r,input_collection_id_t id,input_usage_t usage,int32_t value,int32_t arity_index)199 void InputDriver::inputReportSetUsageInt(input_report_t* r, input_collection_id_t id,
200 input_usage_t usage, int32_t value, int32_t arity_index) {
201 }
202
inputReportSetUsageBool(input_report_t * r,input_collection_id_t id,input_usage_t usage,bool value,int32_t arity_index)203 void InputDriver::inputReportSetUsageBool(input_report_t* r, input_collection_id_t id,
204 input_usage_t usage, bool value, int32_t arity_index) {
205 }
206
reportEvent(input_device_handle_t * d,input_report_t * report)207 void InputDriver::reportEvent(input_device_handle_t* d, input_report_t* report) {
208 ALOGD("report_event %p for handle %p", report, d);
209 }
210
inputGetDevicePropertyMap(input_device_identifier_t * id)211 input_property_map_t* InputDriver::inputGetDevicePropertyMap(input_device_identifier_t* id) {
212 InputDeviceIdentifier idi;
213 idi.name = id->name;
214 idi.uniqueId = id->uniqueId;
215 idi.bus = id->bus;
216 idi.vendor = id->vendorId;
217 idi.product = id->productId;
218 idi.version = id->version;
219
220 std::string configFile =
221 getInputDeviceConfigurationFilePathByDeviceIdentifier(idi,
222 InputDeviceConfigurationFileType::
223 CONFIGURATION);
224 if (configFile.empty()) {
225 ALOGD("No input device configuration file found for device '%s'.",
226 idi.name.c_str());
227 } else {
228 std::unique_ptr<input_property_map_t> propMap = std::make_unique<input_property_map_t>();
229 android::base::Result<std::unique_ptr<PropertyMap>> result =
230 PropertyMap::load(configFile.c_str());
231 if (!result.ok()) {
232 ALOGE("Error loading input device configuration file for device '%s'. "
233 "Using default configuration.",
234 idi.name.c_str());
235 return nullptr;
236 }
237 propMap->propertyMap = std::move(*result);
238 return propMap.release();
239 }
240 return nullptr;
241 }
242
inputGetDeviceProperty(input_property_map_t * map,const char * key)243 input_property_t* InputDriver::inputGetDeviceProperty(input_property_map_t* map,
244 const char* key) {
245 String8 keyString(key);
246 if (map != nullptr) {
247 if (map->propertyMap->hasProperty(keyString)) {
248 auto prop = new input_property_t();
249 if (!map->propertyMap->tryGetProperty(keyString, prop->value)) {
250 delete prop;
251 return nullptr;
252 }
253 prop->key = keyString;
254 return prop;
255 }
256 }
257 return nullptr;
258 }
259
inputGetPropertyKey(input_property_t * property)260 const char* InputDriver::inputGetPropertyKey(input_property_t* property) {
261 if (property != nullptr) {
262 return property->key.string();
263 }
264 return nullptr;
265 }
266
inputGetPropertyValue(input_property_t * property)267 const char* InputDriver::inputGetPropertyValue(input_property_t* property) {
268 if (property != nullptr) {
269 return property->value.string();
270 }
271 return nullptr;
272 }
273
inputFreeDeviceProperty(input_property_t * property)274 void InputDriver::inputFreeDeviceProperty(input_property_t* property) {
275 if (property != nullptr) {
276 delete property;
277 }
278 }
279
inputFreeDevicePropertyMap(input_property_map_t * map)280 void InputDriver::inputFreeDevicePropertyMap(input_property_map_t* map) {
281 if (map != nullptr) {
282 delete map;
283 }
284 }
285
dump(String8 & result)286 void InputDriver::dump(String8& result) {
287 result.appendFormat(INDENT2 "HAL Input Driver (%s)\n", mName.string());
288 }
289
290 } // namespace android
291
292 // HAL wrapper functions
293
294 namespace android {
295
create_device_identifier(input_host_t * host,const char * name,int32_t product_id,int32_t vendor_id,input_bus_t bus,const char * unique_id)296 ::input_device_identifier_t* create_device_identifier(input_host_t* host,
297 const char* name, int32_t product_id, int32_t vendor_id,
298 input_bus_t bus, const char* unique_id) {
299 auto driver = static_cast<InputDriverInterface*>(host);
300 return driver->createDeviceIdentifier(name, product_id, vendor_id, bus, unique_id);
301 }
302
create_device_definition(input_host_t * host)303 input_device_definition_t* create_device_definition(input_host_t* host) {
304 auto driver = static_cast<InputDriverInterface*>(host);
305 return driver->createDeviceDefinition();
306 }
307
create_input_report_definition(input_host_t * host)308 input_report_definition_t* create_input_report_definition(input_host_t* host) {
309 auto driver = static_cast<InputDriverInterface*>(host);
310 return driver->createInputReportDefinition();
311 }
312
create_output_report_definition(input_host_t * host)313 input_report_definition_t* create_output_report_definition(input_host_t* host) {
314 auto driver = static_cast<InputDriverInterface*>(host);
315 return driver->createOutputReportDefinition();
316 }
317
free_report_definition(input_host_t * host,input_report_definition_t * report_def)318 void free_report_definition(input_host_t* host, input_report_definition_t* report_def) {
319 auto driver = static_cast<InputDriverInterface*>(host);
320 driver->freeReportDefinition(report_def);
321 }
322
input_device_definition_add_report(input_host_t * host,input_device_definition_t * d,input_report_definition_t * r)323 void input_device_definition_add_report(input_host_t* host,
324 input_device_definition_t* d, input_report_definition_t* r) {
325 auto driver = static_cast<InputDriverInterface*>(host);
326 driver->inputDeviceDefinitionAddReport(d, r);
327 }
328
input_report_definition_add_collection(input_host_t * host,input_report_definition_t * report,input_collection_id_t id,int32_t arity)329 void input_report_definition_add_collection(input_host_t* host,
330 input_report_definition_t* report, input_collection_id_t id, int32_t arity) {
331 auto driver = static_cast<InputDriverInterface*>(host);
332 driver->inputReportDefinitionAddCollection(report, id, arity);
333 }
334
input_report_definition_declare_usage_int(input_host_t * host,input_report_definition_t * report,input_collection_id_t id,input_usage_t usage,int32_t min,int32_t max,float resolution)335 void input_report_definition_declare_usage_int(input_host_t* host,
336 input_report_definition_t* report, input_collection_id_t id,
337 input_usage_t usage, int32_t min, int32_t max, float resolution) {
338 auto driver = static_cast<InputDriverInterface*>(host);
339 driver->inputReportDefinitionDeclareUsageInt(report, id, usage, min, max, resolution);
340 }
341
input_report_definition_declare_usages_bool(input_host_t * host,input_report_definition_t * report,input_collection_id_t id,input_usage_t * usage,size_t usage_count)342 void input_report_definition_declare_usages_bool(input_host_t* host,
343 input_report_definition_t* report, input_collection_id_t id,
344 input_usage_t* usage, size_t usage_count) {
345 auto driver = static_cast<InputDriverInterface*>(host);
346 driver->inputReportDefinitionDeclareUsagesBool(report, id, usage, usage_count);
347 }
348
register_device(input_host_t * host,input_device_identifier_t * id,input_device_definition_t * d)349 input_device_handle_t* register_device(input_host_t* host,
350 input_device_identifier_t* id, input_device_definition_t* d) {
351 auto driver = static_cast<InputDriverInterface*>(host);
352 return driver->registerDevice(id, d);
353 }
354
unregister_device(input_host_t * host,input_device_handle_t * handle)355 void unregister_device(input_host_t* host, input_device_handle_t* handle) {
356 auto driver = static_cast<InputDriverInterface*>(host);
357 driver->unregisterDevice(handle);
358 }
359
input_allocate_report(input_host_t * host,input_report_definition_t * r)360 input_report_t* input_allocate_report(input_host_t* host, input_report_definition_t* r) {
361 auto driver = static_cast<InputDriverInterface*>(host);
362 return driver->inputAllocateReport(r);
363 }
364
input_report_set_usage_int(input_host_t * host,input_report_t * r,input_collection_id_t id,input_usage_t usage,int32_t value,int32_t arity_index)365 void input_report_set_usage_int(input_host_t* host, input_report_t* r,
366 input_collection_id_t id, input_usage_t usage, int32_t value, int32_t arity_index) {
367 auto driver = static_cast<InputDriverInterface*>(host);
368 driver->inputReportSetUsageInt(r, id, usage, value, arity_index);
369 }
370
input_report_set_usage_bool(input_host_t * host,input_report_t * r,input_collection_id_t id,input_usage_t usage,bool value,int32_t arity_index)371 void input_report_set_usage_bool(input_host_t* host, input_report_t* r,
372 input_collection_id_t id, input_usage_t usage, bool value, int32_t arity_index) {
373 auto driver = static_cast<InputDriverInterface*>(host);
374 driver->inputReportSetUsageBool(r, id, usage, value, arity_index);
375 }
376
report_event(input_host_t * host,input_device_handle_t * d,input_report_t * report)377 void report_event(input_host_t* host, input_device_handle_t* d, input_report_t* report) {
378 auto driver = static_cast<InputDriverInterface*>(host);
379 driver->reportEvent(d, report);
380 }
381
input_get_device_property_map(input_host_t * host,input_device_identifier_t * id)382 input_property_map_t* input_get_device_property_map(input_host_t* host,
383 input_device_identifier_t* id) {
384 auto driver = static_cast<InputDriverInterface*>(host);
385 return driver->inputGetDevicePropertyMap(id);
386 }
387
input_get_device_property(input_host_t * host,input_property_map_t * map,const char * key)388 input_property_t* input_get_device_property(input_host_t* host, input_property_map_t* map,
389 const char* key) {
390 auto driver = static_cast<InputDriverInterface*>(host);
391 return driver->inputGetDeviceProperty(map, key);
392 }
393
input_get_property_key(input_host_t * host,input_property_t * property)394 const char* input_get_property_key(input_host_t* host, input_property_t* property) {
395 auto driver = static_cast<InputDriverInterface*>(host);
396 return driver->inputGetPropertyKey(property);
397 }
398
input_get_property_value(input_host_t * host,input_property_t * property)399 const char* input_get_property_value(input_host_t* host, input_property_t* property) {
400 auto driver = static_cast<InputDriverInterface*>(host);
401 return driver->inputGetPropertyValue(property);
402 }
403
input_free_device_property(input_host_t * host,input_property_t * property)404 void input_free_device_property(input_host_t* host, input_property_t* property) {
405 auto driver = static_cast<InputDriverInterface*>(host);
406 driver->inputFreeDeviceProperty(property);
407 }
408
input_free_device_property_map(input_host_t * host,input_property_map_t * map)409 void input_free_device_property_map(input_host_t* host, input_property_map_t* map) {
410 auto driver = static_cast<InputDriverInterface*>(host);
411 driver->inputFreeDevicePropertyMap(map);
412 }
413
414 } // namespace android
415