Skip to content

Commit 1cccf28

Browse files
tmon-nordickartben
authored andcommitted
drivers: usb: udc: Reduce unnecessary ep config lookups
UDC API passes struct udc_ep_config to all functions. Some UDC functions were using endpoint config structure while some were using device and endpoint number. This API inconsistency led to completely unnecessary endpoint structure lookups. Remove unnecessary lookups by using endpoint config structure pointer where it makes sense. Signed-off-by: Tomasz Moń <[email protected]>
1 parent 01ba1bf commit 1cccf28

16 files changed

+272
-256
lines changed

drivers/usb/udc/udc_ambiq.c

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,14 @@ static int usbd_ctrl_feed_dout(const struct device *dev, const size_t length)
9595
static int udc_ambiq_tx(const struct device *dev, uint8_t ep, struct net_buf *buf)
9696
{
9797
const struct udc_ambiq_data *priv = udc_get_private(dev);
98+
struct udc_ep_config *cfg = udc_get_ep_cfg(dev, ep);
9899
uint32_t status;
99100

100-
if (udc_ep_is_busy(dev, ep)) {
101+
if (udc_ep_is_busy(cfg)) {
101102
LOG_WRN("ep 0x%02x is busy!", ep);
102103
return 0;
103104
}
104-
udc_ep_set_busy(dev, ep, true);
105+
udc_ep_set_busy(cfg, true);
105106

106107
/* buf equals NULL is used as indication of ZLP request */
107108
if (buf == NULL) {
@@ -111,7 +112,7 @@ static int udc_ambiq_tx(const struct device *dev, uint8_t ep, struct net_buf *bu
111112
}
112113

113114
if (status != AM_HAL_STATUS_SUCCESS) {
114-
udc_ep_set_busy(dev, ep, false);
115+
udc_ep_set_busy(cfg, false);
115116
LOG_ERR("am_hal_usb_ep_xfer write failed(0x%02x), %d", ep, (int)status);
116117
return -EIO;
117118
}
@@ -122,15 +123,16 @@ static int udc_ambiq_tx(const struct device *dev, uint8_t ep, struct net_buf *bu
122123
static int udc_ambiq_rx(const struct device *dev, uint8_t ep, struct net_buf *buf)
123124
{
124125
struct udc_ambiq_data *priv = udc_get_private(dev);
126+
struct udc_ep_config *ep_cfg = udc_get_ep_cfg(dev, ep);
125127
struct udc_ep_config *cfg = udc_get_ep_cfg(dev, USB_CONTROL_EP_OUT);
126128
uint32_t status;
127129
uint16_t rx_size = buf->size;
128130

129-
if (udc_ep_is_busy(dev, ep)) {
131+
if (udc_ep_is_busy(ep_cfg)) {
130132
LOG_WRN("ep 0x%02x is busy!", ep);
131133
return 0;
132134
}
133-
udc_ep_set_busy(dev, ep, true);
135+
udc_ep_set_busy(ep_cfg, true);
134136

135137
/* Make sure that OUT transaction size triggered doesn't exceed EP's MPS */
136138
if ((ep != USB_CONTROL_EP_OUT) && (cfg->mps < rx_size)) {
@@ -139,7 +141,7 @@ static int udc_ambiq_rx(const struct device *dev, uint8_t ep, struct net_buf *bu
139141

140142
status = am_hal_usb_ep_xfer(priv->usb_handle, ep, buf->data, rx_size);
141143
if (status != AM_HAL_STATUS_SUCCESS) {
142-
udc_ep_set_busy(dev, ep, false);
144+
udc_ep_set_busy(ep_cfg, false);
143145
LOG_ERR("am_hal_usb_ep_xfer read(rx) failed(0x%02x), %d", ep, (int)status);
144146
return -EIO;
145147
}
@@ -222,7 +224,9 @@ static void udc_ambiq_ep_xfer_complete_callback(const struct device *dev, uint8_
222224
if (USB_EP_DIR_IS_IN(ep_addr)) {
223225
evt.type = UDC_AMBIQ_EVT_HAL_IN_CMP;
224226
} else {
225-
buf = udc_buf_peek(dev, ep_addr);
227+
struct udc_ep_config *ep_cfg = udc_get_ep_cfg(dev, ep_addr);
228+
229+
buf = udc_buf_peek(ep_cfg);
226230
if (buf == NULL) {
227231
LOG_ERR("No buffer for ep 0x%02x", ep_addr);
228232
udc_submit_event(dev, UDC_EVT_ERROR, -ENOBUFS);
@@ -280,12 +284,12 @@ static int udc_ambiq_ep_dequeue(const struct device *dev, struct udc_ep_config *
280284

281285
lock_key = irq_lock();
282286

283-
buf = udc_buf_get_all(dev, ep_cfg->addr);
287+
buf = udc_buf_get_all(ep_cfg);
284288
if (buf) {
285289
udc_submit_ep_event(dev, buf, -ECONNABORTED);
286290
}
287291

288-
udc_ep_set_busy(dev, ep_cfg->addr, false);
292+
udc_ep_set_busy(ep_cfg, false);
289293
am_hal_usb_ep_state_reset(priv->usb_handle, ep_cfg->addr);
290294
irq_unlock(lock_key);
291295

@@ -319,7 +323,7 @@ static int udc_ambiq_ep_clear_halt(const struct device *dev, struct udc_ep_confi
319323
ep_cfg->stat.halted = false;
320324

321325
/* Resume queued transfer if any */
322-
if (udc_buf_peek(dev, ep_cfg->addr)) {
326+
if (udc_buf_peek(ep_cfg)) {
323327
struct udc_ambiq_event evt = {
324328
.ep = ep_cfg->addr,
325329
.type = UDC_AMBIQ_EVT_XFER,
@@ -646,14 +650,14 @@ static inline void ambiq_handle_evt_dout(const struct device *dev, struct udc_ep
646650
struct net_buf *buf;
647651

648652
/* retrieve endpoint buffer */
649-
buf = udc_buf_get(dev, cfg->addr);
653+
buf = udc_buf_get(cfg);
650654
if (buf == NULL) {
651655
LOG_ERR("No buffer queued for control ep");
652656
return;
653657
}
654658

655659
/* Clear endpoint busy status */
656-
udc_ep_set_busy(dev, cfg->addr, false);
660+
udc_ep_set_busy(cfg, false);
657661

658662
/* Handle transfer complete event */
659663
if (cfg->addr == USB_CONTROL_EP_OUT) {
@@ -685,9 +689,9 @@ static void ambiq_handle_evt_din(const struct device *dev, struct udc_ep_config
685689
bool udc_ambiq_rx_status_in_completed = false;
686690

687691
/* Clear endpoint busy status */
688-
udc_ep_set_busy(dev, cfg->addr, false);
692+
udc_ep_set_busy(cfg, false);
689693
/* Check and Handle ZLP flag */
690-
buf = udc_buf_peek(dev, cfg->addr);
694+
buf = udc_buf_peek(cfg);
691695
if (cfg->addr != USB_CONTROL_EP_IN) {
692696
if (udc_ep_buf_has_zlp(buf)) {
693697
udc_ep_buf_clear_zlp(buf);
@@ -698,7 +702,7 @@ static void ambiq_handle_evt_din(const struct device *dev, struct udc_ep_config
698702
}
699703

700704
/* retrieve endpoint buffer */
701-
buf = udc_buf_get(dev, cfg->addr);
705+
buf = udc_buf_get(cfg);
702706
if (buf == NULL) {
703707
LOG_ERR("No buffer queued for control ep");
704708
return;
@@ -753,7 +757,7 @@ static void udc_event_xfer(const struct device *dev, struct udc_ep_config *const
753757
{
754758
struct net_buf *buf;
755759

756-
buf = udc_buf_peek(dev, cfg->addr);
760+
buf = udc_buf_peek(cfg);
757761
if (buf == NULL) {
758762
LOG_ERR("No buffer for ep 0x%02x", cfg->addr);
759763
return;

drivers/usb/udc/udc_common.c

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -78,22 +78,13 @@ struct udc_ep_config *udc_get_ep_cfg(const struct device *dev, const uint8_t ep)
7878
return data->ep_lut[USB_EP_LUT_IDX(ep)];
7979
}
8080

81-
bool udc_ep_is_busy(const struct device *dev, const uint8_t ep)
81+
bool udc_ep_is_busy(const struct udc_ep_config *const ep_cfg)
8282
{
83-
struct udc_ep_config *ep_cfg;
84-
85-
ep_cfg = udc_get_ep_cfg(dev, ep);
86-
__ASSERT(ep_cfg != NULL, "ep 0x%02x is not available", ep);
87-
8883
return ep_cfg->stat.busy;
8984
}
9085

91-
void udc_ep_set_busy(const struct device *dev, const uint8_t ep, const bool busy)
86+
void udc_ep_set_busy(struct udc_ep_config *const ep_cfg, const bool busy)
9287
{
93-
struct udc_ep_config *ep_cfg;
94-
95-
ep_cfg = udc_get_ep_cfg(dev, ep);
96-
__ASSERT(ep_cfg != NULL, "ep 0x%02x is not available", ep);
9788
ep_cfg->stat.busy = busy;
9889
}
9990

@@ -115,34 +106,21 @@ int udc_register_ep(const struct device *dev, struct udc_ep_config *const cfg)
115106
return 0;
116107
}
117108

118-
struct net_buf *udc_buf_get(const struct device *dev, const uint8_t ep)
109+
struct net_buf *udc_buf_get(struct udc_ep_config *const ep_cfg)
119110
{
120-
struct udc_ep_config *ep_cfg;
121-
122-
ep_cfg = udc_get_ep_cfg(dev, ep);
123-
if (ep_cfg == NULL) {
124-
return NULL;
125-
}
126-
127111
return k_fifo_get(&ep_cfg->fifo, K_NO_WAIT);
128112
}
129113

130-
struct net_buf *udc_buf_get_all(const struct device *dev, const uint8_t ep)
114+
struct net_buf *udc_buf_get_all(struct udc_ep_config *const ep_cfg)
131115
{
132-
struct udc_ep_config *ep_cfg;
133116
struct net_buf *buf;
134117

135-
ep_cfg = udc_get_ep_cfg(dev, ep);
136-
if (ep_cfg == NULL) {
137-
return NULL;
138-
}
139-
140118
buf = k_fifo_get(&ep_cfg->fifo, K_NO_WAIT);
141119
if (!buf) {
142120
return NULL;
143121
}
144122

145-
LOG_DBG("ep 0x%02x dequeue %p", ep, buf);
123+
LOG_DBG("ep 0x%02x dequeue %p", ep_cfg->addr, buf);
146124
for (struct net_buf *n = buf; !k_fifo_is_empty(&ep_cfg->fifo); n = n->frags) {
147125
n->frags = k_fifo_get(&ep_cfg->fifo, K_NO_WAIT);
148126
LOG_DBG("|-> %p ", n->frags);
@@ -154,15 +132,8 @@ struct net_buf *udc_buf_get_all(const struct device *dev, const uint8_t ep)
154132
return buf;
155133
}
156134

157-
struct net_buf *udc_buf_peek(const struct device *dev, const uint8_t ep)
135+
struct net_buf *udc_buf_peek(struct udc_ep_config *const ep_cfg)
158136
{
159-
struct udc_ep_config *ep_cfg;
160-
161-
ep_cfg = udc_get_ep_cfg(dev, ep);
162-
if (ep_cfg == NULL) {
163-
return NULL;
164-
}
165-
166137
return k_fifo_peek_head(&ep_cfg->fifo);
167138
}
168139

drivers/usb/udc/udc_common.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,19 @@ struct udc_ep_config *udc_get_ep_cfg(const struct device *dev,
6161
/**
6262
* @brief Checks if the endpoint is busy
6363
*
64-
* @param[in] dev Pointer to device struct of the driver instance
65-
* @param[in] ep Endpoint address
64+
* @param[in] ep_cfg Pointer to endpoint configuration
6665
*
6766
* @return true if endpoint is busy
6867
*/
69-
bool udc_ep_is_busy(const struct device *dev, const uint8_t ep);
68+
bool udc_ep_is_busy(const struct udc_ep_config *const ep_cfg);
7069

7170
/**
7271
* @brief Helper function to set endpoint busy state
7372
*
74-
* @param[in] dev Pointer to device struct of the driver instance
75-
* @param[in] ep Endpoint address
73+
* @param[in] ep_cfg Pointer to endpoint configuration
7674
* @param[in] busy Busy state
7775
*/
78-
void udc_ep_set_busy(const struct device *dev, const uint8_t ep,
76+
void udc_ep_set_busy(struct udc_ep_config *const ep_cfg,
7977
const bool busy);
8078

8179
/**
@@ -85,13 +83,11 @@ void udc_ep_set_busy(const struct device *dev, const uint8_t ep,
8583
* Use it when transfer is finished and request should
8684
* be passed to the higher level.
8785
*
88-
* @param[in] dev Pointer to device struct of the driver instance
89-
* @param[in] ep Endpoint address
86+
* @param[in] ep_cfg Pointer to endpoint configuration
9087
*
9188
* @return pointer to UDC request or NULL on error.
9289
*/
93-
struct net_buf *udc_buf_get(const struct device *dev,
94-
const uint8_t ep);
90+
struct net_buf *udc_buf_get(struct udc_ep_config *const ep_cfg);
9591

9692
/**
9793
* @brief Get all UDC request from endpoint FIFO.
@@ -100,27 +96,23 @@ struct net_buf *udc_buf_get(const struct device *dev,
10096
* This function removes all request from endpoint FIFO and
10197
* is typically used to dequeue endpoint FIFO.
10298
*
103-
* @param[in] dev Pointer to device struct of the driver instance
104-
* @param[in] ep Endpoint address
99+
* @param[in] ep_cfg Pointer to endpoint configuration
105100
*
106101
* @return pointer to UDC request or NULL on error.
107102
*/
108-
struct net_buf *udc_buf_get_all(const struct device *dev,
109-
const uint8_t ep);
103+
struct net_buf *udc_buf_get_all(struct udc_ep_config *const ep_cfg);
110104

111105
/**
112106
* @brief Peek request at the head of endpoint FIFO.
113107
*
114108
* Return request from the head of endpoint FIFO without removing.
115109
* Use it when request buffer is required for a transfer.
116110
*
117-
* @param[in] dev Pointer to device struct of the driver instance
118-
* @param[in] ep Endpoint address
111+
* @param[in] ep_cfg Pointer to endpoint configuration
119112
*
120113
* @return pointer to request or NULL on error.
121114
*/
122-
struct net_buf *udc_buf_peek(const struct device *dev,
123-
const uint8_t ep);
115+
struct net_buf *udc_buf_peek(struct udc_ep_config *const ep_cfg);
124116

125117
/**
126118
* @brief Put request at the tail of endpoint FIFO.

0 commit comments

Comments
 (0)