1

My kernel driver needs to access battery's properties (get_property, set_property).
Problem: How to find the battery's struct power_supply?

I only find power_supply_get_by_name but there can be different names for the battery. I need to check the power_supply's type but this is where I am stuck.

A direct get_by_type or a get_all_power_supplies to check the type on my own or also a get_power_supply_names to pass to power_supply_get_by_name would be fine for me.

I want to avoid file accesses in the kernel so what is a better way to find the type="battery" power_supply?

I suspect I should grab the supply every time again because it may change or vanish / reappear? This driver can access the supply every couple of seconds in some situations so it would be nice to not spend a long time finding the battery.

1 Answer 1

0

Exactly a year late for the party. :) Here's the basic idea that loops over the all objects of power-supply class.

#include <linux/power_supply.h>

static int power_supply_printer(struct device *dev, const void *data)
{
    struct power_supply *psy = dev_get_drvdata(dev);

    (void)data;
    printk(KERN_INF "power-supply = %s\n", psy->desc->name);

    /* Return 1 if found, 0 if this is not valid. */
    return 0;
}

static __init int my_driver_init(void)
{
    struct device *dev;

    dev = class_find_device(power_supply_class, NULL, NULL /* data*/, power_supply_printer);
    ...
}

This function will iterate over all power_suppy_class drivers. Note, as long as the call-back function returns 0, it will check the next available device in that class.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .