xref: /openbmc/qemu/hw/m68k/next-kbd.c (revision 7d87775f)
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 "hw/sysbus.h"
33 #include "hw/m68k/next-cube.h"
34 #include "ui/console.h"
35 #include "migration/vmstate.h"
36 #include "qom/object.h"
37 
38 OBJECT_DECLARE_SIMPLE_TYPE(NextKBDState, NEXTKBD)
39 
40 /* following definitions from next68k netbsd */
41 #define CSR_INT 0x00800000
42 #define CSR_DATA 0x00400000
43 
44 #define KD_KEYMASK    0x007f
45 #define KD_DIRECTION  0x0080 /* pressed or released */
46 #define KD_CNTL       0x0100
47 #define KD_LSHIFT     0x0200
48 #define KD_RSHIFT     0x0400
49 #define KD_LCOMM      0x0800
50 #define KD_RCOMM      0x1000
51 #define KD_LALT       0x2000
52 #define KD_RALT       0x4000
53 #define KD_VALID      0x8000 /* only set for scancode keys ? */
54 #define KD_MODS       0x4f00
55 
56 #define KBD_QUEUE_SIZE 256
57 
58 typedef struct {
59     uint8_t data[KBD_QUEUE_SIZE];
60     int rptr, wptr, count;
61 } KBDQueue;
62 
63 
64 struct NextKBDState {
65     SysBusDevice sbd;
66     MemoryRegion mr;
67     KBDQueue queue;
68     uint16_t shift;
69 };
70 
71 
72 /* lots of magic numbers here */
73 static uint32_t kbd_read_byte(void *opaque, hwaddr addr)
74 {
75     switch (addr & 0x3) {
76     case 0x0:   /* 0xe000 */
77         return 0x80 | 0x20;
78 
79     case 0x1:   /* 0xe001 */
80         return 0x80 | 0x40 | 0x20 | 0x10;
81 
82     case 0x2:   /* 0xe002 */
83         /* returning 0x40 caused mach to hang */
84         return 0x10 | 0x2 | 0x1;
85 
86     default:
87         qemu_log_mask(LOG_UNIMP, "NeXT kbd read byte %"HWADDR_PRIx"\n", addr);
88     }
89 
90     return 0;
91 }
92 
93 static uint32_t kbd_read_word(void *opaque, hwaddr addr)
94 {
95     qemu_log_mask(LOG_UNIMP, "NeXT kbd read word %"HWADDR_PRIx"\n", addr);
96     return 0;
97 }
98 
99 /* even more magic numbers */
100 static uint32_t kbd_read_long(void *opaque, hwaddr addr)
101 {
102     int key = 0;
103     NextKBDState *s = NEXTKBD(opaque);
104     KBDQueue *q = &s->queue;
105 
106     switch (addr & 0xf) {
107     case 0x0:   /* 0xe000 */
108         return 0xA0F09300;
109 
110     case 0x8:   /* 0xe008 */
111         /* get keycode from buffer */
112         if (q->count > 0) {
113             key = q->data[q->rptr];
114             if (++q->rptr == KBD_QUEUE_SIZE) {
115                 q->rptr = 0;
116             }
117 
118             q->count--;
119 
120             if (s->shift) {
121                 key |= s->shift;
122             }
123 
124             if (key & 0x80) {
125                 return 0;
126             } else {
127                 return 0x10000000 | KD_VALID | key;
128             }
129         } else {
130             return 0;
131         }
132 
133     default:
134         qemu_log_mask(LOG_UNIMP, "NeXT kbd read long %"HWADDR_PRIx"\n", addr);
135         return 0;
136     }
137 }
138 
139 static uint64_t kbd_readfn(void *opaque, hwaddr addr, unsigned size)
140 {
141     switch (size) {
142     case 1:
143         return kbd_read_byte(opaque, addr);
144     case 2:
145         return kbd_read_word(opaque, addr);
146     case 4:
147         return kbd_read_long(opaque, addr);
148     default:
149         g_assert_not_reached();
150     }
151 }
152 
153 static void kbd_writefn(void *opaque, hwaddr addr, uint64_t value,
154                         unsigned size)
155 {
156     qemu_log_mask(LOG_UNIMP, "NeXT kbd write: size=%u addr=0x%"HWADDR_PRIx
157                   "val=0x%"PRIx64"\n", size, addr, value);
158 }
159 
160 static const MemoryRegionOps kbd_ops = {
161     .read = kbd_readfn,
162     .write = kbd_writefn,
163     .valid.min_access_size = 1,
164     .valid.max_access_size = 4,
165     .endianness = DEVICE_NATIVE_ENDIAN,
166 };
167 
168 static const int qcode_to_nextkbd_keycode[] = {
169     [Q_KEY_CODE_ESC]           = 0x49,
170     [Q_KEY_CODE_1]             = 0x4a,
171     [Q_KEY_CODE_2]             = 0x4b,
172     [Q_KEY_CODE_3]             = 0x4c,
173     [Q_KEY_CODE_4]             = 0x4d,
174     [Q_KEY_CODE_5]             = 0x50,
175     [Q_KEY_CODE_6]             = 0x4f,
176     [Q_KEY_CODE_7]             = 0x4e,
177     [Q_KEY_CODE_8]             = 0x1e,
178     [Q_KEY_CODE_9]             = 0x1f,
179     [Q_KEY_CODE_0]             = 0x20,
180     [Q_KEY_CODE_MINUS]         = 0x1d,
181     [Q_KEY_CODE_EQUAL]         = 0x1c,
182     [Q_KEY_CODE_BACKSPACE]     = 0x1b,
183 
184     [Q_KEY_CODE_Q]             = 0x42,
185     [Q_KEY_CODE_W]             = 0x43,
186     [Q_KEY_CODE_E]             = 0x44,
187     [Q_KEY_CODE_R]             = 0x45,
188     [Q_KEY_CODE_T]             = 0x48,
189     [Q_KEY_CODE_Y]             = 0x47,
190     [Q_KEY_CODE_U]             = 0x46,
191     [Q_KEY_CODE_I]             = 0x06,
192     [Q_KEY_CODE_O]             = 0x07,
193     [Q_KEY_CODE_P]             = 0x08,
194     [Q_KEY_CODE_RET]           = 0x2a,
195     [Q_KEY_CODE_A]             = 0x39,
196     [Q_KEY_CODE_S]             = 0x3a,
197 
198     [Q_KEY_CODE_D]             = 0x3b,
199     [Q_KEY_CODE_F]             = 0x3c,
200     [Q_KEY_CODE_G]             = 0x3d,
201     [Q_KEY_CODE_H]             = 0x40,
202     [Q_KEY_CODE_J]             = 0x3f,
203     [Q_KEY_CODE_K]             = 0x3e,
204     [Q_KEY_CODE_L]             = 0x2d,
205     [Q_KEY_CODE_SEMICOLON]     = 0x2c,
206     [Q_KEY_CODE_APOSTROPHE]    = 0x2b,
207     [Q_KEY_CODE_GRAVE_ACCENT]  = 0x26,
208     [Q_KEY_CODE_Z]             = 0x31,
209     [Q_KEY_CODE_X]             = 0x32,
210     [Q_KEY_CODE_C]             = 0x33,
211     [Q_KEY_CODE_V]             = 0x34,
212 
213     [Q_KEY_CODE_B]             = 0x35,
214     [Q_KEY_CODE_N]             = 0x37,
215     [Q_KEY_CODE_M]             = 0x36,
216     [Q_KEY_CODE_COMMA]         = 0x2e,
217     [Q_KEY_CODE_DOT]           = 0x2f,
218     [Q_KEY_CODE_SLASH]         = 0x30,
219 
220     [Q_KEY_CODE_SPC]           = 0x38,
221 };
222 
223 static void nextkbd_put_keycode(NextKBDState *s, int keycode)
224 {
225     KBDQueue *q = &s->queue;
226 
227     if (q->count >= KBD_QUEUE_SIZE) {
228         return;
229     }
230 
231     q->data[q->wptr] = keycode;
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_event(DeviceState *dev, QemuConsole *src, InputEvent *evt)
246 {
247     NextKBDState *s = NEXTKBD(dev);
248     int qcode, keycode;
249     bool key_down = evt->u.key.data->down;
250 
251     qcode = qemu_input_key_value_to_qcode(evt->u.key.data->key);
252     if (qcode >= ARRAY_SIZE(qcode_to_nextkbd_keycode)) {
253         return;
254     }
255 
256     /* Shift key currently has no keycode, so handle separately */
257     if (qcode == Q_KEY_CODE_SHIFT) {
258         if (key_down) {
259             s->shift |= KD_LSHIFT;
260         } else {
261             s->shift &= ~KD_LSHIFT;
262         }
263     }
264 
265     if (qcode == Q_KEY_CODE_SHIFT_R) {
266         if (key_down) {
267             s->shift |= KD_RSHIFT;
268         } else {
269             s->shift &= ~KD_RSHIFT;
270         }
271     }
272 
273     keycode = qcode_to_nextkbd_keycode[qcode];
274     if (!keycode) {
275         return;
276     }
277 
278     /* If key release event, create keyboard break code */
279     if (!key_down) {
280         keycode |= 0x80;
281     }
282 
283     nextkbd_put_keycode(s, keycode);
284 }
285 
286 static const QemuInputHandler nextkbd_handler = {
287     .name  = "QEMU NeXT Keyboard",
288     .mask  = INPUT_EVENT_MASK_KEY,
289     .event = nextkbd_event,
290 };
291 
292 static void nextkbd_reset(DeviceState *dev)
293 {
294     NextKBDState *nks = NEXTKBD(dev);
295 
296     memset(&nks->queue, 0, sizeof(KBDQueue));
297     nks->shift = 0;
298 }
299 
300 static void nextkbd_realize(DeviceState *dev, Error **errp)
301 {
302     NextKBDState *s = NEXTKBD(dev);
303 
304     memory_region_init_io(&s->mr, OBJECT(dev), &kbd_ops, s, "next.kbd", 0x1000);
305     sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr);
306 
307     qemu_input_handler_register(dev, &nextkbd_handler);
308 }
309 
310 static const VMStateDescription nextkbd_vmstate = {
311     .name = TYPE_NEXTKBD,
312     .unmigratable = 1,    /* TODO: Implement this when m68k CPU is migratable */
313 };
314 
315 static void nextkbd_class_init(ObjectClass *oc, void *data)
316 {
317     DeviceClass *dc = DEVICE_CLASS(oc);
318 
319     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
320     dc->vmsd = &nextkbd_vmstate;
321     dc->realize = nextkbd_realize;
322     device_class_set_legacy_reset(dc, nextkbd_reset);
323 }
324 
325 static const TypeInfo nextkbd_info = {
326     .name          = TYPE_NEXTKBD,
327     .parent        = TYPE_SYS_BUS_DEVICE,
328     .instance_size = sizeof(NextKBDState),
329     .class_init    = nextkbd_class_init,
330 };
331 
332 static void nextkbd_register_types(void)
333 {
334     type_register_static(&nextkbd_info);
335 }
336 
337 type_init(nextkbd_register_types)
338