// SPDX-License-Identifier: GPL-2.0+ /* * virtio-snd: Virtio sound device * Copyright (C) 2021 OpenSynergy GmbH */ #include <sound/pcm_params.h> #include "virtio_card.h" /** * struct virtio_pcm_msg - VirtIO I/O message. * @substream: VirtIO PCM substream. * @xfer: Request header payload. * @status: Response header payload. * @length: Data length in bytes. * @sgs: Payload scatter-gather table. */ struct virtio_pcm_msg { … }; /** * enum pcm_msg_sg_index - Index values for the virtio_pcm_msg->sgs field in * an I/O message. * @PCM_MSG_SG_XFER: Element containing a virtio_snd_pcm_xfer structure. * @PCM_MSG_SG_STATUS: Element containing a virtio_snd_pcm_status structure. * @PCM_MSG_SG_DATA: The first element containing a data buffer. */ enum pcm_msg_sg_index { … }; /** * virtsnd_pcm_sg_num() - Count the number of sg-elements required to represent * vmalloc'ed buffer. * @data: Pointer to vmalloc'ed buffer. * @length: Buffer size. * * Context: Any context. * Return: Number of physically contiguous parts in the @data. */ static int virtsnd_pcm_sg_num(u8 *data, unsigned int length) { … } /** * virtsnd_pcm_sg_from() - Build sg-list from vmalloc'ed buffer. * @sgs: Preallocated sg-list to populate. * @nsgs: The maximum number of elements in the @sgs. * @data: Pointer to vmalloc'ed buffer. * @length: Buffer size. * * Splits the buffer into physically contiguous parts and makes an sg-list of * such parts. * * Context: Any context. */ static void virtsnd_pcm_sg_from(struct scatterlist *sgs, int nsgs, u8 *data, unsigned int length) { … } /** * virtsnd_pcm_msg_alloc() - Allocate I/O messages. * @vss: VirtIO PCM substream. * @periods: Current number of periods. * @period_bytes: Current period size in bytes. * * The function slices the buffer into @periods parts (each with the size of * @period_bytes), and creates @periods corresponding I/O messages. * * Context: Any context that permits to sleep. * Return: 0 on success, -ENOMEM on failure. */ int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss, unsigned int periods, unsigned int period_bytes) { … } /** * virtsnd_pcm_msg_free() - Free all allocated I/O messages. * @vss: VirtIO PCM substream. * * Context: Any context. */ void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss) { … } /** * virtsnd_pcm_msg_send() - Send asynchronous I/O messages. * @vss: VirtIO PCM substream. * @offset: starting position that has been updated * @bytes: number of bytes that has been updated * * All messages are organized in an ordered circular list. Each time the * function is called, all currently non-enqueued messages are added to the * virtqueue. For this, the function uses offset and bytes to calculate the * messages that need to be added. * * Context: Any context. Expects the tx/rx queue and the VirtIO substream * spinlocks to be held by caller. * Return: 0 on success, -errno on failure. */ int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss, unsigned long offset, unsigned long bytes) { … } /** * virtsnd_pcm_msg_pending_num() - Returns the number of pending I/O messages. * @vss: VirtIO substream. * * Context: Any context. * Return: Number of messages. */ unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss) { … } /** * virtsnd_pcm_msg_complete() - Complete an I/O message. * @msg: I/O message. * @written_bytes: Number of bytes written to the message. * * Completion of the message means the elapsed period. If transmission is * allowed, then each completed message is immediately placed back at the end * of the queue. * * For the playback substream, @written_bytes is equal to sizeof(msg->status). * * For the capture substream, @written_bytes is equal to sizeof(msg->status) * plus the number of captured bytes. * * Context: Interrupt context. Takes and releases the VirtIO substream spinlock. */ static void virtsnd_pcm_msg_complete(struct virtio_pcm_msg *msg, size_t written_bytes) { … } /** * virtsnd_pcm_notify_cb() - Process all completed I/O messages. * @queue: Underlying tx/rx virtqueue. * * Context: Interrupt context. Takes and releases the tx/rx queue spinlock. */ static inline void virtsnd_pcm_notify_cb(struct virtio_snd_queue *queue) { … } /** * virtsnd_pcm_tx_notify_cb() - Process all completed TX messages. * @vqueue: Underlying tx virtqueue. * * Context: Interrupt context. */ void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue) { … } /** * virtsnd_pcm_rx_notify_cb() - Process all completed RX messages. * @vqueue: Underlying rx virtqueue. * * Context: Interrupt context. */ void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue) { … } /** * virtsnd_pcm_ctl_msg_alloc() - Allocate and initialize the PCM device control * message for the specified substream. * @vss: VirtIO PCM substream. * @command: Control request code (VIRTIO_SND_R_PCM_XXX). * @gfp: Kernel flags for memory allocation. * * Context: Any context. May sleep if @gfp flags permit. * Return: Allocated message on success, NULL on failure. */ struct virtio_snd_msg * virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss, unsigned int command, gfp_t gfp) { … }