blob: 315b0ecc7a11ca5b844c864cf6dcffc9a319e52b [file] [log] [blame]
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -02001Including kernel-doc comments
2=============================
3
4The Linux kernel source files may contain structured documentation comments, or
5kernel-doc comments to describe the functions and types and design of the
6code. The documentation comments may be included to any of the reStructuredText
7documents using a dedicated kernel-doc Sphinx directive extension.
8
9The kernel-doc directive is of the format::
10
11 .. kernel-doc:: source
12 :option:
13
14The *source* is the path to a source file, relative to the kernel source
15tree. The following directive options are supported:
16
17export: *[source-pattern ...]*
18 Include documentation for all functions in *source* that have been exported
19 using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either in *source* or in any
20 of the files specified by *source-pattern*.
21
22 The *source-pattern* is useful when the kernel-doc comments have been placed
23 in header files, while ``EXPORT_SYMBOL`` and ``EXPORT_SYMBOL_GPL`` are next to
24 the function definitions.
25
26 Examples::
27
28 .. kernel-doc:: lib/bitmap.c
29 :export:
30
31 .. kernel-doc:: include/net/mac80211.h
32 :export: net/mac80211/*.c
33
34internal: *[source-pattern ...]*
35 Include documentation for all functions and types in *source* that have
36 **not** been exported using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either
37 in *source* or in any of the files specified by *source-pattern*.
38
39 Example::
40
41 .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
42 :internal:
43
44doc: *title*
45 Include documentation for the ``DOC:`` paragraph identified by *title* in
46 *source*. Spaces are allowed in *title*; do not quote the *title*. The *title*
47 is only used as an identifier for the paragraph, and is not included in the
48 output. Please make sure to have an appropriate heading in the enclosing
49 reStructuredText document.
50
51 Example::
52
53 .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
54 :doc: High Definition Audio over HDMI and Display Port
55
56functions: *function* *[...]*
57 Include documentation for each *function* in *source*.
58
59 Example::
60
61 .. kernel-doc:: lib/bitmap.c
62 :functions: bitmap_parselist bitmap_parselist_user
63
64Without options, the kernel-doc directive includes all documentation comments
65from the source file.
66
67The kernel-doc extension is included in the kernel source tree, at
Tom Saegerf66d9062017-10-12 15:23:42 -050068``Documentation/sphinx/kerneldoc.py``. Internally, it uses the
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -020069``scripts/kernel-doc`` script to extract the documentation comments from the
70source.
71
72.. _kernel_doc:
73
74Writing kernel-doc comments
75===========================
76
77In order to provide embedded, "C" friendly, easy to maintain, but consistent and
78extractable overview, function and type documentation, the Linux kernel has
79adopted a consistent style for documentation comments. The format for this
80documentation is called the kernel-doc format, described below. This style
81embeds the documentation within the source files, using a few simple conventions
82for adding documentation paragraphs and documenting functions and their
83parameters, structures and unions and their members, enumerations, and typedefs.
84
85.. note:: The kernel-doc format is deceptively similar to gtk-doc or Doxygen,
86 yet distinctively different, for historical reasons. The kernel source
87 contains tens of thousands of kernel-doc comments. Please stick to the style
88 described here.
89
90The ``scripts/kernel-doc`` script is used by the Sphinx kernel-doc extension in
91the documentation build to extract this embedded documentation into the various
92HTML, PDF, and other format documents.
93
94In order to provide good documentation of kernel functions and data structures,
95please use the following conventions to format your kernel-doc comments in the
96Linux kernel source.
97
98How to format kernel-doc comments
99---------------------------------
100
101The opening comment mark ``/**`` is reserved for kernel-doc comments. Only
102comments so marked will be considered by the ``kernel-doc`` tool. Use it only
103for comment blocks that contain kernel-doc formatted comments. The usual ``*/``
104should be used as the closing comment marker. The lines in between should be
105prefixed by `` * `` (space star space).
106
107The function and type kernel-doc comments should be placed just before the
108function or type being described. The overview kernel-doc comments may be freely
109placed at the top indentation level.
110
111Example kernel-doc function comment::
112
113 /**
114 * foobar() - Brief description of foobar.
Mauro Carvalho Chehab63ac8512017-12-18 10:30:02 -0200115 * @argument1: Description of parameter argument1 of foobar.
116 * @argument2: Description of parameter argument2 of foobar.
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200117 *
118 * Longer description of foobar.
119 *
Matthew Wilcoxda70b8c2018-02-13 13:15:33 -0800120 * Context: Interrupt / locking context of foobar.
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200121 * Return: Description of return value of foobar.
122 */
Mauro Carvalho Chehab63ac8512017-12-18 10:30:02 -0200123 int foobar(int argument1, char *argument2)
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200124
125The format is similar for documentation for structures, enums, paragraphs,
Mauro Carvalho Chehab63ac8512017-12-18 10:30:02 -0200126etc. See the sections below for specific details of each type.
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200127
128The kernel-doc structure is extracted from the comments, and proper `Sphinx C
129Domain`_ function and type descriptions with anchors are generated for them. The
130descriptions are filtered for special kernel-doc highlights and
131cross-references. See below for details.
132
133.. _Sphinx C Domain: http://www.sphinx-doc.org/en/stable/domains.html
134
Mauro Carvalho Chehab63ac8512017-12-18 10:30:02 -0200135
136Parameters and member arguments
137-------------------------------
138
139The kernel-doc function comments describe each parameter to the function and
140function typedefs or each member of struct/union, in order, with the
141``@argument:`` descriptions. For each non-private member argument, one
142``@argument`` definition is needed.
143
144The ``@argument:`` descriptions begin on the very next line following
145the opening brief function description line, with no intervening blank
146comment lines.
147
148The ``@argument:`` descriptions may span multiple lines.
149
150.. note::
151
152 If the ``@argument`` description has multiple lines, the continuation
153 of the description should be starting exactly at the same column as
154 the previous line, e. g.::
155
156 * @argument: some long description
157 * that continues on next lines
158
159 or::
160
161 * @argument:
162 * some long description
163 * that continues on next lines
164
165If a function or typedef parameter argument is ``...`` (e. g. a variable
166number of arguments), its description should be listed in kernel-doc
167notation as::
168
169 * @...: description
170
Mauro Carvalho Chehab01f2c182017-12-18 10:30:03 -0200171Private members
172~~~~~~~~~~~~~~~
173
174Inside a struct or union description, you can use the ``private:`` and
175``public:`` comment tags. Structure fields that are inside a ``private:``
176area are not listed in the generated output documentation.
177
178The ``private:`` and ``public:`` tags must begin immediately following a
179``/*`` comment marker. They may optionally include comments between the
180``:`` and the ending ``*/`` marker.
181
182Example::
183
184 /**
185 * struct my_struct - short description
186 * @a: first member
187 * @b: second member
188 * @d: fourth member
189 *
190 * Longer description
191 */
192 struct my_struct {
193 int a;
194 int b;
195 /* private: internal use only */
196 int c;
197 /* public: the next one is public */
198 int d;
199 };
200
Mauro Carvalho Chehabfc275bf2017-12-18 10:30:04 -0200201Function documentation
202----------------------
203
204The general format of a function and function-like macro kernel-doc comment is::
205
206 /**
207 * function_name() - Brief description of function.
208 * @arg1: Describe the first argument.
209 * @arg2: Describe the second argument.
210 * One can provide multiple line descriptions
211 * for arguments.
212 *
213 * A longer description, with more discussion of the function function_name()
214 * that might be useful to those using or modifying it. Begins with an
215 * empty comment line, and may include additional embedded empty
216 * comment lines.
217 *
218 * The longer description may have multiple paragraphs.
219 *
Matthew Wilcoxda70b8c2018-02-13 13:15:33 -0800220 * Context: Describes whether the function can sleep, what locks it takes,
221 * releases, or expects to be held. It can extend over multiple
222 * lines.
Mauro Carvalho Chehabfc275bf2017-12-18 10:30:04 -0200223 * Return: Describe the return value of foobar.
224 *
225 * The return value description can also have multiple paragraphs, and should
226 * be placed at the end of the comment block.
227 */
228
229The brief description following the function name may span multiple lines, and
230ends with an argument description, a blank comment line, or the end of the
231comment block.
232
Matthew Wilcoxda70b8c2018-02-13 13:15:33 -0800233Function context
234~~~~~~~~~~~~~~~~
235
236The context in which a function can be called should be described in a
237section named ``Context``. This should include whether the function
238sleeps or can be called from interrupt context, as well as what locks
239it takes, releases and expects to be held by its caller.
240
241Examples::
242
243 * Context: Any context.
244 * Context: Any context. Takes and releases the RCU lock.
245 * Context: Any context. Expects <lock> to be held by caller.
246 * Context: Process context. May sleep if @gfp flags permit.
247 * Context: Process context. Takes and releases <mutex>.
248 * Context: Softirq or process context. Takes and releases <lock>, BH-safe.
249 * Context: Interrupt context.
250
Mauro Carvalho Chehabfc275bf2017-12-18 10:30:04 -0200251Return values
252~~~~~~~~~~~~~
253
254The return value, if any, should be described in a dedicated section
255named ``Return``.
256
257.. note::
258
259 #) The multi-line descriptive text you provide does *not* recognize
260 line breaks, so if you try to format some text nicely, as in::
261
262 * Return:
263 * 0 - OK
264 * -EINVAL - invalid argument
265 * -ENOMEM - out of memory
266
267 this will all run together and produce::
268
269 Return: 0 - OK -EINVAL - invalid argument -ENOMEM - out of memory
270
271 So, in order to produce the desired line breaks, you need to use a
272 ReST list, e. g.::
273
274 * Return:
275 * * 0 - OK to runtime suspend the device
276 * * -EBUSY - Device should not be runtime suspended
277
278 #) If the descriptive text you provide has lines that begin with
279 some phrase followed by a colon, each of those phrases will be taken
280 as a new section heading, with probably won't produce the desired
281 effect.
282
Mauro Carvalho Chehab553aa3c2017-12-18 10:30:05 -0200283Structure, union, and enumeration documentation
284-----------------------------------------------
285
286The general format of a struct, union, and enum kernel-doc comment is::
287
288 /**
289 * struct struct_name - Brief description.
290 * @argument: Description of member member_name.
291 *
292 * Description of the structure.
293 */
294
295On the above, ``struct`` is used to mean structs. You can also use ``union``
296and ``enum`` to describe unions and enums. ``argument`` is used
297to mean struct and union member names as well as enumerations in an enum.
298
299The brief description following the structure name may span multiple lines, and
300ends with a member description, a blank comment line, or the end of the
301comment block.
302
303The kernel-doc data structure comments describe each member of the structure,
304in order, with the member descriptions.
305
Mauro Carvalho Chehab8ad72162017-12-18 10:30:13 -0200306Nested structs/unions
307~~~~~~~~~~~~~~~~~~~~~
308
309It is possible to document nested structs unions, like::
310
311 /**
312 * struct nested_foobar - a struct with nested unions and structs
313 * @arg1: - first argument of anonymous union/anonymous struct
314 * @arg2: - second argument of anonymous union/anonymous struct
315 * @arg3: - third argument of anonymous union/anonymous struct
316 * @arg4: - fourth argument of anonymous union/anonymous struct
317 * @bar.st1.arg1 - first argument of struct st1 on union bar
318 * @bar.st1.arg2 - second argument of struct st1 on union bar
319 * @bar.st2.arg1 - first argument of struct st2 on union bar
320 * @bar.st2.arg2 - second argument of struct st2 on union bar
321 struct nested_foobar {
322 /* Anonymous union/struct*/
323 union {
324 struct {
325 int arg1;
326 int arg2;
327 }
328 struct {
329 void *arg3;
330 int arg4;
331 }
332 }
333 union {
334 struct {
335 int arg1;
336 int arg2;
337 } st1;
338 struct {
339 void *arg1;
340 int arg2;
341 } st2;
342 } bar;
343 };
344
345.. note::
346
347 #) When documenting nested structs or unions, if the struct/union ``foo``
348 is named, the argument ``bar`` inside it should be documented as
349 ``@foo.bar:``
350 #) When the nested struct/union is anonymous, the argument ``bar`` on it
351 should be documented as ``@bar:``
Mauro Carvalho Chehab553aa3c2017-12-18 10:30:05 -0200352
Mauro Carvalho Chehabbdb76f92017-12-18 10:30:06 -0200353Typedef documentation
354---------------------
355
356The general format of a typedef kernel-doc comment is::
357
358 /**
359 * typedef type_name - Brief description.
360 *
361 * Description of the type.
362 */
363
364Typedefs with function prototypes can also be documented::
365
366 /**
367 * typedef type_name - Brief description.
368 * @arg1: description of arg1
369 * @arg2: description of arg2
370 *
371 * Description of the type.
Matthew Wilcoxda70b8c2018-02-13 13:15:33 -0800372 *
373 * Context: Locking context.
374 * Return: Meaning of the return value.
Mauro Carvalho Chehabbdb76f92017-12-18 10:30:06 -0200375 */
376 typedef void (*type_name)(struct v4l2_ctrl *arg1, void *arg2);
377
Mauro Carvalho Chehab63ac8512017-12-18 10:30:02 -0200378
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200379Highlights and cross-references
380-------------------------------
381
382The following special patterns are recognized in the kernel-doc comment
383descriptive text and converted to proper reStructuredText markup and `Sphinx C
384Domain`_ references.
385
386.. attention:: The below are **only** recognized within kernel-doc comments,
387 **not** within normal reStructuredText documents.
388
389``funcname()``
390 Function reference.
391
392``@parameter``
393 Name of a function parameter. (No cross-referencing, just formatting.)
394
395``%CONST``
396 Name of a constant. (No cross-referencing, just formatting.)
397
Mauro Carvalho Chehab5d47c312017-05-16 08:17:49 -0300398````literal````
399 A literal block that should be handled as-is. The output will use a
400 ``monospaced font``.
401
402 Useful if you need to use special characters that would otherwise have some
403 meaning either by kernel-doc script of by reStructuredText.
404
405 This is particularly useful if you need to use things like ``%ph`` inside
406 a function description.
407
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200408``$ENVVAR``
409 Name of an environment variable. (No cross-referencing, just formatting.)
410
411``&struct name``
412 Structure reference.
413
414``&enum name``
415 Enum reference.
416
417``&typedef name``
418 Typedef reference.
419
420``&struct_name->member`` or ``&struct_name.member``
421 Structure or union member reference. The cross-reference will be to the struct
422 or union definition, not the member directly.
423
424``&name``
425 A generic type reference. Prefer using the full reference described above
426 instead. This is mostly for legacy comments.
427
428Cross-referencing from reStructuredText
429~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
430
431To cross-reference the functions and types defined in the kernel-doc comments
432from reStructuredText documents, please use the `Sphinx C Domain`_
433references. For example::
434
435 See function :c:func:`foo` and struct/union/enum/typedef :c:type:`bar`.
436
437While the type reference works with just the type name, without the
438struct/union/enum/typedef part in front, you may want to use::
439
440 See :c:type:`struct foo <foo>`.
441 See :c:type:`union bar <bar>`.
442 See :c:type:`enum baz <baz>`.
443 See :c:type:`typedef meh <meh>`.
444
445This will produce prettier links, and is in line with how kernel-doc does the
446cross-references.
447
448For further details, please refer to the `Sphinx C Domain`_ documentation.
449
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200450
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200451
452In-line member documentation comments
453~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
454
Jonathan Corbetca9667f2016-11-19 10:28:58 -0700455The structure members may also be documented in-line within the definition.
456There are two styles, single-line comments where both the opening ``/**`` and
457closing ``*/`` are on the same line, and multi-line comments where they are each
458on a line of their own, like all other kernel-doc comments::
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200459
460 /**
461 * struct foo - Brief description.
462 * @foo: The Foo member.
463 */
464 struct foo {
465 int foo;
466 /**
467 * @bar: The Bar member.
468 */
469 int bar;
470 /**
471 * @baz: The Baz member.
472 *
473 * Here, the member description may contain several paragraphs.
474 */
475 int baz;
Jonathan Corbetca9667f2016-11-19 10:28:58 -0700476 /** @foobar: Single line description. */
477 int foobar;
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200478 }
479
Mauro Carvalho Chehab1dc4bbf2016-11-17 08:32:33 -0200480
481Overview documentation comments
482-------------------------------
483
484To facilitate having source code and comments close together, you can include
485kernel-doc documentation blocks that are free-form comments instead of being
486kernel-doc for functions, structures, unions, enums, or typedefs. This could be
487used for something like a theory of operation for a driver or library code, for
488example.
489
490This is done by using a ``DOC:`` section keyword with a section title.
491
492The general format of an overview or high-level documentation comment is::
493
494 /**
495 * DOC: Theory of Operation
496 *
497 * The whizbang foobar is a dilly of a gizmo. It can do whatever you
498 * want it to do, at any time. It reads your mind. Here's how it works.
499 *
500 * foo bar splat
501 *
502 * The only drawback to this gizmo is that is can sometimes damage
503 * hardware, software, or its subject(s).
504 */
505
506The title following ``DOC:`` acts as a heading within the source file, but also
507as an identifier for extracting the documentation comment. Thus, the title must
508be unique within the file.
509
510Recommendations
511---------------
512
513We definitely need kernel-doc formatted documentation for functions that are
514exported to loadable modules using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL``.
515
516We also look to provide kernel-doc formatted documentation for functions
517externally visible to other kernel files (not marked "static").
518
519We also recommend providing kernel-doc formatted documentation for private (file
520"static") routines, for consistency of kernel source code layout. But this is
521lower priority and at the discretion of the MAINTAINER of that kernel source
522file.
523
524Data structures visible in kernel include files should also be documented using
525kernel-doc formatted comments.
Mauro Carvalho Chehab93626d72017-12-18 10:30:07 -0200526
527How to use kernel-doc to generate man pages
528-------------------------------------------
529
530If you just want to use kernel-doc to generate man pages you can do this
531from the Kernel git tree::
532
533 $ scripts/kernel-doc -man $(git grep -l '/\*\*' |grep -v Documentation/) | ./split-man.pl /tmp/man
534
535Using the small ``split-man.pl`` script below::
536
537
538 #!/usr/bin/perl
539
540 if ($#ARGV < 0) {
541 die "where do I put the results?\n";
542 }
543
544 mkdir $ARGV[0],0777;
545 $state = 0;
546 while (<STDIN>) {
547 if (/^\.TH \"[^\"]*\" 9 \"([^\"]*)\"/) {
548 if ($state == 1) { close OUT }
549 $state = 1;
550 $fn = "$ARGV[0]/$1.9";
551 print STDERR "Creating $fn\n";
552 open OUT, ">$fn" or die "can't open $fn: $!\n";
553 print OUT $_;
554 } elsif ($state != 0) {
555 print OUT $_;
556 }
557 }
558
559 close OUT;