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