⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qudongxx.c

📁 LINUX下的字符设备驱动程序代码
💻 C
字号:
#define __NO_VERSION__/*定义版本信息*/
#include <linux/module.h>
#include <linux/version.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <asm/segment.h>
#include <linux/module.h>
#include <asm/uaccess.h>
#include <linux/sched.h>
#include <linux/proc_fs.h>
#define num_buf_dev 500
char kernel_version[]=UTS_RELEASE;
int MOD_COUNT=0;
static ssize_t read_test(struct file *file,char __user *buf,size_t count,loff_t *t);
static ssize_t write_test (struct file *file,const char __user *buf,size_t count,loff_t *t);
static int open_test(struct inode *inode,struct file *file);
static int release_test (struct inode *inode,struct file *file);
unsigned int test_major=0;

struct file_operations test_fops={
.read=read_test,
.write=write_test,
.open=open_test,
.release=release_test
};

char bufq[num_buf_dev];
int flag=0;
int offset_write=0;
int offset_read=0;

static ssize_t read_test(struct file *file,char __user *buf,size_t count,loff_t *t)
{ int left;
   
   if (verify_area(VERIFY_WRITE,buf,count)==-EFAULT)
      return -EFAULT;
  
  for (left=count;left>0;left--)
  {    
      if (flag&&offset_read<=offset_write){
         put_user(bufq[offset_read],buf++);
         offset_read++; 
      }
      else return count;
  }
  return 0;
}
       

static ssize_t write_test (struct file *file,const char __user *buf,size_t count,loff_t *t)
{   int left;
    if (verify_area(VERIFY_READ,buf,count)==-EFAULT)
        return -EFAULT;
   
    for(left=count;left>0;left--)
    {
         __copy_from_user(bufq+offset_write++,buf++,1);
               
    }
    flag=1;
    return count;
}
static int open_test(struct inode *inode,struct file *file)
{
   offset_write=0;
   offset_read=0;
   flag=0;
   MOD_COUNT++;
   return 0;
}
static int release_test (struct inode *inode,struct file *file)
{
    MOD_COUNT--;
}
 int __init init_module(void) 
{ 
   int result; 
   result = register_chrdev(277,"test",&test_fops); 
   if (result < 0) { 
       printk("<1>t: can't get major number\n"); 
       return result; 
   } 
   if (test_major == 0) test_major = result; /* dynamic */         
   return 0; 
}
 void __exit cleanup_module(void) 
{   
   
   unregister_chrdev(test_major,"test"); 
    
}
module_init(init_module);
module_exit(cleanup_module);




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -