cros_ec_ishtp.c 20 KB

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