Skip to content

Commit c3bcf31

Browse files
jfischer-nojhedberg
authored andcommitted
usb: host: add a structure to represent a USB device.
Add a structure to represent a USB device and wrappers to avoid glue UHC calls when operating on devices. Although there is a long road to device configuration and management, we can start with the tools that can also be used for USB device support testing. Signed-off-by: Johann Fischer <[email protected]>
1 parent aced8f5 commit c3bcf31

File tree

3 files changed

+115
-0
lines changed

3 files changed

+115
-0
lines changed

subsys/usb/host/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ zephyr_library_sources(
88
usbh_ch9.c
99
usbh_core.c
1010
usbh_api.c
11+
usbh_device.c
1112
)
1213

1314
zephyr_library_sources_ifdef(

subsys/usb/host/usbh_device.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/usb/usbh.h>
8+
#include "usbh_device.h"
9+
10+
#define USBH_USB_DEVICE_COUNT 1
11+
12+
static struct usb_device udevs[USBH_USB_DEVICE_COUNT];
13+
14+
struct usb_device *usbh_device_get_any(struct usbh_contex *const ctx)
15+
{
16+
udevs->ctx = ctx;
17+
18+
return udevs;
19+
}

subsys/usb/host/usbh_device.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright (c) 2023 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_USBH_DEVICE_H
8+
#define ZEPHYR_INCLUDE_USBH_DEVICE_H
9+
10+
#include <stdint.h>
11+
#include <zephyr/usb/usbh.h>
12+
#include <zephyr/drivers/usb/uhc.h>
13+
14+
/* USB device state */
15+
enum usb_device_state {
16+
USB_STATE_NOTCONNECTED,
17+
USB_STATE_DEFAULT,
18+
USB_STATE_ADDRESSED,
19+
USB_STATE_CONFIGURED,
20+
};
21+
22+
/* Host support view of a USB device */
23+
struct usb_device {
24+
struct usbh_contex *ctx;
25+
struct usb_device_descriptor dev_desc;
26+
enum usb_device_state state;
27+
uint8_t actual_cfg;
28+
uint8_t addr;
29+
};
30+
31+
/* Callback type to be used for e.g. synchronous requests */
32+
typedef int (*usbh_udev_cb_t)(struct usb_device *const udev,
33+
struct uhc_transfer *const xfer);
34+
35+
/*
36+
* Get a device to work on, there will only be one for the first time
37+
* until we implement USB device configuration/management.
38+
*/
39+
struct usb_device *usbh_device_get_any(struct usbh_contex *const ctx);
40+
41+
/* Wrappers around to avoid glue UHC calls. */
42+
static inline struct uhc_transfer *usbh_xfer_alloc(struct usb_device *udev,
43+
const uint8_t ep,
44+
const uint8_t attrib,
45+
const uint16_t mps,
46+
const uint16_t timeout,
47+
usbh_udev_cb_t *const cb)
48+
{
49+
struct usbh_contex *const ctx = udev->ctx;
50+
51+
return uhc_xfer_alloc(ctx->dev, udev->addr, ep, attrib, mps, timeout, udev, cb);
52+
}
53+
54+
static inline int usbh_xfer_buf_add(const struct usb_device *udev,
55+
struct uhc_transfer *const xfer,
56+
struct net_buf *buf)
57+
{
58+
struct usbh_contex *const ctx = udev->ctx;
59+
60+
return uhc_xfer_buf_add(ctx->dev, xfer, buf);
61+
}
62+
63+
static inline struct net_buf *usbh_xfer_buf_alloc(struct usb_device *udev,
64+
const size_t size)
65+
{
66+
struct usbh_contex *const ctx = udev->ctx;
67+
68+
return uhc_xfer_buf_alloc(ctx->dev, size);
69+
}
70+
71+
static inline int usbh_xfer_free(const struct usb_device *udev,
72+
struct uhc_transfer *const xfer)
73+
{
74+
struct usbh_contex *const ctx = udev->ctx;
75+
76+
return uhc_xfer_free(ctx->dev, xfer);
77+
}
78+
79+
static inline void usbh_xfer_buf_free(const struct usb_device *udev,
80+
struct net_buf *const buf)
81+
{
82+
struct usbh_contex *const ctx = udev->ctx;
83+
84+
uhc_xfer_buf_free(ctx->dev, buf);
85+
}
86+
87+
static inline int usbh_xfer_enqueue(const struct usb_device *udev,
88+
struct uhc_transfer *const xfer)
89+
{
90+
struct usbh_contex *const ctx = udev->ctx;
91+
92+
return uhc_ep_enqueue(ctx->dev, xfer);
93+
}
94+
95+
#endif /* ZEPHYR_INCLUDE_USBH_DEVICE_H */

0 commit comments

Comments
 (0)