2006-04-25 01:58:30 +04:00
|
|
|
/*
|
|
|
|
* QEMU PC speaker emulation
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Joachim Henke
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-01-18 20:33:52 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/isa/isa.h"
|
2017-05-08 23:57:35 +03:00
|
|
|
#include "hw/audio/soundhw.h"
|
2007-11-17 20:14:51 +03:00
|
|
|
#include "audio/audio.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2020-07-02 16:25:22 +03:00
|
|
|
#include "qemu/error-report.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/timer/i8254.h"
|
2019-08-12 08:23:45 +03:00
|
|
|
#include "migration/vmstate.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/audio/pcspk.h"
|
2016-06-22 15:24:46 +03:00
|
|
|
#include "qapi/error.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2006-04-25 01:58:30 +04:00
|
|
|
|
|
|
|
#define PCSPK_BUF_LEN 1792
|
|
|
|
#define PCSPK_SAMPLE_RATE 32000
|
|
|
|
#define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
|
2016-05-31 19:35:57 +03:00
|
|
|
#define PCSPK_MIN_COUNT DIV_ROUND_UP(PIT_FREQ, PCSPK_MAX_FREQ)
|
2006-04-25 01:58:30 +04:00
|
|
|
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(PCSpkState, PC_SPEAKER)
|
2013-04-28 00:18:48 +04:00
|
|
|
|
2020-09-03 23:43:22 +03:00
|
|
|
struct PCSpkState {
|
2013-04-28 00:18:48 +04:00
|
|
|
ISADevice parent_obj;
|
|
|
|
|
2012-02-17 14:24:34 +04:00
|
|
|
MemoryRegion ioport;
|
|
|
|
uint32_t iobase;
|
2006-04-25 01:58:30 +04:00
|
|
|
uint8_t sample_buf[PCSPK_BUF_LEN];
|
|
|
|
QEMUSoundCard card;
|
|
|
|
SWVoiceOut *voice;
|
2012-02-17 14:24:34 +04:00
|
|
|
void *pit;
|
2006-04-25 01:58:30 +04:00
|
|
|
unsigned int pit_count;
|
|
|
|
unsigned int samples;
|
|
|
|
unsigned int play_pos;
|
2016-09-15 12:01:33 +03:00
|
|
|
uint8_t data_on;
|
|
|
|
uint8_t dummy_refresh_clock;
|
2016-11-28 16:32:00 +03:00
|
|
|
bool migrate;
|
2020-09-03 23:43:22 +03:00
|
|
|
};
|
2006-04-25 01:58:30 +04:00
|
|
|
|
|
|
|
static const char *s_spk = "pcspk";
|
2019-03-28 10:11:21 +03:00
|
|
|
static PCSpkState *pcspk_state;
|
2006-04-25 01:58:30 +04:00
|
|
|
|
|
|
|
static inline void generate_samples(PCSpkState *s)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (s->pit_count) {
|
|
|
|
const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
|
|
|
|
const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
|
|
|
|
|
|
|
|
/* multiple of wavelength for gapless looping */
|
2017-06-22 14:04:16 +03:00
|
|
|
s->samples = (QEMU_ALIGN_DOWN(PCSPK_BUF_LEN * PIT_FREQ, m) / (PIT_FREQ >> 1) + 1) >> 1;
|
2006-04-25 01:58:30 +04:00
|
|
|
for (i = 0; i < s->samples; ++i)
|
|
|
|
s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
|
|
|
|
} else {
|
|
|
|
s->samples = PCSPK_BUF_LEN;
|
|
|
|
for (i = 0; i < PCSPK_BUF_LEN; ++i)
|
|
|
|
s->sample_buf[i] = 128; /* silence */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pcspk_callback(void *opaque, int free)
|
|
|
|
{
|
|
|
|
PCSpkState *s = opaque;
|
2012-02-01 23:31:43 +04:00
|
|
|
PITChannelInfo ch;
|
2006-04-25 01:58:30 +04:00
|
|
|
unsigned int n;
|
|
|
|
|
2012-02-01 23:31:43 +04:00
|
|
|
pit_get_channel_info(s->pit, 2, &ch);
|
|
|
|
|
|
|
|
if (ch.mode != 3) {
|
2006-04-25 01:58:30 +04:00
|
|
|
return;
|
2012-02-01 23:31:43 +04:00
|
|
|
}
|
2006-04-25 01:58:30 +04:00
|
|
|
|
2012-02-01 23:31:43 +04:00
|
|
|
n = ch.initial_count;
|
2006-04-25 01:58:30 +04:00
|
|
|
/* avoid frequencies that are not reproducible with sample rate */
|
|
|
|
if (n < PCSPK_MIN_COUNT)
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
if (s->pit_count != n) {
|
|
|
|
s->pit_count = n;
|
|
|
|
s->play_pos = 0;
|
|
|
|
generate_samples(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (free > 0) {
|
2019-08-19 02:06:54 +03:00
|
|
|
n = MIN(s->samples - s->play_pos, (unsigned int)free);
|
2006-04-25 01:58:30 +04:00
|
|
|
n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
|
|
|
|
if (!n)
|
|
|
|
break;
|
|
|
|
s->play_pos = (s->play_pos + n) % s->samples;
|
|
|
|
free -= n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:25:22 +03:00
|
|
|
static int pcspk_audio_init(PCSpkState *s)
|
2019-03-28 10:11:21 +03:00
|
|
|
{
|
|
|
|
struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUDIO_FORMAT_U8, 0};
|
|
|
|
|
2020-07-02 16:25:22 +03:00
|
|
|
if (s->voice) {
|
|
|
|
/* already initialized */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-28 10:11:21 +03:00
|
|
|
AUD_register_card(s_spk, &s->card);
|
|
|
|
|
|
|
|
s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
|
|
|
|
if (!s->voice) {
|
|
|
|
AUD_log(s_spk, "Could not open voice\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
|
2012-02-17 14:24:34 +04:00
|
|
|
unsigned size)
|
2006-04-25 01:58:30 +04:00
|
|
|
{
|
|
|
|
PCSpkState *s = opaque;
|
2012-02-01 23:31:43 +04:00
|
|
|
PITChannelInfo ch;
|
|
|
|
|
|
|
|
pit_get_channel_info(s->pit, 2, &ch);
|
2006-04-25 01:58:30 +04:00
|
|
|
|
|
|
|
s->dummy_refresh_clock ^= (1 << 4);
|
|
|
|
|
2012-02-01 23:31:43 +04:00
|
|
|
return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
|
|
|
|
(ch.out << 5);
|
2006-04-25 01:58:30 +04:00
|
|
|
}
|
|
|
|
|
2012-10-23 14:30:10 +04:00
|
|
|
static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
|
2012-02-17 14:24:34 +04:00
|
|
|
unsigned size)
|
2006-04-25 01:58:30 +04:00
|
|
|
{
|
|
|
|
PCSpkState *s = opaque;
|
|
|
|
const int gate = val & 1;
|
|
|
|
|
|
|
|
s->data_on = (val >> 1) & 1;
|
|
|
|
pit_set_gate(s->pit, 2, gate);
|
|
|
|
if (s->voice) {
|
|
|
|
if (gate) /* restart */
|
|
|
|
s->play_pos = 0;
|
|
|
|
AUD_set_active_out(s->voice, gate & s->data_on);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 14:24:34 +04:00
|
|
|
static const MemoryRegionOps pcspk_io_ops = {
|
|
|
|
.read = pcspk_io_read,
|
|
|
|
.write = pcspk_io_write,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-11-25 05:37:14 +04:00
|
|
|
static void pcspk_initfn(Object *obj)
|
2006-04-25 01:58:30 +04:00
|
|
|
{
|
2012-11-25 05:37:14 +04:00
|
|
|
PCSpkState *s = PC_SPEAKER(obj);
|
2012-02-17 14:24:34 +04:00
|
|
|
|
2015-03-19 15:08:40 +03:00
|
|
|
memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1);
|
2016-06-22 15:24:46 +03:00
|
|
|
|
2016-06-30 17:57:37 +03:00
|
|
|
object_property_add_link(obj, "pit", TYPE_PIT_COMMON,
|
2016-06-22 15:24:46 +03:00
|
|
|
(Object **)&s->pit,
|
|
|
|
qdev_prop_allow_set_link_before_realize,
|
qom: Drop parameter @errp of object_property_add() & friends
The only way object_property_add() can fail is when a property with
the same name already exists. Since our property names are all
hardcoded, failure is a programming error, and the appropriate way to
handle it is passing &error_abort.
Same for its variants, except for object_property_add_child(), which
additionally fails when the child already has a parent. Parentage is
also under program control, so this is a programming error, too.
We have a bit over 500 callers. Almost half of them pass
&error_abort, slightly fewer ignore errors, one test case handles
errors, and the remaining few callers pass them to their own callers.
The previous few commits demonstrated once again that ignoring
programming errors is a bad idea.
Of the few ones that pass on errors, several violate the Error API.
The Error ** argument must be NULL, &error_abort, &error_fatal, or a
pointer to a variable containing NULL. Passing an argument of the
latter kind twice without clearing it in between is wrong: if the
first call sets an error, it no longer points to NULL for the second
call. ich9_pm_add_properties(), sparc32_ledma_realize(),
sparc32_dma_realize(), xilinx_axidma_realize(), xilinx_enet_realize()
are wrong that way.
When the one appropriate choice of argument is &error_abort, letting
users pick the argument is a bad idea.
Drop parameter @errp and assert the preconditions instead.
There's one exception to "duplicate property name is a programming
error": the way object_property_add() implements the magic (and
undocumented) "automatic arrayification". Don't drop @errp there.
Instead, rename object_property_add() to object_property_try_add(),
and add the obvious wrapper object_property_add().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-15-armbru@redhat.com>
[Two semantic rebase conflicts resolved]
2020-05-05 18:29:22 +03:00
|
|
|
0);
|
2012-11-25 05:37:14 +04:00
|
|
|
}
|
2012-02-17 14:24:34 +04:00
|
|
|
|
2012-11-25 05:37:14 +04:00
|
|
|
static void pcspk_realizefn(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
ISADevice *isadev = ISA_DEVICE(dev);
|
|
|
|
PCSpkState *s = PC_SPEAKER(dev);
|
2012-02-17 14:24:34 +04:00
|
|
|
|
2012-11-25 05:37:14 +04:00
|
|
|
isa_register_ioport(isadev, &s->ioport, s->iobase);
|
|
|
|
|
2020-07-02 16:25:22 +03:00
|
|
|
if (s->card.state) {
|
|
|
|
pcspk_audio_init(s);
|
|
|
|
}
|
|
|
|
|
2019-03-28 10:11:21 +03:00
|
|
|
pcspk_state = s;
|
2012-02-17 14:24:34 +04:00
|
|
|
}
|
|
|
|
|
2016-11-28 16:32:00 +03:00
|
|
|
static bool migrate_needed(void *opaque)
|
|
|
|
{
|
|
|
|
PCSpkState *s = opaque;
|
|
|
|
|
|
|
|
return s->migrate;
|
|
|
|
}
|
|
|
|
|
2016-09-15 12:01:33 +03:00
|
|
|
static const VMStateDescription vmstate_spk = {
|
|
|
|
.name = "pcspk",
|
|
|
|
.version_id = 1,
|
|
|
|
.minimum_version_id = 1,
|
|
|
|
.minimum_version_id_old = 1,
|
2016-11-28 16:32:00 +03:00
|
|
|
.needed = migrate_needed,
|
2016-09-15 12:01:33 +03:00
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_UINT8(data_on, PCSpkState),
|
|
|
|
VMSTATE_UINT8(dummy_refresh_clock, PCSpkState),
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-02-17 14:24:34 +04:00
|
|
|
static Property pcspk_properties[] = {
|
2019-08-19 02:06:49 +03:00
|
|
|
DEFINE_AUDIO_PROPERTIES(PCSpkState, card),
|
2020-07-02 16:25:25 +03:00
|
|
|
DEFINE_PROP_UINT32("iobase", PCSpkState, iobase, 0x61),
|
2016-11-28 16:32:00 +03:00
|
|
|
DEFINE_PROP_BOOL("migrate", PCSpkState, migrate, true),
|
2012-02-17 14:24:34 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
2006-04-25 01:58:30 +04:00
|
|
|
|
2012-02-17 14:24:34 +04:00
|
|
|
static void pcspk_class_initfn(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
|
2012-11-25 05:37:14 +04:00
|
|
|
dc->realize = pcspk_realizefn;
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
|
2016-09-15 12:01:33 +03:00
|
|
|
dc->vmsd = &vmstate_spk;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, pcspk_properties);
|
2019-03-28 10:11:21 +03:00
|
|
|
/* Reason: realize sets global pcspk_state */
|
|
|
|
/* Reason: pit object link */
|
|
|
|
dc->user_creatable = false;
|
2012-02-17 14:24:34 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo pcspk_info = {
|
2013-04-28 00:18:48 +04:00
|
|
|
.name = TYPE_PC_SPEAKER,
|
2012-02-17 14:24:34 +04:00
|
|
|
.parent = TYPE_ISA_DEVICE,
|
|
|
|
.instance_size = sizeof(PCSpkState),
|
2012-11-25 05:37:14 +04:00
|
|
|
.instance_init = pcspk_initfn,
|
2012-02-17 14:24:34 +04:00
|
|
|
.class_init = pcspk_class_initfn,
|
|
|
|
};
|
|
|
|
|
2020-07-02 16:25:22 +03:00
|
|
|
static int pcspk_audio_init_soundhw(ISABus *bus)
|
|
|
|
{
|
|
|
|
PCSpkState *s = pcspk_state;
|
|
|
|
|
|
|
|
warn_report("'-soundhw pcspk' is deprecated, "
|
|
|
|
"please set a backend using '-machine pcspk-audiodev=<name>' instead");
|
|
|
|
return pcspk_audio_init(s);
|
|
|
|
}
|
|
|
|
|
2012-02-17 14:24:34 +04:00
|
|
|
static void pcspk_register(void)
|
|
|
|
{
|
|
|
|
type_register_static(&pcspk_info);
|
2020-07-02 16:25:22 +03:00
|
|
|
isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init_soundhw);
|
2006-04-25 01:58:30 +04:00
|
|
|
}
|
2012-02-17 14:24:34 +04:00
|
|
|
type_init(pcspk_register)
|