Skip to content

Commit

Permalink
media: cec-gpio: handle gpiod_get_value errors correctly
Browse files Browse the repository at this point in the history
gpiod_get_value() can return negative values if an error occurs.
In several places this error code was ignored.

Ensure that errors codes are handled correctly throughout the CEC
pin framework and CEC pin drivers.

The return code of the cec_pin_ops read() callback had to be changed
from 'bool' to 'int', which mean the prototype of that callback in the
sun4i drm driver also had to be changed.

Signed-off-by: Hans Verkuil <[email protected]>
Reported-by: Dan Carpenter <[email protected]>
Signed-off-by: Mauro Carvalho Chehab <[email protected]>
  • Loading branch information
Hans Verkuil authored and mchehab committed Apr 29, 2020
1 parent fcab45a commit e5ad7db
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion drivers/gpu/drm/sun4i/sun4i_hdmi_enc.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static const struct drm_connector_funcs sun4i_hdmi_connector_funcs = {
};

#ifdef CONFIG_DRM_SUN4I_HDMI_CEC
static bool sun4i_hdmi_cec_pin_read(struct cec_adapter *adap)
static int sun4i_hdmi_cec_pin_read(struct cec_adapter *adap)
{
struct sun4i_hdmi *hdmi = cec_get_drvdata(adap);

Expand Down
18 changes: 11 additions & 7 deletions drivers/media/cec/platform/cec-gpio/cec-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ struct cec_gpio {
ktime_t v5_ts;
};

static bool cec_gpio_read(struct cec_adapter *adap)
static int cec_gpio_read(struct cec_adapter *adap)
{
struct cec_gpio *cec = cec_get_drvdata(adap);

if (cec->cec_is_low)
return false;
return 0;
return gpiod_get_value(cec->cec_gpio);
}

Expand Down Expand Up @@ -71,9 +71,10 @@ static irqreturn_t cec_hpd_gpio_irq_handler_thread(int irq, void *priv)
static irqreturn_t cec_5v_gpio_irq_handler(int irq, void *priv)
{
struct cec_gpio *cec = priv;
bool is_high = gpiod_get_value(cec->v5_gpio);
int val = gpiod_get_value(cec->v5_gpio);
bool is_high = val > 0;

if (is_high == cec->v5_is_high)
if (val < 0 || is_high == cec->v5_is_high)
return IRQ_HANDLED;
cec->v5_ts = ktime_get();
cec->v5_is_high = is_high;
Expand All @@ -91,9 +92,10 @@ static irqreturn_t cec_5v_gpio_irq_handler_thread(int irq, void *priv)
static irqreturn_t cec_hpd_gpio_irq_handler(int irq, void *priv)
{
struct cec_gpio *cec = priv;
bool is_high = gpiod_get_value(cec->hpd_gpio);
int val = gpiod_get_value(cec->hpd_gpio);
bool is_high = val > 0;

if (is_high == cec->hpd_is_high)
if (val < 0 || is_high == cec->hpd_is_high)
return IRQ_HANDLED;
cec->hpd_ts = ktime_get();
cec->hpd_is_high = is_high;
Expand All @@ -103,8 +105,10 @@ static irqreturn_t cec_hpd_gpio_irq_handler(int irq, void *priv)
static irqreturn_t cec_gpio_irq_handler(int irq, void *priv)
{
struct cec_gpio *cec = priv;
int val = gpiod_get_value(cec->cec_gpio);

cec_pin_changed(cec->adap, gpiod_get_value(cec->cec_gpio));
if (val >= 0)
cec_pin_changed(cec->adap, val > 0);
return IRQ_HANDLED;
}

Expand Down
16 changes: 7 additions & 9 deletions include/media/cec-pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

/**
* struct cec_pin_ops - low-level CEC pin operations
* @read: read the CEC pin. Return true if high, false if low.
* @read: read the CEC pin. Returns > 0 if high, 0 if low, or an error
* if negative.
* @low: drive the CEC pin low.
* @high: stop driving the CEC pin. The pull-up will drive the pin
* high, unless someone else is driving the pin low.
Expand All @@ -22,21 +23,18 @@
* @free: optional. Free any allocated resources. Called when the
* adapter is deleted.
* @status: optional, log status information.
* @read_hpd: read the HPD pin. Return true if high, false if low or
* an error if negative. If NULL or -ENOTTY is returned,
* then this is not supported.
* @read_5v: read the 5V pin. Return true if high, false if low or
* an error if negative. If NULL or -ENOTTY is returned,
* then this is not supported.
*
* @read_hpd: optional. Read the HPD pin. Returns > 0 if high, 0 if low or
* an error if negative.
* @read_5v: optional. Read the 5V pin. Returns > 0 if high, 0 if low or
* an error if negative.
* @received: optional. High-level CEC message callback. Allows the driver
* to process CEC messages.
*
* These operations (except for the @received op) are used by the
* cec pin framework to manipulate the CEC pin.
*/
struct cec_pin_ops {
bool (*read)(struct cec_adapter *adap);
int (*read)(struct cec_adapter *adap);
void (*low)(struct cec_adapter *adap);
void (*high)(struct cec_adapter *adap);
bool (*enable_irq)(struct cec_adapter *adap);
Expand Down

0 comments on commit e5ad7db

Please sign in to comment.