Skip to content

Commit fa77524

Browse files
jfischer-nokartben
authored andcommitted
usb: host: rework usbh_xfer_alloc() parameters
The transfers require enpoint MPS for proper transaction handling, assign it in the common place during transfer allsocation so that it can be reused. Some users, such as USBIP, may need to keep a reference to private data, add a parameter for completion callback data. Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
1 parent 63bd9ac commit fa77524

File tree

5 files changed

+45
-21
lines changed

5 files changed

+45
-21
lines changed

drivers/usb/uhc/uhc_common.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,41 @@ void uhc_xfer_buf_free(const struct device *dev, struct net_buf *const buf)
9393

9494
struct uhc_transfer *uhc_xfer_alloc(const struct device *dev,
9595
const uint8_t ep,
96-
const uint16_t mps,
97-
void *const udev,
98-
void *const cb)
96+
struct usb_device *const udev,
97+
void *const cb,
98+
void *const cb_priv)
9999
{
100+
uint8_t ep_idx = USB_EP_GET_IDX(ep) & 0xF;
100101
const struct uhc_api *api = dev->api;
101102
struct uhc_transfer *xfer = NULL;
103+
uint16_t mps;
102104

103105
api->lock(dev);
104106

105107
if (!uhc_is_initialized(dev)) {
106108
goto xfer_alloc_error;
107109
}
108110

109-
LOG_DBG("Allocate xfer, ep 0x%02x cb %p", ep, cb);
111+
if (ep_idx == 0) {
112+
mps = udev->dev_desc.bMaxPacketSize0;
113+
} else {
114+
struct usb_ep_descriptor *ep_desc;
115+
116+
if (USB_EP_DIR_IS_IN(ep)) {
117+
ep_desc = udev->ep_in[ep_idx].desc;
118+
} else {
119+
ep_desc = udev->ep_out[ep_idx].desc;
120+
}
121+
122+
if (ep_desc == NULL) {
123+
LOG_ERR("Endpoint 0x%02x is not configured", ep);
124+
goto xfer_alloc_error;
125+
}
126+
127+
mps = ep_desc->wMaxPacketSize;
128+
}
129+
130+
LOG_DBG("Allocate xfer, ep 0x%02x mps %u cb %p", ep, mps, cb);
110131

111132
if (k_mem_slab_alloc(&uhc_xfer_pool, (void **)&xfer, K_NO_WAIT)) {
112133
LOG_ERR("Failed to allocate transfer");
@@ -118,6 +139,7 @@ struct uhc_transfer *uhc_xfer_alloc(const struct device *dev,
118139
xfer->mps = mps;
119140
xfer->udev = udev;
120141
xfer->cb = cb;
142+
xfer->priv = cb_priv;
121143

122144
xfer_alloc_error:
123145
api->unlock(dev);
@@ -127,9 +149,9 @@ struct uhc_transfer *uhc_xfer_alloc(const struct device *dev,
127149

128150
struct uhc_transfer *uhc_xfer_alloc_with_buf(const struct device *dev,
129151
const uint8_t ep,
130-
const uint16_t mps,
131-
void *const udev,
152+
struct usb_device *const udev,
132153
void *const cb,
154+
void *const cb_priv,
133155
size_t size)
134156
{
135157
struct uhc_transfer *xfer;
@@ -140,7 +162,7 @@ struct uhc_transfer *uhc_xfer_alloc_with_buf(const struct device *dev,
140162
return NULL;
141163
}
142164

143-
xfer = uhc_xfer_alloc(dev, ep, mps, udev, cb);
165+
xfer = uhc_xfer_alloc(dev, ep, udev, cb, cb_priv);
144166
if (xfer == NULL) {
145167
net_buf_unref(buf);
146168
return NULL;

include/zephyr/drivers/usb/uhc.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ struct uhc_transfer {
9393
struct usb_device *udev;
9494
/** Pointer to transfer completion callback (opaque for the UHC) */
9595
void *cb;
96+
/** Pointer to completion callback private data */
97+
void *priv;
9698
/** Transfer result, 0 on success, other values on error */
9799
int err;
98100
};
@@ -361,17 +363,17 @@ static inline int uhc_bus_resume(const struct device *dev)
361363
*
362364
* @param[in] dev Pointer to device struct of the driver instance
363365
* @param[in] ep Endpoint address
364-
* @param[in] mps Maximum packet size of the endpoint
365-
* @param[in] udev Opaque pointer to USB device
366+
* @param[in] udev Pointer to USB device
366367
* @param[in] cb Transfer completion callback
368+
* @param[in] cb_priv Completion callback callback private data
367369
*
368370
* @return pointer to allocated transfer or NULL on error.
369371
*/
370372
struct uhc_transfer *uhc_xfer_alloc(const struct device *dev,
371373
const uint8_t ep,
372-
const uint16_t mps,
373-
void *const udev,
374-
void *const cb);
374+
struct usb_device *const udev,
375+
void *const cb,
376+
void *const cb_priv);
375377

376378
/**
377379
* @brief Allocate UHC transfer with buffer
@@ -380,18 +382,18 @@ struct uhc_transfer *uhc_xfer_alloc(const struct device *dev,
380382
*
381383
* @param[in] dev Pointer to device struct of the driver instance
382384
* @param[in] ep Endpoint address
383-
* @param[in] mps Maximum packet size of the endpoint
384-
* @param[in] udev Opaque pointer to USB device
385+
* @param[in] udev Pointer to USB device
385386
* @param[in] cb Transfer completion callback
387+
* @param[in] cb_priv Completion callback callback private data
386388
* @param[in] size Size of the buffer
387389
*
388390
* @return pointer to allocated transfer or NULL on error.
389391
*/
390392
struct uhc_transfer *uhc_xfer_alloc_with_buf(const struct device *dev,
391393
const uint8_t ep,
392-
const uint16_t mps,
393-
void *const udev,
394+
struct usb_device *const udev,
394395
void *const cb,
396+
void *const cb_priv,
395397
size_t size);
396398

397399
/**

subsys/usb/host/usbh_ch9.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ int usbh_req_setup(struct usb_device *const udev,
6060
uint8_t ep = usb_reqtype_is_to_device(&req) ? 0x00 : 0x80;
6161
int ret;
6262

63-
xfer = usbh_xfer_alloc(udev, ep, 64, ch9_req_cb);
63+
xfer = usbh_xfer_alloc(udev, ep, ch9_req_cb, NULL);
6464
if (!xfer) {
6565
return -ENOMEM;
6666
}

subsys/usb/host/usbh_device.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ struct usb_device *usbh_device_get_any(struct usbh_contex *const ctx);
2424
/* Wrappers around to avoid glue UHC calls. */
2525
static inline struct uhc_transfer *usbh_xfer_alloc(struct usb_device *udev,
2626
const uint8_t ep,
27-
const uint16_t mps,
28-
usbh_udev_cb_t cb)
27+
usbh_udev_cb_t cb,
28+
void *const cb_priv)
2929
{
3030
struct usbh_contex *const ctx = udev->ctx;
3131

32-
return uhc_xfer_alloc(ctx->dev, ep, mps, udev, cb);
32+
return uhc_xfer_alloc(ctx->dev, ep, udev, cb, cb_priv);
3333
}
3434

3535
static inline int usbh_xfer_buf_add(const struct usb_device *udev,

subsys/usb/host/usbh_shell.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static int cmd_bulk(const struct shell *sh, size_t argc, char **argv)
8787
ep = strtol(argv[1], NULL, 16);
8888
len = MIN(sizeof(vreq_test_buf), strtol(argv[2], NULL, 10));
8989

90-
xfer = usbh_xfer_alloc(udev, ep, 512, bulk_req_cb);
90+
xfer = usbh_xfer_alloc(udev, ep, bulk_req_cb, NULL);
9191
if (!xfer) {
9292
shell_error(sh, "host: Failed to allocate transfer");
9393
return -ENOMEM;

0 commit comments

Comments
 (0)