usb: device_next: add helpers to get private data and device context

The class implementations should not access the members of the struct
usbd_class_node directly.

Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
diff --git a/include/zephyr/usb/usbd.h b/include/zephyr/usb/usbd.h
index d6c6eb0..603b67e 100644
--- a/include/zephyr/usb/usbd.h
+++ b/include/zephyr/usb/usbd.h
@@ -284,6 +284,40 @@
 	struct usbd_class_data *data;
 };
 
+/**
+ * @brief Get the USB device runtime context under which the class is registered
+ *
+ * The class implementation must use this function and not access the members
+ * of the struct directly.
+ *
+ * @param[in] node Pointer to USB device class node
+ *
+ * @return Pointer to USB device runtime context
+ */
+static inline struct usbd_contex *usbd_class_get_ctx(const struct usbd_class_node *const c_nd)
+{
+	struct usbd_class_data *const c_data = c_nd->data;
+
+	return c_data->uds_ctx;
+}
+
+/**
+ * @brief Get class implementation private data
+ *
+ * The class implementation must use this function and not access the members
+ * of the struct directly.
+ *
+ * @param[in] node Pointer to USB device class node
+ *
+ * @return Pointer to class implementation private data
+ */
+static inline void *usbd_class_get_private(const struct usbd_class_node *const c_nd)
+{
+	struct usbd_class_data *const c_data = c_nd->data;
+
+	return c_data->priv;
+}
+
 #define USBD_DEVICE_DEFINE(device_name, uhc_dev, vid, pid)		\
 	static struct usb_device_descriptor				\
 	desc_##device_name = {						\
