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