cros_ec_proto.c 15 KB

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