Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0b28cb4b authored by Srinivas Pandruvada's avatar Srinivas Pandruvada Committed by Jiri Kosina
Browse files

HID: intel-ish-hid: ISH HID client driver



This driver is responsible for implementing ISH HID client, which
gets HID description and report. Once it has completely gets
report descriptors, it registers as a HID LL drivers. This implements
necessary callbacks so that it can be used by HID sensor hub driver.

Original-author: Daniel Drubin <daniel.drubin@intel.com>
Reviewed-and-tested-by: default avatarOoi, Joyce <joyce.ooi@intel.com>
Tested-by: default avatarGrant Likely <grant.likely@secretlab.ca>
Tested-by: default avatarRann Bar-On <rb6@duke.edu>
Tested-by: default avatarAtri Bhattacharya <badshah400@aim.com>
Signed-off-by: default avatarSrinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Signed-off-by: default avatarJiri Kosina <jkosina@suse.cz>
parent ae02e5d4
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -15,4 +15,8 @@ obj-$(CONFIG_INTEL_ISH_HID) += intel-ish-ipc.o
intel-ish-ipc-objs := ipc/ipc.o
intel-ish-ipc-objs += ipc/pci-ish.o

obj-$(CONFIG_INTEL_ISH_HID) += intel-ishtp-hid.o
intel-ishtp-hid-objs := ishtp-hid.o
intel-ishtp-hid-objs += ishtp-hid-client.o

ccflags-y += -Idrivers/hid/intel-ish-hid/ishtp
+978 −0

File added.

Preview size limit exceeded, changes collapsed.

+246 −0
Original line number Diff line number Diff line
/*
 * ISHTP-HID glue driver.
 *
 * Copyright (c) 2012-2016, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 */

#include <linux/hid.h>
#include <uapi/linux/input.h>
#include "ishtp/client.h"
#include "ishtp-hid.h"

/**
 * ishtp_hid_parse() - hid-core .parse() callback
 * @hid:	hid device instance
 *
 * This function gets called during call to hid_add_device
 *
 * Return: 0 on success and non zero on error
 */
static int ishtp_hid_parse(struct hid_device *hid)
{
	struct ishtp_hid_data *hid_data =  hid->driver_data;
	struct ishtp_cl_data *client_data = hid_data->client_data;
	int rv;

	rv = hid_parse_report(hid, client_data->report_descr[hid_data->index],
			      client_data->report_descr_size[hid_data->index]);
	if (rv)
		return	rv;

	return 0;
}

/* Empty callbacks with success return code */
static int ishtp_hid_start(struct hid_device *hid)
{
	return 0;
}

static void ishtp_hid_stop(struct hid_device *hid)
{
}

static int ishtp_hid_open(struct hid_device *hid)
{
	return 0;
}

static void ishtp_hid_close(struct hid_device *hid)
{
}

static int ishtp_raw_request(struct hid_device *hdev, unsigned char reportnum,
	__u8 *buf, size_t len, unsigned char rtype, int reqtype)
{
	return 0;
}

/**
 * ishtp_hid_request() - hid-core .request() callback
 * @hid:	hid device instance
 * @rep:	pointer to hid_report
 * @reqtype:	type of req. [GET|SET]_REPORT
 *
 * This function is used to set/get feaure/input report.
 */
static void ishtp_hid_request(struct hid_device *hid, struct hid_report *rep,
	int reqtype)
{
	struct ishtp_hid_data *hid_data =  hid->driver_data;
	/* the specific report length, just HID part of it */
	unsigned int len = ((rep->size - 1) >> 3) + 1 + (rep->id > 0);
	char *buf;
	unsigned int header_size = sizeof(struct hostif_msg);

	len += header_size;

	hid_data->request_done = false;
	switch (reqtype) {
	case HID_REQ_GET_REPORT:
		hid_ishtp_get_report(hid, rep->id, rep->type);
		break;
	case HID_REQ_SET_REPORT:
		/*
		 * Spare 7 bytes for 64b accesses through
		 * get/put_unaligned_le64()
		 */
		buf = kzalloc(len + 7, GFP_KERNEL);
		if (!buf)
			return;

		hid_output_report(rep, buf + header_size);
		hid_ishtp_set_feature(hid, buf, len, rep->id);
		kfree(buf);
		break;
	}
}

