pcm_iec958.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PCM DRM helpers
  4. */
  5. #include <linux/export.h>
  6. #include <linux/types.h>
  7. #include <sound/asoundef.h>
  8. #include <sound/pcm.h>
  9. #include <sound/pcm_params.h>
  10. #include <sound/pcm_iec958.h>
  11. /**
  12. * snd_pcm_create_iec958_consumer_default - create default consumer format IEC958 channel status
  13. * @cs: channel status buffer, at least four bytes
  14. * @len: length of channel status buffer
  15. *
  16. * Create the consumer format channel status data in @cs of maximum size
  17. * @len. When relevant, the configuration-dependant bits will be set as
  18. * unspecified.
  19. *
  20. * Drivers should then call einter snd_pcm_fill_iec958_consumer() or
  21. * snd_pcm_fill_iec958_consumer_hw_params() to replace these unspecified
  22. * bits by their actual values.
  23. *
  24. * Drivers may wish to tweak the contents of the buffer after creation.
  25. *
  26. * Returns: length of buffer, or negative error code if something failed.
  27. */
  28. int snd_pcm_create_iec958_consumer_default(u8 *cs, size_t len)
  29. {
  30. if (len < 4)
  31. return -EINVAL;
  32. memset(cs, 0, len);
  33. cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
  34. cs[1] = IEC958_AES1_CON_GENERAL;
  35. cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
  36. cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | IEC958_AES3_CON_FS_NOTID;
  37. if (len > 4)
  38. cs[4] = IEC958_AES4_CON_WORDLEN_NOTID;
  39. return len;
  40. }
  41. EXPORT_SYMBOL_GPL(snd_pcm_create_iec958_consumer_default);
  42. static int fill_iec958_consumer(uint rate, uint sample_width,
  43. u8 *cs, size_t len)
  44. {
  45. if (len < 4)
  46. return -EINVAL;
  47. if ((cs[3] & IEC958_AES3_CON_FS) == IEC958_AES3_CON_FS_NOTID) {
  48. unsigned int fs;
  49. switch (rate) {
  50. case 32000:
  51. fs = IEC958_AES3_CON_FS_32000;
  52. break;
  53. case 44100:
  54. fs = IEC958_AES3_CON_FS_44100;
  55. break;
  56. case 48000:
  57. fs = IEC958_AES3_CON_FS_48000;
  58. break;
  59. case 88200:
  60. fs = IEC958_AES3_CON_FS_88200;
  61. break;
  62. case 96000:
  63. fs = IEC958_AES3_CON_FS_96000;
  64. break;
  65. case 176400:
  66. fs = IEC958_AES3_CON_FS_176400;
  67. break;
  68. case 192000:
  69. fs = IEC958_AES3_CON_FS_192000;
  70. break;
  71. default:
  72. return -EINVAL;
  73. }
  74. cs[3] &= ~IEC958_AES3_CON_FS;
  75. cs[3] |= fs;
  76. }
  77. if (len > 4 &&
  78. (cs[4] & IEC958_AES4_CON_WORDLEN) == IEC958_AES4_CON_WORDLEN_NOTID) {
  79. unsigned int ws;
  80. switch (sample_width) {
  81. case 16:
  82. ws = IEC958_AES4_CON_WORDLEN_20_16;
  83. break;
  84. case 18:
  85. ws = IEC958_AES4_CON_WORDLEN_22_18;
  86. break;
  87. case 20:
  88. ws = IEC958_AES4_CON_WORDLEN_20_16 |
  89. IEC958_AES4_CON_MAX_WORDLEN_24;
  90. break;
  91. case 24:
  92. case 32: /* Assume 24-bit width for 32-bit samples. */
  93. ws = IEC958_AES4_CON_WORDLEN_24_20 |
  94. IEC958_AES4_CON_MAX_WORDLEN_24;
  95. break;
  96. default:
  97. return -EINVAL;
  98. }
  99. cs[4] &= ~IEC958_AES4_CON_WORDLEN;
  100. cs[4] |= ws;
  101. }
  102. return len;
  103. }
  104. /**
  105. * snd_pcm_fill_iec958_consumer - Fill consumer format IEC958 channel status
  106. * @runtime: pcm runtime structure with ->rate filled in
  107. * @cs: channel status buffer, at least four bytes
  108. * @len: length of channel status buffer
  109. *
  110. * Fill the unspecified bits in an IEC958 status bits array using the
  111. * parameters of the PCM runtime @runtime.
  112. *
  113. * Drivers may wish to tweak the contents of the buffer after its been
  114. * filled.
  115. *
  116. * Returns: length of buffer, or negative error code if something failed.
  117. */
  118. int snd_pcm_fill_iec958_consumer(struct snd_pcm_runtime *runtime,
  119. u8 *cs, size_t len)
  120. {
  121. return fill_iec958_consumer(runtime->rate,
  122. snd_pcm_format_width(runtime->format),
  123. cs, len);
  124. }
  125. EXPORT_SYMBOL_GPL(snd_pcm_fill_iec958_consumer);
  126. /**
  127. * snd_pcm_fill_iec958_consumer_hw_params - Fill consumer format IEC958 channel status
  128. * @params: the hw_params instance for extracting rate and sample format
  129. * @cs: channel status buffer, at least four bytes
  130. * @len: length of channel status buffer
  131. *
  132. * Fill the unspecified bits in an IEC958 status bits array using the
  133. * parameters of the PCM hardware parameters @params.
  134. *
  135. * Drivers may wish to tweak the contents of the buffer after its been
  136. * filled..
  137. *
  138. * Returns: length of buffer, or negative error code if something failed.
  139. */
  140. int snd_pcm_fill_iec958_consumer_hw_params(struct snd_pcm_hw_params *params,
  141. u8 *cs, size_t len)
  142. {
  143. return fill_iec958_consumer(params_rate(params), params_width(params), cs, len);
  144. }
  145. EXPORT_SYMBOL_GPL(snd_pcm_fill_iec958_consumer_hw_params);
  146. /**
  147. * snd_pcm_create_iec958_consumer - create consumer format IEC958 channel status
  148. * @runtime: pcm runtime structure with ->rate filled in
  149. * @cs: channel status buffer, at least four bytes
  150. * @len: length of channel status buffer
  151. *
  152. * Create the consumer format channel status data in @cs of maximum size
  153. * @len corresponding to the parameters of the PCM runtime @runtime.
  154. *
  155. * Drivers may wish to tweak the contents of the buffer after creation.
  156. *
  157. * Returns: length of buffer, or negative error code if something failed.
  158. */
  159. int snd_pcm_create_iec958_consumer(struct snd_pcm_runtime *runtime, u8 *cs,
  160. size_t len)
  161. {
  162. int ret;
  163. ret = snd_pcm_create_iec958_consumer_default(cs, len);
  164. if (ret < 0)
  165. return ret;
  166. return snd_pcm_fill_iec958_consumer(runtime, cs, len);
  167. }
  168. EXPORT_SYMBOL(snd_pcm_create_iec958_consumer);
  169. /**
  170. * snd_pcm_create_iec958_consumer_hw_params - create IEC958 channel status
  171. * @params: the hw_params instance for extracting rate and sample format
  172. * @cs: channel status buffer, at least four bytes
  173. * @len: length of channel status buffer
  174. *
  175. * Create the consumer format channel status data in @cs of maximum size
  176. * @len corresponding to the parameters of the PCM runtime @runtime.
  177. *
  178. * Drivers may wish to tweak the contents of the buffer after creation.
  179. *
  180. * Returns: length of buffer, or negative error code if something failed.
  181. */
  182. int snd_pcm_create_iec958_consumer_hw_params(struct snd_pcm_hw_params *params,
  183. u8 *cs, size_t len)
  184. {
  185. int ret;
  186. ret = snd_pcm_create_iec958_consumer_default(cs, len);
  187. if (ret < 0)
  188. return ret;
  189. return fill_iec958_consumer(params_rate(params), params_width(params), cs, len);
  190. }
  191. EXPORT_SYMBOL(snd_pcm_create_iec958_consumer_hw_params);