Skip to content

Commit

Permalink
extcon: Fix hang and extcon_get/set_cable_state().
Browse files Browse the repository at this point in the history
Users of find_cable_index_by_name() will cause a kernel hang
as the while loop counter is never incremented and end condition
is never reached.

extcon_get_cable_state() and extcon_set_cable_state() are broken
because they use cable index instead of cable id. This causes
the first cable state (cable.0) to be always invalid in sysfs
or extcon_get_cable_state() users.

Introduce a new function find_cable_id_by_name() that fixes
both of the above issues.

Fixes: commit 73b6ecd ("extcon: Redefine the unique id of supported external connectors without 'enum extcon' type")
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Roger Quadros <rogerq@ti.com>
Tested-by: Ivan T. Ivanov <ivan.ivanov@linaro.org>
[cw00.choi: Fix minor coding style]
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
  • Loading branch information
Roger Quadros authored and Chanwoo Choi committed Jul 31, 2015
1 parent e350f80 commit be052cc
Showing 1 changed file with 34 additions and 12 deletions.
46 changes: 34 additions & 12 deletions drivers/extcon/extcon.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,35 @@ static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id
return -EINVAL;
}

static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
static int find_cable_id_by_name(struct extcon_dev *edev, const char *name)
{
unsigned int id = EXTCON_NONE;
unsigned int id = -EINVAL;
int i = 0;

if (edev->max_supported == 0)
return -EINVAL;

/* Find the the number of extcon cable */
/* Find the id of extcon cable */
while (extcon_name[i]) {
if (!strncmp(extcon_name[i], name, CABLE_NAME_MAX)) {
id = i;
break;
}
i++;
}

if (id == EXTCON_NONE)
return id;
}

static int find_cable_index_by_name(struct extcon_dev *edev, const char *name)
{
unsigned int id;

if (edev->max_supported == 0)
return -EINVAL;

/* Find the the number of extcon cable */
id = find_cable_id_by_name(edev, name);
if (id < 0)
return id;

return find_cable_index_by_id(edev, id);
}

Expand Down Expand Up @@ -228,9 +238,11 @@ static ssize_t cable_state_show(struct device *dev,
struct extcon_cable *cable = container_of(attr, struct extcon_cable,
attr_state);

int i = cable->cable_index;

return sprintf(buf, "%d\n",
extcon_get_cable_state_(cable->edev,
cable->cable_index));
cable->edev->supported_cable[i]));
}

/**
Expand Down Expand Up @@ -361,8 +373,13 @@ EXPORT_SYMBOL_GPL(extcon_get_cable_state_);
*/
int extcon_get_cable_state(struct extcon_dev *edev, const char *cable_name)
{
return extcon_get_cable_state_(edev, find_cable_index_by_name
(edev, cable_name));
unsigned int id;

id = find_cable_id_by_name(edev, cable_name);
if (id < 0)
return id;

return extcon_get_cable_state_(edev, id);
}
EXPORT_SYMBOL_GPL(extcon_get_cable_state);

Expand Down Expand Up @@ -404,8 +421,13 @@ EXPORT_SYMBOL_GPL(extcon_set_cable_state_);
int extcon_set_cable_state(struct extcon_dev *edev,
const char *cable_name, bool cable_state)
{
return extcon_set_cable_state_(edev, find_cable_index_by_name
(edev, cable_name), cable_state);
unsigned int id;

id = find_cable_id_by_name(edev, cable_name);
if (id < 0)
return id;

return extcon_set_cable_state_(edev, id, cable_state);
}
EXPORT_SYMBOL_GPL(extcon_set_cable_state);

Expand Down

0 comments on commit be052cc

Please sign in to comment.