1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
/*
* QEMU model of the Milkymist SoftUSB block.
*
* Copyright (c) 2010 Michael Walle <michael@walle.cc>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
*
* Specification available at:
* not available yet
*/
#include "hw.h"
#include "sysbus.h"
#include "trace.h"
#include "console.h"
#include "usb.h"
#include "qemu-error.h"
enum {
R_CTRL = 0,
R_MAX
};
enum {
CTRL_RESET = (1<<0),
};
#define COMLOC_DEBUG_PRODUCE 0x1000
#define COMLOC_DEBUG_BASE 0x1001
#define COMLOC_MEVT_PRODUCE 0x1101
#define COMLOC_MEVT_BASE 0x1102
#define COMLOC_KEVT_PRODUCE 0x1142
#define COMLOC_KEVT_BASE 0x1143
struct MilkymistSoftUsbState {
SysBusDevice busdev;
USBBus usbbus;
USBPort usbport[2];
USBDevice *usbdev;
qemu_irq irq;
/* device properties */
uint32_t pmem_base;
uint32_t pmem_size;
uint32_t dmem_base;
uint32_t dmem_size;
/* device registers */
uint32_t regs[R_MAX];
/* mouse state */
int mouse_dx;
int mouse_dy;
int mouse_dz;
uint8_t mouse_buttons_state;
/* keyboard state */
uint8_t kbd_usb_buffer[8];
};
typedef struct MilkymistSoftUsbState MilkymistSoftUsbState;
static uint32_t softusb_read(void *opaque, target_phys_addr_t addr)
{
MilkymistSoftUsbState *s = opaque;
uint32_t r = 0;
addr >>= 2;
switch (addr) {
case R_CTRL:
r = s->regs[addr];
break;
default:
error_report("milkymist_softusb: read access to unknown register 0x"
TARGET_FMT_plx, addr << 2);
break;
}
trace_milkymist_softusb_memory_read(addr << 2, r);
return r;
}
static void
softusb_write(void *opaque, target_phys_addr_t addr, uint32_t value)
{
MilkymistSoftUsbState *s = opaque;
trace_milkymist_softusb_memory_write(addr, value);
addr >>= 2;
switch (addr) {
case R_CTRL:
s->regs[addr] = value;
break;
default:
error_report("milkymist_softusb: write access to unknown register 0x"
TARGET_FMT_plx, addr << 2);
break;
}
}
static CPUReadMemoryFunc * const softusb_read_fn[] = {
NULL,
NULL,
&softusb_read,
};
static CPUWriteMemoryFunc * const softusb_write_fn[] = {
NULL,
NULL,
&softusb_write,
};
static inline void softusb_read_dmem(MilkymistSoftUsbState *s,
uint32_t offset, uint8_t *buf, uint32_t len)
{
if (offset + len >= s->dmem_size) {
error_report("milkymist_softusb: read dmem out of bounds "
"at offset 0x%x, len %d", offset, len);
return;
}
cpu_physical_memory_read(s->dmem_base + offset, buf, len);
}
static inline void softusb_write_dmem(MilkymistSoftUsbState *s,
uint32_t offset, uint8_t *buf, uint32_t len)
{
if (offset + len >= s->dmem_size) {
error_report("milkymist_softusb: write dmem out of bounds "
"at offset 0x%x, len %d", offset, len);
return;
}
cpu_physical_memory_write(s->dmem_base + offset, buf, len);
}
static inline void softusb_read_pmem(MilkymistSoftUsbState *s,
uint32_t offset, uint8_t *buf, uint32_t len)
{
if (offset + len >= s->pmem_size) {
error_report("milkymist_softusb: read pmem out of bounds "
"at offset 0x%x, len %d", offset, len);
return;
}
cpu_physical_memory_read(s->pmem_base + offset, buf, len);
}
static inline void softusb_write_pmem(MilkymistSoftUsbState *s,
uint32_t offset, uint8_t *buf, uint32_t len)
{
if (offset + len >= s->pmem_size) {
error_report("milkymist_softusb: write pmem out of bounds "
"at offset 0x%x, len %d", offset, len);
return;
}
cpu_physical_memory_write(s->pmem_base + offset, buf, len);
}
static void softusb_mouse_changed(MilkymistSoftUsbState *s)
{
uint8_t m;
uint8_t buf[4];
buf[0] = s->mouse_buttons_state;
buf[1] = s->mouse_dx;
buf[2] = s->mouse_dy;
buf[3] = s->mouse_dz;
softusb_read_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
trace_milkymist_softusb_mevt(m);
softusb_write_dmem(s, COMLOC_MEVT_BASE + 4 * m, buf, 4);
m = (m + 1) & 0xf;
softusb_write_dmem(s, COMLOC_MEVT_PRODUCE, &m, 1);
trace_milkymist_softusb_pulse_irq();
qemu_irq_pulse(s->irq);
}
static void softusb_kbd_changed(MilkymistSoftUsbState *s)
{
uint8_t m;
softusb_read_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
trace_milkymist_softusb_kevt(m);
softusb_write_dmem(s, COMLOC_KEVT_BASE + 8 * m, s->kbd_usb_buffer, 8);
m = (m + 1) & 0x7;
softusb_write_dmem(s, COMLOC_KEVT_PRODUCE, &m, 1);
trace_milkymist_softusb_pulse_irq();
qemu_irq_pulse(s->irq);
}
static void softusb_mouse_event(void *opaque,
int dx, int dy, int dz, int buttons_state)
{
MilkymistSoftUsbState *s = opaque;
/* if device is in reset, do nothing */
if (s->regs[R_CTRL] & CTRL_RESET) {
return;
}
trace_milkymist_softusb_mouse_event(dx, dy, dz, buttons_state);
s->mouse_dx = dx;
s->mouse_dy = dy;
s->mouse_dz = dz;
s->mouse_buttons_state = buttons_state;
softusb_mouse_changed(s);
}
static void softusb_usbdev_datain(void *opaque)
{
MilkymistSoftUsbState *s = opaque;
USBPacket p;
p.pid = USB_TOKEN_IN;
p.devep = 1;
p.data = s->kbd_usb_buffer;
p.len = sizeof(s->kbd_usb_buffer);
s->usbdev->info->handle_data(s->usbdev, &p);
softusb_kbd_changed(s);
}
static void softusb_attach(USBPort *port)
{
}
static void softusb_detach(USBPort *port)
{
}
static void softusb_child_detach(USBPort *port, USBDevice *child)
{
}
static USBPortOps softusb_ops = {
.attach = softusb_attach,
.detach = softusb_detach,
.child_detach = softusb_child_detach,
};
static USBBusOps softusb_bus_ops = {
};
static void milkymist_softusb_reset(DeviceState *d)
{
MilkymistSoftUsbState *s =
container_of(d, MilkymistSoftUsbState, busdev.qdev);
int i;
for (i = 0; i < R_MAX; i++) {
s->regs[i] = 0;
}
s->mouse_dx = 0;
s->mouse_dy = 0;
s->mouse_dz = 0;
s->mouse_buttons_state = 0;
memset(s->kbd_usb_buffer, 0, sizeof(s->kbd_usb_buffer));
/* defaults */
s->regs[R_CTRL] = CTRL_RESET;
}
static int milkymist_softusb_init(SysBusDevice *dev)
{
MilkymistSoftUsbState *s = FROM_SYSBUS(typeof(*s), dev);
int softusb_regs;
ram_addr_t pmem_ram;
ram_addr_t dmem_ram;
sysbus_init_irq(dev, &s->irq);
softusb_regs = cpu_register_io_memory(softusb_read_fn, softusb_write_fn, s,
DEVICE_NATIVE_ENDIAN);
sysbus_init_mmio(dev, R_MAX * 4, softusb_regs);
/* register pmem and dmem */
pmem_ram = qemu_ram_alloc(NULL, "milkymist_softusb.pmem", s->pmem_size);
cpu_register_physical_memory(s->pmem_base, s->pmem_size,
pmem_ram | IO_MEM_RAM);
dmem_ram = qemu_ram_alloc(NULL, "milkymist_softusb.dmem", s->dmem_size);
cpu_register_physical_memory(s->dmem_base, s->dmem_size,
dmem_ram | IO_MEM_RAM);
qemu_add_mouse_event_handler(softusb_mouse_event, s, 0, "Milkymist Mouse");
/* create our usb bus */
usb_bus_new(&s->usbbus, &softusb_bus_ops, NULL);
/* our two ports */
usb_register_port(&s->usbbus, &s->usbport[0], NULL, 0, &softusb_ops,
USB_SPEED_MASK_LOW);
usb_register_port(&s->usbbus, &s->usbport[1], NULL, 1, &softusb_ops,
USB_SPEED_MASK_LOW);
/* and finally create an usb keyboard */
s->usbdev = usb_create_simple(&s->usbbus, "usb-kbd");
usb_hid_datain_cb(s->usbdev, s, softusb_usbdev_datain);
s->usbdev->info->handle_reset(s->usbdev);
return 0;
}
static const VMStateDescription vmstate_milkymist_softusb = {
.name = "milkymist-softusb",
.version_id = 1,
.minimum_version_id = 1,
.minimum_version_id_old = 1,
.fields = (VMStateField[]) {
VMSTATE_UINT32_ARRAY(regs, MilkymistSoftUsbState, R_MAX),
VMSTATE_INT32(mouse_dx, MilkymistSoftUsbState),
VMSTATE_INT32(mouse_dy, MilkymistSoftUsbState),
VMSTATE_INT32(mouse_dz, MilkymistSoftUsbState),
VMSTATE_UINT8(mouse_buttons_state, MilkymistSoftUsbState),
VMSTATE_BUFFER(kbd_usb_buffer, MilkymistSoftUsbState),
VMSTATE_END_OF_LIST()
}
};
static SysBusDeviceInfo milkymist_softusb_info = {
.init = milkymist_softusb_init,
.qdev.name = "milkymist-softusb",
.qdev.size = sizeof(MilkymistSoftUsbState),
.qdev.vmsd = &vmstate_milkymist_softusb,
.qdev.reset = milkymist_softusb_reset,
.qdev.props = (Property[]) {
DEFINE_PROP_UINT32(
"pmem_base", MilkymistSoftUsbState, pmem_base, 0xa0000000
),
DEFINE_PROP_UINT32(
"pmem_size", MilkymistSoftUsbState, pmem_size, 0x00001000
),
DEFINE_PROP_UINT32(
"dmem_base", MilkymistSoftUsbState, dmem_base, 0xa0020000
),
DEFINE_PROP_UINT32(
"dmem_size", MilkymistSoftUsbState, dmem_size, 0x00002000
),
DEFINE_PROP_END_OF_LIST(),
}
};
static void milkymist_softusb_register(void)
{
sysbus_register_withprop(&milkymist_softusb_info);
}
device_init(milkymist_softusb_register)
|