]> git.proxmox.com Git - mirror_qemu.git/blame - hw/audio/pcspk.c
Use DECLARE_*CHECKER* macros
[mirror_qemu.git] / hw / audio / pcspk.c
CommitLineData
fd06c375
FB
1/*
2 * QEMU PC speaker emulation
3 *
4 * Copyright (c) 2006 Joachim Henke
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
6086a565 25#include "qemu/osdep.h"
0d09e41a 26#include "hw/isa/isa.h"
8a824e4d 27#include "hw/audio/soundhw.h"
87ecb68b 28#include "audio/audio.h"
0b8fa32f 29#include "qemu/module.h"
1de7afc9 30#include "qemu/timer.h"
2e16ec05 31#include "qemu/error-report.h"
0d09e41a 32#include "hw/timer/i8254.h"
d6454270 33#include "migration/vmstate.h"
0d09e41a 34#include "hw/audio/pcspk.h"
873b4d3f 35#include "qapi/error.h"
db1015e9 36#include "qom/object.h"
fd06c375
FB
37
38#define PCSPK_BUF_LEN 1792
39#define PCSPK_SAMPLE_RATE 32000
40#define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
b988a650 41#define PCSPK_MIN_COUNT DIV_ROUND_UP(PIT_FREQ, PCSPK_MAX_FREQ)
fd06c375 42
db1015e9 43typedef struct PCSpkState PCSpkState;
8110fa1d
EH
44DECLARE_INSTANCE_CHECKER(PCSpkState, PC_SPEAKER,
45 TYPE_PC_SPEAKER)
d367ece5 46
db1015e9 47struct PCSpkState {
d367ece5
AF
48 ISADevice parent_obj;
49
302fe51b
JK
50 MemoryRegion ioport;
51 uint32_t iobase;
fd06c375
FB
52 uint8_t sample_buf[PCSPK_BUF_LEN];
53 QEMUSoundCard card;
54 SWVoiceOut *voice;
302fe51b 55 void *pit;
fd06c375
FB
56 unsigned int pit_count;
57 unsigned int samples;
58 unsigned int play_pos;
39c88f56
PD
59 uint8_t data_on;
60 uint8_t dummy_refresh_clock;
04e27c6b 61 bool migrate;
db1015e9 62};
fd06c375
FB
63
64static const char *s_spk = "pcspk";
28605a22 65static PCSpkState *pcspk_state;
fd06c375
FB
66
67static inline void generate_samples(PCSpkState *s)
68{
69 unsigned int i;
70
71 if (s->pit_count) {
72 const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
73 const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
74
75 /* multiple of wavelength for gapless looping */
668c2d1f 76 s->samples = (QEMU_ALIGN_DOWN(PCSPK_BUF_LEN * PIT_FREQ, m) / (PIT_FREQ >> 1) + 1) >> 1;
fd06c375
FB
77 for (i = 0; i < s->samples; ++i)
78 s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
79 } else {
80 s->samples = PCSPK_BUF_LEN;
81 for (i = 0; i < PCSPK_BUF_LEN; ++i)
82 s->sample_buf[i] = 128; /* silence */
83 }
84}
85
86static void pcspk_callback(void *opaque, int free)
87{
88 PCSpkState *s = opaque;
4aa5d285 89 PITChannelInfo ch;
fd06c375
FB
90 unsigned int n;
91
4aa5d285
JK
92 pit_get_channel_info(s->pit, 2, &ch);
93
94 if (ch.mode != 3) {
fd06c375 95 return;
4aa5d285 96 }
fd06c375 97
4aa5d285 98 n = ch.initial_count;
fd06c375
FB
99 /* avoid frequencies that are not reproducible with sample rate */
100 if (n < PCSPK_MIN_COUNT)
101 n = 0;
102
103 if (s->pit_count != n) {
104 s->pit_count = n;
105 s->play_pos = 0;
106 generate_samples(s);
107 }
108
109 while (free > 0) {
58935915 110 n = MIN(s->samples - s->play_pos, (unsigned int)free);
fd06c375
FB
111 n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
112 if (!n)
113 break;
114 s->play_pos = (s->play_pos + n) % s->samples;
115 free -= n;
116 }
117}
118
2e16ec05 119static int pcspk_audio_init(PCSpkState *s)
28605a22 120{
28605a22
GH
121 struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUDIO_FORMAT_U8, 0};
122
2e16ec05
GH
123 if (s->voice) {
124 /* already initialized */
125 return 0;
126 }
127
28605a22
GH
128 AUD_register_card(s_spk, &s->card);
129
130 s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
131 if (!s->voice) {
132 AUD_log(s_spk, "Could not open voice\n");
133 return -1;
134 }
135
136 return 0;
137}
138
a8170e5e 139static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
302fe51b 140 unsigned size)
fd06c375
FB
141{
142 PCSpkState *s = opaque;
4aa5d285
JK
143 PITChannelInfo ch;
144
145 pit_get_channel_info(s->pit, 2, &ch);
fd06c375
FB
146
147 s->dummy_refresh_clock ^= (1 << 4);
fd06c375 148
4aa5d285
JK
149 return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
150 (ch.out << 5);
fd06c375
FB
151}
152
a8170e5e 153static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
302fe51b 154 unsigned size)
fd06c375
FB
155{
156 PCSpkState *s = opaque;
157 const int gate = val & 1;
158
159 s->data_on = (val >> 1) & 1;
160 pit_set_gate(s->pit, 2, gate);
161 if (s->voice) {
162 if (gate) /* restart */
163 s->play_pos = 0;
164 AUD_set_active_out(s->voice, gate & s->data_on);
165 }
166}
167
302fe51b
JK
168static const MemoryRegionOps pcspk_io_ops = {
169 .read = pcspk_io_read,
170 .write = pcspk_io_write,
171 .impl = {
172 .min_access_size = 1,
173 .max_access_size = 1,
174 },
175};
176
db895a1e 177static void pcspk_initfn(Object *obj)
fd06c375 178{
db895a1e 179 PCSpkState *s = PC_SPEAKER(obj);
302fe51b 180
ecf2e5a4 181 memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1);
873b4d3f 182
8a0b4de0 183 object_property_add_link(obj, "pit", TYPE_PIT_COMMON,
873b4d3f
EV
184 (Object **)&s->pit,
185 qdev_prop_allow_set_link_before_realize,
d2623129 186 0);
db895a1e 187}
302fe51b 188
db895a1e
AF
189static void pcspk_realizefn(DeviceState *dev, Error **errp)
190{
191 ISADevice *isadev = ISA_DEVICE(dev);
192 PCSpkState *s = PC_SPEAKER(dev);
302fe51b 193
db895a1e
AF
194 isa_register_ioport(isadev, &s->ioport, s->iobase);
195
2e16ec05
GH
196 if (s->card.state) {
197 pcspk_audio_init(s);
198 }
199
28605a22 200 pcspk_state = s;
302fe51b
JK
201}
202
04e27c6b
DDAG
203static bool migrate_needed(void *opaque)
204{
205 PCSpkState *s = opaque;
206
207 return s->migrate;
208}
209
39c88f56
PD
210static const VMStateDescription vmstate_spk = {
211 .name = "pcspk",
212 .version_id = 1,
213 .minimum_version_id = 1,
214 .minimum_version_id_old = 1,
04e27c6b 215 .needed = migrate_needed,
39c88f56
PD
216 .fields = (VMStateField[]) {
217 VMSTATE_UINT8(data_on, PCSpkState),
218 VMSTATE_UINT8(dummy_refresh_clock, PCSpkState),
219 VMSTATE_END_OF_LIST()
220 }
221};
222
302fe51b 223static Property pcspk_properties[] = {
88e47b9a 224 DEFINE_AUDIO_PROPERTIES(PCSpkState, card),
2336172d 225 DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, 0x61),
04e27c6b 226 DEFINE_PROP_BOOL("migrate", PCSpkState, migrate, true),
302fe51b
JK
227 DEFINE_PROP_END_OF_LIST(),
228};
fd06c375 229
302fe51b
JK
230static void pcspk_class_initfn(ObjectClass *klass, void *data)
231{
232 DeviceClass *dc = DEVICE_CLASS(klass);
302fe51b 233
db895a1e 234 dc->realize = pcspk_realizefn;
125ee0ed 235 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
39c88f56 236 dc->vmsd = &vmstate_spk;
4f67d30b 237 device_class_set_props(dc, pcspk_properties);
28605a22
GH
238 /* Reason: realize sets global pcspk_state */
239 /* Reason: pit object link */
240 dc->user_creatable = false;
302fe51b
JK
241}
242
8c43a6f0 243static const TypeInfo pcspk_info = {
d367ece5 244 .name = TYPE_PC_SPEAKER,
302fe51b
JK
245 .parent = TYPE_ISA_DEVICE,
246 .instance_size = sizeof(PCSpkState),
db895a1e 247 .instance_init = pcspk_initfn,
302fe51b
JK
248 .class_init = pcspk_class_initfn,
249};
250
2e16ec05
GH
251static int pcspk_audio_init_soundhw(ISABus *bus)
252{
253 PCSpkState *s = pcspk_state;
254
255 warn_report("'-soundhw pcspk' is deprecated, "
256 "please set a backend using '-machine pcspk-audiodev=<name>' instead");
257 return pcspk_audio_init(s);
258}
259
302fe51b
JK
260static void pcspk_register(void)
261{
262 type_register_static(&pcspk_info);
2e16ec05 263 isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init_soundhw);
fd06c375 264}
302fe51b 265type_init(pcspk_register)