meson: convert qapi-specific to meson
[qemu.git] / hw / input / milkymist-softusb.c
1 /*
2 * QEMU model of the Milkymist SoftUSB block.
3 *
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 *
20 * Specification available at:
21 * not available yet
22 */
23
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"
35
36 enum {
37 R_CTRL = 0,
38 R_MAX
39 };
40
41 enum {
42 CTRL_RESET = (1<<0),
43 };
44
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
51
52 #define TYPE_MILKYMIST_SOFTUSB "milkymist-softusb"
53 #define MILKYMIST_SOFTUSB(obj) \
54 OBJECT_CHECK(MilkymistSoftUsbState, (obj), TYPE_MILKYMIST_SOFTUSB)
55
56 struct MilkymistSoftUsbState {
57 SysBusDevice parent_obj;
58
59 HIDState hid_kbd;
60 HIDState hid_mouse;
61
62 MemoryRegion regs_region;
63 MemoryRegion pmem;
64 MemoryRegion dmem;
65 qemu_irq irq;
66
67 void *pmem_ptr;
68 void *dmem_ptr;
69
70 /* device properties */
71 uint32_t pmem_size;
72 uint32_t dmem_size;
73
74 /* device registers */
75 uint32_t regs[R_MAX];
76
77 /* mouse state */
78 uint8_t mouse_hid_buffer[4];
79
80 /* keyboard state */
81 uint8_t kbd_hid_buffer[8];
82 };
83 typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
84
85 static uint64_t softusb_read(void *opaque, hwaddr addr,
86 unsigned size)
87 {
88 MilkymistSoftUsbState *s = opaque;
89 uint32_t r = 0;
90
91 addr >>= 2;
92 switch (addr) {
93 case R_CTRL:
94 r = s->regs[addr];
95 break;
96
97 default:
98 error_report("milkymist_softusb: read access to unknown register 0x"
99 TARGET_FMT_plx, addr << 2);
100 break;
101 }
102
103 trace_milkymist_softusb_memory_read(addr << 2, r);
104
105 return r;
106 }
107
108 static void
109 softusb_write(void *opaque, hwaddr addr, uint64_t value,
110 unsigned size)
111 {
112 MilkymistSoftUsbState *s = opaque;
113
114 trace_milkymist_softusb_memory_write(addr, value);
115
116 addr >>= 2;
117 switch (addr) {
118 case R_CTRL:
119 s->regs[addr] = value;
120 break;
121
122 default:
123 error_report("milkymist_softusb: write access to unknown register 0x"
124 TARGET_FMT_plx, addr << 2);
125 break;
126 }
127 }
128
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,
136 },
137 };
138
139 static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
140 uint32_t offset, uint8_t *buf, uint32_t len)
141 {
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;
147 }
148
149 memcpy(buf, s->dmem_ptr + offset, len);
150 }
151
152 static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
153 uint32_t offset, uint8_t *buf, uint32_t len)
154 {
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;
159 }
160
161 memcpy(s->dmem_ptr + offset, buf, len);
162 }
163
164 static void softusb_mouse_changed(MilkymistSoftUsbState *s)
165 {
166 uint8_t m;
167
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);
173
174 trace_milkymist_softusb_pulse_irq();
175 qemu_irq_pulse(s->irq);
176 }
177
178 static void softusb_kbd_changed(MilkymistSoftUsbState *s)
179 {
180 uint8_t m;
181
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);
187
188 trace_milkymist_softusb_pulse_irq();
189 qemu_irq_pulse(s->irq);
190 }
191
192 static void softusb_kbd_hid_datain(HIDState *hs)
193 {
194 MilkymistSoftUsbState *s = container_of(hs, MilkymistSoftUsbState, hid_kbd);
195 int len;
196
197 /* if device is in reset, do nothing */
198 if (s->regs[R_CTRL] & CTRL_RESET) {
199 return;
200 }
201
202 while (hid_has_events(hs)) {
203 len = hid_keyboard_poll(hs, s->kbd_hid_buffer,
204 sizeof(s->kbd_hid_buffer));
205
206 if (len == 8) {
207 softusb_kbd_changed(s);
208 }
209 }
210 }
211
212 static void softusb_mouse_hid_datain(HIDState *hs)
213 {
214 MilkymistSoftUsbState *s =
215 container_of(hs, MilkymistSoftUsbState, hid_mouse);
216 int len;
217
218 /* if device is in reset, do nothing */
219 if (s->regs[R_CTRL] & CTRL_RESET) {
220 return;
221 }
222
223 while (hid_has_events(hs)) {
224 len = hid_pointer_poll(hs, s->mouse_hid_buffer,
225 sizeof(s->mouse_hid_buffer));
226
227 if (len == 4) {
228 softusb_mouse_changed(s);
229 }
230 }
231 }
232
233 static void milkymist_softusb_reset(DeviceState *d)
234 {
235 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(d);
236 int i;
237
238 for (i = 0; i < R_MAX; i++) {
239 s->regs[i] = 0;
240 }
241 memset(s->kbd_hid_buffer, 0, sizeof(s->kbd_hid_buffer));
242 memset(s->mouse_hid_buffer, 0, sizeof(s->mouse_hid_buffer));
243
244 hid_reset(&s->hid_kbd);
245 hid_reset(&s->hid_mouse);
246
247 /* defaults */
248 s->regs[R_CTRL] = CTRL_RESET;
249 }
250
251 static void milkymist_softusb_realize(DeviceState *dev, Error **errp)
252 {
253 MilkymistSoftUsbState *s = MILKYMIST_SOFTUSB(dev);
254 SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
255
256 sysbus_init_irq(sbd, &s->irq);
257
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);
261
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);
273
274 hid_init(&s->hid_kbd, HID_KEYBOARD, softusb_kbd_hid_datain);
275 hid_init(&s->hid_mouse, HID_MOUSE, softusb_mouse_hid_datain);
276 }
277
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()
289 }
290 };
291
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(),
296 };
297
298 static void milkymist_softusb_class_init(ObjectClass *klass, void *data)
299 {
300 DeviceClass *dc = DEVICE_CLASS(klass);
301
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);
306 }
307
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,
313 };
314
315 static void milkymist_softusb_register_types(void)
316 {
317 type_register_static(&milkymist_softusb_info);
318 }
319
320 type_init(milkymist_softusb_register_types)