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