18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * PCM DRM helpers 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci#include <linux/export.h> 68c2ecf20Sopenharmony_ci#include <linux/types.h> 78c2ecf20Sopenharmony_ci#include <sound/asoundef.h> 88c2ecf20Sopenharmony_ci#include <sound/pcm.h> 98c2ecf20Sopenharmony_ci#include <sound/pcm_params.h> 108c2ecf20Sopenharmony_ci#include <sound/pcm_iec958.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_cistatic int create_iec958_consumer(uint rate, uint sample_width, 138c2ecf20Sopenharmony_ci u8 *cs, size_t len) 148c2ecf20Sopenharmony_ci{ 158c2ecf20Sopenharmony_ci unsigned int fs, ws; 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci if (len < 4) 188c2ecf20Sopenharmony_ci return -EINVAL; 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci switch (rate) { 218c2ecf20Sopenharmony_ci case 32000: 228c2ecf20Sopenharmony_ci fs = IEC958_AES3_CON_FS_32000; 238c2ecf20Sopenharmony_ci break; 248c2ecf20Sopenharmony_ci case 44100: 258c2ecf20Sopenharmony_ci fs = IEC958_AES3_CON_FS_44100; 268c2ecf20Sopenharmony_ci break; 278c2ecf20Sopenharmony_ci case 48000: 288c2ecf20Sopenharmony_ci fs = IEC958_AES3_CON_FS_48000; 298c2ecf20Sopenharmony_ci break; 308c2ecf20Sopenharmony_ci case 88200: 318c2ecf20Sopenharmony_ci fs = IEC958_AES3_CON_FS_88200; 328c2ecf20Sopenharmony_ci break; 338c2ecf20Sopenharmony_ci case 96000: 348c2ecf20Sopenharmony_ci fs = IEC958_AES3_CON_FS_96000; 358c2ecf20Sopenharmony_ci break; 368c2ecf20Sopenharmony_ci case 176400: 378c2ecf20Sopenharmony_ci fs = IEC958_AES3_CON_FS_176400; 388c2ecf20Sopenharmony_ci break; 398c2ecf20Sopenharmony_ci case 192000: 408c2ecf20Sopenharmony_ci fs = IEC958_AES3_CON_FS_192000; 418c2ecf20Sopenharmony_ci break; 428c2ecf20Sopenharmony_ci default: 438c2ecf20Sopenharmony_ci return -EINVAL; 448c2ecf20Sopenharmony_ci } 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci if (len > 4) { 478c2ecf20Sopenharmony_ci switch (sample_width) { 488c2ecf20Sopenharmony_ci case 16: 498c2ecf20Sopenharmony_ci ws = IEC958_AES4_CON_WORDLEN_20_16; 508c2ecf20Sopenharmony_ci break; 518c2ecf20Sopenharmony_ci case 18: 528c2ecf20Sopenharmony_ci ws = IEC958_AES4_CON_WORDLEN_22_18; 538c2ecf20Sopenharmony_ci break; 548c2ecf20Sopenharmony_ci case 20: 558c2ecf20Sopenharmony_ci ws = IEC958_AES4_CON_WORDLEN_20_16 | 568c2ecf20Sopenharmony_ci IEC958_AES4_CON_MAX_WORDLEN_24; 578c2ecf20Sopenharmony_ci break; 588c2ecf20Sopenharmony_ci case 24: 598c2ecf20Sopenharmony_ci case 32: /* Assume 24-bit width for 32-bit samples. */ 608c2ecf20Sopenharmony_ci ws = IEC958_AES4_CON_WORDLEN_24_20 | 618c2ecf20Sopenharmony_ci IEC958_AES4_CON_MAX_WORDLEN_24; 628c2ecf20Sopenharmony_ci break; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci default: 658c2ecf20Sopenharmony_ci return -EINVAL; 668c2ecf20Sopenharmony_ci } 678c2ecf20Sopenharmony_ci } 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci memset(cs, 0, len); 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ci cs[0] = IEC958_AES0_CON_NOT_COPYRIGHT | IEC958_AES0_CON_EMPHASIS_NONE; 728c2ecf20Sopenharmony_ci cs[1] = IEC958_AES1_CON_GENERAL; 738c2ecf20Sopenharmony_ci cs[2] = IEC958_AES2_CON_SOURCE_UNSPEC | IEC958_AES2_CON_CHANNEL_UNSPEC; 748c2ecf20Sopenharmony_ci cs[3] = IEC958_AES3_CON_CLOCK_1000PPM | fs; 758c2ecf20Sopenharmony_ci 768c2ecf20Sopenharmony_ci if (len > 4) 778c2ecf20Sopenharmony_ci cs[4] = ws; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci return len; 808c2ecf20Sopenharmony_ci} 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci/** 838c2ecf20Sopenharmony_ci * snd_pcm_create_iec958_consumer - create consumer format IEC958 channel status 848c2ecf20Sopenharmony_ci * @runtime: pcm runtime structure with ->rate filled in 858c2ecf20Sopenharmony_ci * @cs: channel status buffer, at least four bytes 868c2ecf20Sopenharmony_ci * @len: length of channel status buffer 878c2ecf20Sopenharmony_ci * 888c2ecf20Sopenharmony_ci * Create the consumer format channel status data in @cs of maximum size 898c2ecf20Sopenharmony_ci * @len corresponding to the parameters of the PCM runtime @runtime. 908c2ecf20Sopenharmony_ci * 918c2ecf20Sopenharmony_ci * Drivers may wish to tweak the contents of the buffer after creation. 928c2ecf20Sopenharmony_ci * 938c2ecf20Sopenharmony_ci * Returns: length of buffer, or negative error code if something failed. 948c2ecf20Sopenharmony_ci */ 958c2ecf20Sopenharmony_ciint snd_pcm_create_iec958_consumer(struct snd_pcm_runtime *runtime, u8 *cs, 968c2ecf20Sopenharmony_ci size_t len) 978c2ecf20Sopenharmony_ci{ 988c2ecf20Sopenharmony_ci return create_iec958_consumer(runtime->rate, 998c2ecf20Sopenharmony_ci snd_pcm_format_width(runtime->format), 1008c2ecf20Sopenharmony_ci cs, len); 1018c2ecf20Sopenharmony_ci} 1028c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_pcm_create_iec958_consumer); 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci/** 1058c2ecf20Sopenharmony_ci * snd_pcm_create_iec958_consumer_hw_params - create IEC958 channel status 1068c2ecf20Sopenharmony_ci * @params: the hw_params instance for extracting rate and sample format 1078c2ecf20Sopenharmony_ci * @cs: channel status buffer, at least four bytes 1088c2ecf20Sopenharmony_ci * @len: length of channel status buffer 1098c2ecf20Sopenharmony_ci * 1108c2ecf20Sopenharmony_ci * Create the consumer format channel status data in @cs of maximum size 1118c2ecf20Sopenharmony_ci * @len corresponding to the parameters of the PCM runtime @runtime. 1128c2ecf20Sopenharmony_ci * 1138c2ecf20Sopenharmony_ci * Drivers may wish to tweak the contents of the buffer after creation. 1148c2ecf20Sopenharmony_ci * 1158c2ecf20Sopenharmony_ci * Returns: length of buffer, or negative error code if something failed. 1168c2ecf20Sopenharmony_ci */ 1178c2ecf20Sopenharmony_ciint snd_pcm_create_iec958_consumer_hw_params(struct snd_pcm_hw_params *params, 1188c2ecf20Sopenharmony_ci u8 *cs, size_t len) 1198c2ecf20Sopenharmony_ci{ 1208c2ecf20Sopenharmony_ci return create_iec958_consumer(params_rate(params), params_width(params), 1218c2ecf20Sopenharmony_ci cs, len); 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ciEXPORT_SYMBOL(snd_pcm_create_iec958_consumer_hw_params); 124