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 * Scatterlist filler helpers for virtio-media. 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_SCATTERLIST_FILLER_H 10*1b4853f5SAndroid Build Coastguard Worker #define __VIRTIO_MEDIA_SCATTERLIST_FILLER_H 11*1b4853f5SAndroid Build Coastguard Worker 12*1b4853f5SAndroid Build Coastguard Worker #include <linux/scatterlist.h> 13*1b4853f5SAndroid Build Coastguard Worker 14*1b4853f5SAndroid Build Coastguard Worker #include "session.h" 15*1b4853f5SAndroid Build Coastguard Worker 16*1b4853f5SAndroid Build Coastguard Worker /** 17*1b4853f5SAndroid Build Coastguard Worker * struct scatterlist_filler - helper to fill a scatterlist from data. 18*1b4853f5SAndroid Build Coastguard Worker * 19*1b4853f5SAndroid Build Coastguard Worker * @descs: array of scatterlist that will be filled. 20*1b4853f5SAndroid Build Coastguard Worker * @num_descs: number of entries in descs. 21*1b4853f5SAndroid Build Coastguard Worker * @cur_desc: next descriptor to be written in descs. 22*1b4853f5SAndroid Build Coastguard Worker * @shadow_buffer: pointer to a shadow buffer where elements that cannot be 23*1b4853f5SAndroid Build Coastguard Worker * mapped directly into the scatterlist get copied. 24*1b4853f5SAndroid Build Coastguard Worker * @shadow_buffer_size: size of shadow_buffer. 25*1b4853f5SAndroid Build Coastguard Worker * @shadow_buffer_pos: current position in shadow_buffer. 26*1b4853f5SAndroid Build Coastguard Worker * @sgs: list of scatterlist pointers to eventually pass to virtio. 27*1b4853f5SAndroid Build Coastguard Worker * @num_sgs: total number of entries in sgs. 28*1b4853f5SAndroid Build Coastguard Worker * @cur_sg: next entry in @sgs to be written into. 29*1b4853f5SAndroid Build Coastguard Worker * 30*1b4853f5SAndroid Build Coastguard Worker */ 31*1b4853f5SAndroid Build Coastguard Worker struct scatterlist_filler { 32*1b4853f5SAndroid Build Coastguard Worker struct scatterlist *descs; 33*1b4853f5SAndroid Build Coastguard Worker size_t num_descs; 34*1b4853f5SAndroid Build Coastguard Worker size_t cur_desc; 35*1b4853f5SAndroid Build Coastguard Worker 36*1b4853f5SAndroid Build Coastguard Worker void *shadow_buffer; 37*1b4853f5SAndroid Build Coastguard Worker size_t shadow_buffer_size; 38*1b4853f5SAndroid Build Coastguard Worker size_t shadow_buffer_pos; 39*1b4853f5SAndroid Build Coastguard Worker 40*1b4853f5SAndroid Build Coastguard Worker struct scatterlist **sgs; 41*1b4853f5SAndroid Build Coastguard Worker size_t num_sgs; 42*1b4853f5SAndroid Build Coastguard Worker size_t cur_sg; 43*1b4853f5SAndroid Build Coastguard Worker }; 44*1b4853f5SAndroid Build Coastguard Worker 45*1b4853f5SAndroid Build Coastguard Worker /** 46*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_add_sg - Add a scatterlist to the list of sgs. 47*1b4853f5SAndroid Build Coastguard Worker */ 48*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_add_sg(struct scatterlist_filler *filler, 49*1b4853f5SAndroid Build Coastguard Worker struct scatterlist *sg); 50*1b4853f5SAndroid Build Coastguard Worker 51*1b4853f5SAndroid Build Coastguard Worker /** 52*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_add_ioctl_cmd - Add an ioctl command to the list. 53*1b4853f5SAndroid Build Coastguard Worker */ 54*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_add_ioctl_cmd(struct scatterlist_filler *filler, 55*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_session *session, 56*1b4853f5SAndroid Build Coastguard Worker u32 ioctl_code); 57*1b4853f5SAndroid Build Coastguard Worker 58*1b4853f5SAndroid Build Coastguard Worker /** 59*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_add_ioctl_resp - Add storage to receive an ioctl response to the list. 60*1b4853f5SAndroid Build Coastguard Worker */ 61*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_add_ioctl_resp(struct scatterlist_filler *filler, 62*1b4853f5SAndroid Build Coastguard Worker struct virtio_media_session *session); 63*1b4853f5SAndroid Build Coastguard Worker 64*1b4853f5SAndroid Build Coastguard Worker /** 65*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_add_data - Add arbitrary data to the list. 66*1b4853f5SAndroid Build Coastguard Worker * 67*1b4853f5SAndroid Build Coastguard Worker * The data will either be directly mapped, or copied into the shadow buffer to be mapped there. 68*1b4853f5SAndroid Build Coastguard Worker */ 69*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_add_data(struct scatterlist_filler *filler, void *data, 70*1b4853f5SAndroid Build Coastguard Worker size_t len); 71*1b4853f5SAndroid Build Coastguard Worker 72*1b4853f5SAndroid Build Coastguard Worker /** 73*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_add_buffer - Add a v4l2_buffer and its planes to the list. 74*1b4853f5SAndroid Build Coastguard Worker * 75*1b4853f5SAndroid Build Coastguard Worker * The buffer and its pointers will be either directly mapped, or copied into the shadow buffer to be mapped there. 76*1b4853f5SAndroid Build Coastguard Worker */ 77*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_add_buffer(struct scatterlist_filler *filler, 78*1b4853f5SAndroid Build Coastguard Worker struct v4l2_buffer *buffer); 79*1b4853f5SAndroid Build Coastguard Worker 80*1b4853f5SAndroid Build Coastguard Worker /** 81*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_add_buffer_userptr - Add a USERPTR v4l2_buffer's content to the list. 82*1b4853f5SAndroid Build Coastguard Worker * 83*1b4853f5SAndroid Build Coastguard Worker * For performance reasons the shadow buffer is never used. 84*1b4853f5SAndroid Build Coastguard Worker */ 85*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_add_buffer_userptr(struct scatterlist_filler *filler, 86*1b4853f5SAndroid Build Coastguard Worker struct v4l2_buffer *b); 87*1b4853f5SAndroid Build Coastguard Worker 88*1b4853f5SAndroid Build Coastguard Worker /** 89*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_add_ext_ctrls - Add a v4l2_ext_controls and its controls to the list. 90*1b4853f5SAndroid Build Coastguard Worker * 91*1b4853f5SAndroid Build Coastguard Worker * The controls will be either directly mapped, or copied into the shadow buffer to be mapped there. 92*1b4853f5SAndroid Build Coastguard Worker * 93*1b4853f5SAndroid Build Coastguard Worker * For controls with pointer data, the data is always directly mapped, not copied. 94*1b4853f5SAndroid Build Coastguard Worker */ 95*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_add_ext_ctrls(struct scatterlist_filler *filler, 96*1b4853f5SAndroid Build Coastguard Worker struct v4l2_ext_controls *ctrls, 97*1b4853f5SAndroid Build Coastguard Worker bool add_userptrs); 98*1b4853f5SAndroid Build Coastguard Worker 99*1b4853f5SAndroid Build Coastguard Worker /** 100*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_retrieve_data - Retrieve data written by the device on the shadow buffer, if needed. 101*1b4853f5SAndroid Build Coastguard Worker * 102*1b4853f5SAndroid Build Coastguard Worker * If the shadow buffer is pointed to by @sg, copy its content back into @data. 103*1b4853f5SAndroid Build Coastguard Worker */ 104*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_retrieve_data(struct virtio_media_session *session, 105*1b4853f5SAndroid Build Coastguard Worker struct scatterlist *sg, void *data, 106*1b4853f5SAndroid Build Coastguard Worker size_t len); 107*1b4853f5SAndroid Build Coastguard Worker 108*1b4853f5SAndroid Build Coastguard Worker /** 109*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_retrieve_buffer - Retrieve buffer data written by the device on the shadow buffer, if needed. 110*1b4853f5SAndroid Build Coastguard Worker * 111*1b4853f5SAndroid Build Coastguard Worker * If the shadow buffer is pointed to by @sg, copy its content back into @buffer. 112*1b4853f5SAndroid Build Coastguard Worker */ 113*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_retrieve_buffer(struct virtio_media_session *session, 114*1b4853f5SAndroid Build Coastguard Worker struct scatterlist **buffer_sgs, 115*1b4853f5SAndroid Build Coastguard Worker struct v4l2_buffer *buffer, 116*1b4853f5SAndroid Build Coastguard Worker size_t num_planes); 117*1b4853f5SAndroid Build Coastguard Worker 118*1b4853f5SAndroid Build Coastguard Worker /** 119*1b4853f5SAndroid Build Coastguard Worker * scatterlist_filler_retrieve_ext_ctrls - Retrieve controls data written by the device on the shadow buffer, if needed. 120*1b4853f5SAndroid Build Coastguard Worker * 121*1b4853f5SAndroid Build Coastguard Worker * If the shadow buffer is pointed to by @sg, copy its content back into @ctrls. 122*1b4853f5SAndroid Build Coastguard Worker */ 123*1b4853f5SAndroid Build Coastguard Worker int scatterlist_filler_retrieve_ext_ctrls(struct virtio_media_session *session, 124*1b4853f5SAndroid Build Coastguard Worker struct scatterlist **sgs_idx, 125*1b4853f5SAndroid Build Coastguard Worker int num_ctrls_sgs, 126*1b4853f5SAndroid Build Coastguard Worker struct v4l2_ext_controls *ctrls); 127*1b4853f5SAndroid Build Coastguard Worker 128*1b4853f5SAndroid Build Coastguard Worker #endif // __VIRTIO_MEDIA_SCATTERLIST_FILLER_H 129