root / hw / syborg_keyboard.c @ a8a358bf
History | View | Annotate | Download (6.9 kB)
1 | 4af39611 | Paul Brook | /*
|
---|---|---|---|
2 | 4af39611 | Paul Brook | * Syborg keyboard controller.
|
3 | 4af39611 | Paul Brook | *
|
4 | 4af39611 | Paul Brook | * Copyright (c) 2008 CodeSourcery
|
5 | 4af39611 | Paul Brook | *
|
6 | 4af39611 | Paul Brook | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 4af39611 | Paul Brook | * of this software and associated documentation files (the "Software"), to deal
|
8 | 4af39611 | Paul Brook | * in the Software without restriction, including without limitation the rights
|
9 | 4af39611 | Paul Brook | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 4af39611 | Paul Brook | * copies of the Software, and to permit persons to whom the Software is
|
11 | 4af39611 | Paul Brook | * furnished to do so, subject to the following conditions:
|
12 | 4af39611 | Paul Brook | *
|
13 | 4af39611 | Paul Brook | * The above copyright notice and this permission notice shall be included in
|
14 | 4af39611 | Paul Brook | * all copies or substantial portions of the Software.
|
15 | 4af39611 | Paul Brook | *
|
16 | 4af39611 | Paul Brook | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 4af39611 | Paul Brook | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 4af39611 | Paul Brook | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 4af39611 | Paul Brook | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 4af39611 | Paul Brook | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 4af39611 | Paul Brook | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 4af39611 | Paul Brook | * THE SOFTWARE.
|
23 | 4af39611 | Paul Brook | */
|
24 | 4af39611 | Paul Brook | |
25 | 4af39611 | Paul Brook | #include "sysbus.h" |
26 | 4af39611 | Paul Brook | #include "console.h" |
27 | 4af39611 | Paul Brook | #include "syborg.h" |
28 | 4af39611 | Paul Brook | |
29 | 4af39611 | Paul Brook | //#define DEBUG_SYBORG_KEYBOARD
|
30 | 4af39611 | Paul Brook | |
31 | 4af39611 | Paul Brook | #ifdef DEBUG_SYBORG_KEYBOARD
|
32 | 4af39611 | Paul Brook | #define DPRINTF(fmt, ...) \
|
33 | 4af39611 | Paul Brook | do { printf("syborg_keyboard: " fmt , ##args); } while (0) |
34 | 4af39611 | Paul Brook | #define BADF(fmt, ...) \
|
35 | 4af39611 | Paul Brook | do { fprintf(stderr, "syborg_keyboard: error: " fmt , ## __VA_ARGS__); \ |
36 | 4af39611 | Paul Brook | exit(1);} while (0) |
37 | 4af39611 | Paul Brook | #else
|
38 | 4af39611 | Paul Brook | #define DPRINTF(fmt, ...) do {} while(0) |
39 | 4af39611 | Paul Brook | #define BADF(fmt, ...) \
|
40 | 4af39611 | Paul Brook | do { fprintf(stderr, "syborg_keyboard: error: " fmt , ## __VA_ARGS__); \ |
41 | 4af39611 | Paul Brook | } while (0) |
42 | 4af39611 | Paul Brook | #endif
|
43 | 4af39611 | Paul Brook | |
44 | 4af39611 | Paul Brook | enum {
|
45 | 4af39611 | Paul Brook | KBD_ID = 0,
|
46 | 4af39611 | Paul Brook | KBD_DATA = 1,
|
47 | 4af39611 | Paul Brook | KBD_FIFO_COUNT = 2,
|
48 | 4af39611 | Paul Brook | KBD_INT_ENABLE = 3,
|
49 | 4af39611 | Paul Brook | KBD_FIFO_SIZE = 4
|
50 | 4af39611 | Paul Brook | }; |
51 | 4af39611 | Paul Brook | |
52 | 4af39611 | Paul Brook | typedef struct { |
53 | 4af39611 | Paul Brook | SysBusDevice busdev; |
54 | 4af39611 | Paul Brook | int int_enabled;
|
55 | 4af39611 | Paul Brook | int extension_bit;
|
56 | ee6847d1 | Gerd Hoffmann | uint32_t fifo_size; |
57 | 4af39611 | Paul Brook | uint32_t *key_fifo; |
58 | 4af39611 | Paul Brook | int read_pos, read_count;
|
59 | 4af39611 | Paul Brook | qemu_irq irq; |
60 | 4af39611 | Paul Brook | } SyborgKeyboardState; |
61 | 4af39611 | Paul Brook | |
62 | 4af39611 | Paul Brook | static void syborg_keyboard_update(SyborgKeyboardState *s) |
63 | 4af39611 | Paul Brook | { |
64 | 4af39611 | Paul Brook | int level = s->read_count && s->int_enabled;
|
65 | 4af39611 | Paul Brook | DPRINTF("Update IRQ %d\n", level);
|
66 | 4af39611 | Paul Brook | qemu_set_irq(s->irq, level); |
67 | 4af39611 | Paul Brook | } |
68 | 4af39611 | Paul Brook | |
69 | 4af39611 | Paul Brook | static uint32_t syborg_keyboard_read(void *opaque, target_phys_addr_t offset) |
70 | 4af39611 | Paul Brook | { |
71 | 4af39611 | Paul Brook | SyborgKeyboardState *s = (SyborgKeyboardState *)opaque; |
72 | 4af39611 | Paul Brook | int c;
|
73 | 4af39611 | Paul Brook | |
74 | 4af39611 | Paul Brook | DPRINTF("reg read %d\n", (int)offset); |
75 | 4af39611 | Paul Brook | offset &= 0xfff;
|
76 | 4af39611 | Paul Brook | switch (offset >> 2) { |
77 | 4af39611 | Paul Brook | case KBD_ID:
|
78 | 4af39611 | Paul Brook | return SYBORG_ID_KEYBOARD;
|
79 | 4af39611 | Paul Brook | case KBD_FIFO_COUNT:
|
80 | 4af39611 | Paul Brook | return s->read_count;
|
81 | 4af39611 | Paul Brook | case KBD_DATA:
|
82 | 4af39611 | Paul Brook | if (s->read_count == 0) { |
83 | 4af39611 | Paul Brook | c = -1;
|
84 | 4af39611 | Paul Brook | DPRINTF("FIFO underflow\n");
|
85 | 4af39611 | Paul Brook | } else {
|
86 | 4af39611 | Paul Brook | c = s->key_fifo[s->read_pos]; |
87 | 4af39611 | Paul Brook | DPRINTF("FIFO read 0x%x\n", c);
|
88 | 4af39611 | Paul Brook | s->read_count--; |
89 | 4af39611 | Paul Brook | s->read_pos++; |
90 | 4af39611 | Paul Brook | if (s->read_pos == s->fifo_size)
|
91 | 4af39611 | Paul Brook | s->read_pos = 0;
|
92 | 4af39611 | Paul Brook | } |
93 | 4af39611 | Paul Brook | syborg_keyboard_update(s); |
94 | 4af39611 | Paul Brook | return c;
|
95 | 4af39611 | Paul Brook | case KBD_INT_ENABLE:
|
96 | 4af39611 | Paul Brook | return s->int_enabled;
|
97 | 4af39611 | Paul Brook | case KBD_FIFO_SIZE:
|
98 | 4af39611 | Paul Brook | return s->fifo_size;
|
99 | 4af39611 | Paul Brook | default:
|
100 | 4af39611 | Paul Brook | cpu_abort(cpu_single_env, "syborg_keyboard_read: Bad offset %x\n",
|
101 | 4af39611 | Paul Brook | (int)offset);
|
102 | 4af39611 | Paul Brook | return 0; |
103 | 4af39611 | Paul Brook | } |
104 | 4af39611 | Paul Brook | } |
105 | 4af39611 | Paul Brook | |
106 | 4af39611 | Paul Brook | static void syborg_keyboard_write(void *opaque, target_phys_addr_t offset, |
107 | 4af39611 | Paul Brook | uint32_t value) |
108 | 4af39611 | Paul Brook | { |
109 | 4af39611 | Paul Brook | SyborgKeyboardState *s = (SyborgKeyboardState *)opaque; |
110 | 4af39611 | Paul Brook | |
111 | 4af39611 | Paul Brook | DPRINTF("reg write %d\n", (int)offset); |
112 | 4af39611 | Paul Brook | offset &= 0xfff;
|
113 | 4af39611 | Paul Brook | switch (offset >> 2) { |
114 | 4af39611 | Paul Brook | case KBD_INT_ENABLE:
|
115 | 4af39611 | Paul Brook | s->int_enabled = value; |
116 | 4af39611 | Paul Brook | syborg_keyboard_update(s); |
117 | 4af39611 | Paul Brook | break;
|
118 | 4af39611 | Paul Brook | default:
|
119 | 4af39611 | Paul Brook | cpu_abort(cpu_single_env, "syborg_keyboard_write: Bad offset %x\n",
|
120 | 4af39611 | Paul Brook | (int)offset);
|
121 | 4af39611 | Paul Brook | } |
122 | 4af39611 | Paul Brook | } |
123 | 4af39611 | Paul Brook | |
124 | d60efc6b | Blue Swirl | static CPUReadMemoryFunc * const syborg_keyboard_readfn[] = { |
125 | 4af39611 | Paul Brook | syborg_keyboard_read, |
126 | 4af39611 | Paul Brook | syborg_keyboard_read, |
127 | 4af39611 | Paul Brook | syborg_keyboard_read |
128 | 4af39611 | Paul Brook | }; |
129 | 4af39611 | Paul Brook | |
130 | d60efc6b | Blue Swirl | static CPUWriteMemoryFunc * const syborg_keyboard_writefn[] = { |
131 | 4af39611 | Paul Brook | syborg_keyboard_write, |
132 | 4af39611 | Paul Brook | syborg_keyboard_write, |
133 | 4af39611 | Paul Brook | syborg_keyboard_write |
134 | 4af39611 | Paul Brook | }; |
135 | 4af39611 | Paul Brook | |
136 | 4af39611 | Paul Brook | static void syborg_keyboard_event(void *opaque, int keycode) |
137 | 4af39611 | Paul Brook | { |
138 | 4af39611 | Paul Brook | SyborgKeyboardState *s = (SyborgKeyboardState *)opaque; |
139 | 4af39611 | Paul Brook | int slot;
|
140 | 4af39611 | Paul Brook | uint32_t val; |
141 | 4af39611 | Paul Brook | |
142 | 4af39611 | Paul Brook | /* Strip off 0xe0 prefixes and reconstruct the full scancode. */
|
143 | 4af39611 | Paul Brook | if (keycode == 0xe0 && !s->extension_bit) { |
144 | 4af39611 | Paul Brook | DPRINTF("Extension bit\n");
|
145 | 4af39611 | Paul Brook | s->extension_bit = 0x80;
|
146 | 4af39611 | Paul Brook | return;
|
147 | 4af39611 | Paul Brook | } |
148 | 4af39611 | Paul Brook | val = (keycode & 0x7f) | s->extension_bit;
|
149 | 4af39611 | Paul Brook | if (keycode & 0x80) |
150 | 4af39611 | Paul Brook | val |= 0x80000000u;
|
151 | 4af39611 | Paul Brook | s->extension_bit = 0;
|
152 | 4af39611 | Paul Brook | |
153 | 4af39611 | Paul Brook | DPRINTF("FIFO push 0x%x\n", val);
|
154 | 4af39611 | Paul Brook | slot = s->read_pos + s->read_count; |
155 | 4af39611 | Paul Brook | if (slot >= s->fifo_size)
|
156 | 4af39611 | Paul Brook | slot -= s->fifo_size; |
157 | 4af39611 | Paul Brook | |
158 | 4af39611 | Paul Brook | if (s->read_count < s->fifo_size) {
|
159 | 4af39611 | Paul Brook | s->read_count++; |
160 | 4af39611 | Paul Brook | s->key_fifo[slot] = val; |
161 | 4af39611 | Paul Brook | } else {
|
162 | 4af39611 | Paul Brook | fprintf(stderr, "syborg_keyboard error! FIFO overflow\n");
|
163 | 4af39611 | Paul Brook | } |
164 | 4af39611 | Paul Brook | |
165 | 4af39611 | Paul Brook | syborg_keyboard_update(s); |
166 | 4af39611 | Paul Brook | } |
167 | 4af39611 | Paul Brook | |
168 | 4af39611 | Paul Brook | static void syborg_keyboard_save(QEMUFile *f, void *opaque) |
169 | 4af39611 | Paul Brook | { |
170 | 4af39611 | Paul Brook | SyborgKeyboardState *s = (SyborgKeyboardState *)opaque; |
171 | 4af39611 | Paul Brook | int i;
|
172 | 4af39611 | Paul Brook | |
173 | 4af39611 | Paul Brook | qemu_put_be32(f, s->fifo_size); |
174 | 4af39611 | Paul Brook | qemu_put_be32(f, s->int_enabled); |
175 | 4af39611 | Paul Brook | qemu_put_be32(f, s->extension_bit); |
176 | 4af39611 | Paul Brook | qemu_put_be32(f, s->read_pos); |
177 | 4af39611 | Paul Brook | qemu_put_be32(f, s->read_count); |
178 | 4af39611 | Paul Brook | for (i = 0; i < s->fifo_size; i++) { |
179 | 4af39611 | Paul Brook | qemu_put_be32(f, s->key_fifo[i]); |
180 | 4af39611 | Paul Brook | } |
181 | 4af39611 | Paul Brook | } |
182 | 4af39611 | Paul Brook | |
183 | 4af39611 | Paul Brook | static int syborg_keyboard_load(QEMUFile *f, void *opaque, int version_id) |
184 | 4af39611 | Paul Brook | { |
185 | 4af39611 | Paul Brook | SyborgKeyboardState *s = (SyborgKeyboardState *)opaque; |
186 | 4af39611 | Paul Brook | uint32_t val; |
187 | 4af39611 | Paul Brook | int i;
|
188 | 4af39611 | Paul Brook | |
189 | 4af39611 | Paul Brook | if (version_id != 1) |
190 | 4af39611 | Paul Brook | return -EINVAL;
|
191 | 4af39611 | Paul Brook | |
192 | 4af39611 | Paul Brook | val = qemu_get_be32(f); |
193 | 4af39611 | Paul Brook | if (val != s->fifo_size)
|
194 | 4af39611 | Paul Brook | return -EINVAL;
|
195 | 4af39611 | Paul Brook | |
196 | 4af39611 | Paul Brook | s->int_enabled = qemu_get_be32(f); |
197 | 4af39611 | Paul Brook | s->extension_bit = qemu_get_be32(f); |
198 | 4af39611 | Paul Brook | s->read_pos = qemu_get_be32(f); |
199 | 4af39611 | Paul Brook | s->read_count = qemu_get_be32(f); |
200 | 4af39611 | Paul Brook | for (i = 0; i < s->fifo_size; i++) { |
201 | 4af39611 | Paul Brook | s->key_fifo[i] = qemu_get_be32(f); |
202 | 4af39611 | Paul Brook | } |
203 | 4af39611 | Paul Brook | return 0; |
204 | 4af39611 | Paul Brook | } |
205 | 4af39611 | Paul Brook | |
206 | 4af39611 | Paul Brook | static void syborg_keyboard_init(SysBusDevice *dev) |
207 | 4af39611 | Paul Brook | { |
208 | 4af39611 | Paul Brook | SyborgKeyboardState *s = FROM_SYSBUS(SyborgKeyboardState, dev); |
209 | 4af39611 | Paul Brook | int iomemtype;
|
210 | 4af39611 | Paul Brook | |
211 | 4af39611 | Paul Brook | sysbus_init_irq(dev, &s->irq); |
212 | 1eed09cb | Avi Kivity | iomemtype = cpu_register_io_memory(syborg_keyboard_readfn, |
213 | 4af39611 | Paul Brook | syborg_keyboard_writefn, s); |
214 | 4af39611 | Paul Brook | sysbus_init_mmio(dev, 0x1000, iomemtype);
|
215 | 4af39611 | Paul Brook | if (s->fifo_size <= 0) { |
216 | 4af39611 | Paul Brook | fprintf(stderr, "syborg_keyboard: fifo too small\n");
|
217 | 4af39611 | Paul Brook | s->fifo_size = 16;
|
218 | 4af39611 | Paul Brook | } |
219 | 4af39611 | Paul Brook | s->key_fifo = qemu_mallocz(s->fifo_size * sizeof(s->key_fifo[0])); |
220 | 4af39611 | Paul Brook | |
221 | 4af39611 | Paul Brook | qemu_add_kbd_event_handler(syborg_keyboard_event, s); |
222 | 4af39611 | Paul Brook | |
223 | 4af39611 | Paul Brook | register_savevm("syborg_keyboard", -1, 1, |
224 | 4af39611 | Paul Brook | syborg_keyboard_save, syborg_keyboard_load, s); |
225 | 4af39611 | Paul Brook | } |
226 | 4af39611 | Paul Brook | |
227 | ee6847d1 | Gerd Hoffmann | static SysBusDeviceInfo syborg_keyboard_info = {
|
228 | ee6847d1 | Gerd Hoffmann | .init = syborg_keyboard_init, |
229 | ee6847d1 | Gerd Hoffmann | .qdev.name = "syborg,keyboard",
|
230 | ee6847d1 | Gerd Hoffmann | .qdev.size = sizeof(SyborgKeyboardState),
|
231 | ee6847d1 | Gerd Hoffmann | .qdev.props = (Property[]) { |
232 | 51dd5927 | Gerd Hoffmann | DEFINE_PROP_UINT32("fifo-size", SyborgKeyboardState, fifo_size, 16), |
233 | 51dd5927 | Gerd Hoffmann | DEFINE_PROP_END_OF_LIST(), |
234 | ee6847d1 | Gerd Hoffmann | } |
235 | ee6847d1 | Gerd Hoffmann | }; |
236 | ee6847d1 | Gerd Hoffmann | |
237 | 4af39611 | Paul Brook | static void syborg_keyboard_register_devices(void) |
238 | 4af39611 | Paul Brook | { |
239 | ee6847d1 | Gerd Hoffmann | sysbus_register_withprop(&syborg_keyboard_info); |
240 | 4af39611 | Paul Brook | } |
241 | 4af39611 | Paul Brook | |
242 | 4af39611 | Paul Brook | device_init(syborg_keyboard_register_devices) |