components/
usb.rs

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
79
80
81
82
83
84
85
86
87
88
89
90
91
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Generic component for initializing a USB device given a USBController.
//!
//! This provides one Component, UsbComponent, which implements
//! A userspace syscall interface to a USB peripheral.
//!
//! Usage
//! -----
//! ```rust
//! let usb_driver = components::usb::UsbComponent::new(
//!     board_kernel,
//!     capsules_extra::usb::usb_user::DRIVER_NUM,
//!     &peripherals.usbc,
//! )
//! .finalize(components::usb_component_static!(sam4l::usbc::Usbc));
//! ```

use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil::usb::UsbController;

#[macro_export]
macro_rules! usb_component_static {
    ($U:ty $(,)?) => {{
        let usb_client = kernel::static_buf!(capsules_extra::usb::usbc_client::Client<'static, $U>);
        let usb_driver = kernel::static_buf!(
            capsules_extra::usb::usb_user::UsbSyscallDriver<
                'static,
                capsules_extra::usb::usbc_client::Client<'static, $U>,
            >
        );
        (usb_client, usb_driver)
    }};
}

pub struct UsbComponent<U: UsbController<'static> + 'static> {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
    usbc: &'static U,
}

impl<U: UsbController<'static> + 'static> UsbComponent<U> {
    pub fn new(board_kernel: &'static kernel::Kernel, driver_num: usize, usbc: &'static U) -> Self {
        Self {
            board_kernel,
            driver_num,
            usbc,
        }
    }
}

impl<U: UsbController<'static> + 'static> Component for UsbComponent<U> {
    type StaticInput = (
        &'static mut MaybeUninit<capsules_extra::usb::usbc_client::Client<'static, U>>,
        &'static mut MaybeUninit<
            capsules_extra::usb::usb_user::UsbSyscallDriver<
                'static,
                capsules_extra::usb::usbc_client::Client<'static, U>,
            >,
        >,
    );
    type Output = &'static capsules_extra::usb::usb_user::UsbSyscallDriver<
        'static,
        capsules_extra::usb::usbc_client::Client<'static, U>,
    >;

    fn finalize(self, s: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

        // Configure the USB controller
        let usb_client = s.0.write(capsules_extra::usb::usbc_client::Client::new(
            self.usbc,
            capsules_extra::usb::usbc_client::MAX_CTRL_PACKET_SIZE_SAM4L,
        ));
        self.usbc.set_client(usb_client);

        // Configure the USB userspace driver
        let usb_driver =
            s.1.write(capsules_extra::usb::usb_user::UsbSyscallDriver::new(
                usb_client,
                self.board_kernel.create_grant(self.driver_num, &grant_cap),
            ));

        usb_driver
    }
}