summaryrefslogtreecommitdiff
path: root/bsp/qemu-virt64-aarch64/drivers/drv_camera.c
blob: 37fd05d804e3148c6865e020ba75d77d12df9a0f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright (c) 2006-2026, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2026-04-07     GuEe-GUI     the first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#define DBG_TAG "vfio.camera"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#include <drv_camera.h>

#define VFIO_CAMERA_WIDTH_BAR       0
#define VFIO_CAMERA_HEIGHT_BAR      1
#define VFIO_CAMERA_FRAMEBUFFER_BAR 2
#define VFIO_CAMERA_STATUS_BAR      3

/* Status register */
#define BAR3_FRAME_COUNT_OFFSET     0x00
#define BAR3_FRAME_READY_OFFSET     0x04

struct vfio_camera
{
    struct rt_device parent;
    void *framebuffer;
    void *status_regs;      /* BAR3: status registers */
    int width;
    int height;
    rt_uint32_t last_frame_count;
    struct rt_semaphore frame_sem;  /* Frame ready notification */
};

static rt_uint32_t vfio_camera_get_frame_count(struct vfio_camera *vc)
{
    return HWREG32(vc->status_regs + BAR3_FRAME_COUNT_OFFSET);
}

static rt_bool_t vfio_camera_wait_frame(struct vfio_camera *vc, rt_int32_t timeout_ms)
{
    rt_uint32_t current_count;
    rt_tick_t start_tick, timeout_tick;

    /* Fast path: check frame count first */
    current_count = vfio_camera_get_frame_count(vc);
    if (current_count != vc->last_frame_count)
    {
        vc->last_frame_count = current_count;
        return RT_TRUE;
    }

    start_tick = rt_tick_get();
    timeout_tick = rt_tick_from_millisecond(timeout_ms);

    /* Poll with small sleep - allow frame dropping */
    do {
        current_count = vfio_camera_get_frame_count(vc);

        if (current_count != vc->last_frame_count)
        {
            vc->last_frame_count = current_count;
            return RT_TRUE;
        }

        /* Short sleep to reduce CPU usage */
        rt_thread_mdelay(1);

        /* Check overall timeout */
        if ((rt_tick_get() - start_tick) >= timeout_tick)
        {
            /* Timeout */
            return RT_FALSE;
        }
    } while (RT_TRUE);
}

static rt_ssize_t vfio_camera_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
    struct vfio_camera *vc = rt_container_of(dev, struct vfio_camera, parent);

    /* Wait for new frame (up to 30ms - about 1 frame at 30fps) */
    if (!vfio_camera_wait_frame(vc, 30))
    {
        return 0;
    }

    /* If buffer is NULL, just return success (frame is ready) */
    if (buffer == RT_NULL)
    {
        return 1;  /* Frame ready */
    }

    /* Direct memory access - no memcpy overhead */
    rt_memcpy(buffer, vc->framebuffer + pos, size);
    return size;
}

