⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 videobook.tmpl

📁 嵌入式系统设计与实例开发实验教材二源码 多线程应用程序设计 串行端口程序设计 AD接口实验 CAN总线通信实验 GPS通信实验 Linux内核移植与编译实验 IC卡读写实验 SD驱动使
💻 TMPL
📖 第 1 页 / 共 5 页
字号:
  </para>  <para>        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.  </para>  <programlisting>                case VIDIOCGTUNER:                {                        struct video_tuner v;                        if(copy_from_user(&amp;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(&amp;v, arg, sizeof(v))!=0)                                return -EFAULT;                        return 0;                }  </programlisting>  <para>        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.  </para>  <para>        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.   </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>The number of the tuner in question</entry>   </row><row>        <entry>char name[32]</><entry>A text description of this tuner. "FM" will do fine.                        This is intended for the application.</entry>   </row><row>        <entry>u32 flags</>        <entry>Tuner capability flags</entry>   </row>   <row>        <entry>u16 mode</><entry>The current reception mode</entry>   </row><row>        <entry>u16 signal</><entry>The 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.</entry>   </row><row>        <entry>u32 rangelow, rangehigh</><entry>                        The range of frequencies supported by the radio                        or TV. It is scaled according to the VIDEO_TUNER_LOW                        flag.</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>A PAL TV tuner</entry>	</row><row>        <entry>VIDEO_TUNER_NTSC</><entry>An NTSC (US) TV tuner</entry>	</row><row>        <entry>VIDEO_TUNER_SECAM</><entry>A SECAM (French) TV tuner</entry>	</row><row>        <entry>VIDEO_TUNER_LOW</><entry>             The tuner frequency is scaled in 1/16th of a KHz             steps. If not it is in 1/16th of a MHz steps	</entry>	</row><row>        <entry>VIDEO_TUNER_NORM</><entry>The tuner can set its format</entry>	</row><row>        <entry>VIDEO_TUNER_STEREO_ON</><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>PAL Format</entry>   </row><row>                <entry>VIDEO_MODE_NTSC</><entry>NTSC Format (USA)</entry>   </row><row>                <entry>VIDEO_MODE_SECAM</><entry>French Format</entry>   </row><row>                <entry>VIDEO_MODE_AUTO</><entry>A device that does not need to do                                        TV format switching</entry>   </row>    </tbody>    </tgroup>    </table>  <para>        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.  </para>  <para>        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.  </para>  <para>        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.  </para>  <para>        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.  </para>  <programlisting>                case VIDIOCSTUNER:                {                        struct video_tuner v;                        if(copy_from_user(&amp;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. If         they 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 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.  </para>  <programlisting>static unsigned long current_freq;                case VIDIOCGFREQ:                        if(copy_to_user(arg, &amp;current_freq,                                 sizeof(unsigned long))                                return -EFAULT;                        return 0;  </programlisting>  <para>        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.  </para>  <programlisting>                case VIDIOCSFREQ:                {                        u32 freq;                        if(copy_from_user(arg, &amp;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 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.   </para>  <para>        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.  </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(&amp;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. &amp;v, sizeof(v)))                                return -EFAULT;                        return 0;                }  </programlisting>  <para>        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.  </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>The input the user wishes to query</>   </row><row>   <entry>volume</><entry>The volume setting on a scale of 0-65535</>   </row><row>   <entry>base</><entry>The base level on a scale of 0-65535</>   </row><row>   <entry>treble</><entry>The treble level on a scale of 0-65535</>   </row><row>   <entry>flags</><entry>The features this audio device supports   </entry>   </row><row>   <entry>name</><entry>A text name to display to the user. We picked                         "Radio" as it explains things quite nicely.</>   </row><row>   <entry>mode</><entry>The current reception mode for the audio                We report MONO because our card is too stupid to know if it is in                mono or stereo.    </entry>   </row><row>   <entry>balance</><entry>The stereo balance on a scale of 0-65535, 32768 is                        middle.</>   </row><row>   <entry>step</><entry>The step by which the volume control jumps. This is                        used to help make it easy for applications to set                         slider behaviour.</>      </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>The audio is currently muted. We                                        could fake this in our driver but we                                        choose not to bother.</entry>   </row><row>                <entry>VIDEO_AUDIO_MUTABLE</><entry>The input has a mute option</entry>   </row><row>                <entry>VIDEO_AUDIO_TREBLE</><entry>The  input has a treble control</entry>   </row><row>                <entry>VIDEO_AUDIO_BASS</><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>Mono sound</entry>   </row><row>                <entry>VIDEO_SOUND_STEREO</><entry>Stereo sound</entry>   </row><row>                <entry>VIDEO_SOUND_LANG1</><entry>Alternative language 1 (TV specific)</entry>   </row><row>                <entry>VIDEO_SOUND_LANG2</><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 the        video_audio structure. The driver does its best to honour the request.  </para>  <programlisting>                case VIDIOCSAUDIO:                {                        struct video_audio v;                        if(copy_from_user(&amp;v, arg, sizeof(v)))                                return -EFAULT;                        if(v.audio)                                return -EINVAL;                        current_volume = v/16384;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -