Skip to content

Commit

Permalink
Generate USB drivers bindings automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
yogh333 committed May 31, 2024
1 parent 30bd22d commit e5c955b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ledger_device_sdk/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl Comm {
let mut spi_buffer = [0u8; 128];
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
36 changes: 17 additions & 19 deletions ledger_device_sdk/src/seph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ 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)]
/*#[repr(C)]
#[derive(Copy, Clone)]
pub struct apdu_buffer_s {
pub buf: *mut u8,
Expand Down Expand Up @@ -104,25 +104,23 @@ extern "C" {
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(&mut USBD_Device, 1 /*USBD_SPEED_FULL*/);
USBD_LL_Reset(&mut USBD_Device);

if G_io_app.apdu_media != IO_APDU_MEDIA_NONE {
return;
}
Events::USBEventReset => unsafe {
USBD_LL_SetSpeed(&mut USBD_Device, USBD_SPEED_FULL);
USBD_LL_Reset(&mut USBD_Device);

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(&mut USBD_Device);
},
Expand All @@ -136,29 +134,29 @@ 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(&mut USBD_Device, &buffer[6]);
USBD_LL_SetupStage(&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(&mut USBD_Device, endpoint, &buffer[6]);
USBD_LL_DataInStage(&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(&mut USBD_Device, endpoint, &buffer[6], &mut apdu_buf);
USBD_LL_DataOutStage(&mut USBD_Device, endpoint, &mut buffer[6], &mut apdu_buf);
}
}
}
Expand All @@ -183,7 +181,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 @@ -486,6 +486,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",
],
);
Expand Down

0 comments on commit e5c955b

Please sign in to comment.