Re: What's the purpose of subsys_interface in linux/device.h?

2016-12-16 Thread Alexander Kapshuk
On Fri, Dec 16, 2016 at 8:57 AM, Perr Zhang  wrote:
>   On Fri, 16 Dec 2016 00:26:24 +0800 Alexander Kapshuk 
>  wrote 
>  >
>  > Having looked at the pieces of source code below, it appears that
>  > subsys_interface is the means to expose specific functionality of a
>  > subsystem/class of devices, as the commentary below states.
>  > include/linux/device.h:331,350
>  > /**
>  >  * struct subsys_interface - interfaces to device functions
>  >  * @name:   name of the device function
>  >  * @subsys: subsytem of the devices to attach to
>  >  * @node:   the list of functions registered at the subsystem
>  >  * @add_dev:device hookup to device function handler
>  >  * @remove_dev: device hookup to device function handler
>  >  *
>  >  * Simple interfaces attached to a subsystem. Multiple interfaces can
>  >  * attach to a subsystem and its devices. Unlike drivers, they do not
>  >  * exclusively claim or control devices. Interfaces usually represent
>  >  * a specific functionality of a subsystem/class of devices.
>  >  */
>  > struct subsys_interface {
>  > const char *name;
>  > struct bus_type *subsys;
>  > struct list_head node;
>  > int (*add_dev)(struct device *dev, struct subsys_interface *sif);
>  > void (*remove_dev)(struct device *dev, struct subsys_interface *sif);
>  > };
>  >
>  > Here's sample usage:
>  > arch/tile/kernel/sysfs.c:211,216
>  > static struct subsys_interface hv_stats_interface = {
>  > .name = "hv_stats",
>  > .subsys = _subsys,
>  > .add_dev = hv_stats_device_add,
>  > .remove_dev = hv_stats_device_remove,
>  > };
>  >
>  > And the add_dev method, whose main purpose seems to be creating a
>  > sysfs file for a particular device:
>  > arch/tile/kernel/sysfs.c:189,199
>  > static int hv_stats_device_add(struct device *dev, struct subsys_interface 
> *sif)
>  > {
>  > int err, cpu = dev->id;
>  >
>  > if (!cpu_online(cpu))
>  > return 0;
>  >
>  > err = sysfs_create_file(>kobj, _attr_hv_stats.attr);
>  >
>  > return err;
>  > }
>  >
>  > Here's an example of the hv_stats_interface being registered with the 
> subsystem:
>  > arch/tile/kernel/sysfs.c:261
>  > err = subsys_interface_register(_stats_interface);
>  >
>  > Hopefully, other members of this list will offer a better informed
>  > explanation than that given here.
>
> Sorry for sending email repeatedly.
> I have also read the code:
> device_add() ->  bus_add_device() -> device_add_attrs(bus, dev) -> 
> device_create_file(dev, >dev_attrs[i])
> Since the attribute file/functionality could be added through bus->dev_attrs, 
> why not to do it in this way?
>

My understanding is that an interface is like a protocol that has been
put in place to enable a particular set of functions within a given
subsystem or device model.

I am sending a copy of this email to the list as well, so you can
hopefully get a better informed reply to your query.

___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


What's the purpose of subsys_interface in linux/device.h?

2016-12-15 Thread Perr Zhang
Hi, I am reading the code here: device_add()->bus_probe_device()
the source code of bus_probe_device() is:

void bus_probe_device(struct device *dev)
{
struct bus_type *bus = dev->bus;
struct subsys_interface *sif;
..
mutex_lock(>p->mutex);
list_for_each_entry(sif, >p->interfaces, node)
if (sif->add_dev)
sif->add_dev(dev, sif);
mutex_unlock(>p->mutex);
}
location: http://lxr.free-electrons.com/source/drivers/base/bus.c#L543

Problem:
I have already read the relational code of struct subsys_interface, and can't
figure out the purpose of struce subsys_interface and its method add_dev()

Thank you,

--
Perr


___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies