cros_ec_proto.c 12 KB

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