-
Notifications
You must be signed in to change notification settings - Fork 2
/
hda-emu.c
1663 lines (1497 loc) · 37.9 KB
/
hda-emu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* hda-emu - simple HD-audio codec emulator for debugging snd-hda-intel driver
*
* Main routine
*
* Copyright (c) Takashi Iwai <[email protected]>
*
* This driver is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This driver is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <time.h>
#include "hda-types.h"
#include "hda-log.h"
#include <sound/driver.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/hda_codec.h>
#include "hda/hda_local.h"
#ifdef HAVE_HDA_BEEP
#include "hda/hda_beep.h"
#endif
/* fixup for 4.2+ kernels */
#ifdef NEW_HDA_INFRA
#ifndef HAVE_BUS_OPS
#define snd_hda_calc_stream_format snd_hdac_calc_stream_format
#undef STREAM_FORMAT_WITH_CODEC
#define HAVE_HDA_ATTACH_PCM 1
#endif
#endif
#ifndef HAVE_POWER_SAVE
#define snd_hda_power_up(x)
#define snd_hda_power_down(x)
#endif
extern int cmd_loop(FILE *fp);
/*
* interface to kernel
*/
static struct snd_card card = {
.ctl_files = LIST_HEAD_INIT(card.ctl_files),
.files_lock = MYLOCK_UNLOCKED,
};
static struct xhda_codec proc;
static struct hda_bus *bus;
#ifdef NEW_HDA_INFRA
#ifndef HAVE_BUS_OPS
static struct hda_bus _bus;
#endif
#endif
static struct hda_codec *_codec;
static int ignore_invalid_ftype;
static int cmd_send(struct hda_bus *bus, unsigned int cmd)
{
unsigned int nid = (cmd >> 20) & 0x7f;
unsigned int verb = (cmd >> 8) & 0xfff;
unsigned int parm = cmd & 0xff;
int err;
hda_log(HDA_LOG_VERB, "send: NID=0x%x, VERB=0x%x", nid, verb);
switch (verb & 0xf00) {
case AC_VERB_GET_AMP_GAIN_MUTE:
hda_log(HDA_LOG_VERB,
"(%s,%s:%s#%d), PARM=0x%x",
get_verb_name(&proc, cmd),
(verb & (1 << 7) ? "O" : "I"),
(verb & (1 << 5) ? "L" : "R"),
verb & 0x0f,
parm);
break;
case AC_VERB_SET_AMP_GAIN_MUTE:
hda_log(HDA_LOG_VERB,
"(%s,%s%s:%s%s#%d), PARM=0x%x",
get_verb_name(&proc, cmd),
(verb & (1 << 7) ? "O" : ""),
(verb & (1 << 6) ? "I" : ""),
(verb & (1 << 5) ? "L" : ""),
(verb & (1 << 4) ? "R" : ""),
verb & 0x0f,
parm);
break;
case AC_VERB_SET_PROC_COEF:
case AC_VERB_SET_COEF_INDEX:
hda_log(HDA_LOG_VERB,
"(%s), PARM=0x%x",
get_verb_name(&proc, cmd), (verb & 0xff) << 8 | parm);
break;
default:
hda_log(HDA_LOG_VERB,
"(%s), PARM=0x%x",
get_verb_name(&proc, cmd), parm);
if (verb == 0xf00)
hda_log(HDA_LOG_VERB, "(%s)",
get_parameter_name(&proc, cmd));
break;
}
hda_log(HDA_LOG_VERB, "\n");
err = hda_cmd(&proc, cmd);
if (err < 0) {
if (verb != AC_VERB_PARAMETERS || parm != AC_PAR_FUNCTION_TYPE ||
!ignore_invalid_ftype)
hda_log(HDA_LOG_ERR, "invalid command: "
"NID=0x%x, verb=0x%x, parm=0x%x\n",
nid, verb, parm);
return err;
}
return 0;
}
#ifdef HAVE_GET_RESPONSE_WITH_CADDR
static unsigned int resp_get_caddr(struct hda_bus *bus, unsigned int caddr)
{
hda_log(HDA_LOG_VERB, "receive: 0x%x\n", proc.rc);
return proc.rc;
}
#else
static unsigned int resp_get(struct hda_bus *bus)
{
hda_log(HDA_LOG_VERB, "receive: 0x%x\n", proc.rc);
return proc.rc;
}
#endif
#ifdef OLD_HDA_CMD
static int old_cmd_send(struct hda_codec *codec, hda_nid_t nid,
int direct, unsigned int verb,
unsigned int para)
{
u32 val;
val = (u32)(codec->addr & 0x0f) << 28;
val |= (u32)direct << 27;
val |= (u32)nid << 20;
val |= verb << 8;
val |= para;
return cmd_send(codec->bus, val);
}
static unsigned int old_resp_get(struct hda_codec *codec)
{
return resp_get(codec->bus);
}
#endif
void hda_exec_verb(int nid, int verb, int parm)
{
u32 val;
val = (u32)(proc.addr & 0x0f) << 28;
val |= (u32)nid << 20;
val |= verb << 8;
val |= parm;
if (hda_cmd(&proc, val) < 0) {
hda_log(HDA_LOG_ERR, "invalid command: "
"NID=0x%x, verb=0x%x, parm=0x%x\n",
nid, verb, parm);
} else {
hda_log(HDA_LOG_VERB, "Command response:: 0x%x\n", proc.rc);
}
}
/*
* power_save module option handling
*/
#ifdef HDA_OLD_POWER_SAVE
extern int *power_save_parameter; /* defined in kernel/hda_codec.c */
#else
static int power_save;
#endif
int hda_get_power_save(void)
{
#ifdef HDA_OLD_POWER_SAVE
return *power_save_parameter;
#else
return power_save;
#endif
}
void hda_set_power_save(int val)
{
#ifdef HDA_OLD_POWER_SAVE
*power_save_parameter = val;
#else
power_save = val;
#ifdef NEW_HDA_INFRA
snd_hda_set_power_save(bus, val * 1000);
#endif
#endif /* HDA_OLD_POWER_SAVE */
}
int hda_get_dump_coef(void)
{
return _codec->dump_coef;
}
void hda_set_dump_coef(int val)
{
_codec->dump_coef = val;
}
/*
* proc dump
*/
struct snd_info_buffer {
FILE *fp;
unsigned int nid;
int printing;
int processing;
};
extern void (*snd_iprintf_dumper)(struct snd_info_buffer *buf,
const char *fmt, va_list ap);
static void log_dump_proc_file(struct snd_info_buffer *buf,
const char *fmt, va_list ap)
{
char line[512];
int node, in_process;
char *p;
if (buf->nid <= 0) {
vfprintf(buf->fp, fmt, ap);
return;
}
vsnprintf(line, sizeof(line), fmt, ap);
if (buf->processing) {
if (buf->printing == 1)
fputs(line, buf->fp);
p = strchr(line, '\n');
if (p && !p[1])
buf->processing = 0;
return;
}
in_process = buf->processing;
p = strchr(line, '\n');
if (p && !p[1])
buf->processing = 0;
else
buf->processing = 1;
if (in_process) {
if (buf->printing == 1)
fputs(line, buf->fp);
return;
}
switch (buf->printing) {
case 0:
if (sscanf(line, "Node 0x%02x ", &node) == 1 &&
node == buf->nid) {
fputs(line, buf->fp);
buf->printing = 1;
}
break;
case 1:
if (!strncmp(line, "Node 0x", 7))
buf->printing = 2;
else
fputs(line, buf->fp);
break;
}
}
void hda_log_dump_proc(unsigned int nid, const char *file)
{
struct snd_info_buffer buf;
FILE *fp;
int saved_level;
if (!card.proc)
return;
if (file) {
fp = fopen(file, "w");
if (!fp) {
hda_log(HDA_LOG_ERR,
"Cannot open dump file %s\n", file);
return;
}
buf.fp = fp;
} else
buf.fp = hda_get_logfp();
buf.nid = nid;
if (nid > 1)
buf.printing = 0;
else
buf.printing = 1;
snd_hda_power_up(_codec);
/* don't show verbs */
saved_level = hda_log_level_set(HDA_LOG_KERN);
snd_iprintf_dumper = log_dump_proc_file;
card.proc->func(card.proc, &buf);
hda_log_level_set(saved_level);
snd_hda_power_down(_codec);
if (file)
fclose(buf.fp);
}
/*
* show available jacks
*/
static const char *_get_jack_location(u32 cfg)
{
static char *bases[7] = {
"N/A", "Rear", "Front", "Left", "Right", "Top", "Bottom",
};
static unsigned char specials_idx[] = {
0x07, 0x08,
0x17, 0x18, 0x19,
0x37, 0x38
};
static char *specials[] = {
"Rear Panel", "Drive Bar",
"Riser", "HDMI", "ATAPI",
"Mobile-In", "Mobile-Out"
};
int i;
cfg = (cfg & AC_DEFCFG_LOCATION) >> AC_DEFCFG_LOCATION_SHIFT;
if ((cfg & 0x0f) < 7)
return bases[cfg & 0x0f];
for (i = 0; i < ARRAY_SIZE(specials_idx); i++) {
if (cfg == specials_idx[i])
return specials[i];
}
return "UNKNOWN";
}
void hda_log_list_jacks(int raw)
{
struct xhda_node *node;
unsigned int type, conn;
static char *jack_conns[4] = { "Jack", "N/A", "Fixed", "Both" };
static char *jack_types[16] = {
"Line Out", "Speaker", "HP Out", "CD",
"SPDIF Out", "Digital Out", "Modem Line", "Modem Hand",
"Line In", "Aux", "Mic", "Telephony",
"SPDIF In", "Digitial In", "Reserved", "Other"
};
static char *jack_locations[4] = { "Ext", "Int", "Sep", "Oth" };
for (node = proc.afg.next; node; node = node->next) {
unsigned int pin_default;
type = (node->wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
if (type != AC_WID_PIN)
continue;
pin_default = node->pin_default;
#ifdef HAVE_SND_HDA_CODEC_GET_PINCFG
if (!raw)
pin_default = snd_hda_codec_get_pincfg(_codec, node->nid);
#endif
conn = (pin_default & AC_DEFCFG_PORT_CONN) >> AC_DEFCFG_PORT_CONN_SHIFT;
if (conn == AC_JACK_PORT_NONE)
continue;
/* if (!(node->pincap & AC_PINCAP_PRES_DETECT))
continue;*/
hda_log(HDA_LOG_INFO,
"NID 0x%02x: cfg 0x%08x: [%s] %s at %s %s\n",
node->nid, pin_default,
jack_conns[(pin_default & AC_DEFCFG_PORT_CONN)
>> AC_DEFCFG_PORT_CONN_SHIFT],
jack_types[(pin_default & AC_DEFCFG_DEVICE)
>> AC_DEFCFG_DEVICE_SHIFT],
jack_locations[(pin_default >> (AC_DEFCFG_LOCATION_SHIFT + 4)) & 3],
_get_jack_location(pin_default));
}
}
/*
* show jack state of the given NID
*/
void hda_log_jack_state(int nid)
{
int state = hda_get_jack_state(&proc, nid);
if (state < 0)
hda_log(HDA_LOG_ERR, "Invalid Jack NID 0x%x\n", nid);
else
hda_log(HDA_LOG_INFO, "Jack state [0x%x] = %d\n", nid, state);
}
/*
* change the jack state of the given NID
*/
static void issue_unsol(int caddr, int res);
void hda_log_set_jack(int nid, int val)
{
int err = hda_set_jack_state(&proc, nid, val);
if (err > 0)
hda_log_issue_unsol(nid);
}
void hda_log_issue_unsol(int nid)
{
int val = hda_get_unsol_state(&proc, nid);
if (val & (1 << 7))
issue_unsol(proc.addr, val);
}
/*
* suspend/resume simulation
*/
void hda_test_suspend(void)
{
#ifdef NEW_HDA_INFRA
struct device *dev = hda_codec_dev(_codec);
if (dev->driver && dev->driver->pm)
dev->driver->pm->suspend(dev);
#else /* !NEW_HDA_INFRA */
#ifdef HAVE_HDA_SUSPEND_PMSG
snd_hda_suspend(bus, PMSG_SUSPEND);
#else
snd_hda_suspend(bus);
#endif
#endif /* NEW_HDA_INFRA */
}
void hda_test_resume(void)
{
#ifdef NEW_HDA_INFRA
struct device *dev = hda_codec_dev(_codec);
if (dev->driver && dev->driver->pm)
dev->driver->pm->resume(dev);
#else /* !NEW_HDA_INFRA */
snd_hda_resume(bus);
#endif /* NEW_HDA_INFRA */
}
static inline unsigned int random_bit(unsigned int pincap, unsigned int mask,
unsigned int bit)
{
if (pincap & mask) {
if (random() & 1)
return bit;
}
return 0;
}
static void randomize_amp(struct xhda_amp_caps *caps,
struct xhda_amp_vals *vals, int nums)
{
int i, c;
for (i = 0; i < nums; i++)
for (c = 0; c < 2; c++) {
if (caps->nsteps)
vals->vals[i][c] = random() % caps->nsteps;
else
vals->vals[i][c] = 0;
if (caps->mute && (random() & 1))
vals->vals[i][c] |= 0x80;
}
}
void hda_test_pm_randomize(void)
{
struct xhda_node *node;
for (node = proc.afg.next; node; node = node->next) {
unsigned int type;
type = (node->wcaps & AC_WCAP_TYPE) >> AC_WCAP_TYPE_SHIFT;
if (type == AC_WID_PIN)
node->pinctl = random() & 0xff;
if (node->wcaps & AC_WCAP_IN_AMP)
randomize_amp(node->amp_in_caps.override ?
&node->amp_in_caps :
&proc.afg.amp_in_caps,
&node->amp_in_vals, node->num_nodes);
if (node->wcaps & AC_WCAP_OUT_AMP)
randomize_amp(node->amp_out_caps.override ?
&node->amp_out_caps :
&proc.afg.amp_out_caps,
&node->amp_out_vals, 1);
}
}
void hda_test_pm_reinit(void)
{
struct xhda_node *node;
for (node = proc.afg.next; node; node = node->next) {
node->pinctl = node->orig_pinctl;
node->amp_in_vals = node->orig_amp_in_vals;
node->amp_out_vals = node->orig_amp_out_vals;
}
}
/*
* unsol even handling
*/
extern void snd_hdac_bus_queue_event(struct hdac_bus *bus, u32 res, u32 res_ex);
static void issue_unsol(int caddr, int res)
{
int vendor_id;
/* no unsol handling during D3 */
if (proc.afg.power_current == 3)
return;
caddr |= 1 << 4;
/* ALC880 has incompatible unsol tag */
#ifdef HAVE_HDA_CORE
vendor_id = _codec->core.vendor_id;
#else
vendor_id = _codec->vendor_id;
#endif
if (vendor_id == 0x10ec0880)
res = (res & 0x3f) << 28;
else
res = (res & 0x3f) << 26;
#if 0 // old API
snd_hda_queue_unsol_event(bus, res, caddr);
#else
snd_hdac_bus_queue_event(&bus->core, res, caddr);
#endif
}
#ifdef CONFIG_SND_HDA_RECONFIG
/*
* sysfs reset simulation
*/
static void reset_pcm(void);
void hda_codec_reset(void)
{
snd_hda_codec_reset(_codec);
reset_pcm();
}
/*
* sysfs reconfig simulation
*/
int hda_codec_reconfig(void)
{
int err;
snd_hda_power_up(_codec);
snd_hda_codec_reset(_codec);
err = snd_hda_codec_configure(_codec);
if (err < 0)
goto error;
#ifndef NEW_HDA_INFRA
/* rebuild PCMs */
err = snd_hda_build_pcms(bus);
if (err < 0)
goto error;
/* rebuild mixers */
err = snd_hda_codec_build_controls(_codec);
#endif /* !NEW_HDA_INFRA */
error:
snd_hda_power_down(_codec);
return err;
}
#ifdef HAVE_USER_PINCFGS
/*
* sysfs pin_configs simulations
*/
static void show_pincfgs(struct snd_array *list)
{
int i;
for (i = 0; i < list->used; i++) {
struct hda_pincfg *pin = snd_array_elem(list, i);
hda_log(HDA_LOG_INFO, "0x%02x 0x%08x\n", pin->nid, pin->cfg);
}
}
void hda_log_show_driver_pin_configs(void)
{
show_pincfgs(&_codec->driver_pins);
}
void hda_log_show_init_pin_configs(void)
{
show_pincfgs(&_codec->init_pins);
}
void hda_log_show_user_pin_configs(void)
{
show_pincfgs(&_codec->user_pins);
}
void hda_log_set_user_pin_configs(unsigned int nid, unsigned int cfg)
{
snd_hda_add_pincfg(_codec, &_codec->user_pins, nid, cfg);
}
#endif /* HAVE_USER_PINCFGS */
extern int _show_hints(struct hda_codec *codec, const char *key);
extern int _parse_hints(struct hda_codec *codec, const char *buf);
void hda_log_show_hints(char *hint)
{
_show_hints(_codec, hint);
}
void hda_log_set_hints(char *hint)
{
_parse_hints(_codec, hint);
}
#endif /* CONFIG_SND_HDA_RECONFIG */
/*
* PCM
*/
#define MAX_PCM_STREAMS 16
static int num_pcm_streams;
static struct hda_pcm *pcm_streams[MAX_PCM_STREAMS];
#ifndef OLD_HDA_PCM
/* get a string corresponding to the given HDA_PCM_TYPE_XXX */
static const char *get_pcm_type_name(int type)
{
const char *names[] = {
[HDA_PCM_TYPE_AUDIO] = "audio",
[HDA_PCM_TYPE_SPDIF] = "SPDIF",
[HDA_PCM_TYPE_HDMI] = "HDMI",
[HDA_PCM_TYPE_MODEM] = "modem",
};
if (type >= HDA_PCM_TYPE_AUDIO && type <= HDA_PCM_TYPE_MODEM)
return names[type];
else
return "unknown";
}
#endif
/* list registered PCM streams, called from hda-ctlsh.c */
void hda_list_pcms(void)
{
int i;
for (i = 0; i < num_pcm_streams; i++) {
struct hda_pcm *p = pcm_streams[i];
if (!p->stream[0].substreams && !p->stream[1].substreams)
continue;
#ifdef OLD_HDA_PCM
hda_log(HDA_LOG_INFO, "%d: %s, play=%d, capt=%d\n",
i, p->name,
p->stream[0].substreams,
p->stream[1].substreams);
#else
hda_log(HDA_LOG_INFO, "%d: %s:%d (%s), play=%d, capt=%d\n",
i, p->name, p->device,
get_pcm_type_name(p->pcm_type),
p->stream[0].substreams,
p->stream[1].substreams);
#endif
}
}
/* get the appropriate ALSA SNDRV_PCM_FORMAT_* from the format bits */
static int get_alsa_format(int bits)
{
if (bits <= 8)
return SNDRV_PCM_FORMAT_U8;
else if (bits <= 16)
return SNDRV_PCM_FORMAT_S16_LE;
else
return SNDRV_PCM_FORMAT_S32_LE;
}
static char *fmt_names[64] = {
"S8", "U8", "S16_LE", "S16_BE", "U16_LE", "U16_BE", "S24_LE", "S24_BE",
"U24_LE", "U24_BE", "S32_LE", "S32_BE", "U32_LE", "U32_BE",
"FLOAT_LE", "FLOAT_BE", "FLOAT64_LE", "FLOAT64_BE",
"IEC958_LE", "IEC958_BE", "MU_LAW", "A_LAW", "IMA_ADPCM", "MPEG" "GSM",
NULL, NULL, NULL, NULL, NULL, NULL, "SPECIAL",
"S24_3LE", "S24_3BE", "U24_3LE", "U24_3BE"
"S24_3LE", "S20_3BE", "U20_3LE", "U20_3BE",
"S18_3LE", "S18_3BE", "U18_3LE", "U18_3BE",
};
static int rate_consts[] = {
5512, 8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000,
88200, 96000, 176400, 192000,
};
/* test the given PCM stream, called from hda-ctlsh.c */
void hda_test_pcm(int id, int op, int subid,
int dir, int rate, int channels, int format)
{
#ifndef HAVE_HDA_ATTACH_PCM
static struct snd_pcm_substream dummy_substream;
static struct snd_pcm_runtime dummy_runtime;
static struct snd_pcm_str dummy_pstr;
#endif
struct snd_pcm_substream *substream;
struct snd_pcm_runtime *runtime;
static struct snd_pcm_str *pstr;
struct hda_pcm_stream *hinfo;
unsigned int format_val;
unsigned int ctls = 0;
int i, err;
if (id < 0 || id >= num_pcm_streams || !pcm_streams[id]) {
hda_log(HDA_LOG_ERR, "Invalid PCM id %d\n", id);
return;
}
if (!pcm_streams[id]->stream[0].substreams &&
!pcm_streams[id]->stream[1].substreams) {
hda_log(HDA_LOG_ERR, "Empty PCM for id %d\n", id);
return;
}
if (!pcm_streams[id]->stream[dir].substreams) {
hda_log(HDA_LOG_INFO, "No substream in PCM %s for %s\n",
pcm_streams[id]->name,
(dir ? "capt" : "play"));
return;
}
if (subid < 0 || subid >= pcm_streams[id]->stream[dir].substreams) {
hda_log(HDA_LOG_INFO,
"Invalid substream %d for PCM %s for %s\n",
subid, pcm_streams[id]->name,
(dir ? "capt" : "play"));
return;
}
#ifdef HAVE_HDA_ATTACH_PCM
pstr = &pcm_streams[id]->pcm->streams[dir];
substream = &pstr->substream[subid];
runtime = substream->runtime;
if (op == PCM_TEST_END) {
if (!substream->ref_count) {
hda_log(HDA_LOG_ERR, "PCM stream not opened\n");
return;
}
} else {
if (substream->ref_count) {
hda_log(HDA_LOG_ERR, "PCM stream already opened\n");
return;
}
runtime = calloc(1, sizeof(*runtime));
if (!runtime) {
hda_log(HDA_LOG_ERR, "cannot malloc\n");
exit(1);
}
substream->runtime = runtime;
substream->ref_count = 1;
pstr->substream_opened = 1;
}
#else
substream = &dummy_substream;
runtime = &dummy_runtime;
pstr = &dummy_pstr;
memset(substream, 0, sizeof(*substream));
memset(runtime, 0, sizeof(*runtime));
substream->stream = dir;
substream->number = subid;
substream->runtime = runtime;
substream->ref_count = 1;
substream->pstr = pstr;
pstr->substream_opened = 1;
#endif
runtime->rate = rate;
runtime->format = get_alsa_format(format);
runtime->channels = channels;
hinfo = &pcm_streams[id]->stream[dir];
runtime->hw.channels_min = hinfo->channels_min;
runtime->hw.channels_max = hinfo->channels_max;
runtime->hw.formats = hinfo->formats;
runtime->hw.rates = hinfo->rates;
#ifdef INDIVIDUAL_SPDIF_CTLS
{
struct hda_spdif_out *spdif;
spdif = snd_hda_spdif_out_of_nid(_codec, hinfo->nid);
ctls = spdif ? spdif->ctls : 0;
}
#elif defined(STREAM_FORMAT_WITH_SPDIF)
ctls = _codec->spdif_ctls;
#endif
if (op != PCM_TEST_END) {
hda_log(HDA_LOG_INFO, "Open PCM %s for %s\n",
pcm_streams[id]->name,
(dir ? "capt" : "play"));
snd_hda_power_up(_codec);
err = hinfo->ops.open(hinfo, _codec, substream);
if (err < 0) {
hda_log(HDA_LOG_INFO, "Open error = %d\n", err);
goto powerdown;
}
hda_log(HDA_LOG_INFO, "Available PCM parameters:\n");
hda_log(HDA_LOG_INFO, " channels: %d/%d\n",
runtime->hw.channels_min,
runtime->hw.channels_max);
hda_log(HDA_LOG_INFO, " formats:");
for (i = 0; i < ARRAY_SIZE(fmt_names); i++) {
if (runtime->hw.formats & (1ULL << i)) {
if (fmt_names[i])
hda_log(HDA_LOG_INFO, " %s", fmt_names[i]);
else
hda_log(HDA_LOG_INFO, " Uknown#%d", i);
}
}
hda_log(HDA_LOG_INFO, "\n rates:");
for (i = 0; i < ARRAY_SIZE(rate_consts); i++) {
if (runtime->hw.rates & (1UL << i))
hda_log(HDA_LOG_INFO, " %d", rate_consts[i]);
}
hda_log(HDA_LOG_INFO, "\n");
if (channels < runtime->hw.channels_min ||
channels > runtime->hw.channels_max) {
hda_log(HDA_LOG_ERR, "Channels count (%d) not available for %s\n",
channels, (dir ? "capture" : "playback"));
goto closing;
}
hda_log(HDA_LOG_INFO, "Prepare PCM, rate=%d, channels=%d, "
"format=%d bits\n",
rate, channels, format);
format_val = snd_hda_calc_stream_format(
#if defined(STREAM_FORMAT_WITH_CODEC)
_codec,
#endif
rate, channels,
get_alsa_format(format),
format
#if defined(INDIVIDUAL_SPDIF_CTLS) || defined(STREAM_FORMAT_WITH_SPDIF)
, ctls
#endif
);
if (!format_val)
goto closing;
hda_log(HDA_LOG_INFO, "PCM format_val = 0x%x\n", format_val);
#ifdef HAVE_COMMON_PREPARE
err = snd_hda_codec_prepare(_codec, hinfo, subid + 1,
format_val, substream);
#else
err = hinfo->ops.prepare(hinfo, _codec, 1, format_val, substream);
#endif
}
if (op != PCM_TEST_START) {
hda_log(HDA_LOG_INFO, "PCM Clean up\n");
#ifdef HAVE_COMMON_PREPARE
snd_hda_codec_cleanup(_codec, hinfo, substream);
#else
hinfo->ops.cleanup(hinfo, _codec, substream);
#endif
}
closing:
if (op != PCM_TEST_START) {
substream->ref_count = 0;
hda_log(HDA_LOG_INFO, "Close PCM\n");
hinfo->ops.close(hinfo, _codec, substream);
}
powerdown:
if (op != PCM_TEST_START) {
snd_hda_power_down(_codec);
#ifdef HAVE_HDA_ATTACH_PCM
substream->runtime = NULL;
substream->ref_count = 0;
pstr->substream_opened = 0;
free(runtime);
#endif
}
}
/* attach_pcm callback -- register the stream */
static int attach_pcm(struct hda_bus *bus, struct hda_codec *codec,
struct hda_pcm *cpcm)
{
#ifdef HAVE_HDA_ATTACH_PCM
int i, s;
#endif
if (cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams ||
cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams) {
#ifdef OLD_HDA_PCM
hda_log(HDA_LOG_INFO,
"Attach PCM name %s, play #%d, capture #%d\n",
cpcm->name,
cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams);
#else
cpcm->device = num_pcm_streams;
hda_log(HDA_LOG_INFO,
"Attach PCM dev %d, name %s, type %s, play #%d, capture #%d\n",
cpcm->device, cpcm->name, get_pcm_type_name(cpcm->pcm_type),
cpcm->stream[SNDRV_PCM_STREAM_PLAYBACK].substreams,
cpcm->stream[SNDRV_PCM_STREAM_CAPTURE].substreams);
#endif
if (num_pcm_streams >= MAX_PCM_STREAMS) {
hda_log(HDA_LOG_ERR, "Too many streams\n");
return 0;
}
pcm_streams[num_pcm_streams] = cpcm;
#ifdef HAVE_HDA_ATTACH_PCM
cpcm->pcm = calloc(1, sizeof(*cpcm->pcm));
if (!cpcm->pcm) {
hda_log(HDA_LOG_ERR, "cannot malloc\n");
exit(1);
}
cpcm->pcm->device = cpcm->device;
for (s = 0; s < 2; s++) {
struct snd_pcm_str *str = &cpcm->pcm->streams[s];
str->substream_count = cpcm->stream[s].substreams;
if (!str->substream_count)
continue;
str->substream = calloc(str->substream_count,
sizeof(*str->substream));
if (!str->substream) {
hda_log(HDA_LOG_ERR, "cannot malloc\n");
exit(1);
}
for (i = 0; i < str->substream_count; i++) {
str->substream[i].pcm = cpcm->pcm;
str->substream[i].pstr = str;
str->substream[i].stream = s;
str->substream[i].number = i;
}
}
#endif
}
num_pcm_streams++;
return 0;
}
#ifdef CONFIG_SND_HDA_RECONFIG
/* clear the all registered PCM streams */
static void reset_pcm(void)
{
memset(pcm_streams, 0, sizeof(pcm_streams));
num_pcm_streams = 0;
}
#endif
#ifndef HAVE_HDA_ATTACH_PCM
static int azx_pcm_create(struct hda_codec *codec)
{
int c, err;
/* create audio PCMs */
for (c = 0; c < codec->num_pcms; c++) {
err = attach_pcm(codec->bus, codec, &codec->pcm_info[c]);
if (err < 0)
return err;
}
return 0;
}
#endif
/*
* power management
*/
#ifndef NEW_HDA_INFRA
#ifdef HAVE_NEW_PM_NOTIFY
static void new_pm_notify(struct hda_bus *bus, bool power_up)
{
hda_log(HDA_LOG_INFO, "PM-Notified, power_up = %d\n", power_up);
}
#else