i2c-algo-bit: Refactor adapter registration
PATCH 1/2 i2c-algo-bit: Refactor adapter registration
Use a function pointer to decide whether to call i2c_add_adapter or i2c_add_numbered_adapter. This makes the code more compact than the current strategy of having the common code in a separate function.
--- drivers/i2c/algos/i2c-algo-bit.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-)
drivers/i2c/algos/i2c-algo-bit.c +5 -16
@@ -600,7 +600,8 @@ static const struct i2c_algorithm i2c_bi/* * registering functions to load algorithms at runtime */ -static int i2c_bit_prepare_bus (struct i2c_adapter *adap) +static int __i2c_bit_add_bus (struct i2c_adapter *adap, +int (*add_adapter)(struct i2c_adapter *)) {struct i2c_algo_bit_data *bit_adap = adap->algo_data;
@@ -614,30 +615,18 @@ static int i2c_bit_prepare_bus(struct i2 adap->algo = &i2c_bit_algo; adap->retries =3 ; -return 0 ; +return add_adapter (adap); }int i2c_bit_add_bus (struct i2c_adapter *adap) { -int err; - - err =i2c_bit_prepare_bus (adap); -if (err) -return err; - -return i2c_add_adapter (adap); +return __i2c_bit_add_bus (adap, i2c_add_adapter); }EXPORT_SYMBOL (i2c_bit_add_bus);int i2c_bit_add_numbered_bus (struct i2c_adapter *adap) { -int err; - - err =i2c_bit_prepare_bus (adap); -if (err) -return err; - -return i2c_add_numbered_adapter (adap); +return __i2c_bit_add_bus (adap, i2c_add_numbered_adapter); }EXPORT_SYMBOL (i2c_bit_add_numbered_bus);
Signature · 1 line
Jean Delvare
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 3 lines · Jean Delvare
Use a function pointer to decide whether to call
i2c_add_adapterori2c_add_numbered_adapter. This makes the code more compact than the current strategy of having the common code in a separate function.
ok, how about changing i2c_add_numbered_adapter to take a -1 to mean assign bus number automatically? or something similar?
Quoted 59 lines
Signed-off-by Jean Delvare <[email protected]>--- drivers/i2c/algos/i2c-algo-bit.c | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-)drivers/i2c/algos/i2c-algo-bit.c +5
-16@@ -600,7 +600,8 @@ static const struct i2c_algorithm i2c_bi/* * registering functions to load algorithms at runtime */ -static int i2c_bit_prepare_bus (struct i2c_adapter *adap) +static int __i2c_bit_add_bus (struct i2c_adapter *adap, +int (*add_adapter)(struct i2c_adapter *)) {struct i2c_algo_bit_data *bit_adap = adap->algo_data;@@ -614,30 +615,18 @@ static int i2c_bit_prepare_bus(struct i2 adap->algo = &i2c_bit_algo; adap->retries =3 ; -return 0 ; +return add_adapter (adap); }int i2c_bit_add_bus (struct i2c_adapter *adap) { -int err; - - err =i2c_bit_prepare_bus (adap); -if (err) -return err; - -return i2c_add_adapter (adap); +return __i2c_bit_add_bus (adap, i2c_add_adapter); }EXPORT_SYMBOL (i2c_bit_add_bus);int i2c_bit_add_numbered_bus (struct i2c_adapter *adap) { -int err; - - err =i2c_bit_prepare_bus (adap); -if (err) -return err; - -return i2c_add_numbered_adapter (adap); +return __i2c_bit_add_bus (adap, i2c_add_numbered_adapter); }EXPORT_SYMBOL (i2c_bit_add_numbered_bus);Signature · 6 lines
-- Jean Delvare -- To unsubscribe from this list: send the line "unsubscribe linux-i2c" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Signature · 5 lines
-- Ben Q: What's a light-year? A: One-third less calories than a regular year.
A message that is not in the archive (sent off-list or not yet received): 20101207115131.GM20097-SMNkleLxa3Z6Wcw2j4pizdi2O/[email protected]
Ben Dooks said the following:
Quoted 7 lines · Jean Delvare
Quoted 3 lines
Use a function pointer to decide whether to call
i2c_add_adapterori2c_add_numbered_adapter. This makes the code more compact than the current strategy of having the common code in a separate function.ok, how about changing
i2c_add_numbered_adapterto take a -1 to mean assign bus number automatically? or something similar?
IMHO better: i2c_add_adapter with optional (-1) bus parameter?
Signature · 2 lines
KR Michael
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hi Michael, Ben,
Quoted 10 lines · Michael Lawnick
Ben Dooks said the following:
Quoted 7 lines · Jean Delvare
Quoted 3 lines
Use a function pointer to decide whether to call
i2c_add_adapterori2c_add_numbered_adapter. This makes the code more compact than the current strategy of having the common code in a separate function.ok, how about changing
i2c_add_numbered_adapterto take a -1 to mean assign bus number automatically? or something similar?IMHO better:
i2c_add_adapterwith optional (-1) bus parameter?
Which problem are you both trying to solve, please?
Signature · 1 line
Jean Delvare
A message that is not in the archive (sent off-list or not yet received): [email protected]
Jean Delvare said the following:
Quoted 9 lines
Hi Michael, Ben,
Quoted 6 lines · Michael Lawnick
Ben Dooks said the following:
Quoted 5 lines · Jean Delvare
Quoted 3 lines
Use a function pointer to decide whether to call
i2c_add_adapterori2c_add_numbered_adapter. This makes the code more compact than the current strategy of having the common code in a separate function.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
ok, how about changing
i2c_add_numbered_adapterto take a -1 to mean assign bus number automatically? or something similar?IMHO better:
i2c_add_adapterwith optional (-1) bus parameter?Which problem are you both trying to solve, please?
Function pointers tend to hide information. Seeing the targeted function in source code makes it more clear, IMHO.
Signature · 2 lines
KR Michael
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 13 lines · Michael Lawnick
Jean Delvare said the following:
Quoted 12 lines
Hi Michael, Ben,
Quoted 9 lines · Michael Lawnick
Ben Dooks said the following:
Quoted 7 lines · Jean Delvare
Quoted 3 lines
Use a function pointer to decide whether to call
i2c_add_adapterori2c_add_numbered_adapter. This makes the code more compact than the current strategy of having the common code in a separate function.ok, how about changing
i2c_add_numbered_adapterto take a -1 to mean assign bus number automatically? or something similar?
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
IMHO better:
i2c_add_adapterwith optional (-1) bus parameter?Which problem are you both trying to solve, please?
Function pointers tend to hide information. Seeing the targeted function in source code makes it more clear, IMHO.
This doesn't sound like a valid argument when the provider of the function pointer is only 20 lines away from the call site in the very same file, sorry.
Adding a parameter to i2c_add_adapter would mean changing 105 calling sites. You have to understand that we aren't going to do that without a very good reason. Ben's proposal is equally invasive, as every current call to i2c_add_adapter would have to set the id to -1 before. This means changing 74 drivers for a marginal benefit.
If someday calls to i2c_add_numbered_adapter() outnumber calls to i2c_add_adapter() by a factor 3, we can reconsider. But this isn't the case today. I am not particularly happy with the current situation myself, but it seemed like the best option when i2c_add_numbered_adapter() was introduced, and I see no reason to reconsider at this point in time.
Signature · 1 line
Jean Delvare
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 31 lines · Jean Delvare
Quoted 13 lines · Michael Lawnick
Jean Delvare said the following:
Quoted 12 lines
Hi Michael, Ben,
Quoted 9 lines · Michael Lawnick
Ben Dooks said the following:
Quoted 7 lines · Jean Delvare
Quoted 3 lines
Use a function pointer to decide whether to call
i2c_add_adapterori2c_add_numbered_adapter. This makes the code more compact than the current strategy of having the common code in a separate function.ok, how about changing
i2c_add_numbered_adapterto take a -1 to mean assign bus number automatically? or something similar?Quoted 6 lines
Quoted 3 lines
Quoted 1 line
IMHO better:
i2c_add_adapterwith optional (-1) bus parameter?Which problem are you both trying to solve, please?
Function pointers tend to hide information. Seeing the targeted function in source code makes it more clear, IMHO.
This doesn't sound like a valid argument when the provider of the function pointer is only 20 lines away from the call site in the very same file, sorry.
Adding a parameter to
i2c_add_adapterwould mean changing 105 calling sites. You have to understand that we aren't going to do that without a very good reason. Ben's proposal is equally invasive, as every current call toi2c_add_adapterwould have to set the id to -1 before. This means changing 74 drivers for a marginal benefit.
Quoted 6 lines
If someday calls to
i2c_add_numbered_adapter()outnumber calls toi2c_add_adapter()by a factor 3, we can reconsider. But this isn't the case today. I am not particularly happy with the current situation myself, but it seemed like the best option wheni2c_add_numbered_adapter()was introduced, and I see no reason to reconsider at this point in time.
How about adding an in-lined (in header) something this like:
static inline int i2c_add_adapter(struct i2c_adapter *adap)
{
return i2c_add_numbered_adapter(-1, adap);
}anyway, not desperately important (and code not tested either)
Signature · 3 lines
Ben Dooks, [email protected], http://www.fluff.org/ben/ Large Hadron Colada: A large Pina Colada that makes the universe disappear.
A message that is not in the archive (sent off-list or not yet received): 20101213234723.GZ20097-SMNkleLxa3Z6Wcw2j4pizdi2O/[email protected]
Quoted 22 lines · Ben Dooks
Quoted 14 lines · Jean Delvare
Quoted 2 lines · Michael Lawnick
Function pointers tend to hide information. Seeing the targeted function in source code makes it more clear, IMHO.
This doesn't sound like a valid argument when the provider of the function pointer is only 20 lines away from the call site in the very same file, sorry.
Adding a parameter to
i2c_add_adapterwould mean changing 105 calling sites. You have to understand that we aren't going to do that without a very good reason. Ben's proposal is equally invasive, as every current call toi2c_add_adapterwould have to set the id to -1 before. This means changing 74 drivers for a marginal benefit.Quoted 6 lines
If someday calls to
i2c_add_numbered_adapter()outnumber calls toi2c_add_adapter()by a factor 3, we can reconsider. But this isn't the case today. I am not particularly happy with the current situation myself, but it seemed like the best option wheni2c_add_numbered_adapter()was introduced, and I see no reason to reconsider at this point in time.
Quoted 8 lines
How about adding an in-lined (in header) something this like:
static inline int
i2c_add_adapter(struct i2c_adapter *adap){ return i2c_add_numbered_adapter(-1, adap); }anyway, not desperately important (and code not tested either)
i2c_add_numbered_adapter doesn't take the bus number as a parameter, it reads it from adap->nr. So the inline function would rather be:
static inline int i2c_add_adapter(struct i2c_adapter *adap)
{
adap->nr = -1;
return i2c_add_numbered_adapter(adap);
}But then again, which problem are you trying to solve? What you propose it different from what we have today, but I fail to see how it would be _better_ than what we have today.
Signature · 1 line
Jean Delvare