cros_ec_proto.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. // SPDX-License-Identifier: GPL-2.0
  2. // ChromeOS EC communication protocol helper functions
  3. //
  4. // Copyright (C) 2015 Google, Inc
  5. #include <linux/delay.h>
  6. #include <linux/device.h>
  7. #include <linux/limits.h>
  8. #include <linux/module.h>
  9. #include <linux/platform_data/cros_ec_commands.h>
  10. #include <linux/platform_data/cros_ec_proto.h>
  11. #include <linux/slab.h>
  12. #include <linux/unaligned.h>
  13. #include "cros_ec_trace.h"
  14. #define EC_COMMAND_RETRIES 50
  15. static const int cros_ec_error_map[] = {
  16. [EC_RES_INVALID_COMMAND] = -EOPNOTSUPP,
  17. [EC_RES_ERROR] = -EIO,
  18. [EC_RES_INVALID_PARAM] = -EINVAL,
  19. [EC_RES_ACCESS_DENIED] = -EACCES,
  20. [EC_RES_INVALID_RESPONSE] = -EPROTO,
  21. [EC_RES_INVALID_VERSION] = -ENOPROTOOPT,
  22. [EC_RES_INVALID_CHECKSUM] = -EBADMSG,
  23. [EC_RES_IN_PROGRESS] = -EINPROGRESS,
  24. [EC_RES_UNAVAILABLE] = -ENODATA,
  25. [EC_RES_TIMEOUT] = -ETIMEDOUT,
  26. [EC_RES_OVERFLOW] = -EOVERFLOW,
  27. [EC_RES_INVALID_HEADER] = -EBADR,
  28. [EC_RES_REQUEST_TRUNCATED] = -EBADR,
  29. [EC_RES_RESPONSE_TOO_BIG] = -EFBIG,
  30. [EC_RES_BUS_ERROR] = -EFAULT,
  31. [EC_RES_BUSY] = -EBUSY,
  32. [EC_RES_INVALID_HEADER_VERSION] = -EBADMSG,
  33. [EC_RES_INVALID_HEADER_CRC] = -EBADMSG,
  34. [EC_RES_INVALID_DATA_CRC] = -EBADMSG,
  35. [EC_RES_DUP_UNAVAILABLE] = -ENODATA,
  36. };
  37. static int cros_ec_map_error(uint32_t result)
  38. {
  39. int ret = 0;
  40. if (result != EC_RES_SUCCESS) {
  41. if (result < ARRAY_SIZE(cros_ec_error_map) && cros_ec_error_map[result])
  42. ret = cros_ec_error_map[result];
  43. else
  44. ret = -EPROTO;
  45. }
  46. return ret;
  47. }
  48. static int prepare_tx(struct cros_ec_device *ec_dev,
  49. struct cros_ec_command *msg)
  50. {
  51. struct ec_host_request *request;
  52. u8 *out;
  53. int i;
  54. u8 csum = 0;
  55. if (msg->outsize + sizeof(*request) > ec_dev->dout_size)
  56. return -EINVAL;
  57. out = ec_dev->dout;
  58. request = (struct ec_host_request *)out;
  59. request->struct_version = EC_HOST_REQUEST_VERSION;
  60. request->checksum = 0;
  61. request->command = msg->command;
  62. request->command_version = msg->version;
  63. request->reserved = 0;
  64. request->data_len = msg->outsize;
  65. for (i = 0; i < sizeof(*request); i++)
  66. csum += out[i];
  67. /* Copy data and update checksum */
  68. memcpy(out + sizeof(*request), msg->data, msg->outsize);
  69. for (i = 0; i < msg->outsize; i++)
  70. csum += msg->data[i];
  71. request->checksum = -csum;
  72. return sizeof(*request) + msg->outsize;
  73. }
  74. static int prepare_tx_legacy(struct cros_ec_device *ec_dev,
  75. struct cros_ec_command *msg)
  76. {
  77. u8 *out;
  78. u8 csum;
  79. int i;
  80. if (msg->outsize > EC_PROTO2_MAX_PARAM_SIZE)
  81. return -EINVAL;
  82. out = ec_dev->dout;
  83. out[0] = EC_CMD_VERSION0 + msg->version;
  84. out[1] = msg->command;
  85. out[2] = msg->outsize;
  86. csum = out[0] + out[1] + out[2];
  87. for (i = 0; i < msg->outsize; i++)
  88. csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->data[i];
  89. out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = csum;
  90. return EC_MSG_TX_PROTO_BYTES + msg->outsize;
  91. }
  92. static int cros_ec_xfer_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
  93. {
  94. int ret;
  95. int (*xfer_fxn)(struct cros_ec_device *ec, struct cros_ec_command *msg);
  96. if (ec_dev->proto_version > 2)
  97. xfer_fxn = ec_dev->pkt_xfer;
  98. else
  99. xfer_fxn = ec_dev->cmd_xfer;
  100. if (!xfer_fxn) {
  101. /*
  102. * This error can happen if a communication error happened and
  103. * the EC is trying to use protocol v2, on an underlying
  104. * communication mechanism that does not support v2.
  105. */
  106. dev_err_once(ec_dev->dev, "missing EC transfer API, cannot send command\n");
  107. return -EIO;
  108. }
  109. trace_cros_ec_request_start(msg);
  110. ret = (*xfer_fxn)(ec_dev, msg);
  111. trace_cros_ec_request_done(msg, ret);
  112. return ret;
  113. }
  114. static int cros_ec_wait_until_complete(struct cros_ec_device *ec_dev, uint32_t *result)
  115. {
  116. struct {
  117. struct cros_ec_command msg;
  118. struct ec_response_get_comms_status status;
  119. } __packed buf;
  120. struct cros_ec_command *msg = &buf.msg;
  121. struct ec_response_get_comms_status *status = &buf.status;
  122. int ret = 0, i;
  123. msg->version = 0;
  124. msg->command = EC_CMD_GET_COMMS_STATUS;
  125. msg->insize = sizeof(*status);
  126. msg->outsize = 0;
  127. /* Query the EC's status until it's no longer busy or we encounter an error. */
  128. for (i = 0; i < EC_COMMAND_RETRIES; ++i) {
  129. usleep_range(10000, 11000);
  130. ret = cros_ec_xfer_command(ec_dev, msg);
  131. if (ret == -EAGAIN)
  132. continue;
  133. if (ret < 0)
  134. return ret;
  135. *result = msg->result;
  136. if (msg->result != EC_RES_SUCCESS)
  137. return ret;
  138. if (ret == 0) {
  139. ret = -EPROTO;
  140. break;
  141. }
  142. if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
  143. return ret;
  144. }
  145. if (i >= EC_COMMAND_RETRIES)
  146. ret = -EAGAIN;
  147. return ret;
  148. }
  149. static int cros_ec_send_command(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
  150. {
  151. int ret = cros_ec_xfer_command(ec_dev, msg);
  152. if (msg->result == EC_RES_IN_PROGRESS)
  153. ret = cros_ec_wait_until_complete(ec_dev, &msg->result);
  154. return ret;
  155. }
  156. /**
  157. * cros_ec_prepare_tx() - Prepare an outgoing message in the output buffer.
  158. * @ec_dev: Device to register.
  159. * @msg: Message to write.
  160. *
  161. * This is used by all ChromeOS EC drivers to prepare the outgoing message
  162. * according to different protocol versions.
  163. *
  164. * Return: number of prepared bytes on success or negative error code.
  165. */
  166. int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
  167. struct cros_ec_command *msg)
  168. {
  169. if (ec_dev->proto_version > 2)
  170. return prepare_tx(ec_dev, msg);
  171. return prepare_tx_legacy(ec_dev, msg);
  172. }
  173. EXPORT_SYMBOL(cros_ec_prepare_tx);
  174. /**
  175. * cros_ec_check_result() - Check ec_msg->result.
  176. * @ec_dev: EC device.
  177. * @msg: Message to check.
  178. *
  179. * This is used by ChromeOS EC drivers to check the ec_msg->result for
  180. * EC_RES_IN_PROGRESS and to warn about them.
  181. *
  182. * The function should not check for furthermore error codes. Otherwise,
  183. * it would break the ABI.
  184. *
  185. * Return: -EAGAIN if ec_msg->result == EC_RES_IN_PROGRESS. Otherwise, 0.
  186. */
  187. int cros_ec_check_result(struct cros_ec_device *ec_dev,
  188. struct cros_ec_command *msg)
  189. {
  190. switch (msg->result) {
  191. case EC_RES_SUCCESS:
  192. return 0;
  193. case EC_RES_IN_PROGRESS:
  194. dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
  195. msg->command);
  196. return -EAGAIN;
  197. default:
  198. dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
  199. msg->command, msg->result);
  200. return 0;
  201. }
  202. }
  203. EXPORT_SYMBOL(cros_ec_check_result);
  204. /**
  205. * cros_ec_get_host_event_wake_mask
  206. *
  207. * Get the mask of host events that cause wake from suspend.
  208. *
  209. * @ec_dev: EC device to call
  210. * @mask: result when function returns 0.
  211. *
  212. * LOCKING:
  213. * the caller has ec_dev->lock mutex, or the caller knows there is
  214. * no other command in progress.
  215. */
  216. static int cros_ec_get_host_event_wake_mask(struct cros_ec_device *ec_dev, uint32_t *mask)
  217. {
  218. struct cros_ec_command *msg;
  219. struct ec_response_host_event_mask *r;
  220. int ret, mapped;
  221. msg = kzalloc(sizeof(*msg) + sizeof(*r), GFP_KERNEL);
  222. if (!msg)
  223. return -ENOMEM;
  224. msg->command = EC_CMD_HOST_EVENT_GET_WAKE_MASK;
  225. msg->insize = sizeof(*r);
  226. ret = cros_ec_send_command(ec_dev, msg);
  227. if (ret < 0)
  228. goto exit;
  229. mapped = cros_ec_map_error(msg->result);
  230. if (mapped) {
  231. ret = mapped;
  232. goto exit;
  233. }
  234. if (ret == 0) {
  235. ret = -EPROTO;
  236. goto exit;
  237. }
  238. r = (struct ec_response_host_event_mask *)msg->data;
  239. *mask = r->mask;
  240. ret = 0;
  241. exit:
  242. kfree(msg);
  243. return ret;
  244. }
  245. static int cros_ec_get_proto_info(struct cros_ec_device *ec_dev, int devidx)
  246. {
  247. struct cros_ec_command *msg;
  248. struct ec_response_get_protocol_info *info;
  249. int ret, mapped;
  250. ec_dev->proto_version = 3;
  251. if (devidx > 0)
  252. ec_dev->max_passthru = 0;
  253. msg = kzalloc(sizeof(*msg) + sizeof(*info), GFP_KERNEL);
  254. if (!msg)
  255. return -ENOMEM;
  256. msg->command = EC_CMD_PASSTHRU_OFFSET(devidx) | EC_CMD_GET_PROTOCOL_INFO;
  257. msg->insize = sizeof(*info);
  258. ret = cros_ec_send_command(ec_dev, msg);
  259. /*
  260. * Send command once again when timeout occurred.
  261. * Fingerprint MCU (FPMCU) is restarted during system boot which
  262. * introduces small window in which FPMCU won't respond for any
  263. * messages sent by kernel. There is no need to wait before next
  264. * attempt because we waited at least EC_MSG_DEADLINE_MS.
  265. */
  266. if (ret == -ETIMEDOUT)
  267. ret = cros_ec_send_command(ec_dev, msg);
  268. if (ret < 0) {
  269. dev_dbg(ec_dev->dev,
  270. "failed to check for EC[%d] protocol version: %d\n",
  271. devidx, ret);
  272. goto exit;
  273. }
  274. mapped = cros_ec_map_error(msg->result);
  275. if (mapped) {
  276. ret = mapped;
  277. goto exit;
  278. }
  279. if (ret == 0) {
  280. ret = -EPROTO;
  281. goto exit;
  282. }
  283. info = (struct ec_response_get_protocol_info *)msg->data;
  284. switch (devidx) {
  285. case CROS_EC_DEV_EC_INDEX:
  286. ec_dev->max_request = info->max_request_packet_size -
  287. sizeof(struct ec_host_request);
  288. ec_dev->max_response = info->max_response_packet_size -
  289. sizeof(struct ec_host_response);
  290. ec_dev->proto_version = min(EC_HOST_REQUEST_VERSION,
  291. fls(info->protocol_versions) - 1);
  292. ec_dev->din_size = info->max_response_packet_size + EC_MAX_RESPONSE_OVERHEAD;
  293. ec_dev->dout_size = info->max_request_packet_size + EC_MAX_REQUEST_OVERHEAD;
  294. dev_dbg(ec_dev->dev, "using proto v%u\n", ec_dev->proto_version);
  295. break;
  296. case CROS_EC_DEV_PD_INDEX:
  297. ec_dev->max_passthru = info->max_request_packet_size -
  298. sizeof(struct ec_host_request);
  299. dev_dbg(ec_dev->dev, "found PD chip\n");
  300. break;
  301. default:
  302. dev_dbg(ec_dev->dev, "unknown passthru index: %d\n", devidx);
  303. break;
  304. }
  305. ret = 0;
  306. exit:
  307. kfree(msg);
  308. return ret;
  309. }
  310. static int cros_ec_get_proto_info_legacy(struct cros_ec_device *ec_dev)
  311. {
  312. struct cros_ec_command *msg;
  313. struct ec_params_hello *params;
  314. struct ec_response_hello *response;
  315. int ret, mapped;
  316. ec_dev->proto_version = 2;
  317. msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*response)), GFP_KERNEL);
  318. if (!msg)
  319. return -ENOMEM;
  320. msg->command = EC_CMD_HELLO;
  321. msg->insize = sizeof(*response);
  322. msg->outsize = sizeof(*params);
  323. params = (struct ec_params_hello *)msg->data;
  324. params->in_data = 0xa0b0c0d0;
  325. ret = cros_ec_send_command(ec_dev, msg);
  326. if (ret < 0) {
  327. dev_dbg(ec_dev->dev, "EC failed to respond to v2 hello: %d\n", ret);
  328. goto exit;
  329. }
  330. mapped = cros_ec_map_error(msg->result);
  331. if (mapped) {
  332. ret = mapped;
  333. dev_err(ec_dev->dev, "EC responded to v2 hello with error: %d\n", msg->result);
  334. goto exit;
  335. }
  336. if (ret == 0) {
  337. ret = -EPROTO;
  338. goto exit;
  339. }
  340. response = (struct ec_response_hello *)msg->data;
  341. if (response->out_data != 0xa1b2c3d4) {
  342. dev_err(ec_dev->dev,
  343. "EC responded to v2 hello with bad result: %u\n",
  344. response->out_data);
  345. ret = -EBADMSG;
  346. goto exit;
  347. }
  348. ec_dev->max_request = EC_PROTO2_MAX_PARAM_SIZE;
  349. ec_dev->max_response = EC_PROTO2_MAX_PARAM_SIZE;
  350. ec_dev->max_passthru = 0;
  351. ec_dev->pkt_xfer = NULL;
  352. ec_dev->din_size = EC_PROTO2_MSG_BYTES;
  353. ec_dev->dout_size = EC_PROTO2_MSG_BYTES;
  354. dev_dbg(ec_dev->dev, "falling back to proto v2\n");
  355. ret = 0;
  356. exit:
  357. kfree(msg);
  358. return ret;
  359. }
  360. /**
  361. * cros_ec_get_host_command_version_mask
  362. *
  363. * Get the version mask of a given command.
  364. *
  365. * @ec_dev: EC device to call
  366. * @cmd: command to get the version of.
  367. * @mask: result when function returns 0.
  368. *
  369. * @return 0 on success, error code otherwise
  370. *
  371. * LOCKING:
  372. * the caller has ec_dev->lock mutex or the caller knows there is
  373. * no other command in progress.
  374. */
  375. static int cros_ec_get_host_command_version_mask(struct cros_ec_device *ec_dev, u16 cmd, u32 *mask)
  376. {
  377. struct ec_params_get_cmd_versions *pver;
  378. struct ec_response_get_cmd_versions *rver;
  379. struct cros_ec_command *msg;
  380. int ret, mapped;
  381. msg = kmalloc(sizeof(*msg) + max(sizeof(*rver), sizeof(*pver)),
  382. GFP_KERNEL);
  383. if (!msg)
  384. return -ENOMEM;
  385. msg->version = 0;
  386. msg->command = EC_CMD_GET_CMD_VERSIONS;
  387. msg->insize = sizeof(*rver);
  388. msg->outsize = sizeof(*pver);
  389. pver = (struct ec_params_get_cmd_versions *)msg->data;
  390. pver->cmd = cmd;
  391. ret = cros_ec_send_command(ec_dev, msg);
  392. if (ret < 0)
  393. goto exit;
  394. mapped = cros_ec_map_error(msg->result);
  395. if (mapped) {
  396. ret = mapped;
  397. goto exit;
  398. }
  399. if (ret == 0) {
  400. ret = -EPROTO;
  401. goto exit;
  402. }
  403. rver = (struct ec_response_get_cmd_versions *)msg->data;
  404. *mask = rver->version_mask;
  405. ret = 0;
  406. exit:
  407. kfree(msg);
  408. return ret;
  409. }
  410. /**
  411. * cros_ec_query_all() - Query the protocol version supported by the
  412. * ChromeOS EC.
  413. * @ec_dev: Device to register.
  414. *
  415. * Return: 0 on success or negative error code.
  416. */
  417. int cros_ec_query_all(struct cros_ec_device *ec_dev)
  418. {
  419. struct device *dev = ec_dev->dev;
  420. u32 ver_mask;
  421. int ret;
  422. /* First try sending with proto v3. */
  423. if (!cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_EC_INDEX)) {
  424. /* Check for PD. */
  425. cros_ec_get_proto_info(ec_dev, CROS_EC_DEV_PD_INDEX);
  426. } else {
  427. /* Try querying with a v2 hello message. */
  428. ret = cros_ec_get_proto_info_legacy(ec_dev);
  429. if (ret) {
  430. /*
  431. * It's possible for a test to occur too early when
  432. * the EC isn't listening. If this happens, we'll
  433. * test later when the first command is run.
  434. */
  435. ec_dev->proto_version = EC_PROTO_VERSION_UNKNOWN;
  436. dev_dbg(ec_dev->dev, "EC query failed: %d\n", ret);
  437. return ret;
  438. }
  439. }
  440. devm_kfree(dev, ec_dev->din);
  441. devm_kfree(dev, ec_dev->dout);
  442. ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
  443. if (!ec_dev->din) {
  444. ret = -ENOMEM;
  445. goto exit;
  446. }
  447. ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
  448. if (!ec_dev->dout) {
  449. devm_kfree(dev, ec_dev->din);
  450. ret = -ENOMEM;
  451. goto exit;
  452. }
  453. /* Probe if MKBP event is supported */
  454. ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_GET_NEXT_EVENT, &ver_mask);
  455. if (ret < 0 || ver_mask == 0) {
  456. ec_dev->mkbp_event_supported = 0;
  457. } else {
  458. ec_dev->mkbp_event_supported = fls(ver_mask);
  459. dev_dbg(ec_dev->dev, "MKBP support version %u\n", ec_dev->mkbp_event_supported - 1);
  460. }
  461. /* Probe if host sleep v1 is supported for S0ix failure detection. */
  462. ret = cros_ec_get_host_command_version_mask(ec_dev, EC_CMD_HOST_SLEEP_EVENT, &ver_mask);
  463. ec_dev->host_sleep_v1 = (ret == 0 && (ver_mask & EC_VER_MASK(1)));
  464. /* Get host event wake mask. */
  465. ret = cros_ec_get_host_event_wake_mask(ec_dev, &ec_dev->host_event_wake_mask);
  466. if (ret < 0) {
  467. /*
  468. * If the EC doesn't support EC_CMD_HOST_EVENT_GET_WAKE_MASK,
  469. * use a reasonable default. Note that we ignore various
  470. * battery, AC status, and power-state events, because (a)
  471. * those can be quite common (e.g., when sitting at full
  472. * charge, on AC) and (b) these are not actionable wake events;
  473. * if anything, we'd like to continue suspending (to save
  474. * power), not wake up.
  475. */
  476. ec_dev->host_event_wake_mask = U32_MAX &
  477. ~(EC_HOST_EVENT_MASK(EC_HOST_EVENT_LID_CLOSED) |
  478. EC_HOST_EVENT_MASK(EC_HOST_EVENT_AC_DISCONNECTED) |
  479. EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_LOW) |
  480. EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_CRITICAL) |
  481. EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY) |
  482. EC_HOST_EVENT_MASK(EC_HOST_EVENT_PD_MCU) |
  483. EC_HOST_EVENT_MASK(EC_HOST_EVENT_BATTERY_STATUS));
  484. /*
  485. * Old ECs may not support this command. Complain about all
  486. * other errors.
  487. */
  488. if (ret != -EOPNOTSUPP)
  489. dev_err(ec_dev->dev,
  490. "failed to retrieve wake mask: %d\n", ret);
  491. }
  492. ret = 0;
  493. exit:
  494. return ret;
  495. }
  496. EXPORT_SYMBOL(cros_ec_query_all);
  497. /**
  498. * cros_ec_cmd_xfer() - Send a command to the ChromeOS EC.
  499. * @ec_dev: EC device.
  500. * @msg: Message to write.
  501. *
  502. * Call this to send a command to the ChromeOS EC. This should be used instead
  503. * of calling the EC's cmd_xfer() callback directly. This function does not
  504. * convert EC command execution error codes to Linux error codes. Most
  505. * in-kernel users will want to use cros_ec_cmd_xfer_status() instead since
  506. * that function implements the conversion.
  507. *
  508. * Return:
  509. * >0 - EC command was executed successfully. The return value is the number
  510. * of bytes returned by the EC (excluding the header).
  511. * =0 - EC communication was successful. EC command execution results are
  512. * reported in msg->result. The result will be EC_RES_SUCCESS if the
  513. * command was executed successfully or report an EC command execution
  514. * error.
  515. * <0 - EC communication error. Return value is the Linux error code.
  516. */
  517. int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev, struct cros_ec_command *msg)
  518. {
  519. int ret;
  520. mutex_lock(&ec_dev->lock);
  521. if (ec_dev->proto_version == EC_PROTO_VERSION_UNKNOWN) {
  522. ret = cros_ec_query_all(ec_dev);
  523. if (ret) {
  524. dev_err(ec_dev->dev,
  525. "EC version unknown and query failed; aborting command\n");
  526. mutex_unlock(&ec_dev->lock);
  527. return ret;
  528. }
  529. }
  530. if (msg->insize > ec_dev->max_response) {
  531. dev_dbg(ec_dev->dev, "clamping message receive buffer\n");
  532. msg->insize = ec_dev->max_response;
  533. }
  534. if (msg->command < EC_CMD_PASSTHRU_OFFSET(CROS_EC_DEV_PD_INDEX)) {
  535. if (msg->outsize > ec_dev->max_request) {
  536. dev_err(ec_dev->dev,
  537. "request of size %u is too big (max: %u)\n",
  538. msg->outsize,
  539. ec_dev->max_request);
  540. mutex_unlock(&ec_dev->lock);
  541. return -EMSGSIZE;
  542. }
  543. } else {
  544. if (msg->outsize > ec_dev->max_passthru) {
  545. dev_err(ec_dev->dev,
  546. "passthru rq of size %u is too big (max: %u)\n",
  547. msg->outsize,
  548. ec_dev->max_passthru);
  549. mutex_unlock(&ec_dev->lock);
  550. return -EMSGSIZE;
  551. }
  552. }
  553. ret = cros_ec_send_command(ec_dev, msg);
  554. mutex_unlock(&ec_dev->lock);
  555. return ret;
  556. }
  557. EXPORT_SYMBOL(cros_ec_cmd_xfer);
  558. /**
  559. * cros_ec_cmd_xfer_status() - Send a command to the ChromeOS EC.
  560. * @ec_dev: EC device.
  561. * @msg: Message to write.
  562. *
  563. * Call this to send a command to the ChromeOS EC. This should be used instead of calling the EC's
  564. * cmd_xfer() callback directly. It returns success status only if both the command was transmitted
  565. * successfully and the EC replied with success status.
  566. *
  567. * Return:
  568. * >=0 - The number of bytes transferred.
  569. * <0 - Linux error code
  570. */
  571. int cros_ec_cmd_xfer_status(struct cros_ec_device *ec_dev,
  572. struct cros_ec_command *msg)
  573. {
  574. int ret, mapped;
  575. ret = cros_ec_cmd_xfer(ec_dev, msg);
  576. if (ret < 0)
  577. return ret;
  578. mapped = cros_ec_map_error(msg->result);
  579. if (mapped) {
  580. dev_dbg(ec_dev->dev, "Command result (err: %d [%d])\n",
  581. msg->result, mapped);
  582. ret = mapped;
  583. }
  584. return ret;
  585. }
  586. EXPORT_SYMBOL(cros_ec_cmd_xfer_status);
  587. static int get_next_event_xfer(struct cros_ec_device *ec_dev,
  588. struct cros_ec_command *msg,
  589. struct ec_response_get_next_event_v3 *event,
  590. int version, uint32_t size)
  591. {
  592. int ret;
  593. msg->version = version;
  594. msg->command = EC_CMD_GET_NEXT_EVENT;
  595. msg->insize = size;
  596. msg->outsize = 0;
  597. ret = cros_ec_cmd_xfer_status(ec_dev, msg);
  598. if (ret > 0) {
  599. ec_dev->event_size = ret - 1;
  600. ec_dev->event_data = *event;
  601. }
  602. return ret;
  603. }
  604. static int get_next_event(struct cros_ec_device *ec_dev)
  605. {
  606. struct {
  607. struct cros_ec_command msg;
  608. struct ec_response_get_next_event_v3 event;
  609. } __packed buf;
  610. struct cros_ec_command *msg = &buf.msg;
  611. struct ec_response_get_next_event_v3 *event = &buf.event;
  612. int cmd_version = ec_dev->mkbp_event_supported - 1;
  613. u32 size;
  614. memset(msg, 0, sizeof(*msg));
  615. if (ec_dev->suspended) {
  616. dev_dbg(ec_dev->dev, "Device suspended.\n");
  617. return -EHOSTDOWN;
  618. }
  619. if (cmd_version == 0) {
  620. size = sizeof(struct ec_response_get_next_event);
  621. } else if (cmd_version < 3) {
  622. size = sizeof(struct ec_response_get_next_event_v1);
  623. } else {
  624. /*
  625. * The max version we support is v3. So, we speak v3 even if the
  626. * EC says it supports v4+.
  627. */
  628. cmd_version = 3;
  629. size = sizeof(struct ec_response_get_next_event_v3);
  630. }
  631. return get_next_event_xfer(ec_dev, msg, event, cmd_version, size);
  632. }
  633. static int get_keyboard_state_event(struct cros_ec_device *ec_dev)
  634. {
  635. u8 buffer[sizeof(struct cros_ec_command) +
  636. sizeof(ec_dev->event_data.data)];
  637. struct cros_ec_command *msg = (struct cros_ec_command *)&buffer;
  638. msg->version = 0;
  639. msg->command = EC_CMD_MKBP_STATE;
  640. msg->insize = sizeof(ec_dev->event_data.data);
  641. msg->outsize = 0;
  642. ec_dev->event_size = cros_ec_cmd_xfer_status(ec_dev, msg);
  643. ec_dev->event_data.event_type = EC_MKBP_EVENT_KEY_MATRIX;
  644. memcpy(&ec_dev->event_data.data, msg->data,
  645. sizeof(ec_dev->event_data.data));
  646. return ec_dev->event_size;
  647. }
  648. /**
  649. * cros_ec_get_next_event() - Fetch next event from the ChromeOS EC.
  650. * @ec_dev: Device to fetch event from.
  651. * @wake_event: Pointer to a bool set to true upon return if the event might be
  652. * treated as a wake event. Ignored if null.
  653. * @has_more_events: Pointer to bool set to true if more than one event is
  654. * pending.
  655. * Some EC will set this flag to indicate cros_ec_get_next_event()
  656. * can be called multiple times in a row.
  657. * It is an optimization to prevent issuing a EC command for
  658. * nothing or wait for another interrupt from the EC to process
  659. * the next message.
  660. * Ignored if null.
  661. *
  662. * Return: negative error code on errors; 0 for no data; or else number of
  663. * bytes received (i.e., an event was retrieved successfully). Event types are
  664. * written out to @ec_dev->event_data.event_type on success.
  665. */
  666. int cros_ec_get_next_event(struct cros_ec_device *ec_dev,
  667. bool *wake_event,
  668. bool *has_more_events)
  669. {
  670. u8 event_type;
  671. u32 host_event;
  672. int ret;
  673. u32 ver_mask;
  674. /*
  675. * Default value for wake_event.
  676. * Wake up on keyboard event, wake up for spurious interrupt or link
  677. * error to the EC.
  678. */
  679. if (wake_event)
  680. *wake_event = true;
  681. /*
  682. * Default value for has_more_events.
  683. * EC will raise another interrupt if AP does not process all events
  684. * anyway.
  685. */
  686. if (has_more_events)
  687. *has_more_events = false;
  688. if (!ec_dev->mkbp_event_supported)
  689. return get_keyboard_state_event(ec_dev);
  690. ret = get_next_event(ec_dev);
  691. /*
  692. * -ENOPROTOOPT is returned when EC returns EC_RES_INVALID_VERSION.
  693. * This can occur when EC based device (e.g. Fingerprint MCU) jumps to
  694. * the RO image which doesn't support newer version of the command. In
  695. * this case we will attempt to update maximum supported version of the
  696. * EC_CMD_GET_NEXT_EVENT.
  697. */
  698. if (ret == -ENOPROTOOPT) {
  699. dev_dbg(ec_dev->dev,
  700. "GET_NEXT_EVENT returned invalid version error.\n");
  701. mutex_lock(&ec_dev->lock);
  702. ret = cros_ec_get_host_command_version_mask(ec_dev,
  703. EC_CMD_GET_NEXT_EVENT,
  704. &ver_mask);
  705. mutex_unlock(&ec_dev->lock);
  706. if (ret < 0 || ver_mask == 0)
  707. /*
  708. * Do not change the MKBP supported version if we can't
  709. * obtain supported version correctly. Please note that
  710. * calling EC_CMD_GET_NEXT_EVENT returned
  711. * EC_RES_INVALID_VERSION which means that the command
  712. * is present.
  713. */
  714. return -ENOPROTOOPT;
  715. ec_dev->mkbp_event_supported = fls(ver_mask);
  716. dev_dbg(ec_dev->dev, "MKBP support version changed to %u\n",
  717. ec_dev->mkbp_event_supported - 1);
  718. /* Try to get next event with new MKBP support version set. */
  719. ret = get_next_event(ec_dev);
  720. }
  721. if (ret <= 0)
  722. return ret;
  723. if (has_more_events)
  724. *has_more_events = ec_dev->event_data.event_type &
  725. EC_MKBP_HAS_MORE_EVENTS;
  726. ec_dev->event_data.event_type &= EC_MKBP_EVENT_TYPE_MASK;
  727. if (wake_event) {
  728. event_type = ec_dev->event_data.event_type;
  729. host_event = cros_ec_get_host_event(ec_dev);
  730. /*
  731. * Sensor events need to be parsed by the sensor sub-device.
  732. * Defer them, and don't report the wakeup here.
  733. */
  734. if (event_type == EC_MKBP_EVENT_SENSOR_FIFO) {
  735. *wake_event = false;
  736. } else if (host_event) {
  737. /* rtc_update_irq() already handles wakeup events. */
  738. if (host_event & EC_HOST_EVENT_MASK(EC_HOST_EVENT_RTC))
  739. *wake_event = false;
  740. /* Masked host-events should not count as wake events. */
  741. if (!(host_event & ec_dev->host_event_wake_mask))
  742. *wake_event = false;
  743. }
  744. }
  745. return ret;
  746. }
  747. EXPORT_SYMBOL(cros_ec_get_next_event);
  748. /**
  749. * cros_ec_get_host_event() - Return a mask of event set by the ChromeOS EC.
  750. * @ec_dev: Device to fetch event from.
  751. *
  752. * When MKBP is supported, when the EC raises an interrupt, we collect the
  753. * events raised and call the functions in the ec notifier. This function
  754. * is a helper to know which events are raised.
  755. *
  756. * Return: 0 on error or non-zero bitmask of one or more EC_HOST_EVENT_*.
  757. */
  758. u32 cros_ec_get_host_event(struct cros_ec_device *ec_dev)
  759. {
  760. u32 host_event;
  761. if (!ec_dev->mkbp_event_supported)
  762. return 0;
  763. if (ec_dev->event_data.event_type != EC_MKBP_EVENT_HOST_EVENT)
  764. return 0;
  765. if (ec_dev->event_size != sizeof(host_event)) {
  766. dev_warn(ec_dev->dev, "Invalid host event size\n");
  767. return 0;
  768. }
  769. host_event = get_unaligned_le32(&ec_dev->event_data.data.host_event);
  770. return host_event;
  771. }
  772. EXPORT_SYMBOL(cros_ec_get_host_event);
  773. /**
  774. * cros_ec_check_features() - Test for the presence of EC features
  775. *
  776. * @ec: EC device, does not have to be connected directly to the AP,
  777. * can be daisy chained through another device.
  778. * @feature: One of ec_feature_code bit.
  779. *
  780. * Call this function to test whether the ChromeOS EC supports a feature.
  781. *
  782. * Return: true if supported, false if not (or if an error was encountered).
  783. */
  784. bool cros_ec_check_features(struct cros_ec_dev *ec, int feature)
  785. {
  786. struct ec_response_get_features *features = &ec->features;
  787. int ret;
  788. if (features->flags[0] == -1U && features->flags[1] == -1U) {
  789. /* features bitmap not read yet */
  790. ret = cros_ec_cmd(ec->ec_dev, 0, EC_CMD_GET_FEATURES + ec->cmd_offset,
  791. NULL, 0, features, sizeof(*features));
  792. if (ret < 0) {
  793. dev_warn(ec->dev, "cannot get EC features: %d\n", ret);
  794. memset(features, 0, sizeof(*features));
  795. }
  796. dev_dbg(ec->dev, "EC features %08x %08x\n",
  797. features->flags[0], features->flags[1]);
  798. }
  799. return !!(features->flags[feature / 32] & EC_FEATURE_MASK_0(feature));
  800. }
  801. EXPORT_SYMBOL_GPL(cros_ec_check_features);
  802. /**
  803. * cros_ec_get_sensor_count() - Return the number of MEMS sensors supported.
  804. *
  805. * @ec: EC device, does not have to be connected directly to the AP,
  806. * can be daisy chained through another device.
  807. * Return: < 0 in case of error.
  808. */
  809. int cros_ec_get_sensor_count(struct cros_ec_dev *ec)
  810. {
  811. /*
  812. * Issue a command to get the number of sensor reported.
  813. * If not supported, check for legacy mode.
  814. */
  815. int ret, sensor_count;
  816. struct ec_params_motion_sense *params;
  817. struct ec_response_motion_sense *resp;
  818. struct cros_ec_command *msg;
  819. struct cros_ec_device *ec_dev = ec->ec_dev;
  820. u8 status;
  821. msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
  822. GFP_KERNEL);
  823. if (!msg)
  824. return -ENOMEM;
  825. msg->version = 1;
  826. msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
  827. msg->outsize = sizeof(*params);
  828. msg->insize = sizeof(*resp);
  829. params = (struct ec_params_motion_sense *)msg->data;
  830. params->cmd = MOTIONSENSE_CMD_DUMP;
  831. ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
  832. if (ret < 0) {
  833. sensor_count = ret;
  834. } else {
  835. resp = (struct ec_response_motion_sense *)msg->data;
  836. sensor_count = resp->dump.sensor_count;
  837. }
  838. kfree(msg);
  839. /*
  840. * Check legacy mode: Let's find out if sensors are accessible
  841. * via LPC interface.
  842. */
  843. if (sensor_count < 0 && ec->cmd_offset == 0 && ec_dev->cmd_readmem) {
  844. ret = ec_dev->cmd_readmem(ec_dev, EC_MEMMAP_ACC_STATUS,
  845. 1, &status);
  846. if (ret >= 0 &&
  847. (status & EC_MEMMAP_ACC_STATUS_PRESENCE_BIT)) {
  848. /*
  849. * We have 2 sensors, one in the lid, one in the base.
  850. */
  851. sensor_count = 2;
  852. } else {
  853. /*
  854. * EC uses LPC interface and no sensors are presented.
  855. */
  856. sensor_count = 0;
  857. }
  858. }
  859. return sensor_count;
  860. }
  861. EXPORT_SYMBOL_GPL(cros_ec_get_sensor_count);
  862. /**
  863. * cros_ec_cmd - Send a command to the EC.
  864. *
  865. * @ec_dev: EC device
  866. * @version: EC command version
  867. * @command: EC command
  868. * @outdata: EC command output data
  869. * @outsize: Size of outdata
  870. * @indata: EC command input data
  871. * @insize: Size of indata
  872. *
  873. * Return: >= 0 on success, negative error number on failure.
  874. */
  875. int cros_ec_cmd(struct cros_ec_device *ec_dev,
  876. unsigned int version,
  877. int command,
  878. const void *outdata,
  879. size_t outsize,
  880. void *indata,
  881. size_t insize)
  882. {
  883. struct cros_ec_command *msg;
  884. int ret;
  885. msg = kzalloc(sizeof(*msg) + max(insize, outsize), GFP_KERNEL);
  886. if (!msg)
  887. return -ENOMEM;
  888. msg->version = version;
  889. msg->command = command;
  890. msg->outsize = outsize;
  891. msg->insize = insize;
  892. if (outsize)
  893. memcpy(msg->data, outdata, outsize);
  894. ret = cros_ec_cmd_xfer_status(ec_dev, msg);
  895. if (ret < 0)
  896. goto error;
  897. if (insize)
  898. memcpy(indata, msg->data, insize);
  899. error:
  900. kfree(msg);
  901. return ret;
  902. }
  903. EXPORT_SYMBOL_GPL(cros_ec_cmd);
  904. /**
  905. * cros_ec_cmd_readmem - Read from EC memory.
  906. *
  907. * @ec_dev: EC device
  908. * @offset: Is within EC_LPC_ADDR_MEMMAP region.
  909. * @size: Number of bytes to read.
  910. * @dest: EC command output data
  911. *
  912. * Return: >= 0 on success, negative error number on failure.
  913. */
  914. int cros_ec_cmd_readmem(struct cros_ec_device *ec_dev, u8 offset, u8 size, void *dest)
  915. {
  916. struct ec_params_read_memmap params = {};
  917. if (!size)
  918. return -EINVAL;
  919. if (ec_dev->cmd_readmem)
  920. return ec_dev->cmd_readmem(ec_dev, offset, size, dest);
  921. params.offset = offset;
  922. params.size = size;
  923. return cros_ec_cmd(ec_dev, 0, EC_CMD_READ_MEMMAP,
  924. &params, sizeof(params), dest, size);
  925. }
  926. EXPORT_SYMBOL_GPL(cros_ec_cmd_readmem);
  927. /**
  928. * cros_ec_get_cmd_versions - Get supported version mask.
  929. *
  930. * @ec_dev: EC device
  931. * @cmd: Command to test
  932. *
  933. * Return: version mask on success, negative error number on failure.
  934. */
  935. int cros_ec_get_cmd_versions(struct cros_ec_device *ec_dev, u16 cmd)
  936. {
  937. struct ec_params_get_cmd_versions req_v0;
  938. struct ec_params_get_cmd_versions_v1 req_v1;
  939. struct ec_response_get_cmd_versions resp;
  940. int ret;
  941. if (cmd <= U8_MAX) {
  942. req_v0.cmd = cmd;
  943. ret = cros_ec_cmd(ec_dev, 0, EC_CMD_GET_CMD_VERSIONS,
  944. &req_v0, sizeof(req_v0), &resp, sizeof(resp));
  945. } else {
  946. req_v1.cmd = cmd;
  947. ret = cros_ec_cmd(ec_dev, 1, EC_CMD_GET_CMD_VERSIONS,
  948. &req_v1, sizeof(req_v1), &resp, sizeof(resp));
  949. }
  950. if (ret == -EINVAL)
  951. return 0; /* Command not implemented */
  952. else if (ret < 0)
  953. return ret;
  954. else
  955. return resp.version_mask;
  956. }
  957. EXPORT_SYMBOL_GPL(cros_ec_get_cmd_versions);