[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: simple serial device emulation
From: |
Philippe Mathieu-Daudé |
Subject: |
Re: simple serial device emulation |
Date: |
Fri, 10 Sep 2021 23:49:19 +0200 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 |
On 9/10/21 9:35 PM, Hinko Kocevar wrote:
> I have an emulated MMIO area holding couple of registers that deal with
> serial UART. Very simple access to the Tx and Rx registers from the
> userspace point of view involves polling for a bit in one register and
> then writing another; when there is room for another character. When the
> guest app does write to a MMIO Tx register, as expected, io_writex() is
> invoked and my handler is invoked. At the moment it does not do much.
> I'm thinking now that the character needs to be fed to the serial device
> instance or something.
>
> Where should I look for suitable examples in the qemu code? I reckon
> that other machines exist that do the similar. I found lots of
> serial_mm_init() and sysbus_mmio_map() uses around serial port instances
> but I'm not sure how to couple my "serial ops" to the "bus" or SerialMM
> (if that is the way to go).
Your device is a "character device frontend". See the API in
include/chardev/char-fe.h. Frontends can be connected to various
backends. The simplest backend is the standard input/output
(named 'stdio').
To be useful your frontend have to implement some handlers:
IOEventHandler, IOCanReadHandler, IOReadHandler. The frontend
register these handlers by calling qemu_chr_fe_set_handlers()
(usually in the DeviceRealize() handler).
The backends will interact with your device via this API.
I recommend you to look at the hw/char/digic-uart.c model which is
quite simple, it returns the last char received, and only transmit
one char per I/O.
Then for a more complete (and up to date) model you can look at
hw/char/goldfish_tty.c, it uses a FIFO to receive chars, but still
transmit one char at a time.
Finally the hw/char/serial.c is probably the most complete models,
with 2 FIFOs (RX & TX) and try to respect timings.
Regards,
Phil.