[PATCH v6 2/5] rust: pci: provide access to PCI Vendor values
Danilo Krummrich
dakr at kernel.org
Mon Aug 25 12:47:04 UTC 2025
On Mon Aug 25, 2025 at 2:33 PM CEST, Alexandre Courbot wrote:
>> +#[derive(Debug, Clone, Copy, PartialEq, Eq)]
>> +#[repr(transparent)]
>> +pub struct Vendor(u32);
>> +
>> +macro_rules! define_all_pci_vendors {
>> + (
>> + $($variant:ident = $binding:expr,)+
>> + ) => {
>> +
>> + impl Vendor {
>> + $(
>> + #[allow(missing_docs)]
>> + pub const $variant: Self = Self($binding as u32);
>> + )+
>> + }
>> +
>> + /// Convert a raw 16-bit vendor ID to a `Vendor`.
>> + impl From<u32> for Vendor {
>> + fn from(value: u32) -> Self {
>> + match value {
>> + $(x if x == Self::$variant.0 => Self::$variant,)+
>> + _ => Self::UNKNOWN,
>> + }
>> + }
>
> Naive question from someone with a device tree background and almost no
> PCI experience: one consequence of using `From` here is that if I create
> an non-registered Vendor value (e.g. `let vendor =
> Vendor::from(0xf0f0)`), then do `vendor.as_raw()`, I won't get the value
> passed initially but the one for `UNKNOWN`, e.g. `0xffff`. Are we ok
> with this?
I think that's fine, since we shouldn't actually hit this. Drivers should only
ever use the pre-defined constants of Vendor; consequently the
Device::vendor_id() can't return UNKNOWN either.
So, I think the From impl is not ideal, since we can't limit its visibility. In
order to improve this, I suggest to use Vendor::new() directly in the macro, and
make Vendor::new() private. The same goes for Class, I guess.
More information about the Nouveau
mailing list