pcm_iec958.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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_iec958.h>
  13. /**
  14. * snd_pcm_create_iec958_consumer - create consumer format IEC958 channel status
  15. * @runtime: pcm runtime structure with ->rate filled in
  16. * @cs: channel status buffer, at least four bytes
  17. * @len: length of channel status buffer
  18. *
  19. * Create the consumer format channel status data in @cs of maximum size
  20. * @len corresponding to the parameters of the PCM runtime @runtime.
  21. *
  22. * Drivers may wish to tweak the contents of the buffer after creation.
  23. *
  24. * Returns: length of buffer, or negative error code if something failed.
  25. */
  26. int snd_pcm_create_iec958_consumer(struct snd_pcm_runtime *runtime, u8 *cs,
  27. size_t len)
  28. {
  29. unsigned int fs, ws;
  30. if (len < 4)
  31. return -EINVAL;
  32. switch (runtime->rate) {
  33. case 32000:
  34. fs = IEC958_AES3_CON_FS_32000;
  35. break;
  36. case 44100:
  37. fs = IEC958_AES3_CON_FS_44100;
  38. break;
  39. case 48000:
  40. fs = IEC958_AES3_CON_FS_48000;
  41. break;
  42. case 88200:
  43. fs = IEC958_AES3_CON_FS_88200;
  44. break;
  45. case 96000:
  46. fs = IEC958_AES3_CON_FS_96000;
  47. break;
  48. case 176400:
  49. fs = IEC958_AES3_CON_FS_176400;
  50. break;
  51. case 192000:
  52. fs = IEC958_AES3_CON_FS_192000;
  53. break;
  54. default:
  55. return -EINVAL;
  56. }
  57. if (len > 4) {
  58. switch (snd_pcm_format_width(runtime->format)) {
  59. case 16:
  60. ws = IEC958_AES4_CON_WORDLEN_20_16;
  61. break;
  62. case 18:
  63. ws = IEC958_AES4_CON_WORDLEN_22_18;
  64. break;
  65. case 20:
  66. ws = IEC958_AES4_CON_WORDLEN_20_16 |
  67. IEC958_AES4_CON_MAX_WORDLEN_24;
  68. break;
  69. case 24:
  70. ws = IEC958_AES4_CON_WORDLEN_24_20 |
  71. IEC958_AES4_CON_MAX_WORDLEN_24;
  72. break;
  73. default:
  74. return -EINVAL;
  75. }
  76. }
  77. memset(cs, 0, len);
  78. cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE;
  79. cs[1] = IEC958_AES1_CON_GENERAL;
  80. cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC;
  81. cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | fs;
  82. if (len > 4)
  83. cs[4] = ws;
  84. return len;
  85. }
  86. EXPORT_SYMBOL(snd_pcm_create_iec958_consumer);