drivers/input/joystick/walkera0701.c

Source file repositories/reference/linux-study-clean/drivers/input/joystick/walkera0701.c

File Facts

System
Linux kernel
Corpus path
drivers/input/joystick/walkera0701.c
Extension
.c
Size
8400 bytes
Lines
298
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.

Dependency Surface

Detected Declarations

Annotated Snippet

struct walkera_dev {
	unsigned char buf[25];
	u64 irq_time, irq_lasttime;
	int counter;
	int ack;

	struct input_dev *input_dev;
	struct hrtimer timer;

	struct parport *parport;
	struct pardevice *pardevice;
};

static struct walkera_dev w_dev;

static inline void walkera0701_parse_frame(struct walkera_dev *w)
{
	int i;
	int val1, val2, val3, val4, val5, val6, val7, val8;
	int magic, magic_bit;
	int crc1, crc2;

	for (crc1 = crc2 = i = 0; i < 10; i++) {
		crc1 += w->buf[i] & 7;
		crc2 += (w->buf[i] & 8) >> 3;
	}
	if ((w->buf[10] & 7) != (crc1 & 7))
		return;
	if (((w->buf[10] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
		return;
	for (crc1 = crc2 = 0, i = 11; i < 23; i++) {
		crc1 += w->buf[i] & 7;
		crc2 += (w->buf[i] & 8) >> 3;
	}
	if ((w->buf[23] & 7) != (crc1 & 7))
		return;
	if (((w->buf[23] & 8) >> 3) != (((crc1 >> 3) + crc2) & 1))
		return;
	val1 = ((w->buf[0] & 7) * 256 + w->buf[1] * 16 + w->buf[2]) >> 2;
	val1 *= ((w->buf[0] >> 2) & 2) - 1;	/* sign */
	val2 = (w->buf[2] & 1) << 8 | (w->buf[3] << 4) | w->buf[4];
	val2 *= (w->buf[2] & 2) - 1;	/* sign */
	val3 = ((w->buf[5] & 7) * 256 + w->buf[6] * 16 + w->buf[7]) >> 2;
	val3 *= ((w->buf[5] >> 2) & 2) - 1;	/* sign */
	val4 = (w->buf[7] & 1) << 8 | (w->buf[8] << 4) | w->buf[9];
	val4 *= (w->buf[7] & 2) - 1;	/* sign */
	val5 = ((w->buf[11] & 7) * 256 + w->buf[12] * 16 + w->buf[13]) >> 2;
	val5 *= ((w->buf[11] >> 2) & 2) - 1;	/* sign */
	val6 = (w->buf[13] & 1) << 8 | (w->buf[14] << 4) | w->buf[15];
	val6 *= (w->buf[13] & 2) - 1;	/* sign */
	val7 = ((w->buf[16] & 7) * 256 + w->buf[17] * 16 + w->buf[18]) >> 2;
	val7 *= ((w->buf[16] >> 2) & 2) - 1;	/*sign */
	val8 = (w->buf[18] & 1) << 8 | (w->buf[19] << 4) | w->buf[20];
	val8 *= (w->buf[18] & 2) - 1;	/*sign */

	magic = (w->buf[21] << 4) | w->buf[22];
	magic_bit = (w->buf[24] & 8) >> 3;
	pr_debug("%4d %4d %4d %4d  %4d %4d %4d %4d (magic %2x %d)\n",
		 val1, val2, val3, val4, val5, val6, val7, val8,
		 magic, magic_bit);

	input_report_abs(w->input_dev, ABS_X, val2);
	input_report_abs(w->input_dev, ABS_Y, val1);
	input_report_abs(w->input_dev, ABS_Z, val6);
	input_report_abs(w->input_dev, ABS_THROTTLE, val3);
	input_report_abs(w->input_dev, ABS_RUDDER, val4);
	input_report_abs(w->input_dev, ABS_MISC, val7);
	input_report_key(w->input_dev, BTN_GEAR_DOWN, val5 > 0);
}

static inline int read_ack(struct pardevice *p)
{
	return parport_read_status(p->port) & 0x40;
}

/* falling edge, prepare to BIN value calculation */
static void walkera0701_irq_handler(void *handler_data)
{
	u64 pulse_time;
	struct walkera_dev *w = handler_data;

	w->irq_time = ktime_to_ns(ktime_get());
	pulse_time = w->irq_time - w->irq_lasttime;
	w->irq_lasttime = w->irq_time;

	/* cancel timer, if in handler or active do resync */
	if (unlikely(0 != hrtimer_try_to_cancel(&w->timer))) {
		w->counter = NO_SYNC;
		return;
	}

Annotation

Implementation Notes