virtio_spec/
console.rs

1//! Console Device
2
3use num_enum::{FromPrimitive, IntoPrimitive};
4use volatile::access::ReadOnly;
5use volatile_macro::VolatileFieldAccess;
6
7pub use super::features::console::F;
8use crate::{le16, le32};
9
10/// Console Device Configuration Layout
11///
12/// Use [`ConfigVolatileFieldAccess`] to work with this struct.
13#[doc(alias = "virtio_console_config")]
14#[cfg_attr(
15    feature = "zerocopy",
16    derive(
17        zerocopy_derive::KnownLayout,
18        zerocopy_derive::Immutable,
19        zerocopy_derive::FromBytes,
20    )
21)]
22#[derive(VolatileFieldAccess)]
23#[repr(C)]
24pub struct Config {
25    #[access(ReadOnly)]
26    cols: le16,
27    #[access(ReadOnly)]
28    rows: le16,
29    #[access(ReadOnly)]
30    max_nr_ports: le32,
31    #[access(ReadOnly)]
32    emerg_wr: le32,
33}
34
35/// Control Message
36#[doc(alias = "virtio_console_control")]
37#[cfg_attr(
38    feature = "zerocopy",
39    derive(
40        zerocopy_derive::KnownLayout,
41        zerocopy_derive::Immutable,
42        zerocopy_derive::FromBytes,
43        zerocopy_derive::IntoBytes,
44    )
45)]
46#[derive(Clone, Copy, Debug)]
47#[repr(C)]
48pub struct Control {
49    /// Port number
50    pub id: le32,
51    /// The kind of control event
52    pub event: le16,
53    /// Extra information for the event
54    pub value: le16,
55}
56
57/// Event
58///
59/// <div class="warning">
60///
61/// This enum is not ABI-compatible with it's corresponding field.
62/// Use [`Device::from`] for converting from an integer.
63///
64/// </div>
65///
66/// [`Device::from`]: Device#impl-From<u16>-for-Device
67#[doc(alias = "VIRTIO_CONSOLE")]
68#[derive(IntoPrimitive, FromPrimitive, PartialEq, Eq, Clone, Copy, Debug)]
69#[non_exhaustive]
70#[repr(u16)]
71pub enum Device {
72    /// Sent by the driver at initialization to indicate that it is ready to receive control messages.
73    ///
74    /// A value of 1 indicates success, and 0 indicates failure.
75    /// The port number `id` is unused.
76    #[doc(alias = "VIRTIO_CONSOLE_DEVICE_READY")]
77    DeviceReady = 0,
78
79    /// Sent by the device, to create a new port.
80    ///
81    /// `value` is unused.
82    #[doc(alias = "VIRTIO_CONSOLE_DEVICE_ADD")]
83    DeviceAdd = 1,
84
85    /// Sent by the device, to remove an existing port.
86    ///
87    /// `value` is unused.
88    #[doc(alias = "VIRTIO_CONSOLE_DEVICE_REMOVE")]
89    DeviceRemove = 2,
90
91    /// Sent by the driver in response to the device's VIRTIO_CONSOLE_PORT_ADD message, to indicate that the port is ready to be used.
92    ///
93    /// A `value` of 1 indicates success, and 0 indicates failure.
94    #[doc(alias = "VIRTIO_CONSOLE_PORT_READY")]
95    PortReady = 3,
96
97    /// Sent by the device to nominate a port as a console port.
98    ///
99    /// There MAY be more than one console port.
100    #[doc(alias = "VIRTIO_CONSOLE_CONSOLE_PORT")]
101    ConsolePort = 4,
102
103    /// Sent by the device to indicate a console size change.
104    ///
105    /// `value` is unused.
106    /// The buffer is followed by the number of columns and rows ([`virtio_console_resize`]).
107    ///
108    /// [`virtio_console_resize`]: Resize
109    #[doc(alias = "VIRTIO_CONSOLE_RESIZE")]
110    Resize = 5,
111
112    /// This message is sent by both the device and the driver.
113    ///
114    /// `value` indicates the state: 0 (port closed) or 1 (port open).
115    /// This allows for ports to be used directly by guest and host processes to communicate in an application-defined manner.
116    #[doc(alias = "VIRTIO_CONSOLE_PORT_OPEN")]
117    PortOpen = 6,
118
119    /// Sent by the device to give a tag to the port.
120    ///
121    /// This control command is immediately followed by the UTF-8 name of the port for identification within the guest (without a NUL terminator).
122    #[doc(alias = "VIRTIO_CONSOLE_PORT_NAME")]
123    PortName = 7,
124
125    #[num_enum(catch_all)]
126    Unknown(u16),
127}
128
129/// Resize Message Layout
130#[doc(alias = "virtio_console_resize")]
131#[cfg_attr(
132    feature = "zerocopy",
133    derive(
134        zerocopy_derive::KnownLayout,
135        zerocopy_derive::Immutable,
136        zerocopy_derive::FromBytes,
137        zerocopy_derive::IntoBytes,
138    )
139)]
140#[derive(Clone, Copy, Debug)]
141#[repr(C)]
142pub struct Resize {
143    pub cols: le16,
144    pub rows: le16,
145}