drivers/input/joystick/cobra.c
Source file repositories/reference/linux-study-clean/drivers/input/joystick/cobra.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/input/joystick/cobra.c- Extension
.c- Size
- 5635 bytes
- Lines
- 245
- 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.
- 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/slab.hlinux/gameport.hlinux/input.hlinux/jiffies.h
Detected Declarations
struct cobrafunction cobra_read_packetfunction cobra_pollfunction cobra_openfunction cobra_closefunction cobra_connectfunction cobra_disconnect
Annotated Snippet
struct cobra {
struct gameport *gameport;
struct input_dev *dev[2];
int reads;
int bads;
unsigned char exists;
char phys[2][32];
};
static unsigned char cobra_read_packet(struct gameport *gameport, unsigned int *data)
{
unsigned long flags;
unsigned char u, v, w;
__u64 buf[2];
int r[2], t[2];
int i, j, ret;
int strobe = gameport_time(gameport, COBRA_MAX_STROBE);
for (i = 0; i < 2; i++) {
r[i] = buf[i] = 0;
t[i] = COBRA_MAX_STROBE;
}
local_irq_save(flags);
u = gameport_read(gameport);
do {
t[0]--; t[1]--;
v = gameport_read(gameport);
for (i = 0, w = u ^ v; i < 2 && w; i++, w >>= 2)
if (w & 0x30) {
if ((w & 0x30) < 0x30 && r[i] < COBRA_LENGTH && t[i] > 0) {
buf[i] |= (__u64)((w >> 5) & 1) << r[i]++;
t[i] = strobe;
u = v;
} else t[i] = 0;
}
} while (t[0] > 0 || t[1] > 0);
local_irq_restore(flags);
ret = 0;
for (i = 0; i < 2; i++) {
if (r[i] != COBRA_LENGTH) continue;
for (j = 0; j < COBRA_LENGTH && (buf[i] & 0x04104107f) ^ 0x041041040; j++)
buf[i] = (buf[i] >> 1) | ((__u64)(buf[i] & 1) << (COBRA_LENGTH - 1));
if (j < COBRA_LENGTH) ret |= (1 << i);
data[i] = ((buf[i] >> 7) & 0x000001f) | ((buf[i] >> 8) & 0x00003e0)
| ((buf[i] >> 9) & 0x0007c00) | ((buf[i] >> 10) & 0x00f8000)
| ((buf[i] >> 11) & 0x1f00000);
}
return ret;
}
static void cobra_poll(struct gameport *gameport)
{
struct cobra *cobra = gameport_get_drvdata(gameport);
struct input_dev *dev;
unsigned int data[2];
int i, j, r;
cobra->reads++;
if ((r = cobra_read_packet(gameport, data)) != cobra->exists) {
cobra->bads++;
return;
}
for (i = 0; i < 2; i++)
if (cobra->exists & r & (1 << i)) {
dev = cobra->dev[i];
input_report_abs(dev, ABS_X, ((data[i] >> 4) & 1) - ((data[i] >> 3) & 1));
input_report_abs(dev, ABS_Y, ((data[i] >> 2) & 1) - ((data[i] >> 1) & 1));
for (j = 0; cobra_btn[j]; j++)
input_report_key(dev, cobra_btn[j], data[i] & (0x20 << j));
input_sync(dev);
Annotation
- Immediate include surface: `linux/kernel.h`, `linux/module.h`, `linux/slab.h`, `linux/gameport.h`, `linux/input.h`, `linux/jiffies.h`.
- Detected declarations: `struct cobra`, `function cobra_read_packet`, `function cobra_poll`, `function cobra_open`, `function cobra_close`, `function cobra_connect`, `function cobra_disconnect`.
- Atlas domain: Driver Families / drivers/input.
- Implementation status: source implementation candidate.
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.