drivers/hid/surface-hid/surface_kbd.c

Source file repositories/reference/linux-study-clean/drivers/hid/surface-hid/surface_kbd.c

File Facts

System
Linux kernel
Corpus path
drivers/hid/surface-hid/surface_kbd.c
Extension
.c
Size
7914 bytes
Lines
300
Domain
Driver Families
Bucket
drivers/hid
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

// SPDX-License-Identifier: GPL-2.0+
/*
 * Surface System Aggregator Module (SSAM) HID transport driver for the legacy
 * keyboard interface (KBD/TC=0x08 subsystem). Provides support for the
 * integrated HID keyboard on Surface Laptops 1 and 2.
 *
 * Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
 */

#include <linux/unaligned.h>
#include <linux/hid.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/types.h>

#include <linux/surface_aggregator/controller.h>

#include "surface_hid_core.h"


/* -- SAM interface (KBD). -------------------------------------------------- */

#define KBD_FEATURE_REPORT_SIZE			7  /* 6 + report ID */

enum surface_kbd_cid {
	SURFACE_KBD_CID_GET_DESCRIPTOR		= 0x00,
	SURFACE_KBD_CID_SET_CAPSLOCK_LED	= 0x01,
	SURFACE_KBD_CID_EVT_INPUT_GENERIC	= 0x03,
	SURFACE_KBD_CID_EVT_INPUT_HOTKEYS	= 0x04,
	SURFACE_KBD_CID_GET_FEATURE_REPORT	= 0x0b,
};

static int ssam_kbd_get_descriptor(struct surface_hid_device *shid, u8 entry, u8 *buf, size_t len)
{
	struct ssam_request rqst;
	struct ssam_response rsp;
	int status;

	rqst.target_category = shid->uid.category;
	rqst.target_id = shid->uid.target;
	rqst.command_id = SURFACE_KBD_CID_GET_DESCRIPTOR;
	rqst.instance_id = shid->uid.instance;
	rqst.flags = SSAM_REQUEST_HAS_RESPONSE;
	rqst.length = sizeof(entry);
	rqst.payload = &entry;

	rsp.capacity = len;
	rsp.length = 0;
	rsp.pointer = buf;

	status = ssam_retry(ssam_request_do_sync_onstack, shid->ctrl, &rqst, &rsp, sizeof(entry));
	if (status)
		return status;

	if (rsp.length != len) {
		dev_err(shid->dev, "invalid descriptor length: got %zu, expected, %zu\n",
			rsp.length, len);
		return -EPROTO;
	}

	return 0;
}

static int ssam_kbd_set_caps_led(struct surface_hid_device *shid, bool value)
{
	struct ssam_request rqst;
	u8 value_u8 = value;

	rqst.target_category = shid->uid.category;
	rqst.target_id = shid->uid.target;
	rqst.command_id = SURFACE_KBD_CID_SET_CAPSLOCK_LED;
	rqst.instance_id = shid->uid.instance;
	rqst.flags = 0;
	rqst.length = sizeof(value_u8);
	rqst.payload = &value_u8;

	return ssam_retry(ssam_request_do_sync_onstack, shid->ctrl, &rqst, NULL, sizeof(value_u8));
}

static int ssam_kbd_get_feature_report(struct surface_hid_device *shid, u8 *buf, size_t len)
{
	struct ssam_request rqst;
	struct ssam_response rsp;
	u8 payload = 0;
	int status;

	rqst.target_category = shid->uid.category;
	rqst.target_id = shid->uid.target;
	rqst.command_id = SURFACE_KBD_CID_GET_FEATURE_REPORT;

Annotation

Implementation Notes