/**
 * ishtp_wait_for_response() - hid-core .wait() callback
 * @hid:	hid device instance
 *
 * This function is used to wait after get feaure/input report.
 *
 * Return: 0 on success and non zero on error
 */
static int ishtp_wait_for_response(struct hid_device *hid)
{
	struct ishtp_hid_data *hid_data =  hid->driver_data;
	struct ishtp_cl_data *client_data = hid_data->client_data;
	int rv;

	hid_ishtp_trace(client_data,  "%s hid %p\n", __func__, hid);

	rv = ishtp_hid_link_ready_wait(hid_data->client_data);
	if (rv)
		return rv;

	if (!hid_data->request_done)
		wait_event_interruptible_timeout(hid_data->hid_wait,
					hid_data->request_done, 3 * HZ);

	if (!hid_data->request_done) {
		hid_err(hid,
			"timeout waiting for response from ISHTP device\n");
		return -ETIMEDOUT;
	}
	hid_ishtp_trace(client_data,  "%s hid %p done\n", __func__, hid);

	hid_data->request_done = false;

	return 0;
}

/**
 * ishtp_hid_wakeup() - Wakeup caller
 * @hid:	hid device instance
 *
 * This function will wakeup caller waiting for Get/Set feature report
 */
void ishtp_hid_wakeup(struct hid_device *hid)
{
	struct ishtp_hid_data *hid_data = hid->driver_data;

	hid_data->request_done = true;
	wake_up_interruptible(&hid_data->hid_wait);
}

static struct hid_ll_driver ishtp_hid_ll_driver = {
	.parse = ishtp_hid_parse,
	.start = ishtp_hid_start,
	.stop = ishtp_hid_stop,
	.open = ishtp_hid_open,
	.close = ishtp_hid_close,
	.request = ishtp_hid_request,
	.wait = ishtp_wait_for_response,
	.raw_request = ishtp_raw_request
};

/**
 * ishtp_hid_probe() - hid register ll driver
 * @cur_hid_dev:	Index of hid device calling to register
 * @client_data:	Client data pointer
 *
 * This function is used to allocate and add HID device.
 *
 * Return: 0 on success, non zero on error
 */
int ishtp_hid_probe(unsigned int cur_hid_dev,
		    struct ishtp_cl_data *client_data)
{
	int rv;
	struct hid_device *hid;
	struct ishtp_hid_data *hid_data;

	hid = hid_allocate_device();
	if (IS_ERR(hid)) {
		rv = PTR_ERR(hid);
		return	-ENOMEM;
	}

	hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL);
	if (!hid_data) {
		rv = -ENOMEM;
		goto err_hid_data;
	}

	hid_data->index = cur_hid_dev;
	hid_data->client_data = client_data;
	init_waitqueue_head(&hid_data->hid_wait);

	hid->driver_data = hid_data;

	client_data->hid_sensor_hubs[cur_hid_dev] = hid;

	hid->ll_driver = &ishtp_hid_ll_driver;
	hid->bus = BUS_INTEL_ISHTP;
	hid->dev.parent = &client_data->cl_device->dev;
	hid->version = le16_to_cpu(ISH_HID_VERSION);
	hid->vendor = le16_to_cpu(ISH_HID_VENDOR);
	hid->product = le16_to_cpu(ISH_HID_PRODUCT);
	snprintf(hid->name, sizeof(hid->name), "%s %04hX:%04hX", "hid-ishtp",
		hid->vendor, hid->product);

	rv = hid_add_device(hid);
	if (rv)
		goto err_hid_device;

	hid_ishtp_trace(client_data,  "%s allocated hid %p\n", __func__, hid);

	return 0;

