compress_driver.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /* SPDX-License-Identifier: GPL-2.0
  2. *
  3. * compress_driver.h - compress offload driver definations
  4. *
  5. * Copyright (C) 2011 Intel Corporation
  6. * Authors: Vinod Koul <vinod.koul@linux.intel.com>
  7. * Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
  8. */
  9. #ifndef __COMPRESS_DRIVER_H
  10. #define __COMPRESS_DRIVER_H
  11. #include <linux/types.h>
  12. #include <linux/sched.h>
  13. #include <sound/core.h>
  14. #include <sound/compress_offload.h>
  15. #include <sound/asound.h>
  16. #include <sound/pcm.h>
  17. struct snd_compr_ops;
  18. /**
  19. * struct snd_compr_runtime: runtime stream description
  20. * @state: stream state
  21. * @ops: pointer to DSP callbacks
  22. * @buffer: pointer to kernel buffer, valid only when not in mmap mode or
  23. * DSP doesn't implement copy
  24. * @buffer_size: size of the above buffer
  25. * @fragment_size: size of buffer fragment in bytes
  26. * @fragments: number of such fragments
  27. * @total_bytes_available: cumulative number of bytes made available in
  28. * the ring buffer
  29. * @total_bytes_transferred: cumulative bytes transferred by offload DSP
  30. * @sleep: poll sleep
  31. * @private_data: driver private data pointer
  32. * @dma_area: virtual buffer address
  33. * @dma_addr: physical buffer address (not accessible from main CPU)
  34. * @dma_bytes: size of DMA area
  35. * @dma_buffer_p: runtime dma buffer pointer
  36. */
  37. struct snd_compr_runtime {
  38. snd_pcm_state_t state;
  39. struct snd_compr_ops *ops;
  40. void *buffer;
  41. u64 buffer_size;
  42. u32 fragment_size;
  43. u32 fragments;
  44. u64 total_bytes_available;
  45. u64 total_bytes_transferred;
  46. wait_queue_head_t sleep;
  47. void *private_data;
  48. unsigned char *dma_area;
  49. dma_addr_t dma_addr;
  50. size_t dma_bytes;
  51. struct snd_dma_buffer *dma_buffer_p;
  52. };
  53. /**
  54. * struct snd_compr_stream: compressed stream
  55. * @name: device name
  56. * @ops: pointer to DSP callbacks
  57. * @runtime: pointer to runtime structure
  58. * @device: device pointer
  59. * @error_work: delayed work used when closing the stream due to an error
  60. * @direction: stream direction, playback/recording
  61. * @metadata_set: metadata set flag, true when set
  62. * @next_track: has userspace signal next track transition, true when set
  63. * @partial_drain: undergoing partial_drain for stream, true when set
  64. * @pause_in_draining: paused during draining state, true when set
  65. * @private_data: pointer to DSP private data
  66. * @dma_buffer: allocated buffer if any
  67. */
  68. struct snd_compr_stream {
  69. const char *name;
  70. struct snd_compr_ops *ops;
  71. struct snd_compr_runtime *runtime;
  72. struct snd_compr *device;
  73. struct delayed_work error_work;
  74. enum snd_compr_direction direction;
  75. bool metadata_set;
  76. bool next_track;
  77. bool partial_drain;
  78. bool pause_in_draining;
  79. void *private_data;
  80. struct snd_dma_buffer dma_buffer;
  81. };
  82. /**
  83. * struct snd_compr_ops: compressed path DSP operations
  84. * @open: Open the compressed stream
  85. * This callback is mandatory and shall keep dsp ready to receive the stream
  86. * parameter
  87. * @free: Close the compressed stream, mandatory
  88. * @set_params: Sets the compressed stream parameters, mandatory
  89. * This can be called in during stream creation only to set codec params
  90. * and the stream properties
  91. * @get_params: retrieve the codec parameters, mandatory
  92. * @set_metadata: Set the metadata values for a stream
  93. * @get_metadata: retrieves the requested metadata values from stream
  94. * @trigger: Trigger operations like start, pause, resume, drain, stop.
  95. * This callback is mandatory
  96. * @pointer: Retrieve current h/w pointer information. Mandatory
  97. * @copy: Copy the compressed data to/from userspace, Optional
  98. * Can't be implemented if DSP supports mmap
  99. * @mmap: DSP mmap method to mmap DSP memory
  100. * @ack: Ack for DSP when data is written to audio buffer, Optional
  101. * Not valid if copy is implemented
  102. * @get_caps: Retrieve DSP capabilities, mandatory
  103. * @get_codec_caps: Retrieve capabilities for a specific codec, mandatory
  104. */
  105. struct snd_compr_ops {
  106. int (*open)(struct snd_compr_stream *stream);
  107. int (*free)(struct snd_compr_stream *stream);
  108. int (*set_params)(struct snd_compr_stream *stream,
  109. struct snd_compr_params *params);
  110. int (*get_params)(struct snd_compr_stream *stream,
  111. struct snd_codec *params);
  112. int (*set_metadata)(struct snd_compr_stream *stream,
  113. struct snd_compr_metadata *metadata);
  114. int (*get_metadata)(struct snd_compr_stream *stream,
  115. struct snd_compr_metadata *metadata);
  116. int (*trigger)(struct snd_compr_stream *stream, int cmd);
  117. int (*pointer)(struct snd_compr_stream *stream,
  118. struct snd_compr_tstamp *tstamp);
  119. int (*copy)(struct snd_compr_stream *stream, char __user *buf,
  120. size_t count);
  121. int (*mmap)(struct snd_compr_stream *stream,
  122. struct vm_area_struct *vma);
  123. int (*ack)(struct snd_compr_stream *stream, size_t bytes);
  124. int (*get_caps) (struct snd_compr_stream *stream,
  125. struct snd_compr_caps *caps);
  126. int (*get_codec_caps) (struct snd_compr_stream *stream,
  127. struct snd_compr_codec_caps *codec);
  128. };
  129. /**
  130. * struct snd_compr: Compressed device
  131. * @name: DSP device name
  132. * @dev: associated device instance
  133. * @ops: pointer to DSP callbacks
  134. * @private_data: pointer to DSP pvt data
  135. * @card: sound card pointer
  136. * @direction: Playback or capture direction
  137. * @lock: device lock
  138. * @device: device id
  139. * @use_pause_in_draining: allow pause in draining, true when set
  140. */
  141. struct snd_compr {
  142. const char *name;
  143. struct device *dev;
  144. struct snd_compr_ops *ops;
  145. void *private_data;
  146. struct snd_card *card;
  147. unsigned int direction;
  148. struct mutex lock;
  149. int device;
  150. bool use_pause_in_draining;
  151. #ifdef CONFIG_SND_VERBOSE_PROCFS
  152. /* private: */
  153. char id[64];
  154. struct snd_info_entry *proc_root;
  155. struct snd_info_entry *proc_info_entry;
  156. #endif
  157. };
  158. /* compress device register APIs */
  159. int snd_compress_new(struct snd_card *card, int device,
  160. int type, const char *id, struct snd_compr *compr);
  161. /**
  162. * snd_compr_use_pause_in_draining - Allow pause and resume in draining state
  163. * @substream: compress substream to set
  164. *
  165. * Allow pause and resume in draining state.
  166. * Only HW driver supports this transition can call this API.
  167. */
  168. static inline void snd_compr_use_pause_in_draining(struct snd_compr_stream *substream)
  169. {
  170. substream->device->use_pause_in_draining = true;
  171. }
  172. /* dsp driver callback apis
  173. * For playback: driver should call snd_compress_fragment_elapsed() to let the
  174. * framework know that a fragment has been consumed from the ring buffer
  175. *
  176. * For recording: we want to know when a frame is available or when
  177. * at least one frame is available so snd_compress_frame_elapsed()
  178. * callback should be called when a encodeded frame is available
  179. */
  180. static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
  181. {
  182. wake_up(&stream->runtime->sleep);
  183. }
  184. static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
  185. {
  186. if (snd_BUG_ON(!stream))
  187. return;
  188. /* for partial_drain case we are back to running state on success */
  189. if (stream->partial_drain) {
  190. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  191. stream->partial_drain = false; /* clear this flag as well */
  192. } else {
  193. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  194. }
  195. wake_up(&stream->runtime->sleep);
  196. }
  197. /**
  198. * snd_compr_set_runtime_buffer - Set the Compress runtime buffer
  199. * @stream: compress stream to set
  200. * @bufp: the buffer information, NULL to clear
  201. *
  202. * Copy the buffer information to runtime buffer when @bufp is non-NULL.
  203. * Otherwise it clears the current buffer information.
  204. */
  205. static inline void
  206. snd_compr_set_runtime_buffer(struct snd_compr_stream *stream,
  207. struct snd_dma_buffer *bufp)
  208. {
  209. struct snd_compr_runtime *runtime = stream->runtime;
  210. if (bufp) {
  211. runtime->dma_buffer_p = bufp;
  212. runtime->dma_area = bufp->area;
  213. runtime->dma_addr = bufp->addr;
  214. runtime->dma_bytes = bufp->bytes;
  215. } else {
  216. runtime->dma_buffer_p = NULL;
  217. runtime->dma_area = NULL;
  218. runtime->dma_addr = 0;
  219. runtime->dma_bytes = 0;
  220. }
  221. }
  222. int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size);
  223. int snd_compr_free_pages(struct snd_compr_stream *stream);
  224. int snd_compr_stop_error(struct snd_compr_stream *stream,
  225. snd_pcm_state_t state);
  226. #endif