DeviceRegistry

Struct DeviceRegistry 

Source
pub struct DeviceRegistry {
    pub objects: KObjectStore,
    /* private fields */
}
Expand description

The registry for devices.

Devices are specified in file systems with major and minor device numbers, together referred to as a DeviceType. When userspace opens one of those files, we look up the DeviceType in the device registry to instantiate a file for that device.

The DeviceRegistry also manages the KObjectStore, which provides metadata for devices via the sysfs file system, typically mounted at /sys.

Fields§

§objects: KObjectStore

The KObjects for registered devices.

Implementations§

Source§

impl DeviceRegistry

Source

pub fn register_device<'a, L>( &self, locked: &mut Locked<L>, kernel_or_task: impl KernelOrTask<'a>, name: &FsStr, metadata: DeviceMetadata, class: Class, dev_ops: impl DeviceOps, ) -> Result<Device, Errno>

Register a device with the DeviceRegistry.

If you are registering a device that exists in other systems, please check the metadata for that device in /sys and make sure you use the same properties when calling this function because these value are visible to userspace.

For example, a typical device will appear in sysfs at a path like:

/sys/devices/{bus}/{class}/{name}

Many common classes have convenient accessors on DeviceRegistry::objects.

To fill out the DeviceMetadata, look at the uevent file:

/sys/devices/{bus}/{class}/{name}/uevent

which as the following format:

  MAJOR={major-number}
  MINOR={minor-number}
  DEVNAME={devname}
  DEVMODE={devmode}

Often, the {name} and the {devname} are the same, but if they are not the same, please take care to use the correct string in the correct field.

If the {major-number} is 10 and the {minor-number} is in the range 52..128, please use register_misc_device instead because these device numbers are dynamically allocated.

If the {major-number} is in the range 234..255, please use register_dyn_device instead because these device are also dynamically allocated.

If you are unsure which device numbers to use, consult devices.txt:

https://www.kernel.org/doc/Documentation/admin-guide/devices.txt

If you are still unsure, please ask an experienced Starnix contributor rather than make up a device number.

For most devices, the create_device_sysfs_ops parameter should be DeviceDirectory::new, but some devices have custom directories in sysfs.

Finally, the dev_ops parameter is where you provide the callback for instantiating your device.

Source

pub fn register_device_with_dir<'a, L>( &self, locked: &mut Locked<L>, kernel_or_task: impl KernelOrTask<'a>, name: &FsStr, metadata: DeviceMetadata, class: Class, build_directory: impl FnOnce(&Device, &SimpleDirectoryMutator), dev_ops: impl DeviceOps, ) -> Result<Device, Errno>

Register a device with a custom directory.

See register_device for an explanation of the parameters.

Source

pub fn register_misc_device<'a, L>( &self, locked: &mut Locked<L>, kernel_or_task: impl KernelOrTask<'a>, name: &FsStr, dev_ops: impl DeviceOps, ) -> Result<Device, Errno>

Register a dynamic device in the MISC_MAJOR major device number.

MISC devices (major number 10) with minor numbers in the range 52..128 are dynamically assigned. Rather than hardcoding registrations with these device numbers, use this function instead to register the device.

See register_device for an explanation of the parameters.

Source

pub fn register_dyn_device<'a, L>( &self, locked: &mut Locked<L>, kernel_or_task: impl KernelOrTask<'a>, name: &FsStr, class: Class, dev_ops: impl DeviceOps, ) -> Result<Device, Errno>

Register a dynamic device with major numbers 234..255.

Majors device numbers 234..255 are dynamically assigned. Rather than hardcoding registrations with these device numbers, use this function instead to register the device.

Note: We do not currently allocate from this entire range because we have mistakenly hardcoded some device registrations from the dynamic range. Once we fix these registrations to be dynamic, we should expand to using the full dynamic range.

See register_device for an explanation of the parameters.

Source

pub fn register_dyn_device_with_dir<'a, L>( &self, locked: &mut Locked<L>, kernel_or_task: impl KernelOrTask<'a>, name: &FsStr, class: Class, build_directory: impl FnOnce(&Device, &SimpleDirectoryMutator), dev_ops: impl DeviceOps, ) -> Result<Device, Errno>

Register a dynamic device with a custom directory.

See register_device for an explanation of the parameters.

Source

pub fn register_dyn_device_with_devname<'a, L>( &self, locked: &mut Locked<L>, kernel_or_task: impl KernelOrTask<'a>, name: &FsStr, devname: &FsStr, class: Class, build_directory: impl FnOnce(&Device, &SimpleDirectoryMutator), dev_ops: impl DeviceOps, ) -> Result<Device, Errno>

Register a dynamic device with major numbers 234..255.

Majors device numbers 234..255 are dynamically assigned. Rather than hardcoding registrations with these device numbers, use this function instead to register the device.

Note: We do not currently allocate from this entire range because we have mistakenly hardcoded some device registrations from the dynamic range. Once we fix these registrations to be dynamic, we should expand to using the full dynamic range.

See register_device for an explanation of the parameters.

Source

pub fn register_silent_dyn_device<'a, L>( &self, locked: &mut Locked<L>, name: &FsStr, dev_ops: impl DeviceOps, ) -> Result<DeviceMetadata, Errno>

Register a “silent” dynamic device with major numbers 234..255.

Only use for devices that should not be registered with the KObjectStore/appear in sysfs. This is a rare occurrence.

See register_dyn_device for an explanation of dyn devices and of the parameters.

Source

pub fn add_device<'a, L>( &self, locked: &mut Locked<L>, kernel_or_task: impl KernelOrTask<'a>, name: &FsStr, metadata: DeviceMetadata, class: Class, build_directory: impl FnOnce(&Device, &SimpleDirectoryMutator), ) -> Result<Device, Errno>

