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