usb: Add buffer alignment checks

EDCI/EHCI controllers only allow 0x1000 aligned buffers.
So reply with a specific error type instead of a EP xfer error.
This commit is contained in:
CTCaer 2020-11-26 01:54:10 +02:00
parent bd4517abab
commit caae685fab
3 changed files with 15 additions and 0 deletions

View file

@ -316,6 +316,8 @@ static void _ums_transfer_start(usbd_gadget_ums_t *ums, bulk_ctxt_t *bulk_ctxt,
ums->set_text(ums->label, "#C7EA46 Status:# Error EP IN"); ums->set_text(ums->label, "#C7EA46 Status:# Error EP IN");
ums_flush_endpoint(bulk_ctxt->bulk_in); ums_flush_endpoint(bulk_ctxt->bulk_in);
} }
else if (bulk_ctxt->bulk_in_status == USB2_ERROR_XFER_NOT_ALIGNED)
ums->set_text(ums->label, "C7EA46 Status:# EP IN Buffer not aligned!");
if (sync) if (sync)
bulk_ctxt->bulk_in_buf_state = BUF_STATE_EMPTY; bulk_ctxt->bulk_in_buf_state = BUF_STATE_EMPTY;
@ -331,6 +333,8 @@ static void _ums_transfer_start(usbd_gadget_ums_t *ums, bulk_ctxt_t *bulk_ctxt,
ums->set_text(ums->label, "#C7EA46 Status:# Error EP OUT"); ums->set_text(ums->label, "#C7EA46 Status:# Error EP OUT");
ums_flush_endpoint(bulk_ctxt->bulk_out); ums_flush_endpoint(bulk_ctxt->bulk_out);
} }
else if (bulk_ctxt->bulk_out_status == USB2_ERROR_XFER_NOT_ALIGNED)
ums->set_text(ums->label, "C7EA46 Status:# EP OUT Buffer not aligned!");
if (sync) if (sync)
bulk_ctxt->bulk_out_buf_state = BUF_STATE_FULL; bulk_ctxt->bulk_out_buf_state = BUF_STATE_FULL;

View file

@ -1403,6 +1403,9 @@ static usb_ep_status_t _usbd_get_ep1_status(usb_dir_t dir)
int usb_device_ep1_out_read(u8 *buf, u32 len, u32 *bytes_read, bool sync) int usb_device_ep1_out_read(u8 *buf, u32 len, u32 *bytes_read, bool sync)
{ {
if ((u32)buf % USB_EP_BUFFER_ALIGN)
return USB2_ERROR_XFER_NOT_ALIGNED;
if (len > USB_EP_BUFFER_MAX_SIZE) if (len > USB_EP_BUFFER_MAX_SIZE)
len = USB_EP_BUFFER_MAX_SIZE; len = USB_EP_BUFFER_MAX_SIZE;
@ -1416,6 +1419,9 @@ int usb_device_ep1_out_read(u8 *buf, u32 len, u32 *bytes_read, bool sync)
int usb_device_ep1_out_read_big(u8 *buf, u32 len, u32 *bytes_read) int usb_device_ep1_out_read_big(u8 *buf, u32 len, u32 *bytes_read)
{ {
if ((u32)buf % USB_EP_BUFFER_ALIGN)
return USB2_ERROR_XFER_NOT_ALIGNED;
if (len > USB_EP_BULK_OUT_MAX_XFER) if (len > USB_EP_BULK_OUT_MAX_XFER)
len = USB_EP_BULK_OUT_MAX_XFER; len = USB_EP_BULK_OUT_MAX_XFER;
@ -1475,6 +1481,9 @@ int usb_device_ep1_out_reading_finish(u32 *pending_bytes, int tries)
int usb_device_ep1_in_write(u8 *buf, u32 len, u32 *bytes_written, bool sync) int usb_device_ep1_in_write(u8 *buf, u32 len, u32 *bytes_written, bool sync)
{ {
if ((u32)buf % USB_EP_BUFFER_ALIGN)
return USB2_ERROR_XFER_NOT_ALIGNED;
if (len > USB_EP_BUFFER_MAX_SIZE) if (len > USB_EP_BUFFER_MAX_SIZE)
len = USB_EP_BUFFER_MAX_SIZE; len = USB_EP_BUFFER_MAX_SIZE;

View file

@ -28,6 +28,7 @@
#define USB_EP_BUFFER_2_TD (USB_TD_BUFFER_MAX_SIZE * 2) #define USB_EP_BUFFER_2_TD (USB_TD_BUFFER_MAX_SIZE * 2)
#define USB_EP_BUFFER_4_TD (USB_TD_BUFFER_MAX_SIZE * 4) #define USB_EP_BUFFER_4_TD (USB_TD_BUFFER_MAX_SIZE * 4)
#define USB_EP_BUFFER_MAX_SIZE (USB_EP_BUFFER_4_TD) #define USB_EP_BUFFER_MAX_SIZE (USB_EP_BUFFER_4_TD)
#define USB_EP_BUFFER_ALIGN (USB_TD_BUFFER_PAGE_SIZE)
#define USB_XFER_START false #define USB_XFER_START false
#define USB_XFER_SYNCED true #define USB_XFER_SYNCED true
@ -134,6 +135,7 @@ typedef enum _usb_error_t
USB_ERROR_XFER_ERROR = 5, USB_ERROR_XFER_ERROR = 5,
USB2_ERROR_XFER_EP_DISABLED = 28, USB2_ERROR_XFER_EP_DISABLED = 28,
USB2_ERROR_XFER_NOT_ALIGNED = 29,
} usb_error_t; } usb_error_t;
typedef struct _usb_ctrl_setup_t typedef struct _usb_ctrl_setup_t