class_find_device(9)

πŸ“¦ version 7.3.1, in nixpkgs unstable

πŸ’… class_find_device β€” bestie's guide to finding one specific device

ok so real talk, you're in the Kernel API Manual (section 9, kernel internals, iykyk) and the function we're stanning today is class_find_device, and honestly? it's giving "where's that one device" energy. it's the device iterator that goes looking for a particular device instead of just vibing through the whole class. let's get into it πŸŽ€


✨ SYNOPSIS ✨

struct device * class_find_device (const struct class *class , const
       struct device *start , const void *data , device_match_t match );

like, four args, one struct device * back. clean. iconic. no notes. 🌸


🫢 ARGUMENTS 🫢

  • class β€” the class we're iterating, aka the friend group we're scrolling through πŸ‘‘
  • start β€” Device to begin with, aka where you start scrolling from (yes, literally the starting device)
  • data β€” data for the match function β€” this is the little care package you hand to your callback so it knows what it's looking for 🎁
  • match β€” function to check device β€” the vibe check callback, the one who decides who's the one πŸ”₯

πŸ’– DESCRIPTION πŸ’–

so this is similar to the class_for_each_dev function above, but instead of just doing a drive-by over everything, it returns a reference to a device that is 'found' for later use, as determined by the match callback. so you're not just visiting β€” you're keeping one. main character device energy fr fr πŸ’…

the callback should return 0 if the device doesn't match and non-zero if it does. if the callback returns non-zero, this function will return to the caller and not iterate over any more devices β€” it's done, it found its person, the search is over, periodt πŸ¦‹

⚠️ okay bestie listen β€” you will need to drop the reference with put_device after use. do not forget. do not leave it hanging. that's a refcount leak and we do NOT do that in this house 😭

and here's the cute part: match is allowed to do anything including calling back into class code. there's no locking restriction. no cap. go wild. 🌈


πŸ” SEE ALSO πŸ”

Kernel file ./drivers/base/class.c β€” and the whole squad:


and that's the whole page, bestie πŸ’œ this lil slice of kernel knowledge lives in ./drivers/base, stamped October 2026, under the class_find_device(9) banner β€” go forth, find your device, and remember to put_device 🫢✨

πŸ“¦ where this page comes from