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 (*auxputc)(int, int);
54 static int nokbd = 1; /* flag: no PS/2 keyboard */
62 * wait for output no longer busy
69 for(tries = 0; (inb(Status) & Outbusy); tries++){
85 for(tries = 0; !(inb(Status) & Inready); tries++){
94 * ask 8042 to reset the machine
104 *((ushort*)KADDR(0x472)) = 0x1234; /* BIOS warm-boot flag */
107 * newer reset the machine command
114 * Pulse it by hand (old somewhat reliable)
117 for(i = 0; i < 5; i++){
122 outb(Data, x); /* toggle reset */
151 } while(c == 0xFE || c == 0);
155 print("i8042: %2.2ux returned to the %2.2ux command (pc=%#p)\n",
156 c, cmd, getcallerpc(&cmd));
163 * set keyboard's leds for lock states (scroll, numeric, caps).
165 * at least one keyboard (from Qtronics) also sets its numeric-lock
166 * behaviour to match the led state, though it has no numeric keypad,
167 * and some BIOSes bring the system up with numeric-lock set and no
168 * setting to change that. this combination steals the keys for these
169 * characters and makes it impossible to generate them: uiolkjm&*().
170 * thus we'd like to be able to force the numeric-lock led (and behaviour) off.
177 if(nokbd || leds == old)
184 outb(Data, 0xed); /* `reset keyboard lock states' */
200 i8042intr(Ureg*, void*)
222 * if it's the aux port...
231 qproduce(kbd.q, &b, 1);
235 i8042auxenable(void (*putc)(int, int))
237 static char err[] = "i8042: aux init failed\n";
241 /* enable kbd/aux xfers and interrupts */
247 outb(Cmd, 0x60); /* write control register */
253 outb(Cmd, 0xA8); /* auxiliary device enable */
260 intrenable(IrqAUX, i8042intr, 0, BUSUNKNOWN, "kbdaux");
268 if(nokbd || qlen(kbd.q) > 0)
278 /* disable kbd and aux xfers and interrupts */
279 ccc &= ~(Ckbdint|Cauxint);
280 ccc |= (Cauxdis|Ckbddis);
289 kbdattach(char *spec)
291 return devattach(L'b', spec);
295 kbdwalk(Chan *c, Chan *nc, char **name, int nname)
297 return devwalk(c, nc, name, nname, kbdtab, nelem(kbdtab), devgen);
301 kbdstat(Chan *c, uchar *dp, int n)
303 return devstat(c, dp, n, kbdtab, nelem(kbdtab), devgen);
307 kbdopen(Chan *c, int omode)
311 if(c->qid.path == Qscancode){
316 if(incref(&kbd.ref) != 1)
318 c = devopen(c, omode, kbdtab, nelem(kbdtab), devgen);
322 return devopen(c, omode, kbdtab, nelem(kbdtab), devgen);
328 if((c->flag & COPEN) && c->qid.path == Qscancode)
333 kbdbread(Chan *c, long n, ulong off)
335 if(c->qid.path == Qscancode){
337 return qbread(kbd.q, n);
339 return devbread(c, n, off);
343 kbdread(Chan *c, void *a, long n, vlong)
345 if(c->qid.path == Qscancode){
347 return qread(kbd.q, a, n);
349 if(c->qid.path == Qdir)
350 return devdirread(c, a, n, kbdtab, nelem(kbdtab), devgen);
356 kbdwrite(Chan *c, void *a, long n, vlong)
360 if(c->qid.path != Qleds)
365 p = tmp + sizeof(tmp)-1;
366 memmove(tmp, a, p - tmp);
377 static char initfailed[] = "i8042: kbd init failed\n";
380 kbd.q = qopen(1024, Qcoalesce, 0, 0);
385 /* wait for a quiescent controller */
387 while(try-- > 0 && (c = inb(Status)) & (Outbusy | Inready)) {
397 /* get current controller command byte */
400 print("i8042: can't read ccc\n");
405 /* enable kbd xfers and interrupts */
407 ccc |= Csf | Ckbdint | Cscs1;
409 /* disable ps2 mouse */
423 ioalloc(Cmd, 1, 0, "i8042.cs");
424 ioalloc(Data, 1, 0, "i8042.data");
425 intrenable(IrqKBD, i8042intr, 0, BUSUNKNOWN, "kbd");