hello Linux module FIrst

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>

#define MIN(a, b) ( a < b ? a : b )
/*
1、static修饰变量会延长局部变量的生命周期

当static修饰变量的时候,变量会被存储在静态区,存储出在静态区的变量会在程序刚开始运行时就完成初始化,也是唯一的一次初始化,直到程序运行结束以后才进行变量释放。
2、static修饰的全局变量或者函数具有隐藏特性

我们都知道,当同时编译多个文件时,全局变量或者函数都具有全局可见性,当我们为了提升效率和程序安全性考虑时,可以在全局变量或者函数前加上static前缀,这样该全局变量或者函数只能在本文件中被访问,不能在其它文件中访问,即便是extern外部声明也不可以。 

static is used internl


*/
static int major = 0;
static char kernel_buf[1024];
static struct class *hello_class;



	static int hello_drv_open ( struct inode *node, struct file *file){
		printk("%s %s line %d\n",__FILE__,__FUNCTION__,__LINE__);
		/*__LINE__ 文件中的当前行号。
		#__FILE__ 文件的完整路径和文件名。如果用在包含文件中,则返回包含文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径,而在此之前的版本有时会包含一个相对路径。
		#__FUNCTION__ 函数名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该函数被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。
		#__CLASS__ 类的名称(PHP 4.3.0 新加)。自 PHP 5 起本常量返回该类被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。
		#__METHOD__ 类的方法名(PHP 5.0.0 新加)。返回该方法被定义时的名字(区分大小写)
		*/
		return 0;
	}

	static int hello_drv_close(struct inode *node, struct file *file){
		printk("%s %s line %d\n",__FILE__,__FUNCTION__,__LINE__);
		return 0;
	}

	/*
		(1)size_t、ssize_t的类型

size_t是一些C/C++标准在stddef.h中定义的。这个类型也是一个整型。size_t的真实类型与操作系统有关。

在32位系统中被普遍定义为:typedef unsigned int size_t;为无符号整型,长度为4个字节。而在64位系统中定义为:typedef unsigned long size_t;为无符号长整型,长度为8个字节。在不同架构上进行编译时需要长度问题。

ssize_t是有符号整型,在32位机器上等同与int,在64位机器上等同与long int.

(2)size_t和ssize_t作用、区别

size_t一般用来表示一种计数,比如有多少东西被拷贝等。例如:sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小。 它的意义大致是“适于计量内存中可容纳的数据项目个数的无符号整数类型”。所以,它在数组下标和内存管理函数之类的地方广泛使用。

而ssize_t这个数据类型用来表示可以被执行读写操作的数据块的大小.它和size_t类似,但必需是signed.意即:它表示的是signed size_t类型的。

两种类型更像是对int和long类型按照使用方向进行的功能性划分
	
size_t  为 unsigned  long/int 类型

ssize_t 为 long/int 类型

总而言之  ssize_t  =  signed  size_t

loff_t 为 long long 类型

	*/


	/*
		

__user宏简单告诉编译器(通过 noderef)不应该解除这个指针的引用(因为在当前地址空间中它是没有意义的)。 

 

(void __user *)arg 指的是arg值是一个用户空间的地址,不能直接进行拷贝等,要使用例如copy_from_user,copy_to_user等函数。

默认是内核空间,因为这是驱动,是在内核空间运行的。

 

直接拷贝不了,因为一个是在用户空间,一个是在内核空间

如果要拷贝的话,可使用 copy_from_user 


假如用户空间写为:

int i = 0;
ioctl(fd, XXXXX, i);


内核空间需要写为

get_user(xxx, (int __user *)arg);


假如用户空间写为:

int i = 0;
ioctl(fd, XXXXX, &i);

则内核空间需要写为:

copy_from_user(xxx, (void __user *)arg, size);

todo: 1: what is th size mean?


	*/
	//static ssize_t hello_drv_read(struct file *file,char __user *buf, size_t size, loff_t *offset){
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	int err;
		
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
err = copy_to_user(buf,kernel_buf,MIN(1024,size));
return MIN(1024,size);
	}

static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(kernel_buf, buf, MIN(1024, size));
	return MIN(1024, size);
}

/*
'hello_drv_open' undeclared here (not in a function)
This error will happen if we move hello_drv declare before the hello_drv_open method

*/
	// setup my file_operations struct

	static struct file_operations hello_drv = {
		.owner = THIS_MODULE,
		.open = hello_drv_open,
		.read = hello_drv_read,
		.write = hello_drv_write,
		.release = hello_drv_close,
	};

/*
宏定义__init,用于告诉编译器相关函数或变量仅用于初始化。编译器将标__init的所有代码存在特殊的内存段中,初始化结束后就释放这段内存。
*/
static int __init hello_init(void){
	/*
		register device
		create class
		create device
	*/

	int err;
	printk("%s %s %d \n",__FILE__,__FUNCTION__,__LINE__);
	major = register_chrdev(0,"hello",&hello_drv);

	hello_class = class_create(THIS_MODULE, "hello_class");
	err = PTR_ERR(hello_class);
	if (IS_ERR(hello_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "hello");
		return -1;
	}

	device_create(hello_class,NULL,MKDEV(major,0),NULL,"hello");

	return 0;
}

/*

C语言中函数声明对函数的检测最主要的标准是函数名,C语言中函数参数列表中的参数名可以缺省
 
C语言当中参数列表中的数据类型也可以缺省,默认是int。
 
add()并不等价于add(void);  add(void)明确指明add函数不接收任何参数,若对其传参会报错,提示是error;
而add()表示接收参数,他的类型是int,只是他是一种没有意义的接收,并不会用这个参数,提示是wring;

*/
static void __exit hello_exit(void){

	printk("%s %s line %d\n",__FILE__,__FUNCTION__,__LINE__);
	device_destroy(hello_class,MKDEV(major,0));
	class_destroy(hello_class);
	unregister_chrdev(major,"hello");

}

/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

/*
this should be ** here, not *
*/
int main(int argc, char **argv){
	int fd;
	char buf[1024];
	int len;

	if (argc < 2) {
		printf("Usage: %s -w <string>\n",argv[0]);
		printf("	%s -r\n", argv[0]);
		return -1;
	}

	fd = open("/dev/hello",O_RDWR);
	if(fd == -1){
		printf("can not open file: /dev/hello \n");
		return -1;
	}

	if(( 0 == strcmp(argv[1],"-w")) && (argc ==3)){
		len = strlen(argv[2]) + 1;
		len = len <1024 ? len :1024;
		write(fd,argv[2],len);

	} else if (( 0 == strcmp(argv[1],"-r") && (argc == 2))){
		len = read(fd,buf,1024);
		/* If we use "\0" here, it will my_hello_drv_test.c:34:13: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
   buf[1023] = "\0";
*/
		buf[1023] = '\0';
		printf("APP read : %s \n",buf);
	} else {
		close(fd);
		return -1;
	}

	close(fd);

	return 0;

}

 


# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
# 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
# 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
#       请参考各开发板的高级用户使用手册

KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88

all:
	make -C $(KERN_DIR) M=`pwd` modules 
	$(CROSS_COMPILE)gcc -o my_hello_drv_test my_hello_drv_test.c 
	

clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f hello_drv_test

obj-m	+= my_hello_drv.o

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值