0
votes

I am trying to develop my first linux driver, but I got a blocking issue using the API kobj_to_dev in order to get a "struct device*" reference from a "struct kobject*" passed as parameter to a "show" event of a r/o attribute.

I would better explain the scenario describing only some of the instructions I have written to fall into the above problem:

  • static struct kobj_attribute myOption_attr = __ATTR_RO(myOption)

  • static struct attribute *my_attrs[] = { &myOption_attr.attr, NULL }

  • static struct attribute_group my_attr_group = { .attrs = my_attrs }

  • struct device* myDev = device_create( ... SOME PARAMETERS ... )

  • struct kobject* myKObj = kobject_create_and_add("mySettings", &myDev->kobj)

  • sysfs_create_group(myKObj, &my_attr_group)

  • static ssize_t pinNumber_show( struct kobject *kobj, struct kobj_attribute *attr, char *buf ) { struct device *kobjDev = kobj_to_dev(kobj);

    pr_info( "Expected: %p - Passed: %p %d:%d.\n", myDev, kobjDev, MAJOR(kobjDev->devt) MINOR(kobjDev->devt) ); }

After installing the driver module I successfully get the virtual file /sys/class/myDev/mySettings/myOption .

When I run then command "cat /sys/class/myDev/mySettings/myOption" inside the /var/log/kern.log I get to different pointers value for myDev and kobjDev, and the major and minor number are 0.

Could anybody point me to what I missed?

Thank you so much!

1

1 Answers

0
votes

I found the solution on my own, I had to change

kobj_to_dev(kobj)

to

kobj_to_dev(kobj->parent)

Thank you anyway! :)