cros_ec_proto.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /*
  2. * ChromeOS EC communication protocol helper functions
  3. *
  4. * Copyright (C) 2015 Google, Inc
  5. *
  6. * This software is licensed under the terms of the GNU General Public
  7. * License version 2, as published by the Free Software Foundation, and
  8. * may be copied, distributed, and modified under those terms.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/mfd/cros_ec.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <asm/unaligned.h>
  22. #define EC_COMMAND_RETRIES 50
  23. static int prepare_packet(struct cros_ec_device *ec_dev,
  24. struct cros_ec_command *msg)
  25. {
  26. struct ec_host_request *request;
  27. u8 *out;
  28. int i;
  29. u8 csum = 0;
  30. BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
  31. BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
  32. out = ec_dev->dout;
  33. request = (struct ec_host_request *)out;
  34. request->struct_version = EC_HOST_REQUEST_VERSION;
  35. request->checksum = 0;
  36. request->command = msg->command;
  37. request->command_version = msg->version;
  38. request->reserved = 0;
  39. request->data_len = msg->outsize;
  40. for (i = 0; i < sizeof(*request); i++)
  41. csum += out[i];
  42. /* Copy data and update checksum */
  43. memcpy(out + sizeof(*request), msg->data, msg->outsize);
  44. for (i = 0; i < msg->outsize; i++)
  45. csum += msg->data[i];
  46. request->checksum = -csum;
  47. return sizeof(*request) + msg->outsize;
  48. }
  49. static int send_command(struct cros_ec_device *ec_dev,
  50. struct cros_ec_command *msg)
  51. {
  52. int ret;
  53. int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
  54. if (ec_dev->proto_version > 2)
  55. xfer_fxn = ec_dev->pkt_xfer;
  56. else
  57. xfer_fxn = ec_dev->cmd_xfer;
  58. if (!xfer_fxn) {
  59. /*
  60. * This error can happen if a communication error happened and
  61. * the EC is trying to use protocol v2, on an underlying
  62. * communication mechanism that does not support v2.
  63. */
  64. dev_err_once(ec_dev->dev,
  65. "missing EC transfer API, cannot send command\n");
  66. return -EIO;
  67. }
  68. ret = (*xfer_fxn)(ec_dev, msg);
  69. if (msg->result == EC_RES_IN_PROGRESS) {
  70. int i;
  71. struct cros_ec_command *status_msg;
  72. struct ec_response_get_comms_status *status;
  73. status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
  74. GFP_KERNEL);
  75. if (!status_msg)
  76. return -ENOMEM;
  77. status_msg->version = 0;
  78. status_msg->command = EC_CMD_GET_COMMS_STATUS;
  79. status_msg->insize = sizeof(*status);
  80. status_msg->outsize = 0;
  81. /*
  82. * Query the EC's status until it's no longer busy or
  83. * we encounter an error.
  84. */
  85. for (i = 0; i < EC_COMMAND_RETRIES; i++) {
  86. usleep_range(10000, 11000);
  87. ret = (*xfer_fxn)(ec_dev, status_msg);
  88. if (ret == -EAGAIN)
  89. continue;
  90. if (ret < 0)
  91. break;
  92. msg->result = status_msg->result;
  93. if (status_msg->result != EC_RES_SUCCESS)
  94. break;
  95. status = (struct ec_response_get_comms_status *)
  96. status_msg->data;
  97. if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
  98. break;
  99. }
  100. kfree(status_msg);
  101. }
  102. return ret;
  103. }
  104. int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
  105. struct cros_ec_command *msg)
  106. {
  107. u8 *out;
  108. u8 csum;
  109. int i;
  110. if (ec_dev->proto_version > 2)
  111. return prepare_packet(ec_dev, msg);
  112. BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
  113. out = ec_dev->dout;
  114. out[0] = EC_CMD_VERSION0 + msg->version;
  115. out[1] = msg->command;
  116. out[2] = msg->outsize;
  117. csum = out[0] + out[1] + out[2];
  118. for (i = 0; i < msg->outsize; i++)
  119. csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
  120. out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
  121. return EC_MSG_TX_PROTO_BYTES + msg->outsize;
  122. }
  123. EXPORT_SYMBOL(cros_ec_prepare_tx);
  124. int cros_ec_check_result(struct cros_ec_device *ec_dev,
  125. struct cros_ec_command *msg)
  126. {
  127. switch (msg->result) {
  128. case EC_RES_SUCCESS:
  129. return 0;
  130. case EC_RES_IN_PROGRESS:
  131. dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
  132. msg->command);
  133. return -EAGAIN;
  134. default:
  135. dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
  136. msg->command, msg->result);
  137. return 0;
  138. }
  139. }
  140. EXPORT_SYMBOL(cros_ec_check_result);
  141. /*
  142. * cros_ec_get_host_event_wake_mask
  143. *
  144. * Get the mask of host events that cause wake from suspend.
  145. *
  146. * @ec_dev: EC device to call
  147. * @msg: message structure to use
  148. * @mask: result when function returns >=0.
  149. *
  150. * LOCKING:
  151. * the caller has ec_dev->lock mutex, or the caller knows there is
  152. * no other command in progress.
  153. */
  154. static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev,
  155. struct cros_ec_command *msg,
  156. uint32_t *mask)
  157. {
  158. struct ec_response_host_event_mask *r;
  159. int ret;
  160. msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
  161. msg->version = 0;
  162. msg->outsize = 0;
  163. msg->insize = sizeof(*r);
  164. ret = send_command(ec_dev, msg);
  165. if (ret > 0) {
  166. r = (struct ec_response_host_event_mask *)msg->data;
  167. *mask = r->mask;
  168. }
  169. return ret;
  170. }
  171. static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
  172. int devidx,
  173. struct cros_ec_command *msg)
  174. {
  175. /*
  176. * Try using v3+ to query for supported protocols. If this
  177. * command fails, fall back to v2. Returns the highest protocol
  178. * supported by the EC.
  179. * Also sets the max request/response/passthru size.
  180. */
  181. int ret;
  182. if (!ec_dev->pkt_xfer)
  183. return -EPROTONOSUPPORT;
  184. memset(msg, 0, sizeof(*msg));
  185. msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
  186. msg->insize = sizeof(struct ec_response_get_protocol_info);
  187. ret = send_command(ec_dev, msg);
  188. if (ret < 0) {
  189. dev_dbg(ec_dev->dev,
  190. "failed to check for EC[%d] protocol version: %d\n",
  191. devidx, ret);
  192. return ret;
  193. }
  194. if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
  195. return -ENODEV;
  196. else if (msg->result != EC_RES_SUCCESS)
  197. return msg->result;
  198. return 0;
  199. }
  200. static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
  201. {
  202. struct cros_ec_command *msg;
  203. struct ec_params_hello *hello_params;
  204. struct ec_response_hello *hello_response;
  205. int ret;
  206. int len = max(sizeof(*hello_params), sizeof(*hello_response));
  207. msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
  208. if (!msg)
  209. return -ENOMEM;
  210. msg->version = 0;
  211. msg->command = EC_CMD_HELLO;
  212. hello_params = (struct ec_params_hello *)msg->data;
  213. msg->outsize = sizeof(*hello_params);
  214. hello_response = (struct ec_response_hello *)msg->data;
  215. msg->insize = sizeof(*hello_response);
  216. hello_params->in_data = 0xa0b0c0d0;
  217. ret = send_command(ec_dev, msg);
  218. if (ret < 0) {
  219. dev_dbg(ec_dev->dev,
  220. "EC failed to respond to v2 hello: %d\n",
  221. ret);
  222. goto exit;
  223. } else if (msg->result != EC_RES_SUCCESS) {
  224. dev_err(ec_dev->dev,
  225. "EC responded to v2 hello with error: %d\n",
  226. msg->result);
  227. ret = msg->result;
  228. goto exit;
  229. } else if (hello_response->out_data != 0xa1b2c3d4) {
  230. dev_err(ec_dev->dev,
  231. "EC responded to v2 hello with bad result: %u\n",
  232. hello_response->out_data);
  233. ret = -EBADMSG;
  234. goto exit;
  235. }
  236. ret = 0;
  237. exit:
  238. kfree(msg);
  239. return ret;
  240. }
  241. /*
  242. * cros_ec_get_host_command_version_mask
  243. *
  244. * Get the version mask of a given command.
  245. *
  246. * @ec_dev: EC device to call
  247. * @msg: message structure to use
  248. * @cmd: command to get the version of.
  249. * @mask: result when function returns 0.
  250. *
  251. * @return 0 on success, error code otherwise
  252. *
  253. * LOCKING:
  254. * the caller has ec_dev->lock mutex or the caller knows there is
  255. * no other command in progress.
  256. */
  257. static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
  258. u16 cmd, u32 *mask)
  259. {
  260. struct ec_params_get_cmd_versions *pver;
  261. struct ec_response_get_cmd_versions *rver;
  262. struct cros_ec_command *msg;
  263. int ret;
  264. msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
  265. GFP_KERNEL);
  266. if (!msg)
  267. return -ENOMEM;
  268. msg->version = 0;
  269. msg->command = EC_CMD_GET_CMD_VERSIONS;
  270. msg->insize = sizeof(*rver);
  271. msg->outsize = sizeof(*pver);
  272. pver = (struct ec_params_get_cmd_versions *)msg->data;
  273. pver->cmd = cmd;
  274. ret = send_command(ec_dev, msg);
  275. if (ret > 0) {
  276. rver = (struct ec_response_get_cmd_versions *)msg->data;
  277. *mask = rver->version_mask;
  278. }
  279. kfree(msg);
  280. return ret;
  281. }
  282. int cros_ec_query_all(struct cros_ec_device *ec_dev)
  283. {
  284. struct device *dev = ec_dev->dev;
  285. struct cros_ec_command *proto_msg;
  286. struct ec_response_get_protocol_info *proto_info;
  287. u32 ver_mask = 0;
  288. int ret;
  289. proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
  290. GFP_KERNEL);
  291. if (!proto_msg)
  292. return -ENOMEM;
  293. /* First try sending with proto v3. */
  294. ec_dev->proto_version = 3;
  295. ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
  296. if (ret == 0) {
  297. proto_info = (struct ec_response_get_protocol_info *)
  298. proto_msg->data;
  299. ec_dev->max_request = proto_info->max_request_packet_size -
  300. sizeof(struct ec_host_request);
  301. ec_dev->max_response = proto_info->max_response_packet_size -
  302. sizeof(struct ec_host_response);
  303. ec_dev->proto_version =
  304. min(EC_HOST_REQUEST_VERSION,
  305. fls(proto_info->protocol_versions) - 1);
  306. dev_dbg(ec_dev->dev,
  307. "using proto v%u\n",
  308. ec_dev->proto_version);
  309. ec_dev->din_size = ec_dev->max_response +
  310. sizeof(struct ec_host_response) +
  311. EC_MAX_RESPONSE_OVERHEAD;
  312. ec_dev->dout_size = ec_dev->max_request +
  313. sizeof(struct ec_host_request) +
  314. EC_MAX_REQUEST_OVERHEAD;
  315. /*
  316. * Check for PD
  317. */
  318. ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
  319. if (ret) {
  320. dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
  321. ec_dev->max_passthru = 0;
  322. } else {
  323. dev_dbg(ec_dev->dev, "found PD chip\n");
  324. ec_dev->max_passthru =
  325. proto_info->max_request_packet_size -
  326. sizeof(struct ec_host_request);
  327. }
  328. } else {
  329. /* Try querying with a v2 hello message. */
  330. ec_dev->proto_version = 2;
  331. ret = cros_ec_host_command_proto_query_v2(ec_dev);
  332. if (ret == 0) {
  333. /* V2 hello succeeded. */
  334. dev_dbg(ec_dev->dev, "falling back to proto v2\n");
  335. ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
  336. ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
  337. ec_dev->max_passthru = 0;
  338. ec_dev->pkt_xfer = NULL;
  339. ec_dev->din_size = EC_PROTO2_MSG_BYTES;
  340. ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
  341. } else {
  342. /*
  343. * It's possible for a test to occur too early when
  344. * the EC isn't listening. If this happens, we'll
  345. * test later when the first command is run.
  346. */
  347. ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
  348. dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
  349. goto exit;
  350. }
  351. }
  352. devm_kfree(dev, ec_dev->din);
  353. devm_kfree(dev, ec_dev->dout);
  354. ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
  355. if (!ec_dev->din) {
  356. ret = -ENOMEM;
  357. goto exit;
  358. }
  359. ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
  360. if (!ec_dev->dout) {
  361. devm_kfree(dev, ec_dev->din);
  362. ret = -ENOMEM;
  363. goto exit;
  364. }
  365. /* Probe if MKBP event is supported */
  366. ret = cros_ec_get_host_command_version_mask(ec_dev,
  367. EC_CMD_GET_NEXT_EVENT,
  368. &ver_mask);
  369. if (ret < 0 || ver_mask == 0)
  370. ec_dev->mkbp_event_supported = 0;
  371. else
  372. ec_dev->mkbp_event_supported = 1;
  373. /*
  374. * Get host event wake mask, assume all events are wake events
  375. * if unavailable.
  376. */
  377. ret = cros_ec_get_host_event_wake_mask(ec_dev, proto_msg,
  378. &ec_dev->host_event_wake_mask);
  379. if (ret < 0)
  380. ec_dev->host_event_wake_mask = U32_MAX;
  381. ret = 0;
  382. exit:
  383. kfree(proto_msg);
  384. return ret;
  385. }
  386. EXPORT_SYMBOL(cros_ec_query_all);
  387. int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
  388. struct cros_ec_command *msg)
  389. {
  390. int ret;
  391. mutex_lock(&ec_dev->lock);
  392. if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
  393. ret = cros_ec_query_all(ec_dev);
  394. if (ret) {
  395. dev_err(ec_dev->dev,
  396. "EC version unknown and query failed; aborting command\n");
  397. mutex_unlock(&ec_dev->lock);
  398. return ret;
  399. }
  400. }
  401. if (msg->insize > ec_dev->max_response) {
  402. dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
  403. msg->insize = ec_dev->max_response;
  404. }
  405. if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
  406. if (msg->outsize > ec_dev->max_request) {
  407. dev_err(ec_dev->dev,
  408. "request of size %u is too big (max: %u)\n",
  409. msg->outsize,
  410. ec_dev->max_request);
  411. mutex_unlock(&ec_dev->lock);
  412. return -EMSGSIZE;
  413. }
  414. } else {
  415. if (msg->outsize > ec_dev->max_passthru) {
  416. dev_err(ec_dev->dev,
  417. "passthru rq of size %u is too big (max: %u)\n",
  418. msg->outsize,
  419. ec_dev->max_passthru);
  420. mutex_unlock(&ec_dev->lock);
  421. return -EMSGSIZE;
  422. }
  423. }
  424. ret = send_command(ec_dev, msg);
  425. mutex_unlock(&ec_dev->lock);
  426. return ret;
  427. }
  428. EXPORT_SYMBOL(cros_ec_cmd_xfer);
  429. int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
  430. struct cros_ec_command *msg)
  431. {
  432. int ret;
  433. ret = cros_ec_cmd_xfer(ec_dev, msg);
  434. if (ret < 0) {
  435. dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
  436. } else if (msg->result != EC_RES_SUCCESS) {
  437. dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
  438. return -EPROTO;
  439. }
  440. return ret;
  441. }
  442. EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
  443. static int get_next_event_xfer(struct cros_ec_device *ec_dev,
  444. struct cros_ec_command *msg,
  445. int version, uint32_t size)
  446. {
  447. int ret;
  448. msg->version = version;
  449. msg->command = EC_CMD_GET_NEXT_EVENT;
  450. msg->insize = size;
  451. msg->outsize = 0;
  452. ret = cros_ec_cmd_xfer(ec_dev, msg);
  453. if (ret > 0) {
  454. ec_dev->event_size = ret - 1;
  455. memcpy(&ec_dev->event_data, msg->data, ret);
  456. }
  457. return ret;
  458. }
  459. static int get_next_event(struct cros_ec_device *ec_dev)
  460. {
  461. u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
  462. struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
  463. static int cmd_version = 1;
  464. int ret;
  465. if (ec_dev->suspended) {
  466. dev_dbg(ec_dev->dev, "Device suspended.\n");
  467. return -EHOSTDOWN;
  468. }
  469. if (cmd_version == 1) {
  470. ret = get_next_event_xfer(ec_dev, msg, cmd_version,
  471. sizeof(struct ec_response_get_next_event_v1));
  472. if (ret < 0 || msg->result != EC_RES_INVALID_VERSION)
  473. return ret;
  474. /* Fallback to version 0 for future send attempts */
  475. cmd_version = 0;
  476. }
  477. ret = get_next_event_xfer(ec_dev, msg, cmd_version,
  478. sizeof(struct ec_response_get_next_event));
  479. return ret;
  480. }
  481. static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
  482. {
  483. u8 buffer[sizeof(struct cros_ec_command) +
  484. sizeof(ec_dev->event_data.data)];
  485. struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
  486. msg->version = 0;
  487. msg->command = EC_CMD_MKBP_STATE;
  488. msg->insize = sizeof(ec_dev->event_data.data);
  489. msg->outsize = 0;
  490. ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
  491. ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
  492. memcpy(&ec_dev->event_data.data, msg->data,
  493. sizeof(ec_dev->event_data.data));
  494. return ec_dev->event_size;
  495. }
  496. int cros_ec_get_next_event(struct cros_ec_device *ec_dev, bool *wake_event)
  497. {
  498. u8 event_type;
  499. u32 host_event;
  500. int ret;
  501. if (!ec_dev->mkbp_event_supported) {
  502. ret = get_keyboard_state_event(ec_dev);
  503. if (ret < 0)
  504. return ret;
  505. if (wake_event)
  506. *wake_event = true;
  507. return ret;
  508. }
  509. ret = get_next_event(ec_dev);
  510. if (ret < 0)
  511. return ret;
  512. if (wake_event) {
  513. event_type = ec_dev->event_data.event_type;
  514. host_event = cros_ec_get_host_event(ec_dev);
  515. /*
  516. * Sensor events need to be parsed by the sensor sub-device.
  517. * Defer them, and don't report the wakeup here.
  518. */
  519. if (event_type == EC_MKBP_EVENT_SENSOR_FIFO)
  520. *wake_event = false;
  521. /* Masked host-events should not count as wake events. */
  522. else if (host_event &&
  523. !(host_event & ec_dev->host_event_wake_mask))
  524. *wake_event = false;
  525. /* Consider all other events as wake events. */
  526. else
  527. *wake_event = true;
  528. }
  529. return ret;
  530. }
  531. EXPORT_SYMBOL(cros_ec_get_next_event);
  532. u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
  533. {
  534. u32 host_event;
  535. BUG_ON(!ec_dev->mkbp_event_supported);
  536. if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
  537. return 0;
  538. if (ec_dev->event_size != sizeof(host_event)) {
  539. dev_warn(ec_dev->dev, "Invalid host event size\n");
  540. return 0;
  541. }
  542. host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
  543. return host_event;
  544. }
  545. EXPORT_SYMBOL(cros_ec_get_host_event);