/**
* 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(, baseminor, count, name);
if (IS_ERR(cd))
return PTR_ERR(cd);
*dev = MKDEV(cd->major, cd->baseminor);
return ;
}
 static struct char_device_struct *
__register_chrdev_region(unsigned int major, unsigned int baseminor,
int minorct, const char *name)
{
struct char_device_struct *cd, **cp;
int ret = ;
int i; cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
if (cd == NULL)
return ERR_PTR(-ENOMEM); mutex_lock(&chrdevs_lock); /* temporary */
if (major == ) {
for (i = ARRAY_SIZE(chrdevs)-; i > ; i--) {
if (chrdevs[i] == NULL)
break;
} if (i == ) {
ret = -EBUSY;
goto out;
}
major = i;
ret = major;
} cd->major = major;
cd->baseminor = baseminor;
cd->minorct = minorct;
strlcpy(cd->name, name, sizeof(cd->name)); i = major_to_index(major); for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
if ((*cp)->major > major ||
((*cp)->major == major &&
(((*cp)->baseminor >= baseminor) ||
((*cp)->baseminor + (*cp)->minorct > baseminor))))
break; /* Check for overlapping minor ranges. */
if (*cp && (*cp)->major == major) {
int old_min = (*cp)->baseminor;
int old_max = (*cp)->baseminor + (*cp)->minorct - ;
int new_min = baseminor;
int new_max = baseminor + minorct - ; /* New driver overlaps from the left. */
if (new_max >= old_min && new_max <= old_max) {
ret = -EBUSY;
goto out;
} /* New driver overlaps from the right. */
if (new_min <= old_max && new_min >= old_min) {
ret = -EBUSY;
goto out;
}
} cd->next = *cp;
*cp = cd;
mutex_unlock(&chrdevs_lock);
return cd;
out:
mutex_unlock(&chrdevs_lock);
kfree(cd);
return ERR_PTR(ret);
}

char device的更多相关文章

  1. Is an MTD device a block device or a char device?

    转:http://www.linux-mtd.infradead.org/faq/general.html#L_mtd_what Note, you can find Ukranian transla ...

  2. Linux MTD (Memory Technology Device) subsystem analysis -For Atheros char device

    Linux MTD (Memory Technology Device) subsystem analysis For Atheros char device 读了Linux MTD 源代码分析 对这 ...

  3. Char device registration

    The kernel uses structures of type struct cdev to represent char devices internally. Include <lin ...

  4. <<linux device driver,third edition>> Chapter 3:Char Drivers

    The Internal Representation of Device Numbers Within the kernel,the dev_t type(defined in linux/type ...

  5. Class create, device create, device create file (转)

    来自:http://www.hovercool.com/en/Class_create,_device_create,_device_create_file 开始写Linux设备驱动程序的时候,很多时 ...

  6. Class create, device create, device create file【转】

    来自:http://www.hovercool.com/en/Class_create,_device_create,_device_create_file 开始写Linux设备驱动程序的时候,很多时 ...

  7. Writing device drivers in Linux: A brief tutorial

    “Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers?”  ...

  8. Linux内核device结构体分析

    1.前言 Linux内核中的设备驱动模型,是建立在sysfs设备文件系统和kobject上的,由总线(bus).设备(device).驱动(driver)和类(class)所组成的关系结构,在底层,L ...

  9. FFmpeg学习3:播放音频

    参考dranger tutorial,本文将介绍如何使用FFmpeg解码音频数据,并使用SDL将解码后的数据输出. 本文主要包含以下几方面的内容: 关于播放音频的需要的一些基础知识介绍 使用SDL2播 ...

随机推荐

  1. [xsy2300]好题

    题意:有一棵树,每个节点有颜色,要找出最小的连通块使得其中的点至少有$k$种不同的颜色,只需输出这个最小连通块的大小 因为$k$很小,所以如果颜色只有$k$种,我们可以直接状压DP,设$f_{i,j} ...

  2. 软件配置篇-MySQL下载及安装

    1.进入MySQL官网下载 下载地址:https://dev.mysql.com/downloads/mysql/ 选择合适的版本下载. 2.安装MySQL 解压后文件夹一般名为:mysql-x.x. ...

  3. Problem P: 素数求和

    #include<stdio.h> int main() { ; scanf("%d",&n); n>=&&n<=; ;i<= ...

  4. iOS开发笔记_4自定义TabBar

    新博客:http://www.liuchendi.com 好多APP都使用的是自定义的TabBar,那这个功能应该如何实现呢?首先应该解决的问题就是,加载NavigationController的时候 ...

  5. “DllRegisterServer的调用失败”问题解决办法(转)

    在日常的工作中,用regsvr32 命令注册dll组件是,会碰到模块"xxx.dll"已加载,但DllRegisterServer的调用失败.特别是再在xp的系统上能正确注册,但是 ...

  6. 深入理解Vue父子组件通讯的属性和事件

    在html中使用元素,会有一些属性,如class,id,还可以绑定事件,自定义组件也是可以的.当在一个组件中,使用了其他自定义组件时,就会利用子组件的属性和事件来和父组件进行数据交流. 父子组件之间的 ...

  7. [Python爬虫] 之十八:Selenium +phantomjs 利用 pyquery抓取电视之家网数据

    一.介绍 本例子用Selenium +phantomjs爬取电视之家(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓 ...

  8. 【机器学习 & 数据挖掘 通俗介绍】

    如何向小白介绍何谓机器学习和数据挖掘?买回芒果他就懂了 JasonZheng • 2013-01-07 22:18   买芒果 嘴馋的你想吃芒果了,于是你走到水果摊,挑了几个让老板过过秤,然后你再根据 ...

  9. How to rebuild RPM database on a Red Hat Enterprise Linux system?

    本文是笔者最近遇到的一个故障的处理过程,解决方案是Rebuild RPM 的DB,后面内容其实是REDHAT官方的solutions,不过我遇到的现象和解决方案都与官方有点出入,故一直帖出来: 我遇到 ...

  10. Windows下pip安装scipy报错no lapack/blas resources found

    Windows下升级了pandas,但是发现scipy包随后引用出错,后来确认需重新安装scipy, 在用pip安装scipy出现no lapack/blas resources found的错误. ...