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                return UsbControlResult::NoSuchDevice;
195            }
196        };
197
198        // Resetting the device is used to make sure it is in a known state, but it may
199        // still function if the reset fails.
200        if let Err(e) = host_device.lock().reset() {
201            error!("failed to reset device during attach: {:?}", e);
202        }
203
204        self.attach_backend(host_device, event_handler, EventType::ReadWrite)
205    }
206
207    fn handle_detach_device(&self, port: u8) -> UsbControlResult {
208        match self.usb_hub.disconnect_port(port) {
209            Ok(()) => {
210                // The device enters a detaching state in hub.disconnect_port(). We'll remove the
211                // event handler from the event loop in on_event() to properly wait and clean-up
212                // all the in-flight transfers.
213                if let Some(event_handler) = self.devices.lock().remove(&port) {
214                    event_handler.signal_detach();
215                }
216                UsbControlResult::Ok { port }
217            }
218            Err(e) => {
219                error!("failed to disconnect device from port {}: {}", port, e);
220                UsbControlResult::NoSuchDevice
221            }
222        }
223    }
224
225    fn handle_attach_security_key(&self, hidraw: File) -> UsbControlResult {
226        let (fido_device, event_handler) = match attach_security_key(
227            hidraw,
228            self.event_loop.clone(),
229            DeviceState::new(self.fail_handle.clone(), self.job_queue.clone()),
230        ) {
231            Ok((fido_device, event_handler)) => (fido_device, event_handler),
232            Err(e) => {
233                error!(
234                    "could not create a virtual fido device from the given file: {}",
235                    e
236                );
237                return UsbControlResult::NoSuchDevice;
238            }
239        };
240
241        // Reset the device to make sure it's in a usable state.
242        // Resetting it also stops polling on the FD, since we only poll when there is an active
243        // transaction.
244        if let Err(e) = fido_device.lock().reset() {
245            error!("failed to reset fido device during attach: {:?}", e);
246        }
247
248        self.attach_backend(fido_device, event_handler, EventType::Read)
249    }
250
251    fn handle_list_devices(&self, ports: [u8; USB_CONTROL_MAX_PORTS]) -> UsbControlResult {
252        let mut devices: [UsbControlAttachedDevice; USB_CONTROL_MAX_PORTS] = Default::default();
253        for (result_index, &port_id) in ports.iter().enumerate() {
254            match self.usb_hub.get_port(port_id).and_then(|p| {
255                p.backend_device()
256                    .as_ref()
257                    .map(|d| d.lock())
258                    .map(|d| (d.get_vid(), d.get_pid()))
259            }) {
260                Some((vendor_id, product_id)) => {
261                    devices[result_index] = UsbControlAttachedDevice {
262                        port: port_id,
263                        vendor_id,
264                        product_id,
265                    }
266                }
267                None => continue,
268            }
269        }
270        UsbControlResult::Devices(devices)
271    }
272
273    fn on_event_helper(&self) -> Result<()> {
274        let tube = self.control_tube.lock();
275        let cmd = tube.recv().map_err(Error::ReadControlTube)?;
276        let result = match cmd {
277            UsbControlCommand::AttachDevice { file } => self.handle_attach_device(file),
278            UsbControlCommand::AttachSecurityKey { file } => self.handle_attach_security_key(file),
279            UsbControlCommand::DetachDevice { port } => self.handle_detach_device(port),
280            UsbControlCommand::ListDevice { ports } => self.handle_list_devices(ports),
281        };
282        tube.send(&result).map_err(Error::WriteControlTube)?;
283        Ok(())
284    }
285}
286
287impl EventHandler for ProviderInner {
288    fn on_event(&self) -> anyhow::Result<()> {
289        self.on_event_helper()
290            .context("host backend device provider failed")
291    }
292}