forked from mgottschlag/focaltech-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
310 lines (283 loc) · 6.5 KB
/
init.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
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <stdint.h>
#define CMD_SET_SCALING_11 0xe6
#define CMD_SET_RESOLUTION 0xe8
#define CMD_STATUS_REQUEST 0xe9
#define CMD_ENABLE_DATA_REPORTING 0xf4
#define CMD_DISABLE_DATA_REPORTING 0xf5
#define CMD_RESET 0xff
void panic(const char *message)
{
printf("Error: %s", message);
exit(EXIT_FAILURE);
}
static int write_byte(int fd, unsigned char value)
{
int ret;
do {
ret = write(fd, &value, 1);
if (ret < 0) {
return ret;
}
} while (ret == 0);
return 0;
}
static int read_byte(int fd)
{
int ret;
unsigned char value;
do {
ret = read(fd, &value, 1);
if (ret < 0) {
return ret;
}
} while (ret == 0);
return value;
}
int serio_reset(int file)
{
int received;
int device_id;
if (write_byte(file, CMD_RESET) < 0) {
return -1;
}
/* reset might not be acked properly, the mouse sends 0xaa after its
* self-test though. */
do {
received = read_byte(file);
if (received < 0) {
return -1;
}
} while (received != 0xaa);
/* also read the device ID */
device_id = read_byte(file);
if (device_id < 0) {
return -1;
}
printf("reset, device id = %02x\n", device_id); return 0; }
void send_command(int file, int command)
{
if (write_byte(file, command) < 0) {
panic("write_byte failed.");
}
if (read_byte(file) != 0xfa) {
panic("ack missing.");
}
}
void send_status_request(int file)
{
int i;
int received;
if (write_byte(file, CMD_STATUS_REQUEST) < 0) {
panic("write_byte failed.");
}
if (read_byte(file) != 0xfa) {
panic("ack missing.");
}
printf("status:");
for (i = 0; i < 3; i++) {
received = read_byte(file);
if (received < 0) {
panic("read_byte failed.");
}
printf(" %02x", received);
}
printf("\n");
}
int clicked = 0;
struct finger_state
{
int x;
int y;
int weight;
int active;
};
struct finger_state fingers[5] = {
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
void print_state(void)
{
int i;
printf(" (");
for (i = 0; i < 5; i++) {
/*printf("%d ", fingers[i].active);*/
if (fingers[i].weight > 0 && fingers[i].active > 0) {
printf("%5d/%5d ", fingers[i].x, fingers[i].y);
} else {
printf(" ");
}
}
if (clicked) {
printf("click ");
}
printf(")");
}
void parse_packet(unsigned char *data)
{
int i;
unsigned char type = data[0] & 0xf;
if (type == 0x3) {
int active = data[1] & 0x1f;
printf(" act");
for (i = 0; i < 5; i++) {
fingers[i].active = active & 0x1;
if (!fingers[i].active) {
fingers[i].weight = 0;
}
active >>= 1;
}
} else if (type == 0x6) {
int finger = (data[1] >> 4) - 1;
printf(" abs");
fingers[finger].weight = data[5];
fingers[finger].x = ((data[1] << 8) | data[2]) & 0xfff;
fingers[finger].y = (data[3] << 8) | data[4];
clicked = (data[0] >> 4) & 1;
} else if (type == 0x9) {
printf(" rel");
int finger1 = ((data[0] >> 4) & 0x7) - 1;
fingers[finger1].x += (int8_t)data[1];
fingers[finger1].y += (int8_t)data[2];
int finger2 = ((data[3] >> 4) & 0x7) - 1;
if (finger2 != -1) {
fingers[finger2].x += (int8_t)data[4];
fingers[finger2].y += (int8_t)data[5];
}
clicked = data[0] >> 7;
} else {
printf(" UNKNOWN PACKET");
}
print_state();
}
int dump_packets(int file, int length)
{
unsigned char packet[6];
int i;
int received;
assert(length <= 6);
while (1) {
printf("packet:");
for (i = 0; i < length; i++) {
received = read_byte(file);
if (received < 0) {
return received;
}
packet[i] = received;
printf(" %02x", received);
}
if (length == 6) {
parse_packet(packet);
}
printf("\n");
}
return 0;
}
int main(int argc, char **argv)
{
int i, j;
(void)i;
(void)j;
if (argc != 2) {
printf("Error: Invalid parameters.\n");
printf("Usage: %s <path-to-serio_raw-device>\n", argv[0]);
printf("For hints on how to enable serio_raw, see "
"http://people.canonical.com/~sforshee/touchpad/mouse-to-serio.sh.\n"
"Usage example: %s /dev/serio_raw0\n", argv[0]);
return -1;
}
int file = open(argv[1], O_RDWR);
if (file == -1) {
printf("Could not open %s. Check that the program is run "
"with the required privileges and that the file exists.\n",
argv[1]);
perror("open");
return -1;
}
if (serio_reset(file) < 0) {
printf("reset failed.");
return -1;
}
#define COMPAT_PROTOCOL 0
#if COMPAT_PROTOCOL
send_command(file, CMD_ENABLE_DATA_REPORTING);
if (dump_packets(file, 3) < 0) {
printf("dump_packets failed");
return -1;
}
#else
send_command(file, CMD_DISABLE_DATA_REPORTING);
send_command(file, CMD_SET_SCALING_11);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_status_request(file);
send_command(file, CMD_SET_SCALING_11);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 1);
send_status_request(file);
send_command(file, CMD_SET_SCALING_11);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 2);
send_status_request(file);
send_command(file, CMD_SET_SCALING_11);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 5);
send_status_request(file);
send_command(file, CMD_SET_SCALING_11);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 0);
send_command(file, CMD_SET_RESOLUTION);
send_command(file, 6);
send_status_request(file);
send_command(file, 0xf8);
send_command(file, 0);
send_command(file, 0xf8);
send_command(file, 0);
send_command(file, 0xf8);
send_command(file, 0);
send_command(file, 0xf8);
send_command(file, 1);
send_command(file, CMD_SET_SCALING_11);
send_command(file, CMD_ENABLE_DATA_REPORTING);
if (dump_packets(file, 6) < 0) {
printf("dump_packets failed");
return -1;
}
#endif
return 0;
}