compress_driver.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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_task_runtime: task runtime description
  20. * @list: list of all managed tasks
  21. * @input: input DMA buffer
  22. * @output: output DMA buffer
  23. * @seqno: sequence number
  24. * @input_size: really used data in the input buffer
  25. * @output_size: really used data in the output buffer
  26. * @flags: see SND_COMPRESS_TFLG_*
  27. * @state: actual task state
  28. * @private_value: used by the lowlevel driver (opaque)
  29. */
  30. struct snd_compr_task_runtime {
  31. struct list_head list;
  32. struct dma_buf *input;
  33. struct dma_buf *output;
  34. u64 seqno;
  35. u64 input_size;
  36. u64 output_size;
  37. u32 flags;
  38. u8 state;
  39. void *private_value;
  40. };
  41. /**
  42. * struct snd_compr_runtime: runtime stream description
  43. * @state: stream state
  44. * @ops: pointer to DSP callbacks
  45. * @buffer: pointer to kernel buffer, valid only when not in mmap mode or
  46. * DSP doesn't implement copy
  47. * @buffer_size: size of the above buffer
  48. * @fragment_size: size of buffer fragment in bytes
  49. * @fragments: number of such fragments
  50. * @total_bytes_available: cumulative number of bytes made available in
  51. * the ring buffer
  52. * @total_bytes_transferred: cumulative bytes transferred by offload DSP
  53. * @sleep: poll sleep
  54. * @private_data: driver private data pointer
  55. * @dma_area: virtual buffer address
  56. * @dma_addr: physical buffer address (not accessible from main CPU)
  57. * @dma_bytes: size of DMA area
  58. * @dma_buffer_p: runtime dma buffer pointer
  59. * @active_tasks: count of active tasks
  60. * @total_tasks: count of all tasks
  61. * @task_seqno: last task sequence number (!= 0)
  62. * @tasks: list of all tasks
  63. */
  64. struct snd_compr_runtime {
  65. snd_pcm_state_t state;
  66. struct snd_compr_ops *ops;
  67. void *buffer;
  68. u64 buffer_size;
  69. u32 fragment_size;
  70. u32 fragments;
  71. u64 total_bytes_available;
  72. u64 total_bytes_transferred;
  73. wait_queue_head_t sleep;
  74. void *private_data;
  75. unsigned char *dma_area;
  76. dma_addr_t dma_addr;
  77. size_t dma_bytes;
  78. struct snd_dma_buffer *dma_buffer_p;
  79. #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
  80. u32 active_tasks;
  81. u32 total_tasks;
  82. u64 task_seqno;
  83. struct list_head tasks;
  84. #endif
  85. };
  86. /**
  87. * struct snd_compr_stream: compressed stream
  88. * @name: device name
  89. * @ops: pointer to DSP callbacks
  90. * @runtime: pointer to runtime structure
  91. * @device: device pointer
  92. * @error_work: delayed work used when closing the stream due to an error
  93. * @direction: stream direction, playback/recording
  94. * @metadata_set: metadata set flag, true when set
  95. * @next_track: has userspace signal next track transition, true when set
  96. * @partial_drain: undergoing partial_drain for stream, true when set
  97. * @pause_in_draining: paused during draining state, true when set
  98. * @private_data: pointer to DSP private data
  99. * @dma_buffer: allocated buffer if any
  100. */
  101. struct snd_compr_stream {
  102. const char *name;
  103. struct snd_compr_ops *ops;
  104. struct snd_compr_runtime *runtime;
  105. struct snd_compr *device;
  106. struct delayed_work error_work;
  107. enum snd_compr_direction direction;
  108. bool metadata_set;
  109. bool next_track;
  110. bool partial_drain;
  111. bool pause_in_draining;
  112. void *private_data;
  113. struct snd_dma_buffer dma_buffer;
  114. };
  115. /**
  116. * struct snd_compr_ops: compressed path DSP operations
  117. * @open: Open the compressed stream
  118. * This callback is mandatory and shall keep dsp ready to receive the stream
  119. * parameter
  120. * @free: Close the compressed stream, mandatory
  121. * @set_params: Sets the compressed stream parameters, mandatory
  122. * This can be called in during stream creation only to set codec params
  123. * and the stream properties
  124. * @get_params: retrieve the codec parameters, mandatory
  125. * @set_metadata: Set the metadata values for a stream
  126. * @get_metadata: retrieves the requested metadata values from stream
  127. * @trigger: Trigger operations like start, pause, resume, drain, stop.
  128. * This callback is mandatory
  129. * @pointer: Retrieve current h/w pointer information. Mandatory
  130. * @copy: Copy the compressed data to/from userspace, Optional
  131. * Can't be implemented if DSP supports mmap
  132. * @mmap: DSP mmap method to mmap DSP memory
  133. * @ack: Ack for DSP when data is written to audio buffer, Optional
  134. * Not valid if copy is implemented
  135. * @get_caps: Retrieve DSP capabilities, mandatory
  136. * @get_codec_caps: Retrieve capabilities for a specific codec, mandatory
  137. * @task_create: Create a set of input/output buffers for accel operations
  138. * @task_start: Start (queue) a task for accel operations
  139. * @task_stop: Stop (dequeue) a task for accel operations
  140. * @task_free: Free a set of input/output buffers for accel operations
  141. */
  142. struct snd_compr_ops {
  143. int (*open)(struct snd_compr_stream *stream);
  144. int (*free)(struct snd_compr_stream *stream);
  145. int (*set_params)(struct snd_compr_stream *stream,
  146. struct snd_compr_params *params);
  147. int (*get_params)(struct snd_compr_stream *stream,
  148. struct snd_codec *params);
  149. int (*set_metadata)(struct snd_compr_stream *stream,
  150. struct snd_compr_metadata *metadata);
  151. int (*get_metadata)(struct snd_compr_stream *stream,
  152. struct snd_compr_metadata *metadata);
  153. int (*trigger)(struct snd_compr_stream *stream, int cmd);
  154. int (*pointer)(struct snd_compr_stream *stream,
  155. struct snd_compr_tstamp64 *tstamp);
  156. int (*copy)(struct snd_compr_stream *stream, char __user *buf,
  157. size_t count);
  158. int (*mmap)(struct snd_compr_stream *stream,
  159. struct vm_area_struct *vma);
  160. int (*ack)(struct snd_compr_stream *stream, size_t bytes);
  161. int (*get_caps) (struct snd_compr_stream *stream,
  162. struct snd_compr_caps *caps);
  163. int (*get_codec_caps) (struct snd_compr_stream *stream,
  164. struct snd_compr_codec_caps *codec);
  165. #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
  166. int (*task_create) (struct snd_compr_stream *stream, struct snd_compr_task_runtime *task);
  167. int (*task_start) (struct snd_compr_stream *stream, struct snd_compr_task_runtime *task);
  168. int (*task_stop) (struct snd_compr_stream *stream, struct snd_compr_task_runtime *task);
  169. int (*task_free) (struct snd_compr_stream *stream, struct snd_compr_task_runtime *task);
  170. #endif
  171. };
  172. /**
  173. * struct snd_compr: Compressed device
  174. * @name: DSP device name
  175. * @dev: associated device instance
  176. * @ops: pointer to DSP callbacks
  177. * @private_data: pointer to DSP pvt data
  178. * @card: sound card pointer
  179. * @direction: Playback or capture direction
  180. * @lock: device lock
  181. * @device: device id
  182. * @use_pause_in_draining: allow pause in draining, true when set
  183. */
  184. struct snd_compr {
  185. const char *name;
  186. struct device *dev;
  187. struct snd_compr_ops *ops;
  188. void *private_data;
  189. struct snd_card *card;
  190. unsigned int direction;
  191. struct mutex lock;
  192. int device;
  193. bool use_pause_in_draining;
  194. #ifdef CONFIG_SND_VERBOSE_PROCFS
  195. /* private: */
  196. char id[64];
  197. struct snd_info_entry *proc_root;
  198. struct snd_info_entry *proc_info_entry;
  199. #endif
  200. };
  201. /* compress device register APIs */
  202. int snd_compress_new(struct snd_card *card, int device,
  203. int type, const char *id, struct snd_compr *compr);
  204. /**
  205. * snd_compr_use_pause_in_draining - Allow pause and resume in draining state
  206. * @substream: compress substream to set
  207. *
  208. * Allow pause and resume in draining state.
  209. * Only HW driver supports this transition can call this API.
  210. */
  211. static inline void snd_compr_use_pause_in_draining(struct snd_compr_stream *substream)
  212. {
  213. substream->device->use_pause_in_draining = true;
  214. }
  215. /* dsp driver callback apis
  216. * For playback: driver should call snd_compress_fragment_elapsed() to let the
  217. * framework know that a fragment has been consumed from the ring buffer
  218. *
  219. * For recording: we want to know when a frame is available or when
  220. * at least one frame is available so snd_compress_frame_elapsed()
  221. * callback should be called when a encodeded frame is available
  222. */
  223. static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
  224. {
  225. wake_up(&stream->runtime->sleep);
  226. }
  227. static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
  228. {
  229. if (snd_BUG_ON(!stream))
  230. return;
  231. /* for partial_drain case we are back to running state on success */
  232. if (stream->partial_drain) {
  233. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  234. stream->partial_drain = false; /* clear this flag as well */
  235. } else {
  236. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  237. }
  238. wake_up(&stream->runtime->sleep);
  239. }
  240. /**
  241. * snd_compr_set_runtime_buffer - Set the Compress runtime buffer
  242. * @stream: compress stream to set
  243. * @bufp: the buffer information, NULL to clear
  244. *
  245. * Copy the buffer information to runtime buffer when @bufp is non-NULL.
  246. * Otherwise it clears the current buffer information.
  247. */
  248. static inline void
  249. snd_compr_set_runtime_buffer(struct snd_compr_stream *stream,
  250. struct snd_dma_buffer *bufp)
  251. {
  252. struct snd_compr_runtime *runtime = stream->runtime;
  253. if (bufp) {
  254. runtime->dma_buffer_p = bufp;
  255. runtime->dma_area = bufp->area;
  256. runtime->dma_addr = bufp->addr;
  257. runtime->dma_bytes = bufp->bytes;
  258. } else {
  259. runtime->dma_buffer_p = NULL;
  260. runtime->dma_area = NULL;
  261. runtime->dma_addr = 0;
  262. runtime->dma_bytes = 0;
  263. }
  264. }
  265. int snd_compr_malloc_pages(struct snd_compr_stream *stream, size_t size);
  266. int snd_compr_free_pages(struct snd_compr_stream *stream);
  267. int snd_compr_stop_error(struct snd_compr_stream *stream,
  268. snd_pcm_state_t state);
  269. #if IS_ENABLED(CONFIG_SND_COMPRESS_ACCEL)
  270. void snd_compr_task_finished(struct snd_compr_stream *stream,
  271. struct snd_compr_task_runtime *task);
  272. #endif
  273. #endif