linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/9] lib/string: introduce match_string() helper
@ 2016-01-28 13:14 Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 1/9] " Andy Shevchenko
                   ` (8 more replies)
  0 siblings, 9 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

There are users of a simple string matching in the array. Let's do a common
helper for that.

Several users are updated in the series.

The series is compile tested.

There is still a question what function should return. There are variants:
-1, -EINVAL, -ENODATA, something else?

Since v3:
- rebase on top of latest linux-next
- add Rafael's Ack

Since v2:
- slightly modify implementation of the helper:
  - rename len -> n in prototype (Sergey)
  - move to for-loop (Rasmus)
  - change 0 -> -1 to iterate over NULL-terminated arrays (Rasmus)
- add patch 9 from Heikki
- append tags

Since v1:
- convert few more users to get helper useful

Andy Shevchenko (8):
  lib/string: introduce match_string() helper
  device property: convert to use match_string() helper
  pinctrl: convert to use match_string() helper
  drm/edid: convert to use match_string() helper
  power: charger_manager: convert to use match_string() helper
  power: ab8500: convert to use match_string() helper
  ata: hpt366: convert to use match_string() helper
  ide: hpt366: convert to use match_string() helper

Heikki Krogerus (1):
  usb: common: convert to use match_string() helper

 drivers/ata/pata_hpt366.c       | 13 +++++--------
 drivers/base/property.c         | 10 ++--------
 drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
 drivers/ide/hpt366.c            |  9 ++-------
 drivers/pinctrl/pinmux.c        | 13 +++----------
 drivers/power/ab8500_btemp.c    | 15 +++++----------
 drivers/power/ab8500_charger.c  | 16 +++++-----------
 drivers/power/ab8500_fg.c       | 15 +++++----------
 drivers/power/abx500_chargalg.c | 14 +++++---------
 drivers/power/charger-manager.c | 27 ++++-----------------------
 drivers/usb/common/common.c     | 22 ++++++++--------------
 include/linux/string.h          |  2 ++
 lib/string.c                    | 26 ++++++++++++++++++++++++++
 13 files changed, 78 insertions(+), 121 deletions(-)

-- 
2.7.0.rc3

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v4 1/9] lib/string: introduce match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  2016-02-02  6:05   ` Andrew Morton
  2016-01-28 13:14 ` [PATCH v4 2/9] device property: convert to use " Andy Shevchenko
                   ` (7 subsequent siblings)
  8 siblings, 1 reply; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

>From time to time we have to match a string in an array. Make a simple helper
for that purpose.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 include/linux/string.h |  2 ++
 lib/string.c           | 26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index b0a732b..07505aa 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -131,6 +131,8 @@ extern void argv_free(char **argv);
 extern bool sysfs_streq(const char *s1, const char *s2);
 extern int strtobool(const char *s, bool *res);
 
+int match_string(const char * const *array, size_t n, const char *string);
+
 #ifdef CONFIG_BINARY_PRINTF
 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
