driver.xml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <title>V4L2 Driver Programming</title>
  2. <!-- This part defines the interface between the "videodev"
  3. module and individual drivers. -->
  4. <para>to do</para>
  5. <!--
  6. <para>V4L2 is a two-layer driver system. The top layer is the "videodev"
  7. kernel module. When videodev initializes it registers as character device
  8. with major number 81, and it registers a set of file operations. All V4L2
  9. drivers are really clients of videodev, which calls V4L2 drivers through
  10. driver method functions. V4L2 drivers are also written as kernel modules.
  11. After probing the hardware they register one or more devices with
  12. videodev.</para>
  13. <section id="driver-modules">
  14. <title>Driver Modules</title>
  15. <para>V4L2 driver modules must have an initialization function which is
  16. called after the module was loaded into kernel, an exit function whis is
  17. called before the module is removed. When the driver is compiled into the
  18. kernel these functions called at system boot and shutdown time.</para>
  19. <informalexample>
  20. <programlisting>
  21. #include &lt;linux/module.h&gt;
  22. /* Export information about this module. For details and other useful
  23. macros see <filename>linux/module.h</filename>. */
  24. MODULE_DESCRIPTION("my - driver for my hardware");
  25. MODULE_AUTHOR("Your name here");
  26. MODULE_LICENSE("GPL");
  27. static void
  28. my_module_exit (void)
  29. {
  30. /* Free all resources allocated by my_module_init(). */
  31. }
  32. static int
  33. my_module_init (void)
  34. {
  35. /* Bind the driver to the supported hardware, see
  36. <link linkend="driver-pci"> and
  37. <link linkend="driver-usb"> for examples. */
  38. return 0; /* a negative value on error, 0 on success. */
  39. }
  40. /* Export module functions. */
  41. module_init (my_module_init);
  42. module_exit (my_module_exit);
  43. </programlisting>
  44. </informalexample>
  45. <para>Users can add parameters when kernel modules are inserted:</para>
  46. <informalexample>
  47. <programlisting>
  48. include &lt;linux/moduleparam.h&gt;
  49. static int my_option = 123;
  50. static int my_option_array[47];
  51. /* Export the symbol, an int, with access permissions 0664.
  52. See <filename>linux/moduleparam.h</filename> for other types. */
  53. module_param (my_option, int, 0644);
  54. module_param_array (my_option_array, int, NULL, 0644);
  55. MODULE_PARM_DESC (my_option, "Does magic things, default 123");
  56. </programlisting>
  57. </informalexample>
  58. <para>One parameter should be supported by all V4L2 drivers, the minor
  59. number of the device it will register. Purpose is to predictably link V4L2
  60. drivers to device nodes if more than one video device is installed. Use the
  61. name of the device node followed by a "_nr" suffix, for example "video_nr"
  62. for <filename>/dev/video</filename>.</para>
  63. <informalexample>
  64. <programlisting>
  65. /* Minor number of the device, -1 to allocate the first unused. */
  66. static int video_nr = -1;
  67. module_param (video_nr, int, 0444);
  68. </programlisting>
  69. </informalexample>
  70. </section>
  71. <section id="driver-pci">
  72. <title>PCI Devices</title>
  73. <para>PCI devices are initialized like this:</para>
  74. <informalexample>
  75. <programlisting>
  76. typedef struct {
  77. /* State of one physical device. */
  78. } my_device;
  79. static int
  80. my_resume (struct pci_dev * pci_dev)
  81. {
  82. /* Restore the suspended device to working state. */
  83. }
  84. static int
  85. my_suspend (struct pci_dev * pci_dev,
  86. pm_message_t state)
  87. {
  88. /* This function is called before the system goes to sleep.
  89. Stop all DMAs and disable interrupts, then put the device
  90. into a low power state. For details see the kernel
  91. sources under <filename>Documentation/power</filename>. */
  92. return 0; /* a negative value on error, 0 on success. */
  93. }
  94. static void
  95. my_remove (struct pci_dev * pci_dev)
  96. {
  97. my_device *my = pci_get_drvdata (pci_dev);
  98. /* Describe me. */
  99. }
  100. static int
  101. my_probe (struct pci_dev * pci_dev,
  102. const struct pci_device_id * pci_id)
  103. {
  104. my_device *my;
  105. /* Describe me. */
  106. /* You can allocate per-device data here and store a pointer
  107. to it in the pci_dev structure. */
  108. my = ...;
  109. pci_set_drvdata (pci_dev, my);
  110. return 0; /* a negative value on error, 0 on success. */
  111. }
  112. /* A list of supported PCI devices. */
  113. static struct pci_device_id
  114. my_pci_device_ids [] = {
  115. { PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,
  116. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  117. { 0 } /* end of list */
  118. };
  119. /* Load our module if supported PCI devices are installed. */
  120. MODULE_DEVICE_TABLE (pci, my_pci_device_ids);
  121. static struct pci_driver
  122. my_pci_driver = {
  123. .name = "my",
  124. .id_table = my_pci_device_ids,
  125. .probe = my_probe,
  126. .remove = my_remove,
  127. /* Power management functions. */
  128. .suspend = my_suspend,
  129. .resume = my_resume,
  130. };
  131. static void
  132. my_module_exit (void)
  133. {
  134. pci_unregister_driver (&my_pci_driver);
  135. }
  136. static int
  137. my_module_init (void)
  138. {
  139. return pci_register_driver (&my_pci_driver);
  140. }
  141. </programlisting>
  142. </informalexample>
  143. </section>
  144. <section id="driver-usb">
  145. <title>USB Devices</title>
  146. <para>to do</para>
  147. </section>
  148. <section id="driver-registering">
  149. <title>Registering V4L2 Drivers</title>
  150. <para>After a V4L2 driver probed the hardware it registers one or more
  151. devices with the videodev module.</para>
  152. </section>
  153. <section id="driver-file-ops">
  154. <title>File Operations</title>
  155. <para>to do</para>
  156. </section>
  157. <section id="driver-internal-api">
  158. <title>Internal API</title>
  159. <para>to do</para>
  160. </section>
  161. -->