1 <title>V4L2 Driver Programming</title>
3 <!-- This part defines the interface between the "videodev"
4 module and individual drivers. -->
8 <para>V4L2 is a two-layer driver system. The top layer is the "videodev"
9 kernel module. When videodev initializes it registers as character device
10 with major number 81, and it registers a set of file operations. All V4L2
11 drivers are really clients of videodev, which calls V4L2 drivers through
12 driver method functions. V4L2 drivers are also written as kernel modules.
13 After probing the hardware they register one or more devices with
16 <section id="driver-modules">
17 <title>Driver Modules</title>
19 <para>V4L2 driver modules must have an initialization function which is
20 called after the module was loaded into kernel, an exit function whis is
21 called before the module is removed. When the driver is compiled into the
22 kernel these functions called at system boot and shutdown time.</para>
26 #include <linux/module.h>
28 /* Export information about this module. For details and other useful
29 macros see <filename>linux/module.h</filename>. */
30 MODULE_DESCRIPTION("my - driver for my hardware");
31 MODULE_AUTHOR("Your name here");
32 MODULE_LICENSE("GPL");
37 /* Free all resources allocated by my_module_init(). */
43 /* Bind the driver to the supported hardware, see
44 <link linkend="driver-pci"> and
45 <link linkend="driver-usb"> for examples. */
47 return 0; /* a negative value on error, 0 on success. */
50 /* Export module functions. */
51 module_init (my_module_init);
52 module_exit (my_module_exit);
56 <para>Users can add parameters when kernel modules are inserted:</para>
60 include <linux/moduleparam.h>
62 static int my_option = 123;
63 static int my_option_array[47];
65 /* Export the symbol, an int, with access permissions 0664.
66 See <filename>linux/moduleparam.h</filename> for other types. */
67 module_param (my_option, int, 0644);
68 module_param_array (my_option_array, int, NULL, 0644);
70 MODULE_PARM_DESC (my_option, "Does magic things, default 123");
74 <para>One parameter should be supported by all V4L2 drivers, the minor
75 number of the device it will register. Purpose is to predictably link V4L2
76 drivers to device nodes if more than one video device is installed. Use the
77 name of the device node followed by a "_nr" suffix, for example "video_nr"
78 for <filename>/dev/video</filename>.</para>
82 /* Minor number of the device, -1 to allocate the first unused. */
83 static int video_nr = -1;
85 module_param (video_nr, int, 0444);
90 <section id="driver-pci">
91 <title>PCI Devices</title>
93 <para>PCI devices are initialized like this:</para>
98 /* State of one physical device. */
102 my_resume (struct pci_dev * pci_dev)
104 /* Restore the suspended device to working state. */
108 my_suspend (struct pci_dev * pci_dev,
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>. */
116 return 0; /* a negative value on error, 0 on success. */
119 static void __devexit
120 my_remove (struct pci_dev * pci_dev)
122 my_device *my = pci_get_drvdata (pci_dev);
128 my_probe (struct pci_dev * pci_dev,
129 const struct pci_device_id * pci_id)
135 /* You can allocate per-device data here and store a pointer
136 to it in the pci_dev structure. */
138 pci_set_drvdata (pci_dev, my);
140 return 0; /* a negative value on error, 0 on success. */
143 /* A list of supported PCI devices. */
144 static struct pci_device_id
145 my_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 */
151 /* Load our module if supported PCI devices are installed. */
152 MODULE_DEVICE_TABLE (pci, my_pci_device_ids);
154 static struct pci_driver
157 .id_table = my_pci_device_ids,
160 .remove = __devexit_p (my_remove),
162 /* Power management functions. */
163 .suspend = my_suspend,
168 my_module_exit (void)
170 pci_unregister_driver (&my_pci_driver);
174 my_module_init (void)
176 return pci_register_driver (&my_pci_driver);
182 <section id="driver-usb">
183 <title>USB Devices</title>
186 <section id="driver-registering">
187 <title>Registering V4L2 Drivers</title>
189 <para>After a V4L2 driver probed the hardware it registers one or more
190 devices with the videodev module.</para>
192 <section id="driver-file-ops">
193 <title>File Operations</title>
196 <section id="driver-internal-api">
197 <title>Internal API</title>
205 sgml-parent-document: "v4l2.sgml"
206 indent-tabs-mode: nil