1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * PCM DRM helpers
4 */
5 #include <linux/bitfield.h>
6 #include <linux/export.h>
7 #include <linux/hdmi.h>
8 #include <linux/unaligned.h>
9 #include <drm/drm_edid.h>
10 #include <drm/drm_eld.h>
11 #include <sound/info.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_drm_eld.h>
14
15 #define SAD0_CHANNELS_MASK GENMASK(2, 0) /* max number of channels - 1 */
16 #define SAD0_FORMAT_MASK GENMASK(6, 3) /* audio format */
17
18 #define SAD1_RATE_MASK GENMASK(6, 0) /* bitfield of supported rates */
19 #define SAD1_RATE_32000_MASK BIT(0)
20 #define SAD1_RATE_44100_MASK BIT(1)
21 #define SAD1_RATE_48000_MASK BIT(2)
22 #define SAD1_RATE_88200_MASK BIT(3)
23 #define SAD1_RATE_96000_MASK BIT(4)
24 #define SAD1_RATE_176400_MASK BIT(5)
25 #define SAD1_RATE_192000_MASK BIT(6)
26
27 static const unsigned int eld_rates[] = {
28 32000,
29 44100,
30 48000,
31 88200,
32 96000,
33 176400,
34 192000,
35 };
36
map_rate_families(const u8 * sad,unsigned int mask_32000,unsigned int mask_44100,unsigned int mask_48000)37 static unsigned int map_rate_families(const u8 *sad,
38 unsigned int mask_32000,
39 unsigned int mask_44100,
40 unsigned int mask_48000)
41 {
42 unsigned int rate_mask = 0;
43
44 if (sad[1] & SAD1_RATE_32000_MASK)
45 rate_mask |= mask_32000;
46 if (sad[1] & (SAD1_RATE_44100_MASK | SAD1_RATE_88200_MASK | SAD1_RATE_176400_MASK))
47 rate_mask |= mask_44100;
48 if (sad[1] & (SAD1_RATE_48000_MASK | SAD1_RATE_96000_MASK | SAD1_RATE_192000_MASK))
49 rate_mask |= mask_48000;
50 return rate_mask;
51 }
52
sad_rate_mask(const u8 * sad)53 static unsigned int sad_rate_mask(const u8 *sad)
54 {
55 switch (FIELD_GET(SAD0_FORMAT_MASK, sad[0])) {
56 case HDMI_AUDIO_CODING_TYPE_PCM:
57 return sad[1] & SAD1_RATE_MASK;
58 case HDMI_AUDIO_CODING_TYPE_AC3:
59 case HDMI_AUDIO_CODING_TYPE_DTS:
60 return map_rate_families(sad,
61 SAD1_RATE_32000_MASK,
62 SAD1_RATE_44100_MASK,
63 SAD1_RATE_48000_MASK);
64 case HDMI_AUDIO_CODING_TYPE_EAC3:
65 case HDMI_AUDIO_CODING_TYPE_DTS_HD:
66 case HDMI_AUDIO_CODING_TYPE_MLP:
67 return map_rate_families(sad,
68 0,
69 SAD1_RATE_176400_MASK,
70 SAD1_RATE_192000_MASK);
71 default:
72 /* TODO adjust for other compressed formats as well */
73 return sad[1] & SAD1_RATE_MASK;
74 }
75 }
76
sad_max_channels(const u8 * sad)77 static unsigned int sad_max_channels(const u8 *sad)
78 {
79 switch (FIELD_GET(SAD0_FORMAT_MASK, sad[0])) {
80 case HDMI_AUDIO_CODING_TYPE_PCM:
81 return 1 + FIELD_GET(SAD0_CHANNELS_MASK, sad[0]);
82 case HDMI_AUDIO_CODING_TYPE_AC3:
83 case HDMI_AUDIO_CODING_TYPE_DTS:
84 case HDMI_AUDIO_CODING_TYPE_EAC3:
85 return 2;
86 case HDMI_AUDIO_CODING_TYPE_DTS_HD:
87 case HDMI_AUDIO_CODING_TYPE_MLP:
88 return 8;
89 default:
90 /* TODO adjust for other compressed formats as well */
91 return 1 + FIELD_GET(SAD0_CHANNELS_MASK, sad[0]);
92 }
93 }
94
eld_limit_rates(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)95 static int eld_limit_rates(struct snd_pcm_hw_params *params,
96 struct snd_pcm_hw_rule *rule)
97 {
98 struct snd_interval *r = hw_param_interval(params, rule->var);
99 const struct snd_interval *c;
100 unsigned int rate_mask = 7, i;
101 const u8 *sad, *eld = rule->private;
102
103 sad = drm_eld_sad(eld);
104 if (sad) {
105 c = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_CHANNELS);
106
107 for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3) {
108 unsigned max_channels = sad_max_channels(sad);
109
110 /*
111 * Exclude SADs which do not include the
112 * requested number of channels.
113 */
114 if (c->min <= max_channels)
115 rate_mask |= sad_rate_mask(sad);
116 }
117 }
118
119 return snd_interval_list(r, ARRAY_SIZE(eld_rates), eld_rates,
120 rate_mask);
121 }
122
eld_limit_channels(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)123 static int eld_limit_channels(struct snd_pcm_hw_params *params,
124 struct snd_pcm_hw_rule *rule)
125 {
126 struct snd_interval *c = hw_param_interval(params, rule->var);
127 const struct snd_interval *r;
128 struct snd_interval t = { .min = 1, .max = 2, .integer = 1, };
129 unsigned int i;
130 const u8 *sad, *eld = rule->private;
131
132 sad = drm_eld_sad(eld);
133 if (sad) {
134 unsigned int rate_mask = 0;
135
136 /* Convert the rate interval to a mask */
137 r = hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
138 for (i = 0; i < ARRAY_SIZE(eld_rates); i++)
139 if (r->min <= eld_rates[i] && r->max >= eld_rates[i])
140 rate_mask |= BIT(i);
141
142 for (i = drm_eld_sad_count(eld); i > 0; i--, sad += 3)
143 if (rate_mask & sad_rate_mask(sad))
144 t.max = max(t.max, sad_max_channels(sad));
145 }
146
147 return snd_interval_refine(c, &t);
148 }
149
snd_pcm_hw_constraint_eld(struct snd_pcm_runtime * runtime,void * eld)150 int snd_pcm_hw_constraint_eld(struct snd_pcm_runtime *runtime, void *eld)
151 {
152 int ret;
153
154 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
155 eld_limit_rates, eld,
156 SNDRV_PCM_HW_PARAM_CHANNELS, -1);
157 if (ret < 0)
158 return ret;
159
160 ret = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
161 eld_limit_channels, eld,
162 SNDRV_PCM_HW_PARAM_RATE, -1);
163
164 return ret;
165 }
166 EXPORT_SYMBOL_GPL(snd_pcm_hw_constraint_eld);
167
168 #define SND_PRINT_RATES_ADVISED_BUFSIZE 80
169 #define SND_PRINT_BITS_ADVISED_BUFSIZE 16
170 #define SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE 80
171
172 static const char * const eld_connection_type_names[4] = {
173 "HDMI",
174 "DisplayPort",
175 "2-reserved",
176 "3-reserved"
177 };
178
179 static const char * const cea_audio_coding_type_names[] = {
180 /* 0 */ "undefined",
181 /* 1 */ "LPCM",
182 /* 2 */ "AC-3",
183 /* 3 */ "MPEG1",
184 /* 4 */ "MP3",
185 /* 5 */ "MPEG2",
186 /* 6 */ "AAC-LC",
187 /* 7 */ "DTS",
188 /* 8 */ "ATRAC",
189 /* 9 */ "DSD (One Bit Audio)",
190 /* 10 */ "E-AC-3/DD+ (Dolby Digital Plus)",
191 /* 11 */ "DTS-HD",
192 /* 12 */ "MLP (Dolby TrueHD)",
193 /* 13 */ "DST",
194 /* 14 */ "WMAPro",
195 /* 15 */ "HE-AAC",
196 /* 16 */ "HE-AACv2",
197 /* 17 */ "MPEG Surround",
198 };
199
200 static const char * const cea_speaker_allocation_names[] = {
201 /* 0 */ "FL/FR",
202 /* 1 */ "LFE",
203 /* 2 */ "FC",
204 /* 3 */ "RL/RR",
205 /* 4 */ "RC",
206 /* 5 */ "FLC/FRC",
207 /* 6 */ "RLC/RRC",
208 /* 7 */ "FLW/FRW",
209 /* 8 */ "FLH/FRH",
210 /* 9 */ "TC",
211 /* 10 */ "FCH",
212 };
213
214 /*
215 * SS1:SS0 index => sample size
216 */
217 static const int cea_sample_sizes[4] = {
218 0, /* 0: Refer to Stream Header */
219 ELD_PCM_BITS_16, /* 1: 16 bits */
220 ELD_PCM_BITS_20, /* 2: 20 bits */
221 ELD_PCM_BITS_24, /* 3: 24 bits */
222 };
223
224 /*
225 * SF2:SF1:SF0 index => sampling frequency
226 */
227 static const int cea_sampling_frequencies[8] = {
228 0, /* 0: Refer to Stream Header */
229 SNDRV_PCM_RATE_32000, /* 1: 32000Hz */
230 SNDRV_PCM_RATE_44100, /* 2: 44100Hz */
231 SNDRV_PCM_RATE_48000, /* 3: 48000Hz */
232 SNDRV_PCM_RATE_88200, /* 4: 88200Hz */
233 SNDRV_PCM_RATE_96000, /* 5: 96000Hz */
234 SNDRV_PCM_RATE_176400, /* 6: 176400Hz */
235 SNDRV_PCM_RATE_192000, /* 7: 192000Hz */
236 };
237
238 #define GRAB_BITS(buf, byte, lowbit, bits) \
239 ({ \
240 BUILD_BUG_ON(lowbit > 7); \
241 BUILD_BUG_ON(bits > 8); \
242 BUILD_BUG_ON(bits <= 0); \
243 \
244 (buf[byte] >> (lowbit)) & ((1 << (bits)) - 1); \
245 })
246
hdmi_update_short_audio_desc(struct device * dev,struct snd_cea_sad * a,const unsigned char * buf)247 static void hdmi_update_short_audio_desc(struct device *dev,
248 struct snd_cea_sad *a,
249 const unsigned char *buf)
250 {
251 int i;
252 int val;
253
254 val = GRAB_BITS(buf, 1, 0, 7);
255 a->rates = 0;
256 for (i = 0; i < 7; i++)
257 if (val & (1 << i))
258 a->rates |= cea_sampling_frequencies[i + 1];
259
260 a->channels = GRAB_BITS(buf, 0, 0, 3);
261 a->channels++;
262
263 a->sample_bits = 0;
264 a->max_bitrate = 0;
265
266 a->format = GRAB_BITS(buf, 0, 3, 4);
267 switch (a->format) {
268 case AUDIO_CODING_TYPE_REF_STREAM_HEADER:
269 dev_info(dev, "HDMI: audio coding type 0 not expected\n");
270 break;
271
272 case AUDIO_CODING_TYPE_LPCM:
273 val = GRAB_BITS(buf, 2, 0, 3);
274 for (i = 0; i < 3; i++)
275 if (val & (1 << i))
276 a->sample_bits |= cea_sample_sizes[i + 1];
277 break;
278
279 case AUDIO_CODING_TYPE_AC3:
280 case AUDIO_CODING_TYPE_MPEG1:
281 case AUDIO_CODING_TYPE_MP3:
282 case AUDIO_CODING_TYPE_MPEG2:
283 case AUDIO_CODING_TYPE_AACLC:
284 case AUDIO_CODING_TYPE_DTS:
285 case AUDIO_CODING_TYPE_ATRAC:
286 a->max_bitrate = GRAB_BITS(buf, 2, 0, 8);
287 a->max_bitrate *= 8000;
288 break;
289
290 case AUDIO_CODING_TYPE_SACD:
291 break;
292
293 case AUDIO_CODING_TYPE_EAC3:
294 break;
295
296 case AUDIO_CODING_TYPE_DTS_HD:
297 break;
298
299 case AUDIO_CODING_TYPE_MLP:
300 break;
301
302 case AUDIO_CODING_TYPE_DST:
303 break;
304
305 case AUDIO_CODING_TYPE_WMAPRO:
306 a->profile = GRAB_BITS(buf, 2, 0, 3);
307 break;
308
309 case AUDIO_CODING_TYPE_REF_CXT:
310 a->format = GRAB_BITS(buf, 2, 3, 5);
311 if (a->format == AUDIO_CODING_XTYPE_HE_REF_CT ||
312 a->format >= AUDIO_CODING_XTYPE_FIRST_RESERVED) {
313 dev_info(dev,
314 "HDMI: audio coding xtype %d not expected\n",
315 a->format);
316 a->format = 0;
317 } else
318 a->format += AUDIO_CODING_TYPE_HE_AAC -
319 AUDIO_CODING_XTYPE_HE_AAC;
320 break;
321 }
322 }
323
324 /*
325 * Be careful, ELD buf could be totally rubbish!
326 */
snd_parse_eld(struct device * dev,struct snd_parsed_hdmi_eld * e,const unsigned char * buf,int size)327 int snd_parse_eld(struct device *dev, struct snd_parsed_hdmi_eld *e,
328 const unsigned char *buf, int size)
329 {
330 int mnl;
331 int i;
332
333 memset(e, 0, sizeof(*e));
334 e->eld_ver = GRAB_BITS(buf, 0, 3, 5);
335 if (e->eld_ver != ELD_VER_CEA_861D &&
336 e->eld_ver != ELD_VER_PARTIAL) {
337 dev_info(dev, "HDMI: Unknown ELD version %d\n", e->eld_ver);
338 goto out_fail;
339 }
340
341 e->baseline_len = GRAB_BITS(buf, 2, 0, 8);
342 mnl = GRAB_BITS(buf, 4, 0, 5);
343 e->cea_edid_ver = GRAB_BITS(buf, 4, 5, 3);
344
345 e->support_hdcp = GRAB_BITS(buf, 5, 0, 1);
346 e->support_ai = GRAB_BITS(buf, 5, 1, 1);
347 e->conn_type = GRAB_BITS(buf, 5, 2, 2);
348 e->sad_count = GRAB_BITS(buf, 5, 4, 4);
349
350 e->aud_synch_delay = GRAB_BITS(buf, 6, 0, 8) * 2;
351 e->spk_alloc = GRAB_BITS(buf, 7, 0, 7);
352
353 e->port_id = get_unaligned_le64(buf + 8);
354
355 /* not specified, but the spec's tendency is little endian */
356 e->manufacture_id = get_unaligned_le16(buf + 16);
357 e->product_id = get_unaligned_le16(buf + 18);
358
359 if (mnl > ELD_MAX_MNL) {
360 dev_info(dev, "HDMI: MNL is reserved value %d\n", mnl);
361 goto out_fail;
362 } else if (ELD_FIXED_BYTES + mnl > size) {
363 dev_info(dev, "HDMI: out of range MNL %d\n", mnl);
364 goto out_fail;
365 } else
366 strscpy(e->monitor_name, buf + ELD_FIXED_BYTES, mnl + 1);
367
368 for (i = 0; i < e->sad_count; i++) {
369 if (ELD_FIXED_BYTES + mnl + 3 * (i + 1) > size) {
370 dev_info(dev, "HDMI: out of range SAD %d\n", i);
371 goto out_fail;
372 }
373 hdmi_update_short_audio_desc(dev, e->sad + i,
374 buf + ELD_FIXED_BYTES + mnl + 3 * i);
375 }
376
377 /*
378 * HDMI sink's ELD info cannot always be retrieved for now, e.g.
379 * in console or for audio devices. Assume the highest speakers
380 * configuration, to _not_ prohibit multi-channel audio playback.
381 */
382 if (!e->spk_alloc)
383 e->spk_alloc = 0xffff;
384
385 return 0;
386
387 out_fail:
388 return -EINVAL;
389 }
390 EXPORT_SYMBOL_GPL(snd_parse_eld);
391
392 /*
393 * SNDRV_PCM_RATE_* and AC_PAR_PCM values don't match, print correct rates with
394 * hdmi-specific routine.
395 */
hdmi_print_pcm_rates(int pcm,char * buf,int buflen)396 static void hdmi_print_pcm_rates(int pcm, char *buf, int buflen)
397 {
398 static const unsigned int alsa_rates[] = {
399 5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
400 88200, 96000, 176400, 192000, 384000
401 };
402 int i, j;
403
404 for (i = 0, j = 0; i < ARRAY_SIZE(alsa_rates); i++)
405 if (pcm & (1 << i))
406 j += scnprintf(buf + j, buflen - j, " %d",
407 alsa_rates[i]);
408
409 buf[j] = '\0'; /* necessary when j == 0 */
410 }
411
eld_print_pcm_bits(int pcm,char * buf,int buflen)412 static void eld_print_pcm_bits(int pcm, char *buf, int buflen)
413 {
414 static const unsigned int bits[] = { 8, 16, 20, 24, 32 };
415 int i, j;
416
417 for (i = 0, j = 0; i < ARRAY_SIZE(bits); i++)
418 if (pcm & (ELD_PCM_BITS_8 << i))
419 j += scnprintf(buf + j, buflen - j, " %d", bits[i]);
420
421 buf[j] = '\0'; /* necessary when j == 0 */
422 }
423
hdmi_show_short_audio_desc(struct device * dev,struct snd_cea_sad * a)424 static void hdmi_show_short_audio_desc(struct device *dev,
425 struct snd_cea_sad *a)
426 {
427 char buf[SND_PRINT_RATES_ADVISED_BUFSIZE];
428 char buf2[8 + SND_PRINT_BITS_ADVISED_BUFSIZE] = ", bits =";
429
430 if (!a->format)
431 return;
432
433 hdmi_print_pcm_rates(a->rates, buf, sizeof(buf));
434
435 if (a->format == AUDIO_CODING_TYPE_LPCM)
436 eld_print_pcm_bits(a->sample_bits, buf2 + 8, sizeof(buf2) - 8);
437 else if (a->max_bitrate)
438 snprintf(buf2, sizeof(buf2),
439 ", max bitrate = %d", a->max_bitrate);
440 else
441 buf2[0] = '\0';
442
443 dev_dbg(dev,
444 "HDMI: supports coding type %s: channels = %d, rates =%s%s\n",
445 cea_audio_coding_type_names[a->format],
446 a->channels, buf, buf2);
447 }
448
snd_eld_print_channel_allocation(int spk_alloc,char * buf,int buflen)449 static void snd_eld_print_channel_allocation(int spk_alloc, char *buf, int buflen)
450 {
451 int i, j;
452
453 for (i = 0, j = 0; i < ARRAY_SIZE(cea_speaker_allocation_names); i++) {
454 if (spk_alloc & (1 << i))
455 j += scnprintf(buf + j, buflen - j, " %s",
456 cea_speaker_allocation_names[i]);
457 }
458 buf[j] = '\0'; /* necessary when j == 0 */
459 }
460
snd_show_eld(struct device * dev,struct snd_parsed_hdmi_eld * e)461 void snd_show_eld(struct device *dev, struct snd_parsed_hdmi_eld *e)
462 {
463 int i;
464
465 dev_dbg(dev, "HDMI: detected monitor %s at connection type %s\n",
466 e->monitor_name,
467 eld_connection_type_names[e->conn_type]);
468
469 if (e->spk_alloc) {
470 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
471
472 snd_eld_print_channel_allocation(e->spk_alloc, buf, sizeof(buf));
473 dev_dbg(dev, "HDMI: available speakers:%s\n", buf);
474 }
475
476 for (i = 0; i < e->sad_count; i++)
477 hdmi_show_short_audio_desc(dev, e->sad + i);
478 }
479 EXPORT_SYMBOL_GPL(snd_show_eld);
480
481 #ifdef CONFIG_SND_PROC_FS
hdmi_print_sad_info(int i,struct snd_cea_sad * a,struct snd_info_buffer * buffer)482 static void hdmi_print_sad_info(int i, struct snd_cea_sad *a,
483 struct snd_info_buffer *buffer)
484 {
485 char buf[SND_PRINT_RATES_ADVISED_BUFSIZE];
486
487 snd_iprintf(buffer, "sad%d_coding_type\t[0x%x] %s\n",
488 i, a->format, cea_audio_coding_type_names[a->format]);
489 snd_iprintf(buffer, "sad%d_channels\t\t%d\n", i, a->channels);
490
491 hdmi_print_pcm_rates(a->rates, buf, sizeof(buf));
492 snd_iprintf(buffer, "sad%d_rates\t\t[0x%x]%s\n", i, a->rates, buf);
493
494 if (a->format == AUDIO_CODING_TYPE_LPCM) {
495 eld_print_pcm_bits(a->sample_bits, buf, sizeof(buf));
496 snd_iprintf(buffer, "sad%d_bits\t\t[0x%x]%s\n",
497 i, a->sample_bits, buf);
498 }
499
500 if (a->max_bitrate)
501 snd_iprintf(buffer, "sad%d_max_bitrate\t%d\n",
502 i, a->max_bitrate);
503
504 if (a->profile)
505 snd_iprintf(buffer, "sad%d_profile\t\t%d\n", i, a->profile);
506 }
507
snd_print_eld_info(struct snd_parsed_hdmi_eld * e,struct snd_info_buffer * buffer)508 void snd_print_eld_info(struct snd_parsed_hdmi_eld *e,
509 struct snd_info_buffer *buffer)
510 {
511 char buf[SND_PRINT_CHANNEL_ALLOCATION_ADVISED_BUFSIZE];
512 int i;
513 static const char * const eld_version_names[32] = {
514 "reserved",
515 "reserved",
516 "CEA-861D or below",
517 [3 ... 30] = "reserved",
518 [31] = "partial"
519 };
520 static const char * const cea_edid_version_names[8] = {
521 "no CEA EDID Timing Extension block present",
522 "CEA-861",
523 "CEA-861-A",
524 "CEA-861-B, C or D",
525 [4 ... 7] = "reserved"
526 };
527
528 snd_iprintf(buffer, "monitor_name\t\t%s\n", e->monitor_name);
529 snd_iprintf(buffer, "connection_type\t\t%s\n",
530 eld_connection_type_names[e->conn_type]);
531 snd_iprintf(buffer, "eld_version\t\t[0x%x] %s\n", e->eld_ver,
532 eld_version_names[e->eld_ver]);
533 snd_iprintf(buffer, "edid_version\t\t[0x%x] %s\n", e->cea_edid_ver,
534 cea_edid_version_names[e->cea_edid_ver]);
535 snd_iprintf(buffer, "manufacture_id\t\t0x%x\n", e->manufacture_id);
536 snd_iprintf(buffer, "product_id\t\t0x%x\n", e->product_id);
537 snd_iprintf(buffer, "port_id\t\t\t0x%llx\n", (long long)e->port_id);
538 snd_iprintf(buffer, "support_hdcp\t\t%d\n", e->support_hdcp);
539 snd_iprintf(buffer, "support_ai\t\t%d\n", e->support_ai);
540 snd_iprintf(buffer, "audio_sync_delay\t%d\n", e->aud_synch_delay);
541
542 snd_eld_print_channel_allocation(e->spk_alloc, buf, sizeof(buf));
543 snd_iprintf(buffer, "speakers\t\t[0x%x]%s\n", e->spk_alloc, buf);
544
545 snd_iprintf(buffer, "sad_count\t\t%d\n", e->sad_count);
546
547 for (i = 0; i < e->sad_count; i++)
548 hdmi_print_sad_info(i, e->sad + i, buffer);
549 }
550 EXPORT_SYMBOL_GPL(snd_print_eld_info);
551 #endif /* CONFIG_SND_PROC_FS */
552