186beb976SMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
286beb976SMauro Carvalho Chehab
386beb976SMauro Carvalho Chehab=====================================================
486beb976SMauro Carvalho Chehabsysfs - _The_ filesystem for exporting kernel objects
586beb976SMauro Carvalho Chehab=====================================================
686beb976SMauro Carvalho Chehab
786beb976SMauro Carvalho ChehabPatrick Mochel	<mochel@osdl.org>
886beb976SMauro Carvalho Chehab
986beb976SMauro Carvalho ChehabMike Murphy <mamurph@cs.clemson.edu>
1086beb976SMauro Carvalho Chehab
1186beb976SMauro Carvalho Chehab:Revised:    16 August 2011
1286beb976SMauro Carvalho Chehab:Original:   10 January 2003
1386beb976SMauro Carvalho Chehab
1486beb976SMauro Carvalho Chehab
15a3ee8b3aSRandy DunlapWhat it is
16a3ee8b3aSRandy Dunlap~~~~~~~~~~
1786beb976SMauro Carvalho Chehab
18a3ee8b3aSRandy Dunlapsysfs is a RAM-based filesystem initially based on ramfs. It provides
1986beb976SMauro Carvalho Chehaba means to export kernel data structures, their attributes, and the
2086beb976SMauro Carvalho Chehablinkages between them to userspace.
2186beb976SMauro Carvalho Chehab
2286beb976SMauro Carvalho Chehabsysfs is tied inherently to the kobject infrastructure. Please read
230c1bc6b8SMauro Carvalho ChehabDocumentation/core-api/kobject.rst for more information concerning the kobject
2486beb976SMauro Carvalho Chehabinterface.
2586beb976SMauro Carvalho Chehab
2686beb976SMauro Carvalho Chehab
2786beb976SMauro Carvalho ChehabUsing sysfs
2886beb976SMauro Carvalho Chehab~~~~~~~~~~~
2986beb976SMauro Carvalho Chehab
3086beb976SMauro Carvalho Chehabsysfs is always compiled in if CONFIG_SYSFS is defined. You can access
3186beb976SMauro Carvalho Chehabit by doing::
3286beb976SMauro Carvalho Chehab
3386beb976SMauro Carvalho Chehab    mount -t sysfs sysfs /sys
3486beb976SMauro Carvalho Chehab
3586beb976SMauro Carvalho Chehab
3686beb976SMauro Carvalho ChehabDirectory Creation
3786beb976SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~
3886beb976SMauro Carvalho Chehab
3986beb976SMauro Carvalho ChehabFor every kobject that is registered with the system, a directory is
4086beb976SMauro Carvalho Chehabcreated for it in sysfs. That directory is created as a subdirectory
4186beb976SMauro Carvalho Chehabof the kobject's parent, expressing internal object hierarchies to
4286beb976SMauro Carvalho Chehabuserspace. Top-level directories in sysfs represent the common
4386beb976SMauro Carvalho Chehabancestors of object hierarchies; i.e. the subsystems the objects
4486beb976SMauro Carvalho Chehabbelong to.
4586beb976SMauro Carvalho Chehab
46a3ee8b3aSRandy Dunlapsysfs internally stores a pointer to the kobject that implements a
4786beb976SMauro Carvalho Chehabdirectory in the kernfs_node object associated with the directory. In
4886beb976SMauro Carvalho Chehabthe past this kobject pointer has been used by sysfs to do reference
4986beb976SMauro Carvalho Chehabcounting directly on the kobject whenever the file is opened or closed.
5086beb976SMauro Carvalho ChehabWith the current sysfs implementation the kobject reference count is
5186beb976SMauro Carvalho Chehabonly modified directly by the function sysfs_schedule_callback().
5286beb976SMauro Carvalho Chehab
5386beb976SMauro Carvalho Chehab
5486beb976SMauro Carvalho ChehabAttributes
5586beb976SMauro Carvalho Chehab~~~~~~~~~~
5686beb976SMauro Carvalho Chehab
5786beb976SMauro Carvalho ChehabAttributes can be exported for kobjects in the form of regular files in
58a3ee8b3aSRandy Dunlapthe filesystem. sysfs forwards file I/O operations to methods defined
5986beb976SMauro Carvalho Chehabfor the attributes, providing a means to read and write kernel
6086beb976SMauro Carvalho Chehabattributes.
6186beb976SMauro Carvalho Chehab
6286beb976SMauro Carvalho ChehabAttributes should be ASCII text files, preferably with only one value
6386beb976SMauro Carvalho Chehabper file. It is noted that it may not be efficient to contain only one
6486beb976SMauro Carvalho Chehabvalue per file, so it is socially acceptable to express an array of
6586beb976SMauro Carvalho Chehabvalues of the same type.
6686beb976SMauro Carvalho Chehab
6786beb976SMauro Carvalho ChehabMixing types, expressing multiple lines of data, and doing fancy
6886beb976SMauro Carvalho Chehabformatting of data is heavily frowned upon. Doing these things may get
6986beb976SMauro Carvalho Chehabyou publicly humiliated and your code rewritten without notice.
7086beb976SMauro Carvalho Chehab
7186beb976SMauro Carvalho Chehab
7286beb976SMauro Carvalho ChehabAn attribute definition is simply::
7386beb976SMauro Carvalho Chehab
7486beb976SMauro Carvalho Chehab    struct attribute {
7586beb976SMauro Carvalho Chehab	    char                    *name;
7686beb976SMauro Carvalho Chehab	    struct module           *owner;
7786beb976SMauro Carvalho Chehab	    umode_t                 mode;
7886beb976SMauro Carvalho Chehab    };
7986beb976SMauro Carvalho Chehab
8086beb976SMauro Carvalho Chehab
8186beb976SMauro Carvalho Chehab    int sysfs_create_file(struct kobject * kobj, const struct attribute * attr);
8286beb976SMauro Carvalho Chehab    void sysfs_remove_file(struct kobject * kobj, const struct attribute * attr);
8386beb976SMauro Carvalho Chehab
8486beb976SMauro Carvalho Chehab
8586beb976SMauro Carvalho ChehabA bare attribute contains no means to read or write the value of the
8686beb976SMauro Carvalho Chehabattribute. Subsystems are encouraged to define their own attribute
8786beb976SMauro Carvalho Chehabstructure and wrapper functions for adding and removing attributes for
8886beb976SMauro Carvalho Chehaba specific object type.
8986beb976SMauro Carvalho Chehab
9086beb976SMauro Carvalho ChehabFor example, the driver model defines struct device_attribute like::
9186beb976SMauro Carvalho Chehab
9286beb976SMauro Carvalho Chehab    struct device_attribute {
9386beb976SMauro Carvalho Chehab	    struct attribute	attr;
9486beb976SMauro Carvalho Chehab	    ssize_t (*show)(struct device *dev, struct device_attribute *attr,
9586beb976SMauro Carvalho Chehab			    char *buf);
9686beb976SMauro Carvalho Chehab	    ssize_t (*store)(struct device *dev, struct device_attribute *attr,
9786beb976SMauro Carvalho Chehab			    const char *buf, size_t count);
9886beb976SMauro Carvalho Chehab    };
9986beb976SMauro Carvalho Chehab
10086beb976SMauro Carvalho Chehab    int device_create_file(struct device *, const struct device_attribute *);
10186beb976SMauro Carvalho Chehab    void device_remove_file(struct device *, const struct device_attribute *);
10286beb976SMauro Carvalho Chehab
10386beb976SMauro Carvalho ChehabIt also defines this helper for defining device attributes::
10486beb976SMauro Carvalho Chehab
10586beb976SMauro Carvalho Chehab    #define DEVICE_ATTR(_name, _mode, _show, _store) \
10686beb976SMauro Carvalho Chehab    struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)
10786beb976SMauro Carvalho Chehab
10886beb976SMauro Carvalho ChehabFor example, declaring::
10986beb976SMauro Carvalho Chehab
11086beb976SMauro Carvalho Chehab    static DEVICE_ATTR(foo, S_IWUSR | S_IRUGO, show_foo, store_foo);
11186beb976SMauro Carvalho Chehab
11286beb976SMauro Carvalho Chehabis equivalent to doing::
11386beb976SMauro Carvalho Chehab
11486beb976SMauro Carvalho Chehab    static struct device_attribute dev_attr_foo = {
11586beb976SMauro Carvalho Chehab	    .attr = {
11686beb976SMauro Carvalho Chehab		    .name = "foo",
11786beb976SMauro Carvalho Chehab		    .mode = S_IWUSR | S_IRUGO,
11886beb976SMauro Carvalho Chehab	    },
11986beb976SMauro Carvalho Chehab	    .show = show_foo,
12086beb976SMauro Carvalho Chehab	    .store = store_foo,
12186beb976SMauro Carvalho Chehab    };
12286beb976SMauro Carvalho Chehab
12386beb976SMauro Carvalho ChehabNote as stated in include/linux/kernel.h "OTHER_WRITABLE?  Generally
12486beb976SMauro Carvalho Chehabconsidered a bad idea." so trying to set a sysfs file writable for
12586beb976SMauro Carvalho Chehabeveryone will fail reverting to RO mode for "Others".
12686beb976SMauro Carvalho Chehab
12786beb976SMauro Carvalho ChehabFor the common cases sysfs.h provides convenience macros to make
12886beb976SMauro Carvalho Chehabdefining attributes easier as well as making code more concise and
12986beb976SMauro Carvalho Chehabreadable. The above case could be shortened to:
13086beb976SMauro Carvalho Chehab
13186beb976SMauro Carvalho Chehabstatic struct device_attribute dev_attr_foo = __ATTR_RW(foo);
13286beb976SMauro Carvalho Chehab
13386beb976SMauro Carvalho Chehabthe list of helpers available to define your wrapper function is:
13486beb976SMauro Carvalho Chehab
13586beb976SMauro Carvalho Chehab__ATTR_RO(name):
13686beb976SMauro Carvalho Chehab		 assumes default name_show and mode 0444
13786beb976SMauro Carvalho Chehab__ATTR_WO(name):
13886beb976SMauro Carvalho Chehab		 assumes a name_store only and is restricted to mode
13986beb976SMauro Carvalho Chehab                 0200 that is root write access only.
14086beb976SMauro Carvalho Chehab__ATTR_RO_MODE(name, mode):
141a3ee8b3aSRandy Dunlap	         for more restrictive RO access; currently
14286beb976SMauro Carvalho Chehab                 only use case is the EFI System Resource Table
14386beb976SMauro Carvalho Chehab                 (see drivers/firmware/efi/esrt.c)
14486beb976SMauro Carvalho Chehab__ATTR_RW(name):
14586beb976SMauro Carvalho Chehab	         assumes default name_show, name_store and setting
14686beb976SMauro Carvalho Chehab                 mode to 0644.
14786beb976SMauro Carvalho Chehab__ATTR_NULL:
14886beb976SMauro Carvalho Chehab	         which sets the name to NULL and is used as end of list
14986beb976SMauro Carvalho Chehab                 indicator (see: kernel/workqueue.c)
15086beb976SMauro Carvalho Chehab
15186beb976SMauro Carvalho ChehabSubsystem-Specific Callbacks
15286beb976SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15386beb976SMauro Carvalho Chehab
15486beb976SMauro Carvalho ChehabWhen a subsystem defines a new attribute type, it must implement a
15586beb976SMauro Carvalho Chehabset of sysfs operations for forwarding read and write calls to the
15686beb976SMauro Carvalho Chehabshow and store methods of the attribute owners::
15786beb976SMauro Carvalho Chehab
15886beb976SMauro Carvalho Chehab    struct sysfs_ops {
15986beb976SMauro Carvalho Chehab	    ssize_t (*show)(struct kobject *, struct attribute *, char *);
16086beb976SMauro Carvalho Chehab	    ssize_t (*store)(struct kobject *, struct attribute *, const char *, size_t);
16186beb976SMauro Carvalho Chehab    };
16286beb976SMauro Carvalho Chehab
16386beb976SMauro Carvalho Chehab[ Subsystems should have already defined a struct kobj_type as a
16486beb976SMauro Carvalho Chehabdescriptor for this type, which is where the sysfs_ops pointer is
16586beb976SMauro Carvalho Chehabstored. See the kobject documentation for more information. ]
16686beb976SMauro Carvalho Chehab
16786beb976SMauro Carvalho ChehabWhen a file is read or written, sysfs calls the appropriate method
16886beb976SMauro Carvalho Chehabfor the type. The method then translates the generic struct kobject
16986beb976SMauro Carvalho Chehaband struct attribute pointers to the appropriate pointer types, and
17086beb976SMauro Carvalho Chehabcalls the associated methods.
17186beb976SMauro Carvalho Chehab
17286beb976SMauro Carvalho Chehab
17386beb976SMauro Carvalho ChehabTo illustrate::
17486beb976SMauro Carvalho Chehab
17586beb976SMauro Carvalho Chehab    #define to_dev_attr(_attr) container_of(_attr, struct device_attribute, attr)
17686beb976SMauro Carvalho Chehab
17786beb976SMauro Carvalho Chehab    static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
17886beb976SMauro Carvalho Chehab				char *buf)
17986beb976SMauro Carvalho Chehab    {
18086beb976SMauro Carvalho Chehab	    struct device_attribute *dev_attr = to_dev_attr(attr);
181e046de3dSDenis Efremov	    struct device *dev = kobj_to_dev(kobj);
18286beb976SMauro Carvalho Chehab	    ssize_t ret = -EIO;
18386beb976SMauro Carvalho Chehab
18486beb976SMauro Carvalho Chehab	    if (dev_attr->show)
18586beb976SMauro Carvalho Chehab		    ret = dev_attr->show(dev, dev_attr, buf);
18686beb976SMauro Carvalho Chehab	    if (ret >= (ssize_t)PAGE_SIZE) {
18786beb976SMauro Carvalho Chehab		    printk("dev_attr_show: %pS returned bad count\n",
18886beb976SMauro Carvalho Chehab				    dev_attr->show);
18986beb976SMauro Carvalho Chehab	    }
19086beb976SMauro Carvalho Chehab	    return ret;
19186beb976SMauro Carvalho Chehab    }
19286beb976SMauro Carvalho Chehab
19386beb976SMauro Carvalho Chehab
19486beb976SMauro Carvalho Chehab
19586beb976SMauro Carvalho ChehabReading/Writing Attribute Data
19686beb976SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19786beb976SMauro Carvalho Chehab
19886beb976SMauro Carvalho ChehabTo read or write attributes, show() or store() methods must be
19986beb976SMauro Carvalho Chehabspecified when declaring the attribute. The method types should be as
20086beb976SMauro Carvalho Chehabsimple as those defined for device attributes::
20186beb976SMauro Carvalho Chehab
20286beb976SMauro Carvalho Chehab    ssize_t (*show)(struct device *dev, struct device_attribute *attr, char *buf);
20386beb976SMauro Carvalho Chehab    ssize_t (*store)(struct device *dev, struct device_attribute *attr,
20486beb976SMauro Carvalho Chehab		    const char *buf, size_t count);
20586beb976SMauro Carvalho Chehab
20686beb976SMauro Carvalho ChehabIOW, they should take only an object, an attribute, and a buffer as parameters.
20786beb976SMauro Carvalho Chehab
20886beb976SMauro Carvalho Chehab
20986beb976SMauro Carvalho Chehabsysfs allocates a buffer of size (PAGE_SIZE) and passes it to the
210a3ee8b3aSRandy Dunlapmethod. sysfs will call the method exactly once for each read or
21186beb976SMauro Carvalho Chehabwrite. This forces the following behavior on the method
21286beb976SMauro Carvalho Chehabimplementations:
21386beb976SMauro Carvalho Chehab
21486beb976SMauro Carvalho Chehab- On read(2), the show() method should fill the entire buffer.
21586beb976SMauro Carvalho Chehab  Recall that an attribute should only be exporting one value, or an
21686beb976SMauro Carvalho Chehab  array of similar values, so this shouldn't be that expensive.
21786beb976SMauro Carvalho Chehab
21886beb976SMauro Carvalho Chehab  This allows userspace to do partial reads and forward seeks
21986beb976SMauro Carvalho Chehab  arbitrarily over the entire file at will. If userspace seeks back to
22086beb976SMauro Carvalho Chehab  zero or does a pread(2) with an offset of '0' the show() method will
22186beb976SMauro Carvalho Chehab  be called again, rearmed, to fill the buffer.
22286beb976SMauro Carvalho Chehab
22386beb976SMauro Carvalho Chehab- On write(2), sysfs expects the entire buffer to be passed during the
224a3ee8b3aSRandy Dunlap  first write. sysfs then passes the entire buffer to the store() method.
22586beb976SMauro Carvalho Chehab  A terminating null is added after the data on stores. This makes
22686beb976SMauro Carvalho Chehab  functions like sysfs_streq() safe to use.
22786beb976SMauro Carvalho Chehab
22886beb976SMauro Carvalho Chehab  When writing sysfs files, userspace processes should first read the
22986beb976SMauro Carvalho Chehab  entire file, modify the values it wishes to change, then write the
23086beb976SMauro Carvalho Chehab  entire buffer back.
23186beb976SMauro Carvalho Chehab
23286beb976SMauro Carvalho Chehab  Attribute method implementations should operate on an identical
23386beb976SMauro Carvalho Chehab  buffer when reading and writing values.
23486beb976SMauro Carvalho Chehab
23586beb976SMauro Carvalho ChehabOther notes:
23686beb976SMauro Carvalho Chehab
23786beb976SMauro Carvalho Chehab- Writing causes the show() method to be rearmed regardless of current
23886beb976SMauro Carvalho Chehab  file position.
23986beb976SMauro Carvalho Chehab
240a3ee8b3aSRandy Dunlap- The buffer will always be PAGE_SIZE bytes in length. On x86, this
24186beb976SMauro Carvalho Chehab  is 4096.
24286beb976SMauro Carvalho Chehab
24386beb976SMauro Carvalho Chehab- show() methods should return the number of bytes printed into the
2442efc459dSJoe Perches  buffer.
24586beb976SMauro Carvalho Chehab
2462efc459dSJoe Perches- show() should only use sysfs_emit() or sysfs_emit_at() when formatting
2472efc459dSJoe Perches  the value to be returned to user space.
24886beb976SMauro Carvalho Chehab
24986beb976SMauro Carvalho Chehab- store() should return the number of bytes used from the buffer. If the
25086beb976SMauro Carvalho Chehab  entire buffer has been used, just return the count argument.
25186beb976SMauro Carvalho Chehab
25286beb976SMauro Carvalho Chehab- show() or store() can always return errors. If a bad value comes
25386beb976SMauro Carvalho Chehab  through, be sure to return an error.
25486beb976SMauro Carvalho Chehab
25586beb976SMauro Carvalho Chehab- The object passed to the methods will be pinned in memory via sysfs
256a3ee8b3aSRandy Dunlap  reference counting its embedded object. However, the physical
25786beb976SMauro Carvalho Chehab  entity (e.g. device) the object represents may not be present. Be
25886beb976SMauro Carvalho Chehab  sure to have a way to check this, if necessary.
25986beb976SMauro Carvalho Chehab
26086beb976SMauro Carvalho Chehab
26186beb976SMauro Carvalho ChehabA very simple (and naive) implementation of a device attribute is::
26286beb976SMauro Carvalho Chehab
26386beb976SMauro Carvalho Chehab    static ssize_t show_name(struct device *dev, struct device_attribute *attr,
26486beb976SMauro Carvalho Chehab			    char *buf)
26586beb976SMauro Carvalho Chehab    {
266fda8c908SAndy Shevchenko	    return sysfs_emit(buf, "%s\n", dev->name);
26786beb976SMauro Carvalho Chehab    }
26886beb976SMauro Carvalho Chehab
26986beb976SMauro Carvalho Chehab    static ssize_t store_name(struct device *dev, struct device_attribute *attr,
27086beb976SMauro Carvalho Chehab			    const char *buf, size_t count)
27186beb976SMauro Carvalho Chehab    {
27286beb976SMauro Carvalho Chehab	    snprintf(dev->name, sizeof(dev->name), "%.*s",
27386beb976SMauro Carvalho Chehab		    (int)min(count, sizeof(dev->name) - 1), buf);
27486beb976SMauro Carvalho Chehab	    return count;
27586beb976SMauro Carvalho Chehab    }
27686beb976SMauro Carvalho Chehab
27786beb976SMauro Carvalho Chehab    static DEVICE_ATTR(name, S_IRUGO, show_name, store_name);
27886beb976SMauro Carvalho Chehab
27986beb976SMauro Carvalho Chehab
28086beb976SMauro Carvalho Chehab(Note that the real implementation doesn't allow userspace to set the
28186beb976SMauro Carvalho Chehabname for a device.)
28286beb976SMauro Carvalho Chehab
28386beb976SMauro Carvalho Chehab
28486beb976SMauro Carvalho ChehabTop Level Directory Layout
28586beb976SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~~~~~~~~~
28686beb976SMauro Carvalho Chehab
28786beb976SMauro Carvalho ChehabThe sysfs directory arrangement exposes the relationship of kernel
28886beb976SMauro Carvalho Chehabdata structures.
28986beb976SMauro Carvalho Chehab
29086beb976SMauro Carvalho ChehabThe top level sysfs directory looks like::
29186beb976SMauro Carvalho Chehab
29286beb976SMauro Carvalho Chehab    block/
29386beb976SMauro Carvalho Chehab    bus/
29486beb976SMauro Carvalho Chehab    class/
29586beb976SMauro Carvalho Chehab    dev/
29686beb976SMauro Carvalho Chehab    devices/
29786beb976SMauro Carvalho Chehab    firmware/
29886beb976SMauro Carvalho Chehab    fs/
299a3ee8b3aSRandy Dunlap    hypervisor/
300a3ee8b3aSRandy Dunlap    kernel/
301a3ee8b3aSRandy Dunlap    module/
302a3ee8b3aSRandy Dunlap    net/
303a3ee8b3aSRandy Dunlap    power/
30486beb976SMauro Carvalho Chehab
30586beb976SMauro Carvalho Chehabdevices/ contains a filesystem representation of the device tree. It maps
30686beb976SMauro Carvalho Chehabdirectly to the internal kernel device tree, which is a hierarchy of
30786beb976SMauro Carvalho Chehabstruct device.
30886beb976SMauro Carvalho Chehab
30986beb976SMauro Carvalho Chehabbus/ contains flat directory layout of the various bus types in the
31086beb976SMauro Carvalho Chehabkernel. Each bus's directory contains two subdirectories::
31186beb976SMauro Carvalho Chehab
31286beb976SMauro Carvalho Chehab	devices/
31386beb976SMauro Carvalho Chehab	drivers/
31486beb976SMauro Carvalho Chehab
31586beb976SMauro Carvalho Chehabdevices/ contains symlinks for each device discovered in the system
31686beb976SMauro Carvalho Chehabthat point to the device's directory under root/.
31786beb976SMauro Carvalho Chehab
31886beb976SMauro Carvalho Chehabdrivers/ contains a directory for each device driver that is loaded
31986beb976SMauro Carvalho Chehabfor devices on that particular bus (this assumes that drivers do not
32086beb976SMauro Carvalho Chehabspan multiple bus types).
32186beb976SMauro Carvalho Chehab
32286beb976SMauro Carvalho Chehabfs/ contains a directory for some filesystems.  Currently each
32386beb976SMauro Carvalho Chehabfilesystem wanting to export attributes must create its own hierarchy
324a3ee8b3aSRandy Dunlapbelow fs/ (see ./fuse.rst for an example).
32586beb976SMauro Carvalho Chehab
326a3ee8b3aSRandy Dunlapmodule/ contains parameter values and state information for all
327a3ee8b3aSRandy Dunlaploaded system modules, for both builtin and loadable modules.
328a3ee8b3aSRandy Dunlap
329a3ee8b3aSRandy Dunlapdev/ contains two directories: char/ and block/. Inside these two
33086beb976SMauro Carvalho Chehabdirectories there are symlinks named <major>:<minor>.  These symlinks
33186beb976SMauro Carvalho Chehabpoint to the sysfs directory for the given device.  /sys/dev provides a
33286beb976SMauro Carvalho Chehabquick way to lookup the sysfs interface for a device from the result of
33386beb976SMauro Carvalho Chehaba stat(2) operation.
33486beb976SMauro Carvalho Chehab
335a3ee8b3aSRandy DunlapMore information on driver-model specific features can be found in
33686beb976SMauro Carvalho ChehabDocumentation/driver-api/driver-model/.
33786beb976SMauro Carvalho Chehab
33886beb976SMauro Carvalho Chehab
33986beb976SMauro Carvalho ChehabTODO: Finish this section.
34086beb976SMauro Carvalho Chehab
34186beb976SMauro Carvalho Chehab
34286beb976SMauro Carvalho ChehabCurrent Interfaces
34386beb976SMauro Carvalho Chehab~~~~~~~~~~~~~~~~~~
34486beb976SMauro Carvalho Chehab
345a3ee8b3aSRandy DunlapThe following interface layers currently exist in sysfs.
34686beb976SMauro Carvalho Chehab
34786beb976SMauro Carvalho Chehab
34886beb976SMauro Carvalho Chehabdevices (include/linux/device.h)
34986beb976SMauro Carvalho Chehab--------------------------------
35086beb976SMauro Carvalho ChehabStructure::
35186beb976SMauro Carvalho Chehab
35286beb976SMauro Carvalho Chehab    struct device_attribute {
35386beb976SMauro Carvalho Chehab	    struct attribute	attr;
35486beb976SMauro Carvalho Chehab	    ssize_t (*show)(struct device *dev, struct device_attribute *attr,
35586beb976SMauro Carvalho Chehab			    char *buf);
35686beb976SMauro Carvalho Chehab	    ssize_t (*store)(struct device *dev, struct device_attribute *attr,
35786beb976SMauro Carvalho Chehab			    const char *buf, size_t count);
35886beb976SMauro Carvalho Chehab    };
35986beb976SMauro Carvalho Chehab
36086beb976SMauro Carvalho ChehabDeclaring::
36186beb976SMauro Carvalho Chehab
36286beb976SMauro Carvalho Chehab    DEVICE_ATTR(_name, _mode, _show, _store);
36386beb976SMauro Carvalho Chehab
36486beb976SMauro Carvalho ChehabCreation/Removal::
36586beb976SMauro Carvalho Chehab
36686beb976SMauro Carvalho Chehab    int device_create_file(struct device *dev, const struct device_attribute * attr);
36786beb976SMauro Carvalho Chehab    void device_remove_file(struct device *dev, const struct device_attribute * attr);
36886beb976SMauro Carvalho Chehab
36986beb976SMauro Carvalho Chehab
37086beb976SMauro Carvalho Chehabbus drivers (include/linux/device.h)
37186beb976SMauro Carvalho Chehab------------------------------------
37286beb976SMauro Carvalho ChehabStructure::
37386beb976SMauro Carvalho Chehab
37486beb976SMauro Carvalho Chehab    struct bus_attribute {
37586beb976SMauro Carvalho Chehab	    struct attribute        attr;
376*75cff725SGreg Kroah-Hartman	    ssize_t (*show)(const struct bus_type *, char * buf);
377*75cff725SGreg Kroah-Hartman	    ssize_t (*store)(const struct bus_type *, const char * buf, size_t count);
37886beb976SMauro Carvalho Chehab    };
37986beb976SMauro Carvalho Chehab
38086beb976SMauro Carvalho ChehabDeclaring::
38186beb976SMauro Carvalho Chehab
38286beb976SMauro Carvalho Chehab    static BUS_ATTR_RW(name);
38386beb976SMauro Carvalho Chehab    static BUS_ATTR_RO(name);
38486beb976SMauro Carvalho Chehab    static BUS_ATTR_WO(name);
38586beb976SMauro Carvalho Chehab
38686beb976SMauro Carvalho ChehabCreation/Removal::
38786beb976SMauro Carvalho Chehab
38886beb976SMauro Carvalho Chehab    int bus_create_file(struct bus_type *, struct bus_attribute *);
38986beb976SMauro Carvalho Chehab    void bus_remove_file(struct bus_type *, struct bus_attribute *);
39086beb976SMauro Carvalho Chehab
39186beb976SMauro Carvalho Chehab
39286beb976SMauro Carvalho Chehabdevice drivers (include/linux/device.h)
39386beb976SMauro Carvalho Chehab---------------------------------------
39486beb976SMauro Carvalho Chehab
39586beb976SMauro Carvalho ChehabStructure::
39686beb976SMauro Carvalho Chehab
39786beb976SMauro Carvalho Chehab    struct driver_attribute {
39886beb976SMauro Carvalho Chehab	    struct attribute        attr;
39986beb976SMauro Carvalho Chehab	    ssize_t (*show)(struct device_driver *, char * buf);
40086beb976SMauro Carvalho Chehab	    ssize_t (*store)(struct device_driver *, const char * buf,
40186beb976SMauro Carvalho Chehab			    size_t count);
40286beb976SMauro Carvalho Chehab    };
40386beb976SMauro Carvalho Chehab
40486beb976SMauro Carvalho ChehabDeclaring::
40586beb976SMauro Carvalho Chehab
40686beb976SMauro Carvalho Chehab    DRIVER_ATTR_RO(_name)
40786beb976SMauro Carvalho Chehab    DRIVER_ATTR_RW(_name)
40886beb976SMauro Carvalho Chehab
40986beb976SMauro Carvalho ChehabCreation/Removal::
41086beb976SMauro Carvalho Chehab
41186beb976SMauro Carvalho Chehab    int driver_create_file(struct device_driver *, const struct driver_attribute *);
41286beb976SMauro Carvalho Chehab    void driver_remove_file(struct device_driver *, const struct driver_attribute *);
41386beb976SMauro Carvalho Chehab
41486beb976SMauro Carvalho Chehab
41586beb976SMauro Carvalho ChehabDocumentation
41686beb976SMauro Carvalho Chehab~~~~~~~~~~~~~
41786beb976SMauro Carvalho Chehab
41886beb976SMauro Carvalho ChehabThe sysfs directory structure and the attributes in each directory define an
41986beb976SMauro Carvalho ChehabABI between the kernel and user space. As for any ABI, it is important that
42086beb976SMauro Carvalho Chehabthis ABI is stable and properly documented. All new sysfs attributes must be
42186beb976SMauro Carvalho Chehabdocumented in Documentation/ABI. See also Documentation/ABI/README for more
42286beb976SMauro Carvalho Chehabinformation.
423