设为首页 加入收藏

TOP

基于Linux 3.10.49内核添加字符驱动(一)
2017-10-18 09:07:45 】 浏览:1797
Tags:基于 Linux 3.10.49 内核 添加 字符 驱动

Linux kernel 3.10.49+


字符驱动编译进内核。


1.在drivers目录下新建MyDemo目录。


2.MyDemo目录新建三个文件:demo.c  Kconfig  Makefile。


3.修改Kconfig文件:


MyDemo-> cat Kconfig


#
# TPM device configuration
#


config MY_DEMO
 bool "my demo test driver!!!"
    default y
 ---help---
  If you have a TPM security chip that is compliant with the
  TCG TIS 1.2 TPM specification say Yes and it will be accessible
  from within Linux.  To compile this driver as a module, choose
  M here; the module will be called tpm_tis.


这个Kconfig会生成CONFIG_MY_DEMO这个宏, 且默认会生成这个宏, 因为default    y


4.修改Makefile:


MyDemo-> cat Makefile


obj-$(CONFIG_MY_DEMO) += demo.o


5.修改demo.c:


MyDemo-> cat demo.c


/// @file demo.c
/// @brief
/// @author EastonWoo <31417071@qq.com>
/// 0.01
/// @date 2016-05-26


#include <linux/module.h> 
#include <linux/cdev.h> 
#include <linux/fs.h> 
#include <linux/slab.h> 
#include <linux/uaccess.h> 
#include <linux/types.h> 
#include <linux/ioctl.h> 
 
#include <linux/device.h>


#define MYDEMO_MAJOR 105
#define MYDEMO_MINNOR 9
#define MYDEMO_BUF_LEN 100


static loff_t g_llMyDemoPos = 0;


static int mydemodev_open(struct inode *inode, struct file *file)
{
 unsigned int minor = iminor(inode);
    char *demo_buf;


 demo_buf = kzalloc(MYDEMO_BUF_LEN, GFP_KERNEL);
    memset(demo_buf, 0, MYDEMO_BUF_LEN);
 if (!demo_buf) {
  return -ENOMEM;
 }


 file->private_data = demo_buf;
    printk(KERN_WARNING "[%s %s %d] minor = %u\n", __FILE__, __func__, __LINE__, minor);
 return 0;
}


static int mydemodev_release(struct inode *inode, struct file *file)
{
 unsigned int minor = iminor(inode);
    char *demo_buf = file->private_data;
 kfree(demo_buf);
 file->private_data = NULL;
    printk(KERN_WARNING "[%s %s %d] minor = %u\n", __FILE__, __func__, __LINE__, minor);


 return 0;
}


/// @brief
///
/// @param file
/// @param buf          :上层应用read(int fildes, void *buf, size_t nbyte)的buf
/// @param count        :上层应用read(int fildes, void *buf, size_t nbyte)的nbyte
/// @param offset      :驱动层的 不是 &file->f_pos, 只是一个file->f_pos的数值地址
///
/// @return            :上层应用read(int fildes, void *buf, size_t nbyte)的返回值
ssize_t mydemodev_read(struct file *file, char __user *buf, size_t count, loff_t *offset) {
    loff_t pos = file->f_pos + count;
    size_t really_cnt = count;
    int ret = 0;
   
    if (NULL == buf) {
        return -EINVAL;
    }
    if (pos >= MYDEMO_BUF_LEN) {
        really_cnt = MYDEMO_BUF_LEN - 1 - file->f_pos;
    }


    /* ret = copy_to_user(buf, file->private_data + file->f_pos, really_cnt) ? -EFAULT : ret; */  // 不清楚为什么, file->f_pos这个不会变
    ret = copy_to_user(buf, file->private_data + g_llMyDemoPos, really_cnt) ? -EFAULT : ret;
    if (ret != -EFAULT)
        file->f_pos += really_cnt;


    printk(KERN_WARNING "[%s %s %d] really_cnt = %d\n", __FILE__, __func__, __LINE__, really_cnt);
    g

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C#的单例模式实现 下一篇基于Linux 3.10.49内核的pinctrl..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目