blob: ad3506c13b11ed43dda1558c28ad4834c7e10bd0 [file] [log] [blame]
Jani Nikula17defc22016-06-23 15:36:04 +03001==========================
2Linux Kernel Documentation
3==========================
4
5Introduction
6============
7
8The Linux kernel uses `Sphinx`_ to generate pretty documentation from
9`reStructuredText`_ files under ``Documentation``. To build the documentation in
10HTML or PDF formats, use ``make htmldocs`` or ``make pdfdocs``. The generated
11documentation is placed in ``Documentation/output``.
12
13.. _Sphinx: http://www.sphinx-doc.org/
14.. _reStructuredText: http://docutils.sourceforge.net/rst.html
15
16The reStructuredText files may contain directives to include structured
17documentation comments, or kernel-doc comments, from source files. Usually these
18are used to describe the functions and types and design of the code. The
19kernel-doc comments have some special structure and formatting, but beyond that
20they are also treated as reStructuredText.
21
22There is also the deprecated DocBook toolchain to generate documentation from
23DocBook XML template files under ``Documentation/DocBook``. The DocBook files
24are to be converted to reStructuredText, and the toolchain is slated to be
25removed.
26
27Finally, there are thousands of plain text documentation files scattered around
28``Documentation``. Some of these will likely be converted to reStructuredText
29over time, but the bulk of them will remain in plain text.
30
31Sphinx Build
32============
33
34The usual way to generate the documentation is to run ``make htmldocs`` or
35``make pdfdocs``. There are also other formats available, see the documentation
36section of ``make help``. The generated documentation is placed in
37format-specific subdirectories under ``Documentation/output``.
38
39To generate documentation, Sphinx (``sphinx-build``) must obviously be
40installed. For prettier HTML output, the Read the Docs Sphinx theme
41(``sphinx_rtd_theme``) is used if available. For PDF output, ``rst2pdf`` is also
42needed. All of these are widely available and packaged in distributions.
43
44To pass extra options to Sphinx, you can use the ``SPHINXOPTS`` make
45variable. For example, use ``make SPHINXOPTS=-v htmldocs`` to get more verbose
46output.
47
48To remove the generated documentation, run ``make cleandocs``.
49
50Writing Documentation
51=====================
52
53Adding new documentation can be as simple as:
54
551. Add a new ``.rst`` file somewhere under ``Documentation``.
562. Refer to it from the Sphinx main `TOC tree`_ in ``Documentation/index.rst``.
57
58.. _TOC tree: http://www.sphinx-doc.org/en/stable/markup/toctree.html
59
60This is usually good enough for simple documentation (like the one you're
61reading right now), but for larger documents it may be advisable to create a
62subdirectory (or use an existing one). For example, the graphics subsystem
63documentation is under ``Documentation/gpu``, split to several ``.rst`` files,
64and has a separate ``index.rst`` (with a ``toctree`` of its own) referenced from
65the main index.
66
67See the documentation for `Sphinx`_ and `reStructuredText`_ on what you can do
68with them. In particular, the Sphinx `reStructuredText Primer`_ is a good place
69to get started with reStructuredText. There are also some `Sphinx specific
70markup constructs`_.
71
72.. _reStructuredText Primer: http://www.sphinx-doc.org/en/stable/rest.html
73.. _Sphinx specific markup constructs: http://www.sphinx-doc.org/en/stable/markup/index.html
74
75Specific guidelines for the kernel documentation
76------------------------------------------------
77
78Here are some specific guidelines for the kernel documentation:
79
80* Please don't go overboard with reStructuredText markup. Keep it simple.
81
82* Please stick to this order of heading adornments:
83
84 1. ``=`` with overline for document title::
85
86 ==============
87 Document title
88 ==============
89
90 2. ``=`` for chapters::
91
92 Chapters
93 ========
94
95 3. ``-`` for sections::
96
97 Section
98 -------
99
100 4. ``~`` for subsections::
101
102 Subsection
103 ~~~~~~~~~~
104
105 Although RST doesn't mandate a specific order ("Rather than imposing a fixed
106 number and order of section title adornment styles, the order enforced will be
107 the order as encountered."), having the higher levels the same overall makes
108 it easier to follow the documents.
109
110
111Including kernel-doc comments
112=============================
113
114The Linux kernel source files may contain structured documentation comments, or
115kernel-doc comments to describe the functions and types and design of the
116code. The documentation comments may be included to any of the reStructuredText
117documents using a dedicated kernel-doc Sphinx directive extension.
118
119The kernel-doc directive is of the format::
120
121 .. kernel-doc:: source
122 :option:
123
124The *source* is the path to a source file, relative to the kernel source
125tree. The following directive options are supported:
126
127export: *[source-pattern ...]*
128 Include documentation for all functions in *source* that have been exported
129 using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either in *source* or in any
130 of the files specified by *source-pattern*.
131
132 The *source-pattern* is useful when the kernel-doc comments have been placed
133 in header files, while ``EXPORT_SYMBOL`` and ``EXPORT_SYMBOL_GPL`` are next to
134 the function definitions.
135
136 Examples::
137
138 .. kernel-doc:: lib/bitmap.c
139 :export:
140
141 .. kernel-doc:: include/net/mac80211.h
142 :export: net/mac80211/*.c
143
144internal: *[source-pattern ...]*
145 Include documentation for all functions and types in *source* that have
146 **not** been exported using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either
147 in *source* or in any of the files specified by *source-pattern*.
148
149 Example::
150
151 .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
152 :internal:
153
154doc: *title*
155 Include documentation for the ``DOC:`` paragraph identified by *title* in
156 *source*. Spaces are allowed in *title*; do not quote the *title*. The *title*
157 is only used as an identifier for the paragraph, and is not included in the
158 output. Please make sure to have an appropriate heading in the enclosing
159 reStructuredText document.
160
161 Example::
162
163 .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
164 :doc: High Definition Audio over HDMI and Display Port
165
166functions: *function* *[...]*
167 Include documentation for each *function* in *source*.
168
169 Example::
170
171 .. kernel-doc:: lib/bitmap.c
172 :functions: bitmap_parselist bitmap_parselist_user
173
174Without options, the kernel-doc directive includes all documentation comments
175from the source file.
176
177The kernel-doc extension is included in the kernel source tree, at
178``Documentation/sphinx/kernel-doc.py``. Internally, it uses the
179``scripts/kernel-doc`` script to extract the documentation comments from the
180source.
181
182Writing kernel-doc comments
183===========================
184
185In order to provide embedded, "C" friendly, easy to maintain, but consistent and
186extractable overview, function and type documentation, the Linux kernel has
187adopted a consistent style for documentation comments. The format for this
188documentation is called the kernel-doc format, described below. This style
189embeds the documentation within the source files, using a few simple conventions
190for adding documentation paragraphs and documenting functions and their
191parameters, structures and unions and their members, enumerations, and typedefs.
192
193.. note:: The kernel-doc format is deceptively similar to gtk-doc or Doxygen,
194 yet distinctively different, for historical reasons. The kernel source
195 contains tens of thousands of kernel-doc comments. Please stick to the style
196 described here.
197
198The ``scripts/kernel-doc`` script is used by the Sphinx kernel-doc extension in
199the documentation build to extract this embedded documentation into the various
200HTML, PDF, and other format documents.
201
202In order to provide good documentation of kernel functions and data structures,
203please use the following conventions to format your kernel-doc comments in the
204Linux kernel source.
205
206How to format kernel-doc comments
207---------------------------------
208
209The opening comment mark ``/**`` is reserved for kernel-doc comments. Only
210comments so marked will be considered by the ``kernel-doc`` tool. Use it only
211for comment blocks that contain kernel-doc formatted comments. The usual ``*/``
212should be used as the closing comment marker. The lines in between should be
213prefixed by `` * `` (space star space).
214
215The function and type kernel-doc comments should be placed just before the
216function or type being described. The overview kernel-doc comments may be freely
217placed at the top indentation level.
218
219Example kernel-doc function comment::
220
221 /**
222 * foobar() - Brief description of foobar.
223 * @arg: Description of argument of foobar.
224 *
225 * Longer description of foobar.
226 *
227 * Return: Description of return value of foobar.
228 */
229 int foobar(int arg)
230
231The format is similar for documentation for structures, enums, paragraphs,
232etc. See the sections below for details.
233
234The kernel-doc structure is extracted from the comments, and proper `Sphinx C
235Domain`_ function and type descriptions with anchors are generated for them. The
236descriptions are filtered for special kernel-doc highlights and
237cross-references. See below for details.
238
239.. _Sphinx C Domain: http://www.sphinx-doc.org/en/stable/domains.html
240
241Highlights and cross-references
242-------------------------------
243
244The following special patterns are recognized in the kernel-doc comment
245descriptive text and converted to proper reStructuredText markup and `Sphinx C
246Domain`_ references.
247
248.. attention:: The below are **only** recognized within kernel-doc comments,
249 **not** within normal reStructuredText documents.
250
251``funcname()``
252 Function reference.
253
254``@parameter``
255 Name of a function parameter. (No cross-referencing, just formatting.)
256
257``%CONST``
258 Name of a constant. (No cross-referencing, just formatting.)
259
260``$ENVVAR``
261 Name of an environment variable. (No cross-referencing, just formatting.)
262
263``&struct name``
264 Structure reference.
265
266``&enum name``
267 Enum reference.
268
269``&typedef name``
270 Typedef reference.
271
272``&struct_name->member`` or ``&struct_name.member``
273 Structure or union member reference. The cross-reference will be to the struct
274 or union definition, not the member directly.
275
276``&name``
277 A generic type reference. Prefer using the full reference described above
278 instead. This is mostly for legacy comments.
279
280Cross-referencing from reStructuredText
281~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282
283To cross-reference the functions and types defined in the kernel-doc comments
284from reStructuredText documents, please use the `Sphinx C Domain`_
285references. For example::
286
287 See function :c:func:`foo` and struct/union/enum/typedef :c:type:`bar`.
288
289While the type reference works with just the type name, without the
290struct/union/enum/typedef part in front, you may want to use::
291
292 See :c:type:`struct foo <foo>`.
293 See :c:type:`union bar <bar>`.
294 See :c:type:`enum baz <baz>`.
295 See :c:type:`typedef meh <meh>`.
296
297This will produce prettier links, and is in line with how kernel-doc does the
298cross-references.
299
300For further details, please refer to the `Sphinx C Domain`_ documentation.
301
302Function documentation
303----------------------
304
305The general format of a function and function-like macro kernel-doc comment is::
306
307 /**
308 * function_name() - Brief description of function.
309 * @arg1: Describe the first argument.
310 * @arg2: Describe the second argument.
311 * One can provide multiple line descriptions
312 * for arguments.
313 *
314 * A longer description, with more discussion of the function function_name()
315 * that might be useful to those using or modifying it. Begins with an
316 * empty comment line, and may include additional embedded empty
317 * comment lines.
318 *
319 * The longer description may have multiple paragraphs.
320 *
321 * Return: Describe the return value of foobar.
322 *
323 * The return value description can also have multiple paragraphs, and should
324 * be placed at the end of the comment block.
325 */
326
327The brief description following the function name may span multiple lines, and
328ends with an ``@argument:`` description, a blank comment line, or the end of the
329comment block.
330
331The kernel-doc function comments describe each parameter to the function, in
332order, with the ``@argument:`` descriptions. The ``@argument:`` descriptions
333must begin on the very next line following the opening brief function
334description line, with no intervening blank comment lines. The ``@argument:``
335descriptions may span multiple lines. The continuation lines may contain
336indentation. If a function parameter is ``...`` (varargs), it should be listed
337in kernel-doc notation as: ``@...:``.
338
339The return value, if any, should be described in a dedicated section at the end
340of the comment starting with "Return:".
341
342Structure, union, and enumeration documentation
343-----------------------------------------------
344
345The general format of a struct, union, and enum kernel-doc comment is::
346
347 /**
348 * struct struct_name - Brief description.
349 * @member_name: Description of member member_name.
350 *
351 * Description of the structure.
352 */
353
354Below, "struct" is used to mean structs, unions and enums, and "member" is used
355to mean struct and union members as well as enumerations in an enum.
356
357The brief description following the structure name may span multiple lines, and
358ends with a ``@member:`` description, a blank comment line, or the end of the
359comment block.
360
361The kernel-doc data structure comments describe each member of the structure, in
362order, with the ``@member:`` descriptions. The ``@member:`` descriptions must
363begin on the very next line following the opening brief function description
364line, with no intervening blank comment lines. The ``@member:`` descriptions may
365span multiple lines. The continuation lines may contain indentation.
366
367In-line member documentation comments
368~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
369
370The structure members may also be documented in-line within the definition::
371
372 /**
373 * struct foo - Brief description.
374 * @foo: The Foo member.
375 */
376 struct foo {
377 int foo;
378 /**
379 * @bar: The Bar member.
380 */
381 int bar;
382 /**
383 * @baz: The Baz member.
384 *
385 * Here, the member description may contain several paragraphs.
386 */
387 int baz;
388 }
389
390Private members
391~~~~~~~~~~~~~~~
392
393Inside a struct description, you can use the "private:" and "public:" comment
394tags. Structure fields that are inside a "private:" area are not listed in the
395generated output documentation. The "private:" and "public:" tags must begin
396immediately following a ``/*`` comment marker. They may optionally include
397comments between the ``:`` and the ending ``*/`` marker.
398
399Example::
400
401 /**
402 * struct my_struct - short description
403 * @a: first member
404 * @b: second member
405 *
406 * Longer description
407 */
408 struct my_struct {
409 int a;
410 int b;
411 /* private: internal use only */
412 int c;
413 };
414
415
416Typedef documentation
417---------------------
418
419The general format of a typedef kernel-doc comment is::
420
421 /**
422 * typedef type_name - Brief description.
423 *
424 * Description of the type.
425 */
426
427Overview documentation comments
428-------------------------------
429
430To facilitate having source code and comments close together, you can include
431kernel-doc documentation blocks that are free-form comments instead of being
432kernel-doc for functions, structures, unions, enums, or typedefs. This could be
433used for something like a theory of operation for a driver or library code, for
434example.
435
436This is done by using a ``DOC:`` section keyword with a section title.
437
438The general format of an overview or high-level documentation comment is::
439
440 /**
441 * DOC: Theory of Operation
442 *
443 * The whizbang foobar is a dilly of a gizmo. It can do whatever you
444 * want it to do, at any time. It reads your mind. Here's how it works.
445 *
446 * foo bar splat
447 *
448 * The only drawback to this gizmo is that is can sometimes damage
449 * hardware, software, or its subject(s).
450 */
451
452The title following ``DOC:`` acts as a heading within the source file, but also
453as an identifier for extracting the documentation comment. Thus, the title must
454be unique within the file.
455
456Recommendations
457---------------
458
459We definitely need kernel-doc formatted documentation for functions that are
460exported to loadable modules using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL``.
461
462We also look to provide kernel-doc formatted documentation for functions
463externally visible to other kernel files (not marked "static").
464
465We also recommend providing kernel-doc formatted documentation for private (file
466"static") routines, for consistency of kernel source code layout. But this is
467lower priority and at the discretion of the MAINTAINER of that kernel source
468file.
469
470Data structures visible in kernel include files should also be documented using
471kernel-doc formatted comments.
472
473DocBook XML [DEPRECATED]
474========================
475
476.. attention::
477
478 This section describes the deprecated DocBook XML toolchain. Please do not
479 create new DocBook XML template files. Please consider converting existing
480 DocBook XML templates files to Sphinx/reStructuredText.
481
482Converting DocBook to Sphinx
483----------------------------
484
485Over time, we expect all of the documents under ``Documentation/DocBook`` to be
486converted to Sphinx and reStructuredText. For most DocBook XML documents, a good
487enough solution is to use the simple ``Documentation/sphinx/tmplcvt`` script,
488which uses ``pandoc`` under the hood. For example::
489
490 $ cd Documentation/sphinx
491 $ ./tmplcvt ../DocBook/in.tmpl ../out.rst
492
493Then edit the resulting rst files to fix any remaining issues, and add the
494document in the ``toctree`` in ``Documentation/index.rst``.
495
496Components of the kernel-doc system
497-----------------------------------
498
499Many places in the source tree have extractable documentation in the form of
500block comments above functions. The components of this system are:
501
502- ``scripts/kernel-doc``
503
504 This is a perl script that hunts for the block comments and can mark them up
505 directly into reStructuredText, DocBook, man, text, and HTML. (No, not
506 texinfo.)
507
508- ``Documentation/DocBook/*.tmpl``
509
510 These are XML template files, which are normal XML files with special
511 place-holders for where the extracted documentation should go.
512
513- ``scripts/docproc.c``
514
515 This is a program for converting XML template files into XML files. When a
516 file is referenced it is searched for symbols exported (EXPORT_SYMBOL), to be
517 able to distinguish between internal and external functions.
518
519 It invokes kernel-doc, giving it the list of functions that are to be
520 documented.
521
522 Additionally it is used to scan the XML template files to locate all the files
523 referenced herein. This is used to generate dependency information as used by
524 make.
525
526- ``Makefile``
527
528 The targets 'xmldocs', 'psdocs', 'pdfdocs', and 'htmldocs' are used to build
529 DocBook XML files, PostScript files, PDF files, and html files in
530 Documentation/DocBook. The older target 'sgmldocs' is equivalent to 'xmldocs'.
531
532- ``Documentation/DocBook/Makefile``
533
534 This is where C files are associated with SGML templates.
535
536How to use kernel-doc comments in DocBook XML template files
537------------------------------------------------------------
538
539DocBook XML template files (\*.tmpl) are like normal XML files, except that they
540can contain escape sequences where extracted documentation should be inserted.
541
542``!E<filename>`` is replaced by the documentation, in ``<filename>``, for
543functions that are exported using ``EXPORT_SYMBOL``: the function list is
544collected from files listed in ``Documentation/DocBook/Makefile``.
545
546``!I<filename>`` is replaced by the documentation for functions that are **not**
547exported using ``EXPORT_SYMBOL``.
548
549``!D<filename>`` is used to name additional files to search for functions
550exported using ``EXPORT_SYMBOL``.
551
552``!F<filename> <function [functions...]>`` is replaced by the documentation, in
553``<filename>``, for the functions listed.
554
555``!P<filename> <section title>`` is replaced by the contents of the ``DOC:``
556section titled ``<section title>`` from ``<filename>``. Spaces are allowed in
557``<section title>``; do not quote the ``<section title>``.
558
559``!C<filename>`` is replaced by nothing, but makes the tools check that all DOC:
560sections and documented functions, symbols, etc. are used. This makes sense to
561use when you use ``!F`` or ``!P`` only and want to verify that all documentation
562is included.