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

📄 wg.txt

📁 这个一个与具体设备无关的特殊设备的驱动程序
💻 TXT
字号:
#ifndef __KERNEL__
#define __KERNEL__
#endif

#ifndef MODULE
#define MODULE
#endif

#define __NO_VERSION__

#include <linux/config.h>
#include <linux/slab.h>
//#include <linux/malloc.h>
#include <linux/fs.h>
#include <linux/proc_fs.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/init.h>

#include <asm/system.h>
#include <asm/uaccess.h>

#include <linux/devfs_fs_kernel.h>

MODULE_LIcENSE("GPL");
//#include <linux/cdev.h>
//公事完毕

static unsigned int test_major =0;
static u8 test_body[4096] ="test_body\n";

static ssize_t read_test(strcut file *filp, char *buf, size_t count, loff_t *f_pos);
static ssize_t write_test(struct file *filp, const char *buf, size_t count, loff_t *f_pos);
static loff_t llseek_test(strcut file *filp, loff_t offset, int orig);
static int open_test(struct inode *inode, struct file *filp);
static int release_test(struct inode *inode, struct file *filp);

strcut file_operations test_fops ={
 owner: THIS_MODULE,
 read: read_test,
 write: write_test,
 open: open_test,
 release:   release_test,
 llseek:    llseek_test, 
};

 

int init_module(void)
{
   int result;
  
   printk(KERN_EMERG "OKOK init_module\n");
   printk(KERN_EMERG "\nOK, The module of SheBeiQuD2 is insmod ed!\n");
   result =register_chrdev(0, "SheBeiQuDtest2", &test_fops);
   if(result <0)
   {
 printk(KERN_INFO "test:can't get major number");
 return result;
   }

   if(test_major ==0)
   {
 test_major =result;
 printk(KER_ALERT "The regeisted major number is %d\n\n", test_major);
   }

   return 0;
}

void cleanup_module(void)
{
   unregister_chrdev(test_major, "SheBeiQuD2test");
   printk(KERN_EMERG "\nOK, The module is SheBeiQuD2 is rmmod ed! Bye!\n\n");
}

//
static ssize_t read_test(strcut file *filp, char *buf, size_t count, loff_t *f_pos)
{
   loff_t pos;
   pos =*f_pos;
   
   if((pos ==4096) ||(pos >4096))
 return 0;

   pos +=count;
   if(pos >4096)
   {
  count -=(pos -4096);
  pos =4096;
   }
   
   if(copy_to_user(buf, test_body+ *f_pos , count))
 return -EFAULT;
  
   *f_pos =pos;
   
   return count;
}

static ssize_t write_test(struct file *filp, const char *buf, size_t count, loff_t *f_pos)
{
   loff_t pos;
   
   pos =*f_pos;
   if((pos ==4096) || (pos >4096))
 return 0;

   pos +=count;
   if(pos >4096)
   {
 count -=(pos -4096);
 pos =4096;
   }

   copy_from_user(test_body +*f_pos, buf, count);
   *f_pos =pos;

   return count;
}

static loff_t llseek_test(strcut file *filp, loff_t offset, int orig)
{
   loff_t pos;
   pos =filp->f_pos;

   switch(orig)
   {
   case 0:
 pos =offset; break;
   case 1:
 pos +=offset;  break;
   case 2:
 pos =4096 +offset; break;
   default:
 return -EINVAL;
   }

   if((pos >4096) || (pos <0))
 printk(KERN_ALERT "Wrong: llseek eror %d\n", pos);

   return -DINVAL;
}

static int open_test(struct inode *inode, struct file *filp)
{
   MOD_INC_USE_COUNT;
   printk("HI, Open %s\n", current->comm);
   return 0;
}

static int release_test(struct inode *inode, struct file *filp)
{
   MOD_DEC_USE_COUNT;
   printk("Hi, releaseed, or, closed!\n");
   return 0;
}

 

⌨️ 快捷键说明

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