18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// ISHTP interface for ChromeOS Embedded Controller
38c2ecf20Sopenharmony_ci//
48c2ecf20Sopenharmony_ci// Copyright (c) 2019, Intel Corporation.
58c2ecf20Sopenharmony_ci//
68c2ecf20Sopenharmony_ci// ISHTP client driver for talking to the Chrome OS EC firmware running
78c2ecf20Sopenharmony_ci// on Intel Integrated Sensor Hub (ISH) using the ISH Transport protocol
88c2ecf20Sopenharmony_ci// (ISH-TP).
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/pci.h>
138c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_commands.h>
148c2ecf20Sopenharmony_ci#include <linux/platform_data/cros_ec_proto.h>
158c2ecf20Sopenharmony_ci#include <linux/intel-ish-client-if.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "cros_ec.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * ISH TX/RX ring buffer pool size
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * The AP->ISH messages and corresponding ISH->AP responses are
238c2ecf20Sopenharmony_ci * serialized. We need 1 TX and 1 RX buffer for these.
248c2ecf20Sopenharmony_ci *
258c2ecf20Sopenharmony_ci * The MKBP ISH->AP events are serialized. We need one additional RX
268c2ecf20Sopenharmony_ci * buffer for them.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#define CROS_ISH_CL_TX_RING_SIZE		8
298c2ecf20Sopenharmony_ci#define CROS_ISH_CL_RX_RING_SIZE		8
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* ISH CrOS EC Host Commands */
328c2ecf20Sopenharmony_cienum cros_ec_ish_channel {
338c2ecf20Sopenharmony_ci	CROS_EC_COMMAND = 1,			/* AP->ISH message */
348c2ecf20Sopenharmony_ci	CROS_MKBP_EVENT = 2,			/* ISH->AP events */
358c2ecf20Sopenharmony_ci};
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci/*
388c2ecf20Sopenharmony_ci * ISH firmware timeout for 1 message send failure is 1Hz, and the
398c2ecf20Sopenharmony_ci * firmware will retry 2 times, so 3Hz is used for timeout.
408c2ecf20Sopenharmony_ci */
418c2ecf20Sopenharmony_ci#define ISHTP_SEND_TIMEOUT			(3 * HZ)
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci/* ISH Transport CrOS EC ISH client unique GUID */
448c2ecf20Sopenharmony_cistatic const guid_t cros_ish_guid =
458c2ecf20Sopenharmony_ci	GUID_INIT(0x7b7154d0, 0x56f4, 0x4bdc,
468c2ecf20Sopenharmony_ci		  0xb0, 0xd8, 0x9e, 0x7c, 0xda,	0xe0, 0xd6, 0xa0);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct header {
498c2ecf20Sopenharmony_ci	u8 channel;
508c2ecf20Sopenharmony_ci	u8 status;
518c2ecf20Sopenharmony_ci	u8 token;
528c2ecf20Sopenharmony_ci	u8 reserved;
538c2ecf20Sopenharmony_ci} __packed;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistruct cros_ish_out_msg {
568c2ecf20Sopenharmony_ci	struct header hdr;
578c2ecf20Sopenharmony_ci	struct ec_host_request ec_request;
588c2ecf20Sopenharmony_ci} __packed;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistruct cros_ish_in_msg {
618c2ecf20Sopenharmony_ci	struct header hdr;
628c2ecf20Sopenharmony_ci	struct ec_host_response ec_response;
638c2ecf20Sopenharmony_ci} __packed;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define IN_MSG_EC_RESPONSE_PREAMBLE					\
668c2ecf20Sopenharmony_ci	offsetof(struct cros_ish_in_msg, ec_response)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define OUT_MSG_EC_REQUEST_PREAMBLE					\
698c2ecf20Sopenharmony_ci	offsetof(struct cros_ish_out_msg, ec_request)
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci/*
748c2ecf20Sopenharmony_ci * The Read-Write Semaphore is used to prevent message TX or RX while
758c2ecf20Sopenharmony_ci * the ishtp client is being initialized or undergoing reset.
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * The readers are the kernel function calls responsible for IA->ISH
788c2ecf20Sopenharmony_ci * and ISH->AP messaging.
798c2ecf20Sopenharmony_ci *
808c2ecf20Sopenharmony_ci * The writers are .reset() and .probe() function.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_cistatic DECLARE_RWSEM(init_lock);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/**
858c2ecf20Sopenharmony_ci * struct response_info - Encapsulate firmware response related
868c2ecf20Sopenharmony_ci * information for passing between function ish_send() and
878c2ecf20Sopenharmony_ci * process_recv() callback.
888c2ecf20Sopenharmony_ci *
898c2ecf20Sopenharmony_ci * @data: Copy the data received from firmware here.
908c2ecf20Sopenharmony_ci * @max_size: Max size allocated for the @data buffer. If the received
918c2ecf20Sopenharmony_ci * data exceeds this value, we log an error.
928c2ecf20Sopenharmony_ci * @size: Actual size of data received from firmware.
938c2ecf20Sopenharmony_ci * @error: 0 for success, negative error code for a failure in process_recv().
948c2ecf20Sopenharmony_ci * @token: Expected token for response that we are waiting on.
958c2ecf20Sopenharmony_ci * @received: Set to true on receiving a valid firmware	response to host command
968c2ecf20Sopenharmony_ci * @wait_queue: Wait queue for host to wait for firmware response.
978c2ecf20Sopenharmony_ci */
988c2ecf20Sopenharmony_cistruct response_info {
998c2ecf20Sopenharmony_ci	void *data;
1008c2ecf20Sopenharmony_ci	size_t max_size;
1018c2ecf20Sopenharmony_ci	size_t size;
1028c2ecf20Sopenharmony_ci	int error;
1038c2ecf20Sopenharmony_ci	u8 token;
1048c2ecf20Sopenharmony_ci	bool received;
1058c2ecf20Sopenharmony_ci	wait_queue_head_t wait_queue;
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/**
1098c2ecf20Sopenharmony_ci * struct ishtp_cl_data - Encapsulate per ISH TP Client.
1108c2ecf20Sopenharmony_ci *
1118c2ecf20Sopenharmony_ci * @cros_ish_cl: ISHTP firmware client instance.
1128c2ecf20Sopenharmony_ci * @cl_device: ISHTP client device instance.
1138c2ecf20Sopenharmony_ci * @response: Response info passing between ish_send() and process_recv().
1148c2ecf20Sopenharmony_ci * @work_ishtp_reset: Work queue reset handling.
1158c2ecf20Sopenharmony_ci * @work_ec_evt: Work queue for EC events.
1168c2ecf20Sopenharmony_ci * @ec_dev: CrOS EC MFD device.
1178c2ecf20Sopenharmony_ci *
1188c2ecf20Sopenharmony_ci * This structure is used to store per client data.
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_cistruct ishtp_cl_data {
1218c2ecf20Sopenharmony_ci	struct ishtp_cl *cros_ish_cl;
1228c2ecf20Sopenharmony_ci	struct ishtp_cl_device *cl_device;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	/*
1258c2ecf20Sopenharmony_ci	 * Used for passing firmware response information between
1268c2ecf20Sopenharmony_ci	 * ish_send() and process_recv() callback.
1278c2ecf20Sopenharmony_ci	 */
1288c2ecf20Sopenharmony_ci	struct response_info response;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	struct work_struct work_ishtp_reset;
1318c2ecf20Sopenharmony_ci	struct work_struct work_ec_evt;
1328c2ecf20Sopenharmony_ci	struct cros_ec_device *ec_dev;
1338c2ecf20Sopenharmony_ci};
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci/**
1368c2ecf20Sopenharmony_ci * ish_evt_handler - ISH to AP event handler
1378c2ecf20Sopenharmony_ci * @work: Work struct
1388c2ecf20Sopenharmony_ci */
1398c2ecf20Sopenharmony_cistatic void ish_evt_handler(struct work_struct *work)
1408c2ecf20Sopenharmony_ci{
1418c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data =
1428c2ecf20Sopenharmony_ci		container_of(work, struct ishtp_cl_data, work_ec_evt);
1438c2ecf20Sopenharmony_ci	struct cros_ec_device *ec_dev = client_data->ec_dev;
1448c2ecf20Sopenharmony_ci	bool ec_has_more_events;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	do {
1478c2ecf20Sopenharmony_ci		ec_has_more_events = cros_ec_handle_event(ec_dev);
1488c2ecf20Sopenharmony_ci	} while (ec_has_more_events);
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/**
1528c2ecf20Sopenharmony_ci * ish_send() - Send message from host to firmware
1538c2ecf20Sopenharmony_ci *
1548c2ecf20Sopenharmony_ci * @client_data: Client data instance
1558c2ecf20Sopenharmony_ci * @out_msg: Message buffer to be sent to firmware
1568c2ecf20Sopenharmony_ci * @out_size: Size of out going message
1578c2ecf20Sopenharmony_ci * @in_msg: Message buffer where the incoming data is copied. This buffer
1588c2ecf20Sopenharmony_ci * is allocated by calling
1598c2ecf20Sopenharmony_ci * @in_size: Max size of incoming message
1608c2ecf20Sopenharmony_ci *
1618c2ecf20Sopenharmony_ci * Return: Number of bytes copied in the in_msg on success, negative
1628c2ecf20Sopenharmony_ci * error code on failure.
1638c2ecf20Sopenharmony_ci */
1648c2ecf20Sopenharmony_cistatic int ish_send(struct ishtp_cl_data *client_data,
1658c2ecf20Sopenharmony_ci		    u8 *out_msg, size_t out_size,
1668c2ecf20Sopenharmony_ci		    u8 *in_msg, size_t in_size)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	static u8 next_token;
1698c2ecf20Sopenharmony_ci	int rv;
1708c2ecf20Sopenharmony_ci	struct header *out_hdr = (struct header *)out_msg;
1718c2ecf20Sopenharmony_ci	struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	dev_dbg(cl_data_to_dev(client_data),
1748c2ecf20Sopenharmony_ci		"%s: channel=%02u status=%02u\n",
1758c2ecf20Sopenharmony_ci		__func__, out_hdr->channel, out_hdr->status);
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	/* Setup for incoming response */
1788c2ecf20Sopenharmony_ci	client_data->response.data = in_msg;
1798c2ecf20Sopenharmony_ci	client_data->response.max_size = in_size;
1808c2ecf20Sopenharmony_ci	client_data->response.error = 0;
1818c2ecf20Sopenharmony_ci	client_data->response.token = next_token++;
1828c2ecf20Sopenharmony_ci	client_data->response.received = false;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	out_hdr->token = client_data->response.token;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	rv = ishtp_cl_send(cros_ish_cl, out_msg, out_size);
1878c2ecf20Sopenharmony_ci	if (rv) {
1888c2ecf20Sopenharmony_ci		dev_err(cl_data_to_dev(client_data),
1898c2ecf20Sopenharmony_ci			"ishtp_cl_send error %d\n", rv);
1908c2ecf20Sopenharmony_ci		return rv;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	wait_event_interruptible_timeout(client_data->response.wait_queue,
1948c2ecf20Sopenharmony_ci					 client_data->response.received,
1958c2ecf20Sopenharmony_ci					 ISHTP_SEND_TIMEOUT);
1968c2ecf20Sopenharmony_ci	if (!client_data->response.received) {
1978c2ecf20Sopenharmony_ci		dev_err(cl_data_to_dev(client_data),
1988c2ecf20Sopenharmony_ci			"Timed out for response to host message\n");
1998c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
2008c2ecf20Sopenharmony_ci	}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (client_data->response.error < 0)
2038c2ecf20Sopenharmony_ci		return client_data->response.error;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	return client_data->response.size;
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci/**
2098c2ecf20Sopenharmony_ci * process_recv() - Received and parse incoming packet
2108c2ecf20Sopenharmony_ci * @cros_ish_cl: Client instance to get stats
2118c2ecf20Sopenharmony_ci * @rb_in_proc: Host interface message buffer
2128c2ecf20Sopenharmony_ci * @timestamp: Timestamp of when parent callback started
2138c2ecf20Sopenharmony_ci *
2148c2ecf20Sopenharmony_ci * Parse the incoming packet. If it is a response packet then it will
2158c2ecf20Sopenharmony_ci * update per instance flags and wake up the caller waiting to for the
2168c2ecf20Sopenharmony_ci * response. If it is an event packet then it will schedule event work.
2178c2ecf20Sopenharmony_ci */
2188c2ecf20Sopenharmony_cistatic void process_recv(struct ishtp_cl *cros_ish_cl,
2198c2ecf20Sopenharmony_ci			 struct ishtp_cl_rb *rb_in_proc, ktime_t timestamp)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	size_t data_len = rb_in_proc->buf_idx;
2228c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data =
2238c2ecf20Sopenharmony_ci		ishtp_get_client_data(cros_ish_cl);
2248c2ecf20Sopenharmony_ci	struct device *dev = cl_data_to_dev(client_data);
2258c2ecf20Sopenharmony_ci	struct cros_ish_in_msg *in_msg =
2268c2ecf20Sopenharmony_ci		(struct cros_ish_in_msg *)rb_in_proc->buffer.data;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	/* Proceed only if reset or init is not in progress */
2298c2ecf20Sopenharmony_ci	if (!down_read_trylock(&init_lock)) {
2308c2ecf20Sopenharmony_ci		/* Free the buffer */
2318c2ecf20Sopenharmony_ci		ishtp_cl_io_rb_recycle(rb_in_proc);
2328c2ecf20Sopenharmony_ci		dev_warn(dev,
2338c2ecf20Sopenharmony_ci			 "Host is not ready to receive incoming messages\n");
2348c2ecf20Sopenharmony_ci		return;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	/*
2388c2ecf20Sopenharmony_ci	 * All firmware messages contain a header. Check the buffer size
2398c2ecf20Sopenharmony_ci	 * before accessing elements inside.
2408c2ecf20Sopenharmony_ci	 */
2418c2ecf20Sopenharmony_ci	if (!rb_in_proc->buffer.data) {
2428c2ecf20Sopenharmony_ci		dev_warn(dev, "rb_in_proc->buffer.data returned null");
2438c2ecf20Sopenharmony_ci		client_data->response.error = -EBADMSG;
2448c2ecf20Sopenharmony_ci		goto end_error;
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	if (data_len < sizeof(struct header)) {
2488c2ecf20Sopenharmony_ci		dev_err(dev, "data size %zu is less than header %zu\n",
2498c2ecf20Sopenharmony_ci			data_len, sizeof(struct header));
2508c2ecf20Sopenharmony_ci		client_data->response.error = -EMSGSIZE;
2518c2ecf20Sopenharmony_ci		goto end_error;
2528c2ecf20Sopenharmony_ci	}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	dev_dbg(dev, "channel=%02u status=%02u\n",
2558c2ecf20Sopenharmony_ci		in_msg->hdr.channel, in_msg->hdr.status);
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	switch (in_msg->hdr.channel) {
2588c2ecf20Sopenharmony_ci	case CROS_EC_COMMAND:
2598c2ecf20Sopenharmony_ci		if (client_data->response.received) {
2608c2ecf20Sopenharmony_ci			dev_err(dev,
2618c2ecf20Sopenharmony_ci				"Previous firmware message not yet processed\n");
2628c2ecf20Sopenharmony_ci			goto end_error;
2638c2ecf20Sopenharmony_ci		}
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci		if (client_data->response.token != in_msg->hdr.token) {
2668c2ecf20Sopenharmony_ci			dev_err_ratelimited(dev,
2678c2ecf20Sopenharmony_ci					    "Dropping old response token %d\n",
2688c2ecf20Sopenharmony_ci					    in_msg->hdr.token);
2698c2ecf20Sopenharmony_ci			goto end_error;
2708c2ecf20Sopenharmony_ci		}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci		/* Sanity check */
2738c2ecf20Sopenharmony_ci		if (!client_data->response.data) {
2748c2ecf20Sopenharmony_ci			dev_err(dev,
2758c2ecf20Sopenharmony_ci				"Receiving buffer is null. Should be allocated by calling function\n");
2768c2ecf20Sopenharmony_ci			client_data->response.error = -EINVAL;
2778c2ecf20Sopenharmony_ci			goto error_wake_up;
2788c2ecf20Sopenharmony_ci		}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci		if (data_len > client_data->response.max_size) {
2818c2ecf20Sopenharmony_ci			dev_err(dev,
2828c2ecf20Sopenharmony_ci				"Received buffer size %zu is larger than allocated buffer %zu\n",
2838c2ecf20Sopenharmony_ci				data_len, client_data->response.max_size);
2848c2ecf20Sopenharmony_ci			client_data->response.error = -EMSGSIZE;
2858c2ecf20Sopenharmony_ci			goto error_wake_up;
2868c2ecf20Sopenharmony_ci		}
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		if (in_msg->hdr.status) {
2898c2ecf20Sopenharmony_ci			dev_err(dev, "firmware returned status %d\n",
2908c2ecf20Sopenharmony_ci				in_msg->hdr.status);
2918c2ecf20Sopenharmony_ci			client_data->response.error = -EIO;
2928c2ecf20Sopenharmony_ci			goto error_wake_up;
2938c2ecf20Sopenharmony_ci		}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci		/* Update the actual received buffer size */
2968c2ecf20Sopenharmony_ci		client_data->response.size = data_len;
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci		/*
2998c2ecf20Sopenharmony_ci		 * Copy the buffer received in firmware response for the
3008c2ecf20Sopenharmony_ci		 * calling thread.
3018c2ecf20Sopenharmony_ci		 */
3028c2ecf20Sopenharmony_ci		memcpy(client_data->response.data,
3038c2ecf20Sopenharmony_ci		       rb_in_proc->buffer.data, data_len);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_cierror_wake_up:
3068c2ecf20Sopenharmony_ci		/* Free the buffer since we copied data or didn't need it */
3078c2ecf20Sopenharmony_ci		ishtp_cl_io_rb_recycle(rb_in_proc);
3088c2ecf20Sopenharmony_ci		rb_in_proc = NULL;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci		/* Set flag before waking up the caller */
3118c2ecf20Sopenharmony_ci		client_data->response.received = true;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci		/* Wake the calling thread */
3148c2ecf20Sopenharmony_ci		wake_up_interruptible(&client_data->response.wait_queue);
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci		break;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	case CROS_MKBP_EVENT:
3198c2ecf20Sopenharmony_ci		/* Free the buffer. This is just an event without data */
3208c2ecf20Sopenharmony_ci		ishtp_cl_io_rb_recycle(rb_in_proc);
3218c2ecf20Sopenharmony_ci		rb_in_proc = NULL;
3228c2ecf20Sopenharmony_ci		/*
3238c2ecf20Sopenharmony_ci		 * Set timestamp from beginning of function since we actually
3248c2ecf20Sopenharmony_ci		 * got an incoming MKBP event
3258c2ecf20Sopenharmony_ci		 */
3268c2ecf20Sopenharmony_ci		client_data->ec_dev->last_event_time = timestamp;
3278c2ecf20Sopenharmony_ci		schedule_work(&client_data->work_ec_evt);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci		break;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	default:
3328c2ecf20Sopenharmony_ci		dev_err(dev, "Invalid channel=%02d\n", in_msg->hdr.channel);
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ciend_error:
3368c2ecf20Sopenharmony_ci	/* Free the buffer if we already haven't */
3378c2ecf20Sopenharmony_ci	if (rb_in_proc)
3388c2ecf20Sopenharmony_ci		ishtp_cl_io_rb_recycle(rb_in_proc);
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	up_read(&init_lock);
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci/**
3448c2ecf20Sopenharmony_ci * ish_event_cb() - bus driver callback for incoming message
3458c2ecf20Sopenharmony_ci * @cl_device: ISHTP client device for which this message is targeted.
3468c2ecf20Sopenharmony_ci *
3478c2ecf20Sopenharmony_ci * Remove the packet from the list and process the message by calling
3488c2ecf20Sopenharmony_ci * process_recv.
3498c2ecf20Sopenharmony_ci */
3508c2ecf20Sopenharmony_cistatic void ish_event_cb(struct ishtp_cl_device *cl_device)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	struct ishtp_cl_rb *rb_in_proc;
3538c2ecf20Sopenharmony_ci	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
3548c2ecf20Sopenharmony_ci	ktime_t timestamp;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/*
3578c2ecf20Sopenharmony_ci	 * Take timestamp as close to hardware interrupt as possible for sensor
3588c2ecf20Sopenharmony_ci	 * timestamps.
3598c2ecf20Sopenharmony_ci	 */
3608c2ecf20Sopenharmony_ci	timestamp = cros_ec_get_time_ns();
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	while ((rb_in_proc = ishtp_cl_rx_get_rb(cros_ish_cl)) != NULL) {
3638c2ecf20Sopenharmony_ci		/* Decide what to do with received data */
3648c2ecf20Sopenharmony_ci		process_recv(cros_ish_cl, rb_in_proc, timestamp);
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci/**
3698c2ecf20Sopenharmony_ci * cros_ish_init() - Init function for ISHTP client
3708c2ecf20Sopenharmony_ci * @cros_ish_cl: ISHTP client instance
3718c2ecf20Sopenharmony_ci *
3728c2ecf20Sopenharmony_ci * This function complete the initializtion of the client.
3738c2ecf20Sopenharmony_ci *
3748c2ecf20Sopenharmony_ci * Return: 0 for success, negative error code for failure.
3758c2ecf20Sopenharmony_ci */
3768c2ecf20Sopenharmony_cistatic int cros_ish_init(struct ishtp_cl *cros_ish_cl)
3778c2ecf20Sopenharmony_ci{
3788c2ecf20Sopenharmony_ci	int rv;
3798c2ecf20Sopenharmony_ci	struct ishtp_device *dev;
3808c2ecf20Sopenharmony_ci	struct ishtp_fw_client *fw_client;
3818c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	rv = ishtp_cl_link(cros_ish_cl);
3848c2ecf20Sopenharmony_ci	if (rv) {
3858c2ecf20Sopenharmony_ci		dev_err(cl_data_to_dev(client_data),
3868c2ecf20Sopenharmony_ci			"ishtp_cl_link failed\n");
3878c2ecf20Sopenharmony_ci		return rv;
3888c2ecf20Sopenharmony_ci	}
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	dev = ishtp_get_ishtp_device(cros_ish_cl);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	/* Connect to firmware client */
3938c2ecf20Sopenharmony_ci	ishtp_set_tx_ring_size(cros_ish_cl, CROS_ISH_CL_TX_RING_SIZE);
3948c2ecf20Sopenharmony_ci	ishtp_set_rx_ring_size(cros_ish_cl, CROS_ISH_CL_RX_RING_SIZE);
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	fw_client = ishtp_fw_cl_get_client(dev, &cros_ish_guid);
3978c2ecf20Sopenharmony_ci	if (!fw_client) {
3988c2ecf20Sopenharmony_ci		dev_err(cl_data_to_dev(client_data),
3998c2ecf20Sopenharmony_ci			"ish client uuid not found\n");
4008c2ecf20Sopenharmony_ci		rv = -ENOENT;
4018c2ecf20Sopenharmony_ci		goto err_cl_unlink;
4028c2ecf20Sopenharmony_ci	}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	ishtp_cl_set_fw_client_id(cros_ish_cl,
4058c2ecf20Sopenharmony_ci				  ishtp_get_fw_client_id(fw_client));
4068c2ecf20Sopenharmony_ci	ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_CONNECTING);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	rv = ishtp_cl_connect(cros_ish_cl);
4098c2ecf20Sopenharmony_ci	if (rv) {
4108c2ecf20Sopenharmony_ci		dev_err(cl_data_to_dev(client_data),
4118c2ecf20Sopenharmony_ci			"client connect fail\n");
4128c2ecf20Sopenharmony_ci		goto err_cl_unlink;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	ishtp_register_event_cb(client_data->cl_device, ish_event_cb);
4168c2ecf20Sopenharmony_ci	return 0;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cierr_cl_unlink:
4198c2ecf20Sopenharmony_ci	ishtp_cl_unlink(cros_ish_cl);
4208c2ecf20Sopenharmony_ci	return rv;
4218c2ecf20Sopenharmony_ci}
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci/**
4248c2ecf20Sopenharmony_ci * cros_ish_deinit() - Deinit function for ISHTP client
4258c2ecf20Sopenharmony_ci * @cros_ish_cl: ISHTP client instance
4268c2ecf20Sopenharmony_ci *
4278c2ecf20Sopenharmony_ci * Unlink and free cros_ec client
4288c2ecf20Sopenharmony_ci */
4298c2ecf20Sopenharmony_cistatic void cros_ish_deinit(struct ishtp_cl *cros_ish_cl)
4308c2ecf20Sopenharmony_ci{
4318c2ecf20Sopenharmony_ci	ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
4328c2ecf20Sopenharmony_ci	ishtp_cl_disconnect(cros_ish_cl);
4338c2ecf20Sopenharmony_ci	ishtp_cl_unlink(cros_ish_cl);
4348c2ecf20Sopenharmony_ci	ishtp_cl_flush_queues(cros_ish_cl);
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci	/* Disband and free all Tx and Rx client-level rings */
4378c2ecf20Sopenharmony_ci	ishtp_cl_free(cros_ish_cl);
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/**
4418c2ecf20Sopenharmony_ci * prepare_cros_ec_rx() - Check & prepare receive buffer
4428c2ecf20Sopenharmony_ci * @ec_dev: CrOS EC MFD device.
4438c2ecf20Sopenharmony_ci * @in_msg: Incoming message buffer
4448c2ecf20Sopenharmony_ci * @msg: cros_ec command used to send & receive data
4458c2ecf20Sopenharmony_ci *
4468c2ecf20Sopenharmony_ci * Return: 0 for success, negative error code for failure.
4478c2ecf20Sopenharmony_ci *
4488c2ecf20Sopenharmony_ci * Check the received buffer. Convert to cros_ec_command format.
4498c2ecf20Sopenharmony_ci */
4508c2ecf20Sopenharmony_cistatic int prepare_cros_ec_rx(struct cros_ec_device *ec_dev,
4518c2ecf20Sopenharmony_ci			      const struct cros_ish_in_msg *in_msg,
4528c2ecf20Sopenharmony_ci			      struct cros_ec_command *msg)
4538c2ecf20Sopenharmony_ci{
4548c2ecf20Sopenharmony_ci	u8 sum = 0;
4558c2ecf20Sopenharmony_ci	int i, rv, offset;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	/* Check response error code */
4588c2ecf20Sopenharmony_ci	msg->result = in_msg->ec_response.result;
4598c2ecf20Sopenharmony_ci	rv = cros_ec_check_result(ec_dev, msg);
4608c2ecf20Sopenharmony_ci	if (rv < 0)
4618c2ecf20Sopenharmony_ci		return rv;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	if (in_msg->ec_response.data_len > msg->insize) {
4648c2ecf20Sopenharmony_ci		dev_err(ec_dev->dev, "Packet too long (%d bytes, expected %d)",
4658c2ecf20Sopenharmony_ci			in_msg->ec_response.data_len, msg->insize);
4668c2ecf20Sopenharmony_ci		return -ENOSPC;
4678c2ecf20Sopenharmony_ci	}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	/* Copy response packet payload and compute checksum */
4708c2ecf20Sopenharmony_ci	for (i = 0; i < sizeof(struct ec_host_response); i++)
4718c2ecf20Sopenharmony_ci		sum += ((u8 *)in_msg)[IN_MSG_EC_RESPONSE_PREAMBLE + i];
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	offset = sizeof(struct cros_ish_in_msg);
4748c2ecf20Sopenharmony_ci	for (i = 0; i < in_msg->ec_response.data_len; i++)
4758c2ecf20Sopenharmony_ci		sum += msg->data[i] = ((u8 *)in_msg)[offset + i];
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	if (sum) {
4788c2ecf20Sopenharmony_ci		dev_dbg(ec_dev->dev, "Bad received packet checksum %d\n", sum);
4798c2ecf20Sopenharmony_ci		return -EBADMSG;
4808c2ecf20Sopenharmony_ci	}
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	return 0;
4838c2ecf20Sopenharmony_ci}
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cistatic int cros_ec_pkt_xfer_ish(struct cros_ec_device *ec_dev,
4868c2ecf20Sopenharmony_ci				struct cros_ec_command *msg)
4878c2ecf20Sopenharmony_ci{
4888c2ecf20Sopenharmony_ci	int rv;
4898c2ecf20Sopenharmony_ci	struct ishtp_cl *cros_ish_cl = ec_dev->priv;
4908c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
4918c2ecf20Sopenharmony_ci	struct device *dev = cl_data_to_dev(client_data);
4928c2ecf20Sopenharmony_ci	struct cros_ish_in_msg *in_msg = (struct cros_ish_in_msg *)ec_dev->din;
4938c2ecf20Sopenharmony_ci	struct cros_ish_out_msg *out_msg =
4948c2ecf20Sopenharmony_ci		(struct cros_ish_out_msg *)ec_dev->dout;
4958c2ecf20Sopenharmony_ci	size_t in_size = sizeof(struct cros_ish_in_msg) + msg->insize;
4968c2ecf20Sopenharmony_ci	size_t out_size = sizeof(struct cros_ish_out_msg) + msg->outsize;
4978c2ecf20Sopenharmony_ci
4988c2ecf20Sopenharmony_ci	/* Sanity checks */
4998c2ecf20Sopenharmony_ci	if (in_size > ec_dev->din_size) {
5008c2ecf20Sopenharmony_ci		dev_err(dev,
5018c2ecf20Sopenharmony_ci			"Incoming payload size %zu is too large for ec_dev->din_size %d\n",
5028c2ecf20Sopenharmony_ci			in_size, ec_dev->din_size);
5038c2ecf20Sopenharmony_ci		return -EMSGSIZE;
5048c2ecf20Sopenharmony_ci	}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	if (out_size > ec_dev->dout_size) {
5078c2ecf20Sopenharmony_ci		dev_err(dev,
5088c2ecf20Sopenharmony_ci			"Outgoing payload size %zu is too large for ec_dev->dout_size %d\n",
5098c2ecf20Sopenharmony_ci			out_size, ec_dev->dout_size);
5108c2ecf20Sopenharmony_ci		return -EMSGSIZE;
5118c2ecf20Sopenharmony_ci	}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	/* Proceed only if reset-init is not in progress */
5148c2ecf20Sopenharmony_ci	if (!down_read_trylock(&init_lock)) {
5158c2ecf20Sopenharmony_ci		dev_warn(dev,
5168c2ecf20Sopenharmony_ci			 "Host is not ready to send messages to ISH. Try again\n");
5178c2ecf20Sopenharmony_ci		return -EAGAIN;
5188c2ecf20Sopenharmony_ci	}
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	/* Prepare the package to be sent over ISH TP */
5218c2ecf20Sopenharmony_ci	out_msg->hdr.channel = CROS_EC_COMMAND;
5228c2ecf20Sopenharmony_ci	out_msg->hdr.status = 0;
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_ci	ec_dev->dout += OUT_MSG_EC_REQUEST_PREAMBLE;
5258c2ecf20Sopenharmony_ci	cros_ec_prepare_tx(ec_dev, msg);
5268c2ecf20Sopenharmony_ci	ec_dev->dout -= OUT_MSG_EC_REQUEST_PREAMBLE;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	dev_dbg(dev,
5298c2ecf20Sopenharmony_ci		"out_msg: struct_ver=0x%x checksum=0x%x command=0x%x command_ver=0x%x data_len=0x%x\n",
5308c2ecf20Sopenharmony_ci		out_msg->ec_request.struct_version,
5318c2ecf20Sopenharmony_ci		out_msg->ec_request.checksum,
5328c2ecf20Sopenharmony_ci		out_msg->ec_request.command,
5338c2ecf20Sopenharmony_ci		out_msg->ec_request.command_version,
5348c2ecf20Sopenharmony_ci		out_msg->ec_request.data_len);
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_ci	/* Send command to ISH EC firmware and read response */
5378c2ecf20Sopenharmony_ci	rv = ish_send(client_data,
5388c2ecf20Sopenharmony_ci		      (u8 *)out_msg, out_size,
5398c2ecf20Sopenharmony_ci		      (u8 *)in_msg, in_size);
5408c2ecf20Sopenharmony_ci	if (rv < 0)
5418c2ecf20Sopenharmony_ci		goto end_error;
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	rv = prepare_cros_ec_rx(ec_dev, in_msg, msg);
5448c2ecf20Sopenharmony_ci	if (rv)
5458c2ecf20Sopenharmony_ci		goto end_error;
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_ci	rv = in_msg->ec_response.data_len;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	dev_dbg(dev,
5508c2ecf20Sopenharmony_ci		"in_msg: struct_ver=0x%x checksum=0x%x result=0x%x data_len=0x%x\n",
5518c2ecf20Sopenharmony_ci		in_msg->ec_response.struct_version,
5528c2ecf20Sopenharmony_ci		in_msg->ec_response.checksum,
5538c2ecf20Sopenharmony_ci		in_msg->ec_response.result,
5548c2ecf20Sopenharmony_ci		in_msg->ec_response.data_len);
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ciend_error:
5578c2ecf20Sopenharmony_ci	if (msg->command == EC_CMD_REBOOT_EC)
5588c2ecf20Sopenharmony_ci		msleep(EC_REBOOT_DELAY_MS);
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	up_read(&init_lock);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	return rv;
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic int cros_ec_dev_init(struct ishtp_cl_data *client_data)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	struct cros_ec_device *ec_dev;
5688c2ecf20Sopenharmony_ci	struct device *dev = cl_data_to_dev(client_data);
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
5718c2ecf20Sopenharmony_ci	if (!ec_dev)
5728c2ecf20Sopenharmony_ci		return -ENOMEM;
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	client_data->ec_dev = ec_dev;
5758c2ecf20Sopenharmony_ci	dev->driver_data = ec_dev;
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	ec_dev->dev = dev;
5788c2ecf20Sopenharmony_ci	ec_dev->priv = client_data->cros_ish_cl;
5798c2ecf20Sopenharmony_ci	ec_dev->cmd_xfer = NULL;
5808c2ecf20Sopenharmony_ci	ec_dev->pkt_xfer = cros_ec_pkt_xfer_ish;
5818c2ecf20Sopenharmony_ci	ec_dev->phys_name = dev_name(dev);
5828c2ecf20Sopenharmony_ci	ec_dev->din_size = sizeof(struct cros_ish_in_msg) +
5838c2ecf20Sopenharmony_ci			   sizeof(struct ec_response_get_protocol_info);
5848c2ecf20Sopenharmony_ci	ec_dev->dout_size = sizeof(struct cros_ish_out_msg);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	return cros_ec_register(ec_dev);
5878c2ecf20Sopenharmony_ci}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_cistatic void reset_handler(struct work_struct *work)
5908c2ecf20Sopenharmony_ci{
5918c2ecf20Sopenharmony_ci	int rv;
5928c2ecf20Sopenharmony_ci	struct device *dev;
5938c2ecf20Sopenharmony_ci	struct ishtp_cl *cros_ish_cl;
5948c2ecf20Sopenharmony_ci	struct ishtp_cl_device *cl_device;
5958c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data =
5968c2ecf20Sopenharmony_ci		container_of(work, struct ishtp_cl_data, work_ishtp_reset);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	/* Lock for reset to complete */
5998c2ecf20Sopenharmony_ci	down_write(&init_lock);
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	cros_ish_cl = client_data->cros_ish_cl;
6028c2ecf20Sopenharmony_ci	cl_device = client_data->cl_device;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	/* Unlink, flush queues & start again */
6058c2ecf20Sopenharmony_ci	ishtp_cl_unlink(cros_ish_cl);
6068c2ecf20Sopenharmony_ci	ishtp_cl_flush_queues(cros_ish_cl);
6078c2ecf20Sopenharmony_ci	ishtp_cl_free(cros_ish_cl);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	cros_ish_cl = ishtp_cl_allocate(cl_device);
6108c2ecf20Sopenharmony_ci	if (!cros_ish_cl) {
6118c2ecf20Sopenharmony_ci		up_write(&init_lock);
6128c2ecf20Sopenharmony_ci		return;
6138c2ecf20Sopenharmony_ci	}
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	ishtp_set_drvdata(cl_device, cros_ish_cl);
6168c2ecf20Sopenharmony_ci	ishtp_set_client_data(cros_ish_cl, client_data);
6178c2ecf20Sopenharmony_ci	client_data->cros_ish_cl = cros_ish_cl;
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_ci	rv = cros_ish_init(cros_ish_cl);
6208c2ecf20Sopenharmony_ci	if (rv) {
6218c2ecf20Sopenharmony_ci		ishtp_cl_free(cros_ish_cl);
6228c2ecf20Sopenharmony_ci		dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
6238c2ecf20Sopenharmony_ci		up_write(&init_lock);
6248c2ecf20Sopenharmony_ci		return;
6258c2ecf20Sopenharmony_ci	}
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	/* Refresh ec_dev device pointers */
6288c2ecf20Sopenharmony_ci	client_data->ec_dev->priv = client_data->cros_ish_cl;
6298c2ecf20Sopenharmony_ci	dev = cl_data_to_dev(client_data);
6308c2ecf20Sopenharmony_ci	dev->driver_data = client_data->ec_dev;
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	dev_info(cl_data_to_dev(client_data), "Chrome EC ISH reset done\n");
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	up_write(&init_lock);
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci/**
6388c2ecf20Sopenharmony_ci * cros_ec_ishtp_probe() - ISHTP client driver probe callback
6398c2ecf20Sopenharmony_ci * @cl_device: ISHTP client device instance
6408c2ecf20Sopenharmony_ci *
6418c2ecf20Sopenharmony_ci * Return: 0 for success, negative error code for failure.
6428c2ecf20Sopenharmony_ci */
6438c2ecf20Sopenharmony_cistatic int cros_ec_ishtp_probe(struct ishtp_cl_device *cl_device)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	int rv;
6468c2ecf20Sopenharmony_ci	struct ishtp_cl *cros_ish_cl;
6478c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data =
6488c2ecf20Sopenharmony_ci		devm_kzalloc(ishtp_device(cl_device),
6498c2ecf20Sopenharmony_ci			     sizeof(*client_data), GFP_KERNEL);
6508c2ecf20Sopenharmony_ci	if (!client_data)
6518c2ecf20Sopenharmony_ci		return -ENOMEM;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	/* Lock for initialization to complete */
6548c2ecf20Sopenharmony_ci	down_write(&init_lock);
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	cros_ish_cl = ishtp_cl_allocate(cl_device);
6578c2ecf20Sopenharmony_ci	if (!cros_ish_cl) {
6588c2ecf20Sopenharmony_ci		rv = -ENOMEM;
6598c2ecf20Sopenharmony_ci		goto end_ishtp_cl_alloc_error;
6608c2ecf20Sopenharmony_ci	}
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	ishtp_set_drvdata(cl_device, cros_ish_cl);
6638c2ecf20Sopenharmony_ci	ishtp_set_client_data(cros_ish_cl, client_data);
6648c2ecf20Sopenharmony_ci	client_data->cros_ish_cl = cros_ish_cl;
6658c2ecf20Sopenharmony_ci	client_data->cl_device = cl_device;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	init_waitqueue_head(&client_data->response.wait_queue);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	INIT_WORK(&client_data->work_ishtp_reset,
6708c2ecf20Sopenharmony_ci		  reset_handler);
6718c2ecf20Sopenharmony_ci	INIT_WORK(&client_data->work_ec_evt,
6728c2ecf20Sopenharmony_ci		  ish_evt_handler);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	rv = cros_ish_init(cros_ish_cl);
6758c2ecf20Sopenharmony_ci	if (rv)
6768c2ecf20Sopenharmony_ci		goto end_ishtp_cl_init_error;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	ishtp_get_device(cl_device);
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci	up_write(&init_lock);
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	/* Register croc_ec_dev mfd */
6838c2ecf20Sopenharmony_ci	rv = cros_ec_dev_init(client_data);
6848c2ecf20Sopenharmony_ci	if (rv) {
6858c2ecf20Sopenharmony_ci		down_write(&init_lock);
6868c2ecf20Sopenharmony_ci		goto end_cros_ec_dev_init_error;
6878c2ecf20Sopenharmony_ci	}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	return 0;
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ciend_cros_ec_dev_init_error:
6928c2ecf20Sopenharmony_ci	ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
6938c2ecf20Sopenharmony_ci	ishtp_cl_disconnect(cros_ish_cl);
6948c2ecf20Sopenharmony_ci	ishtp_cl_unlink(cros_ish_cl);
6958c2ecf20Sopenharmony_ci	ishtp_cl_flush_queues(cros_ish_cl);
6968c2ecf20Sopenharmony_ci	ishtp_put_device(cl_device);
6978c2ecf20Sopenharmony_ciend_ishtp_cl_init_error:
6988c2ecf20Sopenharmony_ci	ishtp_cl_free(cros_ish_cl);
6998c2ecf20Sopenharmony_ciend_ishtp_cl_alloc_error:
7008c2ecf20Sopenharmony_ci	up_write(&init_lock);
7018c2ecf20Sopenharmony_ci	return rv;
7028c2ecf20Sopenharmony_ci}
7038c2ecf20Sopenharmony_ci
7048c2ecf20Sopenharmony_ci/**
7058c2ecf20Sopenharmony_ci * cros_ec_ishtp_remove() - ISHTP client driver remove callback
7068c2ecf20Sopenharmony_ci * @cl_device: ISHTP client device instance
7078c2ecf20Sopenharmony_ci *
7088c2ecf20Sopenharmony_ci * Return: 0
7098c2ecf20Sopenharmony_ci */
7108c2ecf20Sopenharmony_cistatic int cros_ec_ishtp_remove(struct ishtp_cl_device *cl_device)
7118c2ecf20Sopenharmony_ci{
7128c2ecf20Sopenharmony_ci	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
7138c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	cancel_work_sync(&client_data->work_ishtp_reset);
7168c2ecf20Sopenharmony_ci	cancel_work_sync(&client_data->work_ec_evt);
7178c2ecf20Sopenharmony_ci	cros_ish_deinit(cros_ish_cl);
7188c2ecf20Sopenharmony_ci	ishtp_put_device(cl_device);
7198c2ecf20Sopenharmony_ci
7208c2ecf20Sopenharmony_ci	return 0;
7218c2ecf20Sopenharmony_ci}
7228c2ecf20Sopenharmony_ci
7238c2ecf20Sopenharmony_ci/**
7248c2ecf20Sopenharmony_ci * cros_ec_ishtp_reset() - ISHTP client driver reset callback
7258c2ecf20Sopenharmony_ci * @cl_device: ISHTP client device instance
7268c2ecf20Sopenharmony_ci *
7278c2ecf20Sopenharmony_ci * Return: 0
7288c2ecf20Sopenharmony_ci */
7298c2ecf20Sopenharmony_cistatic int cros_ec_ishtp_reset(struct ishtp_cl_device *cl_device)
7308c2ecf20Sopenharmony_ci{
7318c2ecf20Sopenharmony_ci	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
7328c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci	schedule_work(&client_data->work_ishtp_reset);
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci	return 0;
7378c2ecf20Sopenharmony_ci}
7388c2ecf20Sopenharmony_ci
7398c2ecf20Sopenharmony_ci/**
7408c2ecf20Sopenharmony_ci * cros_ec_ishtp_suspend() - ISHTP client driver suspend callback
7418c2ecf20Sopenharmony_ci * @device: device instance
7428c2ecf20Sopenharmony_ci *
7438c2ecf20Sopenharmony_ci * Return: 0 for success, negative error code for failure.
7448c2ecf20Sopenharmony_ci */
7458c2ecf20Sopenharmony_cistatic int __maybe_unused cros_ec_ishtp_suspend(struct device *device)
7468c2ecf20Sopenharmony_ci{
7478c2ecf20Sopenharmony_ci	struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
7488c2ecf20Sopenharmony_ci	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
7498c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	return cros_ec_suspend(client_data->ec_dev);
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci/**
7558c2ecf20Sopenharmony_ci * cros_ec_ishtp_resume() - ISHTP client driver resume callback
7568c2ecf20Sopenharmony_ci * @device: device instance
7578c2ecf20Sopenharmony_ci *
7588c2ecf20Sopenharmony_ci * Return: 0 for success, negative error code for failure.
7598c2ecf20Sopenharmony_ci */
7608c2ecf20Sopenharmony_cistatic int __maybe_unused cros_ec_ishtp_resume(struct device *device)
7618c2ecf20Sopenharmony_ci{
7628c2ecf20Sopenharmony_ci	struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
7638c2ecf20Sopenharmony_ci	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
7648c2ecf20Sopenharmony_ci	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_ci	return cros_ec_resume(client_data->ec_dev);
7678c2ecf20Sopenharmony_ci}
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(cros_ec_ishtp_pm_ops, cros_ec_ishtp_suspend,
7708c2ecf20Sopenharmony_ci			 cros_ec_ishtp_resume);
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_cistatic struct ishtp_cl_driver	cros_ec_ishtp_driver = {
7738c2ecf20Sopenharmony_ci	.name = "cros_ec_ishtp",
7748c2ecf20Sopenharmony_ci	.guid = &cros_ish_guid,
7758c2ecf20Sopenharmony_ci	.probe = cros_ec_ishtp_probe,
7768c2ecf20Sopenharmony_ci	.remove = cros_ec_ishtp_remove,
7778c2ecf20Sopenharmony_ci	.reset = cros_ec_ishtp_reset,
7788c2ecf20Sopenharmony_ci	.driver = {
7798c2ecf20Sopenharmony_ci		.pm = &cros_ec_ishtp_pm_ops,
7808c2ecf20Sopenharmony_ci	},
7818c2ecf20Sopenharmony_ci};
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_cistatic int __init cros_ec_ishtp_mod_init(void)
7848c2ecf20Sopenharmony_ci{
7858c2ecf20Sopenharmony_ci	return ishtp_cl_driver_register(&cros_ec_ishtp_driver, THIS_MODULE);
7868c2ecf20Sopenharmony_ci}
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_cistatic void __exit cros_ec_ishtp_mod_exit(void)
7898c2ecf20Sopenharmony_ci{
7908c2ecf20Sopenharmony_ci	ishtp_cl_driver_unregister(&cros_ec_ishtp_driver);
7918c2ecf20Sopenharmony_ci}
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_cimodule_init(cros_ec_ishtp_mod_init);
7948c2ecf20Sopenharmony_cimodule_exit(cros_ec_ishtp_mod_exit);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("ChromeOS EC ISHTP Client Driver");
7978c2ecf20Sopenharmony_ciMODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
7988c2ecf20Sopenharmony_ci
7998c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
8008c2ecf20Sopenharmony_ciMODULE_ALIAS("ishtp:*");
801