linked list question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi everyone,

I know that this question may sound so simple to most of you guys but
I could not quite figure out how to use it so I wanted to ask for some
advice.

I am trying to write a kernel module and I do not know how many
devices of this module will be installed prior to actual loading of
this module.
So the user can specify how many devices they want to use when they
load the module but these devices will be exactly same but they will
be available as more than one devices and they will have all same
major number but their minor number will be different and this way I
can differentiate the devices.

My question is that these device will need a list but I cannot specify
how many I will need when I write programs so I will need to rely on
dynamic allocation mechanism and this is where I don't quite
understand how to use the kernel provided list with this dynamic
allocation mechanism.

To make my question more solid, here is the part of my code.
Obviously, this does not work right now and the code fails when it
searches in __find_ztyent() but I am not exactly sure where I am
missing. I think that the way I use INIT_LIST_HEAD is not right but I
have not figured out how I should use them. So please give me some
advice or pointer to the references. Thank you in advance.

struct _ztyent {
   int zty_id;
   int id;
   struct list_head ztylist;
};
struct _ztyent *zty_head;

static int init_zty(void)
{
   register_chrdev(ZTY_DEV_MAJOR, "zty", &zty_fops);

   /* allocate and initialize devices (zty0 - zty3) */
   zty_head = kmalloc(zty_nr_devs * sizeof(struct _ztyent), GFP_KERNEL);
   for (i = 0; i < zty_nr_devs; i++)
       INIT_LIST_HEAD(&(zty_head[i].ztylist));

printk("Initialized zty_head list\n");

   return 0;
}

inline struct _ztyent *__find_ztyent(int dev_id, int id)
{
   struct _ztyent *tmp;

   if (!list_empty(&(zty_head[dev_id].ztylist))) {
       list_for_each_entry(tmp, &(zty_head[dev_id].ztylist), ztylist)
           if (tmp->id == id)
               return tmp;
   }
   return NULL;
}

Then, finally I call __find_ztyent from my ztydev_open() function and
here I first check if there is any duplicate item in the list.

static int ztydev_open(struct inode *inode, struct file *filp)
{

   if ((tmp = __find_ztyent(zty_id, id)) != NULL) {
       printk("There is already zty %d for id %d\n", zty_id, id);
       return -ENOENT;
   }

  ......

   list_add(tmp->ztylist, &(zty_head[zty_id].ztylist));
}

--
YES, JESUS LOVES YOU, TOO.
For God so loved the world that he gave his one and only Son, that
whoever believes in him shall not perish but have eternal life(John
3:16).

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux