我正在研究Linux中的驅動程序。我正在努力獲得一些/ sys文件屬性,以使事情更好。在傳遞這些屬性時,屬性函數必須能夠訪問驅動程序存儲的一些數據。由於看起來如何製作和存儲,我以爲我可以使用來自device_create()的struct device的成員device_private *p
。基本上,它是這樣的:如何在struct device中解除引用device_private
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()
返回struct device*
和我使用的是使設備組,也就是進入了struct kobject*
pxis_show
是由device_create
製作的設備結構的成員。如果這是真的,那麼我應該能夠將一些私人數據填充到該對象中,並在/ sys文件被訪問時使用它。但是,當上面標記的代碼行取消引用p
成員時,我會從gcc取消引用不完整類型指針的。我確定這是struct device
的struct device_private
成員不完整,但爲什麼?我應該使用不同的結構嗎?這似乎是內核真正內部的東西。
我在想這個,並開始看到我應該通過它作爲參數。謝謝。 –