1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Memory.h>
#include <AK/Singleton.h>
#include <AK/StringView.h>
#include <Kernel/Arch/x86/InterruptDisabler.h>
#include <Kernel/Debug.h>
#include <Kernel/Devices/SB16.h>
#include <Kernel/IO.h>
#include <Kernel/Thread.h>
#include <Kernel/VM/AnonymousVMObject.h>
#include <Kernel/VM/MemoryManager.h>
namespace Kernel {
#define SB16_DEFAULT_IRQ 5
enum class SampleFormat : u8 {
Signed = 0x10,
Stereo = 0x20,
};
const u16 DSP_READ = 0x22A;
const u16 DSP_WRITE = 0x22C;
const u16 DSP_STATUS = 0x22E;
const u16 DSP_R_ACK = 0x22F;
/* Write a value to the DSP write register */
void SB16::dsp_write(u8 value)
{
while (IO::in8(DSP_WRITE) & 0x80) {
;
}
IO::out8(DSP_WRITE, value);
}
/* Reads the value of the DSP read register */
u8 SB16::dsp_read()
{
while (!(IO::in8(DSP_STATUS) & 0x80)) {
;
}
return IO::in8(DSP_READ);
}
/* Changes the sample rate of sound output */
void SB16::set_sample_rate(uint16_t hz)
{
dsp_write(0x41); // output
dsp_write((u8)(hz >> 8));
dsp_write((u8)hz);
dsp_write(0x42); // input
dsp_write((u8)(hz >> 8));
dsp_write((u8)hz);
}
static AK::Singleton<SB16> s_the;
UNMAP_AFTER_INIT SB16::SB16()
: IRQHandler(SB16_DEFAULT_IRQ)
, CharacterDevice(42, 42) // ### ?
{
initialize();
}
UNMAP_AFTER_INIT SB16::~SB16()
{
}
UNMAP_AFTER_INIT void SB16::detect()
{
IO::out8(0x226, 1);
IO::delay(32);
IO::out8(0x226, 0);
auto data = dsp_read();
if (data != 0xaa) {
return;
}
SB16::create();
}
UNMAP_AFTER_INIT void SB16::create()
{
s_the.ensure_instance();
}
SB16& SB16::the()
{
return *s_the;
}
UNMAP_AFTER_INIT void SB16::initialize()
{
disable_irq();
IO::out8(0x226, 1);
IO::delay(32);
IO::out8(0x226, 0);
auto data = dsp_read();
if (data != 0xaa) {
dbgln("SB16: SoundBlaster not ready");
return;
}
// Get the version info
dsp_write(0xe1);
m_major_version = dsp_read();
auto vmin = dsp_read();
dmesgln("SB16: Found version {}.{}", m_major_version, vmin);
set_irq_register(SB16_DEFAULT_IRQ);
dmesgln("SB16: IRQ {}", get_irq_line());
}
void SB16::set_irq_register(u8 irq_number)
{
u8 bitmask;
switch (irq_number) {
case 2:
bitmask = 0;
break;
case 5:
bitmask = 0b10;
break;
case 7:
bitmask = 0b100;
break;
case 10:
bitmask = 0b1000;
break;
default:
VERIFY_NOT_REACHED();
}
IO::out8(0x224, 0x80);
IO::out8(0x225, bitmask);
}
u8 SB16::get_irq_line()
{
IO::out8(0x224, 0x80);
u8 bitmask = IO::in8(0x225);
switch (bitmask) {
case 0:
return 2;
case 0b10:
return 5;
case 0b100:
return 7;
case 0b1000:
return 10;
}
return bitmask;
}
void SB16::set_irq_line(u8 irq_number)
{
InterruptDisabler disabler;
if (irq_number == get_irq_line())
return;
set_irq_register(irq_number);
change_irq_number(irq_number);
}
bool SB16::can_read(const FileDescription&, size_t) const
{
return false;
}
KResultOr<size_t> SB16::read(FileDescription&, u64, UserOrKernelBuffer&, size_t)
{
return 0;
}
void SB16::dma_start(uint32_t length)
{
const auto addr = m_dma_region->physical_page(0)->paddr().get();
const u8 channel = 5; // 16-bit samples use DMA channel 5 (on the master DMA controller)
const u8 mode = 0x48;
// Disable the DMA channel
IO::out8(0xd4, 4 + (channel % 4));
// Clear the byte pointer flip-flop
IO::out8(0xd8, 0);
// Write the DMA mode for the transfer
IO::out8(0xd6, (channel % 4) | mode);
// Write the offset of the buffer
u16 offset = (addr / 2) % 65536;
IO::out8(0xc4, (u8)offset);
IO::out8(0xc4, (u8)(offset >> 8));
// Write the transfer length
IO::out8(0xc6, (u8)(length - 1));
IO::out8(0xc6, (u8)((length - 1) >> 8));
// Write the buffer
IO::out8(0x8b, addr >> 16);
auto page_number = addr >> 16;
VERIFY(page_number <= NumericLimits<u8>::max());
IO::out8(0x8b, page_number);
// Enable the DMA channel
IO::out8(0xd4, (channel % 4));
}
bool SB16::handle_irq(const RegisterState&)
{
// FIXME: Check if the interrupt was actually for us or not... (shared IRQs)
// Stop sound output ready for the next block.
dsp_write(0xd5);
IO::in8(DSP_STATUS); // 8 bit interrupt
if (m_major_version >= 4)
IO::in8(DSP_R_ACK); // 16 bit interrupt
m_irq_queue.wake_all();
return true;
}
void SB16::wait_for_irq()
{
m_irq_queue.wait_forever("SB16");
disable_irq();
}
KResultOr<size_t> SB16::write(FileDescription&, u64, const UserOrKernelBuffer& data, size_t length)
{
if (!m_dma_region) {
auto page = MM.allocate_supervisor_physical_page();
if (!page)
return ENOMEM;
auto vmobject = AnonymousVMObject::create_with_physical_page(*page);
if (!vmobject)
return ENOMEM;
m_dma_region = MM.allocate_kernel_region_with_vmobject(*vmobject, PAGE_SIZE, "SB16 DMA buffer", Region::Access::Write);
if (!m_dma_region)
return ENOMEM;
}
dbgln_if(SB16_DEBUG, "SB16: Writing buffer of {} bytes", length);
VERIFY(length <= PAGE_SIZE);
const int BLOCK_SIZE = 32 * 1024;
if (length > BLOCK_SIZE) {
return ENOSPC;
}
u8 mode = (u8)SampleFormat::Signed | (u8)SampleFormat::Stereo;
const int sample_rate = 44100;
set_sample_rate(sample_rate);
if (!data.read(m_dma_region->vaddr().as_ptr(), length))
return EFAULT;
dma_start(length);
// 16-bit single-cycle output.
// FIXME: Implement auto-initialized output.
u8 command = 0xb0;
u16 sample_count = length / sizeof(i16);
if (mode & (u8)SampleFormat::Stereo)
sample_count /= 2;
sample_count -= 1;
cli();
enable_irq();
dsp_write(command);
dsp_write(mode);
dsp_write((u8)sample_count);
dsp_write((u8)(sample_count >> 8));
wait_for_irq();
return length;
}
}
|