xref: /aosp_15_r20/external/virtio-media/driver/session.h (revision 1b4853f54772485c5dd4001ae33a7a958bcc97a1)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0+
2 
3 /*
4  * Definitions of virtio-media session related structures.
5  *
6  * Copyright (c) 2023-2024 Google LLC.
7  */
8 
9 #ifndef __VIRTIO_MEDIA_SESSION_H
10 #define __VIRTIO_MEDIA_SESSION_H
11 
12 #include <linux/scatterlist.h>
13 #include <media/v4l2-fh.h>
14 
15 #include "protocol.h"
16 
17 #define VIRTIO_MEDIA_LAST_QUEUE (V4L2_BUF_TYPE_META_OUTPUT)
18 
19 /**
20  * Size of our virtio shadow and event buffers. 16K should definitely be enough
21  * to contain anything we need.
22  */
23 #define VIRTIO_SHADOW_BUF_SIZE 0x4000
24 
25 struct virtio_media_sg_entry {
26 	u64 start;
27 	u32 len;
28 	u32 __padding;
29 };
30 
31 /**
32  * struct virtio_media_buffer - Current state of a given buffer.
33  *
34  * @buffer: struct v4l2_buffer with current information about the buffer.
35  * @planes: backing planes array for @buffer.
36  * @list: link into the list of buffers pending dequeue.
37  */
38 struct virtio_media_buffer {
39 	struct v4l2_buffer buffer;
40 	struct v4l2_plane planes[VIDEO_MAX_PLANES];
41 	struct list_head list;
42 };
43 
44 /**
45  * struct virtio_media_queue_state - Represents the state of a V4L2 queue.
46  *
47  * @streaming: Whether the queue is currently streaming.
48  * @allocated_bufs: How many buffers are currently allocated.
49  * @is_capture_last: set to true when the last buffer has been received on a
50  * 	capture queue, so we can return -EPIPE on subsequent DQBUF requests.
51  * @buffers: Buffer state array of size @allocated_bufs.
52  * @queued_bufs: How many buffers are currently queued at the host.
53  * @pending_dqbufs: Buffers that are available for being dequeued.
54  */
55 struct virtio_media_queue_state {
56 	bool streaming;
57 	size_t allocated_bufs;
58 	bool is_capture_last;
59 
60 	struct virtio_media_buffer *buffers;
61 	size_t queued_bufs;
62 	struct list_head pending_dqbufs;
63 };
64 
65 /**
66  * struct virtio_media_session - A session on a virtio_media device, created whenever the device is opened.
67  *
68  * @fh: file handler for the session.
69  * @id: session ID used to communicate with the device.
70  * @nonblocking_dequeue: whether dequeue should block or not (nonblocking if file opened with O_NONBLOCK).
71  * @uses_mplane: whether the queues for this session use the MPLANE API or not.
72  * @cmd: union of session-related commands. Each session can have one command currently running.
73  * @resp: union of session-related responses.
74  * @shadow_buf: shadow buffer where commandq data can be staged before being sent to the device.
75  * @command_sg: SG table gathering descriptors for a given command and its response.
76  * @queues: state of all the queues for this session.
77  * @dqbufs_lock: protects pending_dqbufs of virtio_media_queue_state.
78  * @dqbufs_wait: waitqueue for dequeued buffers, if VIDIOC_DQBUF needs to block or when polling.
79  * @list: link into the list of sessions for the device.
80  */
81 struct virtio_media_session {
82 	struct v4l2_fh fh;
83 	u32 id;
84 	bool nonblocking_dequeue;
85 	bool uses_mplane;
86 
87 	union {
88 		struct virtio_media_cmd_close close;
89 		struct virtio_media_cmd_ioctl ioctl;
90 		struct virtio_media_cmd_mmap mmap;
91 	} cmd;
92 
93 	union {
94 		struct virtio_media_resp_ioctl ioctl;
95 		struct virtio_media_resp_mmap mmap;
96 	} resp;
97 
98 	void *shadow_buf;
99 
100 	struct sg_table command_sgs;
101 
102 	struct virtio_media_queue_state queues[VIRTIO_MEDIA_LAST_QUEUE + 1];
103 	struct mutex dqbufs_lock;
104 	wait_queue_head_t dqbufs_wait;
105 
106 	struct list_head list;
107 };
108 
fh_to_session(struct v4l2_fh * fh)109 static inline struct virtio_media_session *fh_to_session(struct v4l2_fh *fh)
110 {
111 	return container_of(fh, struct virtio_media_session, fh);
112 }
113 
114 #endif // __VIRTIO_MEDIA_SESSION_H
115