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