hwmon: (pmbus) Convert pmbus drivers to use devm_kzalloc

Marginally less code and eliminate the possibility of memory leaks.

Signed-off-by: Guenter Roeck <linux@roeck-us.net>
This commit is contained in:
Guenter Roeck 2012-02-22 08:56:43 -08:00 коммит произвёл Guenter Roeck
Родитель 07404aab52
Коммит 8b313ca7f1
8 изменённых файлов: 56 добавлений и 154 удалений

Просмотреть файл

@ -229,7 +229,8 @@ static int adm1275_probe(struct i2c_client *client,
if (device_config < 0) if (device_config < 0)
return device_config; return device_config;
data = kzalloc(sizeof(struct adm1275_data), GFP_KERNEL); data = devm_kzalloc(&client->dev, sizeof(struct adm1275_data),
GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
@ -297,23 +298,12 @@ static int adm1275_probe(struct i2c_client *client,
break; break;
} }
ret = pmbus_do_probe(client, id, info); return pmbus_do_probe(client, id, info);
if (ret)
goto err_mem;
return 0;
err_mem:
kfree(data);
return ret;
} }
static int adm1275_remove(struct i2c_client *client) static int adm1275_remove(struct i2c_client *client)
{ {
const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
const struct adm1275_data *data = to_adm1275_data(info);
pmbus_do_remove(client); pmbus_do_remove(client);
kfree(data);
return 0; return 0;
} }

Просмотреть файл

@ -176,7 +176,6 @@ static int lm25066_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
int config; int config;
int ret;
struct lm25066_data *data; struct lm25066_data *data;
struct pmbus_driver_info *info; struct pmbus_driver_info *info;
@ -184,15 +183,14 @@ static int lm25066_probe(struct i2c_client *client,
I2C_FUNC_SMBUS_READ_BYTE_DATA)) I2C_FUNC_SMBUS_READ_BYTE_DATA))
return -ENODEV; return -ENODEV;
data = kzalloc(sizeof(struct lm25066_data), GFP_KERNEL); data = devm_kzalloc(&client->dev, sizeof(struct lm25066_data),
GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP); config = i2c_smbus_read_byte_data(client, LM25066_DEVICE_SETUP);
if (config < 0) { if (config < 0)
ret = config; return config;
goto err_mem;
}
data->id = id->driver_data; data->id = id->driver_data;
info = &data->info; info = &data->info;
@ -291,27 +289,15 @@ static int lm25066_probe(struct i2c_client *client,
} }
break; break;
default: default:
ret = -ENODEV; return -ENODEV;
goto err_mem;
} }
ret = pmbus_do_probe(client, id, info); return pmbus_do_probe(client, id, info);
if (ret)
goto err_mem;
return 0;
err_mem:
kfree(data);
return ret;
} }
static int lm25066_remove(struct i2c_client *client) static int lm25066_remove(struct i2c_client *client)
{ {
const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
const struct lm25066_data *data = to_lm25066_data(info);
pmbus_do_remove(client); pmbus_do_remove(client);
kfree(data);
return 0; return 0;
} }

Просмотреть файл

