xref: /freebsd/sys/dev/video/video_internal.h (revision 9c9428825f4c55e3cb37412c661bb9d385db4c68)
1 /*
2  * Copyright (c) 2026 Abdelkader Boudih <freebsd@seuros.com>
3  *
4  * SPDX-License-Identifier: BSD-2-Clause
5  */
6 
7 #ifndef _DEV_VIDEO_VIDEO_INTERNAL_H_
8 #define _DEV_VIDEO_VIDEO_INTERNAL_H_
9 
10 #include <sys/param.h>
11 #include <sys/systm.h>
12 #include <sys/bus.h>
13 #include <sys/conf.h>
14 #include <sys/kernel.h>
15 #include <sys/lock.h>
16 #include <sys/malloc.h>
17 #include <sys/mutex.h>
18 #include <sys/sx.h>
19 #include <sys/queue.h>
20 #include <sys/selinfo.h>
21 
22 #include <vm/vm.h>
23 #include <vm/vm_object.h>
24 #include <vm/vm_page.h>
25 #include <vm/vm_pager.h>
26 
27 #include <dev/video/video.h>
28 
29 MALLOC_DECLARE(M_VIDEO);
30 
31 #define	VIDEO_MAX_BUFFERS	8
32 #define	VIDEO_READ_BUFFERS	3
33 
34 enum video_buf_state {
35 	VB_IDLE,
36 	VB_QUEUED,
37 	VB_ACTIVE,
38 	VB_DONE,
39 	VB_ERROR,
40 };
41 
42 struct video_buf {
43 	STAILQ_ENTRY(video_buf) entry;
44 	struct video_buf_pool	*pool;
45 	struct video_device	*vd;
46 
47 	uint32_t		index;
48 	enum video_buf_state	state;
49 
50 	void			*buf;
51 	size_t			length;
52 
53 	size_t			bytesused;
54 	uint32_t		sequence;
55 	struct timeval		timestamp;
56 	uint32_t		flags;
57 };
58 
59 STAILQ_HEAD(video_buf_list, video_buf);
60 
61 /*
62  * All buffers must stay carved out of one OBJT_PHYS vm_object, mapped into
63  * the kernel map and handed to userspace mmap as that same object.  Giving
64  * each buffer its own object breaks the mmap path.
65  */
66 struct video_buf_pool {
67 	vm_object_t		obj;
68 	vm_offset_t		kva;
69 
70 	u_int			nbufs;
71 	size_t			buf_size;
72 	size_t			map_size;
73 	struct video_buf	bufs[VIDEO_MAX_BUFFERS];
74 };
75 
76 struct video_file {
77 	struct video_device	*vd;
78 	bool			is_owner;
79 	bool			reading;	/* read(2) in progress */
80 	size_t			read_offset;
81 	struct video_buf	*read_buf;
82 };
83 
84 enum video_mode {
85 	VMODE_NONE,
86 	VMODE_READ,
87 	VMODE_MMAP,
88 };
89 
90 /* Lock order: cfg_sx -> mtx. */
91 struct video_device {
92 	device_t		dev;
93 	struct cdev		*cdev;
94 	int			unit;
95 
96 	struct sx		cfg_sx;
97 	struct mtx		mtx;
98 
99 	bool			dying;
100 
101 	struct video_file	*owner;
102 	enum video_mode		mode;
103 	bool			streaming;
104 	bool			stopping;
105 
106 	struct video_format	format;
107 
108 	struct video_buf_pool	*pool;
109 	struct video_buf_list	queued;
110 	struct video_buf_list	done;
111 	u_int			readers;	/* copies in flight from pool */
112 
113 	struct selinfo		sel;
114 };
115 
116 
117 #endif /* _DEV_VIDEO_VIDEO_INTERNAL_H_ */
118