cros_ec_proto.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #define EC_COMMAND_RETRIES 50
  22. static int prepare_packet(struct cros_ec_device *ec_dev,
  23. struct cros_ec_command *msg)
  24. {
  25. struct ec_host_request *request;
  26. u8 *out;
  27. int i;
  28. u8 csum = 0;
  29. BUG_ON(ec_dev->proto_version != EC_HOST_REQUEST_VERSION);
  30. BUG_ON(msg->outsize + sizeof(*request) > ec_dev->dout_size);
  31. out = ec_dev->dout;
  32. request = (struct ec_host_request *)out;
  33. request->struct_version = EC_HOST_REQUEST_VERSION;
  34. request->checksum = 0;
  35. request->command = msg->command;
  36. request->command_version = msg->version;
  37. request->reserved = 0;
  38. request->data_len = msg->outsize;
  39. for (i = 0; i < sizeof(*request); i++)
  40. csum += out[i];
  41. /* Copy data and update checksum */
  42. memcpy(out + sizeof(*request), msg->data, msg->outsize);
  43. for (i = 0; i < msg->outsize; i++)
  44. csum += msg->data[i];
  45. request->checksum = -csum;
  46. return sizeof(*request) + msg->outsize;
  47. }
  48. static int send_command(struct cros_ec_device *ec_dev,
  49. struct cros_ec_command *msg)
  50. {
  51. int ret;
  52. if (ec_dev->proto_version > 2)
  53. ret = ec_dev->pkt_xfer(ec_dev, msg);
  54. else
  55. ret = ec_dev->cmd_xfer(ec_dev, msg);
  56. if (msg->result == EC_RES_IN_PROGRESS) {
  57. int i;
  58. struct cros_ec_command *status_msg;
  59. struct ec_response_get_comms_status *status;
  60. status_msg = kmalloc(sizeof(*status_msg) + sizeof(*status),
  61. GFP_KERNEL);
  62. if (!status_msg)
  63. return -ENOMEM;
  64. status_msg->version = 0;
  65. status_msg->command = EC_CMD_GET_COMMS_STATUS;
  66. status_msg->insize = sizeof(*status);
  67. status_msg->outsize = 0;
  68. /*
  69. * Query the EC's status until it's no longer busy or
  70. * we encounter an error.
  71. */
  72. for (i = 0; i < EC_COMMAND_RETRIES; i++) {
  73. usleep_range(10000, 11000);
  74. ret = ec_dev->cmd_xfer(ec_dev, status_msg);
  75. if (ret < 0)
  76. break;
  77. msg->result = status_msg->result;
  78. if (status_msg->result != EC_RES_SUCCESS)
  79. break;
  80. status = (struct ec_response_get_comms_status *)
  81. status_msg->data;
  82. if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
  83. break;
  84. }
  85. kfree(status_msg);
  86. }
  87. return ret;
  88. }
  89. int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
  90. struct cros_ec_command *msg)
  91. {
  92. u8 *out;
  93. u8 csum;
  94. int i;
  95. if (ec_dev->proto_version > 2)
  96. return prepare_packet(ec_dev, msg);
  97. BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
  98. out = ec_dev->dout;
  99. out[0] = EC_CMD_VERSION0 + msg->version;
  100. out[1] = msg->command;
  101. out[2] = msg->outsize;
  102. csum = out[0] + out[1] + out[2];
  103. for (i = 0; i < msg->outsize; i++)
  104. csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
  105. out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
  106. return EC_MSG_TX_PROTO_BYTES + msg->outsize;
  107. }
  108. EXPORT_SYMBOL(cros_ec_prepare_tx);
  109. int cros_ec_check_result(struct cros_ec_device *ec_dev,
  110. struct cros_ec_command *msg)
  111. {
  112. switch (msg->result) {
  113. case EC_RES_SUCCESS:
  114. return 0;
  115. case EC_RES_IN_PROGRESS:
  116. dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
  117. msg->command);
  118. return -EAGAIN;
  119. default:
  120. dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
  121. msg->command, msg->result);
  122. return 0;
  123. }
  124. }
  125. EXPORT_SYMBOL(cros_ec_check_result);
  126. static int cros_ec_host_command_proto_query(struct cros_ec_device *ec_dev,
  127. int devidx,
  128. struct cros_ec_command *msg)
  129. {
  130. /*
  131. * Try using v3+ to query for supported protocols. If this
  132. * command fails, fall back to v2. Returns the highest protocol
  133. * supported by the EC.
  134. * Also sets the max request/response/passthru size.
  135. */
  136. int ret;
  137. if (!ec_dev->pkt_xfer)
  138. return -EPROTONOSUPPORT;
  139. memset(msg, 0, sizeof(*msg));
  140. msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
  141. msg->insize = sizeof(struct ec_response_get_protocol_info);
  142. ret = send_command(ec_dev, msg);
  143. if (ret < 0) {
  144. dev_dbg(ec_dev->dev,
  145. "failed to check for EC[%d] protocol version: %d\n",
  146. devidx, ret);
  147. return ret;
  148. }
  149. if (devidx > 0 && msg->result == EC_RES_INVALID_COMMAND)
  150. return -ENODEV;
  151. else if (msg->result != EC_RES_SUCCESS)
  152. return msg->result;
  153. return 0;
  154. }
  155. static int cros_ec_host_command_proto_query_v2(struct cros_ec_device *ec_dev)
  156. {
  157. struct cros_ec_command *msg;
  158. struct ec_params_hello *hello_params;
  159. struct ec_response_hello *hello_response;
  160. int ret;
  161. int len = max(sizeof(*hello_params), sizeof(*hello_response));
  162. msg = kmalloc(sizeof(*msg) + len, GFP_KERNEL);
  163. if (!msg)
  164. return -ENOMEM;
  165. msg->version = 0;
  166. msg->command = EC_CMD_HELLO;
  167. hello_params = (struct ec_params_hello *)msg->data;
  168. msg->outsize = sizeof(*hello_params);
  169. hello_response = (struct ec_response_hello *)msg->data;
  170. msg->insize = sizeof(*hello_response);
  171. hello_params->in_data = 0xa0b0c0d0;
  172. ret = send_command(ec_dev, msg);
  173. if (ret < 0) {
  174. dev_dbg(ec_dev->dev,
  175. "EC failed to respond to v2 hello: %d\n",
  176. ret);
  177. goto exit;
  178. } else if (msg->result != EC_RES_SUCCESS) {
  179. dev_err(ec_dev->dev,
  180. "EC responded to v2 hello with error: %d\n",
  181. msg->result);
  182. ret = msg->result;
  183. goto exit;
  184. } else if (hello_response->out_data != 0xa1b2c3d4) {
  185. dev_err(ec_dev->dev,
  186. "EC responded to v2 hello with bad result: %u\n",
  187. hello_response->out_data);
  188. ret = -EBADMSG;
  189. goto exit;
  190. }
  191. ret = 0;
  192. exit:
  193. kfree(msg);
  194. return ret;
  195. }
  196. int cros_ec_query_all(struct cros_ec_device *ec_dev)
  197. {
  198. struct device *dev = ec_dev->dev;
  199. struct cros_ec_command *proto_msg;
  200. struct ec_response_get_protocol_info *proto_info;
  201. int ret;
  202. proto_msg = kzalloc(sizeof(*proto_msg) + sizeof(*proto_info),
  203. GFP_KERNEL);
  204. if (!proto_msg)
  205. return -ENOMEM;
  206. /* First try sending with proto v3. */
  207. ec_dev->proto_version = 3;
  208. ret = cros_ec_host_command_proto_query(ec_dev, 0, proto_msg);
  209. if (ret == 0) {
  210. proto_info = (struct ec_response_get_protocol_info *)
  211. proto_msg->data;
  212. ec_dev->max_request = proto_info->max_request_packet_size -
  213. sizeof(struct ec_host_request);
  214. ec_dev->max_response = proto_info->max_response_packet_size -
  215. sizeof(struct ec_host_response);
  216. ec_dev->proto_version =
  217. min(EC_HOST_REQUEST_VERSION,
  218. fls(proto_info->protocol_versions) - 1);
  219. dev_dbg(ec_dev->dev,
  220. "using proto v%u\n",
  221. ec_dev->proto_version);
  222. ec_dev->din_size = ec_dev->max_response +
  223. sizeof(struct ec_host_response) +
  224. EC_MAX_RESPONSE_OVERHEAD;
  225. ec_dev->dout_size = ec_dev->max_request +
  226. sizeof(struct ec_host_request) +
  227. EC_MAX_REQUEST_OVERHEAD;
  228. /*
  229. * Check for PD
  230. */
  231. ret = cros_ec_host_command_proto_query(ec_dev, 1, proto_msg);
  232. if (ret) {
  233. dev_dbg(ec_dev->dev, "no PD chip found: %d\n", ret);
  234. ec_dev->max_passthru = 0;
  235. } else {
  236. dev_dbg(ec_dev->dev, "found PD chip\n");
  237. ec_dev->max_passthru =
  238. proto_info->max_request_packet_size -
  239. sizeof(struct ec_host_request);
  240. }
  241. } else {
  242. /* Try querying with a v2 hello message. */
  243. ec_dev->proto_version = 2;
  244. ret = cros_ec_host_command_proto_query_v2(ec_dev);
  245. if (ret == 0) {
  246. /* V2 hello succeeded. */
  247. dev_dbg(ec_dev->dev, "falling back to proto v2\n");
  248. ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
  249. ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
  250. ec_dev->max_passthru = 0;
  251. ec_dev->pkt_xfer = NULL;
  252. ec_dev->din_size = EC_MSG_BYTES;
  253. ec_dev->dout_size = EC_MSG_BYTES;
  254. } else {
  255. /*
  256. * It's possible for a test to occur too early when
  257. * the EC isn't listening. If this happens, we'll
  258. * test later when the first command is run.
  259. */
  260. ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
  261. dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
  262. goto exit;
  263. }
  264. }
  265. devm_kfree(dev, ec_dev->din);
  266. devm_kfree(dev, ec_dev->dout);
  267. ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
  268. if (!ec_dev->din) {
  269. ret = -ENOMEM;
  270. goto exit;
  271. }
  272. ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
  273. if (!ec_dev->dout) {
  274. devm_kfree(dev, ec_dev->din);
  275. ret = -ENOMEM;
  276. goto exit;
  277. }
  278. exit:
  279. kfree(proto_msg);
  280. return ret;
  281. }
  282. EXPORT_SYMBOL(cros_ec_query_all);
  283. int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
  284. struct cros_ec_command *msg)
  285. {
  286. int ret;
  287. mutex_lock(&ec_dev->lock);
  288. if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
  289. ret = cros_ec_query_all(ec_dev);
  290. if (ret) {
  291. dev_err(ec_dev->dev,
  292. "EC version unknown and query failed; aborting command\n");
  293. mutex_unlock(&ec_dev->lock);
  294. return ret;
  295. }
  296. }
  297. if (msg->insize > ec_dev->max_response) {
  298. dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
  299. msg->insize = ec_dev->max_response;
  300. }
  301. if (msg->command < EC_CMD_PASSTHRU_OFFSET(1)) {
  302. if (msg->outsize > ec_dev->max_request) {
  303. dev_err(ec_dev->dev,
  304. "request of size %u is too big (max: %u)\n",
  305. msg->outsize,
  306. ec_dev->max_request);
  307. mutex_unlock(&ec_dev->lock);
  308. return -EMSGSIZE;
  309. }
  310. } else {
  311. if (msg->outsize > ec_dev->max_passthru) {
  312. dev_err(ec_dev->dev,
  313. "passthru rq of size %u is too big (max: %u)\n",
  314. msg->outsize,
  315. ec_dev->max_passthru);
  316. mutex_unlock(&ec_dev->lock);
  317. return -EMSGSIZE;
  318. }
  319. }
  320. ret = send_command(ec_dev, msg);
  321. mutex_unlock(&ec_dev->lock);
  322. return ret;
  323. }
  324. EXPORT_SYMBOL(cros_ec_cmd_xfer);