xref: /aosp_15_r20/external/crosvm/vhost/src/scmi.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2023 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 use std::fs::File;
6 use std::fs::OpenOptions;
7 use std::os::unix::fs::OpenOptionsExt;
8 use std::path::Path;
9 
10 use base::AsRawDescriptor;
11 use base::RawDescriptor;
12 
13 use super::Error;
14 use super::Result;
15 use super::Vhost;
16 
17 /// Handle for running VHOST_SCMI ioctls.
18 pub struct Scmi {
19     descriptor: File,
20 }
21 
22 impl Scmi {
23     /// Open a handle to a new VHOST_SCMI instance.
new(vhost_scmi_device_path: &Path) -> Result<Scmi>24     pub fn new(vhost_scmi_device_path: &Path) -> Result<Scmi> {
25         Ok(Scmi {
26             descriptor: OpenOptions::new()
27                 .read(true)
28                 .write(true)
29                 .custom_flags(libc::O_CLOEXEC | libc::O_NONBLOCK)
30                 .open(vhost_scmi_device_path)
31                 .map_err(Error::VhostOpen)?,
32         })
33     }
34 }
35 
36 impl Vhost for Scmi {}
37 
38 impl AsRawDescriptor for Scmi {
as_raw_descriptor(&self) -> RawDescriptor39     fn as_raw_descriptor(&self) -> RawDescriptor {
40         self.descriptor.as_raw_descriptor()
41     }
42 }
43