Subject: Device properties anyone?
To: None <tech-kern@netbsd.org>
From: None <eeh@netbsd.org>
List: tech-kern
Date: 01/16/2001 23:57:24
Here's a quick writeup of a proposal I've had floating around that
I think would be quite useful:



				Device Properties


1	Summary

I propose adding properties and accessors to device nodes.  There are a number
of cases where drivers need to pass information to other drivers or acquire
information that is not available to them and accessible only through machine
dependent code.  To solve these sorts of problems I propose adding a property
framework to allow arbitrary data to be associated with particular device nodes.

Examples of data that are currently problematical but could easily be passed
as properties are device locators, which require and arbitrary bus-specific
structure to pass betwen the parent bus node and the child device node, and
MAC addresses for ethernet devices, which on some machines are available by a
simple firmware call, but on others require the driver to dig bits out of an
EPROM.

2	Synopsis

   int setprop(struct device *dev, const char *name, void *val, 
			size_t len, int flags);
   size_t getprop(struct device *dev, const char *name, void *val, 
			size_t len, int flags);
   int delprop(struct device *dev, const char *name, int flags);

   size_t md_getprop(struct device *dev, const char *name, void *val, 
			size_t len, int flags);


3	Description

A property is a tuple that consists of a pointer to a device node (struct
device *), string, and an arbitrary amount of data.  This tuple is established
by setprop(), retrieved by getprop(), and destroyed by delprop().  In
addition, there is a machine dependent hook, md_getprop()[1] that is called if
attempting to retrieve a property fails.  md_getprop() can then use any
arbitrary method to generate property data if it so desires, or cause a failure
to be reported.


int setprop(struct device *dev, const char *name, void *val, size_t len, int flags);

Create a property `name' and attach it to `dev' with a `len' byte value 
copied from location `val'.  Returns 0 on success or an error value.


size_t getprop(struct device *dev, const char *name, void *val, 
			size_t len, int flags);

Retrieve a property called `name' associated with `dev'.  If the property is
not found it will call md_getprop(9).  If the flag PROP_INHERIT is set, and
there is no property with that name associated with this device node, it will
try to retrieve the property from any parent device nodes.  Returns zero if
the property cannot be found, otherwise it returns the length of the value
data and if `val' is not NULL it copies up to `len' bytes of the property data
to the location pointed to by `val'.


int delprop(struct device *dev, const char *name, int flags);

Remove a property from a device node.  If the flag PROP_INHERIT is set it will
look for that property in any parent device nodes.  If the flag PROP_ALL is set
it will remove the property from that device node and all parent device nodes.
It returns 0 on success or an error value.


extern void * md_getprop(struct device *dev, const char *name, void *val, 
			size_t len, int flags);

This function is called when ever an attempt is made to retrieve a property
from a device node but the property is not found.  It allows machine dependent
code to look up properties from other locations.  It should be implemented to
behave the same way as getprop(9) does.




[1] Providing md_setprop() and md_delprop() would be possible but make the
framework much more complicated.  If permanent storage of properties is
desired it should use some machine dependent method since non-volatile
storage is not necessarily available on all architectures.



Eduardo