forked from bsteinsbo/rpi_touch_driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrpi_touch_driver.c
415 lines (364 loc) · 10.3 KB
/
rpi_touch_driver.c
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* User-mode multi-touch driver for
* usb 6-2: New USB device found, idVendor=0eef, idProduct=0005
* usb 6-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
* usb 6-2: Product: By ZH851
* usb 6-2: Manufacturer: RPI_TOUCH
*
* Touch events consists of 25 bytes, one example is
* aa 01 03 1b 01 d2 bb 03 01 68 02 cc 00 5d 01 ef 01 5f 01 fe 00 fb 02 37 cc
* Offset:
* 0 : Start byte (aa)
* 1 : Any touch (0=off,1=on)
* 2-3 : First touch X
* 4-5 : First touch Y
* 6 : Multi-touch start (bb)
* 7 : Bitmask for all touches (bit 0-4 (first-fifth), 0=off, 1=on)
* 8-9 : Second touch X
* 10-11 : Second touch Y
* 12-13 : Third touch X
* 14-15 : Third touch Y
* 16-17 : Fourth touch X
* 18-19 : Fourth touch Y
* 20-21 : Fifth touch X
* 22-23 : Fifth touch Y
* 24 : End byte (cc or 00)
*
* This user mode driver decodes the touch events and injects them back into the
* kernel using uinput.
*
* Copyright (c) 2015 Bjarne Steinsbo
*
* Code and inspiration from http://thiemonge.org/getting-started-with-uinput
* and the CyanogenMod userspace touchscreen driver for cypress ctma395.
*/
/*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <libudev.h>
#include <locale.h>
#include <linux/input.h>
#include <linux/uinput.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <poll.h>
#define die(str, ...) do { \
perror(str); \
syslog(LOG_ERR, str, ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while(0)
#define croak(str, ...) do { \
perror(str); \
syslog(LOG_ERR, str, ##__VA_ARGS__); \
} while(0)
#define info(str, ...) do { \
syslog(LOG_INFO, str, ##__VA_ARGS__); \
} while(0)
int uinput_fd;
int usbraw_fd;
int fifo_fd;
int debug = 0;
#define EVENT_DEBUG 0
/*
* use those values to correct the output from touchpanel
*/
#define CORRECTION_X 1.0f
#define CORRECTION_Y 1.0f
void
closedesc()
{
if (usbraw_fd >= 0) {
close(usbraw_fd);
usbraw_fd = -1;
}
if (uinput_fd >= 0) {
ioctl(uinput_fd, UI_DEV_DESTROY);
close(uinput_fd);
uinput_fd = -1;
}
}
int send_uevent(int fd, __u16 type, __u16 code, __s32 value)
{
struct input_event event;
#if EVENT_DEBUG
char ctype[20], ccode[20];
switch (type) {
case EV_ABS:
strcpy(ctype, "EV_ABS");
break;
case EV_KEY:
strcpy(ctype, "EV_KEY");
break;
case EV_SYN:
strcpy(ctype, "EV_SYN");
break;
}
switch (code) {
case ABS_MT_SLOT:
strcpy(ccode, "ABS_MT_SLOT");
break;
case ABS_MT_TRACKING_ID:
strcpy(ccode, "ABS_MT_TRACKING_ID");
break;
case ABS_MT_TOUCH_MAJOR:
strcpy(ccode, "ABS_MT_TOUCH_MAJOR");
break;
case ABS_X:
strcpy(ccode, "ABS_X");
break;
case ABS_MT_POSITION_X:
strcpy(ccode, "ABS_MT_POSITION_X");
break;
case ABS_Y:
strcpy(ccode, "ABS_Y");
break;
case ABS_MT_POSITION_Y:
strcpy(ccode, "ABS_MT_POSITION_Y");
break;
case SYN_MT_REPORT:
strcpy(ccode, "SYN_MT_REPORT");
break;
case SYN_REPORT:
strcpy(ccode, "SYN_REPORT");
break;
case BTN_TOUCH:
strcpy(ccode, "BTN_TOUCH");
break;
}
printf("event type: '%s' code: '%s' value: %i \n", ctype, ccode, value);
#endif
memset(&event, 0, sizeof(event));
event.type = type;
event.code = code;
event.value = value;
if (write(fd, &event, sizeof(event)) < 0) {
fprintf(stderr, "Error on send_event %lu", sizeof(event));
return -1;
}
return 0;
}
/* process data chunk */
void parse_blob(int ofd, char *data, int dlen)
{
int i = 0;
int dleft = dlen;
int state[5];
int x[5];
int y[5];
int mode;
if (dlen<25) return;
for (i=0; i<=(dlen-25); i++){
if (data[i] == 0xaa && data[i+6] == 0xbb && dleft >=25){
if (debug) info("touch data found at %d: mode: %d", i, data[i+1]);
mode = data[i+1];
state[0] = (data[i+7] & 1) != 0;
x[0] = data[i+2] * 256 + data[i+3];
y[0] = data[i+4] * 256 + data[i+5];
if (mode) {
/* touch operation started */
send_uevent(uinput_fd, EV_ABS, ABS_X, x[0] * CORRECTION_X);
send_uevent(uinput_fd, EV_ABS, ABS_Y, y[0] * CORRECTION_Y);
send_uevent(uinput_fd, EV_KEY, BTN_TOUCH, 1);
} else {
/* touch operation finished */
send_uevent(uinput_fd, EV_KEY, BTN_TOUCH, 0);
}
send_uevent(uinput_fd, EV_SYN, 0, 0);
}
}
}
/* Open hidraw device and translate events until failure */
void handle_hidraw_device(char *path)
{
struct uinput_user_dev device;
unsigned char data[25];
int prev_state[5];
/* Open usbraw-device, communicated from udev through the fifo */
usbraw_fd = open(path, O_RDONLY);
if (usbraw_fd < 0) {
croak("error: open usbraw device : %s", path);
return;
}
syslog(LOG_INFO, "Starting : %s", path);
/* Iniialize uinput */
memset(&device, 0, sizeof(device));
strcpy(device.name, "RPI_TOUCH_uinput");
device.id.bustype = BUS_VIRTUAL;
device.id.vendor = 1;
device.id.product = 1;
device.id.version = 1;
device.absmin[ABS_X] = 0;
device.absmin[ABS_Y] = 0;
device.absmax[ABS_X] = 800;
device.absmax[ABS_Y] = 480;
//device.absmax[ABS_MT_POSITION_X] = 800;
//device.absmax[ABS_MT_POSITION_Y] = 480;
//device.absmax[ABS_MT_SLOT] = 5;
//device.absmax[ABS_MT_TRACKING_ID] = 5;
uinput_fd = open("/dev/input/uinput", O_WRONLY | O_NONBLOCK);
if (uinput_fd < 0) {
uinput_fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (uinput_fd < 0)
die("error: open uinput device");
}
if (write(uinput_fd, &device, sizeof(device)) != sizeof(device))
die("error: setup device");
if (ioctl(uinput_fd, UI_SET_EVBIT, EV_KEY) < 0)
die("error: evbit key\n");
if (ioctl(uinput_fd, UI_SET_EVBIT, EV_SYN) < 0)
die("error: evbit syn\n");
if (ioctl(uinput_fd, UI_SET_EVBIT, EV_ABS) < 0)
die("error: evbit abs\n");
if (ioctl(uinput_fd, UI_SET_ABSBIT, ABS_X) < 0)
die("error: abs x\n");
if (ioctl(uinput_fd, UI_SET_ABSBIT, ABS_Y) < 0)
die("error: abs y\n");
/*if (ioctl(uinput_fd, UI_SET_ABSBIT, ABS_MT_SLOT) < 0)
die("error: abs slot\n");
if (ioctl(uinput_fd, UI_SET_ABSBIT, ABS_MT_TRACKING_ID) < 0)
die("error: abs track id\n");
if (ioctl(uinput_fd, UI_SET_ABSBIT, ABS_MT_POSITION_X) < 0)
die("error: abs mt x\n");
if (ioctl(uinput_fd, UI_SET_ABSBIT, ABS_MT_POSITION_Y) < 0)
die("error: abs mt y\n");
*/
if (ioctl(uinput_fd, UI_SET_KEYBIT, BTN_TOUCH) < 0)
die("error: evbit touch\n");
if (ioctl(uinput_fd, UI_DEV_CREATE) < 0)
die("error: create\n");
/* polling structures */
char bdata[1024];
int retval;
struct pollfd fds[1];
atexit(closedesc);
/* Enter input loop */
while (1) {
memset(&fds, 0, sizeof(fds));
fds[0].fd = usbraw_fd;
fds[0].events = POLLIN;
if((retval = poll(fds, 1, 500)) == -1) {
croak("hidraw device polling error");
break;
} else if (retval) {
int n = read(usbraw_fd, bdata, sizeof(bdata));
if (n < 0) {
break; /* Unplug? */
}
if (debug)
info(". data read: %d bytes", n);
if (n>=25)
parse_blob(uinput_fd, bdata, n);
} else {
// timeout
}
}
/* close open descriptors */
if (usbraw_fd >= 0)
close(usbraw_fd);
if (uinput_fd >= 0) {
ioctl(uinput_fd, UI_DEV_DESTROY);
close(uinput_fd);
}
}
char rpi_dev[64];
char *find_rpi_touch()
{
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev;
/* Create the udev object */
udev = udev_new();
if (!udev)
die("Can't create udev");
/* Create a list of the devices in the 'hidraw' subsystem. */
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "hidraw");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
/* For each item enumerated, print out its information.
udev_list_entry_foreach is a macro which expands to
a loop. The loop will be executed for each member in
devices, setting dev_list_entry to a list entry
which contains the device's path in /sys. */
udev_list_entry_foreach(dev_list_entry, devices) {
const char *path;
/* Get the filename of the /sys entry for the device
and create a udev_device object (dev) representing it */
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
strncpy(rpi_dev, udev_device_get_devnode(dev), 64);
/* The device pointed to by dev contains information about
the hidraw device. In order to get information about the
USB device, get the parent device with the
subsystem/devtype pair of "usb"/"usb_device". This will
be several levels up the tree, but the function will find
it.*/
dev = udev_device_get_parent_with_subsystem_devtype(
dev, "usb", "usb_device");
if (!dev)
die("Unable to find parent usb device.");
if (strcmp(udev_device_get_sysattr_value(dev, "idVendor"), "0eef")
|| strcmp(udev_device_get_sysattr_value(dev, "idProduct"), "0005")) {
rpi_dev[0] = 0;
}
udev_device_unref(dev);
if (rpi_dev[0] != 0)
break;
}
/* Free the enumerator object */
udev_enumerate_unref(enumerate);
udev_unref(udev);
return rpi_dev[0] == 0 ? NULL : rpi_dev;
}
int main(int argc, char **argv)
{
int numc;
struct udev *udev;
struct udev_device *dev;
struct udev_monitor *mon;
char *devname;
/* Syslog */
openlog("rpi_touch_driver", LOG_NOWAIT, LOG_DAEMON);
/* Deamonize? */
if (!(argc > 1 && strlen(argv[1]) > 1 && !strcmp(argv[1], "-d"))) {
if (daemon(0, 0))
die("error: daemonize\n");
}
if ((argc > 1 && strlen(argv[1]) > 1 && !strcmp(argv[1], "-D")))
{
info(". enabling debug");
debug = 1;
}
/* Create the udev object */
udev = udev_new();
if (!udev)
die("Can't create udev");
mon = udev_monitor_new_from_netlink(udev, "udev");
udev_monitor_filter_add_match_subsystem_devtype(mon, "hidraw", NULL);
udev_monitor_enable_receiving(mon);
devname = find_rpi_touch();
if (devname != NULL)
handle_hidraw_device(devname);
while (1)
{
/* Block waiting for an event */
dev = udev_monitor_receive_device(mon);
/* Release immediately */
udev_device_unref(dev);
devname = find_rpi_touch();
if (devname != NULL)
handle_hidraw_device(devname);
}
udev_unref(udev);
return 0;
}