2

I am new to kernel driver programming and would like to understand few aspects.

In the below code from http://lxr.free-electrons.com/source/drivers/i2c/busses/i2c-ocores.c?v=3.19

static int ocores_i2c_probe(struct platform_device *pdev)
{
   struct ocores_i2c *i2c;
    i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
}
  1. pdev is pointer to a platform device. The content of the structure to which pdev is pointing to is already created when platform device is initialized and driver core will pass that information to this probe function. ?? This is the same with pci dev structure where driver core is passing pci_dev it identified during probe for driver to use? Is my understanding right?
  2. I am not clear on the parameters of devm_kzalloc. what does "Device to allocate memory for" mean at line 763 http://lxr.free-electrons.com/source/drivers/base/devres.c?v=3.19#L774 ? At the end of the day we just need to allocate memory in kernel of size struct ocores_i2c. Is the first parameter used as a tracking mechanism to free it later automatically (based on reading devres kernel documentation)?
  3. return value of devm_kzalloc is pointer to newly allocated memory of size ocores_i2c in kernel. If this is the case shouldnt the second parameter of devm_kzalloc be sizeof(struct ocores_i2c) instead of sizeof(*i2c )?
dee
  • 95
  • 1
  • 7

1 Answers1

2

1) Yes, the kernel will fill out this structure for you and then pass it to your probe function to perform initialisation.

2) In short, all of the devm_ suite of functions will tie the lifecycle of the returned resource to the lifecycle of the actual device. Any returned resources will therefore be automatically cleaned up when the specified device is un-probed.

3) Probably, yes, although the sizeof operator will follow the definition of i2c, so this is actually basically the same as sizeof(struct ocores_i2c).

slugonamission
  • 9,562
  • 1
  • 34
  • 41
  • I would recommend to expand your answer. 1. The topic starter should get an understanding at which time the *device creation* and *driver attachment* are happened and what the difference between them. 2. The actual `struct device *dev` pointer is used as a pointer to a *storage* of the resources. Better to look at the code of devres API. 3. Probably not, since it more flexible in case you change you struct (and less characters to type!). Nevertheless both ways has cons. – 0andriy Jun 19 '16 at 10:35
  • 1 & 2: Thanks for the conformation. For 3. I looked in number of other drivers and concluded that sizeof(*i2c) and sizeof(struct ocores_i2c ) are same / similar. It would be helpful to understand why sizeof(*i2c) is preferred ( apart from less characters to type). Andy, when you say change your struct you mean change it to a totally different structure or just add extra members to struct? if its later we can still use sizeof(struct i2c ) right? – dee Jun 20 '16 at 18:50
  • I ended up executing my driver and printed the values. `sizeof(*i2c)=976 sizeof(i2c)=8`. Hopefully this helps others if they stumble across this – dee Jun 20 '16 at 22:40
  • @dee - yes, because `sizeof(*i2c)` follows the pointer and prints the size of `struct ocores_i2c`, whereas `sizeof(i2c)` is the size of the pointer. – slugonamission Jun 21 '16 at 13:00