pcm_iec958.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * PCM DRM helpers
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/export.h>
  9. #include <linux/types.h>
  10. #include <sound/asoundef.h>
  11. #include <sound/pcm.h>
  12. #include <sound/pcm_params.h>
  13. #include <sound/pcm_iec958.h>
  14. static int create_iec958_consumer(uint rate, uint sample_width,
  15. u8 *cs, size_t len)
  16. {
  17. unsigned int fs, ws;
  18. if (len < 4)
  19. return -EINVAL;
  20. switch (rate) {
  21. case 32000:
  22. fs = IEC958_AES3_CON_FS_32000;
  23. break;
  24. case 44100:
  25. fs = IEC958_AES3_CON_FS_44100;
  26. break;
  27. case 48000:
  28. fs = IEC958_AES3_CON_FS_48000;
  29. break;
  30. case 88200:
  31. fs = IEC958_AES3_CON_FS_88200;
  32. break;
  33. case 96000:
  34. fs = IEC958_AES3_CON_FS_96000;
  35. break;
  36. case 176400:
  37. fs = IEC958_AES3_CON_FS_176400;
  38. break;
  39. case 192000:
  40. fs = IEC958_AES3_CON_FS_192000;
  41. break;
  42. default:
  43. return -EINVAL;
  44. }
  45. if (len > 4) {
  46. switch (sample_width) {
  47. case 16:
  48. ws = IEC958_AES4_CON_WORDLEN_20_16;
  49. break;
  50. case 18:
  51. ws = IEC958_AES4_CON_WORDLEN_22_18;
  52. break;
  53. case 20:
  54. ws = IEC958_AES4_CON_WORDLEN_20_16 |
  55. IEC958_AES4_CON_MAX_WORDLEN_24;
  56. break;
  57. case 24:
  58. case 32: /* Assume 24-bit width for 32-bit samples. */
  59. ws = IEC958_AES4_CON_WORDLEN_24_20 |
  60. IEC958_AES4_CON_MAX_WORDLEN_24;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. }
  66. memset(cs, 0, len);
  67. cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
  68. cs[1] = IEC958_AES1_CON_GENERAL;
  69. cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
  70. cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | fs;
  71. if (len > 4)
  72. cs[4] = ws;
  73. return len;
  74. }
  75. /**
  76. * snd_pcm_create_iec958_consumer - create consumer format IEC958 channel status
  77. * @runtime: pcm runtime structure with ->rate filled in
  78. * @cs: channel status buffer, at least four bytes
  79. * @len: length of channel status buffer
  80. *
  81. * Create the consumer format channel status data in @cs of maximum size
  82. * @len corresponding to the parameters of the PCM runtime @runtime.
  83. *
  84. * Drivers may wish to tweak the contents of the buffer after creation.
  85. *
  86. * Returns: length of buffer, or negative error code if something failed.
  87. */
  88. int snd_pcm_create_iec958_consumer(struct snd_pcm_runtime *runtime, u8 *cs,
  89. size_t len)
  90. {
  91. return create_iec958_consumer(runtime->rate,
  92. snd_pcm_format_width(runtime->format),
  93. cs, len);
  94. }
  95. EXPORT_SYMBOL(snd_pcm_create_iec958_consumer);
  96. /**
  97. * snd_pcm_create_iec958_consumer_hw_params - create IEC958 channel status
  98. * @hw_params: the hw_params instance for extracting rate and sample format
  99. * @cs: channel status buffer, at least four bytes
  100. * @len: length of channel status buffer
  101. *
  102. * Create the consumer format channel status data in @cs of maximum size
  103. * @len corresponding to the parameters of the PCM runtime @runtime.
  104. *
  105. * Drivers may wish to tweak the contents of the buffer after creation.
  106. *
  107. * Returns: length of buffer, or negative error code if something failed.
  108. */
  109. int snd_pcm_create_iec958_consumer_hw_params(struct snd_pcm_hw_params *params,
  110. u8 *cs, size_t len)
  111. {
  112. return create_iec958_consumer(params_rate(params), params_width(params),
  113. cs, len);
  114. }
  115. EXPORT_SYMBOL(snd_pcm_create_iec958_consumer_hw_params);