Update patches for newer kernel

This commit is contained in:
Jean-Michel Picod
2020-09-16 11:35:19 +02:00
parent a660d5cfda
commit 5ca23c4e05
4 changed files with 466 additions and 292 deletions

View File

@@ -1,107 +1,90 @@
diff --git a/boards/nordic/nrf52840_dongle/src/main.rs b/boards/nordic/nrf52840_dongle/src/main.rs
index 9a8dccfd..ad3e69b8 100644
index d72d204..8b97f8d 100644
--- a/boards/nordic/nrf52840_dongle/src/main.rs
+++ b/boards/nordic/nrf52840_dongle/src/main.rs
@@ -152,6 +152,7 @@ pub unsafe fn reset_handler() {
FAULT_RESPONSE,
nrf52840::uicr::Regulator0Output::V3_0,
false,
+ &Some(&nrf52840::usbd::USBD),
chip,
);
}
diff --git a/boards/nordic/nrf52840dk/src/main.rs b/boards/nordic/nrf52840dk/src/main.rs
index 127c4f2f..a5847805 100644
--- a/boards/nordic/nrf52840dk/src/main.rs
+++ b/boards/nordic/nrf52840dk/src/main.rs
@@ -244,6 +244,7 @@ pub unsafe fn reset_handler() {
FAULT_RESPONSE,
nrf52840::uicr::Regulator0Output::DEFAULT,
false,
+ &Some(&nrf52840::usbd::USBD),
chip,
);
}
diff --git a/boards/nordic/nrf52dk/src/main.rs b/boards/nordic/nrf52dk/src/main.rs
index d67ac695..b0bd8bf1 100644
--- a/boards/nordic/nrf52dk/src/main.rs
+++ b/boards/nordic/nrf52dk/src/main.rs
@@ -213,6 +213,7 @@ pub unsafe fn reset_handler() {
FAULT_RESPONSE,
nrf52832::uicr::Regulator0Output::DEFAULT,
false,
+ &None,
chip,
);
}
diff --git a/boards/nordic/nrf52dk_base/src/lib.rs b/boards/nordic/nrf52dk_base/src/lib.rs
index 105f7120..535e5cd8 100644
--- a/boards/nordic/nrf52dk_base/src/lib.rs
+++ b/boards/nordic/nrf52dk_base/src/lib.rs
@@ -101,6 +101,13 @@ pub struct Platform {
'static,
capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52::rtc::Rtc<'static>>,
@@ -15,6 +15,7 @@ use kernel::common::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferred
use kernel::component::Component;
#[allow(unused_imports)]
use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init};
+use kernel::hil::usb::UsbController;
use nrf52840::gpio::Pin;
use nrf52_components::{self, UartChannel, UartPins};
@@ -45,6 +46,17 @@ const PAN_ID: u16 = 0xABCD;
/// UART Writer
pub mod io;
+const VENDOR_ID: u16 = 0x1915; // Nordic Semiconductor
+const PRODUCT_ID: u16 = 0x521f; // nRF52840 Dongle (PCA10059)
+static STRINGS: &'static [&'static str] = &[
+ // Manufacturer
+ "Nordic Semiconductor ASA",
+ // Product
+ "OpenSK",
+ // Serial number
+ "v0.1",
+];
+
// State for loading and holding applications.
// How should the kernel respond when a process faults.
const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic;
@@ -96,6 +108,11 @@ pub struct Platform {
capsules::virtual_alarm::VirtualMuxAlarm<'static, nrf52840::rtc::Rtc<'static>>,
>,
+ usb: Option<
+ &'static capsules::usb::usb_ctap::CtapUsbSyscallDriver<
+ 'static,
+ 'static,
+ nrf52::usbd::Usbd<'static>,
+ >,
nvmc: &'static nrf52840::nvmc::SyscallDriver,
+ usb: &'static capsules::usb::usb_ctap::CtapUsbSyscallDriver<
+ 'static,
+ 'static,
+ nrf52840::usbd::Usbd<'static>,
+ >,
// The nRF52dk does not have the flash chip on it, so we make this optional.
nonvolatile_storage:
Option<&'static capsules::nonvolatile_storage_driver::NonvolatileStorage<'static>>,
@@ -130,6 +137,9 @@ impl kernel::Platform for Platform {
f(self.nonvolatile_storage.map_or(None, |nv| Some(nv)))
}
nrf52::nvmc::DRIVER_NUM => f(Some(self.nvmc)),
+ capsules::usb::usb_ctap::DRIVER_NUM => {
+ f(self.usb.map(|ctap| ctap as &dyn kernel::Driver))
+ }
}
impl kernel::Platform for Platform {
@@ -115,6 +132,7 @@ impl kernel::Platform for Platform {
capsules::temperature::DRIVER_NUM => f(Some(self.temp)),
capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)),
nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)),
+ capsules::usb::usb_ctap::DRIVER_NUM => f(Some(self.usb)),
kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
_ => f(None),
}
@@ -176,6 +186,7 @@ pub unsafe fn setup_board<I: nrf52::interrupt_service::InterruptService>(
app_fault_response: kernel::procs::FaultResponse,
reg_vout: Regulator0Output,
nfc_as_gpios: bool,
+ usb: &Option<&'static nrf52::usbd::Usbd<'static>>,
chip: &'static nrf52::chip::NRF52<I>,
) {
// Make non-volatile memory writable and activate the reset button
@@ -434,6 +445,44 @@ pub unsafe fn setup_board<I: nrf52::interrupt_service::InterruptService>(
@@ -323,6 +341,49 @@ pub unsafe fn reset_handler() {
)
);
+ // Configure USB controller if supported
+ let usb_driver: Option<
+ // Configure USB controller
+ let usb:
+ &'static capsules::usb::usb_ctap::CtapUsbSyscallDriver<
+ 'static,
+ 'static,
+ nrf52::usbd::Usbd<'static>,
+ >,
+ > = usb.map(|driver| {
+ nrf52840::usbd::Usbd<'static>,
+ > = {
+ let usb_ctap = static_init!(
+ capsules::usb::usbc_ctap_hid::ClientCtapHID<
+ 'static,
+ 'static,
+ nrf52::usbd::Usbd<'static>,
+ nrf52840::usbd::Usbd<'static>,
+ >,
+ capsules::usb::usbc_ctap_hid::ClientCtapHID::new(driver)
+ capsules::usb::usbc_ctap_hid::ClientCtapHID::new(
+ &nrf52840::usbd::USBD,
+ capsules::usb::usbc_client::MAX_CTRL_PACKET_SIZE_NRF52840,
+ VENDOR_ID,
+ PRODUCT_ID,
+ STRINGS,
+ )
+ );
+ driver.set_client(usb_ctap);
+ nrf52840::usbd::USBD.set_client(usb_ctap);
+
+ // Enable power events to be sent to USB controller
+ nrf52::power::POWER.set_usb_client(driver);
+ nrf52::power::POWER.enable_interrupts();
+ nrf52840::power::POWER.set_usb_client(&nrf52840::usbd::USBD);
+ nrf52840::power::POWER.enable_interrupts();
+
+ // Configure the USB userspace driver
+ let usb_driver = static_init!(
+ capsules::usb::usb_ctap::CtapUsbSyscallDriver<
+ 'static,
+ 'static,
+ nrf52::usbd::Usbd<'static>,
+ nrf52840::usbd::Usbd<'static>,
+ >,
+ capsules::usb::usb_ctap::CtapUsbSyscallDriver::new(
+ usb_ctap,
@@ -110,36 +93,145 @@ index 105f7120..535e5cd8 100644
+ );
+ usb_ctap.set_client(usb_driver);
+ usb_driver as &'static _
+ });
+ };
+
// Start all of the clocks. Low power operation will require a better
// approach than this.
nrf52::clock::CLOCK.low_stop();
@@ -458,6 +507,7 @@ pub unsafe fn setup_board<I: nrf52::interrupt_service::InterruptService>(
temp: temp,
alarm: alarm,
analog_comparator: analog_comparator,
+ usb: usb_driver,
nonvolatile_storage: nonvolatile_storage,
nrf52_components::NrfClockComponent::new().finalize(());
let platform = Platform {
@@ -338,6 +399,7 @@ pub unsafe fn reset_handler() {
alarm,
analog_comparator,
nvmc,
+ usb,
ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability),
nvmc: nvmc,
};
diff --git a/boards/nordic/nrf52840dk/src/main.rs b/boards/nordic/nrf52840dk/src/main.rs
index 2ebb384..303a451 100644
--- a/boards/nordic/nrf52840dk/src/main.rs
+++ b/boards/nordic/nrf52840dk/src/main.rs
@@ -72,6 +72,7 @@ use kernel::common::dynamic_deferred_call::{DynamicDeferredCall, DynamicDeferred
use kernel::component::Component;
#[allow(unused_imports)]
use kernel::{capabilities, create_capability, debug, debug_gpio, debug_verbose, static_init};
+use kernel::hil::usb::UsbController;
use nrf52840::gpio::Pin;
use nrf52_components::{self, UartChannel, UartPins};
@@ -113,6 +114,17 @@ pub mod io;
// - Set to true to use Segger RTT over USB.
const USB_DEBUGGING: bool = false;
+const VENDOR_ID: u16 = 0x1915; // Nordic Semiconductor
+const PRODUCT_ID: u16 = 0x521f; // nRF52840 Dongle (PCA10059)
+static STRINGS: &'static [&'static str] = &[
+ // Manufacturer
+ "Nordic Semiconductor ASA",
+ // Product
+ "OpenSK",
+ // Serial number
+ "v0.1",
+];
+
// State for loading and holding applications.
// How should the kernel respond when a process faults.
const FAULT_RESPONSE: kernel::procs::FaultResponse = kernel::procs::FaultResponse::Panic;
@@ -164,6 +176,11 @@ pub struct Platform {
>,
nonvolatile_storage: &'static capsules::nonvolatile_storage_driver::NonvolatileStorage<'static>,
nvmc: &'static nrf52840::nvmc::SyscallDriver,
+ usb: &'static capsules::usb::usb_ctap::CtapUsbSyscallDriver<
+ 'static,
+ 'static,
+ nrf52840::usbd::Usbd<'static>,
+ >,
}
impl kernel::Platform for Platform {
@@ -184,6 +201,7 @@ impl kernel::Platform for Platform {
capsules::analog_comparator::DRIVER_NUM => f(Some(self.analog_comparator)),
capsules::nonvolatile_storage_driver::DRIVER_NUM => f(Some(self.nonvolatile_storage)),
nrf52840::nvmc::DRIVER_NUM => f(Some(self.nvmc)),
+ capsules::usb::usb_ctap::DRIVER_NUM => f(Some(self.usb)),
kernel::ipc::DRIVER_NUM => f(Some(&self.ipc)),
_ => f(None),
}
@@ -448,6 +466,49 @@ pub unsafe fn reset_handler() {
)
);
+ // Configure USB controller
+ let usb:
+ &'static capsules::usb::usb_ctap::CtapUsbSyscallDriver<
+ 'static,
+ 'static,
+ nrf52840::usbd::Usbd<'static>,
+ > = {
+ let usb_ctap = static_init!(
+ capsules::usb::usbc_ctap_hid::ClientCtapHID<
+ 'static,
+ 'static,
+ nrf52840::usbd::Usbd<'static>,
+ >,
+ capsules::usb::usbc_ctap_hid::ClientCtapHID::new(
+ &nrf52840::usbd::USBD,
+ capsules::usb::usbc_client::MAX_CTRL_PACKET_SIZE_NRF52840,
+ VENDOR_ID,
+ PRODUCT_ID,
+ STRINGS,
+ )
+ );
+ nrf52840::usbd::USBD.set_client(usb_ctap);
+
+ // Enable power events to be sent to USB controller
+ nrf52840::power::POWER.set_usb_client(&nrf52840::usbd::USBD);
+ nrf52840::power::POWER.enable_interrupts();
+
+ // Configure the USB userspace driver
+ let usb_driver = static_init!(
+ capsules::usb::usb_ctap::CtapUsbSyscallDriver<
+ 'static,
+ 'static,
+ nrf52840::usbd::Usbd<'static>,
+ >,
+ capsules::usb::usb_ctap::CtapUsbSyscallDriver::new(
+ usb_ctap,
+ board_kernel.create_grant(&memory_allocation_capability)
+ )
+ );
+ usb_ctap.set_client(usb_driver);
+ usb_driver as &'static _
+ };
+
nrf52_components::NrfClockComponent::new().finalize(());
let platform = Platform {
@@ -464,6 +525,7 @@ pub unsafe fn reset_handler() {
analog_comparator,
nonvolatile_storage,
nvmc,
+ usb,
ipc: kernel::ipc::IPC::new(board_kernel, &memory_allocation_capability),
};
diff --git a/capsules/src/driver.rs b/capsules/src/driver.rs
index bfc06429..5858d352 100644
index 256fc0e..ae458b3 100644
--- a/capsules/src/driver.rs
+++ b/capsules/src/driver.rs
@@ -25,6 +25,7 @@ pub enum NUM {
@@ -26,6 +26,7 @@ pub enum NUM {
I2cMaster = 0x20003,
UsbUser = 0x20005,
I2cMasterSlave = 0x20006,
+ UsbCtap = 0x20009,
// Radio
BleAdvertising = 0x30000,
diff --git a/capsules/src/usb/mod.rs b/capsules/src/usb/mod.rs
index e5c8d6ad..7af3da2e 100644
index 767f5de..3f3a4f6 100644
--- a/capsules/src/usb/mod.rs
+++ b/capsules/src/usb/mod.rs
@@ -1,4 +1,6 @@
@@ -1,5 +1,7 @@
pub mod cdc;
pub mod descriptors;
+pub mod usb_ctap;
pub mod usb_user;
@@ -148,7 +240,7 @@ index e5c8d6ad..7af3da2e 100644
+pub mod usbc_ctap_hid;
diff --git a/capsules/src/usb/usb_ctap.rs b/capsules/src/usb/usb_ctap.rs
new file mode 100644
index 00000000..da3d16d8
index 0000000..da3d16d
--- /dev/null
+++ b/capsules/src/usb/usb_ctap.rs
@@ -0,0 +1,355 @@
@@ -509,16 +601,15 @@ index 00000000..da3d16d8
+}
diff --git a/capsules/src/usb/usbc_ctap_hid.rs b/capsules/src/usb/usbc_ctap_hid.rs
new file mode 100644
index 00000000..4b1916cf
index 0000000..d97b72d
--- /dev/null
+++ b/capsules/src/usb/usbc_ctap_hid.rs
@@ -0,0 +1,359 @@
@@ -0,0 +1,363 @@
+//! A USB HID client of the USB hardware interface
+
+use super::descriptors;
+use super::descriptors::Buffer64;
+use super::descriptors::ConfigurationDescriptor;
+use super::descriptors::DescriptorType;
+use super::descriptors::DeviceDescriptor;
+use super::descriptors::EndpointAddress;
+use super::descriptors::EndpointDescriptor;
+use super::descriptors::HIDCountryCode;
@@ -535,36 +626,11 @@ index 00000000..4b1916cf
+use kernel::hil;
+use kernel::hil::usb::TransferType;
+
+const VENDOR_ID: u16 = 0x1915; // Nordic Semiconductor
+const PRODUCT_ID: u16 = 0x521f; // nRF52840 Dongle (PCA10059)
+
+static LANGUAGES: &'static [u16; 1] = &[
+ 0x0409, // English (United States)
+];
+
+static STRINGS: &'static [&'static str] = &[
+ // Manufacturer
+ "Nordic Semiconductor ASA",
+ // Product
+ "OpenSK",
+ // Serial number
+ "v0.1",
+];
+
+static ENDPOINTS: &'static [EndpointDescriptor] = &[
+ EndpointDescriptor {
+ endpoint_address: EndpointAddress::new_const(1, TransferDirection::HostToDevice),
+ transfer_type: TransferType::Interrupt,
+ max_packet_size: 64,
+ interval: 5,
+ },
+ EndpointDescriptor {
+ endpoint_address: EndpointAddress::new_const(1, TransferDirection::DeviceToHost),
+ transfer_type: TransferType::Interrupt,
+ max_packet_size: 64,
+ interval: 5,
+ },
+];
+const ENDPOINT_NUM: usize = 1;
+
+static CTAP_REPORT_DESCRIPTOR: &'static [u8] = &[
+ 0x06, 0xD0, 0xF1, // HID_UsagePage ( FIDO_USAGE_PAGE ),
@@ -616,41 +682,71 @@ index 00000000..4b1916cf
+}
+
+impl<'a, 'b, C: hil::usb::UsbController<'a>> ClientCtapHID<'a, 'b, C> {
+ pub fn new(controller: &'a C) -> Self {
+ ClientCtapHID {
+ client_ctrl: ClientCtrl::new(
+ controller,
+ DeviceDescriptor {
+ // TODO: set this field at the board level.
+ max_packet_size_ep0: 64,
+ vendor_id: VENDOR_ID,
+ product_id: PRODUCT_ID,
+ pub fn new(
+ controller: &'a C,
+ max_ctrl_packet_size: u8,
+ vendor_id: u16,
+ product_id: u16,
+ strings: &'static [&'static str]
+ ) -> Self {
+ let interfaces: &mut [InterfaceDescriptor] = &mut [
+ // Interface declared in the FIDO2 specification, section 8.1.8.1
+ InterfaceDescriptor {
+ interface_class: 0x03, // HID
+ interface_subclass: 0x00,
+ interface_protocol: 0x00,
+ ..InterfaceDescriptor::default()
+ },
+ ];
+
+ let endpoints: &[&[EndpointDescriptor]] = &[&[
+ EndpointDescriptor {
+ endpoint_address: EndpointAddress::new_const(ENDPOINT_NUM, TransferDirection::HostToDevice),
+ transfer_type: TransferType::Interrupt,
+ max_packet_size: 64,
+ interval: 5,
+ },
+ EndpointDescriptor {
+ endpoint_address: EndpointAddress::new_const(ENDPOINT_NUM, TransferDirection::DeviceToHost),
+ transfer_type: TransferType::Interrupt,
+ max_packet_size: 64,
+ interval: 5,
+ },
+ ]];
+
+ let (device_descriptor_buffer, other_descriptor_buffer) =
+ descriptors::create_descriptor_buffers(
+ descriptors::DeviceDescriptor {
+ vendor_id: vendor_id,
+ product_id: product_id,
+ manufacturer_string: 1,
+ product_string: 2,
+ serial_number_string: 3,
+ ..Default::default()
+ max_packet_size_ep0: max_ctrl_packet_size,
+ ..descriptors::DeviceDescriptor::default()
+ },
+ ConfigurationDescriptor {
+ // Must be non-zero, otherwise dmesg prints the following error:
+ // [...] usb 2-3: config 0 descriptor??
+ descriptors::ConfigurationDescriptor {
+ configuration_value: 1,
+ ..Default::default()
+ ..descriptors::ConfigurationDescriptor::default()
+ },
+ // Interface declared in the FIDO2 specification, section 8.1.8.1
+ InterfaceDescriptor {
+ interface_class: 0x03, // HID
+ interface_subclass: 0x00,
+ interface_protocol: 0x00,
+ ..Default::default()
+ },
+ ENDPOINTS,
+ interfaces,
+ endpoints,
+ Some(&HID),
+ None, // No CDC descriptor array
+ );
+
+ ClientCtapHID {
+ client_ctrl: ClientCtrl::new(
+ controller,
+ device_descriptor_buffer,
+ other_descriptor_buffer,
+ Some(&HID),
+ Some(&CTAP_REPORT),
+ LANGUAGES,
+ STRINGS,
+ strings,
+ ),
+ in_buffer: Default::default(),
+ out_buffer: Default::default(),
+ in_buffer: Buffer64::default(),
+ out_buffer: Buffer64::default(),
+ client: OptionalCell::empty(),
+ tx_packet: OptionalCell::empty(),
+ pending_in: Cell::new(false),
@@ -872,3 +968,19 @@ index 00000000..4b1916cf
+ self.client.map(|client| client.packet_transmitted());
+ }
+}
diff --git a/chips/nrf52840/src/lib.rs b/chips/nrf52840/src/lib.rs
index 942d028..ce73e1f 100644
--- a/chips/nrf52840/src/lib.rs
+++ b/chips/nrf52840/src/lib.rs
@@ -2,7 +2,7 @@
pub use nrf52::{
acomp, adc, aes, ble_radio, clock, constants, crt1, ficr, i2c, ieee802154_radio, init, nvmc,
- pinmux, ppi, pwm, rtc, spi, temperature, timer, trng, uart, uicr, usbd,
+ pinmux, power, ppi, pwm, rtc, spi, temperature, timer, trng, uart, uicr, usbd,
};
pub mod chip;
pub mod gpio;
--
libgit2 1.0.1