当前位置:网站首页>register_ Chrdev and CDEV_ init cdev_ Add usage differences
register_ Chrdev and CDEV_ init cdev_ Add usage differences
2022-06-30 16:40:00 【Glass clear orange brilliant】
register_chrdev
shortcoming :
A kernel can only have 255 Character driver
2.6 After the kernel, it is recommended to register_chrdev an .
Previously, I wanted to take the main equipment number as the subscript , stay chrdevs Find the previously registered file_operations, Now find the structure with the main equipment number and the secondary equipment number .
Begin to 1.register_chrdev_region Registration area
/alloc_chrdev_region region Area means a certain ( Lord , Time ) To a certain ( Lord , Time +n) The device number corresponds to this driver . It used to be ( Lord ,0) To ( Lord ,255) All corresponding to this structure all at once , After expansion, you can narrow the range from the primary equipment number to a secondary equipment number
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;
}
---------------------- Use --------------------------
/* 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");// If the master device number is specified, use this function devid For where to start ((major, 0) Corresponding ops,(major, 1-255) Other mismatches
} else {
rc = alloc_chrdev_region(&devid, 0, MAX_PINS, "scx200_gpio");// The second parameter is the base address of the secondary device number ,MAX_PINS Number of
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// If you let the system automatically create an equipment node , You need to create classes and device
return 0; /* succeed */
边栏推荐
- How to connect the Internet Reading Notes - Summary
- 更多龙蜥自研特性!生产可用的 Anolis OS 8.6 正式发布
- CloudXR如何推动XR的未来发展
- [附下载]渗透测试神器Nessus安装及使用
- Arcmap操作系列:80平面转经纬度84
- topic: Privacy, Deception and Device Abuse
- 荣盛生物冲刺科创板:拟募资12.5亿 年营收2.6亿
- flink sql cdc 同步sqlserver 报错什么原因啊
- 24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);
- MySQL transaction / lock / log summary
猜你喜欢

What role does "low code" play in enterprise digital transformation?

备战数学建模33-灰色预测模型2
![Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.](/img/c1/99ad29789a669c4498fb93ce1fb009.png)
Warning: [antd: Menu] `children` will be removed in next major version. Please use `items` instead.

牛客网:最小花费爬楼梯

Hundreds of lines of code to implement a JSON parser

How the edge computing platform helps the development of the Internet of things

居家办公浅谈远程协助快速提效心得 | 社区征文

RT-Thread 堆区大小设置

KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system

MySQL transaction / lock / log summary
随机推荐
What is XR extended reality and what are the XR cloud streaming platforms
招标公告:深圳市财政局数据库异地灾备项目
flinkcdc如果监控的数据库为mongo就必须是集群版吗
中国传奇教授李泽湘,正在批量制造独角兽
Dart: string replace related methods to solve replacement characters
Explain in detail the use of for loop, break and continue in go language
24:第三章:开发通行证服务:7:自定义异常(来表征程序中出现的错误);创建GraceExceptionHandler来全局统一处理异常(根据异常信息,构建对应的API统一返回对象的,JSON数据);
MicroBlaze serial port learning · 2
MySQL开放远程连接权限的两种方法
CVPR 2022 - Tesla AI proposed: generalized pedestrian re recognition based on graph sampling depth metric learning
快照和备份
MC Instruction Decoder
KDD 2022 | how far are we from the general pre training recommendation model? Universal sequence representation learning model unisrec for recommender system
'&lt;', hexadecimal value 0x3C, is an invalid 问题解决
The inspiration from infant cognitive learning may be the key to the next generation of unsupervised machine learning
Carry two load balancing notes and find them in the future
MySQL master-slave configuration
Unsupported major.minor version 52.0
2022蓝桥杯国赛B组-2022-(01背包求方案数)
大学生研究生毕业找工作,该选择哪个方向?