| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 3 | * Copyright (c) 2006 Josef Cejka
|
|---|
| 4 | * Copyright (c) 2009 Jiri Svoboda
|
|---|
| 5 | * All rights reserved.
|
|---|
| 6 | *
|
|---|
| 7 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 8 | * modification, are permitted provided that the following conditions
|
|---|
| 9 | * are met:
|
|---|
| 10 | *
|
|---|
| 11 | * - Redistributions of source code must retain the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 13 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 14 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 15 | * documentation and/or other materials provided with the distribution.
|
|---|
| 16 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 17 | * derived from this software without specific prior written permission.
|
|---|
| 18 | *
|
|---|
| 19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 20 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 21 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 22 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 23 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 24 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 29 | */
|
|---|
| 30 |
|
|---|
| 31 | /** @addtogroup kbd_port
|
|---|
| 32 | * @ingroup kbd
|
|---|
| 33 | * @{
|
|---|
| 34 | */
|
|---|
| 35 | /** @file
|
|---|
| 36 | * @brief i8042 PS/2 port driver.
|
|---|
| 37 | */
|
|---|
| 38 |
|
|---|
| 39 | #include <ddi.h>
|
|---|
| 40 | #include <libarch/ddi.h>
|
|---|
| 41 | #include <devmap.h>
|
|---|
| 42 | #include <async.h>
|
|---|
| 43 | #include <async_obsolete.h>
|
|---|
| 44 | #include <unistd.h>
|
|---|
| 45 | #include <sysinfo.h>
|
|---|
| 46 | #include <stdio.h>
|
|---|
| 47 | #include <errno.h>
|
|---|
| 48 | #include <inttypes.h>
|
|---|
| 49 | #include "i8042.h"
|
|---|
| 50 |
|
|---|
| 51 | // FIXME: remove this header
|
|---|
| 52 | #include <kernel/ipc/ipc_methods.h>
|
|---|
| 53 |
|
|---|
| 54 | #define NAME "i8042"
|
|---|
| 55 | #define NAMESPACE "char"
|
|---|
| 56 |
|
|---|
| 57 | /* Interesting bits for status register */
|
|---|
| 58 | #define i8042_OUTPUT_FULL 0x01
|
|---|
| 59 | #define i8042_INPUT_FULL 0x02
|
|---|
| 60 | #define i8042_AUX_DATA 0x20
|
|---|
| 61 |
|
|---|
| 62 | /* Command constants */
|
|---|
| 63 | #define i8042_CMD_WRITE_CMDB 0x60 /**< write command byte */
|
|---|
| 64 | #define i8042_CMD_WRITE_AUX 0xd4 /**< write aux device */
|
|---|
| 65 |
|
|---|
| 66 | /* Command byte fields */
|
|---|
| 67 | #define i8042_KBD_IE 0x01
|
|---|
| 68 | #define i8042_AUX_IE 0x02
|
|---|
| 69 | #define i8042_KBD_DISABLE 0x10
|
|---|
| 70 | #define i8042_AUX_DISABLE 0x20
|
|---|
| 71 | #define i8042_KBD_TRANSLATE 0x40
|
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 | enum {
|
|---|
| 75 | DEVID_PRI = 0, /**< primary device */
|
|---|
| 76 | DEVID_AUX = 1, /**< AUX device */
|
|---|
| 77 | MAX_DEVS = 2
|
|---|
| 78 | };
|
|---|
| 79 |
|
|---|
| 80 | static irq_cmd_t i8042_cmds[] = {
|
|---|
| 81 | {
|
|---|
| 82 | .cmd = CMD_PIO_READ_8,
|
|---|
| 83 | .addr = NULL, /* will be patched in run-time */
|
|---|
| 84 | .dstarg = 1
|
|---|
| 85 | },
|
|---|
| 86 | {
|
|---|
| 87 | .cmd = CMD_BTEST,
|
|---|
| 88 | .value = i8042_OUTPUT_FULL,
|
|---|
| 89 | .srcarg = 1,
|
|---|
| 90 | .dstarg = 3
|
|---|
| 91 | },
|
|---|
| 92 | {
|
|---|
| 93 | .cmd = CMD_PREDICATE,
|
|---|
| 94 | .value = 2,
|
|---|
| 95 | .srcarg = 3
|
|---|
| 96 | },
|
|---|
| 97 | {
|
|---|
| 98 | .cmd = CMD_PIO_READ_8,
|
|---|
| 99 | .addr = NULL, /* will be patched in run-time */
|
|---|
| 100 | .dstarg = 2
|
|---|
| 101 | },
|
|---|
| 102 | {
|
|---|
| 103 | .cmd = CMD_ACCEPT
|
|---|
| 104 | }
|
|---|
| 105 | };
|
|---|
| 106 |
|
|---|
| 107 | static irq_code_t i8042_kbd = {
|
|---|
| 108 | sizeof(i8042_cmds) / sizeof(irq_cmd_t),
|
|---|
| 109 | i8042_cmds
|
|---|
| 110 | };
|
|---|
| 111 |
|
|---|
| 112 | static uintptr_t i8042_physical;
|
|---|
| 113 | static uintptr_t i8042_kernel;
|
|---|
| 114 | static i8042_t * i8042;
|
|---|
| 115 |
|
|---|
| 116 | static i8042_port_t i8042_port[MAX_DEVS];
|
|---|
| 117 |
|
|---|
| 118 | static void wait_ready(void)
|
|---|
| 119 | {
|
|---|
| 120 | while (pio_read_8(&i8042->status) & i8042_INPUT_FULL);
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call);
|
|---|
| 124 | static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall);
|
|---|
| 125 | static int i8042_init(void);
|
|---|
| 126 | static void i8042_port_write(int devid, uint8_t data);
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | int main(int argc, char *argv[])
|
|---|
| 130 | {
|
|---|
| 131 | char name[16];
|
|---|
| 132 | int i, rc;
|
|---|
| 133 | char dchar[MAX_DEVS] = { 'a', 'b' };
|
|---|
| 134 |
|
|---|
| 135 | printf(NAME ": i8042 PS/2 port driver\n");
|
|---|
| 136 |
|
|---|
| 137 | rc = devmap_driver_register(NAME, i8042_connection);
|
|---|
| 138 | if (rc < 0) {
|
|---|
| 139 | printf(NAME ": Unable to register driver.\n");
|
|---|
| 140 | return rc;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | if (i8042_init() != EOK)
|
|---|
| 144 | return -1;
|
|---|
| 145 |
|
|---|
| 146 | for (i = 0; i < MAX_DEVS; i++) {
|
|---|
| 147 | i8042_port[i].client_phone = -1;
|
|---|
| 148 |
|
|---|
| 149 | snprintf(name, 16, "%s/ps2%c", NAMESPACE, dchar[i]);
|
|---|
| 150 | rc = devmap_device_register(name, &i8042_port[i].devmap_handle);
|
|---|
| 151 | if (rc != EOK) {
|
|---|
| 152 | printf(NAME ": Unable to register device %s.\n", name);
|
|---|
| 153 | return rc;
|
|---|
| 154 | }
|
|---|
| 155 | printf(NAME ": Registered device %s\n", name);
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | printf(NAME ": Accepting connections\n");
|
|---|
| 159 | task_retval(0);
|
|---|
| 160 | async_manager();
|
|---|
| 161 |
|
|---|
| 162 | /* Not reached */
|
|---|
| 163 | return 0;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | static int i8042_init(void)
|
|---|
| 167 | {
|
|---|
| 168 | if (sysinfo_get_value("i8042.address.physical", &i8042_physical) != EOK)
|
|---|
| 169 | return -1;
|
|---|
| 170 |
|
|---|
| 171 | if (sysinfo_get_value("i8042.address.kernel", &i8042_kernel) != EOK)
|
|---|
| 172 | return -1;
|
|---|
| 173 |
|
|---|
| 174 | void *vaddr;
|
|---|
| 175 | if (pio_enable((void *) i8042_physical, sizeof(i8042_t), &vaddr) != 0)
|
|---|
| 176 | return -1;
|
|---|
| 177 |
|
|---|
| 178 | i8042 = vaddr;
|
|---|
| 179 |
|
|---|
| 180 | sysarg_t inr_a;
|
|---|
| 181 | sysarg_t inr_b;
|
|---|
| 182 |
|
|---|
| 183 | if (sysinfo_get_value("i8042.inr_a", &inr_a) != EOK)
|
|---|
| 184 | return -1;
|
|---|
| 185 |
|
|---|
| 186 | if (sysinfo_get_value("i8042.inr_b", &inr_b) != EOK)
|
|---|
| 187 | return -1;
|
|---|
| 188 |
|
|---|
| 189 | async_set_interrupt_received(i8042_irq_handler);
|
|---|
| 190 |
|
|---|
| 191 | /* Disable kbd and aux */
|
|---|
| 192 | wait_ready();
|
|---|
| 193 | pio_write_8(&i8042->status, i8042_CMD_WRITE_CMDB);
|
|---|
| 194 | wait_ready();
|
|---|
| 195 | pio_write_8(&i8042->data, i8042_KBD_DISABLE | i8042_AUX_DISABLE);
|
|---|
| 196 |
|
|---|
| 197 | /* Flush all current IO */
|
|---|
| 198 | while (pio_read_8(&i8042->status) & i8042_OUTPUT_FULL)
|
|---|
| 199 | (void) pio_read_8(&i8042->data);
|
|---|
| 200 |
|
|---|
| 201 | i8042_kbd.cmds[0].addr = (void *) &((i8042_t *) i8042_kernel)->status;
|
|---|
| 202 | i8042_kbd.cmds[3].addr = (void *) &((i8042_t *) i8042_kernel)->data;
|
|---|
| 203 | register_irq(inr_a, device_assign_devno(), 0, &i8042_kbd);
|
|---|
| 204 | register_irq(inr_b, device_assign_devno(), 0, &i8042_kbd);
|
|---|
| 205 | printf("%s: registered for interrupts %" PRIun " and %" PRIun "\n",
|
|---|
| 206 | NAME, inr_a, inr_b);
|
|---|
| 207 |
|
|---|
| 208 | wait_ready();
|
|---|
| 209 | pio_write_8(&i8042->status, i8042_CMD_WRITE_CMDB);
|
|---|
| 210 | wait_ready();
|
|---|
| 211 | pio_write_8(&i8042->data, i8042_KBD_IE | i8042_KBD_TRANSLATE |
|
|---|
| 212 | i8042_AUX_IE);
|
|---|
| 213 |
|
|---|
| 214 | return 0;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /** Character device connection handler */
|
|---|
| 218 | static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 219 | {
|
|---|
| 220 | ipc_callid_t callid;
|
|---|
| 221 | ipc_call_t call;
|
|---|
| 222 | sysarg_t method;
|
|---|
| 223 | devmap_handle_t dh;
|
|---|
| 224 | int retval;
|
|---|
| 225 | int dev_id, i;
|
|---|
| 226 |
|
|---|
| 227 | printf(NAME ": connection handler\n");
|
|---|
| 228 |
|
|---|
| 229 | /* Get the device handle. */
|
|---|
| 230 | dh = IPC_GET_ARG1(*icall);
|
|---|
| 231 |
|
|---|
| 232 | /* Determine which disk device is the client connecting to. */
|
|---|
| 233 | dev_id = -1;
|
|---|
| 234 | for (i = 0; i < MAX_DEVS; i++) {
|
|---|
| 235 | if (i8042_port[i].devmap_handle == dh)
|
|---|
| 236 | dev_id = i;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | if (dev_id < 0) {
|
|---|
| 240 | async_answer_0(iid, EINVAL);
|
|---|
| 241 | return;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
|---|
| 245 | async_answer_0(iid, EOK);
|
|---|
| 246 |
|
|---|
| 247 | printf(NAME ": accepted connection\n");
|
|---|
| 248 |
|
|---|
| 249 | while (1) {
|
|---|
| 250 | callid = async_get_call(&call);
|
|---|
| 251 | method = IPC_GET_IMETHOD(call);
|
|---|
| 252 |
|
|---|
| 253 | if (!method) {
|
|---|
| 254 | /* The other side has hung up. */
|
|---|
| 255 | async_answer_0(callid, EOK);
|
|---|
| 256 | return;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | switch (method) {
|
|---|
| 260 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 261 | printf(NAME ": creating callback connection\n");
|
|---|
| 262 | if (i8042_port[dev_id].client_phone != -1) {
|
|---|
| 263 | retval = ELIMIT;
|
|---|
| 264 | break;
|
|---|
| 265 | }
|
|---|
| 266 | i8042_port[dev_id].client_phone = IPC_GET_ARG5(call);
|
|---|
| 267 | retval = 0;
|
|---|
| 268 | break;
|
|---|
| 269 | case IPC_FIRST_USER_METHOD:
|
|---|
| 270 | printf(NAME ": write %" PRIun " to devid %d\n",
|
|---|
| 271 | IPC_GET_ARG1(call), dev_id);
|
|---|
| 272 | i8042_port_write(dev_id, IPC_GET_ARG1(call));
|
|---|
| 273 | retval = 0;
|
|---|
| 274 | break;
|
|---|
| 275 | default:
|
|---|
| 276 | retval = EINVAL;
|
|---|
| 277 | break;
|
|---|
| 278 | }
|
|---|
| 279 | async_answer_0(callid, retval);
|
|---|
| 280 | }
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | void i8042_port_write(int devid, uint8_t data)
|
|---|
| 284 | {
|
|---|
| 285 | if (devid == DEVID_AUX) {
|
|---|
| 286 | wait_ready();
|
|---|
| 287 | pio_write_8(&i8042->status, i8042_CMD_WRITE_AUX);
|
|---|
| 288 | }
|
|---|
| 289 | wait_ready();
|
|---|
| 290 | pio_write_8(&i8042->data, data);
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 294 | {
|
|---|
| 295 | int status, data;
|
|---|
| 296 | int devid;
|
|---|
| 297 |
|
|---|
| 298 | status = IPC_GET_ARG1(*call);
|
|---|
| 299 | data = IPC_GET_ARG2(*call);
|
|---|
| 300 |
|
|---|
| 301 | if ((status & i8042_AUX_DATA)) {
|
|---|
| 302 | devid = DEVID_AUX;
|
|---|
| 303 | } else {
|
|---|
| 304 | devid = DEVID_PRI;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | if (i8042_port[devid].client_phone != -1) {
|
|---|
| 308 | async_obsolete_msg_1(i8042_port[devid].client_phone,
|
|---|
| 309 | IPC_FIRST_USER_METHOD, data);
|
|---|
| 310 | }
|
|---|
| 311 | }
|
|---|
| 312 |
|
|---|
| 313 | /**
|
|---|
| 314 | * @}
|
|---|
| 315 | */
|
|---|