devices/usb/backend/host_backend/
host_backend_device_provider.rs

1// Copyright 2019 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::fs::File;
6use std::sync::Arc;
7
8use sync::Mutex;
9use usb_util::Device;
10
11use crate::usb::backend::device::BackendDeviceType;
12use crate::usb::backend::device::DeviceState;
13use crate::usb::backend::error::Error;
14use crate::usb::backend::error::Result;
15use crate::usb::backend::host_backend::host_device::HostDevice;
16use crate::usb::backend::utils::UsbUtilEventHandler;
17use crate::utils::EventLoop;
18
19/// Attaches a host device to the backend. This creates a host USB device object by opening a
20/// usbdevfs file and returns a pair of the device and its fd event handler.
21pub(crate) fn attach_host_backend_device(
22    usb_file: File,
23    event_loop: Arc<EventLoop>,
24    device_state: DeviceState,
25) -> Result<(Arc<Mutex<BackendDeviceType>>, Arc<UsbUtilEventHandler>)> {
26    let device = Device::new(usb_file).map_err(Error::CreateHostUsbDevice)?;
27    let host_device = HostDevice::new(Arc::new(Mutex::new(device)), device_state)?;
28    let device_impl = BackendDeviceType::HostDevice(host_device);
29    let arc_mutex_device = Arc::new(Mutex::new(device_impl));
30    let event_handler = UsbUtilEventHandler::new(arc_mutex_device.clone(), event_loop);
31
32    Ok((arc_mutex_device, event_handler))
33}