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