virtio_pcm.h 3.7 KB

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