xref: /openbmc/qemu/hw/audio/soundhw.c (revision 37677d7d)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 #include "qemu/osdep.h"
25 #include "qemu/help_option.h"
26 #include "qemu/error-report.h"
27 #include "qom/object.h"
28 #include "hw/isa/isa.h"
29 #include "hw/pci/pci.h"
30 #include "hw/audio/soundhw.h"
31 
32 struct soundhw {
33     const char *name;
34     const char *descr;
35     int enabled;
36     int isa;
37     union {
38         int (*init_isa) (ISABus *bus);
39         int (*init_pci) (PCIBus *bus);
40     } init;
41 };
42 
43 static struct soundhw soundhw[9];
44 static int soundhw_count;
45 
46 void isa_register_soundhw(const char *name, const char *descr,
47                           int (*init_isa)(ISABus *bus))
48 {
49     assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
50     soundhw[soundhw_count].name = name;
51     soundhw[soundhw_count].descr = descr;
52     soundhw[soundhw_count].isa = 1;
53     soundhw[soundhw_count].init.init_isa = init_isa;
54     soundhw_count++;
55 }
56 
57 void pci_register_soundhw(const char *name, const char *descr,
58                           int (*init_pci)(PCIBus *bus))
59 {
60     assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
61     soundhw[soundhw_count].name = name;
62     soundhw[soundhw_count].descr = descr;
63     soundhw[soundhw_count].isa = 0;
64     soundhw[soundhw_count].init.init_pci = init_pci;
65     soundhw_count++;
66 }
67 
68 void select_soundhw(const char *optarg)
69 {
70     struct soundhw *c;
71 
72     if (is_help_option(optarg)) {
73     show_valid_cards:
74 
75         if (soundhw_count) {
76              printf("Valid sound card names (comma separated):\n");
77              for (c = soundhw; c->name; ++c) {
78                  printf ("%-11s %s\n", c->name, c->descr);
79              }
80              printf("\n-soundhw all will enable all of the above\n");
81         } else {
82              printf("Machine has no user-selectable audio hardware "
83                     "(it may or may not have always-present audio hardware).\n");
84         }
85         exit(!is_help_option(optarg));
86     }
87     else {
88         size_t l;
89         const char *p;
90         char *e;
91         int bad_card = 0;
92 
93         if (!strcmp(optarg, "all")) {
94             for (c = soundhw; c->name; ++c) {
95                 c->enabled = 1;
96             }
97             return;
98         }
99 
100         p = optarg;
101         while (*p) {
102             e = strchr(p, ',');
103             l = !e ? strlen(p) : (size_t) (e - p);
104 
105             for (c = soundhw; c->name; ++c) {
106                 if (!strncmp(c->name, p, l) && !c->name[l]) {
107                     c->enabled = 1;
108                     break;
109                 }
110             }
111 
112             if (!c->name) {
113                 if (l > 80) {
114                     error_report("Unknown sound card name (too big to show)");
115                 }
116                 else {
117                     error_report("Unknown sound card name `%.*s'",
118                                  (int) l, p);
119                 }
120                 bad_card = 1;
121             }
122             p += l + (e != NULL);
123         }
124 
125         if (bad_card) {
126             goto show_valid_cards;
127         }
128     }
129 }
130 
131 void soundhw_init(void)
132 {
133     struct soundhw *c;
134     ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
135     PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
136 
137     for (c = soundhw; c->name; ++c) {
138         if (c->enabled) {
139             if (c->isa) {
140                 if (!isa_bus) {
141                     error_report("ISA bus not available for %s", c->name);
142                     exit(1);
143                 }
144                 c->init.init_isa(isa_bus);
145             } else {
146                 if (!pci_bus) {
147                     error_report("PCI bus not available for %s", c->name);
148                     exit(1);
149                 }
150                 c->init.init_pci(pci_bus);
151             }
152         }
153     }
154 }
155 
156