@ -287,7 +287,7 @@ MODULE_DEVICE_TABLE(i2c, ltc2978_id);
static int ltc2978_probe(struct i2c_client *client, static int ltc2978_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
int chip_id, ret, i; int chip_id, i;
struct ltc2978_data *data; struct ltc2978_data *data;
struct pmbus_driver_info *info; struct pmbus_driver_info *info;
@ -295,15 +295,14 @@ static int ltc2978_probe(struct i2c_client *client,
I2C_FUNC_SMBUS_READ_WORD_DATA)) I2C_FUNC_SMBUS_READ_WORD_DATA))
return -ENODEV; return -ENODEV;
data = kzalloc(sizeof(struct ltc2978_data), GFP_KERNEL); data = devm_kzalloc(&client->dev, sizeof(struct ltc2978_data),
GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
chip_id = i2c_smbus_read_word_data(client, LTC2978_MFR_SPECIAL_ID); chip_id = i2c_smbus_read_word_data(client, LTC2978_MFR_SPECIAL_ID);
if (chip_id < 0) { if (chip_id < 0)
ret = chip_id; return chip_id;
goto err_mem;
}
if (chip_id == LTC2978_ID_REV1 || chip_id == LTC2978_ID_REV2) { if (chip_id == LTC2978_ID_REV1 || chip_id == LTC2978_ID_REV2) {
data->id = ltc2978; data->id = ltc2978;
@ -311,8 +310,7 @@ static int ltc2978_probe(struct i2c_client *client,
data->id = ltc3880; data->id = ltc3880;
} else { } else {
dev_err(&client->dev, "Unsupported chip ID 0x%x\n", chip_id); dev_err(&client->dev, "Unsupported chip ID 0x%x\n", chip_id);
ret = -ENODEV; return -ENODEV;
goto err_mem;
} }
if (data->id != id->driver_data) if (data->id != id->driver_data)
dev_warn(&client->dev, dev_warn(&client->dev,
@ -357,27 +355,15 @@ static int ltc2978_probe(struct i2c_client *client,
data->vout_min[1] = 0xffff; data->vout_min[1] = 0xffff;
break; break;
default: default:
ret = -ENODEV; return -ENODEV;
goto err_mem;
} }
ret = pmbus_do_probe(client, id, info); return pmbus_do_probe(client, id, info);
if (ret)
goto err_mem;
return 0;
err_mem:
kfree(data);
return ret;
} }
static int ltc2978_remove(struct i2c_client *client) static int ltc2978_remove(struct i2c_client *client)
{ {
const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
const struct ltc2978_data *data = to_ltc2978_data(info);
pmbus_do_remove(client); pmbus_do_remove(client);
kfree(data);
return 0; return 0;
} }

Просмотреть файл

@ -166,32 +166,21 @@ static int pmbus_probe(struct i2c_client *client,
const struct i2c_device_id *id) const struct i2c_device_id *id)
{ {
struct pmbus_driver_info *info; struct pmbus_driver_info *info;
int ret;
info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL); info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info),
GFP_KERNEL);
if (!info) if (!info)
return -ENOMEM; return -ENOMEM;
info->pages = id->driver_data; info->pages = id->driver_data;
info->identify = pmbus_identify; info->identify = pmbus_identify;
ret = pmbus_do_probe(client, id, info); return pmbus_do_probe(client, id, info);
if (ret < 0)
goto out;
return 0;
out:
kfree(info);
return ret;
} }
static int pmbus_remove(struct i2c_client *client) static int pmbus_remove(struct i2c_client *client)
{ {
const struct pmbus_driver_info *info;
info = pmbus_get_driver_info(client);
pmbus_do_remove(client); pmbus_do_remove(client);
kfree(info);
return 0; return 0;
} }

Просмотреть файл

