΢ͻӦ
MICROCOMPUTER & ITS APPLICATIONS
2000 Vol.19 No.2 P.14-16




LINUXַ豸Ʒо
Ҷ
ժ Ҫ ԱȽϵ͵ʽӡ豸о˵ͷLINUXϵͳַ豸Ĺ
ؼʣ LINUX ַ豸 豸
ͬUNIXϵͳƣLINUXϵͳ2豸--豸ַ豸νַ豸Ļλ11ַҲ˳̶ַܽ豸ԼСͻдַ豸ļϵͳӿڣopenwriteȣ豸ֱͬ豸һģ˹̡Ľͨ2ֱȽϵ͵ַ豸꣟mouseʽӡline printer˵ַ豸ĹLINUXϵͳ豸Ҫṹ3飬ֱΪʼ̣init1ļӿڣstruct fileoperationsĶ塢жϴ̻Polling̣Ӿ
1 豸Ĺ
һֳĵַ豸LINUXϵͳе豸Ϊ10ʵϣ豸ļdevmouseӦ1߼꣬ϵͳԶҵ꣬вϵͳÿһֻ֧1꣬ϵͳһʱ֧1ꡣLINUXversion 1320ֵ֧ʾ
define BUSMOUSEMINOR 0 Logitech bus mouse
define PSMOUSEMINOR 1 PS2 mouse
define MSBUSMOUSEMINOR 2 Microsoft bus
 mouse
define ATIXLBUSMOUSEMINOR 3 Atixl board 
bus mouse
11 ߼
mouse豸driverscharmousecֻ21mouseopenΪ߼Ψһļӿڣ1mouseinitΪĳʼ̳ںchrdevinitСmouseopenݲ豸жҪһ꣬Ȼ򵥵ذָļӿ߼豸ļṹfileopȻµʵļȷе֪ʵʵļĸmsbusmouseômouseвͶmsbusmouseвЧһµġmouseinitֱʼϵͳúõĸ豸ڱʱȷġ
12 msbusmouse
1ݽṹ豸򲻿ɱҪ漰Ӳײ㣬Ӧü˽һЩײӲݡ
define MOUSEIRQ 5 ж
define MSMSEDATAPORT 0x23d IOݶ˿
define MSMSESIGNATUREPORT 0x23e źŶ˿
define MSMSECONTROLPORT 0x23c ƶ˿
define MSMSECONFIGPORT 0x23f
 ö˿ڣseems useless
ЩIO˿ڽвĹinboutbжϺŶ5ʹϵͳһֻ֧1̨豸һ龲̬浱ǰ¼Ϣṹļincludelinuxbusmousehʾ
struct mousestatus  ǰ״̬
 unsigned char buttons 갴ť״̬ 
unsigned char latchbuttons
int dx ǰ 
int dy 
int present ״̬ 
int ready ǷЧ 
int active means busy struct waitqueue waitĵȴ

static struct mousestatus mouse
2豸ԭ
ȽγʼġmouseinitMicrosoft busĳʼ̣unsigned long msbusmouseinitunsigned long kmemstartʼʼ״̬mouseе0Ȼ⵱ǰ갲װ״̬ͨɴδźŶ˿MSMSESIGNATUREPORTݣ۲״̬ǷȶӶжǷװֹжϡԿ豸ļɣϵͳʱûаװôʹйٰװҲ޼¡ϵͳȻϣҪҪɶĶ
ļӿڡ򲢷Ҫʵеļӿڣ豸иԵĹص㣬Ҫ
struct fileoperations msbusmousefops
NULL * mouseseekַ豸˳ļ* 
readmouse read buttonx and y coordinates 
writemouse ʵϣñشϢ 
NULL * mousereaddir *
mouseselect * mouseselect*
NULL * mouseioct1*
NULL * mousemmap * 
openmouse 
releasemouse
}
1static int openmousestruct inode * inodestruct file *file
жǷڻǷӦʹãûУͱʶռ꣬ͬʱȻϵͳ10жreuqestirqǼԼжϴmsmouseinterruputѾжϣʧܡƶ˿ڷ豸жϡ
2static void releasemousestruct inode * inodestruct file* file
ͷŹҪ򵥵ö࣬жϣ豸б־ͷж
3static int readmousestruct inode * inodestruct file * filechar  bufferint countҪĹܺǰ1Ϸ¼ͽ״̬mouseеİť״̬ݹ3ֽд뵽ûbufferУ״̬mouse
4static int mouseselectstruct inode * inodestruct file * fileint seltypeselecttable * wait
ǰû¼Ͱwait뵽mouseĵȴУȳ¼ʱжϴѡһreadmouseʹá
LINUXϵͳ깤жϷʽ£жϴǣ
static void msmouseinterruptint irqstruct ptregs * regs
ϵͳڽյӲжϺԶҪǴݶ˿ڶ뵱ǰťݣ״̬mouseжϴҪѵȴеĽֵ̡עǣụ̂ȴselectͬԼͬ豸򣬵ԭƣֻӲ
2 д豸Ĺ
ʽӡUNIXϵͳһرĴӡ豸Ҫ긴ӣԼĻͬʱϵͳһֶ̨ܹ֧ӡLINUXϵͳУ˵ܹ֧3̨ӡʽӡԷֱжϺpolling2ַʽ£2ַʽ໥л
21 ӡݽṹ
дLINUXϵͳе豸ΪLPMAJOR6жݲͬĴӡֱá

