ddata->psy_desc.name = dev_name(ddata->dev);想要获取"bbc"为电源供应器对象,以下代码要如何改写: static int set_charger_type(void) { int ret; static int old_type_en = 0; union power_supply_propval val; struct power_supply *psy = power_supply_get_by_name("bbc"); if (psy == NULL) { pr_info("power_supply_get_by_name error.\n"); return -1; } val.intval = chr_type_en; pr_info("set_charger_type: %d.\n", val.intval); if (val.intval) { if (!old_type_en) { ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_ONLINE, &val); old_type_en = 1; } power_supply_changed(psy); val.intval = POWER_SUPPLY_TYPE_WIRELESS; ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_TYPE, &val); if (!ret) { return val.intval; } else { return 0; } } else { val.intval = POWER_SUPPLY_TYPE_USB; ret = power_supply_set_property(psy, POWER_SUPPLY_PROP_TYPE, &val); if (ret < 0) pr_info("set chg psy failed\n"); power_supply_changed(psy); old_type_en = 0; } return 0; }
时间: 2024-03-30 09:35:16 浏览: 127
uCOS-II在C8051F020单片机上的移植.doc
你可以将以下代码替换原来的 power_supply_get_by_name("bbc"):
```
struct device *dev;
struct power_supply *psy;
dev = class_find_device(power_supply_class, NULL, "bbc", NULL);
if (!dev) {
pr_info("Failed to find power supply device: bbc\n");
return -1;
}
psy = dev_get_drvdata(dev);
if (!psy) {
pr_info("Failed to get power supply for device: bbc\n");
return -1;
}
```
这里使用了 class_find_device 函数来查找名称为 "bbc" 的电源供应器对象,并通过 dev_get_drvdata 函数获取对应的 power_supply 结构体。注意,需要包含头文件 #include <linux/device.h>。
阅读全文