Skip to content

Commit ed0f2dd

Browse files
authored
Merge pull request #1 from stlankes/console
add support of console
2 parents a49989e + 2569b17 commit ed0f2dd

File tree

3 files changed

+195
-21
lines changed

3 files changed

+195
-21
lines changed

src/console.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
//! Console Device
2+
3+
use num_enum::{FromPrimitive, IntoPrimitive};
4+
use volatile::access::ReadOnly;
5+
use volatile_macro::VolatileFieldAccess;
6+
7+
pub use super::features::console::F;
8+
use 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)]
24+
pub 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)]
48+
pub 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)]
71+
pub 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)]
142+
pub struct Resize {
143+
pub cols: le16,
144+
pub rows: le16,
145+
}

src/features.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,34 @@ macro_rules! feature_bits {
289289
() => {};
290290
}
291291

292+
pub mod console {
293+
use crate::le128;
294+
295+
feature_bits! {
296+
/// Console Device Feature Bits
297+
#[doc(alias = "VIRTIO_CONSOLE_F")]
298+
pub struct F: le128 {
299+
/// Configuration `cols` and `rows` are valid.
300+
#[doc(alias = "VIRTIO_CONSOLE_F_SIZE")]
301+
const SIZE = 1 << 0;
302+
303+
/// Device has support for multiple ports;
304+
///
305+
/// `max_nr_ports` is valid and control virtqueues will be used.
306+
#[doc(alias = "VIRTIO_CONSOLE_F_MULTIPORT")]
307+
const MULTIPORT = 1 << 1;
308+
309+
/// Device has support for emergency write.
310+
///
311+
/// Configuration field emerg_wr is valid.
312+
#[doc(alias = "VIRTIO_CONSOLE_F_EMERG_WRITE")]
313+
const EMERG_WRITE = 1 << 2;
314+
}
315+
}
316+
317+
impl crate::FeatureBits for F {}
318+
}
319+
292320
pub mod net {
293321
use crate::le128;
294322

src/lib.rs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,27 @@
6161
//!
6262
//! ## Device Types
6363
//!
64-
//! | Device Type | Available | Module |
65-
//! | --------------------------------- | --------- | --------- |
66-
//! | Network Device | ✅ | [`net`] |
67-
//! | Block Device | ❌ | |
68-
//! | Console Device | | |
69-
//! | Entropy Device | ❌ | |
70-
//! | Traditional Memory Balloon Device | ❌ | |
71-
//! | SCSI Host Device | ❌ | |
72-
//! | GPU Device | ❌ | |
73-
//! | Input Device | ❌ | |
74-
//! | Crypto Device | ❌ | |
75-
//! | Socket Device | ✅ | [`vsock`] |
76-
//! | File System Device | ✅ | [`fs`] |
77-
//! | RPMB Device | ❌ | |
78-
//! | IOMMU Device | ❌ | |
79-
//! | Sound Device | ❌ | |
80-
//! | Memory Device | ❌ | |
81-
//! | I2C Adapter Device | ❌ | |
82-
//! | SCMI Device | ❌ | |
83-
//! | GPIO Device | ❌ | |
84-
//! | PMEM Device | ❌ | |
64+
//! | Device Type | Available | Module |
65+
//! | --------------------------------- | --------- | ----------- |
66+
//! | Network Device | ✅ | [`net`] |
67+
//! | Block Device | ❌ | |
68+
//! | Console Device | | [`console`] |
69+
//! | Entropy Device | ❌ | |
70+
//! | Traditional Memory Balloon Device | ❌ | |
71+
//! | SCSI Host Device | ❌ | |
72+
//! | GPU Device | ❌ | |
73+
//! | Input Device | ❌ | |
74+
//! | Crypto Device | ❌ | |
75+
//! | Socket Device | ✅ | [`vsock`] |
76+
//! | File System Device | ✅ | [`fs`] |
77+
//! | RPMB Device | ❌ | |
78+
//! | IOMMU Device | ❌ | |
79+
//! | Sound Device | ❌ | |
80+
//! | Memory Device | ❌ | |
81+
//! | I2C Adapter Device | ❌ | |
82+
//! | SCMI Device | ❌ | |
83+
//! | GPIO Device | ❌ | |
84+
//! | PMEM Device | ❌ | |
8585
8686
#![cfg_attr(not(test), no_std)]
8787
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
@@ -94,6 +94,7 @@ extern crate alloc;
9494
mod bitflags;
9595
#[macro_use]
9696
pub mod volatile;
97+
pub mod console;
9798
#[cfg(any(feature = "mmio", feature = "pci"))]
9899
mod driver_notifications;
99100
mod features;

0 commit comments

Comments
 (0)