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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Resolver for i8042 devices.

use super::I8042Device;
use async_trait::async_trait;
use chipset_device_resources::ResolveChipsetDeviceHandleParams;
use chipset_device_resources::ResolvedChipsetDevice;
use chipset_device_resources::IRQ_LINE_SET;
use chipset_resources::i8042::I8042DeviceHandle;
use power_resources::PowerRequest;
use power_resources::PowerRequestHandleKind;
use thiserror::Error;
use vm_resource::declare_static_async_resolver;
use vm_resource::kind::ChipsetDeviceHandleKind;
use vm_resource::AsyncResolveResource;
use vm_resource::IntoResource;
use vm_resource::PlatformResource;
use vm_resource::ResolveError;
use vm_resource::ResourceResolver;

/// A resolver for i8042 devices.
pub struct I8042Resolver;

declare_static_async_resolver! {
    I8042Resolver,
    (ChipsetDeviceHandleKind, I8042DeviceHandle),
}

/// Errors that can occur when resolving an i8042 device.
#[derive(Debug, Error)]
#[allow(missing_docs)]
pub enum ResolveI8042Error {
    #[error("failed to resolve keyboard input")]
    ResolveKeyboardInput(#[source] ResolveError),
    #[error("failed to resolve power request")]
    ResolvePowerRequest(#[source] ResolveError),
}

#[async_trait]
impl AsyncResolveResource<ChipsetDeviceHandleKind, I8042DeviceHandle> for I8042Resolver {
    type Output = ResolvedChipsetDevice;
    type Error = ResolveI8042Error;

    async fn resolve(
        &self,
        resolver: &ResourceResolver,
        resource: I8042DeviceHandle,
        input: ResolveChipsetDeviceHandleParams<'_>,
    ) -> Result<Self::Output, Self::Error> {
        // Hard-coded to IRQ line 1, as per x86 spec.
        let keyboard_interrupt = input.configure.new_line(IRQ_LINE_SET, "keyboard", 1);

        // Hard-coded to IRQ line 12, as per x86 convention.
        let mouse_interrupt = input.configure.new_line(IRQ_LINE_SET, "aux", 12);

        let keyboard_input = resolver
            .resolve(resource.keyboard_input, input.device_name)
            .await
            .map_err(ResolveI8042Error::ResolveKeyboardInput)?;

        let power_request = resolver
            .resolve::<PowerRequestHandleKind, _>(PlatformResource.into_resource(), ())
            .await
            .map_err(ResolveI8042Error::ResolvePowerRequest)?;

        let reset = Box::new(move || {
            power_request.power_request(PowerRequest::Reset);
        });

        Ok(
            I8042Device::new(reset, keyboard_interrupt, mouse_interrupt, keyboard_input.0)
                .await
                .into(),
        )
    }
}