我的第一个嵌入式linux驱动2_完善1

/*目的:通过应用函数来实现在底层打印相关内容
*2016年4月30日15:31:36
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>




static struct class *firstdrv_class;
static struct class_device *firstdrv_class_devs[4];
/*firstdrv_class_devs是指针数组,数组中的每一个元素都是一个class_device *类型的指针
*/


/*
first_drv_open函数的形参其实就是first_drv_open结构体中的open函数的参数,因为调用我们自己写的first_drv_open
函数时,会通过主设备号找到first_drv_open结构体,然后执行first_drv_open结构体中的open函数,主严是为了参数的传递


*/




int major;


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


static int first_drv_write(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{

printk("first_drv_write\n");
return 0;
}


/*
file_operations是内核中的结构体,里面定义了一些函数,


*/


static struct file_operations first_drv_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   first_drv_open,    //表示将first_drv_open函数的地址赋给file_operations结构体中open函数,
.write = first_drv_write, //以后执行 first_drv_open函数时会自动调用file_operations结构体中的open函数
};




/*
入口函数
如果将register_chrdev的第一个参数设为0,则系统会自动为我们分配一个未被占用的主设备号,
然后将这个主设备号返回,






*/ 
int first_drv_init(void)
{
major = register_chrdev(0,"first_dev",&first_drv_fops);//注册函数,告诉内核


/*
register_chrdev函数会在主设备号处填充first_drv_fops这个结构体,但是不会生成设备信息
以下代码会在sys目录下生成设备信息,class_create是创建个类,然后在这个类下生成一个设备
MKDEV(major, 0)是创建设备,major是主设备号,0是次设备号,xyz是设备名字。
在入口函数创建,则要在出口函数要把这个设备删掉




firstdrv_class = class_create(THIS_MODULE, "firstdrv");会在sys目录下创建firstdrv这个类,
在这个类下面会创建一个xyz设备,然后加载模块时mdev会自动创建一个 /dev/xyz  设备节点


*/


/*
class_create为该设备创建一个class,再为每个设备调用 class_device_create
创建对应的设备。大致用法如下:
struct class *myclass = class_create(THIS_MODULE, “my_device_driver”);
class_device_create(myclass, NULL, MKDEV(major_num, 0), NULL, “my_device”);
这样的module被加载时,udev daemon就会自动在/dev下创建my_device设备文件。




内核中定义了struct class结构体,一个struct class 
结构体类型变量对应一个类,内核同时提供了class_create()
函数,可以用它来创建一个类,这个类存放于sysfs下面,一旦创建了这个类,再调用
device_create()函数在/dev
目录下创建相应的设备节点。这样,加载模块的时候,用户空间中的udev会自动响应
device_create()函数,去/sysfs下寻找对应的类而创建设备节点。




*/


firstdrv_class = class_create(THIS_MODULE, "firstdrv");//创建个类

// if (IS_ERR(firstdrv_class)) //是一些判断
// return PTR_ERR(firstdrv_class);   


firstdrv_class_devs = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/leds */
// if (unlikely(IS_ERR(firstdrv_class_devs)))
// return PTR_ERR(firstdrv_class_devs);


return 0;


}




//出口函数
void first_drv_exit(void)
{
unregister_chrdev(major,"first_dev");//卸载驱动


class_device_unregister(firstdrv_class_devs);
class_destroy(firstdrv_class);


}


/*修饰
module_init是定义一个结构体,结构体里面有一个函数指针,指向first_drv_init这个入口函数,
当我们用insmod加载一个驱动时,内核会自动找到这个结构体,调用里面的函数指针,
指向入口函数
*/


module_init(first_drv_init);
module_exit(first_drv_exit);


/*
实现过程:当使用insmod加载驱动时,首先通过module_init(first_drv_init);找到入口函数first_drv_init,
该函数告诉内核主设备号111和first_drv_fops结构体一起向内核注册,这样以后在对主设备号111操作时,
就会调用first_drv_fops结构体中相同的函数来处理。
*/






/* MODULE_LICENSE("GPL");描述驱动程序的一些信息,不是必须的 
,如果不加下面的代码,则不知道firstdrv_class,firstdrv_class_devs是怎么实现的
firstdrv_class = class_create(THIS_MODULE, "firstdrv");//创建个类
firstdrv_class_devs = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); 


模块许可证声明(必须)
如果不声明,则在模块加载时会收到内核被污染的警告,一般应遵循GPL协议。




*/


MODULE_LICENSE("GPL");








//测试函数



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


int main (int argc,char ** argv)
{
int fd;
int val = 1;
fd = open("/dev/xyz",O_RDWR);//xxx表示名字不重要
/*
无论我们的主设备号怎么变,/dev/xyz 
这个设备都是系统为我们自动创建的,他的主设备号会根据系统为我们分配的主设备号
而定,
*/
if(fd < 0)
printf("can't open!"\n);
write(fd,&val,4);


return 0;


}
/*
一般函数是可以带参数的,main函数也不例外,格式通常采用这种固定形式。
由于main
不被其他函数调用,所以不能从程序中获取参数。实际上参数是执行时从操作系统上获取?
模琣rgc是参数个数,argv是参数列表。
当我们要运行一个可执行文件时,在DOS
提示符下键入文件名,再输入实际参数即可把这些实参传送到main的形参中去。


argc是你主程序参数的个数。


argv[0]是你编译出来执行时候程序的名字。
argv[1].....是你主程序需要的参数。
举例说明:如下程序 aa.c
#include<stdio.h>
#include<stdlib.h>
#include<stdring.h>
int main(int argc, char *argv[])
{
        printf("%d\n",argc);
        printf("%s\n",argv[0]);
        printf("%s\n",argv[1]);
        printf("%s\n",argv[2]);
        return 0;
}
编译: gcc -o hello aa.c (也就是编译出来的执行文件叫hello,这是linux
上的编译方式)
执行: hello aa  bb
结果:
2
hello
aa
bb


*/






/*
#include<fcntl.h>
intopen(constchar*pathname,intflags);
intopen(constchar*pathname,intflags,mode_tmode);
返回值:成功则返回文件描述符,否则返回-1


对于open函数来说,第三个参数仅当创建新文件时(即 使用了O_CREAT 
时)才使用,用于指定文件的访问权限位(access permission bits)。pathname 
是待打开/创建文件的POSIX路径名(如/home/user/a.cpp);flags 
用于指定文件的打开/创建模式,这个参数可由以下常量(定义于fcntl.h
)通过逻辑位或逻辑构成。


1
2
3


O_RDONLY只读模式
O_WRONLY只写模式
O_RDWR读写模式




write: int write(int fd, char * buf, unsigned count);


*/


/*测试结果是:can't open!
因为在板子上不存在/dev/xxx,如果想要在板子上测试成功的话
需要使用mknod /dev/xxx c 111 0
c 代表字符   111 代表主设备号 0 代表次设备号
mknod /dev/xxx c 111 0 这行命令代表创建设备节点,
把/dev/xxx设备设为字符设备,主设备号为111,次设备号为0;




*/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值