1 /*
2 * Copyright (c) 2022 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <errno.h>
8 #include <zephyr/device.h>
9 #include <zephyr/sys/byteorder.h>
10 #include <zephyr/devicetree.h>
11 #include <zephyr/init.h>
12 #include <zephyr/sys/iterable_sections.h>
13
14 #include "usbh_internal.h"
15 #include "usbh_device.h"
16
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(uhs, CONFIG_USBH_LOG_LEVEL);
19
20 static K_KERNEL_STACK_DEFINE(usbh_stack, CONFIG_USBH_STACK_SIZE);
21 static struct k_thread usbh_thread_data;
22
23 K_MSGQ_DEFINE(usbh_msgq, sizeof(struct uhc_event),
24 CONFIG_USBH_MAX_UHC_MSG, sizeof(uint32_t));
25
usbh_event_carrier(const struct device * dev,const struct uhc_event * const event)26 static int usbh_event_carrier(const struct device *dev,
27 const struct uhc_event *const event)
28 {
29 return k_msgq_put(&usbh_msgq, event, K_NO_WAIT);
30 }
31
discard_ep_request(struct usbh_contex * const ctx,struct uhc_transfer * const xfer)32 static int discard_ep_request(struct usbh_contex *const ctx,
33 struct uhc_transfer *const xfer)
34 {
35 const struct device *dev = ctx->dev;
36
37 if (xfer->buf) {
38 LOG_HEXDUMP_INF(xfer->buf->data, xfer->buf->len, "buf");
39 uhc_xfer_buf_free(dev, xfer->buf);
40 }
41
42 return uhc_xfer_free(dev, xfer);
43 }
44
usbh_event_handler(struct usbh_contex * const ctx,struct uhc_event * const event)45 static ALWAYS_INLINE int usbh_event_handler(struct usbh_contex *const ctx,
46 struct uhc_event *const event)
47 {
48 int ret = 0;
49
50 if (event->type == UHC_EVT_EP_REQUEST) {
51 struct usb_device *const udev = event->xfer->udev;
52 usbh_udev_cb_t cb = event->xfer->cb;
53
54 if (event->xfer->cb) {
55 ret = cb(udev, event->xfer);
56 } else {
57 ret = discard_ep_request(ctx, event->xfer);
58 }
59 return ret;
60 }
61
62 switch (event->type) {
63 case UHC_EVT_DEV_CONNECTED_LS:
64 case UHC_EVT_DEV_CONNECTED_FS:
65 case UHC_EVT_DEV_CONNECTED_HS:
66 LOG_DBG("Device connected event");
67 break;
68 case UHC_EVT_DEV_REMOVED:
69 LOG_DBG("Device removed event");
70 break;
71 case UHC_EVT_RESETED:
72 LOG_DBG("Bus reset");
73 break;
74 case UHC_EVT_SUSPENDED:
75 LOG_DBG("Bus suspended");
76 break;
77 case UHC_EVT_RESUMED:
78 LOG_DBG("Bus resumed");
79 break;
80 case UHC_EVT_RWUP:
81 LOG_DBG("RWUP event");
82 break;
83 case UHC_EVT_ERROR:
84 LOG_DBG("Error event %d", event->status);
85 break;
86 default:
87 break;
88 };
89
90 return ret;
91 }
92
usbh_thread(void * p1,void * p2,void * p3)93 static void usbh_thread(void *p1, void *p2, void *p3)
94 {
95 ARG_UNUSED(p1);
96 ARG_UNUSED(p2);
97 ARG_UNUSED(p3);
98
99 struct uhc_event event;
100
101 while (true) {
102 k_msgq_get(&usbh_msgq, &event, K_FOREVER);
103
104 STRUCT_SECTION_FOREACH(usbh_contex, uhs_ctx) {
105 if (uhs_ctx->dev == event.dev) {
106 usbh_event_handler(uhs_ctx, &event);
107 }
108 }
109 }
110 }
111
usbh_init_device_intl(struct usbh_contex * const uhs_ctx)112 int usbh_init_device_intl(struct usbh_contex *const uhs_ctx)
113 {
114 int ret;
115
116 ret = uhc_init(uhs_ctx->dev, usbh_event_carrier);
117 if (ret != 0) {
118 LOG_ERR("Failed to init device driver");
119 return ret;
120 }
121
122 STRUCT_SECTION_FOREACH(usbh_class_data, cdata) {
123 /*
124 * For now, we have not implemented any class drivers,
125 * so just keep it as placeholder.
126 */
127 break;
128 }
129
130 return 0;
131 }
132
uhs_pre_init(void)133 static int uhs_pre_init(void)
134 {
135 k_thread_create(&usbh_thread_data, usbh_stack,
136 K_KERNEL_STACK_SIZEOF(usbh_stack),
137 usbh_thread,
138 NULL, NULL, NULL,
139 K_PRIO_COOP(9), 0, K_NO_WAIT);
140
141 k_thread_name_set(&usbh_thread_data, "usbh");
142
143 return 0;
144 }
145
146 SYS_INIT(uhs_pre_init, POST_KERNEL, CONFIG_USBH_INIT_PRIO);
147