How to forward a generic resource/data of platform_device to a driver

0

I have a platform_device instance and I want to pass it a function pointer, I am wondering what is the cleanest and most generic way for doing it.

the optimal thing would be if I had some misc resource in kernel I can pass void* for whatever I want but those are the only available resources:

 31 #define IORESOURCE_TYPE_BITS    0x00001f00      /* Resource type */
 32 #define IORESOURCE_IO           0x00000100      /* PCI/ISA I/O ports */
 33 #define IORESOURCE_MEM          0x00000200
 34 #define IORESOURCE_REG          0x00000300      /* Register offsets */
 35 #define IORESOURCE_IRQ          0x00000400
 36 #define IORESOURCE_DMA          0x00000800
 37 #define IORESOURCE_BUS          0x00001000

Actually I have a container for the platform device but it is allocated and initialized dynamically in the probe function.

My question is how to pass a generic pointer as resource to a device ? or how can I do it in the cleanest way?

c
linux
linux-kernel
linux-device-driver
device-driver
asked on Stack Overflow Sep 4, 2013 by 0x90 • edited Sep 12, 2013 by 0x90

1 Answer

4

Use platform_data as described at the bottom of the LWN article you link in the question. In your case, your data structure will look something like this. This is obviously untested, but you get the idea. Your platform_data struct will hold the function pointers, which you set at the same time you define the particulars of your device.

int sleep_function_chip1(struct platform_device *pdev)
{
    // Go to sleep
    return 0;
}
int resume_function_chip1(struct platform_device *pdev)
{
    // Resume
    return 0;
}

struct my_platform_data {
    int (*sleep_function)(struct platform_device *);
    int (*resume_function)(struct platform_device *);
};

// Instance of my_platform_data for a particular hardware (called chip1 for now)
static struct my_platform_data my_platform_data_chip1 = {
    .sleep_function  = &sleep_function_chip1,
    .resume_function = &resume_function_chip1,
};

// Second instance of my_platform_data for a different hardware (called chip2 for now)
static struct my_platform_data my_platform_data_chip2 = {
    .sleep_function  = &sleep_function_chip2,
    .resume_function = &resume_function_chip2,
};

// Now include that data when you create the descriptor for
// your platform
static struct platform_device my_platform_device_chip1 = {
    .name       = "my_device",
    .id     = 0,
    .dev = {
        .platform_data = &my_platform_data_chip1,
    }
};

int some_driver_function() {
    struct platform_device *pdev;
    pdev = // wherever you store this

    // Any time you need that data, extract the platform_data pointer
    struct my_platform_data *pd = (struct my_platform_data*)pdev->dev.platform_data;

    // Call the function pointer
    pd->sleep_function(pdev);
}
answered on Stack Overflow Sep 4, 2013 by Peter • edited Sep 4, 2013 by Peter

User contributions licensed under CC BY-SA 3.0