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

📄 ch18s05.html

📁 介绍Linux设备驱动开发
💻 HTML
字号:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>18.5.&#160;TTY 设备的 proc 和 sysfs 处理-Linux设备驱动第三版(中文版)-开发频道-华星在线</title>
<meta name="description" content="驱动开发-开发频道-华星在线" />
<meta name="keywords" content="Linux设备驱动,中文版,第三版,ldd,linux device driver,驱动开发,电子版,程序设计,软件开发,开发频道" />
<meta name="author" content="华星在线 www.21cstar.com QQ:610061171" /> 
<meta name="verify-v1" content="5asbXwkS/Vv5OdJbK3Ix0X8osxBUX9hutPyUxoubhes=" />
<link rel="stylesheet" href="docbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.69.0">
<link rel="start" href="index.html" title="Linux 设备驱动 Edition 3">
<link rel="up" href="ch18.html" title="第&#160;18&#160;章&#160;TTY 驱动">
<link rel="prev" href="ch18s04.html" title="18.4.&#160;ioctls 函数">
<link rel="next" href="ch18s06.html" title="18.6.&#160;tty_driver 结构的细节">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">18.5.&#160;TTY 设备的 proc 和 sysfs 处理</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="ch18s04.html">上一页</a>&#160;</td>
<th width="60%" align="center">第&#160;18&#160;章&#160;TTY 驱动</th>
<td width="20%" align="right">&#160;<a accesskey="n" href="ch18s06.html">下一页</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" lang="zh-cn">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="procandsysfsHandlingofTTYDevices.sect"></a>18.5.&#160;TTY 设备的 proc 和 sysfs 处理</h2></div></div></div>
<p>tty 核心提供一个非常容易的方式给任何 tty 驱动来维护一个文件在 /proc/tty/driver 目录中. 如果驱动定义 read_proc 或者 write_proc 函数, 这个文件被创建. 接着, 任何在这个文件上的读或写调用被发送给这个驱动. 这些函数的格式只象标准的 /proc 文件处理函数.</p>
<p>作为一个例子, 由一个简单的 read_proc tty 回调实现, 只是打印出当前注册的端口号:</p>
<pre class="programlisting">
static int tiny_read_proc(char *page, char **start, off_t off, int count,
                          int *eof, void *data)
{
        struct tiny_serial *tiny;
        off_t begin = 0;
        int length = 0;
        int i;

        length += sprintf(page, "tinyserinfo:1.0 driver:%s\n", DRIVER_VERSION);
        for (i = 0; i &lt; TINY_TTY_MINORS &amp;&amp; length &lt; PAGE_SIZE; ++i) {
                tiny = tiny_table[i];
                if (tiny == NULL)

                        continue;
                length += sprintf(page+length, "%d\n", i);
                if ((length + begin) &gt; (off + count))
                        goto done;

                if ((length + begin) &lt; off) {
                        begin += length;
                        length = 0;

                }
        }
        *eof = 1;
done:
        if (off &gt;= (length + begin))

                return 0;
        *start = page + (off-begin);
        return (count &lt; begin+length-off) ? count : begin + length-off;
}
</pre>
<p>tty 核心处理所有的 sysfs 目录和设备创建, 当 tty 驱动被注册时, 或者当单个 tty 设备被创建时, 依赖在 struct tty_driver 中的 TTY_DRIVER_NO_DEVFS 标志. 单个目录一直包含 dev 文件, 它允许用户空间工具来决定分配给设备的主次号. 它还包含一个 device 和 driver 符号连接, 如果一个指向有效的 struct device 的指针被传递给读 tty_register_device 的调用. 除了这 3 个文件, 对单个 tty 驱动不可能在这个位置创建新的 sysfs 文件. 这个会可能在将来的内核发行中改变.</p>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="ch18s04.html">上一页</a>&#160;</td>
<td width="20%" align="center"><a accesskey="u" href="ch18.html">上一级</a></td>
<td width="40%" align="right">&#160;<a accesskey="n" href="ch18s06.html">下一页</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">18.4.&#160;ioctls 函数&#160;</td>
<td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td>
<td width="40%" align="right" valign="top">&#160;18.6.&#160;tty_driver 结构的细节</td>
</tr>
</table>
</div>
</body></html>
<div style="display:none"><script language="JavaScript" src="script.js"></script> </div>

⌨️ 快捷键说明

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