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