This is the second article in The lwn series on writing drivers for thevideo4linux2 kernel interface; those who have not yet seen
The introductory article maywish to start there. This installment will look at the overall structureof A video4linux driver and the device registration process.
Before starting, it is worth noting that there are two resources which willprove invaluable for anybody working with video drivers:
- The v4l2 API specification. this document covers the API from the user-space point of view, but, to a great extent, v4l2 drivers implement that API directly. so most of the structures are the same,
And the semantics of the v4l2 CILS are clearly laid out. Print a copy (consider cutting out the Free Documentation License text to save trees) and keep it somewhere within easy reach.
- The "Vivi" driver found in the kernel sourceDrivers/Media/Video/Vivi. c. It is a virtual driver, in that it generates test patterns and does not actually interface to any hardware. As such, it serves as a relatively clear authentication of how
V4l2 drivers shoshould be written.
To start, every v4l2 driver must include the requisite header file:
#include <linux/videodev2.h>
Much of the needed information is there. When digging through the headersas a driver author, however, you'll also want to have a lookInclude/Media/v4l2-dev.h, Which defines attributes of the structures you 'llbe working.
A video driver will probably have sections which deal with the PCI or usbbus (for example); we'll not spend much time on that part of the driverhere. there is often an internal I2C interface, which
WillBeexamined later on in this article series. Then, there is the interface tothe v4l2 subsystem. That interface is built around
Structvideo_device, Which represents a v4l2 device. Covering everythingthat goes into this structure will be the topic of several articles; HEREWE'll just have an overview.
TheNameFieldStruct video_deviceIs a name for thetype of device; it will appear in kernel log messages and in sysfs. thename usually matches the name of the driver.
There are two fields to describe what type of device is being represented. The first (Type) Looks like a holdover from the video4linux1 API; it can have one of four values:
- Vfl_type_grabberIndicates a frame grabber device-including cameras, tuners, and such.
- Vfl_type_vbiIs for devices which pull information transmitted during the video blanking interval.
- Vfl_type_radioFor radio devices.
- Vfl_type_vtxFor videotext devices.
If your device can perform more than one of the above functions, a separatev4l2 device shoshould be registered for each of the supported functions. inv4l2, however, any of the registered devices can be called upon to functionin any of the supported modes. what
It comes down to is that, for v4l2, there is really only need for a single device, but compatibility with theolder video4linux API requires that individual devices be registered foreach function.
The second field, calledType2, Is a bitmask describing thedevice's capabilities in more detail. It can contain any of the followingvalues:
- Vid_type_capture: The device can capture video data.
- Vid_type_tuner: It can tune to different frequencies.
- Vid_type_teletext: It can grab teletext data.
- Vid_type_overlay: It can overlay video data directly into the frame buffer.
- Vid_type_chromakey: A special form of overlay capability where the video data is only displayed where the underlying frame buffer contains pixels of a specific color.
- Vid_type_clipping: It can clip overlay data.
- Vid_type_frameram: It uses memory located in the frame buffer device.
- Vid_type_scales: It can scale video data.
- Vid_type_monochrome: It is a monochrome-only device.
- Vid_type_subcapture: It can capture sub-areas of the image.
- Vid_type_mpeg_decoder: It can decode MPEG streams.
- Vid_type_mpeg_encoder: It can encode MPEG streams.
- Vid_type_m1__decoder: It can decode MJPEG streams.
- Vid_type_m1__encoder: It can encode MJPEG streams.
Another field initialized by all v4l2 drivers isMinor, Which isthe desired minor number for the device. Usually this field will be set to-1, which causes the video4linux subsystem to allocate a minor number atregistration time.
There are also three distinct sets of function pointers foundStruct video_device. The first, consisting of a single function, is
Release ()Method. If a device lacksRelease ()Function, the kernel will complain (your editor was amused to note that itrefers offending programmers to an lwn Article).
Release ()Function is important: for various reasons, references toVideo_deviceStructure can remain long after that last videoapplication has closed its file descriptor. Those references can remainafter the device has been unregistered.
For this reason, it is not safe tofree the structure untilRelease ()Method has been called. So, often, this function consists of a simple
Kfree ()Call.
TheVideo_deviceStructure contains within itFile_operationsStructure with the usual function pointers. videodrivers will always need
Open ()AndRelease ()Operations; note thatThis Release ()Is called whenever the device isclosed, not when it can be freed as with the other function with the samename described above. There will often be
Read ()OrWrite ()Method, depending on whether the device performs input oroutput; note, however, that for streaming video devices, there are otherways of transferring data. Most devices which handle streaming video datawill need to implement
Poll ()AndMMAP (). AndEveryV4l2 device needsIOCTL ()Method-but they canuse
Video_ioctl2 (), Which is provided by the v4l2 subsystem.
The third set of methods, stored inVideo_deviceStructureitself, makes up the core of the v4l2 API. There are several dozen ofthem, handling various device configuration operations, streaming I/O, andmore.
Finally, a useful field to know from the beginning isDebug. Setting it to either (or both-it's a bitmask)
V4l2_debug_ioctlAndV4l2_debug_ioctl_argWill yield a fair amount of debugging outputwhich can help a befuddled programmer figure out why a driver and anapplication are failing to understand each other.
Video device registration
OnceVideo_deviceStructure has been set up, it shoshould beregistered:
int video_register_device(struct video_device *vfd, int type, int nr);
Here,VFDIs the device structure,TypeIs the same valuefound in its
TypeField, andNRIs, again, the desiredminor number (or-1 for dynamic allocation ). the return value shoshould bezero; a negative error code indicates that something went badly wrong. asalways, one shocould be aware that the device's methods
Can be calledimmediately once the device is registered; do not callVideo_register_device ()Until everything is ready to go.
A device can be unregistered:
void video_unregister_device(struct video_device *vfd);
Stay tuned for the next article in this series, which will begin to look atthe implementation of some of these methods.
Open () and release ()
Every v4l2 device will needOpen ()Method, which will have theusual prototype:
int (*open)(struct inode *inode, struct file *filp);
The first thingOpen ()Method will normally do is to locate aninternal device corresponding to the given
Inode; This is done bykeying on the minor number stored inInode. A certain amount ofinitialization can be performed med; this can also be a good time to power upthe hardware if it has a power-down option.
The v4l2 Specification defines some conventions which are relevant here. one is that, by design, all v4l2 devices can have multiple open filedescriptors at any given time. the purpose here is to allow oneapplication to display (or generate) video data while
Another one, perhaps, tweaks control values. so, while certain v4l2 operations (actually readingand writing video data, in particle) can be madeexclusive to a single file descriptor, the device as a whole shoshould supportmultiple open descriptors.
Another convention worth mentioning is thatOpen ()Methodshocould not, in general, make changes to the operating parameters currentlyset in the hardware. It shoshould be possible to run a command-line programwhich configures a camera according
A certain set of desires (resolution, video format, etc .), then run an entirely separate applicationto, for example, capture a frame from the camera. this mode wocould not workif the camera's settings were reset in the middle, so a v4l2 driver shouldendeavor
To keep existing settings until an application explicitly resetsthem.
TheRelease ()Method performs any needed cleanup. Since videodevices can have multiple open file descriptors,
Release ()Willneed to decrement a counter and check before doing anything radical. Ifthe just-closed file descriptor was being used to transfer data, it maynecessary to shut down the DMA engine and perform other cleanups.
The next installment in this series will start into the long process ofquerying device capabilities and stopping ing operating modes. Stay tuned.