基于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 <[email protected]>
/// 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;
}
/// @brief
///
/// @param file
/// @param offset :上层应用lseek(int fd, off_t offset, int whence)的offset
/// @param where :上层应用lseek(int fd, off_t offset, int whence)的whence
///
/// @return :上层应用lseek(int fd, off_t offset, int whence)的返回值
loff_t mydemodev_llseek(struct file *file, loff_t offset, int where) {
// 没有 加锁
loff_t pos = file->f_pos + offset;
switch(where) {
case SEEK_SET:
if (offset >= MYDEMO_BUF_LEN || offset < 0) {
return -EINVAL;
}
file->f_pos = offset;
break;
case SEEK_CUR:
case SEEK_END:
if (pos >= MYDEMO_BUF_LEN || pos < 0) {
return -EINVAL;
}
file->f_pos = pos;
break;
default:
return -EINVAL;
break;
}
printk(KERN_WARNING "[%s %s %d] pos = %lld iminor(file->f_inode) = %u\n", __FILE__, __func__, __LINE__, file->f_pos, iminor(file->f_inode));
g_llMyDemoPos = file->f_pos;
return file->f_pos;
}
static const struct file_operations mydemodev_fops = {
.owner = THIS_MODULE,
.llseek = mydemodev_llseek, // 1-2 --|
.read = mydemodev_read, // 1-2 --|
.write = mydemodev_write, // 1-2 --|这一步写上, 高级操作.
// .unlocked_ioctl = i2cdev_ioctl,
.open = mydemodev_open, // 1-1 --|
.release = mydemodev_release, // 1-1 --| 这一步写上, 基本上就可以看到效果了.
};
static struct class *mydemo_dev_class;
static int __init mydemo_dev_init(void)
{
int res;
printk(KERN_INFO "my char dev demo entries driver\n");
res = register_chrdev(MYDEMO_MAJOR, "mydemo", &mydemodev_fops); //1. 注册字符设备
if (res)
goto out;
mydemo_dev_class = class_create(THIS_MODULE, "mydemo-class"); //2. 生成class, 在 /sys/class/ 下生成mydemo-class文件夹
if (IS_ERR(mydemo_dev_class)) {
res = PTR_ERR(mydemo_dev_class);
goto out_unreg_chrdev;
}
device_create(mydemo_dev_class,NULL,MKDEV(MYDEMO_MAJOR, MYDEMO_MINNOR),NULL,"mydemo-dev" "%d", MYDEMO_MINNOR); //3. 生成class, 在 /dev/ 下生成mydemo-dev9节点.
return 0;
class_destroy(mydemo_dev_class);
out_unreg_chrdev:
unregister_chrdev(MYDEMO_MAJOR, "mydemo");
out:
printk(KERN_ERR "%s: Driver Initialisation failed\n", __FILE__);
return res;
}
static void __exit mydemo_dev_exit(void)
{
printk(KERN_INFO "my char dev demo out driver\n");
class_destroy(mydemo_dev_class);
unregister_chrdev(MYDEMO_MAJOR, "mydemo");
}
MODULE_AUTHOR("EastonWoo <[email protected]>");
MODULE_DESCRIPTION("my char dev demo entries driver");
MODULE_LICENSE("GPL");
module_init(mydemo_dev_init);
module_exit(mydemo_dev_exit);
6. 修改drivers/Makefile:
添加obj-$(CONFIG_MY_DEMO) += MyDemo/
7. 修改drivers/char/Kconfig:
添加source "drivers/MyDemo/Kconfig"
这样就完成了.可以编译测试了.