ע
1lpstructдҪṹ֧3̨豸
2baseд豸IOַ
3irqжΪ0ʱӡѯʽ
4flagʾ˴ӡĵǰ״̬
5charsÿַʱޣڼ㣻
6time¼˵ӡʱĵȴʱ䣻
7lpwaitqǴӡĵȴҵУ
8lpbufferǴӡ
22 ӡ
ӡĻ࣬ԽʡԽƵĲ֡дĳʼ̣
long lpinitlong kmcmstart
lpinitҪϵͳַ豸еǼдregisterchrdevLPMAJORlp壬lpfopslpfopsдļӿڣlp豸Ȼֱ3ӡ˿ڣڴӡӦĴӡṹʼ־ӡ״̬ͨlpresetӡͳʼӳ򿴣һ㿪ʼʱӡѯʽ£Ҿ˴ӡĴ״̬ﻹ˹IOԴ⣬ӡϵͳӦIOַռ䣬ҪIOռѾռˣ˴ӡĳʼ̾ʧܡԴĳļkernelresourcecС
дҪļӿݶ壺

iڵȷĴӡͨMINORinodeirdevȷһʹֱַ治ͬ豸ųһЩϷжϣǷڿСǷȱֽȵȡǰᵽдֱܹ֧жϺpollingʽжϷʽ£ӡӦжϺţжϴlpinterruptæ־
2static void lpreleasestruct inode * inodestruct file * file
̽opeṇͷŻжϺţÿб־pollingʽÿб־
3static int lpwritestruct inode * inodestruct file * fileconst char * bufint count
ݹʽֱвͬдint lpwriteinterruptunsigned int minorconst char  bufint ountint lpwritepolledunsigned int minorconst char  bufint countǵĻͬbufеַд豸ļжϷʽȰbufеݷӡУȻ1ַ1ַشӡַʽҪĲͬǶ쳣Ĵpollingʽschedule˯ߵȴϵͳȽ̻ѣжϷʽinterruptablesleeponڸôӡĵȴв룬ȵӡreadyʱɸôӡжϻѡܵ˵жϷʽЧʸߣһЩ
4static int lpioctlstruct inode * inodestruct file * fileunsigned int cmdunsigned long arg
ioctl1ļӿڣĺڵַsysioctlļfsioctlcСҪļĵײвͨļҪļṹеfflagsвFIOCLEXFIONCLEXFIONBIOȣֱȡԣbmap豸ļ͵豸ioctlӿڡȻе豸ļҪӿڡڴӡУùܺԶȡlpstructṹеĲǳûøôӡжţжΪ0ζŹpollingʽ¡
жϷʽµʱжϴlpinterruptirqҵӦĴӡѸôӡ˯ߵĽ̡ÿ̨ӡжϴͬpollingʽҪ̡ʵӳ򿴣ֹʽȽƣѯʽҲscheduleнлֻжϷʽĿ꣬ѡ¼еĵȴ̽еȡӡĻںĿռУжϷʽݴûӡݡѯʽ´ӡûʹֱӴûȡ
3 
˶ּ򵥵͵ַ豸Ǹص㡣ͨԶ໥ϵĲ˵йַ豸ĸ漰ӲжϺѯơ豸ļ豸ĽӿԼ豸Ŀƺ͹豸İװȵ⣬Щ˽ΪķоΪ1ԵĲϵͳLINUXԽԽ㷺Ӧǰ
Ҷ̣Ӧù̼ѧԺŵϵ310012
ο
1UNIXϵͳݣ㽭ѧ磬1997
2ϣUNIXṹݣ㽭ѧ磬1998
ոڣ19990927
