virtio_pcm.h 3.9 KB

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