嵌入式系统006-生成设备节点

.申明

.杂项设备

.生成杂项设备的设备节点

一、申明

. linux到2.6版本的时候,改动巨大,网上有些资料室针对以前的版本的,大家看到老版本相关的资料,直接跳过即可。
. 现在2.6版本以前的基本都废弃了,不用管了,学了也没有。学习要“以始为终”,学了之后是为了在实际工作中应用的,那么工作中已经用不到的知识就不要浪费时间了。
杂项设备
1.杂项设备可以说对一部分字符设备的封装,还有一部分不好归类驱动也归到杂项设备。
2.为什么引入杂项设备
第一、节省主设备号
如果所有的驱动都是用字符设备,那么素偶有的设备号很快就用完了、总共就255个主设备号。
第二、驱动写起来相对简单
如果直接使用封装好的杂项设备,那么就可以减少一步注册主设备号的过程
杂项设备初始化文件
1.杂项设备初始化部分源文件"drivers/char/misc.c"这一部分通过Makefile可知,是强制编译的。而且是linux官方(不是三星官方)出来的时候就带的,为了一些简单的驱动更容易实现。
2.这部分了解即可,里面的内容也比较简单,就是给字符驱动做一个简单的封装

注册文件
.杂项设备注册头文件

root@ubuntu:/home/topeet/android4.0/iTop4412_Kernel_3.0/include/linux# vim miscdevice.h 

.结构体miscdevice以及注册函数如下所示
在这里插入图片描述
上图常用参数
.minor设备号
.name生成设备节点的名称
.fops指向一个设备节点文件
杂项设备-内核文件的结构体
.linux中一切皆文件,上层调用底层也是通过读取文件的方式
注册设备节点,本质也是新建一个特殊的文件,包含文件名,打开,关闭,操作等函数
.包含文件结构体的头文件是"include/linux/fs.h"
.文件的结构体file_operations如下所示
在这里插入图片描述
1.文件的结构体file_operations参数很多,根据需求选择。
必选的是参数是
.owner一般是THIS_MODULE
.open打开文件函数
.release关闭文件函数
.这里在必选之外使用参数(为了介绍接下来的GPIO操作)
.unlocked_ioctl对GPIO的操作,应用向底层驱动传值

杂项设备
1.驱动代码,在probe_linux_module基础上写devicenode_linux_module驱动
写代码的时候,注意一下函数调用顺序
.编译,在开发板上加载驱动生成设备节点
在/dev中查看时候生成设备节点

实验

1.新建devicenode_linux_module.c文件

topeet@ubuntu:~/Code/devicenode_linux_module$ vim devicenode_linux_module.c 
#include <linux/init.h>
#include <linux/module.h>

/*驱动注册的头文件,包含驱动的结构体和注册和卸载的函数*/
#include <linux/platform_device.h>
/*注册杂项设备头文件*/
#include <linux/miscdevice.h>
/*注册设备节点的文件结构体*/
#include <linux/fs.h>

#define DRIVER_NAME "hello_ctl"
#define DEVICE_NAME "hello_ctl123"
MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("TOPEET");

static long hello_ioctl( struct file *files, unsigned int cmd, unsigned long arg){

	
	printk("cmd is %d,arg is %d\n",cmd,arg);
	return 0;
}

static int hello_release(struct inode *inode, struct file *file){
	printk(KERN_EMERG "hello release\n");
	return 0;
}

static int hello_open(struct inode *inode, struct file *file){
	printk(KERN_EMERG "hello open\n");
	return 0;
}

static struct file_operations hello_ops = {
	.owner = THIS_MODULE,
	.open = hello_open,
	.release = hello_release,
	.unlocked_ioctl = hello_ioctl,
};

static  struct miscdevice hello_dev = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = DEVICE_NAME,
	.fops = &hello_ops,
};


static int hello_probe(struct platform_device *pdv){
	
	printk(KERN_EMERG "\tinitialized\n");
	misc_register(&hello_dev);
	
	return 0;
}

static int hello_remove(struct platform_device *pdv){
	
	printk(KERN_EMERG "\tremove\n");
	misc_deregister(&hello_dev);
	return 0;
}

static void hello_shutdown(struct platform_device *pdv){
	
	;
}

static int hello_suspend(struct platform_device *pdv,pm_message_t pmt){
	
	return 0;
}

static int hello_resume(struct platform_device *pdv){
	
	return 0;
}

struct platform_driver hello_driver = {
	.probe = hello_probe,
	.remove = hello_remove,
	.shutdown = hello_shutdown,
	.suspend = hello_suspend,
	.resume = hello_resume,
	.driver = {
		.name = DRIVER_NAME,
		.owner = THIS_MODULE,
	}
};


static int hello_init(void)
{
	int DriverState;
	
	printk(KERN_EMERG "HELLO WORLD enter!\n");
	DriverState = platform_driver_register(&hello_driver);
	
	printk(KERN_EMERG "\tDriverState is %d\n",DriverState);
	return 0;
}


static void hello_exit(void)
{
	printk(KERN_EMERG "HELLO WORLD exit!\n");
	
	platform_driver_unregister(&hello_driver);	
}

module_init(hello_init);
module_exit(hello_exit);

2.新建Makefile

#!/bin/bash
#通知编译器我们要编译模块的哪些源码
#这里是编译itop4412_hello.c这个文件编译成中间文件itop4412_hello.o
obj-m += devicenode_linux_module.o 

#源码目录变量,这里用户需要根据实际情况选择路径
#作者是将Linux的源码拷贝到目录/home/topeet/android4.0下并解压的
KDIR := /home/topeet/android4.0/iTop4412_Kernel_3.0

#当前目录变量
PWD ?= $(shell pwd)

#make命名默认寻找第一个目标
#make -C就是指调用执行的路径
#$(KDIR)Linux源码目录,作者这里指的是/home/topeet/android4.0/iTop4412_Kernel_3.0
#$(PWD)当前目录变量
#modules要执行的操作
all:
	make -C $(KDIR) M=$(PWD) modules
		
#make clean执行的操作是删除后缀为o的文件
clean:
	rm -rf *.o

在4412开发板执行:insmod devicenode_linux_module.ko

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值