Hi All,

I want register the character device with sysfs and want a device file to be
created. The function device_create() does the same, but it seems to be
failing in my case.

Here is the part of the code

#define MYMAJOR 200

struct mydevice {
    struct device *parent;
    struct device *this_device;
    dev_t dev;
    char name[20];
};

struct mydevice my_misc = {
    .name = "myzero",
    .parent = NULL,
};

struct class *my_class;
struct mydevice zdev;

static int __init myudev_init(void)
{
    int err = 0;
    zdev.dev = MKDEV(MYMAJOR, 10);

    my_class = class_create (THIS_MODULE, "mymisc");
    err = PTR_ERR(my_class);
    if (IS_ERR(my_class))
        goto class_err;

    zdev.this_device = device_create(my_class, zdev.parent, zdev.dev, "%s",
zdev.name);
    err = PTR_ERR(zdev.this_device);
    if (IS_ERR(zdev.this_device))
        goto device_err;

    return 0;
device_err:
    class_destroy(my_class);
class_err:
    return err;
}


While debugging the code, I could findout that device_add() is the failing

int device_add(struct device *dev)
{
    struct device *parent = NULL;
    struct class_interface *class_intf;
    int error;

    dev = get_device(dev);             <<<<<<<<<<<<<<<<<<< here the code is
failing
    if (!dev || !strlen(dev->bus_id)) {
        error = -EINVAL;
        goto Done;
    }
.........
.........
}

struct device *get_device(struct device *dev)
{
    return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
}

Why does the get_device() is returning NULL eventhough the dev is not NULL,

Breakpoint 5, get_device (dev=0xdec5c8b0) at drivers/base/core.c:914
914    {
(gdb) print *dev
$4 = {klist_children = {k_lock = {raw_lock = {slock = 771}}, k_list = {next
= 0xdec5c9f4, prev = 0xdf8b3664}, get = 0xc0247640 <klist_children_get>,
    put = 0xc0247670 <klist_children_put>}, knode_parent = {n_klist =
0xdf862a14, n_node = {next = 0xdf862a18, prev = 0xdf862a18}, n_ref =
{refcount = {counter = 1}},
    n_removed = {done = 0, wait = {lock = {raw_lock = {slock = 0}},
task_list = {next = 0xdec5c8dc, prev = 0xdec5c8dc}}}}, knode_driver =
{n_klist = 0xdc7c4684, n_node = {
      next = 0xdc7c4688, prev = 0xdc7c4688}, n_ref = {refcount = {counter =
1}}, n_removed = {done = 0, wait = {lock = {raw_lock = {slock = 0}},
task_list = {
          next = 0xdec5c8fc, prev = 0xdec5c8fc}}}}, knode_bus = {n_klist =
0xdec7073c, n_node = {next = 0xdf879c6c, prev = 0xdf862a6c}, n_ref =
{refcount = {counter = 1}},
    n_removed = {done = 0, wait = {lock = {raw_lock = {slock = 0}},
task_list = {next = 0xdec5c91c, prev = 0xdec5c91c}}}}, parent = 0xdf862a14,
kobj = {
    name = 0xdfa4b110 "0:0:0:0", kref = {refcount = {counter = 16}}, entry =
{next = 0xdec5ca5c, prev = 0xdf862a94}, parent = 0xdf862a8c, kset =
0xdec05280,
    ktype = 0xc03f306c, sd = 0xdf81bc30, state_initialized = 1,
state_in_sysfs = 1, state_add_uevent_sent = 1, state_remove_uevent_sent =
0},
  bus_id = "0:0:0:0", '\0' <repeats 12 times>, type = 0xc03f3c18,
uevent_suppress = 0, sem = {lock = {raw_lock = {slock = 1542}}, count = 1,
wait_list = {next = 0xdec5c970,
      prev = 0xdec5c970}}, bus = 0xc03f3be0, driver = 0xe0815724,
driver_data = 0xdf8b3a00, platform_data = 0x0, power = {power_state = {event
= 0}, can_wakeup = 0,
    should_wakeup = 0, sleeping = false, entry = {next = 0xdec5cabc, prev =
0xdf862af4}}, dma_mask = 0x0, coherent_dma_mask = 0, dma_parms = 0x0,
dma_pools = {
    next = 0xdec5c9a8, prev = 0xdec5c9a8}, dma_mem = 0x0, archdata =
{acpi_handle = 0x0}, devres_lock = {raw_lock = {slock = 0}}, devres_head =
{next = 0xdec5c9bc,
    prev = 0xdec5c9bc}, node = {next = 0xdec5c9c4, prev = 0xdec5c9c4}, class
= 0x0, devt = 0, groups = 0x0, release = 0}

kobject_get() should return the dev->kobj which is again not NULL
(gdb) print dev->kobj
$5 = {name = 0xdfa4b110 "0:0:0:0", kref = {refcount = {counter = 16}}, entry
= {next = 0xdec5ca5c, prev = 0xdf862a94}, parent = 0xdf862a8c, kset =
0xdec05280,
  ktype = 0xc03f306c, sd = 0xdf81bc30, state_initialized = 1, state_in_sysfs
= 1, state_add_uevent_sent = 1, state_remove_uevent_sent = 0}



and to_dev() is                  #define to_dev(obj) container_of(obj,
struct device, kobj)

So i am not getting why get_device() is returing NULL?
Please help....


Thanks and Regards,
Prasad.

Reply via email to