File: waveshare-raw.c

package info (click to toggle)
tslib 1.22-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: ansic: 18,859; sh: 4,167; makefile: 550
file content (352 lines) | stat: -rw-r--r-- 7,698 bytes parent folder | download | duplicates (3)
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
/*
 * tslib driver for WaveShare touchscreens
 * Copyright (C) 2015 Peter Vicman
 * inspiration from derekhe:
 * https://github.com/derekhe/wavesahre-7inch-touchscreen-driver
 *
 * This file is placed under the LGPL.  Please see the file COPYING for more
 * details.
 *
 * SPDX-License-Identifier: LGPL-2.1
 *
 *
 * Usage:
 *   module_raw waveshare vid_pid=<vendorID>:<productID> len=<raw_data_len>
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/hidraw.h>
#include <stdint.h>

#include "config.h"
#include "tslib-private.h"

struct tslib_input {
	struct tslib_module_info module;
	int vendor;
	int product;
	int len;
};

static int waveshare_read(struct tslib_module_info *inf, struct ts_sample *samp,
			  int nr)
{
	static short reopen = 1;
	struct stat devstat;
	struct hidraw_devinfo info;
	char name_buf[512];
	int cnt;
	short found = 0;
	struct tslib_input *i = (struct tslib_input *) inf;
	struct tsdev *ts = inf->dev;
	struct tsdev *ts_tmp;
	char *buf;
	int ret;

	if (reopen == 1) {
		reopen = 0;

		if (i->vendor > 0 && i->product > 0) {
#ifdef DEBUG
			fprintf(stderr,
				"waveshare: searching for device using hidraw...\n");
#endif
			for (cnt = 0; cnt < HIDRAW_MAX_DEVICES; cnt++) {
				snprintf(name_buf, sizeof(name_buf),
					 "/dev/hidraw%d", cnt);
#ifdef DEBUG
				fprintf(stderr,
					"waveshare: device: %s\n", name_buf);
#endif
				ret = stat(name_buf, &devstat);
				if (ret < 0)
					continue;

				ts_tmp = ts_open(name_buf, 0);
				if (!ts_tmp)
					continue;

#ifdef DEBUG
				fprintf(stderr, "  opened\n");
#endif
				ret = ioctl(ts_tmp->fd, HIDIOCGRAWINFO, &info);
				if (ret < 0) {
					ts_close(ts_tmp);
					continue;
				}

				info.vendor &= 0xFFFF;
				info.product &= 0xFFFF;
#ifdef DEBUG
				fprintf(stderr,
					"  vid=%04X, pid=%04X\n",
					info.vendor, info.product);
#endif

				if (i->vendor == info.vendor &&
				    i->product == info.product) {
					close(ts->fd);

					ts->fd = ts_tmp->fd;
					found = 1;
#ifdef DEBUG
					fprintf(stderr, "  correct device\n");
#endif
					ts_close(ts_tmp);
					break;
				}

				ts_close(ts_tmp);
			} /* for HIDRAW_MAX_DEVICES */

			if (found == 0)
				return -1;
		} /* vid/pid set */
	} /* reopen */

	buf = alloca(i->len * nr);

	ret = read(ts->fd, buf, (size_t) i->len * nr);
	if (ret > 0) {
		while (ret >= (int) i->len) {
	/*
	  0000271: aa01 00e4 0139 bb01 01e0 0320 01e0 0320 01e0 0320 01e0 0320 cc  .....9..... ... ... ... .

	  "aa" is start of the command,
	  "01" means clicked, while
	  "00" means unclicked.
	  "00e4" and "0139" is the X,Y position (HEX).
	  "bb" is start of multi-touch,
	  and the following bytes are the position of each point.
	 */
			samp->pressure = buf[1] & 0xff;
			samp->x = ((buf[2] & 0xff) << 8) | (buf[3] & 0xff);
			samp->y = ((buf[4] & 0xff) << 8) | (buf[5] & 0xff);
			gettimeofday(&samp->tv, NULL);
		#ifdef DEBUG
			fprintf(stderr, "waveshare raw: %d %d %d\n",
				samp->x, samp->y, samp->pressure);
			fprintf(stderr, "%x %x %x %x %x %x\n",
				buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
		#endif
			samp++;
			buf += i->len;
			ret -= i->len;
		}
	} else {
		return -1;
	}

	return nr;
}

static int waveshare_read_mt(struct tslib_module_info *inf,
			     struct ts_sample_mt **samp, int max_slots, int nr)
{
	struct tslib_input *i = (struct tslib_input *)inf;
	struct tsdev *ts = inf->dev;
	static short reopen = 1;
	struct stat devstat;
	struct hidraw_devinfo info;
	char name_buf[512];
	int cnt;
	short found = 0;
	struct tsdev *ts_tmp;
	char *buf;
	int ret;
	int count = 0;

	if (max_slots > 1) {
	#ifdef DEBUG
		printf("WAVESHARE: only one slot supported\n");
	#endif
		max_slots = 1;
	}

