The Ioctl Interface

This leaves the ioctl routine, without which the driver will not be terribly useful to anyone.


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;
                }

  

VIDIOCGCAP is the first ioctl all video4linux devices must support. It allows the applications to find out what sort of a card they have found and to figure out what they want to do about it. The fields in the structure are

Table 2-2. struct video_capability fields

nameThe device text name. This is intended for the user.
channelsThe number of different channels you can tune on this card. It could even by zero for a card that has no tuning capability. For our simple FM radio it is 1. An AM/FM radio would report 2.
audiosThe number of audio inputs on this device. For our radio there is only one audio input.
minwidth,minheightThe smallest size the card is capable of capturing images in. We set these to zero. Radios do not capture pictures
maxwidth,maxheightThe largest image size the card is capable of capturing. For our radio we report 0.
typeThis reports the capabilities of the device, and matches the field we filled in in the struct video_device when registering.

Having filled in the fields, we use copy_to_user to copy the structure into the users buffer. If the copy fails we return an EFAULT to the application so that it knows it tried to feed us garbage.

The next pair of ioctl operations select which tuner is to be used and let the application find the tuner properties. We have only a single FM band tuner in our example device.


                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;
                }

  

The VIDIOCGTUNER ioctl allows applications to query a tuner. The application sets the tuner field to the tuner number it wishes to query. The query does not change the tuner that is being used, it merely enquires about the tuner in question.

We have exactly one tuner so after copying the user buffer to our temporary structure we complain if they asked for a tuner other than tuner 0.

The video_tuner structure has the following fields

Table 2-3. struct video_tuner fields

int tunerThe number of the tuner in question
char name[32]A text description of this tuner. "FM" will do fine. This is intended for the application.
u32 flagsTuner capability flags
u16 modeThe current reception mode
u16 signalThe signal strength scaled between 0 and 65535. If a device cannot tell the signal strength it should report 65535. Many simple cards contain only a signal/no signal bit. Such cards will report either 0 or 65535.
u32 rangelow, rangehigh The range of frequencies supported by the radio or TV. It is scaled according to the VIDEO_TUNER_LOW flag.

Table 2-4. struct video_tuner flags

VIDEO_TUNER_PALA PAL TV tuner
VIDEO_TUNER_NTSCAn NTSC (US) TV tuner
VIDEO_TUNER_SECAMA SECAM (French) TV tuner
VIDEO_TUNER_LOW The tuner frequency is scaled in 1/16th of a KHz steps. If not it is in 1/16th of a MHz steps
VIDEO_TUNER_NORMThe tuner can set its format
VIDEO_TUNER_STEREO_ONThe tuner is currently receiving a stereo signal

Table 2-5. struct video_tuner modes

VIDEO_MODE_PALPAL Format
VIDEO_MODE_NTSCNTSC Format (USA)
VIDEO_MODE_SECAMFrench Format
VIDEO_MODE_AUTOA device that does not need to do TV format switching

The settings for the radio card are thus fairly simple. We report that we are a tuner called "FM" for FM radio. In order to get the best tuning resolution we report VIDEO_TUNER_LOW and select tuning to 1/16th of KHz. Its unlikely our card can do that resolution but it is a fair bet the card can do better than 1/16th of a MHz. VIDEO_TUNER_LOW is appropriate to almost all radio usage.

We report that the tuner automatically handles deciding what format it is receiving - true enough as it only handles FM radio. Our example card is also incapable of detecting stereo or signal strengths so it reports a strength of 0xFFFF (maximum) and no stereo detected.

To finish off we set the range that can be tuned to be 87-108Mhz, the normal FM broadcast radio range. It is important to find out what the card is actually capable of tuning. It is easy enough to simply use the FM broadcast range. Unfortunately if you do this you will discover the FM broadcast ranges in the USA, Europe and Japan are all subtly different and some users cannot receive all the stations they wish.

