1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "hdf_hid_adapter.h"
10 #include <securec.h>
11 #include "event_hub.h"
12 #include "hdf_device_desc.h"
13 #include "hdf_log.h"
14 #include "osal_mem.h"
15 #include "osal_timer.h"
16
17 #ifdef CONFIG_DFX_ZEROHUNG
18 #include <dfx/hung_wp_screen.h>
19 #endif
20
21 #define TIMER_INTERVAL 500
22 #define REPEAT_VALUE 2
23 #define MEMCPY_CHECK_RETURN(ret) do { \
24 if ((ret) != 0) { \
25 HDF_LOGE("%s: memcpy failed, line %d", __func__, __LINE__); \
26 return; \
27 } \
28 } while (0)
29
30 InputDevice *cachedHid[MAX_INPUT_DEV_NUM];
31 HidInfo *g_cachedInfo[MAX_INPUT_DEV_NUM];
32
33 uint32_t g_kbdcode = 0;
34 OsalTimer g_timer;
35
InputDriverLoaded(void)36 static bool InputDriverLoaded(void)
37 {
38 InputManager* g_inputManager = GetInputManager();
39 if ((g_inputManager != NULL) && (g_inputManager->initialized != false)) {
40 return true;
41 }
42 return false;
43 }
44
HaveHidCache(void)45 static bool HaveHidCache(void)
46 {
47 if (cachedHid[0] == NULL) {
48 return false;
49 }
50 return true;
51 }
52
LoadCachedHid(void)53 static void LoadCachedHid(void)
54 {
55 int32_t i = 0;
56 int32_t ret;
57 if (!HaveHidCache()) {
58 HDF_LOGI("%s: exit", __func__);
59 return;
60 }
61 while (i < MAX_INPUT_DEV_NUM && cachedHid[i] != NULL) {
62 ret = RegisterInputDevice(cachedHid[i]);
63 if (ret != HDF_SUCCESS) {
64 HDF_LOGE("%s: add %s failed", __func__, cachedHid[i]->devName);
65 }
66 cachedHid[i] = NULL;
67 i++;
68 }
69 }
70
cachedPosId(void)71 static int cachedPosId(void)
72 {
73 int32_t id = 0;
74 while (id < MAX_INPUT_DEV_NUM) {
75 if (g_cachedInfo[id] == NULL) {
76 return id;
77 }
78 id++;
79 }
80 return HDF_FAILURE;
81 }
82
SendInfoToHdf(HidInfo * info)83 void SendInfoToHdf(HidInfo *info)
84 {
85 int ret;
86 int32_t id = cachedPosId();
87 if (id == HDF_FAILURE) {
88 HDF_LOGE("%s: cached hid info failed", __func__);
89 return;
90 }
91 g_cachedInfo[id] = (HidInfo *)OsalMemAlloc(sizeof(HidInfo));
92 if (g_cachedInfo[id] == NULL) {
93 HDF_LOGE("%s: malloc failed", __func__);
94 return;
95 }
96 ret = memcpy_s(g_cachedInfo[id], sizeof(HidInfo), info, sizeof(HidInfo));
97 if (ret != 0) {
98 HDF_LOGE("%s: memcpy failed", __func__);
99 OsalMemFree(g_cachedInfo[id]);
100 g_cachedInfo[id] = NULL;
101 return;
102 }
103 }
104
FreeCachedInfo(void)105 static void FreeCachedInfo(void)
106 {
107 int32_t id = 0;
108 while (id < MAX_INPUT_DEV_NUM) {
109 if (g_cachedInfo[id] != NULL) {
110 OsalMemFree(g_cachedInfo[id]);
111 g_cachedInfo[id] = NULL;
112 }
113 id++;
114 }
115 }
116
SetInputDevAbsAttr(InputDevice * inputDev,const HidInfo * info)117 static int32_t SetInputDevAbsAttr(InputDevice *inputDev, const HidInfo *info)
118 {
119 int32_t ret;
120 int i;
121 for (i = 0; i < BITS_TO_LONG(ABS_CNT); i++) {
122 if (inputDev->abilitySet.absCode[i] != 0) {
123 ret = memcpy_s(inputDev->attrSet.axisInfo, sizeof(AbsAttr) * ABS_CNT,
124 info->axisInfo, sizeof(AbsAttr) * ABS_CNT);
125 return ret;
126 }
127 }
128 return HDF_SUCCESS;
129 }
130
GetInfoFromCache(InputDevice * inputDev,const HidInfo * info)131 static void GetInfoFromCache(InputDevice *inputDev, const HidInfo *info)
132 {
133 uint32_t len;
134 int32_t ret;
135
136 len = sizeof(unsigned long);
137 ret = memcpy_s(inputDev->abilitySet.devProp, len * BITS_TO_LONG(INPUT_PROP_CNT),
138 info->devProp, len * BITS_TO_LONG(INPUT_PROP_CNT));
139 MEMCPY_CHECK_RETURN(ret);
140 ret = memcpy_s(inputDev->abilitySet.eventType, len * BITS_TO_LONG(EV_CNT),
141 info->eventType, len * BITS_TO_LONG(EV_CNT));
142 MEMCPY_CHECK_RETURN(ret);
143 ret = memcpy_s(inputDev->abilitySet.absCode, len * BITS_TO_LONG(ABS_CNT),
144 info->absCode, len * BITS_TO_LONG(ABS_CNT));
145 MEMCPY_CHECK_RETURN(ret);
146 ret = memcpy_s(inputDev->abilitySet.relCode, len * BITS_TO_LONG(REL_CNT),
147 info->relCode, len * BITS_TO_LONG(REL_CNT));
148 MEMCPY_CHECK_RETURN(ret);
149 ret = memcpy_s(inputDev->abilitySet.keyCode, len * BITS_TO_LONG(KEY_CNT),
150 info->keyCode, len * BITS_TO_LONG(KEY_CNT));
151 MEMCPY_CHECK_RETURN(ret);
152 ret = memcpy_s(inputDev->abilitySet.ledCode, len * BITS_TO_LONG(LED_CNT),
153 info->ledCode, len * BITS_TO_LONG(LED_CNT));
154 MEMCPY_CHECK_RETURN(ret);
155 ret = memcpy_s(inputDev->abilitySet.miscCode, len * BITS_TO_LONG(MSC_CNT),
156 info->miscCode, len * BITS_TO_LONG(MSC_CNT));
157 MEMCPY_CHECK_RETURN(ret);
158 ret = memcpy_s(inputDev->abilitySet.soundCode, len * BITS_TO_LONG(SND_CNT),
159 info->soundCode, len * BITS_TO_LONG(SND_CNT));
160 MEMCPY_CHECK_RETURN(ret);
161 ret = memcpy_s(inputDev->abilitySet.forceCode, len * BITS_TO_LONG(FF_CNT),
162 info->forceCode, len * BITS_TO_LONG(FF_CNT));
163 MEMCPY_CHECK_RETURN(ret);
164 ret = memcpy_s(inputDev->abilitySet.switchCode, len * BITS_TO_LONG(SW_CNT),
165 info->switchCode, len * BITS_TO_LONG(SW_CNT));
166 MEMCPY_CHECK_RETURN(ret);
167 ret = SetInputDevAbsAttr(inputDev, info);
168 MEMCPY_CHECK_RETURN(ret);
169 inputDev->attrSet.id.busType = info->bustype;
170 inputDev->attrSet.id.vendor = info->vendor;
171 inputDev->attrSet.id.product = info->product;
172 inputDev->attrSet.id.version = info->version;
173 }
174
SetInputDevAbility(InputDevice * inputDev)175 static void SetInputDevAbility(InputDevice *inputDev)
176 {
177 HidInfo *info = NULL;
178 int32_t id = 0;
179
180 while (id < MAX_INPUT_DEV_NUM) {
181 if (g_cachedInfo[id] != NULL && !strcmp(inputDev->devName, g_cachedInfo[id]->devName)) {
182 info = g_cachedInfo[id];
183 break;
184 }
185 id++;
186 }
187 if (id == MAX_INPUT_DEV_NUM || info == NULL) {
188 HDF_LOGE("%s: match cached info failed", __func__);
189 return;
190 }
191
192 GetInfoFromCache(inputDev, info);
193 FreeCachedInfo();
194 }
195
HidConstructInputDev(HidInfo * info)196 static InputDevice* HidConstructInputDev(HidInfo *info)
197 {
198 InputDevice *inputDev = (InputDevice *)OsalMemAlloc(sizeof(InputDevice));
199 if (inputDev == NULL) {
200 HDF_LOGE("%s: instance input device failed", __func__);
201 return NULL;
202 }
203 (void)memset_s(inputDev, sizeof(InputDevice), 0, sizeof(InputDevice));
204
205 inputDev->devType = info->devType;
206 inputDev->devName = info->devName;
207 SetInputDevAbility(inputDev);
208
209 return inputDev;
210 }
211
DoRegisterInputDev(InputDevice * inputDev)212 static void DoRegisterInputDev(InputDevice* inputDev)
213 {
214 int32_t ret;
215 ret = RegisterInputDevice(inputDev);
216 if (ret != HDF_SUCCESS) {
217 OsalMemFree(inputDev);
218 return;
219 }
220 }
221
CacheHid(InputDevice * inputDev)222 static void CacheHid(InputDevice* inputDev)
223 {
224 int32_t i = 0;
225 while ((i < MAX_INPUT_DEV_NUM) && (cachedHid[i] != NULL)) {
226 i++;
227 }
228 if (i < MAX_INPUT_DEV_NUM) {
229 cachedHid[i] = inputDev;
230 return;
231 } else {
232 HDF_LOGE("%s: cached hid device failed", __func__);
233 }
234 }
235
HidRegisterHdfInputDev(HidInfo * info)236 void* HidRegisterHdfInputDev(HidInfo *info)
237 {
238 InputDevice* inputDev = HidConstructInputDev(info);
239 if (inputDev == NULL) {
240 HDF_LOGE("%s: hid construct input Dev failed", __func__);
241 return NULL;
242 }
243
244 if (InputDriverLoaded()) {
245 DoRegisterInputDev(inputDev);
246 } else {
247 CacheHid(inputDev);
248 }
249 return inputDev;
250 }
251
HidUnregisterHdfInputDev(const void * inputDev)252 void HidUnregisterHdfInputDev(const void *inputDev)
253 {
254 if (inputDev == NULL) {
255 HDF_LOGE("%s: inputDev is null", __func__);
256 return;
257 }
258 UnregisterInputDevice((InputDevice *)inputDev);
259 }
260
TimerFunc(uintptr_t arg)261 static void TimerFunc(uintptr_t arg)
262 {
263 InputDevice *device = (InputDevice *)arg;
264 PushOnePackage(device, EV_KEY, g_kbdcode, REPEAT_VALUE);
265 PushOnePackage(device, 0, 0, SYN_CONFIG);
266 }
267
RepateEvent(const InputDevice * device)268 static void RepateEvent(const InputDevice *device)
269 {
270 int32_t ret;
271 static int32_t flag = 0;
272 if (flag == 1) {
273 (void)OsalTimerDelete(&g_timer);
274 }
275
276 ret = OsalTimerCreate(&g_timer, TIMER_INTERVAL, TimerFunc, (uintptr_t)device);
277 if (ret != HDF_SUCCESS) {
278 HDF_LOGE("%s: create timer failed, ret = %d", __func__, ret);
279 return;
280 }
281 ret = OsalTimerStartLoop(&g_timer);
282 if (ret != HDF_SUCCESS) {
283 HDF_LOGE("%s: start timer failed, ret = %d", __func__, ret);
284 return;
285 }
286 flag = 1;
287 }
288
HidReportEvent(const void * inputDev,uint32_t type,uint32_t code,int32_t value)289 void HidReportEvent(const void *inputDev, uint32_t type, uint32_t code, int32_t value)
290 {
291 bool loaded = InputDriverLoaded();
292 if (!loaded) {
293 HDF_LOGD("%s: device not loaded", __func__);
294 return;
295 }
296 #ifdef CONFIG_DFX_ZEROHUNG
297 if (type == EV_KEY && code == KEY_POWER)
298 hung_wp_screen_powerkey_ncb(value);
299 #endif
300 InputDevice *device = (InputDevice *)inputDev;
301 PushOnePackage(device, type, code, value);
302 if (type == EV_KEY && KEY_RESERVED < code && code < KEY_MAX && value == 0 && code == g_kbdcode) {
303 OsalTimerDelete(&g_timer);
304 g_kbdcode = 0;
305 }
306 if (type == EV_KEY && KEY_RESERVED < code && code < KEY_MAX && value == 1 &&
307 device->devType == INDEV_TYPE_KEYBOARD) {
308 g_kbdcode = code;
309 RepateEvent(device);
310 }
311 }
312
HdfHIDDriverInit(struct HdfDeviceObject * device)313 static int32_t HdfHIDDriverInit(struct HdfDeviceObject *device)
314 {
315 (void)device;
316 static bool cachedHidRegistered = false;
317 if (!cachedHidRegistered) {
318 cachedHidRegistered = true;
319 LoadCachedHid();
320 }
321 return HDF_SUCCESS;
322 }
323
HidGetDevType(InputDevice * inputDev,struct HdfSBuf * reply)324 static int32_t HidGetDevType(InputDevice *inputDev, struct HdfSBuf *reply)
325 {
326 uint32_t devType = inputDev->devType;
327 HDF_LOGI("%s: enter, devType is %u", __func__, devType);
328 bool ret = HdfSbufWriteUint32(reply, devType);
329 if (!ret) {
330 HDF_LOGE("%s: HdfSbufWriteUint32 failed", __func__);
331 return HDF_FAILURE;
332 }
333 return HDF_SUCCESS;
334 }
335
HidGetDeviceStrInfo(InputDevice * inputDev,int32_t cmd,struct HdfSBuf * reply)336 static int32_t HidGetDeviceStrInfo(InputDevice *inputDev, int32_t cmd, struct HdfSBuf *reply)
337 {
338 const char *info = NULL;
339 if (inputDev == NULL) {
340 HDF_LOGE("%s: parameter invalid", __func__);
341 return HDF_ERR_INVALID_PARAM;
342 }
343
344 switch (cmd) {
345 case GET_CHIP_NAME:
346 info = "null";
347 break;
348 case GET_VENDOR_NAME:
349 info = "null";
350 break;
351 case GET_CHIP_INFO:
352 info = "null";
353 break;
354 default:
355 info = NULL;
356 break;
357 }
358
359 bool ret = HdfSbufWriteString(reply, info);
360 if (!ret) {
361 HDF_LOGE("%s: HdfSbufWriteUint32 failed", __func__);
362 return HDF_FAILURE;
363 }
364 HDF_LOGI("%s: cmd is %d, the info is %s", __func__, cmd, info);
365 return HDF_SUCCESS;
366 }
367
HidGetDeviceAttr(InputDevice * inputDev,struct HdfSBuf * reply)368 static int32_t HidGetDeviceAttr(InputDevice *inputDev, struct HdfSBuf *reply)
369 {
370 int32_t ret;
371 if (inputDev == NULL) {
372 return HDF_FAILURE;
373 }
374
375 ret = strncpy_s(inputDev->attrSet.devName, DEV_NAME_LEN, inputDev->devName, strlen(inputDev->devName));
376 if (ret != 0) {
377 HDF_LOGE("%s: copy name from inputDev failed, ret = %d", __func__, ret);
378 return HDF_FAILURE;
379 }
380
381 if (!HdfSbufWriteBuffer(reply, &inputDev->attrSet, sizeof(DevAttr))) {
382 HDF_LOGE("%s: sbuf write dev attr failed", __func__);
383 return HDF_FAILURE;
384 }
385
386 return HDF_SUCCESS;
387 }
388
HidGetDeviceAbility(InputDevice * inputDev,struct HdfSBuf * reply)389 static int32_t HidGetDeviceAbility(InputDevice *inputDev, struct HdfSBuf *reply)
390 {
391 if (inputDev == NULL) {
392 return HDF_FAILURE;
393 }
394
395 if (!HdfSbufWriteBuffer(reply, &inputDev->abilitySet, sizeof(DevAbility))) {
396 HDF_LOGE("%s: sbuf write dev ability failed", __func__);
397 return HDF_FAILURE;
398 }
399
400 return HDF_SUCCESS;
401 }
402
HdfHIDDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)403 static int32_t HdfHIDDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
404 {
405 (void)cmd;
406 int32_t ret;
407 InputDevice *inputDev = NULL;
408 if (client == NULL || data == NULL || reply == NULL) {
409 HDF_LOGE("%s: param is null", __func__);
410 return HDF_FAILURE;
411 }
412
413 inputDev = (InputDevice *)client->device->priv;
414 if (inputDev == NULL) {
415 HDF_LOGE("%s: inputDev is null", __func__);
416 return HDF_FAILURE;
417 }
418
419 HDF_LOGI("%s: cmd = %d", __func__, cmd);
420 switch (cmd) {
421 case GET_DEV_TYPE:
422 ret = HidGetDevType(inputDev, reply);
423 break;
424 case GET_CHIP_NAME:
425 case GET_VENDOR_NAME:
426 case GET_CHIP_INFO:
427 ret = HidGetDeviceStrInfo(inputDev, cmd, reply);
428 break;
429 case GET_DEV_ATTR:
430 ret = HidGetDeviceAttr(inputDev, reply);
431 break;
432 case GET_DEV_ABILITY:
433 ret = HidGetDeviceAbility(inputDev, reply);
434 break;
435 default:
436 ret = HDF_SUCCESS;
437 HDF_LOGE("%s: cmd unknown, cmd = 0x%x", __func__, cmd);
438 break;
439 }
440 return ret;
441 }
442
HdfHIDDriverBind(struct HdfDeviceObject * device)443 static int32_t HdfHIDDriverBind(struct HdfDeviceObject *device)
444 {
445 if (device == NULL) {
446 return HDF_ERR_INVALID_PARAM;
447 }
448 static struct IDeviceIoService hidService = {
449 .Dispatch = HdfHIDDispatch,
450 };
451 device->service = &hidService;
452 return HDF_SUCCESS;
453 }
454
HdfHIDDriverRelease(struct HdfDeviceObject * device)455 static void HdfHIDDriverRelease(struct HdfDeviceObject *device)
456 {
457 FreeCachedInfo();
458 if (device == NULL) {
459 HDF_LOGE("%s: device is null", __func__);
460 return;
461 }
462 }
463
464 struct HdfDriverEntry g_hdfHIDEntry = {
465 .moduleVersion = 1,
466 .moduleName = "HDF_HID",
467 .Bind = HdfHIDDriverBind,
468 .Init = HdfHIDDriverInit,
469 .Release = HdfHIDDriverRelease,
470 };
471
472 HDF_INIT(g_hdfHIDEntry);
473