1 /* SPDX-License-Identifier: GPL-2.0+ */ 2 /* 3 * virtio-snd: Virtio sound device 4 * Copyright (C) 2021 OpenSynergy GmbH 5 */ 6 #ifndef VIRTIO_SND_PCM_H 7 #define VIRTIO_SND_PCM_H 8 9 #include <linux/atomic.h> 10 #include <linux/virtio_config.h> 11 #include <sound/pcm.h> 12 #include <sound/pcm-indirect.h> 13 14 struct virtio_pcm; 15 struct virtio_pcm_msg; 16 17 /** 18 * struct virtio_pcm_substream - VirtIO PCM substream. 19 * @snd: VirtIO sound device. 20 * @nid: Function group node identifier. 21 * @sid: Stream identifier. 22 * @direction: Stream data flow direction (SNDRV_PCM_STREAM_XXX). 23 * @features: Stream VirtIO feature bit map (1 << VIRTIO_SND_PCM_F_XXX). 24 * @substream: Kernel ALSA substream. 25 * @pcm_indirect: Kernel indirect pcm structure. 26 * @hw: Kernel ALSA substream hardware descriptor. 27 * @elapsed_period: Kernel work to handle the elapsed period state. 28 * @lock: Spinlock that protects fields shared by interrupt handlers and 29 * substream operators. 30 * @buffer_bytes: Current buffer size in bytes. 31 * @hw_ptr: Substream hardware pointer value in bytes [0 ... buffer_bytes). 32 * @xfer_enabled: Data transfer state (0 - off, 1 - on). 33 * @xfer_xrun: Data underflow/overflow state (0 - no xrun, 1 - xrun). 34 * @stopped: True if the substream is stopped and must be released on the device 35 * side. 36 * @suspended: True if the substream is suspended and must be reconfigured on 37 * the device side at resume. 38 * @msgs: Allocated I/O messages. 39 * @nmsgs: Number of allocated I/O messages. 40 * @msg_count: Number of pending I/O messages in the virtqueue. 41 * @msg_empty: Notify when msg_count is zero. 42 */ 43 struct virtio_pcm_substream { 44 struct virtio_snd *snd; 45 u32 nid; 46 u32 sid; 47 u32 direction; 48 u32 features; 49 struct snd_pcm_substream *substream; 50 struct snd_pcm_indirect pcm_indirect; 51 struct snd_pcm_hardware hw; 52 struct work_struct elapsed_period; 53 spinlock_t lock; 54 size_t buffer_bytes; 55 size_t hw_ptr; 56 bool xfer_enabled; 57 bool xfer_xrun; 58 bool stopped; 59 bool suspended; 60 struct virtio_pcm_msg **msgs; 61 unsigned int nmsgs; 62 unsigned int msg_count; 63 wait_queue_head_t msg_empty; 64 }; 65 66 /** 67 * struct virtio_pcm_stream - VirtIO PCM stream. 68 * @substreams: VirtIO substreams belonging to the stream. 69 * @nsubstreams: Number of substreams. 70 * @chmaps: Kernel channel maps belonging to the stream. 71 * @nchmaps: Number of channel maps. 72 */ 73 struct virtio_pcm_stream { 74 struct virtio_pcm_substream **substreams; 75 u32 nsubstreams; 76 struct snd_pcm_chmap_elem *chmaps; 77 u32 nchmaps; 78 }; 79 80 /** 81 * struct virtio_pcm - VirtIO PCM device. 82 * @list: VirtIO PCM list entry. 83 * @nid: Function group node identifier. 84 * @pcm: Kernel PCM device. 85 * @streams: VirtIO PCM streams (playback and capture). 86 */ 87 struct virtio_pcm { 88 struct list_head list; 89 u32 nid; 90 struct snd_pcm *pcm; 91 struct virtio_pcm_stream streams[SNDRV_PCM_STREAM_LAST + 1]; 92 }; 93 94 extern const struct snd_pcm_ops virtsnd_pcm_ops[]; 95 96 int virtsnd_pcm_validate(struct virtio_device *vdev); 97 98 int virtsnd_pcm_parse_cfg(struct virtio_snd *snd); 99 100 int virtsnd_pcm_build_devs(struct virtio_snd *snd); 101 102 void virtsnd_pcm_event(struct virtio_snd *snd, struct virtio_snd_event *event); 103 104 void virtsnd_pcm_tx_notify_cb(struct virtqueue *vqueue); 105 106 void virtsnd_pcm_rx_notify_cb(struct virtqueue *vqueue); 107 108 struct virtio_pcm *virtsnd_pcm_find(struct virtio_snd *snd, u32 nid); 109 110 struct virtio_pcm *virtsnd_pcm_find_or_create(struct virtio_snd *snd, u32 nid); 111 112 struct virtio_snd_msg * 113 virtsnd_pcm_ctl_msg_alloc(struct virtio_pcm_substream *vss, 114 unsigned int command, gfp_t gfp); 115 116 int virtsnd_pcm_msg_alloc(struct virtio_pcm_substream *vss, 117 unsigned int periods, unsigned int period_bytes); 118 119 void virtsnd_pcm_msg_free(struct virtio_pcm_substream *vss); 120 121 int virtsnd_pcm_msg_send(struct virtio_pcm_substream *vss, unsigned long offset, 122 unsigned long bytes); 123 124 unsigned int virtsnd_pcm_msg_pending_num(struct virtio_pcm_substream *vss); 125 126 #endif /* VIRTIO_SND_PCM_H */ 127