1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * ISHTP-HID glue driver's definitions.
4 *
5 * Copyright (c) 2014-2016, Intel Corporation.
6 */
7#ifndef ISHTP_HID__H
8#define	ISHTP_HID__H
9
10/* The fixed ISH product and vendor id */
11#define	ISH_HID_VENDOR	0x8086
12#define	ISH_HID_PRODUCT	0x22D8
13#define	ISH_HID_VERSION	0x0200
14
15#define	CMD_MASK	0x7F
16#define	IS_RESPONSE	0x80
17
18/* Used to dump to Linux trace buffer, if enabled */
19extern void (*hid_print_trace)(void *unused, const char *format, ...);
20#define hid_ishtp_trace(client, ...) \
21		(hid_print_trace)(NULL, __VA_ARGS__)
22
23/* ISH Transport protocol (ISHTP in short) GUID */
24static const guid_t hid_ishtp_guid =
25	GUID_INIT(0x33AECD58, 0xB679, 0x4E54,
26		  0x9B, 0xD9, 0xA0, 0x4D, 0x34, 0xF0, 0xC2, 0x26);
27
28/* ISH HID message structure */
29struct hostif_msg_hdr {
30	uint8_t	command; /* Bit 7: is_response */
31	uint8_t	device_id;
32	uint8_t	status;
33	uint8_t	flags;
34	uint16_t size;
35} __packed;
36
37struct hostif_msg {
38	struct hostif_msg_hdr	hdr;
39} __packed;
40
41struct hostif_msg_to_sensor {
42	struct hostif_msg_hdr	hdr;
43	uint8_t	report_id;
44} __packed;
45
46struct device_info {
47	uint32_t dev_id;
48	uint8_t dev_class;
49	uint16_t pid;
50	uint16_t vid;
51} __packed;
52
53struct ishtp_version {
54	uint8_t	major;
55	uint8_t	minor;
56	uint8_t	hotfix;
57	uint16_t build;
58} __packed;
59
60/* struct for ISHTP aggregated input data */
61struct report_list {
62	uint16_t total_size;
63	uint8_t	num_of_reports;
64	uint8_t	flags;
65	struct {
66		uint16_t	size_of_report;
67		uint8_t report[1];
68	} __packed reports[1];
69} __packed;
70
71/* HOSTIF commands */
72#define	HOSTIF_HID_COMMAND_BASE			0
73#define	HOSTIF_GET_HID_DESCRIPTOR		0
74#define	HOSTIF_GET_REPORT_DESCRIPTOR		1
75#define HOSTIF_GET_FEATURE_REPORT		2
76#define	HOSTIF_SET_FEATURE_REPORT		3
77#define	HOSTIF_GET_INPUT_REPORT			4
78#define	HOSTIF_PUBLISH_INPUT_REPORT		5
79#define	HOSTIF_PUBLISH_INPUT_REPORT_LIST	6
80#define	HOSTIF_DM_COMMAND_BASE			32
81#define	HOSTIF_DM_ENUM_DEVICES			33
82#define	HOSTIF_DM_ADD_DEVICE			34
83
84#define	MAX_HID_DEVICES				32
85
86/**
87 * struct ishtp_cl_data - Encapsulate per ISH TP HID Client
88 * @enum_device_done:	Enum devices response complete flag
89 * @hid_descr_done:	HID descriptor complete flag
90 * @report_descr_done:	Get report descriptor complete flag
91 * @init_done:		Init process completed successfully
92 * @suspended:		System is under suspend state or in progress
93 * @num_hid_devices:	Number of HID devices enumerated in this client
94 * @cur_hid_dev:	This keeps track of the device index for which
95 *			initialization and registration with HID core
96 *			in progress.
97 * @hid_devices:	Store vid/pid/devid for each enumerated HID device
98 * @report_descr:	Stores the raw report descriptors for each HID device
99 * @report_descr_size:	Report description of size of above repo_descr[]
100 * @hid_sensor_hubs:	Pointer to hid_device for all HID device, so that
101 *			when clients are removed, they can be freed
102 * @hid_descr:		Pointer to hid descriptor for each enumerated hid
103 *			device
104 * @hid_descr_size:	Size of each above report descriptor
105 * @init_wait:		Wait queue to wait during initialization, where the
106 *			client send message to ISH FW and wait for response
107 * @ishtp_hid_wait:	The wait for get report during wait callback from hid
108 *			core
109 * @bad_recv_cnt:	Running count of packets received with error
110 * @multi_packet_cnt:	Count of fragmented packet count
111 *
112 * This structure is used to store completion flags and per client data like
113 * report description, number of HID devices etc.
114 */
115struct ishtp_cl_data {
116	/* completion flags */
117	bool enum_devices_done;
118	bool hid_descr_done;
119	bool report_descr_done;
120	bool init_done;
121	bool suspended;
122
123	unsigned int num_hid_devices;
124	unsigned int cur_hid_dev;
125	unsigned int hid_dev_count;
126
127	struct device_info *hid_devices;
128	unsigned char *report_descr[MAX_HID_DEVICES];
129	int report_descr_size[MAX_HID_DEVICES];
130	struct hid_device *hid_sensor_hubs[MAX_HID_DEVICES];
131	unsigned char *hid_descr[MAX_HID_DEVICES];
132	int hid_descr_size[MAX_HID_DEVICES];
133
134	wait_queue_head_t init_wait;
135	wait_queue_head_t ishtp_resume_wait;
136	struct ishtp_cl *hid_ishtp_cl;
137
138	/* Statistics */
139	unsigned int bad_recv_cnt;
140	int multi_packet_cnt;
141
142	struct work_struct work;
143	struct ishtp_cl_device *cl_device;
144};
145
146/**
147 * struct ishtp_hid_data - Per instance HID data
148 * @index:		Device index in the order of enumeration
149 * @request_done:	Get Feature/Input report complete flag
150 *			used during get/set request from hid core
151 * @client_data:	Link to the client instance
152 * @hid_wait:		Completion waitq
153 *
154 * @raw_get_req:	Flag indicating raw get request ongoing
155 * @raw_buf:		raw request buffer filled on receiving get report
156 * @raw_buf_size:	raw request buffer size
157 * Used to tie hid hid->driver data to driver client instance
158 */
159struct ishtp_hid_data {
160	int index;
161	bool request_done;
162	struct ishtp_cl_data *client_data;
163	wait_queue_head_t hid_wait;
164
165	/* raw request */
166	bool raw_get_req;
167	u8 *raw_buf;
168	size_t raw_buf_size;
169};
170
171/* Interface functions between HID LL driver and ISH TP client */
172void hid_ishtp_set_feature(struct hid_device *hid, char *buf, unsigned int len,
173			   int report_id);
174void hid_ishtp_get_report(struct hid_device *hid, int report_id,
175			  int report_type);
176int ishtp_hid_probe(unsigned int cur_hid_dev,
177		    struct ishtp_cl_data *client_data);
178void ishtp_hid_remove(struct ishtp_cl_data *client_data);
179int ishtp_hid_link_ready_wait(struct ishtp_cl_data *client_data);
180void ishtp_hid_wakeup(struct hid_device *hid);
181
182#endif	/* ISHTP_HID__H */
183