summary refs log tree commit diff
path: root/devices/src/usb/host_backend/hotplug.rs
blob: 8abcfd6c457ca1b5c205fb5af04c9d83a3a89372 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright 2019 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use std::sync::Arc;

use crate::usb::usb_util::hotplug::{HotplugEvent, UsbHotplugHandler};
use crate::usb::usb_util::libusb_device::LibUsbDevice;
use crate::usb::xhci::usb_hub::UsbHub;

pub struct HotplugHandler {
    hub: Arc<UsbHub>,
}

impl HotplugHandler {
    pub fn new(hub: Arc<UsbHub>) -> Self {
        HotplugHandler { hub }
    }
}

impl UsbHotplugHandler for HotplugHandler {
    fn hotplug_event(&self, device: LibUsbDevice, event: HotplugEvent) {
        match event {
            HotplugEvent::DeviceLeft => {
                let bus = device.get_bus_number();
                let address = device.get_address();
                let descriptor = match device.get_device_descriptor() {
                    Ok(d) => d,
                    Err(e) => {
                        error!("cannot get device descriptor: {:?}", e);
                        return;
                    }
                };
                let vid = descriptor.idVendor;
                let pid = descriptor.idProduct;

                if let Err(e) = self.hub.try_detach(bus, address, vid, pid) {
                    error!("device left event triggered failed detach from hub: {}", e);
                    return;
                }
            }
            _ => {}
        }
    }
}