Path: blob/master/Documentation/DocBook/v4l/driver.xml
10821 views
<title>V4L2 Driver Programming</title>12<!-- This part defines the interface between the "videodev"3module and individual drivers. -->45<para>to do</para>6<!--7<para>V4L2 is a two-layer driver system. The top layer is the "videodev"8kernel module. When videodev initializes it registers as character device9with major number 81, and it registers a set of file operations. All V4L210drivers are really clients of videodev, which calls V4L2 drivers through11driver method functions. V4L2 drivers are also written as kernel modules.12After probing the hardware they register one or more devices with13videodev.</para>1415<section id="driver-modules">16<title>Driver Modules</title>1718<para>V4L2 driver modules must have an initialization function which is19called after the module was loaded into kernel, an exit function whis is20called before the module is removed. When the driver is compiled into the21kernel these functions called at system boot and shutdown time.</para>2223<informalexample>24<programlisting>25#include <linux/module.h>2627/* Export information about this module. For details and other useful28macros see <filename>linux/module.h</filename>. */29MODULE_DESCRIPTION("my - driver for my hardware");30MODULE_AUTHOR("Your name here");31MODULE_LICENSE("GPL");3233static void34my_module_exit (void)35{36/* Free all resources allocated by my_module_init(). */37}3839static int40my_module_init (void)41{42/* Bind the driver to the supported hardware, see43<link linkend="driver-pci"> and44<link linkend="driver-usb"> for examples. */4546return 0; /* a negative value on error, 0 on success. */47}4849/* Export module functions. */50module_init (my_module_init);51module_exit (my_module_exit);52</programlisting>53</informalexample>5455<para>Users can add parameters when kernel modules are inserted:</para>5657<informalexample>58<programlisting>59include <linux/moduleparam.h>6061static int my_option = 123;62static int my_option_array[47];6364/* Export the symbol, an int, with access permissions 0664.65See <filename>linux/moduleparam.h</filename> for other types. */66module_param (my_option, int, 0644);67module_param_array (my_option_array, int, NULL, 0644);6869MODULE_PARM_DESC (my_option, "Does magic things, default 123");70</programlisting>71</informalexample>7273<para>One parameter should be supported by all V4L2 drivers, the minor74number of the device it will register. Purpose is to predictably link V4L275drivers to device nodes if more than one video device is installed. Use the76name of the device node followed by a "_nr" suffix, for example "video_nr"77for <filename>/dev/video</filename>.</para>7879<informalexample>80<programlisting>81/* Minor number of the device, -1 to allocate the first unused. */82static int video_nr = -1;8384module_param (video_nr, int, 0444);85</programlisting>86</informalexample>87</section>8889<section id="driver-pci">90<title>PCI Devices</title>9192<para>PCI devices are initialized like this:</para>9394<informalexample>95<programlisting>96typedef struct {97/* State of one physical device. */98} my_device;99100static int101my_resume (struct pci_dev * pci_dev)102{103/* Restore the suspended device to working state. */104}105106static int107my_suspend (struct pci_dev * pci_dev,108pm_message_t state)109{110/* This function is called before the system goes to sleep.111Stop all DMAs and disable interrupts, then put the device112into a low power state. For details see the kernel113sources under <filename>Documentation/power</filename>. */114115return 0; /* a negative value on error, 0 on success. */116}117118static void __devexit119my_remove (struct pci_dev * pci_dev)120{121my_device *my = pci_get_drvdata (pci_dev);122123/* Describe me. */124}125126static int __devinit127my_probe (struct pci_dev * pci_dev,128const struct pci_device_id * pci_id)129{130my_device *my;131132/* Describe me. */133134/* You can allocate per-device data here and store a pointer135to it in the pci_dev structure. */136my = ...;137pci_set_drvdata (pci_dev, my);138139return 0; /* a negative value on error, 0 on success. */140}141142/* A list of supported PCI devices. */143static struct pci_device_id144my_pci_device_ids [] = {145{ PCI_VENDOR_ID_FOO, PCI_DEVICE_ID_BAR,146PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },147{ 0 } /* end of list */148};149150/* Load our module if supported PCI devices are installed. */151MODULE_DEVICE_TABLE (pci, my_pci_device_ids);152153static struct pci_driver154my_pci_driver = {155.name = "my",156.id_table = my_pci_device_ids,157158.probe = my_probe,159.remove = __devexit_p (my_remove),160161/* Power management functions. */162.suspend = my_suspend,163.resume = my_resume,164};165166static void167my_module_exit (void)168{169pci_unregister_driver (&my_pci_driver);170}171172static int173my_module_init (void)174{175return pci_register_driver (&my_pci_driver);176}177</programlisting>178</informalexample>179</section>180181<section id="driver-usb">182<title>USB Devices</title>183<para>to do</para>184</section>185<section id="driver-registering">186<title>Registering V4L2 Drivers</title>187188<para>After a V4L2 driver probed the hardware it registers one or more189devices with the videodev module.</para>190</section>191<section id="driver-file-ops">192<title>File Operations</title>193<para>to do</para>194</section>195<section id="driver-internal-api">196<title>Internal API</title>197<para>to do</para>198</section>199-->200201<!--202Local Variables:203mode: sgml204sgml-parent-document: "v4l2.sgml"205indent-tabs-mode: nil206End:207-->208209210