cros_ec_proto.c 13 KB

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