diff --git a/subsys/usb/device_next/class/bt_hci.c b/subsys/usb/device_next/class/bt_hci.c
index 8772ffe..d41024e 100644
--- a/subsys/usb/device_next/class/bt_hci.c
+++ b/subsys/usb/device_next/class/bt_hci.c
@@ -166,7 +166,7 @@
 static void bt_hci_tx_sync_in(struct usbd_class_node *const c_nd,
 			      struct net_buf *const bt_buf, const uint8_t ep)
 {
-	struct bt_hci_data *hci_data = c_nd->data->priv;
+	struct bt_hci_data *hci_data = usbd_class_get_private(c_nd);
 	struct net_buf *buf;
 
 	buf = bt_hci_buf_alloc(ep);
@@ -231,7 +231,7 @@
 
 static int bt_hci_acl_out_start(struct usbd_class_node *const c_nd)
 {
-	struct bt_hci_data *hci_data = c_nd->data->priv;
+	struct bt_hci_data *hci_data = usbd_class_get_private(c_nd);
 	struct net_buf *buf;
 	uint8_t ep;
 	int ret;
@@ -301,7 +301,7 @@
 static int bt_hci_acl_out_cb(struct usbd_class_node *const c_nd,
 			     struct net_buf *const buf, const int err)
 {
-	struct bt_hci_data *hci_data = c_nd->data->priv;
+	struct bt_hci_data *hci_data = usbd_class_get_private(c_nd);
 
 	if (err) {
 		goto restart_out_transfer;
@@ -358,8 +358,8 @@
 static int bt_hci_request(struct usbd_class_node *const c_nd,
 			  struct net_buf *buf, int err)
 {
-	struct usbd_contex *uds_ctx = c_nd->data->uds_ctx;
-	struct bt_hci_data *hci_data = c_nd->data->priv;
+	struct usbd_contex *uds_ctx = usbd_class_get_ctx(c_nd);
+	struct bt_hci_data *hci_data = usbd_class_get_private(c_nd);
 	struct udc_buf_info *bi;
 
 	bi = udc_get_buf_info(buf);
@@ -387,7 +387,7 @@
 
 static void bt_hci_enable(struct usbd_class_node *const c_nd)
 {
-	struct bt_hci_data *hci_data = c_nd->data->priv;
+	struct bt_hci_data *hci_data = usbd_class_get_private(c_nd);
 
 	atomic_set_bit(&hci_data->state, BT_HCI_CLASS_ENABLED);
 	LOG_INF("Configuration enabled");
@@ -399,7 +399,7 @@
 
 static void bt_hci_disable(struct usbd_class_node *const c_nd)
 {
-	struct bt_hci_data *hci_data = c_nd->data->priv;
+	struct bt_hci_data *hci_data = usbd_class_get_private(c_nd);
 
 	atomic_clear_bit(&hci_data->state, BT_HCI_CLASS_ENABLED);
 	LOG_INF("Configuration disabled");
diff --git a/subsys/usb/device_next/class/usbd_cdc_acm.c b/subsys/usb/device_next/class/usbd_cdc_acm.c
index 5854432..99b6773 100644
--- a/subsys/usb/device_next/class/usbd_cdc_acm.c
+++ b/subsys/usb/device_next/class/usbd_cdc_acm.c
@@ -156,8 +156,8 @@
 static int usbd_cdc_acm_request(struct usbd_class_node *const c_nd,
 				struct net_buf *buf, int err)
 {
-	struct usbd_contex *uds_ctx = c_nd->data->uds_ctx;
-	const struct device *dev = c_nd->data->priv;
+	struct usbd_contex *uds_ctx = usbd_class_get_ctx(c_nd);
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_acm_uart_data *data = dev->data;
 	struct udc_buf_info *bi;
 
@@ -216,7 +216,7 @@
 
 static void usbd_cdc_acm_enable(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_acm_uart_data *data = dev->data;
 
 	atomic_set_bit(&data->state, CDC_ACM_CLASS_ENABLED);
@@ -233,7 +233,7 @@
 
 static void usbd_cdc_acm_disable(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_acm_uart_data *data = dev->data;
 
 	atomic_clear_bit(&data->state, CDC_ACM_CLASS_ENABLED);
@@ -243,7 +243,7 @@
 
 static void usbd_cdc_acm_suspended(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_acm_uart_data *data = dev->data;
 
 	/* FIXME: filter stray suspended events earlier */
@@ -252,7 +252,7 @@
 
 static void usbd_cdc_acm_resumed(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_acm_uart_data *data = dev->data;
 
 	atomic_clear_bit(&data->state, CDC_ACM_CLASS_SUSPENDED);
@@ -334,7 +334,7 @@
 			    const struct usb_setup_packet *const setup,
 			    struct net_buf *const buf)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_acm_uart_data *data = dev->data;
 	size_t min_len;
 
@@ -361,8 +361,8 @@
 			    const struct usb_setup_packet *const setup,
 			    const struct net_buf *const buf)
 {
-	struct usbd_contex *uds_ctx = c_nd->data->uds_ctx;
-	const struct device *dev = c_nd->data->priv;
+	struct usbd_contex *uds_ctx = usbd_class_get_ctx(c_nd);
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_acm_uart_data *data = dev->data;
 	size_t len;
 
@@ -750,7 +750,7 @@
 
 	if (atomic_test_bit(&data->state, CDC_ACM_IRQ_RX_ENABLED) ||
 	    atomic_test_bit(&data->state, CDC_ACM_IRQ_TX_ENABLED)) {
-		data->cb(c_nd->data->priv, data->cb_data);
+		data->cb(usbd_class_get_private(c_nd), data->cb_data);
 	}
 
 	if (data->rx_fifo.altered) {
diff --git a/subsys/usb/device_next/class/usbd_cdc_ecm.c b/subsys/usb/device_next/class/usbd_cdc_ecm.c
index 6f7200c..72b35b9 100644
--- a/subsys/usb/device_next/class/usbd_cdc_ecm.c
+++ b/subsys/usb/device_next/class/usbd_cdc_ecm.c
@@ -105,7 +105,6 @@
 static uint8_t cdc_ecm_get_bulk_out(struct usbd_class_node *const c_nd)
 {
 	struct usbd_cdc_ecm_desc *desc = c_nd->data->desc;
-
 	return desc->if1_1_out_ep.bEndpointAddress;
 }
 
@@ -157,7 +156,7 @@
 
 static int cdc_ecm_out_start(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *data = dev->data;
 	struct net_buf *buf;
 	uint8_t ep;
@@ -189,7 +188,7 @@
 static int cdc_ecm_acl_out_cb(struct usbd_class_node *const c_nd,
 			      struct net_buf *const buf, const int err)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *data = dev->data;
 	struct net_pkt *pkt;
 
@@ -239,8 +238,8 @@
 static int usbd_cdc_ecm_request(struct usbd_class_node *const c_nd,
 				struct net_buf *buf, int err)
 {
-	struct usbd_contex *uds_ctx = c_nd->data->uds_ctx;
-	const struct device *dev = c_nd->data->priv;
+	struct usbd_contex *uds_ctx = usbd_class_get_ctx(c_nd);
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *data = dev->data;
 	struct udc_buf_info *bi;
 
@@ -320,7 +319,7 @@
 {
 	struct usbd_cdc_ecm_desc *desc = c_nd->data->desc;
 	const uint8_t data_iface = desc->if1_1.bInterfaceNumber;
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *data = dev->data;
 
 	LOG_INF("New configuration, interface %u alternate %u",
@@ -341,7 +340,7 @@
 
 static void usbd_cdc_ecm_enable(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *data = dev->data;
 
 	atomic_set_bit(&data->state, CDC_ECM_CLASS_ENABLED);
@@ -350,7 +349,7 @@
 
 static void usbd_cdc_ecm_disable(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *data = dev->data;
 
 	if (atomic_test_and_clear_bit(&data->state, CDC_ECM_CLASS_ENABLED)) {
@@ -363,7 +362,7 @@
 
 static void usbd_cdc_ecm_suspended(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *data = dev->data;
 
 	atomic_set_bit(&data->state, CDC_ECM_CLASS_SUSPENDED);
@@ -371,7 +370,7 @@
 
 static void usbd_cdc_ecm_resumed(struct usbd_class_node *const c_nd)
 {
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *data = dev->data;
 
 	atomic_clear_bit(&data->state, CDC_ECM_CLASS_SUSPENDED);
@@ -400,7 +399,7 @@
 {
 	struct usbd_cdc_ecm_desc *desc = c_nd->data->desc;
 	const uint8_t if_num = desc->if0.bInterfaceNumber;
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *const data = dev->data;
 
 	/* Update relevant b*Interface fields */
@@ -421,7 +420,7 @@
 static void usbd_cdc_ecm_shutdown(struct usbd_class_node *const c_nd)
 {
 	struct usbd_cdc_ecm_desc *desc = c_nd->data->desc;
-	const struct device *dev = c_nd->data->priv;
+	const struct device *dev = usbd_class_get_private(c_nd);
 	struct cdc_ecm_eth_data *const data = dev->data;
 
 	desc->if0_ecm.iMACAddress = 0;
diff --git a/subsys/usb/device_next/class/usbd_msc.c b/subsys/usb/device_next/class/usbd_msc.c
index 5e61fc2..6ead21f 100644
--- a/subsys/usb/device_next/class/usbd_msc.c
+++ b/subsys/usb/device_next/class/usbd_msc.c
@@ -163,7 +163,7 @@
 
 static void msc_queue_bulk_out_ep(struct usbd_class_node *const node)
 {
-	struct msc_bot_ctx *ctx = node->data->priv;
+	struct msc_bot_ctx *ctx = usbd_class_get_private(node);
 	struct net_buf *buf;
 	uint8_t ep;
 	int ret;
@@ -194,7 +194,7 @@
 	uint8_t ep;
 
 	ep = msc_get_bulk_out(node);
-	usbd_ep_set_halt(node->data->uds_ctx, ep);
+	usbd_ep_set_halt(usbd_class_get_ctx(node), ep);
 }
 
 static void msc_stall_bulk_in_ep(struct usbd_class_node *const node)
@@ -202,12 +202,12 @@
 	uint8_t ep;
 
 	ep = msc_get_bulk_in(node);
-	usbd_ep_set_halt(node->data->uds_ctx, ep);
+	usbd_ep_set_halt(usbd_class_get_ctx(node), ep);
 }
 
 static void msc_reset_handler(struct usbd_class_node *node)
 {
-	struct msc_bot_ctx *ctx = node->data->priv;
+	struct msc_bot_ctx *ctx = usbd_class_get_private(node);
 	int i;
 
 	LOG_INF("Bulk-Only Mass Storage Reset");
@@ -557,8 +557,8 @@
 static void usbd_msc_handle_request(struct usbd_class_node *node,
 				    struct net_buf *buf, int err)
 {
-	struct usbd_contex *uds_ctx = node->data->uds_ctx;
-	struct msc_bot_ctx *ctx = node->data->priv;
+	struct usbd_contex *uds_ctx = usbd_class_get_ctx(node);
+	struct msc_bot_ctx *ctx = usbd_class_get_private(node);
 	struct udc_buf_info *bi;
 
 	bi = udc_get_buf_info(buf);
@@ -600,7 +600,7 @@
 	while (1) {
 		k_msgq_get(&msc_msgq, &evt, K_FOREVER);
 
-		ctx = evt.node->data->priv;
+		ctx = usbd_class_get_private(evt.node);
 		if (evt.buf == NULL) {
 			msc_reset_handler(evt.node);
 		} else {
@@ -656,16 +656,16 @@
 static void msc_bot_feature_halt(struct usbd_class_node *const node,
 				 const uint8_t ep, const bool halted)
 {
-	struct msc_bot_ctx *ctx = node->data->priv;
+	struct msc_bot_ctx *ctx = usbd_class_get_private(node);
 
 	if (ep == msc_get_bulk_in(node) && !halted &&
 	    atomic_test_bit(&ctx->bits, MSC_BULK_IN_WEDGED)) {
 		/* Endpoint shall remain halted until Reset Recovery */
-		usbd_ep_set_halt(node->data->uds_ctx, ep);
+		usbd_ep_set_halt(usbd_class_get_ctx(node), ep);
 	} else if (ep == msc_get_bulk_out(node) && !halted &&
 	    atomic_test_bit(&ctx->bits, MSC_BULK_OUT_WEDGED)) {
 		/* Endpoint shall remain halted until Reset Recovery */
-		usbd_ep_set_halt(node->data->uds_ctx, ep);
+		usbd_ep_set_halt(usbd_class_get_ctx(node), ep);
 	}
 }
 
@@ -689,7 +689,7 @@
 				   const struct usb_setup_packet *const setup,
 				   struct net_buf *const buf)
 {
-	struct msc_bot_ctx *ctx = node->data->priv;
+	struct msc_bot_ctx *ctx = usbd_class_get_private(node);
 	uint8_t max_lun;
 
 	if (setup->bRequest == GET_MAX_LUN &&
@@ -726,7 +726,7 @@
 /* Class associated configuration is selected */
 static void msc_bot_enable(struct usbd_class_node *const node)
 {
-	struct msc_bot_ctx *ctx = node->data->priv;
+	struct msc_bot_ctx *ctx = usbd_class_get_private(node);
 
 	LOG_INF("Enable");
 	atomic_set_bit(&ctx->bits, MSC_CLASS_ENABLED);
@@ -736,7 +736,7 @@
 /* Class associated configuration is disabled */
 static void msc_bot_disable(struct usbd_class_node *const node)
 {
-	struct msc_bot_ctx *ctx = node->data->priv;
+	struct msc_bot_ctx *ctx = usbd_class_get_private(node);
 
 	LOG_INF("Disable");
 	atomic_clear_bit(&ctx->bits, MSC_CLASS_ENABLED);
diff --git a/subsys/usb/device_next/usbd_endpoint.c b/subsys/usb/device_next/usbd_endpoint.c
index d57507d..82f3509 100644
--- a/subsys/usb/device_next/usbd_endpoint.c
+++ b/subsys/usb/device_next/usbd_endpoint.c
@@ -123,7 +123,7 @@
 struct net_buf *usbd_ep_buf_alloc(const struct usbd_class_node *const c_nd,
 				  const uint8_t ep, const size_t size)
 {
-	struct usbd_contex *uds_ctx = c_nd->data->uds_ctx;
+	struct usbd_contex *uds_ctx = usbd_class_get_ctx(c_nd);
 
 	return udc_ep_buf_alloc(uds_ctx->dev, ep, size);
 }
@@ -131,7 +131,7 @@
 int usbd_ep_enqueue(const struct usbd_class_node *const c_nd,
 		    struct net_buf *const buf)
 {
-	struct usbd_contex *uds_ctx = c_nd->data->uds_ctx;
+	struct usbd_contex *uds_ctx = usbd_class_get_ctx(c_nd);
 	struct udc_buf_info *bi = udc_get_buf_info(buf);
 
 	if (USB_EP_DIR_IS_IN(bi->ep)) {