Skip to content

Commit 63bd9ac

Browse files
jfischer-nokartben
authored andcommitted
drivers: uhc: add opaque pointer to store upper layer private data
Add an opaque pointer to store upper layer private data and initialize it with the USB host context during controller initialization. Use the pointer in event processing to get the correct context. Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
1 parent 5d5de63 commit 63bd9ac

File tree

3 files changed

+29
-8
lines changed

3 files changed

+29
-8
lines changed

drivers/usb/uhc/uhc_common.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ int uhc_disable(const struct device *dev)
288288
return ret;
289289
}
290290

291-
int uhc_init(const struct device *dev, uhc_event_cb_t event_cb)
291+
int uhc_init(const struct device *dev,
292+
uhc_event_cb_t event_cb, const void *const event_ctx)
292293
{
293294
const struct uhc_api *api = dev->api;
294295
struct uhc_data *data = dev->data;
@@ -306,6 +307,7 @@ int uhc_init(const struct device *dev, uhc_event_cb_t event_cb)
306307
}
307308

308309
data->event_cb = event_cb;
310+
data->event_ctx = event_ctx;
309311
sys_dlist_init(&data->ctrl_xfers);
310312
sys_dlist_init(&data->bulk_xfers);
311313

include/zephyr/drivers/usb/uhc.h

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ struct uhc_data {
199199
sys_dlist_t bulk_xfers;
200200
/** Callback to submit an UHC event to upper layer */
201201
uhc_event_cb_t event_cb;
202+
/** Opaque pointer to store higher layer context */
203+
const void *event_ctx;
202204
/** USB host controller status */
203205
atomic_t status;
204206
/** Driver private data */
@@ -477,12 +479,14 @@ int uhc_ep_dequeue(const struct device *dev, struct uhc_transfer *const xfer);
477479
*
478480
* @param[in] dev Pointer to device struct of the driver instance
479481
* @param[in] event_cb Event callback from the higher layer (USB host stack)
482+
* @param[in] event_ctx Opaque pointer to higher layer context
480483
*
481484
* @return 0 on success, all other values should be treated as error.
482485
* @retval -EINVAL on parameter error (no callback is passed)
483486
* @retval -EALREADY already initialized
484487
*/
485-
int uhc_init(const struct device *dev, uhc_event_cb_t event_cb);
488+
int uhc_init(const struct device *dev,
489+
uhc_event_cb_t event_cb, const void *const event_ctx);
486490

487491
/**
488492
* @brief Enable USB host controller
@@ -540,6 +544,23 @@ static inline struct uhc_device_caps uhc_caps(const struct device *dev)
540544
return data->caps;
541545
}
542546

547+
/**
548+
* @brief Get pointer to higher layer context
549+
*
550+
* The address of the context is passed as an argument to the uhc_init()
551+
* function and is stored in the uhc data.
552+
*
553+
* @param[in] dev Pointer to device struct of the driver instance
554+
*
555+
* @return Opaque pointer to higher layer context
556+
*/
557+
static inline const void *uhc_get_event_ctx(const struct device *dev)
558+
{
559+
struct uhc_data *data = dev->data;
560+
561+
return data->event_ctx;
562+
}
563+
543564
/**
544565
* @}
545566
*/

subsys/usb/host/usbh_core.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,24 +96,22 @@ static void usbh_thread(void *p1, void *p2, void *p3)
9696
ARG_UNUSED(p2);
9797
ARG_UNUSED(p3);
9898

99+
struct usbh_contex *uhs_ctx;
99100
struct uhc_event event;
100101

101102
while (true) {
102103
k_msgq_get(&usbh_msgq, &event, K_FOREVER);
103104

104-
STRUCT_SECTION_FOREACH(usbh_contex, uhs_ctx) {
105-
if (uhs_ctx->dev == event.dev) {
106-
usbh_event_handler(uhs_ctx, &event);
107-
}
108-
}
105+
uhs_ctx = (void *)uhc_get_event_ctx(event.dev);
106+
usbh_event_handler(uhs_ctx, &event);
109107
}
110108
}
111109

112110
int usbh_init_device_intl(struct usbh_contex *const uhs_ctx)
113111
{
114112
int ret;
115113

116-
ret = uhc_init(uhs_ctx->dev, usbh_event_carrier);
114+
ret = uhc_init(uhs_ctx->dev, usbh_event_carrier, uhs_ctx);
117115
if (ret != 0) {
118116
LOG_ERR("Failed to init device driver");
119117
return ret;

0 commit comments

Comments
 (0)