Skip to content

Commit

Permalink
Input: rotary_encoder - use guard notation when acquiring mutex
Browse files Browse the repository at this point in the history
Using guard notation makes the code more compact and error handling
more robust by ensuring that mutexes are released in all code paths
when control leaves critical section.

Reviewed-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
Link: https://lore.kernel.org/r/20240904044929.1049700-1-dmitry.torokhov@gmail.com
Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
  • Loading branch information
dtor committed Oct 3, 2024
1 parent 2dc3876 commit 868d163
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions drivers/input/misc/rotary_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
struct rotary_encoder *encoder = dev_id;
unsigned int state;

mutex_lock(&encoder->access_mutex);
guard(mutex)(&encoder->access_mutex);

state = rotary_encoder_get_state(encoder);

Expand All @@ -129,8 +129,6 @@ static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
break;
}

mutex_unlock(&encoder->access_mutex);

return IRQ_HANDLED;
}

Expand All @@ -139,7 +137,7 @@ static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
struct rotary_encoder *encoder = dev_id;
unsigned int state;

mutex_lock(&encoder->access_mutex);
guard(mutex)(&encoder->access_mutex);

state = rotary_encoder_get_state(encoder);

Expand All @@ -152,8 +150,6 @@ static irqreturn_t rotary_encoder_half_period_irq(int irq, void *dev_id)
}
}

mutex_unlock(&encoder->access_mutex);

return IRQ_HANDLED;
}

Expand All @@ -162,22 +158,19 @@ static irqreturn_t rotary_encoder_quarter_period_irq(int irq, void *dev_id)
struct rotary_encoder *encoder = dev_id;
unsigned int state;

mutex_lock(&encoder->access_mutex);
guard(mutex)(&encoder->access_mutex);

state = rotary_encoder_get_state(encoder);

if ((encoder->last_stable + 1) % 4 == state)
if ((encoder->last_stable + 1) % 4 == state) {
encoder->dir = 1;
else if (encoder->last_stable == (state + 1) % 4)
rotary_encoder_report_event(encoder);
} else if (encoder->last_stable == (state + 1) % 4) {
encoder->dir = -1;
else
goto out;

rotary_encoder_report_event(encoder);
rotary_encoder_report_event(encoder);
}

out:
encoder->last_stable = state;
mutex_unlock(&encoder->access_mutex);

return IRQ_HANDLED;
}
Expand Down

0 comments on commit 868d163

Please sign in to comment.