1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-3-08 GuEe-GUI the first version
*/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <poll.h>
#include <errno.h>
#include <drivers/clock_time.h>
#include <dfs_file.h>
#define DBG_TAG "input.uapi"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
#define _IOC_NRBITS 8
#define _IOC_TYPEBITS 8
#ifndef _IOC_SIZEBITS
#define _IOC_SIZEBITS 14
#endif
#ifndef _IOC_DIRBITS
#define _IOC_DIRBITS 2
#endif
#define _IOC_SIZEMASK ((1 << _IOC_SIZEBITS) - 1)
#define _IOC_NRSHIFT 0
#define _IOC_TYPESHIFT (_IOC_NRSHIFT + _IOC_NRBITS)
#define _IOC_SIZESHIFT (_IOC_TYPESHIFT + _IOC_TYPEBITS)
#define _IOC_DIRSHIFT (_IOC_SIZESHIFT + _IOC_SIZEBITS)
#define _IOC_NR(nr) (((nr) >> _IOC_NRSHIFT) & 0xFF)
#define _IOC_TYPE(nr) (((nr) >> _IOC_TYPESHIFT) & 0xFF)
#define _IOC_SIZE(nr) (((nr) >> _IOC_SIZESHIFT) & 0x3FFF)
#define _IOC_DIR(nr) (((nr) >> _IOC_DIRSHIFT) & 0x3)
#ifndef _IOC_READ
#define _IOC_READ 2U
#endif
#ifndef _IOC
#define _IOC(dir, type, nr, size) \
( \
((dir) << _IOC_DIRSHIFT) | \
((type) << _IOC_TYPESHIFT) | \
((nr) << _IOC_NRSHIFT) | \
((size) << _IOC_SIZESHIFT) \
)
#endif
struct input_uapi
{
struct dfs_file *grabbed_file;
rt_atomic_t write_idx;
rt_atomic_t read_idx;
rt_atomic_t sync_count;
struct input_event events[RT_INPUT_UAPI_EVENT_MAX];
};
static int input_uapi_fops_open(struct dfs_file *file)
{
struct rt_input_device *idev = file->vnode->data;
rt_device_open(&idev->parent, RT_DEVICE_OFLAG_RDWR);
return 0;
}
static int input_uapi_fops_close(struct dfs_file *file)
{
struct rt_input_device *idev = file->vnode->data;
struct input_uapi *uapi = idev->uapi;
rt_device_close(&idev->parent);
if (uapi->grabbed_file == file)
{
rt_spin_lock(&idev->lock);
uapi->grabbed_file = RT_NULL;
rt_spin_unlock(&idev->lock);
}
return 0;
}
static int input_uapi_fops_ioctl(struct dfs_file *file, int cmd, void *args)
{
unsigned int size;
struct rt_input_device *idev = file->vnode->data;
struct input_uapi *uapi = idev->uapi;
switch (cmd)
{
case EVIOCGVERSION:
{
int version = EV_VERSION;
rt_memcpy(args, &version, sizeof(int));
return 0;
}
case EVIOCGID:
{
static struct input_id virtual_id =
{
.bustype = 0x06, /* BUS_VIRTUAL */
.vendor = 0x5354, /* "RT" */
.product = 0x4556, /* "EV" */
.version = RT_VER_NUM >> 16,
};
rt_memcpy(args, &virtual_id, sizeof(virtual_id));
return 0;
}
case EVIOCGRAB:
rt_spin_lock(&idev->lock);
if (uapi->grabbed_file && uapi->grabbed_file != file)
{
rt_spin_unlock(&idev->lock);
return -EBUSY;
}
uapi->grabbed_file = args ? file : RT_NULL;
rt_spin_unlock(&idev->lock);
return 0;
}
size = _IOC_SIZE(cmd);
switch (((cmd) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT)))
{
case EVIOCGNAME(0):
rt_strncpy(args, idev->parent.parent.name, rt_min_t(unsigned int, size, RT_NAME_MAX));
return 0;
case EVIOCGPROP(0):
{
rt_bitmap_t *bitmap = args;
const int input_prop_direct = 0x1;
rt_memset(bitmap, 0, size);
if (size >= sizeof(rt_bitmap_t))
{
bitmap[RT_BIT_WORD(input_prop_direct)] |= RT_BIT_MASK(input_prop_direct);
}
return 0;
}
}
if (_IOC_TYPE(cmd) != 'E')
{
return -EINVAL;
}
if (_IOC_DIR(cmd) == _IOC_READ)
{
if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
{
rt_size_t bit_len;
rt_bitmap_t *bitmap;
switch (_IOC_NR(cmd) & EV_MAX)
{
case 0: bitmap = idev->cap; bit_len = EV_MAX; break;
case EV_KEY: bitmap = idev->key_map; bit_len = KEY_MAX; break;
case EV_REL: bitmap = idev->rel_map; bit_len = REL_MAX; break;
case EV_ABS: bitmap = idev->abs_map; bit_len = ABS_MAX; break;
default:
return -EINVAL;
}
size = rt_min_t(rt_size_t, size, ((bit_len + 8) / 8));
rt_memcpy(args, bitmap, size);
return 0;
}
if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0)))
{
rt_size_t max;
if (!idev->absinfo)
{
return -EINVAL;
}
max = _IOC_NR(cmd) & ABS_MAX;
rt_memcpy(args, &idev->absinfo[max], rt_min_t(rt_size_t, size, sizeof(struct input_absinfo)));
return 0;
}
}
return -EINVAL;
}
static ssize_t input_uapi_fops_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
{
int err;
size_t read = 0;
struct input_event *event = buf;
struct rt_input_device *idev = file->vnode->data;
struct input_uapi *uapi = idev->uapi;
rt_spin_lock(&idev->lock);
if (uapi->grabbed_file && uapi->grabbed_file != file)
{
rt_spin_unlock(&idev->lock);
return -EAGAIN;
}
rt_spin_unlock(&idev->lock);
if (count != 0 && count < sizeof(struct input_event))
{
return -EINVAL;
}
for (;;)
{
if (!rt_atomic_load(&uapi->sync_count) && (file->flags & O_NONBLOCK))
{
#ifdef RT_UAPI_FAKE_BLOCK
static struct input_event fake_event =
{
.type = EV_SYN,
.code = SYN_REPORT,
};
rt_memcpy(event, &fake_event, sizeof(struct input_event));
read += sizeof(struct input_event);
return read;
#else
return -EAGAIN;
#endif
}
/* No IO is done but we check for error conditions */
if (count == 0)
{
break;
}
while (read + sizeof(struct input_event) <= count && rt_atomic_load(&uapi->sync_count))
{
rt_ubase_t r_idx = rt_atomic_load(&uapi->read_idx);
rt_memcpy(event, &uapi->events[r_idx], sizeof(struct input_event));
rt_atomic_store(&uapi->read_idx, (r_idx + 1) % RT_ARRAY_SIZE(uapi->events));
if (event->type == EV_SYN && event->code == SYN_REPORT)
{
rt_atomic_sub(&uapi->sync_count, 1);
}
++event;
read += sizeof(struct input_event);
}
if (read)
{
break;
}
if (!(file->flags & O_NONBLOCK))
{
err = rt_wqueue_wait_interruptible(&idev->parent.wait_queue, 0, RT_WAITING_FOREVER);
if (err)
{
return err;
}
}
}
return read;
}
static ssize_t input_uapi_fops_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
{
return -ENOSYS;
}
static int input_uapi_fops_poll(struct dfs_file *file, struct rt_pollreq *req)
{
/* Only support POLLIN */
int mask = 0, flags = file->flags & O_ACCMODE;
struct rt_input_device *idev = file->vnode->data;
struct input_uapi *uapi = idev->uapi;
if (flags == O_RDONLY || flags == O_RDWR)
{
rt_poll_add(&idev->parent.wait_queue, req);
if (rt_atomic_load(&uapi->sync_count))
{
mask |= POLLIN;
}
}
return mask;
}
static const struct dfs_file_ops input_uapi_fops =
{
.open = input_uapi_fops_open,
.close = input_uapi_fops_close,
.ioctl = input_uapi_fops_ioctl,
.read = input_uapi_fops_read,
.write = input_uapi_fops_write,
.lseek = generic_dfs_lseek,
.poll = input_uapi_fops_poll,
};
void input_uapi_init(struct rt_input_device *idev)
{
struct input_uapi *uapi = rt_calloc(1, sizeof(struct input_uapi));
if (!uapi)
{
LOG_W("%s: No memory to create UAPI", rt_dm_dev_get_name(&idev->parent));
return;
}
idev->uapi = uapi;
idev->parent.fops = &input_uapi_fops;
RT_ASSERT(sizeof(struct input_absinfo) == sizeof(struct rt_input_absinfo));
RT_ASSERT(rt_offsetof(struct input_absinfo, value) == rt_offsetof(struct rt_input_absinfo, value));
RT_ASSERT(rt_offsetof(struct input_absinfo, minimum) == rt_offsetof(struct rt_input_absinfo, minimum));
RT_ASSERT(rt_offsetof(struct input_absinfo, maximum) == rt_offsetof(struct rt_input_absinfo, maximum));
RT_ASSERT(rt_offsetof(struct input_absinfo, fuzz) == rt_offsetof(struct rt_input_absinfo, fuzz));
RT_ASSERT(rt_offsetof(struct input_absinfo, flat) == rt_offsetof(struct rt_input_absinfo, flat));
RT_ASSERT(rt_offsetof(struct input_absinfo, resolution) == rt_offsetof(struct rt_input_absinfo, resolution));
}
void input_uapi_finit(struct rt_input_device *idev)
{
if (idev->uapi)
{
rt_free(idev->uapi);
idev->uapi = RT_NULL;
idev->parent.fops = RT_NULL;
}
}
void input_uapi_event(struct rt_input_device *idev, struct rt_input_event *event)
{
rt_ubase_t w_idx, next;
struct input_event *uapi_event;
struct input_uapi *uapi = idev->uapi;
if (!idev->parent.ref_count)
{
return;
}
w_idx = rt_atomic_load(&uapi->write_idx);
next = (w_idx + 1) % RT_ARRAY_SIZE(uapi->events);
if (next == rt_atomic_load(&uapi->read_idx))
{
LOG_W("%s: Event (type: %d code: %d value: %d) dropped",
rt_dm_dev_get_name(&idev->parent),
event->type, event->code, event->value);
return;
}
uapi_event = &uapi->events[w_idx];
rt_clock_boottime_get_us(&uapi_event->time);
uapi_event->type = event->type;
uapi_event->code = event->code;
uapi_event->value = event->value;
rt_atomic_store(&uapi->write_idx, next);
if (event->type == EV_SYN && event->code == SYN_REPORT)
{
rt_atomic_add(&uapi->sync_count, 1);
rt_wqueue_wakeup(&idev->parent.wait_queue, (void *)(rt_ubase_t)POLLIN);
}
}
|