DEVICE_GET_STATE(9)

πŸ’– DEVICE_GET_STATE πŸ’–

okay bestie, we're in the Kernel Developer's Manual section 9 and this is your girl's guide to poking at device state. yes it's kernel-adjacent. yes you're gonna slay it. πŸ’…


✨ NAME ✨

device_get_state, device_busy, device_unbusy, device_is_alive, device_is_attached β€” the whole squad that lets you manipulate device state. that's the entire vibe, no cap.


🌸 SYNOPSIS 🌸

like always, you gotta bring your friends along first:

#include <sys/param.h>
#include <sys/bus.h>

then the actual icons:

device_state_t
device_get_state(device_t dev);

void
device_busy(device_t dev);

void
device_unbusy(device_t dev);

int
device_is_alive(device_t dev);

int
device_is_attached(device_t dev);

πŸ¦‹ DESCRIPTION πŸ¦‹

so, wanna know what's going on with a device rn? call device_get_state(). she hands you back one of these four:

  • DS_NOTPRESENT
  • DS_ALIVE
  • DS_ATTACHED
  • DS_BUSY

(yes all four are described over in device(9), go read that if you need the lore 🫢)

trying to figure out if a device successfully probed? that's device_is_alive(). she literally just returns true if the state is greater or equal to DS_ALIVE. simple. lowkey elegant.

trying to figure out if a device successfully attached? that's device_is_attached(). same deal β€” she just returns if the state is greater or equal to DS_ATTACHED. periodt. ✨


okay now here's the juicy part. every device has a busy count πŸ‘‘

  • device_busy() bumps it up ⬆️
  • device_unbusy() brings it down ⬇️
🚨 both routines return an error if the device state is less than DS_ATTACHED. do not come for them when they're not ready, bestie.

and the glow-up moments:

  • when device_busy() is called on a device that's in the DS_ATTACHED state, the device changes to the DS_BUSY state. main character transition fr fr. πŸ’–
  • when device_unbusy() is called and β€” after decrementing β€” the busy count for the device is zero, the device changes to the DS_ATTACHED state. she's back home. 😌

🌈 SEE ALSO 🌈


πŸ‘‘ AUTHORS πŸ‘‘

this manual page was written by Doug Rabson. thank you king, we are obsessed. πŸŽ€


and that's a wrap, gorgeous! this page came to you from the Debian edition of the Kernel Developer's Manual, dated June 16, 1998 β€” yes, an elder, and yes, still iconic. go busy some devices responsibly. mwah! πŸ’‹πŸ¦‹

πŸ“¦ where this page comes from