Commit ac150a05 authored by Lucas Russo's avatar Lucas Russo

hal/*/protocols/ops/*: fix I2C and SPI init () failure handling

Some functions inside the respective open () calls can fail,
specifically _init () ones. So, we must destroy everything
allocated before that and exit.
parent 6e055aa5
......@@ -116,12 +116,18 @@ int i2c_open (smpr_t *self, uint32_t base, void *args)
"\tconfig register = 0x%08X\n",
i2c_proto->sys_freq, i2c_proto->i2c_freq, i2c_proto->init_config);
self->proto_handler = i2c_proto;
/* Attach specific protocol handler to generic one */
SMPR_PROTO_I2C(self) = i2c_proto;
DBE_DEBUG (DBG_SM_PR | DBG_LVL_INFO, "[sm_pr:i2c] Initializing I2C protocol\n");
_i2c_init (self);
smpr_err_e err = _i2c_init (self);
ASSERT_TEST(err == SMPR_SUCCESS, "Could not initialize I2C protocol handler",
err_proto_handler_init);
return 0;
err_proto_handler_init:
smpr_proto_i2c_destroy (&SMPR_PROTO_I2C(self));
err_proto_handler_alloc:
return -1;
}
......@@ -132,10 +138,8 @@ int i2c_release (smpr_t *self)
assert (self);
/* Deattach specific protocol handler to generic one */
smpr_err_e err = smpr_proto_i2c_destroy ((smpr_proto_i2c_t **) &self->proto_handler);
smpr_err_e err = smpr_proto_i2c_destroy (&SMPR_PROTO_I2C(self));
ASSERT_TEST (err==SMPR_SUCCESS, "Could not close device appropriately", err_dealloc);
self->proto_handler = NULL;
DBE_DEBUG (DBG_SM_PR | DBG_LVL_INFO, "[smpr:i2c] Closed I2C protocol handler\n");
return 0;
......@@ -355,7 +359,6 @@ static smpr_err_e _i2c_set_mode (smpr_t *self, uint32_t flags)
assert (self);
smpr_proto_i2c_t *i2c_proto = SMPR_PROTO_I2C(self);
/* Check if we must send a stop after the last byte */
if ((flags & SMPR_PROTO_I2C_REP_START) != 0) {
i2c_proto->mode = I2C_MODE_REP_START;
......
......@@ -113,12 +113,18 @@ int spi_open (smpr_t *self, uint32_t base, void *args)
"\tconfig register = 0x%08X\n",
spi_proto->sys_freq, spi_proto->spi_freq, spi_proto->init_config);
self->proto_handler = spi_proto;
/* Attach specific protocol handler to generic one */
SMPR_PROTO_SPI(self) = spi_proto;
DBE_DEBUG (DBG_SM_PR | DBG_LVL_INFO, "[sm_pr:spi] Initializing SPI protocol\n");
_spi_init (self);
smpr_err_e err = _spi_init (self);
ASSERT_TEST(err == SMPR_SUCCESS, "Could not initialize SPI protocol handler",
err_proto_handler_init);
return 0;
err_proto_handler_init:
smpr_proto_spi_destroy (&SMPR_PROTO_SPI(self));
err_proto_handler_alloc:
return -1;
}
......@@ -129,10 +135,8 @@ int spi_release (smpr_t *self)
assert (self);
/* Deattach specific protocol handler to generic one */
smpr_err_e err = smpr_proto_spi_destroy ((smpr_proto_spi_t **) &self->proto_handler);
smpr_err_e err = smpr_proto_spi_destroy (&SMPR_PROTO_SPI(self));
ASSERT_TEST (err==SMPR_SUCCESS, "Could not close device appropriately", err_dealloc);
self->proto_handler = NULL;
DBE_DEBUG (DBG_SM_PR | DBG_LVL_INFO, "[smpr:spi] Closed SPI protocol handler\n");
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment