What's the purpose of subsys_interface in linux/device.h?
From: Alexander Kapshuk <hidden>
Date: 2016-12-16 10:44:02
On Fri, Dec 16, 2016 at 8:57 AM, Perr Zhang [off-list ref] wrote:
---- On Fri, 16 Dec 2016 00:26:24 +0800 Alexander Kapshuk [off-list ref] 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 = &cpu_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(&dev->kobj, &dev_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(&hv_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, &bus->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.