mirror of
https://github.com/torvalds/linux.git
synced 2026-05-12 16:18:45 +02:00
Input: rotary_encoder - use guard notation when acquiring mutex
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>
This commit is contained in:
parent
2dc387620b
commit
868d163aec
|
|
@ -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);
|
||||
|
||||
|
|
@ -129,8 +129,6 @@ static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
|
|||
break;
|
||||
}
|
||||
|
||||
mutex_unlock(&encoder->access_mutex);
|
||||
|
||||
return IRQ_HANDLED;
|
||||
}
|
||||
|
||||
|
|
@ -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);
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user