xref: /openbmc/qemu/hw/audio/soundhw.c (revision 77dd098a)
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/option.h"
26 #include "qemu/help_option.h"
27 #include "qemu/error-report.h"
28 #include "qapi/error.h"
29 #include "qom/object.h"
30 #include "hw/qdev-properties.h"
31 #include "hw/isa/isa.h"
32 #include "hw/pci/pci.h"
33 #include "hw/audio/soundhw.h"
34 
35 struct soundhw {
36     const char *name;
37     const char *descr;
38     const char *typename;
39     int isa;
40     int (*init_pci) (PCIBus *bus, const char *audiodev);
41 };
42 
43 static struct soundhw soundhw[9];
44 static int soundhw_count;
45 
46 void pci_register_soundhw(const char *name, const char *descr,
47                           int (*init_pci)(PCIBus *bus, const char *audiodev))
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 = 0;
53     soundhw[soundhw_count].init_pci = init_pci;
54     soundhw_count++;
55 }
56 
57 void deprecated_register_soundhw(const char *name, const char *descr,
58                                  int isa, const char *typename)
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 = isa;
64     soundhw[soundhw_count].typename = typename;
65     soundhw_count++;
66 }
67 
68 void show_valid_soundhw(void)
69 {
70     struct soundhw *c;
71 
72     if (soundhw_count) {
73          printf("Valid sound card names (comma separated):\n");
74          for (c = soundhw; c->name; ++c) {
75              printf ("%-11s %s\n", c->name, c->descr);
76          }
77     } else {
78          printf("Machine has no user-selectable audio hardware "
79                 "(it may or may not have always-present audio hardware).\n");
80     }
81 }
82 
83 static struct soundhw *selected = NULL;
84 static const char *audiodev_id;
85 
86 void select_soundhw(const char *name, const char *audiodev)
87 {
88     struct soundhw *c;
89 
90     if (selected) {
91         error_report("only one -soundhw option is allowed");
92         exit(1);
93     }
94 
95     for (c = soundhw; c->name; ++c) {
96         if (g_str_equal(c->name, name)) {
97             selected = c;
98             audiodev_id = audiodev;
99             break;
100         }
101     }
102 
103     if (!c->name) {
104         error_report("Unknown sound card name `%s'", name);
105         show_valid_soundhw();
106         exit(1);
107     }
108 }
109 
110 void soundhw_init(void)
111 {
112     struct soundhw *c = selected;
113     ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
114     PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
115     BusState *bus;
116 
117     if (!c) {
118         return;
119     }
120     if (c->isa) {
121         if (!isa_bus) {
122             error_report("ISA bus not available for %s", c->name);
123             exit(1);
124         }
125         bus = BUS(isa_bus);
126     } else {
127         if (!pci_bus) {
128             error_report("PCI bus not available for %s", c->name);
129             exit(1);
130         }
131         bus = BUS(pci_bus);
132     }
133 
134     if (c->typename) {
135         DeviceState *dev = qdev_new(c->typename);
136         qdev_prop_set_string(dev, "audiodev", audiodev_id);
137         qdev_realize_and_unref(dev, bus, &error_fatal);
138     } else {
139         assert(!c->isa);
140         c->init_pci(pci_bus, audiodev_id);
141     }
142 }
143 
144