Lines Matching +full:usb +full:- +full:c
1 // SPDX-License-Identifier: GPL-2.0
2 // SPDX-FileCopyrightText: Copyright (C) 2025 Collabora Ltd.
4 //! Abstractions for the USB bus.
6 //! C header: [`include/linux/usb.h`](srctree/include/linux/usb.h)
31 /// An adapter for the registration of USB drivers.
35 // - `bindings::usb_driver` is a C type declared as `repr(C)`.
36 // - `T::Data` is the type of the driver's device private data.
37 // - `struct usb_driver` embeds a `struct device_driver`.
38 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
52 ) -> Result {
74 extern "C" fn probe_callback(
77 ) -> kernel::ffi::c_int {
78 // SAFETY: The USB core only ever calls the probe callback with a valid pointer to a
101 extern "C" fn disconnect_callback(intf: *mut bindings::usb_interface) {
102 // SAFETY: The USB core only ever calls the disconnect callback with a valid pointer to a
119 /// Abstraction for the USB device ID structure, i.e. [`struct usb_device_id`].
121 /// [`struct usb_device_id`]: https://docs.kernel.org/driver-api/basics.html#c.usb_device_id
127 /// Equivalent to C's `USB_DEVICE` macro.
128 pub const fn from_id(vendor: u16, product: u16) -> Self {
137 /// Equivalent to C's `USB_DEVICE_VER` macro.
138 pub const fn from_device_ver(vendor: u16, product: u16, bcd_lo: u16, bcd_hi: u16) -> Self {
149 /// Equivalent to C's `USB_DEVICE_INFO` macro.
150 pub const fn from_device_info(class: u8, subclass: u8, protocol: u8) -> Self {
160 /// Equivalent to C's `USB_INTERFACE_INFO` macro.
161 pub const fn from_interface_info(class: u8, subclass: u8, protocol: u8) -> Self {
171 /// Equivalent to C's `USB_DEVICE_INTERFACE_CLASS` macro.
172 pub const fn from_device_interface_class(vendor: u16, product: u16, class: u8) -> Self {
183 /// Equivalent to C's `USB_DEVICE_INTERFACE_PROTOCOL` macro.
184 pub const fn from_device_interface_protocol(vendor: u16, product: u16, protocol: u8) -> Self {
195 /// Equivalent to C's `USB_DEVICE_INTERFACE_NUMBER` macro.
196 pub const fn from_device_interface_number(vendor: u16, product: u16, number: u8) -> Self {
207 /// Equivalent to C's `USB_DEVICE_AND_INTERFACE_INFO` macro.
214 ) -> Self {
239 /// [`IdTable`](kernel::device_id::IdTable) type for USB.
242 /// Create a USB `IdTable` with its alias for modpost.
246 $crate::module_device_table!("usb", $crate::usb::DeviceId, $($tt)*);
250 /// The USB driver trait.
255 /// # use kernel::{bindings, device::Core, usb};
262 /// <MyDriver as usb::Driver>::IdInfo,
264 /// (usb::DeviceId::from_id(0x1234, 0x5678), ()),
265 /// (usb::DeviceId::from_id(0xabcd, 0xef01), ()),
269 /// impl usb::Driver for MyDriver {
272 /// const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
275 /// _interface: &'bound usb::Interface<Core<'_>>,
276 /// _id: &usb::DeviceId,
278 /// ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound {
283 /// _interface: &'bound usb::Interface<Core<'_>>,
299 /// USB driver probe.
301 /// Called when a new USB interface is bound to this driver.
307 ) -> impl PinInit<Self::Data<'bound>, Error> + 'bound;
309 /// USB driver disconnect.
311 /// Called when the USB interface is about to be unbound from this driver.
318 /// A USB interface.
320 /// This structure represents the Rust abstraction for a C [`struct usb_interface`].
321 /// The implementation abstracts the usage of a C [`struct usb_interface`] passed
322 /// in from the C side.
327 /// by the C portion of the kernel.
329 /// [`struct usb_interface`]: https://www.kernel.org/doc/html/latest/driver-api/usb/usb.html#c.usb_interface
337 fn as_raw(&self) -> *mut bindings::usb_interface {
342 // SAFETY: `usb::Interface` is a transparent wrapper of `struct usb_interface`.
343 // The offset is guaranteed to point to a valid device field inside `usb::Interface`.
354 fn as_ref(&self) -> &device::Device<Ctx> {
365 fn as_ref(&self) -> &Device {
375 // SAFETY: Instances of `Interface` are always reference-counted.
387 // SAFETY: The safety requirements guarantee that the refcount is non-zero.
392 // SAFETY: A `Interface` is always reference-counted and can be released from any thread.
399 /// A USB device.
401 /// This structure represents the Rust abstraction for a C [`struct usb_device`].
402 /// The implementation abstracts the usage of a C [`struct usb_device`] passed in
403 /// from the C side.
407 /// A [`Device`] instance represents a valid [`struct usb_device`] created by the C portion of the
410 /// [`struct usb_device`]: https://www.kernel.org/doc/html/latest/driver-api/usb/usb.html#c.usb_device
418 fn as_raw(&self) -> *mut bindings::usb_device {
428 // SAFETY: Instances of `Device` are always reference-counted.
440 // SAFETY: The safety requirements guarantee that the refcount is non-zero.
446 fn as_ref(&self) -> &device::Device<Ctx> {
456 // SAFETY: A `Device` is always reference-counted and can be released from any thread.
463 // SAFETY: Same as `Device<Normal>` -- the underlying `struct usb_device` is the same;
464 // `Bound` is a zero-sized type-state marker that does not affect thread safety.
467 /// Declares a kernel module that exposes a single USB driver.
483 $crate::module_driver!(<T>, $crate::usb::Adapter<T>, { $($f)* });