@ -1676,7 +1676,7 @@ int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
| I2C_FUNC_SMBUS_WORD_DATA)) | I2C_FUNC_SMBUS_WORD_DATA))
return -ENODEV; return -ENODEV;
data = kzalloc(sizeof(*data), GFP_KERNEL); data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
if (!data) { if (!data) {
dev_err(&client->dev, "No memory to allocate driver data\n"); dev_err(&client->dev, "No memory to allocate driver data\n");
return -ENOMEM; return -ENOMEM;
@ -1688,8 +1688,7 @@ int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
/* Bail out if PMBus status register does not exist. */ /* Bail out if PMBus status register does not exist. */
if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0) { if (i2c_smbus_read_byte_data(client, PMBUS_STATUS_BYTE) < 0) {
dev_err(&client->dev, "PMBus status register not found\n"); dev_err(&client->dev, "PMBus status register not found\n");
ret = -ENODEV; return -ENODEV;
goto out_data;
} }
if (pdata) if (pdata)
@ -1702,50 +1701,49 @@ int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
ret = (*info->identify)(client, info); ret = (*info->identify)(client, info);
if (ret < 0) { if (ret < 0) {
dev_err(&client->dev, "Chip identification failed\n"); dev_err(&client->dev, "Chip identification failed\n");
goto out_data; return ret;
} }
} }
if (info->pages <= 0 || info->pages > PMBUS_PAGES) { if (info->pages <= 0 || info->pages > PMBUS_PAGES) {
dev_err(&client->dev, "Bad number of PMBus pages: %d\n", dev_err(&client->dev, "Bad number of PMBus pages: %d\n",
info->pages); info->pages);
ret = -ENODEV; return -ENODEV;
goto out_data;
} }
ret = pmbus_identify_common(client, data); ret = pmbus_identify_common(client, data);
if (ret < 0) { if (ret < 0) {
dev_err(&client->dev, "Failed to identify chip capabilities\n"); dev_err(&client->dev, "Failed to identify chip capabilities\n");
goto out_data; return ret;
} }
ret = -ENOMEM; ret = -ENOMEM;
data->sensors = kzalloc(sizeof(struct pmbus_sensor) * data->max_sensors, data->sensors = devm_kzalloc(&client->dev, sizeof(struct pmbus_sensor)
GFP_KERNEL); * data->max_sensors, GFP_KERNEL);
if (!data->sensors) { if (!data->sensors) {
dev_err(&client->dev, "No memory to allocate sensor data\n"); dev_err(&client->dev, "No memory to allocate sensor data\n");
goto out_data; return -ENOMEM;
} }
data->booleans = kzalloc(sizeof(struct pmbus_boolean) data->booleans = devm_kzalloc(&client->dev, sizeof(struct pmbus_boolean)
* data->max_booleans, GFP_KERNEL); * data->max_booleans, GFP_KERNEL);
if (!data->booleans) { if (!data->booleans) {
dev_err(&client->dev, "No memory to allocate boolean data\n"); dev_err(&client->dev, "No memory to allocate boolean data\n");
goto out_sensors; return -ENOMEM;
} }
data->labels = kzalloc(sizeof(struct pmbus_label) * data->max_labels, data->labels = devm_kzalloc(&client->dev, sizeof(struct pmbus_label)
GFP_KERNEL); * data->max_labels, GFP_KERNEL);
if (!data->labels) { if (!data->labels) {
dev_err(&client->dev, "No memory to allocate label data\n"); dev_err(&client->dev, "No memory to allocate label data\n");
goto out_booleans; return -ENOMEM;
} }
data->attributes = kzalloc(sizeof(struct attribute *) data->attributes = devm_kzalloc(&client->dev, sizeof(struct attribute *)
* data->max_attributes, GFP_KERNEL); * data->max_attributes, GFP_KERNEL);
if (!data->attributes) { if (!data->attributes) {
dev_err(&client->dev, "No memory to allocate attribute data\n"); dev_err(&client->dev, "No memory to allocate attribute data\n");
goto out_labels; return -ENOMEM;
} }
pmbus_find_attributes(client, data); pmbus_find_attributes(client, data);
@ -1756,8 +1754,7 @@ int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
*/ */
if (!data->num_attributes) { if (!data->num_attributes) {
dev_err(&client->dev, "No attributes found\n"); dev_err(&client->dev, "No attributes found\n");
ret = -ENODEV; return -ENODEV;
goto out_attributes;
} }
/* Register sysfs hooks */ /* Register sysfs hooks */
@ -1765,7 +1762,7 @@ int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
ret = sysfs_create_group(&client->dev.kobj, &data->group); ret = sysfs_create_group(&client->dev.kobj, &data->group);
if (ret) { if (ret) {
dev_err(&client->dev, "Failed to create sysfs entries\n"); dev_err(&client->dev, "Failed to create sysfs entries\n");
goto out_attributes; return ret;
} }
data->hwmon_dev = hwmon_device_register(&client->dev); data->hwmon_dev = hwmon_device_register(&client->dev);
if (IS_ERR(data->hwmon_dev)) { if (IS_ERR(data->hwmon_dev)) {
@ -1777,16 +1774,6 @@ int pmbus_do_probe(struct i2c_client *client, const struct i2c_device_id *id,
out_hwmon_device_register: out_hwmon_device_register:
sysfs_remove_group(&client->dev.kobj, &data->group); sysfs_remove_group(&client->dev.kobj, &data->group);
out_attributes:
kfree(data->attributes);
out_labels:
kfree(data->labels);
out_booleans:
kfree(data->booleans);
out_sensors:
kfree(data->sensors);
out_data:
kfree(data);
return ret; return ret;
} }
EXPORT_SYMBOL_GPL(pmbus_do_probe); EXPORT_SYMBOL_GPL(pmbus_do_probe);
@ -1796,11 +1783,6 @@ void pmbus_do_remove(struct i2c_client *client)
struct pmbus_data *data = i2c_get_clientdata(client); struct pmbus_data *data = i2c_get_clientdata(client);
hwmon_device_unregister(data->hwmon_dev); hwmon_device_unregister(data->hwmon_dev);
sysfs_remove_group(&client->dev.kobj, &data->group); sysfs_remove_group(&client->dev.kobj, &data->group);
kfree(data->attributes);
kfree(data->labels);
kfree(data->booleans);
kfree(data->sensors);
kfree(data);
} }
EXPORT_SYMBOL_GPL(pmbus_do_remove); EXPORT_SYMBOL_GPL(pmbus_do_remove);

