• 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_device_desc.h"
10 #include "osal_mem.h"
11 #include "gpio_if.h"
12 #include "hdf_log.h"
13 #include "hdf_input_device_manager.h"
14 #include "event_hub.h"
15 #include "hdf_key.h"
16 
17 #define CHECK_PARSER_RET(ret, str) do { \
18     if ((ret) != HDF_SUCCESS) { \
19         HDF_LOGE("%s: %s failed, ret = %d!", __func__, str, ret); \
20         return HDF_FAILURE; \
21     } \
22 } while (0)
23 
KeyIrqHandle(uint16_t intGpioNum,void * data)24 int32_t KeyIrqHandle(uint16_t intGpioNum, void *data)
25 {
26     uint16_t gpioValue = 0;
27     KeyDriver *driver = (KeyDriver *)data;
28     if (driver == NULL) {
29         return HDF_FAILURE;
30     }
31     KeyEventData *event = &driver->eventData;
32     if (event == NULL) {
33         return HDF_FAILURE;
34     }
35     int32_t ret = GpioDisableIrq(intGpioNum);
36 
37     if (ret != HDF_SUCCESS) {
38         HDF_LOGE("%s: disable irq failed, ret %d", __func__, ret);
39     }
40 
41     ret = GpioRead(intGpioNum, &gpioValue);
42     if (ret != HDF_SUCCESS) {
43         HDF_LOGE("%s: gpio read failed, ret %d", __func__, ret);
44         return HDF_FAILURE;
45     }
46     uint64_t curTime = OsalGetSysTimeMs();
47     driver->preStatus = gpioValue;
48     driver->timeStamp = curTime;
49 
50     if (gpioValue == GPIO_VAL_LOW) {
51         event->definedEvent = INPUT_KEY_DOWN;
52         input_report_key(driver->inputdev, KEY_POWER, 1);
53     } else if (gpioValue == GPIO_VAL_HIGH) {
54         event->definedEvent = INPUT_KEY_UP;
55         input_report_key(driver->inputdev, KEY_POWER, 0);
56     }
57     input_sync(driver->inputdev);
58 
59     GpioEnableIrq(intGpioNum);
60     return HDF_SUCCESS;
61 }
62 
SetupKeyIrq(KeyDriver * keyDrv)63 static int32_t SetupKeyIrq(KeyDriver *keyDrv)
64 {
65     uint16_t intGpioNum = keyDrv->keyCfg->gpioNum;
66     uint16_t irqFlag = keyDrv->keyCfg->irqFlag;
67     int32_t ret = GpioSetDir(intGpioNum, GPIO_DIR_IN);
68 
69     if (ret != HDF_SUCCESS) {
70         HDF_LOGE("%s: gpio set dir failed, ret %d", __func__, ret);
71         return ret;
72     }
73     ret = GpioSetIrq(intGpioNum, irqFlag | GPIO_IRQ_USING_THREAD, KeyIrqHandle, keyDrv);
74     if (ret != HDF_SUCCESS) {
75         HDF_LOGE("%s: register irq failed, ret %d", __func__, ret);
76         return ret;
77     }
78     ret = GpioEnableIrq(intGpioNum);
79     if (ret != HDF_SUCCESS) {
80         HDF_LOGE("%s: enable irq failed, ret %d", __func__, ret);
81         return HDF_FAILURE;
82     }
83     return HDF_SUCCESS;
84 }
85 
KeyInit(KeyDriver * keyDrv)86 static int32_t KeyInit(KeyDriver *keyDrv)
87 {
88     int32_t ret = SetupKeyIrq(keyDrv);
89     CHECK_RETURN_VALUE(ret);
90     return HDF_SUCCESS;
91 }
92 
KeyConfigInstance(struct HdfDeviceObject * device)93 static KeyChipCfg *KeyConfigInstance(struct HdfDeviceObject *device)
94 {
95     KeyChipCfg *keyCfg = (KeyChipCfg *)OsalMemAlloc(sizeof(KeyChipCfg));
96     if (keyCfg == NULL) {
97         HDF_LOGE("%s: malloc key config failed", __func__);
98         return NULL;
99     }
100     (void)memset_s(keyCfg, sizeof(KeyChipCfg), 0, sizeof(KeyChipCfg));
101     keyCfg->hdfKeyDev = device;
102 
103     if (ParseKeyConfig(device->property, keyCfg) != HDF_SUCCESS) {
104         HDF_LOGE("%s: parse key config failed", __func__);
105         OsalMemFree(keyCfg);
106         keyCfg = NULL;
107     }
108     return keyCfg;
109 }
110 
KeyDriverInstance(KeyChipCfg * keyCfg)111 static KeyDriver *KeyDriverInstance(KeyChipCfg *keyCfg)
112 {
113     KeyDriver *keyDrv = (KeyDriver *)OsalMemAlloc(sizeof(KeyDriver));
114     if (keyDrv == NULL) {
115         HDF_LOGE("%s: malloc key driver failed", __func__);
116         return NULL;
117     }
118     (void)memset_s(keyDrv, sizeof(KeyDriver), 0, sizeof(KeyDriver));
119 
120     keyDrv->devType = keyCfg->devType;
121     keyDrv->keyCfg = keyCfg;
122 
123     return keyDrv;
124 }
125 
InputDeviceInstance(KeyDriver * keyDrv)126 static InputDevice *InputDeviceInstance(KeyDriver *keyDrv)
127 {
128     InputDevice *inputDev = (InputDevice *)OsalMemAlloc(sizeof(InputDevice));
129     if (inputDev == NULL) {
130         HDF_LOGE("%s: malloc input device failed", __func__);
131         return NULL;
132     }
133     (void)memset_s(inputDev, sizeof(InputDevice), 0, sizeof(InputDevice));
134 
135     inputDev->pvtData = (void *)keyDrv;
136     inputDev->devType = keyDrv->devType;
137     inputDev->devName = keyDrv->keyCfg->keyName;
138     inputDev->hdfDevObj = keyDrv->keyCfg->hdfKeyDev;
139     keyDrv->inputdev = inputDev;
140 
141     return inputDev;
142 }
143 
RegisterKeyDevice(KeyChipCfg * keyCfg)144 static int32_t RegisterKeyDevice(KeyChipCfg *keyCfg)
145 {
146     KeyDriver *keyDrv = KeyDriverInstance(keyCfg);
147     if (keyDrv == NULL) {
148         HDF_LOGE("%s: instance key config failed", __func__);
149         return HDF_ERR_MALLOC_FAIL;
150     }
151 
152     int32_t ret = KeyInit(keyDrv);
153     if (ret != HDF_SUCCESS) {
154         HDF_LOGE("%s: key driver init failed, ret %d", __func__, ret);
155         goto EXIT;
156     }
157 
158     InputDevice *inputDev = InputDeviceInstance(keyDrv);
159     if (inputDev == NULL) {
160         HDF_LOGE("%s: instance input device failed", __func__);
161         goto EXIT;
162     }
163 
164     ret = RegisterInputDevice(inputDev);
165     if (ret != HDF_SUCCESS) {
166         goto EXIT1;
167     }
168     return HDF_SUCCESS;
169 
170 EXIT1:
171     OsalMemFree(inputDev->pkgBuf);
172     OsalMemFree(inputDev);
173 EXIT:
174     OsalMemFree(keyDrv);
175     HDF_LOGE("%s: exit failed", __func__);
176     return HDF_FAILURE;
177 }
178 
HdfKeyDriverInit(struct HdfDeviceObject * device)179 static int32_t HdfKeyDriverInit(struct HdfDeviceObject *device)
180 {
181     HDF_LOGI("%s: enter", __func__);
182     if (device == NULL) {
183         return HDF_ERR_INVALID_PARAM;
184     }
185 
186     KeyChipCfg *keyCfg = KeyConfigInstance(device);
187     if (keyCfg == NULL) {
188         HDF_LOGE("%s: instance key config failed", __func__);
189         return HDF_ERR_MALLOC_FAIL;
190     }
191 
192     int32_t ret = RegisterKeyDevice(keyCfg);
193     if (ret != HDF_SUCCESS) {
194         goto EXIT;
195     }
196     HDF_LOGI("%s: exit succ!", __func__);
197     return HDF_SUCCESS;
198 
199 EXIT:
200     OsalMemFree(keyCfg);
201     return HDF_FAILURE;
202 }
203 
HdfKeyDispatch(struct HdfDeviceIoClient * client,int cmd,struct HdfSBuf * data,struct HdfSBuf * reply)204 static int32_t HdfKeyDispatch(struct HdfDeviceIoClient *client, int cmd, struct HdfSBuf *data, struct HdfSBuf *reply)
205 {
206     (void)cmd;
207     if (client == NULL || data == NULL || reply == NULL) {
208         HDF_LOGE("%s: param is null", __func__);
209         return HDF_FAILURE;
210     }
211     return HDF_SUCCESS;
212 }
213 
HdfKeyDriverBind(struct HdfDeviceObject * device)214 static int32_t HdfKeyDriverBind(struct HdfDeviceObject *device)
215 {
216     if (device == NULL) {
217         return HDF_ERR_INVALID_PARAM;
218     }
219     static struct IDeviceIoService keyService = {
220         .object.objectId = 1,
221         .Dispatch = HdfKeyDispatch,
222     };
223     device->service = &keyService;
224     return HDF_SUCCESS;
225 }
226 
227 struct HdfDriverEntry g_hdfKeyEntry = {
228     .moduleVersion = 1,
229     .moduleName = "HDF_KEY",
230     .Bind = HdfKeyDriverBind,
231     .Init = HdfKeyDriverInit,
232 };
233 
234 HDF_INIT(g_hdfKeyEntry);
235