background image

针存放在内核的字符设备地址表中,在用户进程对该设备执行系统调用时提供入口地址。

  file_operations 结构体定义为:

struct file_operations

{

 int (*lseek)();

 int (*read)();

 int (*write)();

 int (*readdir)();

 int (*select)();

 int (*ioctl)();

 int (*mmap)();

 int (*open)();

 void(*release)();

 int (*fsync)();

 int (*fasync)();

 int (*check_media_change)();

 void(*revalidate)();

};

  大多数的驱动程序只是利用了其中的一部分,对于驱动程序中无需提供的功能,只
需要把相应位置的值设为 NULL。对于字符设备来说,要提供的主要入口有:open 
() 、release ()、read ()、write ()、ioctl ()。

  open()函数 对设备特殊文件进行 open()系统调用时,将调用驱动程序的 open () 函
数:

int open(struct inode * inode ,struct file * file);

  其中参数 inode 为设备特殊文件的 inode (索引结点) 结构的指针,参数 file 是指向这
一设备的文件结构的指针。open()的主要任务是确定硬件处在就绪状态、验证次设备号的
合法性(次设备号可以用 MINOR(inode-> i - rdev) 取得)、控制使用设备的进程数、根据执行
情况返回状态码(0 表示成功,负数表示存在错误) 等;

  release()函数 当最后一个打开设备的用户进程执行 close ()系统调用时,内核将调
用驱动程序的 release () 函数:

void release (struct inode * inode ,struct file * file) ;

  release 函数的主要任务是清理未结束的输入/输出操作、释放资源、用户自定义排他
标志的复位等。
read()函数 当对设备特殊文件进行 read() 系统调用时,将调用驱动程序 read() 函数:

void read(struct inode * inode ,struct file * file ,char * buf ,int count) ;

  参数 buf 是指向用户空间缓冲区的指针,由用户进程给出,count 为用户进程要求读
取的字节数,也由用户给出。