?? char_dev.c
字號(hào):
/** linux/fs/char_dev.c** (C) 1991 Linus Torvalds*/#include <errno.h> // 錯(cuò)誤號(hào)頭文件。包含系統(tǒng)中各種出錯(cuò)號(hào)。(Linus 從minix 中引進(jìn)的)。#include <sys/types.h> // 類型頭文件。定義了基本的系統(tǒng)數(shù)據(jù)類型。#include <linux/sched.h> // 調(diào)度程序頭文件,定義了任務(wù)結(jié)構(gòu)task_struct、初始任務(wù)0 的數(shù)據(jù),// 還有一些有關(guān)描述符參數(shù)設(shè)置和獲取的嵌入式匯編函數(shù)宏語句。#include <linux/kernel.h> // 內(nèi)核頭文件。含有一些內(nèi)核常用函數(shù)的原形定義。#include <asm/segment.h> // 段操作頭文件。定義了有關(guān)段寄存器操作的嵌入式匯編函數(shù)。#include <asm/io.h> // io 頭文件。定義硬件端口輸入/輸出宏匯編語句。extern int tty_read (unsigned minor, char *buf, int count); // 終端讀。extern int tty_write (unsigned minor, char *buf, int count); // 終端寫。// 定義字符設(shè)備讀寫函數(shù)指針類型。typedef (*crw_ptr) (int rw, unsigned minor, char *buf, int count, off_t * pos);//// 串口終端讀寫操作函數(shù)。// 參數(shù):rw - 讀寫命令;minor - 終端子設(shè)備號(hào);buf - 緩沖區(qū);cout - 讀寫字節(jié)數(shù);// pos - 讀寫操作當(dāng)前指針,對(duì)于終端操作,該指針無用。// 返回:實(shí)際讀寫的字節(jié)數(shù)。static intrw_ttyx (int rw, unsigned minor, char *buf, int count, off_t * pos){ return ((rw == READ) ? tty_read (minor, buf, count) : tty_write (minor, buf, count));}//// 終端讀寫操作函數(shù)。// 同上rw_ttyx(),只是增加了對(duì)進(jìn)程是否有控制終端的檢測(cè)。static intrw_tty (int rw, unsigned minor, char *buf, int count, off_t * pos){// 若進(jìn)程沒有對(duì)應(yīng)的控制終端,則返回出錯(cuò)號(hào)。 if (current->tty < 0) return -EPERM;// 否則調(diào)用終端讀寫函數(shù)rw_ttyx(),并返回實(shí)際讀寫字節(jié)數(shù)。 return rw_ttyx (rw, current->tty, buf, count, pos);}//// 內(nèi)存數(shù)據(jù)讀寫。未實(shí)現(xiàn)。static intrw_ram (int rw, char *buf, int count, off_t * pos){ return -EIO;}//// 內(nèi)存數(shù)據(jù)讀寫操作函數(shù)。未實(shí)現(xiàn)。static intrw_mem (int rw, char *buf, int count, off_t * pos){ return -EIO;}//// 內(nèi)核數(shù)據(jù)區(qū)讀寫函數(shù)。未實(shí)現(xiàn)。static intrw_kmem (int rw, char *buf, int count, off_t * pos){ return -EIO;}// 端口讀寫操作函數(shù)。// 參數(shù):rw - 讀寫命令;buf - 緩沖區(qū);cout - 讀寫字節(jié)數(shù);pos - 端口地址。// 返回:實(shí)際讀寫的字節(jié)數(shù)。static intrw_port (int rw, char *buf, int count, off_t * pos){ int i = *pos;// 對(duì)于所要求讀寫的字節(jié)數(shù),并且端口地址小于64k 時(shí),循環(huán)執(zhí)行單個(gè)字節(jié)的讀寫操作。 while (count-- > 0 && i < 65536) {// 若是讀命令,則從端口i 中讀取一字節(jié)內(nèi)容并放到用戶緩沖區(qū)中。 if (rw == READ) put_fs_byte (inb (i), buf++);// 若是寫命令,則從用戶數(shù)據(jù)緩沖區(qū)中取一字節(jié)輸出到端口i。 else outb (get_fs_byte (buf++), i);// 前移一個(gè)端口。[??] i++; }// 計(jì)算讀/寫的字節(jié)數(shù),并相應(yīng)調(diào)整讀寫指針。 i -= *pos; *pos += i;// 返回讀/寫的字節(jié)數(shù)。 return i;}//// 內(nèi)存讀寫操作函數(shù)。static intrw_memory (int rw, unsigned minor, char *buf, int count, off_t * pos){// 根據(jù)內(nèi)存設(shè)備子設(shè)備號(hào),分別調(diào)用不同的內(nèi)存讀寫函數(shù)。 switch (minor) { case 0: return rw_ram (rw, buf, count, pos); case 1: return rw_mem (rw, buf, count, pos); case 2: return rw_kmem (rw, buf, count, pos); case 3: return (rw == READ) ? 0 : count; /* rw_null */ case 4: return rw_port (rw, buf, count, pos); default: return -EIO; }}// 定義系統(tǒng)中設(shè)備種數(shù)。#define NRDEVS ((sizeof (crw_table))/(sizeof (crw_ptr)))// 字符設(shè)備讀寫函數(shù)指針表。static crw_ptr crw_table[] = { NULL, /* nodev *//* 無設(shè)備(空設(shè)備) */ rw_memory, /* /dev/mem etc *//* /dev/mem 等 */ NULL, /* /dev/fd *//* /dev/fd 軟驅(qū) */ NULL, /* /dev/hd *//* /dev/hd 硬盤 */ rw_ttyx, /* /dev/ttyx *//* /dev/ttyx 串口終端 */ rw_tty, /* /dev/tty *//* /dev/tty 終端 */ NULL, /* /dev/lp *//* /dev/lp 打印機(jī) */ NULL}; /* unnamed pipes *//* 未命名管道 *///// 字符設(shè)備讀寫操作函數(shù)。// 參數(shù):rw - 讀寫命令;dev - 設(shè)備號(hào);buf - 緩沖區(qū);count - 讀寫字節(jié)數(shù);pos -讀寫指針。// 返回:實(shí)際讀/寫字節(jié)數(shù)。intrw_char (int rw, int dev, char *buf, int count, off_t * pos){ crw_ptr call_addr;// 如果設(shè)備號(hào)超出系統(tǒng)設(shè)備數(shù),則返回出錯(cuò)碼。 if (MAJOR (dev) >= NRDEVS) return -ENODEV;// 若該設(shè)備沒有對(duì)應(yīng)的讀/寫函數(shù),則返回出錯(cuò)碼。 if (!(call_addr = crw_table[MAJOR (dev)])) return -ENODEV;// 調(diào)用對(duì)應(yīng)設(shè)備的讀寫操作函數(shù),并返回實(shí)際讀/寫的字節(jié)數(shù)。 return call_addr (rw, MINOR (dev), buf, count, pos);}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -