diff options
author | Judy Hsiao <judyhsiao@google.com> | 2020-12-17 16:37:12 +0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-01-15 05:49:02 +0000 |
commit | 0458a459ad51cc0b386835f5b909d0d04003ab16 (patch) | |
tree | 65d4e15de47f0464c8e30319dc0596702ae49752 | |
parent | e4ba53351176857be2b264cf768bfa1e8fb3ef05 (diff) | |
download | adhd-0458a459ad51cc0b386835f5b909d0d04003ab16.tar.gz |
sof_sys: generate rust version sof_sys
Use bindgen to generate C structure and constants
for rust version sof-sys from
src/third_party/sound-open-firmware-private/
commit: d4109e090f692c8590b250be41ec7027e7786ada.
BUG=b:157210111
TEST=`cargo run`
Cq-Depend: chromium:2598196
Change-Id: I9536c046531460b22c83189a31746e91adcfca66
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/adhd/+/2595660
Reviewed-by: Chih-Yang Hsia <paulhsia@chromium.org>
Tested-by: Judy Hsiao <judyhsiao@chromium.org>
Commit-Queue: Judy Hsiao <judyhsiao@chromium.org>
-rw-r--r-- | sof_sys/.gitignore | 2 | ||||
-rw-r--r-- | sof_sys/.rustfmt.toml | 2 | ||||
-rw-r--r-- | sof_sys/Cargo.toml | 4 | ||||
-rw-r--r-- | sof_sys/generator/.gitignore | 1 | ||||
-rw-r--r-- | sof_sys/generator/Cargo.toml | 6 | ||||
-rw-r--r-- | sof_sys/generator/README.md | 1 | ||||
-rw-r--r-- | sof_sys/generator/src/main.rs | 42 | ||||
-rw-r--r-- | sof_sys/generator/wrapper.h | 5 | ||||
-rw-r--r-- | sof_sys/src/bindings.rs | 154 | ||||
-rw-r--r-- | sof_sys/src/lib.rs | 12 |
10 files changed, 229 insertions, 0 deletions
diff --git a/sof_sys/.gitignore b/sof_sys/.gitignore new file mode 100644 index 00000000..fa8d85ac --- /dev/null +++ b/sof_sys/.gitignore @@ -0,0 +1,2 @@ +Cargo.lock +target diff --git a/sof_sys/.rustfmt.toml b/sof_sys/.rustfmt.toml new file mode 100644 index 00000000..a2db3012 --- /dev/null +++ b/sof_sys/.rustfmt.toml @@ -0,0 +1,2 @@ +use_field_init_shorthand = true +use_try_shorthand = true diff --git a/sof_sys/Cargo.toml b/sof_sys/Cargo.toml new file mode 100644 index 00000000..21934ee1 --- /dev/null +++ b/sof_sys/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "sof_sys" +version = "0.1.0" +authors = ["The Chromium OS Authors"] diff --git a/sof_sys/generator/.gitignore b/sof_sys/generator/.gitignore new file mode 100644 index 00000000..03314f77 --- /dev/null +++ b/sof_sys/generator/.gitignore @@ -0,0 +1 @@ +Cargo.lock diff --git a/sof_sys/generator/Cargo.toml b/sof_sys/generator/Cargo.toml new file mode 100644 index 00000000..672d41da --- /dev/null +++ b/sof_sys/generator/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "generator" +version = "0.1.0" +authors = ["The Chromium OS Authors"] +[dependencies] +bindgen = "0.43.0" diff --git a/sof_sys/generator/README.md b/sof_sys/generator/README.md new file mode 100644 index 00000000..3e2ca771 --- /dev/null +++ b/sof_sys/generator/README.md @@ -0,0 +1 @@ +Use `cargo run` to generate rust bindings at sof_sys/src/bindings.rs diff --git a/sof_sys/generator/src/main.rs b/sof_sys/generator/src/main.rs new file mode 100644 index 00000000..60f0102d --- /dev/null +++ b/sof_sys/generator/src/main.rs @@ -0,0 +1,42 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +extern crate bindgen; + +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + let bindings = bindgen::Builder::default() + .header("wrapper.h") + .derive_debug(false) + .clang_arg("-I../../../sound-open-firmware-private/src/include") + .whitelist_type("sof_abi_hdr") + .whitelist_type("sof_ipc_ctrl_cmd") + .generate() + .expect("Unable to generate bindings"); + + let header = b"// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +/* + * generated from files in sound-open-firmware-private/src/include: + * kernel/header.h + * ipc/control.h + */ + +"; + + // Write the bindings to the $OUT_DIR/bindings.rs file. + let out_path = PathBuf::from("../src").join("bindings.rs"); + + let mut output_file = + File::create(&out_path).expect(&format!("Couldn't create {:?}", out_path)); + output_file + .write_all(header) + .expect("Couldn't write header"); + output_file + .write_all(bindings.to_string().as_bytes()) + .expect("Couldn't write bindings"); +} diff --git a/sof_sys/generator/wrapper.h b/sof_sys/generator/wrapper.h new file mode 100644 index 00000000..5bac0f57 --- /dev/null +++ b/sof_sys/generator/wrapper.h @@ -0,0 +1,5 @@ +// Copyright 2021 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +#include <kernel/header.h> +#include <ipc/control.h> diff --git a/sof_sys/src/bindings.rs b/sof_sys/src/bindings.rs new file mode 100644 index 00000000..7bed0dcf --- /dev/null +++ b/sof_sys/src/bindings.rs @@ -0,0 +1,154 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +/* + * generated from files in sound-open-firmware-private/src/include: + * kernel/header.h + * ipc/control.h + */ + +/* automatically generated by rust-bindgen */ + +#[repr(C)] +#[derive(Default)] +pub struct __IncompleteArrayField<T>(::std::marker::PhantomData<T>); +impl<T> __IncompleteArrayField<T> { + #[inline] + pub fn new() -> Self { + __IncompleteArrayField(::std::marker::PhantomData) + } + #[inline] + pub unsafe fn as_ptr(&self) -> *const T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_mut_ptr(&mut self) -> *mut T { + ::std::mem::transmute(self) + } + #[inline] + pub unsafe fn as_slice(&self, len: usize) -> &[T] { + ::std::slice::from_raw_parts(self.as_ptr(), len) + } + #[inline] + pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] { + ::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len) + } +} +impl<T> ::std::fmt::Debug for __IncompleteArrayField<T> { + fn fmt(&self, fmt: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + fmt.write_str("__IncompleteArrayField") + } +} +impl<T> ::std::clone::Clone for __IncompleteArrayField<T> { + #[inline] + fn clone(&self) -> Self { + Self::new() + } +} +impl<T> ::std::marker::Copy for __IncompleteArrayField<T> {} +pub type __uint32_t = ::std::os::raw::c_uint; +/// \brief Header for all non IPC ABI data. +/// +/// Identifies data type, size and ABI. +/// Data header used for all component data structures and binary blobs sent to +/// firmware as runtime data. This data is typically sent by userspace +/// applications and tunnelled through any OS kernel (via binary kcontrol on +/// Linux) to the firmware. +#[repr(C, packed)] +pub struct sof_abi_hdr { + ///< 'S', 'O', 'F', '\0' + pub magic: u32, + ///< component specific type + pub type_: u32, + ///< size in bytes of data excl. this struct + pub size: u32, + ///< SOF ABI version + pub abi: u32, + ///< reserved for future use + pub reserved: [u32; 4usize], + ///< Component data - opaque to core + pub data: __IncompleteArrayField<u32>, +} +#[test] +fn bindgen_test_layout_sof_abi_hdr() { + assert_eq!( + ::std::mem::size_of::<sof_abi_hdr>(), + 32usize, + concat!("Size of: ", stringify!(sof_abi_hdr)) + ); + assert_eq!( + ::std::mem::align_of::<sof_abi_hdr>(), + 1usize, + concat!("Alignment of ", stringify!(sof_abi_hdr)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<sof_abi_hdr>())).magic as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(sof_abi_hdr), + "::", + stringify!(magic) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<sof_abi_hdr>())).type_ as *const _ as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(sof_abi_hdr), + "::", + stringify!(type_) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<sof_abi_hdr>())).size as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(sof_abi_hdr), + "::", + stringify!(size) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<sof_abi_hdr>())).abi as *const _ as usize }, + 12usize, + concat!( + "Offset of field: ", + stringify!(sof_abi_hdr), + "::", + stringify!(abi) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<sof_abi_hdr>())).reserved as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(sof_abi_hdr), + "::", + stringify!(reserved) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::<sof_abi_hdr>())).data as *const _ as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(sof_abi_hdr), + "::", + stringify!(data) + ) + ); +} +///< maps to ALSA volume style controls +pub const sof_ipc_ctrl_cmd_SOF_CTRL_CMD_VOLUME: sof_ipc_ctrl_cmd = 0; +///< maps to ALSA enum style controls +pub const sof_ipc_ctrl_cmd_SOF_CTRL_CMD_ENUM: sof_ipc_ctrl_cmd = 1; +///< maps to ALSA switch style controls +pub const sof_ipc_ctrl_cmd_SOF_CTRL_CMD_SWITCH: sof_ipc_ctrl_cmd = 2; +///< maps to ALSA binary style controls +pub const sof_ipc_ctrl_cmd_SOF_CTRL_CMD_BINARY: sof_ipc_ctrl_cmd = 3; +/// Control command type. +pub type sof_ipc_ctrl_cmd = u32; diff --git a/sof_sys/src/lib.rs b/sof_sys/src/lib.rs new file mode 100644 index 00000000..57119cfa --- /dev/null +++ b/sof_sys/src/lib.rs @@ -0,0 +1,12 @@ +// Copyright 2020 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +#![allow(clippy::unreadable_literal)] +#![allow(clippy::cognitive_complexity)] +#![allow(non_upper_case_globals)] +#![allow(non_camel_case_types)] +#![allow(non_snake_case)] + +pub mod bindings; +#[allow(unused_imports)] +pub use bindings::sof_abi_hdr; |