diff --git a/lib/string.c b/lib/string.c
index 0323c0d..ba01d4f 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -631,6 +631,32 @@ bool sysfs_streq(const char *s1, const char *s2)
 EXPORT_SYMBOL(sysfs_streq);
 
 /**
+ * match_string - matches given string in an array
+ * @array:	array of strings
+ * @n:		number of strings in the array or -1 for NULL terminated arrays
+ * @string:	string to match with
+ *
+ * Return:
+ * index of a @string in the @array if matches, or %-ENODATA otherwise.
+ */
+int match_string(const char * const *array, size_t n, const char *string)
+{
+	int index;
+	const char *item;
+
+	for (index = 0; index < n; index++) {
+		item = array[index];
+		if (!item)
+			break;
+		if (!strcmp(item, string))
+			return index;
+	}
+
+	return -ENODATA;
+}
+EXPORT_SYMBOL(match_string);
+
+/**
  * strtobool - convert common user inputs into boolean values
  * @s: input string
  * @res: result
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 2/9] device property: convert to use match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 1/9] " Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 3/9] pinctrl: " Andy Shevchenko
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Acked-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
---
 drivers/base/property.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/drivers/base/property.c b/drivers/base/property.c
index c359351..f902b55 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -647,7 +647,7 @@ int fwnode_property_match_string(struct fwnode_handle *fwnode,
 	const char *propname, const char *string)
 {
 	const char **values;
-	int nval, ret, i;
+	int nval, ret;
 
 	nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
 	if (nval < 0)
@@ -664,13 +664,7 @@ int fwnode_property_match_string(struct fwnode_handle *fwnode,
 	if (ret < 0)
 		goto out;
 
-	ret = -ENODATA;
-	for (i = 0; i < nval; i++) {
-		if (!strcmp(values[i], string)) {
-			ret = i;
-			break;
-		}
-	}
+	ret = match_string(values, nval, string);
 out:
 	kfree(values);
 	return ret;
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 3/9] pinctrl: convert to use match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 1/9] " Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 2/9] device property: convert to use " Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  2016-02-02  6:02   ` Andrew Morton
  2016-01-28 13:14 ` [PATCH v4 4/9] drm/edid: " Andy Shevchenko
                   ` (5 subsequent siblings)
  8 siblings, 1 reply; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/pinctrl/pinmux.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/drivers/pinctrl/pinmux.c b/drivers/pinctrl/pinmux.c
index 29984b3..c223a9e 100644
--- a/drivers/pinctrl/pinmux.c
+++ b/drivers/pinctrl/pinmux.c
@@ -334,7 +334,6 @@ int pinmux_map_to_setting(struct pinctrl_map const *map,
 	unsigned num_groups;
 	int ret;
 	const char *group;
-	int i;
 
 	if (!pmxops) {
 		dev_err(pctldev->dev, "does not support mux function\n");
@@ -363,19 +362,13 @@ int pinmux_map_to_setting(struct pinctrl_map const *map,
 		return -EINVAL;
 	}
 	if (map->data.mux.group) {
-		bool found = false;
 		group = map->data.mux.group;
-		for (i = 0; i < num_groups; i++) {
-			if (!strcmp(group, groups[i])) {
-				found = true;
-				break;
-			}
-		}
-		if (!found) {
+		ret = match_string(groups, num_groups, group);
+		if (ret < 0) {
 			dev_err(pctldev->dev,
 				"invalid group \"%s\" for function \"%s\"\n",
 				group, map->data.mux.function);
-			return -EINVAL;
+			return ret;
 		}
 	} else {
 		group = groups[0];
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 4/9] drm/edid: convert to use match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (2 preceding siblings ...)
  2016-01-28 13:14 ` [PATCH v4 3/9] pinctrl: " Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 5/9] power: charger_manager: " Andy Shevchenko
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/drm_edid_load.c b/drivers/gpu/drm/drm_edid_load.c
index 698b8c3..9a401ae 100644
--- a/drivers/gpu/drm/drm_edid_load.c
+++ b/drivers/gpu/drm/drm_edid_load.c
@@ -170,16 +170,11 @@ static void *edid_load(struct drm_connector *connector, const char *name,
 	int i, valid_extensions = 0;
 	bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
 
-	builtin = 0;
-	for (i = 0; i < GENERIC_EDIDS; i++) {
-		if (strcmp(name, generic_edid_name[i]) == 0) {
-			fwdata = generic_edid[i];
-			fwsize = sizeof(generic_edid[i]);
-			builtin = 1;
-			break;
-		}
-	}
-	if (!builtin) {
+	builtin = match_string(generic_edid_name, GENERIC_EDIDS, name);
+	if (builtin >= 0) {
+		fwdata = generic_edid[builtin];
+		fwsize = sizeof(generic_edid[builtin]);
+	} else {
 		struct platform_device *pdev;
 		int err;
 
@@ -252,7 +247,7 @@ static void *edid_load(struct drm_connector *connector, const char *name,
 	}
 
 	DRM_INFO("Got %s EDID base block and %d extension%s from "
-	    "\"%s\" for connector \"%s\"\n", builtin ? "built-in" :
+	    "\"%s\" for connector \"%s\"\n", (builtin >= 0) ? "built-in" :
 	    "external", valid_extensions, valid_extensions == 1 ? "" : "s",
 	    name, connector_name);
 
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 5/9] power: charger_manager: convert to use match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (3 preceding siblings ...)
  2016-01-28 13:14 ` [PATCH v4 4/9] drm/edid: " Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 6/9] power: ab8500: " Andy Shevchenko
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/power/charger-manager.c | 27 ++++-----------------------
 1 file changed, 4 insertions(+), 23 deletions(-)

diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c
index 1ea5d1a..e664ca7 100644
--- a/drivers/power/charger-manager.c
+++ b/drivers/power/charger-manager.c
@@ -2020,27 +2020,6 @@ static void __exit charger_manager_cleanup(void)
 module_exit(charger_manager_cleanup);
 
 /**
- * find_power_supply - find the associated power_supply of charger
- * @cm: the Charger Manager representing the battery
- * @psy: pointer to instance of charger's power_supply
- */
-static bool find_power_supply(struct charger_manager *cm,
-			struct power_supply *psy)
-{
-	int i;
-	bool found = false;
-
-	for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
-		if (!strcmp(psy->desc->name, cm->desc->psy_charger_stat[i])) {
-			found = true;
-			break;
-		}
-	}
-
-	return found;
-}
-
-/**
  * cm_notify_event - charger driver notify Charger Manager of charger event
  * @psy: pointer to instance of charger's power_supply
  * @type: type of charger event
@@ -2057,9 +2036,11 @@ void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
 
 	mutex_lock(&cm_list_mtx);
 	list_for_each_entry(cm, &cm_list, entry) {
-		found_power_supply = find_power_supply(cm, psy);
-		if (found_power_supply)
+		if (match_string(cm->desc->psy_charger_stat, -1,
+				 psy->desc->name) >= 0) {
+			found_power_supply = true;
 			break;
+		}
 	}
 	mutex_unlock(&cm_list_mtx);
 
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 6/9] power: ab8500: convert to use match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (4 preceding siblings ...)
  2016-01-28 13:14 ` [PATCH v4 5/9] power: charger_manager: " Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 7/9] ata: hpt366: " Andy Shevchenko
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/power/ab8500_btemp.c    | 15 +++++----------
 drivers/power/ab8500_charger.c  | 16 +++++-----------
 drivers/power/ab8500_fg.c       | 15 +++++----------
 drivers/power/abx500_chargalg.c | 14 +++++---------
 4 files changed, 20 insertions(+), 40 deletions(-)

diff --git a/drivers/power/ab8500_btemp.c b/drivers/power/ab8500_btemp.c
index 8f8044e..bf2e5dd 100644
--- a/drivers/power/ab8500_btemp.c
+++ b/drivers/power/ab8500_btemp.c
@@ -906,26 +906,21 @@ static int ab8500_btemp_get_property(struct power_supply *psy,
 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
 {
 	struct power_supply *psy;
-	struct power_supply *ext;
+	struct power_supply *ext = dev_get_drvdata(dev);
+	const char **supplicants = (const char **)ext->supplied_to;
 	struct ab8500_btemp *di;
 	union power_supply_propval ret;
-	int i, j;
-	bool psy_found = false;
+	int j;
 
 	psy = (struct power_supply *)data;
-	ext = dev_get_drvdata(dev);
 	di = power_supply_get_drvdata(psy);
 
 	/*
 	 * For all psy where the name of your driver
 	 * appears in any supplied_to
 	 */
