Skip to content

Generate USB drivers bindings automatically #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ledger_device_sdk/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ impl Comm {
let mut spi_buffer = [0u8; 256];
while sys_seph::is_status_sent() {
sys_seph::seph_recv(&mut spi_buffer, 0);
seph::handle_event(&mut self.apdu_buffer, &spi_buffer);
seph::handle_event(&mut self.apdu_buffer, &mut spi_buffer);
}

match unsafe { G_io_app.apdu_state } {
Expand Down Expand Up @@ -208,7 +208,7 @@ impl Comm {
sys_seph::send_general_status()
}
sys_seph::seph_recv(&mut spi_buffer, 0);
seph::handle_event(&mut self.apdu_buffer, &spi_buffer);
seph::handle_event(&mut self.apdu_buffer, &mut spi_buffer);
}
self.tx = 0;
self.rx = 0;
Expand Down
79 changes: 20 additions & 59 deletions ledger_device_sdk/src/seph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,65 +64,21 @@ impl From<u8> for UsbEp {
}
}

/// FFI bindings to USBD functions inlined here for clarity
/// and also because some of the generated ones are incorrectly
/// assuming mutable pointers when they are not
#[repr(C)]
#[derive(Copy, Clone)]
pub struct apdu_buffer_s {
pub buf: *mut u8,
pub len: u16,
}
impl Default for apdu_buffer_s {
fn default() -> Self {
unsafe { ::core::mem::zeroed() }
}
}
pub type ApduBufferT = apdu_buffer_s;
extern "C" {
pub static mut USBD_Device: USBD_HandleTypeDef;
pub fn USBD_LL_SetupStage(
pdev: *mut USBD_HandleTypeDef,
psetup: *const u8,
) -> USBD_StatusTypeDef;
pub fn USBD_LL_DataOutStage(
pdev: *mut USBD_HandleTypeDef,
epnum: u8,
pdata: *const u8,
arg1: *mut ApduBufferT,
) -> USBD_StatusTypeDef;
pub fn USBD_LL_DataInStage(
pdev: *mut USBD_HandleTypeDef,
epnum: u8,
pdata: *const u8,
) -> USBD_StatusTypeDef;
pub fn USBD_LL_Reset(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef;
pub fn USBD_LL_SetSpeed(
pdev: *mut USBD_HandleTypeDef,
speed: USBD_SpeedTypeDef,
) -> USBD_StatusTypeDef;
pub fn USBD_LL_Suspend(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef;
pub fn USBD_LL_Resume(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef;
pub fn USBD_LL_SOF(pdev: *mut USBD_HandleTypeDef) -> USBD_StatusTypeDef;
}

/// Below is a straightforward translation of the corresponding functions
/// in the C SDK, they could be improved
pub fn handle_usb_event(event: u8) {
match Events::from(event) {
Events::USBEventReset => {
unsafe {
USBD_LL_SetSpeed(&raw mut USBD_Device, 1 /*USBD_SPEED_FULL*/);
USBD_LL_Reset(&raw mut USBD_Device);
Events::USBEventReset => unsafe {
USBD_LL_SetSpeed(&raw mut USBD_Device, USBD_SPEED_FULL);
USBD_LL_Reset(&raw mut USBD_Device);

if G_io_app.apdu_media != IO_APDU_MEDIA_NONE {
return;
}

G_io_app.usb_ep_xfer_len = core::mem::zeroed();
G_io_app.usb_ep_timeouts = core::mem::zeroed();
if G_io_app.apdu_media != IO_APDU_MEDIA_NONE {
return;
}
}

G_io_app.usb_ep_xfer_len = core::mem::zeroed();
G_io_app.usb_ep_timeouts = core::mem::zeroed();
},
Events::USBEventSOF => unsafe {
USBD_LL_SOF(&raw mut USBD_Device);
},
Expand All @@ -136,29 +92,34 @@ pub fn handle_usb_event(event: u8) {
}
}

pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
pub fn handle_usb_ep_xfer_event(apdu_buffer: &mut [u8], buffer: &mut [u8]) {
let endpoint = buffer[3] & 0x7f;
match UsbEp::from(buffer[4]) {
UsbEp::USBEpXFERSetup => unsafe {
USBD_LL_SetupStage(&raw mut USBD_Device, &buffer[6]);
USBD_LL_SetupStage(&raw mut USBD_Device, &mut buffer[6]);
},
UsbEp::USBEpXFERIn => {
if (endpoint as u32) < IO_USB_MAX_ENDPOINTS {
unsafe {
G_io_app.usb_ep_timeouts[endpoint as usize].timeout = 0;
USBD_LL_DataInStage(&raw mut USBD_Device, endpoint, &buffer[6]);
USBD_LL_DataInStage(&raw mut USBD_Device, endpoint, &mut buffer[6]);
}
}
}
UsbEp::USBEpXFEROut => {
if (endpoint as u32) < IO_USB_MAX_ENDPOINTS {
unsafe {
G_io_app.usb_ep_xfer_len[endpoint as usize] = buffer[5];
let mut apdu_buf = ApduBufferT {
let mut apdu_buf = apdu_buffer_s {
buf: apdu_buffer.as_mut_ptr(),
len: 260,
};
USBD_LL_DataOutStage(&raw mut USBD_Device, endpoint, &buffer[6], &mut apdu_buf);
USBD_LL_DataOutStage(
&raw mut USBD_Device,
endpoint,
&mut buffer[6],
&mut apdu_buf,
);
}
}
}
Expand All @@ -185,7 +146,7 @@ pub fn handle_capdu_event(apdu_buffer: &mut [u8], buffer: &[u8]) {
}
}

pub fn handle_event(apdu_buffer: &mut [u8], spi_buffer: &[u8]) {
pub fn handle_event(apdu_buffer: &mut [u8], spi_buffer: &mut [u8]) {
let len = u16::from_be_bytes([spi_buffer[1], spi_buffer[2]]);
match Events::from(spi_buffer[0]) {
Events::USBEvent => {
Expand Down
1 change: 1 addition & 0 deletions ledger_secure_sdk_sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ impl SDKBuilder {
"include/os_ux.h",
"include/ox.h", /* crypto-related syscalls */
"lib_stusb/STM32_USB_Device_Library/Core/Inc/usbd_def.h",
"lib_stusb/STM32_USB_Device_Library/Core/Inc/usbd_core.h",
"include/os_io_usb.h",
"lib_standard_app/swap_lib_calls.h",
],
Expand Down
Loading