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