xref: /linux/rust/kernel/usb.rs (revision 24799831d631239ff21ea1bf7feee832df48b81f)
1 // SPDX-License-Identifier: GPL-2.0
2 // SPDX-FileCopyrightText: Copyright (C) 2025 Collabora Ltd.
3 
4 //! Abstractions for the USB bus.
5 //!
6 //! C header: [`include/linux/usb.h`](srctree/include/linux/usb.h)
7 
8 use crate::{
9     bindings,
10     device,
11     device_id::{
12         RawDeviceId,
13         RawDeviceIdIndex, //
14     },
15     driver,
16     error::{
17         from_result,
18         to_result, //
19     },
20     prelude::*,
21     sync::aref::AlwaysRefCounted,
22     types::Opaque,
23     ThisModule, //
24 };
25 use core::{
26     marker::PhantomData,
27     mem::{
28         offset_of,
29         MaybeUninit, //
30     },
31     ptr::NonNull,
32 };
33 
34 /// An adapter for the registration of USB drivers.
35 pub struct Adapter<T: Driver>(T);
36 
37 // SAFETY:
38 // - `bindings::usb_driver` is a C type declared as `repr(C)`.
39 // - `T::Data` is the type of the driver's device private data.
40 // - `struct usb_driver` embeds a `struct device_driver`.
41 // - `DEVICE_DRIVER_OFFSET` is the correct byte offset to the embedded `struct device_driver`.
42 unsafe impl<T: Driver> driver::DriverLayout for Adapter<T> {
43     type DriverType = bindings::usb_driver;
44     type DriverData<'bound> = T::Data;
45     const DEVICE_DRIVER_OFFSET: usize = core::mem::offset_of!(Self::DriverType, driver);
46 }
47 
48 // SAFETY: A call to `unregister` for a given instance of `DriverType` is guaranteed to be valid if
49 // a preceding call to `register` has been successful.
50 unsafe impl<T: Driver> driver::RegistrationOps for Adapter<T> {
51     unsafe fn register(
52         udrv: &Opaque<Self::DriverType>,
53         name: &'static CStr,
54         module: &'static ThisModule,
55     ) -> Result {
56         // SAFETY: It's safe to set the fields of `struct usb_driver` on initialization.
57         unsafe {
58             (*udrv.get()).name = name.as_char_ptr();
59             (*udrv.get()).probe = Some(Self::probe_callback);
60             (*udrv.get()).disconnect = Some(Self::disconnect_callback);
61             (*udrv.get()).id_table = T::ID_TABLE.as_ptr();
62         }
63 
64         // SAFETY: `udrv` is guaranteed to be a valid `DriverType`.
65         to_result(unsafe {
66             bindings::usb_register_driver(udrv.get(), module.0, name.as_char_ptr())
67         })
68     }
69 
70     unsafe fn unregister(udrv: &Opaque<Self::DriverType>) {
71         // SAFETY: `udrv` is guaranteed to be a valid `DriverType`.
72         unsafe { bindings::usb_deregister(udrv.get()) };
73     }
74 }
75 
76 impl<T: Driver> Adapter<T> {
77     extern "C" fn probe_callback(
78         intf: *mut bindings::usb_interface,
79         id: *const bindings::usb_device_id,
80     ) -> kernel::ffi::c_int {
81         // SAFETY: The USB core only ever calls the probe callback with a valid pointer to a
82         // `struct usb_interface` and `struct usb_device_id`.
83         //
84         // INVARIANT: `intf` is valid for the duration of `probe_callback()`.
85         let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal<'_>>>() };
86 
87         from_result(|| {
88             // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `struct usb_device_id` and
89             // does not add additional invariants, so it's safe to transmute.
90             let id = unsafe { &*id.cast::<DeviceId>() };
91 
92             let info = T::ID_TABLE.info(id.index());
93             let data = T::probe(intf, id, info);
94 
95             let dev: &device::Device<device::CoreInternal<'_>> = intf.as_ref();
96             dev.set_drvdata(data)?;
97             Ok(0)
98         })
99     }
100 
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
103         // `struct usb_interface`.
104         //
105         // INVARIANT: `intf` is valid for the duration of `disconnect_callback()`.
106         let intf = unsafe { &*intf.cast::<Interface<device::CoreInternal<'_>>>() };
107 
108         let dev: &device::Device<device::CoreInternal<'_>> = intf.as_ref();
109 
110         // SAFETY: `disconnect_callback` is only ever called after a successful call to
111         // `probe_callback`, hence it's guaranteed that `Device::set_drvdata()` has been called
112         // and stored a `Pin<KBox<T::Data>>`.
113         let data = unsafe { dev.drvdata_borrow::<T::Data>() };
114 
115         T::disconnect(intf, data);
116     }
117 }
118 
119 /// Abstraction for the USB device ID structure, i.e. [`struct usb_device_id`].
120 ///
121 /// [`struct usb_device_id`]: https://docs.kernel.org/driver-api/basics.html#c.usb_device_id
122 #[repr(transparent)]
123 #[derive(Clone, Copy)]
124 pub struct DeviceId(bindings::usb_device_id);
125 
126 impl DeviceId {
127     /// Equivalent to C's `USB_DEVICE` macro.
128     pub const fn from_id(vendor: u16, product: u16) -> Self {
129         Self(bindings::usb_device_id {
130             match_flags: bindings::USB_DEVICE_ID_MATCH_DEVICE as u16,
131             idVendor: vendor,
132             idProduct: product,
133             // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
134             ..unsafe { MaybeUninit::zeroed().assume_init() }
135         })
136     }
137 
138     /// Equivalent to C's `USB_DEVICE_VER` macro.
139     pub const fn from_device_ver(vendor: u16, product: u16, bcd_lo: u16, bcd_hi: u16) -> Self {
140         Self(bindings::usb_device_id {
141             match_flags: bindings::USB_DEVICE_ID_MATCH_DEVICE_AND_VERSION as u16,
142             idVendor: vendor,
143             idProduct: product,
144             bcdDevice_lo: bcd_lo,
145             bcdDevice_hi: bcd_hi,
146             // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
147             ..unsafe { MaybeUninit::zeroed().assume_init() }
148         })
149     }
150 
151     /// Equivalent to C's `USB_DEVICE_INFO` macro.
152     pub const fn from_device_info(class: u8, subclass: u8, protocol: u8) -> Self {
153         Self(bindings::usb_device_id {
154             match_flags: bindings::USB_DEVICE_ID_MATCH_DEV_INFO as u16,
155             bDeviceClass: class,
156             bDeviceSubClass: subclass,
157             bDeviceProtocol: protocol,
158             // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
159             ..unsafe { MaybeUninit::zeroed().assume_init() }
160         })
161     }
162 
163     /// Equivalent to C's `USB_INTERFACE_INFO` macro.
164     pub const fn from_interface_info(class: u8, subclass: u8, protocol: u8) -> Self {
165         Self(bindings::usb_device_id {
166             match_flags: bindings::USB_DEVICE_ID_MATCH_INT_INFO as u16,
167             bInterfaceClass: class,
168             bInterfaceSubClass: subclass,
169             bInterfaceProtocol: protocol,
170             // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
171             ..unsafe { MaybeUninit::zeroed().assume_init() }
172         })
173     }
174 
175     /// Equivalent to C's `USB_DEVICE_INTERFACE_CLASS` macro.
176     pub const fn from_device_interface_class(vendor: u16, product: u16, class: u8) -> Self {
177         Self(bindings::usb_device_id {
178             match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
179                 | bindings::USB_DEVICE_ID_MATCH_INT_CLASS) as u16,
180             idVendor: vendor,
181             idProduct: product,
182             bInterfaceClass: class,
183             // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
184             ..unsafe { MaybeUninit::zeroed().assume_init() }
185         })
186     }
187 
188     /// Equivalent to C's `USB_DEVICE_INTERFACE_PROTOCOL` macro.
189     pub const fn from_device_interface_protocol(vendor: u16, product: u16, protocol: u8) -> Self {
190         Self(bindings::usb_device_id {
191             match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
192                 | bindings::USB_DEVICE_ID_MATCH_INT_PROTOCOL) as u16,
193             idVendor: vendor,
194             idProduct: product,
195             bInterfaceProtocol: protocol,
196             // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
197             ..unsafe { MaybeUninit::zeroed().assume_init() }
198         })
199     }
200 
201     /// Equivalent to C's `USB_DEVICE_INTERFACE_NUMBER` macro.
202     pub const fn from_device_interface_number(vendor: u16, product: u16, number: u8) -> Self {
203         Self(bindings::usb_device_id {
204             match_flags: (bindings::USB_DEVICE_ID_MATCH_DEVICE
205                 | bindings::USB_DEVICE_ID_MATCH_INT_NUMBER) as u16,
206             idVendor: vendor,
207             idProduct: product,
208             bInterfaceNumber: number,
209             // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
210             ..unsafe { MaybeUninit::zeroed().assume_init() }
211         })
212     }
213 
214     /// Equivalent to C's `USB_DEVICE_AND_INTERFACE_INFO` macro.
215     pub const fn from_device_and_interface_info(
216         vendor: u16,
217         product: u16,
218         class: u8,
219         subclass: u8,
220         protocol: u8,
221     ) -> Self {
222         Self(bindings::usb_device_id {
223             match_flags: (bindings::USB_DEVICE_ID_MATCH_INT_INFO
224                 | bindings::USB_DEVICE_ID_MATCH_DEVICE) as u16,
225             idVendor: vendor,
226             idProduct: product,
227             bInterfaceClass: class,
228             bInterfaceSubClass: subclass,
229             bInterfaceProtocol: protocol,
230             // SAFETY: It is safe to use all zeroes for the other fields of `usb_device_id`.
231             ..unsafe { MaybeUninit::zeroed().assume_init() }
232         })
233     }
234 }
235 
236 // SAFETY: `DeviceId` is a `#[repr(transparent)]` wrapper of `usb_device_id` and does not add
237 // additional invariants, so it's safe to transmute to `RawType`.
238 unsafe impl RawDeviceId for DeviceId {
239     type RawType = bindings::usb_device_id;
240 }
241 
242 // SAFETY: `DRIVER_DATA_OFFSET` is the offset to the `driver_info` field.
243 unsafe impl RawDeviceIdIndex for DeviceId {
244     const DRIVER_DATA_OFFSET: usize = core::mem::offset_of!(bindings::usb_device_id, driver_info);
245 
246     fn index(&self) -> usize {
247         self.0.driver_info
248     }
249 }
250 
251 /// [`IdTable`](kernel::device_id::IdTable) type for USB.
252 pub type IdTable<T> = &'static dyn kernel::device_id::IdTable<DeviceId, T>;
253 
254 /// Create a USB `IdTable` with its alias for modpost.
255 #[macro_export]
256 macro_rules! usb_device_table {
257     ($table_name:ident, $module_table_name:ident, $id_info_type: ty, $table_data: expr) => {
258         const $table_name: $crate::device_id::IdArray<
259             $crate::usb::DeviceId,
260             $id_info_type,
261             { $table_data.len() },
262         > = $crate::device_id::IdArray::new($table_data);
263 
264         $crate::module_device_table!("usb", $module_table_name, $table_name);
265     };
266 }
267 
268 /// The USB driver trait.
269 ///
270 /// # Examples
271 ///
272 ///```
273 /// # use kernel::{bindings, device::Core, usb};
274 /// use kernel::prelude::*;
275 ///
276 /// struct MyDriver;
277 ///
278 /// kernel::usb_device_table!(
279 ///     USB_TABLE,
280 ///     MODULE_USB_TABLE,
281 ///     <MyDriver as usb::Driver>::IdInfo,
282 ///     [
283 ///         (usb::DeviceId::from_id(0x1234, 0x5678), ()),
284 ///         (usb::DeviceId::from_id(0xabcd, 0xef01), ()),
285 ///     ]
286 /// );
287 ///
288 /// impl usb::Driver for MyDriver {
289 ///     type IdInfo = ();
290 ///     type Data = Self;
291 ///     const ID_TABLE: usb::IdTable<Self::IdInfo> = &USB_TABLE;
292 ///
293 ///     fn probe(
294 ///         _interface: &usb::Interface<Core<'_>>,
295 ///         _id: &usb::DeviceId,
296 ///         _info: &Self::IdInfo,
297 ///     ) -> impl PinInit<Self::Data, Error> {
298 ///         Err(ENODEV)
299 ///     }
300 ///
301 ///     fn disconnect(_interface: &usb::Interface<Core<'_>>, _data: Pin<&Self::Data>) {}
302 /// }
303 ///```
304 pub trait Driver {
305     /// The type holding information about each one of the device ids supported by the driver.
306     type IdInfo: 'static;
307 
308     /// The type of the driver's bus device private data.
309     type Data: Send;
310 
311     /// The table of device ids supported by the driver.
312     const ID_TABLE: IdTable<Self::IdInfo>;
313 
314     /// USB driver probe.
315     ///
316     /// Called when a new USB interface is bound to this driver.
317     /// Implementers should attempt to initialize the interface here.
318     fn probe(
319         interface: &Interface<device::Core<'_>>,
320         id: &DeviceId,
321         id_info: &Self::IdInfo,
322     ) -> impl PinInit<Self::Data, Error>;
323 
324     /// USB driver disconnect.
325     ///
326     /// Called when the USB interface is about to be unbound from this driver.
327     fn disconnect(interface: &Interface<device::Core<'_>>, data: Pin<&Self::Data>);
328 }
329 
330 /// A USB interface.
331 ///
332 /// This structure represents the Rust abstraction for a C [`struct usb_interface`].
333 /// The implementation abstracts the usage of a C [`struct usb_interface`] passed
334 /// in from the C side.
335 ///
336 /// # Invariants
337 ///
338 /// An [`Interface`] instance represents a valid [`struct usb_interface`] created
339 /// by the C portion of the kernel.
340 ///
341 /// [`struct usb_interface`]: https://www.kernel.org/doc/html/latest/driver-api/usb/usb.html#c.usb_interface
342 #[repr(transparent)]
343 pub struct Interface<Ctx: device::DeviceContext = device::Normal>(
344     Opaque<bindings::usb_interface>,
345     PhantomData<Ctx>,
346 );
347 
348 impl<Ctx: device::DeviceContext> Interface<Ctx> {
349     fn as_raw(&self) -> *mut bindings::usb_interface {
350         self.0.get()
351     }
352 }
353 
354 // SAFETY: `usb::Interface` is a transparent wrapper of `struct usb_interface`.
355 // The offset is guaranteed to point to a valid device field inside `usb::Interface`.
356 unsafe impl<Ctx: device::DeviceContext> device::AsBusDevice<Ctx> for Interface<Ctx> {
357     const OFFSET: usize = offset_of!(bindings::usb_interface, dev);
358 }
359 
360 // SAFETY: `Interface` is a transparent wrapper of a type that doesn't depend on
361 // `Interface`'s generic argument.
362 kernel::impl_device_context_deref!(unsafe { Interface });
363 kernel::impl_device_context_into_aref!(Interface);
364 
365 impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Interface<Ctx> {
366     fn as_ref(&self) -> &device::Device<Ctx> {
367         // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
368         // `struct usb_interface`.
369         let dev = unsafe { &raw mut ((*self.as_raw()).dev) };
370 
371         // SAFETY: `dev` points to a valid `struct device`.
372         unsafe { device::Device::from_raw(dev) }
373     }
374 }
375 
376 impl<Ctx: device::DeviceContext> AsRef<Device> for Interface<Ctx> {
377     fn as_ref(&self) -> &Device {
378         // SAFETY: `self.as_raw()` is valid by the type invariants.
379         let usb_dev = unsafe { bindings::interface_to_usbdev(self.as_raw()) };
380 
381         // SAFETY: For a valid `struct usb_interface` pointer, the above call to
382         // `interface_to_usbdev()` guarantees to return a valid pointer to a `struct usb_device`.
383         unsafe { &*(usb_dev.cast()) }
384     }
385 }
386 
387 // SAFETY: Instances of `Interface` are always reference-counted.
388 unsafe impl AlwaysRefCounted for Interface {
389     fn inc_ref(&self) {
390         // SAFETY: The invariants of `Interface` guarantee that `self.as_raw()`
391         // returns a valid `struct usb_interface` pointer, for which we will
392         // acquire a new refcount.
393         unsafe { bindings::usb_get_intf(self.as_raw()) };
394     }
395 
396     unsafe fn dec_ref(obj: NonNull<Self>) {
397         // SAFETY: The safety requirements guarantee that the refcount is non-zero.
398         unsafe { bindings::usb_put_intf(obj.cast().as_ptr()) }
399     }
400 }
401 
402 // SAFETY: A `Interface` is always reference-counted and can be released from any thread.
403 unsafe impl Send for Interface {}
404 
405 // SAFETY: It is safe to send a &Interface to another thread because we do not
406 // allow any mutation through a shared reference.
407 unsafe impl Sync for Interface {}
408 
409 /// A USB device.
410 ///
411 /// This structure represents the Rust abstraction for a C [`struct usb_device`].
412 /// The implementation abstracts the usage of a C [`struct usb_device`] passed in
413 /// from the C side.
414 ///
415 /// # Invariants
416 ///
417 /// A [`Device`] instance represents a valid [`struct usb_device`] created by the C portion of the
418 /// kernel.
419 ///
420 /// [`struct usb_device`]: https://www.kernel.org/doc/html/latest/driver-api/usb/usb.html#c.usb_device
421 #[repr(transparent)]
422 struct Device<Ctx: device::DeviceContext = device::Normal>(
423     Opaque<bindings::usb_device>,
424     PhantomData<Ctx>,
425 );
426 
427 impl<Ctx: device::DeviceContext> Device<Ctx> {
428     fn as_raw(&self) -> *mut bindings::usb_device {
429         self.0.get()
430     }
431 }
432 
433 // SAFETY: `Device` is a transparent wrapper of a type that doesn't depend on `Device`'s generic
434 // argument.
435 kernel::impl_device_context_deref!(unsafe { Device });
436 kernel::impl_device_context_into_aref!(Device);
437 
438 // SAFETY: Instances of `Device` are always reference-counted.
439 unsafe impl AlwaysRefCounted for Device {
440     fn inc_ref(&self) {
441         // SAFETY: The invariants of `Device` guarantee that `self.as_raw()`
442         // returns a valid `struct usb_device` pointer, for which we will
443         // acquire a new refcount.
444         unsafe { bindings::usb_get_dev(self.as_raw()) };
445     }
446 
447     unsafe fn dec_ref(obj: NonNull<Self>) {
448         // SAFETY: The safety requirements guarantee that the refcount is non-zero.
449         unsafe { bindings::usb_put_dev(obj.cast().as_ptr()) }
450     }
451 }
452 
453 impl<Ctx: device::DeviceContext> AsRef<device::Device<Ctx>> for Device<Ctx> {
454     fn as_ref(&self) -> &device::Device<Ctx> {
455         // SAFETY: By the type invariant of `Self`, `self.as_raw()` is a pointer to a valid
456         // `struct usb_device`.
457         let dev = unsafe { &raw mut ((*self.as_raw()).dev) };
458 
459         // SAFETY: `dev` points to a valid `struct device`.
460         unsafe { device::Device::from_raw(dev) }
461     }
462 }
463 
464 // SAFETY: A `Device` is always reference-counted and can be released from any thread.
465 unsafe impl Send for Device {}
466 
467 // SAFETY: It is safe to send a &Device to another thread because we do not
468 // allow any mutation through a shared reference.
469 unsafe impl Sync for Device {}
470 
471 // SAFETY: Same as `Device<Normal>` -- the underlying `struct usb_device` is the same;
472 // `Bound` is a zero-sized type-state marker that does not affect thread safety.
473 unsafe impl Sync for Device<device::Bound> {}
474 
475 /// Declares a kernel module that exposes a single USB driver.
476 ///
477 /// # Examples
478 ///
479 /// ```ignore
480 /// module_usb_driver! {
481 ///     type: MyDriver,
482 ///     name: "Module name",
483 ///     author: ["Author name"],
484 ///     description: "Description",
485 ///     license: "GPL v2",
486 /// }
487 /// ```
488 #[macro_export]
489 macro_rules! module_usb_driver {
490     ($($f:tt)*) => {
491         $crate::module_driver!(<T>, $crate::usb::Adapter<T>, { $($f)* });
492     }
493 }
494