cros_ec_proto.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
  128. int devidx,
  129. struct cros_ec_command *msg)
  130. {
  131. /*
  132. * Try using v3+ to query for supported protocols. If this
  133. * command fails, fall back to v2. Returns the highest protocol
  134. * supported by the EC.
  135. * Also sets the max request/response/passthru size.
  136. */
  137. int ret;
  138. if (!ec_dev->pkt_xfer)
  139. return -EPROTONOSUPPORT;
  140. memset(msg, 0, sizeof(*msg));
  141. msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
  142. msg->insize = sizeof(struct ec_response_get_protocol_info);
  143. ret = send_command(ec_dev, msg);
  144. if (ret < 0) {
  145. dev_dbg(ec_dev->dev,
  146. "failed to check for EC[%d] protocol version: %d\n",
  147. devidx, ret);
  148. return ret;
  149. }
  150. if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
  151. return -ENODEV;
  152. else if (msg->result != EC_RES_SUCCESS)
  153. return msg->result;
  154. return 0;
  155. }
  156. static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
  157. {
  158. struct cros_ec_command *msg;
  159. struct ec_params_hello *hello_params;
  160. struct ec_response_hello *hello_response;
  161. int ret;
  162. int len = max(sizeof(*hello_params), sizeof(*hello_response));
  163. msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
  164. if (!msg)
  165. return -ENOMEM;
  166. msg->version = 0;
  167. msg->command = EC_CMD_HELLO;
  168. hello_params = (struct ec_params_hello *)msg->data;
  169. msg->outsize = sizeof(*hello_params);
  170. hello_response = (struct ec_response_hello *)msg->data;
  171. msg->insize = sizeof(*hello_response);
  172. hello_params->in_data = 0xa0b0c0d0;
  173. ret = send_command(ec_dev, msg);
  174. if (ret < 0) {
  175. dev_dbg(ec_dev->dev,
  176. "EC failed to respond to v2 hello: %d\n",
  177. ret);
  178. goto exit;
  179. } else if (msg->result != EC_RES_SUCCESS) {
  180. dev_err(ec_dev->dev,
  181. "EC responded to v2 hello with error: %d\n",
  182. msg->result);
  183. ret = msg->result;
  184. goto exit;
  185. } else if (hello_response->out_data != 0xa1b2c3d4) {
  186. dev_err(ec_dev->dev,
  187. "EC responded to v2 hello with bad result: %u\n",
  188. hello_response->out_data);
  189. ret = -EBADMSG;
  190. goto exit;
  191. }
  192. ret = 0;
  193. exit:
  194. kfree(msg);
  195. return ret;
  196. }
  197. static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev,
  198. u16 cmd, u32 *mask)
  199. {
  200. struct ec_params_get_cmd_versions *pver;
  201. struct ec_response_get_cmd_versions *rver;
  202. struct cros_ec_command *msg;
  203. int ret;
  204. msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
  205. GFP_KERNEL);
  206. if (!msg)
  207. return -ENOMEM;
  208. msg->version = 0;
  209. msg->command = EC_CMD_GET_CMD_VERSIONS;
  210. msg->insize = sizeof(*rver);
  211. msg->outsize = sizeof(*pver);
  212. pver = (struct ec_params_get_cmd_versions *)msg->data;
  213. pver->cmd = cmd;
  214. ret = cros_ec_cmd_xfer(ec_dev, msg);
  215. if (ret > 0) {
  216. rver = (struct ec_response_get_cmd_versions *)msg->data;
  217. *mask = rver->version_mask;
  218. }
  219. kfree(msg);
  220. return ret;
  221. }
  222. int cros_ec_query_all(struct cros_ec_device *ec_dev)
  223. {
  224. struct device *dev = ec_dev->dev;
  225. struct cros_ec_command *proto_msg;
  226. struct ec_response_get_protocol_info *proto_info;
  227. u32 ver_mask = 0;
  228. int ret;
  229. proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
  230. GFP_KERNEL);
  231. if (!proto_msg)
  232. return -ENOMEM;
  233. /* First try sending with proto v3. */
  234. ec_dev->proto_version = 3;
  235. ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
  236. if (ret == 0) {
  237. proto_info = (struct ec_response_get_protocol_info *)
  238. proto_msg->data;
  239. ec_dev->max_request = proto_info->max_request_packet_size -
  240. sizeof(struct ec_host_request);
  241. ec_dev->max_response = proto_info->max_response_packet_size -
  242. sizeof(struct ec_host_response);
  243. ec_dev->proto_version =
  244. min(EC_HOST_REQUEST_VERSION,
  245. fls(proto_info->protocol_versions) - 1);
  246. dev_dbg(ec_dev->dev,
  247. "using proto v%u\n",
  248. ec_dev->proto_version);
  249. ec_dev->din_size = ec_dev->max_response +
  250. sizeof(struct ec_host_response) +
  251. EC_MAX_RESPONSE_OVERHEAD;
  252. ec_dev->dout_size = ec_dev->max_request +
  253. sizeof(struct ec_host_request) +
  254. EC_MAX_REQUEST_OVERHEAD;
  255. /*
  256. * Check for PD
  257. */
  258. ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
  259. if (ret) {
  260. dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
  261. ec_dev->max_passthru = 0;
  262. } else {
  263. dev_dbg(ec_dev->dev, "found PD chip\n");
  264. ec_dev->max_passthru =
  265. proto_info->max_request_packet_size -
  266. sizeof(struct ec_host_request);
  267. }
  268. } else {
  269. /* Try querying with a v2 hello message. */
  270. ec_dev->proto_version = 2;
  271. ret = cros_ec_host_command_proto_query_v2(ec_dev);
  272. if (ret == 0) {
  273. /* V2 hello succeeded. */
  274. dev_dbg(ec_dev->dev, "falling back to proto v2\n");
  275. ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
  276. ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
  277. ec_dev->max_passthru = 0;
  278. ec_dev->pkt_xfer = NULL;
  279. ec_dev->din_size = EC_PROTO2_MSG_BYTES;
  280. ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
  281. } else {
  282. /*
  283. * It's possible for a test to occur too early when
  284. * the EC isn't listening. If this happens, we'll
  285. * test later when the first command is run.
  286. */
  287. ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
  288. dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
  289. goto exit;
  290. }
  291. }
  292. devm_kfree(dev, ec_dev->din);
  293. devm_kfree(dev, ec_dev->dout);
  294. ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
  295. if (!ec_dev->din) {
  296. ret = -ENOMEM;
  297. goto exit;
  298. }
  299. ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
  300. if (!ec_dev->dout) {
  301. devm_kfree(dev, ec_dev->din);
  302. ret = -ENOMEM;
  303. goto exit;
  304. }
  305. /* Probe if MKBP event is supported */
  306. ret = cros_ec_get_host_command_version_mask(ec_dev,
  307. EC_CMD_GET_NEXT_EVENT,
  308. &ver_mask);
  309. if (ret < 0 || ver_mask == 0)
  310. ec_dev->mkbp_event_supported = 0;
  311. else
  312. ec_dev->mkbp_event_supported = 1;
  313. exit:
  314. kfree(proto_msg);
  315. return ret;
  316. }
  317. EXPORT_SYMBOL(cros_ec_query_all);
  318. int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
  319. struct cros_ec_command *msg)
  320. {
  321. int ret;
  322. mutex_lock(&ec_dev->lock);
  323. if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
  324. ret = cros_ec_query_all(ec_dev);
  325. if (ret) {
  326. dev_err(ec_dev->dev,
  327. "EC version unknown and query failed; aborting command\n");
  328. mutex_unlock(&ec_dev->lock);
  329. return ret;
  330. }
  331. }
  332. if (msg->insize > ec_dev->max_response) {
  333. dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
  334. msg->insize = ec_dev->max_response;
  335. }
  336. if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
  337. if (msg->outsize > ec_dev->max_request) {
  338. dev_err(ec_dev->dev,
  339. "request of size %u is too big (max: %u)\n",
  340. msg->outsize,
  341. ec_dev->max_request);
  342. mutex_unlock(&ec_dev->lock);
  343. return -EMSGSIZE;
  344. }
  345. } else {
  346. if (msg->outsize > ec_dev->max_passthru) {
  347. dev_err(ec_dev->dev,
  348. "passthru rq of size %u is too big (max: %u)\n",
  349. msg->outsize,
  350. ec_dev->max_passthru);
  351. mutex_unlock(&ec_dev->lock);
  352. return -EMSGSIZE;
  353. }
  354. }
  355. ret = send_command(ec_dev, msg);
  356. mutex_unlock(&ec_dev->lock);
  357. return ret;
  358. }
  359. EXPORT_SYMBOL(cros_ec_cmd_xfer);
  360. int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
  361. struct cros_ec_command *msg)
  362. {
  363. int ret;
  364. ret = cros_ec_cmd_xfer(ec_dev, msg);
  365. if (ret < 0) {
  366. dev_err(ec_dev->dev, "Command xfer error (err:%d)\n", ret);
  367. } else if (msg->result != EC_RES_SUCCESS) {
  368. dev_dbg(ec_dev->dev, "Command result (err: %d)\n", msg->result);
  369. return -EPROTO;
  370. }
  371. return ret;
  372. }
  373. EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
  374. static int get_next_event(struct cros_ec_device *ec_dev)
  375. {
  376. u8 buffer[sizeof(struct cros_ec_command) + sizeof(ec_dev->event_data)];
  377. struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
  378. int ret;
  379. msg->version = 0;
  380. msg->command = EC_CMD_GET_NEXT_EVENT;
  381. msg->insize = sizeof(ec_dev->event_data);
  382. msg->outsize = 0;
  383. ret = cros_ec_cmd_xfer(ec_dev, msg);
  384. if (ret > 0) {
  385. ec_dev->event_size = ret - 1;
  386. memcpy(&ec_dev->event_data, msg->data,
  387. sizeof(ec_dev->event_data));
  388. }
  389. return ret;
  390. }
  391. static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
  392. {
  393. u8 buffer[sizeof(struct cros_ec_command) +
  394. sizeof(ec_dev->event_data.data)];
  395. struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
  396. msg->version = 0;
  397. msg->command = EC_CMD_MKBP_STATE;
  398. msg->insize = sizeof(ec_dev->event_data.data);
  399. msg->outsize = 0;
  400. ec_dev->event_size = cros_ec_cmd_xfer(ec_dev, msg);
  401. ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
  402. memcpy(&ec_dev->event_data.data, msg->data,
  403. sizeof(ec_dev->event_data.data));
  404. return ec_dev->event_size;
  405. }
  406. int cros_ec_get_next_event(struct cros_ec_device *ec_dev)
  407. {
  408. if (ec_dev->mkbp_event_supported)
  409. return get_next_event(ec_dev);
  410. else
  411. return get_keyboard_state_event(ec_dev);
  412. }
  413. EXPORT_SYMBOL(cros_ec_get_next_event);