URL
https://opencores.org/ocsvn/test_project/test_project/trunk
Subversion Repositories test_project
[/] [test_project/] [trunk/] [linux_sd_driver/] [Documentation/] [DocBook/] [videobook.tmpl] - Rev 78
Go to most recent revision | Compare with Previous | Blame | View Log
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN""http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []><book id="V4LGuide"><bookinfo><title>Video4Linux Programming</title><authorgroup><author><firstname>Alan</firstname><surname>Cox</surname><affiliation><address><email>alan@redhat.com</email></address></affiliation></author></authorgroup><copyright><year>2000</year><holder>Alan Cox</holder></copyright><legalnotice><para>This documentation is free software; you can redistributeit and/or modify it under the terms of the GNU General PublicLicense as published by the Free Software Foundation; eitherversion 2 of the License, or (at your option) any laterversion.</para><para>This program is distributed in the hope that it will beuseful, but WITHOUT ANY WARRANTY; without even the impliedwarranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License for more details.</para><para>You should have received a copy of the GNU General PublicLicense along with this program; if not, write to the FreeSoftware Foundation, Inc., 59 Temple Place, Suite 330, Boston,MA 02111-1307 USA</para><para>For more details see the file COPYING in the sourcedistribution of Linux.</para></legalnotice></bookinfo><toc></toc><chapter id="intro"><title>Introduction</title><para>Parts of this document first appeared in Linux Magazine under aninety day exclusivity.</para><para>Video4Linux is intended to provide a common programming interfacefor the many TV and capture cards now on the market, as well asparallel port and USB video cameras. Radio, teletext decoders andvertical blanking data interfaces are also provided.</para></chapter><chapter id="radio"><title>Radio Devices</title><para>There are a wide variety of radio interfaces available for PC's, and theseare generally very simple to program. The biggest problem with supportingsuch devices is normally extracting documentation from the vendor.</para><para>The radio interface supports a simple set of control ioctls standardisedacross all radio and tv interfaces. It does not support read or write, whichare used for video streams. The reason radio cards do not allow you to readthe audio stream into an application is that without exception they providea connection on to a soundcard. Soundcards can be used to read the radiodata just fine.</para><sect1 id="registerradio"><title>Registering Radio Devices</title><para>The Video4linux core provides an interface for registering devices. Thefirst step in writing our radio card driver is to register it.</para><programlisting>static struct video_device my_radio{"My radio",VID_TYPE_TUNER,VID_HARDWARE_MYRADIO,radio_open.radio_close,NULL, /* no read */NULL, /* no write */NULL, /* no poll */radio_ioctl,NULL, /* no special init function */NULL /* no private data */};</programlisting><para>This declares our video4linux device driver interface. The VID_TYPE_ valuedefines what kind of an interface we are, and defines basic capabilities.</para><para>The only defined value relevant for a radio card is VID_TYPE_TUNER whichindicates that the device can be tuned. Clearly our radio is going to have someway to change channel so it is tuneable.</para><para>The VID_HARDWARE_ types are unique to each device. Numbers are assigned by<email>alan@redhat.com</email> when device drivers are going to be released. Until then youcan pull a suitably large number out of your hat and use it. 10000 should besafe for a very long time even allowing for the huge number of vendorsmaking new and different radio cards at the moment.</para><para>We declare an open and close routine, but we do not need read or write,which are used to read and write video data to or from the card itself. Aswe have no read or write there is no poll function.</para><para>The private initialise function is run when the device is registered. Inthis driver we've already done all the work needed. The final pointer is aprivate data pointer that can be used by the device driver to attach andretrieve private data structures. We set this field "priv" to NULL forthe moment.</para><para>Having the structure defined is all very well but we now need to register itwith the kernel.</para><programlisting>static int io = 0x320;int __init myradio_init(struct video_init *v){if(!request_region(io, MY_IO_SIZE, "myradio")){printk(KERN_ERR"myradio: port 0x%03X is in use.\n", io);return -EBUSY;}if(video_device_register(&my_radio, VFL_TYPE_RADIO)==-1) {release_region(io, MY_IO_SIZE);return -EINVAL;}return 0;}</programlisting><para>The first stage of the initialisation, as is normally the case, is to checkthat the I/O space we are about to fiddle with doesn't belong to some otherdriver. If it is we leave well alone. If the user gives the address of thewrong device then we will spot this. These policies will generally avoidcrashing the machine.</para><para>Now we ask the Video4Linux layer to register the device for us. We hand itour carefully designed video_device structure and also tell it which groupof devices we want it registered with. In this case VFL_TYPE_RADIO.</para><para>The types available are</para><table frame="all"><title>Device Types</title><tgroup cols="3" align="left"><tbody><row><entry>VFL_TYPE_RADIO</entry><entry>/dev/radio{n}</entry><entry>Radio devices are assigned in this block. As with all of theseselections the actual number assignment is done by the video layeraccordijng to what is free.</entry></row><row><entry>VFL_TYPE_GRABBER</entry><entry>/dev/video{n}</entry><entry>Video capture devices and also -- counter-intuitively for the name --hardware video playback devices such as MPEG2 cards.</entry></row><row><entry>VFL_TYPE_VBI</entry><entry>/dev/vbi{n}</entry><entry>The VBI devices capture the hidden lines on a television picturethat carry further information like closed caption data, teletext(primarily in Europe) and now Intercast and the ATVEC internettelevision encodings.</entry></row><row><entry>VFL_TYPE_VTX</entry><entry>/dev/vtx[n}</entry><entry>VTX is 'Videotext' also known as 'Teletext'. This is a system forsending numbered, 40x25, mostly textual page images over the hiddenlines. Unlike the /dev/vbi interfaces, this is for 'smart' decoderchips. (The use of the word smart here has to be taken in context,the smartest teletext chips are fairly dumb pieces of technology).</entry></row></tbody></tgroup></table><para>We are most definitely a radio.</para><para>Finally we allocate our I/O space so that nobody treads on us and return 0to signify general happiness with the state of the universe.</para></sect1><sect1 id="openradio"><title>Opening And Closing The Radio</title><para>The functions we declared in our video_device are mostly very simple.Firstly we can drop in what is basically standard code for open and close.</para><programlisting>static int users = 0;static int radio_open(struct video_device *dev, int flags){if(users)return -EBUSY;users++;return 0;}</programlisting><para>At open time we need to do nothing but check if someone else is also usingthe radio card. If nobody is using it we make a note that we are using it,then we ensure that nobody unloads our driver on us.</para><programlisting>static int radio_close(struct video_device *dev){users--;}</programlisting><para>At close time we simply need to reduce the user count and allow the moduleto become unloadable.</para><para>If you are sharp you will have noticed neither the open nor the closeroutines attempt to reset or change the radio settings. This is intentional.It allows an application to set up the radio and exit. It avoids a userhaving to leave an application running all the time just to listen to theradio.</para></sect1><sect1 id="ioctlradio"><title>The Ioctl Interface</title><para>This leaves the ioctl routine, without which the driver will not beterribly useful to anyone.</para><programlisting>static int radio_ioctl(struct video_device *dev, unsigned int cmd, void *arg){switch(cmd){case VIDIOCGCAP:{struct video_capability v;v.type = VID_TYPE_TUNER;v.channels = 1;v.audios = 1;v.maxwidth = 0;v.minwidth = 0;v.maxheight = 0;v.minheight = 0;strcpy(v.name, "My Radio");if(copy_to_user(arg, &v, sizeof(v)))return -EFAULT;return 0;}</programlisting><para>VIDIOCGCAP is the first ioctl all video4linux devices must support. Itallows the applications to find out what sort of a card they have found andto figure out what they want to do about it. The fields in the structure are</para><table frame="all"><title>struct video_capability fields</title><tgroup cols="2" align="left"><tbody><row><entry>name</entry><entry>The device text name. This is intended for the user.</entry></row><row><entry>channels</entry><entry>The number of different channels you can tune onthis card. It could even by zero for a card that hasno tuning capability. For our simple FM radio it is 1.An AM/FM radio would report 2.</entry></row><row><entry>audios</entry><entry>The number of audio inputs on this device. For ourradio there is only one audio input.</entry></row><row><entry>minwidth,minheight</entry><entry>The smallest size the card is capable of capturingimages in. We set these to zero. Radios do notcapture pictures</entry></row><row><entry>maxwidth,maxheight</entry><entry>The largest image size the card is capable ofcapturing. For our radio we report 0.</entry></row><row><entry>type</entry><entry>This reports the capabilities of the device, andmatches the field we filled in in the structvideo_device when registering.</entry></row></tbody></tgroup></table><para>Having filled in the fields, we use copy_to_user to copy the structure intothe users buffer. If the copy fails we return an EFAULT to the applicationso that it knows it tried to feed us garbage.</para><para>The next pair of ioctl operations select which tuner is to be used and letthe application find the tuner properties. We have only a single FM bandtuner in our example device.</para><programlisting>case VIDIOCGTUNER:{struct video_tuner v;if(copy_from_user(&v, arg, sizeof(v))!=0)return -EFAULT;if(v.tuner)return -EINVAL;v.rangelow=(87*16000);v.rangehigh=(108*16000);v.flags = VIDEO_TUNER_LOW;v.mode = VIDEO_MODE_AUTO;v.signal = 0xFFFF;strcpy(v.name, "FM");if(copy_to_user(&v, arg, sizeof(v))!=0)return -EFAULT;return 0;}</programlisting><para>The VIDIOCGTUNER ioctl allows applications to query a tuner. The applicationsets the tuner field to the tuner number it wishes to query. The query doesnot change the tuner that is being used, it merely enquires about the tunerin question.</para><para>We have exactly one tuner so after copying the user buffer to our temporarystructure we complain if they asked for a tuner other than tuner 0.</para><para>The video_tuner structure has the following fields</para><table frame="all"><title>struct video_tuner fields</title><tgroup cols="2" align="left"><tbody><row><entry>int tuner</entry><entry>The number of the tuner in question</entry></row><row><entry>char name[32]</entry><entry>A text description of this tuner. "FM" will do fine.This is intended for the application.</entry></row><row><entry>u32 flags</entry><entry>Tuner capability flags</entry></row><row><entry>u16 mode</entry><entry>The current reception mode</entry></row><row><entry>u16 signal</entry><entry>The signal strength scaled between 0 and 65535. Ifa device cannot tell the signal strength it shouldreport 65535. Many simple cards contain only asignal/no signal bit. Such cards will report either0 or 65535.</entry></row><row><entry>u32 rangelow, rangehigh</entry><entry>The range of frequencies supported by the radioor TV. It is scaled according to the VIDEO_TUNER_LOWflag.</entry></row></tbody></tgroup></table><table frame="all"><title>struct video_tuner flags</title><tgroup cols="2" align="left"><tbody><row><entry>VIDEO_TUNER_PAL</entry><entry>A PAL TV tuner</entry></row><row><entry>VIDEO_TUNER_NTSC</entry><entry>An NTSC (US) TV tuner</entry></row><row><entry>VIDEO_TUNER_SECAM</entry><entry>A SECAM (French) TV tuner</entry></row><row><entry>VIDEO_TUNER_LOW</entry><entry>The tuner frequency is scaled in 1/16th of a KHzsteps. If not it is in 1/16th of a MHz steps</entry></row><row><entry>VIDEO_TUNER_NORM</entry><entry>The tuner can set its format</entry></row><row><entry>VIDEO_TUNER_STEREO_ON</entry><entry>The tuner is currently receiving a stereo signal</entry></row></tbody></tgroup></table><table frame="all"><title>struct video_tuner modes</title><tgroup cols="2" align="left"><tbody><row><entry>VIDEO_MODE_PAL</entry><entry>PAL Format</entry></row><row><entry>VIDEO_MODE_NTSC</entry><entry>NTSC Format (USA)</entry></row><row><entry>VIDEO_MODE_SECAM</entry><entry>French Format</entry></row><row><entry>VIDEO_MODE_AUTO</entry><entry>A device that does not need to doTV format switching</entry></row></tbody></tgroup></table><para>The settings for the radio card are thus fairly simple. We report that weare a tuner called "FM" for FM radio. In order to get the best tuningresolution we report VIDEO_TUNER_LOW and select tuning to 1/16th of KHz. Itsunlikely our card can do that resolution but it is a fair bet the card cando better than 1/16th of a MHz. VIDEO_TUNER_LOW is appropriate to almost allradio usage.</para><para>We report that the tuner automatically handles deciding what format it isreceiving - true enough as it only handles FM radio. Our example card isalso incapable of detecting stereo or signal strengths so it reports astrength of 0xFFFF (maximum) and no stereo detected.</para><para>To finish off we set the range that can be tuned to be 87-108Mhz, the normalFM broadcast radio range. It is important to find out what the card isactually capable of tuning. It is easy enough to simply use the FM broadcastrange. Unfortunately if you do this you will discover the FM broadcastranges in the USA, Europe and Japan are all subtly different and some userscannot receive all the stations they wish.</para><para>The application also needs to be able to set the tuner it wishes to use. Inour case, with a single tuner this is rather simple to arrange.</para><programlisting>case VIDIOCSTUNER:{struct video_tuner v;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.tuner != 0)return -EINVAL;return 0;}</programlisting><para>We copy the user supplied structure into kernel memory so we can examine it.If the user has selected a tuner other than zero we reject the request. Ifthey wanted tuner 0 then, surprisingly enough, that is the current tuner already.</para><para>The next two ioctls we need to provide are to get and set the frequency ofthe radio. These both use an unsigned long argument which is the frequency.The scale of the frequency depends on the VIDEO_TUNER_LOW flag as Imentioned earlier on. Since we have VIDEO_TUNER_LOW set this will be in1/16ths of a KHz.</para><programlisting>static unsigned long current_freq;case VIDIOCGFREQ:if(copy_to_user(arg, &current_freq,sizeof(unsigned long))return -EFAULT;return 0;</programlisting><para>Querying the frequency in our case is relatively simple. Our radio card istoo dumb to let us query the signal strength so we remember our setting ifwe know it. All we have to do is copy it to the user.</para><programlisting>case VIDIOCSFREQ:{u32 freq;if(copy_from_user(arg, &freq,sizeof(unsigned long))!=0)return -EFAULT;if(hardware_set_freq(freq)<0)return -EINVAL;current_freq = freq;return 0;}</programlisting><para>Setting the frequency is a little more complex. We begin by copying thedesired frequency into kernel space. Next we call a hardware specific routineto set the radio up. This might be as simple as some scaling and a fewwrites to an I/O port. For most radio cards it turns out a good deal morecomplicated and may involve programming things like a phase locked loop onthe card. This is what documentation is for.</para><para>The final set of operations we need to provide for our radio are thevolume controls. Not all radio cards can even do volume control. After allthere is a perfectly good volume control on the sound card. We will assumeour radio card has a simple 4 step volume control.</para><para>There are two ioctls with audio we need to support</para><programlisting>static int current_volume=0;case VIDIOCGAUDIO:{struct video_audio v;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.audio != 0)return -EINVAL;v.volume = 16384*current_volume;v.step = 16384;strcpy(v.name, "Radio");v.mode = VIDEO_SOUND_MONO;v.balance = 0;v.base = 0;v.treble = 0;if(copy_to_user(arg. &v, sizeof(v)))return -EFAULT;return 0;}</programlisting><para>Much like the tuner we start by copying the user structure into kernelspace. Again we check if the user has asked for a valid audio input. We haveonly input 0 and we punt if they ask for another input.</para><para>Then we fill in the video_audio structure. This has the following format</para><table frame="all"><title>struct video_audio fields</title><tgroup cols="2" align="left"><tbody><row><entry>audio</entry><entry>The input the user wishes to query</entry></row><row><entry>volume</entry><entry>The volume setting on a scale of 0-65535</entry></row><row><entry>base</entry><entry>The base level on a scale of 0-65535</entry></row><row><entry>treble</entry><entry>The treble level on a scale of 0-65535</entry></row><row><entry>flags</entry><entry>The features this audio device supports</entry></row><row><entry>name</entry><entry>A text name to display to the user. We picked"Radio" as it explains things quite nicely.</entry></row><row><entry>mode</entry><entry>The current reception mode for the audioWe report MONO because our card is too stupid to know if it is inmono or stereo.</entry></row><row><entry>balance</entry><entry>The stereo balance on a scale of 0-65535, 32768 ismiddle.</entry></row><row><entry>step</entry><entry>The step by which the volume control jumps. This isused to help make it easy for applications to setslider behaviour.</entry></row></tbody></tgroup></table><table frame="all"><title>struct video_audio flags</title><tgroup cols="2" align="left"><tbody><row><entry>VIDEO_AUDIO_MUTE</entry><entry>The audio is currently muted. Wecould fake this in our driver but wechoose not to bother.</entry></row><row><entry>VIDEO_AUDIO_MUTABLE</entry><entry>The input has a mute option</entry></row><row><entry>VIDEO_AUDIO_TREBLE</entry><entry>The input has a treble control</entry></row><row><entry>VIDEO_AUDIO_BASS</entry><entry>The input has a base control</entry></row></tbody></tgroup></table><table frame="all"><title>struct video_audio modes</title><tgroup cols="2" align="left"><tbody><row><entry>VIDEO_SOUND_MONO</entry><entry>Mono sound</entry></row><row><entry>VIDEO_SOUND_STEREO</entry><entry>Stereo sound</entry></row><row><entry>VIDEO_SOUND_LANG1</entry><entry>Alternative language 1 (TV specific)</entry></row><row><entry>VIDEO_SOUND_LANG2</entry><entry>Alternative language 2 (TV specific)</entry></row></tbody></tgroup></table><para>Having filled in the structure we copy it back to user space.</para><para>The VIDIOCSAUDIO ioctl allows the user to set the audio parameters in thevideo_audio structure. The driver does its best to honour the request.</para><programlisting>case VIDIOCSAUDIO:{struct video_audio v;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.audio)return -EINVAL;current_volume = v/16384;hardware_set_volume(current_volume);return 0;}</programlisting><para>In our case there is very little that the user can set. The volume isbasically the limit. Note that we could pretend to have a mute featureby rewriting this to</para><programlisting>case VIDIOCSAUDIO:{struct video_audio v;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.audio)return -EINVAL;current_volume = v/16384;if(v.flags&VIDEO_AUDIO_MUTE)hardware_set_volume(0);elsehardware_set_volume(current_volume);current_muted = v.flags &VIDEO_AUDIO_MUTE;return 0;}</programlisting><para>This with the corresponding changes to the VIDIOCGAUDIO code to report thestate of the mute flag we save and to report the card has a mute function,will allow applications to use a mute facility with this card. It isquestionable whether this is a good idea however. User applications can alreadyfake this themselves and kernel space is precious.</para><para>We now have a working radio ioctl handler. So we just wrap up the function</para><programlisting>}return -ENOIOCTLCMD;}</programlisting><para>and pass the Video4Linux layer back an error so that it knows we did notunderstand the request we got passed.</para></sect1><sect1 id="modradio"><title>Module Wrapper</title><para>Finally we add in the usual module wrapping and the driver is done.</para><programlisting>#ifndef MODULEstatic int io = 0x300;#elsestatic int io = -1;#endifMODULE_AUTHOR("Alan Cox");MODULE_DESCRIPTION("A driver for an imaginary radio card.");module_param(io, int, 0444);MODULE_PARM_DESC(io, "I/O address of the card.");static int __init init(void){if(io==-1){printk(KERN_ERR"You must set an I/O address with io=0x???\n");return -EINVAL;}return myradio_init(NULL);}static void __exit cleanup(void){video_unregister_device(&my_radio);release_region(io, MY_IO_SIZE);}module_init(init);module_exit(cleanup);</programlisting><para>In this example we set the IO base by default if the driver is compiled intothe kernel: you can still set it using "my_radio.irq" if this file is called <filename>my_radio.c</filename>. For the module we require theuser sets the parameter. We set io to a nonsense port (-1) so that we cantell if the user supplied an io parameter or not.</para><para>We use MODULE_ defines to give an author for the card driver and adescription. We also use them to declare that io is an integer and it is theaddress of the card, and can be read by anyone from sysfs.</para><para>The clean-up routine unregisters the video_device we registered, and freesup the I/O space. Note that the unregister takes the actual video_devicestructure as its argument. Unlike the file operations structure which can beshared by all instances of a device a video_device structure as an actualinstance of the device. If you are registering multiple radio devices youneed to fill in one structure per device (most likely by setting up atemplate and copying it to each of the actual device structures).</para></sect1></chapter><chapter><title>Video Capture Devices</title><sect1 id="introvid"><title>Video Capture Device Types</title><para>The video capture devices share the same interfaces as radio devices. Inorder to explain the video capture interface I will use the example of acamera that has no tuners or audio input. This keeps the example relativelyclean. To get both combine the two driver examples.</para><para>Video capture devices divide into four categories. A little technologybackgrounder. Full motion video even at television resolution (which isactually fairly low) is pretty resource-intensive. You are continuallypassing megabytes of data every second from the capture card to the display.several alternative approaches have emerged because copying this through theprocessor and the user program is a particularly bad idea .</para><para>The first is to add the television image onto the video output directly.This is also how some 3D cards work. These basic cards can generally drop thevideo into any chosen rectangle of the display. Cards like this, whichinclude most mpeg1 cards that used the feature connector, aren't veryfriendly in a windowing environment. They don't understand windows orclipping. The video window is always on the top of the display.</para><para>Chroma keying is a technique used by cards to get around this. It is an oldtelevision mixing trick where you mark all the areas you wish to replacewith a single clear colour that isn't used in the image - TV people use anincredibly bright blue while computing people often use a particularlyvirulent purple. Bright blue occurs on the desktop. Anyone with virulentpurple windows has another problem besides their TV overlay.</para><para>The third approach is to copy the data from the capture card to the videocard, but to do it directly across the PCI bus. This relieves the processorfrom doing the work but does require some smartness on the part of the videocapture chip, as well as a suitable video card. Programming this kind ofcard and more so debugging it can be extremely tricky. There are some quitecomplicated interactions with the display and you may also have to cope withvarious chipset bugs that show up when PCI cards start talking to eachother.</para><para>To keep our example fairly simple we will assume a card that supportsoverlaying a flat rectangular image onto the frame buffer output, and whichcan also capture stuff into processor memory.</para></sect1><sect1 id="regvid"><title>Registering Video Capture Devices</title><para>This time we need to add more functions for our camera device.</para><programlisting>static struct video_device my_camera{"My Camera",VID_TYPE_OVERLAY|VID_TYPE_SCALES|\VID_TYPE_CAPTURE|VID_TYPE_CHROMAKEY,VID_HARDWARE_MYCAMERA,camera_open.camera_close,camera_read, /* no read */NULL, /* no write */camera_poll, /* no poll */camera_ioctl,NULL, /* no special init function */NULL /* no private data */};</programlisting><para>We need a read() function which is used for capturing data fromthe card, and we need a poll function so that a driver can wait for the nextframe to be captured.</para><para>We use the extra video capability flags that did not apply to theradio interface. The video related flags are</para><table frame="all"><title>Capture Capabilities</title><tgroup cols="2" align="left"><tbody><row><entry>VID_TYPE_CAPTURE</entry><entry>We support image capture</entry></row><row><entry>VID_TYPE_TELETEXT</entry><entry>A teletext capture device (vbi{n])</entry></row><row><entry>VID_TYPE_OVERLAY</entry><entry>The image can be directly overlaid onto theframe buffer</entry></row><row><entry>VID_TYPE_CHROMAKEY</entry><entry>Chromakey can be used to select which partsof the image to display</entry></row><row><entry>VID_TYPE_CLIPPING</entry><entry>It is possible to give the board a list ofrectangles to draw around. </entry></row><row><entry>VID_TYPE_FRAMERAM</entry><entry>The video capture goes into the video memoryand actually changes it. Applications needto know this so they can clean up after thecard</entry></row><row><entry>VID_TYPE_SCALES</entry><entry>The image can be scaled to various sizes,rather than being a single fixed size.</entry></row><row><entry>VID_TYPE_MONOCHROME</entry><entry>The capture will be monochrome. This isn't acomplete answer to the question since a monocamera on a colour capture card will stillproduce mono output.</entry></row><row><entry>VID_TYPE_SUBCAPTURE</entry><entry>The card allows only part of its field ofview to be captured. This enablesapplications to avoid copying all of a largeimage into memory when only some section isrelevant.</entry></row></tbody></tgroup></table><para>We set VID_TYPE_CAPTURE so that we are seen as a capture card,VID_TYPE_CHROMAKEY so the application knows it is time to draw in virulentpurple, and VID_TYPE_SCALES because we can be resized.</para><para>Our setup is fairly similar. This time we also want an interrupt linefor the 'frame captured' signal. Not all cards have this so some of themcannot handle poll().</para><programlisting>static int io = 0x320;static int irq = 11;int __init mycamera_init(struct video_init *v){if(!request_region(io, MY_IO_SIZE, "mycamera")){printk(KERN_ERR"mycamera: port 0x%03X is in use.\n", io);return -EBUSY;}if(video_device_register(&my_camera,VFL_TYPE_GRABBER)==-1) {release_region(io, MY_IO_SIZE);return -EINVAL;}return 0;}</programlisting><para>This is little changed from the needs of the radio card. We specifyVFL_TYPE_GRABBER this time as we want to be allocated a /dev/video name.</para></sect1><sect1 id="opvid"><title>Opening And Closing The Capture Device</title><programlisting>static int users = 0;static int camera_open(struct video_device *dev, int flags){if(users)return -EBUSY;if(request_irq(irq, camera_irq, 0, "camera", dev)<0)return -EBUSY;users++;return 0;}static int camera_close(struct video_device *dev){users--;free_irq(irq, dev);}</programlisting><para>The open and close routines are also quite similar. The only real change isthat we now request an interrupt for the camera device interrupt line. If wecannot get the interrupt we report EBUSY to the application and give up.</para></sect1><sect1 id="irqvid"><title>Interrupt Handling</title><para>Our example handler is for an ISA bus device. If it was PCI you would beable to share the interrupt and would have set IRQF_SHARED to indicate ashared IRQ. We pass the device pointer as the interrupt routine argument. Wedon't need to since we only support one card but doing this will make iteasier to upgrade the driver for multiple devices in the future.</para><para>Our interrupt routine needs to do little if we assume the card can simplyqueue one frame to be read after it captures it.</para><programlisting>static struct wait_queue *capture_wait;static int capture_ready = 0;static void camera_irq(int irq, void *dev_id,struct pt_regs *regs){capture_ready=1;wake_up_interruptible(&capture_wait);}</programlisting><para>The interrupt handler is nice and simple for this card as we are assumingthe card is buffering the frame for us. This means we have little to do butwake up anybody interested. We also set a capture_ready flag, as we maycapture a frame before an application needs it. In this case we need to knowthat a frame is ready. If we had to collect the frame on the interrupt lifewould be more complex.</para><para>The two new routines we need to supply are camera_read which returns aframe, and camera_poll which waits for a frame to become ready.</para><programlisting>static int camera_poll(struct video_device *dev,struct file *file, struct poll_table *wait){poll_wait(file, &capture_wait, wait);if(capture_read)return POLLIN|POLLRDNORM;return 0;}</programlisting><para>Our wait queue for polling is the capture_wait queue. This will cause thetask to be woken up by our camera_irq routine. We check capture_read to seeif there is an image present and if so report that it is readable.</para></sect1><sect1 id="rdvid"><title>Reading The Video Image</title><programlisting>static long camera_read(struct video_device *dev, char *buf,unsigned long count){struct wait_queue wait = { current, NULL };u8 *ptr;int len;int i;add_wait_queue(&capture_wait, &wait);while(!capture_ready){if(file->flags&O_NDELAY){remove_wait_queue(&capture_wait, &wait);current->state = TASK_RUNNING;return -EWOULDBLOCK;}if(signal_pending(current)){remove_wait_queue(&capture_wait, &wait);current->state = TASK_RUNNING;return -ERESTARTSYS;}schedule();current->state = TASK_INTERRUPTIBLE;}remove_wait_queue(&capture_wait, &wait);current->state = TASK_RUNNING;</programlisting><para>The first thing we have to do is to ensure that the application waits untilthe next frame is ready. The code here is almost identical to the mouse codewe used earlier in this chapter. It is one of the common building blocks ofLinux device driver code and probably one which you will find occurs in anydrivers you write.</para><para>We wait for a frame to be ready, or for a signal to interrupt our waiting. If asignal occurs we need to return from the system call so that the signal canbe sent to the application itself. We also check to see if the user actuallywanted to avoid waiting - ie if they are using non-blocking I/O and have other thingsto get on with.</para><para>Next we copy the data from the card to the user application. This is rarelyas easy as our example makes out. We will add capture_w, and capture_h hereto hold the width and height of the captured image. We assume the card onlysupports 24bit RGB for now.</para><programlisting>capture_ready = 0;ptr=(u8 *)buf;len = capture_w * 3 * capture_h; /* 24bit RGB */if(len>count)len=count; /* Doesn't all fit */for(i=0; i<len; i++){put_user(inb(io+IMAGE_DATA), ptr);ptr++;}hardware_restart_capture();return i;}</programlisting><para>For a real hardware device you would try to avoid the loop with put_user().Each call to put_user() has a time overhead checking whether the accesses to userspace are allowed. It would be better to read a line into a temporary bufferthen copy this to user space in one go.</para><para>Having captured the image and put it into user space we can kick the card toget the next frame acquired.</para></sect1><sect1 id="iocvid"><title>Video Ioctl Handling</title><para>As with the radio driver the major control interface is via the ioctl()function. Video capture devices support the same tuner calls as a radiodevice and also support additional calls to control how the video functionsare handled. In this simple example the card has no tuners to avoid makingthe code complex.</para><programlisting>static int camera_ioctl(struct video_device *dev, unsigned int cmd, void *arg){switch(cmd){case VIDIOCGCAP:{struct video_capability v;v.type = VID_TYPE_CAPTURE|\VID_TYPE_CHROMAKEY|\VID_TYPE_SCALES|\VID_TYPE_OVERLAY;v.channels = 1;v.audios = 0;v.maxwidth = 640;v.minwidth = 16;v.maxheight = 480;v.minheight = 16;strcpy(v.name, "My Camera");if(copy_to_user(arg, &v, sizeof(v)))return -EFAULT;return 0;}</programlisting><para>The first ioctl we must support and which all video capture and radiodevices are required to support is VIDIOCGCAP. This behaves exactly the sameas with a radio device. This time, however, we report the extra capabilitieswe outlined earlier on when defining our video_dev structure.</para><para>We now set the video flags saying that we support overlay, capture,scaling and chromakey. We also report size limits - our smallest image is16x16 pixels, our largest is 640x480.</para><para>To keep things simple we report no audio and no tuning capabilities at all.</para><programlisting>case VIDIOCGCHAN:{struct video_channel v;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.channel != 0)return -EINVAL;v.flags = 0;v.tuners = 0;v.type = VIDEO_TYPE_CAMERA;v.norm = VIDEO_MODE_AUTO;strcpy(v.name, "Camera Input");break;if(copy_to_user(&v, arg, sizeof(v)))return -EFAULT;return 0;}</programlisting><para>This follows what is very much the standard way an ioctl handler looksin Linux. We copy the data into a kernel space variable and we check that therequest is valid (in this case that the input is 0). Finally we copy thecamera info back to the user.</para><para>The VIDIOCGCHAN ioctl allows a user to ask about video channels (that isinputs to the video card). Our example card has a single camera input. Thefields in the structure are</para><table frame="all"><title>struct video_channel fields</title><tgroup cols="2" align="left"><tbody><row><entry>channel</entry><entry>The channel number we are selecting</entry></row><row><entry>name</entry><entry>The name for this channel. This is intendedto describe the port to the user.Appropriate names are therefore things like"Camera" "SCART input"</entry></row><row><entry>flags</entry><entry>Channel properties</entry></row><row><entry>type</entry><entry>Input type</entry></row><row><entry>norm</entry><entry>The current television encoding being usedif relevant for this channel.</entry></row></tbody></tgroup></table><table frame="all"><title>struct video_channel flags</title><tgroup cols="2" align="left"><tbody><row><entry>VIDEO_VC_TUNER</entry><entry>Channel has a tuner.</entry></row><row><entry>VIDEO_VC_AUDIO</entry><entry>Channel has audio.</entry></row></tbody></tgroup></table><table frame="all"><title>struct video_channel types</title><tgroup cols="2" align="left"><tbody><row><entry>VIDEO_TYPE_TV</entry><entry>Television input.</entry></row><row><entry>VIDEO_TYPE_CAMERA</entry><entry>Fixed camera input.</entry></row><row><entry>0</entry><entry>Type is unknown.</entry></row></tbody></tgroup></table><table frame="all"><title>struct video_channel norms</title><tgroup cols="2" align="left"><tbody><row><entry>VIDEO_MODE_PAL</entry><entry>PAL encoded Television</entry></row><row><entry>VIDEO_MODE_NTSC</entry><entry>NTSC (US) encoded Television</entry></row><row><entry>VIDEO_MODE_SECAM</entry><entry>SECAM (French) Television </entry></row><row><entry>VIDEO_MODE_AUTO</entry><entry>Automatic switching, or format does notmatter</entry></row></tbody></tgroup></table><para>The corresponding VIDIOCSCHAN ioctl allows a user to change channel and torequest the norm is changed - for example to switch between a PAL or an NTSCformat camera.</para><programlisting>case VIDIOCSCHAN:{struct video_channel v;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.channel != 0)return -EINVAL;if(v.norm != VIDEO_MODE_AUTO)return -EINVAL;return 0;}</programlisting><para>The implementation of this call in our driver is remarkably easy. Because weare assuming fixed format hardware we need only check that the user has nottried to change anything.</para><para>The user also needs to be able to configure and adjust the picture they areseeing. This is much like adjusting a television set. A user applicationalso needs to know the palette being used so that it knows how to displaythe image that has been captured. The VIDIOCGPICT and VIDIOCSPICT ioctlcalls provide this information.</para><programlisting>case VIDIOCGPICT{struct video_picture v;v.brightness = hardware_brightness();v.hue = hardware_hue();v.colour = hardware_saturation();v.contrast = hardware_brightness();/* Not settable */v.whiteness = 32768;v.depth = 24; /* 24bit */v.palette = VIDEO_PALETTE_RGB24;if(copy_to_user(&v, arg,sizeof(v)))return -EFAULT;return 0;}</programlisting><para>The brightness, hue, color, and contrast provide the picture controls thatare akin to a conventional television. Whiteness provides additionalcontrol for greyscale images. All of these values are scaled between 0-65535and have 32768 as the mid point setting. The scaling means that applicationsdo not have to worry about the capability range of the hardware but can letit make a best effort attempt.</para><para>Our depth is 24, as this is in bits. We will be returning RGB24 format. Thishas one byte of red, then one of green, then one of blue. This then repeatsfor every other pixel in the image. The other common formats the interfacedefines are</para><table frame="all"><title>Framebuffer Encodings</title><tgroup cols="2" align="left"><tbody><row><entry>GREY</entry><entry>Linear greyscale. This is for simple cameras and thelike</entry></row><row><entry>RGB565</entry><entry>The top 5 bits hold 32 red levels, the next six bitshold green and the low 5 bits hold blue. </entry></row><row><entry>RGB555</entry><entry>The top bit is clear. The red green and blue levelseach occupy five bits.</entry></row></tbody></tgroup></table><para>Additional modes are support for YUV capture formats. These are common forTV and video conferencing applications.</para><para>The VIDIOCSPICT ioctl allows a user to set some of the picture parameters.Exactly which ones are supported depends heavily on the card itself. It ispossible to support many modes and effects in software. In general doingthis in the kernel is a bad idea. Video capture is a performance-sensitiveapplication and the programs can often do better if they aren't being'helped' by an overkeen driver writer. Thus for our device we will reportRGB24 only and refuse to allow a change.</para><programlisting>case VIDIOCSPICT:{struct video_picture v;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.depth!=24 ||v.palette != VIDEO_PALETTE_RGB24)return -EINVAL;set_hardware_brightness(v.brightness);set_hardware_hue(v.hue);set_hardware_saturation(v.colour);set_hardware_brightness(v.contrast);return 0;}</programlisting><para>We check the user has not tried to change the palette or the depth. We donot want to carry out some of the changes and then return an error. This mayconfuse the application which will be assuming no change occurred.</para><para>In much the same way as you need to be able to set the picture controls toget the right capture images, many cards need to know what they aredisplaying onto when generating overlay output. In some cases getting thiswrong even makes a nasty mess or may crash the computer. For that reasonthe VIDIOCSBUF ioctl used to set up the frame buffer information may wellonly be usable by root.</para><para>We will assume our card is one of the old ISA devices with feature connectorand only supports a couple of standard video modes. Very common for oldercards although the PCI devices are way smarter than this.</para><programlisting>static struct video_buffer capture_fb;case VIDIOCGFBUF:{if(copy_to_user(arg, &capture_fb,sizeof(capture_fb)))return -EFAULT;return 0;}</programlisting><para>We keep the frame buffer information in the format the ioctl uses. Thismakes it nice and easy to work with in the ioctl calls.</para><programlisting>case VIDIOCSFBUF:{struct video_buffer v;if(!capable(CAP_SYS_ADMIN))return -EPERM;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.width!=320 && v.width!=640)return -EINVAL;if(v.height!=200 && v.height!=240&& v.height!=400&& v.height !=480)return -EINVAL;memcpy(&capture_fb, &v, sizeof(v));hardware_set_fb(&v);return 0;}</programlisting><para>The capable() function checks a user has the required capability. The Linuxoperating system has a set of about 30 capabilities indicating privilegedaccess to services. The default set up gives the superuser (uid 0) all ofthem and nobody else has any.</para><para>We check that the user has the SYS_ADMIN capability, that is they areallowed to operate as the machine administrator. We don't want anyone butthe administrator making a mess of the display.</para><para>Next we check for standard PC video modes (320 or 640 wide with eitherEGA or VGA depths). If the mode is not a standard video mode we reject it asnot supported by our card. If the mode is acceptable we save it so thatVIDIOCFBUF will give the right answer next time it is called. Thehardware_set_fb() function is some undescribed card specific function toprogram the card for the desired mode.</para><para>Before the driver can display an overlay window it needs to know where thewindow should be placed, and also how large it should be. If the cardsupports clipping it needs to know which rectangles to omit from thedisplay. The video_window structure is used to describe the way the imageshould be displayed.</para><table frame="all"><title>struct video_window fields</title><tgroup cols="2" align="left"><tbody><row><entry>width</entry><entry>The width in pixels of the desired image. The cardmay use a smaller size if this size is not available</entry></row><row><entry>height</entry><entry>The height of the image. The card may use a smallersize if this size is not available.</entry></row><row><entry>x</entry><entry> The X position of the top left of the window. Thisis in pixels relative to the left hand edge of thepicture. Not all cards can display images aligned onany pixel boundary. If the position is unsuitablethe card adjusts the image right and reduces thewidth.</entry></row><row><entry>y</entry><entry> The Y position of the top left of the window. Thisis counted in pixels relative to the top edge of thepicture. As with the width if the card cannotdisplay starting on this line it will adjust thevalues.</entry></row><row><entry>chromakey</entry><entry>The colour (expressed in RGB32 format) for thechromakey colour if chroma keying is being used. </entry></row><row><entry>clips</entry><entry>An array of rectangles that must not be drawnover.</entry></row><row><entry>clipcount</entry><entry>The number of clips in this array.</entry></row></tbody></tgroup></table><para>Each clip is a struct video_clip which has the following fields</para><table frame="all"><title>video_clip fields</title><tgroup cols="2" align="left"><tbody><row><entry>x, y</entry><entry>Co-ordinates relative to the display</entry></row><row><entry>width, height</entry><entry>Width and height in pixels</entry></row><row><entry>next</entry><entry>A spare field for the application to use</entry></row></tbody></tgroup></table><para>The driver is required to ensure it always draws in the area requested or a smaller area, and that it never draws in any of the areas that are clipped.This may well mean it has to leave alone. small areas the application wished to bedrawn.</para><para>Our example card uses chromakey so does not have to address most of theclipping. We will add a video_window structure to our global variables toremember our parameters, as we did with the frame buffer.</para><programlisting>case VIDIOCGWIN:{if(copy_to_user(arg, &capture_win,sizeof(capture_win)))return -EFAULT;return 0;}case VIDIOCSWIN:{struct video_window v;if(copy_from_user(&v, arg, sizeof(v)))return -EFAULT;if(v.width > 640 || v.height > 480)return -EINVAL;if(v.width < 16 || v.height < 16)return -EINVAL;hardware_set_key(v.chromakey);hardware_set_window(v);memcpy(&capture_win, &v, sizeof(v));capture_w = v.width;capture_h = v.height;return 0;}</programlisting><para>Because we are using Chromakey our setup is fairly simple. Mostly we have tocheck the values are sane and load them into the capture card.</para><para>With all the setup done we can now turn on the actual capture/overlay. Thisis done with the VIDIOCCAPTURE ioctl. This takes a single integer argumentwhere 0 is on and 1 is off.</para><programlisting>case VIDIOCCAPTURE:{int v;if(get_user(v, (int *)arg))return -EFAULT;if(v==0)hardware_capture_off();else{if(capture_fb.width == 0|| capture_w == 0)return -EINVAL;hardware_capture_on();}return 0;}</programlisting><para>We grab the flag from user space and either enable or disable according toits value. There is one small corner case we have to consider here. Supposethat the capture was requested before the video window or the frame bufferhad been set up. In those cases there will be unconfigured fields in ourcard data, as well as unconfigured hardware settings. We check for this case andreturn an error if the frame buffer or the capture window width is zero.</para><programlisting>default:return -ENOIOCTLCMD;}}</programlisting><para>We don't need to support any other ioctls, so if we get this far, it is timeto tell the video layer that we don't now what the user is talking about.</para></sect1><sect1 id="endvid"><title>Other Functionality</title><para>The Video4Linux layer supports additional features, including a highperformance mmap() based capture mode and capturing part of the image.These features are out of the scope of the book. You should however have enoughexample code to implement most simple video4linux devices for radio and TVcards.</para></sect1></chapter><chapter id="bugs"><title>Known Bugs And Assumptions</title><para><variablelist><varlistentry><term>Multiple Opens</term><listitem><para>The driver assumes multiple opens should not be allowed. A drivercan work around this but not cleanly.</para></listitem></varlistentry><varlistentry><term>API Deficiencies</term><listitem><para>The existing API poorly reflects compression capable devices. Thereare plans afoot to merge V4L, V4L2 and some other ideas into abetter interface.</para></listitem></varlistentry></variablelist></para></chapter><chapter id="pubfunctions"><title>Public Functions Provided</title>!Edrivers/media/video/videodev.c</chapter></book>
Go to most recent revision | Compare with Previous | Blame | View Log
