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