-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect-alps.c
469 lines (409 loc) · 11.6 KB
/
detect-alps.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* Copyright (C) 2010-2011 Canonical
* Author: Seth Forshee <[email protected]>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <limits.h>
#include <glob.h>
/*
* PS/2 mouse commands (copied from kernel)
*
* Bits 12 - 15: Data bytes to send following command
* Bits 8 - 11: Data bytes to receive following command
* Bits 0 - 7: Command
*/
#define PSMOUSE_CMD_SETSCALE11 0x00e6
#define PSMOUSE_CMD_SETSCALE21 0x00e7
#define PSMOUSE_CMD_SETRES 0x10e8
#define PSMOUSE_CMD_GETINFO 0x03e9
#define PSMOUSE_CMD_SETSTREAM 0x00ea
#define PSMOUSE_CMD_SETPOLL 0x00f0
#define PSMOUSE_CMD_POLL 0x00eb /* caller sets number of bytes to receive */
#define PSMOUSE_CMD_GETID 0x02f2
#define PSMOUSE_CMD_SETRATE 0x10f3
#define PSMOUSE_CMD_ENABLE 0x00f4
#define PSMOUSE_CMD_DISABLE 0x00f5
#define PSMOUSE_CMD_RESET_DIS 0x00f6
#define PSMOUSE_CMD_RESET_BAT 0x02ff
enum {
ALPS_PROTO_V3,
ALPS_PROTO_V4,
};
struct alps_serio_dev {
int proto_version;
char *serio_drvctl_path;
int serio_fd;
};
static int verbose = 0;
#define verbose_printf(fmt, args...) do { if (verbose) printf(fmt, ##args); } while (0)
/*
* Read wrapper that continues until the requested number of bytes have
* been read or an error occurs. Returns 0 on success.
*/
static int do_read(int fd, void *buf, size_t count)
{
size_t bytes_read = 0;
struct pollfd pfd;
int ret;
pfd.fd = fd;
pfd.events = POLLIN;
do {
/* Use poll to timeout if we aren't getting data */
ret = poll(&pfd, 1, 500);
if (ret == 0 || ret == -1)
return -1;
ret = read(fd, (char *)buf + bytes_read, count - bytes_read);
if (ret == -1) {
switch (errno) {
case EAGAIN:
continue;
default:
return -1;
}
} else if (ret == 0) {
printf("Unexpected end of file\n");
return -1;
}
bytes_read += ret;
} while (bytes_read < count);
return 0;
}
/*
* Write wrapper that continues until the requested number of bytes have
* been written or an error occurs. Returns 0 on success.
*/
static int do_write(int fd, const void *buf, size_t count)
{
size_t bytes_written = 0;
do {
size_t ret = write(fd, (const char *)buf + bytes_written,
count - bytes_written);
if (ret == -1) {
switch (errno) {
case EAGAIN:
continue;
default:
return -1;
}
} else if (ret == 0) {
printf("Unexpected end of file\n");
return -1;
}
bytes_written += ret;
} while (bytes_written < count);
return 0;
}
static struct alps_serio_dev *alps_serio_alloc(void)
{
struct alps_serio_dev *dev;
dev = calloc(1, sizeof(*dev));
if (!dev)
return NULL;
dev->serio_fd = -1;
return dev;
}
static void alps_serio_free(struct alps_serio_dev *dev)
{
if (dev->serio_drvctl_path)
free(dev->serio_drvctl_path);
free(dev);
}
static int serio_mouse_init(struct alps_serio_dev *dev)
{
static const char serio_mouse_desc[] = "i8042 AUX port";
char *path;
glob_t globbuf;
int ret;
int i;
int fd;
path = malloc(PATH_MAX);
if (!path)
return -1;
globbuf.gl_offs = 0;
ret = glob("/sys/bus/serio/devices/serio*",
GLOB_ERR|GLOB_MARK|GLOB_NOSORT|GLOB_ONLYDIR, NULL, &globbuf);
if (ret)
goto free_path;
ret = -1;
for (i = 0; i < globbuf.gl_pathc; i++) {
char read_buf[64] = {0,};
verbose_printf("Checking path %s\n", globbuf.gl_pathv[i]);
snprintf(path, PATH_MAX, "%sdescription", globbuf.gl_pathv[i]);
fd = open(path, O_RDONLY);
if (fd == -1) {
printf("Could not open %s for reading: %s\n",
path, strerror(errno));
continue;
}
if (read(fd, read_buf, sizeof(read_buf)) <= 0) {
printf("Could not read from %s\n", path);
} else if (!strncmp(read_buf, serio_mouse_desc,
sizeof(serio_mouse_desc) - 1)) {
printf("Found serio mouse at %s\n", globbuf.gl_pathv[i]);
close(fd);
snprintf(path, PATH_MAX, "%sdrvctl", globbuf.gl_pathv[i]);
fd = open(path, O_WRONLY);
if (fd == -1) {
printf("Could not open %s for writing: %s\n",
path, strerror(errno));
return -1;
}
if (write(fd, "serio_raw", 9) != 9) {
printf("Error writing to %s\n", path);
} else {
dev->serio_drvctl_path = strdup(path);
if (dev->serio_drvctl_path)
ret = 0;
}
close(fd);
break;
}
close(fd);
}
if (dev->serio_drvctl_path) {
/*
* XXX: Assume that the first serio device we find
* is the right one, should be improved
*/
globfree(&globbuf);
ret = glob("/dev/serio_raw*", GLOB_ERR, NULL, &globbuf);
if (ret) {
printf("Could not find serio_raw device node\n");
goto free_glob;
}
ret = open(globbuf.gl_pathv[0], O_RDWR);
if (ret != -1) {
dev->serio_fd = ret;
ret = 0;
} else {
printf("Failed to open device node %s\n",
globbuf.gl_pathv[0]);
}
}
free_glob:
globfree(&globbuf);
free_path:
free(path);
return ret ? -1 : 0;
}
static void serio_mouse_deinit(struct alps_serio_dev *dev)
{
int fd;
if (dev->serio_fd != -1) {
close(dev->serio_fd);
dev->serio_fd = -1;
}
fd = open(dev->serio_drvctl_path, O_WRONLY);
if (fd != -1) {
write(fd, "psmouse", 7);
close(fd);
}
}
/*
* This is quick and dirty, and prone to errors. I'm basically assuming
* that there will be no data send other than responses to the commands
* we send the touchpad. Of course this might not be true, and if it
* isn't we're going to fail.
*/
static int ps2_command(struct alps_serio_dev *dev, unsigned char *data,
int command)
{
int send_bytes = (command & 0xf000) >> 12;
int recv_bytes = (command & 0x0f00) >> 8;
unsigned char cmd = command & 0x00ff;
unsigned char ack_byte;
if ((send_bytes || recv_bytes) && !data)
return -1;
if (do_write(dev->serio_fd, &cmd, 1)) {
printf("Error writing to serio device\n");
return -1;
}
/* Need to read ack byte before sending/receiving data */
if (do_read(dev->serio_fd, &ack_byte, 1)) {
printf("Error reading ack byte\n");
return -1;
}
if (ack_byte != 0xfa) {
printf("Invalid ack byte 0x%02hhx after command\n", ack_byte);
return -1;
}
if (send_bytes) {
if (do_write(dev->serio_fd, data, send_bytes)) {
printf("Error writing command data\n");
return -1;
} else if (do_read(dev->serio_fd, &ack_byte, 1)) {
printf("Error reading ack byte after sending data\n");
return -1;
} else if (ack_byte != 0xfa) {
printf("Invalid ack byte 0x%02hhx after data\n", ack_byte);
return -1;
}
}
if (recv_bytes && do_read(dev->serio_fd, data, recv_bytes)) {
printf("Error reading command response\n");
return -1;
}
return 0;
}
static void ps2_drain(struct alps_serio_dev *dev)
{
unsigned char byte;
long flags;
flags = fcntl(dev->serio_fd, F_GETFL);
if (flags == -1) {
printf("Could not get serio_fd flags, device will not be drained\n");
return;
}
if (fcntl(dev->serio_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
printf("Could not set serio_fd flags, device will not be drained\n");
return;
}
while (read(dev->serio_fd, &byte, 1) == 1)
;
if (fcntl(dev->serio_fd, F_SETFL, flags))
printf("Error restoring serio_fd flags, left in nonblock mode\n");
}
#define ALPS_CMD_NIBBLE_10 0x01f2
struct alps_command_nibbles {
int command;
unsigned char data;
};
static const struct alps_command_nibbles alps_nibble_commands_v3[] = {
{ PSMOUSE_CMD_SETPOLL, 0x00 }, /* 0 */
{ PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 */
{ PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 */
{ PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 */
{ PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 */
{ PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 */
{ PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 */
{ PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 */
{ PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 */
{ PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 */
{ ALPS_CMD_NIBBLE_10, 0x00 }, /* a */
{ PSMOUSE_CMD_SETRES, 0x00 }, /* b */
{ PSMOUSE_CMD_SETRES, 0x01 }, /* c */
{ PSMOUSE_CMD_SETRES, 0x02 }, /* d */
{ PSMOUSE_CMD_SETRES, 0x03 }, /* e */
{ PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f */
};
static const struct alps_command_nibbles alps_nibble_commands_v4[] = {
{ PSMOUSE_CMD_ENABLE, 0x00 }, /* 0 y*/
{ PSMOUSE_CMD_RESET_DIS, 0x00 }, /* 1 y*/
{ PSMOUSE_CMD_SETSCALE21, 0x00 }, /* 2 p*/
{ PSMOUSE_CMD_SETRATE, 0x0a }, /* 3 p*/
{ PSMOUSE_CMD_SETRATE, 0x14 }, /* 4 y*/
{ PSMOUSE_CMD_SETRATE, 0x28 }, /* 5 y*/
{ PSMOUSE_CMD_SETRATE, 0x3c }, /* 6 y*/
{ PSMOUSE_CMD_SETRATE, 0x50 }, /* 7 y*/
{ PSMOUSE_CMD_SETRATE, 0x64 }, /* 8 y*/
{ PSMOUSE_CMD_SETRATE, 0xc8 }, /* 9 y*/
{ ALPS_CMD_NIBBLE_10, 0x00 }, /* a p*/
{ PSMOUSE_CMD_SETRES, 0x00 }, /* b y*/
{ PSMOUSE_CMD_SETRES, 0x01 }, /* c p*/
{ PSMOUSE_CMD_SETRES, 0x02 }, /* d ?*/
{ PSMOUSE_CMD_SETRES, 0x03 }, /* e ?*/
{ PSMOUSE_CMD_SETSCALE11, 0x00 }, /* f y*/
};
int main(void)
{
struct alps_serio_dev *dev;
unsigned char param[4];
if (geteuid() != 0) {
fprintf(stderr, "Error: Must be run as root\n");
exit(EXIT_FAILURE);
}
dev = alps_serio_alloc();
if (!dev) {
printf("Error: Could not allocate device\n");
exit(EXIT_FAILURE);
}
if (serio_mouse_init(dev)) {
printf("Error: Could not locate serio mouse\n");
serio_mouse_deinit(dev);
exit(EXIT_FAILURE);
}
/*
* First reset the device. Hopefully this will reset any changes
* already made and will get it to stop reporting data. We ignore
* errors for this one.
*/
ps2_command(dev, param, PSMOUSE_CMD_RESET_BAT);
/*
* Now drain the device. If any data has been queued up from the
* driver this will get rid of it, and hopefully things will go
* smoothly from here.
*/
ps2_drain(dev);
/*
* E6 report. ALPS should return 0,0,10 or 0,0,100.
*/
param[0] = 0;
if (ps2_command(dev, param, PSMOUSE_CMD_SETRES) ||
ps2_command(dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
ps2_command(dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
ps2_command(dev, NULL, PSMOUSE_CMD_SETSCALE11) ||
ps2_command(dev, param, PSMOUSE_CMD_GETINFO)) {
printf("E6 report failed, not an ALPS touchpad\n");
goto cleanup;
}
if (param[0] != 0 || param[1] != 0 ||
(param[2] != 10 && param[2] != 100)) {
printf("Invalid E6 report: %02hhx %02hhx %02hhx\n",
param[0], param[1], param[2]);
printf("Not an ALPS touchpad\n");
goto cleanup;
}
/*
* E7 report. Expect 0x73,0x02,0x64 for a v3 touchpad.
*/
param[0] = 0;
if (ps2_command(dev, param, PSMOUSE_CMD_SETRES) ||
ps2_command(dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
ps2_command(dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
ps2_command(dev, NULL, PSMOUSE_CMD_SETSCALE21) ||
ps2_command(dev, param, PSMOUSE_CMD_GETINFO)) {
printf("E7 report failed, not an ALPS touchpad\n");
goto cleanup;
}
if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x50) {
printf("V6 ALPS touchpad found\n");
}
else if (param[0] == 0x73 && param[1] == 0x03 && param[2] == 0x0a) {
printf("V5 ALPS touchpad found\n");
}
else if (param[0] == 0x73 && param[1] == 0x02 && param[2] == 0x64) {
printf("V3 ALPS touchpad found\n");
}
else {
printf("E7 report: %02hhx %02hhx %02hhx\n",
param[0], param[1], param[2]);
printf("Not an ALPS touchpad\n");
}
cleanup:
serio_mouse_deinit(dev);
alps_serio_free(dev);
exit(EXIT_SUCCESS);
}