| 
		    
                    
   read()函数 当对设备特殊文件进行read() 系统调用时,将调用驱动程序read() 函数: 
 
    
        
            | ssize_t (*read) (struct file * filp, char * buf, size_t count, loff_t * offp); | 
         
    
 
  参数buf是指向用户空间缓冲区的指针,由用户进程给出,count 为用户进程要求读取的字节数,也由用户给出。
    read() 函数的功能就是从硬设备或内核内存中读取或复制count个字节到buf 指定的缓冲区中。在复制数据时要注意,驱动程序运行在内核中,而buf指定的缓冲区在用户内存区中,是不能直接在内核中访问使用的,因此,必须使用特殊的复制函数来完成复制工作,这些函数在include/asm/uaccess.h中被声明: 
 
    
        
            | unsigned long copy_to_user (void * to, void * from, unsigned long len); | 
         
    
 
   此外,put_user()函数用于内核空间和用户空间的单值交互(如char、int、long)。
    write( ) 函数 当设备特殊文件进行write () 系统调用时,将调用驱动程序的write () 函数:
 
    
        
            | ssize_t (*write) (struct file *, const char *, size_t, loff_t *); | 
         
    
 
  write ()的功能是将参数buf 指定的缓冲区中的count 个字节内容复制到硬件或内核内存中,和read() 一样,复制工作也需要由特殊函数来完成: 
 
    
        
            | unsigned long copy_from_user(void *to, const void *from, unsigned long n); | 
         
    
 
  此外,get_user()函数用于内核空间和用户空间的单值交互(如char、int、long)。
    ioctl() 函数 该函数是特殊的控制函数,可以通过它向设备传递控制信息或从设备取得状态信息,函数原型为: 
 
    
        
            | int (*ioctl) (struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg); | 
         
    
 
  参数cmd为设备驱动程序要执行的命令的代码,由用户自定义,参数arg 为相应的命令提供参数,类型可以是整型、指针等。
    同样,在驱动程序中,这些函数的定义也必须符合命名规则,按照本文约定,设备"exampledev"的驱动程序的这些函数应分别命名为 exampledev_open、exampledev_ release、exampledev_read、exampledev_write、exampledev_ioctl,因此设备 "exampledev"的基本入口点结构变量exampledev_fops 赋值如下(对较早版本的内核): 
 
    
        
            struct file_operations exampledev_fops {  NULL ,  exampledev_read ,  exampledev_write ,  NULL ,  NULL ,  exampledev_ioctl ,  NULL ,  exampledev_open ,  exampledev_release ,  NULL ,  NULL ,  NULL ,  NULL } ; | 
         
    
 
  就目前而言,由于file_operations结构体已经很庞大,我们更适合用GNU扩展的C语法来初始化exampledev_fops: 
 
    
        
            struct file_operations exampledev_fops = {  read: exampledev _read,  write: exampledev _write,  ioctl: exampledev_ioctl ,  open: exampledev_open ,  release : exampledev_release , }; | 
         
    
 
		    
                      
		      
		      
		   |