cros_ec_proto.c 15 KB

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