Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added sound driver based on virtio v1.2. #137

Merged
merged 78 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
9ebda13
Add more traits for struct ReadOnly, etc.
muxinyu1 Apr 26, 2024
5574b97
Add virtio sound driver, and its basic configurations are finished.
muxinyu1 Apr 26, 2024
e96533f
Implemented item information request.
muxinyu1 Apr 27, 2024
bc59270
Implemented available_jacks(), but left some errors.
muxinyu1 Apr 28, 2024
18adfec
Add some test code.
muxinyu1 Apr 29, 2024
3340ab6
Implemented pcm_set_params.
muxinyu1 Apr 29, 2024
927d6c3
Implemented the operations which are in different pcm stages.
muxinyu1 Apr 29, 2024
531d232
Configured the sound device correctly.
muxinyu1 May 1, 2024
48b32ce
Add test code.
muxinyu1 May 1, 2024
07b7dc8
Implemented all methods except I/O transfer.
muxinyu1 May 1, 2024
03ae7a3
Implemented some PCM methods.
muxinyu1 May 1, 2024
df95710
Add test code.
muxinyu1 May 3, 2024
8f78ee5
Fixed bugs.
muxinyu1 May 3, 2024
ecb57c7
Changed music size to be played.
muxinyu1 May 3, 2024
40340bf
Added error processing code.
muxinyu1 May 3, 2024
11f2811
Removed useless comments.
muxinyu1 May 5, 2024
01619b3
Implemented chmap infos.
muxinyu1 May 5, 2024
5e2004b
Implemented notifications.
muxinyu1 May 8, 2024
5ff955b
Implemented the sync version.
muxinyu1 May 8, 2024
55cec61
Try xfer twice.
muxinyu1 May 8, 2024
54318b0
Implemented pcm_xfer_with_buffer_size().
muxinyu1 May 8, 2024
86044f5
Add music Nocturne(夜曲 in Chinese).
muxinyu1 May 8, 2024
cef931e
Fixed pcm xfer bugs.
muxinyu1 May 9, 2024
f4d762d
Improved the smoothness of audio playback and fixed the pause bug dur…
muxinyu1 May 9, 2024
4c3759c
Added stop, release code.
muxinyu1 May 10, 2024
8764b15
Simplified code.
muxinyu1 May 12, 2024
d868645
Fixed bugs.
muxinyu1 May 13, 2024
05f2ff0
Increased buffer_bytes.
muxinyu1 May 14, 2024
7fca8ba
Added non-blocking method.
muxinyu1 May 14, 2024
cd50590
Replaced test music.
muxinyu1 Jun 3, 2024
f694b6b
Added docs.
muxinyu1 Jun 3, 2024
f2fb86b
Merge branch 'rcore-os:master' into master
muxinyu1 Jun 3, 2024
4177f41
Merge pull request #1 from muxinyu1/mxy-dev-nb
muxinyu1 Jun 3, 2024
9714a76
Removed unused code and fix some bugs.
muxinyu1 Jul 4, 2024
f9bb984
Merge branch 'rcore-os:master' into master
muxinyu1 Jul 4, 2024
4062ebd
Fix device ID test.
qwandor Jul 9, 2024
29166e3
cargo fmt
qwandor Jul 9, 2024
00f3bf1
Remove incorrect derived traits from ReadOnly.
qwandor Jul 9, 2024
95f1f6f
Config struct needs to be repr(C) to have correct layout.
qwandor Jul 9, 2024
272f98a
RING_INDIRECT_DESC feature is supported too.
qwandor Jul 9, 2024
c2aa987
Clean up use of bitflags.
qwandor Jul 9, 2024
cac4f53
Remove common prefix from flag names.
qwandor Jul 9, 2024
aad6a8d
Rename PcmRate to PcmRates.
qwandor Jul 9, 2024
b883a0d
Distinguish between a single format or rate and a set.
qwandor Jul 9, 2024
0a437b4
Use repr(transparent) for bitflags.
qwandor Jul 9, 2024
170110e
Don't derive Copy for complex structs.
qwandor Jul 9, 2024
f08f45e
Derive more traits where appropriate.
qwandor Jul 9, 2024
4520416
Implement Debug as well as Display.
qwandor Jul 9, 2024
e914e3f
Use standard format for safety comment.
qwandor Jul 9, 2024
7910b7e
Group imports.
qwandor Jul 9, 2024
5e8e1a8
Add test for reading config space.
qwandor Jul 9, 2024
c893aa7
Use ubuntu-24.04 for examples.
qwandor Jul 9, 2024
bec99c8
No need to Box a Vec.
qwandor Jul 9, 2024
2c67f25
Fix clippy lint warnings.
qwandor Jul 9, 2024
647d0ee
Use doc comments.
qwandor Jul 9, 2024
c83f0cc
Reduce the size of the test audio.
muxinyu1 Jul 10, 2024
1b5108c
Add sound to list of supported devices.
qwandor Jul 12, 2024
25d97fa
Use dummy audio device in CI.
qwandor Jul 12, 2024
57a2c51
Merge branch 'master' of github.com:muxinyu1/virtio-drivers
muxinyu1 Jul 16, 2024
1f06d28
Merge branch 'rcore-os:master' into master
muxinyu1 Jul 16, 2024
7aa92be
Add buffers to the event_queue for the device to write into.
muxinyu1 Jul 16, 2024
aee9dba
Merge remote-tracking branch 'origin/master' into mux
qwandor Jul 25, 2024
f9cd36a
Use OwningQueue for VirtIO sound event queue.
qwandor Jul 26, 2024
f7be927
Avoid allocating strings in Display implementation.
qwandor Jul 26, 2024
00095f8
Add fake sound device, and test getting stream info.
qwandor Jul 26, 2024
3e4eb14
Test empty stream/jack/chmap infos.
qwandor Jul 26, 2024
c8c40f6
Don't ignore errors getting pcm_infos.
qwandor Jul 26, 2024
bc71e36
Separate method for enabling interrupts.
qwandor Jul 26, 2024
edcc996
No need for generic return type for request function.
qwandor Jul 26, 2024
70b8c7b
Ignore trying to play an empty buffer.
qwandor Jul 26, 2024
aa59323
Unit test playing sound.
qwandor Jul 26, 2024
addb26b
Use add_notify_wait_pop when relevant.
qwandor Jul 26, 2024
c21497b
Validate buffer_bytes and period_bytes.
qwandor Jul 26, 2024
6720108
Make fake_read_write_queue return whether queue had anything available.
qwandor Jul 26, 2024
26fa867
Re-write pcm_xfer.
qwandor Jul 29, 2024
84928cb
Tidy imports.
qwandor Jul 29, 2024
a557268
Fix pcm_xfer_nb and pcm_xfer_ok.
qwandor Jul 29, 2024
881f902
Use enumn rather than num-enum.
qwandor Jul 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
args: --all-features

