1 /* 2 * QEMU NeXT Keyboard/Mouse emulation 3 * 4 * Copyright (c) 2011 Bryce Lanham 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 /* 26 * This is admittedly hackish, but works well enough for basic input. Mouse 27 * support will be added once we can boot something that needs the mouse. 28 */ 29 30 #include "qemu/osdep.h" 31 #include "qemu/log.h" 32 #include "exec/address-spaces.h" 33 #include "hw/hw.h" 34 #include "hw/sysbus.h" 35 #include "hw/m68k/next-cube.h" 36 #include "ui/console.h" 37 #include "sysemu/sysemu.h" 38 #include "migration/vmstate.h" 39 #include "qom/object.h" 40 41 typedef struct NextKBDState NextKBDState; 42 #define NEXTKBD(obj) OBJECT_CHECK(NextKBDState, (obj), TYPE_NEXTKBD) 43 44 /* following defintions from next68k netbsd */ 45 #define CSR_INT 0x00800000 46 #define CSR_DATA 0x00400000 47 48 #define KD_KEYMASK 0x007f 49 #define KD_DIRECTION 0x0080 /* pressed or released */ 50 #define KD_CNTL 0x0100 51 #define KD_LSHIFT 0x0200 52 #define KD_RSHIFT 0x0400 53 #define KD_LCOMM 0x0800 54 #define KD_RCOMM 0x1000 55 #define KD_LALT 0x2000 56 #define KD_RALT 0x4000 57 #define KD_VALID 0x8000 /* only set for scancode keys ? */ 58 #define KD_MODS 0x4f00 59 60 #define KBD_QUEUE_SIZE 256 61 62 typedef struct { 63 uint8_t data[KBD_QUEUE_SIZE]; 64 int rptr, wptr, count; 65 } KBDQueue; 66 67 68 struct NextKBDState { 69 SysBusDevice sbd; 70 MemoryRegion mr; 71 KBDQueue queue; 72 uint16_t shift; 73 }; 74 75 static void queue_code(void *opaque, int code); 76 77 /* lots of magic numbers here */ 78 static uint32_t kbd_read_byte(void *opaque, hwaddr addr) 79 { 80 switch (addr & 0x3) { 81 case 0x0: /* 0xe000 */ 82 return 0x80 | 0x20; 83 84 case 0x1: /* 0xe001 */ 85 return 0x80 | 0x40 | 0x20 | 0x10; 86 87 case 0x2: /* 0xe002 */ 88 /* returning 0x40 caused mach to hang */ 89 return 0x10 | 0x2 | 0x1; 90 91 default: 92 qemu_log_mask(LOG_UNIMP, "NeXT kbd read byte %"HWADDR_PRIx"\n", addr); 93 } 94 95 return 0; 96 } 97 98 static uint32_t kbd_read_word(void *opaque, hwaddr addr) 99 { 100 qemu_log_mask(LOG_UNIMP, "NeXT kbd read word %"HWADDR_PRIx"\n", addr); 101 return 0; 102 } 103 104 /* even more magic numbers */ 105 static uint32_t kbd_read_long(void *opaque, hwaddr addr) 106 { 107 int key = 0; 108 NextKBDState *s = NEXTKBD(opaque); 109 KBDQueue *q = &s->queue; 110 111 switch (addr & 0xf) { 112 case 0x0: /* 0xe000 */ 113 return 0xA0F09300; 114 115 case 0x8: /* 0xe008 */ 116 /* get keycode from buffer */ 117 if (q->count > 0) { 118 key = q->data[q->rptr]; 119 if (++q->rptr == KBD_QUEUE_SIZE) { 120 q->rptr = 0; 121 } 122 123 q->count--; 124 125 if (s->shift) { 126 key |= s->shift; 127 } 128 129 if (key & 0x80) { 130 return 0; 131 } else { 132 return 0x10000000 | KD_VALID | key; 133 } 134 } else { 135 return 0; 136 } 137 138 default: 139 qemu_log_mask(LOG_UNIMP, "NeXT kbd read long %"HWADDR_PRIx"\n", addr); 140 return 0; 141 } 142 } 143 144 static uint64_t kbd_readfn(void *opaque, hwaddr addr, unsigned size) 145 { 146 switch (size) { 147 case 1: 148 return kbd_read_byte(opaque, addr); 149 case 2: 150 return kbd_read_word(opaque, addr); 151 case 4: 152 return kbd_read_long(opaque, addr); 153 default: 154 g_assert_not_reached(); 155 } 156 } 157 158 static void kbd_writefn(void *opaque, hwaddr addr, uint64_t value, 159 unsigned size) 160 { 161 qemu_log_mask(LOG_UNIMP, "NeXT kbd write: size=%u addr=0x%"HWADDR_PRIx 162 "val=0x%"PRIx64"\n", size, addr, value); 163 } 164 165 static const MemoryRegionOps kbd_ops = { 166 .read = kbd_readfn, 167 .write = kbd_writefn, 168 .valid.min_access_size = 1, 169 .valid.max_access_size = 4, 170 .endianness = DEVICE_NATIVE_ENDIAN, 171 }; 172 173 static void nextkbd_event(void *opaque, int ch) 174 { 175 /* 176 * Will want to set vars for caps/num lock 177 * if (ch & 0x80) -> key release 178 * there's also e0 escaped scancodes that might need to be handled 179 */ 180 queue_code(opaque, ch); 181 } 182 183 static const unsigned char next_keycodes[128] = { 184 0x00, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x4F, 185 0x4E, 0x1E, 0x1F, 0x20, 0x1D, 0x1C, 0x1B, 0x00, 186 0x42, 0x43, 0x44, 0x45, 0x48, 0x47, 0x46, 0x06, 187 0x07, 0x08, 0x00, 0x00, 0x2A, 0x00, 0x39, 0x3A, 188 0x3B, 0x3C, 0x3D, 0x40, 0x3F, 0x3E, 0x2D, 0x2C, 189 0x2B, 0x26, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, 190 0x35, 0x37, 0x36, 0x2e, 0x2f, 0x30, 0x00, 0x00, 191 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 192 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 193 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 194 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 195 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 196 }; 197 198 static void queue_code(void *opaque, int code) 199 { 200 NextKBDState *s = NEXTKBD(opaque); 201 KBDQueue *q = &s->queue; 202 int key = code & KD_KEYMASK; 203 int release = code & 0x80; 204 static int ext; 205 206 if (code == 0xE0) { 207 ext = 1; 208 } 209 210 if (code == 0x2A || code == 0x1D || code == 0x36) { 211 if (code == 0x2A) { 212 s->shift = KD_LSHIFT; 213 } else if (code == 0x36) { 214 s->shift = KD_RSHIFT; 215 ext = 0; 216 } else if (code == 0x1D && !ext) { 217 s->shift = KD_LCOMM; 218 } else if (code == 0x1D && ext) { 219 ext = 0; 220 s->shift = KD_RCOMM; 221 } 222 return; 223 } else if (code == (0x2A | 0x80) || code == (0x1D | 0x80) || 224 code == (0x36 | 0x80)) { 225 s->shift = 0; 226 return; 227 } 228 229 if (q->count >= KBD_QUEUE_SIZE) { 230 return; 231 } 232 233 q->data[q->wptr] = next_keycodes[key] | release; 234 235 if (++q->wptr == KBD_QUEUE_SIZE) { 236 q->wptr = 0; 237 } 238 239 q->count++; 240 241 /* 242 * might need to actually trigger the NeXT irq, but as the keyboard works 243 * at the moment, I'll worry about it later 244 */ 245 /* s->update_irq(s->update_arg, 1); */ 246 } 247 248 static void nextkbd_reset(DeviceState *dev) 249 { 250 NextKBDState *nks = NEXTKBD(dev); 251 252 memset(&nks->queue, 0, sizeof(KBDQueue)); 253 nks->shift = 0; 254 } 255 256 static void nextkbd_realize(DeviceState *dev, Error **errp) 257 { 258 NextKBDState *s = NEXTKBD(dev); 259 260 memory_region_init_io(&s->mr, OBJECT(dev), &kbd_ops, s, "next.kbd", 0x1000); 261 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr); 262 263 qemu_add_kbd_event_handler(nextkbd_event, s); 264 } 265 266 static const VMStateDescription nextkbd_vmstate = { 267 .name = TYPE_NEXTKBD, 268 .unmigratable = 1, /* TODO: Implement this when m68k CPU is migratable */ 269 }; 270 271 static void nextkbd_class_init(ObjectClass *oc, void *data) 272 { 273 DeviceClass *dc = DEVICE_CLASS(oc); 274 275 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 276 dc->vmsd = &nextkbd_vmstate; 277 dc->realize = nextkbd_realize; 278 dc->reset = nextkbd_reset; 279 } 280 281 static const TypeInfo nextkbd_info = { 282 .name = TYPE_NEXTKBD, 283 .parent = TYPE_SYS_BUS_DEVICE, 284 .instance_size = sizeof(NextKBDState), 285 .class_init = nextkbd_class_init, 286 }; 287 288 static void nextkbd_register_types(void) 289 { 290 type_register_static(&nextkbd_info); 291 } 292 293 type_init(nextkbd_register_types) 294