net: invoke callback when purging queue
[qemu/qmp-unstable.git] / hw / char / lm32_juart.c
blob628a86fc06dc73c1f618a085efa0fc989d8475a8
1 /*
2 * LatticeMico32 JTAG UART model.
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 #include "hw/hw.h"
21 #include "hw/sysbus.h"
22 #include "trace.h"
23 #include "sysemu/char.h"
25 #include "hw/char/lm32_juart.h"
27 enum {
28 LM32_JUART_MIN_SAVE_VERSION = 0,
29 LM32_JUART_CURRENT_SAVE_VERSION = 0,
30 LM32_JUART_MAX_SAVE_VERSION = 0,
33 enum {
34 JTX_FULL = (1<<8),
37 enum {
38 JRX_FULL = (1<<8),
41 #define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
43 struct LM32JuartState {
44 SysBusDevice parent_obj;
46 CharDriverState *chr;
48 uint32_t jtx;
49 uint32_t jrx;
51 typedef struct LM32JuartState LM32JuartState;
53 uint32_t lm32_juart_get_jtx(DeviceState *d)
55 LM32JuartState *s = LM32_JUART(d);
57 trace_lm32_juart_get_jtx(s->jtx);
58 return s->jtx;
61 uint32_t lm32_juart_get_jrx(DeviceState *d)
63 LM32JuartState *s = LM32_JUART(d);
65 trace_lm32_juart_get_jrx(s->jrx);
66 return s->jrx;
69 void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
71 LM32JuartState *s = LM32_JUART(d);
72 unsigned char ch = jtx & 0xff;
74 trace_lm32_juart_set_jtx(s->jtx);
76 s->jtx = jtx;
77 if (s->chr) {
78 qemu_chr_fe_write_all(s->chr, &ch, 1);
82 void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
84 LM32JuartState *s = LM32_JUART(d);
86 trace_lm32_juart_set_jrx(s->jrx);
87 s->jrx &= ~JRX_FULL;
90 static void juart_rx(void *opaque, const uint8_t *buf, int size)
92 LM32JuartState *s = opaque;
94 s->jrx = *buf | JRX_FULL;
97 static int juart_can_rx(void *opaque)
99 LM32JuartState *s = opaque;
101 return !(s->jrx & JRX_FULL);
104 static void juart_event(void *opaque, int event)
108 static void juart_reset(DeviceState *d)
110 LM32JuartState *s = LM32_JUART(d);
112 s->jtx = 0;
113 s->jrx = 0;
116 static int lm32_juart_init(SysBusDevice *dev)
118 LM32JuartState *s = LM32_JUART(dev);
120 s->chr = qemu_char_get_next_serial();
121 if (s->chr) {
122 qemu_chr_add_handlers(s->chr, juart_can_rx, juart_rx, juart_event, s);
125 return 0;
128 static const VMStateDescription vmstate_lm32_juart = {
129 .name = "lm32-juart",
130 .version_id = 1,
131 .minimum_version_id = 1,
132 .fields = (VMStateField[]) {
133 VMSTATE_UINT32(jtx, LM32JuartState),
134 VMSTATE_UINT32(jrx, LM32JuartState),
135 VMSTATE_END_OF_LIST()
139 static void lm32_juart_class_init(ObjectClass *klass, void *data)
141 DeviceClass *dc = DEVICE_CLASS(klass);
142 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
144 k->init = lm32_juart_init;
145 dc->reset = juart_reset;
146 dc->vmsd = &vmstate_lm32_juart;
149 static const TypeInfo lm32_juart_info = {
150 .name = TYPE_LM32_JUART,
151 .parent = TYPE_SYS_BUS_DEVICE,
152 .instance_size = sizeof(LM32JuartState),
153 .class_init = lm32_juart_class_init,
156 static void lm32_juart_register_types(void)
158 type_register_static(&lm32_juart_info);
161 type_init(lm32_juart_register_types)