devices/usb/backend/
device_provider.rs

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
5use std::collections::HashMap;
6use std::fs::File;
7use std::mem;
8use std::sync::Arc;
9use std::time::Duration;
10
11use anyhow::Context;
12use base::error;
13use base::AsRawDescriptor;
14use base::EventType;
15use base::RawDescriptor;
16use base::Tube;
17use sync::Mutex;
18use vm_control::UsbControlAttachedDevice;
19use vm_control::UsbControlCommand;
20use vm_control::UsbControlResult;
21use vm_control::USB_CONTROL_MAX_PORTS;
22
23use crate::usb::backend::device::BackendDeviceType;
24use crate::usb::backend::device::DeviceState;
25use crate::usb::backend::error::Error;
26use crate::usb::backend::error::Result;
27use crate::usb::backend::fido_backend::fido_provider::attach_security_key;
28use crate::usb::backend::host_backend::host_backend_device_provider::attach_host_backend_device;
29use crate::usb::backend::utils::UsbUtilEventHandler;
30use crate::usb::xhci::usb_hub::UsbHub;
31use crate::usb::xhci::xhci_backend_device::XhciBackendDevice;
32use crate::usb::xhci::xhci_backend_device_provider::XhciBackendDeviceProvider;
33use crate::utils::AsyncJobQueue;
34use crate::utils::EventHandler;
35use crate::utils::EventLoop;
36use crate::utils::FailHandle;
37
38const SOCKET_TIMEOUT_MS: u64 = 2000;
39
40/// Device provider is an xhci backend device provider that provides generic semantics to handle
41/// various types of backend devices and connects them to the xhci layer.
42pub enum DeviceProvider {
43    // The provider is created but not yet started.
44    Created { control_tube: Mutex<Tube> },
45    // The provider is started on an event loop.
46    Started { inner: Arc<ProviderInner> },
47    // The provider has failed.
48    Failed,
49}
50
51impl DeviceProvider {
52    pub fn new() -> Result<(Tube, DeviceProvider)> {
53        let (child_tube, control_tube) = Tube::pair().map_err(Error::CreateControlTube)?;
54        control_tube
55            .set_send_timeout(Some(Duration::from_millis(SOCKET_TIMEOUT_MS)))
56            .map_err(Error::SetupControlTube)?;
57        control_tube
58            .set_recv_timeout(Some(Duration::from_millis(SOCKET_TIMEOUT_MS)))
59            .map_err(Error::SetupControlTube)?;
60
61        let provider = DeviceProvider::Created {
62            control_tube: Mutex::new(child_tube),
63        };
64        Ok((control_tube, provider))
65    }
66
67    fn start_helper(
68        &mut self,
69        fail_handle: Arc<dyn FailHandle>,
70        event_loop: Arc<EventLoop>,
71        hub: Arc<UsbHub>,
72    ) -> Result<()> {
73        match mem::replace(self, DeviceProvider::Failed) {
74            DeviceProvider::Created { control_tube } => {
75                let job_queue =
76                    AsyncJobQueue::init(&event_loop).map_err(Error::StartAsyncJobQueue)?;
77                let inner = Arc::new(ProviderInner::new(
78                    fail_handle,
79                    job_queue,
80                    event_loop.clone(),
81                    control_tube,
82                    hub,
83                ));
84                let handler: Arc<dyn EventHandler> = inner.clone();
85                event_loop
86                    .add_event(
87                        &*inner.control_tube.lock(),
88                        EventType::Read,
89                        Arc::downgrade(&handler),
90                    )
91                    .map_err(Error::AddToEventLoop)?;
92                *self = DeviceProvider::Started { inner };
93                Ok(())
94            }
95            DeviceProvider::Started { .. } => {
96                error!("Usb device provider has already started");
97                Err(Error::BadBackendProviderState)
98            }
99            DeviceProvider::Failed => {
100                error!("Usb device provider has already failed");
101                Err(Error::BadBackendProviderState)
102            }
103        }
104    }
105}
106
107impl XhciBackendDeviceProvider for DeviceProvider {
108    fn start(
109        &mut self,
110        fail_handle: Arc<dyn FailHandle>,
111        event_loop: Arc<EventLoop>,
112        hub: Arc<UsbHub>,
113    ) -> Result<()> {
114        self.start_helper(fail_handle, event_loop, hub)
115    }
116
117    fn keep_rds(&self) -> Vec<RawDescriptor> {
118        match self {
119            DeviceProvider::Created { control_tube } => {
120                vec![control_tube.lock().as_raw_descriptor()]
121            }
122            _ => {
123                error!("Trying to get keepfds when DeviceProvider is not in created state");
124                vec![]
125            }
126        }
127    }
128}
129
130/// ProviderInner listens to control socket.
131pub struct ProviderInner {
132    fail_handle: Arc<dyn FailHandle>,
133    job_queue: Arc<AsyncJobQueue>,
134    event_loop: Arc<EventLoop>,
135    control_tube: Mutex<Tube>,
136    usb_hub: Arc<UsbHub>,
137
138    // Map of USB hub port number to per-device context.
139    devices: Mutex<HashMap<u8, Arc<UsbUtilEventHandler>>>,
140}
141
142impl ProviderInner {
143    fn new(
144        fail_handle: Arc<dyn FailHandle>,
145        job_queue: Arc<AsyncJobQueue>,
146        event_loop: Arc<EventLoop>,
147        control_tube: Mutex<Tube>,
148        usb_hub: Arc<UsbHub>,
149    ) -> ProviderInner {
150        ProviderInner {
151            fail_handle,
152            job_queue,
153            event_loop,
154            control_tube,
155            usb_hub,
156            devices: Mutex::new(HashMap::new()),
157        }
158    }
159
160    fn attach_backend(
161        &self,
162        device: Arc<Mutex<BackendDeviceType>>,
163        event_handler: Arc<UsbUtilEventHandler>,
164        event_type: EventType,
165    ) -> UsbControlResult {
166        let hub_port = match self.usb_hub.connect_backend(device.clone()) {
167            Ok(port) => port,
168            Err(e) => {
169                error!("failed to connect device to hub: {}", e);
170                return UsbControlResult::NoAvailablePort;
171            }
172        };
173
174        let port_id = hub_port.port_id();
175        if let Err(e) = event_handler.activate(event_type, Arc::downgrade(&hub_port)) {
176            error!("failed to activate USB event handler: {}", e);
177            let _ = self.usb_hub.disconnect_port(port_id);
178            return UsbControlResult::FailedToOpenDevice;
179        }
180
181        self.devices.lock().insert(port_id, event_handler);
182        UsbControlResult::Ok { port: port_id }
183    }
184
185    fn handle_attach_device(&self, usb_file: File) -> UsbControlResult {
186        let (host_device, event_handler) = match attach_host_backend_device(
187            usb_file,
188            self.event_loop.clone(),
189            DeviceState::new(self.fail_handle.clone(), self.job_queue.clone()),
190        ) {
191            Ok((host_device, event_handler)) => (host_device, event_handler),
192            Err(e) => {
193                error!("could not construct USB device from the given file: {}", e);
194                // The device file itself was valid; constructing state for it is what failed, so
195                // report this as a host-device-init failure rather than an enumeration error.
196                return UsbControlResult::FailedToInitHostDevice;
197            }
198        };
199
200        // Resetting the device is used to make sure it is in a known state, but it may
201        // still function if the reset fails.
202        if let Err(e) = host_device.lock().reset() {
203            error!("failed to reset device during attach: {:?}", e);
204        }
205
206        self.attach_backend(host_device, event_handler, EventType::ReadWrite)
207    }
208
209    fn handle_detach_device(&self, port: u8) -> UsbControlResult {
210        match self.usb_hub.disconnect_port(port) {
211            Ok(()) => {
212                // The device enters a detaching state in hub.disconnect_port(). We'll remove the
213                // event handler from the event loop in on_event() to properly wait and clean-up
214                // all the in-flight transfers.
215                if let Some(event_handler) = self.devices.lock().remove(&port) {
216                    event_handler.signal_detach();
217                }
218                UsbControlResult::Ok { port }
219            }
220            Err(e) => {
221                error!("failed to disconnect device from port {}: {}", port, e);
222                UsbControlResult::NoSuchDevice
223            }
224        }
225    }
226
227    fn handle_attach_security_key(&self, hidraw: File) -> UsbControlResult {
228        let (fido_device, event_handler) = match attach_security_key(
229            hidraw,
230            self.event_loop.clone(),
231            DeviceState::new(self.fail_handle.clone(), self.job_queue.clone()),
232        ) {
233            Ok((fido_device, event_handler)) => (fido_device, event_handler),
234            Err(e) => {
235                error!(
236                    "could not create a virtual fido device from the given file: {}",
237                    e
238                );
239                return UsbControlResult::NoSuchDevice;
240            }
241        };
242
243        // Reset the device to make sure it's in a usable state.
244        // Resetting it also stops polling on the FD, since we only poll when there is an active
245        // transaction.
246        if let Err(e) = fido_device.lock().reset() {
247            error!("failed to reset fido device during attach: {:?}", e);
248        }
249
250        self.attach_backend(fido_device, event_handler, EventType::Read)
251    }
252
253    fn handle_list_devices(&self, ports: [u8; USB_CONTROL_MAX_PORTS]) -> UsbControlResult {
254        let mut devices: [UsbControlAttachedDevice; USB_CONTROL_MAX_PORTS] = Default::default();
255        for (result_index, &port_id) in ports.iter().enumerate() {
256            match self.usb_hub.get_port(port_id).and_then(|p| {
257                p.backend_device()
258                    .as_ref()
259                    .map(|d| d.lock())
260                    .map(|d| (d.get_vid(), d.get_pid()))
261            }) {
262                Some((vendor_id, product_id)) => {
263                    devices[result_index] = UsbControlAttachedDevice {
264                        port: port_id,
265                        vendor_id,
266                        product_id,
267                    }
268                }
269                None => continue,
270            }
271        }
272        UsbControlResult::Devices(devices)
273    }
274
275    fn on_event_helper(&self) -> Result<()> {
276        let tube = self.control_tube.lock();
277        let cmd = tube.recv().map_err(Error::ReadControlTube)?;
278        let result = match cmd {
279            UsbControlCommand::AttachDevice { file } => self.handle_attach_device(file),
280            UsbControlCommand::AttachSecurityKey { file } => self.handle_attach_security_key(file),
281            UsbControlCommand::DetachDevice { port } => self.handle_detach_device(port),
282            UsbControlCommand::ListDevice { ports } => self.handle_list_devices(ports),
283        };
284        tube.send(&result).map_err(Error::WriteControlTube)?;
285        Ok(())
286    }
287}
288
289impl EventHandler for ProviderInner {
290    fn on_event(&self) -> anyhow::Result<()> {
291        self.on_event_helper()
292            .context("host backend device provider failed")
293    }
294}