qapi/error: Check format string argument in error_*prepend()
[qemu/armbru.git] / hw / input / milkymist-softusb.c
blob3e0a7eb0bd519f5c1157034a1a8cd9d199bb8d96
1 /*
2 * QEMU model of the Milkymist SoftUSB block.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Specification available at:
21 * not available yet
24 #include "qemu/osdep.h"
25 #include "qapi/error.h"
26 #include "hw/sysbus.h"
27 #include "migration/vmstate.h"
28 #include "trace.h"
29 #include "ui/console.h"
30 #include "hw/input/hid.h"
31 #include "hw/irq.h"
32 #include "hw/qdev-properties.h"
33 #include "qemu/error-report.h"
34 #include "qemu/module.h"
36 enum {
37 R_CTRL = 0,
38 R_MAX
41 enum {
42 CTRL_RESET = (1<<0),
45 #define COMLOC_DEBUG_PRODUCE 0x1000
46 #define COMLOC_DEBUG_BASE 0x1001
47 #define COMLOC_MEVT_PRODUCE 0x1101
48 #define COMLOC_MEVT_BASE 0x1102
49 #define COMLOC_KEVT_PRODUCE 0x1142
50 #define COMLOC_KEVT_BASE 0x1143
52 #define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
53 #define MILKYMIST_SOFTUSB(obj) \
54 OBJECT_CHECK(MilkymistSoftUsbState, (obj), TYPE_MILKYMIST_SOFTUSB)
56 struct MilkymistSoftUsbState {
57 SysBusDevice parent_obj;
59 HIDState hid_kbd;
60 HIDState hid_mouse;
62 MemoryRegion regs_region;
63 MemoryRegion pmem;
64 MemoryRegion dmem;
65 qemu_irq irq;
67 void *pmem_ptr;
68 void *dmem_ptr;
70 /* device properties */
71 uint32_t pmem_size;
72 uint32_t dmem_size;
74 /* device registers */
75 uint32_t regs[R_MAX];
77 /* mouse state */
78 uint8_t mouse_hid_buffer[4];
80 /* keyboard state */
81 uint8_t kbd_hid_buffer[8];
83 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
85 static uint64_t softusb_read(void *opaque, hwaddr addr,
86 unsigned size)
88 MilkymistSoftUsbState *s = opaque;
89 uint32_t r = 0;
91 addr >>= 2;
92 switch (addr) {
93 case R_CTRL:
94 r = s->regs[addr];
95 break;
97 default:
98 error_report("milkymist_softusb: read access to unknown register 0x"
99 TARGET_FMT_plx, addr << 2);
100 break;
103 trace_milkymist_softusb_memory_read(addr << 2, r);
105 return r;
108 static void
109 softusb_write(void *opaque, hwaddr addr, uint64_t value,
110 unsigned size)
112 MilkymistSoftUsbState *s = opaque;
114 trace_milkymist_softusb_memory_write(addr, value);
116 addr >>= 2;
117 switch (addr) {
118 case R_CTRL:
119 s->regs[addr] = value;
120 break;
122 default:
123 error_report("milkymist_softusb: write access to unknown register 0x"
124 TARGET_FMT_plx, addr << 2);
125 break;
129 static const MemoryRegionOps softusb_mmio_ops = {
130 .read = softusb_read,
131 .write = softusb_write,
132 .endianness = DEVICE_NATIVE_ENDIAN,
133 .valid = {
134 .min_access_size = 4,
135 .max_access_size = 4,
139 static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
140 uint32_t offset, uint8_t *buf, uint32_t len)
142 if (offset + len >= s->dmem_size) {
143 error_report("milkymist_softusb: read dmem out of bounds "
144 "at offset 0x%x, len %d", offset, len);
145 memset(buf, 0, len);
146 return;
149 memcpy(buf, s->dmem_ptr + offset, len);
152 static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
153 uint32_t offset, uint8_t *buf, uint32_t len)
155 if (offset + len >= s->dmem_size) {
156 error_report("milkymist_softusb: write dmem out of bounds "
157 "at offset 0x%x, len %d", offset, len);
158 return;
161 memcpy(s->dmem_ptr + offset, buf, len);
164 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
166 uint8_t m;
168 softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
169 trace_milkymist_softusb_mevt(m);
170 softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, s->mouse_hid_buffer, 4);
171 m = (m + 1) & 0xf;
172 softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
174 trace_milkymist_softusb_pulse_irq();
175 qemu_irq_pulse(s->irq);
178 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
180 uint8_t m;
182 softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
183 trace_milkymist_softusb_kevt(m);
184 softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_hid_buffer, 8);
185 m = (m + 1) & 0x7;
186 softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
188 trace_milkymist_softusb_pulse_irq();
189 qemu_irq_pulse(s->irq);
192 static void softusb_kbd_hid_datain(HIDState *hs)
194 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
195 int len;
197 /* if device is in reset, do nothing */
198 if (s->regs[R_CTRL] & CTRL_RESET) {
199 return;
202 while (hid_has_events(hs)) {
203 len = hid_keyboard_poll(hs, s->kbd_hid_buffer,
204 sizeof(s->kbd_hid_buffer));
206 if (len == 8) {
207 softusb_kbd_changed(s);
212 static void softusb_mouse_hid_datain(HIDState *hs)
214 MilkymistSoftUsbState *s =
215 container_of(hs, MilkymistSoftUsbState, hid_mouse);
216 int len;
218 /* if device is in reset, do nothing */
219 if (s->regs[R_CTRL] & CTRL_RESET) {
220 return;
223 while (hid_has_events(hs)) {
224 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
225 sizeof(s->mouse_hid_buffer));
227 if (len == 4) {
228 softusb_mouse_changed(s);
233 static void milkymist_softusb_reset(DeviceState *d)
235 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
236 int i;
238 for (i = 0; i < R_MAX; i++) {
239 s->regs[i] = 0;
241 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
242 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
244 hid_reset(&s->hid_kbd);
245 hid_reset(&s->hid_mouse);
247 /* defaults */
248 s->regs[R_CTRL] = CTRL_RESET;
251 static void milkymist_softusb_realize(DeviceState *dev, Error **errp)
253 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
254 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
256 sysbus_init_irq(sbd, &s->irq);
258 memory_region_init_io(&s->regs_region, OBJECT(s), &softusb_mmio_ops, s,
259 "milkymist-softusb", R_MAX * 4);
260 sysbus_init_mmio(sbd, &s->regs_region);
262 /* register pmem and dmem */
263 memory_region_init_ram_nomigrate(&s->pmem, OBJECT(s), "milkymist-softusb.pmem",
264 s->pmem_size, &error_fatal);
265 vmstate_register_ram_global(&s->pmem);
266 s->pmem_ptr = memory_region_get_ram_ptr(&s->pmem);
267 sysbus_init_mmio(sbd, &s->pmem);
268 memory_region_init_ram_nomigrate(&s->dmem, OBJECT(s), "milkymist-softusb.dmem",
269 s->dmem_size, &error_fatal);
270 vmstate_register_ram_global(&s->dmem);
271 s->dmem_ptr = memory_region_get_ram_ptr(&s->dmem);
272 sysbus_init_mmio(sbd, &s->dmem);
274 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
275 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
278 static const VMStateDescription vmstate_milkymist_softusb = {
279 .name = "milkymist-softusb",
280 .version_id = 1,
281 .minimum_version_id = 1,
282 .fields = (VMStateField[]) {
283 VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
284 VMSTATE_HID_KEYBOARD_DEVICE(hid_kbd, MilkymistSoftUsbState),
285 VMSTATE_HID_POINTER_DEVICE(hid_mouse, MilkymistSoftUsbState),
286 VMSTATE_BUFFER(kbd_hid_buffer, MilkymistSoftUsbState),
287 VMSTATE_BUFFER(mouse_hid_buffer, MilkymistSoftUsbState),
288 VMSTATE_END_OF_LIST()
292 static Property milkymist_softusb_properties[] = {
293 DEFINE_PROP_UINT32("pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000),
294 DEFINE_PROP_UINT32("dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000),
295 DEFINE_PROP_END_OF_LIST(),
298 static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
300 DeviceClass *dc = DEVICE_CLASS(klass);
302 dc->realize = milkymist_softusb_realize;
303 dc->reset = milkymist_softusb_reset;
304 dc->vmsd = &vmstate_milkymist_softusb;
305 device_class_set_props(dc, milkymist_softusb_properties);
308 static const TypeInfo milkymist_softusb_info = {
309 .name = TYPE_MILKYMIST_SOFTUSB,
310 .parent = TYPE_SYS_BUS_DEVICE,
311 .instance_size = sizeof(MilkymistSoftUsbState),
312 .class_init = milkymist_softusb_class_init,
315 static void milkymist_softusb_register_types(void)
317 type_register_static(&milkymist_softusb_info);
320 type_init(milkymist_softusb_register_types)