1 // SPDX-License-Identifier: GPL-2.0
2 // ISHTP interface for ChromeOS Embedded Controller
3 //
4 // Copyright (c) 2019, Intel Corporation.
5 //
6 // ISHTP client driver for talking to the Chrome OS EC firmware running
7 // on Intel Integrated Sensor Hub (ISH) using the ISH Transport protocol
8 // (ISH-TP).
9 
10 #include <linux/delay.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/platform_data/cros_ec_commands.h>
14 #include <linux/platform_data/cros_ec_proto.h>
15 #include <linux/intel-ish-client-if.h>
16 
17 /*
18  * ISH TX/RX ring buffer pool size
19  *
20  * The AP->ISH messages and corresponding ISH->AP responses are
21  * serialized. We need 1 TX and 1 RX buffer for these.
22  *
23  * The MKBP ISH->AP events are serialized. We need one additional RX
24  * buffer for them.
25  */
26 #define CROS_ISH_CL_TX_RING_SIZE		8
27 #define CROS_ISH_CL_RX_RING_SIZE		8
28 
29 /* ISH CrOS EC Host Commands */
30 enum cros_ec_ish_channel {
31 	CROS_EC_COMMAND = 1,			/* AP->ISH message */
32 	CROS_MKBP_EVENT = 2,			/* ISH->AP events */
33 };
34 
35 /*
36  * ISH firmware timeout for 1 message send failure is 1Hz, and the
37  * firmware will retry 2 times, so 3Hz is used for timeout.
38  */
39 #define ISHTP_SEND_TIMEOUT			(3 * HZ)
40 
41 /* ISH Transport CrOS EC ISH client unique GUID */
42 static const guid_t cros_ish_guid =
43 	GUID_INIT(0x7b7154d0, 0x56f4, 0x4bdc,
44 		  0xb0, 0xd8, 0x9e, 0x7c, 0xda,	0xe0, 0xd6, 0xa0);
45 
46 struct header {
47 	u8 channel;
48 	u8 status;
49 	u8 reserved[2];
50 } __packed;
51 
52 struct cros_ish_out_msg {
53 	struct header hdr;
54 	struct ec_host_request ec_request;
55 } __packed;
56 
57 struct cros_ish_in_msg {
58 	struct header hdr;
59 	struct ec_host_response ec_response;
60 } __packed;
61 
62 #define IN_MSG_EC_RESPONSE_PREAMBLE					\
63 	offsetof(struct cros_ish_in_msg, ec_response)
64 
65 #define OUT_MSG_EC_REQUEST_PREAMBLE					\
66 	offsetof(struct cros_ish_out_msg, ec_request)
67 
68 #define cl_data_to_dev(client_data) ishtp_device((client_data)->cl_device)
69 
70 /*
71  * The Read-Write Semaphore is used to prevent message TX or RX while
72  * the ishtp client is being initialized or undergoing reset.
73  *
74  * The readers are the kernel function calls responsible for IA->ISH
75  * and ISH->AP messaging.
76  *
77  * The writers are .reset() and .probe() function.
78  */
79 DECLARE_RWSEM(init_lock);
80 
81 /**
82  * struct response_info - Encapsulate firmware response related
83  * information for passing between function ish_send() and
84  * process_recv() callback.
85  *
86  * @data: Copy the data received from firmware here.
87  * @max_size: Max size allocated for the @data buffer. If the received
88  * data exceeds this value, we log an error.
89  * @size: Actual size of data received from firmware.
90  * @error: 0 for success, negative error code for a failure in process_recv().
91  * @received: Set to true on receiving a valid firmware	response to host command
92  * @wait_queue: Wait queue for host to wait for firmware response.
93  */
94 struct response_info {
95 	void *data;
96 	size_t max_size;
97 	size_t size;
98 	int error;
99 	bool received;
100 	wait_queue_head_t wait_queue;
101 };
102 
103 /**
104  * struct ishtp_cl_data - Encapsulate per ISH TP Client.
105  *
106  * @cros_ish_cl: ISHTP firmware client instance.
107  * @cl_device: ISHTP client device instance.
108  * @response: Response info passing between ish_send() and process_recv().
109  * @work_ishtp_reset: Work queue reset handling.
110  * @work_ec_evt: Work queue for EC events.
111  * @ec_dev: CrOS EC MFD device.
112  *
113  * This structure is used to store per client data.
114  */
115 struct ishtp_cl_data {
116 	struct ishtp_cl *cros_ish_cl;
117 	struct ishtp_cl_device *cl_device;
118 
119 	/*
120 	 * Used for passing firmware response information between
121 	 * ish_send() and process_recv() callback.
122 	 */
123 	struct response_info response;
124 
125 	struct work_struct work_ishtp_reset;
126 	struct work_struct work_ec_evt;
127 	struct cros_ec_device *ec_dev;
128 };
129 
130 /**
131  * ish_evt_handler - ISH to AP event handler
132  * @work: Work struct
133  */
134 static void ish_evt_handler(struct work_struct *work)
135 {
136 	struct ishtp_cl_data *client_data =
137 		container_of(work, struct ishtp_cl_data, work_ec_evt);
138 	struct cros_ec_device *ec_dev = client_data->ec_dev;
139 	bool ec_has_more_events;
140 
141 	do {
142 		ec_has_more_events = cros_ec_handle_event(ec_dev);
143 	} while (ec_has_more_events);
144 }
145 
146 /**
147  * ish_send() - Send message from host to firmware
148  *
149  * @client_data: Client data instance
150  * @out_msg: Message buffer to be sent to firmware
151  * @out_size: Size of out going message
152  * @in_msg: Message buffer where the incoming data is copied. This buffer
153  * is allocated by calling
154  * @in_size: Max size of incoming message
155  *
156  * Return: Number of bytes copied in the in_msg on success, negative
157  * error code on failure.
158  */
159 static int ish_send(struct ishtp_cl_data *client_data,
160 		    u8 *out_msg, size_t out_size,
161 		    u8 *in_msg, size_t in_size)
162 {
163 	int rv;
164 	struct header *out_hdr = (struct header *)out_msg;
165 	struct ishtp_cl *cros_ish_cl = client_data->cros_ish_cl;
166 
167 	dev_dbg(cl_data_to_dev(client_data),
168 		"%s: channel=%02u status=%02u\n",
169 		__func__, out_hdr->channel, out_hdr->status);
170 
171 	/* Setup for incoming response */
172 	client_data->response.data = in_msg;
173 	client_data->response.max_size = in_size;
174 	client_data->response.error = 0;
175 	client_data->response.received = false;
176 
177 	rv = ishtp_cl_send(cros_ish_cl, out_msg, out_size);
178 	if (rv) {
179 		dev_err(cl_data_to_dev(client_data),
180 			"ishtp_cl_send error %d\n", rv);
181 		return rv;
182 	}
183 
184 	wait_event_interruptible_timeout(client_data->response.wait_queue,
185 					 client_data->response.received,
186 					 ISHTP_SEND_TIMEOUT);
187 	if (!client_data->response.received) {
188 		dev_err(cl_data_to_dev(client_data),
189 			"Timed out for response to host message\n");
190 		return -ETIMEDOUT;
191 	}
192 
193 	if (client_data->response.error < 0)
194 		return client_data->response.error;
195 
196 	return client_data->response.size;
197 }
198 
199 /**
200  * process_recv() - Received and parse incoming packet
201  * @cros_ish_cl: Client instance to get stats
202  * @rb_in_proc: Host interface message buffer
203  * @timestamp: Timestamp of when parent callback started
204  *
205  * Parse the incoming packet. If it is a response packet then it will
206  * update per instance flags and wake up the caller waiting to for the
207  * response. If it is an event packet then it will schedule event work.
208  */
209 static void process_recv(struct ishtp_cl *cros_ish_cl,
210 			 struct ishtp_cl_rb *rb_in_proc, ktime_t timestamp)
211 {
212 	size_t data_len = rb_in_proc->buf_idx;
213 	struct ishtp_cl_data *client_data =
214 		ishtp_get_client_data(cros_ish_cl);
215 	struct device *dev = cl_data_to_dev(client_data);
216 	struct cros_ish_in_msg *in_msg =
217 		(struct cros_ish_in_msg *)rb_in_proc->buffer.data;
218 
219 	/* Proceed only if reset or init is not in progress */
220 	if (!down_read_trylock(&init_lock)) {
221 		/* Free the buffer */
222 		ishtp_cl_io_rb_recycle(rb_in_proc);
223 		dev_warn(dev,
224 			 "Host is not ready to receive incoming messages\n");
225 		return;
226 	}
227 
228 	/*
229 	 * All firmware messages contain a header. Check the buffer size
230 	 * before accessing elements inside.
231 	 */
232 	if (!rb_in_proc->buffer.data) {
233 		dev_warn(dev, "rb_in_proc->buffer.data returned null");
234 		client_data->response.error = -EBADMSG;
235 		goto end_error;
236 	}
237 
238 	if (data_len < sizeof(struct header)) {
239 		dev_err(dev, "data size %zu is less than header %zu\n",
240 			data_len, sizeof(struct header));
241 		client_data->response.error = -EMSGSIZE;
242 		goto end_error;
243 	}
244 
245 	dev_dbg(dev, "channel=%02u status=%02u\n",
246 		in_msg->hdr.channel, in_msg->hdr.status);
247 
248 	switch (in_msg->hdr.channel) {
249 	case CROS_EC_COMMAND:
250 		/* Sanity check */
251 		if (!client_data->response.data) {
252 			dev_err(dev,
253 				"Receiving buffer is null. Should be allocated by calling function\n");
254 			client_data->response.error = -EINVAL;
255 			goto error_wake_up;
256 		}
257 
258 		if (client_data->response.received) {
259 			dev_err(dev,
260 				"Previous firmware message not yet processed\n");
261 			client_data->response.error = -EINVAL;
262 			goto error_wake_up;
263 		}
264 
265 		if (data_len > client_data->response.max_size) {
266 			dev_err(dev,
267 				"Received buffer size %zu is larger than allocated buffer %zu\n",
268 				data_len, client_data->response.max_size);
269 			client_data->response.error = -EMSGSIZE;
270 			goto error_wake_up;
271 		}
272 
273 		if (in_msg->hdr.status) {
274 			dev_err(dev, "firmware returned status %d\n",
275 				in_msg->hdr.status);
276 			client_data->response.error = -EIO;
277 			goto error_wake_up;
278 		}
279 
280 		/* Update the actual received buffer size */
281 		client_data->response.size = data_len;
282 
283 		/*
284 		 * Copy the buffer received in firmware response for the
285 		 * calling thread.
286 		 */
287 		memcpy(client_data->response.data,
288 		       rb_in_proc->buffer.data, data_len);
289 
290 		/* Set flag before waking up the caller */
291 		client_data->response.received = true;
292 error_wake_up:
293 		/* Wake the calling thread */
294 		wake_up_interruptible(&client_data->response.wait_queue);
295 
296 		break;
297 
298 	case CROS_MKBP_EVENT:
299 		/*
300 		 * Set timestamp from beginning of function since we actually
301 		 * got an incoming MKBP event
302 		 */
303 		client_data->ec_dev->last_event_time = timestamp;
304 		/* The event system doesn't send any data in buffer */
305 		schedule_work(&client_data->work_ec_evt);
306 
307 		break;
308 
309 	default:
310 		dev_err(dev, "Invalid channel=%02d\n", in_msg->hdr.channel);
311 	}
312 
313 end_error:
314 	/* Free the buffer */
315 	ishtp_cl_io_rb_recycle(rb_in_proc);
316 
317 	up_read(&init_lock);
318 }
319 
320 /**
321  * ish_event_cb() - bus driver callback for incoming message
322  * @cl_device: ISHTP client device for which this message is targeted.
323  *
324  * Remove the packet from the list and process the message by calling
325  * process_recv.
326  */
327 static void ish_event_cb(struct ishtp_cl_device *cl_device)
328 {
329 	struct ishtp_cl_rb *rb_in_proc;
330 	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
331 	ktime_t timestamp;
332 
333 	/*
334 	 * Take timestamp as close to hardware interrupt as possible for sensor
335 	 * timestamps.
336 	 */
337 	timestamp = cros_ec_get_time_ns();
338 
339 	while ((rb_in_proc = ishtp_cl_rx_get_rb(cros_ish_cl)) != NULL) {
340 		/* Decide what to do with received data */
341 		process_recv(cros_ish_cl, rb_in_proc, timestamp);
342 	}
343 }
344 
345 /**
346  * cros_ish_init() - Init function for ISHTP client
347  * @cros_ish_cl: ISHTP client instance
348  *
349  * This function complete the initializtion of the client.
350  *
351  * Return: 0 for success, negative error code for failure.
352  */
353 static int cros_ish_init(struct ishtp_cl *cros_ish_cl)
354 {
355 	int rv;
356 	struct ishtp_device *dev;
357 	struct ishtp_fw_client *fw_client;
358 	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
359 
360 	rv = ishtp_cl_link(cros_ish_cl);
361 	if (rv) {
362 		dev_err(cl_data_to_dev(client_data),
363 			"ishtp_cl_link failed\n");
364 		return rv;
365 	}
366 
367 	dev = ishtp_get_ishtp_device(cros_ish_cl);
368 
369 	/* Connect to firmware client */
370 	ishtp_set_tx_ring_size(cros_ish_cl, CROS_ISH_CL_TX_RING_SIZE);
371 	ishtp_set_rx_ring_size(cros_ish_cl, CROS_ISH_CL_RX_RING_SIZE);
372 
373 	fw_client = ishtp_fw_cl_get_client(dev, &cros_ish_guid);
374 	if (!fw_client) {
375 		dev_err(cl_data_to_dev(client_data),
376 			"ish client uuid not found\n");
377 		rv = -ENOENT;
378 		goto err_cl_unlink;
379 	}
380 
381 	ishtp_cl_set_fw_client_id(cros_ish_cl,
382 				  ishtp_get_fw_client_id(fw_client));
383 	ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_CONNECTING);
384 
385 	rv = ishtp_cl_connect(cros_ish_cl);
386 	if (rv) {
387 		dev_err(cl_data_to_dev(client_data),
388 			"client connect fail\n");
389 		goto err_cl_unlink;
390 	}
391 
392 	ishtp_register_event_cb(client_data->cl_device, ish_event_cb);
393 	return 0;
394 
395 err_cl_unlink:
396 	ishtp_cl_unlink(cros_ish_cl);
397 	return rv;
398 }
399 
400 /**
401  * cros_ish_deinit() - Deinit function for ISHTP client
402  * @cros_ish_cl: ISHTP client instance
403  *
404  * Unlink and free cros_ec client
405  */
406 static void cros_ish_deinit(struct ishtp_cl *cros_ish_cl)
407 {
408 	ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
409 	ishtp_cl_disconnect(cros_ish_cl);
410 	ishtp_cl_unlink(cros_ish_cl);
411 	ishtp_cl_flush_queues(cros_ish_cl);
412 
413 	/* Disband and free all Tx and Rx client-level rings */
414 	ishtp_cl_free(cros_ish_cl);
415 }
416 
417 /**
418  * prepare_cros_ec_rx() - Check & prepare receive buffer
419  * @ec_dev: CrOS EC MFD device.
420  * @in_msg: Incoming message buffer
421  * @msg: cros_ec command used to send & receive data
422  *
423  * Return: 0 for success, negative error code for failure.
424  *
425  * Check the received buffer. Convert to cros_ec_command format.
426  */
427 static int prepare_cros_ec_rx(struct cros_ec_device *ec_dev,
428 			      const struct cros_ish_in_msg *in_msg,
429 			      struct cros_ec_command *msg)
430 {
431 	u8 sum = 0;
432 	int i, rv, offset;
433 
434 	/* Check response error code */
435 	msg->result = in_msg->ec_response.result;
436 	rv = cros_ec_check_result(ec_dev, msg);
437 	if (rv < 0)
438 		return rv;
439 
440 	if (in_msg->ec_response.data_len > msg->insize) {
441 		dev_err(ec_dev->dev, "Packet too long (%d bytes, expected %d)",
442 			in_msg->ec_response.data_len, msg->insize);
443 		return -ENOSPC;
444 	}
445 
446 	/* Copy response packet payload and compute checksum */
447 	for (i = 0; i < sizeof(struct ec_host_response); i++)
448 		sum += ((u8 *)in_msg)[IN_MSG_EC_RESPONSE_PREAMBLE + i];
449 
450 	offset = sizeof(struct cros_ish_in_msg);
451 	for (i = 0; i < in_msg->ec_response.data_len; i++)
452 		sum += msg->data[i] = ((u8 *)in_msg)[offset + i];
453 
454 	if (sum) {
455 		dev_dbg(ec_dev->dev, "Bad received packet checksum %d\n", sum);
456 		return -EBADMSG;
457 	}
458 
459 	return 0;
460 }
461 
462 static int cros_ec_pkt_xfer_ish(struct cros_ec_device *ec_dev,
463 				struct cros_ec_command *msg)
464 {
465 	int rv;
466 	struct ishtp_cl *cros_ish_cl = ec_dev->priv;
467 	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
468 	struct device *dev = cl_data_to_dev(client_data);
469 	struct cros_ish_in_msg *in_msg = (struct cros_ish_in_msg *)ec_dev->din;
470 	struct cros_ish_out_msg *out_msg =
471 		(struct cros_ish_out_msg *)ec_dev->dout;
472 	size_t in_size = sizeof(struct cros_ish_in_msg) + msg->insize;
473 	size_t out_size = sizeof(struct cros_ish_out_msg) + msg->outsize;
474 
475 	/* Sanity checks */
476 	if (in_size > ec_dev->din_size) {
477 		dev_err(dev,
478 			"Incoming payload size %zu is too large for ec_dev->din_size %d\n",
479 			in_size, ec_dev->din_size);
480 		return -EMSGSIZE;
481 	}
482 
483 	if (out_size > ec_dev->dout_size) {
484 		dev_err(dev,
485 			"Outgoing payload size %zu is too large for ec_dev->dout_size %d\n",
486 			out_size, ec_dev->dout_size);
487 		return -EMSGSIZE;
488 	}
489 
490 	/* Proceed only if reset-init is not in progress */
491 	if (!down_read_trylock(&init_lock)) {
492 		dev_warn(dev,
493 			 "Host is not ready to send messages to ISH. Try again\n");
494 		return -EAGAIN;
495 	}
496 
497 	/* Prepare the package to be sent over ISH TP */
498 	out_msg->hdr.channel = CROS_EC_COMMAND;
499 	out_msg->hdr.status = 0;
500 
501 	ec_dev->dout += OUT_MSG_EC_REQUEST_PREAMBLE;
502 	cros_ec_prepare_tx(ec_dev, msg);
503 	ec_dev->dout -= OUT_MSG_EC_REQUEST_PREAMBLE;
504 
505 	dev_dbg(dev,
506 		"out_msg: struct_ver=0x%x checksum=0x%x command=0x%x command_ver=0x%x data_len=0x%x\n",
507 		out_msg->ec_request.struct_version,
508 		out_msg->ec_request.checksum,
509 		out_msg->ec_request.command,
510 		out_msg->ec_request.command_version,
511 		out_msg->ec_request.data_len);
512 
513 	/* Send command to ISH EC firmware and read response */
514 	rv = ish_send(client_data,
515 		      (u8 *)out_msg, out_size,
516 		      (u8 *)in_msg, in_size);
517 	if (rv < 0)
518 		goto end_error;
519 
520 	rv = prepare_cros_ec_rx(ec_dev, in_msg, msg);
521 	if (rv)
522 		goto end_error;
523 
524 	rv = in_msg->ec_response.data_len;
525 
526 	dev_dbg(dev,
527 		"in_msg: struct_ver=0x%x checksum=0x%x result=0x%x data_len=0x%x\n",
528 		in_msg->ec_response.struct_version,
529 		in_msg->ec_response.checksum,
530 		in_msg->ec_response.result,
531 		in_msg->ec_response.data_len);
532 
533 end_error:
534 	if (msg->command == EC_CMD_REBOOT_EC)
535 		msleep(EC_REBOOT_DELAY_MS);
536 
537 	up_read(&init_lock);
538 
539 	return rv;
540 }
541 
542 static int cros_ec_dev_init(struct ishtp_cl_data *client_data)
543 {
544 	struct cros_ec_device *ec_dev;
545 	struct device *dev = cl_data_to_dev(client_data);
546 
547 	ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL);
548 	if (!ec_dev)
549 		return -ENOMEM;
550 
551 	client_data->ec_dev = ec_dev;
552 	dev->driver_data = ec_dev;
553 
554 	ec_dev->dev = dev;
555 	ec_dev->priv = client_data->cros_ish_cl;
556 	ec_dev->cmd_xfer = NULL;
557 	ec_dev->pkt_xfer = cros_ec_pkt_xfer_ish;
558 	ec_dev->phys_name = dev_name(dev);
559 	ec_dev->din_size = sizeof(struct cros_ish_in_msg) +
560 			   sizeof(struct ec_response_get_protocol_info);
561 	ec_dev->dout_size = sizeof(struct cros_ish_out_msg);
562 
563 	return cros_ec_register(ec_dev);
564 }
565 
566 static void reset_handler(struct work_struct *work)
567 {
568 	int rv;
569 	struct device *dev;
570 	struct ishtp_cl *cros_ish_cl;
571 	struct ishtp_cl_device *cl_device;
572 	struct ishtp_cl_data *client_data =
573 		container_of(work, struct ishtp_cl_data, work_ishtp_reset);
574 
575 	/* Lock for reset to complete */
576 	down_write(&init_lock);
577 
578 	cros_ish_cl = client_data->cros_ish_cl;
579 	cl_device = client_data->cl_device;
580 
581 	/* Unlink, flush queues & start again */
582 	ishtp_cl_unlink(cros_ish_cl);
583 	ishtp_cl_flush_queues(cros_ish_cl);
584 	ishtp_cl_free(cros_ish_cl);
585 
586 	cros_ish_cl = ishtp_cl_allocate(cl_device);
587 	if (!cros_ish_cl) {
588 		up_write(&init_lock);
589 		return;
590 	}
591 
592 	ishtp_set_drvdata(cl_device, cros_ish_cl);
593 	ishtp_set_client_data(cros_ish_cl, client_data);
594 	client_data->cros_ish_cl = cros_ish_cl;
595 
596 	rv = cros_ish_init(cros_ish_cl);
597 	if (rv) {
598 		ishtp_cl_free(cros_ish_cl);
599 		dev_err(cl_data_to_dev(client_data), "Reset Failed\n");
600 		up_write(&init_lock);
601 		return;
602 	}
603 
604 	/* Refresh ec_dev device pointers */
605 	client_data->ec_dev->priv = client_data->cros_ish_cl;
606 	dev = cl_data_to_dev(client_data);
607 	dev->driver_data = client_data->ec_dev;
608 
609 	dev_info(cl_data_to_dev(client_data), "Chrome EC ISH reset done\n");
610 
611 	up_write(&init_lock);
612 }
613 
614 /**
615  * cros_ec_ishtp_probe() - ISHTP client driver probe callback
616  * @cl_device: ISHTP client device instance
617  *
618  * Return: 0 for success, negative error code for failure.
619  */
620 static int cros_ec_ishtp_probe(struct ishtp_cl_device *cl_device)
621 {
622 	int rv;
623 	struct ishtp_cl *cros_ish_cl;
624 	struct ishtp_cl_data *client_data =
625 		devm_kzalloc(ishtp_device(cl_device),
626 			     sizeof(*client_data), GFP_KERNEL);
627 	if (!client_data)
628 		return -ENOMEM;
629 
630 	/* Lock for initialization to complete */
631 	down_write(&init_lock);
632 
633 	cros_ish_cl = ishtp_cl_allocate(cl_device);
634 	if (!cros_ish_cl) {
635 		rv = -ENOMEM;
636 		goto end_ishtp_cl_alloc_error;
637 	}
638 
639 	ishtp_set_drvdata(cl_device, cros_ish_cl);
640 	ishtp_set_client_data(cros_ish_cl, client_data);
641 	client_data->cros_ish_cl = cros_ish_cl;
642 	client_data->cl_device = cl_device;
643 
644 	init_waitqueue_head(&client_data->response.wait_queue);
645 
646 	INIT_WORK(&client_data->work_ishtp_reset,
647 		  reset_handler);
648 	INIT_WORK(&client_data->work_ec_evt,
649 		  ish_evt_handler);
650 
651 	rv = cros_ish_init(cros_ish_cl);
652 	if (rv)
653 		goto end_ishtp_cl_init_error;
654 
655 	ishtp_get_device(cl_device);
656 
657 	up_write(&init_lock);
658 
659 	/* Register croc_ec_dev mfd */
660 	rv = cros_ec_dev_init(client_data);
661 	if (rv)
662 		goto end_cros_ec_dev_init_error;
663 
664 	return 0;
665 
666 end_cros_ec_dev_init_error:
667 	ishtp_set_connection_state(cros_ish_cl, ISHTP_CL_DISCONNECTING);
668 	ishtp_cl_disconnect(cros_ish_cl);
669 	ishtp_cl_unlink(cros_ish_cl);
670 	ishtp_cl_flush_queues(cros_ish_cl);
671 	ishtp_put_device(cl_device);
672 end_ishtp_cl_init_error:
673 	ishtp_cl_free(cros_ish_cl);
674 end_ishtp_cl_alloc_error:
675 	up_write(&init_lock);
676 	return rv;
677 }
678 
679 /**
680  * cros_ec_ishtp_remove() - ISHTP client driver remove callback
681  * @cl_device: ISHTP client device instance
682  *
683  * Return: 0
684  */
685 static int cros_ec_ishtp_remove(struct ishtp_cl_device *cl_device)
686 {
687 	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
688 	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
689 
690 	cancel_work_sync(&client_data->work_ishtp_reset);
691 	cancel_work_sync(&client_data->work_ec_evt);
692 	cros_ish_deinit(cros_ish_cl);
693 	ishtp_put_device(cl_device);
694 
695 	return 0;
696 }
697 
698 /**
699  * cros_ec_ishtp_reset() - ISHTP client driver reset callback
700  * @cl_device: ISHTP client device instance
701  *
702  * Return: 0
703  */
704 static int cros_ec_ishtp_reset(struct ishtp_cl_device *cl_device)
705 {
706 	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
707 	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
708 
709 	schedule_work(&client_data->work_ishtp_reset);
710 
711 	return 0;
712 }
713 
714 /**
715  * cros_ec_ishtp_suspend() - ISHTP client driver suspend callback
716  * @device: device instance
717  *
718  * Return: 0 for success, negative error code for failure.
719  */
720 static int __maybe_unused cros_ec_ishtp_suspend(struct device *device)
721 {
722 	struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
723 	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
724 	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
725 
726 	return cros_ec_suspend(client_data->ec_dev);
727 }
728 
729 /**
730  * cros_ec_ishtp_resume() - ISHTP client driver resume callback
731  * @device: device instance
732  *
733  * Return: 0 for success, negative error code for failure.
734  */
735 static int __maybe_unused cros_ec_ishtp_resume(struct device *device)
736 {
737 	struct ishtp_cl_device *cl_device = ishtp_dev_to_cl_device(device);
738 	struct ishtp_cl	*cros_ish_cl = ishtp_get_drvdata(cl_device);
739 	struct ishtp_cl_data *client_data = ishtp_get_client_data(cros_ish_cl);
740 
741 	return cros_ec_resume(client_data->ec_dev);
742 }
743 
744 static SIMPLE_DEV_PM_OPS(cros_ec_ishtp_pm_ops, cros_ec_ishtp_suspend,
745 			 cros_ec_ishtp_resume);
746 
747 static struct ishtp_cl_driver	cros_ec_ishtp_driver = {
748 	.name = "cros_ec_ishtp",
749 	.guid = &cros_ish_guid,
750 	.probe = cros_ec_ishtp_probe,
751 	.remove = cros_ec_ishtp_remove,
752 	.reset = cros_ec_ishtp_reset,
753 	.driver = {
754 		.pm = &cros_ec_ishtp_pm_ops,
755 	},
756 };
757 
758 static int __init cros_ec_ishtp_mod_init(void)
759 {
760 	return ishtp_cl_driver_register(&cros_ec_ishtp_driver, THIS_MODULE);
761 }
762 
763 static void __exit cros_ec_ishtp_mod_exit(void)
764 {
765 	ishtp_cl_driver_unregister(&cros_ec_ishtp_driver);
766 }
767 
768 module_init(cros_ec_ishtp_mod_init);
769 module_exit(cros_ec_ishtp_mod_exit);
770 
771 MODULE_DESCRIPTION("ChromeOS EC ISHTP Client Driver");
772 MODULE_AUTHOR("Rushikesh S Kadam <rushikesh.s.kadam@intel.com>");
773 
774 MODULE_LICENSE("GPL v2");
775 MODULE_ALIAS("ishtp:*");
776