【发布时间】:2016-11-23 13:28:36
【问题描述】:
在我使用的旧设备驱动程序之一中,我的结构格式如下 -
struct inner_struct_containing_cdev {
struct cdev cdev;
....
....
/* more variables */
};
struct outer_struct {
/* because I need 10 devices */
struct inner_struct_containing_cdev inner[10];
....
....
/* more variables which are common to all the 10 devices. */
};
现在在我的 init_module 中,我正在调用 alloc_chrdev_region 和其他与分配相关的调用。现在在带有签名int open(struct inode *inode, struct file *filp) 的open 文件操作中,我可以访问cdev 结构,因此可以访问inner_struct_containing_cdev。但我想获得指向outer_struct 的指针。在open 调用中,我不知道我收到了指向inner_struct_containing_cdev 结构的哪个数组索引。
在这种情况下可以使用container_of 宏吗?还是需要重新设计结构?
目前我正在使用全局变量来处理这种情况。但这会阻止我进行多次实例化。
【问题讨论】:
标签: c linux linux-kernel driver