drivers/input/keyboard/dlink-dir685-touchkeys.c

Source file repositories/reference/linux-study-clean/drivers/input/keyboard/dlink-dir685-touchkeys.c

File Facts

System
Linux kernel
Corpus path
drivers/input/keyboard/dlink-dir685-touchkeys.c
Extension
.c
Size
3932 bytes
Lines
157
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 dir685_touchkeys {
	struct device		*dev;
	struct i2c_client	*client;
	struct input_dev	*input;
	unsigned long		cur_key;
	u16			codes[7];
};

static irqreturn_t dir685_tk_irq_thread(int irq, void *data)
{
	struct dir685_touchkeys *tk = data;
	const int num_bits = min_t(int, ARRAY_SIZE(tk->codes), 16);
	unsigned long changed;
	u8 buf[6];
	unsigned long key;
	int i;
	int err;

	memset(buf, 0, sizeof(buf));
	err = i2c_master_recv(tk->client, buf, sizeof(buf));
	if (err != sizeof(buf)) {
		dev_err(tk->dev, "short read %d\n", err);
		return IRQ_HANDLED;
	}

	dev_dbg(tk->dev, "IN: %*ph\n", (int)sizeof(buf), buf);
	key = be16_to_cpup((__be16 *) &buf[4]);

	/* Figure out if any bits went high or low since last message */
	changed = tk->cur_key ^ key;
	for_each_set_bit(i, &changed, num_bits) {
		dev_dbg(tk->dev, "key %d is %s\n", i,
			str_down_up(test_bit(i, &key)));
		input_report_key(tk->input, tk->codes[i], test_bit(i, &key));
	}

	/* Store currently down keys */
	tk->cur_key = key;
	input_sync(tk->input);

	return IRQ_HANDLED;
}

static int dir685_tk_probe(struct i2c_client *client)
{
	static const u8 bl_data[] = { 0xa7, 0x40 };
	struct device *dev = &client->dev;
	struct dir685_touchkeys *tk;
	int err;
	int i;

	tk = devm_kzalloc(&client->dev, sizeof(*tk), GFP_KERNEL);
	if (!tk)
		return -ENOMEM;

	tk->input = devm_input_allocate_device(dev);
	if (!tk->input)
		return -ENOMEM;

	tk->client = client;
	tk->dev = dev;

	tk->input->keycodesize = sizeof(u16);
	tk->input->keycodemax = ARRAY_SIZE(tk->codes);
	tk->input->keycode = tk->codes;
	tk->codes[0] = KEY_UP;
	tk->codes[1] = KEY_DOWN;
	tk->codes[2] = KEY_LEFT;
	tk->codes[3] = KEY_RIGHT;
	tk->codes[4] = KEY_ENTER;
	tk->codes[5] = KEY_WPS_BUTTON;
	/*
	 * This key appears in the vendor driver, but I have
	 * not been able to activate it.
	 */
	tk->codes[6] = KEY_RESERVED;

	__set_bit(EV_KEY, tk->input->evbit);
	for (i = 0; i < ARRAY_SIZE(tk->codes); i++)
		__set_bit(tk->codes[i], tk->input->keybit);
	__clear_bit(KEY_RESERVED, tk->input->keybit);

	tk->input->name = "D-Link DIR-685 touchkeys";
	tk->input->id.bustype = BUS_I2C;

	err = input_register_device(tk->input);
	if (err)
		return err;

	/* Set the brightness to max level */

Annotation

Implementation Notes