compress_driver.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. */
  33. struct snd_compr_runtime {
  34. snd_pcm_state_t state;
  35. struct snd_compr_ops *ops;
  36. void *buffer;
  37. u64 buffer_size;
  38. u32 fragment_size;
  39. u32 fragments;
  40. u64 total_bytes_available;
  41. u64 total_bytes_transferred;
  42. wait_queue_head_t sleep;
  43. void *private_data;
  44. };
  45. /**
  46. * struct snd_compr_stream: compressed stream
  47. * @name: device name
  48. * @ops: pointer to DSP callbacks
  49. * @runtime: pointer to runtime structure
  50. * @device: device pointer
  51. * @error_work: delayed work used when closing the stream due to an error
  52. * @direction: stream direction, playback/recording
  53. * @metadata_set: metadata set flag, true when set
  54. * @next_track: has userspace signal next track transition, true when set
  55. * @partial_drain: undergoing partial_drain for stream, true when set
  56. * @private_data: pointer to DSP private data
  57. */
  58. struct snd_compr_stream {
  59. const char *name;
  60. struct snd_compr_ops *ops;
  61. struct snd_compr_runtime *runtime;
  62. struct snd_compr *device;
  63. struct delayed_work error_work;
  64. enum snd_compr_direction direction;
  65. bool metadata_set;
  66. bool next_track;
  67. bool partial_drain;
  68. void *private_data;
  69. };
  70. /**
  71. * struct snd_compr_ops: compressed path DSP operations
  72. * @open: Open the compressed stream
  73. * This callback is mandatory and shall keep dsp ready to receive the stream
  74. * parameter
  75. * @free: Close the compressed stream, mandatory
  76. * @set_params: Sets the compressed stream parameters, mandatory
  77. * This can be called in during stream creation only to set codec params
  78. * and the stream properties
  79. * @get_params: retrieve the codec parameters, mandatory
  80. * @set_metadata: Set the metadata values for a stream
  81. * @get_metadata: retrieves the requested metadata values from stream
  82. * @trigger: Trigger operations like start, pause, resume, drain, stop.
  83. * This callback is mandatory
  84. * @pointer: Retrieve current h/w pointer information. Mandatory
  85. * @copy: Copy the compressed data to/from userspace, Optional
  86. * Can't be implemented if DSP supports mmap
  87. * @mmap: DSP mmap method to mmap DSP memory
  88. * @ack: Ack for DSP when data is written to audio buffer, Optional
  89. * Not valid if copy is implemented
  90. * @get_caps: Retrieve DSP capabilities, mandatory
  91. * @get_codec_caps: Retrieve capabilities for a specific codec, mandatory
  92. */
  93. struct snd_compr_ops {
  94. int (*open)(struct snd_compr_stream *stream);
  95. int (*free)(struct snd_compr_stream *stream);
  96. int (*set_params)(struct snd_compr_stream *stream,
  97. struct snd_compr_params *params);
  98. int (*get_params)(struct snd_compr_stream *stream,
  99. struct snd_codec *params);
  100. int (*set_metadata)(struct snd_compr_stream *stream,
  101. struct snd_compr_metadata *metadata);
  102. int (*get_metadata)(struct snd_compr_stream *stream,
  103. struct snd_compr_metadata *metadata);
  104. int (*trigger)(struct snd_compr_stream *stream, int cmd);
  105. int (*pointer)(struct snd_compr_stream *stream,
  106. struct snd_compr_tstamp *tstamp);
  107. int (*copy)(struct snd_compr_stream *stream, char __user *buf,
  108. size_t count);
  109. int (*mmap)(struct snd_compr_stream *stream,
  110. struct vm_area_struct *vma);
  111. int (*ack)(struct snd_compr_stream *stream, size_t bytes);
  112. int (*get_caps) (struct snd_compr_stream *stream,
  113. struct snd_compr_caps *caps);
  114. int (*get_codec_caps) (struct snd_compr_stream *stream,
  115. struct snd_compr_codec_caps *codec);
  116. };
  117. /**
  118. * struct snd_compr: Compressed device
  119. * @name: DSP device name
  120. * @dev: associated device instance
  121. * @ops: pointer to DSP callbacks
  122. * @private_data: pointer to DSP pvt data
  123. * @card: sound card pointer
  124. * @direction: Playback or capture direction
  125. * @lock: device lock
  126. * @device: device id
  127. */
  128. struct snd_compr {
  129. const char *name;
  130. struct device dev;
  131. struct snd_compr_ops *ops;
  132. void *private_data;
  133. struct snd_card *card;
  134. unsigned int direction;
  135. struct mutex lock;
  136. int device;
  137. #ifdef CONFIG_SND_VERBOSE_PROCFS
  138. /* private: */
  139. char id[64];
  140. struct snd_info_entry *proc_root;
  141. struct snd_info_entry *proc_info_entry;
  142. #endif
  143. };
  144. /* compress device register APIs */
  145. int snd_compress_register(struct snd_compr *device);
  146. int snd_compress_deregister(struct snd_compr *device);
  147. int snd_compress_new(struct snd_card *card, int device,
  148. int type, const char *id, struct snd_compr *compr);
  149. /* dsp driver callback apis
  150. * For playback: driver should call snd_compress_fragment_elapsed() to let the
  151. * framework know that a fragment has been consumed from the ring buffer
  152. *
  153. * For recording: we want to know when a frame is available or when
  154. * at least one frame is available so snd_compress_frame_elapsed()
  155. * callback should be called when a encodeded frame is available
  156. */
  157. static inline void snd_compr_fragment_elapsed(struct snd_compr_stream *stream)
  158. {
  159. wake_up(&stream->runtime->sleep);
  160. }
  161. static inline void snd_compr_drain_notify(struct snd_compr_stream *stream)
  162. {
  163. if (snd_BUG_ON(!stream))
  164. return;
  165. /* for partial_drain case we are back to running state on success */
  166. if (stream->partial_drain) {
  167. stream->runtime->state = SNDRV_PCM_STATE_RUNNING;
  168. stream->partial_drain = false; /* clear this flag as well */
  169. } else {
  170. stream->runtime->state = SNDRV_PCM_STATE_SETUP;
  171. }
  172. wake_up(&stream->runtime->sleep);
  173. }
  174. int snd_compr_stop_error(struct snd_compr_stream *stream,
  175. snd_pcm_state_t state);
  176. #endif