?? ch14s05.html
字號:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>14.5. 類-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="ch14.html" title="第 14 章 Linux 設備模型">
<link rel="prev" href="ch14s04.html" title="14.4. 總線, 設備, 和驅動">
<link rel="next" href="ch14s06.html" title="14.6. 集成起來">
</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">14.5. 類</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="ch14s04.html">上一頁</a> </td>
<th width="60%" align="center">第 14 章 Linux 設備模型</th>
<td width="20%" align="right"> <a accesskey="n" href="ch14s06.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="Classes.sect1"></a>14.5. 類</h2></div></div></div>
<p>我們在本章中要考察最后的設備模型概念是類.一個類是一個設備的高級視圖, 它抽象出低級的實現細節. 驅動可以見到一個SCSI 磁盤或者一個 ATA 磁盤, 在類的級別, 它們都是磁盤. 類允許用戶空間基于它們做什么來使用設備, 而不是它們如何被連接或者它們如何工作.</p>
<p>幾乎所有的類都在 sysfs 中在 /sys/class 下出現. 因此, 例如, 所有的網絡接口可在 /sys/class/net 下發現, 不管接口類型. 輸入設備可在 /sys/class/input 下, 以及串行設備在 /sys/class/tty. 一個例外是塊設備, 由于歷史的原因在 /sys/block. </p>
<p>類成員關系常常由高級的代碼處理, 不必要驅動的明確的支持. 當 sbull 驅動( 見 16 章) 創建一個虛擬磁盤設備, 它自動出現在 /sys/block. snull 網絡驅動(見 17 章)沒有做任何特殊事情給它的接口在 /sys/class/net 中出現. 將有多次, 但是, 當驅動結束直接處理類.</p>
<p>在許多情況, 類子系統是最好的輸出信息到用戶空間的方法. 當一個子系統創建一個類, 它完全擁有這個類, 因此沒有必要擔心哪個模塊擁有那里發現的屬性. 它也用極少的時間徘徊于更加面向硬件的 sysfs 部分來了解, 它不是一個直接瀏覽的好地方. 用戶會更加高興地在 /sys/class/some-widget 中發現信息, 而不是, /sys/device/pci0000:00/0000:00:10.0/usb2/2-0:1.0.</p>
<p>驅動核心輸出 2 個清晰的接口來管理類. class_simple 函數設計來盡可能容易地添加新類到系統. 它們的主要目的, 常常, 是暴露包含設備號的屬性來使能設備節點的自動創建. 常用的類接口更加復雜但是同時提供更多特性. 我們從簡單版本開始.</p>
<div class="sect2" lang="zh-cn">
<div class="titlepage"><div><div><h3 class="title">
<a name="TheclasssimpleInterface.sect2"></a>14.5.1. class_simple 接口</h3></div></div></div>
<p>class_simple 接口意圖是易于使用, 以至于沒人會抱怨沒有暴露至少一個包含設備的被分配的號的屬性. 使用這個接口只不過是一對函數調用, 沒有通常的和 Linux 設備模型關聯的樣板.</p>
<p>第一步是創建類自身. 使用一個對 class_simple_create 的調用來完成:</p>
<pre class="programlisting">
struct class_simple *class_simple_create(struct module *owner, char *name);
</pre>
<p>這個函數使用給定的名子創建一個類. 這個操作可能失敗, 當然, 因此在繼續之前返回值應當一直被檢查( 使用 IS_ERR, 在第 1 章的"指針和錯誤值"一節中描述過).</p>
<p>一個簡單的類可被銷毀, 使用:</p>
<pre class="programlisting">
void class_simple_destroy(struct class_simple *cs);
</pre>
<p>創建一個簡單類的真實目的是添加設備給它; 這個任務使用:</p>
<pre class="programlisting">
struct class_device *class_simple_device_add(struct class_simple *cs, dev_t devnum, struct device *device, const char *fmt, ...);
</pre>
<p>這里, cs 是之前創建的簡單類, devnum 是分配的設備號, device 是代表這個設備的 struct device, 其他的參數是一個 printk-風格 的格式串和參數來創建設備名子. 這個調用添加一項到類, 包含一個屬性, dev, 含有設備號. 如果設備參數是非 NULL, 一個符號連接( 稱為 device )指向在 /sys/devices 下的設備的入口.</p>
<p>可能添加其他的屬性到設備入口. 它只是使用 class_device_create_file, 我們在下一節和完整類子系統所剩下的內容討論.</p>
<p>當設備進出時類產生熱插拔事件. 如果你的驅動需要添加變量到環境中給用戶空間事件處理者, 可以建立一個熱插拔回調, 使用:</p>
<pre class="programlisting">
int class_simple_set_hotplug(struct class_simple *cs,
int (*hotplug)(struct class_device *dev,
char **envp, int num_envp,
char *buffer, int buffer_size));
</pre>
<p>當你的設備離開時, 類入口應當被去除, 使用:</p>
<pre class="programlisting">
void class_simple_device_remove(dev_t dev);
</pre>
<p>注意, 由 class_simple_device_add 返回的 class_device 結構這里不需要; 設備號(它當然應當是唯一的)足夠了.</p>
</div>
<div class="sect2" lang="zh-cn">
<div class="titlepage"><div><div><h3 class="title">
<a name="TheFullClassInterface.sect2"></a>14.5.2. 完整的類接口</h3></div></div></div>
<p>class_simple 接口滿足許多需要, 但是有時需要更多靈活性. 下面的討論描述如何使用完整的類機制, class_simple 正是基于此. 它是簡短的: 類函數和結構遵循設備模型其他部分相同的模式, 因此這里沒有什么真正是新的.</p>
<div class="sect3" lang="zh-cn">
<div class="titlepage"><div><div><h4 class="title">
<a name="Managingclasses.sect3"></a>14.5.2.1. 管理類</h4></div></div></div>
<p>一個類由一個 struct class 的實例來定義:</p>
<pre class="programlisting">
struct class {
char *name;
struct class_attribute *class_attrs;
struct class_device_attribute *class_dev_attrs;
int (*hotplug)(struct class_device *dev, char **envp,
int num_envp, char *buffer, int buffer_size);
void (*release)(struct class_device *dev);
void (*class_release)(struct class *class);
/* Some fields omitted */
};
</pre>
<p>每個類需要一個唯一的名子, 它是這個類如何在 /sys/class 中出現. 當這個類被注冊, 由 class_attrs 所指向的數組中列出的所有屬性被創建. 還有一套缺省屬性給每個添加到類中的設備; class_dev_attrs 指向它們. 有通常的熱插拔函數來添加變量到環境中, 當事件產生時. 還有 2 個釋放方法: release 在無論何時從類中去除一個設備時被調用, 而 class_release 在類自己被釋放時調用.</p>
<p>注冊函數是:</p>
<pre class="programlisting">
int class_register(struct class *cls);
void class_unregister(struct class *cls);
</pre>
<p>使用屬性的接口不應當在這點嚇人:</p>
<pre class="programlisting">
struct class_attribute {
struct attribute attr;
ssize_t (*show)(struct class *cls, char *buf);
ssize_t (*store)(struct class *cls, const char *buf, size_t count);
};
CLASS_ATTR(name, mode, show, store);
int class_create_file(struct class *cls, const struct class_attribute *attr);
void class_remove_file(struct class *cls, const struct class_attribute *attr);
</pre>
</div>
<div class="sect3" lang="zh-cn">
<div class="titlepage"><div><div><h4 class="title">
<a name="Classdevices.sect3"></a>14.5.2.2. 類設備</h4></div></div></div>
<p>一個類的真正目的是作為一個是該類成員的設備的容器. 一個成員由 struct class_device 來表示:</p>
<pre class="programlisting">
struct class_device {
struct kobject kobj;
struct class *class;
struct device *dev;
void *class_data;
char class_id[BUS_ID_SIZE];
};
</pre>
<p>class_id 成員持有設備名子, 如同它在 sysfs 中的一樣. class 指針應當指向持有這個設備的類, 并且 dev 應當指向關聯的設備結構. 設置 dev 是可選的; 如果它是非 NULL, 它用來創建一個符號連接從類入口到對應的在 /sys/devices 下的入口, 使得易于在用戶空間找到設備入口. 類可以使用 class_data 來持有一個私有指針.</p>
<p>通常的注冊函數已經被提供:</p>
<pre class="programlisting">
int class_device_register(struct class_device *cd);
void class_device_unregister(struct class_device *cd);
</pre>
<p>類設備接口也允許重命名一個已經注冊的入口:</p>
<pre class="programlisting">
int class_device_rename(struct class_device *cd, char *new_name);
</pre>
<p>類設備入口有屬性:</p>
<pre class="programlisting">
struct class_device_attribute {
struct attribute attr;
ssize_t (*show)(struct class_device *cls, char *buf);
ssize_t (*store)(struct class_device *cls, const char *buf,
size_t count);
};
CLASS_DEVICE_ATTR(name, mode, show, store);
int class_device_create_file(struct class_device *cls, const struct class_device_attribute *attr);
void class_device_remove_file(struct class_device *cls, const struct class_device_attribute *attr);
</pre>
<p>一個缺省的屬性集合, 在類的 class_dev_attrs 成員, 被創建當類設備被注冊時; class_device_create_file 可用來創建額外的屬性. 屬性還可以被加入到由 class_simple 接口創建的類設備.</p>
</div>
<div class="sect3" lang="zh-cn">
<div class="titlepage"><div><div><h4 class="title">
<a name="Classinterfaces.sect3"></a>14.5.2.3. 類接口</h4></div></div></div>
<p>類子系統有一個額外的在 Linux 設備模型其他部分找不到的概念. 這個機制稱為一個接口, 但是它是, 也許, 最好作為一種觸發機制可用來在設備進入或離開類時得到通知.</p>
<p>一個接口被表示, 使用:</p>
<pre class="programlisting">
struct class_interface {
struct class *class;
int (*add) (struct class_device *cd);
void (*remove) (struct class_device *cd);
};
</pre>
<p>接口可被注冊或注銷, 使用:</p>
<pre class="programlisting">
int class_interface_register(struct class_interface *intf);
void class_interface_unregister(struct class_interface *intf);
</pre>
<p>一個接口的功能是簡單明了的. 無論何時一個類設備被加入到在 class_interface 結構中指定的類時, 接口的 add 函數被調用. 這個函數可進行任何額外的這個設備需要的設置; 這個設置常常采取增加更多屬性的形式, 但是其他的應用都可能. 當設備被從類中去除, remove 方法被調用來進行任何需要的清理.</p>
<p>可注冊多個接口給一個類.</p>
</div>
</div>
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="ch14s04.html">上一頁</a> </td>
<td width="20%" align="center"><a accesskey="u" href="ch14.html">上一級</a></td>
<td width="40%" align="right"> <a accesskey="n" href="ch14s06.html">下一頁</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">14.4. 總線, 設備, 和驅動 </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">起始頁</a></td>
<td width="40%" align="right" valign="top"> 14.6. 集成起來</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 + -