驱动文件:qudong.c,make生成qudong.ko文件,烧录到ARM板上
#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
#include <linux/poll.h>    /* COPY_TO_USER */
#include<linux/errno.h>
#define DEV_NAME "rwtest"

static int major=0;
static int MAX_BUF_LEN=1024;
static char drv_buf[1024];
static int WRI_LENGTH=0;
/************************************写入*************************************************/
static ssize_t  dx_write(struct file *file, const char __user *buffer, size_t count, loff_t * ppos)
{ 
    if(count > MAX_BUF_LEN)count = MAX_BUF_LEN;
    copy_from_user(drv_buf , buffer, count);
    WRI_LENGTH = count;
    printk("write data to driver\n");
    return count;
}
/**************************************读取***********************************************/
static ssize_t  dx_read(struct file *filp, char __user *buffer, size_t count, loff_t *ppos)
{
    int i=0,j=0;
    if(count > MAX_BUF_LEN)
        count=MAX_BUF_LEN;
    for(i=strlen(drv_buf);i>0;i--)
    {
        buffer[j]=drv_buf[i-1];
        j++;
    }
    //copy_to_user(buffer,drv_buf,count);
    printk("read data from driver\n");
    return count;
}
 //===========================打开=========================================
static int dx_open(struct inode *inode, struct file *filp)
{
    printk("device open sucess!\n");
    return 0;
}
/**********************************release***************************************************/
static int  dx_release(struct inode *inode, struct file *filp)
{
    printk("device release\n");
    return 0;
}

//======================结构体,驱动各属性==================================================
static struct file_operations file_opt = {
    owner:    THIS_MODULE,
    write:    dx_write,    
    read:    dx_read,    
    open:    dx_open,
    release:dx_release,
};
//----------------------------------------------------------------------
static int __init qudong_init(void)
{
    int ret;
     ret = register_chrdev(0, DEV_NAME, &file_opt);
     if(ret<0)
     {
         printk(DEV_NAME " can't get major number\n");
        return 0;
     }
     major=ret;
     printk("dx module major number is %d\n", ret);
     return 0;
}
//-----------------------------------------------------------------------
static void __exit qudong_exit(void)
{
     unregister_chrdev(major,DEV_NAME);
     printk("exit\n");
}
module_init(qudong_init);
module_exit(qudong_exit);
MODULE_LICENSE("GPL");
View Code

相关文章:

  • 2022-12-23
  • 2022-02-27
  • 2021-12-05
  • 2021-06-07
  • 2021-06-03
  • 2021-11-03
  • 2021-08-25
  • 2022-12-23
猜你喜欢
  • 2021-07-09
  • 2021-05-21
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2021-04-23
相关资源
相似解决方案