当前位置:网站首页>register_chrdev和cdev_init cdev_add用法区别
register_chrdev和cdev_init cdev_add用法区别
2022-06-30 15:44:00 【玻璃晴朗橘子辉煌】
register_chrdev
缺点:
一个内核只能有255个字符驱动程序
2.6内核后建议把register_chrdev展开.
以前想以主设备号为下标,在chrdevs里找到之前注册的file_operations,而现在以主设备号和次设备号来找到该结构体。
展开为1.register_chrdev_region注册区域
/alloc_chrdev_region region区域的意思为某个(主,次)到某个(主,次+n)设备号都对应这个驱动。以前是(主,0)到(主,255)都对应这个结构体一下子都占住位置,展开后可以缩小主设备号次设备号到某个次设备号范围
2.cdev_init
3.cdev_add
/** * __register_chrdev() - create and register a cdev occupying a range of minors * @major: major device number or 0 for dynamic allocation * @baseminor: first of the requested range of minor numbers * @count: the number of minor numbers required * @name: name of this range of devices * @fops: file operations associated with this devices * * If @major == 0 this functions will dynamically allocate a major and return * its number. * * If @major > 0 this function will attempt to reserve a device with the given * major number and will return zero on success. * * Returns a -ve errno on failure. * * The name of this device has nothing to do with the name of the device in * /dev. It only helps to keep track of the different owners of devices. If * your module name has only one type of devices it's ok to use e.g. the name * of the module here. */
int __register_chrdev(unsigned int major, unsigned int baseminor,
unsigned int count, const char *name,
const struct file_operations *fops)
{
struct char_device_struct *cd;
struct cdev *cdev;
int err = -ENOMEM;
cd = __register_chrdev_region(major, baseminor, count, name);
if (IS_ERR(cd))
return PTR_ERR(cd);
cdev = cdev_alloc();
if (!cdev)
goto out2;
cdev->owner = fops->owner;
cdev->ops = fops;
kobject_set_name(&cdev->kobj, "%s", name);
err = cdev_add(cdev, MKDEV(cd->major, baseminor), count);
if (err)
goto out;
cd->cdev = cdev;
return major ? 0 : cd->major;
out:
kobject_put(&cdev->kobj);
out2:
kfree(__unregister_chrdev_region(cd->major, baseminor, count));
return err;
}
cdev_init
/** * cdev_init() - initialize a cdev structure * @cdev: the structure to initialize * @fops: the file_operations for this device * * Initializes @cdev, remembering @fops, making it ready to add to the * system with cdev_add(). */
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
memset(cdev, 0, sizeof *cdev);
INIT_LIST_HEAD(&cdev->list);
kobject_init(&cdev->kobj, &ktype_cdev_default);
cdev->ops = fops;
}
----------------------使用--------------------------
/* nsc_gpio uses dev_dbg(), so needs this */
scx200_gpio_ops.dev = &pdev->dev;
if (major) {
devid = MKDEV(major, 0);
rc = register_chrdev_region(devid, MAX_PINS, "scx200_gpio");//如果指定了主设备号用这函数devid为从哪开始((major, 0)对应ops,(major, 1-255)其他不对应
} else {
rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");//第二个参数为次设备号基地址,MAX_PINS为个数
major = MAJOR(devid);
}
if (rc < 0) {
dev_err(&pdev->dev, "SCx200 chrdev_region err: %d\n", rc);
goto undo_platform_device_add;
}
cdev_init(&scx200_gpio_cdev, &scx200_gpio_fileops);
cdev_add(&scx200_gpio_cdev, devid, MAX_PINS);
class_create
device_create//如果让系统自动创建设备节点,需要创建类和device
return 0; /* succeed */
边栏推荐
- flinkcdc如果监控的数据库为mongo就必须是集群版吗
- 几百行代码实现一个 JSON 解析器
- Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
- Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
- Table responsive layout tips for super nice
- Simulation of two-color ball system to judge the winning situation
- 招标公告:2022年台州联通Oracle一体机和数据库维保服务项目
- topic: Privacy, Deception and Device Abuse
- How to connect the Internet Reading Notes - Summary
- Map reduce case super detailed explanation
猜你喜欢

Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world

Swagger's asp Net core web API help page

Parameter optimization - bias and variance

MySQL transaction / lock / log summary

婴儿认知学习所带来的启发,也许是下一代无监督机器学习的关键

19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project

Policy Center > Device and Network Abuse

Imeta | Ye Mao / Shi Yu reviewed the dynamic shuttle and ecological function of intracellular and extracellular genes in the environmental microbiome

Build cloud native observability capability suitable for organizations

Open source STM32 USB-CAN project
随机推荐
MySQL开放远程连接权限的两种方法
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
抖快B为啥做不好综艺
MySQL8.0开启远程连接权限的方法步骤
【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment
云和恩墨中标天津滨海农村商业银行2022-2023年度Oracle维保项目
交调与互调的区别
ArcMap operation series: 80 plane to latitude and longitude 84
Asp.NetCore利用缓存使用AOP方式防止重复提交
Under the pressure of technology, you can quickly get started with eth smart contract development, which will take you into the ETH world
超 Nice 的表格响应式布局小技巧
Unsupported major. minor version 52.0
Swagger's asp Net core web API help page
互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
Reptile (1) - Introduction to basic reptile theory
With as subquery in Oracle
flink sql cdc 同步sqlserver 报错什么原因啊