[f89979b] | 1 | /*
|
---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
| 3 | * Copyright (c) 2006 Josef Cejka
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[24ff4df] | 30 | /** @addtogroup kbd_port
|
---|
| 31 | * @ingroup kbd
|
---|
[f89979b] | 32 | * @{
|
---|
| 33 | */
|
---|
| 34 | /** @file
|
---|
[24ff4df] | 35 | * @brief i8042 port driver.
|
---|
[f89979b] | 36 | */
|
---|
| 37 |
|
---|
[ff685c9] | 38 | #include <ddi.h>
|
---|
| 39 | #include <libarch/ddi.h>
|
---|
[f89979b] | 40 | #include <ipc/ipc.h>
|
---|
| 41 | #include <async.h>
|
---|
| 42 | #include <unistd.h>
|
---|
| 43 | #include <sysinfo.h>
|
---|
| 44 | #include <kbd_port.h>
|
---|
| 45 | #include <kbd.h>
|
---|
| 46 | #include "i8042.h"
|
---|
| 47 |
|
---|
| 48 | /* Interesting bits for status register */
|
---|
| 49 | #define i8042_OUTPUT_FULL 0x1
|
---|
| 50 | #define i8042_INPUT_FULL 0x2
|
---|
| 51 | #define i8042_MOUSE_DATA 0x20
|
---|
| 52 |
|
---|
| 53 | /* Command constants */
|
---|
| 54 | #define i8042_CMD_KBD 0x60
|
---|
| 55 | #define i8042_CMD_MOUSE 0xd4
|
---|
| 56 |
|
---|
| 57 | /* Keyboard cmd byte */
|
---|
| 58 | #define i8042_KBD_IE 0x1
|
---|
| 59 | #define i8042_MOUSE_IE 0x2
|
---|
| 60 | #define i8042_KBD_DISABLE 0x10
|
---|
| 61 | #define i8042_MOUSE_DISABLE 0x20
|
---|
| 62 | #define i8042_KBD_TRANSLATE 0x40
|
---|
| 63 |
|
---|
| 64 | /* Mouse constants */
|
---|
| 65 | #define MOUSE_OUT_INIT 0xf4
|
---|
| 66 | #define MOUSE_ACK 0xfa
|
---|
| 67 |
|
---|
[cecb0789] | 68 | static irq_cmd_t i8042_cmds[] = {
|
---|
| 69 | {
|
---|
| 70 | .cmd = CMD_PIO_READ_8,
|
---|
[ff685c9] | 71 | .addr = NULL, /* will be patched in run-time */
|
---|
[cecb0789] | 72 | .dstarg = 1
|
---|
| 73 | },
|
---|
| 74 | {
|
---|
| 75 | .cmd = CMD_BTEST,
|
---|
| 76 | .value = i8042_OUTPUT_FULL,
|
---|
| 77 | .srcarg = 1,
|
---|
| 78 | .dstarg = 3
|
---|
| 79 | },
|
---|
| 80 | {
|
---|
| 81 | .cmd = CMD_PREDICATE,
|
---|
| 82 | .value = 2,
|
---|
| 83 | .srcarg = 3
|
---|
| 84 | },
|
---|
| 85 | {
|
---|
| 86 | .cmd = CMD_PIO_READ_8,
|
---|
[ff685c9] | 87 | .addr = NULL, /* will be patched in run-time */
|
---|
[cecb0789] | 88 | .dstarg = 2
|
---|
| 89 | },
|
---|
| 90 | {
|
---|
| 91 | .cmd = CMD_ACCEPT
|
---|
| 92 | }
|
---|
[f89979b] | 93 | };
|
---|
| 94 |
|
---|
| 95 | static irq_code_t i8042_kbd = {
|
---|
[cecb0789] | 96 | sizeof(i8042_cmds) / sizeof(irq_cmd_t),
|
---|
[f89979b] | 97 | i8042_cmds
|
---|
| 98 | };
|
---|
| 99 |
|
---|
[ff685c9] | 100 | static uintptr_t i8042_physical;
|
---|
| 101 | static uintptr_t i8042_kernel;
|
---|
| 102 | static i8042_t * i8042;
|
---|
| 103 |
|
---|
[f89979b] | 104 | static void wait_ready(void) {
|
---|
[ff685c9] | 105 | while (pio_read_8(&i8042->status) & i8042_INPUT_FULL)
|
---|
| 106 | ;
|
---|
[f89979b] | 107 | }
|
---|
| 108 |
|
---|
| 109 | static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call);
|
---|
| 110 |
|
---|
| 111 | int kbd_port_init(void)
|
---|
| 112 | {
|
---|
| 113 | int mouseenabled = 0;
|
---|
[ff685c9] | 114 | void *vaddr;
|
---|
| 115 |
|
---|
| 116 | i8042_physical = sysinfo_value("kbd.address.physical");
|
---|
| 117 | i8042_kernel = sysinfo_value("kbd.address.kernel");
|
---|
| 118 | if (pio_enable((void *) i8042_physical, sizeof(i8042_t), &vaddr) != 0)
|
---|
| 119 | return -1;
|
---|
| 120 | i8042 = vaddr;
|
---|
[f89979b] | 121 |
|
---|
| 122 | async_set_interrupt_received(i8042_irq_handler);
|
---|
| 123 |
|
---|
| 124 | /* Disable kbd, enable mouse */
|
---|
[ff685c9] | 125 | pio_write_8(&i8042->status, i8042_CMD_KBD);
|
---|
[f89979b] | 126 | wait_ready();
|
---|
[ff685c9] | 127 | pio_write_8(&i8042->status, i8042_CMD_KBD);
|
---|
[f89979b] | 128 | wait_ready();
|
---|
[ff685c9] | 129 | pio_write_8(&i8042->data, i8042_KBD_DISABLE);
|
---|
[f89979b] | 130 | wait_ready();
|
---|
| 131 |
|
---|
| 132 | /* Flush all current IO */
|
---|
[ff685c9] | 133 | while (pio_read_8(&i8042->status) & i8042_OUTPUT_FULL)
|
---|
| 134 | (void) pio_read_8(&i8042->data);
|
---|
[f89979b] | 135 |
|
---|
| 136 | /* Enable kbd */
|
---|
[ff685c9] | 137 | i8042_kbd.cmds[0].addr = &((i8042_t *) i8042_kernel)->status;
|
---|
| 138 | i8042_kbd.cmds[3].addr = &((i8042_t *) i8042_kernel)->data;
|
---|
[f89979b] | 139 | ipc_register_irq(sysinfo_value("kbd.inr"), sysinfo_value("kbd.devno"), 0, &i8042_kbd);
|
---|
| 140 |
|
---|
| 141 | int newcontrol = i8042_KBD_IE | i8042_KBD_TRANSLATE;
|
---|
| 142 | if (mouseenabled)
|
---|
| 143 | newcontrol |= i8042_MOUSE_IE;
|
---|
| 144 |
|
---|
[ff685c9] | 145 | pio_write_8(&i8042->status, i8042_CMD_KBD);
|
---|
[f89979b] | 146 | wait_ready();
|
---|
[ff685c9] | 147 | pio_write_8(&i8042->data, newcontrol);
|
---|
[f89979b] | 148 | wait_ready();
|
---|
| 149 |
|
---|
| 150 | return 0;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call)
|
---|
| 154 | {
|
---|
| 155 | int status = IPC_GET_ARG1(*call);
|
---|
| 156 |
|
---|
| 157 | if ((status & i8042_MOUSE_DATA))
|
---|
[ff685c9] | 158 | return;
|
---|
[f89979b] | 159 |
|
---|
| 160 | int scan_code = IPC_GET_ARG2(*call);
|
---|
| 161 |
|
---|
| 162 | kbd_push_scancode(scan_code);
|
---|
[ff685c9] | 163 | return;
|
---|
[f89979b] | 164 | }
|
---|
| 165 |
|
---|
| 166 | /**
|
---|
| 167 | * @}
|
---|
| 168 | */
|
---|