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 /* Allocate/free USB device */
26 struct usb_device *usbh_device_alloc(struct usbh_contex *const uhs_ctx);
27 void usbh_device_free(struct usb_device *const udev);
28 
29 /* Reset and configure new USB device */
30 int usbh_device_init(struct usb_device *const udev);
31 
32 /* Set USB device interface alternate */
33 int usbh_device_interface_set(struct usb_device *const udev,
34 			      const uint8_t iface, const uint8_t alt,
35 			      const bool dry);
36 
37 /* 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)38 static inline struct uhc_transfer *usbh_xfer_alloc(struct usb_device *udev,
39 						   const uint8_t ep,
40 						   usbh_udev_cb_t cb,
41 						   void *const cb_priv)
42 {
43 	struct usbh_contex *const ctx = udev->ctx;
44 
45 	return uhc_xfer_alloc(ctx->dev, ep, udev, cb, cb_priv);
46 }
47 
usbh_xfer_buf_add(const struct usb_device * udev,struct uhc_transfer * const xfer,struct net_buf * buf)48 static inline int usbh_xfer_buf_add(const struct usb_device *udev,
49 				    struct uhc_transfer *const xfer,
50 				    struct net_buf *buf)
51 {
52 	struct usbh_contex *const ctx = udev->ctx;
53 
54 	return uhc_xfer_buf_add(ctx->dev, xfer, buf);
55 }
56 
usbh_xfer_buf_alloc(struct usb_device * udev,const size_t size)57 static inline struct net_buf *usbh_xfer_buf_alloc(struct usb_device *udev,
58 						  const size_t size)
59 {
60 	struct usbh_contex *const ctx = udev->ctx;
61 
62 	return uhc_xfer_buf_alloc(ctx->dev, size);
63 }
64 
usbh_xfer_free(const struct usb_device * udev,struct uhc_transfer * const xfer)65 static inline int usbh_xfer_free(const struct usb_device *udev,
66 				 struct uhc_transfer *const xfer)
67 {
68 	struct usbh_contex *const ctx = udev->ctx;
69 
70 	return uhc_xfer_free(ctx->dev, xfer);
71 }
72 
usbh_xfer_buf_free(const struct usb_device * udev,struct net_buf * const buf)73 static inline void usbh_xfer_buf_free(const struct usb_device *udev,
74 				      struct net_buf *const buf)
75 {
76 	struct usbh_contex *const ctx = udev->ctx;
77 
78 	uhc_xfer_buf_free(ctx->dev, buf);
79 }
80 
usbh_xfer_enqueue(const struct usb_device * udev,struct uhc_transfer * const xfer)81 static inline int usbh_xfer_enqueue(const struct usb_device *udev,
82 				    struct uhc_transfer *const xfer)
83 {
84 	struct usbh_contex *const ctx = udev->ctx;
85 
86 	return uhc_ep_enqueue(ctx->dev, xfer);
87 }
88 
usbh_xfer_dequeue(const struct usb_device * udev,struct uhc_transfer * const xfer)89 static inline int usbh_xfer_dequeue(const struct usb_device *udev,
90 				    struct uhc_transfer *const xfer)
91 {
92 	struct usbh_contex *const ctx = udev->ctx;
93 
94 	return uhc_ep_dequeue(ctx->dev, xfer);
95 }
96 
97 #endif /* ZEPHYR_INCLUDE_USBH_DEVICE_H */
98