blob: b056c05387c8c9a6bea5b9f098820ae197e2c652 [file] [log] [blame]
bellardfd06c372006-04-24 21:58:30 +00001/*
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
Peter Maydell6086a562016-01-18 17:33:52 +000025#include "qemu/osdep.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010026#include "hw/isa/isa.h"
Eduardo Habkost8a824e42017-05-08 17:57:35 -030027#include "hw/audio/soundhw.h"
pbrook87ecb682007-11-17 17:14:51 +000028#include "audio/audio.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020029#include "qemu/module.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010030#include "qemu/timer.h"
Gerd Hoffmann2e16ec02020-07-02 15:25:22 +020031#include "qemu/error-report.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "hw/timer/i8254.h"
Markus Armbrusterd6454272019-08-12 07:23:45 +020033#include "migration/vmstate.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010034#include "hw/audio/pcspk.h"
Efimov Vasily873b4d32016-06-22 15:24:46 +030035#include "qapi/error.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040036#include "qom/object.h"
bellardfd06c372006-04-24 21:58:30 +000037
38#define PCSPK_BUF_LEN 1792
39#define PCSPK_SAMPLE_RATE 32000
40#define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
Laurent Vivierb988a652016-05-31 18:35:57 +020041#define PCSPK_MIN_COUNT DIV_ROUND_UP(PIT_FREQ, PCSPK_MAX_FREQ)
bellardfd06c372006-04-24 21:58:30 +000042
Eduardo Habkost80633962020-09-16 14:25:19 -040043OBJECT_DECLARE_SIMPLE_TYPE(PCSpkState, PC_SPEAKER)
Andreas Färberd367ece2013-04-27 22:18:48 +020044
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040045struct PCSpkState {
Andreas Färberd367ece2013-04-27 22:18:48 +020046 ISADevice parent_obj;
47
Jan Kiszka302fe512012-02-17 11:24:34 +010048 MemoryRegion ioport;
49 uint32_t iobase;
bellardfd06c372006-04-24 21:58:30 +000050 uint8_t sample_buf[PCSPK_BUF_LEN];
51 QEMUSoundCard card;
52 SWVoiceOut *voice;
Jan Kiszka302fe512012-02-17 11:24:34 +010053 void *pit;
bellardfd06c372006-04-24 21:58:30 +000054 unsigned int pit_count;
55 unsigned int samples;
56 unsigned int play_pos;
Pavel Dovgalyuk39c88f52016-09-15 12:01:33 +030057 uint8_t data_on;
58 uint8_t dummy_refresh_clock;
Dr. David Alan Gilbert04e27c62016-11-28 13:32:00 +000059 bool migrate;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040060};
bellardfd06c372006-04-24 21:58:30 +000061
62static const char *s_spk = "pcspk";
Gerd Hoffmann28605a22019-03-28 08:11:21 +010063static PCSpkState *pcspk_state;
bellardfd06c372006-04-24 21:58:30 +000064
65static inline void generate_samples(PCSpkState *s)
66{
67 unsigned int i;
68
69 if (s->pit_count) {
70 const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
71 const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
72
73 /* multiple of wavelength for gapless looping */
Marc-André Lureau668c2d12017-06-22 13:04:16 +020074 s->samples = (QEMU_ALIGN_DOWN(PCSPK_BUF_LEN * PIT_FREQ, m) / (PIT_FREQ >> 1) + 1) >> 1;
bellardfd06c372006-04-24 21:58:30 +000075 for (i = 0; i < s->samples; ++i)
76 s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
77 } else {
78 s->samples = PCSPK_BUF_LEN;
79 for (i = 0; i < PCSPK_BUF_LEN; ++i)
80 s->sample_buf[i] = 128; /* silence */
81 }
82}
83
84static void pcspk_callback(void *opaque, int free)
85{
86 PCSpkState *s = opaque;
Jan Kiszka4aa5d282012-02-01 20:31:43 +010087 PITChannelInfo ch;
bellardfd06c372006-04-24 21:58:30 +000088 unsigned int n;
89
Jan Kiszka4aa5d282012-02-01 20:31:43 +010090 pit_get_channel_info(s->pit, 2, &ch);
bellardfd06c372006-04-24 21:58:30 +000091
Jan Kiszka4aa5d282012-02-01 20:31:43 +010092 if (ch.mode != 3) {
93 return;
94 }
95
96 n = ch.initial_count;
bellardfd06c372006-04-24 21:58:30 +000097 /* avoid frequencies that are not reproducible with sample rate */
98 if (n < PCSPK_MIN_COUNT)
99 n = 0;
100
101 if (s->pit_count != n) {
102 s->pit_count = n;
103 s->play_pos = 0;
104 generate_samples(s);
105 }
106
107 while (free > 0) {
Kővágó, Zoltán58935912019-08-19 01:06:54 +0200108 n = MIN(s->samples - s->play_pos, (unsigned int)free);
bellardfd06c372006-04-24 21:58:30 +0000109 n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
110 if (!n)
111 break;
112 s->play_pos = (s->play_pos + n) % s->samples;
113 free -= n;
114 }
115}
116
Gerd Hoffmann2e16ec02020-07-02 15:25:22 +0200117static int pcspk_audio_init(PCSpkState *s)
Gerd Hoffmann28605a22019-03-28 08:11:21 +0100118{
Gerd Hoffmann28605a22019-03-28 08:11:21 +0100119 struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUDIO_FORMAT_U8, 0};
120
Gerd Hoffmann2e16ec02020-07-02 15:25:22 +0200121 if (s->voice) {
122 /* already initialized */
123 return 0;
124 }
125
Gerd Hoffmann28605a22019-03-28 08:11:21 +0100126 AUD_register_card(s_spk, &s->card);
127
128 s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
129 if (!s->voice) {
130 AUD_log(s_spk, "Could not open voice\n");
131 return -1;
132 }
133
134 return 0;
135}
136
Avi Kivitya8170e52012-10-23 12:30:10 +0200137static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
Jan Kiszka302fe512012-02-17 11:24:34 +0100138 unsigned size)
bellardfd06c372006-04-24 21:58:30 +0000139{
140 PCSpkState *s = opaque;
Jan Kiszka4aa5d282012-02-01 20:31:43 +0100141 PITChannelInfo ch;
142
143 pit_get_channel_info(s->pit, 2, &ch);
bellardfd06c372006-04-24 21:58:30 +0000144
145 s->dummy_refresh_clock ^= (1 << 4);
bellardfd06c372006-04-24 21:58:30 +0000146
Jan Kiszka4aa5d282012-02-01 20:31:43 +0100147 return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
148 (ch.out << 5);
bellardfd06c372006-04-24 21:58:30 +0000149}
150
Avi Kivitya8170e52012-10-23 12:30:10 +0200151static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
Jan Kiszka302fe512012-02-17 11:24:34 +0100152 unsigned size)
bellardfd06c372006-04-24 21:58:30 +0000153{
154 PCSpkState *s = opaque;
155 const int gate = val & 1;
156
157 s->data_on = (val >> 1) & 1;
158 pit_set_gate(s->pit, 2, gate);
159 if (s->voice) {
160 if (gate) /* restart */
161 s->play_pos = 0;
162 AUD_set_active_out(s->voice, gate & s->data_on);
163 }
164}
165
Jan Kiszka302fe512012-02-17 11:24:34 +0100166static const MemoryRegionOps pcspk_io_ops = {
167 .read = pcspk_io_read,
168 .write = pcspk_io_write,
169 .impl = {
170 .min_access_size = 1,
171 .max_access_size = 1,
172 },
173};
bellardfd06c372006-04-24 21:58:30 +0000174
Andreas Färberdb895a12012-11-25 02:37:14 +0100175static void pcspk_initfn(Object *obj)
Jan Kiszka302fe512012-02-17 11:24:34 +0100176{
Andreas Färberdb895a12012-11-25 02:37:14 +0100177 PCSpkState *s = PC_SPEAKER(obj);
Jan Kiszka302fe512012-02-17 11:24:34 +0100178
Jan Kiszkaecf2e5a2015-03-19 13:08:40 +0100179 memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1);
Efimov Vasily873b4d32016-06-22 15:24:46 +0300180
Paolo Bonzini8a0b4de2016-06-30 16:57:37 +0200181 object_property_add_link(obj, "pit", TYPE_PIT_COMMON,
Efimov Vasily873b4d32016-06-22 15:24:46 +0300182 (Object **)&s->pit,
183 qdev_prop_allow_set_link_before_realize,
Markus Armbrusterd2623122020-05-05 17:29:22 +0200184 0);
Andreas Färberdb895a12012-11-25 02:37:14 +0100185}
186
187static void pcspk_realizefn(DeviceState *dev, Error **errp)
188{
189 ISADevice *isadev = ISA_DEVICE(dev);
190 PCSpkState *s = PC_SPEAKER(dev);
191
192 isa_register_ioport(isadev, &s->ioport, s->iobase);
Jan Kiszka302fe512012-02-17 11:24:34 +0100193
Gerd Hoffmann2e16ec02020-07-02 15:25:22 +0200194 if (s->card.state) {
195 pcspk_audio_init(s);
196 }
197
Gerd Hoffmann28605a22019-03-28 08:11:21 +0100198 pcspk_state = s;
bellardfd06c372006-04-24 21:58:30 +0000199}
Jan Kiszka302fe512012-02-17 11:24:34 +0100200
Dr. David Alan Gilbert04e27c62016-11-28 13:32:00 +0000201static bool migrate_needed(void *opaque)
202{
203 PCSpkState *s = opaque;
204
205 return s->migrate;
206}
207
Pavel Dovgalyuk39c88f52016-09-15 12:01:33 +0300208static const VMStateDescription vmstate_spk = {
209 .name = "pcspk",
210 .version_id = 1,
211 .minimum_version_id = 1,
212 .minimum_version_id_old = 1,
Dr. David Alan Gilbert04e27c62016-11-28 13:32:00 +0000213 .needed = migrate_needed,
Pavel Dovgalyuk39c88f52016-09-15 12:01:33 +0300214 .fields = (VMStateField[]) {
215 VMSTATE_UINT8(data_on, PCSpkState),
216 VMSTATE_UINT8(dummy_refresh_clock, PCSpkState),
217 VMSTATE_END_OF_LIST()
218 }
219};
220
Jan Kiszka302fe512012-02-17 11:24:34 +0100221static Property pcspk_properties[] = {
Kővágó, Zoltán88e47b92019-08-19 01:06:49 +0200222 DEFINE_AUDIO_PROPERTIES(PCSpkState, card),
Gerd Hoffmann23361722020-07-02 15:25:25 +0200223 DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, 0x61),
Dr. David Alan Gilbert04e27c62016-11-28 13:32:00 +0000224 DEFINE_PROP_BOOL("migrate", PCSpkState, migrate, true),
Jan Kiszka302fe512012-02-17 11:24:34 +0100225 DEFINE_PROP_END_OF_LIST(),
226};
227
228static void pcspk_class_initfn(ObjectClass *klass, void *data)
229{
230 DeviceClass *dc = DEVICE_CLASS(klass);
Jan Kiszka302fe512012-02-17 11:24:34 +0100231
Andreas Färberdb895a12012-11-25 02:37:14 +0100232 dc->realize = pcspk_realizefn;
Marcel Apfelbaum125ee0e2013-07-29 17:17:45 +0300233 set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
Pavel Dovgalyuk39c88f52016-09-15 12:01:33 +0300234 dc->vmsd = &vmstate_spk;
Marc-André Lureau4f67d302020-01-10 19:30:32 +0400235 device_class_set_props(dc, pcspk_properties);
Gerd Hoffmann28605a22019-03-28 08:11:21 +0100236 /* Reason: realize sets global pcspk_state */
237 /* Reason: pit object link */
238 dc->user_creatable = false;
Jan Kiszka302fe512012-02-17 11:24:34 +0100239}
240
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100241static const TypeInfo pcspk_info = {
Andreas Färberd367ece2013-04-27 22:18:48 +0200242 .name = TYPE_PC_SPEAKER,
Jan Kiszka302fe512012-02-17 11:24:34 +0100243 .parent = TYPE_ISA_DEVICE,
244 .instance_size = sizeof(PCSpkState),
Andreas Färberdb895a12012-11-25 02:37:14 +0100245 .instance_init = pcspk_initfn,
Jan Kiszka302fe512012-02-17 11:24:34 +0100246 .class_init = pcspk_class_initfn,
247};
248
Gerd Hoffmann2e16ec02020-07-02 15:25:22 +0200249static int pcspk_audio_init_soundhw(ISABus *bus)
250{
251 PCSpkState *s = pcspk_state;
252
253 warn_report("'-soundhw pcspk' is deprecated, "
254 "please set a backend using '-machine pcspk-audiodev=<name>' instead");
255 return pcspk_audio_init(s);
256}
257
Jan Kiszka302fe512012-02-17 11:24:34 +0100258static void pcspk_register(void)
259{
260 type_register_static(&pcspk_info);
Gerd Hoffmann2e16ec02020-07-02 15:25:22 +0200261 isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init_soundhw);
Jan Kiszka302fe512012-02-17 11:24:34 +0100262}
263type_init(pcspk_register)