Skip to content

Commit 8db20e2

Browse files
WhatAmISupposedToPutHerejannau
authored andcommitted
rust: WIP(?): Various bits of rust glue for AOP series
Signed-off-by: Sasha Finkelstein <fnkl.kernel@gmail.com>
1 parent df468dc commit 8db20e2

5 files changed

Lines changed: 61 additions & 3 deletions

File tree

rust/bindgen_parameters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
# Packed type cannot transitively contain a `#[repr(align)]` type.
99
--opaque-type alt_instr
10+
--opaque-type snd_codec_options
11+
--opaque-type snd_codec
12+
--opaque-type snd_compr_params
1013
--opaque-type x86_msi_data
1114
--opaque-type x86_msi_addr_lo
1215

rust/bindings/bindings_helper.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/ethtool.h>
2323
#include <linux/firmware.h>
2424
#include <linux/fs.h>
25+
#include <linux/iio/iio.h>
26+
#include <linux/iio/types.h>
2527
#include <linux/io-pgtable.h>
2628
#include <linux/jiffies.h>
2729
#include <linux/ktime.h>
@@ -43,6 +45,9 @@
4345
#include <linux/wait.h>
4446
#include <linux/workqueue.h>
4547
#include <linux/xarray.h>
48+
#include <sound/core.h>
49+
#include <sound/dmaengine_pcm.h>
50+
#include <sound/pcm.h>
4651

4752
/* `bindgen` gets confused at certain things. */
4853
const size_t RUST_CONST_HELPER_ARCH_SLAB_MINALIGN = ARCH_SLAB_MINALIGN;
@@ -72,3 +77,10 @@ const xa_mark_t BINDINGS_XA_MARK_2 = XA_MARK_2;
7277
const xa_mark_t BINDINGS_XA_PRESENT = XA_PRESENT;
7378
const xa_mark_t BINDINGS_XA_MARK_MAX = XA_MARK_MAX;
7479
const xa_mark_t BINDINGS_XA_FREE_MARK = XA_FREE_MARK;
80+
81+
const u64 BINDINGS_SNDRV_PCM_FMTBIT_FLOAT_LE = SNDRV_PCM_FMTBIT_FLOAT_LE;
82+
83+
const u32 BINDINGS_IIO_CHAN_INFO_RAW = IIO_CHAN_INFO_RAW;
84+
const u32 BINDINGS_IIO_CHAN_INFO_PROCESSED = IIO_CHAN_INFO_PROCESSED;
85+
const u32 BINDINGS_IIO_ANGL = IIO_ANGL;
86+
const u32 BINDINGS_IIO_LIGHT = IIO_LIGHT;

rust/kernel/alloc/kvec.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ where
460460
/// If `new_len` is greater than len, the Vec is extended by the difference,
461461
/// with each additional slot filled with `value`.
462462
/// If `new_len` is less than len, the Vec is simply truncated.
463-
fn resize(&mut self, new_len: usize, value: T, flags: Flags) -> Result<(), AllocError>
463+
pub fn resize(&mut self, new_len: usize, value: T, flags: Flags) -> Result<(), AllocError>
464464
where
465465
T: Clone,
466466
{
@@ -625,6 +625,26 @@ where
625625
}
626626
}
627627
}
628+
/// Removes an element from the vector and returns it.
629+
///
630+
/// The removed element is replaced by the last element of the vector.
631+
///
632+
/// This does not preserve ordering of the remaining elements, but is *O*(1).
633+
/// If you need to preserve the element order, use [`remove`] instead.
634+
pub fn swap_remove(&mut self, index: usize) -> T {
635+
if index > self.len() {
636+
panic!("Index out of range");
637+
}
638+
// SAFETY: index is in range
639+
// self.len() - 1 is in range since at last 1 element exists
640+
unsafe {
641+
let old = ptr::read(self.as_ptr().add(index));
642+
let last = ptr::read(self.as_ptr().add(self.len() - 1));
643+
ptr::write(self.as_mut_ptr().add(index), last);
644+
self.set_len(self.len - 1);
645+
old
646+
}
647+
}
628648
}
629649

630650
impl<T: Clone, A: Allocator> Vec<T, A> {

rust/kernel/device.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Device {
6060
}
6161

6262
/// Obtain the raw `struct device *`.
63-
pub(crate) fn as_raw(&self) -> *mut bindings::device {
63+
pub fn as_raw(&self) -> *mut bindings::device {
6464
self.0.get()
6565
}
6666

@@ -75,6 +75,13 @@ impl Device {
7575
unsafe { Some(Self::get_device(pdev)) }
7676
}
7777

78+
/// Returns the driver_data pointer.
79+
pub fn get_drvdata<T>(&self) -> *mut T {
80+
// SAFETY: dev_get_drvdata returns a field of the device,
81+
// pointer to which is valid by type invariant
82+
unsafe { bindings::dev_get_drvdata(self.as_raw()) as *mut T }
83+
}
84+
7885
/// Convert a raw C `struct device` pointer to a `&'a Device`.
7986
///
8087
/// # Safety

rust/kernel/of.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl Node {
9595
}
9696

9797
/// Returns a reference to the underlying C `device_node` structure.
98-
fn node(&self) -> &bindings::device_node {
98+
pub fn node(&self) -> &bindings::device_node {
9999
// SAFETY: `raw_node` is valid per the type invariant.
100100
unsafe { &*self.raw_node }
101101
}
@@ -355,6 +355,22 @@ impl<'a> Property<'a> {
355355
pub fn is_empty(&self) -> bool {
356356
self.len() == 0
357357
}
358+
359+
pub fn copy_to_slice<T: PropertyUnit>(&self, target: &mut [T]) -> Result<()> {
360+
if self.len() % T::UNIT_SIZE != 0 {
361+
return Err(EINVAL);
362+
}
363+
364+
if self.len() / T::UNIT_SIZE != target.len() {
365+
return Err(EINVAL);
366+
}
367+
368+
let val = self.value();
369+
for (i, off) in (0..self.len()).step_by(T::UNIT_SIZE).enumerate() {
370+
target[i] = T::from_bytes(&val[off..off + T::UNIT_SIZE])?
371+
}
372+
Ok(())
373+
}
358374
}
359375

360376
/// A trait that represents a value decodable from a property with a fixed unit size.

0 commit comments

Comments
 (0)