static rt_err_t vfio_camera_control(rt_device_t dev, int cmd, void *args)
{
    struct vfio_camera *vc = rt_container_of(dev, struct vfio_camera, parent);

    switch (cmd)
    {
    case RT_DEVICE_CTRL_CAMERA_GET_WIDTH:
        if (args)
        {
            *(int *)args = vc->width;
        }
        break;

    case RT_DEVICE_CTRL_CAMERA_GET_HEIGHT:
        if (args)
        {
            *(int *)args = vc->height;
        }
        break;

    case RT_DEVICE_CTRL_CAMERA_GET_FRAMEBUFFER:
        if (args)
        {
            *(void **)args = vc->framebuffer;
        }
        break;

    default:
        return -RT_EINVAL;
    }

    return RT_EOK;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops vfio_camera_ops =
{
    .read = vfio_camera_read,
    .control = vfio_camera_control,
};
#endif

static void vfio_camera_isr(int irqno, void *args)
{
    struct vfio_camera *vc = args;

    /* Frame ready interrupt from backend */
    rt_sem_release(&vc->frame_sem);
}

static rt_err_t vfio_camera_probe(struct rt_pci_device *pdev)
{
    rt_err_t err;
    void *fmt_regs;
    const char *dev_name;
    struct vfio_camera *vc = rt_calloc(1, sizeof(*vc));

    if (!vc)
    {
        LOG_E("Failed to allocate vfio_camera");
        return -RT_ENOMEM;
    }

    /* Initialize semaphore for frame notification */
    rt_sem_init(&vc->frame_sem, "cam_frm", 0, RT_IPC_FLAG_PRIO);

    fmt_regs = rt_pci_iomap(pdev, VFIO_CAMERA_WIDTH_BAR);
    if (!fmt_regs)
    {
        LOG_E("Failed to map BAR0 (width)");
        goto _fail;
    }
    vc->width = HWREG32(fmt_regs);
    rt_iounmap(fmt_regs);

    fmt_regs = rt_pci_iomap(pdev, VFIO_CAMERA_HEIGHT_BAR);
    if (!fmt_regs)
    {
        LOG_E("Failed to map BAR1 (height)");
        goto _fail;
    }
    vc->height = HWREG32(fmt_regs);
    rt_iounmap(fmt_regs);

    vc->framebuffer = rt_pci_iomap(pdev, VFIO_CAMERA_FRAMEBUFFER_BAR);
    if (!vc->framebuffer)
    {
        LOG_E("Failed to map BAR2 (framebuffer)");
        goto _fail;
    }

    vc->status_regs = rt_pci_iomap(pdev, VFIO_CAMERA_STATUS_BAR);
    if (!vc->status_regs)
    {
        LOG_E("Failed to map BAR3 (status)");
        goto _fail;
    }

    /* Initialize frame count */
    vc->last_frame_count = vfio_camera_get_frame_count(vc);

    vc->parent.type = RT_Device_Class_Miscellaneous;
#ifdef RT_USING_DEVICE_OPS
    vc->parent.ops = &vfio_camera_ops;
#else
    vc->parent.read = vfio_camera_read;
    vc->parent.control = vfio_camera_control;
#endif

    rt_dm_dev_set_name_auto(&vc->parent, "camera");
    dev_name = rt_dm_dev_get_name(&vc->parent);

    if ((err = rt_device_register(&vc->parent, dev_name, RT_DEVICE_FLAG_RDONLY)))
    {
        LOG_E("Failed to register device: %d", err);
        goto _fail;
    }

    rt_hw_interrupt_install(pdev->irq, vfio_camera_isr, vc, dev_name);
    rt_hw_interrupt_umask(pdev->irq);
    rt_pci_irq_unmask(pdev);

    pdev->parent.user_data = vc;

    LOG_I("%s %ux%u %s", dev_name, vc->width, vc->height, "RGB24");

    return RT_EOK;

_fail:
    if (vc->status_regs)
    {
        rt_iounmap(vc->status_regs);
    }
    if (vc->framebuffer)
    {
        rt_iounmap(vc->framebuffer);
    }
    rt_sem_detach(&vc->frame_sem);
    rt_free(vc);

    return err;
}

static rt_err_t vfio_camera_remove(struct rt_pci_device *pdev)
{
    struct vfio_camera *vc = pdev->parent.user_data;

    /* INTx is shared, don't mask all */
    rt_hw_interrupt_umask(pdev->irq);
    rt_pci_irq_mask(pdev);
    rt_pci_clear_master(pdev);

    rt_device_unregister(&vc->parent);

    rt_iounmap(vc->status_regs);
    rt_iounmap(vc->framebuffer);
    rt_sem_detach(&vc->frame_sem);
    rt_free(vc);

    return RT_EOK;
}

static const struct rt_pci_device_id vfio_camera_pci_ids[] =
{
    { RT_PCI_DEVICE_ID(PCI_VENDOR_ID_QEMU, 0x5678), },
    { /* sentinel */ }
};

static struct rt_pci_driver vfio_camera_driver =
{
    .name = "vfio-camera",

    .ids = vfio_camera_pci_ids,
    .probe = vfio_camera_probe,
    .remove = vfio_camera_remove,
};
RT_PCI_DRIVER_EXPORT(vfio_camera_driver);