| 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 <unistd.h>
|
|---|
| 44 | #include <sysinfo.h>
|
|---|
| 45 | #include <stdio.h>
|
|---|
| 46 | #include <errno.h>
|
|---|
| 47 | #include <inttypes.h>
|
|---|
| 48 |
|
|---|
| 49 | #include "i8042.h"
|
|---|
| 50 |
|
|---|
| 51 | #define NAME "i8042"
|
|---|
| 52 | #define NAMESPACE "char"
|
|---|
| 53 |
|
|---|
| 54 | /* Interesting bits for status register */
|
|---|
| 55 | #define i8042_OUTPUT_FULL 0x01
|
|---|
| 56 | #define i8042_INPUT_FULL 0x02
|
|---|
| 57 | #define i8042_AUX_DATA 0x20
|
|---|
| 58 |
|
|---|
| 59 | /* Command constants */
|
|---|
| 60 | #define i8042_CMD_WRITE_CMDB 0x60 /**< write command byte */
|
|---|
| 61 | #define i8042_CMD_WRITE_AUX 0xd4 /**< write aux device */
|
|---|
| 62 |
|
|---|
| 63 | /* Command byte fields */
|
|---|
| 64 | #define i8042_KBD_IE 0x01
|
|---|
| 65 | #define i8042_AUX_IE 0x02
|
|---|
| 66 | #define i8042_KBD_DISABLE 0x10
|
|---|
| 67 | #define i8042_AUX_DISABLE 0x20
|
|---|
| 68 | #define i8042_KBD_TRANSLATE 0x40
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | enum {
|
|---|
| 72 | DEVID_PRI = 0, /**< primary device */
|
|---|
| 73 | DEVID_AUX = 1, /**< AUX device */
|
|---|
| 74 | MAX_DEVS = 2
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | static irq_cmd_t i8042_cmds[] = {
|
|---|
| 78 | {
|
|---|
| 79 | .cmd = CMD_PIO_READ_8,
|
|---|
| 80 | .addr = NULL, /* will be patched in run-time */
|
|---|
| 81 | .dstarg = 1
|
|---|
| 82 | },
|
|---|
| 83 | {
|
|---|
| 84 | .cmd = CMD_BTEST,
|
|---|
| 85 | .value = i8042_OUTPUT_FULL,
|
|---|
| 86 | .srcarg = 1,
|
|---|
| 87 | .dstarg = 3
|
|---|
| 88 | },
|
|---|
| 89 | {
|
|---|
| 90 | .cmd = CMD_PREDICATE,
|
|---|
| 91 | .value = 2,
|
|---|
| 92 | .srcarg = 3
|
|---|
| 93 | },
|
|---|
| 94 | {
|
|---|
| 95 | .cmd = CMD_PIO_READ_8,
|
|---|
| 96 | .addr = NULL, /* will be patched in run-time */
|
|---|
| 97 | .dstarg = 2
|
|---|
| 98 | },
|
|---|
| 99 | {
|
|---|
| 100 | .cmd = CMD_ACCEPT
|
|---|
| 101 | }
|
|---|
| 102 | };
|
|---|
| 103 |
|
|---|
| 104 | static irq_code_t i8042_kbd = {
|
|---|
| 105 | sizeof(i8042_cmds) / sizeof(irq_cmd_t),
|
|---|
| 106 | i8042_cmds
|
|---|
| 107 | };
|
|---|
| 108 |
|
|---|
| 109 | static uintptr_t i8042_physical;
|
|---|
| 110 | static uintptr_t i8042_kernel;
|
|---|
| 111 | static i8042_t * i8042;
|
|---|
| 112 |
|
|---|
| 113 | static i8042_port_t i8042_port[MAX_DEVS];
|
|---|
| 114 |
|
|---|
| 115 | static void wait_ready(void)
|
|---|
| 116 | {
|
|---|
| 117 | while (pio_read_8(&i8042->status) & i8042_INPUT_FULL);
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call);
|
|---|
| 121 | static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall);
|
|---|
| 122 | static int i8042_init(void);
|
|---|
| 123 | static void i8042_port_write(int devid, uint8_t data);
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | int main(int argc, char *argv[])
|
|---|
| 127 | {
|
|---|
| 128 | char name[16];
|
|---|
| 129 | int i, rc;
|
|---|
| 130 | char dchar[MAX_DEVS] = { 'a', 'b' };
|
|---|
| 131 |
|
|---|
| 132 | printf(NAME ": i8042 PS/2 port driver\n");
|
|---|
| 133 |
|
|---|
| 134 | rc = devmap_driver_register(NAME, i8042_connection);
|
|---|
| 135 | if (rc < 0) {
|
|---|
| 136 | printf(NAME ": Unable to register driver.\n");
|
|---|
| 137 | return rc;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | if (i8042_init() != EOK)
|
|---|
| 141 | return -1;
|
|---|
| 142 |
|
|---|
| 143 | for (i = 0; i < MAX_DEVS; i++) {
|
|---|
| 144 | i8042_port[i].client_phone = -1;
|
|---|
| 145 |
|
|---|
| 146 | snprintf(name, 16, "%s/ps2%c", NAMESPACE, dchar[i]);
|
|---|
| 147 | rc = devmap_device_register(name, &i8042_port[i].devmap_handle);
|
|---|
| 148 | if (rc != EOK) {
|
|---|
| 149 | printf(NAME ": Unable to register device %s.\n", name);
|
|---|
| 150 | return rc;
|
|---|
| 151 | }
|
|---|
| 152 | printf(NAME ": Registered device %s\n", name);
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | printf(NAME ": Accepting connections\n");
|
|---|
| 156 | task_retval(0);
|
|---|
| 157 | async_manager();
|
|---|
| 158 |
|
|---|
| 159 | /* Not reached */
|
|---|
| 160 | return 0;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | static int i8042_init(void)
|
|---|
| 164 | {
|
|---|
| 165 | if (sysinfo_get_value("i8042.address.physical", &i8042_physical) != EOK)
|
|---|
| 166 | return -1;
|
|---|
| 167 |
|
|---|
| 168 | if (sysinfo_get_value("i8042.address.kernel", &i8042_kernel) != EOK)
|
|---|
| 169 | return -1;
|
|---|
| 170 |
|
|---|
| 171 | void *vaddr;
|
|---|
| 172 | if (pio_enable((void *) i8042_physical, sizeof(i8042_t), &vaddr) != 0)
|
|---|
| 173 | return -1;
|
|---|
| 174 |
|
|---|
| 175 | i8042 = vaddr;
|
|---|
| 176 |
|
|---|
| 177 | sysarg_t inr_a;
|
|---|
| 178 | sysarg_t inr_b;
|
|---|
| 179 |
|
|---|
| 180 | if (sysinfo_get_value("i8042.inr_a", &inr_a) != EOK)
|
|---|
| 181 | return -1;
|
|---|
| 182 |
|
|---|
| 183 | if (sysinfo_get_value("i8042.inr_b", &inr_b) != EOK)
|
|---|
| 184 | return -1;
|
|---|
| 185 |
|
|---|
| 186 | async_set_interrupt_received(i8042_irq_handler);
|
|---|
| 187 |
|
|---|
| 188 | /* Disable kbd and aux */
|
|---|
| 189 | wait_ready();
|
|---|
| 190 | pio_write_8(&i8042->status, i8042_CMD_WRITE_CMDB);
|
|---|
| 191 | wait_ready();
|
|---|
| 192 | pio_write_8(&i8042->data, i8042_KBD_DISABLE | i8042_AUX_DISABLE);
|
|---|
| 193 |
|
|---|
| 194 | /* Flush all current IO */
|
|---|
| 195 | while (pio_read_8(&i8042->status) & i8042_OUTPUT_FULL)
|
|---|
| 196 | (void) pio_read_8(&i8042->data);
|
|---|
| 197 |
|
|---|
| 198 | i8042_kbd.cmds[0].addr = (void *) &((i8042_t *) i8042_kernel)->status;
|
|---|
| 199 | i8042_kbd.cmds[3].addr = (void *) &((i8042_t *) i8042_kernel)->data;
|
|---|
| 200 | register_irq(inr_a, device_assign_devno(), 0, &i8042_kbd);
|
|---|
| 201 | register_irq(inr_b, device_assign_devno(), 0, &i8042_kbd);
|
|---|
| 202 | printf("%s: registered for interrupts %" PRIun " and %" PRIun "\n",
|
|---|
| 203 | NAME, inr_a, inr_b);
|
|---|
| 204 |
|
|---|
| 205 | wait_ready();
|
|---|
| 206 | pio_write_8(&i8042->status, i8042_CMD_WRITE_CMDB);
|
|---|
| 207 | wait_ready();
|
|---|
| 208 | pio_write_8(&i8042->data, i8042_KBD_IE | i8042_KBD_TRANSLATE |
|
|---|
| 209 | i8042_AUX_IE);
|
|---|
| 210 |
|
|---|
| 211 | return 0;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | /** Character device connection handler */
|
|---|
| 215 | static void i8042_connection(ipc_callid_t iid, ipc_call_t *icall)
|
|---|
| 216 | {
|
|---|
| 217 | ipc_callid_t callid;
|
|---|
| 218 | ipc_call_t call;
|
|---|
| 219 | sysarg_t method;
|
|---|
| 220 | devmap_handle_t dh;
|
|---|
| 221 | int retval;
|
|---|
| 222 | int dev_id, i;
|
|---|
| 223 |
|
|---|
| 224 | printf(NAME ": connection handler\n");
|
|---|
| 225 |
|
|---|
| 226 | /* Get the device handle. */
|
|---|
| 227 | dh = IPC_GET_ARG1(*icall);
|
|---|
| 228 |
|
|---|
| 229 | /* Determine which disk device is the client connecting to. */
|
|---|
| 230 | dev_id = -1;
|
|---|
| 231 | for (i = 0; i < MAX_DEVS; i++) {
|
|---|
| 232 | if (i8042_port[i].devmap_handle == dh)
|
|---|
| 233 | dev_id = i;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | if (dev_id < 0) {
|
|---|
| 237 | async_answer_0(iid, EINVAL);
|
|---|
| 238 | return;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | /* Answer the IPC_M_CONNECT_ME_TO call. */
|
|---|
| 242 | async_answer_0(iid, EOK);
|
|---|
| 243 |
|
|---|
| 244 | printf(NAME ": accepted connection\n");
|
|---|
| 245 |
|
|---|
| 246 | while (1) {
|
|---|
| 247 | callid = async_get_call(&call);
|
|---|
| 248 | method = IPC_GET_IMETHOD(call);
|
|---|
| 249 | switch (method) {
|
|---|
| 250 | case IPC_M_PHONE_HUNGUP:
|
|---|
| 251 | /* The other side has hung up. */
|
|---|
| 252 | async_answer_0(callid, EOK);
|
|---|
| 253 | return;
|
|---|
| 254 | case IPC_M_CONNECT_TO_ME:
|
|---|
| 255 | printf(NAME ": creating callback connection\n");
|
|---|
| 256 | if (i8042_port[dev_id].client_phone != -1) {
|
|---|
| 257 | retval = ELIMIT;
|
|---|
| 258 | break;
|
|---|
| 259 | }
|
|---|
| 260 | i8042_port[dev_id].client_phone = IPC_GET_ARG5(call);
|
|---|
| 261 | retval = 0;
|
|---|
| 262 | break;
|
|---|
| 263 | case IPC_FIRST_USER_METHOD:
|
|---|
| 264 | printf(NAME ": write %" PRIun " to devid %d\n",
|
|---|
| 265 | IPC_GET_ARG1(call), dev_id);
|
|---|
| 266 | i8042_port_write(dev_id, IPC_GET_ARG1(call));
|
|---|
| 267 | retval = 0;
|
|---|
| 268 | break;
|
|---|
| 269 | default:
|
|---|
| 270 | retval = EINVAL;
|
|---|
| 271 | break;
|
|---|
| 272 | }
|
|---|
| 273 | async_answer_0(callid, retval);
|
|---|
| 274 | }
|
|---|
| 275 | }
|
|---|
| 276 |
|
|---|
| 277 | void i8042_port_write(int devid, uint8_t data)
|
|---|
| 278 | {
|
|---|
| 279 | if (devid == DEVID_AUX) {
|
|---|
| 280 | wait_ready();
|
|---|
| 281 | pio_write_8(&i8042->status, i8042_CMD_WRITE_AUX);
|
|---|
| 282 | }
|
|---|
| 283 | wait_ready();
|
|---|
| 284 | pio_write_8(&i8042->data, data);
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | static void i8042_irq_handler(ipc_callid_t iid, ipc_call_t *call)
|
|---|
| 288 | {
|
|---|
| 289 | int status, data;
|
|---|
| 290 | int devid;
|
|---|
| 291 |
|
|---|
| 292 | status = IPC_GET_ARG1(*call);
|
|---|
| 293 | data = IPC_GET_ARG2(*call);
|
|---|
| 294 |
|
|---|
| 295 | if ((status & i8042_AUX_DATA)) {
|
|---|
| 296 | devid = DEVID_AUX;
|
|---|
| 297 | } else {
|
|---|
| 298 | devid = DEVID_PRI;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | if (i8042_port[devid].client_phone != -1) {
|
|---|
| 302 | async_msg_1(i8042_port[devid].client_phone,
|
|---|
| 303 | IPC_FIRST_USER_METHOD, data);
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /**
|
|---|
| 308 | * @}
|
|---|
| 309 | */
|
|---|