|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 tinyVision.ai Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT zephyr_uvc_host |
| 8 | + |
| 9 | +#include <stdlib.h> |
| 10 | + |
| 11 | +#include <zephyr/init.h> |
| 12 | +#include <zephyr/devicetree.h> |
| 13 | +#include <zephyr/kernel.h> |
| 14 | +#include <zephyr/sys/byteorder.h> |
| 15 | +#include <zephyr/sys/atomic.h> |
| 16 | +#include <zephyr/usb/usbh.h> |
| 17 | +#include <zephyr/usb/usb_ch9.h> |
| 18 | +#include <zephyr/drivers/usb/udc.h> |
| 19 | +#include <zephyr/drivers/video.h> |
| 20 | +#include <zephyr/drivers/video-controls.h> |
| 21 | +#include <zephyr/logging/log.h> |
| 22 | + |
| 23 | +#include <zephyr/devicetree.h> |
| 24 | +#include <zephyr/sys/util.h> |
| 25 | +#include <zephyr/usb/usb_ch9.h> |
| 26 | +#include <zephyr/usb/class/usb_uvc.h> |
| 27 | + |
| 28 | +#include "usbh_device.h" |
| 29 | +#include "usbh_ch9.h" |
| 30 | + |
| 31 | +#include "../../../drivers/video/video_ctrls.h" |
| 32 | +#include "../../../drivers/video/video_device.h" |
| 33 | + |
| 34 | +LOG_MODULE_REGISTER(usbh_uvc, CONFIG_USBH_VIDEO_LOG_LEVEL); |
| 35 | + |
| 36 | +struct usbh_uvc_config { |
| 37 | + struct usbh_contex *uhs_ctx; |
| 38 | +}; |
| 39 | + |
| 40 | +static int usbh_uvc_request(struct usbh_contex *const uhs_ctx, struct uhc_transfer *const xfer, |
| 41 | + int err) |
| 42 | +{ |
| 43 | + LOG_INF("%p %p %d", uhs_ctx, xfer, err); |
| 44 | + |
| 45 | + return 0; |
| 46 | +} |
| 47 | + |
| 48 | +static int usbh_uvc_connected(struct usbh_contex *const uhs_ctx) |
| 49 | +{ |
| 50 | + struct usb_device *const udev = uhs_ctx->root; |
| 51 | + const size_t len = 512; |
| 52 | + struct net_buf *buf; |
| 53 | + int ret; |
| 54 | + |
| 55 | + LOG_INF("%p", uhs_ctx); |
| 56 | + |
| 57 | + buf = usbh_xfer_buf_alloc(udev, len); |
| 58 | + if (buf == NULL) { |
| 59 | + LOG_ERR("Failed to allocate a host transfer buffer"); |
| 60 | + return -ENOMEM; |
| 61 | + } |
| 62 | + |
| 63 | + ret = usbh_req_desc(udev, USB_DESC_DEVICE, 0, 0, len, buf); |
| 64 | + if (ret != 0) { |
| 65 | + LOG_ERR("Failed to request descriptor"); |
| 66 | + return ret; |
| 67 | + } |
| 68 | + |
| 69 | + LOG_HEXDUMP_INF(buf->data, buf->len, "buf"); |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
| 73 | + |
| 74 | +static int usbh_uvc_removed(struct usbh_contex *const uhs_ctx) |
| 75 | +{ |
| 76 | + LOG_INF("%p", uhs_ctx); |
| 77 | + |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +static int usbh_uvc_rwup(struct usbh_contex *const uhs_ctx) |
| 82 | +{ |
| 83 | + LOG_INF("%p", uhs_ctx); |
| 84 | + |
| 85 | + return 0; |
| 86 | +} |
| 87 | + |
| 88 | +static int usbh_uvc_suspended(struct usbh_contex *const uhs_ctx) |
| 89 | +{ |
| 90 | + LOG_INF("%p", uhs_ctx); |
| 91 | + |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +static int usbh_uvc_resumed(struct usbh_contex *const uhs_ctx) |
| 96 | +{ |
| 97 | + LOG_INF("%p", uhs_ctx); |
| 98 | + |
| 99 | + return 0; |
| 100 | +} |
| 101 | + |
| 102 | +static int usbh_uvc_preinit(const struct device *dev) |
| 103 | +{ |
| 104 | + LOG_INF("%s", dev->name); |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
| 108 | + |
| 109 | +static const struct usbh_class_data usbh_uvc_class = { |
| 110 | + .code = {0, 0, 0}, |
| 111 | + .request = usbh_uvc_request, |
| 112 | + .connected = usbh_uvc_connected, |
| 113 | + .removed = usbh_uvc_removed, |
| 114 | + .rwup = usbh_uvc_rwup, |
| 115 | + .suspended = usbh_uvc_suspended, |
| 116 | + .resumed = usbh_uvc_resumed, |
| 117 | +}; |
| 118 | + |
| 119 | +/* TODO this hardcodes a single class instance as only class of the system */ |
| 120 | +const struct usbh_class_data *usbh_class_data = &usbh_uvc_class; |
| 121 | + |
| 122 | +int usbh_uvc_get_caps(const struct device *const dev, struct video_caps *const caps) |
| 123 | +{ |
| 124 | + return 0; |
| 125 | +} |
| 126 | + |
| 127 | +int usbh_uvc_get_format(const struct device *const dev, struct video_format *const fmt) |
| 128 | +{ |
| 129 | + return 0; |
| 130 | +} |
| 131 | + |
| 132 | +int usbh_uvc_set_stream(const struct device *const dev, bool enable, enum video_buf_type type) |
| 133 | +{ |
| 134 | + return 0; |
| 135 | +} |
| 136 | + |
| 137 | +int usbh_uvc_enqueue(const struct device *const dev, struct video_buffer *const vbuf) |
| 138 | +{ |
| 139 | + return 0; |
| 140 | +} |
| 141 | + |
| 142 | +int usbh_uvc_dequeue(const struct device *const dev, struct video_buffer **const vbuf, |
| 143 | + k_timeout_t timeout) |
| 144 | +{ |
| 145 | + return 0; |
| 146 | +} |
| 147 | + |
| 148 | +static DEVICE_API(video, uvc_video_api) = { |
| 149 | + .get_caps = usbh_uvc_get_caps, |
| 150 | + .get_format = usbh_uvc_get_format, |
| 151 | + .set_stream = usbh_uvc_set_stream, |
| 152 | + .enqueue = usbh_uvc_enqueue, |
| 153 | + .dequeue = usbh_uvc_dequeue, |
| 154 | +}; |
| 155 | + |
| 156 | +#define USBH_VIDEO_DT_DEVICE_DEFINE(n) \ |
| 157 | + \ |
| 158 | + DEVICE_DT_INST_DEFINE(n, usbh_uvc_preinit, NULL, \ |
| 159 | + NULL, &usbh_uvc_class, \ |
| 160 | + POST_KERNEL, CONFIG_VIDEO_INIT_PRIORITY, \ |
| 161 | + &uvc_video_api); \ |
| 162 | + \ |
| 163 | + VIDEO_DEVICE_DEFINE(uvc_host_##n, DEVICE_DT_INST_GET(n), NULL); |
| 164 | + |
| 165 | +DT_INST_FOREACH_STATUS_OKAY(USBH_VIDEO_DT_DEVICE_DEFINE) |
0 commit comments