Video4Linux Programming | ||
---|---|---|
Prev | Chapter 2. Radio Devices |
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
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
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, surprisingly 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