xref: /openbmc/qemu/hw/audio/pcspk.c (revision 59a3a1c0)
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 
25 #include "qemu/osdep.h"
26 #include "hw/isa/isa.h"
27 #include "hw/audio/soundhw.h"
28 #include "audio/audio.h"
29 #include "qemu/module.h"
30 #include "qemu/timer.h"
31 #include "hw/timer/i8254.h"
32 #include "migration/vmstate.h"
33 #include "hw/audio/pcspk.h"
34 #include "qapi/error.h"
35 
36 #define PCSPK_BUF_LEN 1792
37 #define PCSPK_SAMPLE_RATE 32000
38 #define PCSPK_MAX_FREQ (PCSPK_SAMPLE_RATE >> 1)
39 #define PCSPK_MIN_COUNT DIV_ROUND_UP(PIT_FREQ, PCSPK_MAX_FREQ)
40 
41 #define PC_SPEAKER(obj) OBJECT_CHECK(PCSpkState, (obj), TYPE_PC_SPEAKER)
42 
43 typedef struct {
44     ISADevice parent_obj;
45 
46     MemoryRegion ioport;
47     uint32_t iobase;
48     uint8_t sample_buf[PCSPK_BUF_LEN];
49     QEMUSoundCard card;
50     SWVoiceOut *voice;
51     void *pit;
52     unsigned int pit_count;
53     unsigned int samples;
54     unsigned int play_pos;
55     uint8_t data_on;
56     uint8_t dummy_refresh_clock;
57     bool migrate;
58 } PCSpkState;
59 
60 static const char *s_spk = "pcspk";
61 static PCSpkState *pcspk_state;
62 
63 static inline void generate_samples(PCSpkState *s)
64 {
65     unsigned int i;
66 
67     if (s->pit_count) {
68         const uint32_t m = PCSPK_SAMPLE_RATE * s->pit_count;
69         const uint32_t n = ((uint64_t)PIT_FREQ << 32) / m;
70 
71         /* multiple of wavelength for gapless looping */
72         s->samples = (QEMU_ALIGN_DOWN(PCSPK_BUF_LEN * PIT_FREQ, m) / (PIT_FREQ >> 1) + 1) >> 1;
73         for (i = 0; i < s->samples; ++i)
74             s->sample_buf[i] = (64 & (n * i >> 25)) - 32;
75     } else {
76         s->samples = PCSPK_BUF_LEN;
77         for (i = 0; i < PCSPK_BUF_LEN; ++i)
78             s->sample_buf[i] = 128; /* silence */
79     }
80 }
81 
82 static void pcspk_callback(void *opaque, int free)
83 {
84     PCSpkState *s = opaque;
85     PITChannelInfo ch;
86     unsigned int n;
87 
88     pit_get_channel_info(s->pit, 2, &ch);
89 
90     if (ch.mode != 3) {
91         return;
92     }
93 
94     n = ch.initial_count;
95     /* avoid frequencies that are not reproducible with sample rate */
96     if (n < PCSPK_MIN_COUNT)
97         n = 0;
98 
99     if (s->pit_count != n) {
100         s->pit_count = n;
101         s->play_pos = 0;
102         generate_samples(s);
103     }
104 
105     while (free > 0) {
106         n = audio_MIN(s->samples - s->play_pos, (unsigned int)free);
107         n = AUD_write(s->voice, &s->sample_buf[s->play_pos], n);
108         if (!n)
109             break;
110         s->play_pos = (s->play_pos + n) % s->samples;
111         free -= n;
112     }
113 }
114 
115 static int pcspk_audio_init(ISABus *bus)
116 {
117     PCSpkState *s = pcspk_state;
118     struct audsettings as = {PCSPK_SAMPLE_RATE, 1, AUDIO_FORMAT_U8, 0};
119 
120     AUD_register_card(s_spk, &s->card);
121 
122     s->voice = AUD_open_out(&s->card, s->voice, s_spk, s, pcspk_callback, &as);
123     if (!s->voice) {
124         AUD_log(s_spk, "Could not open voice\n");
125         return -1;
126     }
127 
128     return 0;
129 }
130 
131 static uint64_t pcspk_io_read(void *opaque, hwaddr addr,
132                               unsigned size)
133 {
134     PCSpkState *s = opaque;
135     PITChannelInfo ch;
136 
137     pit_get_channel_info(s->pit, 2, &ch);
138 
139     s->dummy_refresh_clock ^= (1 << 4);
140 
141     return ch.gate | (s->data_on << 1) | s->dummy_refresh_clock |
142        (ch.out << 5);
143 }
144 
145 static void pcspk_io_write(void *opaque, hwaddr addr, uint64_t val,
146                            unsigned size)
147 {
148     PCSpkState *s = opaque;
149     const int gate = val & 1;
150 
151     s->data_on = (val >> 1) & 1;
152     pit_set_gate(s->pit, 2, gate);
153     if (s->voice) {
154         if (gate) /* restart */
155             s->play_pos = 0;
156         AUD_set_active_out(s->voice, gate & s->data_on);
157     }
158 }
159 
160 static const MemoryRegionOps pcspk_io_ops = {
161     .read = pcspk_io_read,
162     .write = pcspk_io_write,
163     .impl = {
164         .min_access_size = 1,
165         .max_access_size = 1,
166     },
167 };
168 
169 static void pcspk_initfn(Object *obj)
170 {
171     PCSpkState *s = PC_SPEAKER(obj);
172 
173     memory_region_init_io(&s->ioport, OBJECT(s), &pcspk_io_ops, s, "pcspk", 1);
174 
175     object_property_add_link(obj, "pit", TYPE_PIT_COMMON,
176                              (Object **)&s->pit,
177                              qdev_prop_allow_set_link_before_realize,
178                              0, &error_abort);
179 }
180 
181 static void pcspk_realizefn(DeviceState *dev, Error **errp)
182 {
183     ISADevice *isadev = ISA_DEVICE(dev);
184     PCSpkState *s = PC_SPEAKER(dev);
185 
186     isa_register_ioport(isadev, &s->ioport, s->iobase);
187 
188     pcspk_state = s;
189 }
190 
191 static bool migrate_needed(void *opaque)
192 {
193     PCSpkState *s = opaque;
194 
195     return s->migrate;
196 }
197 
198 static const VMStateDescription vmstate_spk = {
199     .name = "pcspk",
200     .version_id = 1,
201     .minimum_version_id = 1,
202     .minimum_version_id_old = 1,
203     .needed = migrate_needed,
204     .fields      = (VMStateField[]) {
205         VMSTATE_UINT8(data_on, PCSpkState),
206         VMSTATE_UINT8(dummy_refresh_clock, PCSpkState),
207         VMSTATE_END_OF_LIST()
208     }
209 };
210 
211 static Property pcspk_properties[] = {
212     DEFINE_PROP_UINT32("iobase", PCSpkState, iobase,  -1),
213     DEFINE_PROP_BOOL("migrate", PCSpkState, migrate,  true),
214     DEFINE_PROP_END_OF_LIST(),
215 };
216 
217 static void pcspk_class_initfn(ObjectClass *klass, void *data)
218 {
219     DeviceClass *dc = DEVICE_CLASS(klass);
220 
221     dc->realize = pcspk_realizefn;
222     set_bit(DEVICE_CATEGORY_SOUND, dc->categories);
223     dc->vmsd = &vmstate_spk;
224     dc->props = pcspk_properties;
225     /* Reason: realize sets global pcspk_state */
226     /* Reason: pit object link */
227     dc->user_creatable = false;
228 }
229 
230 static const TypeInfo pcspk_info = {
231     .name           = TYPE_PC_SPEAKER,
232     .parent         = TYPE_ISA_DEVICE,
233     .instance_size  = sizeof(PCSpkState),
234     .instance_init  = pcspk_initfn,
235     .class_init     = pcspk_class_initfn,
236 };
237 
238 static void pcspk_register(void)
239 {
240     type_register_static(&pcspk_info);
241     isa_register_soundhw("pcspk", "PC speaker", pcspk_audio_init);
242 }
243 type_init(pcspk_register)
244