• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
HaveHidCache(void)36 static bool HaveHidCache(void)
37 {
38     if (cachedHid[0] == NULL) {
39         return false;
40     }
41     return true;
42 }
43 
LoadCachedHid(void)44 static void LoadCachedHid(void)
45 {
46     int32_t i = 0;
47     int32_t ret;
48     if (!HaveHidCache()) {
49         HDF_LOGI("%s: exit", __func__);
50         return;
51     }
52     while (i < MAX_INPUT_DEV_NUM && cachedHid[i] != NULL) {
53         ret = RegisterInputDevice(cachedHid[i]);
54         if (ret != HDF_SUCCESS) {
55             HDF_LOGE("%s: add %s failed", __func__, cachedHid[i]->devName);
56         }
57         cachedHid[i] = NULL;
58         i++;
59     }
60 }
61 
cachedPosId(void)62 static int cachedPosId(void)
63 {
64     int32_t id = 0;
65     while (id < MAX_INPUT_DEV_NUM) {
66         if (g_cachedInfo[id] == NULL) {
67             return id;
68         }
69         id++;
70     }
71     return HDF_FAILURE;
72 }
73 
SendInfoToHdf(HidInfo * info)74 void SendInfoToHdf(HidInfo *info)
75 {
76     int ret;
77     int32_t id = cachedPosId();
78     if (id == HDF_FAILURE) {
79         HDF_LOGE("%s: cached hid info failed", __func__);
80         return;
81     }
82     g_cachedInfo[id] = (HidInfo *)OsalMemAlloc(sizeof(HidInfo));
83     if (g_cachedInfo[id] == NULL) {
84         HDF_LOGE("%s: malloc failed", __func__);
85         return;
86     }
87     ret = memcpy_s(g_cachedInfo[id], sizeof(HidInfo), info, sizeof(HidInfo));
88     if (ret != 0) {
89         HDF_LOGE("%s: memcpy failed", __func__);
90         OsalMemFree(g_cachedInfo[id]);
91         g_cachedInfo[id] = NULL;
92         return;
93     }
94 }
95 
FreeCachedInfo(void)96 static void FreeCachedInfo(void)
97 {
98     int32_t id = 0;
99     while (id < MAX_INPUT_DEV_NUM) {
100         if (g_cachedInfo[id] != NULL) {
101             OsalMemFree(g_cachedInfo[id]);
102             g_cachedInfo[id] = NULL;
103         }
104         id++;
105     }
106 }
107 
SetInputDevAbsAttr(InputDevice * inputDev,const HidInfo * info)108 static int32_t SetInputDevAbsAttr(InputDevice *inputDev, const HidInfo *info)
109 {
110     int32_t ret;
111     int i;
112     for (i = 0; i < BITS_TO_LONG(ABS_CNT); i++) {
113         if (inputDev->abilitySet.absCode[i] != 0) {
114             ret = memcpy_s(inputDev->attrSet.axisInfo, sizeof(AbsAttr) * ABS_CNT,
115                            info->axisInfo, sizeof(AbsAttr) * ABS_CNT);
116             return ret;
117         }
118     }
119     return HDF_SUCCESS;
120 }
121 
GetInfoFromCache(InputDevice * inputDev,const HidInfo * info)122 static void GetInfoFromCache(InputDevice *inputDev, const HidInfo *info)
123 {
124     uint32_t len;
125     int32_t ret;
126 
127     len = sizeof(unsigned long);
128     ret = memcpy_s(inputDev->abilitySet.devProp, len * BITS_TO_LONG(INPUT_PROP_CNT),
129         info->devProp, len * BITS_TO_LONG(INPUT_PROP_CNT));
130     MEMCPY_CHECK_RETURN(ret);
131     ret = memcpy_s(inputDev->abilitySet.eventType, len * BITS_TO_LONG(EV_CNT),
132         info->eventType, len * BITS_TO_LONG(EV_CNT));
133     MEMCPY_CHECK_RETURN(ret);
134     ret = memcpy_s(inputDev->abilitySet.absCode, len * BITS_TO_LONG(ABS_CNT),
135         info->absCode, len * BITS_TO_LONG(ABS_CNT));
136     MEMCPY_CHECK_RETURN(ret);
137     ret = memcpy_s(inputDev->abilitySet.relCode, len * BITS_TO_LONG(REL_CNT),
138         info->relCode, len * BITS_TO_LONG(REL_CNT));
139     MEMCPY_CHECK_RETURN(ret);
140     ret = memcpy_s(inputDev->abilitySet.keyCode, len * BITS_TO_LONG(KEY_CNT),
141         info->keyCode, len * BITS_TO_LONG(KEY_CNT));
142     MEMCPY_CHECK_RETURN(ret);
143     ret = memcpy_s(inputDev->abilitySet.ledCode, len * BITS_TO_LONG(LED_CNT),
144         info->ledCode, len * BITS_TO_LONG(LED_CNT));
145     MEMCPY_CHECK_RETURN(ret);
146     ret = memcpy_s(inputDev->abilitySet.miscCode, len * BITS_TO_LONG(MSC_CNT),
147         info->miscCode, len * BITS_TO_LONG(MSC_CNT));
148     MEMCPY_CHECK_RETURN(ret);
149     ret = memcpy_s(inputDev->abilitySet.soundCode, len * BITS_TO_LONG(SND_CNT),
150         info->soundCode, len * BITS_TO_LONG(SND_CNT));
151     MEMCPY_CHECK_RETURN(ret);
152     ret = memcpy_s(inputDev->abilitySet.forceCode, len * BITS_TO_LONG(FF_CNT),
153         info->forceCode, len * BITS_TO_LONG(FF_CNT));
154     MEMCPY_CHECK_RETURN(ret);
155     ret = memcpy_s(inputDev->abilitySet.switchCode, len * BITS_TO_LONG(SW_CNT),
156         info->switchCode, len * BITS_TO_LONG(SW_CNT));
157     MEMCPY_CHECK_RETURN(ret);
158     ret = SetInputDevAbsAttr(inputDev, info);
159     MEMCPY_CHECK_RETURN(ret);
160     inputDev->attrSet.id.busType = info->bustype;
161     inputDev->attrSet.id.vendor = info->vendor;
162     inputDev->attrSet.id.product = info->product;
163     inputDev->attrSet.id.version = info->version;
164 }
165 
SetInputDevAbility(InputDevice * inputDev)166 static void SetInputDevAbility(InputDevice *inputDev)
167 {
168     HidInfo *info = NULL;
169     int32_t id = 0;
170 
171     while (id < MAX_INPUT_DEV_NUM) {
172         if (g_cachedInfo[id] != NULL && !strcmp(inputDev->devName, g_cachedInfo[id]->devName)) {
173             info = g_cachedInfo[id];
174             break;
175         }
176         id++;
177     }
178     if (id == MAX_INPUT_DEV_NUM || info == NULL) {
179         HDF_LOGE("%s: match cached info failed", __func__);
180         return;
181     }
182 
183     GetInfoFromCache(inputDev, info);
184     FreeCachedInfo();
185 }
186 
HidConstructInputDev(HidInfo * info)187 static InputDevice* HidConstructInputDev(HidInfo *info)
188 {
189     InputDevice *inputDev = (InputDevice *)OsalMemAlloc(sizeof(InputDevice));
190     if (inputDev == NULL) {
191         HDF_LOGE("%s: instance input device failed", __func__);
192         return NULL;
193     }
194     (void)memset_s(inputDev, sizeof(InputDevice), 0, sizeof(InputDevice));
195 
196     inputDev->devType = info->devType;
197     inputDev->devName = info->devName;
198     SetInputDevAbility(inputDev);
199 
200     return inputDev;
201 }
202 
DoRegisterInputDev(InputDevice * inputDev)203 static void DoRegisterInputDev(InputDevice* inputDev)
204 {
205     int32_t ret;
206     ret = RegisterInputDevice(inputDev);
207     if (ret != HDF_SUCCESS) {
208         OsalMemFree(inputDev);
209         return;
210     }
211 }
212 
CacheHid(InputDevice * inputDev)213 static void CacheHid(InputDevice* inputDev)
214 {
215     int32_t i = 0;
216     while ((i < MAX_INPUT_DEV_NUM) && (cachedHid[i] != NULL)) {
217         i++;
218     }
219     if (i < MAX_INPUT_DEV_NUM) {
220         cachedHid[i] = inputDev;
221         return;
222     } else {
223         HDF_LOGE("%s: cached hid device failed", __func__);
224     }
225 }
226 
InputDriverLoaded(void)227 static bool InputDriverLoaded(void)
228 {
229     InputManager* g_inputManager = GetInputManager();
230     if ((g_inputManager != NULL) && (g_inputManager->initialized != false)) {
231         return true;
232     }
233     return false;
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 #ifdef CONFIG_DFX_ZEROHUNG
292     if (type == EV_KEY && code == KEY_POWER)
293         hung_wp_screen_powerkey_ncb(value);
294 #endif
295     InputDevice *device = (InputDevice *)inputDev;
296     PushOnePackage(device, type, code, value);
297     if (type == EV_KEY && KEY_RESERVED < code && code < KEY_MAX && value == 0 && code == g_kbdcode) {
298         OsalTimerDelete(&g_timer);
299         g_kbdcode = 0;
300     }
301     if (type == EV_KEY && KEY_RESERVED < code && code < KEY_MAX && value == 1 &&
302         device->devType == INDEV_TYPE_KEYBOARD) {
303         g_kbdcode = code;
304         RepateEvent(device);
305     }
306 }
307 
HdfHIDDriverInit(struct HdfDeviceObject * device)308 static int32_t HdfHIDDriverInit(struct HdfDeviceObject *device)
309 {
310     (void)device;
311     static bool cachedHidRegistered = false;
312     if (!cachedHidRegistered) {
313         cachedHidRegistered = true;
314         LoadCachedHid();
315     }
316     return HDF_SUCCESS;
317 }
318 
HidGetDevType(InputDevice * inputDev,struct HdfSBuf * reply)319 static int32_t HidGetDevType(InputDevice *inputDev, struct HdfSBuf *reply)
320 {
321     uint32_t devType = inputDev->devType;
322     HDF_LOGI("%s: enter, devType is %u", __func__, devType);
323     bool ret = HdfSbufWriteUint32(reply, devType);
324     if (!ret) {
325         HDF_LOGE("%s: HdfSbufWriteUint32 failed", __func__);
326         return HDF_FAILURE;
327     }
328     return HDF_SUCCESS;
329 }
330 
HidGetDeviceStrInfo(InputDevice * inputDev,int32_t cmd,struct HdfSBuf * reply)331 static int32_t HidGetDeviceStrInfo(InputDevice *inputDev, int32_t cmd, struct HdfSBuf *reply)
332 {
333     const char *info = NULL;
334     if (inputDev == NULL) {
335         HDF_LOGE("%s: parameter invalid", __func__);
336         return HDF_ERR_INVALID_PARAM;
337     }
338 
339     switch (cmd) {
340         case GET_CHIP_NAME:
341             info = "null";
342             break;
343         case GET_VENDOR_NAME:
344             info = "null";
345             break;
346         case GET_CHIP_INFO:
347             info = "null";
348             break;
349         default:
350             info = NULL;
351             break;
352     }
353 
354     bool ret = HdfSbufWriteString(reply, info);
355     if (!ret) {
356         HDF_LOGE("%s: HdfSbufWriteUint32 failed", __func__);
357         return HDF_FAILURE;
358     }
359     HDF_LOGI("%s: cmd is %d, the info is %s", __func__, cmd, info);
360     return HDF_SUCCESS;
361 }
362 
HidGetDeviceAttr(InputDevice * inputDev,struct HdfSBuf * reply)363 static int32_t HidGetDeviceAttr(InputDevice *inputDev, struct HdfSBuf *reply)
364 {
365     int32_t ret;
366     if (inputDev == NULL) {
367         return HDF_FAILURE;
368     }
369 
370     ret = strncpy_s(inputDev->attrSet.devName, DEV_NAME_LEN, inputDev->devName, strlen(inputDev->devName));
371     if (ret != 0) {
372         HDF_LOGE("%s: copy name from inputDev failed, ret = %d", __func__, ret);
373         return HDF_FAILURE;
374     }
375 
376     if (!HdfSbufWriteBuffer(reply, &inputDev->attrSet, sizeof(DevAttr))) {
377         HDF_LOGE("%s: sbuf write dev attr failed", __func__);
378         return HDF_FAILURE;
379     }
380 
381     return HDF_SUCCESS;
382 }
383 
HidGetDeviceAbility(InputDevice * inputDev,struct HdfSBuf * reply)384 static int32_t HidGetDeviceAbility(InputDevice *inputDev, struct HdfSBuf *reply)
385 {
386     if (inputDev == NULL) {
387         return HDF_FAILURE;
388     }
389 
390     if (!HdfSbufWriteBuffer(reply, &inputDev->abilitySet, sizeof(DevAbility))) {
391         HDF_LOGE("%s: sbuf write dev ability failed", __func__);
392         return HDF_FAILURE;
393     }
394 
395     return HDF_SUCCESS;
396 }
397 
HdfHIDDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)398 static int32_t HdfHIDDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
399 {
400     (void)cmd;
401     int32_t ret;
402     InputDevice *inputDev = NULL;
403     if (client == NULL || data == NULL || reply == NULL) {
404         HDF_LOGE("%s: param is null", __func__);
405         return HDF_FAILURE;
406     }
407 
408     inputDev = (InputDevice *)client->device->priv;
409     if (inputDev == NULL) {
410         HDF_LOGE("%s: inputDev is null", __func__);
411         return HDF_FAILURE;
412     }
413 
414     HDF_LOGI("%s: cmd = %d", __func__, cmd);
415     switch (cmd) {
416         case GET_DEV_TYPE:
417             ret = HidGetDevType(inputDev, reply);
418             break;
419         case GET_CHIP_NAME:
420         case GET_VENDOR_NAME:
421         case GET_CHIP_INFO:
422             ret = HidGetDeviceStrInfo(inputDev, cmd, reply);
423             break;
424         case GET_DEV_ATTR:
425             ret = HidGetDeviceAttr(inputDev, reply);
426             break;
427         case GET_DEV_ABILITY:
428             ret = HidGetDeviceAbility(inputDev, reply);
429             break;
430         default:
431             ret = HDF_SUCCESS;
432             HDF_LOGE("%s: cmd unknown, cmd = 0x%x", __func__, cmd);
433             break;
434     }
435     return ret;
436 }
437 
HdfHIDDriverBind(struct HdfDeviceObject * device)438 static int32_t HdfHIDDriverBind(struct HdfDeviceObject *device)
439 {
440     if (device == NULL) {
441         return HDF_ERR_INVALID_PARAM;
442     }
443     static struct IDeviceIoService hidService = {
444         .Dispatch = HdfHIDDispatch,
445     };
446     device->service = &hidService;
447     return HDF_SUCCESS;
448 }
449 
HdfHIDDriverRelease(struct HdfDeviceObject * device)450 static void HdfHIDDriverRelease(struct HdfDeviceObject *device)
451 {
452     FreeCachedInfo();
453     if (device == NULL) {
454         HDF_LOGE("%s: device is null", __func__);
455         return;
456     }
457 }
458 
459 struct HdfDriverEntry g_hdfHIDEntry = {
460     .moduleVersion = 1,
461     .moduleName = "HDF_HID",
462     .Bind = HdfHIDDriverBind,
463     .Init = HdfHIDDriverInit,
464     .Release = HdfHIDDriverRelease,
465 };
466 
467 HDF_INIT(g_hdfHIDEntry);
468