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 protocol 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_PROTOCOL_H 10*1b4853f5SAndroid Build Coastguard Worker #define __VIRTIO_MEDIA_PROTOCOL_H 11*1b4853f5SAndroid Build Coastguard Worker 12*1b4853f5SAndroid Build Coastguard Worker #include <linux/videodev2.h> 13*1b4853f5SAndroid Build Coastguard Worker 14*1b4853f5SAndroid Build Coastguard Worker /* 15*1b4853f5SAndroid Build Coastguard Worker * Virtio protocol definition. 16*1b4853f5SAndroid Build Coastguard Worker */ 17*1b4853f5SAndroid Build Coastguard Worker 18*1b4853f5SAndroid Build Coastguard Worker /** 19*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_cmd_header - Header for all virtio commands from the driver to the device on the commandq. 20*1b4853f5SAndroid Build Coastguard Worker * 21*1b4853f5SAndroid Build Coastguard Worker * @cmd: one of VIRTIO_MEDIA_CMD_*. 22*1b4853f5SAndroid Build Coastguard Worker * @__padding: must be set to zero by the guest. 23*1b4853f5SAndroid Build Coastguard Worker */ 24*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_header { 25*1b4853f5SAndroid Build Coastguard Worker u32 cmd; 26*1b4853f5SAndroid Build Coastguard Worker u32 __padding; 27*1b4853f5SAndroid Build Coastguard Worker }; 28*1b4853f5SAndroid Build Coastguard Worker 29*1b4853f5SAndroid Build Coastguard Worker /** 30*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_resp_header - Header for all virtio responses from the device to the driver on the commandq. 31*1b4853f5SAndroid Build Coastguard Worker * 32*1b4853f5SAndroid Build Coastguard Worker * @status: 0 if the command was successful, or one of the standard Linux error 33*1b4853f5SAndroid Build Coastguard Worker * codes. 34*1b4853f5SAndroid Build Coastguard Worker * @__padding: must be set to zero by the device. 35*1b4853f5SAndroid Build Coastguard Worker */ 36*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_header { 37*1b4853f5SAndroid Build Coastguard Worker u32 status; 38*1b4853f5SAndroid Build Coastguard Worker u32 __padding; 39*1b4853f5SAndroid Build Coastguard Worker }; 40*1b4853f5SAndroid Build Coastguard Worker 41*1b4853f5SAndroid Build Coastguard Worker /** 42*1b4853f5SAndroid Build Coastguard Worker * VIRTIO_MEDIA_CMD_OPEN - Command for creating a new session. 43*1b4853f5SAndroid Build Coastguard Worker * 44*1b4853f5SAndroid Build Coastguard Worker * This is the equivalent of calling `open` on a V4L2 device node. Upon 45*1b4853f5SAndroid Build Coastguard Worker * success, a session id is returned which can be used to perform other 46*1b4853f5SAndroid Build Coastguard Worker * commands on the session, notably ioctls. 47*1b4853f5SAndroid Build Coastguard Worker */ 48*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_CMD_OPEN 1 49*1b4853f5SAndroid Build Coastguard Worker 50*1b4853f5SAndroid Build Coastguard Worker /** 51*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_cmd_open - Driver command for VIRTIO_MEDIA_CMD_OPEN. 52*1b4853f5SAndroid Build Coastguard Worker * 53*1b4853f5SAndroid Build Coastguard Worker * @hdr: header which cmd member is set to VIRTIO_MEDIA_CMD_OPEN. 54*1b4853f5SAndroid Build Coastguard Worker */ 55*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_open { 56*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_header hdr; 57*1b4853f5SAndroid Build Coastguard Worker }; 58*1b4853f5SAndroid Build Coastguard Worker 59*1b4853f5SAndroid Build Coastguard Worker /** 60*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_resp_open - Device response for VIRTIO_MEDIA_CMD_OPEN. 61*1b4853f5SAndroid Build Coastguard Worker * 62*1b4853f5SAndroid Build Coastguard Worker * @hdr: header containing the status of the command. 63*1b4853f5SAndroid Build Coastguard Worker * @session_id: if hdr.status == 0, contains the id of the newly created session. 64*1b4853f5SAndroid Build Coastguard Worker * @__padding: must be set to zero by the device. 65*1b4853f5SAndroid Build Coastguard Worker */ 66*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_open { 67*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_header hdr; 68*1b4853f5SAndroid Build Coastguard Worker u32 session_id; 69*1b4853f5SAndroid Build Coastguard Worker u32 __padding; 70*1b4853f5SAndroid Build Coastguard Worker }; 71*1b4853f5SAndroid Build Coastguard Worker 72*1b4853f5SAndroid Build Coastguard Worker /** 73*1b4853f5SAndroid Build Coastguard Worker * VIRTIO_MEDIA_CMD_OPEN - Command for closing an active session. 74*1b4853f5SAndroid Build Coastguard Worker * 75*1b4853f5SAndroid Build Coastguard Worker * This is the equivalent of calling `close` on a previously opened V4L2 FD. 76*1b4853f5SAndroid Build Coastguard Worker * All resources associated with this session will be freed and the session ID shall not be used again after queueing this command. 77*1b4853f5SAndroid Build Coastguard Worker * 78*1b4853f5SAndroid Build Coastguard Worker * This command does not require a response from the device. 79*1b4853f5SAndroid Build Coastguard Worker */ 80*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_CMD_CLOSE 2 81*1b4853f5SAndroid Build Coastguard Worker 82*1b4853f5SAndroid Build Coastguard Worker /** 83*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_cmd_close - Driver command for VIRTIO_MEDIA_CMD_CLOSE. 84*1b4853f5SAndroid Build Coastguard Worker * 85*1b4853f5SAndroid Build Coastguard Worker * @hdr: header which cmd member is set to VIRTIO_MEDIA_CMD_CLOSE. 86*1b4853f5SAndroid Build Coastguard Worker * @session_id: id of the session to close. 87*1b4853f5SAndroid Build Coastguard Worker * @__padding: must be set to zero by the driver. 88*1b4853f5SAndroid Build Coastguard Worker */ 89*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_close { 90*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_header hdr; 91*1b4853f5SAndroid Build Coastguard Worker u32 session_id; 92*1b4853f5SAndroid Build Coastguard Worker u32 __padding; 93*1b4853f5SAndroid Build Coastguard Worker }; 94*1b4853f5SAndroid Build Coastguard Worker 95*1b4853f5SAndroid Build Coastguard Worker /** 96*1b4853f5SAndroid Build Coastguard Worker * VIRTIO_MEDIA_CMD_IOCTL - Command for executing an ioctl on an open session. 97*1b4853f5SAndroid Build Coastguard Worker * 98*1b4853f5SAndroid Build Coastguard Worker * This command asks the device to run one of the `VIDIOC_*` ioctls on the active session. 99*1b4853f5SAndroid Build Coastguard Worker * 100*1b4853f5SAndroid Build Coastguard Worker * @hdr: header which cmd member is set to VIRTIO_MEDIA_CMD_IOCTL. 101*1b4853f5SAndroid Build Coastguard Worker * @session_id: id of the session to run this ioctl on. 102*1b4853f5SAndroid Build Coastguard Worker * @code: code of the ioctl. 103*1b4853f5SAndroid Build Coastguard Worker * 104*1b4853f5SAndroid Build Coastguard Worker * The code of the ioctl is extracted from the VIDIOC_* definitions in 105*1b4853f5SAndroid Build Coastguard Worker * `videodev2.h`, and consists of the second argument of the `_IO*` macro. 106*1b4853f5SAndroid Build Coastguard Worker * 107*1b4853f5SAndroid Build Coastguard Worker * Each ioctl has a payload, which is defined by the third argument of the 108*1b4853f5SAndroid Build Coastguard Worker * `_IO*` macro defining it. It can be writable by the driver (`_IOW`), the 109*1b4853f5SAndroid Build Coastguard Worker * device (`_IOR`), or both (`_IOWR`). 110*1b4853f5SAndroid Build Coastguard Worker * 111*1b4853f5SAndroid Build Coastguard Worker * If an ioctl is writable by the driver, it must be followed by a 112*1b4853f5SAndroid Build Coastguard Worker * driver-writable descriptor containing the payload. 113*1b4853f5SAndroid Build Coastguard Worker * 114*1b4853f5SAndroid Build Coastguard Worker * If an ioctl is writable by the device, it must be followed by a 115*1b4853f5SAndroid Build Coastguard Worker * device-writable descriptor of the size of the payload that the device will 116*1b4853f5SAndroid Build Coastguard Worker * write into. 117*1b4853f5SAndroid Build Coastguard Worker * 118*1b4853f5SAndroid Build Coastguard Worker */ 119*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_CMD_IOCTL 3 120*1b4853f5SAndroid Build Coastguard Worker 121*1b4853f5SAndroid Build Coastguard Worker /** 122*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_cmd_ioctl - Driver command for VIRTIO_MEDIA_CMD_IOCTL. 123*1b4853f5SAndroid Build Coastguard Worker * 124*1b4853f5SAndroid Build Coastguard Worker * @hdr: header which cmd member is set to VIRTIO_MEDIA_CMD_IOCTL. 125*1b4853f5SAndroid Build Coastguard Worker * @session_id: id of the session to run the ioctl on. 126*1b4853f5SAndroid Build Coastguard Worker * @code: code of the ioctl to run. 127*1b4853f5SAndroid Build Coastguard Worker */ 128*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_ioctl { 129*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_header hdr; 130*1b4853f5SAndroid Build Coastguard Worker u32 session_id; 131*1b4853f5SAndroid Build Coastguard Worker u32 code; 132*1b4853f5SAndroid Build Coastguard Worker }; 133*1b4853f5SAndroid Build Coastguard Worker 134*1b4853f5SAndroid Build Coastguard Worker /** 135*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_resp_ioctl - Device response for VIRTIO_MEDIA_CMD_IOCTL. 136*1b4853f5SAndroid Build Coastguard Worker * 137*1b4853f5SAndroid Build Coastguard Worker * @hdr: header containing the status of the ioctl. 138*1b4853f5SAndroid Build Coastguard Worker */ 139*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_ioctl { 140*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_header hdr; 141*1b4853f5SAndroid Build Coastguard Worker }; 142*1b4853f5SAndroid Build Coastguard Worker 143*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_MMAP_FLAG_RW (1 << 0) 144*1b4853f5SAndroid Build Coastguard Worker 145*1b4853f5SAndroid Build Coastguard Worker /** 146*1b4853f5SAndroid Build Coastguard Worker * VIRTIO_MEDIA_CMD_MMAP - Command for mapping a MMAP buffer into the guest's address space. 147*1b4853f5SAndroid Build Coastguard Worker * 148*1b4853f5SAndroid Build Coastguard Worker */ 149*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_CMD_MMAP 4 150*1b4853f5SAndroid Build Coastguard Worker 151*1b4853f5SAndroid Build Coastguard Worker /** 152*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_cmd_mmap - Driver command for VIRTIO_MEDIA_CMD_MMAP. 153*1b4853f5SAndroid Build Coastguard Worker */ 154*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_mmap { 155*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_header hdr; 156*1b4853f5SAndroid Build Coastguard Worker u32 session_id; 157*1b4853f5SAndroid Build Coastguard Worker u32 flags; 158*1b4853f5SAndroid Build Coastguard Worker u32 offset; 159*1b4853f5SAndroid Build Coastguard Worker }; 160*1b4853f5SAndroid Build Coastguard Worker 161*1b4853f5SAndroid Build Coastguard Worker /** 162*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_resp_mmap - Device response for VIRTIO_MEDIA_CMD_MMAP. 163*1b4853f5SAndroid Build Coastguard Worker * 164*1b4853f5SAndroid Build Coastguard Worker * @hdr: header containing the status of the command. 165*1b4853f5SAndroid Build Coastguard Worker * @guest_addr: offset into SHM region 0 of the start of the mapping. 166*1b4853f5SAndroid Build Coastguard Worker * @len: length of the mapping. 167*1b4853f5SAndroid Build Coastguard Worker */ 168*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_mmap { 169*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_header hdr; 170*1b4853f5SAndroid Build Coastguard Worker u64 guest_addr; 171*1b4853f5SAndroid Build Coastguard Worker u64 len; 172*1b4853f5SAndroid Build Coastguard Worker }; 173*1b4853f5SAndroid Build Coastguard Worker 174*1b4853f5SAndroid Build Coastguard Worker /** 175*1b4853f5SAndroid Build Coastguard Worker * VIRTIO_MEDIA_CMD_MUNMAP - Unmap a MMAP buffer previously mapped using VIRTIO_MEDIA_CMD_MMAP. 176*1b4853f5SAndroid Build Coastguard Worker */ 177*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_CMD_MUNMAP 5 178*1b4853f5SAndroid Build Coastguard Worker 179*1b4853f5SAndroid Build Coastguard Worker /** 180*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_cmd_munmap - Driver command for VIRTIO_MEDIA_CMD_MUNMAP. 181*1b4853f5SAndroid Build Coastguard Worker * 182*1b4853f5SAndroid Build Coastguard Worker * @guest_addr: offset into SHM region 0 at which the buffer has been previously 183*1b4853f5SAndroid Build Coastguard Worker * mapped. 184*1b4853f5SAndroid Build Coastguard Worker */ 185*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_munmap { 186*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_cmd_header hdr; 187*1b4853f5SAndroid Build Coastguard Worker u64 guest_addr; 188*1b4853f5SAndroid Build Coastguard Worker }; 189*1b4853f5SAndroid Build Coastguard Worker 190*1b4853f5SAndroid Build Coastguard Worker /** 191*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_resp_munmap - Device response for VIRTIO_MEDIA_CMD_MUNMAP. 192*1b4853f5SAndroid Build Coastguard Worker * 193*1b4853f5SAndroid Build Coastguard Worker * @hdr: header containing the status of the command. 194*1b4853f5SAndroid Build Coastguard Worker */ 195*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_munmap { 196*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_resp_header hdr; 197*1b4853f5SAndroid Build Coastguard Worker }; 198*1b4853f5SAndroid Build Coastguard Worker 199*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_EVT_ERROR 0 200*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_EVT_DQBUF 1 201*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_EVT_EVENT 2 202*1b4853f5SAndroid Build Coastguard Worker 203*1b4853f5SAndroid Build Coastguard Worker /** 204*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_event_header - Header for events queued by the device for the driver on the eventq. 205*1b4853f5SAndroid Build Coastguard Worker * 206*1b4853f5SAndroid Build Coastguard Worker * @event: one of VIRTIO_MEDIA_EVT_* 207*1b4853f5SAndroid Build Coastguard Worker * @session_id: ID of the session the event applies to. 208*1b4853f5SAndroid Build Coastguard Worker */ 209*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_event_header { 210*1b4853f5SAndroid Build Coastguard Worker u32 event; 211*1b4853f5SAndroid Build Coastguard Worker u32 session_id; 212*1b4853f5SAndroid Build Coastguard Worker }; 213*1b4853f5SAndroid Build Coastguard Worker 214*1b4853f5SAndroid Build Coastguard Worker /** 215*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_event_error - Device-side error. 216*1b4853f5SAndroid Build Coastguard Worker * 217*1b4853f5SAndroid Build Coastguard Worker * Upon receiving this event, the session mentioned in the header is considered corrupted and closed. 218*1b4853f5SAndroid Build Coastguard Worker * 219*1b4853f5SAndroid Build Coastguard Worker * @hdr: header for the event. 220*1b4853f5SAndroid Build Coastguard Worker * @errno: error code describing the kind of error that occurred. 221*1b4853f5SAndroid Build Coastguard Worker */ 222*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_event_error { 223*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_event_header hdr; 224*1b4853f5SAndroid Build Coastguard Worker u32 errno; 225*1b4853f5SAndroid Build Coastguard Worker u32 __padding; 226*1b4853f5SAndroid Build Coastguard Worker }; 227*1b4853f5SAndroid Build Coastguard Worker 228*1b4853f5SAndroid Build Coastguard Worker /** 229*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_event_dqbuf - Signals that a buffer is not being used anymore by the device and is returned to the driver. 230*1b4853f5SAndroid Build Coastguard Worker * 231*1b4853f5SAndroid Build Coastguard Worker * @hdr: header for the event. 232*1b4853f5SAndroid Build Coastguard Worker * @buffer: struct v4l2_buffer describing the buffer that has been dequeued. 233*1b4853f5SAndroid Build Coastguard Worker */ 234*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_event_dqbuf { 235*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_event_header hdr; 236*1b4853f5SAndroid Build Coastguard Worker struct v4l2_buffer buffer; 237*1b4853f5SAndroid Build Coastguard Worker struct v4l2_plane planes[VIDEO_MAX_PLANES]; 238*1b4853f5SAndroid Build Coastguard Worker }; 239*1b4853f5SAndroid Build Coastguard Worker 240*1b4853f5SAndroid Build Coastguard Worker /** 241*1b4853f5SAndroid Build Coastguard Worker * struct virtio_media_event_event - Signals that a V4L2 event has been emitted for a session. 242*1b4853f5SAndroid Build Coastguard Worker * 243*1b4853f5SAndroid Build Coastguard Worker * @hdr: header for the event. 244*1b4853f5SAndroid Build Coastguard Worker * @event: description of the event that occurred. 245*1b4853f5SAndroid Build Coastguard Worker */ 246*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_event_event { 247*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_event_header hdr; 248*1b4853f5SAndroid Build Coastguard Worker struct v4l2_event event; 249*1b4853f5SAndroid Build Coastguard Worker }; 250*1b4853f5SAndroid Build Coastguard Worker 251*1b4853f5SAndroid Build Coastguard Worker /** 252*1b4853f5SAndroid Build Coastguard Worker * Maximum size of an event. We will queue descriptors of this size on the eventq. 253*1b4853f5SAndroid Build Coastguard Worker */ 254*1b4853f5SAndroid Build Coastguard Worker #define VIRTIO_MEDIA_EVENT_MAX_SIZE sizeof(struct virtio_media_event_dqbuf) 255*1b4853f5SAndroid Build Coastguard Worker 256*1b4853f5SAndroid Build Coastguard Worker #endif // __VIRTIO_MEDIA_PROTOCOL_H 257