我正在研究 Linux 中的驱动程序。我正在努力获得一些 /sys 文件属性,这将使事情变得更好。在传递这些属性要说明的内容时,属性函数必须能够访问驱动程序存储的某些数据。由于事物的制作和存储方式,我认为我可以使用device_private *p
来自 device_create() 的 struct device 的成员。基本上,它是这样的:
for (i = 0; i < total; i++) {
pDevice = device_create(ahcip_class, NULL, /*no parent*/
MKDEV(AHCIP_MAJOR, AHCIP_MINOR + i), NULL, /*no additional info*/
DRIVER_NAME "%d", AHCIP_MINOR + i);
if (IS_ERR(pDevice)) {
ret = PTR_ERR(pDevice);
printk(KERN_ERR "%s:%d device_create failed AHCIP_MINOR %d\n",
__func__, __LINE__, (AHCIP_MINOR + i));
break;
}
mydevs[i].psysfs_dev = pDevice;
ret = sysfs_create_group(&pDevice->kobj, &attr_group);
if (!ret) {
pr_err("%s:%d failed in making the device attributes\n",
__func__, __LINE__);
goto build_udev_quick_out;
}
}
这还没有显示分配到device_private
指针中,但这就是我要去的地方。在此类下制造的每个新设备都需要与该组相同的属性。这是我从“概念证明”开始的单一属性
static ahcip_dev *get_ahcip_dev(struct kobject *ko)
{
ahcip_dev *adev = NULL;
struct device *pdev = container_of(ko, struct device, kobj);
if (!pdev) {
pr_err("%s:%d unable to find device struct in kobject\n",
__func__, __LINE__);
return NULL;
}
/* **** problem dereferencing p **** */
adev = (ahcip_dev*)pdev->p->driver_data;
/* return the pointer anyway, but if it's null, print to klog */
if (!adev)
pr_err("%s:%d no ahcip_dev, private driver data is NULL\n",
__func__, __LINE__);
/* **** again problem dereferencing p **** */
return pdev->p->(ahcip_dev*)driver_data; // <--- problem here
}
static ssize_t pxis_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buff)
{
u32 pi = 0;
ahcip_dev *adev = get_ahcip_dev(kobj);
/* get_ahcip_dev() will print what happened, this needs to return
* error code
*/
if (!adev)
return -EIO;
pi = adev->port_index;
return sprintf(buff, "%08x\n", get_port_reg(adev->hba->ports[pi], 0x10));
}
我认为,由于device_create()
返回 astruct device*
并且我正在使用它来制作设备组,因此struct kobject*
进入pxis_show
的是由device_create
. 如果这是真的,那么我应该能够将一些私有数据填充到该对象中,并在访问 /sys 文件时使用它。但是,当上面标记的代码行取消引用p
成员时,我会从 gcc中获得不完整类型的取消引用指针。我已经确定这是不完整的struct device_private
成员,struct device
但为什么呢?我应该使用不同的结构吗?这似乎是内核真正内部的东西。