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 /* Callback type to be used for e.g. synchronous requests */
15 typedef int (*usbh_udev_cb_t)(struct usb_device *const udev,
16 			      struct uhc_transfer *const xfer);
17 
18 /*
19  * This will return the first available USB device; for a single-point
20  * connection without hub support, this is the device connected directly to the
21  * host controller.
22  */
23 struct usb_device *usbh_device_get_any(struct usbh_contex *const ctx);
24 
25 struct usb_device *usbh_device_get(struct usbh_contex *const uhs_ctx, const uint8_t addr);
26 
27 /* Allocate/free USB device */
28 struct usb_device *usbh_device_alloc(struct usbh_contex *const uhs_ctx);
29 void usbh_device_free(struct usb_device *const udev);
30 
31 /* Reset and configure new USB device */
32 int usbh_device_init(struct usb_device *const udev);
33 
34 /* Set USB device interface alternate */
35 int usbh_device_interface_set(struct usb_device *const udev,
36 			      const uint8_t iface, const uint8_t alt,
37 			      const bool dry);
38 
39 /* Wrappers around to avoid glue UHC calls. */
usbh_xfer_alloc(struct usb_device * udev,const uint8_t ep,usbh_udev_cb_t cb,void * const cb_priv)40 static inline struct uhc_transfer *usbh_xfer_alloc(struct usb_device *udev,
41 						   const uint8_t ep,
42 						   usbh_udev_cb_t cb,
43 						   void *const cb_priv)
44 {
45 	struct usbh_contex *const ctx = udev->ctx;
46 
47 	return uhc_xfer_alloc(ctx->dev, ep, udev, cb, cb_priv);
48 }
49 
usbh_xfer_buf_add(const struct usb_device * udev,struct uhc_transfer * const xfer,struct net_buf * buf)50 static inline int usbh_xfer_buf_add(const struct usb_device *udev,
51 				    struct uhc_transfer *const xfer,
52 				    struct net_buf *buf)
53 {
54 	struct usbh_contex *const ctx = udev->ctx;
55 
56 	return uhc_xfer_buf_add(ctx->dev, xfer, buf);
57 }
58 
usbh_xfer_buf_alloc(struct usb_device * udev,const size_t size)59 static inline struct net_buf *usbh_xfer_buf_alloc(struct usb_device *udev,
60 						  const size_t size)
61 {
62 	struct usbh_contex *const ctx = udev->ctx;
63 
64 	return uhc_xfer_buf_alloc(ctx->dev, size);
65 }
66 
usbh_xfer_free(const struct usb_device * udev,struct uhc_transfer * const xfer)67 static inline int usbh_xfer_free(const struct usb_device *udev,
68 				 struct uhc_transfer *const xfer)
69 {
70 	struct usbh_contex *const ctx = udev->ctx;
71 
72 	return uhc_xfer_free(ctx->dev, xfer);
73 }
74 
usbh_xfer_buf_free(const struct usb_device * udev,struct net_buf * const buf)75 static inline void usbh_xfer_buf_free(const struct usb_device *udev,
76 				      struct net_buf *const buf)
77 {
78 	struct usbh_contex *const ctx = udev->ctx;
79 
80 	uhc_xfer_buf_free(ctx->dev, buf);
81 }
82 
usbh_xfer_enqueue(const struct usb_device * udev,struct uhc_transfer * const xfer)83 static inline int usbh_xfer_enqueue(const struct usb_device *udev,
84 				    struct uhc_transfer *const xfer)
85 {
86 	struct usbh_contex *const ctx = udev->ctx;
87 
88 	return uhc_ep_enqueue(ctx->dev, xfer);
89 }
90 
usbh_xfer_dequeue(const struct usb_device * udev,struct uhc_transfer * const xfer)91 static inline int usbh_xfer_dequeue(const struct usb_device *udev,
92 				    struct uhc_transfer *const xfer)
93 {
94 	struct usbh_contex *const ctx = udev->ctx;
95 
96 	return uhc_ep_dequeue(ctx->dev, xfer);
97 }
98 
99 #endif /* ZEPHYR_INCLUDE_USBH_DEVICE_H */
100