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