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 "qom/object.h"
29 #include "hw/isa/isa.h"
30 #include "hw/pci/pci.h"
31 #include "hw/audio/soundhw.h"
32 
33 struct soundhw {
34     const char *name;
35     const char *descr;
36     const char *typename;
37     int enabled;
38     int isa;
39     union {
40         int (*init_isa) (ISABus *bus);
41         int (*init_pci) (PCIBus *bus);
42     } init;
43 };
44 
45 static struct soundhw soundhw[9];
46 static int soundhw_count;
47 
isa_register_soundhw(const char * name,const char * descr,int (* init_isa)(ISABus * bus))48 void isa_register_soundhw(const char *name, const char *descr,
49                           int (*init_isa)(ISABus *bus))
50 {
51     assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
52     soundhw[soundhw_count].name = name;
53     soundhw[soundhw_count].descr = descr;
54     soundhw[soundhw_count].isa = 1;
55     soundhw[soundhw_count].init.init_isa = init_isa;
56     soundhw_count++;
57 }
58 
pci_register_soundhw(const char * name,const char * descr,int (* init_pci)(PCIBus * bus))59 void pci_register_soundhw(const char *name, const char *descr,
60                           int (*init_pci)(PCIBus *bus))
61 {
62     assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
63     soundhw[soundhw_count].name = name;
64     soundhw[soundhw_count].descr = descr;
65     soundhw[soundhw_count].isa = 0;
66     soundhw[soundhw_count].init.init_pci = init_pci;
67     soundhw_count++;
68 }
69 
deprecated_register_soundhw(const char * name,const char * descr,int isa,const char * typename)70 void deprecated_register_soundhw(const char *name, const char *descr,
71                                  int isa, const char *typename)
72 {
73     assert(soundhw_count < ARRAY_SIZE(soundhw) - 1);
74     soundhw[soundhw_count].name = name;
75     soundhw[soundhw_count].descr = descr;
76     soundhw[soundhw_count].isa = isa;
77     soundhw[soundhw_count].typename = typename;
78     soundhw_count++;
79 }
80 
select_soundhw(const char * optarg)81 void select_soundhw(const char *optarg)
82 {
83     struct soundhw *c;
84 
85     if (is_help_option(optarg)) {
86     show_valid_cards:
87 
88         if (soundhw_count) {
89              printf("Valid sound card names (comma separated):\n");
90              for (c = soundhw; c->name; ++c) {
91                  printf ("%-11s %s\n", c->name, c->descr);
92              }
93              printf("\n-soundhw all will enable all of the above\n");
94         } else {
95              printf("Machine has no user-selectable audio hardware "
96                     "(it may or may not have always-present audio hardware).\n");
97         }
98         exit(!is_help_option(optarg));
99     }
100     else {
101         size_t l;
102         const char *p;
103         char *e;
104         int bad_card = 0;
105 
106         if (!strcmp(optarg, "all")) {
107             for (c = soundhw; c->name; ++c) {
108                 c->enabled = 1;
109             }
110             return;
111         }
112 
113         p = optarg;
114         while (*p) {
115             e = strchr(p, ',');
116             l = !e ? strlen(p) : (size_t) (e - p);
117 
118             for (c = soundhw; c->name; ++c) {
119                 if (!strncmp(c->name, p, l) && !c->name[l]) {
120                     c->enabled = 1;
121                     break;
122                 }
123             }
124 
125             if (!c->name) {
126                 if (l > 80) {
127                     error_report("Unknown sound card name (too big to show)");
128                 }
129                 else {
130                     error_report("Unknown sound card name `%.*s'",
131                                  (int) l, p);
132                 }
133                 bad_card = 1;
134             }
135             p += l + (e != NULL);
136         }
137 
138         if (bad_card) {
139             goto show_valid_cards;
140         }
141     }
142 }
143 
soundhw_init(void)144 void soundhw_init(void)
145 {
146     struct soundhw *c;
147     ISABus *isa_bus = (ISABus *) object_resolve_path_type("", TYPE_ISA_BUS, NULL);
148     PCIBus *pci_bus = (PCIBus *) object_resolve_path_type("", TYPE_PCI_BUS, NULL);
149 
150     for (c = soundhw; c->name; ++c) {
151         if (c->enabled) {
152             if (c->typename) {
153                 warn_report("'-soundhw %s' is deprecated, "
154                             "please use '-device %s' instead",
155                             c->name, c->typename);
156                 if (c->isa) {
157                     isa_create_simple(isa_bus, c->typename);
158                 } else {
159                     pci_create_simple(pci_bus, -1, c->typename);
160                 }
161             } else if (c->isa) {
162                 if (!isa_bus) {
163                     error_report("ISA bus not available for %s", c->name);
164                     exit(1);
165                 }
166                 c->init.init_isa(isa_bus);
167             } else {
168                 if (!pci_bus) {
169                     error_report("PCI bus not available for %s", c->name);
170                     exit(1);
171                 }
172                 c->init.init_pci(pci_bus);
173             }
174         }
175     }
176 }
177 
178