]> git.proxmox.com Git - mirror_qemu.git/blame - hw/input/lasips2.c
lasips2: LASI PS/2 devices are not user-createable
[mirror_qemu.git] / hw / input / lasips2.c
CommitLineData
2a6505b0
SS
1/*
2 * QEMU HP Lasi PS/2 interface emulation
3 *
4 * Copyright (c) 2019 Sven Schnelle
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24#include "qemu/osdep.h"
25#include "qemu/log.h"
26#include "hw/qdev-properties.h"
653b388c 27#include "hw/sysbus.h"
2a6505b0
SS
28#include "hw/input/ps2.h"
29#include "hw/input/lasips2.h"
2a6505b0 30#include "exec/hwaddr.h"
2a6505b0
SS
31#include "trace.h"
32#include "exec/address-spaces.h"
33#include "migration/vmstate.h"
34#include "hw/irq.h"
653b388c 35#include "qapi/error.h"
2a6505b0
SS
36
37
1b7bd0ab
MCA
38static const VMStateDescription vmstate_lasips2_port = {
39 .name = "lasips2-port",
40 .version_id = 1,
41 .minimum_version_id = 1,
42 .fields = (VMStateField[]) {
43 VMSTATE_UINT8(control, LASIPS2Port),
44 VMSTATE_UINT8(buf, LASIPS2Port),
45 VMSTATE_BOOL(loopback_rbne, LASIPS2Port),
46 VMSTATE_END_OF_LIST()
47 }
48};
49
2a6505b0
SS
50static const VMStateDescription vmstate_lasips2 = {
51 .name = "lasips2",
1b7bd0ab
MCA
52 .version_id = 1,
53 .minimum_version_id = 1,
2a6505b0 54 .fields = (VMStateField[]) {
1b7bd0ab
MCA
55 VMSTATE_UINT8(int_status, LASIPS2State),
56 VMSTATE_STRUCT(kbd_port.parent_obj, LASIPS2State, 1,
57 vmstate_lasips2_port, LASIPS2Port),
58 VMSTATE_STRUCT(mouse_port.parent_obj, LASIPS2State, 1,
59 vmstate_lasips2_port, LASIPS2Port),
2a6505b0
SS
60 VMSTATE_END_OF_LIST()
61 }
62};
63
64typedef enum {
65 REG_PS2_ID = 0,
66 REG_PS2_RCVDATA = 4,
67 REG_PS2_CONTROL = 8,
68 REG_PS2_STATUS = 12,
69} lasips2_read_reg_t;
70
71typedef enum {
72 REG_PS2_RESET = 0,
73 REG_PS2_XMTDATA = 4,
74} lasips2_write_reg_t;
75
76typedef enum {
77 LASIPS2_CONTROL_ENABLE = 0x01,
78 LASIPS2_CONTROL_LOOPBACK = 0x02,
79 LASIPS2_CONTROL_DIAG = 0x20,
80 LASIPS2_CONTROL_DATDIR = 0x40,
81 LASIPS2_CONTROL_CLKDIR = 0x80,
82} lasips2_control_reg_t;
83
84typedef enum {
85 LASIPS2_STATUS_RBNE = 0x01,
86 LASIPS2_STATUS_TBNE = 0x02,
87 LASIPS2_STATUS_TERR = 0x04,
88 LASIPS2_STATUS_PERR = 0x08,
89 LASIPS2_STATUS_CMPINTR = 0x10,
90 LASIPS2_STATUS_DATSHD = 0x40,
91 LASIPS2_STATUS_CLKSHD = 0x80,
92} lasips2_status_reg_t;
93
5d2bd735 94static const char *lasips2_read_reg_name(uint64_t addr)
2a6505b0
SS
95{
96 switch (addr & 0xc) {
97 case REG_PS2_ID:
98 return " PS2_ID";
99
100 case REG_PS2_RCVDATA:
101 return " PS2_RCVDATA";
102
103 case REG_PS2_CONTROL:
104 return " PS2_CONTROL";
105
106 case REG_PS2_STATUS:
107 return " PS2_STATUS";
108
109 default:
110 return "";
111 }
112}
113
5d2bd735 114static const char *lasips2_write_reg_name(uint64_t addr)
2a6505b0
SS
115{
116 switch (addr & 0x0c) {
117 case REG_PS2_RESET:
118 return " PS2_RESET";
119
120 case REG_PS2_XMTDATA:
121 return " PS2_XMTDATA";
122
123 case REG_PS2_CONTROL:
124 return " PS2_CONTROL";
125
126 default:
127 return "";
128 }
129}
130
131static void lasips2_update_irq(LASIPS2State *s)
132{
212a3003
MCA
133 int level = s->int_status ? 1 : 0;
134
135 trace_lasips2_intr(level);
136 qemu_set_irq(s->irq, level);
2a6505b0
SS
137}
138
ca735a81
MCA
139static void lasips2_set_irq(void *opaque, int n, int level)
140{
141 LASIPS2State *s = LASIPS2(opaque);
142
143 if (level) {
144 s->int_status |= BIT(n);
145 } else {
146 s->int_status &= ~BIT(n);
147 }
148
149 lasips2_update_irq(s);
150}
151
2a6505b0
SS
152static void lasips2_reg_write(void *opaque, hwaddr addr, uint64_t val,
153 unsigned size)
154{
902691d4 155 LASIPS2Port *lp = LASIPS2_PORT(opaque);
2a6505b0 156
902691d4 157 trace_lasips2_reg_write(size, lp->id, addr,
5d2bd735 158 lasips2_write_reg_name(addr), val);
2a6505b0
SS
159
160 switch (addr & 0xc) {
161 case REG_PS2_CONTROL:
902691d4 162 lp->control = val;
2a6505b0
SS
163 break;
164
165 case REG_PS2_XMTDATA:
902691d4
MCA
166 if (lp->control & LASIPS2_CONTROL_LOOPBACK) {
167 lp->buf = val;
168 lp->loopback_rbne = true;
169 qemu_set_irq(lp->irq, 1);
2a6505b0
SS
170 break;
171 }
172
902691d4
MCA
173 if (lp->id) {
174 ps2_write_mouse(PS2_MOUSE_DEVICE(lp->ps2dev), val);
2a6505b0 175 } else {
902691d4 176 ps2_write_keyboard(PS2_KBD_DEVICE(lp->ps2dev), val);
2a6505b0
SS
177 }
178 break;
179
180 case REG_PS2_RESET:
181 break;
182
183 default:
184 qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
185 __func__, addr);
186 break;
187 }
188}
189
190static uint64_t lasips2_reg_read(void *opaque, hwaddr addr, unsigned size)
191{
902691d4 192 LASIPS2Port *lp = LASIPS2_PORT(opaque);
2a6505b0
SS
193 uint64_t ret = 0;
194
195 switch (addr & 0xc) {
196 case REG_PS2_ID:
902691d4 197 ret = lp->id;
2a6505b0
SS
198 break;
199
200 case REG_PS2_RCVDATA:
902691d4
MCA
201 if (lp->control & LASIPS2_CONTROL_LOOPBACK) {
202 lp->loopback_rbne = false;
203 qemu_set_irq(lp->irq, 0);
204 ret = lp->buf;
2a6505b0
SS
205 break;
206 }
207
902691d4 208 ret = ps2_read_data(lp->ps2dev);
2a6505b0
SS
209 break;
210
211 case REG_PS2_CONTROL:
902691d4 212 ret = lp->control;
2a6505b0
SS
213 break;
214
215 case REG_PS2_STATUS:
2a6505b0
SS
216 ret = LASIPS2_STATUS_DATSHD | LASIPS2_STATUS_CLKSHD;
217
902691d4
MCA
218 if (lp->control & LASIPS2_CONTROL_DIAG) {
219 if (!(lp->control & LASIPS2_CONTROL_DATDIR)) {
2a6505b0
SS
220 ret &= ~LASIPS2_STATUS_DATSHD;
221 }
222
902691d4 223 if (!(lp->control & LASIPS2_CONTROL_CLKDIR)) {
2a6505b0
SS
224 ret &= ~LASIPS2_STATUS_CLKSHD;
225 }
226 }
227
902691d4
MCA
228 if (lp->control & LASIPS2_CONTROL_LOOPBACK) {
229 if (lp->loopback_rbne) {
2a6505b0
SS
230 ret |= LASIPS2_STATUS_RBNE;
231 }
232 } else {
902691d4 233 if (!ps2_queue_empty(lp->ps2dev)) {
2a6505b0
SS
234 ret |= LASIPS2_STATUS_RBNE;
235 }
236 }
237
902691d4 238 if (lp->lasips2->int_status) {
212a3003 239 ret |= LASIPS2_STATUS_CMPINTR;
2a6505b0
SS
240 }
241 break;
242
243 default:
244 qemu_log_mask(LOG_UNIMP, "%s: unknown register 0x%02" HWADDR_PRIx "\n",
245 __func__, addr);
246 break;
247 }
2a93d3c1 248
902691d4 249 trace_lasips2_reg_read(size, lp->id, addr,
5d2bd735 250 lasips2_read_reg_name(addr), ret);
2a6505b0
SS
251 return ret;
252}
253
254static const MemoryRegionOps lasips2_reg_ops = {
255 .read = lasips2_reg_read,
256 .write = lasips2_reg_write,
257 .impl = {
258 .min_access_size = 1,
259 .max_access_size = 4,
260 },
2ee1b52d 261 .endianness = DEVICE_BIG_ENDIAN,
2a6505b0
SS
262};
263
1702627c
MCA
264static void lasips2_realize(DeviceState *dev, Error **errp)
265{
266 LASIPS2State *s = LASIPS2(dev);
b7047733 267 LASIPS2Port *lp;
2a6505b0 268
b7047733
MCA
269 lp = LASIPS2_PORT(&s->kbd_port);
270 if (!(qdev_realize(DEVICE(lp), NULL, errp))) {
271 return;
272 }
273
212a3003
MCA
274 qdev_connect_gpio_out(DEVICE(lp), 0,
275 qdev_get_gpio_in_named(dev, "lasips2-port-input-irq",
276 lp->id));
a088ce9b
MCA
277
278 lp = LASIPS2_PORT(&s->mouse_port);
279 if (!(qdev_realize(DEVICE(lp), NULL, errp))) {
280 return;
281 }
282
212a3003
MCA
283 qdev_connect_gpio_out(DEVICE(lp), 0,
284 qdev_get_gpio_in_named(dev, "lasips2-port-input-irq",
285 lp->id));
2a6505b0 286}
653b388c 287
63195aa5
MCA
288static void lasips2_init(Object *obj)
289{
290 LASIPS2State *s = LASIPS2(obj);
b7047733
MCA
291 LASIPS2Port *lp;
292
293 object_initialize_child(obj, "lasips2-kbd-port", &s->kbd_port,
294 TYPE_LASIPS2_KBD_PORT);
a088ce9b
MCA
295 object_initialize_child(obj, "lasips2-mouse-port", &s->mouse_port,
296 TYPE_LASIPS2_MOUSE_PORT);
63195aa5 297
b7047733
MCA
298 lp = LASIPS2_PORT(&s->kbd_port);
299 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &lp->reg);
a088ce9b
MCA
300 lp = LASIPS2_PORT(&s->mouse_port);
301 sysbus_init_mmio(SYS_BUS_DEVICE(obj), &lp->reg);
97bc0597
MCA
302
303 sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
0d1ac496 304
ca735a81
MCA
305 qdev_init_gpio_in_named(DEVICE(obj), lasips2_set_irq,
306 "lasips2-port-input-irq", 2);
63195aa5
MCA
307}
308
42119fdb
MCA
309static void lasips2_class_init(ObjectClass *klass, void *data)
310{
311 DeviceClass *dc = DEVICE_CLASS(klass);
312
1702627c 313 dc->realize = lasips2_realize;
17b8013a 314 dc->vmsd = &vmstate_lasips2;
42119fdb
MCA
315 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
316}
317
653b388c
MCA
318static const TypeInfo lasips2_info = {
319 .name = TYPE_LASIPS2,
320 .parent = TYPE_SYS_BUS_DEVICE,
63195aa5 321 .instance_init = lasips2_init,
42119fdb
MCA
322 .instance_size = sizeof(LASIPS2State),
323 .class_init = lasips2_class_init,
653b388c
MCA
324};
325
d0af5d6a
MCA
326static void lasips2_port_set_irq(void *opaque, int n, int level)
327{
328 LASIPS2Port *s = LASIPS2_PORT(opaque);
329
330 qemu_set_irq(s->irq, level);
331}
332
333static void lasips2_port_realize(DeviceState *dev, Error **errp)
334{
335 LASIPS2Port *s = LASIPS2_PORT(dev);
336
337 qdev_connect_gpio_out(DEVICE(s->ps2dev), PS2_DEVICE_IRQ,
338 qdev_get_gpio_in_named(dev, "ps2-input-irq", 0));
339}
340
8db817be
MCA
341static void lasips2_port_init(Object *obj)
342{
343 LASIPS2Port *s = LASIPS2_PORT(obj);
344
345 qdev_init_gpio_out(DEVICE(obj), &s->irq, 1);
d0af5d6a
MCA
346 qdev_init_gpio_in_named(DEVICE(obj), lasips2_port_set_irq,
347 "ps2-input-irq", 1);
348}
349
350static void lasips2_port_class_init(ObjectClass *klass, void *data)
351{
352 DeviceClass *dc = DEVICE_CLASS(klass);
353
a1e6a5c4
HD
354 /*
355 * The PS/2 mouse port is integreal part of LASI and can not be
356 * created by users without LASI.
357 */
358 dc->user_creatable = false;
d0af5d6a 359 dc->realize = lasips2_port_realize;
8db817be
MCA
360}
361
f8d89a7d
MCA
362static const TypeInfo lasips2_port_info = {
363 .name = TYPE_LASIPS2_PORT,
364 .parent = TYPE_DEVICE,
8db817be 365 .instance_init = lasips2_port_init,
f8d89a7d 366 .instance_size = sizeof(LASIPS2Port),
62201e43
MCA
367 .class_init = lasips2_port_class_init,
368 .class_size = sizeof(LASIPS2PortDeviceClass),
f8d89a7d
MCA
369 .abstract = true,
370};
371
b41eee94
MCA
372static void lasips2_kbd_port_realize(DeviceState *dev, Error **errp)
373{
e2b50aea 374 LASIPS2KbdPort *s = LASIPS2_KBD_PORT(dev);
b41eee94 375 LASIPS2Port *lp = LASIPS2_PORT(dev);
212a3003 376 LASIPS2PortDeviceClass *lpdc = LASIPS2_PORT_GET_CLASS(lp);
b41eee94 377
e2b50aea
MCA
378 if (!sysbus_realize(SYS_BUS_DEVICE(&s->kbd), errp)) {
379 return;
380 }
381
382 lp->ps2dev = PS2_DEVICE(&s->kbd);
212a3003 383 lpdc->parent_realize(dev, errp);
b41eee94
MCA
384}
385
b7047733
MCA
386static void lasips2_kbd_port_init(Object *obj)
387{
388 LASIPS2KbdPort *s = LASIPS2_KBD_PORT(obj);
389 LASIPS2Port *lp = LASIPS2_PORT(obj);
390
391 memory_region_init_io(&lp->reg, obj, &lasips2_reg_ops, lp, "lasips2-kbd",
392 0x100);
e2b50aea
MCA
393
394 object_initialize_child(obj, "kbd", &s->kbd, TYPE_PS2_KBD_DEVICE);
395
b7047733 396 lp->id = 0;
01f6c546 397 lp->lasips2 = container_of(s, LASIPS2State, kbd_port);
b7047733
MCA
398}
399
b41eee94
MCA
400static void lasips2_kbd_port_class_init(ObjectClass *klass, void *data)
401{
402 DeviceClass *dc = DEVICE_CLASS(klass);
d0af5d6a 403 LASIPS2PortDeviceClass *lpdc = LASIPS2_PORT_CLASS(klass);
b41eee94 404
a1e6a5c4
HD
405 /*
406 * The PS/2 keyboard port is integreal part of LASI and can not be
407 * created by users without LASI.
408 */
409 dc->user_creatable = false;
d0af5d6a
MCA
410 device_class_set_parent_realize(dc, lasips2_kbd_port_realize,
411 &lpdc->parent_realize);
b41eee94
MCA
412}
413
ef90a06f
MCA
414static const TypeInfo lasips2_kbd_port_info = {
415 .name = TYPE_LASIPS2_KBD_PORT,
416 .parent = TYPE_LASIPS2_PORT,
417 .instance_size = sizeof(LASIPS2KbdPort),
b7047733 418 .instance_init = lasips2_kbd_port_init,
b41eee94 419 .class_init = lasips2_kbd_port_class_init,
ef90a06f
MCA
420};
421
8d490f8d
MCA
422static void lasips2_mouse_port_realize(DeviceState *dev, Error **errp)
423{
d316983c 424 LASIPS2MousePort *s = LASIPS2_MOUSE_PORT(dev);
8d490f8d 425 LASIPS2Port *lp = LASIPS2_PORT(dev);
212a3003 426 LASIPS2PortDeviceClass *lpdc = LASIPS2_PORT_GET_CLASS(lp);
8d490f8d 427
d316983c
MCA
428 if (!sysbus_realize(SYS_BUS_DEVICE(&s->mouse), errp)) {
429 return;
430 }
431
432 lp->ps2dev = PS2_DEVICE(&s->mouse);
212a3003 433 lpdc->parent_realize(dev, errp);
8d490f8d
MCA
434}
435
a088ce9b
MCA
436static void lasips2_mouse_port_init(Object *obj)
437{
438 LASIPS2MousePort *s = LASIPS2_MOUSE_PORT(obj);
439 LASIPS2Port *lp = LASIPS2_PORT(obj);
440
441 memory_region_init_io(&lp->reg, obj, &lasips2_reg_ops, lp, "lasips2-mouse",
442 0x100);
d316983c
MCA
443
444 object_initialize_child(obj, "mouse", &s->mouse, TYPE_PS2_MOUSE_DEVICE);
445
a088ce9b 446 lp->id = 1;
01f6c546 447 lp->lasips2 = container_of(s, LASIPS2State, mouse_port);
a088ce9b
MCA
448}
449
8d490f8d
MCA
450static void lasips2_mouse_port_class_init(ObjectClass *klass, void *data)
451{
452 DeviceClass *dc = DEVICE_CLASS(klass);
d0af5d6a 453 LASIPS2PortDeviceClass *lpdc = LASIPS2_PORT_CLASS(klass);
8d490f8d 454
d0af5d6a
MCA
455 device_class_set_parent_realize(dc, lasips2_mouse_port_realize,
456 &lpdc->parent_realize);
8d490f8d
MCA
457}
458
cb5827ce
MCA
459static const TypeInfo lasips2_mouse_port_info = {
460 .name = TYPE_LASIPS2_MOUSE_PORT,
461 .parent = TYPE_LASIPS2_PORT,
462 .instance_size = sizeof(LASIPS2MousePort),
a088ce9b 463 .instance_init = lasips2_mouse_port_init,
8d490f8d 464 .class_init = lasips2_mouse_port_class_init,
cb5827ce
MCA
465};
466
653b388c
MCA
467static void lasips2_register_types(void)
468{
469 type_register_static(&lasips2_info);
f8d89a7d 470 type_register_static(&lasips2_port_info);
ef90a06f 471 type_register_static(&lasips2_kbd_port_info);
cb5827ce 472 type_register_static(&lasips2_mouse_port_info);
653b388c
MCA
473}
474
475type_init(lasips2_register_types)