linux-pm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/9] lib/string: introduce match_string() helper
@ 2016-01-08 13:09 Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 1/9] " Andy Shevchenko
                   ` (9 more replies)
  0 siblings, 10 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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.

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.6.4

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

* [PATCH v3 1/9] lib/string: introduce match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 2/9] device property: convert to use " Andy Shevchenko
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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.6.4

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

* [PATCH v3 2/9] device property: convert to use match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 1/9] " Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 23:33   ` Rafael J. Wysocki
  2016-01-08 13:09 ` [PATCH v3 3/9] pinctrl: " Andy Shevchenko
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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>
---
 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.6.4

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

* [PATCH v3 3/9] pinctrl: convert to use match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 1/9] " Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 2/9] device property: convert to use " Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 4/9] drm/edid: " Andy Shevchenko
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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.6.4


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

* [PATCH v3 4/9] drm/edid: convert to use match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (2 preceding siblings ...)
  2016-01-08 13:09 ` [PATCH v3 3/9] pinctrl: " Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 5/9] power: charger_manager: " Andy Shevchenko
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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.6.4

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

* [PATCH v3 5/9] power: charger_manager: convert to use match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (3 preceding siblings ...)
  2016-01-08 13:09 ` [PATCH v3 4/9] drm/edid: " Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 6/9] power: ab8500: " Andy Shevchenko
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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.6.4

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

* [PATCH v3 6/9] power: ab8500: convert to use match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (4 preceding siblings ...)
  2016-01-08 13:09 ` [PATCH v3 5/9] power: charger_manager: " Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 7/9] ata: hpt366: " Andy Shevchenko
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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.6.4

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

* [PATCH v3 7/9] ata: hpt366: convert to use match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (5 preceding siblings ...)
  2016-01-08 13:09 ` [PATCH v3 6/9] power: ab8500: " Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 8/9] ide: " Andy Shevchenko
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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.6.4

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

* [PATCH v3 8/9] ide: hpt366: convert to use match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (6 preceding siblings ...)
  2016-01-08 13:09 ` [PATCH v3 7/9] ata: hpt366: " Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 13:09 ` [PATCH v3 9/9] usb: common: " Andy Shevchenko
  2016-01-08 13:19 ` [PATCH v3 0/9] lib/string: introduce " Al Viro
  9 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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.6.4


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

* [PATCH v3 9/9] usb: common: convert to use match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (7 preceding siblings ...)
  2016-01-08 13:09 ` [PATCH v3 8/9] ide: " Andy Shevchenko
@ 2016-01-08 13:09 ` Andy Shevchenko
  2016-01-08 13:19 ` [PATCH v3 0/9] lib/string: introduce " Al Viro
  9 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-08 13:09 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 e6ec125..677b3f0 100644
--- a/drivers/usb/common/common.c
+++ b/drivers/usb/common/common.c
@@ -64,18 +64,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);
 
@@ -109,13 +106,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.6.4

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

* Re: [PATCH v3 0/9] lib/string: introduce match_string() helper
  2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (8 preceding siblings ...)
  2016-01-08 13:09 ` [PATCH v3 9/9] usb: common: " Andy Shevchenko
@ 2016-01-08 13:19 ` Al Viro
  2016-01-08 14:13   ` Andy Shevchenko
  9 siblings, 1 reply; 14+ messages in thread
From: Al Viro @ 2016-01-08 13:19 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes

On Fri, Jan 08, 2016 at 03:09:09PM +0200, Andy Shevchenko wrote:
> There are users of a simple string matching in the array. Let's do a common
> helper for that.
 
What's the reason for making it return -ENODATA when no match is found?
That one of the callers wants to return that as error in such case?
At least one other is returning -EINVAL in the same situation...

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

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

On Fri, 2016-01-08 at 13:19 +0000, Al Viro wrote:
> On Fri, Jan 08, 2016 at 03:09:09PM +0200, Andy Shevchenko wrote:
> > There are users of a simple string matching in the array. Let's do
> > a common
> > helper for that.
>  
> What's the reason for making it return -ENODATA when no match is
> found?

What else can be suitable?

> That one of the callers wants to return that as error in such case?
> At least one other is returning -EINVAL in the same situation...

Linus Acked this, maybe he missed that one.

Linus, do we still need to return -EINVAL in pinmux?

In general our error reporting sucks, you know. So, any return value
will be not ideal and self-explanatory (see json approach in perf). But
I prefer return some return code instead of opaque -1, for example.
This at least helps some users.

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

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

* Re: [PATCH v3 2/9] device property: convert to use match_string() helper
  2016-01-08 13:09 ` [PATCH v3 2/9] device property: convert to use " Andy Shevchenko
@ 2016-01-08 23:33   ` Rafael J. Wysocki
  2016-01-28 12:40     ` Andy Shevchenko
  0 siblings, 1 reply; 14+ messages in thread
From: Rafael J. Wysocki @ 2016-01-08 23:33 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes

On Friday, January 08, 2016 03:09:11 PM Andy Shevchenko wrote:
> 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>

in case you need that.

I think though that it might be more efficient to simply push [1/9] first
maybe with one user and then send the other patches to the relevant maintainers
when that has been applied.

> ---
>  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;
> 

-- 
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.

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

* Re: [PATCH v3 2/9] device property: convert to use match_string() helper
  2016-01-08 23:33   ` Rafael J. Wysocki
@ 2016-01-28 12:40     ` Andy Shevchenko
  0 siblings, 0 replies; 14+ messages in thread
From: Andy Shevchenko @ 2016-01-28 12:40 UTC (permalink / raw)
  To: Rafael J. Wysocki
  Cc: Tejun Heo, Linus Walleij, Dmitry Eremin-Solenikov, linux-kernel,
	linux-pm, David S . Miller, David Airlie, Andrew Morton,
	Rasmus Villemoes

On Sat, 2016-01-09 at 00:33 +0100, Rafael J. Wysocki wrote:
> On Friday, January 08, 2016 03:09:11 PM Andy Shevchenko wrote:
> > 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>
> 
> in case you need that.

Thanks!

> 
> I think though that it might be more efficient to simply push [1/9]
> first
> maybe with one user and then send the other patches to the relevant
> maintainers
> when that has been applied.

I tried to do such with the first version, there was a comment besides
others that there is not enough '-' lines in the statistics ;-)


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


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

end of thread, other threads:[~2016-01-28 12:40 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-08 13:09 [PATCH v3 0/9] lib/string: introduce match_string() helper Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 1/9] " Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 2/9] device property: convert to use " Andy Shevchenko
2016-01-08 23:33   ` Rafael J. Wysocki
2016-01-28 12:40     ` Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 3/9] pinctrl: " Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 4/9] drm/edid: " Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 5/9] power: charger_manager: " Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 6/9] power: ab8500: " Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 7/9] ata: hpt366: " Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 8/9] ide: " Andy Shevchenko
2016-01-08 13:09 ` [PATCH v3 9/9] usb: common: " Andy Shevchenko
2016-01-08 13:19 ` [PATCH v3 0/9] lib/string: introduce " Al Viro
2016-01-08 14:13   ` 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).