5 #include "../port/lib.h"
10 #include "../port/error.h"
13 Data= 0x60, /* data port */
15 Status= 0x64, /* status port */
16 Inready= 0x01, /* input character ready */
17 Outbusy= 0x02, /* output busy */
18 Sysflag= 0x04, /* system flag */
19 Cmddata= 0x08, /* cmd==0, data==1 */
20 Inhibit= 0x10, /* keyboard/mouse inhibited */
21 Minready= 0x20, /* mouse character ready */
22 Rtimeout= 0x40, /* general timeout */
25 Cmd= 0x64, /* command port (write only) */
30 /* controller command byte */
31 Cscs1= (1<<6), /* scan code set 1 */
32 Cauxdis= (1<<5), /* mouse disable */
33 Ckbddis= (1<<4), /* kbd disable */
34 Csf= (1<<2), /* system flag */
35 Cauxint= (1<<1), /* mouse interrupt enable */
36 Ckbdint= (1<<0), /* kbd interrupt enable */
45 static Dirtab kbdtab[] = {
46 ".", {Qdir, 0, QTDIR}, 0, 0555,
47 "scancode", {Qscancode, 0}, 0, 0440,
48 "leds", {Qleds, 0}, 0, 0220,
51 static Lock i8042lock;
53 static void kbdputc(int);
54 static void (*auxputc)(int, int);
55 static int nokbd = 1; /* flag: no PS/2 keyboard */
63 * wait for output no longer busy
70 for(tries = 0; (inb(Status) & Outbusy); tries++){
86 for(tries = 0; !(inb(Status) & Inready); tries++){
95 * ask 8042 to reset the machine
105 *((ushort*)KADDR(0x472)) = 0x1234; /* BIOS warm-boot flag */
108 * newer reset the machine command
115 * Pulse it by hand (old somewhat reliable)
118 for(i = 0; i < 5; i++){
123 outb(Data, x); /* toggle reset */
152 } while(c == 0xFE || c == 0);
156 print("i8042: %2.2ux returned to the %2.2ux command (pc=%#p)\n", c, cmd, getcallerpc(&cmd));
163 i8042auxcmds(uchar *cmd, int ncmd)
168 for(i=0; i<ncmd; i++){
181 * set keyboard's leds for lock states (scroll, numeric, caps).
183 * at least one keyboard (from Qtronics) also sets its numeric-lock
184 * behaviour to match the led state, though it has no numeric keypad,
185 * and some BIOSes bring the system up with numeric-lock set and no
186 * setting to change that. this combination steals the keys for these
187 * characters and makes it impossible to generate them: uiolkjm&*().
188 * thus we'd like to be able to force the numeric-lock led (and behaviour) off.
195 if(nokbd || leds == old)
202 outb(Data, 0xed); /* `reset keyboard lock states' */
218 i8042intr(Ureg*, void*)
240 * if it's the aux port...
249 qproduce(kbd.q, &b, 1);
253 i8042auxenable(void (*putc)(int, int))
255 char *err = "i8042: aux init failed\n";
257 /* enable kbd/aux xfers and interrupts */
264 outb(Cmd, 0x60); /* write control register */
270 outb(Cmd, 0xA8); /* auxiliary device enable */
277 intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
282 kbdattach(char *spec)
284 return devattach(L'b', spec);
288 kbdwalk(Chan *c, Chan *nc, char **name, int nname)
290 return devwalk(c, nc, name, nname, kbdtab, nelem(kbdtab), devgen);
294 kbdstat(Chan *c, uchar *dp, int n)
296 return devstat(c, dp, n, kbdtab, nelem(kbdtab), devgen);
300 kbdopen(Chan *c, int omode)
304 if(c->qid.path == Qscancode){
309 if(incref(&kbd.ref) != 1)
311 c = devopen(c, omode, kbdtab, nelem(kbdtab), devgen);
315 return devopen(c, omode, kbdtab, nelem(kbdtab), devgen);
321 if((c->flag & COPEN) && c->qid.path == Qscancode)
326 kbdbread(Chan *c, long n, ulong off)
328 if(c->qid.path == Qscancode)
329 return qbread(kbd.q, n);
331 return devbread(c, n, off);
335 kbdread(Chan *c, void *a, long n, vlong)
337 if(c->qid.path == Qscancode)
338 return qread(kbd.q, a, n);
339 if(c->qid.path == Qdir)
340 return devdirread(c, a, n, kbdtab, nelem(kbdtab), devgen);
347 kbdwrite(Chan *c, void *a, long n, vlong)
351 if(c->qid.path != Qleds)
356 p = tmp + sizeof(tmp)-1;
357 memmove(tmp, a, p - tmp);
387 static char *initfailed = "i8042: kbdinit failed\n";
390 outbyte(int port, int c)
403 kbd.q = qopen(1024, Qcoalesce, 0, 0);
408 ioalloc(Data, 1, 0, "kbd");
409 ioalloc(Cmd, 1, 0, "kbd");
411 intrenable(IrqKBD, i8042intr, 0, BUSUNKNOWN, "kbd");
419 /* wait for a quiescent controller */
421 while(try-- > 0 && (c = inb(Status)) & (Outbusy | Inready)) {
431 /* get current controller command byte */
434 print("i8042: kbdinit can't read ccc\n");
439 /* enable kbd xfers and interrupts */
441 ccc |= Csf | Ckbdint | Cscs1;
450 if (outbyte(Cmd, 0x60) < 0 || outbyte(Data, ccc) < 0)
451 print("i8042: kbdinit mouse disable failed\n");