kernel-doc.rst 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. .. title:: Kernel-doc comments
  2. ===========================
  3. Writing kernel-doc comments
  4. ===========================
  5. The Linux kernel source files may contain structured documentation
  6. comments in the kernel-doc format to describe the functions, types
  7. and design of the code. It is easier to keep documentation up-to-date
  8. when it is embedded in source files.
  9. .. note:: The kernel-doc format is deceptively similar to javadoc,
  10. gtk-doc or Doxygen, yet distinctively different, for historical
  11. reasons. The kernel source contains tens of thousands of kernel-doc
  12. comments. Please stick to the style described here.
  13. .. note:: kernel-doc does not cover Rust code: please see
  14. Documentation/rust/general-information.rst instead.
  15. The kernel-doc structure is extracted from the comments, and proper
  16. `Sphinx C Domain`_ function and type descriptions with anchors are
  17. generated from them. The descriptions are filtered for special kernel-doc
  18. highlights and cross-references. See below for details.
  19. .. _Sphinx C Domain: http://www.sphinx-doc.org/en/stable/domains.html
  20. Every function that is exported to loadable modules using
  21. ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` should have a kernel-doc
  22. comment. Functions and data structures in header files which are intended
  23. to be used by modules should also have kernel-doc comments.
  24. It is good practice to also provide kernel-doc formatted documentation
  25. for functions externally visible to other kernel files (not marked
  26. ``static``). We also recommend providing kernel-doc formatted
  27. documentation for private (file ``static``) routines, for consistency of
  28. kernel source code layout. This is lower priority and at the discretion
  29. of the maintainer of that kernel source file.
  30. How to format kernel-doc comments
  31. ---------------------------------
  32. The opening comment mark ``/**`` is used for kernel-doc comments. The
  33. ``kernel-doc`` tool will extract comments marked this way. The rest of
  34. the comment is formatted like a normal multi-line comment with a column
  35. of asterisks on the left side, closing with ``*/`` on a line by itself.
  36. The function and type kernel-doc comments should be placed just before
  37. the function or type being described in order to maximise the chance
  38. that somebody changing the code will also change the documentation. The
  39. overview kernel-doc comments may be placed anywhere at the top indentation
  40. level.
  41. Running the ``kernel-doc`` tool with increased verbosity and without actual
  42. output generation may be used to verify proper formatting of the
  43. documentation comments. For example::
  44. tools/docs/kernel-doc -v -none drivers/foo/bar.c
  45. The documentation format of ``.c`` files is also verified by the kernel build
  46. when it is requested to perform extra gcc checks::
  47. make W=n
  48. However, the above command does not verify header files. These should be checked
  49. separately using ``kernel-doc``.
  50. Function documentation
  51. ----------------------
  52. The general format of a function and function-like macro kernel-doc comment is::
  53. /**
  54. * function_name() - Brief description of function.
  55. * @arg1: Describe the first argument.
  56. * @arg2: Describe the second argument.
  57. * One can provide multiple line descriptions
  58. * for arguments.
  59. *
  60. * A longer description, with more discussion of the function function_name()
  61. * that might be useful to those using or modifying it. Begins with an
  62. * empty comment line, and may include additional embedded empty
  63. * comment lines.
  64. *
  65. * The longer description may have multiple paragraphs.
  66. *
  67. * Context: Describes whether the function can sleep, what locks it takes,
  68. * releases, or expects to be held. It can extend over multiple
  69. * lines.
  70. * Return: Describe the return value of function_name.
  71. *
  72. * The return value description can also have multiple paragraphs, and should
  73. * be placed at the end of the comment block.
  74. */
  75. The brief description following the function name may span multiple lines, and
  76. ends with an argument description, a blank comment line, or the end of the
  77. comment block.
  78. Function parameters
  79. ~~~~~~~~~~~~~~~~~~~
  80. Each function argument should be described in order, immediately following
  81. the short function description. Do not leave a blank line between the
  82. function description and the arguments, nor between the arguments.
  83. Each ``@argument:`` description may span multiple lines.
  84. .. note::
  85. If the ``@argument`` description has multiple lines, the continuation
  86. of the description should start at the same column as the previous line::
  87. * @argument: some long description
  88. * that continues on next lines
  89. or::
  90. * @argument:
  91. * some long description
  92. * that continues on next lines
  93. If a function has a variable number of arguments, its description should
  94. be written in kernel-doc notation as::
  95. * @...: description
  96. Function context
  97. ~~~~~~~~~~~~~~~~
  98. The context in which a function can be called should be described in a
  99. section named ``Context``. This should include whether the function
  100. sleeps or can be called from interrupt context, as well as what locks
  101. it takes, releases and expects to be held by its caller.
  102. Examples::
  103. * Context: Any context.
  104. * Context: Any context. Takes and releases the RCU lock.
  105. * Context: Any context. Expects <lock> to be held by caller.
  106. * Context: Process context. May sleep if @gfp flags permit.
  107. * Context: Process context. Takes and releases <mutex>.
  108. * Context: Softirq or process context. Takes and releases <lock>, BH-safe.
  109. * Context: Interrupt context.
  110. Return values
  111. ~~~~~~~~~~~~~
  112. The return value, if any, should be described in a dedicated section
  113. named ``Return`` (or ``Returns``).
  114. .. note::
  115. #) The multi-line descriptive text you provide does *not* recognize
  116. line breaks, so if you try to format some text nicely, as in::
  117. * Return:
  118. * %0 - OK
  119. * %-EINVAL - invalid argument
  120. * %-ENOMEM - out of memory
  121. this will all run together and produce::
  122. Return: 0 - OK -EINVAL - invalid argument -ENOMEM - out of memory
  123. So, in order to produce the desired line breaks, you need to use a
  124. ReST list, e. g.::
  125. * Return:
  126. * * %0 - OK to runtime suspend the device
  127. * * %-EBUSY - Device should not be runtime suspended
  128. #) If the descriptive text you provide has lines that begin with
  129. some phrase followed by a colon, each of those phrases will be taken
  130. as a new section heading, which probably won't produce the desired
  131. effect.
  132. Structure, union, and enumeration documentation
  133. -----------------------------------------------
  134. The general format of a ``struct``, ``union``, and ``enum`` kernel-doc
  135. comment is::
  136. /**
  137. * struct struct_name - Brief description.
  138. * @member1: Description of member1.
  139. * @member2: Description of member2.
  140. * One can provide multiple line descriptions
  141. * for members.
  142. *
  143. * Description of the structure.
  144. */
  145. You can replace the ``struct`` in the above example with ``union`` or
  146. ``enum`` to describe unions or enums. ``member`` is used to mean ``struct``
  147. and ``union`` member names as well as enumerations in an ``enum``.
  148. The brief description following the structure name may span multiple
  149. lines, and ends with a member description, a blank comment line, or the
  150. end of the comment block.
  151. Members
  152. ~~~~~~~
  153. Members of structs, unions and enums should be documented the same way
  154. as function parameters; they immediately succeed the short description
  155. and may be multi-line.
  156. Inside a ``struct`` or ``union`` description, you can use the ``private:`` and
  157. ``public:`` comment tags. Structure fields that are inside a ``private:``
  158. area are not listed in the generated output documentation.
  159. The ``private:`` and ``public:`` tags must begin immediately following a
  160. ``/*`` comment marker. They may optionally include comments between the
  161. ``:`` and the ending ``*/`` marker.
  162. Example::
  163. /**
  164. * struct my_struct - short description
  165. * @a: first member
  166. * @b: second member
  167. * @d: fourth member
  168. *
  169. * Longer description
  170. */
  171. struct my_struct {
  172. int a;
  173. int b;
  174. /* private: internal use only */
  175. int c;
  176. /* public: the next one is public */
  177. int d;
  178. };
  179. Nested structs/unions
  180. ~~~~~~~~~~~~~~~~~~~~~
  181. It is possible to document nested structs and unions, like::
  182. /**
  183. * struct nested_foobar - a struct with nested unions and structs
  184. * @memb1: first member of anonymous union/anonymous struct
  185. * @memb2: second member of anonymous union/anonymous struct
  186. * @memb3: third member of anonymous union/anonymous struct
  187. * @memb4: fourth member of anonymous union/anonymous struct
  188. * @bar: non-anonymous union
  189. * @bar.st1: struct st1 inside @bar
  190. * @bar.st2: struct st2 inside @bar
  191. * @bar.st1.memb1: first member of struct st1 on union bar
  192. * @bar.st1.memb2: second member of struct st1 on union bar
  193. * @bar.st2.memb1: first member of struct st2 on union bar
  194. * @bar.st2.memb2: second member of struct st2 on union bar
  195. */
  196. struct nested_foobar {
  197. /* Anonymous union/struct*/
  198. union {
  199. struct {
  200. int memb1;
  201. int memb2;
  202. };
  203. struct {
  204. void *memb3;
  205. int memb4;
  206. };
  207. };
  208. union {
  209. struct {
  210. int memb1;
  211. int memb2;
  212. } st1;
  213. struct {
  214. void *memb1;
  215. int memb2;
  216. } st2;
  217. } bar;
  218. };
  219. .. note::
  220. #) When documenting nested structs or unions, if the ``struct``/``union``
  221. ``foo`` is named, the member ``bar`` inside it should be documented as
  222. ``@foo.bar:``
  223. #) When the nested ``struct``/``union`` is anonymous, the member ``bar`` in
  224. it should be documented as ``@bar:``
  225. In-line member documentation comments
  226. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  227. The structure members may also be documented in-line within the definition.
  228. There are two styles, single-line comments where both the opening ``/**`` and
  229. closing ``*/`` are on the same line, and multi-line comments where they are each
  230. on a line of their own, like all other kernel-doc comments::
  231. /**
  232. * struct foo - Brief description.
  233. * @foo: The Foo member.
  234. */
  235. struct foo {
  236. int foo;
  237. /**
  238. * @bar: The Bar member.
  239. */
  240. int bar;
  241. /**
  242. * @baz: The Baz member.
  243. *
  244. * Here, the member description may contain several paragraphs.
  245. */
  246. int baz;
  247. union {
  248. /** @foobar: Single line description. */
  249. int foobar;
  250. };
  251. /** @bar2: Description for struct @bar2 inside @foo */
  252. struct {
  253. /**
  254. * @bar2.barbar: Description for @barbar inside @foo.bar2
  255. */
  256. int barbar;
  257. } bar2;
  258. };
  259. Typedef documentation
  260. ---------------------
  261. The general format of a ``typedef`` kernel-doc comment is::
  262. /**
  263. * typedef type_name - Brief description.
  264. *
  265. * Description of the type.
  266. */
  267. Typedefs with function prototypes can also be documented::
  268. /**
  269. * typedef type_name - Brief description.
  270. * @arg1: description of arg1
  271. * @arg2: description of arg2
  272. *
  273. * Description of the type.
  274. *
  275. * Context: Locking context.
  276. * Returns: Meaning of the return value.
  277. */
  278. typedef void (*type_name)(struct v4l2_ctrl *arg1, void *arg2);
  279. Variables documentation
  280. -----------------------
  281. The general format of a kernel-doc variable comment is::
  282. /**
  283. * var var_name - Brief description.
  284. *
  285. * Description of the var_name variable.
  286. */
  287. extern int var_name;
  288. Object-like macro documentation
  289. -------------------------------
  290. Object-like macros are distinct from function-like macros. They are
  291. differentiated by whether the macro name is immediately followed by a
  292. left parenthesis ('(') for function-like macros or not followed by one
  293. for object-like macros.
  294. Function-like macros are handled like functions by ``tools/docs/kernel-doc``.
  295. They may have a parameter list. Object-like macros have do not have a
  296. parameter list.
  297. The general format of an object-like macro kernel-doc comment is::
  298. /**
  299. * define object_name - Brief description.
  300. *
  301. * Description of the object.
  302. */
  303. Example::
  304. /**
  305. * define MAX_ERRNO - maximum errno value that is supported
  306. *
  307. * Kernel pointers have redundant information, so we can use a
  308. * scheme where we can return either an error code or a normal
  309. * pointer with the same return value.
  310. */
  311. #define MAX_ERRNO 4095
  312. Example::
  313. /**
  314. * define DRM_GEM_VRAM_PLANE_HELPER_FUNCS - \
  315. * Initializes struct drm_plane_helper_funcs for VRAM handling
  316. *
  317. * This macro initializes struct drm_plane_helper_funcs to use the
  318. * respective helper functions.
  319. */
  320. #define DRM_GEM_VRAM_PLANE_HELPER_FUNCS \
  321. .prepare_fb = drm_gem_vram_plane_helper_prepare_fb, \
  322. .cleanup_fb = drm_gem_vram_plane_helper_cleanup_fb
  323. Highlights and cross-references
  324. -------------------------------
  325. The following special patterns are recognized in the kernel-doc comment
  326. descriptive text and converted to proper reStructuredText markup and `Sphinx C
  327. Domain`_ references.
  328. .. attention:: The below are **only** recognized within kernel-doc comments,
  329. **not** within normal reStructuredText documents.
  330. ``funcname()``
  331. Function reference.
  332. ``@parameter``
  333. Name of a function parameter. (No cross-referencing, just formatting.)
  334. ``%CONST``
  335. Name of a constant. (No cross-referencing, just formatting.)
  336. Examples::
  337. %0 %NULL %-1 %-EFAULT %-EINVAL %-ENOMEM
  338. ````literal````
  339. A literal block that should be handled as-is. The output will use a
  340. ``monospaced font``.
  341. Useful if you need to use special characters that would otherwise have some
  342. meaning either by kernel-doc script or by reStructuredText.
  343. This is particularly useful if you need to use things like ``%ph`` inside
  344. a function description.
  345. ``$ENVVAR``
  346. Name of an environment variable. (No cross-referencing, just formatting.)
  347. ``&struct name``
  348. Structure reference.
  349. ``&enum name``
  350. Enum reference.
  351. ``&typedef name``
  352. Typedef reference.
  353. ``&struct_name->member`` or ``&struct_name.member``
  354. ``struct`` or ``union`` member reference. The cross-reference will be to the
  355. ``struct`` or ``union`` definition, not the member directly.
  356. ``&name``
  357. A generic type reference. Prefer using the full reference described above
  358. instead. This is mostly for legacy comments.
  359. Cross-referencing from reStructuredText
  360. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  361. No additional syntax is needed to cross-reference the functions and types
  362. defined in the kernel-doc comments from reStructuredText documents.
  363. Just end function names with ``()`` and write ``struct``, ``union``, ``enum``
  364. or ``typedef`` before types.
  365. For example::
  366. See foo().
  367. See struct foo.
  368. See union bar.
  369. See enum baz.
  370. See typedef meh.
  371. However, if you want custom text in the cross-reference link, that can be done
  372. through the following syntax::
  373. See :c:func:`my custom link text for function foo <foo>`.
  374. See :c:type:`my custom link text for struct bar <bar>`.
  375. For further details, please refer to the `Sphinx C Domain`_ documentation.
  376. .. note::
  377. Variables aren't automatically cross referenced. For those, you need to
  378. explicitly add a C domain cross-reference.
  379. Overview documentation comments
  380. -------------------------------
  381. To facilitate having source code and comments close together, you can include
  382. kernel-doc documentation blocks that are free-form comments instead of being
  383. kernel-doc for functions, structures, unions, enums, typedefs or variables.
  384. This could be used for something like a theory of operation for a driver or
  385. library code, for example.
  386. This is done by using a ``DOC:`` section keyword with a section title.
  387. The general format of an overview or high-level documentation comment is::
  388. /**
  389. * DOC: Theory of Operation
  390. *
  391. * The whizbang foobar is a dilly of a gizmo. It can do whatever you
  392. * want it to do, at any time. It reads your mind. Here's how it works.
  393. *
  394. * foo bar splat
  395. *
  396. * The only drawback to this gizmo is that is can sometimes damage
  397. * hardware, software, or its subject(s).
  398. */
  399. The title following ``DOC:`` acts as a heading within the source file, but also
  400. as an identifier for extracting the documentation comment. Thus, the title must
  401. be unique within the file.
  402. =============================
  403. Including kernel-doc comments
  404. =============================
  405. The documentation comments may be included in any of the reStructuredText
  406. documents using a dedicated kernel-doc Sphinx directive extension.
  407. The kernel-doc directive is of the format::
  408. .. kernel-doc:: source
  409. :option:
  410. The *source* is the path to a source file, relative to the kernel source
  411. tree. The following directive options are supported:
  412. export: *[source-pattern ...]*
  413. Include documentation for all functions in *source* that have been exported
  414. using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either in *source* or in any
  415. of the files specified by *source-pattern*.
  416. The *source-pattern* is useful when the kernel-doc comments have been placed
  417. in header files, while ``EXPORT_SYMBOL`` and ``EXPORT_SYMBOL_GPL`` are next to
  418. the function definitions.
  419. Examples::
  420. .. kernel-doc:: lib/bitmap.c
  421. :export:
  422. .. kernel-doc:: include/net/mac80211.h
  423. :export: net/mac80211/*.c
  424. internal: *[source-pattern ...]*
  425. Include documentation for all functions and types in *source* that have
  426. **not** been exported using ``EXPORT_SYMBOL`` or ``EXPORT_SYMBOL_GPL`` either
  427. in *source* or in any of the files specified by *source-pattern*.
  428. Example::
  429. .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
  430. :internal:
  431. identifiers: *[ function/type ...]*
  432. Include documentation for each *function* and *type* in *source*.
  433. If no *function* is specified, the documentation for all functions
  434. and types in the *source* will be included.
  435. *type* can be a ``struct``, ``union``, ``enum``, ``typedef`` or ``var``
  436. identifier.
  437. Examples::
  438. .. kernel-doc:: lib/bitmap.c
  439. :identifiers: bitmap_parselist bitmap_parselist_user
  440. .. kernel-doc:: lib/idr.c
  441. :identifiers:
  442. no-identifiers: *[ function/type ...]*
  443. Exclude documentation for each *function* and *type* in *source*.
  444. Example::
  445. .. kernel-doc:: lib/bitmap.c
  446. :no-identifiers: bitmap_parselist
  447. functions: *[ function/type ...]*
  448. This is an alias of the 'identifiers' directive and deprecated.
  449. doc: *title*
  450. Include documentation for the ``DOC:`` paragraph identified by *title* in
  451. *source*. Spaces are allowed in *title*; do not quote the *title*. The *title*
  452. is only used as an identifier for the paragraph, and is not included in the
  453. output. Please make sure to have an appropriate heading in the enclosing
  454. reStructuredText document.
  455. Example::
  456. .. kernel-doc:: drivers/gpu/drm/i915/intel_audio.c
  457. :doc: High Definition Audio over HDMI and Display Port
  458. Without options, the kernel-doc directive includes all documentation comments
  459. from the source file.
  460. The kernel-doc extension is included in the kernel source tree, at
  461. ``Documentation/sphinx/kerneldoc.py``. Internally, it uses the
  462. ``tools/docs/kernel-doc`` script to extract the documentation comments from
  463. the source.
  464. .. _kernel_doc:
  465. How to use kernel-doc to generate man pages
  466. -------------------------------------------
  467. To generate man pages for all files that contain kernel-doc markups, run::
  468. $ make mandocs
  469. Or calling ``script-build-wrapper`` directly::
  470. $ ./tools/docs/sphinx-build-wrapper mandocs
  471. The output will be at ``/man`` directory inside the output directory
  472. (by default: ``Documentation/output``).
  473. Optionally, it is possible to generate a partial set of man pages by
  474. using SPHINXDIRS:
  475. $ make SPHINXDIRS=driver-api/media mandocs
  476. .. note::
  477. When SPHINXDIRS={subdir} is used, it will only generate man pages for
  478. the files explicitly inside a ``Documentation/{subdir}/.../*.rst`` file.