	if (reopen == 1) {
		reopen = 0;

		if (i->vendor > 0 && i->product > 0) {
#ifdef DEBUG
			fprintf(stderr,
				"waveshare: searching for device using hidraw...\n");
#endif
			for (cnt = 0; cnt < HIDRAW_MAX_DEVICES; cnt++) {
				snprintf(name_buf, sizeof(name_buf),
					 "/dev/hidraw%d", cnt);
#ifdef DEBUG
				fprintf(stderr,
					"waveshare: device: %s\n", name_buf);
#endif
				ret = stat(name_buf, &devstat);
				if (ret < 0)
					continue;

				ts_tmp = ts_open(name_buf, 0);
				if (!ts_tmp)
					continue;

#ifdef DEBUG
				fprintf(stderr, "  opened\n");
#endif
				ret = ioctl(ts_tmp->fd, HIDIOCGRAWINFO, &info);
				if (ret < 0) {
					ts_close(ts_tmp);
					continue;
				}

				info.vendor &= 0xFFFF;
				info.product &= 0xFFFF;
#ifdef DEBUG
				fprintf(stderr,
					"  vid=%04X, pid=%04X\n",
					info.vendor, info.product);
#endif

				if (i->vendor == info.vendor &&
				    i->product == info.product) {
					close(ts->fd);

					ts->fd = ts_tmp->fd;
					found = 1;
#ifdef DEBUG
					fprintf(stderr, "  correct device\n");
#endif
					ts_close(ts_tmp);
					break;
				}

				ts_close(ts_tmp);
			} /* for HIDRAW_MAX_DEVICES */

			if (found == 0)
				return -1;
		} /* vid/pid set */
	} /* reopen */

	buf = alloca(i->len * nr);

	ret = read(ts->fd, buf, (size_t) i->len * nr);
	if (ret > 0) {
		while (ret >= (int) i->len) {
	/*
	  0000271: aa01 00e4 0139 bb01 01e0 0320 01e0 0320 01e0 0320 01e0 0320 cc  .....9..... ... ... ... .

	  "aa" is start of the command,
	  "01" means clicked, while
	  "00" means unclicked.
	  "00e4" and "0139" is the X,Y position (HEX).
	  "bb" is start of multi-touch,
	  and the following bytes are the position of each point.
	 */
			samp[count][0].pressure = buf[1] & 0xff;
			samp[count][0].x = ((buf[2] & 0xff) << 8) | (buf[3] & 0xff);
			samp[count][0].y = ((buf[4] & 0xff) << 8) | (buf[5] & 0xff);
			samp[count][0].valid |= TSLIB_MT_VALID;
			gettimeofday(&samp[count][0].tv, NULL);
		#ifdef DEBUG
			fprintf(stderr, "waveshare raw: %d %d %d\n",
				samp[count][0].x, samp[count][0].y, samp[count][0].pressure);
			fprintf(stderr, "%x %x %x %x %x %x\n",
				buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
		#endif
			count++;
			buf += i->len;
			ret -= i->len;
		}
	} else {
		return -1;
	}

	return nr;
}

static const struct tslib_ops waveshare_ops = {
	.read = waveshare_read,
	.read_mt = waveshare_read_mt,
};

static int parse_vid_pid(struct tslib_module_info *inf, char *str, void *data)
{
	struct tslib_input *i = (struct tslib_input *)inf;

	if (strlen(str) < 9 || (int)(intptr_t) data != 1)
		return 0;   /* -1 */

	str[4] = str[9] = '\0';
	i->vendor = strtol(&str[0], NULL, 16);
	i->product = strtol(&str[5], NULL, 16);
#ifdef DEBUG
	fprintf(stderr, "waveshare vid:pid - %04X:%04X\n",
		i->vendor, i->product);
#endif /*DEBUG*/
	return 0;
}

static int parse_len(struct tslib_module_info *inf, char *str, void *data)
{
	struct tslib_input *i = (struct tslib_input *)inf;
	int v;
	int err = errno;

	v = atoi(str);

	if (v < 0)
		return -1;

	errno = err;
	switch ((int)(intptr_t) data) {
	case 1:
		i->len = v;
#ifdef DEBUG
		fprintf(stderr, "waveshare raw data len: %d bytes\n", i->len);
#endif
		break;
	default:
		return -1;
	}
	return 0;
}

static const struct tslib_vars raw_vars[] = {
	{ "vid_pid", (void *) 1, parse_vid_pid },
	{ "len", (void *) 1, parse_len },
};

#define NR_VARS (sizeof(raw_vars) / sizeof(raw_vars[0]))

TSAPI struct tslib_module_info *waveshare_mod_init(struct tsdev *dev,
						   const char *params)
{
	struct tslib_input *i;

	(void) dev;

	i = malloc(sizeof(struct tslib_input));
	if (i == NULL)
		return NULL;

	i->module.ops = &waveshare_ops;
	i->vendor = 0;
	i->product = 0;
	i->len = 25;

	if (tslib_parse_vars(&i->module, raw_vars, NR_VARS, params)) {
		free(i);
		return NULL;
	}

	return &(i->module);
}

#ifndef TSLIB_STATIC_WAVESHARE_MODULE
	TSLIB_MODULE_INIT(waveshare_mod_init);
#endif