File: dmc-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 (151 lines) | stat: -rw-r--r-- 3,170 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
/*
 * tslib driver for DMC touchscreens
 * Copyright (C) 2009 Michael Olbrich, Pengutronix e.K.
 * some inspiration from the old xf86-input-dmc xorg input driver.
 *
 * This file is placed under the LGPL.  Please see the file COPYING for more
 * details.
 *
 * SPDX-License-Identifier: LGPL-2.1
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include <errno.h>
#include <termios.h>

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

struct tslib_dmc {
	struct tslib_module_info module;

	int	current_x;
	int	current_y;
	int	sane_fd;
};

static int dmc_init_device(struct tsdev *dev)
{
	int fd = dev->fd;
	struct termios t;
	char buf[1];

	/* flags from the old xorg driver.
	 * I have no idea what all of these mean but it works (tm).
	 */
	tcgetattr(fd, &t);
	t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IXOFF);

	t.c_oflag &= ~OPOST;

	t.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);

	t.c_cflag &= ~(CSIZE|PARENB|CSTOPB);
	t.c_cflag |= CS8|CLOCAL;

	cfsetispeed(&t, B9600);
	cfsetospeed(&t, B9600);

	t.c_cc[VMIN] = 3;
	t.c_cc[VTIME] = 1;

	tcsetattr(fd, TCSANOW, &t);

	if (write(fd, "\x55", 1) != 1) {
		fprintf(stderr,
			"dmc: failed to write. Check permissions of the device!\n");
		return -EINVAL;
	}
	sleep(1);
	if (write(fd, "\x05\x40", 2) != 2) {
		perror("dmc write");
		goto fail;
	}
	if (read(fd, buf, 1) != 1) {
		perror("dmc read");
		goto fail;
	}
	if (buf[0] != 0x6)
		fprintf(stderr,
			"dmc: got wrong return value. The touchscreen may not work.\n");

	if (write(fd, "\x31", 1) != 1) {
		perror("dmc write");
		goto fail;
	}
	return 0;
fail:
	fprintf(stderr,
		"dmc: selected device is not a touchscreen I understand\n");
	return -EINVAL;
}

static int dmc_read(struct tslib_module_info *inf, struct ts_sample *samp,
		    int nr)
{
	struct tslib_dmc *dmc = (struct tslib_dmc *)inf;
	struct tsdev *ts = inf->dev;
	uint8_t buf[5];
	int ret;
	int i;

	for (i = 0; i < nr; ++i) {
		if ((ret = read(ts->fd, buf, 1)) != 1) {
			--i;
			break;
		}
		if (buf[0] == 0x10) {
			/* release. No coords follow. Use old values */
			samp->x = dmc->current_x;
			samp->y = dmc->current_y;
			samp->pressure = 0;
		} else if (buf[0] == 0x11) {
			/* read coords */
			if ((ret = read(ts->fd, buf, 4)) != 4) {
				/* must have 4 bytes */
				--i;
				break;
			}
			samp->x = dmc->current_x = (int)((buf[0] << 8) + buf[1]);
			samp->y = dmc->current_y = (int)((buf[2] << 8) + buf[3]);
			samp->pressure = 100;
		} else {
			continue;
		}
#ifdef DEBUG
		fprintf(stderr,
			"RAW---------------------------> %d %d %d\n",
			samp->x, samp->y, samp->pressure);
#endif /*DEBUG*/
		gettimeofday(&samp->tv, NULL);
		++samp;
	}
	return i;
}

static const struct tslib_ops dmc_ops = {
	.read	= dmc_read,
};

TSAPI struct tslib_module_info *dmc_mod_init(struct tsdev *dev,
					     __attribute__ ((unused)) const char *params)
{
	struct tslib_dmc *m;

	if (dmc_init_device(dev) != 0)
		return NULL;

	m = calloc(1, sizeof(struct tslib_dmc));
	if (m == NULL)
		return NULL;

	m->module.ops = &dmc_ops;
	return (struct tslib_module_info *)m;
}

#ifndef TSLIB_STATIC_DMC_MODULE
	TSLIB_MODULE_INIT(dmc_mod_init);
#endif