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/sysbus.h" 34 #include "hw/m68k/next-cube.h" 35 #include "ui/console.h" 36 #include "migration/vmstate.h" 37 #include "qom/object.h" 38 39 OBJECT_DECLARE_SIMPLE_TYPE(NextKBDState, NEXTKBD) 40 41 /* following defintions from next68k netbsd */ 42 #define CSR_INT 0x00800000 43 #define CSR_DATA 0x00400000 44 45 #define KD_KEYMASK 0x007f 46 #define KD_DIRECTION 0x0080 /* pressed or released */ 47 #define KD_CNTL 0x0100 48 #define KD_LSHIFT 0x0200 49 #define KD_RSHIFT 0x0400 50 #define KD_LCOMM 0x0800 51 #define KD_RCOMM 0x1000 52 #define KD_LALT 0x2000 53 #define KD_RALT 0x4000 54 #define KD_VALID 0x8000 /* only set for scancode keys ? */ 55 #define KD_MODS 0x4f00 56 57 #define KBD_QUEUE_SIZE 256 58 59 typedef struct { 60 uint8_t data[KBD_QUEUE_SIZE]; 61 int rptr, wptr, count; 62 } KBDQueue; 63 64 65 struct NextKBDState { 66 SysBusDevice sbd; 67 MemoryRegion mr; 68 KBDQueue queue; 69 uint16_t shift; 70 }; 71 72 static void queue_code(void *opaque, int code); 73 74 /* lots of magic numbers here */ 75 static uint32_t kbd_read_byte(void *opaque, hwaddr addr) 76 { 77 switch (addr & 0x3) { 78 case 0x0: /* 0xe000 */ 79 return 0x80 | 0x20; 80 81 case 0x1: /* 0xe001 */ 82 return 0x80 | 0x40 | 0x20 | 0x10; 83 84 case 0x2: /* 0xe002 */ 85 /* returning 0x40 caused mach to hang */ 86 return 0x10 | 0x2 | 0x1; 87 88 default: 89 qemu_log_mask(LOG_UNIMP, "NeXT kbd read byte %"HWADDR_PRIx"\n", addr); 90 } 91 92 return 0; 93 } 94 95 static uint32_t kbd_read_word(void *opaque, hwaddr addr) 96 { 97 qemu_log_mask(LOG_UNIMP, "NeXT kbd read word %"HWADDR_PRIx"\n", addr); 98 return 0; 99 } 100 101 /* even more magic numbers */ 102 static uint32_t kbd_read_long(void *opaque, hwaddr addr) 103 { 104 int key = 0; 105 NextKBDState *s = NEXTKBD(opaque); 106 KBDQueue *q = &s->queue; 107 108 switch (addr & 0xf) { 109 case 0x0: /* 0xe000 */ 110 return 0xA0F09300; 111 112 case 0x8: /* 0xe008 */ 113 /* get keycode from buffer */ 114 if (q->count > 0) { 115 key = q->data[q->rptr]; 116 if (++q->rptr == KBD_QUEUE_SIZE) { 117 q->rptr = 0; 118 } 119 120 q->count--; 121 122 if (s->shift) { 123 key |= s->shift; 124 } 125 126 if (key & 0x80) { 127 return 0; 128 } else { 129 return 0x10000000 | KD_VALID | key; 130 } 131 } else { 132 return 0; 133 } 134 135 default: 136 qemu_log_mask(LOG_UNIMP, "NeXT kbd read long %"HWADDR_PRIx"\n", addr); 137 return 0; 138 } 139 } 140 141 static uint64_t kbd_readfn(void *opaque, hwaddr addr, unsigned size) 142 { 143 switch (size) { 144 case 1: 145 return kbd_read_byte(opaque, addr); 146 case 2: 147 return kbd_read_word(opaque, addr); 148 case 4: 149 return kbd_read_long(opaque, addr); 150 default: 151 g_assert_not_reached(); 152 } 153 } 154 155 static void kbd_writefn(void *opaque, hwaddr addr, uint64_t value, 156 unsigned size) 157 { 158 qemu_log_mask(LOG_UNIMP, "NeXT kbd write: size=%u addr=0x%"HWADDR_PRIx 159 "val=0x%"PRIx64"\n", size, addr, value); 160 } 161 162 static const MemoryRegionOps kbd_ops = { 163 .read = kbd_readfn, 164 .write = kbd_writefn, 165 .valid.min_access_size = 1, 166 .valid.max_access_size = 4, 167 .endianness = DEVICE_NATIVE_ENDIAN, 168 }; 169 170 static void nextkbd_event(void *opaque, int ch) 171 { 172 /* 173 * Will want to set vars for caps/num lock 174 * if (ch & 0x80) -> key release 175 * there's also e0 escaped scancodes that might need to be handled 176 */ 177 queue_code(opaque, ch); 178 } 179 180 static const unsigned char next_keycodes[128] = { 181 0x00, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x50, 0x4F, 182 0x4E, 0x1E, 0x1F, 0x20, 0x1D, 0x1C, 0x1B, 0x00, 183 0x42, 0x43, 0x44, 0x45, 0x48, 0x47, 0x46, 0x06, 184 0x07, 0x08, 0x00, 0x00, 0x2A, 0x00, 0x39, 0x3A, 185 0x3B, 0x3C, 0x3D, 0x40, 0x3F, 0x3E, 0x2D, 0x2C, 186 0x2B, 0x26, 0x00, 0x00, 0x31, 0x32, 0x33, 0x34, 187 0x35, 0x37, 0x36, 0x2e, 0x2f, 0x30, 0x00, 0x00, 188 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 189 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 190 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 191 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 193 }; 194 195 static void queue_code(void *opaque, int code) 196 { 197 NextKBDState *s = NEXTKBD(opaque); 198 KBDQueue *q = &s->queue; 199 int key = code & KD_KEYMASK; 200 int release = code & 0x80; 201 static int ext; 202 203 if (code == 0xE0) { 204 ext = 1; 205 } 206 207 if (code == 0x2A || code == 0x1D || code == 0x36) { 208 if (code == 0x2A) { 209 s->shift = KD_LSHIFT; 210 } else if (code == 0x36) { 211 s->shift = KD_RSHIFT; 212 ext = 0; 213 } else if (code == 0x1D && !ext) { 214 s->shift = KD_LCOMM; 215 } else if (code == 0x1D && ext) { 216 ext = 0; 217 s->shift = KD_RCOMM; 218 } 219 return; 220 } else if (code == (0x2A | 0x80) || code == (0x1D | 0x80) || 221 code == (0x36 | 0x80)) { 222 s->shift = 0; 223 return; 224 } 225 226 if (q->count >= KBD_QUEUE_SIZE) { 227 return; 228 } 229 230 q->data[q->wptr] = next_keycodes[key] | release; 231 232 if (++q->wptr == KBD_QUEUE_SIZE) { 233 q->wptr = 0; 234 } 235 236 q->count++; 237 238 /* 239 * might need to actually trigger the NeXT irq, but as the keyboard works 240 * at the moment, I'll worry about it later 241 */ 242 /* s->update_irq(s->update_arg, 1); */ 243 } 244 245 static void nextkbd_reset(DeviceState *dev) 246 { 247 NextKBDState *nks = NEXTKBD(dev); 248 249 memset(&nks->queue, 0, sizeof(KBDQueue)); 250 nks->shift = 0; 251 } 252 253 static void nextkbd_realize(DeviceState *dev, Error **errp) 254 { 255 NextKBDState *s = NEXTKBD(dev); 256 257 memory_region_init_io(&s->mr, OBJECT(dev), &kbd_ops, s, "next.kbd", 0x1000); 258 sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr); 259 260 qemu_add_kbd_event_handler(nextkbd_event, s); 261 } 262 263 static const VMStateDescription nextkbd_vmstate = { 264 .name = TYPE_NEXTKBD, 265 .unmigratable = 1, /* TODO: Implement this when m68k CPU is migratable */ 266 }; 267 268 static void nextkbd_class_init(ObjectClass *oc, void *data) 269 { 270 DeviceClass *dc = DEVICE_CLASS(oc); 271 272 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 273 dc->vmsd = &nextkbd_vmstate; 274 dc->realize = nextkbd_realize; 275 dc->reset = nextkbd_reset; 276 } 277 278 static const TypeInfo nextkbd_info = { 279 .name = TYPE_NEXTKBD, 280 .parent = TYPE_SYS_BUS_DEVICE, 281 .instance_size = sizeof(NextKBDState), 282 .class_init = nextkbd_class_init, 283 }; 284 285 static void nextkbd_register_types(void) 286 { 287 type_register_static(&nextkbd_info); 288 } 289 290 type_init(nextkbd_register_types) 291