栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

linux驱动学习——字符设备号

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

linux驱动学习——字符设备号

字符设备号本质就是一个32位的无符号整型值。高12位为主设备号;低20位为次设备号。

  • 查看设备号
cat /proc/devices
4.1、构造设备号

源码路径: include/linux/kdev_t.h

#define MINORBITS	20
#define MINORMASK	((1U << MINORBITS) - 1)

#define MAJOR(dev)	((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev)	((unsigned int) ((dev) & MINORMASK))
#define MKDEV(ma,mi)	(((ma) << MINORBITS) | (mi))
4.2、注册/注销设备号
int register_chrdev_region(dev_t from, unsigned count, const char *name)
void unregister_chrdev_region(dev_t from, unsigned count)
4.3、示例代码
#include
#include
#include
#include

MODULE_LICENSE("GPL");
MODULE_AUTHOR("NEONAN");

static int major = 230;
static int minor = 0;
static dev_t devno;

static int hello_init(void)
{
	int result;

	printk("hello_initn");

	devno = MKDEV(major, minor);
	result = register_chrdev_region(devno, 1, "dev_test");
	if(result < 0){
		printk("register_chrdev_region fauiln");
		return result;
	}

	return 0;
}

static void hello_exit(void)
{
	printk("hello_exitn");
	unregister_chrdev_region(devno,1);
	return; 
}

module_init(hello_init);
module_exit(hello_exit);

编译后,注册该驱动

root@work:/home/work/dirve/dev_t# insmod dev_t.ko 
root@work:/home/work/dirve/dev_t# cat /proc/devices 
Character devices:
  ...
  230 dev_test
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/334694.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号