cros_ec_proto.c 14 KB

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