pci_get_class(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… pci_get_class(9) β€” from the Kernel API Manual, bestie

Okay so picture this: you're deep in kernel land, you need to find a PCI device by its class, and you don't wanna write the loop yourself. That's literally this function. It's pci_get_class, and it's here to start β€” or keep going with β€” a search for a PCI device by class. πŸŽ€


✨ SYNOPSIS ✨

struct pci_dev * pci_get_class (unsigned int class , struct pci_dev *from );

That's the whole vibe. Two args, one pointer back out. 🫢


🌸 ARGUMENTS 🌸

  • class β€” the class designation you're hunting for. You want a PCI device with this class, and this is where you say so.
  • from β€” the previous PCI device you already found in this search, or NULL if you're starting fresh. Brand new search? Pass NULL. Continuing? Pass the last one. Easy.

πŸ¦‹ DESCRIPTION πŸ¦‹

So what it actually does: it iterates through the list of known PCI devices. Iconic behavior, no notes. πŸ’…

If a PCI device turns up with a matching class, the reference count on that device gets incremented and you get back a pointer to its device structure. Slay. You got your device.

Otherwise? NULL. That's it. That's the answer. 😭

Now, the search mechanics, because this is where people get caught out:

  • You kick off a new search by passing NULL as the from argument. Fresh start, main character energy. ✨
  • If from is not NULL, the search continues from the next device on the global list. Not the one you passed β€” the next one. iykyk.
  • And listen β€” the reference count for from is always decremented if it is not NULL. Always. Non-negotiable. So don't go thinking you get to keep that reference, because you don't. 🫠
Heads up bestie: that automatic decrement is the whole reason this pattern works as a loop. You hand it the last thing, it lets go of it, it gives you the next thing. Trust the process. πŸ’–

πŸ‘‘ SEE ALSO πŸ‘‘

Kernel file ./drivers/pci/search.c β€” go read it, it's giving source of truth. 🌈

And the whole friend group:


⭐ Okay that's the page!! Shoutout to ./drivers/pci for hosting, and this little love letter is dated October 2026 β€” go find your devices, bestie, you've got this πŸ’œπŸ§šπŸ“

πŸ“¦ where this page comes from