xref: /OK3568_Linux_fs/kernel/Documentation/doc-guide/kernel-doc.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunWriting kernel-doc comments
2*4882a593Smuzhiyun===========================
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunThe Linux kernel source files may contain structured documentation
5*4882a593Smuzhiyuncomments in the kernel-doc format to describe the functions, types
6*4882a593Smuzhiyunand design of the code. It is easier to keep documentation up-to-date
7*4882a593Smuzhiyunwhen it is embedded in source files.
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun.. note:: The kernel-doc format is deceptively similar to javadoc,
10*4882a593Smuzhiyun   gtk-doc or Doxygen, yet distinctively different, for historical
11*4882a593Smuzhiyun   reasons. The kernel source contains tens of thousands of kernel-doc
12*4882a593Smuzhiyun   comments. Please stick to the style described here.
13*4882a593Smuzhiyun
14*4882a593SmuzhiyunThe kernel-doc structure is extracted from the comments, and proper
15*4882a593Smuzhiyun`Sphinx C Domain`_ function and type descriptions with anchors are
16*4882a593Smuzhiyungenerated from them. The descriptions are filtered for special kernel-doc
17*4882a593Smuzhiyunhighlights and cross-references. See below for details.
18*4882a593Smuzhiyun
19*4882a593Smuzhiyun.. _Sphinx C Domain: http://www.sphinx-doc.org/en/stable/domains.html
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunEvery function that is exported to loadable modules using
22*4882a593Smuzhiyun``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` should have a kernel-doc
23*4882a593Smuzhiyuncomment. Functions and data structures in header files which are intended
24*4882a593Smuzhiyunto be used by modules should also have kernel-doc comments.
25*4882a593Smuzhiyun
26*4882a593SmuzhiyunIt is good practice to also provide kernel-doc formatted documentation
27*4882a593Smuzhiyunfor functions externally visible to other kernel files (not marked
28*4882a593Smuzhiyun``static``). We also recommend providing kernel-doc formatted
29*4882a593Smuzhiyundocumentation for private (file ``static``) routines, for consistency of
30*4882a593Smuzhiyunkernel source code layout. This is lower priority and at the discretion
31*4882a593Smuzhiyunof the maintainer of that kernel source file.
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunHow to format kernel-doc comments
34*4882a593Smuzhiyun---------------------------------
35*4882a593Smuzhiyun
36*4882a593SmuzhiyunThe opening comment mark ``/**`` is used for kernel-doc comments. The
37*4882a593Smuzhiyun``kernel-doc`` tool will extract comments marked this way. The rest of
38*4882a593Smuzhiyunthe comment is formatted like a normal multi-line comment with a column
39*4882a593Smuzhiyunof asterisks on the left side, closing with ``*/`` on a line by itself.
40*4882a593Smuzhiyun
41*4882a593SmuzhiyunThe function and type kernel-doc comments should be placed just before
42*4882a593Smuzhiyunthe function or type being described in order to maximise the chance
43*4882a593Smuzhiyunthat somebody changing the code will also change the documentation. The
44*4882a593Smuzhiyunoverview kernel-doc comments may be placed anywhere at the top indentation
45*4882a593Smuzhiyunlevel.
46*4882a593Smuzhiyun
47*4882a593SmuzhiyunRunning the ``kernel-doc`` tool with increased verbosity and without actual
48*4882a593Smuzhiyunoutput generation may be used to verify proper formatting of the
49*4882a593Smuzhiyundocumentation comments. For example::
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun	scripts/kernel-doc -v -none drivers/foo/bar.c
52*4882a593Smuzhiyun
53*4882a593SmuzhiyunThe documentation format is verified by the kernel build when it is
54*4882a593Smuzhiyunrequested to perform extra gcc checks::
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun	make W=n
57*4882a593Smuzhiyun
58*4882a593SmuzhiyunFunction documentation
59*4882a593Smuzhiyun----------------------
60*4882a593Smuzhiyun
61*4882a593SmuzhiyunThe general format of a function and function-like macro kernel-doc comment is::
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun  /**
64*4882a593Smuzhiyun   * function_name() - Brief description of function.
65*4882a593Smuzhiyun   * @arg1: Describe the first argument.
66*4882a593Smuzhiyun   * @arg2: Describe the second argument.
67*4882a593Smuzhiyun   *        One can provide multiple line descriptions
68*4882a593Smuzhiyun   *        for arguments.
69*4882a593Smuzhiyun   *
70*4882a593Smuzhiyun   * A longer description, with more discussion of the function function_name()
71*4882a593Smuzhiyun   * that might be useful to those using or modifying it. Begins with an
72*4882a593Smuzhiyun   * empty comment line, and may include additional embedded empty
73*4882a593Smuzhiyun   * comment lines.
74*4882a593Smuzhiyun   *
75*4882a593Smuzhiyun   * The longer description may have multiple paragraphs.
76*4882a593Smuzhiyun   *
77*4882a593Smuzhiyun   * Context: Describes whether the function can sleep, what locks it takes,
78*4882a593Smuzhiyun   *          releases, or expects to be held. It can extend over multiple
79*4882a593Smuzhiyun   *          lines.
80*4882a593Smuzhiyun   * Return: Describe the return value of function_name.
81*4882a593Smuzhiyun   *
82*4882a593Smuzhiyun   * The return value description can also have multiple paragraphs, and should
83*4882a593Smuzhiyun   * be placed at the end of the comment block.
84*4882a593Smuzhiyun   */
85*4882a593Smuzhiyun
86*4882a593SmuzhiyunThe brief description following the function name may span multiple lines, and
87*4882a593Smuzhiyunends with an argument description, a blank comment line, or the end of the
88*4882a593Smuzhiyuncomment block.
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunFunction parameters
91*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunEach function argument should be described in order, immediately following
94*4882a593Smuzhiyunthe short function description.  Do not leave a blank line between the
95*4882a593Smuzhiyunfunction description and the arguments, nor between the arguments.
96*4882a593Smuzhiyun
97*4882a593SmuzhiyunEach ``@argument:`` description may span multiple lines.
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun.. note::
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun   If the ``@argument`` description has multiple lines, the continuation
102*4882a593Smuzhiyun   of the description should start at the same column as the previous line::
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun      * @argument: some long description
105*4882a593Smuzhiyun      *            that continues on next lines
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun   or::
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun      * @argument:
110*4882a593Smuzhiyun      *		some long description
111*4882a593Smuzhiyun      *		that continues on next lines
112*4882a593Smuzhiyun
113*4882a593SmuzhiyunIf a function has a variable number of arguments, its description should
114*4882a593Smuzhiyunbe written in kernel-doc notation as::
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun      * @...: description
117*4882a593Smuzhiyun
118*4882a593SmuzhiyunFunction context
119*4882a593Smuzhiyun~~~~~~~~~~~~~~~~
120*4882a593Smuzhiyun
121*4882a593SmuzhiyunThe context in which a function can be called should be described in a
122*4882a593Smuzhiyunsection named ``Context``. This should include whether the function
123*4882a593Smuzhiyunsleeps or can be called from interrupt context, as well as what locks
124*4882a593Smuzhiyunit takes, releases and expects to be held by its caller.
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunExamples::
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun  * Context: Any context.
129*4882a593Smuzhiyun  * Context: Any context. Takes and releases the RCU lock.
130*4882a593Smuzhiyun  * Context: Any context. Expects <lock> to be held by caller.
131*4882a593Smuzhiyun  * Context: Process context. May sleep if @gfp flags permit.
132*4882a593Smuzhiyun  * Context: Process context. Takes and releases <mutex>.
133*4882a593Smuzhiyun  * Context: Softirq or process context. Takes and releases <lock>, BH-safe.
134*4882a593Smuzhiyun  * Context: Interrupt context.
135*4882a593Smuzhiyun
136*4882a593SmuzhiyunReturn values
137*4882a593Smuzhiyun~~~~~~~~~~~~~
138*4882a593Smuzhiyun
139*4882a593SmuzhiyunThe return value, if any, should be described in a dedicated section
140*4882a593Smuzhiyunnamed ``Return``.
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun.. note::
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun  #) The multi-line descriptive text you provide does *not* recognize
145*4882a593Smuzhiyun     line breaks, so if you try to format some text nicely, as in::
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun	* Return:
148*4882a593Smuzhiyun	* 0 - OK
149*4882a593Smuzhiyun	* -EINVAL - invalid argument
150*4882a593Smuzhiyun	* -ENOMEM - out of memory
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun     this will all run together and produce::
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun	Return: 0 - OK -EINVAL - invalid argument -ENOMEM - out of memory
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun     So, in order to produce the desired line breaks, you need to use a
157*4882a593Smuzhiyun     ReST list, e. g.::
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun      * Return:
160*4882a593Smuzhiyun      * * 0		- OK to runtime suspend the device
161*4882a593Smuzhiyun      * * -EBUSY	- Device should not be runtime suspended
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun  #) If the descriptive text you provide has lines that begin with
164*4882a593Smuzhiyun     some phrase followed by a colon, each of those phrases will be taken
165*4882a593Smuzhiyun     as a new section heading, which probably won't produce the desired
166*4882a593Smuzhiyun     effect.
167*4882a593Smuzhiyun
168*4882a593SmuzhiyunStructure, union, and enumeration documentation
169*4882a593Smuzhiyun-----------------------------------------------
170*4882a593Smuzhiyun
171*4882a593SmuzhiyunThe general format of a struct, union, and enum kernel-doc comment is::
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun  /**
174*4882a593Smuzhiyun   * struct struct_name - Brief description.
175*4882a593Smuzhiyun   * @member1: Description of member1.
176*4882a593Smuzhiyun   * @member2: Description of member2.
177*4882a593Smuzhiyun   *           One can provide multiple line descriptions
178*4882a593Smuzhiyun   *           for members.
179*4882a593Smuzhiyun   *
180*4882a593Smuzhiyun   * Description of the structure.
181*4882a593Smuzhiyun   */
182*4882a593Smuzhiyun
183*4882a593SmuzhiyunYou can replace the ``struct`` in the above example with ``union`` or
184*4882a593Smuzhiyun``enum``  to describe unions or enums. ``member`` is used to mean struct
185*4882a593Smuzhiyunand union member names as well as enumerations in an enum.
186*4882a593Smuzhiyun
187*4882a593SmuzhiyunThe brief description following the structure name may span multiple
188*4882a593Smuzhiyunlines, and ends with a member description, a blank comment line, or the
189*4882a593Smuzhiyunend of the comment block.
190*4882a593Smuzhiyun
191*4882a593SmuzhiyunMembers
192*4882a593Smuzhiyun~~~~~~~
193*4882a593Smuzhiyun
194*4882a593SmuzhiyunMembers of structs, unions and enums should be documented the same way
195*4882a593Smuzhiyunas function parameters; they immediately succeed the short description
196*4882a593Smuzhiyunand may be multi-line.
197*4882a593Smuzhiyun
198*4882a593SmuzhiyunInside a struct or union description, you can use the ``private:`` and
199*4882a593Smuzhiyun``public:`` comment tags. Structure fields that are inside a ``private:``
200*4882a593Smuzhiyunarea are not listed in the generated output documentation.
201*4882a593Smuzhiyun
202*4882a593SmuzhiyunThe ``private:`` and ``public:`` tags must begin immediately following a
203*4882a593Smuzhiyun``/*`` comment marker. They may optionally include comments between the
204*4882a593Smuzhiyun``:`` and the ending ``*/`` marker.
205*4882a593Smuzhiyun
206*4882a593SmuzhiyunExample::
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun  /**
209*4882a593Smuzhiyun   * struct my_struct - short description
210*4882a593Smuzhiyun   * @a: first member
211*4882a593Smuzhiyun   * @b: second member
212*4882a593Smuzhiyun   * @d: fourth member
213*4882a593Smuzhiyun   *
214*4882a593Smuzhiyun   * Longer description
215*4882a593Smuzhiyun   */
216*4882a593Smuzhiyun  struct my_struct {
217*4882a593Smuzhiyun      int a;
218*4882a593Smuzhiyun      int b;
219*4882a593Smuzhiyun  /* private: internal use only */
220*4882a593Smuzhiyun      int c;
221*4882a593Smuzhiyun  /* public: the next one is public */
222*4882a593Smuzhiyun      int d;
223*4882a593Smuzhiyun  };
224*4882a593Smuzhiyun
225*4882a593SmuzhiyunNested structs/unions
226*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~
227*4882a593Smuzhiyun
228*4882a593SmuzhiyunIt is possible to document nested structs and unions, like::
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun      /**
231*4882a593Smuzhiyun       * struct nested_foobar - a struct with nested unions and structs
232*4882a593Smuzhiyun       * @memb1: first member of anonymous union/anonymous struct
233*4882a593Smuzhiyun       * @memb2: second member of anonymous union/anonymous struct
234*4882a593Smuzhiyun       * @memb3: third member of anonymous union/anonymous struct
235*4882a593Smuzhiyun       * @memb4: fourth member of anonymous union/anonymous struct
236*4882a593Smuzhiyun       * @bar: non-anonymous union
237*4882a593Smuzhiyun       * @bar.st1: struct st1 inside @bar
238*4882a593Smuzhiyun       * @bar.st2: struct st2 inside @bar
239*4882a593Smuzhiyun       * @bar.st1.memb1: first member of struct st1 on union bar
240*4882a593Smuzhiyun       * @bar.st1.memb2: second member of struct st1 on union bar
241*4882a593Smuzhiyun       * @bar.st2.memb1: first member of struct st2 on union bar
242*4882a593Smuzhiyun       * @bar.st2.memb2: second member of struct st2 on union bar
243*4882a593Smuzhiyun       */
244*4882a593Smuzhiyun      struct nested_foobar {
245*4882a593Smuzhiyun        /* Anonymous union/struct*/
246*4882a593Smuzhiyun        union {
247*4882a593Smuzhiyun          struct {
248*4882a593Smuzhiyun            int memb1;
249*4882a593Smuzhiyun            int memb2;
250*4882a593Smuzhiyun        }
251*4882a593Smuzhiyun          struct {
252*4882a593Smuzhiyun            void *memb3;
253*4882a593Smuzhiyun            int memb4;
254*4882a593Smuzhiyun          }
255*4882a593Smuzhiyun        }
256*4882a593Smuzhiyun        union {
257*4882a593Smuzhiyun          struct {
258*4882a593Smuzhiyun            int memb1;
259*4882a593Smuzhiyun            int memb2;
260*4882a593Smuzhiyun          } st1;
261*4882a593Smuzhiyun          struct {
262*4882a593Smuzhiyun            void *memb1;
263*4882a593Smuzhiyun            int memb2;
264*4882a593Smuzhiyun          } st2;
265*4882a593Smuzhiyun        } bar;
266*4882a593Smuzhiyun      };
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun.. note::
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun   #) When documenting nested structs or unions, if the struct/union ``foo``
271*4882a593Smuzhiyun      is named, the member ``bar`` inside it should be documented as
272*4882a593Smuzhiyun      ``@foo.bar:``
273*4882a593Smuzhiyun   #) When the nested struct/union is anonymous, the member ``bar`` in it
274*4882a593Smuzhiyun      should be documented as ``@bar:``
275*4882a593Smuzhiyun
276*4882a593SmuzhiyunIn-line member documentation comments
277*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
278*4882a593Smuzhiyun
279*4882a593SmuzhiyunThe structure members may also be documented in-line within the definition.
280*4882a593SmuzhiyunThere are two styles, single-line comments where both the opening ``/**`` and
281*4882a593Smuzhiyunclosing ``*/`` are on the same line, and multi-line comments where they are each
282*4882a593Smuzhiyunon a line of their own, like all other kernel-doc comments::
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun  /**
285*4882a593Smuzhiyun   * struct foo - Brief description.
286*4882a593Smuzhiyun   * @foo: The Foo member.
287*4882a593Smuzhiyun   */
288*4882a593Smuzhiyun  struct foo {
289*4882a593Smuzhiyun        int foo;
290*4882a593Smuzhiyun        /**
291*4882a593Smuzhiyun         * @bar: The Bar member.
292*4882a593Smuzhiyun         */
293*4882a593Smuzhiyun        int bar;
294*4882a593Smuzhiyun        /**
295*4882a593Smuzhiyun         * @baz: The Baz member.
296*4882a593Smuzhiyun         *
297*4882a593Smuzhiyun         * Here, the member description may contain several paragraphs.
298*4882a593Smuzhiyun         */
299*4882a593Smuzhiyun        int baz;
300*4882a593Smuzhiyun        union {
301*4882a593Smuzhiyun                /** @foobar: Single line description. */
302*4882a593Smuzhiyun                int foobar;
303*4882a593Smuzhiyun        };
304*4882a593Smuzhiyun        /** @bar2: Description for struct @bar2 inside @foo */
305*4882a593Smuzhiyun        struct {
306*4882a593Smuzhiyun                /**
307*4882a593Smuzhiyun                 * @bar2.barbar: Description for @barbar inside @foo.bar2
308*4882a593Smuzhiyun                 */
309*4882a593Smuzhiyun                int barbar;
310*4882a593Smuzhiyun        } bar2;
311*4882a593Smuzhiyun  };
312*4882a593Smuzhiyun
313*4882a593SmuzhiyunTypedef documentation
314*4882a593Smuzhiyun---------------------
315*4882a593Smuzhiyun
316*4882a593SmuzhiyunThe general format of a typedef kernel-doc comment is::
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun  /**
319*4882a593Smuzhiyun   * typedef type_name - Brief description.
320*4882a593Smuzhiyun   *
321*4882a593Smuzhiyun   * Description of the type.
322*4882a593Smuzhiyun   */
323*4882a593Smuzhiyun
324*4882a593SmuzhiyunTypedefs with function prototypes can also be documented::
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun  /**
327*4882a593Smuzhiyun   * typedef type_name - Brief description.
328*4882a593Smuzhiyun   * @arg1: description of arg1
329*4882a593Smuzhiyun   * @arg2: description of arg2
330*4882a593Smuzhiyun   *
331*4882a593Smuzhiyun   * Description of the type.
332*4882a593Smuzhiyun   *
333*4882a593Smuzhiyun   * Context: Locking context.
334*4882a593Smuzhiyun   * Return: Meaning of the return value.
335*4882a593Smuzhiyun   */
336*4882a593Smuzhiyun   typedef void (*type_name)(struct v4l2_ctrl *arg1, void *arg2);
337*4882a593Smuzhiyun
338*4882a593SmuzhiyunHighlights and cross-references
339*4882a593Smuzhiyun-------------------------------
340*4882a593Smuzhiyun
341*4882a593SmuzhiyunThe following special patterns are recognized in the kernel-doc comment
342*4882a593Smuzhiyundescriptive text and converted to proper reStructuredText markup and `Sphinx C
343*4882a593SmuzhiyunDomain`_ references.
344*4882a593Smuzhiyun
345*4882a593Smuzhiyun.. attention:: The below are **only** recognized within kernel-doc comments,
346*4882a593Smuzhiyun	       **not** within normal reStructuredText documents.
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun``funcname()``
349*4882a593Smuzhiyun  Function reference.
350*4882a593Smuzhiyun
351*4882a593Smuzhiyun``@parameter``
352*4882a593Smuzhiyun  Name of a function parameter. (No cross-referencing, just formatting.)
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun``%CONST``
355*4882a593Smuzhiyun  Name of a constant. (No cross-referencing, just formatting.)
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun````literal````
358*4882a593Smuzhiyun  A literal block that should be handled as-is. The output will use a
359*4882a593Smuzhiyun  ``monospaced font``.
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun  Useful if you need to use special characters that would otherwise have some
362*4882a593Smuzhiyun  meaning either by kernel-doc script or by reStructuredText.
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun  This is particularly useful if you need to use things like ``%ph`` inside
365*4882a593Smuzhiyun  a function description.
366*4882a593Smuzhiyun
367*4882a593Smuzhiyun``$ENVVAR``
368*4882a593Smuzhiyun  Name of an environment variable. (No cross-referencing, just formatting.)
369*4882a593Smuzhiyun
370*4882a593Smuzhiyun``&struct name``
371*4882a593Smuzhiyun  Structure reference.
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun``&enum name``
374*4882a593Smuzhiyun  Enum reference.
375*4882a593Smuzhiyun
376*4882a593Smuzhiyun``&typedef name``
377*4882a593Smuzhiyun  Typedef reference.
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun``&struct_name->member`` or ``&struct_name.member``
380*4882a593Smuzhiyun  Structure or union member reference. The cross-reference will be to the struct
381*4882a593Smuzhiyun  or union definition, not the member directly.
382*4882a593Smuzhiyun
383*4882a593Smuzhiyun``&name``
384*4882a593Smuzhiyun  A generic type reference. Prefer using the full reference described above
385*4882a593Smuzhiyun  instead. This is mostly for legacy comments.
386*4882a593Smuzhiyun
387*4882a593SmuzhiyunCross-referencing from reStructuredText
388*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
389*4882a593Smuzhiyun
390*4882a593SmuzhiyunNo additional syntax is needed to cross-reference the functions and types
391*4882a593Smuzhiyundefined in the kernel-doc comments from reStructuredText documents.
392*4882a593SmuzhiyunJust end function names with ``()`` and write ``struct``, ``union``, ``enum``
393*4882a593Smuzhiyunor ``typedef`` before types.
394*4882a593SmuzhiyunFor example::
395*4882a593Smuzhiyun
396*4882a593Smuzhiyun  See foo().
397*4882a593Smuzhiyun  See struct foo.
398*4882a593Smuzhiyun  See union bar.
399*4882a593Smuzhiyun  See enum baz.
400*4882a593Smuzhiyun  See typedef meh.
401*4882a593Smuzhiyun
402*4882a593SmuzhiyunHowever, if you want custom text in the cross-reference link, that can be done
403*4882a593Smuzhiyunthrough the following syntax::
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun  See :c:func:`my custom link text for function foo <foo>`.
406*4882a593Smuzhiyun  See :c:type:`my custom link text for struct bar <bar>`.
407*4882a593Smuzhiyun
408*4882a593SmuzhiyunFor further details, please refer to the `Sphinx C Domain`_ documentation.
409*4882a593Smuzhiyun
410*4882a593SmuzhiyunOverview documentation comments
411*4882a593Smuzhiyun-------------------------------
412*4882a593Smuzhiyun
413*4882a593SmuzhiyunTo facilitate having source code and comments close together, you can include
414*4882a593Smuzhiyunkernel-doc documentation blocks that are free-form comments instead of being
415*4882a593Smuzhiyunkernel-doc for functions, structures, unions, enums, or typedefs. This could be
416*4882a593Smuzhiyunused for something like a theory of operation for a driver or library code, for
417*4882a593Smuzhiyunexample.
418*4882a593Smuzhiyun
419*4882a593SmuzhiyunThis is done by using a ``DOC:`` section keyword with a section title.
420*4882a593Smuzhiyun
421*4882a593SmuzhiyunThe general format of an overview or high-level documentation comment is::
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun  /**
424*4882a593Smuzhiyun   * DOC: Theory of Operation
425*4882a593Smuzhiyun   *
426*4882a593Smuzhiyun   * The whizbang foobar is a dilly of a gizmo. It can do whatever you
427*4882a593Smuzhiyun   * want it to do, at any time. It reads your mind. Here's how it works.
428*4882a593Smuzhiyun   *
429*4882a593Smuzhiyun   * foo bar splat
430*4882a593Smuzhiyun   *
431*4882a593Smuzhiyun   * The only drawback to this gizmo is that is can sometimes damage
432*4882a593Smuzhiyun   * hardware, software, or its subject(s).
433*4882a593Smuzhiyun   */
434*4882a593Smuzhiyun
435*4882a593SmuzhiyunThe title following ``DOC:`` acts as a heading within the source file, but also
436*4882a593Smuzhiyunas an identifier for extracting the documentation comment. Thus, the title must
437*4882a593Smuzhiyunbe unique within the file.
438*4882a593Smuzhiyun
439*4882a593SmuzhiyunIncluding kernel-doc comments
440*4882a593Smuzhiyun=============================
441*4882a593Smuzhiyun
442*4882a593SmuzhiyunThe documentation comments may be included in any of the reStructuredText
443*4882a593Smuzhiyundocuments using a dedicated kernel-doc Sphinx directive extension.
444*4882a593Smuzhiyun
445*4882a593SmuzhiyunThe kernel-doc directive is of the format::
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun  .. kernel-doc:: source
448*4882a593Smuzhiyun     :option:
449*4882a593Smuzhiyun
450*4882a593SmuzhiyunThe *source* is the path to a source file, relative to the kernel source
451*4882a593Smuzhiyuntree. The following directive options are supported:
452*4882a593Smuzhiyun
453*4882a593Smuzhiyunexport: *[source-pattern ...]*
454*4882a593Smuzhiyun  Include documentation for all functions in *source* that have been exported
455*4882a593Smuzhiyun  using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either in *source* or in any
456*4882a593Smuzhiyun  of the files specified by *source-pattern*.
457*4882a593Smuzhiyun
458*4882a593Smuzhiyun  The *source-pattern* is useful when the kernel-doc comments have been placed
459*4882a593Smuzhiyun  in header files, while ``EXPORT_SYMBOL`` and ``EXPORT_SYMBOL_GPL`` are next to
460*4882a593Smuzhiyun  the function definitions.
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun  Examples::
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun    .. kernel-doc:: lib/bitmap.c
465*4882a593Smuzhiyun       :export:
466*4882a593Smuzhiyun
467*4882a593Smuzhiyun    .. kernel-doc:: include/net/mac80211.h
468*4882a593Smuzhiyun       :export: net/mac80211/*.c
469*4882a593Smuzhiyun
470*4882a593Smuzhiyuninternal: *[source-pattern ...]*
471*4882a593Smuzhiyun  Include documentation for all functions and types in *source* that have
472*4882a593Smuzhiyun  **not** been exported using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either
473*4882a593Smuzhiyun  in *source* or in any of the files specified by *source-pattern*.
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun  Example::
476*4882a593Smuzhiyun
477*4882a593Smuzhiyun    .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
478*4882a593Smuzhiyun       :internal:
479*4882a593Smuzhiyun
480*4882a593Smuzhiyunidentifiers: *[ function/type ...]*
481*4882a593Smuzhiyun  Include documentation for each *function* and *type* in *source*.
482*4882a593Smuzhiyun  If no *function* is specified, the documentation for all functions
483*4882a593Smuzhiyun  and types in the *source* will be included.
484*4882a593Smuzhiyun
485*4882a593Smuzhiyun  Examples::
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun    .. kernel-doc:: lib/bitmap.c
488*4882a593Smuzhiyun       :identifiers: bitmap_parselist bitmap_parselist_user
489*4882a593Smuzhiyun
490*4882a593Smuzhiyun    .. kernel-doc:: lib/idr.c
491*4882a593Smuzhiyun       :identifiers:
492*4882a593Smuzhiyun
493*4882a593Smuzhiyunno-identifiers: *[ function/type ...]*
494*4882a593Smuzhiyun  Exclude documentation for each *function* and *type* in *source*.
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun  Example::
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun    .. kernel-doc:: lib/bitmap.c
499*4882a593Smuzhiyun       :no-identifiers: bitmap_parselist
500*4882a593Smuzhiyun
501*4882a593Smuzhiyunfunctions: *[ function/type ...]*
502*4882a593Smuzhiyun  This is an alias of the 'identifiers' directive and deprecated.
503*4882a593Smuzhiyun
504*4882a593Smuzhiyundoc: *title*
505*4882a593Smuzhiyun  Include documentation for the ``DOC:`` paragraph identified by *title* in
506*4882a593Smuzhiyun  *source*. Spaces are allowed in *title*; do not quote the *title*. The *title*
507*4882a593Smuzhiyun  is only used as an identifier for the paragraph, and is not included in the
508*4882a593Smuzhiyun  output. Please make sure to have an appropriate heading in the enclosing
509*4882a593Smuzhiyun  reStructuredText document.
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun  Example::
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun    .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
514*4882a593Smuzhiyun       :doc: High Definition Audio over HDMI and Display Port
515*4882a593Smuzhiyun
516*4882a593SmuzhiyunWithout options, the kernel-doc directive includes all documentation comments
517*4882a593Smuzhiyunfrom the source file.
518*4882a593Smuzhiyun
519*4882a593SmuzhiyunThe kernel-doc extension is included in the kernel source tree, at
520*4882a593Smuzhiyun``Documentation/sphinx/kerneldoc.py``. Internally, it uses the
521*4882a593Smuzhiyun``scripts/kernel-doc`` script to extract the documentation comments from the
522*4882a593Smuzhiyunsource.
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun.. _kernel_doc:
525*4882a593Smuzhiyun
526*4882a593SmuzhiyunHow to use kernel-doc to generate man pages
527*4882a593Smuzhiyun-------------------------------------------
528*4882a593Smuzhiyun
529*4882a593SmuzhiyunIf you just want to use kernel-doc to generate man pages you can do this
530*4882a593Smuzhiyunfrom the kernel git tree::
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun  $ scripts/kernel-doc -man \
533*4882a593Smuzhiyun    $(git grep -l '/\*\*' -- :^Documentation :^tools) \
534*4882a593Smuzhiyun    | scripts/split-man.pl /tmp/man
535*4882a593Smuzhiyun
536*4882a593SmuzhiyunSome older versions of git do not support some of the variants of syntax for
537*4882a593Smuzhiyunpath exclusion.  One of the following commands may work for those versions::
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun  $ scripts/kernel-doc -man \
540*4882a593Smuzhiyun    $(git grep -l '/\*\*' -- . ':!Documentation' ':!tools') \
541*4882a593Smuzhiyun    | scripts/split-man.pl /tmp/man
542*4882a593Smuzhiyun
543*4882a593Smuzhiyun  $ scripts/kernel-doc -man \
544*4882a593Smuzhiyun    $(git grep -l '/\*\*' -- . ":(exclude)Documentation" ":(exclude)tools") \
545*4882a593Smuzhiyun    | scripts/split-man.pl /tmp/man
546