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. */
usbh_xfer_alloc(struct usb_device * udev,const uint8_t ep,const uint8_t attrib,const uint16_t mps,const uint16_t timeout,usbh_udev_cb_t * const cb)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 
usbh_xfer_buf_add(const struct usb_device * udev,struct uhc_transfer * const xfer,struct net_buf * buf)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 
usbh_xfer_buf_alloc(struct usb_device * udev,const size_t size)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 
usbh_xfer_free(const struct usb_device * udev,struct uhc_transfer * const xfer)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 
usbh_xfer_buf_free(const struct usb_device * udev,struct net_buf * const buf)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 
usbh_xfer_enqueue(const struct usb_device * udev,struct uhc_transfer * const xfer)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 */
96