Просмотреть файл

@ -155,7 +155,8 @@ static int ucd9000_probe(struct i2c_client *client,
"Device mismatch: Configured %s, detected %s\n", "Device mismatch: Configured %s, detected %s\n",
id->name, mid->name); id->name, mid->name);
data = kzalloc(sizeof(struct ucd9000_data), GFP_KERNEL); data = devm_kzalloc(&client->dev, sizeof(struct ucd9000_data),
GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
info = &data->info; info = &data->info;
@ -164,13 +165,12 @@ static int ucd9000_probe(struct i2c_client *client,
if (ret < 0) { if (ret < 0) {
dev_err(&client->dev, dev_err(&client->dev,
"Failed to read number of active pages\n"); "Failed to read number of active pages\n");
goto out; return ret;
} }
info->pages = ret; info->pages = ret;
if (!info->pages) { if (!info->pages) {
dev_err(&client->dev, "No pages configured\n"); dev_err(&client->dev, "No pages configured\n");
ret = -ENODEV; return -ENODEV;
goto out;
} }
/* The internal temperature sensor is always active */ /* The internal temperature sensor is always active */
@ -181,8 +181,7 @@ static int ucd9000_probe(struct i2c_client *client,
block_buffer); block_buffer);
if (ret <= 0) { if (ret <= 0) {
dev_err(&client->dev, "Failed to read configuration data\n"); dev_err(&client->dev, "Failed to read configuration data\n");
ret = -ENODEV; return -ENODEV;
goto out;
} }
for (i = 0; i < ret; i++) { for (i = 0; i < ret; i++) {
int page = UCD9000_MON_PAGE(block_buffer[i]); int page = UCD9000_MON_PAGE(block_buffer[i]);
@ -218,7 +217,7 @@ static int ucd9000_probe(struct i2c_client *client,
UCD9000_FAN_CONFIG, UCD9000_FAN_CONFIG,
data->fan_data[i]); data->fan_data[i]);
if (ret < 0) if (ret < 0)
goto out; return ret;
} }
i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0); i2c_smbus_write_byte_data(client, UCD9000_FAN_CONFIG_INDEX, 0);
@ -227,23 +226,12 @@ static int ucd9000_probe(struct i2c_client *client,
| PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34; | PMBUS_HAVE_FAN34 | PMBUS_HAVE_STATUS_FAN34;
} }
ret = pmbus_do_probe(client, mid, info); return pmbus_do_probe(client, mid, info);
if (ret < 0)
goto out;
return 0;
out:
kfree(data);
return ret;
} }
static int ucd9000_remove(struct i2c_client *client) static int ucd9000_remove(struct i2c_client *client)
{ {
struct ucd9000_data *data;
data = to_ucd9000_data(pmbus_get_driver_info(client));
pmbus_do_remove(client); pmbus_do_remove(client);
kfree(data);
return 0; return 0;
} }

Просмотреть файл

