2 * Syborg keyboard controller.
4 * Copyright (c) 2008 CodeSourcery
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
29 //#define DEBUG_SYBORG_KEYBOARD
31 #ifdef DEBUG_SYBORG_KEYBOARD
32 #define DPRINTF(fmt, ...) \
33 do { printf("syborg_keyboard: " fmt , ##args); } while (0)
34 #define BADF(fmt, ...) \
35 do { fprintf(stderr, "syborg_keyboard: error: " fmt , ## __VA_ARGS__); \
38 #define DPRINTF(fmt, ...) do {} while(0)
39 #define BADF(fmt, ...) \
40 do { fprintf(stderr, "syborg_keyboard: error: " fmt , ## __VA_ARGS__); \
58 int read_pos
, read_count
;
60 } SyborgKeyboardState
;
62 static void syborg_keyboard_update(SyborgKeyboardState
*s
)
64 int level
= s
->read_count
&& s
->int_enabled
;
65 DPRINTF("Update IRQ %d\n", level
);
66 qemu_set_irq(s
->irq
, level
);
69 static uint32_t syborg_keyboard_read(void *opaque
, target_phys_addr_t offset
)
71 SyborgKeyboardState
*s
= (SyborgKeyboardState
*)opaque
;
74 DPRINTF("reg read %d\n", (int)offset
);
76 switch (offset
>> 2) {
78 return SYBORG_ID_KEYBOARD
;
82 if (s
->read_count
== 0) {
84 DPRINTF("FIFO underflow\n");
86 c
= s
->key_fifo
[s
->read_pos
];
87 DPRINTF("FIFO read 0x%x\n", c
);
90 if (s
->read_pos
== s
->fifo_size
)
93 syborg_keyboard_update(s
);
96 return s
->int_enabled
;
100 cpu_abort(cpu_single_env
, "syborg_keyboard_read: Bad offset %x\n",
106 static void syborg_keyboard_write(void *opaque
, target_phys_addr_t offset
,
109 SyborgKeyboardState
*s
= (SyborgKeyboardState
*)opaque
;
111 DPRINTF("reg write %d\n", (int)offset
);
113 switch (offset
>> 2) {
115 s
->int_enabled
= value
;
116 syborg_keyboard_update(s
);
119 cpu_abort(cpu_single_env
, "syborg_keyboard_write: Bad offset %x\n",
124 static CPUReadMemoryFunc
* const syborg_keyboard_readfn
[] = {
125 syborg_keyboard_read
,
126 syborg_keyboard_read
,
130 static CPUWriteMemoryFunc
* const syborg_keyboard_writefn
[] = {
131 syborg_keyboard_write
,
132 syborg_keyboard_write
,
133 syborg_keyboard_write
136 static void syborg_keyboard_event(void *opaque
, int keycode
)
138 SyborgKeyboardState
*s
= (SyborgKeyboardState
*)opaque
;
142 /* Strip off 0xe0 prefixes and reconstruct the full scancode. */
143 if (keycode
== 0xe0 && !s
->extension_bit
) {
144 DPRINTF("Extension bit\n");
145 s
->extension_bit
= 0x80;
148 val
= (keycode
& 0x7f) | s
->extension_bit
;
151 s
->extension_bit
= 0;
153 DPRINTF("FIFO push 0x%x\n", val
);
154 slot
= s
->read_pos
+ s
->read_count
;
155 if (slot
>= s
->fifo_size
)
156 slot
-= s
->fifo_size
;
158 if (s
->read_count
< s
->fifo_size
) {
160 s
->key_fifo
[slot
] = val
;
162 fprintf(stderr
, "syborg_keyboard error! FIFO overflow\n");
165 syborg_keyboard_update(s
);
168 static void syborg_keyboard_save(QEMUFile
*f
, void *opaque
)
170 SyborgKeyboardState
*s
= (SyborgKeyboardState
*)opaque
;
173 qemu_put_be32(f
, s
->fifo_size
);
174 qemu_put_be32(f
, s
->int_enabled
);
175 qemu_put_be32(f
, s
->extension_bit
);
176 qemu_put_be32(f
, s
->read_pos
);
177 qemu_put_be32(f
, s
->read_count
);
178 for (i
= 0; i
< s
->fifo_size
; i
++) {
179 qemu_put_be32(f
, s
->key_fifo
[i
]);
183 static int syborg_keyboard_load(QEMUFile
*f
, void *opaque
, int version_id
)
185 SyborgKeyboardState
*s
= (SyborgKeyboardState
*)opaque
;
192 val
= qemu_get_be32(f
);
193 if (val
!= s
->fifo_size
)
196 s
->int_enabled
= qemu_get_be32(f
);
197 s
->extension_bit
= qemu_get_be32(f
);
198 s
->read_pos
= qemu_get_be32(f
);
199 s
->read_count
= qemu_get_be32(f
);
200 for (i
= 0; i
< s
->fifo_size
; i
++) {
201 s
->key_fifo
[i
] = qemu_get_be32(f
);
206 static int syborg_keyboard_init(SysBusDevice
*dev
)
208 SyborgKeyboardState
*s
= FROM_SYSBUS(SyborgKeyboardState
, dev
);
211 sysbus_init_irq(dev
, &s
->irq
);
212 iomemtype
= cpu_register_io_memory(syborg_keyboard_readfn
,
213 syborg_keyboard_writefn
, s
,
214 DEVICE_NATIVE_ENDIAN
);
215 sysbus_init_mmio(dev
, 0x1000, iomemtype
);
216 if (s
->fifo_size
<= 0) {
217 fprintf(stderr
, "syborg_keyboard: fifo too small\n");
220 s
->key_fifo
= qemu_mallocz(s
->fifo_size
* sizeof(s
->key_fifo
[0]));
222 qemu_add_kbd_event_handler(syborg_keyboard_event
, s
);
224 register_savevm(&dev
->qdev
, "syborg_keyboard", -1, 1,
225 syborg_keyboard_save
, syborg_keyboard_load
, s
);
229 static SysBusDeviceInfo syborg_keyboard_info
= {
230 .init
= syborg_keyboard_init
,
231 .qdev
.name
= "syborg,keyboard",
232 .qdev
.size
= sizeof(SyborgKeyboardState
),
233 .qdev
.props
= (Property
[]) {
234 DEFINE_PROP_UINT32("fifo-size", SyborgKeyboardState
, fifo_size
, 16),
235 DEFINE_PROP_END_OF_LIST(),
239 static void syborg_keyboard_register_devices(void)
241 sysbus_register_withprop(&syborg_keyboard_info
);
244 device_init(syborg_keyboard_register_devices
)