日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

Linux操作系統(tǒng)內(nèi)核和設(shè)備文件對(duì)話

設(shè)備文件是用來(lái)代表物理設(shè)備的 。多數(shù)物理設(shè)備是用來(lái)進(jìn)行輸出或輸入的,所以必須由某種機(jī)制使得內(nèi)核中的設(shè)備驅(qū)動(dòng)從進(jìn)程中得到輸出送給設(shè)備 。這可以通過(guò)打開輸出設(shè)備文件并且寫入做到,就想寫入一個(gè)普通文件 。在下面的例子里,這由device_write實(shí)現(xiàn) 。
這不是總能奏效的 。設(shè)想你與一個(gè)連向modem的串口(技是你有一個(gè)內(nèi)貓,從CPU看來(lái)它也是作為一個(gè)串口實(shí)現(xiàn),所以你不需要認(rèn)為這個(gè)設(shè)想太困難) 。最自然要做的事情就是使用設(shè)備文件把內(nèi)容寫到modem上(無(wú)論用modem命令還是電話線)或者從modem讀信息(同樣可以從modem命令回答或者通過(guò)電話線) 。但是這留下的問(wèn)題是當(dāng)你需要和串口本身對(duì)話的時(shí)候需要怎樣做?比如發(fā)送數(shù)據(jù)發(fā)送和接收的速率 。
回答是Unix使用一個(gè)叫做ioctl(input output control的簡(jiǎn)寫)的特殊函數(shù) 。每個(gè)設(shè)備都有自己的ioctl命令,這個(gè)命令可以是ioctl讀的,也可以是寫的,也可以是兩者都是或都不是 。Ioctl函數(shù)由三個(gè)參數(shù)調(diào)用:適當(dāng)設(shè)備的描述子,ioctl數(shù),和一個(gè)長(zhǎng)整型參數(shù),可以賦予一個(gè)角色用來(lái)傳遞任何東西 。
Ioctl數(shù)對(duì)設(shè)備主碼、ioctl類型、編碼、和參數(shù)的類型進(jìn)行編碼 。Ioctl數(shù)通常在頭文件由一個(gè)宏調(diào)用(_IO,_IOR,_IOW或_IOWR——決定于類型) 。這個(gè)頭文件必須包含在使用ioctl(所以它們可以產(chǎn)生正確的ioctl"s)程序和內(nèi)核模塊(所以它可以理解)中 。在下面的例子里,這個(gè)頭文件是chardev.h,使用它的程序是ioctl.c 。
如果你希望在你自己的內(nèi)核模塊中使用ioctl"s,最好去接受一分正式的ioctl職位,這樣你就可以得到別人的ioctl"s,或者他們得到你,你就可以知道哪里出了錯(cuò)誤 。如果想得到更多的信息,到"documentation/ioctl-number.txt"中查看內(nèi)核源文件樹 。
ex chardev.c;
/* chardev.c;
*;
* Create an input/output character device;
*/;

/* Copyright (C) 1998-99 by Ori Pomerantz */;
/* The necessary header files */;
/* Standard in kernel modules */;
#include /* Were doing kernel work */;
#include /* Specifically, a module */;
/* Deal with CONFIG_MODVERSIONS */;
#if CONFIG_MODVERSIONS==1;
#define MODVERSIONS;
#include;
#endif;
/* For character devices */;
/* The character device definitions are here */;
#include;
/* A wrapper which does next to nothing at;
* at present, but may help for compatibility;
* with future versions of Linux */;
#include;

/* Our own ioctl numbers */;
#include "chardev.h";

/* In 2.2.3 /usr/include/linux/version.h includes a;
* macro for this, but 2.0.35 doesnt - so I add it;
* here if necessary. */;
#ifndef KERNEL_VERSION;
#define KERNEL_VERSION(a,b,c) ((a)*65536 (b)*256 (c));
#endif;
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0);
#include /* for get_user and put_user */;
#endif;
#define SUCCESS 0;

/* Device Declarations ******************************** */;

/* The name for our device, as it will appear in;
* /proc/devices */;
#define DEVICE_NAME "char_dev";

/* The maximum length of the message for the device */;
#define BUF_LEN 80;
/* Is the device open right now? Used to prevent;
* concurent access into the same device */;
static int Device_Open = 0
/* The message the device will give when asked */;
static char Message[BUF_LEN]
/* How far did the process reading the message get?;
* Useful if the message is larger than the size of the;
* buffer we get to fill in device_read. */;
static char *Message_Ptr

/* This function is called whenever a process attempts;
* to open the device file */;
static int device_open(struct inode *inode,;
struct file *file);
{;
#ifdef DEBUG;
printk ("device_open(%p)n", file)

推薦閱讀