-	for (i = 0; i < ext->num_supplicants; i++) {
-		if (!strcmp(ext->supplied_to[i], psy->desc->name))
-			psy_found = true;
-	}
-
-	if (!psy_found)
+	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
+	if (j < 0)
 		return 0;
 
 	/* Go through all properties for the psy */
diff --git a/drivers/power/ab8500_charger.c b/drivers/power/ab8500_charger.c
index e388171..30de5d4 100644
--- a/drivers/power/ab8500_charger.c
+++ b/drivers/power/ab8500_charger.c
@@ -1929,11 +1929,11 @@ static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
 {
 	struct power_supply *psy;
-	struct power_supply *ext;
+	struct power_supply *ext = dev_get_drvdata(dev);
+	const char **supplicants = (const char **)ext->supplied_to;
 	struct ab8500_charger *di;
 	union power_supply_propval ret;
-	int i, j;
-	bool psy_found = false;
+	int j;
 	struct ux500_charger *usb_chg;
 
 	usb_chg = (struct ux500_charger *)data;
@@ -1941,15 +1941,9 @@ static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
 
 	di = to_ab8500_charger_usb_device_info(usb_chg);
 
-	ext = dev_get_drvdata(dev);
-
 	/* For all psy where the driver name appears in any supplied_to */
-	for (i = 0; i < ext->num_supplicants; i++) {
-		if (!strcmp(ext->supplied_to[i], psy->desc->name))
-			psy_found = true;
-	}
-
-	if (!psy_found)
+	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
+	if (j < 0)
 		return 0;
 
 	/* Go through all properties for the psy */
diff --git a/drivers/power/ab8500_fg.c b/drivers/power/ab8500_fg.c
index 3830dad..5a36cf8 100644
--- a/drivers/power/ab8500_fg.c
+++ b/drivers/power/ab8500_fg.c
@@ -2168,26 +2168,21 @@ static int ab8500_fg_get_property(struct power_supply *psy,
 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
 {
 	struct power_supply *psy;
-	struct power_supply *ext;
+	struct power_supply *ext = dev_get_drvdata(dev);
+	const char **supplicants = (const char **)ext->supplied_to;
 	struct ab8500_fg *di;
 	union power_supply_propval ret;
-	int i, j;
-	bool psy_found = false;
+	int j;
 
 	psy = (struct power_supply *)data;
-	ext = dev_get_drvdata(dev);
 	di = power_supply_get_drvdata(psy);
 
 	/*
 	 * For all psy where the name of your driver
 	 * appears in any supplied_to
 	 */
-	for (i = 0; i < ext->num_supplicants; i++) {
-		if (!strcmp(ext->supplied_to[i], psy->desc->name))
-			psy_found = true;
-	}
-
-	if (!psy_found)
+	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
+	if (j < 0)
 		return 0;
 
 	/* Go through all properties for the psy */
diff --git a/drivers/power/abx500_chargalg.c b/drivers/power/abx500_chargalg.c
index 541f702..d9104b1 100644
--- a/drivers/power/abx500_chargalg.c
+++ b/drivers/power/abx500_chargalg.c
@@ -975,22 +975,18 @@ static void handle_maxim_chg_curr(struct abx500_chargalg *di)
 static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
 {
 	struct power_supply *psy;
-	struct power_supply *ext;
+	struct power_supply *ext = dev_get_drvdata(dev);
+	const char **supplicants = (const char **)ext->supplied_to;
 	struct abx500_chargalg *di;
 	union power_supply_propval ret;
-	int i, j;
-	bool psy_found = false;
+	int j;
 	bool capacity_updated = false;
 
 	psy = (struct power_supply *)data;
-	ext = dev_get_drvdata(dev);
 	di = power_supply_get_drvdata(psy);
 	/* For all psy where the driver name appears in any supplied_to */
-	for (i = 0; i < ext->num_supplicants; i++) {
-		if (!strcmp(ext->supplied_to[i], psy->desc->name))
-			psy_found = true;
-	}
-	if (!psy_found)
+	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
+	if (j < 0)
 		return 0;
 
 	/*
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 7/9] ata: hpt366: convert to use match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (5 preceding siblings ...)
  2016-01-28 13:14 ` [PATCH v4 6/9] power: ab8500: " Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 8/9] ide: " Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 9/9] usb: common: " Andy Shevchenko
  8 siblings, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Acked-by: Tejun Heo <tj@kernel.org>
---
 drivers/ata/pata_hpt366.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/drivers/ata/pata_hpt366.c b/drivers/ata/pata_hpt366.c
index 0038dc4..e5fb752 100644
--- a/drivers/ata/pata_hpt366.c
+++ b/drivers/ata/pata_hpt366.c
@@ -176,17 +176,14 @@ static int hpt_dma_blacklisted(const struct ata_device *dev, char *modestr,
 			       const char * const list[])
 {
 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
-	int i = 0;
+	int i;
 
 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
 
-	while (list[i] != NULL) {
-		if (!strcmp(list[i], model_num)) {
-			pr_warn("%s is not supported for %s\n",
-				modestr, list[i]);
-			return 1;
-		}
-		i++;
+	i = match_string(list, -1, model_num);
+	if (i >= 0) {
+		pr_warn("%s is not supported for %s\n", modestr, list[i]);
+		return 1;
 	}
 	return 0;
 }
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 8/9] ide: hpt366: convert to use match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (6 preceding siblings ...)
  2016-01-28 13:14 ` [PATCH v4 7/9] ata: hpt366: " Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  2016-01-28 13:14 ` [PATCH v4 9/9] usb: common: " Andy Shevchenko
  8 siblings, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/ide/hpt366.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/drivers/ide/hpt366.c b/drivers/ide/hpt366.c
index 696b6c1..f94baad 100644
--- a/drivers/ide/hpt366.c
+++ b/drivers/ide/hpt366.c
@@ -531,14 +531,9 @@ static const struct hpt_info hpt371n = {
 	.timings	= &hpt37x_timings
 };
 
-static int check_in_drive_list(ide_drive_t *drive, const char **list)
+static bool check_in_drive_list(ide_drive_t *drive, const char **list)
 {
-	char *m = (char *)&drive->id[ATA_ID_PROD];
-
-	while (*list)
-		if (!strcmp(*list++, m))
-			return 1;
-	return 0;
+	return match_string(list, -1, (char *)&drive->id[ATA_ID_PROD]) >= 0;
 }
 
 static struct hpt_info *hpt3xx_get_info(struct device *dev)
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* [PATCH v4 9/9] usb: common: convert to use match_string() helper
  2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (7 preceding siblings ...)
  2016-01-28 13:14 ` [PATCH v4 8/9] ide: " Andy Shevchenko
@ 2016-01-28 13:14 ` Andy Shevchenko
  8 siblings, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-01-28 13:14 UTC (permalink / raw)
  To: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes
  Cc: Heikki Krogerus, Andy Shevchenko

From: Heikki Krogerus <heikki.krogerus@linux.intel.com>

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Heikki Krogerus <heikki.krogerus@linux.intel.com>
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/usb/common/common.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/drivers/usb/common/common.c b/drivers/usb/common/common.c
index 49fbfe8..e3d0161 100644
--- a/drivers/usb/common/common.c
+++ b/drivers/usb/common/common.c
@@ -65,18 +65,15 @@ EXPORT_SYMBOL_GPL(usb_speed_string);
 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
 {
 	const char *maximum_speed;
-	int err;
-	int i;
+	int ret;
 
-	err = device_property_read_string(dev, "maximum-speed", &maximum_speed);
-	if (err < 0)
+	ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
+	if (ret < 0)
 		return USB_SPEED_UNKNOWN;
 
-	for (i = 0; i < ARRAY_SIZE(speed_names); i++)
-		if (strcmp(maximum_speed, speed_names[i]) == 0)
-			return i;
+	ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
 
-	return USB_SPEED_UNKNOWN;
+	return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
 }
 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
 
@@ -110,13 +107,10 @@ static const char *const usb_dr_modes[] = {
 
 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
 {
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
-		if (!strcmp(usb_dr_modes[i], str))
-			return i;
+	int ret;
 
-	return USB_DR_MODE_UNKNOWN;
+	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
+	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
 }
 
 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
-- 
2.7.0.rc3

^ permalink raw reply related	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 3/9] pinctrl: convert to use match_string() helper
  2016-01-28 13:14 ` [PATCH v4 3/9] pinctrl: " Andy Shevchenko
@ 2016-02-02  6:02   ` Andrew Morton
  2016-02-02  8:08     ` Andy Shevchenko
  0 siblings, 1 reply; 15+ messages in thread
From: Andrew Morton @ 2016-02-02  6:02 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Rasmus Villemoes

On Thu, 28 Jan 2016 15:14:19 +0200 Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> The new helper returns index of the mathing string in an array. We would use it
> here.
> 
> --- a/drivers/pinctrl/pinmux.c
> +++ b/drivers/pinctrl/pinmux.c
> @@ -334,7 +334,6 @@ int pinmux_map_to_setting(struct pinctrl_map const *map,
>  	unsigned num_groups;
>  	int ret;
>  	const char *group;
> -	int i;
>  
>  	if (!pmxops) {
>  		dev_err(pctldev->dev, "does not support mux function\n");
> @@ -363,19 +362,13 @@ int pinmux_map_to_setting(struct pinctrl_map const *map,
>  		return -EINVAL;
>  	}
>  	if (map->data.mux.group) {
> -		bool found = false;
>  		group = map->data.mux.group;
> -		for (i = 0; i < num_groups; i++) {
> -			if (!strcmp(group, groups[i])) {
> -				found = true;
> -				break;
> -			}
> -		}
> -		if (!found) {
> +		ret = match_string(groups, num_groups, group);
> +		if (ret < 0) {
>  			dev_err(pctldev->dev,
>  				"invalid group \"%s\" for function \"%s\"\n",
>  				group, map->data.mux.function);
> -			return -EINVAL;
> +			return ret;

Changes the return value from -EINVAL to -ENODATA.  I'm not reeeeeealy
sure what ENODATA means - it seems mostly a networking thing?  People
use it in various places because it kinda sounds like whatever it is
that just went wrong.

But the question is: what will the end user think when this error comes
out of the kernel?  Given that he/she has just tried to misconfigure
the pinctrl system, ENODATA will cause much head-scratching.  EINVAL is
more appropriate?  "You tried to do something invalid".

>  		}
>  	} else {
>  		group = groups[0];
> -- 
> 2.7.0.rc3

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 1/9] lib/string: introduce match_string() helper
  2016-01-28 13:14 ` [PATCH v4 1/9] " Andy Shevchenko
@ 2016-02-02  6:05   ` Andrew Morton
  2016-02-02  8:00     ` Andy Shevchenko
  2016-02-02 19:50     ` Rasmus Villemoes
  0 siblings, 2 replies; 15+ messages in thread
From: Andrew Morton @ 2016-02-02  6:05 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Rasmus Villemoes

On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> >From time to time we have to match a string in an array. Make a simple helper
> for that purpose.
> 
> ...
>
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -131,6 +131,8 @@ extern void argv_free(char **argv);
>  extern bool sysfs_streq(const char *s1, const char *s2);
>  extern int strtobool(const char *s, bool *res);
>  
> +int match_string(const char * const *array, size_t n, const char *string);
> +
>  #ifdef CONFIG_BINARY_PRINTF
>  int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
>  int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
> diff --git a/lib/string.c b/lib/string.c
> index 0323c0d..ba01d4f 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -631,6 +631,32 @@ bool sysfs_streq(const char *s1, const char *s2)
>  EXPORT_SYMBOL(sysfs_streq);
>  
>  /**
> + * match_string - matches given string in an array

I can't say I'm in love with the name, but I guess it will suffice. 
search_string() doesn't seem much better.


> + * @array:	array of strings
> + * @n:		number of strings in the array or -1 for NULL terminated arrays
> + * @string:	string to match with
> + *
> + * Return:
> + * index of a @string in the @array if matches, or %-ENODATA otherwise.
> + */
> +int match_string(const char * const *array, size_t n, const char *string)
> +{
> +	int index;
> +	const char *item;
> +
> +	for (index = 0; index < n; index++) {

So we're taking an int and comparing that with (size_t)-1, relying upon
the compiler promoting the int to an unsigned type because size_t is
unsigned.  It works, but it isn't pretty - there wasn't really much
point in making size have type size_t.


> +		item = array[index];
> +		if (!item)
> +			break;
> +		if (!strcmp(item, string))
> +			return index;
> +	}
> +
> +	return -ENODATA;
> +}
> +EXPORT_SYMBOL(match_string);

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 1/9] lib/string: introduce match_string() helper
  2016-02-02  6:05   ` Andrew Morton
@ 2016-02-02  8:00     ` Andy Shevchenko
  2016-02-02 19:50     ` Rasmus Villemoes
  1 sibling, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-02-02  8:00 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Rasmus Villemoes

On Mon, 2016-02-01 at 22:05 -0800, Andrew Morton wrote:
> On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko
> @linux.intel.com> wrote:

> > + * @array:	array of strings
> > + * @n:		number of strings in the array or -1 for
> > NULL terminated arrays
> > + * @string:	string to match with
> > + *
> > + * Return:
> > + * index of a @string in the @array if matches, or %-ENODATA
> > otherwise.
> > + */
> > +int match_string(const char * const *array, size_t n, const char
> > *string)
> > +{
> > +	int index;
> > +	const char *item;
> > +
> > +	for (index = 0; index < n; index++) {
> 
> So we're taking an int and comparing that with (size_t)-1, relying
> upon
> the compiler promoting the int to an unsigned type because size_t is
> unsigned.  It works, but it isn't pretty - there wasn't really much
> point in making size have type size_t.

It was Rasmus' idea [1], before that I used int and tristate branch. I
agreed that one of that branches wasn't a good idea, but the rest was
exactly about differentiating size to compare and infinite (till
terminator) loop.

[1] http://www.spinics.net/lists/kernel/msg2156925.html

> 
> 
> > +		item = array[index];
> > +		if (!item)
> > +			break;
> > +		if (!strcmp(item, string))
> > +			return index;
> > +	}
> > +
> > +	return -ENODATA;
> > +}
> > +EXPORT_SYMBOL(match_string);
> 

-- 
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Intel Finland Oy

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 3/9] pinctrl: convert to use match_string() helper
  2016-02-02  6:02   ` Andrew Morton
@ 2016-02-02  8:08     ` Andy Shevchenko
  0 siblings, 0 replies; 15+ messages in thread
From: Andy Shevchenko @ 2016-02-02  8:08 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Rasmus Villemoes

On Mon, 2016-02-01 at 22:02 -0800, Andrew Morton wrote:
> On Thu, 28 Jan 2016 15:14:19 +0200 Andy Shevchenko <andriy.shevchenko
> @linux.intel.com> wrote:
> 
> > The new helper returns index of the mathing string in an array. We
> > would use it
> > here.
> > 
> > --- a/drivers/pinctrl/pinmux.c
> > +++ b/drivers/pinctrl/pinmux.c
> > @@ -334,7 +334,6 @@ int pinmux_map_to_setting(struct pinctrl_map
> > const *map,
> >  	unsigned num_groups;
> >  	int ret;
> >  	const char *group;
> > -	int i;
> >  
> >  	if (!pmxops) {
> >  		dev_err(pctldev->dev, "does not support mux
> > function\n");
> > @@ -363,19 +362,13 @@ int pinmux_map_to_setting(struct pinctrl_map
> > const *map,
> >  		return -EINVAL;
> >  	}
> >  	if (map->data.mux.group) {
> > -		bool found = false;
> >  		group = map->data.mux.group;
> > -		for (i = 0; i < num_groups; i++) {
> > -			if (!strcmp(group, groups[i])) {
> > -				found = true;
> > -				break;
> > -			}
> > -		}
> > -		if (!found) {
> > +		ret = match_string(groups, num_groups, group);
> > +		if (ret < 0) {
> >  			dev_err(pctldev->dev,
> >  				"invalid group \"%s\" for function
> > \"%s\"\n",
> >  				group, map->data.mux.function);
> > -			return -EINVAL;
> > +			return ret;
> 
> Changes the return value from -EINVAL to -ENODATA.

Yeah, Al is concerned about this as well [1]. That's why I emphasized
this in cover letter.

>   I'm not reeeeeealy
> sure what ENODATA means - it seems mostly a networking thing?  People
> use it in various places because it kinda sounds like whatever it is
> that just went wrong.
> 
> But the question is: what will the end user think when this error
> comes
> out of the kernel?  Given that he/she has just tried to misconfigure
> the pinctrl system, ENODATA will cause much head-scratching.  EINVAL
> is
> more appropriate?  "You tried to do something invalid".

Yeah, my arguments still the same, our error reporting from 70s sucks.

Since few people are concerned about EINVAL vs. ENODATA, I will re-do
patches 1 and 2 to follow this default instead of ENODATA.

[1] http://www.spinics.net/lists/kernel/msg2157541.html

> >  		}
> >  	} else {
> >  		group = groups[0];

-- 
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Intel Finland Oy

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v4 1/9] lib/string: introduce match_string() helper
  2016-02-02  6:05   ` Andrew Morton
  2016-02-02  8:00     ` Andy Shevchenko
@ 2016-02-02 19:50     ` Rasmus Villemoes
  1 sibling, 0 replies; 15+ messages in thread
From: Rasmus Villemoes @ 2016-02-02 19:50 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Andy Shevchenko, Tejun Heo, Linus Walleij,
	Dmitry Eremin-Solenikov, linux-kernel, linux-pm,
	David S . Miller, David Airlie

On Tue, Feb 02 2016, Andrew Morton <akpm@linux-foundation.org> wrote:

> On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
>
>> + * @array:	array of strings
>> + * @n:		number of strings in the array or -1 for NULL terminated arrays
>> + * @string:	string to match with
>> + *
>> + * Return:
>> + * index of a @string in the @array if matches, or %-ENODATA otherwise.
>> + */
>> +int match_string(const char * const *array, size_t n, const char *string)
>> +{
>> +	int index;
>> +	const char *item;
>> +
>> +	for (index = 0; index < n; index++) {
>
> So we're taking an int and comparing that with (size_t)-1, relying upon
> the compiler promoting the int to an unsigned type because size_t is
> unsigned.  It works, but it isn't pretty - there wasn't really much
> point in making size have type size_t.

n has unsigned type to make it easy to pass 'infinity'; *that's* where
we rely on integer promotion.

One could make index be unsigned int (or size_t), but it won't matter,
because it will never have a value where the type promotion (nor the
implicit cast back to int if/when it's used as a return value) changes
its value.

Rasmus

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2016-02-02 19:50 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-28 13:14 [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko
2016-01-28 13:14 ` [PATCH v4 1/9] " Andy Shevchenko
2016-02-02  6:05   ` Andrew Morton
2016-02-02  8:00     ` Andy Shevchenko
2016-02-02 19:50     ` Rasmus Villemoes
2016-01-28 13:14 ` [PATCH v4 2/9] device property: convert to use " Andy Shevchenko
2016-01-28 13:14 ` [PATCH v4 3/9] pinctrl: " Andy Shevchenko
2016-02-02  6:02   ` Andrew Morton
2016-02-02  8:08     ` Andy Shevchenko
2016-01-28 13:14 ` [PATCH v4 4/9] drm/edid: " Andy Shevchenko
2016-01-28 13:14 ` [PATCH v4 5/9] power: charger_manager: " Andy Shevchenko
2016-01-28 13:14 ` [PATCH v4 6/9] power: ab8500: " Andy Shevchenko
2016-01-28 13:14 ` [PATCH v4 7/9] ata: hpt366: " Andy Shevchenko
2016-01-28 13:14 ` [PATCH v4 8/9] ide: " Andy Shevchenko
2016-01-28 13:14 ` [PATCH v4 9/9] usb: common: " Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).