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