Directly add a device to the KObjectStore.

This function should be used only by device that have registered an entire major device number. If you want to add a single minor device, use the register_device function instead.

See register_device for an explanation of the parameters.

Source

pub fn add_net_device(&self, name: &FsStr) -> Device

Add a net device to the device registry.

Net devices are different from other devices because they do not have a device number. Instead, their uevent files have the following format:

INTERFACE={name}
IFINDEX={index}

Currently, we only register the net devices by name and use an empty uevent file.

Source

pub fn remove_net_device(&self, device: Device)

Remove a net device from the device registry.

See add_net_device for more details.

Source

pub fn add_numberless_device<L>( &self, _locked: &mut Locked<L>, name: &FsStr, class: Class, build_directory: impl FnOnce(&Device, &SimpleDirectoryMutator), ) -> Device

Directly add a device to the KObjectStore that lacks a device number.

This function should be used only by device do not have a major or a minor number. You can identify these devices because they appear in sysfs and have an empty uevent file.

See register_device for an explanation of the parameters.

Source

pub fn remove_device<L>( &self, locked: &mut Locked<L>, current_task: &CurrentTask, device: Device, )

Remove a device directly added with add_device.

This function should be used only by device that have registered an entire major device number. Individually registered minor device cannot be removed at this time.

Source

pub fn list_major_devices<L>( &self, locked: &mut Locked<L>, mode: DeviceMode, ) -> Vec<(u32, FsString)>

Returns a list of the registered major device numbers for the given DeviceMode and their names.

Source

pub fn list_minor_devices<L>( &self, locked: &mut Locked<L>, mode: DeviceMode, range: Range<DeviceType>, ) -> Vec<(DeviceType, FsString)>

Returns a list of the registered minor devices for the given DeviceMode and their names.

Source

pub fn register_major<'a, L>( &self, locked: &mut Locked<L>, name: FsString, mode: DeviceMode, major: u32, dev_ops: impl DeviceOps, ) -> Result<(), Errno>

Register an entire major device number.

If you register an entire major device, use add_device and remove_device to manage the sysfs entiries for your device rather than trying to register and unregister individual minor devices.

Source

pub fn next_anonymous_dev_id<'a, L>(&self, locked: &mut Locked<L>) -> DeviceType

Allocate an anonymous device identifier.

Source

pub fn register_listener<'a, L>( &self, locked: &mut Locked<L>, listener: impl DeviceListener + 'static, ) -> DeviceListenerKey

Register a new listener for uevents on devices.

Returns a key used to unregister the listener.

Source

pub fn unregister_listener<'a, L>( &self, locked: &mut Locked<L>, key: &DeviceListenerKey, )

Unregister a listener previously registered through register_listener.

Source

pub fn dispatch_uevent<'a, L>( &self, locked: &mut Locked<L>, action: UEventAction, device: Device, )

Dispatch an uevent for the given device.

Source

pub fn open_device<L>( &self, locked: &mut Locked<L>, current_task: &CurrentTask, node: &NamespaceNode, flags: OpenFlags, device_type: DeviceType, mode: DeviceMode, ) -> Result<Box<dyn FileOps>, Errno>

Instantiate a file for the specified device.

The device will be looked up in the device registry by DeviceMode and DeviceType.

Source

pub fn get_device<L>( &self, locked: &mut Locked<L>, device_type: DeviceType, mode: DeviceMode, ) -> Result<DeviceHandle, Errno>

Trait Implementations§

Source§

impl Default for DeviceRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsAny for T
where T: Any,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn type_name(&self) -> &'static str

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T, D> Encode<Ambiguous1, D> for T
where D: ResourceDialect,

Source§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
Source§

impl<T, D> Encode<Ambiguous2, D> for T
where D: ResourceDialect,

Source§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> InstanceFromServiceTransport<T> for T

§

fn from_service_transport(handle: T) -> T

Converts the given service transport handle of type T to [Self]
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> Into32<U> for T
where U: MultiArchFrom<T>,

Source§

fn into_32(self) -> U

Source§

impl<T, U> Into64<U> for T
where U: MultiArchFrom<T>,

Source§

fn into_64(self) -> U

Source§

impl<T> IntoAny for T
where T: 'static + Send + Sync,

Source§

fn into_any(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Cast the given object into a dyn std::any::Any.
Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T, U> IntoExt<U> for T
where U: FromExt<T>,

§

fn into_ext(self) -> U

Performs the conversion.
Source§

impl<T, U> IntoFidl<U> for T
where U: FromFidl<T>,

Source§

fn into_fidl(self) -> U

Source§

impl<T, U> MultiArchFrom<T> for U
where U: From<T>,

Source§

fn from_64(value: T) -> U

Source§

fn from_32(value: T) -> U

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T, U> TryIntoExt<U> for T
where U: TryFromExt<T>,

§

type Error = <U as TryFromExt<T>>::Error

§

fn try_into_ext(self) -> Result<U, <T as TryIntoExt<U>>::Error>

Tries to perform the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<St> WithTag for St

§

fn tagged<T>(self, tag: T) -> Tagged<T, St>

Produce a new stream from this one which yields item tupled with a constant tag
Source§

impl<B, A> LockBefore<B> for A
where B: LockAfter<A>,

Source§

impl<B, A> LockEqualOrBefore<B> for A
where A: LockBefore<B>,

§

impl<E> RunsTransport<Mpsc> for E

§

impl<E> RunsTransport<Mpsc> for E
where E: RunsTransport<Mpsc>,