📄 ch18s05.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>18.5. 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="第 18 章 TTY 驱动"><link rel="prev" href="ch18s04.html" title="18.4. ioctls 函数"><link rel="next" href="ch18s06.html" title="18.6. 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. TTY 设备的 proc 和 sysfs 处理</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch18s04.html">上一页</a> </td><th width="60%" align="center">第 18 章 TTY 驱动</th><td width="20%" align="right"> <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. 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 < TINY_TTY_MINORS && length < PAGE_SIZE; ++i) { tiny = tiny_table[i]; if (tiny == NULL) continue; length += sprintf(page+length, "%d\n", i); if ((length + begin) > (off + count)) goto done; if ((length + begin) < off) { begin += length; length = 0; } } *eof = 1;done: if (off >= (length + begin)) return 0; *start = page + (off-begin); return (count < 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> </td><td width="20%" align="center"><a accesskey="u" href="ch18.html">上一级</a></td><td width="40%" align="right"> <a accesskey="n" href="ch18s06.html">下一页</a></td></tr><tr><td width="40%" align="left" valign="top">18.4. ioctls 函数 </td><td width="20%" align="center"><a accesskey="h" href="index.html">起始页</a></td><td width="40%" align="right" valign="top"> 18.6. 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 + -