The application also needs to be able to set the tuner it wishes to use. In our case, with a single tuner this is rather simple to arrange.


                case VIDIOCSTUNER:
                {
                        struct video_tuner v;
                        if(copy_from_user(&v, arg, sizeof(v)))
                                return -EFAULT;
                        if(v.tuner != 0)
                                return -EINVAL;
                        return 0;
                }

  

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. If they wanted tuner 0 then, suprisingly enough, that is the current tuner already.

The next two ioctls we need to provide are to get and set the frequency of the 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 I mentioned earlier on. Since we have VIDEO_TUNER_LOW set this will be in 1/16ths of a KHz.


static unsigned long current_freq;



                case VIDIOCGFREQ:
                        if(copy_to_user(arg, &current_freq, 
                                sizeof(unsigned long))
                                return -EFAULT;
                        return 0;

  

Querying the frequency in our case is relatively simple. Our radio card is too dumb to let us query the signal strength so we remember our setting if we know it. All we have to do is copy it to the user.


                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;
                }

  

Setting the frequency is a little more complex. We begin by copying the desired frequency into kernel space. Next we call a hardware specific routine to set the radio up. This might be as simple as some scaling and a few writes to an I/O port. For most radio cards it turns out a good deal more complicated and may involve programming things like a phase locked loop on the card. This is what documentation is for.

The final set of operations we need to provide for our radio are the volume controls. Not all radio cards can even do volume control. After all there is a perfectly good volume control on the sound card. We will assume our radio card has a simple 4 step volume control.

There are two ioctls with audio we need to support


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;
                }

  

Much like the tuner we start by copying the user structure into kernel space. Again we check if the user has asked for a valid audio input. We have only input 0 and we punt if they ask for another input.

Then we fill in the video_audio structure. This has the following format

Table 2-6. struct video_audio fields

audioThe input the user wishes to query
volumeThe volume setting on a scale of 0-65535
baseThe base level on a scale of 0-65535
trebleThe treble level on a scale of 0-65535
flagsThe features this audio device supports
nameA text name to display to the user. We picked "Radio" as it explains things quite nicely.
modeThe current reception mode for the audio We report MONO because our card is too stupid to know if it is in mono or stereo.
balanceThe stereo balance on a scale of 0-65535, 32768 is middle.
stepThe step by which the volume control jumps. This is used to help make it easy for applications to set slider behaviour.

Table 2-7. struct video_audio flags

VIDEO_AUDIO_MUTEThe audio is currently muted. We could fake this in our driver but we choose not to bother.
VIDEO_AUDIO_MUTABLEThe input has a mute option
VIDEO_AUDIO_TREBLEThe input has a treble control
VIDEO_AUDIO_BASSThe input has a base control

Table 2-8. struct video_audio modes

VIDEO_SOUND_MONOMono sound
VIDEO_SOUND_STEREOStereo sound
VIDEO_SOUND_LANG1Alternative language 1 (TV specific)
VIDEO_SOUND_LANG2Alternative language 2 (TV specific)

Having filled in the structure we copy it back to user space.

The VIDIOCSAUDIO ioctl allows the user to set the audio parameters in the video_audio stucture. The driver does its best to honour the request.


                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;
                }

  

In our case there is very little that the user can set. The volume is basically the limit. Note that we could pretend to have a mute feature by rewriting this to


                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);
                        else
                                hardware_set_volume(current_volume);
                        current_muted = v.flags & 
                                              VIDEO_AUDIO_MUTE;
                        return 0;
                }

  

This with the corresponding changes to the VIDIOCGAUDIO code to report the state 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 is questionable whether this is a good idea however. User applications can already fake this themselves and kernel space is precious.

We now have a working radio ioctl handler. So we just wrap up the function


        }
        return -ENOIOCTLCMD;
}

  

and pass the Video4Linux layer back an error so that it knows we did not understand the request we got passed.