162306a36Sopenharmony_ci.. title:: Kernel-doc comments
262306a36Sopenharmony_ci
362306a36Sopenharmony_ci===========================
462306a36Sopenharmony_ciWriting kernel-doc comments
562306a36Sopenharmony_ci===========================
662306a36Sopenharmony_ci
762306a36Sopenharmony_ciThe Linux kernel source files may contain structured documentation
862306a36Sopenharmony_cicomments in the kernel-doc format to describe the functions, types
962306a36Sopenharmony_ciand design of the code. It is easier to keep documentation up-to-date
1062306a36Sopenharmony_ciwhen it is embedded in source files.
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci.. note:: The kernel-doc format is deceptively similar to javadoc,
1362306a36Sopenharmony_ci   gtk-doc or Doxygen, yet distinctively different, for historical
1462306a36Sopenharmony_ci   reasons. The kernel source contains tens of thousands of kernel-doc
1562306a36Sopenharmony_ci   comments. Please stick to the style described here.
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci.. note:: kernel-doc does not cover Rust code: please see
1862306a36Sopenharmony_ci   Documentation/rust/general-information.rst instead.
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ciThe kernel-doc structure is extracted from the comments, and proper
2162306a36Sopenharmony_ci`Sphinx C Domain`_ function and type descriptions with anchors are
2262306a36Sopenharmony_cigenerated from them. The descriptions are filtered for special kernel-doc
2362306a36Sopenharmony_cihighlights and cross-references. See below for details.
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci.. _Sphinx C Domain: http://www.sphinx-doc.org/en/stable/domains.html
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ciEvery function that is exported to loadable modules using
2862306a36Sopenharmony_ci``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` should have a kernel-doc
2962306a36Sopenharmony_cicomment. Functions and data structures in header files which are intended
3062306a36Sopenharmony_cito be used by modules should also have kernel-doc comments.
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ciIt is good practice to also provide kernel-doc formatted documentation
3362306a36Sopenharmony_cifor functions externally visible to other kernel files (not marked
3462306a36Sopenharmony_ci``static``). We also recommend providing kernel-doc formatted
3562306a36Sopenharmony_cidocumentation for private (file ``static``) routines, for consistency of
3662306a36Sopenharmony_cikernel source code layout. This is lower priority and at the discretion
3762306a36Sopenharmony_ciof the maintainer of that kernel source file.
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ciHow to format kernel-doc comments
4062306a36Sopenharmony_ci---------------------------------
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ciThe opening comment mark ``/**`` is used for kernel-doc comments. The
4362306a36Sopenharmony_ci``kernel-doc`` tool will extract comments marked this way. The rest of
4462306a36Sopenharmony_cithe comment is formatted like a normal multi-line comment with a column
4562306a36Sopenharmony_ciof asterisks on the left side, closing with ``*/`` on a line by itself.
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciThe function and type kernel-doc comments should be placed just before
4862306a36Sopenharmony_cithe function or type being described in order to maximise the chance
4962306a36Sopenharmony_cithat somebody changing the code will also change the documentation. The
5062306a36Sopenharmony_cioverview kernel-doc comments may be placed anywhere at the top indentation
5162306a36Sopenharmony_cilevel.
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ciRunning the ``kernel-doc`` tool with increased verbosity and without actual
5462306a36Sopenharmony_cioutput generation may be used to verify proper formatting of the
5562306a36Sopenharmony_cidocumentation comments. For example::
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	scripts/kernel-doc -v -none drivers/foo/bar.c
5862306a36Sopenharmony_ci
5962306a36Sopenharmony_ciThe documentation format is verified by the kernel build when it is
6062306a36Sopenharmony_cirequested to perform extra gcc checks::
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	make W=n
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ciFunction documentation
6562306a36Sopenharmony_ci----------------------
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciThe general format of a function and function-like macro kernel-doc comment is::
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci  /**
7062306a36Sopenharmony_ci   * function_name() - Brief description of function.
7162306a36Sopenharmony_ci   * @arg1: Describe the first argument.
7262306a36Sopenharmony_ci   * @arg2: Describe the second argument.
7362306a36Sopenharmony_ci   *        One can provide multiple line descriptions
7462306a36Sopenharmony_ci   *        for arguments.
7562306a36Sopenharmony_ci   *
7662306a36Sopenharmony_ci   * A longer description, with more discussion of the function function_name()
7762306a36Sopenharmony_ci   * that might be useful to those using or modifying it. Begins with an
7862306a36Sopenharmony_ci   * empty comment line, and may include additional embedded empty
7962306a36Sopenharmony_ci   * comment lines.
8062306a36Sopenharmony_ci   *
8162306a36Sopenharmony_ci   * The longer description may have multiple paragraphs.
8262306a36Sopenharmony_ci   *
8362306a36Sopenharmony_ci   * Context: Describes whether the function can sleep, what locks it takes,
8462306a36Sopenharmony_ci   *          releases, or expects to be held. It can extend over multiple
8562306a36Sopenharmony_ci   *          lines.
8662306a36Sopenharmony_ci   * Return: Describe the return value of function_name.
8762306a36Sopenharmony_ci   *
8862306a36Sopenharmony_ci   * The return value description can also have multiple paragraphs, and should
8962306a36Sopenharmony_ci   * be placed at the end of the comment block.
9062306a36Sopenharmony_ci   */
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ciThe brief description following the function name may span multiple lines, and
9362306a36Sopenharmony_ciends with an argument description, a blank comment line, or the end of the
9462306a36Sopenharmony_cicomment block.
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ciFunction parameters
9762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ciEach function argument should be described in order, immediately following
10062306a36Sopenharmony_cithe short function description.  Do not leave a blank line between the
10162306a36Sopenharmony_cifunction description and the arguments, nor between the arguments.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ciEach ``@argument:`` description may span multiple lines.
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci.. note::
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci   If the ``@argument`` description has multiple lines, the continuation
10862306a36Sopenharmony_ci   of the description should start at the same column as the previous line::
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci      * @argument: some long description
11162306a36Sopenharmony_ci      *            that continues on next lines
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci   or::
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci      * @argument:
11662306a36Sopenharmony_ci      *		some long description
11762306a36Sopenharmony_ci      *		that continues on next lines
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ciIf a function has a variable number of arguments, its description should
12062306a36Sopenharmony_cibe written in kernel-doc notation as::
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci      * @...: description
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ciFunction context
12562306a36Sopenharmony_ci~~~~~~~~~~~~~~~~
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_ciThe context in which a function can be called should be described in a
12862306a36Sopenharmony_cisection named ``Context``. This should include whether the function
12962306a36Sopenharmony_cisleeps or can be called from interrupt context, as well as what locks
13062306a36Sopenharmony_ciit takes, releases and expects to be held by its caller.
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ciExamples::
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci  * Context: Any context.
13562306a36Sopenharmony_ci  * Context: Any context. Takes and releases the RCU lock.
13662306a36Sopenharmony_ci  * Context: Any context. Expects <lock> to be held by caller.
13762306a36Sopenharmony_ci  * Context: Process context. May sleep if @gfp flags permit.
13862306a36Sopenharmony_ci  * Context: Process context. Takes and releases <mutex>.
13962306a36Sopenharmony_ci  * Context: Softirq or process context. Takes and releases <lock>, BH-safe.
14062306a36Sopenharmony_ci  * Context: Interrupt context.
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ciReturn values
14362306a36Sopenharmony_ci~~~~~~~~~~~~~
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ciThe return value, if any, should be described in a dedicated section
14662306a36Sopenharmony_cinamed ``Return``.
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci.. note::
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci  #) The multi-line descriptive text you provide does *not* recognize
15162306a36Sopenharmony_ci     line breaks, so if you try to format some text nicely, as in::
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	* Return:
15462306a36Sopenharmony_ci	* %0 - OK
15562306a36Sopenharmony_ci	* %-EINVAL - invalid argument
15662306a36Sopenharmony_ci	* %-ENOMEM - out of memory
15762306a36Sopenharmony_ci
15862306a36Sopenharmony_ci     this will all run together and produce::
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	Return: 0 - OK -EINVAL - invalid argument -ENOMEM - out of memory
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci     So, in order to produce the desired line breaks, you need to use a
16362306a36Sopenharmony_ci     ReST list, e. g.::
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci      * Return:
16662306a36Sopenharmony_ci      * * %0		- OK to runtime suspend the device
16762306a36Sopenharmony_ci      * * %-EBUSY	- Device should not be runtime suspended
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci  #) If the descriptive text you provide has lines that begin with
17062306a36Sopenharmony_ci     some phrase followed by a colon, each of those phrases will be taken
17162306a36Sopenharmony_ci     as a new section heading, which probably won't produce the desired
17262306a36Sopenharmony_ci     effect.
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ciStructure, union, and enumeration documentation
17562306a36Sopenharmony_ci-----------------------------------------------
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ciThe general format of a struct, union, and enum kernel-doc comment is::
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci  /**
18062306a36Sopenharmony_ci   * struct struct_name - Brief description.
18162306a36Sopenharmony_ci   * @member1: Description of member1.
18262306a36Sopenharmony_ci   * @member2: Description of member2.
18362306a36Sopenharmony_ci   *           One can provide multiple line descriptions
18462306a36Sopenharmony_ci   *           for members.
18562306a36Sopenharmony_ci   *
18662306a36Sopenharmony_ci   * Description of the structure.
18762306a36Sopenharmony_ci   */
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ciYou can replace the ``struct`` in the above example with ``union`` or
19062306a36Sopenharmony_ci``enum``  to describe unions or enums. ``member`` is used to mean struct
19162306a36Sopenharmony_ciand union member names as well as enumerations in an enum.
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ciThe brief description following the structure name may span multiple
19462306a36Sopenharmony_cilines, and ends with a member description, a blank comment line, or the
19562306a36Sopenharmony_ciend of the comment block.
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ciMembers
19862306a36Sopenharmony_ci~~~~~~~
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ciMembers of structs, unions and enums should be documented the same way
20162306a36Sopenharmony_cias function parameters; they immediately succeed the short description
20262306a36Sopenharmony_ciand may be multi-line.
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ciInside a struct or union description, you can use the ``private:`` and
20562306a36Sopenharmony_ci``public:`` comment tags. Structure fields that are inside a ``private:``
20662306a36Sopenharmony_ciarea are not listed in the generated output documentation.
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ciThe ``private:`` and ``public:`` tags must begin immediately following a
20962306a36Sopenharmony_ci``/*`` comment marker. They may optionally include comments between the
21062306a36Sopenharmony_ci``:`` and the ending ``*/`` marker.
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ciExample::
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci  /**
21562306a36Sopenharmony_ci   * struct my_struct - short description
21662306a36Sopenharmony_ci   * @a: first member
21762306a36Sopenharmony_ci   * @b: second member
21862306a36Sopenharmony_ci   * @d: fourth member
21962306a36Sopenharmony_ci   *
22062306a36Sopenharmony_ci   * Longer description
22162306a36Sopenharmony_ci   */
22262306a36Sopenharmony_ci  struct my_struct {
22362306a36Sopenharmony_ci      int a;
22462306a36Sopenharmony_ci      int b;
22562306a36Sopenharmony_ci  /* private: internal use only */
22662306a36Sopenharmony_ci      int c;
22762306a36Sopenharmony_ci  /* public: the next one is public */
22862306a36Sopenharmony_ci      int d;
22962306a36Sopenharmony_ci  };
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ciNested structs/unions
23262306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ciIt is possible to document nested structs and unions, like::
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci      /**
23762306a36Sopenharmony_ci       * struct nested_foobar - a struct with nested unions and structs
23862306a36Sopenharmony_ci       * @memb1: first member of anonymous union/anonymous struct
23962306a36Sopenharmony_ci       * @memb2: second member of anonymous union/anonymous struct
24062306a36Sopenharmony_ci       * @memb3: third member of anonymous union/anonymous struct
24162306a36Sopenharmony_ci       * @memb4: fourth member of anonymous union/anonymous struct
24262306a36Sopenharmony_ci       * @bar: non-anonymous union
24362306a36Sopenharmony_ci       * @bar.st1: struct st1 inside @bar
24462306a36Sopenharmony_ci       * @bar.st2: struct st2 inside @bar
24562306a36Sopenharmony_ci       * @bar.st1.memb1: first member of struct st1 on union bar
24662306a36Sopenharmony_ci       * @bar.st1.memb2: second member of struct st1 on union bar
24762306a36Sopenharmony_ci       * @bar.st2.memb1: first member of struct st2 on union bar
24862306a36Sopenharmony_ci       * @bar.st2.memb2: second member of struct st2 on union bar
24962306a36Sopenharmony_ci       */
25062306a36Sopenharmony_ci      struct nested_foobar {
25162306a36Sopenharmony_ci        /* Anonymous union/struct*/
25262306a36Sopenharmony_ci        union {
25362306a36Sopenharmony_ci          struct {
25462306a36Sopenharmony_ci            int memb1;
25562306a36Sopenharmony_ci            int memb2;
25662306a36Sopenharmony_ci          };
25762306a36Sopenharmony_ci          struct {
25862306a36Sopenharmony_ci            void *memb3;
25962306a36Sopenharmony_ci            int memb4;
26062306a36Sopenharmony_ci          };
26162306a36Sopenharmony_ci        };
26262306a36Sopenharmony_ci        union {
26362306a36Sopenharmony_ci          struct {
26462306a36Sopenharmony_ci            int memb1;
26562306a36Sopenharmony_ci            int memb2;
26662306a36Sopenharmony_ci          } st1;
26762306a36Sopenharmony_ci          struct {
26862306a36Sopenharmony_ci            void *memb1;
26962306a36Sopenharmony_ci            int memb2;
27062306a36Sopenharmony_ci          } st2;
27162306a36Sopenharmony_ci        } bar;
27262306a36Sopenharmony_ci      };
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci.. note::
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci   #) When documenting nested structs or unions, if the struct/union ``foo``
27762306a36Sopenharmony_ci      is named, the member ``bar`` inside it should be documented as
27862306a36Sopenharmony_ci      ``@foo.bar:``
27962306a36Sopenharmony_ci   #) When the nested struct/union is anonymous, the member ``bar`` in it
28062306a36Sopenharmony_ci      should be documented as ``@bar:``
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ciIn-line member documentation comments
28362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ciThe structure members may also be documented in-line within the definition.
28662306a36Sopenharmony_ciThere are two styles, single-line comments where both the opening ``/**`` and
28762306a36Sopenharmony_ciclosing ``*/`` are on the same line, and multi-line comments where they are each
28862306a36Sopenharmony_cion a line of their own, like all other kernel-doc comments::
28962306a36Sopenharmony_ci
29062306a36Sopenharmony_ci  /**
29162306a36Sopenharmony_ci   * struct foo - Brief description.
29262306a36Sopenharmony_ci   * @foo: The Foo member.
29362306a36Sopenharmony_ci   */
29462306a36Sopenharmony_ci  struct foo {
29562306a36Sopenharmony_ci        int foo;
29662306a36Sopenharmony_ci        /**
29762306a36Sopenharmony_ci         * @bar: The Bar member.
29862306a36Sopenharmony_ci         */
29962306a36Sopenharmony_ci        int bar;
30062306a36Sopenharmony_ci        /**
30162306a36Sopenharmony_ci         * @baz: The Baz member.
30262306a36Sopenharmony_ci         *
30362306a36Sopenharmony_ci         * Here, the member description may contain several paragraphs.
30462306a36Sopenharmony_ci         */
30562306a36Sopenharmony_ci        int baz;
30662306a36Sopenharmony_ci        union {
30762306a36Sopenharmony_ci                /** @foobar: Single line description. */
30862306a36Sopenharmony_ci                int foobar;
30962306a36Sopenharmony_ci        };
31062306a36Sopenharmony_ci        /** @bar2: Description for struct @bar2 inside @foo */
31162306a36Sopenharmony_ci        struct {
31262306a36Sopenharmony_ci                /**
31362306a36Sopenharmony_ci                 * @bar2.barbar: Description for @barbar inside @foo.bar2
31462306a36Sopenharmony_ci                 */
31562306a36Sopenharmony_ci                int barbar;
31662306a36Sopenharmony_ci        } bar2;
31762306a36Sopenharmony_ci  };
31862306a36Sopenharmony_ci
31962306a36Sopenharmony_ciTypedef documentation
32062306a36Sopenharmony_ci---------------------
32162306a36Sopenharmony_ci
32262306a36Sopenharmony_ciThe general format of a typedef kernel-doc comment is::
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci  /**
32562306a36Sopenharmony_ci   * typedef type_name - Brief description.
32662306a36Sopenharmony_ci   *
32762306a36Sopenharmony_ci   * Description of the type.
32862306a36Sopenharmony_ci   */
32962306a36Sopenharmony_ci
33062306a36Sopenharmony_ciTypedefs with function prototypes can also be documented::
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci  /**
33362306a36Sopenharmony_ci   * typedef type_name - Brief description.
33462306a36Sopenharmony_ci   * @arg1: description of arg1
33562306a36Sopenharmony_ci   * @arg2: description of arg2
33662306a36Sopenharmony_ci   *
33762306a36Sopenharmony_ci   * Description of the type.
33862306a36Sopenharmony_ci   *
33962306a36Sopenharmony_ci   * Context: Locking context.
34062306a36Sopenharmony_ci   * Return: Meaning of the return value.
34162306a36Sopenharmony_ci   */
34262306a36Sopenharmony_ci   typedef void (*type_name)(struct v4l2_ctrl *arg1, void *arg2);
34362306a36Sopenharmony_ci
34462306a36Sopenharmony_ciHighlights and cross-references
34562306a36Sopenharmony_ci-------------------------------
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ciThe following special patterns are recognized in the kernel-doc comment
34862306a36Sopenharmony_cidescriptive text and converted to proper reStructuredText markup and `Sphinx C
34962306a36Sopenharmony_ciDomain`_ references.
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_ci.. attention:: The below are **only** recognized within kernel-doc comments,
35262306a36Sopenharmony_ci	       **not** within normal reStructuredText documents.
35362306a36Sopenharmony_ci
35462306a36Sopenharmony_ci``funcname()``
35562306a36Sopenharmony_ci  Function reference.
35662306a36Sopenharmony_ci
35762306a36Sopenharmony_ci``@parameter``
35862306a36Sopenharmony_ci  Name of a function parameter. (No cross-referencing, just formatting.)
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci``%CONST``
36162306a36Sopenharmony_ci  Name of a constant. (No cross-referencing, just formatting.)
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci````literal````
36462306a36Sopenharmony_ci  A literal block that should be handled as-is. The output will use a
36562306a36Sopenharmony_ci  ``monospaced font``.
36662306a36Sopenharmony_ci
36762306a36Sopenharmony_ci  Useful if you need to use special characters that would otherwise have some
36862306a36Sopenharmony_ci  meaning either by kernel-doc script or by reStructuredText.
36962306a36Sopenharmony_ci
37062306a36Sopenharmony_ci  This is particularly useful if you need to use things like ``%ph`` inside
37162306a36Sopenharmony_ci  a function description.
37262306a36Sopenharmony_ci
37362306a36Sopenharmony_ci``$ENVVAR``
37462306a36Sopenharmony_ci  Name of an environment variable. (No cross-referencing, just formatting.)
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_ci``&struct name``
37762306a36Sopenharmony_ci  Structure reference.
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci``&enum name``
38062306a36Sopenharmony_ci  Enum reference.
38162306a36Sopenharmony_ci
38262306a36Sopenharmony_ci``&typedef name``
38362306a36Sopenharmony_ci  Typedef reference.
38462306a36Sopenharmony_ci
38562306a36Sopenharmony_ci``&struct_name->member`` or ``&struct_name.member``
38662306a36Sopenharmony_ci  Structure or union member reference. The cross-reference will be to the struct
38762306a36Sopenharmony_ci  or union definition, not the member directly.
38862306a36Sopenharmony_ci
38962306a36Sopenharmony_ci``&name``
39062306a36Sopenharmony_ci  A generic type reference. Prefer using the full reference described above
39162306a36Sopenharmony_ci  instead. This is mostly for legacy comments.
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ciCross-referencing from reStructuredText
39462306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
39562306a36Sopenharmony_ci
39662306a36Sopenharmony_ciNo additional syntax is needed to cross-reference the functions and types
39762306a36Sopenharmony_cidefined in the kernel-doc comments from reStructuredText documents.
39862306a36Sopenharmony_ciJust end function names with ``()`` and write ``struct``, ``union``, ``enum``
39962306a36Sopenharmony_cior ``typedef`` before types.
40062306a36Sopenharmony_ciFor example::
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci  See foo().
40362306a36Sopenharmony_ci  See struct foo.
40462306a36Sopenharmony_ci  See union bar.
40562306a36Sopenharmony_ci  See enum baz.
40662306a36Sopenharmony_ci  See typedef meh.
40762306a36Sopenharmony_ci
40862306a36Sopenharmony_ciHowever, if you want custom text in the cross-reference link, that can be done
40962306a36Sopenharmony_cithrough the following syntax::
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci  See :c:func:`my custom link text for function foo <foo>`.
41262306a36Sopenharmony_ci  See :c:type:`my custom link text for struct bar <bar>`.
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ciFor further details, please refer to the `Sphinx C Domain`_ documentation.
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ciOverview documentation comments
41762306a36Sopenharmony_ci-------------------------------
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ciTo facilitate having source code and comments close together, you can include
42062306a36Sopenharmony_cikernel-doc documentation blocks that are free-form comments instead of being
42162306a36Sopenharmony_cikernel-doc for functions, structures, unions, enums, or typedefs. This could be
42262306a36Sopenharmony_ciused for something like a theory of operation for a driver or library code, for
42362306a36Sopenharmony_ciexample.
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ciThis is done by using a ``DOC:`` section keyword with a section title.
42662306a36Sopenharmony_ci
42762306a36Sopenharmony_ciThe general format of an overview or high-level documentation comment is::
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci  /**
43062306a36Sopenharmony_ci   * DOC: Theory of Operation
43162306a36Sopenharmony_ci   *
43262306a36Sopenharmony_ci   * The whizbang foobar is a dilly of a gizmo. It can do whatever you
43362306a36Sopenharmony_ci   * want it to do, at any time. It reads your mind. Here's how it works.
43462306a36Sopenharmony_ci   *
43562306a36Sopenharmony_ci   * foo bar splat
43662306a36Sopenharmony_ci   *
43762306a36Sopenharmony_ci   * The only drawback to this gizmo is that is can sometimes damage
43862306a36Sopenharmony_ci   * hardware, software, or its subject(s).
43962306a36Sopenharmony_ci   */
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ciThe title following ``DOC:`` acts as a heading within the source file, but also
44262306a36Sopenharmony_cias an identifier for extracting the documentation comment. Thus, the title must
44362306a36Sopenharmony_cibe unique within the file.
44462306a36Sopenharmony_ci
44562306a36Sopenharmony_ci=============================
44662306a36Sopenharmony_ciIncluding kernel-doc comments
44762306a36Sopenharmony_ci=============================
44862306a36Sopenharmony_ci
44962306a36Sopenharmony_ciThe documentation comments may be included in any of the reStructuredText
45062306a36Sopenharmony_cidocuments using a dedicated kernel-doc Sphinx directive extension.
45162306a36Sopenharmony_ci
45262306a36Sopenharmony_ciThe kernel-doc directive is of the format::
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci  .. kernel-doc:: source
45562306a36Sopenharmony_ci     :option:
45662306a36Sopenharmony_ci
45762306a36Sopenharmony_ciThe *source* is the path to a source file, relative to the kernel source
45862306a36Sopenharmony_citree. The following directive options are supported:
45962306a36Sopenharmony_ci
46062306a36Sopenharmony_ciexport: *[source-pattern ...]*
46162306a36Sopenharmony_ci  Include documentation for all functions in *source* that have been exported
46262306a36Sopenharmony_ci  using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either in *source* or in any
46362306a36Sopenharmony_ci  of the files specified by *source-pattern*.
46462306a36Sopenharmony_ci
46562306a36Sopenharmony_ci  The *source-pattern* is useful when the kernel-doc comments have been placed
46662306a36Sopenharmony_ci  in header files, while ``EXPORT_SYMBOL`` and ``EXPORT_SYMBOL_GPL`` are next to
46762306a36Sopenharmony_ci  the function definitions.
46862306a36Sopenharmony_ci
46962306a36Sopenharmony_ci  Examples::
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci    .. kernel-doc:: lib/bitmap.c
47262306a36Sopenharmony_ci       :export:
47362306a36Sopenharmony_ci
47462306a36Sopenharmony_ci    .. kernel-doc:: include/net/mac80211.h
47562306a36Sopenharmony_ci       :export: net/mac80211/*.c
47662306a36Sopenharmony_ci
47762306a36Sopenharmony_ciinternal: *[source-pattern ...]*
47862306a36Sopenharmony_ci  Include documentation for all functions and types in *source* that have
47962306a36Sopenharmony_ci  **not** been exported using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either
48062306a36Sopenharmony_ci  in *source* or in any of the files specified by *source-pattern*.
48162306a36Sopenharmony_ci
48262306a36Sopenharmony_ci  Example::
48362306a36Sopenharmony_ci
48462306a36Sopenharmony_ci    .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
48562306a36Sopenharmony_ci       :internal:
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ciidentifiers: *[ function/type ...]*
48862306a36Sopenharmony_ci  Include documentation for each *function* and *type* in *source*.
48962306a36Sopenharmony_ci  If no *function* is specified, the documentation for all functions
49062306a36Sopenharmony_ci  and types in the *source* will be included.
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_ci  Examples::
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci    .. kernel-doc:: lib/bitmap.c
49562306a36Sopenharmony_ci       :identifiers: bitmap_parselist bitmap_parselist_user
49662306a36Sopenharmony_ci
49762306a36Sopenharmony_ci    .. kernel-doc:: lib/idr.c
49862306a36Sopenharmony_ci       :identifiers:
49962306a36Sopenharmony_ci
50062306a36Sopenharmony_cino-identifiers: *[ function/type ...]*
50162306a36Sopenharmony_ci  Exclude documentation for each *function* and *type* in *source*.
50262306a36Sopenharmony_ci
50362306a36Sopenharmony_ci  Example::
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci    .. kernel-doc:: lib/bitmap.c
50662306a36Sopenharmony_ci       :no-identifiers: bitmap_parselist
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_cifunctions: *[ function/type ...]*
50962306a36Sopenharmony_ci  This is an alias of the 'identifiers' directive and deprecated.
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_cidoc: *title*
51262306a36Sopenharmony_ci  Include documentation for the ``DOC:`` paragraph identified by *title* in
51362306a36Sopenharmony_ci  *source*. Spaces are allowed in *title*; do not quote the *title*. The *title*
51462306a36Sopenharmony_ci  is only used as an identifier for the paragraph, and is not included in the
51562306a36Sopenharmony_ci  output. Please make sure to have an appropriate heading in the enclosing
51662306a36Sopenharmony_ci  reStructuredText document.
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci  Example::
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci    .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
52162306a36Sopenharmony_ci       :doc: High Definition Audio over HDMI and Display Port
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ciWithout options, the kernel-doc directive includes all documentation comments
52462306a36Sopenharmony_cifrom the source file.
52562306a36Sopenharmony_ci
52662306a36Sopenharmony_ciThe kernel-doc extension is included in the kernel source tree, at
52762306a36Sopenharmony_ci``Documentation/sphinx/kerneldoc.py``. Internally, it uses the
52862306a36Sopenharmony_ci``scripts/kernel-doc`` script to extract the documentation comments from the
52962306a36Sopenharmony_cisource.
53062306a36Sopenharmony_ci
53162306a36Sopenharmony_ci.. _kernel_doc:
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ciHow to use kernel-doc to generate man pages
53462306a36Sopenharmony_ci-------------------------------------------
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_ciIf you just want to use kernel-doc to generate man pages you can do this
53762306a36Sopenharmony_cifrom the kernel git tree::
53862306a36Sopenharmony_ci
53962306a36Sopenharmony_ci  $ scripts/kernel-doc -man \
54062306a36Sopenharmony_ci    $(git grep -l '/\*\*' -- :^Documentation :^tools) \
54162306a36Sopenharmony_ci    | scripts/split-man.pl /tmp/man
54262306a36Sopenharmony_ci
54362306a36Sopenharmony_ciSome older versions of git do not support some of the variants of syntax for
54462306a36Sopenharmony_cipath exclusion.  One of the following commands may work for those versions::
54562306a36Sopenharmony_ci
54662306a36Sopenharmony_ci  $ scripts/kernel-doc -man \
54762306a36Sopenharmony_ci    $(git grep -l '/\*\*' -- . ':!Documentation' ':!tools') \
54862306a36Sopenharmony_ci    | scripts/split-man.pl /tmp/man
54962306a36Sopenharmony_ci
55062306a36Sopenharmony_ci  $ scripts/kernel-doc -man \
55162306a36Sopenharmony_ci    $(git grep -l '/\*\*' -- . ":(exclude)Documentation" ":(exclude)tools") \
55262306a36Sopenharmony_ci    | scripts/split-man.pl /tmp/man
553