基于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_llMyDemoPos = file->f_pos;
    return really_cnt;
}

/// @brief
///
/// @param file
/// @param buf          :上层应用write(int fildes, const void *buf, size_t nbyte)的buf
/// @param count        :上层应用write(int fildes, const void *buf, size_t nbyte)的nbyte
/// @param offset      :驱动层的 不是 &file->f_pos, 只是一个file->f_pos的数值地址
///
/// @return            :上层应用write(int fildes, const void *buf, size_t nbyte)的返回值
ssize_t mydemodev_write(struct file *file, const char __user *buf, size_t count, loff_t *offset) {
    int ret = 0;
   
    if (NULL == buf) {
        return -EINVAL;
    }
    if (file->f_pos + count >= MYDEMO_BUF_LEN) {
        return -EINVAL;
    }
    /* ret = copy_from_user(file->private_data + file->f_pos, buf, count) ? -EFAULT : ret; */ // 不清楚为什么, file->f_pos这个不会变
    ret = copy_from_user(file->private_data + g_llMyDemoPos, buf, count) ? -EFAULT : ret;
    if (ret != -EFAULT)
        file->f_pos += count;

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

g_llMyDemoPos = file->f_pos;
    return count;
}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/4083a2749568a151bc4765aa50643842.html