err_hid_device:
	kfree(hid_data);
err_hid_data:
	kfree(hid);
	return rv;
}

/**
 * ishtp_hid_probe() - Remove registered hid device
 * @client_data:	client data pointer
 *
 * This function is used to destroy allocatd HID device.
 */
void ishtp_hid_remove(struct ishtp_cl_data *client_data)
{
	int i;

	for (i = 0; i < client_data->num_hid_devices; ++i) {
		if (client_data->hid_sensor_hubs[i]) {
			kfree(client_data->hid_sensor_hubs[i]->driver_data);
			hid_destroy_device(client_data->hid_sensor_hubs[i]);
			client_data->hid_sensor_hubs[i] = NULL;
		}
	}
}
+182 −0
Original line number Diff line number Diff line
/*
 * ISHTP-HID glue driver's definitions.
 *
 * Copyright (c) 2014-2016, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 */
#ifndef ISHTP_HID__H
#define	ISHTP_HID__H

/* The fixed ISH product and vendor id */
#define	ISH_HID_VENDOR	0x8086
#define	ISH_HID_PRODUCT	0x22D8
#define	ISH_HID_VERSION	0x0200

#define	CMD_MASK	0x7F
#define	IS_RESPONSE	0x80

/* Used to dump to Linux trace buffer, if enabled */
#define hid_ishtp_trace(client, ...)	\
	client->cl_device->ishtp_dev->print_log(\
		client->cl_device->ishtp_dev, __VA_ARGS__)

/* ISH Transport protocol (ISHTP in short) GUID */
static const uuid_le hid_ishtp_guid = UUID_LE(0x33AECD58, 0xB679, 0x4E54,
					      0x9B, 0xD9, 0xA0, 0x4D, 0x34,
					      0xF0, 0xC2, 0x26);

/* ISH HID message structure */
struct hostif_msg_hdr {
	uint8_t	command; /* Bit 7: is_response */
	uint8_t	device_id;
	uint8_t	status;
	uint8_t	flags;
	uint16_t size;
} __packed;

struct hostif_msg {
	struct hostif_msg_hdr	hdr;
} __packed;

struct hostif_msg_to_sensor {
	struct hostif_msg_hdr	hdr;
	uint8_t	report_id;
} __packed;

struct device_info {
	uint32_t dev_id;
	uint8_t dev_class;
	uint16_t pid;
	uint16_t vid;
} __packed;

struct ishtp_version {
	uint8_t	major;
	uint8_t	minor;
	uint8_t	hotfix;
	uint16_t build;
} __packed;

/* struct for ISHTP aggregated input data */
struct report_list {
	uint16_t total_size;
	uint8_t	num_of_reports;
	uint8_t	flags;
	struct {
		uint16_t	size_of_report;
		uint8_t report[1];
	} __packed reports[1];
} __packed;

/* HOSTIF commands */
#define	HOSTIF_HID_COMMAND_BASE			0
#define	HOSTIF_GET_HID_DESCRIPTOR		0
#define	HOSTIF_GET_REPORT_DESCRIPTOR		1
#define HOSTIF_GET_FEATURE_REPORT		2
#define	HOSTIF_SET_FEATURE_REPORT		3
#define	HOSTIF_GET_INPUT_REPORT			4
#define	HOSTIF_PUBLISH_INPUT_REPORT		5
#define	HOSTIF_PUBLISH_INPUT_REPORT_LIST	6
#define	HOSTIF_DM_COMMAND_BASE			32
#define	HOSTIF_DM_ENUM_DEVICES			33
#define	HOSTIF_DM_ADD_DEVICE			34

#define	MAX_HID_DEVICES				32

