Skip to content

Commit 511cd38

Browse files
committed
[nrf fromtree] 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]> (cherry picked from commit 1cccf28)
1 parent a4c30ef commit 511cd38

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
@@ -96,13 +96,14 @@ static int usbd_ctrl_feed_dout(const struct device *dev, const size_t length)
9696
static int udc_ambiq_tx(const struct device *dev, uint8_t ep, struct net_buf *buf)
9797
{
9898
const struct udc_ambiq_data *priv = udc_get_private(dev);
99+
struct udc_ep_config *cfg = udc_get_ep_cfg(dev, ep);
99100
uint32_t status;
100101

101-
if (udc_ep_is_busy(dev, ep)) {
102+
if (udc_ep_is_busy(cfg)) {
102103
LOG_WRN("ep 0x%02x is busy!", ep);
103104
return 0;
104105
}
105-
udc_ep_set_busy(dev, ep, true);
106+
udc_ep_set_busy(cfg, true);
106107

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

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

130-
if (udc_ep_is_busy(dev, ep)) {
132+
if (udc_ep_is_busy(ep_cfg)) {
131133
LOG_WRN("ep 0x%02x is busy!", ep);
132134
return 0;
133135
}
134-
udc_ep_set_busy(dev, ep, true);
136+
udc_ep_set_busy(ep_cfg, true);
135137

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

141143
status = am_hal_usb_ep_xfer(priv->usb_handle, ep, buf->data, rx_size);
142144
if (status != AM_HAL_STATUS_SUCCESS) {
143-
udc_ep_set_busy(dev, ep, false);
145+
udc_ep_set_busy(ep_cfg, false);
144146
LOG_ERR("am_hal_usb_ep_xfer read(rx) failed(0x%02x), %d", ep, (int)status);
145147
return -EIO;
146148
}
@@ -223,7 +225,9 @@ static void udc_ambiq_ep_xfer_complete_callback(const struct device *dev, uint8_
223225
if (USB_EP_DIR_IS_IN(ep_addr)) {
224226
evt.type = UDC_AMBIQ_EVT_HAL_IN_CMP;
225227
} else {
226-
buf = udc_buf_peek(dev, ep_addr);
228+
struct udc_ep_config *ep_cfg = udc_get_ep_cfg(dev, ep_addr);
229+
230+
buf = udc_buf_peek(ep_cfg);
227231
if (buf == NULL) {
228232
LOG_ERR("No buffer for ep 0x%02x", ep_addr);
229233
udc_submit_event(dev, UDC_EVT_ERROR, -ENOBUFS);
@@ -281,12 +285,12 @@ static int udc_ambiq_ep_dequeue(const struct device *dev, struct udc_ep_config *
281285

282286
lock_key = irq_lock();
283287

284-
buf = udc_buf_get_all(dev, ep_cfg->addr);
288+
buf = udc_buf_get_all(ep_cfg);
285289
if (buf) {
286290
udc_submit_ep_event(dev, buf, -ECONNABORTED);
287291
}
288292

289-
udc_ep_set_busy(dev, ep_cfg->addr, false);
293+
udc_ep_set_busy(ep_cfg, false);
290294
am_hal_usb_ep_state_reset(priv->usb_handle, ep_cfg->addr);
291295
irq_unlock(lock_key);
292296

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

322326
/* Resume queued transfer if any */
323-
if (udc_buf_peek(dev, ep_cfg->addr)) {
327+
if (udc_buf_peek(ep_cfg)) {
324328
struct udc_ambiq_event evt = {
325329
.ep = ep_cfg->addr,
326330
.type = UDC_AMBIQ_EVT_XFER,
@@ -647,14 +651,14 @@ static inline void ambiq_handle_evt_dout(const struct device *dev, struct udc_ep
647651
struct net_buf *buf;
648652

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

656660
/* Clear endpoint busy status */
657-
udc_ep_set_busy(dev, cfg->addr, false);
661+
udc_ep_set_busy(cfg, false);
658662

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

688692
/* Clear endpoint busy status */
689-
udc_ep_set_busy(dev, cfg->addr, false);
693+
udc_ep_set_busy(cfg, false);
690694
/* Check and Handle ZLP flag */
691-
buf = udc_buf_peek(dev, cfg->addr);
695+
buf = udc_buf_peek(cfg);
692696
if (cfg->addr != USB_CONTROL_EP_IN) {
693697
if (udc_ep_buf_has_zlp(buf)) {
694698
udc_ep_buf_clear_zlp(buf);
@@ -699,7 +703,7 @@ static void ambiq_handle_evt_din(const struct device *dev, struct udc_ep_config
699703
}
700704

701705
/* retrieve endpoint buffer */
702-
buf = udc_buf_get(dev, cfg->addr);
706+
buf = udc_buf_get(cfg);
703707
if (buf == NULL) {
704708
LOG_ERR("No buffer queued for control ep");
705709
return;
@@ -754,7 +758,7 @@ static void udc_event_xfer(const struct device *dev, struct udc_ep_config *const
754758
{
755759
struct net_buf *buf;
756760

757-
buf = udc_buf_peek(dev, cfg->addr);
761+
buf = udc_buf_peek(cfg);
758762
if (buf == NULL) {
759763
LOG_ERR("No buffer for ep 0x%02x", cfg->addr);
760764
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)