xref: /plan9/sys/src/cmd/usb/kb/kb.c (revision 27acba7cf6d37c65abba4ecf8ad572a5980447ad)
1 /*
2  * USB Human Interaction Device: keyboard and mouse.
3  *
4  * If there's no usb keyboard, it tries to setup the mouse, if any.
5  * It should be started at boot time.
6  *
7  * Mouse events are converted to the format of mouse(3)'s mousein file.
8  * Keyboard keycodes are translated to scan codes and sent to kbin(3).
9  *
10  * If there is no keyboard, it tries to setup the mouse properly, else it falls
11  * back to boot protocol.
12  */
13 
14 #include <u.h>
15 #include <libc.h>
16 #include <thread.h>
17 #include "usb.h"
18 #include "hid.h"
19 
20 enum
21 {
22 	Awakemsg= 0xdeaddead,
23 	Diemsg	= 0xbeefbeef,
24 };
25 
26 typedef struct KDev KDev;
27 typedef struct Kin Kin;
28 
29 struct KDev
30 {
31 	Dev*	dev;		/* usb device*/
32 	Dev*	ep;		/* endpoint to get events */
33 	Kin*	in;		/* used to send events to kernel */
34 	Channel*repeatc;	/* only for keyboard */
35 	int	accel;		/* only for mouse */
36 	int	bootp;		/* has associated keyboard */
37 	HidRepTempl templ;
38 	int	(*ptrvals)(KDev *kd, Chain *ch, int *px, int *py, int *pb);
39 };
40 
41 /*
42  * Kbdin and mousein files must be shared among all instances.
43  */
44 struct Kin
45 {
46 	int	ref;
47 	int	fd;
48 	char*	name;
49 };
50 
51 /*
52  * Map for the logitech bluetooth mouse with 8 buttons and wheels.
53  *	{ ptr ->mouse}
54  *	{ 0x01, 0x01 },	// left
55  *	{ 0x04, 0x02 },	// middle
56  *	{ 0x02, 0x04 },	// right
57  *	{ 0x40, 0x08 },	// up
58  *	{ 0x80, 0x10 },	// down
59  *	{ 0x10, 0x08 },	// side up
60  *	{ 0x08, 0x10 },	// side down
61  *	{ 0x20, 0x02 }, // page
62  * besides wheel and regular up/down report the 4th byte as 1/-1
63  */
64 
65 /*
66  * key code to scan code; for the page table used by
67  * the logitech bluetooth keyboard.
68  */
69 static char sctab[256] =
70 {
71 [0x00]	0x0,	0x0,	0x0,	0x0,	0x1e,	0x30,	0x2e,	0x20,
72 [0x08]	0x12,	0x21,	0x22,	0x23,	0x17,	0x24,	0x25,	0x26,
73 [0x10]	0x32,	0x31,	0x18,	0x19,	0x10,	0x13,	0x1f,	0x14,
74 [0x18]	0x16,	0x2f,	0x11,	0x2d,	0x15,	0x2c,	0x2,	0x3,
75 [0x20]	0x4,	0x5,	0x6,	0x7,	0x8,	0x9,	0xa,	0xb,
76 [0x28]	0x1c,	0x1,	0xe,	0xf,	0x39,	0xc,	0xd,	0x1a,
77 [0x30]	0x1b,	0x2b,	0x2b,	0x27,	0x28,	0x29,	0x33,	0x34,
78 [0x38]	0x35,	0x3a,	0x3b,	0x3c,	0x3d,	0x3e,	0x3f,	0x40,
79 [0x40]	0x41,	0x42,	0x43,	0x44,	0x57,	0x58,	0x63,	0x46,
80 [0x48]	0x77,	0x52,	0x47,	0x49,	0x53,	0x4f,	0x51,	0x4d,
81 [0x50]	0x4b,	0x50,	0x48,	0x45,	0x35,	0x37,	0x4a,	0x4e,
82 [0x58]	0x1c,	0x4f,	0x50,	0x51,	0x4b,	0x4c,	0x4d,	0x47,
83 [0x60]	0x48,	0x49,	0x52,	0x53,	0x56,	0x7f,	0x74,	0x75,
84 [0x68]	0x55,	0x59,	0x5a,	0x5b,	0x5c,	0x5d,	0x5e,	0x5f,
85 [0x70]	0x78,	0x79,	0x7a,	0x7b,	0x0,	0x0,	0x0,	0x0,
86 [0x78]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x71,
87 [0x80]	0x73,	0x72,	0x0,	0x0,	0x0,	0x7c,	0x0,	0x0,
88 [0x88]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
89 [0x90]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
90 [0x98]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
91 [0xa0]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
92 [0xa8]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
93 [0xb0]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
94 [0xb8]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
95 [0xc0]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
96 [0xc8]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
97 [0xd0]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
98 [0xd8]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
99 [0xe0]	0x1d,	0x2a,	0x38,	0x7d,	0x61,	0x36,	0x64,	0x7e,
100 [0xe8]	0x0,	0x0,	0x0,	0x0,	0x0,	0x73,	0x72,	0x71,
101 [0xf0]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
102 [0xf8]	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,	0x0,
103 };
104 
105 static QLock inlck;
106 static Kin kbdin =
107 {
108 	.ref = 0,
109 	.name = "#Ι/kbin",
110 	.fd = -1,
111 };
112 static Kin ptrin =
113 {
114 	.ref = 0,
115 	.name = "#m/mousein",
116 	.fd = -1,
117 };
118 
119 static int kbdebug;
120 
121 static int ptrbootpvals(KDev *kd, Chain *ch, int *px, int *py, int *pb);
122 static int ptrrepvals(KDev *kd, Chain *ch, int *px, int *py, int *pb);
123 
124 static int
125 setbootproto(KDev* f, int eid, uchar *, int)
126 {
127 	int r, id;
128 
129 	f->ptrvals = ptrbootpvals;
130 	r = Rh2d|Rclass|Riface;
131 	dprint(2, "setting boot protocol\n");
132 	id = f->dev->usb->ep[eid]->iface->id;
133 	return usbcmd(f->dev, r, Setproto, Bootproto, id, nil, 0);
134 }
135 
136 static uchar ignoredesc[128];
137 
138 static int
139 setfirstconfig(KDev* f, int eid, uchar *desc, int descsz)
140 {
141 	int nr, r, id, i;
142 
143 	dprint(2, "setting first config\n");
144 	if(desc == nil){
145 		descsz = sizeof ignoredesc;
146 		desc = ignoredesc;
147 	}
148 	id = f->dev->usb->ep[eid]->iface->id;
149 	r = Rh2d | Rstd | Rdev;
150 	nr =usbcmd(f->dev,  r, Rsetconf, 1, id, nil, 0);
151 	if(nr < 0)
152 		return -1;
153 	r = Rh2d | Rclass | Riface;
154 	nr=usbcmd(f->dev,   r, Setidle,  0, id, nil, 0);
155 	if(nr < 0)
156 		return -1;
157 	r = Rd2h | Rstd | Riface;
158 	nr=usbcmd(f->dev,  r, Rgetdesc, Dreport<<8, id, desc, descsz);
159 	if(nr < 0)
160 		return -1;
161 	if(kbdebug && nr > 0){
162 		fprint(2, "report descriptor:");
163 		for(i = 0; i < nr; i++){
164 			if(i%8 == 0)
165 				fprint(2, "\n\t");
166 			fprint(2, "%#2.2ux ", desc[i]);
167 		}
168 		fprint(2, "\n");
169 	}
170 	f->ptrvals = ptrrepvals;
171 	return nr;
172 }
173 
174 /*
175  * Try to recover from a babble error. A port reset is the only way out.
176  * BUG: we should be careful not to reset a bundle with several devices.
177  */
178 static void
179 recoverkb(KDev *f)
180 {
181 	int i;
182 
183 	close(f->dev->dfd);		/* it's for usbd now */
184 	devctl(f->dev, "reset");
185 	for(i = 0; i < 10; i++){
186 		if(i == 5)
187 			f->bootp++;
188 		sleep(500);
189 		if(opendevdata(f->dev, ORDWR) >= 0){
190 			if(f->bootp)
191 				/* TODO func pointer */
192 				setbootproto(f, f->ep->id, nil, 0);
193 			else
194 				setfirstconfig(f, f->ep->id, nil, 0);
195 			break;
196 		}
197 		/* else usbd still working... */
198 	}
199 }
200 
201 static void
202 kbfatal(KDev *kd, char *sts)
203 {
204 	Dev *dev;
205 
206 	if(sts != nil)
207 		fprint(2, "kb: fatal: %s\n", sts);
208 	else
209 		fprint(2, "kb: exiting\n");
210 	if(kd->repeatc != nil)
211 		nbsendul(kd->repeatc, Diemsg);
212 	dev = kd->dev;
213 	kd->dev = nil;
214 	if(kd->ep != nil)
215 		closedev(kd->ep);
216 	kd->ep = nil;
217 	devctl(dev, "detach");
218 	closedev(dev);
219 	/*
220 	 * free(kd); done by closedev.
221 	 */
222 	threadexits(sts);
223 }
224 
225 static int
226 scale(KDev *f, int x)
227 {
228 	int sign = 1;
229 
230 	if(x < 0){
231 		sign = -1;
232 		x = -x;
233 	}
234 	switch(x){
235 	case 0:
236 	case 1:
237 	case 2:
238 	case 3:
239 		break;
240 	case 4:
241 		x = 6 + (f->accel>>2);
242 		break;
243 	case 5:
244 		x = 9 + (f->accel>>1);
245 		break;
246 	default:
247 		x *= MaxAcc;
248 		break;
249 	}
250 	return sign*x;
251 }
252 
253 /*
254  * ps2 mouse is processed mostly at interrupt time.
255  * for usb we do what we can.
256  */
257 static void
258 sethipri(void)
259 {
260 	char fn[30];
261 	int fd;
262 
263 	snprint(fn, sizeof fn, "/proc/%d/ctl", getpid());
264 	fd = open(fn, OWRITE);
265 	if(fd >= 0) {
266 		fprint(fd, "pri 13");
267 		close(fd);
268 	}
269 }
270 
271 static int
272 ptrrepvals(KDev *kd, Chain *ch, int *px, int *py, int *pb)
273 {
274 	int i, x, y, b, c;
275 	static char buts[] = {0x0, 0x2, 0x1};
276 
277 	c = ch->e / 8;
278 	parsereport(&kd->templ, ch);
279 
280 	if(kbdebug)
281 		dumpreport(&kd->templ);
282 	if(c < 3)
283 		return -1;
284 	x = hidifcval(&kd->templ, KindX, 0);
285 	y = hidifcval(&kd->templ, KindY, 0);
286 	b = 0;
287 	for(i = 0; i<sizeof buts; i++)
288 		b |= (hidifcval(&kd->templ, KindButtons, i) & 1) << buts[i];
289 	if(c > 3 && hidifcval(&kd->templ, KindWheel, 0) > 0)	/* up */
290 		b |= 0x10;
291 	if(c > 3 && hidifcval(&kd->templ, KindWheel, 0) < 0)	/* down */
292 		b |= 0x08;
293 
294 	*px = x;
295 	*py = y;
296 	*pb = b;
297 	return 0;
298 }
299 
300 static int
301 ptrbootpvals(KDev *kd, Chain *ch, int *px, int *py, int *pb)
302 {
303 	int b, c;
304 	char x, y;
305 	static char maptab[] = {0x0, 0x1, 0x4, 0x5, 0x2, 0x3, 0x6, 0x7};
306 
307 	c = ch->e / 8;
308 	if(c < 3)
309 		return -1;
310 	x = hidifcval(&kd->templ, KindX, 0);
311 	y = hidifcval(&kd->templ, KindY, 0);
312 
313 	b = maptab[ch->buf[0] & 0x7];
314 	if(c > 3 && ch->buf[3] == 1)		/* up */
315 		b |= 0x08;
316 	if(c > 3 && ch->buf[3] == 0xff)		/* down */
317 		b |= 0x10;
318 	*px = x;
319 	*py = y;
320 	*pb = b;
321 	return 0;
322 }
323 
324 static void
325 ptrwork(void* a)
326 {
327 	int hipri, mfd, nerrs, x, y, b, c, ptrfd;
328 	char mbuf[80];
329 	Chain ch;
330 	KDev* f = a;
331 
332 	hipri = nerrs = 0;
333 	ptrfd = f->ep->dfd;
334 	mfd = f->in->fd;
335 	if(f->ep->maxpkt < 3 || f->ep->maxpkt > MaxChLen)
336 		kbfatal(f, "weird mouse maxpkt");
337 	for(;;){
338 		memset(ch.buf, 0, MaxChLen);
339 		if(f->ep == nil)
340 			kbfatal(f, nil);
341 		c = read(ptrfd, ch.buf, f->ep->maxpkt);
342 		assert(f->dev != nil);
343 		assert(f->ep != nil);
344 		if(c < 0){
345 			dprint(2, "kb: mouse: %s: read: %r\n", f->ep->dir);
346 			if(++nerrs < 3){
347 				recoverkb(f);
348 				continue;
349 			}
350 		}
351 		if(c <= 0)
352 			kbfatal(f, nil);
353 		ch.b = 0;
354 		ch.e = 8 * c;
355 		if(f->ptrvals(f, &ch, &x, &y, &b) < 0)
356 			continue;
357 
358 		if(f->accel){
359 			x = scale(f, x);
360 			y = scale(f, y);
361 		}
362 		if(kbdebug > 1)
363 			fprint(2, "kb: m%11d %11d %11d\n", x, y, b);
364 		seprint(mbuf, mbuf+sizeof(mbuf), "m%11d %11d %11d", x, y,b);
365 		if(write(mfd, mbuf, strlen(mbuf)) < 0)
366 			kbfatal(f, "mousein i/o");
367 		if(hipri == 0){
368 			sethipri();
369 			hipri = 1;
370 		}
371 	}
372 }
373 
374 static void
375 stoprepeat(KDev *f)
376 {
377 	sendul(f->repeatc, Awakemsg);
378 }
379 
380 static void
381 startrepeat(KDev *f, uchar esc1, uchar sc)
382 {
383 	ulong c;
384 
385 	if(esc1)
386 		c = SCesc1 << 8 | (sc & 0xff);
387 	else
388 		c = sc;
389 	sendul(f->repeatc, c);
390 }
391 
392 static void
393 putscan(int kbinfd, uchar esc, uchar sc)
394 {
395 	uchar s[2] = {SCesc1, 0};
396 
397 	if(sc == 0x41){
398 		kbdebug += 2;
399 		return;
400 	}
401 	if(sc == 0x42){
402 		kbdebug = 0;
403 		return;
404 	}
405 	if(kbdebug)
406 		fprint(2, "sc: %x %x\n", (esc? SCesc1: 0), sc);
407 	s[1] = sc;
408 	if(esc && sc != 0)
409 		write(kbinfd, s, 2);
410 	else if(sc != 0)
411 		write(kbinfd, s+1, 1);
412 }
413 
414 static void
415 repeatproc(void* a)
416 {
417 	KDev *f;
418 	Channel *repeatc;
419 	int kbdinfd;
420 	ulong l, t, i;
421 	uchar esc1, sc;
422 
423 	/*
424 	 * too many jumps here.
425 	 * Rewrite instead of debug, if needed.
426 	 */
427 	f = a;
428 	repeatc = f->repeatc;
429 	kbdinfd = f->in->fd;
430 	l = Awakemsg;
431 Repeat:
432 	if(l == Diemsg)
433 		goto Abort;
434 	while(l == Awakemsg)
435 		l = recvul(repeatc);
436 	if(l == Diemsg)
437 		goto Abort;
438 	esc1 = l >> 8;
439 	sc = l;
440 	t = 160;
441 	for(;;){
442 		for(i = 0; i < t; i += 5){
443 			if(l = nbrecvul(repeatc))
444 				goto Repeat;
445 			sleep(5);
446 		}
447 		putscan(kbdinfd, esc1, sc);
448 		t = 30;
449 	}
450 Abort:
451 	chanfree(repeatc);
452 	threadexits("aborted");
453 
454 }
455 
456 
457 #define hasesc1(sc)	(((sc) > 0x47) || ((sc) == 0x38))
458 
459 static void
460 putmod(int fd, uchar mods, uchar omods, uchar mask, uchar esc, uchar sc)
461 {
462 	/* BUG: Should be a single write */
463 	if((mods&mask) && !(omods&mask))
464 		putscan(fd, esc, sc);
465 	if(!(mods&mask) && (omods&mask))
466 		putscan(fd, esc, Keyup|sc);
467 }
468 
469 /*
470  * This routine diffs the state with the last known state
471  * and invents the scan codes that would have been sent
472  * by a non-usb keyboard in that case. This also requires supplying
473  * the extra esc1 byte as well as keyup flags.
474  * The aim is to allow future addition of other keycode pages
475  * for other keyboards.
476  */
477 static uchar
478 putkeys(KDev *f, uchar buf[], uchar obuf[], int n, uchar dk)
479 {
480 	int i, j;
481 	uchar uk;
482 	int fd;
483 
484 	fd = f->in->fd;
485 	putmod(fd, buf[0], obuf[0], Mctrl, 0, SCctrl);
486 	putmod(fd, buf[0], obuf[0], (1<<Mlshift), 0, SClshift);
487 	putmod(fd, buf[0], obuf[0], (1<<Mrshift), 0, SCrshift);
488 	putmod(fd, buf[0], obuf[0], Mcompose, 0, SCcompose);
489 	putmod(fd, buf[0], obuf[0], Maltgr, 1, SCcompose);
490 
491 	/* Report key downs */
492 	for(i = 2; i < n; i++){
493 		for(j = 2; j < n; j++)
494 			if(buf[i] == obuf[j])
495 			 	break;
496 		if(j == n && buf[i] != 0){
497 			dk = sctab[buf[i]];
498 			putscan(fd, hasesc1(dk), dk);
499 			startrepeat(f, hasesc1(dk), dk);
500 		}
501 	}
502 
503 	/* Report key ups */
504 	uk = 0;
505 	for(i = 2; i < n; i++){
506 		for(j = 2; j < n; j++)
507 			if(obuf[i] == buf[j])
508 				break;
509 		if(j == n && obuf[i] != 0){
510 			uk = sctab[obuf[i]];
511 			putscan(fd, hasesc1(uk), uk|Keyup);
512 		}
513 	}
514 	if(uk && (dk == 0 || dk == uk)){
515 		stoprepeat(f);
516 		dk = 0;
517 	}
518 	return dk;
519 }
520 
521 static int
522 kbdbusy(uchar* buf, int n)
523 {
524 	int i;
525 
526 	for(i = 1; i < n; i++)
527 		if(buf[i] == 0 || buf[i] != buf[0])
528 			return 0;
529 	return 1;
530 }
531 
532 static void
533 kbdwork(void *a)
534 {
535 	int c, i, kbdfd, nerrs;
536 	uchar dk, buf[64], lbuf[64];
537 	char err[128];
538 	KDev *f = a;
539 
540 	kbdfd = f->ep->dfd;
541 
542 	if(f->ep->maxpkt < 3 || f->ep->maxpkt > sizeof buf)
543 		kbfatal(f, "weird maxpkt");
544 
545 	f->repeatc = chancreate(sizeof(ulong), 0);
546 	if(f->repeatc == nil)
547 		kbfatal(f, "chancreate failed");
548 
549 	proccreate(repeatproc, f, Stack);
550 	memset(lbuf, 0, sizeof lbuf);
551 	dk = nerrs = 0;
552 	for(;;){
553 		memset(buf, 0, sizeof buf);
554 		c = read(kbdfd, buf, f->ep->maxpkt);
555 		assert(f->dev != nil);
556 		assert(f->ep != nil);
557 		if(c < 0){
558 			rerrstr(err, sizeof(err));
559 			fprint(2, "kb: %s: read: %s\n", f->ep->dir, err);
560 			if(strstr(err, "babble") != 0 && ++nerrs < 3){
561 				recoverkb(f);
562 				continue;
563 			}
564 		}
565 		if(c <= 0)
566 			kbfatal(f, nil);
567 		if(c < 3)
568 			continue;
569 		if(kbdbusy(buf + 2, c - 2))
570 			continue;
571 		if(usbdebug > 2 || kbdebug > 1){
572 			fprint(2, "kbd mod %x: ", buf[0]);
573 			for(i = 2; i < c; i++)
574 				fprint(2, "kc %x ", buf[i]);
575 			fprint(2, "\n");
576 		}
577 		dk = putkeys(f, buf, lbuf, f->ep->maxpkt, dk);
578 		memmove(lbuf, buf, c);
579 		nerrs = 0;
580 	}
581 }
582 
583 static void
584 freekdev(void *a)
585 {
586 	KDev *kd;
587 
588 	kd = a;
589 	if(kd->in != nil){
590 		qlock(&inlck);
591 		if(--kd->in->ref == 0){
592 			close(kd->in->fd);
593 			kd->in->fd = -1;
594 		}
595 		qunlock(&inlck);
596 	}
597 	dprint(2, "freekdev\n");
598 	free(kd);
599 }
600 
601 static void
602 kbstart(Dev *d, Ep *ep, Kin *in, void (*f)(void*), KDev *kd)
603 {
604 	uchar desc[128];
605 	int res;
606 
607 	qlock(&inlck);
608 	if(in->fd < 0){
609 		in->fd = open(in->name, OWRITE);
610 		if(in->fd < 0){
611 			fprint(2, "kb: %s: %r\n", in->name);
612 			qunlock(&inlck);
613 			return;
614 		}
615 	}
616 	in->ref++;	/* for kd->in = in */
617 	qunlock(&inlck);
618 	d->free = freekdev;
619 	kd->in = in;
620 	kd->dev = d;
621 	res = -1;
622 	if(!kd->bootp)
623 		res= setfirstconfig(kd, ep->id, desc, sizeof desc);
624 	if(res > 0)
625 		res = parsereportdesc(&kd->templ, desc, sizeof desc);
626 	/* if we could not set the first config, we give up */
627 	if(kd->bootp || res < 0){
628 		kd->bootp = 1;
629 		if(setbootproto(kd, ep->id, nil, 0) < 0){
630 			fprint(2, "kb: %s: bootproto: %r\n", d->dir);
631 			return;
632 		}
633 	}else if(kbdebug)
634 		dumpreport(&kd->templ);
635 	kd->ep = openep(d, ep->id);
636 	if(kd->ep == nil){
637 		fprint(2, "kb: %s: openep %d: %r\n", d->dir, ep->id);
638 		return;
639 	}
640 	if(opendevdata(kd->ep, OREAD) < 0){
641 		fprint(2, "kb: %s: opendevdata: %r\n", kd->ep->dir);
642 		closedev(kd->ep);
643 		kd->ep = nil;
644 		return;
645 	}
646 
647 	incref(d);
648 	proccreate(f, kd, Stack);
649 }
650 
651 static int
652 usage(void)
653 {
654 	werrstr("usage: usb/kb [-bdkm] [-a n] [-N nb]");
655 	return -1;
656 }
657 
658 int
659 kbmain(Dev *d, int argc, char* argv[])
660 {
661 	int bootp, i, kena, pena, accel, devid;
662 	Ep *ep;
663 	KDev *kd;
664 	Usbdev *ud;
665 
666 	kena = pena = 1;
667 	bootp = 0;
668 	accel = 0;
669 	devid = d->id;
670 	ARGBEGIN{
671 	case 'a':
672 		accel = strtol(EARGF(usage()), nil, 0);
673 		break;
674 	case 'd':
675 		kbdebug++;
676 		break;
677 	case 'k':
678 		kena = 1;
679 		pena = 0;
680 		break;
681 	case 'm':
682 		kena = 0;
683 		pena = 1;
684 		break;
685 	case 'N':
686 		devid = atoi(EARGF(usage()));		/* ignore dev number */
687 		break;
688 	case 'b':
689 		bootp++;
690 		break;
691 	default:
692 		return usage();
693 	}ARGEND;
694 	if(argc != 0)
695 		return usage();
696 	USED(devid);
697 	ud = d->usb;
698 	d->aux = nil;
699 	dprint(2, "kb: main: dev %s ref %ld\n", d->dir, d->ref);
700 
701 	if(kena)
702 		for(i = 0; i < nelem(ud->ep); i++)
703 			if((ep = ud->ep[i]) == nil)
704 				break;
705 			else if(ep->iface->csp == KbdCSP)
706 				bootp = 1;
707 
708 	for(i = 0; i < nelem(ud->ep); i++){
709 		if((ep = ud->ep[i]) == nil)
710 			break;
711 		if(kena && ep->type == Eintr && ep->dir == Ein &&
712 		    ep->iface->csp == KbdCSP){
713 			kd = d->aux = emallocz(sizeof(KDev), 1);
714 			kd->accel = 0;
715 			kd->bootp = 1;
716 			kbstart(d, ep, &kbdin, kbdwork, kd);
717 		}
718 		if(pena && ep->type == Eintr && ep->dir == Ein &&
719 		    ep->iface->csp == PtrCSP){
720 			kd = d->aux = emallocz(sizeof(KDev), 1);
721 			kd->accel = accel;
722 			kd->bootp = bootp;
723 			kbstart(d, ep, &ptrin, ptrwork, kd);
724 		}
725 	}
726 	return 0;
727 }
728