@ -81,7 +81,8 @@ static int ucd9200_probe(struct i2c_client *client,
"Device mismatch: Configured %s, detected %s\n", "Device mismatch: Configured %s, detected %s\n",
id->name, mid->name); id->name, mid->name);
info = kzalloc(sizeof(struct pmbus_driver_info), GFP_KERNEL); info = devm_kzalloc(&client->dev, sizeof(struct pmbus_driver_info),
GFP_KERNEL);
if (!info) if (!info)
return -ENOMEM; return -ENOMEM;
@ -89,7 +90,7 @@ static int ucd9200_probe(struct i2c_client *client,
block_buffer); block_buffer);
if (ret < 0) { if (ret < 0) {
dev_err(&client->dev, "Failed to read phase information\n"); dev_err(&client->dev, "Failed to read phase information\n");
goto out; return ret;
} }
/* /*
@ -106,8 +107,7 @@ static int ucd9200_probe(struct i2c_client *client,
} }
if (!info->pages) { if (!info->pages) {
dev_err(&client->dev, "No rails configured\n"); dev_err(&client->dev, "No rails configured\n");
ret = -ENODEV; return -ENODEV;
goto out;
} }
dev_info(&client->dev, "%d rails configured\n", info->pages); dev_info(&client->dev, "%d rails configured\n", info->pages);
@ -137,7 +137,7 @@ static int ucd9200_probe(struct i2c_client *client,
if (ret < 0) { if (ret < 0) {
dev_err(&client->dev, dev_err(&client->dev,
"Failed to initialize PHASE registers\n"); "Failed to initialize PHASE registers\n");
goto out; return ret;
} }
} }
if (info->pages > 1) if (info->pages > 1)
@ -160,22 +160,12 @@ static int ucd9200_probe(struct i2c_client *client,
if (mid->driver_data == ucd9240) if (mid->driver_data == ucd9240)
info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12; info->func[0] |= PMBUS_HAVE_FAN12 | PMBUS_HAVE_STATUS_FAN12;
ret = pmbus_do_probe(client, mid, info); return pmbus_do_probe(client, mid, info);
if (ret < 0)
goto out;
return 0;
out:
kfree(info);
return ret;
} }
static int ucd9200_remove(struct i2c_client *client) static int ucd9200_remove(struct i2c_client *client)
{ {
const struct pmbus_driver_info *info;
info = pmbus_get_driver_info(client);
pmbus_do_remove(client); pmbus_do_remove(client);
kfree(info);
return 0; return 0;
} }

Просмотреть файл

@ -193,7 +193,8 @@ static int zl6100_probe(struct i2c_client *client,
"Device mismatch: Configured %s, detected %s\n", "Device mismatch: Configured %s, detected %s\n",
id->name, mid->name); id->name, mid->name);
data = kzalloc(sizeof(struct zl6100_data), GFP_KERNEL); data = devm_kzalloc(&client->dev, sizeof(struct zl6100_data),
GFP_KERNEL);
if (!data) if (!data)
return -ENOMEM; return -ENOMEM;
@ -223,7 +224,8 @@ static int zl6100_probe(struct i2c_client *client,
ret = i2c_smbus_read_word_data(client, ZL6100_MFR_CONFIG); ret = i2c_smbus_read_word_data(client, ZL6100_MFR_CONFIG);
if (ret < 0) if (ret < 0)
goto err_mem; return ret;
if (ret & ZL6100_MFR_XTEMP_ENABLE) if (ret & ZL6100_MFR_XTEMP_ENABLE)
info->func[0] |= PMBUS_HAVE_TEMP2; info->func[0] |= PMBUS_HAVE_TEMP2;
@ -235,23 +237,12 @@ static int zl6100_probe(struct i2c_client *client,
info->write_word_data = zl6100_write_word_data; info->write_word_data = zl6100_write_word_data;
info->write_byte = zl6100_write_byte; info->write_byte = zl6100_write_byte;
ret = pmbus_do_probe(client, mid, info); return pmbus_do_probe(client, mid, info);
if (ret)
goto err_mem;
return 0;
err_mem:
kfree(data);
return ret;
} }
static int zl6100_remove(struct i2c_client *client) static int zl6100_remove(struct i2c_client *client)
{ {
const struct pmbus_driver_info *info = pmbus_get_driver_info(client);
const struct zl6100_data *data = to_zl6100_data(info);
pmbus_do_remove(client); pmbus_do_remove(client);
kfree(data);
return 0; return 0;
} }