examples:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
strategy:
fail-fast: false
matrix:
Expand All @@ -77,4 +77,4 @@ jobs:
run: make kernel
- name: Run
working-directory: examples/${{ matrix.example }}
run: QEMU_ARGS="-display none" make qemu accel="off"
run: QEMU_ARGS="-display none -audiodev none,id=audio0" make qemu accel="off"
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ categories = ["hardware-support", "no-std"]
[dependencies]
log = "0.4"
bitflags = "2.3.0"
enumn = "0.1.13"
zerocopy = { version = "0.7.5", features = ["derive"] }

qwandor marked this conversation as resolved.
Show resolved Hide resolved
[features]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ VirtIO guest drivers in Rust. For **no_std** environment.
| Input | ✅ |
| Console | ✅ |
| Socket | ✅ |
| Sound | ✅ |
| ... | ❌ |

### Transports
Expand Down
9 changes: 6 additions & 3 deletions examples/riscv/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ qemu-legacy: kernel $(img)
-device virtio-gpu-device \
-device virtio-mouse-device \
-device virtio-net-device,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::5555-:5555
-netdev user,id=net0,hostfwd=tcp::5555-:5555\
-device virtio-sound-device,audiodev=audio0 \
-audiodev alsa,id=audio0

qemu: kernel $(img)
# Wait a few seconds, then try to open a connection to the VM so it can test its networking.
Expand All @@ -75,8 +77,9 @@ qemu: kernel $(img)
-device virtio-gpu-device \
-device virtio-mouse-device \
-device virtio-net-device,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::5555-:5555

-netdev user,id=net0,hostfwd=tcp::5555-:5555\
-device virtio-sound-device,audiodev=audio0 \
-audiodev alsa,id=audio0

$(img):
dd if=/dev/zero of=$@ bs=512 count=32
Expand Down
1 change: 1 addition & 0 deletions examples/riscv/music_44100Hz_u8_stereo.raw

Large diffs are not rendered by default.

