rk3399 android11字符驱动入门之读取

在 android11源码的kernel\drivers\andy\hello02目录下创建以下3个文件:

hello02.c:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kernel.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/joystick.h>
#include <linux/input.h>
#include <linux/major.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/poll.h>
#include <linux/device.h>
#include <linux/cdev.h>

/**

mknod /dev/mycdev001 c  403 0
cat /dev/mycdev001

*/

#define MY_MAJOR       403
#define MY_MAX_MINORS  1

struct my_device_data {
	struct cdev cdev;
};

struct my_device_data devs[MY_MAX_MINORS];

static int my_open(struct inode *inode, struct file *file) {
	pr_info("my_open\n");
	return 0;
}

static int r = 0;
 
static ssize_t my_read(struct file *filp,   
		char __user *buffer, 
		size_t count, 
		loff_t *offset)
{
	uint8_t *data = "123456\n";
	size_t datalen = strlen(data);
	int p = *offset;

	pr_info("a3 my_read: %d \n",r++);
	pr_info("length of the buffer : %zu \n",count);
	pr_info("offset : %lld \n",*offset);
	
	if(p > datalen) {
		return 0;
	}
	if (count > datalen -p ) {
		count = datalen-p;
	}
	if (copy_to_user(buffer, data+*offset, count)) {
		return -EFAULT;
	}
	*offset += count;
	return count;
}

const struct file_operations my_fops = {
		.open = my_open,
		.read = my_read,
		};

static __init int hello_2_init(void) {
	int err;
	int i;
	pr_info("a3 init_module\n");
	err = register_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS,
			"my_device_driver");
	if (err != 0) {
		/* report error */
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MY_MAJOR, i), 1);
	}

	return 0;
}

static void __exit hello_2_exit(void) {
	int i;
	pr_info("a3 cleanup_module\n");
	for (i = 0; i < MY_MAX_MINORS; i++) {
		cdev_del(&devs[i].cdev);
	}
	unregister_chrdev_region(MKDEV(MY_MAJOR, 0), MY_MAX_MINORS);
}

module_init(hello_2_init);
module_exit(hello_2_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("A sample c  driver");


Makefile:

obj-m += hello02.o

Kconfig :

config HELLO02
tristate "First Android Driver"
default m
help
This is the first Android driver.

在源码的kernel\drivers\Makefile 文件中添加一行:

obj-y				+= hello02/  

然后开始编译内核,我的这个开发板的编译命令为:

./build.sh  -K 

编译成功之后,可以看到在源码的kernel\drivers\andy\hello02目录下生成了hello02.ko 文件,

然后把hello02.ko push到设备的sdcard,然后在adb shell下执行

insmod   hello02.ko
mknod /dev/mycdev001 c  403 0

继续执行:

cat /dev/mycdev001

可以看到输出123456。

成功 。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值