diff --git a/ChangeLog b/ChangeLog index 8cbcd54..6a90611 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2015-07-27 Niibe Yutaka + + * src/usb_stm32f103.c (usb_lld_answer_control): New. + (usb_lld_set_data_to_send): Remove. + (usb_lld_set_data_to_recv): Not a macro but a function. + (std_get_status): Don't use statically allocated memory. + (std_get_configuration): Use usb_lld_answer_control. + (handle_setup0): Follow the change. + * src/usb_ctrl.c (vcom_port_data_setup, usb_cb_setup) + (usb_cb_interface): Use usb_lld_answer_control. + * src/usb_desc.c (usb_cb_get_descriptor): Likewise. + 2015-07-24 Niibe Yutaka * tool/gnuk_put_binary.py: Remove. diff --git a/src/usb_ctrl.c b/src/usb_ctrl.c index 01e7139..b76ffaa 100644 --- a/src/usb_ctrl.c +++ b/src/usb_ctrl.c @@ -63,10 +63,7 @@ vcom_port_data_setup (uint8_t req, uint8_t req_no, uint16_t value) if (USB_SETUP_GET (req)) { if (req_no == USB_CDC_REQ_GET_LINE_CODING) - { - usb_lld_set_data_to_send (&line_coding, sizeof(line_coding)); - return USB_SUCCESS; - } + return usb_lld_answer_control (&line_coding, sizeof(line_coding)); } else /* USB_SETUP_SET (req) */ { @@ -272,10 +269,7 @@ usb_cb_setup (uint8_t req, uint8_t req_no, if (USB_SETUP_GET (req)) { if (req_no == USB_FSIJ_GNUK_MEMINFO) - { - usb_lld_set_data_to_send (mem_info, sizeof (mem_info)); - return USB_SUCCESS; - } + return usb_lld_answer_control (mem_info, sizeof (mem_info)); } else /* SETUP_SET */ { @@ -322,16 +316,10 @@ usb_cb_setup (uint8_t req, uint8_t req_no, if (USB_SETUP_GET (req)) { if (req_no == USB_CCID_REQ_GET_CLOCK_FREQUENCIES) - { - usb_lld_set_data_to_send (freq_table, sizeof (freq_table)); - return USB_SUCCESS; - } + return usb_lld_answer_control (freq_table, sizeof (freq_table)); else if (req_no == USB_CCID_REQ_GET_DATA_RATES) - { - usb_lld_set_data_to_send (data_rate_table, - sizeof (data_rate_table)); - return USB_SUCCESS; - } + return usb_lld_answer_control (data_rate_table, + sizeof (data_rate_table)); } else { @@ -347,16 +335,14 @@ usb_cb_setup (uint8_t req, uint8_t req_no, switch (req_no) { case USB_HID_REQ_GET_IDLE: - usb_lld_set_data_to_send (&hid_idle_rate, 1); - return USB_SUCCESS; + return usb_lld_answer_control (&hid_idle_rate, 1); case USB_HID_REQ_SET_IDLE: usb_lld_set_data_to_recv (&hid_idle_rate, 1); return USB_SUCCESS; case USB_HID_REQ_GET_REPORT: /* Request of LED status and key press */ - usb_lld_set_data_to_send (&hid_report, 2); - return USB_SUCCESS; + return usb_lld_answer_control (&hid_report, 2); case USB_HID_REQ_SET_REPORT: /* Received LED set request */ @@ -384,10 +370,7 @@ usb_cb_setup (uint8_t req, uint8_t req_no, if (USB_SETUP_GET (req)) { if (req_no == MSC_GET_MAX_LUN_COMMAND) - { - usb_lld_set_data_to_send (lun_table, sizeof (lun_table)); - return USB_SUCCESS; - } + return usb_lld_answer_control (lun_table, sizeof (lun_table)); } else if (req_no == MSC_MASS_STORAGE_RESET_COMMAND) @@ -495,9 +478,7 @@ int usb_cb_interface (uint8_t cmd, uint16_t interface, uint16_t alt) } case USB_GET_INTERFACE: - usb_lld_write (ENDP0, &zero, 1); - usb_lld_set_data_to_send (NULL, 1); - return USB_SUCCESS; + return usb_lld_answer_control (&zero, 1); default: case USB_QUERY_INTERFACE: diff --git a/src/usb_desc.c b/src/usb_desc.c index dcd49af..804b3be 100644 --- a/src/usb_desc.c +++ b/src/usb_desc.c @@ -351,25 +351,16 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index, if (rcp == DEVICE_RECIPIENT) { if (desc_type == DEVICE_DESCRIPTOR) - { - usb_lld_set_data_to_send (gnukDeviceDescriptor, - sizeof (gnukDeviceDescriptor)); - return USB_SUCCESS; - } + return usb_lld_answer_control (gnukDeviceDescriptor, + sizeof (gnukDeviceDescriptor)); else if (desc_type == CONFIG_DESCRIPTOR) - { - usb_lld_set_data_to_send (gnukConfigDescriptor, - sizeof (gnukConfigDescriptor)); - return USB_SUCCESS; - } + return usb_lld_answer_control (gnukConfigDescriptor, + sizeof (gnukConfigDescriptor)); else if (desc_type == STRING_DESCRIPTOR) { if (desc_index < NUM_STRING_DESC) - { - usb_lld_set_data_to_send (string_descriptors[desc_index].desc, - string_descriptors[desc_index].size); - return USB_SUCCESS; - } + return usb_lld_answer_control (string_descriptors[desc_index].desc, + string_descriptors[desc_index].size); else if (desc_index == NUM_STRING_DESC) { uint8_t usbbuf[64]; @@ -388,9 +379,7 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index, usbbuf[1] = USB_STRING_DESCRIPTOR_TYPE; if (len > length) len = length; - usb_lld_write (ENDP0, usbbuf, len); - usb_lld_set_data_to_send (NULL, len); - return USB_SUCCESS; + return usb_lld_answer_control (usbbuf, len); } } } @@ -400,16 +389,9 @@ usb_cb_get_descriptor (uint8_t rcp, uint8_t desc_type, uint8_t desc_index, if (index == 1) { if (desc_type == USB_DT_HID) - { - usb_lld_set_data_to_send (gnukConfigDescriptor+ICC_TOTAL_LENGTH+9, - 9); - return USB_SUCCESS; - } + return usb_lld_answer_control (gnukConfigDescriptor+ICC_TOTAL_LENGTH+9, 9); else if (desc_type == USB_DT_REPORT) - { - usb_lld_set_data_to_send (hid_report_desc, HID_REPORT_DESC_SIZE); - return USB_SUCCESS; - } + return usb_lld_answer_control (hid_report_desc, HID_REPORT_DESC_SIZE); } else #else diff --git a/src/usb_lld.h b/src/usb_lld.h index dfaedb7..2608bd7 100644 --- a/src/usb_lld.h +++ b/src/usb_lld.h @@ -99,6 +99,7 @@ int usb_lld_tx_data_len (int ep_num); void usb_lld_txcpy (const void *src, int ep_num, int offset, size_t len); void usb_lld_tx_enable (int ep_num, size_t len); void usb_lld_write (uint8_t ep_num, const void *buf, size_t len); +int usb_lld_answer_control (const void *buf, size_t buflen); void usb_lld_rx_enable (int ep_num); int usb_lld_rx_data_len (int ep_num); void usb_lld_rxcpy (uint8_t *dst, int ep_num, int offset, size_t len); @@ -109,12 +110,7 @@ void usb_lld_setup_endpoint (int ep_num, int ep_type, int ep_kind, void usb_lld_set_configuration (uint8_t config); uint8_t usb_lld_current_configuration (void); void usb_lld_set_feature (uint8_t feature); -void usb_lld_set_data_to_send (const void *p, size_t len); - -extern inline void usb_lld_set_data_to_recv (void *p, size_t len) -{ - usb_lld_set_data_to_send ((const void *)p, len); -} +void usb_lld_set_data_to_recv (const void *p, size_t len); void usb_lld_prepare_shutdown (void); void usb_lld_shutdown (void); diff --git a/src/usb_stm32f103.c b/src/usb_stm32f103.c index a624e9b..a874e41 100644 --- a/src/usb_stm32f103.c +++ b/src/usb_stm32f103.c @@ -488,11 +488,8 @@ static int std_none (uint8_t req, static int std_get_status (uint8_t req, uint16_t value, uint16_t index, uint16_t length) { - static uint16_t status_info; uint8_t rcp = req & RECIPIENT; - - status_info = 0; /* Reset Status Information */ - data_p->addr = (uint8_t *)&status_info; + uint16_t status_info = 0; if (value != 0 || length != 2 || (index >> 8) != 0 || (req & REQUEST_DIR) == 0) @@ -517,8 +514,7 @@ static int std_get_status (uint8_t req, else /* Self-powered */ status_info &= ~1; - data_p->len = 2; - return USB_SUCCESS; + return usb_lld_answer_control (&status_info, 2); } } else if (rcp == INTERFACE_RECIPIENT) @@ -532,8 +528,7 @@ static int std_get_status (uint8_t req, if (r != USB_SUCCESS) return USB_UNSUPPORT; - data_p->len = 2; - return USB_SUCCESS; + return usb_lld_answer_control (&status_info, 2); } else if (rcp == ENDPOINT_RECIPIENT) { @@ -560,8 +555,7 @@ static int std_get_status (uint8_t req, status_info |= 1; /* OUT Endpoint stalled */ } - data_p->len = 2; - return USB_SUCCESS; + return usb_lld_answer_control (&status_info, 2); } return USB_UNSUPPORT; @@ -712,11 +706,7 @@ static int std_get_configuration (uint8_t req, uint16_t value, (void)value; (void)index; (void)length; if (rcp == DEVICE_RECIPIENT) - { - data_p->addr = &dev_p->current_configuration; - data_p->len = 1; - return USB_SUCCESS; - } + return usb_lld_answer_control (&dev_p->current_configuration, 1); return USB_UNSUPPORT; } @@ -845,30 +835,7 @@ static void handle_setup0 (void) else { if (USB_SETUP_GET (ctrl_p->bmRequestType)) - { - uint32_t len = ctrl_p->wLength; - - /* Restrict the data length to be the one host asks for */ - if (data_p->len > len) - data_p->len = len; - - if (data_p->len != 0 && (data_p->len % USB_MAX_PACKET_SIZE) == 0) - data_p->require_zlp = TRUE; - else - data_p->require_zlp = FALSE; - - if (data_p->addr == NULL) - { - /* usb_lld_wite was called already by the setup callback. */ - dev_p->state = LAST_IN_DATA; - data_p->len = 0; - } - else - { - dev_p->state = IN_DATA; - handle_datastage_in (); - } - } + ; else if (ctrl_p->wLength == 0) { dev_p->state = WAIT_STATUS_IN; @@ -1121,7 +1088,7 @@ void usb_lld_set_feature (uint8_t feature) dev_p->current_feature = feature; } -void usb_lld_set_data_to_send (const void *p, size_t len) +void usb_lld_set_data_to_recv (const void *p, size_t len) { data_p->addr = (uint8_t *)p; data_p->len = len; @@ -1199,3 +1166,51 @@ void usb_lld_from_pmabuf (void *dst, uint16_t addr, size_t n) *d = (w & 0xff); } } + + +/* + * BUF: Pointer to data memory. Data memory should not be allocated + * on stack when BUFLEN > USB_MAX_PACKET_SIZE. + * + * BUFLEN: size of the data. + */ +int +usb_lld_answer_control (const void *buf, size_t buflen) +{ + uint32_t len_asked = ctrl_p->wLength; + uint32_t len; + + data_p->addr = (void *)buf; + data_p->len = buflen; + + /* Restrict the data length to be the one host asks for */ + if (data_p->len > len_asked) + data_p->len = len_asked; + + if (data_p->len != 0 && (data_p->len % USB_MAX_PACKET_SIZE) == 0) + data_p->require_zlp = TRUE; + else + data_p->require_zlp = FALSE; + + if (data_p->len < USB_MAX_PACKET_SIZE) + { + len = data_p->len; + dev_p->state = LAST_IN_DATA; + } + else + { + len = USB_MAX_PACKET_SIZE; + dev_p->state = IN_DATA; + } + + if (len) + { + usb_lld_to_pmabuf (data_p->addr, st103_get_tx_addr (ENDP0), len); + data_p->len -= len; + data_p->offset += len; + } + + st103_set_tx_count (ENDP0, len); + st103_ep_set_tx_status (ENDP0, EP_TX_VALID); + return USB_SUCCESS; +}