66 changes: 65 additions & 1 deletion examples/riscv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@ use core::ptr::NonNull;
use fdt::{node::FdtNode, standard_nodes::Compatible, Fdt};
use log::LevelFilter;
use virtio_drivers::{
device::{blk::VirtIOBlk, gpu::VirtIOGpu, input::VirtIOInput},
device::{
blk::VirtIOBlk,
gpu::VirtIOGpu,
input::VirtIOInput,
sound::{PcmFormat, PcmRate, VirtIOSound},
},
transport::{
mmio::{MmioTransport, VirtIOHeader},
DeviceType, Transport,
Expand Down Expand Up @@ -85,6 +90,7 @@ fn virtio_device(transport: impl Transport) {
DeviceType::GPU => virtio_gpu(transport),
DeviceType::Input => virtio_input(transport),
DeviceType::Network => virtio_net(transport),
DeviceType::Sound => virtio_sound(transport),
t => warn!("Unrecognized virtio device: {:?}", t),
}
}
Expand Down Expand Up @@ -175,3 +181,61 @@ fn virtio_net<T: Transport>(transport: T) {
tcp::test_echo_server(net);
}
}

fn virtio_sound<T: Transport>(transport: T) {
let mut sound =
VirtIOSound::<HalImpl, T>::new(transport).expect("failed to create sound driver");
let output_streams = sound.output_streams().unwrap();
if output_streams.len() > 0 {
let output_stream_id = *output_streams.first().unwrap();
let rates = sound.rates_supported(output_stream_id).unwrap();
let formats = sound.formats_supported(output_stream_id).unwrap();
let channel_range = sound.channel_range_supported(output_stream_id).unwrap();
let features = sound.features_supported(output_stream_id).unwrap();

let rate = if rates.contains(PcmRate::Rate44100.into()) {
PcmRate::Rate44100
} else {
PcmRate::Rate32000
};
let format = if formats.contains(PcmFormat::U8.into()) {
PcmFormat::U8
} else {
PcmFormat::U32
};
let channel = if channel_range.contains(&2) {
2 as u8
} else {
*channel_range.start()
};
sound
.pcm_set_params(
output_stream_id,
4410 * 2,
4410,
features,
channel,
format,
rate,
)
.expect("pcm_set_params error");
sound
.pcm_prepare(output_stream_id)
.expect("pcm_prepare error");
sound.pcm_start(output_stream_id).expect("pcm_start error");
let music = include_bytes!("../music_44100Hz_u8_stereo.raw");
info!("[sound device] music len is {} bytes.", music.len());
// xfer buffer
sound
.pcm_xfer(output_stream_id, &music[..])
.expect("pcm_xfer error");
sound.pcm_stop(output_stream_id).expect("pcm_stop error");
sound
.pcm_release(output_stream_id)
.expect("pcm_release error");
match sound.latest_notification() {
Ok(notification) => info!("{:?}", notification),
Err(e) => warn!("{}", e),
}
}
}
16 changes: 8 additions & 8 deletions src/device/blk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ mod tests {
State::wait_until_queue_notified(&state, QUEUE);
println!("Transmit queue was notified.");

state
assert!(state
.lock()
.unwrap()
.read_write_queue::<{ QUEUE_SIZE as usize }>(QUEUE, |request| {
Expand All @@ -656,7 +656,7 @@ mod tests {
);

response
});
}));
});

// Read a block from the device.
Expand Down Expand Up @@ -702,7 +702,7 @@ mod tests {
State::wait_until_queue_notified(&state, QUEUE);
println!("Transmit queue was notified.");

state
assert!(state
.lock()
.unwrap()
.read_write_queue::<{ QUEUE_SIZE as usize }>(QUEUE, |request| {
Expand All @@ -728,7 +728,7 @@ mod tests {
);

response
});
}));
});

// Write a block to the device.
Expand Down Expand Up @@ -777,7 +777,7 @@ mod tests {
State::wait_until_queue_notified(&state, QUEUE);
println!("Transmit queue was notified.");

state
assert!(state
.lock()
.unwrap()
.read_write_queue::<{ QUEUE_SIZE as usize }>(QUEUE, |request| {
Expand All @@ -800,7 +800,7 @@ mod tests {
);

response
});
}));
});

// Request to flush.
Expand Down Expand Up @@ -844,7 +844,7 @@ mod tests {
State::wait_until_queue_notified(&state, QUEUE);
println!("Transmit queue was notified.");

state
assert!(state
.lock()
.unwrap()
.read_write_queue::<{ QUEUE_SIZE as usize }>(QUEUE, |request| {
Expand All @@ -868,7 +868,7 @@ mod tests {
);

response
});
}));
});

let mut id = [0; 20];
Expand Down
2 changes: 2 additions & 0 deletions src/device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ pub mod input;
pub mod net;

pub mod socket;
#[cfg(feature = "alloc")]
pub mod sound;
qwandor marked this conversation as resolved.
Show resolved Hide resolved

pub(crate) mod common;
Loading
Loading