10 TIMEOUT = 5000, /* timeout for writes */
24 #define DEBUG if(debug)
26 int can9600; /* true if type W mouse can be set to 9600 */
28 int dontset; /* true if we shouldn't try to set the mouse type */
33 fprint(2, "%s: usage: %s [device]\n", argv0, argv0);
38 catch(void *a, char *msg)
41 if(strstr(msg, "alarm"))
47 dumpbuf(char *buf, int nbytes, char *s)
51 print("#%ux ", *buf++ & 0xFF);
56 timedwrite(int fd, void *p, int n)
64 fprint(2, "%s: timed out\n", argv0);
77 if(read(fd, &c, sizeof(c)) == -1){
79 errstr(buf, sizeof buf);
80 if(strcmp(buf, "interrupted") == 0)
82 fprint(2, "%s: readbyte failed - %s\n", argv0, buf);
90 slowread(int fd, char *buf, int nbytes, char *msg)
95 for(p = buf; nbytes > 1 && (c = readbyte(fd)) != -1; *p++ = c, nbytes--)
98 DEBUG dumpbuf(buf, p-buf, msg);
107 * reset the mouse (toggle RTS)
110 timedwrite(fd, "d1", 2);
111 timedwrite(fd, "r1", 2);
113 timedwrite(fd, "d0", 2);
114 timedwrite(fd, "r0", 2);
116 timedwrite(fd, "d1", 2);
117 timedwrite(fd, "r1", 2);
122 setupeia(int fd, char *baud, char *bits)
126 * set the speed to 1200/2400/4800/9600 baud,
127 * 7/8-bit data, one stop bit and no parity
129 DEBUG print("setupeia(%s,%s)\n", baud, bits);
130 timedwrite(fd, baud, strlen(baud));
131 timedwrite(fd, bits, strlen(bits));
132 timedwrite(fd, "s1", 2);
133 timedwrite(fd, "pn", 2);
134 timedwrite(fd, "i1", 2);
139 * check for a types M, M3, & W
141 * we talk to all these mice using 1200 baud
144 MorW(int ctl, int data)
150 * set up for type M, V or W
151 * flush any pending data
153 setupeia(ctl, "b1200", "l7");
155 while(slowread(data, buf, sizeof(buf), "flush: ") > 0)
160 * see if there's any data from the mouse
161 * (type M, V and W mice)
163 c = slowread(data, buf, sizeof(buf), "check M: ");
166 * type M, V and W mice return "M" or "M3" after reset.
167 * check for type W by sending a 'Send Standard Configuration'
170 * the second check is a kludge for some type W mice on next's
171 * that send a garbage character back before the "M3".
173 if((c > 0 && buf[0] == 'M') || (c > 1 && buf[1] == 'M')){
174 timedwrite(data, "*?", 2);
175 c = slowread(data, buf, sizeof(buf), "check W: ");
178 * indicates a type W mouse
183 setupeia(ctl, "b1200", "l8");
184 timedwrite(data, "*U", 2);
185 slowread(data, buf, sizeof(buf), "check W: ");
194 * check for type C by seeing if it responds to the status
195 * command "s". the mouse is at an unknown speed so we
196 * have to check all possible speeds.
206 for(s = speeds; *s; s++){
207 DEBUG print("%s\n", *s);
208 setupeia(ctl, *s, "l8");
209 timedwrite(data, "s", 1);
210 c = slowread(data, buf, sizeof(buf), "check C: ");
211 if(c >= 1 && (*buf & 0xBF) == 0x0F){
213 timedwrite(data, "*n", 2);
215 setupeia(ctl, "b1200", "l8");
216 timedwrite(data, "s", 1);
217 c = slowread(data, buf, sizeof(buf), "recheck C: ");
218 if(c >= 1 && (*buf & 0xBF) == 0x0F){
219 timedwrite(data, "U", 1);
229 char *bauderr = "mouse: can't set baud rate, mouse at 1200\n";
232 Cbaud(int ctl, int data, int baud)
257 timedwrite(data, buf, 2);
259 timedwrite(data, buf, 2);
260 sprint(buf, "b%d", baud);
261 setupeia(ctl, buf, "l8");
265 Wbaud(int ctl, int data, int baud)
281 timedwrite(data, "*q", 2);
282 setupeia(ctl, "b9600", "l8");
283 slowread(data, buf, sizeof(buf), "setbaud: ");
287 main(int argc, char *argv[])
291 int tries, conf, ctl, data, def, type;
318 if((conf = open("/dev/mousectl", OWRITE)) == -1){
319 fprint(2, "%s: can't open /dev/mousectl - %r\n", argv0);
321 exits("open /dev/mousectl");
324 if(strncmp(p, "ps2", 3) == 0){
325 if(write(conf, p, strlen(p)) < 0){
326 fprint(2, "%s: error setting mouse type - %r\n", argv0);
333 for(tries = 0; type == 0 && tries < 6; tries++){
335 fprint(2, "%s: Unknown mouse type, retrying...\n", argv0);
336 sprint(buf, "#t/eia%sctl", p);
337 if((ctl = open(buf, ORDWR)) == -1){
338 fprint(2, "%s: can't open %s - %r\n", argv0, buf);
341 sprint(buf, "#t/eia%s", p);
342 if((data = open(buf, ORDWR)) == -1){
343 fprint(2, "%s: can't open %s - %r\n", argv0, buf);
349 type = MorW(ctl, data);
353 /* with the default we can't assume anything */
356 /* try the default */
359 setupeia(ctl, "b1200", "l8");
362 setupeia(ctl, "b1200", "l7");
369 sprint(buf, "serial %s", p);
376 DEBUG print("Logitech 5 byte mouse\n");
377 Cbaud(ctl, data, baud);
380 DEBUG print("Type W mouse\n");
381 Wbaud(ctl, data, baud);
384 DEBUG print("Microsoft compatible mouse\n");
391 fprint(2, "%s: Unknown mouse type, giving up\n", argv0);
395 DEBUG fprint(2, "mouse configured as '%s'\n", buf);
396 if(dontset == 0 && write(conf, buf, strlen(buf)) < 0){
397 fprint(2, "%s: error setting mouse type - %r\n", argv0);