## 6.7.?快速參考
本章介紹了下面的符號和頭文件:
~~~
#include <linux/ioctl.h>
~~~
聲明用來定義 ioctl 命令的宏定義. 當前被 <linux/fs.h> 包含.
~~~
_IOC_NRBITS
_IOC_TYPEBITS
_IOC_SIZEBITS
_IOC_DIRBITS
~~~
ioctl 命令的不同位段所使用的位數. 還有 4 個宏來指定 MASK 和 4 個指定 SHIFT, 但是它們主要是給內部使用. _IOC_SIZEBIT 是一個要檢查的重要的值, 因為它跨體系改變.
~~~
_IOC_NONE
_IOC_READ
_IOC_WRITE
~~~
"方向"位段可能的值. "read" 和 "write" 是不同的位并且可相或來指定 read/write. 這些值是基于 0 的.
~~~
_IOC(dir,type,nr,size)
_IO(type,nr)
_IOR(type,nr,size)
_IOW(type,nr,size)
_IOWR(type,nr,size)
~~~
用來創建 ioclt 命令的宏定義.
~~~
_IOC_DIR(nr)
_IOC_TYPE(nr)
_IOC_NR(nr)
_IOC_SIZE(nr)
~~~
用來解碼一個命令的宏定義. 特別地, _IOC_TYPE(nr) 是 _IOC_READ 和 _IOC_WRITE 的 OR 結合.
~~~
#include <asm/uaccess.h>
int access_ok(int type, const void *addr, unsigned long size);
~~~
檢查一個用戶空間的指針是可用的. access_ok 返回一個非零值, 如果應當允許存取.
~~~
VERIFY_READ
VERIFY_WRITE
~~~
access_ok 中 type 參數的可能取值. VERIFY_WRITE 是 VERIFY_READ 的超集.
~~~
#include <asm/uaccess.h>
int put_user(datum,ptr);
int get_user(local,ptr);
int __put_user(datum,ptr);
int __get_user(local,ptr);
~~~
用來存儲或獲取一個數據到或從用戶空間的宏. 傳送的字節數依賴 sizeof(*ptr). 常規的版本調用 access_ok , 而常規版本( __put_user 和 __get_user ) 假定 access_ok 已經被調用了.
~~~
#include <linux/capability.h>
~~~
定義各種 CAP_ 符號, 描述一個用戶空間進程可有的能力.
~~~
int capable(int capability);
~~~
返回非零值如果進程有給定的能力.
~~~
#include <linux/wait.h>
typedef struct { /* ... */ } wait_queue_head_t;
void init_waitqueue_head(wait_queue_head_t *queue);
DECLARE_WAIT_QUEUE_HEAD(queue);
~~~
Linux 等待隊列的定義類型. 一個 wait_queue_head_t 必須被明確在運行時使用 init_waitqueue_head 或者編譯時使用 DEVLARE_WAIT_QUEUE_HEAD 進行初始化.
~~~
void wait_event(wait_queue_head_t q, int condition);
int wait_event_interruptible(wait_queue_head_t q, int condition);
int wait_event_timeout(wait_queue_head_t q, int condition, int time);
int wait_event_interruptible_timeout(wait_queue_head_t q, int condition,int time);
~~~
使進程在給定隊列上睡眠, 直到給定條件值為真值.
~~~
void wake_up(struct wait_queue **q);
void wake_up_interruptible(struct wait_queue **q);
void wake_up_nr(struct wait_queue **q, int nr);
void wake_up_interruptible_nr(struct wait_queue **q, int nr);
void wake_up_all(struct wait_queue **q);
void wake_up_interruptible_all(struct wait_queue **q);
void wake_up_interruptible_sync(struct wait_queue **q);
~~~
喚醒在隊列 q 上睡眠的進程. _interruptible 的形式只喚醒可中斷的進程. 正常地, 只有一個互斥等待者被喚醒, 但是這個行為可被 _nr 或者 _all 形式所改變. _sync 版本在返回之前不重新調度 CPU.
~~~
#include <linux/sched.h>
set_current_state(int state);
~~~
設置當前進程的執行狀態. TASK_RUNNING 意味著它已經運行, 而睡眠狀態是 TASK_INTERRUPTIBLE 和 TASK_UNINTERRUPTIBLE.
~~~
void schedule(void);
~~~
選擇一個可運行的進程從運行隊列中. 被選中的進程可是當前進程或者另外一個.
~~~
typedef struct { /* ... */ } wait_queue_t;
init_waitqueue_entry(wait_queue_t *entry, struct task_struct *task);
~~~
wait_queue_t 類型用來放置一個進程到一個等待隊列.
~~~
void prepare_to_wait(wait_queue_head_t *queue, wait_queue_t *wait, int state);
void prepare_to_wait_exclusive(wait_queue_head_t *queue, wait_queue_t *wait, int state);
void finish_wait(wait_queue_head_t *queue, wait_queue_t *wait);
~~~
幫忙函數, 可用來編碼一個手工睡眠.
~~~
void sleep_on(wiat_queue_head_t *queue);
void interruptible_sleep_on(wiat_queue_head_t *queue);
~~~
老式的不推薦的函數, 它們無條件地使當前進程睡眠.
~~~
#include <linux/poll.h>
void poll_wait(struct file *filp, wait_queue_head_t *q, poll_table *p);
~~~
將當前進程放入一個等待隊列, 不立刻調度. 它被設計來被設備驅動的 poll 方法使用.
~~~
int fasync_helper(struct inode *inode, struct file *filp, int mode, struct fasync_struct **fa);
~~~
一個"幫忙者", 來實現 fasync 設備方法. mode 參數是傳遞給方法的相同的值, 而 fa 指針指向一個設備特定的 fasync_struct *.
~~~
void kill_fasync(struct fasync_struct *fa, int sig, int band);
~~~
如果這個驅動支持異步通知, 這個函數可用來發送一個信號到登記在 fa 中的進程.
~~~
int nonseekable_open(struct inode *inode, struct file *filp);
loff_t no_llseek(struct file *file, loff_t offset, int whence);
~~~
nonseekable_open 應當在任何不支持移位的設備的 open 方法中被調用. 這樣的設備應當使用 no_llseek 作為它們的 llseek 方法.
- Linux設備驅動第三版
- 第 1 章 設備驅動簡介
- 1.1. 驅動程序的角色
- 1.2. 劃分內核
- 1.3. 設備和模塊的分類
- 1.4. 安全問題
- 1.5. 版本編號
- 1.6. 版權條款
- 1.7. 加入內核開發社團
- 1.8. 本書的內容
- 第 2 章 建立和運行模塊
- 2.1. 設置你的測試系統
- 2.2. Hello World 模塊
- 2.3. 內核模塊相比于應用程序
- 2.4. 編譯和加載
- 2.5. 內核符號表
- 2.6. 預備知識
- 2.7. 初始化和關停
- 2.8. 模塊參數
- 2.9. 在用戶空間做
- 2.10. 快速參考
- 第 3 章 字符驅動
- 3.1. scull 的設計
- 3.2. 主次編號
- 3.3. 一些重要數據結構
- 3.4. 字符設備注冊
- 3.5. open 和 release
- 3.6. scull 的內存使用
- 3.7. 讀和寫
- 3.8. 使用新設備
- 3.9. 快速參考
- 第 4 章 調試技術
- 4.1. 內核中的調試支持
- 4.2. 用打印調試
- 4.3. 用查詢來調試
- 4.4. 使用觀察來調試
- 4.5. 調試系統故障
- 4.6. 調試器和相關工具
- 第 5 章 并發和競爭情況
- 5.1. scull 中的缺陷
- 5.2. 并發和它的管理
- 5.3. 旗標和互斥體
- 5.4. Completions 機制
- 5.5. 自旋鎖
- 5.6. 鎖陷阱
- 5.7. 加鎖的各種選擇
- 5.8. 快速參考
- 第 6 章 高級字符驅動操作
- 6.1. ioctl 接口
- 6.2. 阻塞 I/O
- 6.3. poll 和 select
- 6.4. 異步通知
- 6.5. 移位一個設備
- 6.6. 在一個設備文件上的存取控制
- 6.7. 快速參考
- 第 7 章 時間, 延時, 和延后工作
- 7.1. 測量時間流失
- 7.2. 獲知當前時間
- 7.3. 延后執行
- 7.4. 內核定時器
- 7.5. Tasklets 機制
- 7.6. 工作隊列
- 7.7. 快速參考
- 第 8 章 分配內存
- 8.1. kmalloc 的真實故事
- 8.2. 后備緩存
- 8.3. get_free_page 和其友
- 8.4. 每-CPU 的變量
- 8.5. 獲得大量緩沖
- 8.6. 快速參考
- 第 9 章 與硬件通訊
- 9.1. I/O 端口和 I/O 內存
- 9.2. 使用 I/O 端口
- 9.3. 一個 I/O 端口例子
- 9.4. 使用 I/O 內存
- 9.5. 快速參考
- 第 10 章 中斷處理
- 10.1. 準備并口
- 10.2. 安裝一個中斷處理
- 10.3. 前和后半部
- 10.4. 中斷共享
- 10.5. 中斷驅動 I/O
- 10.6. 快速參考
- 第 11 章 內核中的數據類型
- 11.1. 標準 C 類型的使用
- 11.2. 安排一個明確大小給數據項
- 11.3. 接口特定的類型
- 11.4. 其他移植性問題
- 11.5. 鏈表
- 11.6. 快速參考
- 第 12 章 PCI 驅動
- 12.1. PCI 接口
- 12.2. 回顧: ISA
- 12.3. PC/104 和 PC/104+
- 12.4. 其他的 PC 總線
- 12.5. SBus
- 12.6. NuBus 總線
- 12.7. 外部總線
- 12.8. 快速參考
- 第 13 章 USB 驅動
- 13.1. USB 設備基礎知識
- 13.2. USB 和 sysfs
- 13.3. USB 的 Urbs
- 13.4. 編寫一個 USB 驅動
- 13.5. 無 urb 的 USB 傳送
- 13.6. 快速參考
- 第 14 章 Linux 設備模型
- 14.1. Kobjects, Ksets 和 Subsystems
- 14.2. 低級 sysfs 操作
- 14.3. 熱插拔事件產生
- 14.4. 總線, 設備, 和驅動
- 14.5. 類
- 14.6. 集成起來
- 14.7. 熱插拔
- 14.8. 處理固件
- 14.9. 快速參考
- 第 15 章 內存映射和 DMA
- 15.1. Linux 中的內存管理
- 15.2. mmap 設備操作
- 15.3. 進行直接 I/O
- 15.4. 直接內存存取
- 15.5. 快速參考
- 第 16 章 塊驅動
- 16.1. 注冊
- 16.2. 塊設備操作
- 16.3. 請求處理
- 16.4. 一些其他的細節
- 16.5. 快速參考
- 第 17 章 網絡驅動
- 17.1. snull 是如何設計的
- 17.2. 連接到內核
- 17.3. net_device 結構的詳情
- 17.4. 打開與關閉
- 17.5. 報文傳送
- 17.6. 報文接收
- 17.7. 中斷處理
- 17.8. 接收中斷緩解
- 17.9. 連接狀態的改變
- 17.10. Socket 緩存
- 17.11. MAC 地址解析
- 17.12. 定制 ioctl 命令
- 17.13. 統計信息
- 17.14. 多播
- 17.15. 幾個其他細節
- 17.16. 快速參考
- 第 18 章 TTY 驅動
- 18.1. 一個小 TTY 驅動
- 18.2. tty_driver 函數指針
- 18.3. TTY 線路設置
- 18.4. ioctls 函數
- 18.5. TTY 設備的 proc 和 sysfs 處理
- 18.6. tty_driver 結構的細節
- 18.7. tty_operaions 結構的細節
- 18.8. tty_struct 結構的細節
- 18.9. 快速參考