当前位置: 首页 > article >正文

ARM驱动学习之8 动态申请字符类设备号

                            ARM驱动学习之8 动态申请字符类设备号

KernelCode:
• 字符设备函数在文件“include/linux/fs.h”中
• alloc_chrdev_region() 是动态分配主次设备号。
• 宏定义MAJOR提取dev_t数据中的主设备号

源码:
/**
 * alloc_chrdev_region() - register a range of char device numbers
 * @dev: output parameter for first assigned number
 * @baseminor: first of the requested range of minor numbers 
 * @count: the number of minor numbers required
 * @name: the name of the associated device or driver
 *
 * Allocates a range of char device numbers.  The major number will be
 * chosen dynamically, and returned (along with the first minor number)
 * in @dev.  Returns zero or a negative error code.
 */

int alloc_chrdev_region(dev_t *dev,
                        unsigned baseminor, 
                        unsigned count,
			            const char *name)
{
	struct char_device_struct *cd;
	cd = __register_chrdev_region(0, baseminor, count, name);
	if (IS_ERR(cd))
		return PTR_ERR(cd);
	*dev = MKDEV(cd->major, cd->baseminor);
	return 0;
}

/**
 * unregister_chrdev_region() - return a range of device numbers
 * @from: the first in the range of numbers to unregister
 * @count: the number of device numbers to unregister
 *
 * This function will unregister a range of @count device numbers,
 * starting with @from.  The caller should normally be the one who
 * allocated those numbers in the first place...
 */
void unregister_chrdev_region(dev_t from, unsigned count)
{
	dev_t to = from + count;
	dev_t n, next;

	for (n = from; n < to; n = next) {
		next = MKDEV(MAJOR(n)+1, 0);
		if (next > to)
			next = to;
		kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
	}
}


1.在scdev_init如下:
DEVICE_NAME改为“ascdev”
ret = alloc_chrdev_region(&num_dev,numdev_minor,DEVICE_MINOR_NUM,DEVICE_NAME);
numdev_magor = MAJOR(num_dev);
printk(KERN_EMERGE "numdev_magor is %d \n",numdev_magor);


• 加载运行
– 使用命令“cat /proc/devices”查看
– 动态加载模块之后再查看设备号

源码:

#include <linux/init.h>
/*包含初始化宏定义的头文件,代码中的module_init和module_exit在此文件中*/
#include <linux/module.h>
/*包含初始化加载模块的头文件,代码中的MODULE_LICENSE在此头文件中*/

/*定义module_param module_param_array的头文件*/
#include <linux/moduleparam.h>
/*定义module_param module_param_array中perm的头文件*/
#include <linux/stat.h>

/*字符设备函数*/
#include <linux/fs.h>
/*MDKDEV转换设备号数据类型宏定义*/
#include <linux/kdev_t.h>
/*定义字符设备的结构体*/
#include <linux/cdev.h>

#define DEVICE_NAME "Ascdev"
#define DEVICE_MINOR_NUM 2
#define DEV_MAJOR 0
#define DEV_MINOR 0


MODULE_LICENSE("Dual BSD/GPL");
/*声明是开源的,没有内核版本限制*/
MODULE_AUTHOR("iTOPEET_dz");
/*声明作者*/

static int numdev_major = DEV_MAJOR ;//主设备号
static int numdev_minor = DEV_MINOR ;//次设备号

module_param(numdev_major,int,S_IRUSR);
module_param(numdev_minor,int,S_IRUSR);



static int Ascdev_init(void)
{
	int ret = 0;
	dev_t num_dev;
	if(numdev_major){
		num_dev = MKDEV(numdev_major,numdev_minor);
		//宏命令,用于处理各种设备号相关的数据
		//设备注册
		ret = register_chrdev_region(num_dev,DEVICE_MINOR_NUM,DEVICE_NAME);
		if(ret < 0)
		{
			 printk(KERN_EMERG "register_chrdev_region req is %d is failed \n",num_dev );
			 return -1;
		}
		printk(KERN_EMERG "register_chrdev_region %d is success \n",numdev_major);	
	}
	else{
		ret = alloc_chrdev_region(&num_dev,numdev_minor,DEVICE_MINOR_NUM,DEVICE_NAME);
		if(ret < 0)
		{
			 printk(KERN_EMERG "alloc_chrdev_region req is %d is failed \n",num_dev );
			 return -1;
		}
		numdev_major = MAJOR(num_dev);
		printk(KERN_EMERG "numdev_major is %d \n",numdev_major);
	}
		
	printk(KERN_EMERG "Ascdev enter!\n");
	/*打印信息,KERN_EMERG表示紧急信息*/
	return 0;
}

static void Ascdev_exit(void)
{	
	unregister_chrdev_region(MKDEV(numdev_major,numdev_minor),DEVICE_MINOR_NUM);
	printk(KERN_EMERG "Ascdev exit!\n");
}


module_init(Ascdev_init);
/*初始化函数*/
module_exit(Ascdev_exit);
/*卸载函数*/

Makefile:

#!/bin/bash
#通知编译器我们要编译模块的哪些源码
#这里是编译itop4412_hello.c这个文件编译成中间文件itop4412_hello.o
#obj-m += mini_linux_module.o 
obj-m += request_cdev_num.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 *.mod.c *.o *.order *.ko *.mod.o *.symvers


http://www.kler.cn/news/306109.html

相关文章:

  • sqli-labs Basic Challenge Less_1 通关指南
  • 【AI学习】陶哲轩在 2024 年第 65 届国际数学奥林匹克(IMO)的演讲:AI 与数学
  • 基于YOLOV8+Pyqt5光伏太阳能电池板目标检测系统
  • 使用docker配置wordpress
  • SQLite的入门级项目学习记录(一)
  • Kafka 消息丢失如何处理?
  • kubeadm方式安装k8s续:
  • 【Unity实战】SO反序列化正确姿势
  • 【智路】智路OS Airos Edge 2.0 Quick Start
  • 利用 FastAPI 和 Jinja2 模板引擎快速构建 Web 应用
  • 通过logstash同步elasticsearch数据
  • 【资料分析】常见的坑
  • 文件外发怎么保证安全
  • sqli-labs靶场自动化利用工具——第1关
  • 在Coontroller层中我们经常使用的@RequestParam, @PathVariable, @RequestBody ,区别以及各自的使用场景
  • golang实现从服务器下载文件到本地指定目录
  • 简单接口自动化框架实现(Python+requests+pytest)
  • _Array类,类似于Vector,其实就是_string
  • 编写程序模版的搭建
  • Android Kotlin 中的 `groupBy` 方法详解
  • 手机玩机常识____展讯芯片刷机平台ResearchDownload的一些基本常识与问题解决
  • 基于CosyVoice的多语言语音合成技术解析
  • STM32(十三):通信协议——USART串口协议
  • React源码学习(一):如何学习React源码
  • Python学习——【1.2】数据类型、数据类型转换
  • yjs04——matplotlib的使用(多个坐标图)
  • Java网络编程 TCP通信(Socket 与 ServerSocket)
  • (批处理)设置延时+设置关机倒计时
  • Flink CEP(复杂事件处理)高级进阶
  • 【大数据方案】智慧大数据平台总体建设方案书(word原件)