Made changes as recommended by Dmitri Torokhov email 19-06-08
[inexio.git] / inputattach.c
blob52314882df3b679d951c695cc40ce4a469cbc0b3
1 /*
2 * $Id: inputattach.c,v 1.24 2006/02/08 12:19:31 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * Sponsored by SuSE
8 * Twiddler support Copyright (c) 2001 Arndt Schoenewald
9 * Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany
13 * Input line discipline attach program
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * Should you need to contact me, the author, you can do so either by
32 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
33 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
36 #include <linux/serio.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <sys/ioctl.h>
41 #include <sys/time.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <termios.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <assert.h>
51 #include <ctype.h>
53 #include "serio-ids.h"
55 static int readchar(int fd, unsigned char *c, int timeout)
57 struct timeval tv;
58 fd_set set;
60 tv.tv_sec = 0;
61 tv.tv_usec = timeout * 1000;
63 FD_ZERO(&set);
64 FD_SET(fd, &set);
66 if (!select(fd + 1, &set, NULL, NULL, &tv))
67 return -1;
69 if (read(fd, c, 1) != 1)
70 return -1;
72 return 0;
75 static void setline(int fd, int flags, int speed)
77 struct termios t;
79 tcgetattr(fd, &t);
81 t.c_cflag = flags | CREAD | HUPCL | CLOCAL;
82 t.c_iflag = IGNBRK | IGNPAR;
83 t.c_oflag = 0;
84 t.c_lflag = 0;
85 t.c_cc[VMIN ] = 1;
86 t.c_cc[VTIME] = 0;
88 cfsetispeed(&t, speed);
89 cfsetospeed(&t, speed);
91 tcsetattr(fd, TCSANOW, &t);
94 static int logitech_command(int fd, char *c)
96 int i;
97 unsigned char d;
99 for (i = 0; c[i]; i++) {
100 write(fd, c + i, 1);
101 if (readchar(fd, &d, 1000))
102 return -1;
103 if (c[i] != d)
104 return -1;
106 return 0;
109 static int magellan_init(int fd, unsigned long *id, unsigned long *extra)
111 write(fd, "m3\rpBB\rz\r", 9);
112 return 0;
115 static int warrior_init(int fd, unsigned long *id, unsigned long *extra)
117 if (logitech_command(fd, "*S"))
118 return -1;
120 setline(fd, CS8, B4800);
121 return 0;
124 static int spaceball_waitchar(int fd, unsigned char c, unsigned char *d,
125 int timeout)
127 unsigned char b = 0;
129 while (!readchar(fd, &b, timeout)) {
130 if (b == 0x0a)
131 continue;
132 *d++ = b;
133 if (b == c)
134 break;
137 *d = 0;
139 return -(b != c);
142 static int spaceball_waitcmd(int fd, char c, char *d)
144 int i;
146 for (i = 0; i < 8; i++) {
147 if (spaceball_waitchar(fd, 0x0d, d, 1000))
148 return -1;
149 if (d[0] == c)
150 return 0;
153 return -1;
156 static int spaceball_cmd(int fd, char *c, char *d)
158 int i;
160 for (i = 0; c[i]; i++)
161 write(fd, c + i, 1);
162 write(fd, "\r", 1);
164 i = spaceball_waitcmd(fd, toupper(c[0]), d);
166 return i;
169 #define SPACEBALL_1003 1
170 #define SPACEBALL_2003B 3
171 #define SPACEBALL_2003C 4
172 #define SPACEBALL_3003C 7
173 #define SPACEBALL_4000FLX 8
174 #define SPACEBALL_4000FLX_L 9
176 static int spaceball_init(int fd, unsigned long *id, unsigned long *extra)
178 char r[64];
180 if (spaceball_waitchar(fd, 0x11, r, 4000) ||
181 spaceball_waitchar(fd, 0x0d, r, 1000))
182 return -1;
184 if (spaceball_waitcmd(fd, '@', r))
185 return -1;
187 if (strncmp("@1 Spaceball alive", r, 18))
188 return -1;
190 if (spaceball_waitcmd(fd, '@', r))
191 return -1;
193 if (spaceball_cmd(fd, "hm", r))
194 return -1;
196 if (!strncmp("Hm2003B", r, 7))
197 *id = SPACEBALL_2003B;
198 if (!strncmp("Hm2003C", r, 7))
199 *id = SPACEBALL_2003C;
200 if (!strncmp("Hm3003C", r, 7))
201 *id = SPACEBALL_3003C;
203 if (!strncmp("HvFirmware", r, 10)) {
205 if (spaceball_cmd(fd, "\"", r))
206 return -1;
208 if (strncmp("\"1 Spaceball 4000 FLX", r, 21))
209 return -1;
211 if (spaceball_waitcmd(fd, '"', r))
212 return -1;
214 if (strstr(r, " L "))
215 *id = SPACEBALL_4000FLX_L;
216 else
217 *id = SPACEBALL_4000FLX;
219 if (spaceball_waitcmd(fd, '"', r))
220 return -1;
222 if (spaceball_cmd(fd, "YS", r))
223 return -1;
225 if (spaceball_cmd(fd, "M", r))
226 return -1;
228 return 0;
231 if (spaceball_cmd(fd, "P@A@A", r) ||
232 spaceball_cmd(fd, "FT@", r) ||
233 spaceball_cmd(fd, "MSS", r))
234 return -1;
236 return 0;
239 static int stinger_init(int fd, unsigned long *id, unsigned long *extra)
241 int i;
242 unsigned char c;
243 unsigned char *response = "\r\n0600520058C272";
245 if (write(fd, " E5E5", 5) != 5) /* Enable command */
246 return -1;
248 for (i = 0; i < 16; i++) /* Check for Stinger */
249 if (readchar(fd, &c, 200) || c != response[i])
250 return -1;
252 return 0;
255 static int mzp_init(int fd, unsigned long *id, unsigned long *extra)
257 if (logitech_command(fd, "*X*q"))
258 return -1;
260 setline(fd, CS8, B9600);
261 return 0;
264 static int newton_init(int fd, unsigned long *id, unsigned long *extra)
266 int i;
267 unsigned char c;
268 unsigned char response[35] = {
269 0x16, 0x10, 0x02, 0x64, 0x5f, 0x69, 0x64, 0x00,
270 0x00, 0x00, 0x0c, 0x6b, 0x79, 0x62, 0x64, 0x61,
271 0x70, 0x70, 0x6c, 0x00, 0x00, 0x00, 0x01, 0x6e,
272 0x6f, 0x66, 0x6d, 0x00, 0x00, 0x00, 0x00, 0x10,
273 0x03, 0xdd, 0xe7
276 for (i = 0; i < sizeof(response); i++)
277 if (readchar(fd, &c, 400) || c != response[i])
278 return -1;
280 return 0;
283 static int twiddler_init(int fd, unsigned long *id, unsigned long *extra)
285 unsigned char c[10];
286 int count, line;
288 /* Turn DTR off, otherwise the Twiddler won't send any data. */
289 if (ioctl(fd, TIOCMGET, &line))
290 return -1;
291 line &= ~TIOCM_DTR;
292 if (ioctl(fd, TIOCMSET, &line))
293 return -1;
296 * Check whether the device on the serial line is the Twiddler.
298 * The Twiddler sends data packets of 5 bytes which have the following
299 * properties: the MSB is 0 on the first and 1 on all other bytes, and
300 * the high order nibble of the last byte is always 0x8.
302 * We read and check two of those 5 byte packets to be sure that we
303 * are indeed talking to a Twiddler.
306 /* Read at most 5 bytes until we find one with the MSB set to 0 */
307 for (count = 0; count < 5; count++) {
308 if (readchar(fd, c, 500))
309 return -1;
310 if ((c[0] & 0x80) == 0)
311 break;
314 if (count == 5) {
315 /* Could not find header byte in data stream */
316 return -1;
319 /* Read remaining 4 bytes plus the full next data packet */
320 for (count = 1; count < 10; count++)
321 if (readchar(fd, c + count, 500))
322 return -1;
324 /* Check whether the bytes of both data packets obey the rules */
325 for (count = 1; count < 10; count++) {
326 if ((count % 5 == 0 && (c[count] & 0x80) != 0x00) ||
327 (count % 5 == 4 && (c[count] & 0xF0) != 0x80) ||
328 (count % 5 != 0 && (c[count] & 0x80) != 0x80)) {
329 /* Invalid byte in data packet */
330 return -1;
334 return 0;
337 static int fujitsu_init(int fd, unsigned long *id, unsigned long *extra)
339 unsigned char cmd, data;
341 /* Wake up the touchscreen */
342 cmd = 0xff; /* Dummy data */;
343 if (write(fd, &cmd, 1) != 1)
344 return -1;
346 /* Wait to settle down */
347 usleep(100 * 1000); /* 100 ms */
349 /* Reset the touchscreen */
350 cmd = 0x81; /* Cold reset */
351 if (write(fd, &cmd, 1) != 1)
352 return -1;
354 /* Read ACK */
355 if (readchar(fd, &data, 100) || (data & 0xbf) != 0x90)
356 return -1;
358 /* Read status */
359 if (readchar(fd, &data, 100) || data != 0x00)
360 return -1;
362 return 0;
365 static int dump_init(int fd, unsigned long *id, unsigned long *extra)
367 unsigned char c, o = 0;
369 c = 0x80;
371 if (write(fd, &c, 1) != 1) /* Enable command */
372 return -1;
374 while (1)
375 if (!readchar(fd, &c, 1)) {
376 printf("%02x (%c) ", c, ((c > 32) && (c < 127)) ? c : 'x');
377 o = 1;
378 } else {
379 if (o) {
380 printf("\n");
381 o = 0;
386 struct input_types {
387 const char *name;
388 const char *name2;
389 const char *desc;
390 int speed;
391 int flags;
392 unsigned long type;
393 unsigned long id;
394 unsigned long extra;
395 int flush;
396 int (*init)(int fd, unsigned long *id, unsigned long *extra);
399 static struct input_types input_types[] = {
400 { "--sunkbd", "-skb", "Sun Type 4 and Type 5 keyboards",
401 B1200, CS8,
402 SERIO_SUNKBD, 0x00, 0x00, 1, NULL },
403 { "--lkkbd", "-lk", "DEC LK201 / LK401 keyboards",
404 B4800, CS8|CSTOPB,
405 SERIO_LKKBD, 0x00, 0x00, 1, NULL },
406 { "--vsxxx-aa", "-vs",
407 "DEC VSXXX-AA / VSXXX-GA mouse and VSXXX-A tablet",
408 B4800, CS8|CSTOPB|PARENB|PARODD,
409 SERIO_VSXXXAA, 0x00, 0x00, 1, NULL },
410 { "--spaceorb", "-orb", "SpaceOrb 360 / SpaceBall Avenger",
411 B9600, CS8,
412 SERIO_SPACEORB, 0x00, 0x00, 1, NULL },
413 { "--spaceball", "-sbl", "SpaceBall 2003 / 3003 / 4000 FLX",
414 B9600, CS8,
415 SERIO_SPACEBALL, 0x00, 0x00, 0, spaceball_init },
416 { "--magellan", "-mag", "Magellan / SpaceMouse",
417 B9600, CS8 | CSTOPB | CRTSCTS,
418 SERIO_MAGELLAN, 0x00, 0x00, 1, magellan_init },
419 { "--warrior", "-war", "WingMan Warrior",
420 B1200, CS7 | CSTOPB,
421 SERIO_WARRIOR, 0x00, 0x00, 1, warrior_init },
422 { "--stinger", "-sting", "Gravis Stinger",
423 B1200, CS8,
424 SERIO_STINGER, 0x00, 0x00, 1, stinger_init },
425 { "--mousesystems", "-msc", "3-button Mouse Systems mouse",
426 B1200, CS8,
427 SERIO_MSC, 0x00, 0x01, 1, NULL },
428 { "--sunmouse", "-sun", "3-button Sun mouse",
429 B1200, CS8,
430 SERIO_SUN, 0x00, 0x01, 1, NULL },
431 { "--microsoft", "-bare", "2-button Microsoft mouse",
432 B1200, CS7,
433 SERIO_MS, 0x00, 0x00, 1, NULL },
434 { "--mshack", "-ms", "3-button mouse in Microsoft mode",
435 B1200, CS7,
436 SERIO_MS, 0x00, 0x01, 1, NULL },
437 { "--mouseman", "-mman", "3-button Logitech / Genius mouse",
438 B1200, CS7,
439 SERIO_MP, 0x00, 0x01, 1, NULL },
440 { "--intellimouse", "-ms3", "Microsoft IntelliMouse",
441 B1200, CS7,
442 SERIO_MZ, 0x00, 0x11, 1, NULL },
443 { "--mmwheel", "-mmw",
444 "Logitech mouse with 4-5 buttons or a wheel",
445 B1200, CS7 | CSTOPB,
446 SERIO_MZP, 0x00, 0x13, 1, mzp_init },
447 { "--iforce", "-ifor", "I-Force joystick or wheel",
448 B38400, CS8,
449 SERIO_IFORCE, 0x00, 0x00, 0, NULL },
450 { "--newtonkbd", "-newt", "Newton keyboard",
451 B9600, CS8,
452 SERIO_NEWTON, 0x00, 0x00, 1, newton_init },
453 { "--h3600ts", "-ipaq", "Ipaq h3600 touchscreen",
454 B115200, CS8,
455 SERIO_H3600, 0x00, 0x00, 0, NULL },
456 { "--stowawaykbd", "-ipaqkbd", "Stowaway keyboard",
457 B115200, CS8,
458 SERIO_STOWAWAY, 0x00, 0x00, 1, NULL },
459 { "--ps2serkbd", "-ps2ser", "PS/2 via serial keyboard",
460 B1200, CS8,
461 SERIO_PS2SER, 0x00, 0x00, 1, NULL },
462 { "--twiddler", "-twid", "Handykey Twiddler chording keyboard",
463 B2400, CS8,
464 SERIO_TWIDKBD, 0x00, 0x00, 0, twiddler_init },
465 { "--twiddler-joy", "-twidjoy", "Handykey Twiddler used as a joystick",
466 B2400, CS8,
467 SERIO_TWIDJOY, 0x00, 0x00, 0, twiddler_init },
468 { "--elotouch", "-elo", "ELO touchscreen, 10-byte mode",
469 B9600, CS8 | CRTSCTS,
470 SERIO_ELO, 0x00, 0x00, 0, NULL },
471 { "--elo4002", "-elo6b", "ELO touchscreen, 6-byte mode",
472 B9600, CS8 | CRTSCTS,
473 SERIO_ELO, 0x01, 0x00, 0, NULL },
474 { "--elo271-140", "-elo4b", "ELO touchscreen, 4-byte mode",
475 B9600, CS8 | CRTSCTS,
476 SERIO_ELO, 0x02, 0x00, 0, NULL },
477 { "--elo261-280", "-elo3b", "ELO Touchscreen, 3-byte mode",
478 B9600, CS8 | CRTSCTS,
479 SERIO_ELO, 0x03, 0x00, 0, NULL },
480 { "--mtouch", "-mtouch", "MicroTouch (3M) touchscreen",
481 B9600, CS8 | CRTSCTS,
482 SERIO_MICROTOUCH, 0x00, 0x00, 0, NULL },
483 { "--touchright", "-tr", "Touchright serial touchscreen",
484 B9600, CS8 | CRTSCTS,
485 SERIO_TOUCHRIGHT, 0x00, 0x00, 0, NULL },
486 { "--touchwin", "-tw", "Touchwindow serial touchscreen",
487 B4800, CS8 | CRTSCTS,
488 SERIO_TOUCHWIN, 0x00, 0x00, 0, NULL },
489 { "--penmount", "-pm", "Penmount touchscreen",
490 B19200, CS8 | CRTSCTS,
491 SERIO_PENMOUNT, 0x00, 0x00, 0, NULL },
492 { "--fujitsu", "-fjt", "Fujitsu serial touchscreen",
493 B9600, CS8,
494 SERIO_FUJITSU, 0x00, 0x00, 1, fujitsu_init },
495 { "--inexio", "-inx", "iNexio serial touchscreen",
496 B19200, CS8|CREAD|CLOCAL|HUPCL,
497 SERIO_INEXIO, 0x00, 0x00, 1, NULL },
498 { "--dump", "-dump", "Just enable device",
499 B2400, CS8,
500 0, 0x00, 0x00, 0, dump_init },
501 { NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, NULL }
504 static void show_help(void)
506 struct input_types *type;
508 puts("");
509 puts("Usage: inputattach [--daemon] <mode> <device>");
510 puts("");
511 puts("Modes:");
513 for (type = input_types; type->name; type++)
514 printf(" %-16s %-8s %s\n",
515 type->name, type->name2, type->desc);
517 puts("");
520 int main(int argc, char **argv)
522 unsigned long devt;
523 int ldisc;
524 struct input_types *type = NULL;
525 const char *device = NULL;
526 int daemon_mode = 0;
527 int need_device = 0;
528 unsigned long id, extra;
529 int fd;
530 int i;
531 char c;
532 int retval;
534 for (i = 1; i < argc; i++) {
535 if (!strcasecmp(argv[i], "--help")) {
536 show_help();
537 return EXIT_SUCCESS;
538 } else if (!strcasecmp(argv[i], "--daemon")) {
539 daemon_mode = 1;
540 } else if (need_device) {
541 device = argv[i];
542 need_device = 0;
543 } else {
544 if (type && type->name) {
545 fprintf(stderr,
546 "inputattach: '%s' - "
547 "only one mode allowed\n", argv[i]);
548 return EXIT_FAILURE;
550 for (type = input_types; type->name; type++) {
551 if (!strcasecmp(argv[i], type->name) ||
552 !strcasecmp(argv[i], type->name2)) {
553 break;
556 if (!type->name) {
557 fprintf(stderr,
558 "inputattach: invalid mode '%s'\n",
559 argv[i]);
560 return EXIT_FAILURE;
562 need_device = 1;
566 if (!type || !type->name) {
567 fprintf(stderr, "inputattach: must specify mode\n");
568 return EXIT_FAILURE;
571 if (need_device) {
572 fprintf(stderr, "inputattach: must specify device\n");
573 return EXIT_FAILURE;
576 fd = open(device, O_RDWR | O_NOCTTY | O_NONBLOCK);
577 if (fd < 0) {
578 fprintf(stderr, "inputattach: '%s' - %s\n",
579 device, strerror(errno));
580 return 1;
583 setline(fd, type->flags, type->speed);
585 if (type->flush)
586 while (!readchar(fd, &c, 100))
587 /* empty */;
589 id = type->id;
590 extra = type->extra;
592 if (type->init && type->init(fd, &id, &extra)) {
593 fprintf(stderr, "inputattach: device initialization failed\n");
594 return EXIT_FAILURE;
597 ldisc = N_MOUSE;
598 if (ioctl(fd, TIOCSETD, &ldisc)) {
599 fprintf(stderr, "inputattach: can't set line discipline\n");
600 return EXIT_FAILURE;
603 devt = type->type | (id << 8) | (extra << 16);
605 if (ioctl(fd, SPIOCSTYPE, &devt)) {
606 fprintf(stderr, "inputattach: can't set device type\n");
607 return EXIT_FAILURE;
610 retval = EXIT_SUCCESS;
611 if (daemon_mode && daemon(0, 0) < 0) {
612 perror("inputattach");
613 retval = EXIT_FAILURE;
616 read(fd, NULL, 0);
618 ldisc = 0;
619 ioctl(fd, TIOCSETD, &ldisc);
620 close(fd);
622 return retval;