drivers/input/misc/rotary_encoder.c
Source file repositories/reference/linux-study-clean/drivers/input/misc/rotary_encoder.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/input/misc/rotary_encoder.c- Extension
.c- Size
- 8391 bytes
- Lines
- 359
- Domain
- Driver Families
- Bucket
- drivers/input
- Inferred role
- Driver Families: implementation source
- Status
- source implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/kernel.hlinux/module.hlinux/interrupt.hlinux/input.hlinux/device.hlinux/platform_device.hlinux/gpio/consumer.hlinux/slab.hlinux/of.hlinux/pm.hlinux/property.h
Detected Declarations
struct rotary_encoderenum rotary_encoder_encodingfunction rotary_encoder_get_statefunction rotary_encoder_report_eventfunction rotary_encoder_irqfunction rotary_encoder_half_period_irqfunction rotary_encoder_quarter_period_irqfunction rotary_encoder_probefunction rotary_encoder_suspendfunction rotary_encoder_resume
Annotated Snippet
struct rotary_encoder {
struct input_dev *input;
struct mutex access_mutex;
u32 steps;
u32 axis;
bool relative_axis;
bool rollover;
enum rotary_encoder_encoding encoding;
unsigned int pos;
struct gpio_descs *gpios;
unsigned int *irq;
bool armed;
signed char dir; /* 1 - clockwise, -1 - CCW */
unsigned int last_stable;
};
static unsigned int rotary_encoder_get_state(struct rotary_encoder *encoder)
{
int i;
unsigned int ret = 0;
for (i = 0; i < encoder->gpios->ndescs; ++i) {
int val = gpiod_get_value_cansleep(encoder->gpios->desc[i]);
/* convert from gray encoding to normal */
if (encoder->encoding == ROTENC_GRAY && ret & 1)
val = !val;
ret = ret << 1 | val;
}
return ret & 3;
}
static void rotary_encoder_report_event(struct rotary_encoder *encoder)
{
if (encoder->relative_axis) {
input_report_rel(encoder->input,
encoder->axis, encoder->dir);
} else {
unsigned int pos = encoder->pos;
if (encoder->dir < 0) {
/* turning counter-clockwise */
if (encoder->rollover)
pos += encoder->steps;
if (pos)
pos--;
} else {
/* turning clockwise */
if (encoder->rollover || pos < encoder->steps)
pos++;
}
if (encoder->rollover)
pos %= encoder->steps;
encoder->pos = pos;
input_report_abs(encoder->input, encoder->axis, encoder->pos);
}
input_sync(encoder->input);
}
static irqreturn_t rotary_encoder_irq(int irq, void *dev_id)
{
struct rotary_encoder *encoder = dev_id;
unsigned int state;
guard(mutex)(&encoder->access_mutex);
state = rotary_encoder_get_state(encoder);
switch (state) {
case 0x0:
if (encoder->armed) {
rotary_encoder_report_event(encoder);
encoder->armed = false;
}
break;
case 0x1:
case 0x3:
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/interrupt.h`, `linux/input.h`, `linux/device.h`, `linux/platform_device.h`, `linux/gpio/consumer.h`, `linux/slab.h`.
- Detected declarations: `struct rotary_encoder`, `enum rotary_encoder_encoding`, `function rotary_encoder_get_state`, `function rotary_encoder_report_event`, `function rotary_encoder_irq`, `function rotary_encoder_half_period_irq`, `function rotary_encoder_quarter_period_irq`, `function rotary_encoder_probe`, `function rotary_encoder_suspend`, `function rotary_encoder_resume`.
- Atlas domain: Driver Families / drivers/input.
- Implementation status: source implementation candidate.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.