/**
 * struct ishtp_cl_data - Encapsulate per ISH TP HID Client
 * @enum_device_done:	Enum devices response complete flag
 * @hid_descr_done:	HID descriptor complete flag
 * @report_descr_done:	Get report descriptor complete flag
 * @init_done:		Init process completed successfully
 * @suspended:		System is under suspend state or in progress
 * @num_hid_devices:	Number of HID devices enumerated in this client
 * @cur_hid_dev:	This keeps track of the device index for which
 *			initialization and registration with HID core
 *			in progress.
 * @hid_devices:	Store vid/pid/devid for each enumerated HID device
 * @report_descr:	Stores the raw report descriptors for each HID device
 * @report_descr_size:	Report description of size of above repo_descr[]
 * @hid_sensor_hubs:	Pointer to hid_device for all HID device, so that
 *			when clients are removed, they can be freed
 * @hid_descr:		Pointer to hid descriptor for each enumerated hid
 *			device
 * @hid_descr_size:	Size of each above report descriptor
 * @init_wait:		Wait queue to wait during initialization, where the
 *			client send message to ISH FW and wait for response
 * @ishtp_hid_wait:	The wait for get report during wait callback from hid
 *			core
 * @bad_recv_cnt:	Running count of packets received with error
 * @multi_packet_cnt:	Count of fragmented packet count
 *
 * This structure is used to store completion flags and per client data like
 * like report description, number of HID devices etc.
 */
struct ishtp_cl_data {
	/* completion flags */
	bool enum_devices_done;
	bool hid_descr_done;
	bool report_descr_done;
	bool init_done;
	bool suspended;

	unsigned int num_hid_devices;
	unsigned int cur_hid_dev;
	unsigned int hid_dev_count;

	struct device_info *hid_devices;
	unsigned char *report_descr[MAX_HID_DEVICES];
	int report_descr_size[MAX_HID_DEVICES];
	struct hid_device *hid_sensor_hubs[MAX_HID_DEVICES];
	unsigned char *hid_descr[MAX_HID_DEVICES];
	int hid_descr_size[MAX_HID_DEVICES];

	wait_queue_head_t init_wait;
	wait_queue_head_t ishtp_resume_wait;
	struct ishtp_cl *hid_ishtp_cl;

	/* Statistics */
	unsigned int bad_recv_cnt;
	int multi_packet_cnt;

	struct work_struct work;
	struct ishtp_cl_device *cl_device;
};

/**
 * struct ishtp_hid_data - Per instance HID data
 * @index:		Device index in the order of enumeration
 * @request_done:	Get Feature/Input report complete flag
 *			used during get/set request from hid core
 * @client_data:	Link to the client instance
 * @hid_wait:		Completion waitq
 *
 * Used to tie hid hid->driver data to driver client instance
 */
struct ishtp_hid_data {
	int index;
	bool request_done;
	struct ishtp_cl_data *client_data;
	wait_queue_head_t hid_wait;
};

/* Interface functions between HID LL driver and ISH TP client */
void hid_ishtp_set_feature(struct hid_device *hid, char *buf, unsigned int len,
			   int report_id);
void hid_ishtp_get_report(struct hid_device *hid, int report_id,
			  int report_type);
int ishtp_hid_probe(unsigned int cur_hid_dev,
		    struct ishtp_cl_data *client_data);
void ishtp_hid_remove(struct ishtp_cl_data *client_data);
int ishtp_hid_link_ready_wait(struct ishtp_cl_data *client_data);
void ishtp_hid_wakeup(struct hid_device *hid);

#endif	/* ISHTP_HID__H */
+1 −0
Original line number Diff line number Diff line
@@ -248,6 +248,7 @@ struct input_mask {
#define BUS_SPI			0x1C
#define BUS_RMI			0x1D
#define BUS_CEC			0x1E
#define BUS_INTEL_ISHTP		0x1F

/*
 * MT_TOOL types