// Copyright 2024 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34//! Virtio console device output handling.56use std::io;7use std::io::Read;89use base::error;1011use crate::virtio::Queue;12use crate::virtio::Reader;1314/// Writes the available data from the reader into the given output queue.15///16/// # Arguments17///18/// * `reader` - The Reader with the data we want to write.19/// * `output` - The output sink we are going to write the data to.20fn process_transmit_request(reader: &mut Reader, output: &mut dyn io::Write) -> io::Result<()> {21let len = reader.available_bytes();22let mut data = vec![0u8; len];23reader.read_exact(&mut data)?;24output.write_all(&data)?;25output.flush()?;26Ok(())27}2829/// Processes the data taken from the given transmit queue into the output sink.30///31/// # Arguments32///33/// * `interrupt` - Interrupt used to signal (if required) that the queue has been used34/// * `transmit_queue` - The transmit virtio Queue35/// * `output` - The output sink we are going to write the data into36pub fn process_transmit_queue(transmit_queue: &mut Queue, output: &mut dyn io::Write) {37let mut needs_interrupt = false;38while let Some(mut avail_desc) = transmit_queue.pop() {39if let Err(e) = process_transmit_request(&mut avail_desc.reader, output) {40error!("console: process_transmit_request failed: {}", e);41}4243transmit_queue.add_used(avail_desc);44needs_interrupt = true;45}4647if needs_interrupt {48transmit_queue.trigger_interrupt();49}50}515253