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 #ifndef HDF_INPUT_DEVICE_MANAGER_H
10 #define HDF_INPUT_DEVICE_MANAGER_H
11 
12 #include "input-event-codes.h"
13 #include "osal_mutex.h"
14 #include "osal_spinlock.h"
15 #include "hdf_types.h"
16 #include "hdf_device_desc.h"
17 #include "hdf_workqueue.h"
18 
19 #ifdef HDF_LOG_TAG
20 #undef HDF_LOG_TAG
21 #endif
22 #define HDF_LOG_TAG HDF_INPUT_DRV
23 #define INPUT_DEV_PATH_LEN 64
24 #define MAX_INPUT_DEV_NUM  32
25 #define DEV_NAME_LEN 64
26 #define ONLINE    0
27 #define OFFLINE   1
28 
29 #ifdef DIV_ROUND_UP
30 #undef DIV_ROUND_UP
31 #endif
32 #define DIV_ROUND_UP(nr, d) (((nr) + (d) - 1) / (d))
33 
34 #define BYTE_HAS_BITS 8
35 #define BITS_TO_UINT64(count)    DIV_ROUND_UP(count, BYTE_HAS_BITS * sizeof(unsigned long))
36 
37 #ifndef BITS_PER_LONG
38 #define BITS_PER_LONG 32
39 #endif
40 #define SET_BIT(nr)    (1UL << ((nr) % BITS_PER_LONG))
41 
42 #define FF_MAX    0x7f
43 #define FF_CNT    (FF_MAX + 1)
44 
45 #define CHECK_RETURN_VALUE(ret) do { \
46     if ((ret) != HDF_SUCCESS) { \
47         return ret; \
48     } \
49 } while (0)
50 
51 typedef struct {
52     uint32_t devId;
53     uint32_t devType;
54 } DevDesc;
55 
56 typedef struct {
57     uint32_t devId;
58     uint32_t devType;
59     uint32_t status;
60 } HotPlugEvent;
61 
62 typedef struct {
63     struct IDeviceIoService ioService;
64     uint32_t (*getDeviceCount)(void);
65 } IInputManagerService;
66 
67 typedef struct {
68     unsigned long devProp[BITS_TO_UINT64(INPUT_PROP_CNT)];
69     unsigned long eventType[BITS_TO_UINT64(EV_CNT)];
70     unsigned long absCode[BITS_TO_UINT64(ABS_CNT)];
71     unsigned long relCode[BITS_TO_UINT64(REL_CNT)];
72     unsigned long keyCode[BITS_TO_UINT64(KEY_CNT)];
73     unsigned long ledCode[BITS_TO_UINT64(LED_CNT)];
74     unsigned long miscCode[BITS_TO_UINT64(MSC_CNT)];
75     unsigned long soundCode[BITS_TO_UINT64(SND_CNT)];
76     unsigned long forceCode[BITS_TO_UINT64(FF_CNT)];
77     unsigned long switchCode[BITS_TO_UINT64(SW_CNT)];
78     unsigned long keyType[BITS_TO_UINT64(KEY_CNT)];
79     unsigned long ledType[BITS_TO_UINT64(LED_CNT)];
80     unsigned long soundType[BITS_TO_UINT64(SND_CNT)];
81     unsigned long switchType[BITS_TO_UINT64(SW_CNT)];
82 } DevAbility;
83 
84 typedef struct {
85     int32_t axis;
86     int32_t min;
87     int32_t max;
88     int32_t fuzz;
89     int32_t flat;
90     int32_t range;
91 } DimensionInfo;
92 
93 typedef struct {
94     uint16_t busType;
95     uint16_t vendor;
96     uint16_t product;
97     uint16_t version;
98 } InputDevIdentify;
99 
100 typedef struct {
101     char devName[DEV_NAME_LEN];
102     InputDevIdentify id;
103     DimensionInfo axisInfo[ABS_CNT];
104 } DevAttr;
105 
106 typedef struct InputDeviceInfo {
107     struct HdfDeviceObject *hdfDevObj;
108 #ifndef __LITEOS_M__
109     HdfWorkQueue eventWorkQueue;
110     HdfWork eventWork;
111 #endif // __LITEOS_M__
112     uint32_t devId;
113     uint32_t devType;
114     const char *devName;
115     uint16_t pkgNum;
116     uint16_t pkgCount;
117     bool errFrameFlag;
118     struct HdfSBuf *pkgBuf;
119     struct HdfSBuf *eventBuf;
120     void *pvtData;
121     DevAttr attrSet;
122     DevAbility abilitySet;
123     struct InputDeviceInfo *next;
124 } InputDevice;
125 
126 typedef struct {
127     struct HdfDeviceObject *hdfDevObj;
128     uint32_t devCount;
129     struct OsalMutex mutex;
130     OsalSpinlock lock;
131     bool initialized;
132     InputDevice *inputDevList;
133 } InputManager;
134 
135 enum InputDevType {
136     INDEV_TYPE_TOUCH,               /* Touchscreen */
137     INDEV_TYPE_KEY,                 /* Physical key */
138     INDEV_TYPE_BUTTON,              /* Virtual button */
139     INDEV_TYPE_CROWN,               /* Watch crown */
140     INDEV_TYPE_HID_BEGIN_POS = 33,  /* HID type start position */
141     INDEV_TYPE_ENCODER,             /* Customized type of a specific function or event */
142     INDEV_TYPE_MOUSE,               /* Mouse */
143     INDEV_TYPE_KEYBOARD,            /* Keyboard */
144     INDEV_TYPE_ROCKER,              /* ROCKER */
145     INDEV_TYPE_TRACKBALL,           /* TRACKBALL */
146     INDEV_TYPE_UNKNOWN,             /* Unknown input device type */
147 };
148 
149 enum InputIOsvcCmdId {
150     GET_DEV_TYPE,
151     SET_PWR_STATUS,
152     GET_PWR_STATUS,
153     GET_CHIP_INFO,
154     GET_VENDOR_NAME,
155     GET_CHIP_NAME,
156     GET_DEV_ATTR,
157     GET_DEV_ABILITY,
158     SET_GESTURE_MODE,
159     RUN_CAPAC_TEST,
160     RUN_EXTRA_CMD,
161 };
162 
163 enum TouchIoctlCmd {
164     INPUT_IOCTL_GET_EVENT_DATA,
165     INPUT_IOCTL_SET_POWER_STATUS,
166     INPUT_IOCTL_GET_POWER_STATUS,
167     INPUT_IOCTL_GET_DEVICE_TYPE,
168     INPUT_IOCTL_GET_CHIP_INFO,
169     INPUT_IOCTL_GET_VENDOR_NAME,
170     INPUT_IOCTL_GET_CHIP_NAME,
171     INPUT_IOCTL_SET_GESTURE_MODE,
172     INPUT_IOCTL_RUN_CAPACITANCE_TEST,
173     INPUT_IOCTL_RUN_EXTRA_CMD,
174 };
175 InputManager* GetInputManager(void);
176 int32_t RegisterInputDevice(InputDevice *inputDev);
177 void UnregisterInputDevice(InputDevice *inputDev);
178 
179 #endif