xref: /aosp_15_r20/external/virtio-media/driver/virtio_media.h (revision 1b4853f54772485c5dd4001ae33a7a958bcc97a1)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0+
2 
3 /*
4  * Virtio-media structures & functions declarations.
5  *
6  * Copyright (c) 2023-2024 Google LLC.
7  */
8 
9 #ifndef __VIRTIO_MEDIA_H
10 #define __VIRTIO_MEDIA_H
11 
12 #include <linux/virtio_config.h>
13 #include <media/v4l2-device.h>
14 
15 #include "protocol.h"
16 
17 #define DESC_CHAIN_MAX_LEN SG_MAX_SINGLE_ALLOC
18 
19 #define VIRTIO_MEDIA_DEFAULT_DRIVER_NAME "virtio_media"
20 
21 extern char *driver_name;
22 
23 /**
24  * Virtio-media device.
25  */
26 struct virtio_media {
27 	struct v4l2_device v4l2_dev;
28 	struct video_device video_dev;
29 
30 	struct virtio_device *virtio_dev;
31 	struct virtqueue *commandq;
32 	struct virtqueue *eventq;
33 	struct work_struct eventq_work;
34 
35 	/* Region into which MMAP buffers are mapped by the host. */
36 	struct virtio_shm_region mmap_region;
37 
38 	/* Buffer for event descriptors. */
39 	void *event_buffer;
40 
41 	/* List of active decoding sessions */
42 	struct list_head sessions;
43 	/* Protects `sessions` */
44 	struct mutex sessions_lock;
45 
46 	/* Make sure we don't have two threads processing events at the same time */
47 	struct mutex events_process_lock;
48 
49 	union {
50 		struct virtio_media_cmd_open open;
51 		struct virtio_media_cmd_munmap munmap;
52 	} cmd;
53 
54 	union {
55 		struct virtio_media_resp_open open;
56 		struct virtio_media_resp_munmap munmap;
57 	} resp;
58 
59 	/* Protects `cmd_buf` and `resp_buf` */
60 	struct mutex bufs_lock;
61 
62 	/* Used to serialize all virtio commands */
63 	struct mutex vlock;
64 
65 	/* Waitqueue for host responses on the command queue */
66 	wait_queue_head_t wq;
67 };
68 
69 static inline struct virtio_media *
to_virtio_media(struct video_device * video_dev)70 to_virtio_media(struct video_device *video_dev)
71 {
72 	return container_of(video_dev, struct virtio_media, video_dev);
73 }
74 
75 /* virtio_media_driver.c */
76 
77 int virtio_media_send_command(struct virtio_media *vv, struct scatterlist **sgs,
78 			      const size_t out_sgs, const size_t in_sgs,
79 			      size_t minimum_resp_len, size_t *resp_len);
80 void virtio_media_process_events(struct virtio_media *vv);
81 
82 /* virtio_media_ioctls.c */
83 
84 long virtio_media_device_ioctl(struct file *file, unsigned int cmd,
85 			       unsigned long arg);
86 extern const struct v4l2_ioctl_ops virtio_media_ioctl_ops;
87 
88 #endif // __VIRTIO_MEDIA_H
89